-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
154 lines (128 loc) · 3.07 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
require_once 'includes/config.php';
require_once 'includes/mcs-user.php';
require_once 'includes/template.php';
require_once 'includes/mcs-database.php';
User::setSubclass('MCSUser');
$db = new MCSDatabase();
MCSUser::setDatabaseConnection($db);
// Start PHP session (only if not already set)
if (session_id() == '') {
session_start();
}
require_once 'includes/login.php';
$redirections = array(
'./index.php' => './pages/home/index.php'
);
function processFilePath($filePath)
{
global $CONFIG;
global $db;
//Get extension
$extension = explode ('.', $filePath);
$extension = $extension[count($extension) - 1];
if ($extension == "php" || $extension == "html" || $extension == "htm")
{
$pageContents = '';
if ($extension == "php")
{
// Execute PHP
ob_start(); // capture anything sent to the output buffer
include $filePath;
$pageContents = ob_get_clean(); // end output buffering
}
else if ($extension == "html" or $extension == "htm")
{
// Print out the HTML contents
$pageContents = file_get_contents($filePath);
}
// Now that we have page contents, time to extract key HTML elements
extractContents($pageContents);
// Finally, execute the templating script
include 'templates/'.$CONFIG[CONFIG_TEMPLATE_KEY].'/index.php';
}
else
{
if ($extension == "css")
{
$contentType = "text/css;charset=UTF-8";
}
else
{
$contentType = mime_content_type($filePath);
}
//Serve the file
header('Content-Type: ' . $contentType);
//Check these
header("Expires: Mon, 1 Jan 2099 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
$size= filesize($filePath);
header("Content-Length: $size bytes");
readfile($filePath);
}
}
$url=strtok($_SERVER["REQUEST_URI"],'?');
$urlArray = explode ('/', $url);
//This is in case of a custom request that includes a "..", we don't want that
foreach($urlArray as $piece)
{
if($piece == "..")
{
http_response_code(404);
echo "404: Not found";
die();
}
}
//Get filePath
$filePath = urldecode('.' . $url);
if(file_exists($filePath))
{
//If it's a directory check if there is an index file
if(is_dir($filePath))
{
$defaultFiles = array(
'index.php',
'index.html',
'index.htm'
);
$foundDefaultFile = false;
foreach ($defaultFiles as $defaultFile)
{
if (file_exists($filePath . $defaultFile))
{
$filePath = $filePath . $defaultFile;
$foundDefaultFile = true;
break;
}
}
if (!$foundDefaultFile)
{
//Produce a 404
http_response_code(404);
echo "404: Not found";
die();
}
}
// Apply any redirections
if (isset($redirections[$filePath]))
{
$filePath = $redirections[$filePath];
if (!file_exists($filePath))
{
http_response_code(404);
echo "404: Not found";
die();
}
}
processFilePath($filePath);
}
else
{
//Produce a 404
http_response_code(404);
echo "404: Not found";
die();
}