2 /*=====================================================================*\
3 || ###################################################################
4 || # Iris Studios Shared Object Framework [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
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.
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
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 \*=====================================================================*/
23 * Globalized Functions
29 $OBJECT = 'Core Functions';
34 * Globalized Functions
36 * This framework is a set of functions that are commonly used in most
39 * @author Iris Studios, Inc.
40 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
48 * The path that is used to set cookies
52 var $cookiepath = '/';
55 * The domain used for cookie setting
62 * The time it takes for a cookie to expire
69 * State of the current background colour during alternation
76 * Sets a cookie with a friendly interface
78 * @param string The name of the cookie
79 * @param string Value of the cookie
80 * @param bool Is the cookie permanent?
82 function cookie($name, $value = '', $sticky = true
)
87 setcookie($name, $value, time() - (2 * $this->cookieexp
), $this->cookiepath
, $this->cookiedom
);
94 $expire = time() +
60 * 60 * 24 * 365;
98 $expire = time() +
$this->cookieexp
;
101 setcookie($name, $value, $expire, $this->cookiepath
, $this->cookiedom
);
106 * Alternate between two background colours
108 * @param string First CSS class name
109 * @param string Second CSS class name
111 function exec_swap_bg($class1 = 'alt1', $class2 = 'alt2')
115 $this->bgcolour
= ($count %
2) ?
$class1 : $class2;
120 * Force-download a file by sending application/octetstream
122 * @param string The text of the file to be streamed
123 * @param string File name of the new file
124 * @param bool Whether or not to die after stringeaming the file
126 function download_file($file, $name, $exit = true
)
128 if ($this->is_browser('ie') OR $this->is_browser('opera') OR $this->is_browser('safari'))
130 $mime = 'application/octetstream';
134 $mime = 'application/octet-stream';
137 header("Content-Type: $mime");
138 header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
139 header('Content-Disposition: attachment; filename="' . $name . '"');
140 header('Content-length: ' . strlen($file));
141 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
142 header('Pragma: public');
153 * Verify that an email address is valid via regex
155 * @param string An email address
157 * @return bool Validity of the email address
159 function is_valid_email($email)
161 if (preg_match('#^[a-z0-9\.\-\+_]+?@(.*?\.)*?[a-z0-9\-_]+?\.[a-z]{2,4}$#i', $email))
172 * Check a browser's user agent against a pre-determined list
174 * @param string Browser name
175 * @param string The browser's version
177 * @param mixed False if there is no match, the version if there is
179 function is_browser($check, $version = '')
181 $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
185 // -------------------------------------------------------------------
187 // -------------------------------------------------------------------
188 # Opera/6.05 (Windows 98; U) [en]
189 # Mozilla/4.0 (compatible; MSIE 5.0; Windows 98) Opera 6.0 [en]
190 # Mozilla/5.0 (Windows 98; U) Opera 6.0 [en]
191 # Mozilla/4.0 (compatible; MSIE, 6.0; Windows 98) Opera 7.0 [en]
192 if (preg_match('#opera ([0-9\.]+)#', $useragent, $matches) !== false
)
194 if (isset($matches[1]))
196 $browser['opera'] = $matches[1];
200 // -------------------------------------------------------------------
202 // -------------------------------------------------------------------
203 if (strpos($useragent, 'mac') !== false
)
205 $browser['mac'] = true
;
208 // -------------------------------------------------------------------
209 // -- Internet explorer
210 // -------------------------------------------------------------------
211 # Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1; .NET CLR 1.0.2914)
212 # Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)
213 if (preg_match('#msie ([0-9\.]+)#', $useragent, $matches) !== false
AND !isset($browser['opera']))
215 if (isset($matches[1]))
217 $browser['ie'] = $matches[1];
221 // -------------------------------------------------------------------
223 // -------------------------------------------------------------------
224 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/125.4 (KHTML, like Gecko) Safari/125.9
225 if (preg_match('#safari/([0-9\.]+)#', $useragent, $matches) !== false
)
227 if (isset($matches[1]))
229 $browser['safari'] = $matches[1];
233 // -------------------------------------------------------------------
235 // -------------------------------------------------------------------
236 # Mozilla/5.0 (compatible; Konqueror/3)
237 # Mozilla/5.0 (compatible; Konqueror/3.1; i686 Linux; 20020628)
238 if (preg_match('#konqueror/([0-9\.]+)#', $useragent, $matches) !== false
)
240 if (isset($matches[1]))
242 $browser['konqueror'] = $matches[1];
246 // -------------------------------------------------------------------
248 // -------------------------------------------------------------------
249 # Mozilla/5.001 (windows; U; NT4.0; en-us) Gecko/25250101
250 # Mozilla/5.001 (Macintosh; N; PPC; ja) Gecko/25250101 MegaCorpBrowser/1.0 (MegaCorp, Inc.)
251 if (preg_match('#gecko/([0-9]+)#', $useragent, $matches) !== false
AND !isset($browser['safari']))
253 if (isset($matches[1]))
255 $browser['mozilla'] = $matches[1];
258 // -------------------------------------------------------------------
260 // -------------------------------------------------------------------
261 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7) Gecko/20040628 Firefox/0.9.1
262 # Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4a) Gecko/20030423 Firebird Browser/0.6
263 if (preg_match('#(firebird|firefox)( browser)?/([0-9\.]+)#', $useragent, $matches) !== false
)
265 if (isset($matches[3]))
267 $browser['firefox'] = $matches[3];
271 // -------------------------------------------------------------------
273 // -------------------------------------------------------------------
274 # Mozilla/5.0 (Macintosh; U; PPC; en-US; rv:0.9.4.1) Gecko/20020318 Netscape6/6.2.2
275 if (preg_match('#netscape([0-9].*?)?/([0-9\.]+)#', $useragent, $matches) !== false
)
277 if (isset($matches[2]))
279 $browser['netscape'] = $matches[2];
283 // -------------------------------------------------------------------
285 // -------------------------------------------------------------------
286 # Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7) Gecko/20040623 Camino/0.8
287 if (preg_match('#camino/([0-9\.]+)#', $useragent, $matches) !== false
)
289 if (isset($matches[1]))
291 $browser['camino'] = $matches[1];
296 if (isset($browser["$check"]))
300 if ($browser["$check"] >= $version)
302 return $browser["$check"];
311 return $browser["$check"];
321 * Generates a random string of random length (unless otherwise specified)
323 * @param integer Optional length
325 * @return string A random string
327 function rand($length = 0)
329 // custom high and lows
330 if (is_array($length))
332 $length = rand($length[0], $length[1]);
337 $length = rand(20, 65);
340 // Number of ints in our salt
341 $intcount = rand(0, intval($length / 2));
344 $charcount = $length - $intcount;
347 $upperchars = rand(1, intval($charcount / 2));
350 $lowerchars = $charcount - $upperchars;
353 for ($i = 0; $i < $intcount; $i++
)
355 $string[] = rand(0, 9);
358 // Generate upper chars
359 for ($i = 0; $i < $upperchars; $i++
)
361 $string[] = chr(rand(65, 90));
364 // Generate lower chars
365 for ($i = 0; $i < $lowerchars; $i++
)
367 $string[] = chr(rand(97, 122));
370 // Randomly key the chars
371 foreach ($string AS $char)
374 $newstr["$rand"] = $char;
377 // Sort the chars by thier random assignment
382 foreach ($newstr AS $char)
391 * Sets the current array position to be the specified key. This function should be avoided on large arrays.
393 * @param array The array whose counter is to be updated
394 * @param mixed The key of the element of the array that the counter is to be set to
396 * @return mixed Return the elelment of the array that we just put the counter to
398 function array_set_current(&$array, $key)
401 while (current($array) !== false
)
403 if (key($array) == $key)
409 return current($array);
413 * Calculates the microtime difference by taking a given microtime and subtracting it from the current one
415 * @param string The start microtime
417 * @return float Microtime difference
419 function fetch_microtime_diff($mtstart)
421 $mtend = microtime();
422 list ($starttime['micro'], $starttime['sec']) = explode(' ', $mtstart);
423 list ($endtime['micro'], $endtime['sec']) = explode(' ', $mtend);
424 return ($endtime['micro'] +
$endtime['sec']) - ($starttime['micro'] +
$starttime['sec']);
428 * Fetches the extension of a file by extracting everything after the last period
430 * @param string Filename
432 * @return string The extension for the specifid file name
434 function fetch_extension($filename)
436 return strval(end(explode('.', $filename)));
440 * Gets the maximum file size for attachment uploading, as specified by PHP. If no value is present, 10 MB (represented in bytes) is returned
442 * @return integer The maximum file upload size in bytes
444 function fetch_max_attachment_size()
446 if ($size = @ini_get('upload_max_filesize'))
448 if (preg_match('#[^0-9].#', $size))
454 return intval($size) * 1048576;
464 * Scans a specified directory path and returns an array of all the itmes in that directory. Directories found by this are end in a "/"
466 * @param string Path to scan
467 * @param bool Whether or not to recursively scan the directories encountered
468 * @param bool Ignore files beginning with a dot
469 * @param bool Ignore 'CVS' dirctories
471 * @return array A list of all the files in the specified path
473 function scandir($path, $recurse = true
, $ignoredot = true
, $ignorecvs = true
, $basepath = '', $unset = 1)
482 if (substr($path, (strlen($path) - 1), 1) != '/')
487 if ($handle = opendir($path))
489 while (($file = readdir($handle)) !== false
)
491 $isdot = ((substr($file, 0, 1) == '.') ? true
: false
);
492 $isdot = (($ignoredot) ?
$isdot : true
);
493 $iscvs = (($file == 'CVS') ? true
: false
);
494 $iscvs = (($ignorecvs) ?
$iscvs : true
);
495 if (!$isdot AND !$iscvs)
497 if (is_dir($path . $file))
499 $filelist["$basepath"][] = "$file/";
502 $this->scandir("$path$file", true
, $ignoredot, $ignorecvs, "$basepath$file/", 0);
507 $filelist["$basepath"][] = $file;
517 /*=====================================================================*\
518 || ###################################################################
521 || ###################################################################
522 \*=====================================================================*/