2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright (c)2005-2008 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 2 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 (c)2005 - 2008, Blue Static
45 private static $instance;
51 private $cookiePath = '/';
54 * Cookie domain setting
57 private $cookieDomain = '';
60 * Cookie expiration time
63 private $cookieTimeout = 900;
66 * Current swapped CSS class
69 public static $cssClass = '';
71 // ###################################################################
75 private function __construct() {}
77 // ###################################################################
79 * Returns the shared instance for singleton
81 * @return object Shared instance
83 private function _instance()
87 self
::$instance = new BSFunctions();
89 return self
::$instance;
92 // ###################################################################
94 * Sets the cookie path
96 * @param string New path
98 public static function set_cookie_path($path)
100 self
::_instance()->cookiePath
= $path;
103 // ###################################################################
105 * Sets the cookie domain setting
107 * @param string Cookie domain
109 public static function set_cookie_domain($domain)
111 self
::_instance()->cookieDomain
= $domain;
114 // ###################################################################
116 * Sets the cookie timeout
118 * @param integer Cookie timeout
120 public static function set_cookie_timeout($timeout)
122 self
::_instance()->cookieTimeout
= intval($timeout);
125 // ###################################################################
127 * Sets a cookie in the user's computer/browing session
129 * @param string Name of the cookie
130 * @param string Value of the cookie, FALSE to clear
131 * @param bool Is the cookie permanent?
133 public static function cookie($name, $value, $sticky = true
)
136 if ($value === false
)
138 setcookie($name, $value, time() - (2 * self
::_instance()->cookieTimeout
), self
::_instance()->cookiePath
, self
::_instance()->cookieDomain
);
145 $expire = time() +
60 * 60 * 24 * 365;
149 $expire = time() + self
::_instance()->cookieTimeout
;
152 setcookie($name, $value, $expire, self
::_instance()->cookiePath
, self
::_instance()->cookieDomain
);
156 // ###################################################################
158 * Alternate between two CSS classes
160 * @param string First CSS class name
161 * @param string Second CSS class name
163 public static function swap_css_classes($class1 = 'alt1', $class2 = 'alt2')
167 self
::$cssClass = ($count %
2) ?
$class1 : $class2;
171 // ###################################################################
173 * Returns the 'source path' version of a file path. It adds a
174 * directory separator to the end of a string if it does not already
179 * @return string Path with directory separator ending
181 public static function fetch_source_path($source)
183 if (substr($source, strlen($source) - 1) != DIRECTORY_SEPARATOR
)
185 $source .= DIRECTORY_SEPARATOR
;
190 // ###################################################################
192 * 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 public static function download_file($file, $name, $exit = true
)
200 header("Content-Type: application/octetstream");
201 header("Content-Type: application/octet-stream");
202 header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
203 header('Content-Disposition: attachment; filename="' . $name . '"');
204 header('Content-length: ' . strlen($file));
205 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
206 header('Pragma: public');
216 // ###################################################################
218 * Verify that an email address is valid via regex
220 * @param string An email address
222 * @return bool Validity of the email address
224 public static function is_valid_email($email)
226 if (preg_match('#^[a-z0-9\.\-\+_]+?@(.*?\.)*?[a-z0-9\-_]+?\.[a-z]{2,4}$#i', $email))
236 // ###################################################################
238 * Generates a random string of random length (unless otherwise
241 * @param integer Optional length
243 * @return string A random string
245 public static function random($length = 0)
247 // length wasn't provided, so create our own
250 $length = rand(20, 65);
254 while (strlen($string) < $length)
256 $type = rand(0, 300);
259 $string .= rand(0, 9);
261 else if ($type < 200)
263 $string .= chr(rand(65, 90));
267 $string .= chr(rand(97, 122));
274 // ###################################################################
276 * Sets the current array position to be the specified key. This
277 * function should be avoided on large arrays.
279 * @param array The array whose counter is to be updated
280 * @param mixed The key of the element of the array that the counter is to be set to
282 * @return mixed Return the elelment of the array that we just put the counter to
284 public static function array_set_current(&$array, $key)
287 while (current($array) !== false
)
289 if (key($array) == $key)
295 return current($array);
298 // ###################################################################
300 * Calculates the microtime difference by taking a given microtime and
301 * subtracting it from the current one
303 * @param string The start microtime
305 * @return float Microtime difference
307 public static function fetch_microtime_diff($mtstart)
309 $mtend = microtime();
310 list($startMicro, $startSec) = explode(' ', $mtstart);
311 list($endMicro, $endSec) = explode(' ', $mtend);
312 return ($endMicro +
$endSec) - ($startMicro +
$startSec);
315 // ###################################################################
317 * Fetches the extension of a file by extracting everything after the
320 * @param string Filename
322 * @return string The extension for the specifid file name
324 public static function fetch_extension($filename)
326 $array = explode('.', $filename);
328 if (sizeof($array) == 1)
333 return strval(end($array));
336 // ###################################################################
338 * Gets the maximum file size for attachment uploading, as specified by
339 * PHP. If no value is present, 10 MB (represented in bytes) is
342 * @return integer The maximum file upload size in bytes
344 public static function fetch_max_php_file_size()
346 if ($size = @ini_get('upload_max_filesize'))
348 if (preg_match('#[^0-9].#', $size))
354 return intval($size) * 1048576;
363 // ###################################################################
365 * Scans a specified directory path and returns an array of all the
366 * items in that directory. Directories found by this are end in a "/"
368 * @param string Path to scan
369 * @param bool Whether or not to recursively scan the directories encountered
370 * @param bool Ignore files beginning with a dot
372 * @return array A list of all the files in the specified path
374 public static function scan_directory($path, $recurse = true
, $ignoreDot = true
)
376 return self
::_help_scan_directory($path, $recurse, $ignoreDot, '');
379 // ###################################################################
381 * Scans a specified directory path and returns an array of all the
382 * items in that directory. Directories found by this are end in a "/"
384 * @param string Path to scan
385 * @param bool Whether or not to recursively scan the directories encountered
386 * @param bool Ignore files beginning with a dot
387 * @param string Add to the beginning of the path
389 * @return array A list of all the files in the specified path
391 private static function _help_scan_directory($path, $recurse = true
, $ignoreDot = true
, $pathAdd = '')
394 $path = self
::fetch_source_path($path);
396 $dir = new DirectoryIterator($path);
397 foreach ($dir as $file)
399 $name = $file->getFilename();
400 if (($file->isDot() ||
$name[0] == '.') && $ignoreDot)
405 if ($file->isDir() && $recurse)
407 $filelist = array_merge($filelist, self
::_help_scan_directory($path . $name, $recurse, $ignoreDot, $pathAdd . BSFunctions
::fetch_source_path(str_replace($path, '', $file->getPathname()))));
411 $filelist[] = $pathAdd . $name;
417 // ###################################################################
419 * Changes line breaks into one format
422 * @param string New line break (default is UNIX \n format)
424 * @return string Text with one type of line break
426 public static function convert_line_breaks($text, $convert_to = "\n")
429 $text = str_replace(array("\r\n", "\r", "\n"), "\n", $text);
430 $text = str_replace("\n", $convert_to, $text);
434 // ###################################################################
436 * Removes all empty() [by PHP's standards] elements in an array. This
437 * can be used in place of using PREG_SPLIT_NO_EMPTY.
439 * @param array An array to strip empties from
441 * @return array Full-valued array
443 public static function array_strip_empty($array)
445 foreach ($array as $key => $value)
447 if (is_array($array["$key"]))
449 $array["$key"] = self
::array_strip_empty($array["$key"]);
451 else if (empty($value) ||
is_null($value))
453 unset($array["$key"]);
459 // ###################################################################
461 * A backtrace formatter.
463 * This is very slightly modified from PEAR/PHP_Compat (PHP license)
465 * @author Laurent Laville <pear@laurent-laville.org>
466 * @author Aidan Lister <aidan@php.net>
468 * @param array The backtrace from debug_backtrace() to format
470 * @return string Formatted output
472 public static function format_backtrace($backtrace)
474 // Unset call to debug_print_backtrace
475 array_shift($backtrace);
476 if (empty($backtrace))
483 foreach ($backtrace as $i => $call)
485 if (!isset($call['file']))
487 $call['file'] = '(null)';
489 if (!isset($call['line']))
493 $location = $call['file'] . ':' . $call['line'];
494 $function = (isset($call['class'])) ?
$call['class'] . (isset($call['type']) ?
$call['type'] : '.') . $call['function'] : $call['function'];
497 if (isset($call['args']))
500 foreach ($call['args'] as $arg)
506 elseif (is_object($arg))
508 $args[] = get_class($arg);
515 $params = implode(', ', $args);
518 $calls[] = sprintf('#%d %s(%s) called at [%s]', $i, $function, $params, $location);
521 return implode("\n", $calls);
524 // ###################################################################
526 * A variation of PHP's substr() method that takes in the start
527 * and end position of a string, rather than a start and length. This
528 * mimics Java's String.substring() method.
530 * @param string The string
531 * @param integer Start position
532 * @param integer End position
534 * @return string Part of a string
536 public static function substring($string, $start, $end)
538 return substr($string, $start, $end - $start);