init.php (3304B)
1 <?php 2 3 // Entry point for SGI-like httpd 4 5 $f = fopen('php://stdin', 'r'); 6 $line = str_replace("\r",'',str_replace("\r\n", "\n", fgets($f))); 7 $tokens = explode(' ', $line); 8 9 $_SERVER['REQUEST_METHOD'] = array_shift($tokens); 10 $_SERVER['REQUEST_URI'] = array_shift($tokens); 11 12 // Keep it simple 13 $statusCodes = array( 14 200 => 'OK', 15 400 => 'Bad Request', 16 404 => 'Not Found', 17 500 => 'Internal Server Error', 18 ); 19 $mimeTypes = array( 20 'css' => 'text/css', 21 'htm' => 'text/html', 22 'html' => 'text/html', 23 'js' => 'text/javascript', 24 ); 25 26 $status = 200; 27 $headers = array(); 28 ob_start(function( $buffer ) { 29 global $status; 30 global $statusCodes; 31 global $headers; 32 $extra = 'HTTP/1.0 '.$status.' '.$statusCodes[$status].PHP_EOL; 33 $extra .= 'Content-Length: '.strlen($buffer) . PHP_EOL; 34 foreach ($headers as $header) $extra .= $header . PHP_EOL; 35 $extra .= PHP_EOL; 36 return $extra . $buffer; 37 }); 38 39 // Make sure we support this 40 if(!in_array($_SERVER['REQUEST_METHOD'],array('GET'))) { 41 $status = 501; 42 die('The requested method has not (yet) been implemented'.PHP_EOL); 43 } 44 45 // Some security 46 if (strpos($_SERVER['REQUEST_URI'], '..')!==false) { 47 $status = 400; 48 die('You\'ve send something we don\'t understand or allow'.PHP_EOL); 49 } 50 51 // Read headers 52 while(($line=str_replace("\r",'',str_replace("\r\n", "\n", fgets($f))))!="\n") { 53 while(substr(rtrim($line), -1)=="\\") { 54 $line = rtrim($line); 55 $line = substr($line, 0, strlen($line)-1); 56 $line .= str_replace("\r",'',str_replace("\r\n", "\n", fgets($f))); 57 } 58 list($key, $value) = array_map('trim', explode(':', $line, 2)); 59 $_SERVER['HTTP_'.strtoupper(str_replace('-','_',$key))] = $value; 60 } 61 62 $params = explode('?', $_SERVER['REQUEST_URI'], 2); 63 $path = rtrim(__DIR__,'/').'/'.trim(array_shift($params),'/'); 64 $ext = @array_pop(explode('.',$path)); 65 66 function set_deep($path, &$dataHolder = array(), $value = null) { 67 $keys = explode('.', $path); 68 while (count($keys)) { 69 $dataHolder = &$dataHolder[array_shift($keys)]; 70 } 71 $dataHolder = $value; 72 } 73 74 if(count($params)) { 75 $params = array_shift($params); 76 $_GET = array(); 77 $variables = explode('&', $params); 78 foreach ($variables as $variable) { 79 $components = explode('=', $variable); 80 $key = str_replace(array( '[', ']' ), array( '.', '' ), urldecode(array_shift($components))); 81 $value = urldecode(array_shift($components)); 82 set_deep($key, $_GET, $value); 83 } 84 } 85 86 if(is_dir($path)) { 87 foreach( 88 array( 89 $path . '/index.php', 90 $path . '/index.html', 91 $path . '/index.htm', 92 ) as $file 93 ) { 94 if(is_file($file)) { 95 $path = $file; 96 break; 97 } 98 } 99 } 100 101 if(is_file($path)) { 102 switch($ext) { 103 case 'php': 104 include $path; 105 exit(0); 106 default: 107 if(isset($mimeTypes[$ext])) $headers[]='Content-Type: '.$mimeTypes[$ext]; 108 $headers[]='Expires:'.date('c',time()+300); 109 readfile($path); 110 exit(0); 111 } 112 } 113 if(is_file(rtrim(__DIR__,'/').'/app.php')) { 114 include $path; 115 exit(0); 116 } 117 118 $status = 404; 119 die('We could not find the page you\'re looking for.'.PHP_EOL);