Added input sanitize functions. The new system works by merging $_GET and $_POST...
[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 * Alternate between two background colours
75 *
76 * @param str First CSS class name
77 * @param str Second CSS class name
78 */
79 function exec_swap_bg($class1 = 'alt1', $class2 = 'alt2')
80 {
81 static $count;
82
83 $this->bgcolour = iff($count % 2, $class1, $class2);
84 $count++;
85 }
86
87 /**
88 * Force-download a file by sending application/octetstream
89 *
90 * @param str The text of the file to be streamed
91 * @param str File name of the new file
92 */
93 function download_file($file, $name)
94 {
95 if ($this->is_browser('ie') OR $this->is_browser('opera') OR $this->is_browser('safari'))
96 {
97 $mime = 'application/octetstream';
98 }
99 else
100 {
101 $mime = 'application/octet-stream';
102 }
103
104 header("Content-Type: $mime");
105 header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
106 header('Content-Disposition: attachment; filename="' . $name . '"');
107 header('Content-length: ' . strlen($file));
108 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
109 header('Pragma: public');
110
111 print($file);
112 exit;
113 }
114
115 /**
116 * Verify that an email address is valid via regex
117 *
118 * @param str An email address
119 *
120 * @return bool Validity of the email address
121 */
122 function is_valid_email($email)
123 {
124 if (preg_match('#^[a-z0-9\.\-\+_]+?@(.*?\.)*?[a-z0-9\-_]+?\.[a-z]{2,4}$#i', $email))
125 {
126 return true;
127 }
128 else
129 {
130 return false;
131 }
132 }
133
134 /**
135 * Check a browser's user agent against a pre-determined list
136 *
137 * @param str Browser name
138 * @param str The browser's version
139 *
140 * @param mixed False if there is no match, the version if there is
141 */
142 function is_browser($check, $version = '')
143 {
144 $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
145 $browser = array();
146 $matches = array();
147
148 // -------------------------------------------------------------------
149 // -- Opera
150 // -------------------------------------------------------------------
151 # Opera/6.05 (Windows 98; U) [en]
152 # Mozilla/4.0 (compatible; MSIE 5.0; Windows 98) Opera 6.0 [en]
153 # Mozilla/5.0 (Windows 98; U) Opera 6.0 [en]
154 # Mozilla/4.0 (compatible; MSIE, 6.0; Windows 98) Opera 7.0 [en]
155 if (preg_match('#opera ([0-9\.]+)#', $useragent, $matches) !== false)
156 {
157 if (isset($matches[1]))
158 {
159 $browser['opera'] = $matches[1];
160 }
161 }
162
163 // -------------------------------------------------------------------
164 // -- Mac browser
165 // -------------------------------------------------------------------
166 if (strpos($useragent, 'mac') !== false)
167 {
168 $browser['mac'] = true;
169 }
170
171 // -------------------------------------------------------------------
172 // -- Internet explorer
173 // -------------------------------------------------------------------
174 # Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1; .NET CLR 1.0.2914)
175 # Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)
176 if (preg_match('#msie ([0-9\.]+)#', $useragent, $matches) !== false AND !isset($browser['opera']))
177 {
178 if (isset($matches[1]))
179 {
180 $browser['ie'] = $matches[1];
181 }
182 }
183
184 // -------------------------------------------------------------------
185 // -- Safari
186 // -------------------------------------------------------------------
187 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/125.4 (KHTML, like Gecko) Safari/125.9
188 if (preg_match('#safari/([0-9\.]+)#', $useragent, $matches) !== false)
189 {
190 if (isset($matches[1]))
191 {
192 $browser['safari'] = $matches[1];
193 }
194 }
195
196 // -------------------------------------------------------------------
197 // -- Konqueror
198 // -------------------------------------------------------------------
199 # Mozilla/5.0 (compatible; Konqueror/3)
200 # Mozilla/5.0 (compatible; Konqueror/3.1; i686 Linux; 20020628)
201 if (preg_match('#konqueror/([0-9\.]+)#', $useragent, $matches) !== false)
202 {
203 if (isset($matches[1]))
204 {
205 $browser['konqueror'] = $matches[1];
206 }
207 }
208
209 // -------------------------------------------------------------------
210 // -- Mozilla
211 // -------------------------------------------------------------------
212 # Mozilla/5.001 (windows; U; NT4.0; en-us) Gecko/25250101
213 # Mozilla/5.001 (Macintosh; N; PPC; ja) Gecko/25250101 MegaCorpBrowser/1.0 (MegaCorp, Inc.)
214 if (preg_match('#gecko/([0-9]+)#', $useragent, $matches) !== false AND !isset($browser['safari']))
215 {
216 if (isset($matches[1]))
217 {
218 $browser['mozilla'] = $matches[1];
219 }
220
221 // -------------------------------------------------------------------
222 // -- Firefox
223 // -------------------------------------------------------------------
224 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7) Gecko/20040628 Firefox/0.9.1
225 # Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4a) Gecko/20030423 Firebird Browser/0.6
226 if (preg_match('#(firebird|firefox)( browser)?/([0-9\.]+)#', $useragent, $matches) !== false)
227 {
228 if (isset($matches[3]))
229 {
230 $browser['firefox'] = $matches[3];
231 }
232 }
233
234 // -------------------------------------------------------------------
235 // -- Netscape
236 // -------------------------------------------------------------------
237 # Mozilla/5.0 (Macintosh; U; PPC; en-US; rv:0.9.4.1) Gecko/20020318 Netscape6/6.2.2
238 if (preg_match('#netscape([0-9].*?)?/([0-9\.]+)#', $useragent, $matches) !== false)
239 {
240 if (isset($matches[2]))
241 {
242 $browser['netscape'] = $matches[2];
243 }
244 }
245
246 // -------------------------------------------------------------------
247 // -- Camino
248 // -------------------------------------------------------------------
249 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7) Gecko/20040623 Camino/0.8
250 if (preg_match('#camino/([0-9\.]+)#', $useragent, $matches) !== false)
251 {
252 if (isset($matches[1]))
253 {
254 $browser['camino'] = $matches[1];
255 }
256 }
257 }
258
259 if (isset($browser["$check"]))
260 {
261 if ($version)
262 {
263 if ($browser["$check"] >= $version)
264 {
265 return $browser["$check"];
266 }
267 else
268 {
269 return false;
270 }
271 }
272 else
273 {
274 return $browser["$check"];
275 }
276 }
277 else
278 {
279 return false;
280 }
281 }
282 }
283
284 /*=====================================================================*\
285 || ###################################################################
286 || # $HeadURL$
287 || # $Id$
288 || ###################################################################
289 \*=====================================================================*/
290 ?>