3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the Revised BSD License.
6 This program is distributed in the hope that it will be useful,
7 but WITHOUT ANY WARRANTY; without even the implied warranty of
8 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 Revised BSD License for more details.
11 Copyright 2004-2010 iDB Support - http://idb.berlios.de/
12 Copyright 2004-2010 Game Maker 2k - http://gamemaker2k.org/
14 $FileInfo: function.php - Last Update: 09/21/2010 SVN 554 - Author: cooldude2k $
16 $File3Name = basename($_SERVER['SCRIPT_NAME']);
17 if ($File3Name=="function.php"||$File3Name=="/function.php") {
20 require_once($SettDir['misc'].'functions.php');
21 /* Change Some PHP Settings Fix the & to &
22 if($Settings['use_iniset']==true&&$Settings['qstr']!="/") {
23 ini_set("arg_separator.output",htmlentities($Settings['qstr'], ENT_QUOTES, $Settings['charset']));
24 ini_set("arg_separator.input",$Settings['qstr']);
25 ini_set("arg_separator",htmlentities($Settings['qstr'], ENT_QUOTES, $Settings['charset'])); }
26 //$basepath = pathinfo($_SERVER['REQUEST_URI']);
27 if(dirname($_SERVER['REQUEST_URI'])!="."||
28 dirname($_SERVER['REQUEST_URI'])!=null) {
29 $basedir = dirname($_SERVER['REQUEST_URI'])."/"; }*/
30 // Get the base dir name
31 /*if(dirname($_SERVER['SCRIPT_NAME'])!="."||
32 dirname($_SERVER['SCRIPT_NAME'])!=null) {
33 $basedir = dirname($_SERVER['SCRIPT_NAME'])."/"; }
34 if($basedir==null||$basedir==".") {
35 if(dirname($_SERVER['SCRIPT_NAME'])=="."||
36 dirname($_SERVER['SCRIPT_NAME'])==null) {
37 $basedir = dirname($_SERVER['PHP_SELF'])."/"; } }
38 if($basedir=="\/") { $basedir="/"; }
39 $basedir = str_replace("//", "/", $basedir);*/
40 if($Settings['qstr']!="/") {
41 $iDBURLCHK = $Settings['idburl']; }
42 if($Settings['qstr']=="/") {
43 $iDBURLCHK = preg_replace("/\/$/","",$Settings['idburl']); }
44 $basecheck = parse_url($iDBURLCHK);
45 $basedir = $basecheck['path'];
48 if($Settings['fixbasedir']!=null&&$Settings['fixbasedir']!="off") {
49 $basedir = $Settings['fixbasedir']; }
50 if($Settings['fixcookiedir']!=null&&$Settings['fixcookiedir']!="") {
51 $cbasedir = $Settings['fixcookiedir']; }
52 if($Settings['fixredirectdir']!=null) {
53 $rbasedir = $Settings['fixredirectdir']; }
55 // Get our Host Name and Referer URL's Host Name
56 if(!isset($_SERVER['HTTP_REFERER'])) { $_SERVER['HTTP_REFERER'] = null; }
57 $REFERERurl = parse_url($_SERVER['HTTP_REFERER']);
58 if(!isset($REFERERurl['host'])) { $REFERERurl['host'] = null; }
59 $URL['REFERER'] = $REFERERurl['host'];
60 $URL['HOST'] = $basecheck['host'];
62 // Function made by Howard Yeend
63 // http://php.net/manual/en/function.trigger-error.php#92016
64 // http://www.puremango.co.uk/
65 function output_error($message, $level=E_USER_ERROR) {
66 $caller = next(debug_backtrace());
67 trigger_error($message.' in <strong>'.$caller['function'].'</strong> called from <strong>'.$caller['file'].'</strong> on line <strong>'.$caller['line'].'</strong>'."\n<br />error handler", $level); }
69 function untar($tarfile,$outdir) {
70 if($outdir!=""&&!file_exists($outdir)) {
72 $thandle = fopen($tarfile, "r");
73 while (!feof($thandle)) {
74 $FileName = $outdir.trim(fread($thandle,100));
75 $FileMode = trim(fread($thandle,8));
76 $OwnerID = trim(fread($thandle,8));
77 $GroupID = trim(fread($thandle,8));
78 $FileSize = octdec(trim(fread($thandle,12)));
79 $LastEdit = trim(fread($thandle,12));
80 $Checksum = trim(fread($thandle,8));
81 $FileType = trim(fread($thandle,1));
82 fseek($thandle,355,SEEK_CUR);
84 $FileContent = fread($thandle,$FileSize); }
86 $FileContent = null; }
88 $subhandle = fopen($FileName, "a+");
89 fwrite($subhandle,$FileContent,$FileSize);
93 touch($FileName,$LastEdit);
96 while ($CheckSize<$FileSize) {
97 if($CheckSize<$FileSize) {
98 $CheckSize = $CheckSize + 512; } }
99 $SeekSize = $CheckSize - $FileSize;
100 fseek($thandle,$SeekSize,SEEK_CUR); } }
103 // http://us.php.net/manual/en/function.uniqid.php#94959
107 * @author Andrew Moore
108 * @url http://us.php.net/manual/en/function.uniqid.php#94959
110 function uuid($uuidver = "v4", $rndty = "rand", $namespace = null, $name = null) {
111 if($uuidver!="v3"&&$uuidver!="v4"&&$uuidver!="v5") { $uuidver = "v4"; }
113 return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
114 $rndty(0, 0xffff), $rndty(0, 0xffff),
116 $rndty(0, 0x0fff) | 0x4000,
117 $rndty(0, 0x3fff) | 0x8000,
118 $rndty(0, 0xffff), $rndty(0, 0xffff), $rndty(0, 0xffff) ); }
119 if($uuidver=="v3"||$uuidver=="v5") {
120 if($namespace===null) {
121 $namespace = uuid("v4",$rndty); }
122 $nhex = str_replace(array('-','{','}'), '', $namespace);
124 for($i = 0; $i < strlen($nhex); $i+=2) {
125 if(isset($nhex[$i+1])) {
126 $nstr .= chr(hexdec($nhex[$i].$nhex[$i+1])); }
127 if(!isset($nhex[$i+1])) {
128 $nstr .= chr(hexdec($nhex[$i])); }
130 if($name===null) { $name = salt_hmac(); }
131 // Calculate hash value
134 if (function_exists('hash')) {
135 $hash = hash("md5", $nstr . $name); }
136 if (!function_exists('hash')) {
137 $hash = md5($nstr . $name); } }
140 if (function_exists('hash')) {
141 $hash = hash("sha1", $nstr . $name); }
142 if (!function_exists('hash')) {
143 $hash = sha1($nstr . $name); } }
144 return sprintf('%08s-%04s-%04x-%04x-%12s',
147 (hexdec(substr($hash, 12, 4)) & 0x0fff) | $uuidverid,
148 (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,
149 substr($hash, 20, 12) ); } }
150 function rand_uuid($rndty = "rand", $namespace = null, $name = null) {
151 $rand_array = array(1 => "v3", 2 => "v4", 3 => "v5");
152 if($name===null) { $name = salt_hmac(); }
153 $my_uuid = $rand_array[$rndty(1,3)];
154 if($my_uuid=="v4") { return uuid("v4",$rndty); }
155 if($my_uuid=="v3"||$my_uuid=="v5") {
156 return uuid($my_uuid,$rndty,$name); } }
157 // unserialize sessions variables
158 function unserialize_session($data) {
159 $vars=preg_split('/([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff^|]*)\|/',
160 $data,-1,PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
162 for($i=0; isset($vars[$i]); $i++) {
164 if(!isset($vars[$i])) { $vars[$i] = null; }
165 if(!isset($vars[$l])) { $vars[$l] = null; }
166 $result[$vars[$i]]=unserialize($vars[$l]);
168 if(!isset($result)) { $result = null; }
171 // Make the Query String if we are not useing &=
172 function qstring($qstr=";",$qsep="=")
173 { $_GET = array(); $_GET = null;
174 if (!isset($_SERVER['QUERY_STRING'])) {
175 $_SERVER['QUERY_STRING'] = getenv('QUERY_STRING'); }
176 ini_get("arg_separator.input", $qstr);
177 $_SERVER['QUERY_STRING'] = urldecode($_SERVER['QUERY_STRING']);
178 $preqs = explode($qstr,$_SERVER["QUERY_STRING"]);
179 $qsnum = count($preqs); $qsi = 0;
180 while ($qsi < $qsnum) {
181 $preqst = explode($qsep,$preqs[$qsi],2);
182 $fix1 = array(" ",'$'); $fix2 = array("_","_");
183 $preqst[0] = str_replace($fix1, $fix2, $preqst[0]);
184 $preqst[0] = killbadvars($preqst[0]);
185 if($preqst[0]!=null) {
186 $_GET[$preqst[0]] = $preqst[1]; }
187 ++$qsi; } return true; }
188 if($_SERVER['PATH_INFO']==null) {
189 if(getenv('PATH_INFO')!=null&&getenv('PATH_INFO')!="1") {
190 $_SERVER['PATH_INFO'] = getenv('PATH_INFO'); }
191 if(getenv('PATH_INFO')==null) {
192 $myscript = $_SERVER["SCRIPT_NAME"];
193 $myphpath = $_SERVER["PHP_SELF"];
194 $mypathinfo = str_replace($myscript, "", $myphpath);
195 @putenv("PATH_INFO=".$mypathinfo); } }
196 // Change raw post data to POST array
197 // Not sure why I made but alwell. :P
198 function parse_post_data()
199 { $_POST = array(); $_POST = null;
200 $postdata = file_get_contents("php://input");
201 if (!isset($postdata)) { $postdata = null; }
202 $postdata = urldecode($postdata);
203 $preqs = explode("&",$postdata);
204 $qsnum = count($preqs); $qsi = 0;
205 while ($qsi < $qsnum) {
206 $preqst = explode("=",$preqs[$qsi],2);
207 $fix1 = array(" ",'$'); $fix2 = array("_","_");
208 $preqst[0] = str_replace($fix1, $fix2, $preqst[0]);
209 $preqst[0] = killbadvars($preqst[0]);
210 if($preqst[0]!=null) {
211 $_POST[$preqst[0]] = $preqst[1]; }
212 ++$qsi; } return true; }
213 // Change Path info to Get Vars :
214 function mrstring() {
215 $urlvar = explode('/',$_SERVER['PATH_INFO']);
216 $num=count($urlvar); $i=1;
218 //$urlvar[$i] = urldecode($urlvar[$i]);
219 if(!isset($_GET[$urlvar[$i]])) { $_GET[$urlvar[$i]] = null; }
220 if(!isset($urlvar[$i])) { $urlvar[$i] = null; }
221 if($_GET[$urlvar[$i]]==null&&$urlvar[$i]!=null) {
222 $fix1 = array(" ",'$'); $fix2 = array("_","_");
223 $urlvar[$i] = str_replace($fix1, $fix2, $urlvar[$i]);
224 $urlvar[$i] = killbadvars($urlvar[$i]);
225 $_GET[$urlvar[$i]] = $urlvar[$i+1]; }
226 ++$i; ++$i; } return true; }
227 // Redirect to another file with ether timed or nontimed redirect
228 function redirect($type,$file,$time=0,$url=null,$dbsr=true) {
229 if($type!="location"&&$type!="refresh") { $type=="location"; }
230 if($url!=null) { $file = $url.$file; }
231 if($dbsr===true) { $file = str_replace("//", "/", $file); }
232 if($type=="refresh") { header("Refresh: ".$time."; URL=".$file); }
233 if($type=="location") { session_write_close();
234 header("Location: ".$file); } return true; }
235 function redirects($type,$url,$time=0) {
236 if($type!="location"&&$type!="refresh") { $type=="location"; }
237 if($type=="refresh") { header("Refresh: ".$time."; URL=".$url); }
238 if($type=="location") { header("Location: ".$url); } return true; }
240 function html_tag_make($name="br",$emptytag=true,$attbvar=null,$attbval=null,$extratest=null) {
241 $var_num = count($attbvar); $value_num = count($attbval);
242 if($var_num!=$value_num) {
243 output_error("Erorr Number of Var and Values dont match!",E_USER_ERROR);
244 return false; } $i = 0;
245 while ($i < $var_num) {
246 if($i==0) { $mytag = "<".$name." ".$attbvar[$i]."=\"".$attbval[$i]."\""; }
247 if($i>=1) { $mytag = $mytag." ".$attbvar[$i]."=\"".$attbval[$i]."\""; }
249 if($emptytag===false) { $mytag = $mytag.">"; }
250 if($emptytag===true) { $mytag = $mytag." />"; } } ++$i; }
251 if($attbvar==null&&$attbval==null) { $mytag = "<".$name;
252 if($emptytag===true) { $mytag = $mytag." />"; }
253 if($emptytag===false) { $mytag = $mytag.">"; } }
254 if($emptytag===false&&$extratest!=null) {
255 $mytag = $mytag.$extratest; $mytag = $mytag."</".$name.">"; }
257 // Start a xml document
258 function xml_tag_make($type,$attbs,$retval=false) {
259 $renee1 = explode("&",$attbs);
260 $reneenum=count($renee1);
261 $reneei=0; $attblist = null;
262 while ($reneei < $reneenum) {
263 $renee2 = explode("=",$renee1[$reneei]);
264 if($renee2[0]!=null||$renee2[1]!=null) {
265 $attblist = $attblist.' '.$renee2[0].'="'.$renee2[1].'"'; }
267 if($retval!==false&&$retval!==true) { $retval=false; }
268 if($retval===false) {
269 echo '<?'.$type.$attblist.'?>'."\n"; }
271 return '<?'.$type.$attblist.'?>'."\n"; } }
272 // Start a xml document (old version)
273 function xml_doc_start($ver,$encode,$retval=false) {
274 if($retval===false) {
275 echo xml_tag_make('xml','version='.$ver.'&encoding='.$encode,true); }
277 return xml_tag_make('xml','version='.$ver.'&encoding='.$encode,true); } }
278 $icharset = $Settings['charset'];
280 if(isset($_GET['debug'])) {
281 if($_GET['debug']=="true"||
282 $_GET['debug']=="on") {
283 $debug_on = true; } }
284 $BoardURL = $Settings['idburl'];
285 // Change URLs to Links
286 function pre_url2link($matches) {
287 global $BoardURL; $opennew = true;
288 $burlCHCK = parse_url($BoardURL);
289 $urlCHCK = parse_url($matches[0]);
290 if($urlCHCK['host']==$burlCHCK['host']) {
292 $outurl = $urlCHCK['scheme']."://";
293 if(isset($urlCHCK['user'])) {
294 $outurl = $outurl.$urlCHCK['user'];
295 if(isset($urlCHCK['pass'])) {
296 $outurl = $outurl.":".$urlCHCK['pass']; }
297 $outurl = $outurl."@"; }
298 $outurl = $outurl.$urlCHCK['host'];
299 if(isset($urlCHCK['path'])) {
300 $outurl = $outurl.$urlCHCK['path']; }
301 if(!isset($urlCHCK['path'])) {
302 $outurl = $outurl."/"; }
303 if(isset($urlCHCK['query'])) {
304 $urlCHCK['query'] = str_replace(" ", "+", $urlCHCK['query']);
305 $outurl = $outurl."?".$urlCHCK['query']; }
306 if(isset($urlCHCK['fragment'])) {
307 $urlCHCK['fragment'] = str_replace(" ", "+", $urlCHCK['fragment']);
308 $outurl = $outurl."#".$urlCHCK['fragment']; }
309 if($opennew===true) {
310 $outlink = "<a onclick=\"window.open(this.href); return false;\" href=\"".$outurl."\">".$outurl."</a>"; }
311 if($opennew===false) {
312 $outlink = "<a href=\"".$outurl."\">".$outurl."</a>"; }
314 function url2link($string) {
315 return preg_replace_callback("/([a-zA-Z]+)\:\/\/([a-z0-9\-\.]+)(\:[0-9]+)?\/([A-Za-z0-9\.\/%\?\-_;]+)?(\?)?([A-Za-z0-9\.\/%&=\?\-_;]+)?(\#)?([A-Za-z0-9\.\/%&=\?\-_;]+)?/is", "pre_url2link", $string); }
316 function urlcheck($string) {
318 $retnum = preg_match_all("/([a-zA-Z]+)\:\/\/([a-z0-9\-\.]+)(\:[0-9]+)?\/([A-Za-z0-9\.\/%\?\-_;]+)?(\?)?([A-Za-z0-9\.\/%&=\?\-_;]+)?(\#)?([A-Za-z0-9\.\/%&=\?\-_;]+)?/is", $string, $urlcheck);
319 if(isset($urlcheck[0][0])) { $url = $urlcheck[0][0]; }
320 if(!isset($urlcheck[0][0])) { $url = $BoardURL; }
322 //Check to make sure theme exists
323 $BoardTheme = $Settings['DefaultTheme'];
324 $ThemeDir = $SettDir['themes'];
325 function chack_themes($theme) {
326 global $BoardTheme,$ThemeDir;
327 if(!isset($theme)) { $theme = null; }
328 if(preg_match("/([a-zA-Z]+)\:/isU",$theme)) {
329 $theme = $BoardTheme; }
330 if(!preg_match("/^[a-z0-9]+$/isU",$theme)) {
331 $theme = $BoardTheme; }
332 require('settings.php');
333 $ckskindir = dirname(realpath("settings.php"))."/".$ThemeDir;
334 if ($handle = opendir($ckskindir)) { $dirnum = null;
335 while (false !== ($ckfile = readdir($handle))) {
336 if ($dirnum==null) { $dirnum = 0; }
337 if (file_exists($ckskindir.$ckfile."/info.php")) {
338 if ($ckfile != "." && $ckfile != "..") {
339 //include($ckskindir.$ckfile."/info.php");
340 $cktheme[$dirnum] = $ckfile;
342 closedir($handle); asort($cktheme); }
343 $theme=preg_replace("/(.*?)\.\/(.*?)/", $BoardTheme, $theme);
344 if(!in_array($theme,$cktheme)||strlen($theme)>26) {
345 $theme = $BoardTheme; } return $theme; }
346 // Make a url with query string
347 function url_maker($file="index",$ext=".php",$qvarstr=null,$qstr=";",$qsep="=",$prexqstr=null,$exqstr=null,$fixhtml=true) {
348 global $sidurls, $icharset, $debug_on;
349 $fileurl = null; if(!isset($ext)) { $ext = null; }
350 if($ext==null) { $ext = ".php"; }
351 if($ext=="noext"||$ext=="no ext"||$ext=="no+ext") { $ext = null; }
353 if($sidurls=="on"&&$qstr!="/") {
355 if($qvarstr==null) { $qvarstr = SID; }
356 if($qvarstr!=null) { $qvarstr = SID."&".$qvarstr; } } }
357 if($debug_on===true) {
358 if($qvarstr==null) { $qvarstr = "debug=on"; }
359 if($qvarstr!=null) { $qvarstr = $qvarstr."&debug=on"; } }
360 if($qvarstr==null) { $fileurl = $file; }
361 if($fixhtml===true) {
362 $qstr = htmlentities($qstr, ENT_QUOTES, $icharset);
363 $qsep = htmlentities($qsep, ENT_QUOTES, $icharset); }
364 if($prexqstr!=null) {
365 $rene1 = explode("&",$prexqstr);
366 $renenum=count($rene1);
368 $reneqstr = "index.php?";
369 if($qstr!="/") { $fileurl = $file."?"; }
370 if($qstr=="/") { $fileurl = $file."/"; }
371 while ($renei < $renenum) {
372 $rene2 = explode("=",$rene1[$renei]);
373 if(!isset($rene2[0])) { $rene2[0] = null; }
374 $rene2[1] = urlencode($rene2[1]);
375 if(!isset($rene2[0])) { $rene2[0] = null; }
376 $rene2[1] = urlencode($rene2[1]);
378 $fileurl = $fileurl.$rene2[0].$qsep.$rene2[1]; }
380 $fileurl = $fileurl.$rene2[0]."/".$rene2[1]."/"; }
381 $reneis = $renei + 1;
383 if($reneis < $renenum) { $fileurl = $fileurl.$qstr; } }
385 if($qvarstr!=null&&$qstr!="/") { $fileurl = $fileurl.$qstr; }
387 if($prexqstr==null) {
388 if($qstr!="/") { $fileurl = $file."?"; }
389 if($qstr=="/") { $fileurl = $file."/"; } }
390 $cind1 = explode("&",$qvarstr);
391 $cindnum=count($cind1);
393 $cindqstr = "index.php?";
394 while ($cindi < $cindnum) {
395 $cind2 = explode("=",$cind1[$cindi]);
396 if(!isset($cind2[0])) { $cind2[0] = null; }
397 $cind2[0] = urlencode($cind2[0]);
398 if(!isset($cind2[1])) { $cind2[1] = null; }
399 $cind2[1] = urlencode($cind2[1]);
401 $fileurl = $fileurl.$cind2[0].$qsep.$cind2[1]; }
403 $fileurl = $fileurl.$cind2[0]."/".$cind2[1]."/"; }
404 $cindis = $cindi + 1;
406 if($cindis < $cindnum) { $fileurl = $fileurl.$qstr; } }
408 if($exqstr!=null&&$qstr!="/") { $fileurl = $fileurl.$qstr; }
410 if($qvarstr==null&&$prexqstr==null) {
411 if($qstr!="/") { $fileurl = $file."?"; }
412 if($qstr=="/") { $fileurl = $file."/"; } }
413 $sand1 = explode("&",$exqstr);
414 $sanum=count($sand1);
416 $sandqstr = "index.php?";
417 while ($sandi < $sanum) {
418 $sand2 = explode("=",$sand1[$sandi]);
419 if(!isset($sand2[0])) { $sand2[0] = null; }
420 $sand2[0] = urlencode($sand2[0]);
421 if(!isset($sand2[1])) { $sand2[1] = null; }
422 $sand2[1] = urlencode($sand2[1]);
424 $fileurl = $fileurl.$sand2[0].$qsep.$sand2[1]; }
426 $fileurl = $fileurl.$sand2[0]."/".$sand2[1]."/"; }
427 $sandis = $sandi + 1;
429 if($sandis < $sanum) { $fileurl = $fileurl.$qstr; } }
432 $thisdir = dirname(realpath("Preindex.php"))."/";
433 // Get the Query String
434 function GetQueryStr($qstr=";",$qsep="=",$fixhtml=true)
435 { $pregqstr = preg_quote($qstr,"/");
436 $pregqsep = preg_quote($qsep,"/");
437 $oqstr = $qstr; $oqsep = $qsep;
438 if($fixhtml===true||$fixhtml==null) {
439 $qstr = htmlentities($qstr, ENT_QUOTES, $icharset);
440 $qsep = htmlentities($qsep, ENT_QUOTES, $icharset); }
441 $OldBoardQuery = preg_replace("/".$pregqstr."/isxS", $qstr, $_SERVER['QUERY_STRING']);
442 $BoardQuery = "?".$OldBoardQuery;
443 return $BoardQuery; }