Source for file class.dir.php

Documentation is available at class.dir.php


1 <?php
2 /*
3 METAjour version 1.6 - Content Management System
4 Copyright (C) 2003 IPW Systems A/S
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 $Id: class.dir.php,v 1.1.1.1 2004/09/15 15:33:52 Administrator Exp $
21
22 */
23 class phpMyDIR {
24
25 /**
26 * @access public
27 * @param string
28 */
29 var $baseRoot = './';
30
31 /**
32 * @access public
33 * @param string
34 */
35 var $globalDateFormat = '%d. %B %Y | %H:%M';
36 /**
37 * @access public
38 * @param array
39 */
40 var $globalDateDays = array('So', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa');
41 /**
42 * @access public
43 * @param array
44 */
45 var $globalDateMonths = array('Jan', 'Feb', 'Mär', 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sept', 'Okt', 'Nov', 'Dez');
46
47 /**
48 * @access public
49 * @param integer
50 */
51 var $globalSizeRound = 2;
52 /**
53 * @access public
54 * @param array
55 */
56 var $globalSizeUnits = array('bytes', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb', 'Eb');
57
58 /**
59 * @access public
60 * @param string
61 */
62 var $globalSepThousands = '.';
63 /**
64 * @access public
65 * @param string
66 */
67 var $globalSepDecimals = ',';
68
69 /**
70 * @access public
71 * @param string
72 */
73 var $fileMask = '*';
74 /**
75 * @access public
76 * @param bool
77 */
78 var $fileShowTime = 1;
79 /**
80 * @access public
81 * @param bool
82 */
83 var $fileShowSize = 1;
84 /**
85 * @access public
86 * @param bool
87 */
88 var $fileConvertTime = 1;
89 /**
90 * @access public
91 * @param bool
92 */
93 var $fileConvertSize = 1;
94
95 /**
96 * @access public
97 * @param string
98 */
99 var $folderMask = '*';
100 /**
101 * @access public
102 * @param bool
103 */
104 var $folderOnly = 0;
105 /**
106 * @access public
107 * @param bool
108 */
109 var $folderShowTime = 1;
110 /**
111 * @access public
112 * @param bool
113 */
114 var $folderConvertTime = 1;
115
116 /**
117 * @access public
118 * @param bool
119 */
120 var $spiderRecursive = 0;
121 /**
122 * @access public
123 * @param integer
124 */
125 var $spiderLevel = 1;
126
127 /**
128 * @access public
129 * @param array
130 */
131 var $resultMixed = array();
132 /**
133 * @access public
134 * @param array
135 */
136 var $resultFiles = array();
137 /**
138 * @access public
139 * @param array
140 */
141 var $resultFolder = array();
142
143 /**
144 * @access private
145 * @param mixed
146 */
147 var $_maskArray = false;
148
149 /**
150 * getResult()
151 *
152 * start the search and return true or false based on search success
153 *
154 * @access public
155 * @return bool
156 *
157 ***/
158
159 function getResult()
160 {
161 if (!ereg("/$", $this->baseRoot))
162 {
163 $this->baseRoot = $this->baseRoot."/";
164 }
165
166 $this->_search($this->baseRoot);
167
168 ksort ($this->resultMixed);
169 sort ($this->resultFiles);
170 sort ($this->resultFolder);
171
172 return true;
173 }
174
175 /**
176 * fileMatch()
177 *
178 * compare the given file with the file-mask
179 *
180 * @access public
181 * @param string
182 * @return bool
183 *
184 ***/
185
186 function fileMatch($file)
187 {
188 if ($this->_maskArray == 0)
189 {
190 $mask = $this->fileMask;
191 $mask = str_replace(".", "\.", $mask);
192 $mask = str_replace("*", "(.*)", $mask);
193 $mask_array = explode(",",$mask);
194 $this->_maskArray = array_map("trim", $mask_array);
195 }
196
197 foreach ($this->_maskArray as $valid)
198 {
199 if( eregi("^$valid", $file) )
200 {
201 return true;
202 }
203 }
204 return false;
205 }
206
207 /**
208 * getResult()
209 *
210 * read the contenst of the given root
211 *
212 * @access private
213 * @param string
214 * @param integer
215 *
216 ***/
217
218 function _search($dir, $pos = 0)
219 {
220 $handle = @opendir($dir);
221 while ($file = @readdir ($handle))
222 {
223 if (eregi("^\.{1,2}$",$file))
224 {
225 continue;
226 }
227 if (is_dir($dir.$file))
228 {
229 $this->_folder($dir,$file,$pos);
230 }
231 if (is_file($dir.$file))
232 {
233 $this->_files($dir,$file,$pos);
234 }
235 }
236 @closedir($handle);
237 }
238
239 /**
240 *
241 * _folder($dir,$file,$pos)
242 *
243 * prepare a folder
244 *
245 * @access private
246 * @param string
247 * @param string
248 * @param integer
249 *
250 ***/
251
252 function _folder($dir,$file,$pos)
253 {
254 $sdir = "/".str_replace($this->baseRoot, "", $dir.$file)."/";
255 $this->resultFolder[] = $sdir;
256
257 $tmp = array();
258 $tmp["level"] = $pos;
259 // $tmp["_name"] = $sdir;
260 // $tmp["_type"] = "folder";
261
262 if ($this->folderShowTime == 1)
263 {
264 // $tmp["_time"] = filemtime($dir.$file);
265 }
266
267 $this->resultMixed[$sdir] = $tmp;
268
269 if ($this->spiderRecursive == 1 && ($pos < $this->spiderLevel || $this->spiderLevel == 0))
270 {
271 $this->_search($dir.$file."/", $pos + 1);
272 }
273
274 }
275
276 /**
277 *
278 * _files($dir,$file,$pos)
279 *
280 * prepare a file
281 *
282 * @access private
283 * @param string
284 * @param string
285 * @param integer
286 *
287 ***/
288
289 function _files($dir,$file,$pos)
290 {
291 $sdir = "/".str_replace($this->baseRoot, "", $dir.$file)."/";
292 if ( $this->fileMatch($file) == 1 && $this->folderOnly == 0 )
293 {
294 $this->resultFiles[] = $sdir.$file;
295
296 $tmp = array();
297 $tmp["level"] = $pos;
298 $tmp["name"] = $file;
299
300 if ($this->fileShowTime == 1)
301 {
302 $tmp["time"] = filemtime($dir.$file);
303 if ($this->fileConvertTime == 1)
304 {
305 $tmp["time"] = $this->humanReadableTime($tmp["time"]);
306 }
307 if ($this->fileConvertTime == 2)
308 {
309 $tmp["time"] = date ("Y-m-d H:i:s",$tmp["time"]);
310 }
311 }
312
313 if ($this->fileShowSize == 1)
314 {
315 $tmp["size"] = filesize($dir.$file);
316 if ($this->fileConvertSize == 1)
317 {
318 $tmp["size"] = $this->humanReadableSize($tmp["size"]);
319 }
320 }
321
322 $sdir = "/".str_replace($this->baseRoot, "", $dir);
323
324 $this->resultMixed[$sdir][$file] = $tmp;
325 }
326 }
327
328 /**
329 *
330 * humanReadable($size=0)
331 *
332 * convert bytes to kb, mb, gb
333 *
334 * @access public
335 * @param integer the size
336 * @return string
337 *
338 ***/
339
340 function humanReadableSize($size = 0)
341 {
342 $faktor = $tmpfaktor = 1024;
343
344 for ($i=0; $i < count($this->globalSizeUnits); $i++)
345 {
346 if ( ($size/$tmpfaktor) < 1 )
347 {
348 return round($size/($tmpfaktor/$faktor), $this->globalSizeRound)." ".$this->globalSizeUnits[$i];
349 }
350 $tmpfaktor = $faktor*$tmpfaktor;
351 }
352 }
353
354 /**
355 *
356 * @access public
357 * @param integer the current timestamp
358 * @return string the formatted date
359 *
360 */
361
362 function humanReadableTime($timestamp = -1)
363 {
364 if ($timestamp == -1)
365 {
366 $timestamp = time();
367 }
368
369 $date = ereg_replace('%[aA]', $this->globalDateDays[(int)strftime('%w', $timestamp)], $this->globalDateFormat);
370 $date = ereg_replace('%[bB]', $this->globalDateMonths[(int)strftime('%m', $timestamp)-1], $date);
371
372 return strftime($date, $timestamp);
373 }
374
375 }
376
377 ?>

Documentation generated on Thu, 9 Jun 2005 06:51:39 +0200 by phpDocumentor 1.2.3