We want to play nice with PHP5, right?
[isso.git] / functions.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Iris Studios Shared Object Framework [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 /**
23 * Globalized Functions
24 * functions.php
25 *
26 * @package ISSO
27 */
28
29 /**
30 * Globalized Functions
31 *
32 * This framework is a set of functions that are commonly used in most
33 * applications.
34 *
35 * @author Iris Studios, Inc.
36 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
37 * @version $Revision$
38 * @package ISSO
39 *
40 */
41 class Functions
42 {
43 /**
44 * Framework registry object
45 * @var object
46 */
47 var $registry = null;
48
49 /**
50 * The path that is used to set cookies
51 * @var string
52 */
53 var $cookiepath = '/';
54
55 /**
56 * The domain used for cookie setting
57 * @var string
58 */
59 var $cookiedom = '';
60
61 /**
62 * The time it takes for a cookie to expire
63 * @var integer
64 */
65 var $cookieexp = 900;
66
67 /**
68 * State of the current background colour during alternation
69 * @var string
70 */
71 var $bgcolour = '';
72
73 /**
74 * Constructor
75 */
76 function __construct(&$registry)
77 {
78 $this->registry =& $registry;
79 }
80
81 /**
82 * (PHP 4) Constructor
83 */
84 function Functions(&$registry)
85 {
86 $this->__construct($registry);
87 }
88
89 /**
90 * Sets a cookie with a friendly interface
91 *
92 * @param string The name of the cookie
93 * @param string Value of the cookie
94 * @param bool Is the cookie permanent?
95 */
96 function cookie($name, $value = '', $sticky = true)
97 {
98 // expire the cookie
99 if (!$value)
100 {
101 setcookie($name, $value, time() - (2 * $this->cookieexp), $this->cookiepath, $this->cookiedom);
102 }
103 // set the cookie
104 else
105 {
106 if ($sticky)
107 {
108 $expire = time() + 60 * 60 * 24 * 365;
109 }
110 else
111 {
112 $expire = time() + $this->cookieexp;
113 }
114
115 setcookie($name, $value, $expire, $this->cookiepath, $this->cookiedom);
116 }
117 }
118
119 /**
120 * Alternate between two background colours
121 *
122 * @param string First CSS class name
123 * @param string Second CSS class name
124 */
125 function exec_swap_bg($class1 = 'alt1', $class2 = 'alt2')
126 {
127 static $count;
128
129 $this->bgcolour = ($count % 2) ? $class1 : $class2;
130 $count++;
131 }
132
133 /**
134 * Force-download a file by sending application/octetstream
135 *
136 * @param string The text of the file to be streamed
137 * @param string File name of the new file
138 * @param bool Whether or not to die after stringeaming the file
139 */
140 function download_file($file, $name, $exit = true)
141 {
142 if ($this->is_browser('ie') OR $this->is_browser('opera') OR $this->is_browser('safari'))
143 {
144 $mime = 'application/octetstream';
145 }
146 else
147 {
148 $mime = 'application/octet-stream';
149 }
150
151 header("Content-Type: $mime");
152 header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
153 header('Content-Disposition: attachment; filename="' . $name . '"');
154 header('Content-length: ' . strlen($file));
155 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
156 header('Pragma: public');
157
158 print($file);
159
160 if ($exit)
161 {
162 exit;
163 }
164 }
165
166 /**
167 * Verify that an email address is valid via regex
168 *
169 * @param string An email address
170 *
171 * @return bool Validity of the email address
172 */
173 function is_valid_email($email)
174 {
175 if (preg_match('#^[a-z0-9\.\-\+_]+?@(.*?\.)*?[a-z0-9\-_]+?\.[a-z]{2,4}$#i', $email))
176 {
177 return true;
178 }
179 else
180 {
181 return false;
182 }
183 }
184
185 /**
186 * Check a browser's user agent against a pre-determined list
187 *
188 * @param string Browser name
189 * @param string The browser's version
190 *
191 * @param mixed False if there is no match, the version if there is
192 */
193 function is_browser($check, $version = '')
194 {
195 $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
196 $browser = array();
197 $matches = array();
198
199 // -------------------------------------------------------------------
200 // -- Opera
201 // -------------------------------------------------------------------
202 # Opera/6.05 (Windows 98; U) [en]
203 # Mozilla/4.0 (compatible; MSIE 5.0; Windows 98) Opera 6.0 [en]
204 # Mozilla/5.0 (Windows 98; U) Opera 6.0 [en]
205 # Mozilla/4.0 (compatible; MSIE, 6.0; Windows 98) Opera 7.0 [en]
206 if (preg_match('#opera ([0-9\.]+)#', $useragent, $matches) !== false)
207 {
208 if (isset($matches[1]))
209 {
210 $browser['opera'] = $matches[1];
211 }
212 }
213
214 // -------------------------------------------------------------------
215 // -- Mac browser
216 // -------------------------------------------------------------------
217 if (strpos($useragent, 'mac') !== false)
218 {
219 $browser['mac'] = true;
220 }
221
222 // -------------------------------------------------------------------
223 // -- Internet explorer
224 // -------------------------------------------------------------------
225 # Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1; .NET CLR 1.0.2914)
226 # Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)
227 if (preg_match('#msie ([0-9\.]+)#', $useragent, $matches) !== false AND !isset($browser['opera']))
228 {
229 if (isset($matches[1]))
230 {
231 $browser['ie'] = $matches[1];
232 }
233 }
234
235 // -------------------------------------------------------------------
236 // -- Safari
237 // -------------------------------------------------------------------
238 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/125.4 (KHTML, like Gecko) Safari/125.9
239 if (preg_match('#safari/([0-9\.]+)#', $useragent, $matches) !== false)
240 {
241 if (isset($matches[1]))
242 {
243 $browser['safari'] = $matches[1];
244 }
245 }
246
247 // -------------------------------------------------------------------
248 // -- Konqueror
249 // -------------------------------------------------------------------
250 # Mozilla/5.0 (compatible; Konqueror/3)
251 # Mozilla/5.0 (compatible; Konqueror/3.1; i686 Linux; 20020628)
252 if (preg_match('#konqueror/([0-9\.]+)#', $useragent, $matches) !== false)
253 {
254 if (isset($matches[1]))
255 {
256 $browser['konqueror'] = $matches[1];
257 }
258 }
259
260 // -------------------------------------------------------------------
261 // -- Mozilla
262 // -------------------------------------------------------------------
263 # Mozilla/5.001 (windows; U; NT4.0; en-us) Gecko/25250101
264 # Mozilla/5.001 (Macintosh; N; PPC; ja) Gecko/25250101 MegaCorpBrowser/1.0 (MegaCorp, Inc.)
265 if (preg_match('#gecko/([0-9]+)#', $useragent, $matches) !== false AND !isset($browser['safari']))
266 {
267 if (isset($matches[1]))
268 {
269 $browser['mozilla'] = $matches[1];
270 }
271
272 // -------------------------------------------------------------------
273 // -- Firefox
274 // -------------------------------------------------------------------
275 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7) Gecko/20040628 Firefox/0.9.1
276 # Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4a) Gecko/20030423 Firebird Browser/0.6
277 if (preg_match('#(firebird|firefox)( browser)?/([0-9\.]+)#', $useragent, $matches) !== false)
278 {
279 if (isset($matches[3]))
280 {
281 $browser['firefox'] = $matches[3];
282 }
283 }
284
285 // -------------------------------------------------------------------
286 // -- Netscape
287 // -------------------------------------------------------------------
288 # Mozilla/5.0 (Macintosh; U; PPC; en-US; rv:0.9.4.1) Gecko/20020318 Netscape6/6.2.2
289 if (preg_match('#netscape([0-9].*?)?/([0-9\.]+)#', $useragent, $matches) !== false)
290 {
291 if (isset($matches[2]))
292 {
293 $browser['netscape'] = $matches[2];
294 }
295 }
296
297 // -------------------------------------------------------------------
298 // -- Camino
299 // -------------------------------------------------------------------
300 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7) Gecko/20040623 Camino/0.8
301 if (preg_match('#camino/([0-9\.]+)#', $useragent, $matches) !== false)
302 {
303 if (isset($matches[1]))
304 {
305 $browser['camino'] = $matches[1];
306 }
307 }
308 }
309
310 if (isset($browser["$check"]))
311 {
312 if ($version)
313 {
314 if ($browser["$check"] >= $version)
315 {
316 return $browser["$check"];
317 }
318 else
319 {
320 return false;
321 }
322 }
323 else
324 {
325 return $browser["$check"];
326 }
327 }
328 else
329 {
330 return false;
331 }
332 }
333
334 /**
335 * Generates a random string of random length (unless otherwise specified)
336 *
337 * @param integer Optional length
338 *
339 * @return string A random string
340 */
341 function rand($length = 0)
342 {
343 // custom high and lows
344 if (is_array($length))
345 {
346 $length = rand($length[0], $length[1]);
347 }
348 else if (!$length)
349 {
350 // Gimme a length!
351 $length = rand(20, 65);
352 }
353
354 // Number of ints in our salt
355 $intcount = rand(0, intval($length / 2));
356
357 // Number of chars
358 $charcount = $length - $intcount;
359
360 // Upper-case chars
361 $upperchars = rand(1, intval($charcount / 2));
362
363 // Lower-case chars
364 $lowerchars = $charcount - $upperchars;
365
366 // Generate ints
367 for ($i = 0; $i < $intcount; $i++)
368 {
369 $string[] = rand(0, 9);
370 }
371
372 // Generate upper chars
373 for ($i = 0; $i < $upperchars; $i++)
374 {
375 $string[] = chr(rand(65, 90));
376 }
377
378 // Generate lower chars
379 for ($i = 0; $i < $lowerchars; $i++)
380 {
381 $string[] = chr(rand(97, 122));
382 }
383
384 // Randomly key the chars
385 foreach ($string AS $char)
386 {
387 $rand = mt_rand();
388 $newstr["$rand"] = $char;
389 }
390
391 // Sort the chars by thier random assignment
392 ksort($newstr);
393
394 // Flatten the array
395 $string = '';
396 foreach ($newstr AS $char)
397 {
398 $string .= $char;
399 }
400
401 return $string;
402 }
403
404 /**
405 * Sets the current array position to be the specified key. This function should be avoided on large arrays.
406 *
407 * @param array The array whose counter is to be updated
408 * @param mixed The key of the element of the array that the counter is to be set to
409 *
410 * @return mixed Return the elelment of the array that we just put the counter to
411 */
412 function array_set_current(&$array, $key)
413 {
414 reset($array);
415 while (current($array) !== false)
416 {
417 if (key($array) == $key)
418 {
419 break;
420 }
421 next($array);
422 }
423 return current($array);
424 }
425
426 /**
427 * Calculates the microtime difference by taking a given microtime and subtracting it from the current one
428 *
429 * @param string The start microtime
430 *
431 * @return float Microtime difference
432 */
433 function fetch_microtime_diff($mtstart)
434 {
435 $mtend = microtime();
436 list ($starttime['micro'], $starttime['sec']) = explode(' ', $mtstart);
437 list ($endtime['micro'], $endtime['sec']) = explode(' ', $mtend);
438 return ($endtime['micro'] + $endtime['sec']) - ($starttime['micro'] + $starttime['sec']);
439 }
440
441 /**
442 * Fetches the extension of a file by extracting everything after the last period
443 *
444 * @param string Filename
445 *
446 * @return string The extension for the specifid file name
447 */
448 function fetch_extension($filename)
449 {
450 return strval(end(explode('.', $filename)));
451 }
452
453 /**
454 * Gets the maximum file size for attachment uploading, as specified by PHP. If no value is present, 10 MB (represented in bytes) is returned
455 *
456 * @return integer The maximum file upload size in bytes
457 */
458 function fetch_max_attachment_size()
459 {
460 if ($size = @ini_get('upload_max_filesize'))
461 {
462 if (preg_match('#[^0-9].#', $size))
463 {
464 return $size;
465 }
466 else
467 {
468 return intval($size) * 1048576;
469 }
470 }
471 else
472 {
473 return 10 * 1048576;
474 }
475 }
476
477 /**
478 * Scans a specified directory path and returns an array of all the itmes in that directory. Directories found by this are end in a "/"
479 *
480 * @param string Path to scan
481 * @param bool Whether or not to recursively scan the directories encountered
482 * @param bool Ignore files beginning with a dot
483 * @param bool Ignore 'CVS' dirctories
484 *
485 * @return array A list of all the files in the specified path
486 */
487 function scandir($path, $recurse = true, $ignoredot = true, $ignorecvs = true, $basepath = '', $unset = 1)
488 {
489 static $filelist;
490
491 if ($unset)
492 {
493 $filelist = array();
494 }
495
496 if (substr($path, (strlen($path) - 1), 1) != '/')
497 {
498 $path .= '/';
499 }
500
501 if ($handle = opendir($path))
502 {
503 while (($file = readdir($handle)) !== false)
504 {
505 $isdot = ((substr($file, 0, 1) == '.') ? true : false);
506 $isdot = (($ignoredot) ? $isdot : true);
507 $iscvs = (($file == 'CVS') ? true : false);
508 $iscvs = (($ignorecvs) ? $iscvs : true);
509 if (!$isdot AND !$iscvs)
510 {
511 if (is_dir($path . $file))
512 {
513 $filelist["$basepath"][] = "$file/";
514 if ($recurse)
515 {
516 $this->scandir("$path$file", true, $ignoredot, $ignorecvs, "$basepath$file/", 0);
517 }
518 }
519 else
520 {
521 $filelist["$basepath"][] = $file;
522 }
523 }
524 }
525 closedir($handle);
526 }
527 return $filelist;
528 }
529 }
530
531 /*=====================================================================*\
532 || ###################################################################
533 || # $HeadURL$
534 || # $Id$
535 || ###################################################################
536 \*=====================================================================*/
537 ?>