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 692def1

Browse files
author
iustin
committed
cleanup
1 parent 80d9af3 commit 692def1

File tree

1 file changed

+14
-129
lines changed

1 file changed

+14
-129
lines changed

‎src/router.class.php‎

Lines changed: 14 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ class PHP_Webserver_Router
2929
private $request_uri = "";
3030
private $physical_file = "";
3131
private $extension = "";
32-
private $extension_last = "";
33-
private $has_inline_extension = "";
3432
private $eTag = "";
3533
private $eTagHeader = "";
3634
private $last_modified = "";
@@ -60,18 +58,6 @@ function __construct()
6058
private function init()
6159
{
6260

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-
7561
set_error_handler(function ($error_type) {
7662

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

8369
}, E_ALL);
8470

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-
9471
$this->request_uri = \filter_input(\INPUT_SERVER, 'REQUEST_URI', \FILTER_SANITIZE_ENCODED);
9572
$this->request_uri = preg_replace('([/\\\]+)', '/', urldecode($this->request_uri));
9673

9774
$this->physical_file = preg_replace('([/\\\]+)', '/', $_SERVER['SCRIPT_FILENAME']);
98-
99-
if( ($file = $this->URI_validFolder()) !== FALSE){
100-
$this->physical_file = $file;
101-
}
102-
10375
$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();*/
11976

12077
$this->last_modified = time();
12178
$this->eTag = md5($this->last_modified);
@@ -240,20 +197,9 @@ function process_request()
240197

241198
$uri_path = $this->URI_no_query();
242199

243-
$fileIsNotPresent = !file_exists($_SERVER['DOCUMENT_ROOT'] . '/' . urldecode(substr($uri_path, 1)));
200+
if (!file_exists($_SERVER['DOCUMENT_ROOT'] . '/' . urldecode(substr($uri_path, 1)))) {
244201

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())) {
202+
$this->favicon();
257203

258204
header('HTTP/1.1 404 Not Found');
259205
$this->http_status = 404;
@@ -272,9 +218,6 @@ function process_request()
272218
header('Content-Type: ' . $mime_type);
273219
header('Content-Length: ' . $this->file_length);
274220

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

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

297240
}
298241

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-
345242
/**
346243
* Serve your application
347244
*/
@@ -416,8 +313,17 @@ function console_output()
416313
$load_index = $_SERVER['DOCUMENT_ROOT'] . "/" . $this->indexPath;
417314
$load_index = preg_replace('([/\\\]+)', '/', trim($load_index));
418315

316+
/**
317+
* Fix server globals
318+
*/
419319
$_SERVER['SCRIPT_NAME'] = DIRECTORY_SEPARATOR . $this->indexPath;
420-
320+
$_SERVER['PHP_SELF'] = DIRECTORY_SEPARATOR . $this->indexPath;
321+
$_SERVER['SCRIPT_FILENAME'] = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . $this->indexPath;
322+
/*
323+
echo '<pre>';
324+
print_r($_SERVER);
325+
die();
326+
*/
421327
if (!file_exists($load_index)) {
422328

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

428334
} else {
429335

430-
if (file_exists($uri_filepath) && !is_dir($uri_filepath)) {
336+
if (file_exists($uri_filepath) && !is_dir($uri_filepath)) {
431337

432338
$this->process_request();
433339

@@ -501,28 +407,7 @@ private function URI_Filename()
501407
private function URIhasPHP()
502408
{
503409

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;
410+
return strrev(strstr(strrev(strtolower($this->URI_Filename())), '.', TRUE)) == 'php' ? TRUE : FALSE;
526411

527412
}
528413

0 commit comments

Comments
(0)

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