In mail.php, we don't want to double encode fields so in send() create local variable...
[isso.git] / functions.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
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 * Globalized Functions
24 * functions.php
25 *
26 * @package ISSO
27 */
28
29 /**
30 * Globalized Functions
31 *
32 * This framework is a set of functions that are commonly used in most
33 * applications.
34 *
35 * @author Blue Static
36 * @copyright Copyright ©2002 - [#]year[#], Blue Static
37 * @version $Revision$
38 * @package ISSO
39 *
40 */
41 class Functions
42 {
43 /**
44 * Framework registry object
45 * @var object
46 * @access private
47 */
48 var $registry = null;
49
50 /**
51 * The path that is used to set cookies
52 * @var string
53 * @access private
54 */
55 var $cookiepath = '/';
56
57 /**
58 * The domain used for cookie setting
59 * @var string
60 * @access private
61 */
62 var $cookiedom = '';
63
64 /**
65 * The time it takes for a cookie to expire
66 * @var integer
67 * @access private
68 */
69 var $cookieexp = 900;
70
71 /**
72 * State of the current background colour during alternation
73 * @var string
74 * @access public
75 */
76 var $bgcolour = '';
77
78 // ###################################################################
79 /**
80 * Constructor
81 */
82 function __construct(&$registry)
83 {
84 $this->registry =& $registry;
85 }
86
87 // ###################################################################
88 /**
89 * (PHP 4) Constructor
90 */
91 function Functions(&$registry)
92 {
93 $this->__construct($registry);
94 }
95
96 // ###################################################################
97 /**
98 * Sets the cookie path
99 *
100 * @access public
101 *
102 * @param string Cookie path
103 */
104 function setCookiePath($path)
105 {
106 $this->cookiepath = $path;
107 }
108
109 // ###################################################################
110 /**
111 * Gets the cookie path
112 *
113 * @access public
114 *
115 * @return string The cookie path
116 */
117 function getCookiePath()
118 {
119 return $this->cookiepath;
120 }
121
122 // ###################################################################
123 /**
124 * Sets the cookie domain
125 *
126 * @access public
127 *
128 * @param string Cookie domain
129 */
130 function setCookieDomain($domain)
131 {
132 $this->cookiedom = $domain;
133 }
134
135 // ###################################################################
136 /**
137 * Gets the cookie domain
138 *
139 * @access public
140 *
141 * @return string The cookie domain
142 */
143 function getCookieDomain()
144 {
145 return $this->cookiedom;
146 }
147
148 // ###################################################################
149 /**
150 * Sets the cookie expiration time
151 *
152 * @access public
153 *
154 * @param integer Cookie expiration time
155 */
156 function setCookieExpiration($exp)
157 {
158 $this->cookieexp = $exp;
159 }
160
161 // ###################################################################
162 /**
163 * Gets the cookie expiration time
164 *
165 * @access public
166 *
167 * @return integer The cookie expiration time
168 */
169 function getCookieExpiration()
170 {
171 return $this->cookieexp;
172 }
173
174 // ###################################################################
175 /**
176 * Sets a cookie with a friendly interface
177 *
178 * @access public
179 *
180 * @param string The name of the cookie
181 * @param string Value of the cookie
182 * @param bool Is the cookie permanent?
183 */
184 function cookie($name, $value = '', $sticky = true)
185 {
186 // expire the cookie
187 if (!$value)
188 {
189 setcookie($name, $value, time() - (2 * $this->cookieexp), $this->cookiepath, $this->cookiedom);
190 }
191 // set the cookie
192 else
193 {
194 if ($sticky)
195 {
196 $expire = time() + 60 * 60 * 24 * 365;
197 }
198 else
199 {
200 $expire = time() + $this->cookieexp;
201 }
202
203 setcookie($name, $value, $expire, $this->cookiepath, $this->cookiedom);
204 }
205 }
206
207 // ###################################################################
208 /**
209 * Alternate between two background colours
210 *
211 * @access public
212 *
213 * @param string First CSS class name
214 * @param string Second CSS class name
215 */
216 function exec_swap_bg($class1 = 'alt1', $class2 = 'alt2')
217 {
218 static $count;
219
220 $this->bgcolour = ($count % 2) ? $class1 : $class2;
221 $count++;
222 }
223
224 // ###################################################################
225 /**
226 * Force-download a file by sending application/octetstream
227 *
228 * @access public
229 *
230 * @param string The text of the file to be streamed
231 * @param string File name of the new file
232 * @param bool Whether or not to die after stringeaming the file
233 */
234 function download_file($file, $name, $exit = true)
235 {
236 if ($this->is_browser('ie') OR $this->is_browser('opera') OR $this->is_browser('safari'))
237 {
238 $mime = 'application/octetstream';
239 }
240 else
241 {
242 $mime = 'application/octet-stream';
243 }
244
245 header("Content-Type: $mime");
246 header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
247 header('Content-Disposition: attachment; filename="' . $name . '"');
248 header('Content-length: ' . strlen($file));
249 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
250 header('Pragma: public');
251
252 print($file);
253
254 if ($exit)
255 {
256 exit;
257 }
258 }
259
260 // ###################################################################
261 /**
262 * Verify that an email address is valid via regex
263 *
264 * @access public
265 *
266 * @param string An email address
267 *
268 * @return bool Validity of the email address
269 */
270 function is_valid_email($email)
271 {
272 if (preg_match('#^[a-z0-9\.\-\+_]+?@(.*?\.)*?[a-z0-9\-_]+?\.[a-z]{2,4}$#i', $email))
273 {
274 return true;
275 }
276 else
277 {
278 return false;
279 }
280 }
281
282 // ###################################################################
283 /**
284 * Check a browser's user agent against a pre-determined list
285 *
286 * @access public
287 *
288 * @param string Browser name
289 * @param string The browser's version
290 *
291 * @param mixed False if there is no match, the version if there is
292 */
293 function is_browser($check, $version = '')
294 {
295 $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
296 $browser = array();
297 $matches = array();
298
299 // -------------------------------------------------------------------
300 // -- Opera
301 // -------------------------------------------------------------------
302 # Opera/6.05 (Windows 98; U) [en]
303 # Mozilla/4.0 (compatible; MSIE 5.0; Windows 98) Opera 6.0 [en]
304 # Mozilla/5.0 (Windows 98; U) Opera 6.0 [en]
305 # Mozilla/4.0 (compatible; MSIE, 6.0; Windows 98) Opera 7.0 [en]
306 if (preg_match('#opera ([0-9\.]+)#', $useragent, $matches) !== false)
307 {
308 if (isset($matches[1]))
309 {
310 $browser['opera'] = $matches[1];
311 }
312 }
313
314 // -------------------------------------------------------------------
315 // -- Mac browser
316 // -------------------------------------------------------------------
317 if (strpos($useragent, 'mac') !== false)
318 {
319 $browser['mac'] = true;
320 }
321
322 // -------------------------------------------------------------------
323 // -- Internet explorer
324 // -------------------------------------------------------------------
325 # Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1; .NET CLR 1.0.2914)
326 # Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)
327 if (preg_match('#msie ([0-9\.]+)#', $useragent, $matches) !== false AND !isset($browser['opera']))
328 {
329 if (isset($matches[1]))
330 {
331 $browser['ie'] = $matches[1];
332 }
333 }
334
335 // -------------------------------------------------------------------
336 // -- Safari
337 // -------------------------------------------------------------------
338 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/125.4 (KHTML, like Gecko) Safari/125.9
339 if (preg_match('#safari/([0-9\.]+)#', $useragent, $matches) !== false)
340 {
341 if (isset($matches[1]))
342 {
343 $browser['safari'] = $matches[1];
344 }
345 }
346
347 // -------------------------------------------------------------------
348 // -- Konqueror
349 // -------------------------------------------------------------------
350 # Mozilla/5.0 (compatible; Konqueror/3)
351 # Mozilla/5.0 (compatible; Konqueror/3.1; i686 Linux; 20020628)
352 if (preg_match('#konqueror/([0-9\.]+)#', $useragent, $matches) !== false)
353 {
354 if (isset($matches[1]))
355 {
356 $browser['konqueror'] = $matches[1];
357 }
358 }
359
360 // -------------------------------------------------------------------
361 // -- Mozilla
362 // -------------------------------------------------------------------
363 # Mozilla/5.001 (windows; U; NT4.0; en-us) Gecko/25250101
364 # Mozilla/5.001 (Macintosh; N; PPC; ja) Gecko/25250101 MegaCorpBrowser/1.0 (MegaCorp, Inc.)
365 if (preg_match('#gecko/([0-9]+)#', $useragent, $matches) !== false AND !isset($browser['safari']))
366 {
367 if (isset($matches[1]))
368 {
369 $browser['mozilla'] = $matches[1];
370 }
371
372 // -------------------------------------------------------------------
373 // -- Firefox
374 // -------------------------------------------------------------------
375 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7) Gecko/20040628 Firefox/0.9.1
376 # Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4a) Gecko/20030423 Firebird Browser/0.6
377 if (preg_match('#(firebird|firefox)( browser)?/([0-9\.]+)#', $useragent, $matches) !== false)
378 {
379 if (isset($matches[3]))
380 {
381 $browser['firefox'] = $matches[3];
382 }
383 }
384
385 // -------------------------------------------------------------------
386 // -- Netscape
387 // -------------------------------------------------------------------
388 # Mozilla/5.0 (Macintosh; U; PPC; en-US; rv:0.9.4.1) Gecko/20020318 Netscape6/6.2.2
389 if (preg_match('#netscape([0-9].*?)?/([0-9\.]+)#', $useragent, $matches) !== false)
390 {
391 if (isset($matches[2]))
392 {
393 $browser['netscape'] = $matches[2];
394 }
395 }
396
397 // -------------------------------------------------------------------
398 // -- Camino
399 // -------------------------------------------------------------------
400 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7) Gecko/20040623 Camino/0.8
401 if (preg_match('#camino/([0-9\.]+)#', $useragent, $matches) !== false)
402 {
403 if (isset($matches[1]))
404 {
405 $browser['camino'] = $matches[1];
406 }
407 }
408 }
409
410 if (isset($browser["$check"]))
411 {
412 if ($version)
413 {
414 if ($browser["$check"] >= $version)
415 {
416 return $browser["$check"];
417 }
418 else
419 {
420 return false;
421 }
422 }
423 else
424 {
425 return $browser["$check"];
426 }
427 }
428 else
429 {
430 return false;
431 }
432 }
433
434 // ###################################################################
435 /**
436 * Generates a random string of random length (unless otherwise
437 * specified)
438 *
439 * @access public
440 *
441 * @param integer Optional length
442 *
443 * @return string A random string
444 */
445 function rand($length = 0)
446 {
447 // custom high and lows
448 if (is_array($length))
449 {
450 $length = rand($length[0], $length[1]);
451 }
452 else if (!$length)
453 {
454 // Gimme a length!
455 $length = rand(20, 65);
456 }
457
458 // Number of ints in our salt
459 $intcount = rand(1, intval($length / 2));
460
461 // Number of chars
462 $charcount = $length - $intcount;
463
464 // Upper-case chars
465 $upperchars = rand(1, intval($charcount / 2));
466
467 // Lower-case chars
468 $lowerchars = $charcount - $upperchars;
469
470 // Generate ints
471 for ($i = 0; $i < $intcount; $i++)
472 {
473 $string[ mt_rand() ] = rand(0, 9);
474 }
475
476 // Generate upper chars
477 for ($i = 0; $i < $upperchars; $i++)
478 {
479 $string[ mt_rand() ] = chr(rand(65, 90));
480 }
481
482 // Generate lower chars
483 for ($i = 0; $i < $lowerchars; $i++)
484 {
485 $string[ mt_rand() ] = chr(rand(97, 122));
486 }
487
488 // Sort the chars by thier random assignment
489 ksort($string);
490
491 // Flatten the array
492 $return = '';
493 foreach ($string AS $char)
494 {
495 $return .= $char;
496 }
497
498 return $return;
499 }
500
501 // ###################################################################
502 /**
503 * Sets the current array position to be the specified key. This
504 * function should be avoided on large arrays.
505 *
506 * @access public
507 *
508 * @param array The array whose counter is to be updated
509 * @param mixed The key of the element of the array that the counter is to be set to
510 *
511 * @return mixed Return the elelment of the array that we just put the counter to
512 */
513 function array_set_current(&$array, $key)
514 {
515 reset($array);
516 while (current($array) !== false)
517 {
518 if (key($array) == $key)
519 {
520 break;
521 }
522 next($array);
523 }
524 return current($array);
525 }
526
527 // ###################################################################
528 /**
529 * Calculates the microtime difference by taking a given microtime and
530 * subtracting it from the current one
531 *
532 * @access public
533 *
534 * @param string The start microtime
535 *
536 * @return float Microtime difference
537 */
538 function fetch_microtime_diff($mtstart)
539 {
540 $mtend = microtime();
541 list ($starttime['micro'], $starttime['sec']) = explode(' ', $mtstart);
542 list ($endtime['micro'], $endtime['sec']) = explode(' ', $mtend);
543 return ($endtime['micro'] + $endtime['sec']) - ($starttime['micro'] + $starttime['sec']);
544 }
545
546 // ###################################################################
547 /**
548 * Fetches the extension of a file by extracting everything after the
549 * last period
550 *
551 * @access public
552 *
553 * @param string Filename
554 *
555 * @return string The extension for the specifid file name
556 */
557 function fetch_extension($filename)
558 {
559 return strval(end(explode('.', $filename)));
560 }
561
562 // ###################################################################
563 /**
564 * Gets the maximum file size for attachment uploading, as specified by
565 * PHP. If no value is present, 10 MB (represented in bytes) is
566 * returned.
567 *
568 * @access public
569 *
570 * @return integer The maximum file upload size in bytes
571 */
572 function fetch_max_attachment_size()
573 {
574 if ($size = @ini_get('upload_max_filesize'))
575 {
576 if (preg_match('#[^0-9].#', $size))
577 {
578 return $size;
579 }
580 else
581 {
582 return intval($size) * 1048576;
583 }
584 }
585 else
586 {
587 return 10 * 1048576;
588 }
589 }
590
591 // ###################################################################
592 /**
593 * Scans a specified directory path and returns an array of all the
594 * items in that directory. Directories found by this are end in a "/"
595 *
596 * @access public
597 *
598 * @param string Path to scan
599 * @param bool Whether or not to recursively scan the directories encountered
600 * @param bool Ignore files beginning with a dot
601 * @param bool Ignore 'CVS' dirctories
602 *
603 * @return array A list of all the files in the specified path
604 */
605 function scandir($path, $recurse = true, $ignoredot = true, $ignorecvs = true, $basepath = '', $unset = 1)
606 {
607 static $filelist;
608
609 if ($unset)
610 {
611 $filelist = array();
612 }
613
614 if (substr($path, (strlen($path) - 1), 1) != '/')
615 {
616 $path .= '/';
617 }
618
619 if ($handle = opendir($path))
620 {
621 while (($file = readdir($handle)) !== false)
622 {
623 $isdot = ((substr($file, 0, 1) == '.') ? true : false);
624 $isdot = (($ignoredot) ? $isdot : true);
625 $iscvs = (($file == 'CVS') ? true : false);
626 $iscvs = (($ignorecvs) ? $iscvs : true);
627 if (!$isdot AND !$iscvs)
628 {
629 if (is_dir($path . $file))
630 {
631 $filelist["$basepath"][] = "$file/";
632 if ($recurse)
633 {
634 $this->scandir("$path$file", true, $ignoredot, $ignorecvs, "$basepath$file/", 0);
635 }
636 }
637 else
638 {
639 $filelist["$basepath"][] = $file;
640 }
641 }
642 }
643 closedir($handle);
644 }
645 return $filelist;
646 }
647
648 // ###################################################################
649 /**
650 * Changes line breaks into one format
651 *
652 * @access public
653 *
654 * @param string Text
655 * @param string New line break (default is UNIX format)
656 *
657 * @return string Text with one type of line break
658 */
659 function convert_line_breaks($text, $convert_to = "\n")
660 {
661 $text = trim($text);
662 $text = str_replace(array("\r\n", "\r", "\n"), "\n", $text);
663 $text = str_replace("\n", $convert_to, $text);
664 return $text;
665 }
666
667 // ###################################################################
668 /**
669 * Removes all empty() [by PHP's standards] elements in an array. This
670 * can be used in place of using PREG_SPLIT_NO_EMPTY.
671 *
672 * @access public
673 *
674 * @param array An array to strip empties from
675 *
676 * @return array Full-valued array
677 */
678 function array_strip_empty($array)
679 {
680 foreach ($array AS $key => $value)
681 {
682 if (is_array($array["$key"]))
683 {
684 $array["$key"] = $this->array_strip_empty($array["$key"]);
685 }
686 else if (empty($value) OR is_null($value))
687 {
688 unset($array["$key"]);
689 }
690 }
691 return $array;
692 }
693 }
694
695 /*=====================================================================*\
696 || ###################################################################
697 || # $HeadURL$
698 || # $Id$
699 || ###################################################################
700 \*=====================================================================*/
701 ?>