functions.php
1 <?php
2
/*
3 +----------------------------------------------------------------+
4 |                                                                                            |
5 |    GaMerZ File Explorer Version 1.20                                            |
6 |    Copyright (c) 2004-2008 Lester "GaMerZ" Chan                            |
7 |                                                                                            |
8 |    File Written By:                                                                    |
9 |    - Lester "GaMerZ" Chan                                                            |
10 |    - http://lesterchan.net                                                            |
11 |                                                                                            |
12 |    File Information:                                                                    |
13 |    - Functions Needed                                                                |
14 |    - function.php                                                                        |
15 |                                                                                            |
16 +----------------------------------------------------------------+
17 */
18
19
20 ### Function: Start Timer
21
function StartTimer() {
22     global 
$timestart;
23     
$mtime microtime();
24     
$mtime explode(" ",$mtime);
25     
$mtime $mtime[1] + $mtime[0];
26     
$timestart $mtime;
27     return 
true;
28 }
29
30
### Function: Stop Timer
31
function StopTimer($precision=5) {
32     global 
$timestart;
33     
$mtime microtime();
34     
$mtime explode(" ",$mtime);
35     
$mtime $mtime[1] + $mtime[0];
36     
$timeend $mtime;
37     
$timetotal $timeend-$timestart;
38     
$scripttime number_format($timetotal,$precision);
39     return 
$scripttime;
40 }
41
42
### Function: Format Size
43
function format_size($rawSize) {
44     if(
$rawSize 1073741824 1) {
45         return 
round($rawSize/10737418241).'GB';
46     } elseif (
$rawSize 1048576 1) {
47         return 
round($rawSize/10485761).'MB';
48     } elseif (
$rawSize 1024 1) {
49         return 
round($rawSize/10241).'KB';
50     } else {
51         return 
round($rawSize1).'b';
52     }
53 }
54
55
56
### Function: List All Directory
57
function list_directories($path) {
58     global 
$root_directory$gmz_directories$ignore_folders;
59     if (
$handle = @opendir($path)) {     
60         while (
false !== ($filename readdir($handle))) {  
61             if (
$filename != '.' && $filename != '..') {
62                 
$file_path substr($path.'/'.$filenamestrlen($root_directory)+1strlen($path.'/'.$filename));
63                 
$file_folder substr($file_path0, -(strlen($filename)+1));
64                 if(
is_dir($path.'/'.$filename)) {
65                     if(!
in_array($file_path$ignore_folders)) {
66                         
$gmz_directories[] = $file_path;
67                     }
68                     
list_directories($path.'/'.$filename);
69                 }
70             } 
71         } 
72         
closedir($handle);  
73     }  else {
74         
display_error('Invalid Directory');
75     }
76
77
78
### Function: List All Files
79
function list_files($path) {
80     global 
$root_directory$gmz_files$gmz_directories$extensions$ignore_files$ignore_ext$ignore_folders;
81     if (
$handle = @opendir($path)) {     
82         while (
false !== ($filename readdir($handle))) {  
83             if (
$filename != '.' && $filename != '..') {
84                 
$file_path substr($path.'/'.$filenamestrlen($root_directory)+1strlen($path.'/'.$filename));
85                 
$file_folder substr($file_path0, -(strlen($filename)+1));
86                 if(
is_dir($path.'/'.$filename)) {
87                     if(!
in_array($file_path$ignore_folders)) {
88                         
$gmz_directories[] = $file_path;
89                     }
90                     
list_files($path.'/'.$filename);
91                 } else {
92                     if (
is_file($path.'/'.$filename)) {
93                         
$file_ext explode('.'$filename);
94                         
$file_ext $file_ext[sizeof($file_ext)-1];
95                         
$file_ext strtolower($file_ext);
96                         if(!
in_array($file_ext$ignore_ext) && !in_array($file_path$ignore_files) && !in_array($file_folder$ignore_folders)) {
97                             
$gmz_files[] = array('name' => $filename'ext' => $file_ext'path' => $file_path'type' => $extensions[$file_ext][0], 'size' => sprintf("%u"filesize($path.'/'.$filename)), 'date' => filemtime($path.'/'.$filename));
98                         }
99                     } 
100                 }
101             } 
102         } 
103         
closedir($handle);  
104     }  else {
105         
display_error('Invalid Directory');
106     }
107
108
109
### Function: List Directory Files
110
function list_dir($path) {
111     global 
$gmz_files$gmz_directories$extensions$ignore_files$ignore_ext$ignore_folders$directories_before_current_path$current_directory_path;
112     if (
$handle = @opendir($path)) {     
113         while (
false !== ($filename readdir($handle))) {  
114             if (
$filename != '.' && $filename != '..') {
115                 if (
is_file($path.'/'.$filename) && !in_array($directories_before_current_path.$current_directory_path.$filename$ignore_files)) {
116                     
$file_ext explode('.'$filename);
117                     
$file_ext $file_ext[sizeof($file_ext)-1];
118                     
$file_ext strtolower($file_ext);
119                     if(!
in_array($file_ext$ignore_ext)) {
120                         
$gmz_files[] = array('name' => $filename'ext' => $file_ext'type' => $extensions[$file_ext][0], 'size' => sprintf("%u"filesize($path.'/'.$filename)), 'date' => filemtime($path.'/'.$filename));
121                     }
122                 } 
123                 if (
is_dir($path.'/'.$filename) && !in_array($directories_before_current_path.$current_directory_path.$filename$ignore_folders)) {
124                     
$gmz_directories[] = array('name' => $filename'size' => dir_size($path.'/'.$filename), 'date' => filemtime($path.'/'.$filename));
125                 } 
126             } 
127         } 
128         
closedir($handle);  
129     }  else {
130         
display_error('Invalid Directory');
131     }
132
133
134
### Function: Find Directory Size
135
function dir_size($dir) { 
136     
$totalsize 0
137     if (
$dirstream = @opendir($dir))  { 
138         while (
false !== ($filename readdir($dirstream))) { 
139             if (
$filename != '.' && $filename != '..') { 
140                 if (
is_file($dir.'/'.$filename)) { 
141                     
$totalsize += sprintf("%u"filesize($dir.'/'.$filename)); 
142                 } 
143                 if (
is_dir($dir.'/'.$filename)) { 
144                     
$totalsize += dir_size($dir.'/'.$filename); 
145                 } 
146             } 
147         } 
148         
closedir($dirstream); 
149     } 
150     return 
$totalsize
151 }
152
153
### Function: Sort Array By Alphabets
154
function array_alphabetsort() {
155    
$arguments func_get_args();
156    
$arrays    $arguments[0];  
157    for (
$c = (count($arguments)-1); $c 0$c--) {
158        if (
in_array($arguments[$c], array(SORT_ASC SORT_DESC))) {
159            continue;
160        }
161        
$compare create_function('$a,$b','return strcasecmp($a["'.$arguments[$c].'"], $b["'.$arguments[$c].'"]);');
162        
usort($arrays$compare);
163        if (
$arguments[$c+1] == SORT_DESC) {
164            
$arrays array_reverse($arrays);
165        }
166    }
167    return 
$arrays;
168 }
169
170
### Function: Sort Array By Numbers
171
function array_numbersort($a$b) {
172     global 
$sort_by;
173     if (
$a[$sort_by] == $b[$sort_by]) {
174         return 
0;
175     }
176     return (
$a[$sort_by] < $b[$sort_by]) ? -1;
177 }
178
179
### Function: Check Key In Multiple Arrays
180
function in_multi_array($needle$haystack) {
181     
$in_multi_array false;
182     if(
in_array($needle$haystack)) {
183         
$in_multi_array true;
184     } else {
185         foreach (
$haystack as $key => $val) {
186             if(
is_array($val)) {
187                 if(
in_multi_array($needle$val)) {
188                     
$in_multi_array true;
189                     break;
190                 }
191             }
192         }
193     }
194     return 
$in_multi_array;
195 }
196
197
### Function: Form Sorting URL
198
function url($url$mode) {
199     global 
$gfe_url$nice_url$root_filename$sort_by$sort_order;
200     
$temp_url '';
201     
$temp_url_nice '';
202     
$GET_sortby trim($_GET['by']);
203     
$GET_sortorder trim($_GET['order']);
204     
$url urldecode($url);
205     
$url urlencode($url);
206     
$url str_replace('%2F''/'$url);
207     switch(
$mode) {
208         case 
'dir':
209             if(
$url == 'home') {
210                 
$temp_url $gfe_url.'/'.$root_filename;
211                 
$temp_url_nice $gfe_url.'/';
212             } else {
213                 
$temp_url "$gfe_url/$root_filename?dir=$url";
214                 
$temp_url_nice "$gfe_url/browse/$url/";
215             }
216             if(!empty(
$GET_sortby)) {
217                 if(
strpos($temp_url'?') === false) {
218                     
$temp_url .= "?by=$sort_by&amp;order=$GET_sortorder";
219                 } else {
220                     
$temp_url .= "&amp;by=$sort_by&amp;order=$GET_sortorder";
221                 }
222                 
$temp_url_nice .= "sortby/$sort_by/sortorder/$GET_sortorder/";
223             }
224             break;
225         case 
'file':
226             
$temp_url "$gfe_url/view.php?file=$url";
227             
$temp_url_nice "$gfe_url/viewing/$url/";
228             break;
229         case 
'download';
230             
$temp_url "$gfe_url/view.php?file=$url&amp;dl=1";
231             
$temp_url_nice "$gfe_url/download/$url/";
232             break;
233     }
234     if(
$nice_url) {
235         return 
$temp_url_nice;
236     } else {
237         return 
$temp_url;
238     }
239 }
240
241
### Function: Create Sorting URL
242
function create_sort_url($sortby) {
243     global 
$gfe_url$nice_url$directories_before_current_path$current_directory_name$sort_order;
244     
$temp_url '';
245     
$temp_url_nice '';
246     
$sortorder '';
247     
$directories_before_current_path urldecode($directories_before_current_path);
248     
$directories_before_current_path urlencode($directories_before_current_path);
249     
$directories_before_current_path str_replace('%2F''/'$directories_before_current_path);
250     
$current_directory_name urldecode($current_directory_name);
251     
$current_directory_name urlencode($current_directory_name);
252     
$current_directory_name str_replace('%2F''/'$current_directory_name);
253     if(
$sort_order == SORT_DESC) {
254         
$sortorder 'asc';
255     } else {
256         
$sortorder 'desc';
257     }
258     if(empty(
$current_directory_name)) {
259         
$temp_url "?by=$sortby&amp;order=$sortorder";
260         
$temp_url_nice "$gfe_url/sortby/$sortby/sortorder/$sortorder/";
261     } else {
262         
$temp_url "?dir=$directories_before_current_path$current_directory_name&amp;by=$sortby&amp;order=$sortorder";
263         
$temp_url_nice "$gfe_url/browse/$directories_before_current_path$current_directory_name/sortby/$sortby/sortorder/$sortorder/";
264     }
265     if(
$nice_url) {
266         return 
$temp_url_nice;
267     } else {
268         return 
$temp_url;
269     }
270 }
271
272
### Function: Create Sorting Image
273
function create_sort_image($sortby) {
274     global 
$sort_order_image$sort_order_text;
275     if(
trim($_GET['by']) == $sortby) {
276         return 
"<img src=\"$sort_order_image\" alt=\"Sorted By ".ucfirst($sortby)." In $sort_order_text Order\" />";
277     }
278 }
279
280
281
### Function: Show Source Of Text File
282
function display_text($file) {
283     global 
$lines;
284     
ob_start();
285     
show_source($file);
286     
$filecontents ob_get_contents();
287     
ob_end_clean();
288     
$filecontents str_replace('<code>'''$filecontents);
289     
$filecontents str_replace('</code>'''$filecontents);
290     
$filecontents str_replace("\n"''$filecontents);
291     
$filecontents explode('<br />'$filecontents);
292     
$lines count($filecontents);
293     
$iLen strlen($lines);
294     for(
$i 0$i $lines$i++) {
295         
$sGap = ($iLen strlen($i+1));
296         
$filecontents[$i] = '<span style="color: #999999">'.str_repeat(' '$sGap).($i+1).' </span>'.$filecontents[$i]."<br />\n";
297     }
298     
$filecontents implode(''$filecontents);
299     return 
"\n$filecontents\n";
300 }
301
302
### Function: Breadcrumbs
303
function breadcrumbs($delim '<b>&raquo;</b>') {
304     global 
$root_filename$directory_names$current_directory_name;
305     
$temp_breadcrumb_path '';
306     
$temp_breadcrumb_url '';
307     
$temp_breadcrumb '<a href="'.url('home''dir').'">Home</a> ';
308     foreach(
$directory_names as $directory_name) {
309         
$temp_breadcrumb_path .= $directory_name.'/';
310         
$temp_breadcrumb_url substr($temp_breadcrumb_path0, -1);
311         
$temp_breadcrumb .= $delim.' <a href="'.url($temp_breadcrumb_url,'dir').'">'.$directory_name.'</a> ';
312     }
313     if(!empty(
$current_directory_name)) {
314         
$temp_breadcrumb .= " $delim <b>".$current_directory_name.'</b>';
315     }
316     return 
$temp_breadcrumb;
317 }
318
319
### Function: Breadcrumbs For view.php
320
function breadcrumbs_view($delim '<b>&raquo;</b>') {
321     global 
$file_name$file$root_filename;
322     
$temp_breadcrumb_path '';
323     
$temp_breadcrumb_url '';
324     
$directory_names explode('/'$file);
325     unset(
$directory_names[sizeof($directory_names)-1]);
326     foreach(
$directory_names as $directory_name) {
327         
$temp_breadcrumb_path .= $directory_name.'/';
328         
$temp_breadcrumb_url substr($temp_breadcrumb_path0, -1);
329         
$temp_breadcrumb .= $delim.' <a href="'.url($temp_breadcrumb_url,'dir').'">'.$directory_name.'</a> ';
330     }
331     return 
"<a href=\"".url('home''dir')."\">Home</a> $temp_breadcrumb $delim <b>$file_name</b>";
332 }
333
334
### Function: Display Error Message
335
function display_error($msg) {
336     global 
$site_name$gfe_url$root_url;
337     echo 
'<html>'."\n";
338     echo 
'<head>'."\n";
339     echo 
"<title>$site_name - Error - $msg</title>\n";
340     echo 
'<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />'."\n";
341     echo 
"<link rel=\"shortcut icon\" href=\"$gfe_url/resources/favicon.ico\" type=\"image/ico\">\n";
342     echo 
'<style type="text/css" media="screen">'."\n";
343     echo 
"@import url( $gfe_url/resources/style.css );\n";
344     echo 
'</style>'."\n";
345     echo 
'</head>'."\n";
346     echo 
'<body>'."\n";
347     echo 
"<p align=\"center\"><b>$msg</b></p>\n";
348     echo 
"<p align=\"center\"><a href=\"$root_url\">Go To $site_name</a> | <a href=\"#\" onclick=\"javascript: history.go(-1)\">Go To Previous Page</a></p>\n";
349     echo 
'</body>'."\n";
350     echo 
'</html>'."\n";
351     exit();
352 }
353
?>

http://files.fwbase.com/files/Php/functions.php