Added fetch_extension() method
[isso.git] / functions.php
1 <?php
2 /*=====================================================================*\
3 || ################################################################### ||
4 || # Iris Studios Shared Object Framework [#]version[#]
5 || # --------------------------------------------------------------- # ||
6 || # Copyright ©2002-[#]year[#] by Iris Studios, Inc. All Rights Reserved. # ||
7 || # This file may not be reproduced in any way without permission. # ||
8 || # --------------------------------------------------------------- # ||
9 || # User License Agreement at http://www.iris-studios.com/license/ # ||
10 || ################################################################### ||
11 \*=====================================================================*/
12
13 /**
14 * Globalized Functions
15 * functions.php
16 *
17 * @package ISSO
18 */
19
20 $OBJECT = 'Core Functions';
21 $CLASS = 'Functions';
22 $OBJ = 'funct';
23
24 /**
25 * Globalized Functions
26 *
27 * This framework is a set of functions that are commonly used in most
28 * applications.
29 *
30 * @author Iris Studios, Inc.
31 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
32 * @version $Revision$
33 * @package ISSO
34 *
35 */
36 class Functions
37 {
38 /**
39 * The path that is used to set cookies
40 * @var str
41 * @see cookie()
42 */
43 var $cookiepath = '/';
44
45 /**
46 * The domain used for cookie setting
47 * @var str
48 * @see cookie()
49 */
50 var $cookiedom = '';
51
52 /**
53 * The time it takes for a cookie to expire
54 * @var int
55 * @see cookie()
56 */
57 var $cookieexp = 900;
58
59 /**
60 * State of the current background colour during alternation
61 * @var str
62 * @see exec_swap_bg()
63 */
64 var $bgcolour = '';
65
66 /**
67 * Sets a cookie with a friendly interface
68 *
69 * @param str The name of the cookie
70 * @param str Value of the cookie
71 * @param bool Is the cookie going to stay for a while?
72 */
73 function cookie($name, $value = '', $sticky = true)
74 {
75 // expire the cookie
76 if (!$value)
77 {
78 setcookie($name, $value, time() - (2 * $this->cookieexp), $this->cookiepath, $this->cookiedom);
79 }
80 // set the cookie
81 else
82 {
83 if ($sticky)
84 {
85 $expire = time() + 60 * 60 * 24 * 365;
86 }
87 else
88 {
89 $expire = time() + $this->cookieexp;
90 }
91
92 setcookie($name, $value, $expire, $this->cookiepath, $this->cookiedom);
93 }
94 }
95
96 /**
97 * Alternate between two background colours
98 *
99 * @param str First CSS class name
100 * @param str Second CSS class name
101 */
102 function exec_swap_bg($class1 = 'alt1', $class2 = 'alt2')
103 {
104 static $count;
105
106 $this->bgcolour = ($count % 2) ? $class1 : $class2;
107 $count++;
108 }
109
110 /**
111 * Force-download a file by sending application/octetstream
112 *
113 * @param str The text of the file to be streamed
114 * @param str File name of the new file
115 */
116 function download_file($file, $name)
117 {
118 if ($this->is_browser('ie') OR $this->is_browser('opera') OR $this->is_browser('safari'))
119 {
120 $mime = 'application/octetstream';
121 }
122 else
123 {
124 $mime = 'application/octet-stream';
125 }
126
127 header("Content-Type: $mime");
128 header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
129 header('Content-Disposition: attachment; filename="' . $name . '"');
130 header('Content-length: ' . strlen($file));
131 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
132 header('Pragma: public');
133
134 print($file);
135 exit;
136 }
137
138 /**
139 * Verify that an email address is valid via regex
140 *
141 * @param str An email address
142 *
143 * @return bool Validity of the email address
144 */
145 function is_valid_email($email)
146 {
147 if (preg_match('#^[a-z0-9\.\-\+_]+?@(.*?\.)*?[a-z0-9\-_]+?\.[a-z]{2,4}$#i', $email))
148 {
149 return true;
150 }
151 else
152 {
153 return false;
154 }
155 }
156
157 /**
158 * Check a browser's user agent against a pre-determined list
159 *
160 * @param str Browser name
161 * @param str The browser's version
162 *
163 * @param mixed False if there is no match, the version if there is
164 */
165 function is_browser($check, $version = '')
166 {
167 $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
168 $browser = array();
169 $matches = array();
170
171 // -------------------------------------------------------------------
172 // -- Opera
173 // -------------------------------------------------------------------
174 # Opera/6.05 (Windows 98; U) [en]
175 # Mozilla/4.0 (compatible; MSIE 5.0; Windows 98) Opera 6.0 [en]
176 # Mozilla/5.0 (Windows 98; U) Opera 6.0 [en]
177 # Mozilla/4.0 (compatible; MSIE, 6.0; Windows 98) Opera 7.0 [en]
178 if (preg_match('#opera ([0-9\.]+)#', $useragent, $matches) !== false)
179 {
180 if (isset($matches[1]))
181 {
182 $browser['opera'] = $matches[1];
183 }
184 }
185
186 // -------------------------------------------------------------------
187 // -- Mac browser
188 // -------------------------------------------------------------------
189 if (strpos($useragent, 'mac') !== false)
190 {
191 $browser['mac'] = true;
192 }
193
194 // -------------------------------------------------------------------
195 // -- Internet explorer
196 // -------------------------------------------------------------------
197 # Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1; .NET CLR 1.0.2914)
198 # Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)
199 if (preg_match('#msie ([0-9\.]+)#', $useragent, $matches) !== false AND !isset($browser['opera']))
200 {
201 if (isset($matches[1]))
202 {
203 $browser['ie'] = $matches[1];
204 }
205 }
206
207 // -------------------------------------------------------------------
208 // -- Safari
209 // -------------------------------------------------------------------
210 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/125.4 (KHTML, like Gecko) Safari/125.9
211 if (preg_match('#safari/([0-9\.]+)#', $useragent, $matches) !== false)
212 {
213 if (isset($matches[1]))
214 {
215 $browser['safari'] = $matches[1];
216 }
217 }
218
219 // -------------------------------------------------------------------
220 // -- Konqueror
221 // -------------------------------------------------------------------
222 # Mozilla/5.0 (compatible; Konqueror/3)
223 # Mozilla/5.0 (compatible; Konqueror/3.1; i686 Linux; 20020628)
224 if (preg_match('#konqueror/([0-9\.]+)#', $useragent, $matches) !== false)
225 {
226 if (isset($matches[1]))
227 {
228 $browser['konqueror'] = $matches[1];
229 }
230 }
231
232 // -------------------------------------------------------------------
233 // -- Mozilla
234 // -------------------------------------------------------------------
235 # Mozilla/5.001 (windows; U; NT4.0; en-us) Gecko/25250101
236 # Mozilla/5.001 (Macintosh; N; PPC; ja) Gecko/25250101 MegaCorpBrowser/1.0 (MegaCorp, Inc.)
237 if (preg_match('#gecko/([0-9]+)#', $useragent, $matches) !== false AND !isset($browser['safari']))
238 {
239 if (isset($matches[1]))
240 {
241 $browser['mozilla'] = $matches[1];
242 }
243
244 // -------------------------------------------------------------------
245 // -- Firefox
246 // -------------------------------------------------------------------
247 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7) Gecko/20040628 Firefox/0.9.1
248 # Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4a) Gecko/20030423 Firebird Browser/0.6
249 if (preg_match('#(firebird|firefox)( browser)?/([0-9\.]+)#', $useragent, $matches) !== false)
250 {
251 if (isset($matches[3]))
252 {
253 $browser['firefox'] = $matches[3];
254 }
255 }
256
257 // -------------------------------------------------------------------
258 // -- Netscape
259 // -------------------------------------------------------------------
260 # Mozilla/5.0 (Macintosh; U; PPC; en-US; rv:0.9.4.1) Gecko/20020318 Netscape6/6.2.2
261 if (preg_match('#netscape([0-9].*?)?/([0-9\.]+)#', $useragent, $matches) !== false)
262 {
263 if (isset($matches[2]))
264 {
265 $browser['netscape'] = $matches[2];
266 }
267 }
268
269 // -------------------------------------------------------------------
270 // -- Camino
271 // -------------------------------------------------------------------
272 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7) Gecko/20040623 Camino/0.8
273 if (preg_match('#camino/([0-9\.]+)#', $useragent, $matches) !== false)
274 {
275 if (isset($matches[1]))
276 {
277 $browser['camino'] = $matches[1];
278 }
279 }
280 }
281
282 if (isset($browser["$check"]))
283 {
284 if ($version)
285 {
286 if ($browser["$check"] >= $version)
287 {
288 return $browser["$check"];
289 }
290 else
291 {
292 return false;
293 }
294 }
295 else
296 {
297 return $browser["$check"];
298 }
299 }
300 else
301 {
302 return false;
303 }
304 }
305
306 /**
307 * Generates a random string of random length (unless otherwise specified)
308 *
309 * @param int Optional length
310 *
311 * @return str A random string
312 */
313 function rand($length = 0)
314 {
315 // custom high and lows
316 if (is_array($length))
317 {
318 $length = rand($length[0], $length[1]);
319 }
320 else if (!$length)
321 {
322 // Gimme a length!
323 $length = rand(20, 65);
324 }
325
326 // Number of ints in our salt
327 $intcount = rand(0, intval($length / 2));
328
329 // Number of chars
330 $charcount = $length - $intcount;
331
332 // Upper-case chars
333 $upperchars = rand(1, intval($charcount / 2));
334
335 // Lower-case chars
336 $lowerchars = $charcount - $upperchars;
337
338 // Generate ints
339 for ($i = 0; $i < $intcount; $i++)
340 {
341 $string[] = rand(0, 9);
342 }
343
344 // Generate upper chars
345 for ($i = 0; $i < $upperchars; $i++)
346 {
347 $string[] = chr(rand(65, 90));
348 }
349
350 // Generate lower chars
351 for ($i = 0; $i < $lowerchars; $i++)
352 {
353 $string[] = chr(rand(97, 122));
354 }
355
356 // Randomly key the chars
357 foreach ($string AS $char)
358 {
359 $rand = mt_rand();
360 $newstr["$rand"] = $char;
361 }
362
363 // Sort the chars by thier random assignment
364 ksort($newstr);
365
366 // Flatten the array
367 $string = '';
368 foreach ($newstr AS $char)
369 {
370 $string .= $char;
371 }
372
373 return $string;
374 }
375
376 /**
377 * Sets the current array position to be the specified key. This function should be avoided on large arrays.
378 *
379 * @param array The array whose counter is to be updated
380 * @param mixed The key of the element of the array that the counter is to be set to
381 *
382 * @return mixed Return the elelment of the array that we just put the counter to
383 */
384 function array_set_current(&$array, $key)
385 {
386 reset($array);
387 while (current($array) !== false)
388 {
389 if (key($array) == $key)
390 {
391 break;
392 }
393 next($array);
394 }
395 return current($array);
396 }
397
398 /**
399 * Calculates the microtime difference by taking a given microtime and subtracting it from the current one
400 *
401 * @param str The start microtime
402 *
403 * @return float Microtime difference
404 */
405 function fetch_microtime_diff($mtstart)
406 {
407 $mtend = microtime();
408 list ($starttime['micro'], $starttime['sec']) = explode(' ', $mtstart);
409 list ($endtime['micro'], $endtime['sec']) = explode(' ', $mtend);
410 return ($endtime['micro'] + $endtime['sec']) - ($starttime['micro'] + $starttime['sec']);
411 }
412
413 /**
414 * Fetches the extension of a file by extracting everything after the last period
415 *
416 * @param str Filename
417 *
418 * @return str The extension for the specifid file name
419 */
420 function fetch_extension($filename)
421 {
422 return strval(end(explode('.', $filename)));
423 }
424 }
425
426 /*=====================================================================*\
427 || ###################################################################
428 || # $HeadURL$
429 || # $Id$
430 || ###################################################################
431 \*=====================================================================*/
432 ?>