]>
src.bluestatic.org Git - isso.git/blob - functions.php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework [#]issoversion[#]
5 || # Copyright ©2002-[#]year[#] Blue Static
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
36 * @copyright Copyright ©2002 - [#]year[#], Blue Static
44 * Framework registry object
47 private $registry = null;
50 * The path that is used to set cookies
53 private $cookiepath = '/';
56 * The domain used for cookie setting
59 private $cookiedom = '';
62 * The time it takes for a cookie to expire
65 private $cookieexp = 900;
68 * State of the current background colour during alternation
71 public $bgcolour = '';
74 * Fields array that is used in this module
77 private $fields = array(
78 'cookiepath' => array(REQ_YES
, null, true),
79 'cookiedom' => array(REQ_YES
, null, true),
80 'cookieexp' => array(REQ_YES
, null, true)
83 // ###################################################################
87 public function __construct(&$registry)
89 $this->registry
=& $registry;
92 // ###################################################################
96 * @param string Field name
97 * @param mixed Value of the field
99 public function set($name, $value)
101 $this->registry
->do_set($name, $value, 'functions');
104 // ###################################################################
108 * @param string Field name
110 * @return mixed Value of the field
112 public function get($fieldname)
114 return $this->registry
->do_get($fieldname, 'functions');
117 // ###################################################################
119 * Sets a cookie with a friendly interface
121 * @param string The name of the cookie
122 * @param string Value of the cookie
123 * @param bool Is the cookie permanent?
125 public function cookie($name, $value = '', $sticky = true)
127 $this->registry
->check_isso_fields(get_class($this));
132 setcookie($name, $value, time() - (2 * $this->cookieexp
), $this->cookiepath
, $this->cookiedom
);
139 $expire = time() +
60 * 60 * 24 * 365;
143 $expire = time() +
$this->cookieexp
;
146 setcookie($name, $value, $expire, $this->cookiepath
, $this->cookiedom
);
150 // ###################################################################
152 * Alternate between two background colours
154 * @param string First CSS class name
155 * @param string Second CSS class name
157 public function exec_swap_bg($class1 = 'alt1', $class2 = 'alt2')
161 $this->bgcolour
= ($count %
2) ? $class1 : $class2;
165 // ###################################################################
167 * Force-download a file by sending application/octetstream
169 * @param string The text of the file to be streamed
170 * @param string File name of the new file
171 * @param bool Whether or not to die after stringeaming the file
173 public function download_file($file, $name, $exit = true)
175 if ($this->is_browser('ie') OR $this->is_browser('opera') OR $this->is_browser('safari'))
177 $mime = 'application/octetstream';
181 $mime = 'application/octet-stream';
184 header("Content-Type: $mime");
185 header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
186 header('Content-Disposition: attachment; filename="' . $name . '"');
187 header('Content-length: ' . strlen($file));
188 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
189 header('Pragma: public');
199 // ###################################################################
201 * Verify that an email address is valid via regex
203 * @param string An email address
205 * @return bool Validity of the email address
207 public function is_valid_email($email)
209 if (preg_match('#^[a-z0-9\.\-\+_]+?@(.*?\.)*?[a-z0-9\-_]+?\.[a-z]{2,4}$#i', $email))
219 // ###################################################################
221 * Check a browser's user agent against a pre-determined list
223 * @param string Browser name
224 * @param string The browser's version
226 * @param mixed False if there is no match, the version if there is
228 public function is_browser($check, $version = '')
230 $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
234 // -------------------------------------------------------------------
236 // -------------------------------------------------------------------
237 # Opera/6.05 (Windows 98; U) [en]
238 # Mozilla/4.0 (compatible; MSIE 5.0; Windows 98) Opera 6.0 [en]
239 # Mozilla/5.0 (Windows 98; U) Opera 6.0 [en]
240 # Mozilla/4.0 (compatible; MSIE, 6.0; Windows 98) Opera 7.0 [en]
241 if (preg_match('#opera ([0-9\.]+)#', $useragent, $matches) !== false)
243 if (isset($matches[1]))
245 $browser['opera'] = $matches[1];
249 // -------------------------------------------------------------------
251 // -------------------------------------------------------------------
252 if (strpos($useragent, 'mac') !== false)
254 $browser['mac'] = true;
257 // -------------------------------------------------------------------
258 // -- Internet explorer
259 // -------------------------------------------------------------------
260 # Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1; .NET CLR 1.0.2914)
261 # Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)
262 if (preg_match('#msie ([0-9\.]+)#', $useragent, $matches) !== false AND !isset($browser['opera']))
264 if (isset($matches[1]))
266 $browser['ie'] = $matches[1];
270 // -------------------------------------------------------------------
272 // -------------------------------------------------------------------
273 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/125.4 (KHTML, like Gecko) Safari/125.9
274 if (preg_match('#safari/([0-9\.]+)#', $useragent, $matches) !== false)
276 if (isset($matches[1]))
278 $browser['safari'] = $matches[1];
282 // -------------------------------------------------------------------
284 // -------------------------------------------------------------------
285 # Mozilla/5.0 (compatible; Konqueror/3)
286 # Mozilla/5.0 (compatible; Konqueror/3.1; i686 Linux; 20020628)
287 if (preg_match('#konqueror/([0-9\.]+)#', $useragent, $matches) !== false)
289 if (isset($matches[1]))
291 $browser['konqueror'] = $matches[1];
295 // -------------------------------------------------------------------
297 // -------------------------------------------------------------------
298 # Mozilla/5.001 (windows; U; NT4.0; en-us) Gecko/25250101
299 # Mozilla/5.001 (Macintosh; N; PPC; ja) Gecko/25250101 MegaCorpBrowser/1.0 (MegaCorp, Inc.)
300 if (preg_match('#gecko/([0-9]+)#', $useragent, $matches) !== false AND !isset($browser['safari']))
302 if (isset($matches[1]))
304 $browser['mozilla'] = $matches[1];
307 // -------------------------------------------------------------------
309 // -------------------------------------------------------------------
310 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7) Gecko/20040628 Firefox/0.9.1
311 # Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4a) Gecko/20030423 Firebird Browser/0.6
312 if (preg_match('#(firebird|firefox)( browser)?/([0-9\.]+)#', $useragent, $matches) !== false)
314 if (isset($matches[3]))
316 $browser['firefox'] = $matches[3];
320 // -------------------------------------------------------------------
322 // -------------------------------------------------------------------
323 # Mozilla/5.0 (Macintosh; U; PPC; en-US; rv:0.9.4.1) Gecko/20020318 Netscape6/6.2.2
324 if (preg_match('#netscape([0-9].*?)?/([0-9\.]+)#', $useragent, $matches) !== false)
326 if (isset($matches[2]))
328 $browser['netscape'] = $matches[2];
332 // -------------------------------------------------------------------
334 // -------------------------------------------------------------------
335 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7) Gecko/20040623 Camino/0.8
336 if (preg_match('#camino/([0-9\.]+)#', $useragent, $matches) !== false)
338 if (isset($matches[1]))
340 $browser['camino'] = $matches[1];
345 if (isset($browser["$check"]))
349 if ($browser["$check"] >= $version)
351 return $browser["$check"];
360 return $browser["$check"];
369 // ###################################################################
371 * Generates a random string of random length (unless otherwise
374 * @param integer Optional length
376 * @return string A random string
378 public function rand($length = 0)
380 // custom high and lows
381 if (is_array($length))
383 $length = rand($length[0], $length[1]);
388 $length = rand(20, 65);
391 // Number of ints in our salt
392 $intcount = rand(1, intval($length / 2));
395 $charcount = $length - $intcount;
398 $upperchars = rand(1, intval($charcount / 2));
401 $lowerchars = $charcount - $upperchars;
404 for ($i = 0; $i < $intcount; $i++)
406 $string[ mt_rand() ] = rand(0, 9);
409 // Generate upper chars
410 for ($i = 0; $i < $upperchars; $i++)
412 $string[ mt_rand() ] = chr(rand(65, 90));
415 // Generate lower chars
416 for ($i = 0; $i < $lowerchars; $i++)
418 $string[ mt_rand() ] = chr(rand(97, 122));
421 if ($length != sizeof($string))
423 return $this->rand($length);
426 // Sort the chars by thier random assignment
431 foreach ($string AS $char)
439 // ###################################################################
441 * Sets the current array position to be the specified key. This
442 * function should be avoided on large arrays.
444 * @param array The array whose counter is to be updated
445 * @param mixed The key of the element of the array that the counter is to be set to
447 * @return mixed Return the elelment of the array that we just put the counter to
449 public function array_set_current(&$array, $key)
452 while (current($array) !== false)
454 if (key($array) == $key)
460 return current($array);
463 // ###################################################################
465 * Calculates the microtime difference by taking a given microtime and
466 * subtracting it from the current one
468 * @param string The start microtime
470 * @return float Microtime difference
472 public function fetch_microtime_diff($mtstart)
474 $mtend = microtime();
475 list ($starttime['micro'], $starttime['sec']) = explode(' ', $mtstart);
476 list ($endtime['micro'], $endtime['sec']) = explode(' ', $mtend);
477 return ($endtime['micro'] + $endtime['sec']) - ($starttime['micro'] + $starttime['sec']);
480 // ###################################################################
482 * Fetches the extension of a file by extracting everything after the
485 * @param string Filename
487 * @return string The extension for the specifid file name
489 public function fetch_extension($filename)
491 return strval(end(explode('.', $filename)));
494 // ###################################################################
496 * Gets the maximum file size for attachment uploading, as specified by
497 * PHP. If no value is present, 10 MB (represented in bytes) is
500 * @return integer The maximum file upload size in bytes
502 public function fetch_max_attachment_size()
504 if ($size = @ini_get('upload_max_filesize'))
506 if (preg_match('#[^0-9].#', $size))
512 return intval($size) * 1048576;
521 // ###################################################################
523 * Scans a specified directory path and returns an array of all the
524 * items in that directory. Directories found by this are end in a "/"
526 * @param string Path to scan
527 * @param bool Whether or not to recursively scan the directories encountered
528 * @param bool Ignore files beginning with a dot
529 * @param bool Ignore 'CVS' dirctories
531 * @return array A list of all the files in the specified path
533 public function scandir($path, $recurse = true, $ignoredot = true, $ignorecvs = true, $basepath = '', $unset = 1)
542 if (substr($path, (strlen($path) - 1), 1) != '/')
547 if ($handle = opendir($path))
549 while (($file = readdir($handle)) !== false)
551 $isdot = ((substr($file, 0, 1) == '.') ? true : false);
552 $isdot = (($ignoredot) ? $isdot : true);
553 $iscvs = (($file == 'CVS') ? true : false);
554 $iscvs = (($ignorecvs) ? $iscvs : true);
555 if (!$isdot AND !$iscvs)
557 if (is_dir($path . $file))
559 $filelist["$basepath"][] = "$file/";
562 $this->scandir("$path$file", true, $ignoredot, $ignorecvs, "$basepath$file/", 0);
567 $filelist["$basepath"][] = $file;
576 // ###################################################################
578 * Changes line breaks into one format
581 * @param string New line break (default is UNIX format)
583 * @return string Text with one type of line break
585 public function convert_line_breaks($text, $convert_to = "\n")
588 $text = str_replace(array("\r\n", "\r", "\n"), "\n", $text);
589 $text = str_replace("\n", $convert_to, $text);
593 // ###################################################################
595 * Removes all empty() [by PHP's standards] elements in an array. This
596 * can be used in place of using PREG_SPLIT_NO_EMPTY.
598 * @param array An array to strip empties from
600 * @return array Full-valued array
602 public function array_strip_empty($array)
604 foreach ($array AS $key => $value)
606 if (is_array($array["$key"]))
608 $array["$key"] = $this->array_strip_empty($array["$key"]);
610 else if (empty($value) OR is_null($value))
612 unset($array["$key"]);
619 /*=====================================================================*\
620 || ###################################################################
623 || ###################################################################
624 \*=====================================================================*/