]>
src.bluestatic.org Git - isso.git/blob - Functions.php
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 = '';
74 private function __construct() {}
77 * Returns the shared instance for singleton
79 * @return object Shared instance
81 private function _instance()
85 self
::$instance = new BSFunctions();
87 return self
::$instance;
91 * Sets the cookie path
93 * @param string New path
95 public static function set_cookie_path($path)
97 self
::_instance()->cookiePath
= $path;
101 * Sets the cookie domain setting
103 * @param string Cookie domain
105 public static function set_cookie_domain($domain)
107 self
::_instance()->cookieDomain
= $domain;
111 * Sets the cookie timeout
113 * @param integer Cookie timeout
115 public static function set_cookie_timeout($timeout)
117 self
::_instance()->cookieTimeout
= intval($timeout);
121 * Sets a cookie in the user's computer/browing session
123 * @param string Name of the cookie
124 * @param string Value of the cookie, FALSE to clear
125 * @param bool Is the cookie permanent?
127 public static function cookie($name, $value, $sticky = true)
130 if ($value === false)
132 setcookie($name, $value, time() - (2 * self
::_instance()->cookieTimeout
), self
::_instance()->cookiePath
, self
::_instance()->cookieDomain
);
139 $expire = time() +
60 * 60 * 24 * 365;
143 $expire = time() + self
::_instance()->cookieTimeout
;
146 setcookie($name, $value, $expire, self
::_instance()->cookiePath
, self
::_instance()->cookieDomain
);
151 * Alternate between two CSS classes
153 * @param string First CSS class name
154 * @param string Second CSS class name
156 public static function swap_css_classes($class1 = 'alt1', $class2 = 'alt2')
160 self
::$cssClass = ($count %
2) ? $class1 : $class2;
165 * Returns the 'source path' version of a file path. It adds a
166 * directory separator to the end of a string if it does not already
171 * @return string Path with directory separator ending
173 public static function fetch_source_path($source)
175 if (substr($source, strlen($source) - 1) != DIRECTORY_SEPARATOR
)
177 $source .= DIRECTORY_SEPARATOR
;
183 * Force-download a file by sending application/octetstream
185 * @param string The text of the file to be streamed
186 * @param string File name of the new file
187 * @param bool Whether or not to die after stringeaming the file
189 public static function download_file($file, $name, $exit = true)
191 header("Content-Type: application/octetstream");
192 header("Content-Type: application/octet-stream");
193 header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
194 header('Content-Disposition: attachment; filename="' . $name . '"');
195 header('Content-length: ' . strlen($file));
196 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
197 header('Pragma: public');
208 * Verify that an email address is valid via regex
210 * @param string An email address
212 * @return bool Validity of the email address
214 public static function is_valid_email($email)
216 if (preg_match('#^[a-z0-9\.\-\+_]+?@(.*?\.)*?[a-z0-9\-_]+?\.[a-z]{2,4}$#i', $email))
227 * Generates a random string of random length (unless otherwise
230 * @param integer Optional length
232 * @return string A random string
234 public static function random($length = 0)
236 // length wasn't provided, so create our own
239 $length = rand(20, 65);
243 while (strlen($string) < $length)
245 $type = rand(0, 300);
248 $string .= rand(0, 9);
250 else if ($type < 200)
252 $string .= chr(rand(65, 90));
256 $string .= chr(rand(97, 122));
264 * Sets the current array position to be the specified key. This
265 * function should be avoided on large arrays.
267 * @param array The array whose counter is to be updated
268 * @param mixed The key of the element of the array that the counter is to be set to
270 * @return mixed Return the elelment of the array that we just put the counter to
272 public static function array_set_current(&$array, $key)
275 while (current($array) !== false)
277 if (key($array) == $key)
283 return current($array);
287 * Calculates the microtime difference by taking a given microtime and
288 * subtracting it from the current one
290 * @param string The start microtime
292 * @return float Microtime difference
294 public static function fetch_microtime_diff($mtstart)
296 $mtend = microtime();
297 list($startMicro, $startSec) = explode(' ', $mtstart);
298 list($endMicro, $endSec) = explode(' ', $mtend);
299 return ($endMicro +
$endSec) - ($startMicro +
$startSec);
303 * Fetches the extension of a file by extracting everything after the
306 * @param string Filename
308 * @return string The extension for the specifid file name
310 public static function fetch_extension($filename)
312 $array = explode('.', $filename);
314 if (sizeof($array) == 1)
319 return strval(end($array));
323 * Gets the maximum file size for attachment uploading, as specified by
324 * PHP. If no value is present, 10 MB (represented in bytes) is
327 * @return integer The maximum file upload size in bytes
329 public static function fetch_max_php_file_size()
331 if ($size = @ini_get('upload_max_filesize'))
333 if (preg_match('#[^0-9].#', $size))
339 return intval($size) * 1048576;
349 * Scans a specified directory path and returns an array of all the
350 * items in that directory. Directories found by this are end in a "/"
352 * @param string Path to scan
353 * @param bool Whether or not to recursively scan the directories encountered
354 * @param bool Ignore files beginning with a dot
356 * @return array A list of all the files in the specified path
358 public static function scan_directory($path, $recurse = true, $ignoreDot = true)
360 return self
::_help_scan_directory($path, $recurse, $ignoreDot, '');
364 * Scans a specified directory path and returns an array of all the
365 * items in that directory. Directories found by this are end in a "/"
367 * @param string Path to scan
368 * @param bool Whether or not to recursively scan the directories encountered
369 * @param bool Ignore files beginning with a dot
370 * @param string Add to the beginning of the path
372 * @return array A list of all the files in the specified path
374 private static function _help_scan_directory($path, $recurse = true, $ignoreDot = true, $pathAdd = '')
377 $path = self
::fetch_source_path($path);
379 $dir = new DirectoryIterator($path);
380 foreach ($dir as $file)
382 $name = $file->getFilename();
383 if (($file->isDot() || $name[0] == '.') && $ignoreDot)
388 if ($file->isDir() && $recurse)
390 $filelist = array_merge($filelist, self
::_help_scan_directory($path . $name, $recurse, $ignoreDot, $pathAdd . BSFunctions
::fetch_source_path(str_replace($path, '', $file->getPathname()))));
394 $filelist[] = $pathAdd . $name;
401 * Changes line breaks into one format
404 * @param string New line break (default is UNIX \n format)
406 * @return string Text with one type of line break
408 public static function convert_line_breaks($text, $convert_to = "\n")
411 $text = str_replace(array("\r\n", "\r", "\n"), "\n", $text);
412 $text = str_replace("\n", $convert_to, $text);
417 * Removes all empty() [by PHP's standards] elements in an array. This
418 * can be used in place of using PREG_SPLIT_NO_EMPTY.
420 * @param array An array to strip empties from
422 * @return array Full-valued array
424 public static function array_strip_empty($array)
426 foreach ($array as $key => $value)
428 if (is_array($array["$key"]))
430 $array["$key"] = self
::array_strip_empty($array["$key"]);
432 else if (empty($value) || is_null($value))
434 unset($array["$key"]);
441 * A backtrace formatter.
443 * This is very slightly modified from PEAR/PHP_Compat (PHP license)
445 * @author Laurent Laville <pear@laurent-laville.org>
446 * @author Aidan Lister <aidan@php.net>
448 * @param array The backtrace from debug_backtrace() to format
450 * @return string Formatted output
452 public static function format_backtrace($backtrace)
454 // Unset call to debug_print_backtrace
455 array_shift($backtrace);
456 if (empty($backtrace))
463 foreach ($backtrace as $i => $call)
465 if (!isset($call['file']))
467 $call['file'] = '(null)';
469 if (!isset($call['line']))
473 $location = $call['file'] . ':' . $call['line'];
474 $function = (isset($call['class'])) ? $call['class'] . (isset($call['type']) ? $call['type'] : '.') . $call['function'] : $call['function'];
477 if (isset($call['args']))
480 foreach ($call['args'] as $arg)
486 elseif (is_object($arg))
488 $args[] = get_class($arg);
495 $params = implode(', ', $args);
498 $calls[] = sprintf('#%d %s(%s) called at [%s]', $i, $function, $params, $location);
501 return implode("\n", $calls);
505 * A variation of PHP's substr() method that takes in the start
506 * and end position of a string, rather than a start and length. This
507 * mimics Java's String.substring() method.
509 * @param string The string
510 * @param integer Start position
511 * @param integer End position
513 * @return string Part of a string
515 public static function substring($string, $start, $end)
517 return substr($string, $start, $end - $start);