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