From 619e47778b1100bae5f21cbd44013706b42c3c28 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 16 Jan 2005 22:44:39 +0000 Subject: [PATCH] Added core functions framework. --- functions.php | 314 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 314 insertions(+) create mode 100644 functions.php diff --git a/functions.php b/functions.php new file mode 100644 index 0000000..2532f4d --- /dev/null +++ b/functions.php @@ -0,0 +1,314 @@ +cookieexp), $this->cookiepath, $this->cookiedom); + } + // set the cookie + else + { + if ($sticky) + { + $expire = time() + 60 * 60 * 24 * 365; + } + else + { + $expire = time() + $this->cookieexp; + } + + setcookie($name, $value, $expire, $this->cookiepath, $this->cookiedom); + } + } + + /** + * Simple way to protect against HTML attacks with Unicode support + * + * @param str Unsanitzed text + * + * @return str Properly protected text that only encodes potential threats + */ + function sanitize($text) + { + return str_replace(array('<', '>', '"'), array('<', '>', '"'), $text); + } + + /** + * Takes text that has been processed for HTML and unsanitizes it + * + * @param str Text that needs to be turned back into HTML + * + * @return str Unsanitized text + */ + function unsanitize($text) + { + return str_replace(array('<', '>', '"'), array('<', '>', '"'), $text); + } + + /** + * Alternate between two background colours + * + * @param str First CSS class name + * @param str Second CSS class name + */ + function exec_swap_bg($class1 = 'alt1', $class2 = 'alt2') + { + static $count; + + $this->bgcolour = iff($count % 2, $class1, $class2); + $count++; + } + + /** + * Force-download a file by sending application/octetstream + * + * @param str The text of the file to be streamed + * @param str File name of the new file + */ + function download_file($file, $name) + { + if ($this->is_browser('ie') OR $this->is_browser('opera') OR $this->is_browser('safari')) + { + $mime = 'application/octetstream'; + } + else + { + $mime = 'application/octet-stream'; + } + + header("Content-Type: $mime"); + header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT'); + header('Content-Disposition: attachment; filename="' . $name . '"'); + header('Content-length: ' . strlen($file)); + header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); + header('Pragma: public'); + + print($file); + exit; + } + + /** + * Verify that an email address is valid via regex + * + * @param str An email address + * + * @return bool Validity of the email address + */ + function is_valid_email($email) + { + if (preg_match('#^[a-z0-9\.\-\+_]+?@(.*?\.)*?[a-z0-9\-_]+?\.[a-z]{2,4}$#i', $email)) + { + return true; + } + else + { + return false; + } + } + + /** + * Check a browser's user agent against a pre-determined list + * + * @param str Browser name + * @param str The browser's version + * + * @param mixed False if there is no match, the version if there is + */ + function is_browser($check, $version = '') + { + $useragent = strtolower($_SERVER['HTTP_USER_AGENT']); + $browser = array(); + $matches = array(); + + // ------------------------------------------------------------------- + // -- Opera + // ------------------------------------------------------------------- + # Opera/6.05 (Windows 98; U) [en] + # Mozilla/4.0 (compatible; MSIE 5.0; Windows 98) Opera 6.0 [en] + # Mozilla/5.0 (Windows 98; U) Opera 6.0 [en] + # Mozilla/4.0 (compatible; MSIE, 6.0; Windows 98) Opera 7.0 [en] + if (preg_match('#opera ([0-9\.]+)#', $useragent, $matches) !== false) + { + if (isset($matches[1])) + { + $browser['opera'] = $matches[1]; + } + } + + // ------------------------------------------------------------------- + // -- Mac browser + // ------------------------------------------------------------------- + if (strpos($useragent, 'mac') !== false) + { + $browser['mac'] = true; + } + + // ------------------------------------------------------------------- + // -- Internet explorer + // ------------------------------------------------------------------- + # Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1; .NET CLR 1.0.2914) + # Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0) + if (preg_match('#msie ([0-9\.]+)#', $useragent, $matches) !== false AND !isset($browser['opera'])) + { + if (isset($matches[1])) + { + $browser['ie'] = $matches[1]; + } + } + + // ------------------------------------------------------------------- + // -- Safari + // ------------------------------------------------------------------- + # Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/125.4 (KHTML, like Gecko) Safari/125.9 + if (preg_match('#safari/([0-9\.]+)#', $useragent, $matches) !== false) + { + if (isset($matches[1])) + { + $browser['safari'] = $matches[1]; + } + } + + // ------------------------------------------------------------------- + // -- Konqueror + // ------------------------------------------------------------------- + # Mozilla/5.0 (compatible; Konqueror/3) + # Mozilla/5.0 (compatible; Konqueror/3.1; i686 Linux; 20020628) + if (preg_match('#konqueror/([0-9\.]+)#', $useragent, $matches) !== false) + { + if (isset($matches[1])) + { + $browser['konqueror'] = $matches[1]; + } + } + + // ------------------------------------------------------------------- + // -- Mozilla + // ------------------------------------------------------------------- + # Mozilla/5.001 (windows; U; NT4.0; en-us) Gecko/25250101 + # Mozilla/5.001 (Macintosh; N; PPC; ja) Gecko/25250101 MegaCorpBrowser/1.0 (MegaCorp, Inc.) + if (preg_match('#gecko/([0-9]+)#', $useragent, $matches) !== false AND !isset($browser['safari'])) + { + if (isset($matches[1])) + { + $browser['mozilla'] = $matches[1]; + } + + // ------------------------------------------------------------------- + // -- Firefox + // ------------------------------------------------------------------- + # Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7) Gecko/20040628 Firefox/0.9.1 + # Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4a) Gecko/20030423 Firebird Browser/0.6 + if (preg_match('#(firebird|firefox)( browser)?/([0-9\.]+)#', $useragent, $matches) !== false) + { + if (isset($matches[3])) + { + $browser['firefox'] = $matches[3]; + } + } + + // ------------------------------------------------------------------- + // -- Netscape + // ------------------------------------------------------------------- + # Mozilla/5.0 (Macintosh; U; PPC; en-US; rv:0.9.4.1) Gecko/20020318 Netscape6/6.2.2 + if (preg_match('#netscape([0-9].*?)?/([0-9\.]+)#', $useragent, $matches) !== false) + { + if (isset($matches[2])) + { + $browser['netscape'] = $matches[2]; + } + } + + // ------------------------------------------------------------------- + // -- Camino + // ------------------------------------------------------------------- + # Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7) Gecko/20040623 Camino/0.8 + if (preg_match('#camino/([0-9\.]+)#', $useragent, $matches) !== false) + { + if (isset($matches[1])) + { + $browser['camino'] = $matches[1]; + } + } + } + + if (isset($browser["$check"])) + { + if ($version) + { + if ($browser["$check"] >= $version) + { + return $browser["$check"]; + } + else + { + return false; + } + } + else + { + return $browser["$check"]; + } + } + else + { + return false; + } + } +} + +/*=====================================================================*\ +|| ################################################################### +|| # $HeadURL$ +|| # $Id$ +|| ################################################################### +\*=====================================================================*/ +?> \ No newline at end of file -- 2.22.5