]>
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 * 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()
79 // ###################################################################
81 * Returns the shared instance for singleton
83 * @return object Shared instance
85 private function SharedInstance()
89 self
::$instance = new BSFunctions
;
91 return self
::$instance;
94 // ###################################################################
96 * Sets the cookie path
98 * @param string New path
100 public static function SetCookiePath($path)
102 self
::SharedInstance()->cookiePath
= $path;
105 // ###################################################################
107 * Sets the cookie domain setting
109 * @param string Cookie domain
111 public static function SetCookieDomain($domain)
113 self
::SharedInstance()->cookieDomain
= $domain;
116 // ###################################################################
118 * Sets the cookie timeout
120 * @param integer Cookie timeout
122 public static function SetCookieTimeout($timeout)
124 self
::SharedInstance()->cookieTimeout
= intval($timeout);
127 // ###################################################################
129 * Sets a cookie in the user's computer/browing session
131 * @param string Name of the cookie
132 * @param string Value of the cookie, FALSE to clear
133 * @param bool Is the cookie permanent?
135 public static function Cookie($name, $value, $sticky = true)
138 if ($value === false)
140 setcookie($name, $value, time() - (2 * self
::SharedInstance()->cookieTimeout
), self
::SharedInstance()->cookiePath
, self
::SharedInstance()->cookieDomain
);
147 $expire = time() +
60 * 60 * 24 * 365;
151 $expire = time() + self
::SharedInstance()->cookieTimeout
;
154 setcookie($name, $value, $expire, self
::SharedInstance()->cookiePath
, self
::SharedInstance()->cookieDomain
);
158 // ###################################################################
160 * Alternate between two CSS classes
162 * @param string First CSS class name
163 * @param string Second CSS class name
165 public function SwapCssClasses($class1 = 'alt1', $class2 = 'alt2')
169 self
::$cssClass = ($count %
2) ? $class1 : $class2;
173 // ###################################################################
175 * Returns the 'source path' version of a file path. It adds a
176 * directory separator to the end of a string if it does not already
181 * @return string Path with directory separator ending
183 public static function FetchSourcePath($source)
185 if (substr($source, strlen($source) - 1) != DIRECTORY_SEPARATOR
)
187 $source .= DIRECTORY_SEPARATOR
;
192 // ###################################################################
194 * Force-download a file by sending application/octetstream
196 * @param string The text of the file to be streamed
197 * @param string File name of the new file
198 * @param bool Whether or not to die after stringeaming the file
200 public static function DownloadFile($file, $name, $exit = true)
202 if (self
::IsBrowser('ie') OR self
::IsBrowser('opera') OR self
::IsBrowser('safari'))
204 $mime = 'application/octetstream';
208 $mime = 'application/octet-stream';
211 header("Content-Type: $mime");
212 header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
213 header('Content-Disposition: attachment; filename="' . $name . '"');
214 header('Content-length: ' . strlen($file));
215 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
216 header('Pragma: public');
226 // ###################################################################
228 * Verify that an email address is valid via regex
230 * @param string An email address
232 * @return bool Validity of the email address
234 public static function IsValidEmail($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
250 * @param string Browser name
251 * @param string The browser's version
253 * @param mixed False if there is no match, the version if there is
255 public static function IsBrowser($check, $version = '')
257 $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
261 // -------------------------------------------------------------------
263 // -------------------------------------------------------------------
264 # Opera/6.05 (Windows 98; U) [en]
265 # Mozilla/4.0 (compatible; MSIE 5.0; Windows 98) Opera 6.0 [en]
266 # Mozilla/5.0 (Windows 98; U) Opera 6.0 [en]
267 # Mozilla/4.0 (compatible; MSIE, 6.0; Windows 98) Opera 7.0 [en]
268 if (preg_match('#opera ([0-9\.]+)#', $useragent, $matches) !== false)
270 if (isset($matches[1]))
272 $browser['opera'] = $matches[1];
276 // -------------------------------------------------------------------
278 // -------------------------------------------------------------------
279 if (strpos($useragent, 'mac') !== false)
281 $browser['mac'] = true;
284 // -------------------------------------------------------------------
285 // -- Internet explorer
286 // -------------------------------------------------------------------
287 # Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1; .NET CLR 1.0.2914)
288 # Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)
289 if (preg_match('#msie ([0-9\.]+)#', $useragent, $matches) !== false AND !isset($browser['opera']))
291 if (isset($matches[1]))
293 $browser['ie'] = $matches[1];
297 // -------------------------------------------------------------------
299 // -------------------------------------------------------------------
300 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/125.4 (KHTML, like Gecko) Safari/125.9
301 if (preg_match('#safari/([0-9\.]+)#', $useragent, $matches) !== false)
303 if (isset($matches[1]))
305 $browser['safari'] = $matches[1];
309 // -------------------------------------------------------------------
311 // -------------------------------------------------------------------
312 # Mozilla/5.0 (compatible; Konqueror/3)
313 # Mozilla/5.0 (compatible; Konqueror/3.1; i686 Linux; 20020628)
314 if (preg_match('#konqueror/([0-9\.]+)#', $useragent, $matches) !== false)
316 if (isset($matches[1]))
318 $browser['konqueror'] = $matches[1];
322 // -------------------------------------------------------------------
324 // -------------------------------------------------------------------
325 # Mozilla/5.001 (windows; U; NT4.0; en-us) Gecko/25250101
326 # Mozilla/5.001 (Macintosh; N; PPC; ja) Gecko/25250101 MegaCorpBrowser/1.0 (MegaCorp, Inc.)
327 if (preg_match('#gecko/([0-9]+)#', $useragent, $matches) !== false AND !isset($browser['safari']))
329 if (isset($matches[1]))
331 $browser['mozilla'] = $matches[1];
334 // -------------------------------------------------------------------
336 // -------------------------------------------------------------------
337 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7) Gecko/20040628 Firefox/0.9.1
338 # Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4a) Gecko/20030423 Firebird Browser/0.6
339 if (preg_match('#(firebird|firefox)( browser)?/([0-9\.]+)#', $useragent, $matches) !== false)
341 if (isset($matches[3]))
343 $browser['firefox'] = $matches[3];
347 // -------------------------------------------------------------------
349 // -------------------------------------------------------------------
350 # Mozilla/5.0 (Macintosh; U; PPC; en-US; rv:0.9.4.1) Gecko/20020318 Netscape6/6.2.2
351 if (preg_match('#netscape([0-9].*?)?/([0-9\.]+)#', $useragent, $matches) !== false)
353 if (isset($matches[2]))
355 $browser['netscape'] = $matches[2];
359 // -------------------------------------------------------------------
361 // -------------------------------------------------------------------
362 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7) Gecko/20040623 Camino/0.8
363 if (preg_match('#camino/([0-9\.]+)#', $useragent, $matches) !== false)
365 if (isset($matches[1]))
367 $browser['camino'] = $matches[1];
372 if (isset($browser["$check"]))
376 if ($browser["$check"] >= $version)
378 return $browser["$check"];
387 return $browser["$check"];
396 // ###################################################################
398 * Generates a random string of random length (unless otherwise
401 * @param integer Optional length
403 * @return string A random string
405 public static function Random($length = 0)
407 // custom high and lows
408 if (is_array($length))
410 $length = rand($length[0], $length[1]);
415 $length = rand(20, 65);
418 // Number of ints in our salt
419 $intcount = rand(1, intval($length / 2));
422 $charcount = $length - $intcount;
425 $upperchars = rand(1, intval($charcount / 2));
428 $lowerchars = $charcount - $upperchars;
431 for ($i = 0; $i < $intcount; $i++)
433 $string[ mt_rand() ] = rand(0, 9);
436 // Generate upper chars
437 for ($i = 0; $i < $upperchars; $i++)
439 $string[ mt_rand() ] = chr(rand(65, 90));
442 // Generate lower chars
443 for ($i = 0; $i < $lowerchars; $i++)
445 $string[ mt_rand() ] = chr(rand(97, 122));
448 if ($length != sizeof($string))
450 return $this->rand($length);
453 // Sort the chars by thier random assignment
458 foreach ($string AS $char)
466 // ###################################################################
468 * Sets the current array position to be the specified key. This
469 * function should be avoided on large arrays.
471 * @param array The array whose counter is to be updated
472 * @param mixed The key of the element of the array that the counter is to be set to
474 * @return mixed Return the elelment of the array that we just put the counter to
476 public static function ArraySetCurrent(&$array, $key)
479 while (current($array) !== false)
481 if (key($array) == $key)
487 return current($array);
490 // ###################################################################
492 * Calculates the microtime difference by taking a given microtime and
493 * subtracting it from the current one
495 * @param string The start microtime
497 * @return float Microtime difference
499 public static function FetchMicrotimeDiff($mtstart)
501 $mtend = microtime();
502 list ($starttime['micro'], $starttime['sec']) = explode(' ', $mtstart);
503 list ($endtime['micro'], $endtime['sec']) = explode(' ', $mtend);
504 return ($endtime['micro'] + $endtime['sec']) - ($starttime['micro'] + $starttime['sec']);
507 // ###################################################################
509 * Fetches the extension of a file by extracting everything after the
512 * @param string Filename
514 * @return string The extension for the specifid file name
516 public static function FetchExtension($filename)
518 $array = explode('.', $filename);
520 if (sizeof($array) == 1)
525 return strval(end($array));
528 // ###################################################################
530 * Gets the maximum file size for attachment uploading, as specified by
531 * PHP. If no value is present, 10 MB (represented in bytes) is
534 * @return integer The maximum file upload size in bytes
536 public static function FetchMaxPhpFileSize()
538 if ($size = @ini_get('upload_max_filesize'))
540 if (preg_match('#[^0-9].#', $size))
546 return intval($size) * 1048576;
555 // ###################################################################
557 * Scans a specified directory path and returns an array of all the
558 * items in that directory. Directories found by this are end in a "/"
560 * @param string Path to scan
561 * @param bool Whether or not to recursively scan the directories encountered
562 * @param bool Ignore files beginning with a dot
563 * @param bool Ignore 'CVS' dirctories
565 * @return array A list of all the files in the specified path
567 public static function ScanDirectory($path, $recurse = true, $ignoredot = true, $ignorecvs = true, $basepath = '', $unset = 1)
576 if (substr($path, (strlen($path) - 1), 1) != '/')
581 if ($handle = opendir($path))
583 while (($file = readdir($handle)) !== false)
585 $isdot = ((substr($file, 0, 1) == '.') ? true : false);
586 $isdot = (($ignoredot) ? $isdot : true);
587 $iscvs = (($file == 'CVS') ? true : false);
588 $iscvs = (($ignorecvs) ? $iscvs : true);
589 if (!$isdot AND !$iscvs)
591 if (is_dir($path . $file))
593 $filelist["$basepath"][] = "$file/";
596 self
::ScanDirectory("$path$file", true, $ignoredot, $ignorecvs, "$basepath$file/", 0);
601 $filelist["$basepath"][] = $file;
610 // ###################################################################
612 * Changes line breaks into one format
615 * @param string New line break (default is UNIX \n format)
617 * @return string Text with one type of line break
619 public static function ConvertLineBreaks($text, $convert_to = "\n")
622 $text = str_replace(array("\r\n", "\r", "\n"), "\n", $text);
623 $text = str_replace("\n", $convert_to, $text);
627 // ###################################################################
629 * Removes all empty() [by PHP's standards] elements in an array. This
630 * can be used in place of using PREG_SPLIT_NO_EMPTY.
632 * @param array An array to strip empties from
634 * @return array Full-valued array
636 public static function ArrayStripEmpty($array)
638 foreach ($array AS $key => $value)
640 if (is_array($array["$key"]))
642 $array["$key"] = self
::ArrayStripEmpty($array["$key"]);
644 else if (empty($value) OR is_null($value))
646 unset($array["$key"]);
653 /*=====================================================================*\
654 || ###################################################################
657 || ###################################################################
658 \*=====================================================================*/