-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile_helpers.php
484 lines (406 loc) · 16.1 KB
/
file_helpers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
<?php
require("configuration.php");
require("stat_keeper.php"); // File.php is not based on index.php so we require stat_keeper.php
/***************************************************************************
* Copyright (C) 2010 by Periklis Ntanasis , Ammar Qammaz *
*
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
/* THE MESSAGES CAN BE IMPROVED BUT SHOULD NOT TAKE MORE THAN 10 lines each in the php file*/
function refuse_withoutcompression()
{
echo "<!DOCTYPE html>
<html><body>
<h2>Please dont send uncompressed images :P</h2>
I refuse to waste good bandwidth to upload this file..
</body></html>";
}
function refuse_overquotta()
{ global $ENABLE_MIRROR_LINK;
echo "<!DOCTYPE html>
<html><body>
<h2>This server has exceeded its upload quotta</h2>";
if ($ENABLE_MIRROR_LINK==1) { echo "Please try <a href=\"mirrors.php\">one of the mirrors</a>.."; }
echo " </body></html>";
}
function link_not_found()
{
echo "<!DOCTYPE html>
<html><body>
<h2>Could not find file / File Expired ?</h2>
Your link is invalid , or maybe the file expired..
</body></html>";
}
function tampered_data()
{
error_reporting (0);
global $HOST_NAME;
echo "<!DOCTYPE html>
<html>
<body>
<h2>Tampered Request</h2>
<a href=\"#\" onclick=\"window.home();\">Get Lost</a> , this incident along with your IP has been logged..!
</body></html>";
$ip = "unknown IP..";
if ($_SERVER['HTTP_X_FORWARD_FOR'])
{ $ip = $_SERVER['HTTP_X_FORWARD_FOR']; } else
{ $ip = $_SERVER['REMOTE_ADDR']; }
error_log("MyLoader service @ ".$HOST_NAME." encountered tampering attempt with an asked file name by IP ".$ip." ..", 0);
}
function ExtentionIsGoogleDocsViewable($ext)
{
if ( ( $ext == "pdf" )||
( $ext == "xls" )|| ( $ext == "xlsx" )||
( $ext == "doc" )|| ( $ext == "docx" )||
( $ext == "ppt" )|| ( $ext == "pptx" )||
( $ext == "odt" )|| ( $ext == "ods" )||
( $ext == "pages" )||
( $ext == "ai" )||
( $ext == "psd" )||
( $ext == "tiff" )||
( $ext == "dxf" )||
( $ext == "eps" )|| ( $ext == "ps" )||
( $ext == "ttf" )|| ( $ext == "otf" )||
( $ext == "xps" )
) return 1;
return 0;
}
function ExtentionIsExecutable($ext)
{
if ( ( $ext == "exe" )||
( $ext == "scr" )||
( $ext == "bat" )
) return 1;
return 0;
}
function ExtentionIsImage($ext)
{
if ( ( $ext == "png" )||
( $ext == "gif" )||
( $ext == "svg" )||( $ext == "svgz" )||
( $ext == "bmp" )||( $ext == "ico" )||
//PPM , and pbm files are not opened by firefox :P ( $ext == "ppm" )||( $ext == "pbm" )||
( $ext == "jpg" )||
( $ext == "jpeg" )
) return 1;
return 0;
}
function ExtentionIsVideo($ext)
{
if ( ( $ext == "avi" )||
( $ext == "ogv" )||
( $ext == "webm" )||
( $ext == "mpeg4" )||( $ext == "mp4" )||
( $ext == "mpeg" )||( $ext == "mpg" )
) return 1;
return 0;
}
function ExtentionIsFlash($ext)
{
if ( ( $ext == "swf" )
) return 1;
return 0;
}
function ExtentionIsAudio($ext)
{
if ( ( $ext == "ogg" )|| ( $ext == "mp3" )||( $ext == "wav" )
) return 1;
return 0;
}
function PrintFileInBox($weblink,$filename,$ext)
{
global $HTML5_VIDEO , $HTML5_AUDIO , $FLASH_EMBEDS , $ENABLE_GOOGLEDOCS_LINK;
if ( ExtentionIsImage($ext)==1 )
{
echo "<a href=\"".$weblink."\">";
echo "<img src=\"".$weblink."\" width=\"70%\" alt=\"Uploaded Image\" >";
echo "</a><br><br>";
echo "<a href=\"".$weblink."\">";
echo $filename;
echo "</a><br><br>";
} else
if ( ( ExtentionIsVideo($ext)==1 ) && ($HTML5_VIDEO==1) )
{
echo "<video poster=\"images/video_logo.png\" autoplay=\"autoplay\" controls>";
if ( $ext == "ogv" ) { echo "<source src=\"".$weblink."\" type='video/ogg; codecs=\"theora, vorbis\"' />"; } else
if ( $ext == "webm") { echo "<source src=\"".$weblink."\" type='video/webm; codecs=\"vp8, vorbis\"' />"; } else
if ( $ext == "mp4" ) { echo "<source src=\"".$weblink."\" type='video/mp4; codecs=\"avc1.42E01E, mp4a.40.2\"' />"; }
echo "</video> <br><a href=\"".$weblink."\">Download the video file</a><br><br>";
/* video is a weird thing , due to the multitude of codecs , etc.. this is another instance of the code
echo"<video width=\"320\" src=\"".$weblink."\" controls autoplay>
<p> Try this page on HTML5 capable brosers</p>
</video>
*/
} else
if ( (ExtentionIsAudio($ext)==1) && ($HTML5_AUDIO==1) )
{
echo"<audio autoplay=\"autoplay\" controls>";
if ( $ext == "ogg" ) { echo "<source src=\"".$weblink."\" type=\"audio/ogg\" />"; } else
if ( $ext == "wav" ) { echo "<source src=\"".$weblink."\" type=\"audio/wav\" />"; } else
if ( $ext == "mp3" ) { echo "<source src=\"".$weblink."\" type=\"audio/mp3\" />"; }
echo " <p> Try this page on HTML5 capable brosers</p>
</audio>
<br><a href=\"".$weblink."\">Download the audio file</a><br><br>";
} else
if ( (ExtentionIsFlash($ext)==1) && ($FLASH_EMBEDS==1) )
{
echo"<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"
codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0\" width=\"640\" height=\"480\">
<param name=\"movie\" value=\"".$weblink."\">
<param name=\"quality\" value=\"high\">
<embed src=\"".$weblink."\" quality=\"high\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\"640\" height=\"480\"></embed>
</object>
<br><a href=\"".$weblink."\">Download the flash file</a><br><br>";
} else
{
echo "If you want you can download the file by clicking its link..";
echo "<br>This file has a .".$ext." extention <br><br>";
echo "<br>Only png/gif/jpg/jpeg , audio and video files are embedded in the pages for fast viewing :)<br><br>";
echo "<a href=\"".$weblink."\">";
echo "<img src=\"images/host_logo.png\">";
echo "</a><br><br>";
if ( ($ENABLE_GOOGLEDOCS_LINK)&&(ExtentionIsGoogleDocsViewable($ext)) )
{
echo "<a href=\"http://docs.google.com/viewer?url=".$weblink."\">";
echo "File can be also viewed using Google Docs<br><br>";
}
echo "<a href=\"".$weblink."\">";
echo $filename;
echo "</a><br><br>";
}
}
function ServeFile($dirty_filename)
{
$dirty_filename=trim($dirty_filename,"/"); // Accept requests for http://xxx/file.php?i=hash-file.ext/ ( the last slash )
//A triple = sign (===) is a comparison to see whether two variables / expresions / constants are equal AND have the same type
//- i.e. both are strings or both are integers.
$exploit_pos = strpos($dirty_filename,"/");
if ($exploit_pos === false) { } else { tampered_data(); return; }
$exploit_pos = strpos($dirty_filename,"\\");
if ($exploit_pos === false) { } else { tampered_data(); return; }
//FIX EXPLOITABLE HOLE THAT CAN SERVE THE WRONG FILE :P
$filename = filter_var(stripslashes($dirty_filename),FILTER_SANITIZE_STRIPPED);
str_ireplace("/",".",$filename);
$filename = strip_tags($filename,0);
//FIX EXPLOITABLE HOLE THAT CAN SERVE THE WRONG FILE :P
//DEBUG OUTPUT TO FIGURE OUT IF INPUT FILTERING WORKS OK echo $filename;
global $MAXIMUM_UPLOAD_BANDWIDTH_QUOTA,$MAXIMUM_UPLOAD_BANDWIDTH_QUOTA,$SCRIPT_LOCAL_BASE,$SCRIPT_CACHE_FOLDERNAME;
if ($filename!=$dirty_filename)
{
//The filename was really dirty , refuse service
tampered_data();
} else
if ( ($MAXIMUM_UPLOAD_BANDWIDTH_QUOTA!=0) && ( $MAXIMUM_UPLOAD_BANDWIDTH_QUOTA<get_uploaded_bandwidth() ) )
{
// Upload guard quota
refuse_overquotta();
} else
{
$fullPath = $SCRIPT_LOCAL_BASE.$SCRIPT_CACHE_FOLDERNAME."/".$filename;
if ($fd = fopen ($fullPath, "r"))
{
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext="bin";
if (!isset($path_parts["extension"])) { /* EXTENTION REMAINS BIN :P */ } else
{ $ext = strtolower($path_parts["extension"]); }
$filename_parts= explode("-",$path_parts["basename"],2);
$hash_part=$filename_parts[0];
if (!isset( $filename_parts[1]) ) { tampered_data(); return; }
$filename_part=$filename_parts[1];
switch ($ext)
{
// VIDEO
case "mp4":
header("Content-type: video/mp4"); // add here more headers for diff. extensions
break;
case "mpeg4":
header("Content-type: video/mp4"); // add here more headers for diff. extensions
break;
case "ogv":
header("Content-type: video/ogg"); // add here more headers for diff. extensions
break;
case "avi":
header("Content-type: video/avi"); // add here more headers for diff. extensions
break;
case "webm":
header("Content-type: video/webm"); // add here more headers for diff. extensions
break;
// AUDIO
case "mp3":
header("Content-type: audio/mp3"); // add here more headers for diff. extensions
break;
case "ogg":
header("Content-type: audio/ogg"); // add here more headers for diff. extensions
break;
case "wav":
header("Content-type: audio/wav"); // add here more headers for diff. extensions
break;
case "pdf":
header("Content-type: application/pdf"); // add here more headers for diff. extensions
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
break;
case "png":
header("Content-type: image/png"); // add here more headers for diff. extensions
break;
case "gif":
header("Content-type: image/gif"); // add here more headers for diff. extensions
break;
case "jpg":
header("Content-type: image/jpg"); // add here more headers for diff. extensions
break;
case "jpeg":
header("Content-type: image/jpg"); // add here more headers for diff. extensions
break;
case "svg":
header("Content-type: image/svg+xml"); // add here more headers for diff. extensions
break;
case "svgz":
header("Content-type: image/svg-xml"); // add here more headers for diff. extensions
break;
case "pbm":
header("Content-type: image/x-portable-bitmap"); // add here more headers for diff. extensions
break;
case "ppm":
header("Content-type: image/x-portable-pixmap"); // add here more headers for diff. extensions
break;
case "ico":
header("Content-type: image/x-ico"); // add here more headers for diff. extensions
break;
case "bmp":
refuse_withoutcompression();
exit;
break;
default;
header("Content-type: application/octet-stream");
}
header("Content-Disposition: filename=\"".$filename_part."\"");
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly
while(!feof($fd))
{
$buffer = fread($fd, 2048);
echo $buffer;
}
fclose ($fd);
add_to_uploaded_bandwidth($fsize);
} else
{
link_not_found();/* DEBUG ONLY :P */
}
}
exit;
}
//This function returns the size of a remote file
//it was found here: http://codesnippets.joyent.com/posts/show/1214
function get_remote_file_size($url, $readable = true){
$parsed = parse_url($url);
$host = $parsed["host"];
$fp = @fsockopen($host, 80, $errno, $errstr, 20);
if(!$fp) return false;
else {
@fputs($fp, "HEAD $url HTTP/1.1\r\n");
@fputs($fp, "HOST: $host\r\n");
@fputs($fp, "Connection: close\r\n\r\n");
$headers = "";
while(!@feof($fp))$headers .= @fgets ($fp, 128);
}
@fclose ($fp);
$return = false;
$arr_headers = explode("\n", $headers);
foreach($arr_headers as $header) {
// follow redirect
$s = 'Location: ';
if(substr(strtolower ($header), 0, strlen($s)) == strtolower($s)) {
$url = trim(substr($header, strlen($s)));
return get_remote_file_size($url, $readable);
}
// parse for content length
$s = "Content-Length: ";
if(substr(strtolower ($header), 0, strlen($s)) == strtolower($s)) {
$return = trim(substr($header, strlen($s)));
break;
}
}
//this part is modified to return size in bytes
if($return && $readable) {
$size = $return;
$return = $size;
}
return $return;
}
function DownloadFile($url)
{
global $ENABLE_URL_UPLOAD,$SCRIPT_LOCAL_BASE,$SCRIPT_CACHE_FOLDERNAME,$SCRIPT_WEB_BASE,$LOCAL_PHP_FILE_LIMIT;
if ( ($ENABLE_URL_UPLOAD == 1 ) )
{
/*
TODO ADD REGULAR EXPRESSION TO CLEAN URL!
*/
if(!strstr($url, "http://"))
{
$url = "http://".$url;
}
if(get_remote_file_size($url)>$LOCAL_PHP_FILE_LIMIT)
{
echo "</br>Files > ".($LOCAL_PHP_FILE_LIMIT/(1024*1024))."MB are not permitted</br>";
}
else
{
$destination_folder = $SCRIPT_CACHE_FOLDERNAME."/";
$newfname = basename($url);
$new_filename=md5($newfname.date('l jS \of F Y h:i:s A'))."-".$newfname;
$file = fopen ($url, "rb");
if ($file) {
$newf = fopen ($destination_folder.$new_filename, "wb");
if ($newf)
while(!feof($file)) {
fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
}
}
if ($file) {
fclose($file);
}
if ($newf) {
fclose($newf);
}
if(file_exists($SCRIPT_LOCAL_BASE.$destination_folder.$new_filename))
{
$direct_target_path = "file.php?i=".$new_filename;
$new_target_path = "vfile.php?i=".$new_filename;
echo "<br/>";
echo "You can access the file <a href='$new_target_path' target=\"_new\">here</a><br/><br/>";
echo "<table>";
echo "<tr>
<td>Link : </td>
<td><input type=\"text\" value=\"".$SCRIPT_WEB_BASE.$new_target_path."\"></td>
</tr>";
echo "<tr>
<td>Direct Link : </td>
<td><input type=\"text\" value=\"".$SCRIPT_WEB_BASE.$direct_target_path."\"></td>
</tr>
</table></br></br>";
add_to_cache_size(get_remote_file_size($url));
}
else
{
echo "</br><h2>Sorry, there was an error during the upload!</br>Please check if the url is valid and try again!</h2></br>";
}
}
}
}
?>