@@ -29,6 +29,8 @@ 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 = "" ;
32
34
private $ eTag = "" ;
33
35
private $ eTagHeader = "" ;
34
36
private $ last_modified = "" ;
@@ -58,6 +60,18 @@ function __construct()
58
60
private function init ()
59
61
{
60
62
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
+
61
75
set_error_handler (function ($ error_type ) {
62
76
63
77
switch ($ error_type ) {
@@ -68,11 +82,40 @@ private function init()
68
82
69
83
}, E_ALL );
70
84
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
+
71
94
$ this ->request_uri = \filter_input (\INPUT_SERVER , 'REQUEST_URI ' , \FILTER_SANITIZE_ENCODED );
72
95
$ this ->request_uri = preg_replace ('([/ \\\]+) ' , '/ ' , urldecode ($ this ->request_uri ));
73
96
74
97
$ this ->physical_file = preg_replace ('([/ \\\]+) ' , '/ ' , $ _SERVER ['SCRIPT_FILENAME ' ]);
98
+
99
+ if ( ($ file = $ this ->URI_validFolder ()) !== FALSE ){
100
+ $ this ->physical_file = $ file ;
101
+ }
102
+
75
103
$ 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();*/
76
119
77
120
$ this ->last_modified = time ();
78
121
$ this ->eTag = md5 ($ this ->last_modified );
@@ -197,9 +240,20 @@ function process_request()
197
240
198
241
$ uri_path = $ this ->URI_no_query ();
199
242
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 ())) {
203
257
204
258
header ('HTTP/1.1 404 Not Found ' );
205
259
$ this ->http_status = 404 ;
@@ -218,6 +272,9 @@ function process_request()
218
272
header ('Content-Type: ' . $ mime_type );
219
273
header ('Content-Length: ' . $ this ->file_length );
220
274
275
+ /**
276
+ * Serve Cached files if available or else serve Raw Files
277
+ */
221
278
if (@strtotime ($ _SERVER ['HTTP_IF_MODIFIED_SINCE ' ]) == $ this ->last_modified || $ this ->eTagHeader == $ this ->eTag ) {
222
279
223
280
header ('HTTP/1.1 304 Not Modified ' );
@@ -239,6 +296,52 @@ function process_request()
239
296
240
297
}
241
298
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
+
242
345
/**
243
346
* Serve your application
244
347
*/
@@ -313,6 +416,8 @@ function console_output()
313
416
$ load_index = $ _SERVER ['DOCUMENT_ROOT ' ] . "/ " . $ this ->indexPath ;
314
417
$ load_index = preg_replace ('([/ \\\]+) ' , '/ ' , trim ($ load_index ));
315
418
419
+ $ _SERVER ['SCRIPT_NAME ' ] = DIRECTORY_SEPARATOR . $ this ->indexPath ;
420
+
316
421
if (!file_exists ($ load_index )) {
317
422
318
423
$ not_found_message = "Your script file doesn't exist at " . $ load_index ;
@@ -322,7 +427,7 @@ function console_output()
322
427
323
428
} else {
324
429
325
- if (file_exists ($ uri_filepath ) && !is_dir ($ uri_filepath )) {
430
+ if (file_exists ($ uri_filepath ) && !is_dir ($ uri_filepath )) {
326
431
327
432
$ this ->process_request ();
328
433
@@ -396,7 +501,28 @@ private function URI_Filename()
396
501
private function URIhasPHP ()
397
502
{
398
503
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 ;
400
526
401
527
}
402
528
@@ -505,4 +631,4 @@ private function create_mime_file()
505
631
506
632
}
507
633
508
- }
634
+ }
0 commit comments