61d9ec71 |
1 | <?php |
2 | |
3 | /** |
0c701a88 |
4 | * global.php |
61d9ec71 |
5 | * |
62f7daa5 |
6 | * This includes code to update < 4.1.0 globals to the newer format |
242342d0 |
7 | * It also has some session register functions that work across various |
62f7daa5 |
8 | * php versions. |
61d9ec71 |
9 | * |
47ccfad4 |
10 | * @copyright © 1999-2006 The SquirrelMail Project Team |
4b4abf93 |
11 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
31841a9e |
12 | * @version $Id$ |
d6c32258 |
13 | * @package squirrelmail |
61d9ec71 |
14 | */ |
15 | |
051f6245 |
16 | /** |
2ca4c65a |
17 | */ |
7f62aaef |
18 | define('SQ_INORDER',0); |
19 | define('SQ_GET',1); |
20 | define('SQ_POST',2); |
21 | define('SQ_SESSION',3); |
22 | define('SQ_COOKIE',4); |
23 | define('SQ_SERVER',5); |
24 | define('SQ_FORM',6); |
a32985a5 |
25 | |
202bcbcc |
26 | |
62f7daa5 |
27 | /** |
28 | * returns true if current php version is at mimimum a.b.c |
29 | * |
97bdc607 |
30 | * Called: check_php_version(4,1) |
8b096f0a |
31 | * @param int a major version number |
32 | * @param int b minor version number |
33 | * @param int c release number |
34 | * @return bool |
97bdc607 |
35 | */ |
62f7daa5 |
36 | function check_php_version ($a = '0', $b = '0', $c = '0') |
9697c5ab |
37 | { |
5673cabe |
38 | return version_compare ( PHP_VERSION, "$a.$b.$c", 'ge' ); |
9697c5ab |
39 | } |
40 | |
97bdc607 |
41 | /** |
62f7daa5 |
42 | * returns true if the current internal SM version is at minimum a.b.c |
43 | * These are plain integer comparisons, as our internal version is |
97bdc607 |
44 | * constructed by us, as an array of 3 ints. |
45 | * |
46 | * Called: check_sm_version(1,3,3) |
8b096f0a |
47 | * @param int a major version number |
48 | * @param int b minor version number |
49 | * @param int c release number |
50 | * @return bool |
97bdc607 |
51 | */ |
52 | function check_sm_version($a = 0, $b = 0, $c = 0) |
53 | { |
54 | global $SQM_INTERNAL_VERSION; |
55 | if ( !isset($SQM_INTERNAL_VERSION) || |
56 | $SQM_INTERNAL_VERSION[0] < $a || |
150c28d6 |
57 | ( $SQM_INTERNAL_VERSION[0] == $a && |
58 | $SQM_INTERNAL_VERSION[1] < $b) || |
59 | ( $SQM_INTERNAL_VERSION[0] == $a && |
60 | $SQM_INTERNAL_VERSION[1] == $b && |
97bdc607 |
61 | $SQM_INTERNAL_VERSION[2] < $c ) ) { |
62 | return FALSE; |
62f7daa5 |
63 | } |
64 | return TRUE; |
97bdc607 |
65 | } |
66 | |
67 | |
8b096f0a |
68 | /** |
69 | * Recursively strip slashes from the values of an array. |
70 | * @param array array the array to strip, passed by reference |
71 | * @return void |
72 | */ |
a32985a5 |
73 | function sqstripslashes(&$array) { |
3aa17cf9 |
74 | if(count($array) > 0) { |
75 | foreach ($array as $index=>$value) { |
76 | if (is_array($array[$index])) { |
77 | sqstripslashes($array[$index]); |
78 | } |
79 | else { |
80 | $array[$index] = stripslashes($value); |
81 | } |
a32985a5 |
82 | } |
83 | } |
84 | } |
85 | |
8b096f0a |
86 | /** |
87 | * Add a variable to the session. |
88 | * @param mixed $var the variable to register |
89 | * @param string $name the name to refer to this variable |
90 | * @return void |
91 | */ |
61d9ec71 |
92 | function sqsession_register ($var, $name) { |
281c3d5b |
93 | |
94 | sqsession_is_active(); |
95 | |
62f7daa5 |
96 | $_SESSION["$name"] = $var; |
97 | |
dcc1cc82 |
98 | session_register("$name"); |
61d9ec71 |
99 | } |
3aa17cf9 |
100 | |
8b096f0a |
101 | /** |
102 | * Delete a variable from the session. |
103 | * @param string $name the name of the var to delete |
104 | * @return void |
105 | */ |
61d9ec71 |
106 | function sqsession_unregister ($name) { |
281c3d5b |
107 | |
108 | sqsession_is_active(); |
109 | |
abd74f7d |
110 | unset($_SESSION[$name]); |
62f7daa5 |
111 | |
dcc1cc82 |
112 | session_unregister("$name"); |
61d9ec71 |
113 | } |
3aa17cf9 |
114 | |
8b096f0a |
115 | /** |
116 | * Checks to see if a variable has already been registered |
117 | * in the session. |
118 | * @param string $name the name of the var to check |
119 | * @return bool whether the var has been registered |
120 | */ |
d7c82551 |
121 | function sqsession_is_registered ($name) { |
122 | $test_name = &$name; |
123 | $result = false; |
62f7daa5 |
124 | |
abd74f7d |
125 | if (isset($_SESSION[$test_name])) { |
126 | $result = true; |
d7c82551 |
127 | } |
62f7daa5 |
128 | |
d7c82551 |
129 | return $result; |
130 | } |
131 | |
54ce41dd |
132 | |
133 | /** |
134 | * Retrieves a form variable, from a set of possible similarly named |
135 | * form variables, based on finding a different, single field. This |
136 | * is intended to allow more than one same-named inputs in a single |
137 | * <form>, where the submit button that is clicked tells us which |
138 | * input we should retrieve. An example is if we have: |
139 | * <select name="startMessage_1"> |
140 | * <select name="startMessage_2"> |
141 | * <input type="submit" name="form_submit_1"> |
142 | * <input type="submit" name="form_submit_2"> |
143 | * and we want to know which one of the select inputs should be |
144 | * returned as $startMessage (without the suffix!), this function |
145 | * decides by looking for either "form_submit_1" or "form_submit_2" |
146 | * (both should not appear). In this example, $name should be |
147 | * "startMessage" and $indicator_field should be "form_submit". |
148 | * |
149 | * NOTE that form widgets must be named with the suffix "_1", "_2", "_3" |
150 | * and so on, or this function will not work. |
151 | * |
152 | * If more than one of the indicator fields is found, the first one |
153 | * (numerically) will win. |
154 | * |
139a4b99 |
155 | * If an indicator field is found without a matching input ($name) |
156 | * field, FALSE is returned. |
157 | * |
158 | * If no indicator fields are found, a field of $name *without* any |
159 | * suffix is searched for (but only if $fallback_no_suffix is TRUE), |
160 | * and if not found, FALSE is ultimately returned. |
54ce41dd |
161 | * |
162 | * It should also be possible to use the same string for both |
163 | * $name and $indicator_field to look for the first possible |
164 | * widget with a suffix that can be found (and possibly fallback |
165 | * to a widget without a suffix). |
166 | * |
167 | * @param string name the name of the var to search |
168 | * @param mixed value the variable to return |
169 | * @param string indicator_field the name of the field upon which to base |
170 | * our decision upon (see above) |
171 | * @param int search constant defining where to look |
172 | * @param bool fallback_no_suffix whether or not to look for $name with |
173 | * no suffix when nothing else is found |
174 | * @param mixed default the value to assign to $value when nothing is found |
175 | * @param int typecast force variable to be cast to given type (please |
176 | * use SQ_TYPE_XXX constants or set to FALSE (default) |
177 | * to leave variable type unmolested) |
178 | * |
179 | * @return bool whether variable is found. |
180 | */ |
181 | function sqGetGlobalVarMultiple($name, &$value, $indicator_field, |
182 | $search = SQ_INORDER, |
183 | $fallback_no_suffix=TRUE, $default=NULL, |
184 | $typecast=FALSE) { |
185 | |
186 | $max_form_search = 15; |
187 | |
188 | for ($i = 1; $i <= $max_form_search; $i++) { |
189 | if (sqGetGlobalVar($indicator_field . '_' . $i, $temp, $search)) { |
190 | return sqGetGlobalVar($name . '_' . $i, $value, $search, $default, $typecast); |
191 | } |
192 | } |
193 | |
194 | |
195 | // no indicator field found; just try without suffix if allowed |
196 | // |
197 | if ($fallback_no_suffix) { |
198 | return sqGetGlobalVar($name, $value, $search, $default, $typecast); |
199 | } |
200 | |
201 | |
202 | // no dice, set default and return FALSE |
203 | // |
204 | if (!is_null($default)) { |
205 | $value = $default; |
206 | } |
207 | return FALSE; |
208 | |
209 | } |
210 | |
211 | |
4cd8ae7d |
212 | /** |
2d055f0a |
213 | * Search for the var $name in $_SESSION, $_POST, $_GET, $_COOKIE, or $_SERVER |
214 | * and set it in provided var. |
d1975c5b |
215 | * |
2d055f0a |
216 | * If $search is not provided, or if it is SQ_INORDER, it will search $_SESSION, |
217 | * then $_POST, then $_GET. If $search is SQ_FORM it will search $_POST and |
218 | * $_GET. Otherwise, use one of the defined constants to look for a var in one |
219 | * place specifically. |
d1975c5b |
220 | * |
2d055f0a |
221 | * Note: $search is an int value equal to one of the constants defined above. |
d1975c5b |
222 | * |
2d055f0a |
223 | * Example: |
224 | * sqgetGlobalVar('username',$username,SQ_SESSION); |
225 | * // No quotes around last param, it's a constant - not a string! |
d1975c5b |
226 | * |
8b096f0a |
227 | * @param string name the name of the var to search |
228 | * @param mixed value the variable to return |
229 | * @param int search constant defining where to look |
54ce41dd |
230 | * @param mixed default the value to assign to $value when nothing is found |
c2b585c5 |
231 | * @param int typecast force variable to be cast to given type (please |
232 | * use SQ_TYPE_XXX constants or set to FALSE (default) |
233 | * to leave variable type unmolested) |
54ce41dd |
234 | * |
8b096f0a |
235 | * @return bool whether variable is found. |
4cd8ae7d |
236 | */ |
202bcbcc |
237 | function sqgetGlobalVar($name, &$value, $search = SQ_INORDER, $default = NULL, $typecast = false) { |
238 | |
239 | $result = false; |
f79c19a4 |
240 | |
4cd8ae7d |
241 | switch ($search) { |
62f7daa5 |
242 | /* we want the default case to be first here, |
051f6245 |
243 | so that if a valid value isn't specified, |
244 | all three arrays will be searched. */ |
d1975c5b |
245 | default: |
d9ad2525 |
246 | case SQ_INORDER: // check session, post, get |
d1975c5b |
247 | case SQ_SESSION: |
248 | if( isset($_SESSION[$name]) ) { |
4cd8ae7d |
249 | $value = $_SESSION[$name]; |
202bcbcc |
250 | $result = TRUE; |
251 | break; |
d1975c5b |
252 | } elseif ( $search == SQ_SESSION ) { |
253 | break; |
254 | } |
d9ad2525 |
255 | case SQ_FORM: // check post, get |
d1975c5b |
256 | case SQ_POST: |
257 | if( isset($_POST[$name]) ) { |
4cd8ae7d |
258 | $value = $_POST[$name]; |
202bcbcc |
259 | $result = TRUE; |
260 | break; |
d1975c5b |
261 | } elseif ( $search == SQ_POST ) { |
27d0841c |
262 | break; |
d1975c5b |
263 | } |
264 | case SQ_GET: |
265 | if ( isset($_GET[$name]) ) { |
266 | $value = $_GET[$name]; |
202bcbcc |
267 | $result = TRUE; |
268 | break; |
62f7daa5 |
269 | } |
d1975c5b |
270 | /* NO IF HERE. FOR SQ_INORDER CASE, EXIT after GET */ |
271 | break; |
272 | case SQ_COOKIE: |
273 | if ( isset($_COOKIE[$name]) ) { |
274 | $value = $_COOKIE[$name]; |
202bcbcc |
275 | $result = TRUE; |
276 | break; |
d1975c5b |
277 | } |
278 | break; |
279 | case SQ_SERVER: |
d1975c5b |
280 | if ( isset($_SERVER[$name]) ) { |
281 | $value = $_SERVER[$name]; |
202bcbcc |
282 | $result = TRUE; |
283 | break; |
d1975c5b |
284 | } |
285 | break; |
4cd8ae7d |
286 | } |
202bcbcc |
287 | if ($result && $typecast) { |
288 | switch ($typecast) { |
c2b585c5 |
289 | case SQ_TYPE_INT: $value = (int) $value; break; |
290 | case SQ_TYPE_STRING: $value = (string) $value; break; |
291 | case SQ_TYPE_BOOL: $value = (bool) $value; break; |
202bcbcc |
292 | default: break; |
293 | } |
ced8272a |
294 | } else if (!$result && !is_null($default)) { |
202bcbcc |
295 | $value = $default; |
296 | } |
297 | return $result; |
4cd8ae7d |
298 | } |
299 | |
8b096f0a |
300 | /** |
301 | * Deletes an existing session, more advanced than the standard PHP |
302 | * session_destroy(), it explicitly deletes the cookies and global vars. |
66c7cd3f |
303 | * |
304 | * WARNING: Older PHP versions have some issues with session management. |
305 | * See http://bugs.php.net/11643 (warning, spammed bug tracker) and |
306 | * http://bugs.php.net/13834. SID constant is not destroyed in PHP 4.1.2, |
307 | * 4.2.3 and maybe other versions. If you restart session after session |
308 | * is destroyed, affected PHP versions produce PHP notice. Bug should |
309 | * be fixed only in 4.3.0 |
8b096f0a |
310 | */ |
513db22c |
311 | function sqsession_destroy() { |
242342d0 |
312 | |
281c3d5b |
313 | /* |
314 | * php.net says we can kill the cookie by setting just the name: |
315 | * http://www.php.net/manual/en/function.setcookie.php |
316 | * maybe this will help fix the session merging again. |
317 | * |
318 | * Changed the theory on this to kill the cookies first starting |
319 | * a new session will provide a new session for all instances of |
320 | * the browser, we don't want that, as that is what is causing the |
321 | * merging of sessions. |
322 | */ |
242342d0 |
323 | |
f9902ccb |
324 | global $base_uri; |
f31687f6 |
325 | |
3a1de9f1 |
326 | if (isset($_COOKIE[session_name()])) sqsetcookie(session_name(), '', 0, $base_uri); |
327 | if (isset($_COOKIE['username'])) sqsetcookie('username','',0,$base_uri); |
328 | if (isset($_COOKIE['key'])) sqsetcookie('key','',0,$base_uri); |
281c3d5b |
329 | |
330 | $sessid = session_id(); |
331 | if (!empty( $sessid )) { |
abd74f7d |
332 | $_SESSION = array(); |
21e18f59 |
333 | @session_destroy(); |
242342d0 |
334 | } |
281c3d5b |
335 | } |
242342d0 |
336 | |
8b096f0a |
337 | /** |
281c3d5b |
338 | * Function to verify a session has been started. If it hasn't |
339 | * start a session up. php.net doesn't tell you that $_SESSION |
340 | * (even though autoglobal), is not created unless a session is |
341 | * started, unlike $_POST, $_GET and such |
342 | */ |
281c3d5b |
343 | function sqsession_is_active() { |
281c3d5b |
344 | $sessid = session_id(); |
345 | if ( empty( $sessid ) ) { |
3a1de9f1 |
346 | sqsession_start(); |
281c3d5b |
347 | } |
513db22c |
348 | } |
349 | |
3a1de9f1 |
350 | /** |
351 | * Function to start the session and store the cookie with the session_id as |
352 | * HttpOnly cookie which means that the cookie isn't accessible by javascript |
353 | * (IE6 only) |
354 | */ |
355 | function sqsession_start() { |
202bcbcc |
356 | global $base_uri; |
7f62aaef |
357 | |
3a1de9f1 |
358 | session_start(); |
202bcbcc |
359 | $session_id = session_id(); |
360 | |
3a1de9f1 |
361 | // session_starts sets the sessionid cookie buth without the httponly var |
362 | // setting the cookie again sets the httponly cookie attribute |
09569b55 |
363 | |
364 | // disable, @see sqsetcookie and php 5.1.2 |
365 | // sqsetcookie(session_name(),session_id(),false,$base_uri); |
3a1de9f1 |
366 | } |
367 | |
368 | |
369 | /** |
370 | * Set a cookie |
371 | * @param string $sName The name of the cookie. |
372 | * @param string $sValue The value of the cookie. |
373 | * @param int $iExpire The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch. |
374 | * @param string $sPath The path on the server in which the cookie will be available on. |
375 | * @param string $sDomain The domain that the cookie is available. |
376 | * @param boolean $bSecure Indicates that the cookie should only be transmitted over a secure HTTPS connection. |
377 | * @param boolean $bHttpOnly Disallow JS to access the cookie (IE6 only) |
378 | * @return void |
379 | */ |
202bcbcc |
380 | function sqsetcookie($sName,$sValue,$iExpire=false,$sPath="",$sDomain="",$bSecure=false,$bHttpOnly=true,$bFlush=false) { |
381 | static $sCookieCache; |
382 | if (!isset($sCache)) { |
383 | $sCache = ''; |
384 | } |
385 | /** |
386 | * We have to send all cookies with one header call otherwise we loose cookies. |
387 | * In order to achieve that the sqsetcookieflush function calls this function with $bFlush = true. |
388 | * If that happens we send the cookie header. |
389 | */ |
390 | if ($bFlush) { |
09569b55 |
391 | // header($sCookieCache); |
202bcbcc |
392 | return; |
393 | } |
09569b55 |
394 | if (!$sName) return; |
395 | |
396 | // php 5.1.2 and 4.4.2 do not allow to send multiple headers at once. |
397 | // Because that's the only way to get this thing working we fallback to |
398 | // setcookie until we solved this |
399 | if ($iExpire===false) $iExpire = 0; |
400 | setcookie($sName, $sValue, $iExpire, $sPath); |
401 | return; |
202bcbcc |
402 | |
3a1de9f1 |
403 | $sHeader = "Set-Cookie: $sName=$sValue"; |
404 | if ($sPath) { |
6f9fa51a |
405 | $sHeader .= "; path=$sPath"; |
3a1de9f1 |
406 | } |
6f9fa51a |
407 | if ($iExpire !== false) { |
3a1de9f1 |
408 | $sHeader .= "; Max-Age=$iExpire"; |
6f9fa51a |
409 | // php uses Expire header, also add the expire header |
a1ef1d05 |
410 | $sHeader .= "; expires=". gmdate('D, d-M-Y H:i:s T',$iExpire); |
3a1de9f1 |
411 | } |
412 | if ($sDomain) { |
413 | $sHeader .= "; Domain=$sDomain"; |
414 | } |
8ca9f662 |
415 | // TODO: IE for Mac (5.2) thinks that semicolon is part of cookie domain |
3a1de9f1 |
416 | if ($bSecure) { |
417 | $sHeader .= "; Secure"; |
418 | } |
419 | if ($bHttpOnly) { |
420 | $sHeader .= "; HttpOnly"; |
421 | } |
6f9fa51a |
422 | // $sHeader .= "; Version=1"; |
202bcbcc |
423 | $sCookieCache .= $sHeader ."\r\n"; |
09569b55 |
424 | //header($sHeader."\r\n"); |
202bcbcc |
425 | } |
426 | |
427 | /** |
428 | * Send the cookie header |
429 | * |
430 | * Cookies set with sqsetcookie will bet set after a sqsetcookieflush call. |
431 | * @return void |
432 | */ |
433 | function sqsetcookieflush() { |
434 | sqsetcookie('','','','','','','',true); |
435 | } |
436 | |
437 | /** |
438 | * session_regenerate_id replacement for PHP < 4.3.2 |
439 | * |
440 | * This code is borrowed from Gallery, session.php version 1.53.2.1 |
441 | */ |
442 | if (!function_exists('session_regenerate_id')) { |
443 | function make_seed() { |
444 | list($usec, $sec) = explode(' ', microtime()); |
445 | return (float)$sec + ((float)$usec * 100000); |
446 | } |
447 | |
448 | function php_combined_lcg() { |
449 | mt_srand(make_seed()); |
450 | $tv = gettimeofday(); |
451 | $lcg['s1'] = $tv['sec'] ^ (~$tv['usec']); |
452 | $lcg['s2'] = mt_rand(); |
453 | $q = (int) ($lcg['s1'] / 53668); |
454 | $lcg['s1'] = (int) (40014 * ($lcg['s1'] - 53668 * $q) - 12211 * $q); |
455 | if ($lcg['s1'] < 0) { |
456 | $lcg['s1'] += 2147483563; |
457 | } |
458 | $q = (int) ($lcg['s2'] / 52774); |
459 | $lcg['s2'] = (int) (40692 * ($lcg['s2'] - 52774 * $q) - 3791 * $q); |
460 | if ($lcg['s2'] < 0) { |
461 | $lcg['s2'] += 2147483399; |
462 | } |
463 | $z = (int) ($lcg['s1'] - $lcg['s2']); |
464 | if ($z < 1) { |
465 | $z += 2147483562; |
466 | } |
467 | return $z * 4.656613e-10; |
468 | } |
3a1de9f1 |
469 | |
202bcbcc |
470 | function session_regenerate_id() { |
471 | global $base_uri; |
472 | $tv = gettimeofday(); |
473 | sqgetGlobalVar('REMOTE_ADDR',$remote_addr,SQ_SERVER); |
474 | $buf = sprintf("%.15s%ld%ld%0.8f", $remote_addr, $tv['sec'], $tv['usec'], php_combined_lcg() * 10); |
475 | session_id(md5($buf)); |
476 | if (ini_get('session.use_cookies')) { |
477 | // at a later stage we use sqsetcookie. At this point just do |
478 | // what session_regenerate_id would do |
479 | setcookie(session_name(), session_id(), NULL, $base_uri); |
480 | } |
481 | return TRUE; |
482 | } |
3a1de9f1 |
483 | } |
7f62aaef |
484 | |
202bcbcc |
485 | |
7f62aaef |
486 | /** |
487 | * php_self |
488 | * |
489 | * Creates an URL for the page calling this function, using either the PHP global |
490 | * REQUEST_URI, or the PHP global PHP_SELF with QUERY_STRING added. Before 1.5.1 |
491 | * function was stored in function/strings.php. |
492 | * |
493 | * @return string the complete url for this page |
494 | * @since 1.2.3 |
495 | */ |
496 | function php_self () { |
497 | if ( sqgetGlobalVar('REQUEST_URI', $req_uri, SQ_SERVER) && !empty($req_uri) ) { |
498 | return $req_uri; |
499 | } |
500 | |
501 | if ( sqgetGlobalVar('PHP_SELF', $php_self, SQ_SERVER) && !empty($php_self) ) { |
502 | |
503 | // need to add query string to end of PHP_SELF to match REQUEST_URI |
504 | // |
505 | if ( sqgetGlobalVar('QUERY_STRING', $query_string, SQ_SERVER) && !empty($query_string) ) { |
506 | $php_self .= '?' . $query_string; |
507 | } |
508 | |
509 | return $php_self; |
510 | } |
511 | |
512 | return ''; |
513 | } |
aa201211 |
514 | |
515 | |
516 | /** |
8f32a0a3 |
517 | * Find files and/or directories in a given directory optionally |
518 | * limited to only those with the given file extension. If the |
519 | * directory is not found or cannot be opened, no error is generated; |
520 | * only an empty file list is returned. |
aa201211 |
521 | FIXME: do we WANT to throw an error or a notice or... or return FALSE? |
522 | * |
8f32a0a3 |
523 | * @param string $directory_path The path (relative or absolute) |
524 | * to the desired directory. |
525 | * @param string $extension The file extension filter (optional; |
526 | * default is to return all files (dirs). |
527 | * @param boolean $return_filenames_only When TRUE, only file/dir names |
aa201211 |
528 | * are returned, otherwise the |
529 | * $directory_path string is |
8f32a0a3 |
530 | * prepended to each file/dir in |
aa201211 |
531 | * the returned list (optional; |
8f32a0a3 |
532 | * default is filename/dirname only) |
533 | * @param boolean $include_directories When TRUE, directories are |
534 | * included (optional; default |
535 | * DO include directories). |
536 | * @param boolean $directories_only When TRUE, ONLY directories |
537 | * are included (optional; default |
538 | * is to include files too). |
539 | * @param boolean $separate_files_and_directories When TRUE, files and |
540 | * directories are returned |
541 | * in separate lists, so |
542 | * the return value is |
543 | * formatted as a two-element |
544 | * array with the two keys |
545 | * "FILES" and "DIRECTORIES", |
546 | * where corresponding values |
547 | * are lists of either all |
548 | * files or all directories |
549 | * (optional; default do not |
550 | * split up return array). |
551 | * |
aa201211 |
552 | * |
8f32a0a3 |
553 | * @return array The requested file/directory list(s). |
aa201211 |
554 | * |
555 | * @since 1.5.2 |
556 | * |
557 | */ |
8f32a0a3 |
558 | function list_files($directory_path, $extension='', $return_filenames_only=TRUE, |
559 | $include_directories=TRUE, $directories_only=FALSE, |
560 | $separate_files_and_directories=FALSE) { |
aa201211 |
561 | |
562 | $files = array(); |
8f32a0a3 |
563 | $directories = array(); |
aa201211 |
564 | |
565 | //FIXME: do we want to place security restrictions here like only allowing |
566 | // directories under SM_PATH? |
567 | // validate given directory |
568 | // |
569 | if (empty($directory_path) |
570 | || !is_dir($directory_path) |
571 | || !($DIR = opendir($directory_path))) { |
572 | return $files; |
573 | } |
574 | |
8f32a0a3 |
575 | |
576 | if (!empty($extension)) $extension = '.' . trim($extension, '.'); |
577 | $directory_path = rtrim($directory_path, '/'); |
578 | |
579 | |
aa201211 |
580 | // parse through the files |
581 | // |
aa201211 |
582 | while (($file = readdir($DIR)) !== false) { |
583 | |
584 | if ($file == '.' || $file == '..') continue; |
585 | |
8f32a0a3 |
586 | if (!empty($extension) |
587 | && strrpos($file, $extension) !== (strlen($file) - strlen($extension))) |
588 | continue; |
589 | |
590 | // only use is_dir() if we really need to (be as efficient as possible) |
591 | // |
592 | $is_dir = FALSE; |
593 | if (!$include_directories || $directories_only |
594 | || $separate_files_and_directories) { |
595 | if (is_dir($directory_path . '/' . $file)) { |
596 | if (!$include_directories) continue; |
597 | $is_dir = TRUE; |
598 | $directories[] = ($return_filenames_only |
599 | ? $file |
600 | : $directory_path . '/' . $file); |
601 | } |
602 | if ($directories_only) continue; |
603 | } |
604 | |
605 | if (!$separate_files_and_directories |
606 | || ($separate_files_and_directories && !$is_dir)) { |
aa201211 |
607 | $files[] = ($return_filenames_only |
8f32a0a3 |
608 | ? $file |
609 | : $directory_path . '/' . $file); |
aa201211 |
610 | } |
611 | |
612 | } |
613 | closedir($DIR); |
614 | |
8f32a0a3 |
615 | |
616 | if ($directories_only) return $directories; |
617 | if ($separate_files_and_directories) return array('FILES' => $files, |
618 | 'DIRECTORIES' => $directories); |
aa201211 |
619 | return $files; |
620 | |
621 | } |
622 | |
623 | |
624 | /** |
625 | * Print variable |
626 | * |
627 | * sm_print_r($some_variable, [$some_other_variable [, ...]]); |
628 | * |
629 | * Debugging function - does the same as print_r, but makes sure special |
630 | * characters are converted to htmlentities first. This will allow |
631 | * values like <some@email.address> to be displayed. |
632 | * The output is wrapped in <<pre>> and <</pre>> tags. |
633 | * Since 1.4.2 accepts unlimited number of arguments. |
634 | * @since 1.4.1 |
635 | * @return void |
636 | */ |
637 | function sm_print_r() { |
638 | ob_start(); // Buffer output |
639 | foreach(func_get_args() as $var) { |
640 | print_r($var); |
641 | echo "\n"; |
642 | // php has get_class_methods function that can print class methods |
643 | if (is_object($var)) { |
644 | // get class methods if $var is object |
645 | $aMethods=get_class_methods(get_class($var)); |
646 | // make sure that $aMethods is array and array is not empty |
647 | if (is_array($aMethods) && $aMethods!=array()) { |
648 | echo "Object methods:\n"; |
649 | foreach($aMethods as $method) { |
650 | echo '* ' . $method . "\n"; |
651 | } |
652 | } |
653 | echo "\n"; |
654 | } |
655 | } |
656 | $buffer = ob_get_contents(); // Grab the print_r output |
657 | ob_end_clean(); // Silently discard the output & stop buffering |
658 | print '<div align="left"><pre>'; |
659 | print htmlentities($buffer); |
660 | print '</pre></div>'; |
661 | } |
662 | |
663 | |