-
Notifications
You must be signed in to change notification settings - Fork 1
/
syn_class.php
650 lines (570 loc) · 15.8 KB
/
syn_class.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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
<?php
/**
* @name SYN Directory Listing (Simple Yet Nifty)
* @desc Directory Listing Class
*
* With this file and the original index.php that came with the package you can make
* a nice simple listing of what is in that directory.
*
* You can also sort by name, filesize and filetype (both ascending and descending)
* by clicking on the Topmost icon, "Filename" or "Size"
*
* @author Olafur Waage, olafurw@gmail.com
* @credits, Icons made by Mark James ( http://www.famfamfam.com/lab/icons/silk/ )
*
* Distributed under the terms of the MIT License
*/
class synDirectory
{
/**
* Outputs the main directory table, this can be customized if you know php a little.
*
* This function needs the array that the function synDir makes if you are going to call it.
* Look at index.php for an example on how its done.
*
* @param array $array
* @return HTML table
*/
function __construct()
{
global $dSize, $fCount, $dCount, $lang, $config;
$array = $this->synDir();
/**
* Prints out the directory name, i.e. "Index of /downloads/sources/"
*/
if(isset($config["show_header"]) && $config["show_header"] == true)
{
echo "<b class='synHeader'>".$lang["index_of"]." ".$this->getDirName()."</b><br /><br />";
}
/**
* This section of code is the first two lines of the table, the header and the "Parent Directory" line.
*/
?>
<table class="synTable">
<tr class="synTopTr">
<td class="synTopTd"><a href="?sort=filetype&in=<?php if((isset($_GET["sort"]) && $_GET["sort"] == "filetype" && isset($_GET["in"]) && $_GET["in"] == "asc") || !$_GET){ echo "desc"; }else{ echo "asc"; } ?>"><b>T</b></a></td>
<td class="synTopTd"><a href="?sort=filename&in=<?php if((isset($_GET["sort"]) && $_GET["sort"] == "filename" && isset($_GET["in"]) && $_GET["in"] == "asc") || !$_GET){ echo "desc"; }else{ echo "asc"; } ?>"><b><?php echo $lang["filename"]; ?></b></a></td>
<?php
if(isset($config["show_last_modified"]) && $config["show_last_modified"] == true)
{
?>
<td class="synTopTd" align="right"><a href="?sort=modified&in=<?php if((isset($_GET["sort"]) && $_GET["sort"] == "modified" && isset($_GET["in"]) && $_GET["in"] == "asc") || !$_GET){ echo "desc"; }else{ echo "asc"; } ?>"><b><?php echo $lang["last_modified"]; ?></b></a></td>
<?php
}
if(isset($config["show_size"]) && $config["show_size"] == true)
{
?>
<td class="synTopTd" width="10"></td>
<td class="synTopTd" align="right"><a href="?sort=size&in=<?php if((isset($_GET["sort"]) && $_GET["sort"] == "size" && isset($_GET["in"]) && $_GET["in"] == "asc") || !$_GET){ echo "desc"; }else{ echo "asc"; } ?>"><b><?php echo $lang["size"]; ?></b></a></td>
<?php
}
?>
</tr>
<?php
if(isset($config["show_parent_dir"]) && $config["show_parent_dir"] == true)
{
?>
<tr class="synParentTr">
<td class="synParentTd">
<?php
if(isset($config["show_icons"]) && $config["show_icons"] == true)
{
?>
<img src="<?php echo $config["class_root"]; ?>folder_go.png" />
<?php
}
?>
</td>
<td class="synParentTd">
<a href=".."><?php echo $lang["parent_dir"]; ?></a>
</td>
<?php
if(isset($config["show_last_modified"]) && $config["show_last_modified"] == true)
{
?>
<td class="synParentTd" align="right">
-
</td>
<?php
}
if(isset($config["show_size"]) && $config["show_size"] == true)
{
?>
<td class="synParentTd" width="10"></td>
<td class="synParentTd" align="right">
-
</td>
<?php
}
?>
</tr>
<?php
}
$line = 0;
/**
* This section of code loops through all the directories that are found within this one.
*/
if(isset($array[0]) && is_array($array[0]) && isset($config["show_dir"]) && $config["show_dir"] == true)
{
foreach($array[0] as $dirData)
{
?>
<tr class="synFolderTr<?php echo $line % 2?>">
<td class="synFolderTd">
<?php
if(isset($config["show_icons"]) && $config["show_icons"] == true)
{
?>
<img src="<?php echo $config["class_root"]."folder.png"; ?>" />
<?php
}
?>
</td>
<td class="synFolderTd">
<a href="<?php echo $dirData; ?>"><?php echo $dirData; ?></a>
</td>
<?php
if(isset($config["show_last_modified"]) && $config["show_last_modified"] == true)
{
?>
<td class="synFolderTd" align="right">
<?php echo date("d-M-Y H:i.s", filemtime($dirData)); ?>
</td>
<?php
}
if(isset($config["show_size"]) && $config["show_size"] == true)
{
?>
<td class="synParentTd" width="10"></td>
<td class="synParentTd" align="right">
-
</td>
<?php
}
?>
</tr>
<?php
$line++;
if(isset($config["show_dir_statistics"]) && $config["show_dir_statistics"] == true)
{
$dCount = $dCount + 1;
}
}
}
if($line % 2 == 1)
{
$line = 1;
}
/**
* This section of code loops through all the files that are found within this directory.
*/
if(isset($array[1]) && is_array($array[1]) && isset($config["show_files"]) && $config["show_files"] == true )
{
foreach($array[1] as $fileKey => $fileData)
{
?>
<tr class="synFileTr<?php echo $line % 2?>">
<td class="synFileTd">
<?php
if(isset($config["show_icons"]) && $config["show_icons"] == true)
{
?>
<img src="<?php echo $this->iconImage($fileData); ?>" />
<?php
}
?>
</td>
<td class="synFileTd">
<a href="<?php echo $fileData; ?>"><?php echo $this->showFileExtension($fileData); ?></a>
</td>
<?php
if(isset($config["show_last_modified"]) && $config["show_last_modified"] == true)
{
?>
<td class="synFileTd" align="right">
<?php echo date("d-M-Y H:i.s", filemtime($fileData)); ?>
</td>
<?php
}
if(isset($config["show_size"]) && $config["show_size"] == true)
{
?>
<td class="synFileTd" width="10"></td>
<td class="synFileTd" align="right">
<?php echo $this->fileSizeCalc($fileData); ?>
</td>
<?php
}
?>
</tr>
<?php
$line++;
if(isset($config["show_dir_statistics"]) && $config["show_dir_statistics"] == true)
{
$dSize += filesize($fileData);
$fCount = $fCount + 1;
}
}
}
/**
* Misc statistics about the directory
*/
if(isset($config["show_dir_statistics"]) && $config["show_dir_statistics"] == true)
{
?>
<tr class="synFooterTr">
<td class="synFooterTd" colspan="2"><?php echo ($dCount + 0)." ".$lang["directories"].", ".($fCount + 0)." ".$lang["files"]; ?></td>
<?php
if(isset($config["show_size"]) && $config["show_size"] == true)
{
?>
<td class="synFooterTd" colspan="3" align="right"><?php echo ($fCount > 0 ? $this->byteConvert($dSize) : ""); ?></td>
<?php
}
?>
</tr>
<?php
}
echo "</table>";
/**
* Prints out misc server info, i.e. "Apache/1.3.33 Server at example.com Port 80"
*/
if(isset($config["show_server_info"]) && $config["show_server_info"] && $_SERVER['SERVER_SIGNATURE'] != "")
{
echo '<p class="serverInfo">'.$_SERVER['SERVER_SIGNATURE'].'</p>';
}
}
/* ################################################# */
/**
* Only edit the functions below if you are knowledgeable in php
*/
/* ################################################# */
/**
* Converts bytes into human readable form
*
* @param int $bytes
* @return parsed string
*/
function byteConvert($bytes)
{
if($bytes == 0)
{
return "0 B";
}
$s = array('B', 'Kb', 'MB', 'GB', 'TB', 'PB');
$e = floor(log($bytes) / log(1024));
return sprintf('%.2f '.$s[$e], ($bytes / pow(1024, floor($e))));
}
/**
* @desc File size calculator and parser
*
* @return string, Returns the filesize in a human readable form
*/
function fileSizeCalc($filename)
{
return $this->byteConvert(filesize($filename));
}
function getDirName()
{
$self = substr(strrchr($_SERVER['SCRIPT_NAME'],'/'), 1);
$strpos = strrpos($_SERVER['SCRIPT_NAME'], $self);
$cuttofmark = strlen($_SERVER['SCRIPT_NAME']) - $strpos;
$cuttofminus = '-'.$cuttofmark;
return substr($_SERVER['SCRIPT_NAME'], 0, intval($cuttofminus));
}
/**
* If the config file is set for this, it will
* display the filename without the extension. Else it will
* display the full name
*/
function showFileExtension($filename)
{
global $config;
/* modifies the $entry so only the filename appears */
if(isset($config["show_file_extensions"]) && $config["show_file_extensions"] == true)
{
return $filename;
}
else
{
return substr($filename, 0, strpos($filename, "."));
}
}
/**
* Returns if the entry is in the blacklist
*/
function isEntryInBlacklist($entry)
{
$entry = strtolower($entry);
if($entry == "." || $entry == ".." || $entry == "index.php" || $entry == "config.php" || $entry == "syn_class.php" ||
$entry == "syn_style.css" || $entry == "avi.png" || $entry == "control_repeat.png" || $entry == "doc.png" || $entry == "exe.png" ||
$entry == "folder.png" || $entry == "folder_go.png" || $entry == "html.png" || $entry == "jpg.png" ||
$entry == "mp3.png" || $entry == "page_white.png" || $entry == "pdf.png" || $entry == "php.png" || $entry == "ppt.png" ||
$entry == "xls.png" || $entry == "zip.png" || $entry == "syndirectory.txt" || $entry == "txt.png")
{
return true;
}
return false;
}
/**
* @desc Directory Array Generator
*
* @return array, Based on what is selected with $_GET or if $_GET is selected at all
* The array returned includes the files that are in the directory
*/
function synDir()
{
global $config;
$iterator = new DirectoryIterator(getcwd());
// Reads the directory and outputs into a simple array what we have in it.
// Excluding the items in the first if sentance.
$files = array();
$dirs = array();
$i = 0;
foreach($iterator as $entry)
{
$entryName = $entry->getFilename();
// If the file is not on the blacklist
if(!$this->isEntryInBlacklist($entryName))
{
if($entry->getType() == "dir")
{
if(isset($config["show_last_modified"]) && $config["show_last_modified"] == true && isset($_GET["sort"]) && $_GET["sort"] == "modified")
{
$dirs[$entry->getMTime().".0".$i] = $entryName;
}
else
{
$dirs[] .= $entryName;
}
}
else
{
// If the show only configure is not set, or if it's set and the file extension is in the show only array
if(!isset($config["show_only"]) ||
(isset($config["show_only"]) && is_array($config["show_only"]) && in_array(substr(strrchr($entry, "."), 1), $config["show_only"])))
{
if(isset($config["show_size"]) && $config["show_size"] == true && isset($_GET["sort"]) && $_GET["sort"] == "size")
{
$entryFilesizeIndex = $entry->getSize().".0".$i;
if(isset($files[$entryFilesizeIndex]))
{
$files[$entryFilesizeIndex] .= $entryName;
}
else
{
$files[$entryFilesizeIndex] = $entryName;
}
}
elseif(isset($_GET["sort"]) && $_GET["sort"] == "filetype")
{
$fileExtension = substr(strrchr($entryName, "."), 1);
$fileExtension = strtolower($fileExtension);
if(isset($files[$fileExtension.$entryName]))
{
$files[$fileExtension.$entryName] .= $entryName;
}
else
{
$files[$fileExtension.$entryName] = $entryName;
}
}
elseif(isset($config["show_last_modified"]) && $config["show_last_modified"] == true && isset($_GET["sort"]) && $_GET["sort"] == "modified")
{
$files[filemtime($entryName).".0".$i] = $entryName;
}
else
{
$files[] .= $entryName;
}
}
}
}
$i++;
}
if(isset($dirs) && !is_array($dirs) && isset($files) && !is_array($files))
{
return false;
}
// Returns an array with filenames and directory names and file sizes
// Sorted by filename
if(isset($_GET["sort"]) && $_GET["sort"] == "filename" || !isset($_GET["sort"]))
{
if((isset($_GET["in"]) && $_GET["in"] == "asc") || !isset($_GET["sort"]))
{
if(is_array($files))
{
asort($files, SORT_STRING);
}
if(isset($dirs) && is_array($dirs))
{
asort($dirs, SORT_STRING);
}
}
if(isset($_GET["in"]) && $_GET["in"] == "desc")
{
if(is_array($files))
{
arsort($files, SORT_STRING);
}
if(isset($dirs) && is_array($dirs))
{
arsort($dirs, SORT_STRING);
}
}
return array($dirs, $files);
}
// Returns an array with filenames and directory names and file sizes
// Sorted by filesize
if($config["show_size"] == true && $_GET["sort"] == "size")
{
if($_GET["sort"] == "size" && $_GET["in"] == "asc")
{
if(is_array($files))
{
ksort($files, SORT_NUMERIC);
}
if(is_array($dirs))
{
asort($dirs, SORT_STRING);
}
}
if($_GET["sort"] == "size" && $_GET["in"] == "desc")
{
if(is_array($files))
{
krsort($files, SORT_NUMERIC);
}
if(is_array($dirs))
{
asort($dirs, SORT_STRING);
}
}
return array($dirs, $files);
}
// Returns an array with filenames and directory names and file sizes
// Sorted by filetype
if($_GET["sort"] == "filetype")
{
if($_GET["sort"] == "filetype" && $_GET["in"] == "asc")
{
if(is_array($files))
{
ksort($files, SORT_STRING);
}
if(is_array($dirs))
{
asort($dirs, SORT_STRING);
}
}
if($_GET["sort"] == "filetype" && $_GET["in"] == "desc")
{
if(is_array($files))
{
krsort($files, SORT_STRING);
}
if(is_array($dirs))
{
asort($dirs, SORT_STRING);
}
}
return array($dirs, $files);
}
// Returns an array with filenames and directory names and file sizes
// Sorted by file last modified unixtime
if($config["show_last_modified"] == true && $_GET["sort"] == "modified")
{
if($_GET["sort"] == "modified" && $_GET["in"] == "asc")
{
if(is_array($files))
{
ksort($files, SORT_NUMERIC);
}
if(is_array($dirs))
{
ksort($dirs, SORT_NUMERIC);
}
}
if($_GET["sort"] == "modified" && $_GET["in"] == "desc")
{
if(is_array($files))
{
krsort($files, SORT_NUMERIC);
}
if(is_array($dirs))
{
krsort($dirs, SORT_NUMERIC);
}
}
return array($dirs, $files);
}
}
/**
* @desc Image File extension
* @param $filename, string. The filename to be checked out what image to use based on its extension
*
* @return string, Returns the image filename corresponding to the filename parameter
*/
function iconImage($filename)
{
global $config;
$fileExtension = substr($filename, -4, 4);
$fileExtension = strtolower($fileExtension);
switch($fileExtension)
{
case ".mp3":
case ".wav":
$fileImage = "mp3.png";
break;
case ".jpg":
case "jpeg":
case ".gif":
case ".png":
case ".bmp":
$fileImage = "jpg.png";
break;
case ".zip":
case ".rar":
$fileImage = "zip.png";
break;
case ".avi":
case ".mpg":
case "mpeg":
case ".wmv":
case "divx":
case ".mov":
case ".swf":
$fileImage = "avi.png";
break;
case ".exe":
case ".bat":
$fileImage = "exe.png";
break;
case ".pdf":
$fileImage = "pdf.png";
break;
case ".htm":
case "html":
$fileImage = "html.png";
break;
case ".txt":
$fileImage = "txt.png";
break;
case ".php":
$fileImage = "php.png";
break;
case ".doc":
$fileImage = "doc.png";
break;
case ".xls":
$fileImage = "xls.png";
break;
case ".ppt":
$fileImage = "ppt.png";
break;
default:
$fileImage = "page_white.png";
}
return $config["class_root"].$fileImage;
}
}