view("http://localhost:8000/", "text/html"); //Opens the do{ //this is the MAIN loop if(($accept_socket_resource=socket_accept($socket_resource))<0) { echo "socket_accept() failed \r\n"; break; } if(FALSE===($buffer_text=socket_read($accept_socket_resource,2048))) { echo "socket_read() failed \r\n"; break; } //I commented out the next line, because throws segmentation fault //preg_match("/ .+ /i",$buffer_text,$matches); //and made a workaround $request_headers = split("\r\n", $buffer_text); $first_line=split(" ", $request_headers[0]); //We use only first $full_request = $first_line[1]; $request_and_arguments = explode("?",$full_request); $request = urldecode( $request_and_arguments[0] ); if(substr($request,strlen($request)-1,1)=="/"){ //find the first default file foreach($default_array as $default_file){ $tfile = $default_http_dir."/".$default_file; if( file_exists($tfile)){ $request = "/$default_file"; break; } } } $file_extension = get_file_extension($request); $path_to_file = get_path_to_file($request); //what mime type should be returned? switch($file_extension){ case "png": $mime="image/png"; break; case "gif": $mime="image/gif"; break; case "jpg": $mime="image/jpeg"; break; case "zip": $mime="aplication/zip"; break; case "exe": $mime="aplication/exe"; break; case "bmp": $mime="image/bmp"; break; case "mov": $mime="video/quicktime"; break; case "mp3": $mime="video/mpeg"; break; case "mpeg": $mime="video/mpeg"; break; case "txt": $mime="text/plain"; break; default: $mime="text/html; charset=iso-8859-1;"; break; } if(file_exists($path_to_file)){ //we have a file to return to the user $return_file = true; //is this a php file? if($file_extension == "php"){ //we had better parse the php //where are we now? $start_dir= getcwd(); //cd to the php files dir $php_file_dir = dirname( $path_to_file); $php_file = basename( $path_to_file ); chdir($php_file_dir); //run the PHP file $file_contents = get_include_contents("$php_file", $request_and_arguments[1]); $file_length=strlen($file_contents); //return to the start dir chdir($start_dir); }else{ echo "serving: $path_to_file \r\n"; $file_contents = file_get_contents($path_to_file); $file_length=filesize($path_to_file); } if($return_file){ $today=date("m.d.y"); $output="HTTP/1.0 200 OK\r\nServer: PHP Proof of Concept\r\nDate: $today\r\nConnection:close\r\nContent-Length:$file_length\r\nContent- Type:$mime\r\n\r\n"; socket_send($accept_socket_resource,$output,strlen($output),0); $start_chunk=0; while($start_chunk<=$file_length){ $text_chunk=substr($file_contents,$start_chunk,2048); socket_write($accept_socket_resource, $text_chunk,strlen($text_chunk)); $start_chunk+=2048; } } }else{ $output="HTTP/1.0 404 OBJECT NOT FOUND\r\nServer: PHP Server\r \nConnection: close\r\n\r\n"; socket_write($accept_socket_resource,$output,strlen($output)); } socket_close($accept_socket_resource); }while(true); socket_close($socket_resource); ?>