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