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