]>
src.bluestatic.org Git - bugdar.git/blob - framework/functions.php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright (c)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 (c)2002 - [#]year[#], Blue Static
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
78 // ###################################################################
82 function __construct(&$registry)
84 $this->registry
=& $registry;
87 // ###################################################################
91 function Functions(&$registry)
93 $this->__construct($registry);
96 // ###################################################################
98 * Sets the cookie path
102 * @param string Cookie path
104 function setCookiePath($path)
106 $this->cookiepath
= $path;
109 // ###################################################################
111 * Gets the cookie path
115 * @return string The cookie path
117 function getCookiePath()
119 return $this->cookiepath
;
122 // ###################################################################
124 * Sets the cookie domain
128 * @param string Cookie domain
130 function setCookieDomain($domain)
132 $this->cookiedom
= $domain;
135 // ###################################################################
137 * Gets the cookie domain
141 * @return string The cookie domain
143 function getCookieDomain()
145 return $this->cookiedom
;
148 // ###################################################################
150 * Sets the cookie expiration time
154 * @param integer Cookie expiration time
156 function setCookieExpiration($exp)
158 $this->cookieexp
= $exp;
161 // ###################################################################
163 * Gets the cookie expiration time
167 * @return integer The cookie expiration time
169 function getCookieExpiration()
171 return $this->cookieexp
;
174 // ###################################################################
176 * Sets a cookie with a friendly interface
180 * @param string The name of the cookie
181 * @param string Value of the cookie
182 * @param bool Is the cookie permanent?
184 function cookie($name, $value = '', $sticky = true)
189 setcookie($name, $value, time() - (2 * $this->cookieexp
), $this->cookiepath
, $this->cookiedom
);
196 $expire = time() +
60 * 60 * 24 * 365;
200 $expire = time() +
$this->cookieexp
;
203 setcookie($name, $value, $expire, $this->cookiepath
, $this->cookiedom
);
207 // ###################################################################
209 * Alternate between two background colours
213 * @param string First CSS class name
214 * @param string Second CSS class name
216 function exec_swap_bg($class1 = 'alt1', $class2 = 'alt2')
220 $this->bgcolour
= ($count %
2) ? $class1 : $class2;
224 // ###################################################################
226 * Force-download a file by sending application/octetstream
230 * @param string The text of the file to be streamed
231 * @param string File name of the new file
232 * @param bool Whether or not to die after stringeaming the file
234 function download_file($file, $name, $exit = true)
236 if ($this->is_browser('ie') OR $this->is_browser('opera') OR $this->is_browser('safari'))
238 $mime = 'application/octetstream';
242 $mime = 'application/octet-stream';
245 header("Content-Type: $mime");
246 header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
247 header('Content-Disposition: attachment; filename="' . $name . '"');
248 header('Content-length: ' . strlen($file));
249 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
250 header('Pragma: public');
260 // ###################################################################
262 * Verify that an email address is valid via regex
266 * @param string An email address
268 * @return bool Validity of the email address
270 function is_valid_email($email)
272 if (preg_match('#^[a-z0-9\.\-\+_]+?@(.*?\.)*?[a-z0-9\-_]+?\.[a-z]{2,4}$#i', $email))
282 // ###################################################################
284 * Check a browser's user agent against a pre-determined list
288 * @param string Browser name
289 * @param string The browser's version
291 * @param mixed False if there is no match, the version if there is
293 function is_browser($check, $version = '')
295 $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
299 // -------------------------------------------------------------------
301 // -------------------------------------------------------------------
302 # Opera/6.05 (Windows 98; U) [en]
303 # Mozilla/4.0 (compatible; MSIE 5.0; Windows 98) Opera 6.0 [en]
304 # Mozilla/5.0 (Windows 98; U) Opera 6.0 [en]
305 # Mozilla/4.0 (compatible; MSIE, 6.0; Windows 98) Opera 7.0 [en]
306 if (preg_match('#opera ([0-9\.]+)#', $useragent, $matches) !== false)
308 if (isset($matches[1]))
310 $browser['opera'] = $matches[1];
314 // -------------------------------------------------------------------
316 // -------------------------------------------------------------------
317 if (strpos($useragent, 'mac') !== false)
319 $browser['mac'] = true;
322 // -------------------------------------------------------------------
323 // -- Internet explorer
324 // -------------------------------------------------------------------
325 # Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1; .NET CLR 1.0.2914)
326 # Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)
327 if (preg_match('#msie ([0-9\.]+)#', $useragent, $matches) !== false AND !isset($browser['opera']))
329 if (isset($matches[1]))
331 $browser['ie'] = $matches[1];
335 // -------------------------------------------------------------------
337 // -------------------------------------------------------------------
338 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/125.4 (KHTML, like Gecko) Safari/125.9
339 if (preg_match('#safari/([0-9\.]+)#', $useragent, $matches) !== false)
341 if (isset($matches[1]))
343 $browser['safari'] = $matches[1];
347 // -------------------------------------------------------------------
349 // -------------------------------------------------------------------
350 # Mozilla/5.0 (compatible; Konqueror/3)
351 # Mozilla/5.0 (compatible; Konqueror/3.1; i686 Linux; 20020628)
352 if (preg_match('#konqueror/([0-9\.]+)#', $useragent, $matches) !== false)
354 if (isset($matches[1]))
356 $browser['konqueror'] = $matches[1];
360 // -------------------------------------------------------------------
362 // -------------------------------------------------------------------
363 # Mozilla/5.001 (windows; U; NT4.0; en-us) Gecko/25250101
364 # Mozilla/5.001 (Macintosh; N; PPC; ja) Gecko/25250101 MegaCorpBrowser/1.0 (MegaCorp, Inc.)
365 if (preg_match('#gecko/([0-9]+)#', $useragent, $matches) !== false AND !isset($browser['safari']))
367 if (isset($matches[1]))
369 $browser['mozilla'] = $matches[1];
372 // -------------------------------------------------------------------
374 // -------------------------------------------------------------------
375 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7) Gecko/20040628 Firefox/0.9.1
376 # Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4a) Gecko/20030423 Firebird Browser/0.6
377 if (preg_match('#(firebird|firefox)( browser)?/([0-9\.]+)#', $useragent, $matches) !== false)
379 if (isset($matches[3]))
381 $browser['firefox'] = $matches[3];
385 // -------------------------------------------------------------------
387 // -------------------------------------------------------------------
388 # Mozilla/5.0 (Macintosh; U; PPC; en-US; rv:0.9.4.1) Gecko/20020318 Netscape6/6.2.2
389 if (preg_match('#netscape([0-9].*?)?/([0-9\.]+)#', $useragent, $matches) !== false)
391 if (isset($matches[2]))
393 $browser['netscape'] = $matches[2];
397 // -------------------------------------------------------------------
399 // -------------------------------------------------------------------
400 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7) Gecko/20040623 Camino/0.8
401 if (preg_match('#camino/([0-9\.]+)#', $useragent, $matches) !== false)
403 if (isset($matches[1]))
405 $browser['camino'] = $matches[1];
410 if (isset($browser["$check"]))
414 if ($browser["$check"] >= $version)
416 return $browser["$check"];
425 return $browser["$check"];
434 // ###################################################################
436 * Generates a random string of random length (unless otherwise
441 * @param integer Optional length
443 * @return string A random string
445 function rand($length = 0)
447 // custom high and lows
448 if (is_array($length))
450 $length = rand($length[0], $length[1]);
455 $length = rand(20, 65);
458 // Number of ints in our salt
459 $intcount = rand(1, intval($length / 2));
462 $charcount = $length - $intcount;
465 $upperchars = rand(1, intval($charcount / 2));
468 $lowerchars = $charcount - $upperchars;
471 for ($i = 0; $i < $intcount; $i++)
473 $string[ mt_rand() ] = rand(0, 9);
476 // Generate upper chars
477 for ($i = 0; $i < $upperchars; $i++)
479 $string[ mt_rand() ] = chr(rand(65, 90));
482 // Generate lower chars
483 for ($i = 0; $i < $lowerchars; $i++)
485 $string[ mt_rand() ] = chr(rand(97, 122));
488 // Sort the chars by thier random assignment
493 foreach ($string AS $char)
501 // ###################################################################
503 * Sets the current array position to be the specified key. This
504 * function should be avoided on large arrays.
508 * @param array The array whose counter is to be updated
509 * @param mixed The key of the element of the array that the counter is to be set to
511 * @return mixed Return the elelment of the array that we just put the counter to
513 function array_set_current(&$array, $key)
516 while (current($array) !== false)
518 if (key($array) == $key)
524 return current($array);
527 // ###################################################################
529 * Calculates the microtime difference by taking a given microtime and
530 * subtracting it from the current one
534 * @param string The start microtime
536 * @return float Microtime difference
538 function fetch_microtime_diff($mtstart)
540 $mtend = microtime();
541 list ($starttime['micro'], $starttime['sec']) = explode(' ', $mtstart);
542 list ($endtime['micro'], $endtime['sec']) = explode(' ', $mtend);
543 return ($endtime['micro'] + $endtime['sec']) - ($starttime['micro'] + $starttime['sec']);
546 // ###################################################################
548 * Fetches the extension of a file by extracting everything after the
553 * @param string Filename
555 * @return string The extension for the specifid file name
557 function fetch_extension($filename)
559 return strval(end(explode('.', $filename)));
562 // ###################################################################
564 * Gets the maximum file size for attachment uploading, as specified by
565 * PHP. If no value is present, 10 MB (represented in bytes) is
570 * @return integer The maximum file upload size in bytes
572 function fetch_max_attachment_size()
574 if ($size = @ini_get('upload_max_filesize'))
576 if (preg_match('#[^0-9].#', $size))
582 return intval($size) * 1048576;
591 // ###################################################################
593 * Scans a specified directory path and returns an array of all the
594 * items in that directory. Directories found by this are end in a "/"
598 * @param string Path to scan
599 * @param bool Whether or not to recursively scan the directories encountered
600 * @param bool Ignore files beginning with a dot
601 * @param bool Ignore 'CVS' dirctories
603 * @return array A list of all the files in the specified path
605 function scandir($path, $recurse = true, $ignoredot = true, $ignorecvs = true, $basepath = '', $unset = 1)
614 if (substr($path, (strlen($path) - 1), 1) != '/')
619 if ($handle = opendir($path))
621 while (($file = readdir($handle)) !== false)
623 $isdot = ((substr($file, 0, 1) == '.') ? true : false);
624 $isdot = (($ignoredot) ? $isdot : true);
625 $iscvs = (($file == 'CVS') ? true : false);
626 $iscvs = (($ignorecvs) ? $iscvs : true);
627 if (!$isdot AND !$iscvs)
629 if (is_dir($path . $file))
631 $filelist["$basepath"][] = "$file/";
634 $this->scandir("$path$file", true, $ignoredot, $ignorecvs, "$basepath$file/", 0);
639 $filelist["$basepath"][] = $file;
648 // ###################################################################
650 * Changes line breaks into one format
655 * @param string New line break (default is UNIX format)
657 * @return string Text with one type of line break
659 function convert_line_breaks($text, $convert_to = "\n")
662 $text = str_replace(array("\r\n", "\r", "\n"), "\n", $text);
663 $text = str_replace("\n", $convert_to, $text);
667 // ###################################################################
669 * Removes all empty() [by PHP's standards] elements in an array. This
670 * can be used in place of using PREG_SPLIT_NO_EMPTY.
674 * @param array An array to strip empties from
676 * @return array Full-valued array
678 function array_strip_empty($array)
680 foreach ($array AS $key => $value)
682 if (is_array($array["$key"]))
684 $array["$key"] = $this->array_strip_empty($array["$key"]);
686 else if (empty($value) OR is_null($value))
688 unset($array["$key"]);