]>
src.bluestatic.org Git - isso.git/blob - functions.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 \*=====================================================================*/
14 * Globalized Functions
20 $OBJECT = 'Core Functions';
25 * Globalized Functions
27 * This framework is a set of functions that are commonly used in most
30 * @author Iris Studios, Inc.
31 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
39 * The path that is used to set cookies
43 var $cookiepath = '/';
46 * The domain used for cookie setting
53 * The time it takes for a cookie to expire
60 * State of the current background colour during alternation
67 * Sets a cookie with a friendly interface
69 * @param str The name of the cookie
70 * @param str Value of the cookie
71 * @param bool Is the cookie permanent?
73 function cookie($name, $value = '', $sticky = true)
78 setcookie($name, $value, time() - (2 * $this->cookieexp
), $this->cookiepath
, $this->cookiedom
);
85 $expire = time() +
60 * 60 * 24 * 365;
89 $expire = time() +
$this->cookieexp
;
92 setcookie($name, $value, $expire, $this->cookiepath
, $this->cookiedom
);
97 * Alternate between two background colours
99 * @param str First CSS class name
100 * @param str Second CSS class name
102 function exec_swap_bg($class1 = 'alt1', $class2 = 'alt2')
106 $this->bgcolour
= ($count %
2) ? $class1 : $class2;
111 * Force-download a file by sending application/octetstream
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
117 function download_file($file, $name, $exit = true)
119 if ($this->is_browser('ie') OR $this->is_browser('opera') OR $this->is_browser('safari'))
121 $mime = 'application/octetstream';
125 $mime = 'application/octet-stream';
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');
144 * Verify that an email address is valid via regex
146 * @param str An email address
148 * @return bool Validity of the email address
150 function is_valid_email($email)
152 if (preg_match('#^[a-z0-9\.\-\+_]+?@(.*?\.)*?[a-z0-9\-_]+?\.[a-z]{2,4}$#i', $email))
163 * Check a browser's user agent against a pre-determined list
165 * @param str Browser name
166 * @param str The browser's version
168 * @param mixed False if there is no match, the version if there is
170 function is_browser($check, $version = '')
172 $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
176 // -------------------------------------------------------------------
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)
185 if (isset($matches[1]))
187 $browser['opera'] = $matches[1];
191 // -------------------------------------------------------------------
193 // -------------------------------------------------------------------
194 if (strpos($useragent, 'mac') !== false)
196 $browser['mac'] = true;
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']))
206 if (isset($matches[1]))
208 $browser['ie'] = $matches[1];
212 // -------------------------------------------------------------------
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)
218 if (isset($matches[1]))
220 $browser['safari'] = $matches[1];
224 // -------------------------------------------------------------------
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)
231 if (isset($matches[1]))
233 $browser['konqueror'] = $matches[1];
237 // -------------------------------------------------------------------
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']))
244 if (isset($matches[1]))
246 $browser['mozilla'] = $matches[1];
249 // -------------------------------------------------------------------
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)
256 if (isset($matches[3]))
258 $browser['firefox'] = $matches[3];
262 // -------------------------------------------------------------------
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)
268 if (isset($matches[2]))
270 $browser['netscape'] = $matches[2];
274 // -------------------------------------------------------------------
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)
280 if (isset($matches[1]))
282 $browser['camino'] = $matches[1];
287 if (isset($browser["$check"]))
291 if ($browser["$check"] >= $version)
293 return $browser["$check"];
302 return $browser["$check"];
312 * Generates a random string of random length (unless otherwise specified)
314 * @param int Optional length
316 * @return str A random string
318 function rand($length = 0)
320 // custom high and lows
321 if (is_array($length))
323 $length = rand($length[0], $length[1]);
328 $length = rand(20, 65);
331 // Number of ints in our salt
332 $intcount = rand(0, intval($length / 2));
335 $charcount = $length - $intcount;
338 $upperchars = rand(1, intval($charcount / 2));
341 $lowerchars = $charcount - $upperchars;
344 for ($i = 0; $i < $intcount; $i++)
346 $string[] = rand(0, 9);
349 // Generate upper chars
350 for ($i = 0; $i < $upperchars; $i++)
352 $string[] = chr(rand(65, 90));
355 // Generate lower chars
356 for ($i = 0; $i < $lowerchars; $i++)
358 $string[] = chr(rand(97, 122));
361 // Randomly key the chars
362 foreach ($string AS $char)
365 $newstr["$rand"] = $char;
368 // Sort the chars by thier random assignment
373 foreach ($newstr AS $char)
382 * Sets the current array position to be the specified key. This function should be avoided on large arrays.
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
387 * @return mixed Return the elelment of the array that we just put the counter to
389 function array_set_current(&$array, $key)
392 while (current($array) !== false)
394 if (key($array) == $key)
400 return current($array);
404 * Calculates the microtime difference by taking a given microtime and subtracting it from the current one
406 * @param str The start microtime
408 * @return float Microtime difference
410 function fetch_microtime_diff($mtstart)
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']);
419 * Fetches the extension of a file by extracting everything after the last period
421 * @param str Filename
423 * @return str The extension for the specifid file name
425 function fetch_extension($filename)
427 return strval(end(explode('.', $filename)));
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
433 * @return int The maximum file upload size in bytes
435 function fetch_max_attachment_size()
437 if ($size = @ini_get('upload_max_filesize'))
439 if (preg_match('#[^0-9].#', $size))
445 return intval($size) * 1048576;
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 "/"
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
462 * @return array A list of all the files in the specified path
464 function scandir($path, $recurse = true, $ignoredot = true, $ignorecvs = true, $basepath = '', $unset = 1)
473 if (substr($path, (strlen($path) - 1), 1) != '/')
478 if ($handle = opendir($path))
480 while (($file = readdir($handle)) !== false)
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)
488 if (is_dir($path . $file))
490 $filelist["$basepath"][] = "$file/";
493 $this->scandir("$path$file", true, $ignoredot, $ignorecvs, "$basepath$file/", 0);
498 $filelist["$basepath"][] = $file;
508 /*=====================================================================*\
509 || ###################################################################
512 || ###################################################################
513 \*=====================================================================*/