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