added tips to lamer.bash help
[mirrors/Programs.git] / php / hhttpd / http_server.phps
1 #!usr/bin/php
2 <?php
3 //Harvie's HTTPd 0.7
4 /*
5 Allows you to binary safe download any file from remote pc
6 http://server:port/file
7 http://server:port//etc/passwd
8 http://server:port/C:\dir\file
9 http://server:port/C:/dir/file
10 Since version 0.6 there is filelisting
11 http://server:port/dir
12 http://server:port//etc
13 http://server:port/C:/dir
14 http://server:port/C:/dir/
15 You can also play multimedia like streams (using XMMS, Winamp, etc...)
16 But this can serve only one file a time
17 (if you are streaming or downloading, you can download/browse anything other,
18 but you can use download manager to download file by file...)
19 You can change port or interface by passing arguments
20 Usage: (httpd.php [port] [interface_IP])
21 This is very nice utility to use in your zombie.
22 */
23
24 //////////////////////////////////////////////////////////////////////////////////
25 $interface = "127.0.0.1";
26 //$interface = "192.168.2.130";
27 $port = 80;
28
29 //////////////////////////////////////////////////////////////////////////////////
30 $okheader = //Header 200 FILE
31 "HTTP/1.0 200 OK\n".
32 "Server: Harvie's HTTPd\n".
33 "Connection: close\n\n";
34
35 $dirheader = //Header 301 DIR
36 "HTTP/1.0 301 Moved Permanently\n".
37 "Server: Harvie's HTTPd\n".
38 "Location: %DIR%\n".
39 "Connection: close\n\n";
40
41 $badheader = //Header 404
42 "HTTP/1.0 404 File not found!\n".
43 "Server: Harvie's HTTPd\n".
44 "Connection: close\n\n";
45
46 $err404 = "ERR 404 - NOT FOUND!"; //Error 404
47
48 //////////////////////////////////////////////////////////////////////////////////
49 if(isset($argc)) {
50 if($argc > 1) $port = trim($argv[1]);
51 if($argc > 2) $interface = trim($argv[2]);
52 }
53
54 ///FUNCTIONS//////////////////////////////////////////////////////////////////////
55 function send_dir_listing($fp, $directory) {
56 $num = "0";
57 //Header
58 @fwrite($fp, "<html>\n<head><title>Index of $directory</title></head>\n<body><tt>\n");
59 @fwrite($fp, "<b>Available volumes:</b><br />\n");
60
61 //Special folders
62 @fwrite($fp, "[<a href=\"/\">/</a>]\n"); //Server root
63 if(is_dir("/")) { //Unix root
64 @fwrite($fp, "[<a href=\"//\">//</a>]\n");
65 }
66
67 //Available volumes
68 $dsks = "cdefghijklmnopqrstuvwxyz"; //Show this volumes (if available)
69 for($i=0;$i<strlen($dsks);$i++) {
70 if(is_dir($dsks[$i].":")) {
71 $vol = $dsks[$i];
72 @fwrite($fp, "[<a href=\"/$vol:/\">$vol:</a>]\n");
73 }
74 //echo($dsks[$i].":");
75 }
76 @fwrite($fp, "<br />\n\n");
77
78 //Directory listing
79 @fwrite($fp, "<b>Directory listing of $directory :</b><br /><br />\n\n");
80 @fwrite($fp, "[DIR] <a href=\"./../\">Parent Directory (../)</a><br />\n");
81 $files=opendir ($directory);
82 while (false!==($file = readdir($files)))
83 {
84 if ($file != "." && $file != "..")
85 {
86 $num++;
87 if(is_dir("$directory/$file")) {
88 fwrite($fp, "$num - [DIR] <a href=\"./$file\">$file</a><br />\n");
89 } else {
90 fwrite($fp, "$num - <a href=\"./$file\">$file</a><br />\n");
91 }
92 }
93 }
94
95 //Footer
96 fwrite($fp, "<br />\n<b>Total: $num</b>\n</tt></body>\n</html>");
97 }
98
99 ///CODE///////////////////////////////////////////////////////////////////////////
100 echo("\n\tStarting Harvie's HTTPd at:\n\ttcp://$interface:$port\n\n");
101 //system("title Harvie's HTTPd at tcp://$interface:$port"); //Microsoft Windows only
102 set_time_limit(0);
103 $sss = stream_socket_server("tcp://$interface:$port");
104
105 while(1) {
106 @$sfp = stream_socket_accept($sss);
107 if(!$sfp) continue;
108
109 $loop = 1;
110 while($sfp && !@feof($sfp) && $loop) {
111 $line = fgets($sfp);
112 //echo($line);
113 if(eregi("(GET|POST)", $line)) {
114 $line = trim($line);
115 //echo($line);
116 $line = explode(" ", $line);
117
118 $line[1] = trim(urldecode($line[1]));
119
120
121 if($line[1] == "/") {
122 $line[1] = "./";
123 } else {
124 $line[1] = substr($line[1], 1);
125 }
126
127
128 if(is_file($line[1])) { //200 OK FILE
129 fwrite($sfp, $okheader);
130 echo("200 ".$line[1]);
131 $fp = fopen($line[1], "rb");
132 while( fwrite($sfp, fgets($fp)) );
133 @fclose($sfp);
134 echo(" SENT!\n");
135 }
136
137 elseif(is_dir($line[1])) { //200 OK DIR
138 if(substr($line[1], strlen($line[1])-1) != "/") { //301 MOV DIR
139 $header = ("/".$line[1]."/");
140 echo("301 ".$line[1]." -> $header\n");
141 $header = str_replace("%DIR%", $header, $dirheader);
142 //$header = str_replace("./", "./", $dirheader);
143 //echo($header);
144 fwrite($sfp, $header);
145 @fclose($sfp);
146 break;
147 }
148 echo("200 [DIR] ".$line[1]."\n");
149 send_dir_listing($sfp, $line[1]);
150 }
151
152 else { //404 NOT FOUND
153 echo("404 ".$line[1]."\n");
154 fwrite($sfp, $badheader);
155 fwrite($sfp, $err404);
156 @fclose($sfp);
157 }
158
159 $loop = 0;
160 }
161 }
162 @fclose($sfp);
163 }
164
165 ?>
This page took 2.227527 seconds and 4 git commands to generate.