]>
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
116 function download_file($file, $name)
118 if ($this->is_browser('ie') OR $this->is_browser('opera') OR $this->is_browser('safari'))
120 $mime = 'application/octetstream';
124 $mime = 'application/octet-stream';
127 header("Content-Type: $mime");
128 header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
129 header('Content-Disposition: attachment; filename="' . $name . '"');
130 header('Content-length: ' . strlen($file));
131 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
132 header('Pragma: public');
139 * Verify that an email address is valid via regex
141 * @param str An email address
143 * @return bool Validity of the email address
145 function is_valid_email($email)
147 if (preg_match('#^[a-z0-9\.\-\+_]+?@(.*?\.)*?[a-z0-9\-_]+?\.[a-z]{2,4}$#i', $email))
158 * Check a browser's user agent against a pre-determined list
160 * @param str Browser name
161 * @param str The browser's version
163 * @param mixed False if there is no match, the version if there is
165 function is_browser($check, $version = '')
167 $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
171 // -------------------------------------------------------------------
173 // -------------------------------------------------------------------
174 # Opera/6.05 (Windows 98; U) [en]
175 # Mozilla/4.0 (compatible; MSIE 5.0; Windows 98) Opera 6.0 [en]
176 # Mozilla/5.0 (Windows 98; U) Opera 6.0 [en]
177 # Mozilla/4.0 (compatible; MSIE, 6.0; Windows 98) Opera 7.0 [en]
178 if (preg_match('#opera ([0-9\.]+)#', $useragent, $matches) !== false)
180 if (isset($matches[1]))
182 $browser['opera'] = $matches[1];
186 // -------------------------------------------------------------------
188 // -------------------------------------------------------------------
189 if (strpos($useragent, 'mac') !== false)
191 $browser['mac'] = true;
194 // -------------------------------------------------------------------
195 // -- Internet explorer
196 // -------------------------------------------------------------------
197 # Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1; .NET CLR 1.0.2914)
198 # Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)
199 if (preg_match('#msie ([0-9\.]+)#', $useragent, $matches) !== false AND !isset($browser['opera']))
201 if (isset($matches[1]))
203 $browser['ie'] = $matches[1];
207 // -------------------------------------------------------------------
209 // -------------------------------------------------------------------
210 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/125.4 (KHTML, like Gecko) Safari/125.9
211 if (preg_match('#safari/([0-9\.]+)#', $useragent, $matches) !== false)
213 if (isset($matches[1]))
215 $browser['safari'] = $matches[1];
219 // -------------------------------------------------------------------
221 // -------------------------------------------------------------------
222 # Mozilla/5.0 (compatible; Konqueror/3)
223 # Mozilla/5.0 (compatible; Konqueror/3.1; i686 Linux; 20020628)
224 if (preg_match('#konqueror/([0-9\.]+)#', $useragent, $matches) !== false)
226 if (isset($matches[1]))
228 $browser['konqueror'] = $matches[1];
232 // -------------------------------------------------------------------
234 // -------------------------------------------------------------------
235 # Mozilla/5.001 (windows; U; NT4.0; en-us) Gecko/25250101
236 # Mozilla/5.001 (Macintosh; N; PPC; ja) Gecko/25250101 MegaCorpBrowser/1.0 (MegaCorp, Inc.)
237 if (preg_match('#gecko/([0-9]+)#', $useragent, $matches) !== false AND !isset($browser['safari']))
239 if (isset($matches[1]))
241 $browser['mozilla'] = $matches[1];
244 // -------------------------------------------------------------------
246 // -------------------------------------------------------------------
247 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7) Gecko/20040628 Firefox/0.9.1
248 # Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4a) Gecko/20030423 Firebird Browser/0.6
249 if (preg_match('#(firebird|firefox)( browser)?/([0-9\.]+)#', $useragent, $matches) !== false)
251 if (isset($matches[3]))
253 $browser['firefox'] = $matches[3];
257 // -------------------------------------------------------------------
259 // -------------------------------------------------------------------
260 # Mozilla/5.0 (Macintosh; U; PPC; en-US; rv:0.9.4.1) Gecko/20020318 Netscape6/6.2.2
261 if (preg_match('#netscape([0-9].*?)?/([0-9\.]+)#', $useragent, $matches) !== false)
263 if (isset($matches[2]))
265 $browser['netscape'] = $matches[2];
269 // -------------------------------------------------------------------
271 // -------------------------------------------------------------------
272 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7) Gecko/20040623 Camino/0.8
273 if (preg_match('#camino/([0-9\.]+)#', $useragent, $matches) !== false)
275 if (isset($matches[1]))
277 $browser['camino'] = $matches[1];
282 if (isset($browser["$check"]))
286 if ($browser["$check"] >= $version)
288 return $browser["$check"];
297 return $browser["$check"];
307 * Generates a random string of random length (unless otherwise specified)
309 * @param int Optional length
311 * @return str A random string
313 function rand($length = 0)
315 // custom high and lows
316 if (is_array($length))
318 $length = rand($length[0], $length[1]);
323 $length = rand(20, 65);
326 // Number of ints in our salt
327 $intcount = rand(0, intval($length / 2));
330 $charcount = $length - $intcount;
333 $upperchars = rand(1, intval($charcount / 2));
336 $lowerchars = $charcount - $upperchars;
339 for ($i = 0; $i < $intcount; $i++)
341 $string[] = rand(0, 9);
344 // Generate upper chars
345 for ($i = 0; $i < $upperchars; $i++)
347 $string[] = chr(rand(65, 90));
350 // Generate lower chars
351 for ($i = 0; $i < $lowerchars; $i++)
353 $string[] = chr(rand(97, 122));
356 // Randomly key the chars
357 foreach ($string AS $char)
360 $newstr["$rand"] = $char;
363 // Sort the chars by thier random assignment
368 foreach ($newstr AS $char)
377 * Sets the current array position to be the specified key. This function should be avoided on large arrays.
379 * @param array The array whose counter is to be updated
380 * @param mixed The key of the element of the array that the counter is to be set to
382 * @return mixed Return the elelment of the array that we just put the counter to
384 function array_set_current(&$array, $key)
387 while (current($array) !== false)
389 if (key($array) == $key)
395 return current($array);
399 * Calculates the microtime difference by taking a given microtime and subtracting it from the current one
401 * @param str The start microtime
403 * @return float Microtime difference
405 function fetch_microtime_diff($mtstart)
407 $mtend = microtime();
408 list ($starttime['micro'], $starttime['sec']) = explode(' ', $mtstart);
409 list ($endtime['micro'], $endtime['sec']) = explode(' ', $mtend);
410 return ($endtime['micro'] +
$endtime['sec']) - ($starttime['micro'] +
$starttime['sec']);
414 * Fetches the extension of a file by extracting everything after the last period
416 * @param str Filename
418 * @return str The extension for the specifid file name
420 function fetch_extension($filename)
422 return strval(end(explode('.', $filename)));
426 * Gets the maximum file size for attachment uploading, as specified by PHP. If no value is present, 10 MB (represented in bytes) is returned
428 * @return int The maximum file upload size in bytes
430 function fetch_max_attachment_size()
432 if ($size = @ini_get('upload_max_filesize'))
434 if (preg_match('#[^0-9].#', $size))
440 return intval($size) * 1048576;
450 * Scans a specified directory path and returns an array of all the itmes in that directory. Directories found by this are end in a "/"
452 * @param str Path to scan
453 * @param bool Whether or not to recursively scan the directories encountered
454 * @param bool Ignore files beginning with a dot
455 * @param bool Ignore 'CVS' dirctories
457 * @return array A list of all the files in the specified path
459 function scandir($path, $recurse = true, $ignoredot = true, $ignorecvs = true, $basepath = '', $unset = 1)
468 if (substr($path, (strlen($path) - 1), 1) != '/')
473 if ($handle = opendir($path))
475 while (($file = readdir($handle)) !== false)
477 $isdot = ((substr($file, 0, 1) == '.') ? true : false);
478 $isdot = (($ignoredot) ? $isdot : true);
479 $iscvs = (($file == 'CVS') ? true : false);
480 $iscvs = (($ignorecvs) ? $iscvs : true);
481 if (!$isdot AND !$iscvs)
483 if (is_dir($path . $file))
485 $filelist["$basepath"][] = "$file/";
488 $this->scandir("$path$file", true, $ignoredot, $ignorecvs, "$basepath$file/", 0);
493 $filelist["$basepath"][] = $file;
503 /*=====================================================================*\
504 || ###################################################################
507 || ###################################################################
508 \*=====================================================================*/