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