Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 80d9af3

Browse files
author
iustin
committed
s
1 parent 5589c4f commit 80d9af3

File tree

1 file changed

+132
-6
lines changed

1 file changed

+132
-6
lines changed

‎src/router.class.php‎

Lines changed: 132 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class PHP_Webserver_Router
2929
private $request_uri = "";
3030
private $physical_file = "";
3131
private $extension = "";
32+
private $extension_last = "";
33+
private $has_inline_extension = "";
3234
private $eTag = "";
3335
private $eTagHeader = "";
3436
private $last_modified = "";
@@ -58,6 +60,18 @@ function __construct()
5860
private function init()
5961
{
6062

63+
//$_SERVER['SERVER_NAME'] = "localhost";
64+
65+
66+
//$_SERVER['PHP_SELF'] = $index_file_relative;
67+
68+
/*$_SERVER['REQUEST_URI'] = $_SERVER['REQUEST_URI'] .'/';
69+
echo '<pre>';
70+
print_r($_SERVER);
71+
72+
die();*/
73+
74+
6175
set_error_handler(function ($error_type) {
6276

6377
switch ($error_type) {
@@ -68,11 +82,40 @@ private function init()
6882

6983
}, E_ALL);
7084

85+
/**
86+
* Fix SCRIPT_FILENAME
87+
*/
88+
if (substr_count($_SERVER['SCRIPT_FILENAME'], $_SERVER['DOCUMENT_ROOT']) > 0) {
89+
$_SERVER['SCRIPT_FILENAME'] = trim(str_replace($_SERVER['DOCUMENT_ROOT'], '', $_SERVER['SCRIPT_FILENAME']), '/');
90+
}
91+
//echo $_SERVER['SCRIPT_FILENAME'].'<br />';
92+
//echo $_SERVER['DOCUMENT_ROOT'].'<br />';
93+
7194
$this->request_uri = \filter_input(\INPUT_SERVER, 'REQUEST_URI', \FILTER_SANITIZE_ENCODED);
7295
$this->request_uri = preg_replace('([/\\\]+)', '/', urldecode($this->request_uri));
7396

7497
$this->physical_file = preg_replace('([/\\\]+)', '/', $_SERVER['SCRIPT_FILENAME']);
98+
99+
if( ($file = $this->URI_validFolder()) !== FALSE){
100+
$this->physical_file = $file;
101+
}
102+
75103
$this->extension = strrev(strstr(strrev($this->physical_file), '.', TRUE));
104+
$this->extension_last = $this->URI_extension();
105+
$this->has_inline_extension = strrev(strstr(strrev($this->URI_Filename()), '.', TRUE)) !== FALSE ? TRUE : FALSE;
106+
107+
/*echo '<pre>';
108+
print_r($this->URI_validFolder());
109+
echo '<Br />';
110+
print_r($_SERVER);
111+
echo '</pre>';
112+
echo '<br />' . $this->physical_file . '<br />';
113+
echo $this->request_uri . '<br />';
114+
echo $this->extension . '<br />';
115+
echo $this->extension_last . '<br />';
116+
echo $this->URI_Filename() . '<br />';
117+
118+
die();*/
76119

77120
$this->last_modified = time();
78121
$this->eTag = md5($this->last_modified);
@@ -197,9 +240,20 @@ function process_request()
197240

198241
$uri_path = $this->URI_no_query();
199242

200-
if (!file_exists($_SERVER['DOCUMENT_ROOT'] . '/' . urldecode(substr($uri_path, 1)))) {
201-
202-
$this->favicon();
243+
$fileIsNotPresent = !file_exists($_SERVER['DOCUMENT_ROOT'] . '/' . urldecode(substr($uri_path, 1)));
244+
245+
/**
246+
* Check if file exists so we can serve it and
247+
* Double check the request path for falsies e.g.
248+
*
249+
* http://domain.com/this/is/a/valid/path.extension/but/this/is/the/real/request/file.css
250+
*
251+
* The path http://domain.com/this/is/a/valid/path.extension is the falsie it does not exist but it requests for
252+
* /but/this/is/the/real/request/file.css , therefore this must be the SCRIPT_FILENAME
253+
* and since we cannot know the logic behind that URL we follow the last path for a valid file so we can return
254+
* the correct Content-Type and Content-Length
255+
*/
256+
if ($fileIsNotPresent && !file_exists($this->URI_validFolder())) {
203257

204258
header('HTTP/1.1 404 Not Found');
205259
$this->http_status = 404;
@@ -218,6 +272,9 @@ function process_request()
218272
header('Content-Type: ' . $mime_type);
219273
header('Content-Length: ' . $this->file_length);
220274

275+
/**
276+
* Serve Cached files if available or else serve Raw Files
277+
*/
221278
if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $this->last_modified || $this->eTagHeader == $this->eTag) {
222279

223280
header('HTTP/1.1 304 Not Modified');
@@ -239,6 +296,52 @@ function process_request()
239296

240297
}
241298

299+
/**
300+
* Search and return valid folder from URI
301+
*/
302+
function URI_validFolder()
303+
{
304+
305+
$uri = $this->URI_no_query();
306+
$exp = explode('/', $uri);
307+
308+
$exp = array_filter($exp, function ($k) {
309+
return !in_array($k, array("", "", "..", "."));
310+
});
311+
312+
$tmp = array();
313+
314+
foreach ($exp as $k => $item) {
315+
316+
$tmp[$k] = array();
317+
318+
for ($i = $k; $i < count($exp); $i++) {
319+
$tmp[$k][] = $exp[$i];
320+
}
321+
322+
array_push($tmp[$k], $exp[count($exp)]);
323+
324+
$tmp[$k] = implode("/", $tmp[$k]);
325+
326+
}
327+
$tmp = array_filter($tmp, function ($k) {
328+
return strlen($k);
329+
});
330+
331+
foreach ($tmp as $item) {
332+
333+
if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/' . $item)) {
334+
335+
return $_SERVER['DOCUMENT_ROOT'] . '/' . $item;
336+
337+
}
338+
339+
}
340+
341+
return FALSE;
342+
343+
}
344+
242345
/**
243346
* Serve your application
244347
*/
@@ -313,6 +416,8 @@ function console_output()
313416
$load_index = $_SERVER['DOCUMENT_ROOT'] . "/" . $this->indexPath;
314417
$load_index = preg_replace('([/\\\]+)', '/', trim($load_index));
315418

419+
$_SERVER['SCRIPT_NAME'] = DIRECTORY_SEPARATOR . $this->indexPath;
420+
316421
if (!file_exists($load_index)) {
317422

318423
$not_found_message = "Your script file doesn't exist at " . $load_index;
@@ -322,7 +427,7 @@ function console_output()
322427

323428
} else {
324429

325-
if (file_exists($uri_filepath) && !is_dir($uri_filepath)) {
430+
if (file_exists($uri_filepath) && !is_dir($uri_filepath)) {
326431

327432
$this->process_request();
328433

@@ -396,7 +501,28 @@ private function URI_Filename()
396501
private function URIhasPHP()
397502
{
398503

399-
return strrev(strstr(strrev(strtolower($this->URI_Filename())), '.', TRUE)) == 'php' ? TRUE : FALSE;
504+
return $this->URI_extension() == 'php' ? TRUE : FALSE;
505+
506+
}
507+
508+
function URI_extension()
509+
{
510+
511+
$uri = $this->URI_no_query();
512+
513+
/**
514+
* Retrieve last segment
515+
*/
516+
if (strstr($uri, '/', TRUE) !== FALSE) {
517+
$uri_split = explode('/', $uri);
518+
$uri = $uri_split[count($uri_split) - 1];
519+
}
520+
521+
if (strstr($uri, '.', TRUE) !== FALSE) {
522+
return strrev(strstr(strrev(strtolower($uri)), '.', TRUE));
523+
}
524+
525+
return FALSE;
400526

401527
}
402528

@@ -505,4 +631,4 @@ private function create_mime_file()
505631

506632
}
507633

508-
}
634+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /