]>
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
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 // ###################################################################
127 * @param string Field name
129 * @return mixed Value of the field
131 function get($fieldname)
133 return $this->registry
->do_get($fieldname, 'functions');
136 // ###################################################################
138 * Sets a cookie with a friendly interface
142 * @param string The name of the cookie
143 * @param string Value of the cookie
144 * @param bool Is the cookie permanent?
146 function cookie($name, $value = '', $sticky = true)
148 $this->registry
->check_isso_fields(get_class($this));
153 setcookie($name, $value, time() - (2 * $this->cookieexp
), $this->cookiepath
, $this->cookiedom
);
160 $expire = time() +
60 * 60 * 24 * 365;
164 $expire = time() +
$this->cookieexp
;
167 setcookie($name, $value, $expire, $this->cookiepath
, $this->cookiedom
);
171 // ###################################################################
173 * Alternate between two background colours
177 * @param string First CSS class name
178 * @param string Second CSS class name
180 function exec_swap_bg($class1 = 'alt1', $class2 = 'alt2')
184 $this->bgcolour
= ($count %
2) ? $class1 : $class2;
188 // ###################################################################
190 * Force-download a file by sending application/octetstream
194 * @param string The text of the file to be streamed
195 * @param string File name of the new file
196 * @param bool Whether or not to die after stringeaming the file
198 function download_file($file, $name, $exit = true)
200 if ($this->is_browser('ie') OR $this->is_browser('opera') OR $this->is_browser('safari'))
202 $mime = 'application/octetstream';
206 $mime = 'application/octet-stream';
209 header("Content-Type: $mime");
210 header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
211 header('Content-Disposition: attachment; filename="' . $name . '"');
212 header('Content-length: ' . strlen($file));
213 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
214 header('Pragma: public');
224 // ###################################################################
226 * Verify that an email address is valid via regex
230 * @param string An email address
232 * @return bool Validity of the email address
234 function is_valid_email($email)
236 if (preg_match('#^[a-z0-9\.\-\+_]+?@(.*?\.)*?[a-z0-9\-_]+?\.[a-z]{2,4}$#i', $email))
246 // ###################################################################
248 * Check a browser's user agent against a pre-determined list
252 * @param string Browser name
253 * @param string The browser's version
255 * @param mixed False if there is no match, the version if there is
257 function is_browser($check, $version = '')
259 $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
263 // -------------------------------------------------------------------
265 // -------------------------------------------------------------------
266 # Opera/6.05 (Windows 98; U) [en]
267 # Mozilla/4.0 (compatible; MSIE 5.0; Windows 98) Opera 6.0 [en]
268 # Mozilla/5.0 (Windows 98; U) Opera 6.0 [en]
269 # Mozilla/4.0 (compatible; MSIE, 6.0; Windows 98) Opera 7.0 [en]
270 if (preg_match('#opera ([0-9\.]+)#', $useragent, $matches) !== false)
272 if (isset($matches[1]))
274 $browser['opera'] = $matches[1];
278 // -------------------------------------------------------------------
280 // -------------------------------------------------------------------
281 if (strpos($useragent, 'mac') !== false)
283 $browser['mac'] = true;
286 // -------------------------------------------------------------------
287 // -- Internet explorer
288 // -------------------------------------------------------------------
289 # Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1; .NET CLR 1.0.2914)
290 # Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)
291 if (preg_match('#msie ([0-9\.]+)#', $useragent, $matches) !== false AND !isset($browser['opera']))
293 if (isset($matches[1]))
295 $browser['ie'] = $matches[1];
299 // -------------------------------------------------------------------
301 // -------------------------------------------------------------------
302 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/125.4 (KHTML, like Gecko) Safari/125.9
303 if (preg_match('#safari/([0-9\.]+)#', $useragent, $matches) !== false)
305 if (isset($matches[1]))
307 $browser['safari'] = $matches[1];
311 // -------------------------------------------------------------------
313 // -------------------------------------------------------------------
314 # Mozilla/5.0 (compatible; Konqueror/3)
315 # Mozilla/5.0 (compatible; Konqueror/3.1; i686 Linux; 20020628)
316 if (preg_match('#konqueror/([0-9\.]+)#', $useragent, $matches) !== false)
318 if (isset($matches[1]))
320 $browser['konqueror'] = $matches[1];
324 // -------------------------------------------------------------------
326 // -------------------------------------------------------------------
327 # Mozilla/5.001 (windows; U; NT4.0; en-us) Gecko/25250101
328 # Mozilla/5.001 (Macintosh; N; PPC; ja) Gecko/25250101 MegaCorpBrowser/1.0 (MegaCorp, Inc.)
329 if (preg_match('#gecko/([0-9]+)#', $useragent, $matches) !== false AND !isset($browser['safari']))
331 if (isset($matches[1]))
333 $browser['mozilla'] = $matches[1];
336 // -------------------------------------------------------------------
338 // -------------------------------------------------------------------
339 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7) Gecko/20040628 Firefox/0.9.1
340 # Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4a) Gecko/20030423 Firebird Browser/0.6
341 if (preg_match('#(firebird|firefox)( browser)?/([0-9\.]+)#', $useragent, $matches) !== false)
343 if (isset($matches[3]))
345 $browser['firefox'] = $matches[3];
349 // -------------------------------------------------------------------
351 // -------------------------------------------------------------------
352 # Mozilla/5.0 (Macintosh; U; PPC; en-US; rv:0.9.4.1) Gecko/20020318 Netscape6/6.2.2
353 if (preg_match('#netscape([0-9].*?)?/([0-9\.]+)#', $useragent, $matches) !== false)
355 if (isset($matches[2]))
357 $browser['netscape'] = $matches[2];
361 // -------------------------------------------------------------------
363 // -------------------------------------------------------------------
364 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7) Gecko/20040623 Camino/0.8
365 if (preg_match('#camino/([0-9\.]+)#', $useragent, $matches) !== false)
367 if (isset($matches[1]))
369 $browser['camino'] = $matches[1];
374 if (isset($browser["$check"]))
378 if ($browser["$check"] >= $version)
380 return $browser["$check"];
389 return $browser["$check"];
398 // ###################################################################
400 * Generates a random string of random length (unless otherwise
405 * @param integer Optional length
407 * @return string A random string
409 function rand($length = 0)
411 // custom high and lows
412 if (is_array($length))
414 $length = rand($length[0], $length[1]);
419 $length = rand(20, 65);
422 // Number of ints in our salt
423 $intcount = rand(0, intval($length / 2));
426 $charcount = $length - $intcount;
429 $upperchars = rand(1, intval($charcount / 2));
432 $lowerchars = $charcount - $upperchars;
435 for ($i = 0; $i < $intcount; $i++)
437 $string[] = rand(0, 9);
440 // Generate upper chars
441 for ($i = 0; $i < $upperchars; $i++)
443 $string[] = chr(rand(65, 90));
446 // Generate lower chars
447 for ($i = 0; $i < $lowerchars; $i++)
449 $string[] = chr(rand(97, 122));
452 // Randomly key the chars
453 foreach ($string AS $char)
456 $newstr["$rand"] = $char;
459 // Sort the chars by thier random assignment
464 foreach ($newstr AS $char)
472 // ###################################################################
474 * Sets the current array position to be the specified key. This
475 * function should be avoided on large arrays.
479 * @param array The array whose counter is to be updated
480 * @param mixed The key of the element of the array that the counter is to be set to
482 * @return mixed Return the elelment of the array that we just put the counter to
484 function array_set_current(&$array, $key)
487 while (current($array) !== false)
489 if (key($array) == $key)
495 return current($array);
498 // ###################################################################
500 * Calculates the microtime difference by taking a given microtime and
501 * subtracting it from the current one
505 * @param string The start microtime
507 * @return float Microtime difference
509 function fetch_microtime_diff($mtstart)
511 $mtend = microtime();
512 list ($starttime['micro'], $starttime['sec']) = explode(' ', $mtstart);
513 list ($endtime['micro'], $endtime['sec']) = explode(' ', $mtend);
514 return ($endtime['micro'] +
$endtime['sec']) - ($starttime['micro'] +
$starttime['sec']);
517 // ###################################################################
519 * Fetches the extension of a file by extracting everything after the
524 * @param string Filename
526 * @return string The extension for the specifid file name
528 function fetch_extension($filename)
530 return strval(end(explode('.', $filename)));
533 // ###################################################################
535 * Gets the maximum file size for attachment uploading, as specified by
536 * PHP. If no value is present, 10 MB (represented in bytes) is
541 * @return integer The maximum file upload size in bytes
543 function fetch_max_attachment_size()
545 if ($size = @ini_get('upload_max_filesize'))
547 if (preg_match('#[^0-9].#', $size))
553 return intval($size) * 1048576;
562 // ###################################################################
564 * Scans a specified directory path and returns an array of all the
565 * items in that directory. Directories found by this are end in a "/"
569 * @param string Path to scan
570 * @param bool Whether or not to recursively scan the directories encountered
571 * @param bool Ignore files beginning with a dot
572 * @param bool Ignore 'CVS' dirctories
574 * @return array A list of all the files in the specified path
576 function scandir($path, $recurse = true, $ignoredot = true, $ignorecvs = true, $basepath = '', $unset = 1)
585 if (substr($path, (strlen($path) - 1), 1) != '/')
590 if ($handle = opendir($path))
592 while (($file = readdir($handle)) !== false)
594 $isdot = ((substr($file, 0, 1) == '.') ? true : false);
595 $isdot = (($ignoredot) ? $isdot : true);
596 $iscvs = (($file == 'CVS') ? true : false);
597 $iscvs = (($ignorecvs) ? $iscvs : true);
598 if (!$isdot AND !$iscvs)
600 if (is_dir($path . $file))
602 $filelist["$basepath"][] = "$file/";
605 $this->scandir("$path$file", true, $ignoredot, $ignorecvs, "$basepath$file/", 0);
610 $filelist["$basepath"][] = $file;
619 // ###################################################################
621 * Changes line breaks into one format
626 * @param string New line break (default is UNIX format)
628 * @return string Text with one type of line break
630 function convert_line_breaks($text, $convert_to = "\n
")
633 $text = str_replace(array("\r\n
", "\r
", "\n
"), "\n
", $text);
634 $text = str_replace("\n
", $convert_to, $text);
639 /*=====================================================================*\
640 || ###################################################################
643 || ###################################################################
644 \*=====================================================================*/