Haha. Fegister.php
[isso.git] / Functions2.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework [#]issoversion[#]
5 || # Copyright ©2002-[#]year[#] Blue Static
6 || #
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.
10 || #
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
14 || # more details.
15 || #
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 \*=====================================================================*/
21
22 /**
23 * Static functions (Functions.php)
24 *
25 * @package ISSO
26 */
27
28 /**
29 * Functions
30 *
31 * This is a bunch of static functions. This class is singleton so it
32 * can store data while remaining static.
33 *
34 * @author Blue Static
35 * @copyright Copyright ©2002 - [#]year[#], Blue Static
36 * @version $Revision$
37 * @package ISSO
38 *
39 */
40 class BSFunctions
41 {
42 // ###################################################################
43 /**
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
46 * exist.
47 *
48 * @param string Path
49 *
50 * @return string Path with directory separator ending
51 */
52 public static function FetchSourcePath($source)
53 {
54 if (substr($source, strlen($source) - 1) != DIRECTORY_SEPARATOR)
55 {
56 $source .= DIRECTORY_SEPARATOR;
57 }
58 return $source;
59 }
60
61 // ###################################################################
62 /**
63 * Force-download a file by sending application/octetstream
64 *
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
68 */
69 public static function DownloadFile($file, $name, $exit = true)
70 {
71 if (self::IsBrowser('ie') OR self::IsBrowser('opera') OR self::IsBrowser('safari'))
72 {
73 $mime = 'application/octetstream';
74 }
75 else
76 {
77 $mime = 'application/octet-stream';
78 }
79
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');
86
87 print($file);
88
89 if ($exit)
90 {
91 exit;
92 }
93 }
94
95 // ###################################################################
96 /**
97 * Verify that an email address is valid via regex
98 *
99 * @param string An email address
100 *
101 * @return bool Validity of the email address
102 */
103 public static function IsValidEmail($email)
104 {
105 if (preg_match('#^[a-z0-9\.\-\+_]+?@(.*?\.)*?[a-z0-9\-_]+?\.[a-z]{2,4}$#i', $email))
106 {
107 return true;
108 }
109 else
110 {
111 return false;
112 }
113 }
114
115 // ###################################################################
116 /**
117 * Check a browser's user agent against a pre-determined list
118 *
119 * @param string Browser name
120 * @param string The browser's version
121 *
122 * @param mixed False if there is no match, the version if there is
123 */
124 public static function IsBrowser($check, $version = '')
125 {
126 $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
127 $browser = array();
128 $matches = array();
129
130 // -------------------------------------------------------------------
131 // -- Opera
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)
138 {
139 if (isset($matches[1]))
140 {
141 $browser['opera'] = $matches[1];
142 }
143 }
144
145 // -------------------------------------------------------------------
146 // -- Mac browser
147 // -------------------------------------------------------------------
148 if (strpos($useragent, 'mac') !== false)
149 {
150 $browser['mac'] = true;
151 }
152
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']))
159 {
160 if (isset($matches[1]))
161 {
162 $browser['ie'] = $matches[1];
163 }
164 }
165
166 // -------------------------------------------------------------------
167 // -- Safari
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)
171 {
172 if (isset($matches[1]))
173 {
174 $browser['safari'] = $matches[1];
175 }
176 }
177
178 // -------------------------------------------------------------------
179 // -- Konqueror
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)
184 {
185 if (isset($matches[1]))
186 {
187 $browser['konqueror'] = $matches[1];
188 }
189 }
190
191 // -------------------------------------------------------------------
192 // -- Mozilla
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']))
197 {
198 if (isset($matches[1]))
199 {
200 $browser['mozilla'] = $matches[1];
201 }
202
203 // -------------------------------------------------------------------
204 // -- Firefox
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)
209 {
210 if (isset($matches[3]))
211 {
212 $browser['firefox'] = $matches[3];
213 }
214 }
215
216 // -------------------------------------------------------------------
217 // -- Netscape
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)
221 {
222 if (isset($matches[2]))
223 {
224 $browser['netscape'] = $matches[2];
225 }
226 }
227
228 // -------------------------------------------------------------------
229 // -- Camino
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)
233 {
234 if (isset($matches[1]))
235 {
236 $browser['camino'] = $matches[1];
237 }
238 }
239 }
240
241 if (isset($browser["$check"]))
242 {
243 if ($version)
244 {
245 if ($browser["$check"] >= $version)
246 {
247 return $browser["$check"];
248 }
249 else
250 {
251 return false;
252 }
253 }
254 else
255 {
256 return $browser["$check"];
257 }
258 }
259 else
260 {
261 return false;
262 }
263 }
264
265 // ###################################################################
266 /**
267 * Generates a random string of random length (unless otherwise
268 * specified)
269 *
270 * @param integer Optional length
271 *
272 * @return string A random string
273 */
274 public static function Random($length = 0)
275 {
276 // custom high and lows
277 if (is_array($length))
278 {
279 $length = rand($length[0], $length[1]);
280 }
281 else if (!$length)
282 {
283 // Gimme a length!
284 $length = rand(20, 65);
285 }
286
287 // Number of ints in our salt
288 $intcount = rand(1, intval($length / 2));
289
290 // Number of chars
291 $charcount = $length - $intcount;
292
293 // Upper-case chars
294 $upperchars = rand(1, intval($charcount / 2));
295
296 // Lower-case chars
297 $lowerchars = $charcount - $upperchars;
298
299 // Generate ints
300 for ($i = 0; $i < $intcount; $i++)
301 {
302 $string[ mt_rand() ] = rand(0, 9);
303 }
304
305 // Generate upper chars
306 for ($i = 0; $i < $upperchars; $i++)
307 {
308 $string[ mt_rand() ] = chr(rand(65, 90));
309 }
310
311 // Generate lower chars
312 for ($i = 0; $i < $lowerchars; $i++)
313 {
314 $string[ mt_rand() ] = chr(rand(97, 122));
315 }
316
317 if ($length != sizeof($string))
318 {
319 return $this->rand($length);
320 }
321
322 // Sort the chars by thier random assignment
323 ksort($string);
324
325 // Flatten the array
326 $return = '';
327 foreach ($string AS $char)
328 {
329 $return .= $char;
330 }
331
332 return $return;
333 }
334
335 // ###################################################################
336 /**
337 * Sets the current array position to be the specified key. This
338 * function should be avoided on large arrays.
339 *
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
342 *
343 * @return mixed Return the elelment of the array that we just put the counter to
344 */
345 public static function ArraySetCurrent(&$array, $key)
346 {
347 reset($array);
348 while (current($array) !== false)
349 {
350 if (key($array) == $key)
351 {
352 break;
353 }
354 next($array);
355 }
356 return current($array);
357 }
358
359 // ###################################################################
360 /**
361 * Calculates the microtime difference by taking a given microtime and
362 * subtracting it from the current one
363 *
364 * @param string The start microtime
365 *
366 * @return float Microtime difference
367 */
368 public static function FetchMicrotimeDiff($mtstart)
369 {
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']);
374 }
375
376 // ###################################################################
377 /**
378 * Fetches the extension of a file by extracting everything after the
379 * last period
380 *
381 * @param string Filename
382 *
383 * @return string The extension for the specifid file name
384 */
385 public static function FetchExtension($filename)
386 {
387 return strval(end(explode('.', $filename)));
388 }
389
390 // ###################################################################
391 /**
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
394 * returned.
395 *
396 * @return integer The maximum file upload size in bytes
397 */
398 public static function FetchMaxPhpFileSize()
399 {
400 if ($size = @ini_get('upload_max_filesize'))
401 {
402 if (preg_match('#[^0-9].#', $size))
403 {
404 return $size;
405 }
406 else
407 {
408 return intval($size) * 1048576;
409 }
410 }
411 else
412 {
413 return 10 * 1048576;
414 }
415 }
416
417 // ###################################################################
418 /**
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 "/"
421 *
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
426 *
427 * @return array A list of all the files in the specified path
428 */
429 public static function ScanDirectory($path, $recurse = true, $ignoredot = true, $ignorecvs = true, $basepath = '', $unset = 1)
430 {
431 static $filelist;
432
433 if ($unset)
434 {
435 $filelist = array();
436 }
437
438 if (substr($path, (strlen($path) - 1), 1) != '/')
439 {
440 $path .= '/';
441 }
442
443 if ($handle = opendir($path))
444 {
445 while (($file = readdir($handle)) !== false)
446 {
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)
452 {
453 if (is_dir($path . $file))
454 {
455 $filelist["$basepath"][] = "$file/";
456 if ($recurse)
457 {
458 self::ScanDirectory("$path$file", true, $ignoredot, $ignorecvs, "$basepath$file/", 0);
459 }
460 }
461 else
462 {
463 $filelist["$basepath"][] = $file;
464 }
465 }
466 }
467 closedir($handle);
468 }
469 return $filelist;
470 }
471
472 // ###################################################################
473 /**
474 * Changes line breaks into one format
475 *
476 * @param string Text
477 * @param string New line break (default is UNIX \n format)
478 *
479 * @return string Text with one type of line break
480 */
481 public static function ConvertLineBreaks($text, $convert_to = "\n")
482 {
483 $text = trim($text);
484 $text = str_replace(array("\r\n", "\r", "\n"), "\n", $text);
485 $text = str_replace("\n", $convert_to, $text);
486 return $text;
487 }
488
489 // ###################################################################
490 /**
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.
493 *
494 * @param array An array to strip empties from
495 *
496 * @return array Full-valued array
497 */
498 public static function ArrayStripEmpty($array)
499 {
500 foreach ($array AS $key => $value)
501 {
502 if (is_array($array["$key"]))
503 {
504 $array["$key"] = self::ArrayStripEmpty($array["$key"]);
505 }
506 else if (empty($value) OR is_null($value))
507 {
508 unset($array["$key"]);
509 }
510 }
511 return $array;
512 }
513 }
514
515 /*=====================================================================*\
516 || ###################################################################
517 || # $HeadURL$
518 || # $Id$
519 || ###################################################################
520 \*=====================================================================*/
521 ?>