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