]>
src.bluestatic.org Git - isso.git/blob - functions.php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Iris Studios Shared Object Framework [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
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.
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
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 \*=====================================================================*/
23 * Globalized Functions
30 * Globalized Functions
32 * This framework is a set of functions that are commonly used in most
35 * @author Iris Studios, Inc.
36 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
44 * Framework registry object
50 * The path that is used to set cookies
53 var $cookiepath = '/';
56 * The domain used for cookie setting
62 * The time it takes for a cookie to expire
68 * State of the current background colour during alternation
73 // ###################################################################
77 function __construct(&$registry)
79 $this->registry
=& $registry;
82 // ###################################################################
86 function Functions(&$registry)
88 $this->__construct($registry);
91 // ###################################################################
93 * Sets a cookie with a friendly interface
97 * @param string The name of the cookie
98 * @param string Value of the cookie
99 * @param bool Is the cookie permanent?
101 function cookie($name, $value = '', $sticky = true)
106 setcookie($name, $value, time() - (2 * $this->cookieexp
), $this->cookiepath
, $this->cookiedom
);
113 $expire = time() +
60 * 60 * 24 * 365;
117 $expire = time() +
$this->cookieexp
;
120 setcookie($name, $value, $expire, $this->cookiepath
, $this->cookiedom
);
124 // ###################################################################
126 * Alternate between two background colours
130 * @param string First CSS class name
131 * @param string Second CSS class name
133 function exec_swap_bg($class1 = 'alt1', $class2 = 'alt2')
137 $this->bgcolour
= ($count %
2) ? $class1 : $class2;
141 // ###################################################################
143 * Force-download a file by sending application/octetstream
147 * @param string The text of the file to be streamed
148 * @param string File name of the new file
149 * @param bool Whether or not to die after stringeaming the file
151 function download_file($file, $name, $exit = true)
153 if ($this->is_browser('ie') OR $this->is_browser('opera') OR $this->is_browser('safari'))
155 $mime = 'application/octetstream';
159 $mime = 'application/octet-stream';
162 header("Content-Type: $mime");
163 header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
164 header('Content-Disposition: attachment; filename="' . $name . '"');
165 header('Content-length: ' . strlen($file));
166 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
167 header('Pragma: public');
177 // ###################################################################
179 * Verify that an email address is valid via regex
183 * @param string An email address
185 * @return bool Validity of the email address
187 function is_valid_email($email)
189 if (preg_match('#^[a-z0-9\.\-\+_]+?@(.*?\.)*?[a-z0-9\-_]+?\.[a-z]{2,4}$#i', $email))
199 // ###################################################################
201 * Check a browser's user agent against a pre-determined list
205 * @param string Browser name
206 * @param string The browser's version
208 * @param mixed False if there is no match, the version if there is
210 function is_browser($check, $version = '')
212 $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
216 // -------------------------------------------------------------------
218 // -------------------------------------------------------------------
219 # Opera/6.05 (Windows 98; U) [en]
220 # Mozilla/4.0 (compatible; MSIE 5.0; Windows 98) Opera 6.0 [en]
221 # Mozilla/5.0 (Windows 98; U) Opera 6.0 [en]
222 # Mozilla/4.0 (compatible; MSIE, 6.0; Windows 98) Opera 7.0 [en]
223 if (preg_match('#opera ([0-9\.]+)#', $useragent, $matches) !== false)
225 if (isset($matches[1]))
227 $browser['opera'] = $matches[1];
231 // -------------------------------------------------------------------
233 // -------------------------------------------------------------------
234 if (strpos($useragent, 'mac') !== false)
236 $browser['mac'] = true;
239 // -------------------------------------------------------------------
240 // -- Internet explorer
241 // -------------------------------------------------------------------
242 # Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1; .NET CLR 1.0.2914)
243 # Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)
244 if (preg_match('#msie ([0-9\.]+)#', $useragent, $matches) !== false AND !isset($browser['opera']))
246 if (isset($matches[1]))
248 $browser['ie'] = $matches[1];
252 // -------------------------------------------------------------------
254 // -------------------------------------------------------------------
255 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/125.4 (KHTML, like Gecko) Safari/125.9
256 if (preg_match('#safari/([0-9\.]+)#', $useragent, $matches) !== false)
258 if (isset($matches[1]))
260 $browser['safari'] = $matches[1];
264 // -------------------------------------------------------------------
266 // -------------------------------------------------------------------
267 # Mozilla/5.0 (compatible; Konqueror/3)
268 # Mozilla/5.0 (compatible; Konqueror/3.1; i686 Linux; 20020628)
269 if (preg_match('#konqueror/([0-9\.]+)#', $useragent, $matches) !== false)
271 if (isset($matches[1]))
273 $browser['konqueror'] = $matches[1];
277 // -------------------------------------------------------------------
279 // -------------------------------------------------------------------
280 # Mozilla/5.001 (windows; U; NT4.0; en-us) Gecko/25250101
281 # Mozilla/5.001 (Macintosh; N; PPC; ja) Gecko/25250101 MegaCorpBrowser/1.0 (MegaCorp, Inc.)
282 if (preg_match('#gecko/([0-9]+)#', $useragent, $matches) !== false AND !isset($browser['safari']))
284 if (isset($matches[1]))
286 $browser['mozilla'] = $matches[1];
289 // -------------------------------------------------------------------
291 // -------------------------------------------------------------------
292 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7) Gecko/20040628 Firefox/0.9.1
293 # Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4a) Gecko/20030423 Firebird Browser/0.6
294 if (preg_match('#(firebird|firefox)( browser)?/([0-9\.]+)#', $useragent, $matches) !== false)
296 if (isset($matches[3]))
298 $browser['firefox'] = $matches[3];
302 // -------------------------------------------------------------------
304 // -------------------------------------------------------------------
305 # Mozilla/5.0 (Macintosh; U; PPC; en-US; rv:0.9.4.1) Gecko/20020318 Netscape6/6.2.2
306 if (preg_match('#netscape([0-9].*?)?/([0-9\.]+)#', $useragent, $matches) !== false)
308 if (isset($matches[2]))
310 $browser['netscape'] = $matches[2];
314 // -------------------------------------------------------------------
316 // -------------------------------------------------------------------
317 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7) Gecko/20040623 Camino/0.8
318 if (preg_match('#camino/([0-9\.]+)#', $useragent, $matches) !== false)
320 if (isset($matches[1]))
322 $browser['camino'] = $matches[1];
327 if (isset($browser["$check"]))
331 if ($browser["$check"] >= $version)
333 return $browser["$check"];
342 return $browser["$check"];
351 // ###################################################################
353 * Generates a random string of random length (unless otherwise
358 * @param integer Optional length
360 * @return string A random string
362 function rand($length = 0)
364 // custom high and lows
365 if (is_array($length))
367 $length = rand($length[0], $length[1]);
372 $length = rand(20, 65);
375 // Number of ints in our salt
376 $intcount = rand(0, intval($length / 2));
379 $charcount = $length - $intcount;
382 $upperchars = rand(1, intval($charcount / 2));
385 $lowerchars = $charcount - $upperchars;
388 for ($i = 0; $i < $intcount; $i++)
390 $string[] = rand(0, 9);
393 // Generate upper chars
394 for ($i = 0; $i < $upperchars; $i++)
396 $string[] = chr(rand(65, 90));
399 // Generate lower chars
400 for ($i = 0; $i < $lowerchars; $i++)
402 $string[] = chr(rand(97, 122));
405 // Randomly key the chars
406 foreach ($string AS $char)
409 $newstr["$rand"] = $char;
412 // Sort the chars by thier random assignment
417 foreach ($newstr AS $char)
425 // ###################################################################
427 * Sets the current array position to be the specified key. This
428 * function should be avoided on large arrays.
432 * @param array The array whose counter is to be updated
433 * @param mixed The key of the element of the array that the counter is to be set to
435 * @return mixed Return the elelment of the array that we just put the counter to
437 function array_set_current(&$array, $key)
440 while (current($array) !== false)
442 if (key($array) == $key)
448 return current($array);
451 // ###################################################################
453 * Calculates the microtime difference by taking a given microtime and
454 * subtracting it from the current one
458 * @param string The start microtime
460 * @return float Microtime difference
462 function fetch_microtime_diff($mtstart)
464 $mtend = microtime();
465 list ($starttime['micro'], $starttime['sec']) = explode(' ', $mtstart);
466 list ($endtime['micro'], $endtime['sec']) = explode(' ', $mtend);
467 return ($endtime['micro'] +
$endtime['sec']) - ($starttime['micro'] +
$starttime['sec']);
470 // ###################################################################
472 * Fetches the extension of a file by extracting everything after the
477 * @param string Filename
479 * @return string The extension for the specifid file name
481 function fetch_extension($filename)
483 return strval(end(explode('.', $filename)));
486 // ###################################################################
488 * Gets the maximum file size for attachment uploading, as specified by
489 * PHP. If no value is present, 10 MB (represented in bytes) is
494 * @return integer The maximum file upload size in bytes
496 function fetch_max_attachment_size()
498 if ($size = @ini_get('upload_max_filesize'))
500 if (preg_match('#[^0-9].#', $size))
506 return intval($size) * 1048576;
515 // ###################################################################
517 * Scans a specified directory path and returns an array of all the
518 * itmes in that directory. Directories found by this are end in a "/"
522 * @param string Path to scan
523 * @param bool Whether or not to recursively scan the directories encountered
524 * @param bool Ignore files beginning with a dot
525 * @param bool Ignore 'CVS' dirctories
527 * @return array A list of all the files in the specified path
529 function scandir($path, $recurse = true, $ignoredot = true, $ignorecvs = true, $basepath = '', $unset = 1)
538 if (substr($path, (strlen($path) - 1), 1) != '/')
543 if ($handle = opendir($path))
545 while (($file = readdir($handle)) !== false)
547 $isdot = ((substr($file, 0, 1) == '.') ? true : false);
548 $isdot = (($ignoredot) ? $isdot : true);
549 $iscvs = (($file == 'CVS') ? true : false);
550 $iscvs = (($ignorecvs) ? $iscvs : true);
551 if (!$isdot AND !$iscvs)
553 if (is_dir($path . $file))
555 $filelist["$basepath"][] = "$file/";
558 $this->scandir("$path$file", true, $ignoredot, $ignorecvs, "$basepath$file/", 0);
563 $filelist["$basepath"][] = $file;
573 /*=====================================================================*\
574 || ###################################################################
577 || ###################################################################
578 \*=====================================================================*/