]>
src.bluestatic.org Git - isso.git/blob - Functions.php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
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 * Static functions (Functions.php)
31 * This is a bunch of static functions. This class is singleton so it
32 * can store data while remaining static.
35 * @copyright Copyright ©2002 - [#]year[#], Blue Static
46 private static $instance;
52 private $cookiePath = '/';
55 * Cookie domain setting
58 private $cookieDomain = '';
61 * Cookie expiration time
64 private $cookieTimeout = 900;
67 * Current swapped CSS class
70 public static $cssClass = '';
72 // ###################################################################
76 private function __construct() {}
78 // ###################################################################
80 * Returns the shared instance for singleton
82 * @return object Shared instance
84 private function _Instance()
88 self
::$instance = new BSFunctions();
90 return self
::$instance;
93 // ###################################################################
95 * Sets the cookie path
97 * @param string New path
99 public static function SetCookiePath($path)
101 self
::_Instance()->cookiePath
= $path;
104 // ###################################################################
106 * Sets the cookie domain setting
108 * @param string Cookie domain
110 public static function SetCookieDomain($domain)
112 self
::_Instance()->cookieDomain
= $domain;
115 // ###################################################################
117 * Sets the cookie timeout
119 * @param integer Cookie timeout
121 public static function SetCookieTimeout($timeout)
123 self
::_Instance()->cookieTimeout
= intval($timeout);
126 // ###################################################################
128 * Sets a cookie in the user's computer/browing session
130 * @param string Name of the cookie
131 * @param string Value of the cookie, FALSE to clear
132 * @param bool Is the cookie permanent?
134 public static function Cookie($name, $value, $sticky = true)
137 if ($value === false)
139 setcookie($name, $value, time() - (2 * self
::_Instance()->cookieTimeout
), self
::_Instance()->cookiePath
, self
::_Instance()->cookieDomain
);
146 $expire = time() +
60 * 60 * 24 * 365;
150 $expire = time() + self
::_Instance()->cookieTimeout
;
153 setcookie($name, $value, $expire, self
::_Instance()->cookiePath
, self
::_Instance()->cookieDomain
);
157 // ###################################################################
159 * Alternate between two CSS classes
161 * @param string First CSS class name
162 * @param string Second CSS class name
164 public function SwapCssClasses($class1 = 'alt1', $class2 = 'alt2')
168 self
::$cssClass = ($count %
2) ? $class1 : $class2;
172 // ###################################################################
174 * Returns the 'source path' version of a file path. It adds a
175 * directory separator to the end of a string if it does not already
180 * @return string Path with directory separator ending
182 public static function FetchSourcePath($source)
184 if (substr($source, strlen($source) - 1) != DIRECTORY_SEPARATOR
)
186 $source .= DIRECTORY_SEPARATOR
;
191 // ###################################################################
193 * Force-download a file by sending application/octetstream
195 * @param string The text of the file to be streamed
196 * @param string File name of the new file
197 * @param bool Whether or not to die after stringeaming the file
199 public static function DownloadFile($file, $name, $exit = true)
201 if (self
::IsBrowser('ie') OR self
::IsBrowser('opera') OR self
::IsBrowser('safari'))
203 $mime = 'application/octetstream';
207 $mime = 'application/octet-stream';
210 header("Content-Type: $mime");
211 header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
212 header('Content-Disposition: attachment; filename="' . $name . '"');
213 header('Content-length: ' . strlen($file));
214 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
215 header('Pragma: public');
225 // ###################################################################
227 * Verify that an email address is valid via regex
229 * @param string An email address
231 * @return bool Validity of the email address
233 public static function IsValidEmail($email)
235 if (preg_match('#^[a-z0-9\.\-\+_]+?@(.*?\.)*?[a-z0-9\-_]+?\.[a-z]{2,4}$#i', $email))
245 // ###################################################################
247 * Check a browser's user agent against a pre-determined list
249 * @param string Browser name
250 * @param string The browser's version
252 * @param mixed False if there is no match, the version if there is
254 public static function IsBrowser($check, $version = '')
256 $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
260 // -------------------------------------------------------------------
262 // -------------------------------------------------------------------
263 # Opera/6.05 (Windows 98; U) [en]
264 # Mozilla/4.0 (compatible; MSIE 5.0; Windows 98) Opera 6.0 [en]
265 # Mozilla/5.0 (Windows 98; U) Opera 6.0 [en]
266 # Mozilla/4.0 (compatible; MSIE, 6.0; Windows 98) Opera 7.0 [en]
267 if (preg_match('#opera ([0-9\.]+)#', $useragent, $matches) !== false)
269 if (isset($matches[1]))
271 $browser['opera'] = $matches[1];
275 // -------------------------------------------------------------------
277 // -------------------------------------------------------------------
278 if (strpos($useragent, 'mac') !== false)
280 $browser['mac'] = true;
283 // -------------------------------------------------------------------
284 // -- Internet explorer
285 // -------------------------------------------------------------------
286 # Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1; .NET CLR 1.0.2914)
287 # Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)
288 if (preg_match('#msie ([0-9\.]+)#', $useragent, $matches) !== false AND !isset($browser['opera']))
290 if (isset($matches[1]))
292 $browser['ie'] = $matches[1];
296 // -------------------------------------------------------------------
298 // -------------------------------------------------------------------
299 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/125.4 (KHTML, like Gecko) Safari/125.9
300 if (preg_match('#safari/([0-9\.]+)#', $useragent, $matches) !== false)
302 if (isset($matches[1]))
304 $browser['safari'] = $matches[1];
308 // -------------------------------------------------------------------
310 // -------------------------------------------------------------------
311 # Mozilla/5.0 (compatible; Konqueror/3)
312 # Mozilla/5.0 (compatible; Konqueror/3.1; i686 Linux; 20020628)
313 if (preg_match('#konqueror/([0-9\.]+)#', $useragent, $matches) !== false)
315 if (isset($matches[1]))
317 $browser['konqueror'] = $matches[1];
321 // -------------------------------------------------------------------
323 // -------------------------------------------------------------------
324 # Mozilla/5.001 (windows; U; NT4.0; en-us) Gecko/25250101
325 # Mozilla/5.001 (Macintosh; N; PPC; ja) Gecko/25250101 MegaCorpBrowser/1.0 (MegaCorp, Inc.)
326 if (preg_match('#gecko/([0-9]+)#', $useragent, $matches) !== false AND !isset($browser['safari']))
328 if (isset($matches[1]))
330 $browser['mozilla'] = $matches[1];
333 // -------------------------------------------------------------------
335 // -------------------------------------------------------------------
336 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7) Gecko/20040628 Firefox/0.9.1
337 # Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4a) Gecko/20030423 Firebird Browser/0.6
338 if (preg_match('#(firebird|firefox)( browser)?/([0-9\.]+)#', $useragent, $matches) !== false)
340 if (isset($matches[3]))
342 $browser['firefox'] = $matches[3];
346 // -------------------------------------------------------------------
348 // -------------------------------------------------------------------
349 # Mozilla/5.0 (Macintosh; U; PPC; en-US; rv:0.9.4.1) Gecko/20020318 Netscape6/6.2.2
350 if (preg_match('#netscape([0-9].*?)?/([0-9\.]+)#', $useragent, $matches) !== false)
352 if (isset($matches[2]))
354 $browser['netscape'] = $matches[2];
358 // -------------------------------------------------------------------
360 // -------------------------------------------------------------------
361 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7) Gecko/20040623 Camino/0.8
362 if (preg_match('#camino/([0-9\.]+)#', $useragent, $matches) !== false)
364 if (isset($matches[1]))
366 $browser['camino'] = $matches[1];
371 if (isset($browser["$check"]))
375 if ($browser["$check"] >= $version)
377 return $browser["$check"];
386 return $browser["$check"];
395 // ###################################################################
397 * Generates a random string of random length (unless otherwise
400 * @param integer Optional length
402 * @return string A random string
404 public static function Random($length = 0)
406 // custom high and lows
407 if (is_array($length))
409 $length = rand($length[0], $length[1]);
413 $length = rand(20, 65);
417 while (strlen($string) < $length)
419 $type = rand(0, 300);
422 $string .= rand(0, 9);
424 else if ($type < 200)
426 $string .= chr(rand(65, 90));
430 $string .= chr(rand(97, 122));
437 // ###################################################################
439 * Sets the current array position to be the specified key. This
440 * function should be avoided on large arrays.
442 * @param array The array whose counter is to be updated
443 * @param mixed The key of the element of the array that the counter is to be set to
445 * @return mixed Return the elelment of the array that we just put the counter to
447 public static function ArraySetCurrent(&$array, $key)
450 while (current($array) !== false)
452 if (key($array) == $key)
458 return current($array);
461 // ###################################################################
463 * Calculates the microtime difference by taking a given microtime and
464 * subtracting it from the current one
466 * @param string The start microtime
468 * @return float Microtime difference
470 public static function FetchMicrotimeDiff($mtstart)
472 $mtend = microtime();
473 list ($starttime['micro'], $starttime['sec']) = explode(' ', $mtstart);
474 list ($endtime['micro'], $endtime['sec']) = explode(' ', $mtend);
475 return ($endtime['micro'] + $endtime['sec']) - ($starttime['micro'] + $starttime['sec']);
478 // ###################################################################
480 * Fetches the extension of a file by extracting everything after the
483 * @param string Filename
485 * @return string The extension for the specifid file name
487 public static function FetchExtension($filename)
489 $array = explode('.', $filename);
491 if (sizeof($array) == 1)
496 return strval(end($array));
499 // ###################################################################
501 * Gets the maximum file size for attachment uploading, as specified by
502 * PHP. If no value is present, 10 MB (represented in bytes) is
505 * @return integer The maximum file upload size in bytes
507 public static function FetchMaxPhpFileSize()
509 if ($size = @ini_get('upload_max_filesize'))
511 if (preg_match('#[^0-9].#', $size))
517 return intval($size) * 1048576;
526 // ###################################################################
528 * Scans a specified directory path and returns an array of all the
529 * items in that directory. Directories found by this are end in a "/"
531 * @param string Path to scan
532 * @param bool Whether or not to recursively scan the directories encountered
533 * @param bool Ignore files beginning with a dot
535 * @return array A list of all the files in the specified path
537 public static function ScanDirectory($path, $recurse = true, $ignoreDot = true)
539 return self::_helpScanDirectory($path, $recurse, $ignoreDot, '');
542 // ###################################################################
544 * Scans a specified directory path and returns an array of all the
545 * items in that directory. Directories found by this are end in a "/"
547 * @param string Path to scan
548 * @param bool Whether or not to recursively scan the directories encountered
549 * @param bool Ignore files beginning with a dot
550 * @param string Add to the beginning of the path
552 * @return array A list of all the files in the specified path
554 private static function _helpScanDirectory($path, $recurse = true, $ignoreDot = true, $pathAdd = '')
557 $path = self::FetchSourcePath($path);
559 $dir = new DirectoryIterator($path);
560 foreach ($dir AS $file)
562 $name = $file->getFilename();
563 if (($file->isDot() OR $name[0] == '.') AND $ignoreDot)
568 if ($file->isDir() AND $recurse)
570 $filelist = array_merge($filelist, self::_helpScanDirectory($path . $name, $recurse, $ignoreDot, $pathAdd . BSFunctions::FetchSourcePath(str_replace($path, '', $file->getPathname()))));
574 $filelist[] = $pathAdd . $name;
580 // ###################################################################
582 * Changes line breaks into one format
585 * @param string New line break (default is UNIX \n format)
587 * @return string Text with one type of line break
589 public static function ConvertLineBreaks($text, $convert_to = "\n
")
592 $text = str_replace(array("\r\n
", "\r
", "\n
"), "\n
", $text);
593 $text = str_replace("\n
", $convert_to, $text);
597 // ###################################################################
599 * Removes all empty() [by PHP's standards] elements in an array. This
600 * can be used in place of using PREG_SPLIT_NO_EMPTY.
602 * @param array An array to strip empties from
604 * @return array Full-valued array
606 public static function ArrayStripEmpty($array)
608 foreach ($array AS $key => $value)
610 if (is_array($array["$key"]))
612 $array["$key"] = self::ArrayStripEmpty($array["$key"]);
614 else if (empty($value) OR is_null($value))
616 unset($array["$key"]);
622 // ###################################################################
624 * A backtrace formatter.
626 * This is very slightly modified from PEAR/PHP_Compat (PHP license)
628 * @author Laurent Laville <pear@laurent-laville.org>
629 * @author Aidan Lister <aidan@php.net>
631 * @param array The backtrace from debug_backtrace() to format
633 * @return string Formatted output
635 public static function FormatBacktrace($backtrace)
637 // Unset call to debug_print_backtrace
638 array_shift($backtrace);
639 if (empty($backtrace))
646 foreach ($backtrace AS $i => $call)
648 if (!isset($call['file']))
650 $call['file'] = '(null)';
652 if (!isset($call['line']))
656 $location = $call['file'] . ':' . $call['line'];
657 $function = (isset($call['class'])) ? $call['class'] . (isset($call['type']) ? $call['type'] : '.') . $call['function'] : $call['function'];
660 if (isset($call['args']))
663 foreach ($call['args'] AS $arg)
669 elseif (is_object($arg))
671 $args[] = get_class($arg);
678 $params = implode(', ', $args);
681 $calls[] = sprintf('#%d %s(%s) called at [%s]', $i, $function, $params, $location);
684 return implode("\n
", $calls);
687 // ###################################################################
689 * A variation of PHP's substr() method that takes in the start
690 * and end position of a string, rather than a start and length. This
691 * mimics Java's String.substring() method.
693 * @param string The string
694 * @param integer Start position
695 * @param integer End position
697 * @return string Part of a string
699 public static function Substring($string, $start, $end)
701 return substr($string, $start, $end - $start);
705 /*=====================================================================*\
706 || ###################################################################
709 || ###################################################################
710 \*=====================================================================*/