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 * Globalized Functions
30 * Globalized Functions
32 * This framework is a set of functions that are commonly used in most
36 * @copyright Copyright ©2002 - [#]year[#], Blue Static
44 * Framework registry object
47 private $registry = null
;
50 * The path that is used to set cookies
53 private $cookiepath = '/';
56 * The domain used for cookie setting
59 private $cookiedom = '';
62 * The time it takes for a cookie to expire
65 private $cookieexp = 900;
68 * State of the current background colour during alternation
71 public $bgcolour = '';
74 * Fields array that is used in this module
77 private $fields = array(
78 'cookiepath' => array(REQ_YES
, null
, true
),
79 'cookiedom' => array(REQ_YES
, null
, true
),
80 'cookieexp' => array(REQ_YES
, null
, true
)
83 // ###################################################################
87 function __construct(&$registry)
89 $this->registry
=& $registry;
92 // ###################################################################
96 function Functions(&$registry)
98 $this->__construct($registry);
101 // ###################################################################
107 * @param string Field name
108 * @param mixed Value of the field
110 function set($name, $value)
112 $this->registry
->do_set($name, $value, 'functions');
115 // ###################################################################
121 * @param string Field name
123 * @return mixed Value of the field
125 function get($fieldname)
127 return $this->registry
->do_get($fieldname, 'functions');
130 // ###################################################################
132 * Sets a cookie with a friendly interface
136 * @param string The name of the cookie
137 * @param string Value of the cookie
138 * @param bool Is the cookie permanent?
140 function cookie($name, $value = '', $sticky = true
)
142 $this->registry
->check_isso_fields(get_class($this));
147 setcookie($name, $value, time() - (2 * $this->cookieexp
), $this->cookiepath
, $this->cookiedom
);
154 $expire = time() +
60 * 60 * 24 * 365;
158 $expire = time() +
$this->cookieexp
;
161 setcookie($name, $value, $expire, $this->cookiepath
, $this->cookiedom
);
165 // ###################################################################
167 * Alternate between two background colours
171 * @param string First CSS class name
172 * @param string Second CSS class name
174 function exec_swap_bg($class1 = 'alt1', $class2 = 'alt2')
178 $this->bgcolour
= ($count %
2) ?
$class1 : $class2;
182 // ###################################################################
184 * Force-download a file by sending application/octetstream
188 * @param string The text of the file to be streamed
189 * @param string File name of the new file
190 * @param bool Whether or not to die after stringeaming the file
192 function download_file($file, $name, $exit = true
)
194 if ($this->is_browser('ie') OR $this->is_browser('opera') OR $this->is_browser('safari'))
196 $mime = 'application/octetstream';
200 $mime = 'application/octet-stream';
203 header("Content-Type: $mime");
204 header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
205 header('Content-Disposition: attachment; filename="' . $name . '"');
206 header('Content-length: ' . strlen($file));
207 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
208 header('Pragma: public');
218 // ###################################################################
220 * Verify that an email address is valid via regex
224 * @param string An email address
226 * @return bool Validity of the email address
228 function is_valid_email($email)
230 if (preg_match('#^[a-z0-9\.\-\+_]+?@(.*?\.)*?[a-z0-9\-_]+?\.[a-z]{2,4}$#i', $email))
240 // ###################################################################
242 * Check a browser's user agent against a pre-determined list
246 * @param string Browser name
247 * @param string The browser's version
249 * @param mixed False if there is no match, the version if there is
251 function is_browser($check, $version = '')
253 $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
257 // -------------------------------------------------------------------
259 // -------------------------------------------------------------------
260 # Opera/6.05 (Windows 98; U) [en]
261 # Mozilla/4.0 (compatible; MSIE 5.0; Windows 98) Opera 6.0 [en]
262 # Mozilla/5.0 (Windows 98; U) Opera 6.0 [en]
263 # Mozilla/4.0 (compatible; MSIE, 6.0; Windows 98) Opera 7.0 [en]
264 if (preg_match('#opera ([0-9\.]+)#', $useragent, $matches) !== false
)
266 if (isset($matches[1]))
268 $browser['opera'] = $matches[1];
272 // -------------------------------------------------------------------
274 // -------------------------------------------------------------------
275 if (strpos($useragent, 'mac') !== false
)
277 $browser['mac'] = true
;
280 // -------------------------------------------------------------------
281 // -- Internet explorer
282 // -------------------------------------------------------------------
283 # Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1; .NET CLR 1.0.2914)
284 # Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)
285 if (preg_match('#msie ([0-9\.]+)#', $useragent, $matches) !== false
AND !isset($browser['opera']))
287 if (isset($matches[1]))
289 $browser['ie'] = $matches[1];
293 // -------------------------------------------------------------------
295 // -------------------------------------------------------------------
296 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/125.4 (KHTML, like Gecko) Safari/125.9
297 if (preg_match('#safari/([0-9\.]+)#', $useragent, $matches) !== false
)
299 if (isset($matches[1]))
301 $browser['safari'] = $matches[1];
305 // -------------------------------------------------------------------
307 // -------------------------------------------------------------------
308 # Mozilla/5.0 (compatible; Konqueror/3)
309 # Mozilla/5.0 (compatible; Konqueror/3.1; i686 Linux; 20020628)
310 if (preg_match('#konqueror/([0-9\.]+)#', $useragent, $matches) !== false
)
312 if (isset($matches[1]))
314 $browser['konqueror'] = $matches[1];
318 // -------------------------------------------------------------------
320 // -------------------------------------------------------------------
321 # Mozilla/5.001 (windows; U; NT4.0; en-us) Gecko/25250101
322 # Mozilla/5.001 (Macintosh; N; PPC; ja) Gecko/25250101 MegaCorpBrowser/1.0 (MegaCorp, Inc.)
323 if (preg_match('#gecko/([0-9]+)#', $useragent, $matches) !== false
AND !isset($browser['safari']))
325 if (isset($matches[1]))
327 $browser['mozilla'] = $matches[1];
330 // -------------------------------------------------------------------
332 // -------------------------------------------------------------------
333 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7) Gecko/20040628 Firefox/0.9.1
334 # Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4a) Gecko/20030423 Firebird Browser/0.6
335 if (preg_match('#(firebird|firefox)( browser)?/([0-9\.]+)#', $useragent, $matches) !== false
)
337 if (isset($matches[3]))
339 $browser['firefox'] = $matches[3];
343 // -------------------------------------------------------------------
345 // -------------------------------------------------------------------
346 # Mozilla/5.0 (Macintosh; U; PPC; en-US; rv:0.9.4.1) Gecko/20020318 Netscape6/6.2.2
347 if (preg_match('#netscape([0-9].*?)?/([0-9\.]+)#', $useragent, $matches) !== false
)
349 if (isset($matches[2]))
351 $browser['netscape'] = $matches[2];
355 // -------------------------------------------------------------------
357 // -------------------------------------------------------------------
358 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7) Gecko/20040623 Camino/0.8
359 if (preg_match('#camino/([0-9\.]+)#', $useragent, $matches) !== false
)
361 if (isset($matches[1]))
363 $browser['camino'] = $matches[1];
368 if (isset($browser["$check"]))
372 if ($browser["$check"] >= $version)
374 return $browser["$check"];
383 return $browser["$check"];
392 // ###################################################################
394 * Generates a random string of random length (unless otherwise
399 * @param integer Optional length
401 * @return string A random string
403 function rand($length = 0)
405 // custom high and lows
406 if (is_array($length))
408 $length = rand($length[0], $length[1]);
413 $length = rand(20, 65);
416 // Number of ints in our salt
417 $intcount = rand(1, intval($length / 2));
420 $charcount = $length - $intcount;
423 $upperchars = rand(1, intval($charcount / 2));
426 $lowerchars = $charcount - $upperchars;
429 for ($i = 0; $i < $intcount; $i++
)
431 $string[ mt_rand() ] = rand(0, 9);
434 // Generate upper chars
435 for ($i = 0; $i < $upperchars; $i++
)
437 $string[ mt_rand() ] = chr(rand(65, 90));
440 // Generate lower chars
441 for ($i = 0; $i < $lowerchars; $i++
)
443 $string[ mt_rand() ] = chr(rand(97, 122));
446 if ($length != sizeof($string))
448 return $this->rand($length);
451 // Sort the chars by thier random assignment
456 foreach ($string AS $char)
464 // ###################################################################
466 * Sets the current array position to be the specified key. This
467 * 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 function array_set_current(&$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
497 * @param string The start microtime
499 * @return float Microtime difference
501 function fetch_microtime_diff($mtstart)
503 $mtend = microtime();
504 list ($starttime['micro'], $starttime['sec']) = explode(' ', $mtstart);
505 list ($endtime['micro'], $endtime['sec']) = explode(' ', $mtend);
506 return ($endtime['micro'] +
$endtime['sec']) - ($starttime['micro'] +
$starttime['sec']);
509 // ###################################################################
511 * Fetches the extension of a file by extracting everything after the
516 * @param string Filename
518 * @return string The extension for the specifid file name
520 function fetch_extension($filename)
522 return strval(end(explode('.', $filename)));
525 // ###################################################################
527 * Gets the maximum file size for attachment uploading, as specified by
528 * PHP. If no value is present, 10 MB (represented in bytes) is
533 * @return integer The maximum file upload size in bytes
535 function fetch_max_attachment_size()
537 if ($size = @ini_get('upload_max_filesize'))
539 if (preg_match('#[^0-9].#', $size))
545 return intval($size) * 1048576;
554 // ###################################################################
556 * Scans a specified directory path and returns an array of all the
557 * items in that directory. Directories found by this are end in a "/"
561 * @param string Path to scan
562 * @param bool Whether or not to recursively scan the directories encountered
563 * @param bool Ignore files beginning with a dot
564 * @param bool Ignore 'CVS' dirctories
566 * @return array A list of all the files in the specified path
568 function scandir($path, $recurse = true
, $ignoredot = true
, $ignorecvs = true
, $basepath = '', $unset = 1)
577 if (substr($path, (strlen($path) - 1), 1) != '/')
582 if ($handle = opendir($path))
584 while (($file = readdir($handle)) !== false
)
586 $isdot = ((substr($file, 0, 1) == '.') ? true
: false
);
587 $isdot = (($ignoredot) ?
$isdot : true
);
588 $iscvs = (($file == 'CVS') ? true
: false
);
589 $iscvs = (($ignorecvs) ?
$iscvs : true
);
590 if (!$isdot AND !$iscvs)
592 if (is_dir($path . $file))
594 $filelist["$basepath"][] = "$file/";
597 $this->scandir("$path$file", true
, $ignoredot, $ignorecvs, "$basepath$file/", 0);
602 $filelist["$basepath"][] = $file;
611 // ###################################################################
613 * Changes line breaks into one format
618 * @param string New line break (default is UNIX format)
620 * @return string Text with one type of line break
622 function convert_line_breaks($text, $convert_to = "\n")
625 $text = str_replace(array("\r\n", "\r", "\n"), "\n", $text);
626 $text = str_replace("\n", $convert_to, $text);
630 // ###################################################################
632 * Removes all empty() [by PHP's standards] elements in an array. This
633 * can be used in place of using PREG_SPLIT_NO_EMPTY.
637 * @param array An array to strip empties from
639 * @return array Full-valued array
641 function array_strip_empty($array)
643 foreach ($array AS $key => $value)
645 if (is_array($array["$key"]))
647 $array["$key"] = $this->array_strip_empty($array["$key"]);
649 else if (empty($value) OR is_null($value))
651 unset($array["$key"]);
658 /*=====================================================================*\
659 || ###################################################################
662 || ###################################################################
663 \*=====================================================================*/