forked from alx/torrentflux
/
dir.php
executable file
·533 lines (456 loc) · 17.8 KB
/
dir.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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
<?php
/*************************************************************
* TorrentFlux - PHP Torrent Manager
* www.torrentflux.com
**************************************************************/
/*
This file is part of TorrentFlux.
TorrentFlux 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.
TorrentFlux 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 TorrentFlux; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
include_once("config.php");
include_once("functions.php");
checkUserPath();
// Setup some defaults if they are not set.
$del = getRequestVar('del');
$down = getRequestVar('down');
$tar = getRequestVar('tar');
$dir = stripslashes(urldecode(getRequestVar('dir')));
if (strpos(stripslashes($dir),"../")===false) {} else {echo "Can't go to parent directories!";exit;}
// Are we to delete something?
if ($del != "")
{
$current = "";
// The following lines of code were suggested by Jody Steele jmlsteele@stfu.ca
// this is so only the owner of the file(s) or admin can delete
if(IsAdmin($cfg["user"]) || preg_match("/^" . $cfg["user"] . "/",$del))
{
// Yes, then delete it
// we need to strip slashes twice in some circumstances
// Ex. If we are trying to delete test/tester's file/test.txt
// $del will be "test/tester\\\'s file/test.txt"
// one strip will give us "test/tester\'s file/test.txt
// the second strip will give us the correct
// "test/tester's file/test.txt"
$del = stripslashes(stripslashes($del));
if (!ereg("(\.\.\/)", $del))
{
avddelete($cfg["path"].$del);
$arTemp = explode("/", $del);
if (count($arTemp) > 1)
{
array_pop($arTemp);
$current = implode("/", $arTemp);
}
AuditAction($cfg["constants"]["fm_delete"], $del);
}
else
{
AuditAction($cfg["constants"]["error"], "ILLEGAL DELETE: ".$cfg['user']." tried to delete ".$del);
}
}
else
{
AuditAction($cfg["constants"]["error"], "ILLEGAL DELETE: ".$cfg['user']." tried to delete ".$del);
}
header("Location: dir.php?dir=".urlencode($current));
}
// Are we to download something?
if ($down != "" && $cfg["enable_file_download"])
{
$current = "";
// Yes, then download it
// we need to strip slashes twice in some circumstances
// Ex. If we are trying to download test/tester's file/test.txt
// $down will be "test/tester\\\'s file/test.txt"
// one strip will give us "test/tester\'s file/test.txt
// the second strip will give us the correct
// "test/tester's file/test.txt"
$down = stripslashes(stripslashes($down));
if (!ereg("(\.\.\/)", $down))
{
$path = $cfg["path"].$down;
$p = explode(".", $path);
$pc = count($p);
$f = explode("/", $path);
$file = array_pop($f);
$arTemp = explode("/", $down);
if (count($arTemp) > 1)
{
array_pop($arTemp);
$current = implode("/", $arTemp);
}
if (file_exists($path))
{
header("Content-type: application/octet-stream\n");
header("Content-disposition: attachment; filename=\"".$file."\"\n");
header("Content-transfer-encoding: binary\n");
header("Content-length: " . file_size($path) . "\n");
// write the session to close so you can continue to browse on the site.
session_write_close();
//$fp = fopen($path, "r");
$fp = popen("cat \"$path\"", "r");
fpassthru($fp);
pclose($fp);
AuditAction($cfg["constants"]["fm_download"], $down);
exit();
}
else
{
AuditAction($cfg["constants"]["error"], "File Not found for download: ".$cfg['user']." tried to download ".$down);
}
}
else
{
AuditAction($cfg["constants"]["error"], "ILLEGAL DOWNLOAD: ".$cfg['user']." tried to download ".$down);
}
header("Location: dir.php?dir=".urlencode($current));
}
// Are we to download something?
if ($tar != "" && $cfg["enable_file_download"])
{
$current = "";
// Yes, then tar and download it
// we need to strip slashes twice in some circumstances
// Ex. If we are trying to download test/tester's file/test.txt
// $down will be "test/tester\\\'s file/test.txt"
// one strip will give us "test/tester\'s file/test.txt
// the second strip will give us the correct
// "test/tester's file/test.txt"
$tar = stripslashes(stripslashes($tar));
if (!ereg("(\.\.\/)", $tar))
{
// This prevents the script from getting killed off when running lengthy tar jobs.
ini_set("max_execution_time", 3600);
$tar = $cfg["path"].$tar;
$arTemp = explode("/", $tar);
if (count($arTemp) > 1)
{
array_pop($arTemp);
$current = implode("/", $arTemp);
}
// Find out if we're really trying to access a file within the
// proper directory structure. Sadly, this way requires that $cfg["path"]
// is a REAL path, not a symlinked one. Also check if $cfg["path"] is part
// of the REAL path.
if (is_dir($tar))
{
$sendname = basename($tar);
switch ($cfg["package_type"])
{
Case "tar":
$command = "tar cf - \"".addslashes($sendname)."\"";
break;
Case "zip":
$command = "zip -0r - \"".addslashes($sendname)."\"";
break;
default:
$cfg["package_type"] = "tar";
$command = "tar cf - \"".addslashes($sendname)."\"";
break;
}
// HTTP/1.0
header("Pragma: no-cache");
header("Content-Description: File Transfer");
header("Content-Type: application/force-download");
header('Content-Disposition: attachment; filename="'.$sendname.'.'.$cfg["package_type"].'"');
// write the session to close so you can continue to browse on the site.
session_write_close();
// Make it a bit easier for tar/zip.
chdir(dirname($tar));
passthru($command);
AuditAction($cfg["constants"]["fm_download"], $sendname.".".$cfg["package_type"]);
exit();
}
else
{
AuditAction($cfg["constants"]["error"], "Illegal download: ".$cfg['user']." tried to download ".$tar);
}
}
else
{
AuditAction($cfg["constants"]["error"], "ILLEGAL TAR DOWNLOAD: ".$cfg['user']." tried to download ".$tar);
}
header("Location: dir.php?dir=".urlencode($current));
}
if ($dir == "")
{
unset($dir);
}
if (isset($dir))
{
if (ereg("(\.\.)", $dir))
{
unset($dir);
}
else
{
$dir = $dir."/";
}
}
DisplayHead(_DIRECTORYLIST);
?>
<script language="JavaScript">
function MakeTorrent(name_file)
{
window.open (name_file,'_blank','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=600,height=430')
}
function ConfirmDelete(file)
{
return confirm("<?php echo _ABOUTTODELETE ?>: " + file)
}
function checkCheck(thisIn)
{
var form = thisIn.form, i = 0;
for(i=0; i < form.length; i++)
{
if(form[i].type == 'checkbox' && form[i].name != 'checkall')
{
form[i].checked = thisIn.checked;
}
}
}
</script>
<?php
displayDriveSpaceBar(getDriveSpace($cfg["path"]));
echo "<br>";
if(!isset($dir)) $dir = "";
if (!file_exists($cfg["path"].$dir))
{
echo "<strong>".htmlentities($dir)."</strong> could not be found or is not valid.";
}
else
{
ListDirectory($cfg["path"].$dir);
}
DisplayFoot();
//**************************************************************************
// ListDirectory()
// This method reads files and directories in the specified path and
// displayes them.
function ListDirectory($dirName)
{
global $dir, $cfg;
$bgLight = $cfg["bgLight"];
$bgDark = $cfg["bgDark"];
$entrys = array();
$bg = $bgLight;
$dirName = stripslashes($dirName);
if (isset($dir))
{
//setup default parent directory URL
$parentURL = "dir.php";
//get the real parentURL
if (preg_match("/^(.+)\/.+$/",$dir,$matches) == 1)
{
$parentURL="dir.php?dir=" . urlencode($matches[1]);
}
echo "<table cellpadding=2 width=740>";
echo '<form action="multi.php" method="post" name="multidir">';
echo '<input type="hidden" name="action" value="fileDelete" />';
echo '<tr><td colspan="2">';
echo "<a href=\"" . $parentURL . "\"><img src=\"images/up_dir.gif\" width=16 height=16 title=\""._BACKTOPARRENT."\" border=0>["._BACKTOPARRENT."]</a>";
echo '</td>';
echo '<td align="right">Multi-Delete-></td>';
echo '<td align="right"><a href="javascript:document.multidir.submit()" onclick="return ConfirmDelete(\'Multiple Files\')">';
echo '<img src="images/delete_on.gif" title="Delete Multiple Files" border="0" height="16" width="16"></a>';
echo '<input type="checkbox" onclick="checkCheck(this);" /></td></tr>';
}
$handle = opendir($dirName);
while($entry = readdir($handle))
{
$entrys[] = $entry;
}
natsort($entrys);
foreach($entrys as $entry)
{
if ($entry != "." && $entry != ".." && substr($entry, 0, 1) != ".")
{
if (@is_dir($dirName.$entry))
{
echo "<tr bgcolor=\"".$bg."\"><td><a href=\"dir.php?dir=".urlencode($dir.$entry)."\"><img src=\"images/folder2.gif\" width=\"16\" height=\"16\" title=\"".$entry."\" border=\"0\" align=\"absmiddle\">".$entry."</a></td>";
echo "<td> </td>";
echo "<td> </td>";
echo "<td align=\"right\">";
if ($cfg["enable_maketorrent"])
{
echo "<a href=\"JavaScript:MakeTorrent('maketorrent.php?path=".urlencode($dir.$entry)."')\"><img src=\"images/make.gif\" width=16 height=16 title=\"Make Torrent\" border=0></a>";
}
if ($cfg["enable_file_download"])
{
echo "<a href=\"dir.php?tar=".urlencode($dir.$entry)."\"><img src=\"images/tar_down.gif\" width=16 height=16 title=\"Download as ".$cfg["package_type"]."\" border=0></a>";
}
// The following lines of code were suggested by Jody Steele jmlsteele@stfu.ca
// this is so only the owner of the file(s) or admin can delete
// only give admins and users who "own" this directory
// the ability to delete sub directories
if(IsAdmin($cfg["user"]) || preg_match("/^" . $cfg["user"] . "/",$dir))
{
echo "<a href=\"dir.php?del=".urlencode($dir.$entry)."\" onclick=\"return ConfirmDelete('".addslashes($entry)."')\"><img src=\"images/delete_on.gif\" width=16 height=16 title=\""._DELETE."\" border=0></a>";
echo "<input type=\"checkbox\" name=\"file[]\" value=\"".urlencode($dir.$entry)."\">";
}
else
{
echo " ";
}
echo "</td></tr>\n";
if ($bg == $bgLight)
{
$bg = $bgDark;
}
else
{
$bg = $bgLight;
}
}
else
{
// Do nothing
}
}
}
closedir($handle);
$entrys = array();
$handle = opendir($dirName);
while($entry = readdir($handle))
{
$entrys[] = $entry;
}
natsort($entrys);
foreach($entrys as $entry)
{
if ($entry != "." && $entry != "..")
{
if (@is_dir($dirName.$entry))
{
// Do nothing
}
else
{
$arStat = @lstat($dirName.$entry);
$arStat[7] = ( $arStat[7] == 0 )? file_size( $dirName . $entry ) : $arStat[7];
if (array_key_exists(10,$arStat))
{
$timeStamp = $arStat[10];
}
else
{
$timeStamp = "";
}
$fileSize = number_format(($arStat[7])/1024);
// Code added by Remko Jantzen to assign an icon per file-type. But when not
// available all stays the same.
$image="images/time.gif";
$imageOption="images/files/".getExtension($entry).".png";
if (file_exists("./".$imageOption))
{
$image = $imageOption;
}
echo "<tr bgcolor=\"".$bg."\">";
echo "<td>";
// Can users download files?
if ($cfg["enable_file_download"])
{
// Yes, let them download
echo "<a href=\"dir.php?down=".urlencode($dir.$entry)."\" >";
echo "<img src=\"".$image."\" width=\"16\" height=\"16\" alt=\"".$entry."\" border=\"0\"></a>";
echo "<a href=\"dir.php?down=".urlencode($dir.$entry)."\" >".$entry."</a>";
}
else
{
// No, just show the name
echo "<img src=\"".$image."\" width=\"16\" height=\"16\" alt=\"".$entry."\" border=\"0\">";
echo $entry;
}
echo "</td>";
echo "<td align=\"right\">".$fileSize." KB</td>";
echo "<td>".date("m-d-Y g:i a", $timeStamp)."</td>";
echo "<td align=\"right\">";
if( $cfg["enable_view_nfo"] && (( substr( strtolower($entry), -4 ) == ".nfo" ) || ( substr( strtolower($entry), -4 ) == ".txt" )) )
{
echo "<a href=\"viewnfo.php?path=".urlencode(addslashes($dir.$entry))."\"><img src=\"images/view_nfo.gif\" width=16 height=16 title=\"View '$entry'\" border=0></a>";
}
if ($cfg["enable_maketorrent"])
{
echo "<a href=\"JavaScript:MakeTorrent('maketorrent.php?path=".urlencode($dir.$entry)."')\"><img src=\"images/make.gif\" width=16 height=16 title=\"Make Torrent\" border=0></a>";
}
if ($cfg["enable_file_download"])
{
// Show the download button
echo "<a href=\"dir.php?down=".urlencode($dir.$entry)."\" >";
echo "<img src=\"images/download_owner.gif\" width=16 height=16 title=\"Download\" border=0>";
echo "</a>";
}
// The following lines of code were suggested by Jody Steele jmlsteele@stfu.ca
// this is so only the owner of the file(s) or admin can delete
// only give admins and users who "own" this directory
// the ability to delete files
if(IsAdmin($cfg["user"]) || preg_match("/^" . $cfg["user"] . "/",$dir))
{
echo "<a href=\"dir.php?del=".urlencode($dir.$entry)."\" onclick=\"return ConfirmDelete('".addslashes($entry)."')\"><img src=\"images/delete_on.gif\" width=16 height=16 title=\""._DELETE."\" border=0></a>";
echo "<input type=\"checkbox\" name=\"file[]\" value=\"".urlencode($dir.$entry)."\">";
}
else
{
echo " ";
}
echo "</td></tr>\n";
if ($bg == $bgLight)
{
$bg = $bgDark;
}
else
{
$bg = $bgLight;
}
}
}
}
closedir($handle);
echo "</table>";
}
// ***************************************************************************
// ***************************************************************************
// Checks for the location of the users directory
// If it does not exist, then it creates it.
function checkUserPath()
{
global $cfg;
// is there a user dir?
if (!is_dir($cfg["path"].$cfg["user"]))
{
//Then create it
mkdir($cfg["path"].$cfg["user"], 0777);
}
}
// This function returns the extension of a given file.
// Where the extension is the part after the last dot.
// When no dot is found the noExtensionFile string is
// returned. This should point to a 'unknown-type' image
// time by default. This string is also returned when the
// file starts with an dot.
function getExtension($fileName)
{
$noExtensionFile="unknown"; // The return when no extension is found
//Prepare the loop to find an extension
$length = -1*(strlen($fileName)); // The maximum negative value for $i
$i=-1; //The counter which counts back to $length
//Find the last dot in an string
while (substr($fileName,$i,1) != "." && $i > $length) {$i -= 1; }
//Get the extension (with dot)
$ext = substr($fileName,$i);
//Decide what to return.
if (substr($ext,0,1)==".") {$ext = substr($ext,((-1 * strlen($ext))+1)); } else {$ext = $noExtensionFile;}
//Return the extension
return strtolower($ext);
}
?>