]>
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
51 * The path that is used to set cookies
53 * @access pprivateublic
55 var $cookiepath = '/';
58 * The domain used for cookie setting
65 * The time it takes for a cookie to expire
72 * State of the current background colour during alternation
79 * Fields array that is used in this module
84 'cookiepath' => array(REQ_YES
, null, true),
85 'cookiedom' => array(REQ_YES
, null, true),
86 'cookieexp' => array(REQ_YES
, null, true)
89 // ###################################################################
93 function __construct(&$registry)
95 $this->registry
=& $registry;
98 // ###################################################################
100 * (PHP 4) Constructor
102 function Functions(&$registry)
104 $this->__construct($registry);
107 // ###################################################################
113 * @param string Field name
114 * @param mixed Value of the field
116 function set($name, $value)
118 $this->registry
->do_set($name, $value, 'functions');
121 // ###################################################################
123 * Sets a cookie with a friendly interface
127 * @param string The name of the cookie
128 * @param string Value of the cookie
129 * @param bool Is the cookie permanent?
131 function cookie($name, $value = '', $sticky = true)
136 setcookie($name, $value, time() - (2 * $this->cookieexp
), $this->cookiepath
, $this->cookiedom
);
143 $expire = time() +
60 * 60 * 24 * 365;
147 $expire = time() +
$this->cookieexp
;
150 setcookie($name, $value, $expire, $this->cookiepath
, $this->cookiedom
);
154 // ###################################################################
156 * Alternate between two background colours
160 * @param string First CSS class name
161 * @param string Second CSS class name
163 function exec_swap_bg($class1 = 'alt1', $class2 = 'alt2')
167 $this->bgcolour
= ($count %
2) ? $class1 : $class2;
171 // ###################################################################
173 * Force-download a file by sending application/octetstream
177 * @param string The text of the file to be streamed
178 * @param string File name of the new file
179 * @param bool Whether or not to die after stringeaming the file
181 function download_file($file, $name, $exit = true)
183 if ($this->is_browser('ie') OR $this->is_browser('opera') OR $this->is_browser('safari'))
185 $mime = 'application/octetstream';
189 $mime = 'application/octet-stream';
192 header("Content-Type: $mime");
193 header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
194 header('Content-Disposition: attachment; filename="' . $name . '"');
195 header('Content-length: ' . strlen($file));
196 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
197 header('Pragma: public');
207 // ###################################################################
209 * Verify that an email address is valid via regex
213 * @param string An email address
215 * @return bool Validity of the email address
217 function is_valid_email($email)
219 if (preg_match('#^[a-z0-9\.\-\+_]+?@(.*?\.)*?[a-z0-9\-_]+?\.[a-z]{2,4}$#i', $email))
229 // ###################################################################
231 * Check a browser's user agent against a pre-determined list
235 * @param string Browser name
236 * @param string The browser's version
238 * @param mixed False if there is no match, the version if there is
240 function is_browser($check, $version = '')
242 $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
246 // -------------------------------------------------------------------
248 // -------------------------------------------------------------------
249 # Opera/6.05 (Windows 98; U) [en]
250 # Mozilla/4.0 (compatible; MSIE 5.0; Windows 98) Opera 6.0 [en]
251 # Mozilla/5.0 (Windows 98; U) Opera 6.0 [en]
252 # Mozilla/4.0 (compatible; MSIE, 6.0; Windows 98) Opera 7.0 [en]
253 if (preg_match('#opera ([0-9\.]+)#', $useragent, $matches) !== false)
255 if (isset($matches[1]))
257 $browser['opera'] = $matches[1];
261 // -------------------------------------------------------------------
263 // -------------------------------------------------------------------
264 if (strpos($useragent, 'mac') !== false)
266 $browser['mac'] = true;
269 // -------------------------------------------------------------------
270 // -- Internet explorer
271 // -------------------------------------------------------------------
272 # Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1; .NET CLR 1.0.2914)
273 # Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)
274 if (preg_match('#msie ([0-9\.]+)#', $useragent, $matches) !== false AND !isset($browser['opera']))
276 if (isset($matches[1]))
278 $browser['ie'] = $matches[1];
282 // -------------------------------------------------------------------
284 // -------------------------------------------------------------------
285 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/125.4 (KHTML, like Gecko) Safari/125.9
286 if (preg_match('#safari/([0-9\.]+)#', $useragent, $matches) !== false)
288 if (isset($matches[1]))
290 $browser['safari'] = $matches[1];
294 // -------------------------------------------------------------------
296 // -------------------------------------------------------------------
297 # Mozilla/5.0 (compatible; Konqueror/3)
298 # Mozilla/5.0 (compatible; Konqueror/3.1; i686 Linux; 20020628)
299 if (preg_match('#konqueror/([0-9\.]+)#', $useragent, $matches) !== false)
301 if (isset($matches[1]))
303 $browser['konqueror'] = $matches[1];
307 // -------------------------------------------------------------------
309 // -------------------------------------------------------------------
310 # Mozilla/5.001 (windows; U; NT4.0; en-us) Gecko/25250101
311 # Mozilla/5.001 (Macintosh; N; PPC; ja) Gecko/25250101 MegaCorpBrowser/1.0 (MegaCorp, Inc.)
312 if (preg_match('#gecko/([0-9]+)#', $useragent, $matches) !== false AND !isset($browser['safari']))
314 if (isset($matches[1]))
316 $browser['mozilla'] = $matches[1];
319 // -------------------------------------------------------------------
321 // -------------------------------------------------------------------
322 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7) Gecko/20040628 Firefox/0.9.1
323 # Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4a) Gecko/20030423 Firebird Browser/0.6
324 if (preg_match('#(firebird|firefox)( browser)?/([0-9\.]+)#', $useragent, $matches) !== false)
326 if (isset($matches[3]))
328 $browser['firefox'] = $matches[3];
332 // -------------------------------------------------------------------
334 // -------------------------------------------------------------------
335 # Mozilla/5.0 (Macintosh; U; PPC; en-US; rv:0.9.4.1) Gecko/20020318 Netscape6/6.2.2
336 if (preg_match('#netscape([0-9].*?)?/([0-9\.]+)#', $useragent, $matches) !== false)
338 if (isset($matches[2]))
340 $browser['netscape'] = $matches[2];
344 // -------------------------------------------------------------------
346 // -------------------------------------------------------------------
347 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7) Gecko/20040623 Camino/0.8
348 if (preg_match('#camino/([0-9\.]+)#', $useragent, $matches) !== false)
350 if (isset($matches[1]))
352 $browser['camino'] = $matches[1];
357 if (isset($browser["$check"]))
361 if ($browser["$check"] >= $version)
363 return $browser["$check"];
372 return $browser["$check"];
381 // ###################################################################
383 * Generates a random string of random length (unless otherwise
388 * @param integer Optional length
390 * @return string A random string
392 function rand($length = 0)
394 // custom high and lows
395 if (is_array($length))
397 $length = rand($length[0], $length[1]);
402 $length = rand(20, 65);
405 // Number of ints in our salt
406 $intcount = rand(0, intval($length / 2));
409 $charcount = $length - $intcount;
412 $upperchars = rand(1, intval($charcount / 2));
415 $lowerchars = $charcount - $upperchars;
418 for ($i = 0; $i < $intcount; $i++)
420 $string[] = rand(0, 9);
423 // Generate upper chars
424 for ($i = 0; $i < $upperchars; $i++)
426 $string[] = chr(rand(65, 90));
429 // Generate lower chars
430 for ($i = 0; $i < $lowerchars; $i++)
432 $string[] = chr(rand(97, 122));
435 // Randomly key the chars
436 foreach ($string AS $char)
439 $newstr["$rand"] = $char;
442 // Sort the chars by thier random assignment
447 foreach ($newstr AS $char)
455 // ###################################################################
457 * Sets the current array position to be the specified key. This
458 * function should be avoided on large arrays.
462 * @param array The array whose counter is to be updated
463 * @param mixed The key of the element of the array that the counter is to be set to
465 * @return mixed Return the elelment of the array that we just put the counter to
467 function array_set_current(&$array, $key)
470 while (current($array) !== false)
472 if (key($array) == $key)
478 return current($array);
481 // ###################################################################
483 * Calculates the microtime difference by taking a given microtime and
484 * subtracting it from the current one
488 * @param string The start microtime
490 * @return float Microtime difference
492 function fetch_microtime_diff($mtstart)
494 $mtend = microtime();
495 list ($starttime['micro'], $starttime['sec']) = explode(' ', $mtstart);
496 list ($endtime['micro'], $endtime['sec']) = explode(' ', $mtend);
497 return ($endtime['micro'] +
$endtime['sec']) - ($starttime['micro'] +
$starttime['sec']);
500 // ###################################################################
502 * Fetches the extension of a file by extracting everything after the
507 * @param string Filename
509 * @return string The extension for the specifid file name
511 function fetch_extension($filename)
513 return strval(end(explode('.', $filename)));
516 // ###################################################################
518 * Gets the maximum file size for attachment uploading, as specified by
519 * PHP. If no value is present, 10 MB (represented in bytes) is
524 * @return integer The maximum file upload size in bytes
526 function fetch_max_attachment_size()
528 if ($size = @ini_get('upload_max_filesize'))
530 if (preg_match('#[^0-9].#', $size))
536 return intval($size) * 1048576;
545 // ###################################################################
547 * Scans a specified directory path and returns an array of all the
548 * items in that directory. Directories found by this are end in a "/"
552 * @param string Path to scan
553 * @param bool Whether or not to recursively scan the directories encountered
554 * @param bool Ignore files beginning with a dot
555 * @param bool Ignore 'CVS' dirctories
557 * @return array A list of all the files in the specified path
559 function scandir($path, $recurse = true, $ignoredot = true, $ignorecvs = true, $basepath = '', $unset = 1)
568 if (substr($path, (strlen($path) - 1), 1) != '/')
573 if ($handle = opendir($path))
575 while (($file = readdir($handle)) !== false)
577 $isdot = ((substr($file, 0, 1) == '.') ? true : false);
578 $isdot = (($ignoredot) ? $isdot : true);
579 $iscvs = (($file == 'CVS') ? true : false);
580 $iscvs = (($ignorecvs) ? $iscvs : true);
581 if (!$isdot AND !$iscvs)
583 if (is_dir($path . $file))
585 $filelist["$basepath"][] = "$file/";
588 $this->scandir("$path$file", true, $ignoredot, $ignorecvs, "$basepath$file/", 0);
593 $filelist["$basepath"][] = $file;
603 /*=====================================================================*\
604 || ###################################################################
607 || ###################################################################
608 \*=====================================================================*/