Added core functions framework.
[isso.git] / functions.php
1 <?php
2 /*=====================================================================*\
3 || ################################################################### ||
4 || # Iris Studios Shared Object Framework [#]version[#]
5 || # --------------------------------------------------------------- # ||
6 || # All parts of this file are ©2003-[#]year[#] Iris Studios, Inc. No # ||
7 || # part of this file may be reproduced in any way: part or whole. # ||
8 || # --------------------------------------------------------------- # ||
9 || # ©2003 - [#]year[#] Iris Studios, Inc. | http://www.iris-studios.com # ||
10 || ################################################################### ||
11 \*=====================================================================*/
12
13 $OBJECT = 'Core Functions';
14 $CLASS = 'Functions';
15 $OBJ = 'funct';
16
17 /**
18 * Globalized function framework
19 *
20 * This framework is a set of functions that are commonly used in most
21 * applications.
22 *
23 * @author Iris Studios, Inc.
24 * @copyright Copyright ©2003 - [#]year[#], Iris Studios, Inc.
25 * @version $Revision$
26 *
27 */
28 class Functions
29 {
30 /**
31 * Global environment variables
32 *
33 * @var cookiepath The path for cookies
34 * @var cookiedom Cookie domain
35 * @var cookieexp The time in which a cookie will expire
36 * @var bgcolour Current background colour of the alternation system
37 */
38 var $cookiepath = '/';
39 var $cookiedom = '';
40 var $cookieexp = 900;
41 var $bgcolour = '';
42
43 /**
44 * Sets a cookie with a friendly interface
45 *
46 * @param str The name of the cookie
47 * @param str Value of the cookie
48 * @param bool Is the cookie going to stay for a while?
49 */
50 function cookie($name, $value = '', $sticky = true)
51 {
52 // expire the cookie
53 if (!$value)
54 {
55 setcookie($name, $value, time() - (2 * $this->cookieexp), $this->cookiepath, $this->cookiedom);
56 }
57 // set the cookie
58 else
59 {
60 if ($sticky)
61 {
62 $expire = time() + 60 * 60 * 24 * 365;
63 }
64 else
65 {
66 $expire = time() + $this->cookieexp;
67 }
68
69 setcookie($name, $value, $expire, $this->cookiepath, $this->cookiedom);
70 }
71 }
72
73 /**
74 * Simple way to protect against HTML attacks with Unicode support
75 *
76 * @param str Unsanitzed text
77 *
78 * @return str Properly protected text that only encodes potential threats
79 */
80 function sanitize($text)
81 {
82 return str_replace(array('<', '>', '"'), array('&lt;', '&gt;', '&quot;'), $text);
83 }
84
85 /**
86 * Takes text that has been processed for HTML and unsanitizes it
87 *
88 * @param str Text that needs to be turned back into HTML
89 *
90 * @return str Unsanitized text
91 */
92 function unsanitize($text)
93 {
94 return str_replace(array('&lt;', '&gt;', '&quot;'), array('<', '>', '"'), $text);
95 }
96
97 /**
98 * Alternate between two background colours
99 *
100 * @param str First CSS class name
101 * @param str Second CSS class name
102 */
103 function exec_swap_bg($class1 = 'alt1', $class2 = 'alt2')
104 {
105 static $count;
106
107 $this->bgcolour = iff($count % 2, $class1, $class2);
108 $count++;
109 }
110
111 /**
112 * Force-download a file by sending application/octetstream
113 *
114 * @param str The text of the file to be streamed
115 * @param str File name of the new file
116 */
117 function download_file($file, $name)
118 {
119 if ($this->is_browser('ie') OR $this->is_browser('opera') OR $this->is_browser('safari'))
120 {
121 $mime = 'application/octetstream';
122 }
123 else
124 {
125 $mime = 'application/octet-stream';
126 }
127
128 header("Content-Type: $mime");
129 header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
130 header('Content-Disposition: attachment; filename="' . $name . '"');
131 header('Content-length: ' . strlen($file));
132 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
133 header('Pragma: public');
134
135 print($file);
136 exit;
137 }
138
139 /**
140 * Verify that an email address is valid via regex
141 *
142 * @param str An email address
143 *
144 * @return bool Validity of the email address
145 */
146 function is_valid_email($email)
147 {
148 if (preg_match('#^[a-z0-9\.\-\+_]+?@(.*?\.)*?[a-z0-9\-_]+?\.[a-z]{2,4}$#i', $email))
149 {
150 return true;
151 }
152 else
153 {
154 return false;
155 }
156 }
157
158 /**
159 * Check a browser's user agent against a pre-determined list
160 *
161 * @param str Browser name
162 * @param str The browser's version
163 *
164 * @param mixed False if there is no match, the version if there is
165 */
166 function is_browser($check, $version = '')
167 {
168 $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
169 $browser = array();
170 $matches = array();
171
172 // -------------------------------------------------------------------
173 // -- Opera
174 // -------------------------------------------------------------------
175 # Opera/6.05 (Windows 98; U) [en]
176 # Mozilla/4.0 (compatible; MSIE 5.0; Windows 98) Opera 6.0 [en]
177 # Mozilla/5.0 (Windows 98; U) Opera 6.0 [en]
178 # Mozilla/4.0 (compatible; MSIE, 6.0; Windows 98) Opera 7.0 [en]
179 if (preg_match('#opera ([0-9\.]+)#', $useragent, $matches) !== false)
180 {
181 if (isset($matches[1]))
182 {
183 $browser['opera'] = $matches[1];
184 }
185 }
186
187 // -------------------------------------------------------------------
188 // -- Mac browser
189 // -------------------------------------------------------------------
190 if (strpos($useragent, 'mac') !== false)
191 {
192 $browser['mac'] = true;
193 }
194
195 // -------------------------------------------------------------------
196 // -- Internet explorer
197 // -------------------------------------------------------------------
198 # Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1; .NET CLR 1.0.2914)
199 # Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)
200 if (preg_match('#msie ([0-9\.]+)#', $useragent, $matches) !== false AND !isset($browser['opera']))
201 {
202 if (isset($matches[1]))
203 {
204 $browser['ie'] = $matches[1];
205 }
206 }
207
208 // -------------------------------------------------------------------
209 // -- Safari
210 // -------------------------------------------------------------------
211 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/125.4 (KHTML, like Gecko) Safari/125.9
212 if (preg_match('#safari/([0-9\.]+)#', $useragent, $matches) !== false)
213 {
214 if (isset($matches[1]))
215 {
216 $browser['safari'] = $matches[1];
217 }
218 }
219
220 // -------------------------------------------------------------------
221 // -- Konqueror
222 // -------------------------------------------------------------------
223 # Mozilla/5.0 (compatible; Konqueror/3)
224 # Mozilla/5.0 (compatible; Konqueror/3.1; i686 Linux; 20020628)
225 if (preg_match('#konqueror/([0-9\.]+)#', $useragent, $matches) !== false)
226 {
227 if (isset($matches[1]))
228 {
229 $browser['konqueror'] = $matches[1];
230 }
231 }
232
233 // -------------------------------------------------------------------
234 // -- Mozilla
235 // -------------------------------------------------------------------
236 # Mozilla/5.001 (windows; U; NT4.0; en-us) Gecko/25250101
237 # Mozilla/5.001 (Macintosh; N; PPC; ja) Gecko/25250101 MegaCorpBrowser/1.0 (MegaCorp, Inc.)
238 if (preg_match('#gecko/([0-9]+)#', $useragent, $matches) !== false AND !isset($browser['safari']))
239 {
240 if (isset($matches[1]))
241 {
242 $browser['mozilla'] = $matches[1];
243 }
244
245 // -------------------------------------------------------------------
246 // -- Firefox
247 // -------------------------------------------------------------------
248 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7) Gecko/20040628 Firefox/0.9.1
249 # Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4a) Gecko/20030423 Firebird Browser/0.6
250 if (preg_match('#(firebird|firefox)( browser)?/([0-9\.]+)#', $useragent, $matches) !== false)
251 {
252 if (isset($matches[3]))
253 {
254 $browser['firefox'] = $matches[3];
255 }
256 }
257
258 // -------------------------------------------------------------------
259 // -- Netscape
260 // -------------------------------------------------------------------
261 # Mozilla/5.0 (Macintosh; U; PPC; en-US; rv:0.9.4.1) Gecko/20020318 Netscape6/6.2.2
262 if (preg_match('#netscape([0-9].*?)?/([0-9\.]+)#', $useragent, $matches) !== false)
263 {
264 if (isset($matches[2]))
265 {
266 $browser['netscape'] = $matches[2];
267 }
268 }
269
270 // -------------------------------------------------------------------
271 // -- Camino
272 // -------------------------------------------------------------------
273 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7) Gecko/20040623 Camino/0.8
274 if (preg_match('#camino/([0-9\.]+)#', $useragent, $matches) !== false)
275 {
276 if (isset($matches[1]))
277 {
278 $browser['camino'] = $matches[1];
279 }
280 }
281 }
282
283 if (isset($browser["$check"]))
284 {
285 if ($version)
286 {
287 if ($browser["$check"] >= $version)
288 {
289 return $browser["$check"];
290 }
291 else
292 {
293 return false;
294 }
295 }
296 else
297 {
298 return $browser["$check"];
299 }
300 }
301 else
302 {
303 return false;
304 }
305 }
306 }
307
308 /*=====================================================================*\
309 || ###################################################################
310 || # $HeadURL$
311 || # $Id$
312 || ###################################################################
313 \*=====================================================================*/
314 ?>