@@ -29,8 +29,6 @@ class PHP_Webserver_Router
29
29
private $ request_uri = "" ;
30
30
private $ physical_file = "" ;
31
31
private $ extension = "" ;
32
- private $ extension_last = "" ;
33
- private $ has_inline_extension = "" ;
34
32
private $ eTag = "" ;
35
33
private $ eTagHeader = "" ;
36
34
private $ last_modified = "" ;
@@ -60,18 +58,6 @@ function __construct()
60
58
private function init ()
61
59
{
62
60
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
-
75
61
set_error_handler (function ($ error_type ) {
76
62
77
63
switch ($ error_type ) {
@@ -82,40 +68,11 @@ private function init()
82
68
83
69
}, E_ALL );
84
70
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
-
94
71
$ this ->request_uri = \filter_input (\INPUT_SERVER , 'REQUEST_URI ' , \FILTER_SANITIZE_ENCODED );
95
72
$ this ->request_uri = preg_replace ('([/ \\\]+) ' , '/ ' , urldecode ($ this ->request_uri ));
96
73
97
74
$ this ->physical_file = preg_replace ('([/ \\\]+) ' , '/ ' , $ _SERVER ['SCRIPT_FILENAME ' ]);
98
-
99
- if ( ($ file = $ this ->URI_validFolder ()) !== FALSE ){
100
- $ this ->physical_file = $ file ;
101
- }
102
-
103
75
$ 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();*/
119
76
120
77
$ this ->last_modified = time ();
121
78
$ this ->eTag = md5 ($ this ->last_modified );
@@ -240,20 +197,9 @@ function process_request()
240
197
241
198
$ uri_path = $ this ->URI_no_query ();
242
199
243
- $ fileIsNotPresent = !file_exists ($ _SERVER ['DOCUMENT_ROOT ' ] . '/ ' . urldecode (substr ($ uri_path , 1 )));
200
+ if ( !file_exists ($ _SERVER ['DOCUMENT_ROOT ' ] . '/ ' . urldecode (substr ($ uri_path , 1 )))) {
244
201
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 ();
257
203
258
204
header ('HTTP/1.1 404 Not Found ' );
259
205
$ this ->http_status = 404 ;
@@ -272,9 +218,6 @@ function process_request()
272
218
header ('Content-Type: ' . $ mime_type );
273
219
header ('Content-Length: ' . $ this ->file_length );
274
220
275
- /**
276
- * Serve Cached files if available or else serve Raw Files
277
- */
278
221
if (@strtotime ($ _SERVER ['HTTP_IF_MODIFIED_SINCE ' ]) == $ this ->last_modified || $ this ->eTagHeader == $ this ->eTag ) {
279
222
280
223
header ('HTTP/1.1 304 Not Modified ' );
@@ -296,52 +239,6 @@ function process_request()
296
239
297
240
}
298
241
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
-
345
242
/**
346
243
* Serve your application
347
244
*/
@@ -416,8 +313,17 @@ function console_output()
416
313
$ load_index = $ _SERVER ['DOCUMENT_ROOT ' ] . "/ " . $ this ->indexPath ;
417
314
$ load_index = preg_replace ('([/ \\\]+) ' , '/ ' , trim ($ load_index ));
418
315
316
+ /**
317
+ * Fix server globals
318
+ */
419
319
$ _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
+ */
421
327
if (!file_exists ($ load_index )) {
422
328
423
329
$ not_found_message = "Your script file doesn't exist at " . $ load_index ;
@@ -427,7 +333,7 @@ function console_output()
427
333
428
334
} else {
429
335
430
- if (file_exists ($ uri_filepath ) && !is_dir ($ uri_filepath )) {
336
+ if (file_exists ($ uri_filepath ) && !is_dir ($ uri_filepath )) {
431
337
432
338
$ this ->process_request ();
433
339
@@ -501,28 +407,7 @@ private function URI_Filename()
501
407
private function URIhasPHP ()
502
408
{
503
409
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 ;
526
411
527
412
}
528
413
0 commit comments