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