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