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