]>
src.bluestatic.org Git - isso.git/blob - Functions2.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 (Fegister.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
42 // ###################################################################
44 * Returns the 'source path' version of a file path. It adds a
45 * directory separator to the end of a string if it does not already
50 * @return string Path with directory separator ending
52 public static function FetchSourcePath($source)
54 if (substr($source, strlen($source) - 1) != DIRECTORY_SEPARATOR
)
56 $source .= DIRECTORY_SEPARATOR
;
61 // ###################################################################
63 * Force-download a file by sending application/octetstream
65 * @param string The text of the file to be streamed
66 * @param string File name of the new file
67 * @param bool Whether or not to die after stringeaming the file
69 public static function DownloadFile($file, $name, $exit = true)
71 if (self
::IsBrowser('ie') OR self
::IsBrowser('opera') OR self
::IsBrowser('safari'))
73 $mime = 'application/octetstream';
77 $mime = 'application/octet-stream';
80 header("Content-Type: $mime");
81 header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
82 header('Content-Disposition: attachment; filename="' . $name . '"');
83 header('Content-length: ' . strlen($file));
84 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
85 header('Pragma: public');
95 // ###################################################################
97 * Verify that an email address is valid via regex
99 * @param string An email address
101 * @return bool Validity of the email address
103 public static function IsValidEmail($email)
105 if (preg_match('#^[a-z0-9\.\-\+_]+?@(.*?\.)*?[a-z0-9\-_]+?\.[a-z]{2,4}$#i', $email))
115 // ###################################################################
117 * Check a browser's user agent against a pre-determined list
119 * @param string Browser name
120 * @param string The browser's version
122 * @param mixed False if there is no match, the version if there is
124 public static function IsBrowser($check, $version = '')
126 $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
130 // -------------------------------------------------------------------
132 // -------------------------------------------------------------------
133 # Opera/6.05 (Windows 98; U) [en]
134 # Mozilla/4.0 (compatible; MSIE 5.0; Windows 98) Opera 6.0 [en]
135 # Mozilla/5.0 (Windows 98; U) Opera 6.0 [en]
136 # Mozilla/4.0 (compatible; MSIE, 6.0; Windows 98) Opera 7.0 [en]
137 if (preg_match('#opera ([0-9\.]+)#', $useragent, $matches) !== false)
139 if (isset($matches[1]))
141 $browser['opera'] = $matches[1];
145 // -------------------------------------------------------------------
147 // -------------------------------------------------------------------
148 if (strpos($useragent, 'mac') !== false)
150 $browser['mac'] = true;
153 // -------------------------------------------------------------------
154 // -- Internet explorer
155 // -------------------------------------------------------------------
156 # Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1; .NET CLR 1.0.2914)
157 # Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)
158 if (preg_match('#msie ([0-9\.]+)#', $useragent, $matches) !== false AND !isset($browser['opera']))
160 if (isset($matches[1]))
162 $browser['ie'] = $matches[1];
166 // -------------------------------------------------------------------
168 // -------------------------------------------------------------------
169 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/125.4 (KHTML, like Gecko) Safari/125.9
170 if (preg_match('#safari/([0-9\.]+)#', $useragent, $matches) !== false)
172 if (isset($matches[1]))
174 $browser['safari'] = $matches[1];
178 // -------------------------------------------------------------------
180 // -------------------------------------------------------------------
181 # Mozilla/5.0 (compatible; Konqueror/3)
182 # Mozilla/5.0 (compatible; Konqueror/3.1; i686 Linux; 20020628)
183 if (preg_match('#konqueror/([0-9\.]+)#', $useragent, $matches) !== false)
185 if (isset($matches[1]))
187 $browser['konqueror'] = $matches[1];
191 // -------------------------------------------------------------------
193 // -------------------------------------------------------------------
194 # Mozilla/5.001 (windows; U; NT4.0; en-us) Gecko/25250101
195 # Mozilla/5.001 (Macintosh; N; PPC; ja) Gecko/25250101 MegaCorpBrowser/1.0 (MegaCorp, Inc.)
196 if (preg_match('#gecko/([0-9]+)#', $useragent, $matches) !== false AND !isset($browser['safari']))
198 if (isset($matches[1]))
200 $browser['mozilla'] = $matches[1];
203 // -------------------------------------------------------------------
205 // -------------------------------------------------------------------
206 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7) Gecko/20040628 Firefox/0.9.1
207 # Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4a) Gecko/20030423 Firebird Browser/0.6
208 if (preg_match('#(firebird|firefox)( browser)?/([0-9\.]+)#', $useragent, $matches) !== false)
210 if (isset($matches[3]))
212 $browser['firefox'] = $matches[3];
216 // -------------------------------------------------------------------
218 // -------------------------------------------------------------------
219 # Mozilla/5.0 (Macintosh; U; PPC; en-US; rv:0.9.4.1) Gecko/20020318 Netscape6/6.2.2
220 if (preg_match('#netscape([0-9].*?)?/([0-9\.]+)#', $useragent, $matches) !== false)
222 if (isset($matches[2]))
224 $browser['netscape'] = $matches[2];
228 // -------------------------------------------------------------------
230 // -------------------------------------------------------------------
231 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7) Gecko/20040623 Camino/0.8
232 if (preg_match('#camino/([0-9\.]+)#', $useragent, $matches) !== false)
234 if (isset($matches[1]))
236 $browser['camino'] = $matches[1];
241 if (isset($browser["$check"]))
245 if ($browser["$check"] >= $version)
247 return $browser["$check"];
256 return $browser["$check"];
265 // ###################################################################
267 * Generates a random string of random length (unless otherwise
270 * @param integer Optional length
272 * @return string A random string
274 public static function Random($length = 0)
276 // custom high and lows
277 if (is_array($length))
279 $length = rand($length[0], $length[1]);
284 $length = rand(20, 65);
287 // Number of ints in our salt
288 $intcount = rand(1, intval($length / 2));
291 $charcount = $length - $intcount;
294 $upperchars = rand(1, intval($charcount / 2));
297 $lowerchars = $charcount - $upperchars;
300 for ($i = 0; $i < $intcount; $i++)
302 $string[ mt_rand() ] = rand(0, 9);
305 // Generate upper chars
306 for ($i = 0; $i < $upperchars; $i++)
308 $string[ mt_rand() ] = chr(rand(65, 90));
311 // Generate lower chars
312 for ($i = 0; $i < $lowerchars; $i++)
314 $string[ mt_rand() ] = chr(rand(97, 122));
317 if ($length != sizeof($string))
319 return $this->rand($length);
322 // Sort the chars by thier random assignment
327 foreach ($string AS $char)
335 // ###################################################################
337 * Sets the current array position to be the specified key. This
338 * function should be avoided on large arrays.
340 * @param array The array whose counter is to be updated
341 * @param mixed The key of the element of the array that the counter is to be set to
343 * @return mixed Return the elelment of the array that we just put the counter to
345 public static function ArraySetCurrent(&$array, $key)
348 while (current($array) !== false)
350 if (key($array) == $key)
356 return current($array);
359 // ###################################################################
361 * Calculates the microtime difference by taking a given microtime and
362 * subtracting it from the current one
364 * @param string The start microtime
366 * @return float Microtime difference
368 public static function FetchMicrotimeDiff($mtstart)
370 $mtend = microtime();
371 list ($starttime['micro'], $starttime['sec']) = explode(' ', $mtstart);
372 list ($endtime['micro'], $endtime['sec']) = explode(' ', $mtend);
373 return ($endtime['micro'] + $endtime['sec']) - ($starttime['micro'] + $starttime['sec']);
376 // ###################################################################
378 * Fetches the extension of a file by extracting everything after the
381 * @param string Filename
383 * @return string The extension for the specifid file name
385 public static function FetchExtension($filename)
387 return strval(end(explode('.', $filename)));
390 // ###################################################################
392 * Gets the maximum file size for attachment uploading, as specified by
393 * PHP. If no value is present, 10 MB (represented in bytes) is
396 * @return integer The maximum file upload size in bytes
398 public static function FetchMaxPhpFileSize()
400 if ($size = @ini_get('upload_max_filesize'))
402 if (preg_match('#[^0-9].#', $size))
408 return intval($size) * 1048576;
417 // ###################################################################
419 * Scans a specified directory path and returns an array of all the
420 * items in that directory. Directories found by this are end in a "/"
422 * @param string Path to scan
423 * @param bool Whether or not to recursively scan the directories encountered
424 * @param bool Ignore files beginning with a dot
425 * @param bool Ignore 'CVS' dirctories
427 * @return array A list of all the files in the specified path
429 public static function ScanDirectory($path, $recurse = true, $ignoredot = true, $ignorecvs = true, $basepath = '', $unset = 1)
438 if (substr($path, (strlen($path) - 1), 1) != '/')
443 if ($handle = opendir($path))
445 while (($file = readdir($handle)) !== false)
447 $isdot = ((substr($file, 0, 1) == '.') ? true : false);
448 $isdot = (($ignoredot) ? $isdot : true);
449 $iscvs = (($file == 'CVS') ? true : false);
450 $iscvs = (($ignorecvs) ? $iscvs : true);
451 if (!$isdot AND !$iscvs)
453 if (is_dir($path . $file))
455 $filelist["$basepath"][] = "$file/";
458 self
::ScanDirectory("$path$file", true, $ignoredot, $ignorecvs, "$basepath$file/", 0);
463 $filelist["$basepath"][] = $file;
472 // ###################################################################
474 * Changes line breaks into one format
477 * @param string New line break (default is UNIX \n format)
479 * @return string Text with one type of line break
481 public static function ConvertLineBreaks($text, $convert_to = "\n")
484 $text = str_replace(array("\r\n", "\r", "\n"), "\n", $text);
485 $text = str_replace("\n", $convert_to, $text);
489 // ###################################################################
491 * Removes all empty() [by PHP's standards] elements in an array. This
492 * can be used in place of using PREG_SPLIT_NO_EMPTY.
494 * @param array An array to strip empties from
496 * @return array Full-valued array
498 public static function ArrayStripEmpty($array)
500 foreach ($array AS $key => $value)
502 if (is_array($array["$key"]))
504 $array["$key"] = self
::ArrayStripEmpty($array["$key"]);
506 else if (empty($value) OR is_null($value))
508 unset($array["$key"]);
515 /*=====================================================================*\
516 || ###################################################################
519 || ###################################################################
520 \*=====================================================================*/