OSDN Git Service

1dbedadd404187673ea6177b9726a383c50eaf26
[idb/iDB.git.git] / inc / function.php
1 <?php
2 /*
3     This program is free software; you can redistribute it and/or modify
4     it under the terms of the Revised BSD License.
5
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.
10
11     Copyright 2004-2017 iDB Support - http://idb.berlios.de/
12     Copyright 2004-2017 Game Maker 2k - http://gamemaker2k.org/
13
14     $FileInfo: function.php - Last Update: 01/26/2017 SVN 810 - Author: cooldude2k $
15 */
16 $File3Name = basename($_SERVER['SCRIPT_NAME']);
17 if ($File3Name=="function.php"||$File3Name=="/function.php") {
18         require('index.php');
19         exit(); }
20 require_once($SettDir['misc'].'functions.php');
21 require_once($SettDir['misc'].'ibbcode.php');
22 require_once($SettDir['misc'].'iuntar.php');
23 /* In php 6 and up the function get_magic_quotes_gpc dose not exist. 
24    here we make a fake version that always sends false out. :P */
25 if(!function_exists('get_magic_quotes_gpc')) {
26 function get_magic_quotes_gpc() { return false; } }
27 /**
28  * Undo the damage of magic_quotes_gpc if in effect
29  * @return bool
30  * @link http://www.charles-reace.com/blog/2010/07/13/undoing-magic-quotes/
31  */
32 function fix_magic_quotes()
33 {
34    if (get_magic_quotes_gpc()) {
35       $func = create_function(
36          '&$val, $key',
37          'if(!is_numeric($val)) {$val = stripslashes($val);}'
38       );
39       array_walk_recursive($_GET, $func);
40       array_walk_recursive($_POST, $func);
41       array_walk_recursive($_COOKIE, $func);
42    }
43    return true;
44 }
45 fix_magic_quotes();
46 /* Change Some PHP Settings Fix the & to &amp;
47 if($Settings['use_iniset']==true&&$Settings['qstr']!="/") {
48 ini_set("arg_separator.output",htmlentities($Settings['qstr'], ENT_QUOTES, $Settings['charset']));
49 ini_set("arg_separator.input",$Settings['qstr']);
50 ini_set("arg_separator",htmlentities($Settings['qstr'], ENT_QUOTES, $Settings['charset'])); }
51 //$basepath = pathinfo($_SERVER['REQUEST_URI']);
52 if(dirname($_SERVER['REQUEST_URI'])!="."||
53         dirname($_SERVER['REQUEST_URI'])!=null) {
54 $basedir = dirname($_SERVER['REQUEST_URI'])."/"; }*/
55 // Get the base dir name
56 /*if(dirname($_SERVER['SCRIPT_NAME'])!="."||
57         dirname($_SERVER['SCRIPT_NAME'])!=null) {
58 $basedir = dirname($_SERVER['SCRIPT_NAME'])."/"; }
59 if($basedir==null||$basedir==".") {
60 if(dirname($_SERVER['SCRIPT_NAME'])=="."||
61         dirname($_SERVER['SCRIPT_NAME'])==null) {
62 $basedir = dirname($_SERVER['PHP_SELF'])."/"; } }
63 if($basedir=="\/") { $basedir="/"; }
64 $basedir = str_replace("//", "/", $basedir);*/
65 if($Settings['qstr']!="/") {
66 $iDBURLCHK = $Settings['idburl']; }
67 if($Settings['qstr']=="/") {
68 $iDBURLCHK = preg_replace("/\/$/","",$Settings['idburl']); }
69 $basecheck = parse_url($iDBURLCHK);
70 $basedir = $basecheck['path'];
71 $cbasedir = $basedir;
72 $rbasedir = $basedir;
73 if($Settings['fixbasedir']!=null&&$Settings['fixbasedir']!="off") {
74                 $basedir = $Settings['fixbasedir']; }
75 if($Settings['fixcookiedir']!=null&&$Settings['fixcookiedir']!="") {
76                 $cbasedir = $Settings['fixcookiedir']; }
77 if($Settings['fixredirectdir']!=null) {
78                 $rbasedir = $Settings['fixredirectdir']; }
79 $BaseURL = $basedir;
80 // Get our Host Name and Referer URL's Host Name
81 if(!isset($_SERVER['HTTP_REFERER'])) { $_SERVER['HTTP_REFERER'] = null; }
82 $REFERERurl = parse_url($_SERVER['HTTP_REFERER']);
83 if(!isset($REFERERurl['host'])) { $REFERERurl['host'] = null; }
84 $URL['REFERER'] = $REFERERurl['host'];
85 $URL['HOST'] = $basecheck['host'];
86 $REFERERurl = null;
87 // Function made by Howard Yeend
88 // http://php.net/manual/en/function.trigger-error.php#92016
89 // http://www.puremango.co.uk/
90 function output_error($message, $level=E_USER_ERROR) {
91     $caller = next(debug_backtrace());
92     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); }
93         $Names['D'] = "Dagmara";
94 define("_dagmara_", $Names['D']);
95 // http://us.php.net/manual/en/function.uniqid.php#94959
96 /**
97   * Generates an UUID
98   * 
99   * @author     Andrew Moore
100   * @url        http://us.php.net/manual/en/function.uniqid.php#94959
101   */
102 function uuid($uuidver = "v4", $rndty = "rand", $namespace = null, $name = null) {
103 if($uuidver!="v3"&&$uuidver!="v4"&&$uuidver!="v5") { $uuidver = "v4"; }
104 if($uuidver=="v4") {
105     return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
106       $rndty(0, 0xffff), $rndty(0, 0xffff),
107       $rndty(0, 0xffff),
108       $rndty(0, 0x0fff) | 0x4000,
109       $rndty(0, 0x3fff) | 0x8000,
110       $rndty(0, 0xffff), $rndty(0, 0xffff), $rndty(0, 0xffff) ); }
111 if($uuidver=="v3"||$uuidver=="v5") {
112         if($namespace===null) {
113         $namespace = uuid("v4",$rndty); }
114     $nhex = str_replace(array('-','{','}'), '', $namespace);
115     $nstr = '';
116     for($i = 0; $i < strlen($nhex); $i+=2) {
117       if(isset($nhex[$i+1])) {
118           $nstr .= chr(hexdec($nhex[$i].$nhex[$i+1])); }
119       if(!isset($nhex[$i+1])) {
120           $nstr .= chr(hexdec($nhex[$i])); }
121     }
122         if($name===null) { $name = salt_hmac(); }
123     // Calculate hash value
124         if($uuidver=="v3") {
125         $uuidverid = 0x3000;
126         if (function_exists('hash')) {
127         $hash = hash("md5", $nstr . $name); }
128         if (!function_exists('hash')) {
129         $hash = md5($nstr . $name); } }
130         if($uuidver=="v5") {
131         $uuidverid = 0x5000;
132         if (function_exists('hash')) {
133         $hash = hash("sha1", $nstr . $name); }
134         if (!function_exists('hash')) {
135         $hash = sha1($nstr . $name); } }
136     return sprintf('%08s-%04s-%04x-%04x-%12s',
137       substr($hash, 0, 8),
138       substr($hash, 8, 4),
139       (hexdec(substr($hash, 12, 4)) & 0x0fff) | $uuidverid,
140       (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,
141       substr($hash, 20, 12) ); } }
142 function rand_uuid($rndty = "rand", $namespace = null, $name = null) {
143 $rand_array = array(1 => "v3", 2 => "v4", 3 => "v5");
144 if($name===null) { $name = salt_hmac(); }
145 $my_uuid = $rand_array[$rndty(1,3)];
146 if($my_uuid=="v4") { return uuid("v4",$rndty); }
147 if($my_uuid=="v3"||$my_uuid=="v5") {
148 return uuid($my_uuid,$rndty,$name); } }
149 // unserialize sessions variables
150 // By: jason@joeymail.net
151 // URL: http://us2.php.net/manual/en/function.session-decode.php#101687
152 function unserialize_session($data)
153 {
154     if(  strlen( $data) == 0)
155     {
156         return array();
157     }
158     // match all the session keys and offsets
159     preg_match_all('/(^|;|\})([a-zA-Z0-9_]+)\|/i', $data, $matchesarray, PREG_OFFSET_CAPTURE);
160     $returnArray = array();
161     $lastOffset = null;
162     $currentKey = '';
163     foreach ( $matchesarray[2] as $value )
164     {
165         $offset = $value[1];
166         if(!is_null( $lastOffset))
167         {
168             $valueText = substr($data, $lastOffset, $offset - $lastOffset );
169             $returnArray[$currentKey] = unserialize($valueText);
170         }
171         $currentKey = $value[0];
172         $lastOffset = $offset + strlen( $currentKey )+1;
173     }
174     $valueText = substr($data, $lastOffset );
175     $returnArray[$currentKey] = unserialize($valueText);
176     return $returnArray;
177 }
178 // Make the Query String if we are not useing &=
179 function qstring($qstr=";",$qsep="=")
180 { $_GET = array(); $_GET = null;
181 if (!isset($_SERVER['QUERY_STRING'])) {
182 $_SERVER['QUERY_STRING'] = getenv('QUERY_STRING'); }
183 ini_set("arg_separator.input", $qstr);
184 $_SERVER['QUERY_STRING'] = urldecode($_SERVER['QUERY_STRING']);
185 $preqs = explode($qstr,$_SERVER["QUERY_STRING"]);
186 $qsnum = count($preqs); $qsi = 0;
187 while ($qsi < $qsnum) {
188 $preqst = explode($qsep,$preqs[$qsi],2);
189 $fix1 = array(" ",'$'); $fix2  = array("_","_");
190 $preqst[0] = str_replace($fix1, $fix2, $preqst[0]);
191 $preqst[0] = killbadvars($preqst[0]);
192 if($preqst[0]!=null) {
193 $_GET[$preqst[0]] = $preqst[1]; }
194 ++$qsi; } return true; }
195 if($Settings['qstr']!="&"&&
196         $Settings['qstr']!="/") {
197 qstring($Settings['qstr'],$Settings['qsep']); 
198 if(!isset($_GET['page'])) { $_GET['page'] = null; }
199 if(!isset($_GET['act'])) { $_GET['act'] = null; }
200 if(!isset($_POST['act'])) { $_POST['act'] = null; }
201 if(!isset($_GET['id'])) { $_GET['id'] = null; } 
202 if(!isset($_GET['debug'])) { $_GET['debug'] = "false"; }
203 if(!isset($_GET['post'])) { $_GET['post'] = null; }
204 if(!isset($_POST['License'])) { $_POST['License'] = null; } }
205 if($_SERVER['PATH_INFO']==null) {
206         if(getenv('PATH_INFO')!=null&&getenv('PATH_INFO')!="1") {
207 $_SERVER['PATH_INFO'] = getenv('PATH_INFO'); }
208 if(getenv('PATH_INFO')==null) {
209 $myscript = $_SERVER["SCRIPT_NAME"];
210 $myphpath = $_SERVER["PHP_SELF"];
211 $mypathinfo = str_replace($myscript, "", $myphpath);
212 @putenv("PATH_INFO=".$mypathinfo); } }
213 // Change raw post data to POST array
214 // Not sure why I made but alwell. :P 
215 function parse_post_data()
216 { $_POST = array(); $_POST = null;
217 $postdata = file_get_contents("php://input");
218 if (!isset($postdata)) { $postdata = null; }
219 $postdata = urldecode($postdata);
220 $preqs = explode("&",$postdata);
221 $qsnum = count($preqs); $qsi = 0;
222 while ($qsi < $qsnum) {
223 $preqst = explode("=",$preqs[$qsi],2);
224 $fix1 = array(" ",'$'); $fix2  = array("_","_");
225 $preqst[0] = str_replace($fix1, $fix2, $preqst[0]);
226 $preqst[0] = killbadvars($preqst[0]);
227 if($preqst[0]!=null) {
228 $_POST[$preqst[0]] = $preqst[1]; }
229 ++$qsi; } return true; }
230 // Change Path info to Get Vars :
231 function mrstring() {
232 $urlvar = explode('/',$_SERVER['PATH_INFO']);
233 $num=count($urlvar); $i=1;
234 while ($i < $num) {
235 //$urlvar[$i] = urldecode($urlvar[$i]);
236 if(!isset($_GET[$urlvar[$i]])) { $_GET[$urlvar[$i]] = null; }
237 if(!isset($urlvar[$i])) { $urlvar[$i] = null; }
238 if($_GET[$urlvar[$i]]==null&&$urlvar[$i]!=null) {
239 $fix1 = array(" ",'$'); $fix2  = array("_","_");
240 $urlvar[$i] = str_replace($fix1, $fix2, $urlvar[$i]);
241 $urlvar[$i] = killbadvars($urlvar[$i]);
242         $_GET[$urlvar[$i]] = $urlvar[$i+1]; }
243 ++$i; ++$i; } return true; }
244 // Redirect to another file with ether timed or nontimed redirect
245 function redirect($type,$file,$time=0,$url=null,$dbsr=true) {
246 if($type!="location"&&$type!="refresh") { $type=="location"; }
247 if($url!=null) { $file = $url.$file; }
248 if($dbsr===true) { $file = str_replace("//", "/", $file); }
249 if($type=="refresh") { header("Refresh: ".$time."; URL=".$file); }
250 if($type=="location") { session_write_close(); 
251 header("Location: ".$file); } return true; }
252 function redirects($type,$url,$time=0) {
253 if($type!="location"&&$type!="refresh") { $type=="location"; }
254 if($type=="refresh") { header("Refresh: ".$time."; URL=".$url); }
255 if($type=="location") { idb_log_maker(302,"-"); }
256 if($type=="location") { header("Location: ".$url); } return true; }
257 // Make xhtml tags
258 function html_tag_make($name="br",$emptytag=true,$attbvar=null,$attbval=null,$extratest=null) {
259         $var_num = count($attbvar); $value_num = count($attbval);
260         if($var_num!=$value_num) { 
261                 output_error("Erorr Number of Var and Values dont match!",E_USER_ERROR);
262         return false; } $i = 0;
263         while ($i < $var_num) {
264         if($i==0) { $mytag = "<".$name." ".$attbvar[$i]."=\"".$attbval[$i]."\""; }
265         if($i>=1) { $mytag = $mytag." ".$attbvar[$i]."=\"".$attbval[$i]."\""; }
266         if($i==$var_num-1) { 
267         if($emptytag===false) { $mytag = $mytag.">"; }
268         if($emptytag===true) { $mytag = $mytag." />"; } }       ++$i; }
269         if($attbvar==null&&$attbval==null) { $mytag = "<".$name;
270         if($emptytag===true) { $mytag = $mytag." />"; }
271         if($emptytag===false) { $mytag = $mytag.">"; } }
272         if($emptytag===false&&$extratest!=null) { 
273         $mytag = $mytag.$extratest; $mytag = $mytag."</".$name.">"; } 
274         return $mytag; }
275 // Start a xml document
276 function xml_tag_make($type,$attbs,$retval=false) {
277         $melanie1 = explode("&",$attbs);
278         $melanienum=count($melanie1);
279         $melaniei=0; $attblist = null;
280         while ($melaniei < $melanienum) {
281         $melanie2 = explode("=",$melanie1[$melaniei]);
282         if($melanie2[0]!=null||$melanie2[1]!=null) {
283         $attblist = $attblist.' '.$melanie2[0].'="'.$melanie2[1].'"'; }
284         ++$melaniei; }
285         if($retval!==false&&$retval!==true) { $retval=false; }
286         if($retval===false) {
287         echo '<?'.$type.$attblist.'?>'."\n"; }
288         if($retval===true) {
289         return '<?'.$type.$attblist.'?>'."\n"; } }
290 // Start a xml document (old version)
291 function xml_doc_start($ver,$encode,$retval=false) {
292         if($retval===false) {
293         echo xml_tag_make('xml','version='.$ver.'&encoding='.$encode,true); }
294         if($retval===true) {
295         return xml_tag_make('xml','version='.$ver.'&encoding='.$encode,true); } }
296 $icharset = $Settings['charset'];
297 $debug_on = false;
298 if(isset($_GET['debug'])) {
299 if($_GET['debug']=="true"||
300         $_GET['debug']=="on") {
301 $debug_on = true; } }
302 $BoardURL = $Settings['idburl'];
303 // Change URLs to Links
304 function pre_url2link($matches) {
305 global $BoardURL; $opennew = true;
306 $burlCHCK = parse_url($BoardURL);
307 $urlCHCK = parse_url($matches[0]);
308 if($urlCHCK['host']==$burlCHCK['host']) {
309         $opennew = false; }
310 $outurl = $urlCHCK['scheme']."://";
311 if(isset($urlCHCK['user'])) {
312 $outurl = $outurl.$urlCHCK['user'];
313 if(isset($urlCHCK['pass'])) {
314 $outurl = $outurl.":".$urlCHCK['pass']; }
315 $outurl = $outurl."@"; }
316 $outurl = $outurl.$urlCHCK['host'];
317 if(isset($urlCHCK['path'])) {
318 $outurl = $outurl.$urlCHCK['path']; }
319 if(!isset($urlCHCK['path'])) {
320 $outurl = $outurl."/"; }
321 if(isset($urlCHCK['query'])) {
322 $urlCHCK['query'] = str_replace(" ", "+", $urlCHCK['query']);
323 $outurl = $outurl."?".$urlCHCK['query']; }
324 if(isset($urlCHCK['fragment'])) {
325 $urlCHCK['fragment'] = str_replace(" ", "+", $urlCHCK['fragment']);
326 $outurl = $outurl."#".$urlCHCK['fragment']; }
327 if($opennew===true) {
328 $outlink = "<a onclick=\"window.open(this.href); return false;\" href=\"".$outurl."\">".$outurl."</a>"; }
329 if($opennew===false) {
330 $outlink = "<a href=\"".$outurl."\">".$outurl."</a>"; }
331 return $outlink; }
332 function url2link($string) {
333 return preg_replace_callback("/(?<![\">])\b([a-zA-Z]+)\:\/\/([a-z0-9\-\.@\:]+)(\:[0-9]+)?\/([A-Za-z0-9\.\/%\?\!\$\(\)\*\-_\:;,\+\@~]+)?(\?)?([A-Za-z0-9\.\/%&\=\?\!\$\(\)\*\-_\:;,\+\@~]+)?(\#)?([A-Za-z0-9\.\/%&\=\?\!\$\(\)\*\-_\:;,\+\@~]+)?/is", "pre_url2link", $string); }
334 function urlcheck($string) {
335 global $BoardURL;
336 $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); 
337 if(isset($urlcheck[0][0])) { $url = $urlcheck[0][0]; }
338 if(!isset($urlcheck[0][0])) { $url = $BoardURL; }
339 return $url; }
340 //Check to make sure theme exists
341 $BoardTheme = $Settings['DefaultTheme'];
342 $ThemeDir = $SettDir['themes'];
343 function chack_themes($theme) {
344 global $BoardTheme,$ThemeDir;
345 if(!isset($theme)) { $theme = null; }
346 if(preg_match("/([a-zA-Z]+)\:/isU",$theme)) {
347         $theme = $BoardTheme; }
348 if(!preg_match("/^[a-z0-9]+$/isU",$theme)) {
349         $theme = $BoardTheme; }
350 require('settings.php');
351 $ckskindir = dirname(realpath("settings.php"))."/".$ThemeDir;
352 if ($handle = opendir($ckskindir)) { $dirnum = null;
353    while (false !== ($ckfile = readdir($handle))) {
354            if ($dirnum==null) { $dirnum = 0; }
355            if (is_dir($ckskindir.$ckfile)&&file_exists($ckskindir.$ckfile."/info.php")) {
356                    if ($ckfile != "." && $ckfile != "..") {
357            //require($ckskindir.$ckfile."/info.php");
358        $cktheme[$dirnum] =  $ckfile;
359            ++$dirnum; } } }
360    closedir($handle); asort($cktheme); }
361 $theme=preg_replace("/(.*?)\.\/(.*?)/", $BoardTheme, $theme);
362 if(!in_array($theme,$cktheme)||strlen($theme)>26) {
363         $theme = $BoardTheme; } return $theme; }
364 // Make a url with query string
365 function url_maker($file="index",$ext=".php",$qvarstr=null,$qstr=";",$qsep="=",$prexqstr=null,$exqstr=null,$fixhtml=true) {
366 global $sidurls, $icharset, $debug_on;
367 $fileurl = null; if(!isset($ext)) { $ext = null; }
368 if($ext==null) { $ext = ".php"; } 
369 if($ext=="noext"||$ext=="no ext"||$ext=="no+ext") { $ext = null; }
370 $file = $file.$ext;
371 if($sidurls=="on"&&$qstr!="/") { 
372         if(defined('SID')) {
373 if($qvarstr==null) { $qvarstr = SID; }
374 if($qvarstr!=null) { $qvarstr = SID."&".$qvarstr; } } }
375 if($debug_on===true) {
376 if($qvarstr==null) { $qvarstr = "debug=on"; }
377 if($qvarstr!=null) { $qvarstr = $qvarstr."&debug=on"; } }
378 if($qvarstr==null) { $fileurl = $file; }
379 if($fixhtml===true) {
380 $qstr = htmlentities($qstr, ENT_QUOTES, $icharset);
381 $qsep = htmlentities($qsep, ENT_QUOTES, $icharset); }
382 if($prexqstr!=null) { 
383 $rene1 = explode("&",$prexqstr);
384 $renenum=count($rene1);
385 $renei=0;
386 $reneqstr = "index.php?";
387 if($qstr!="/") { $fileurl = $file."?"; }
388 if($qstr=="/") { $fileurl = $file."/"; }
389 while ($renei < $renenum) {
390         $rene2 = explode("=",$rene1[$renei]);
391         if(!isset($rene2[0])) { $rene2[0] = null; }
392         $rene2[1] = urlencode($rene2[1]);
393         if(!isset($rene2[0])) { $rene2[0] = null; }
394         $rene2[1] = urlencode($rene2[1]);
395         if($qstr!="/") {
396         $fileurl = $fileurl.$rene2[0].$qsep.$rene2[1]; }
397         if($qstr=="/") {
398         $fileurl = $fileurl.$rene2[0]."/".$rene2[1]."/"; }
399         $reneis = $renei + 1;
400         if($qstr!="/") {
401         if($reneis < $renenum) { $fileurl = $fileurl.$qstr; } }
402         ++$renei; } }
403 if($qvarstr!=null&&$qstr!="/") { $fileurl = $fileurl.$qstr; }
404 if($qvarstr!=null) { 
405 if($prexqstr==null) {
406 if($qstr!="/") { $fileurl = $file."?"; }
407 if($qstr=="/") { $fileurl = $file."/"; } }
408 $cind1 = explode("&",$qvarstr);
409 $cindnum=count($cind1);
410 $cindi=0;
411 $cindqstr = "index.php?";
412 while ($cindi < $cindnum) {
413         $cind2 = explode("=",$cind1[$cindi]);
414         if(!isset($cind2[0])) { $cind2[0] = null; }
415         $cind2[0] = urlencode($cind2[0]);
416         if(!isset($cind2[1])) { $cind2[1] = null; }
417         $cind2[1] = urlencode($cind2[1]);
418         if($qstr!="/") {
419         $fileurl = $fileurl.$cind2[0].$qsep.$cind2[1]; }
420         if($qstr=="/") {
421         $fileurl = $fileurl.$cind2[0]."/".$cind2[1]."/"; }
422         $cindis = $cindi + 1;
423         if($qstr!="/") {
424         if($cindis < $cindnum) { $fileurl = $fileurl.$qstr; } }
425         ++$cindi; } }
426 if($exqstr!=null&&$qstr!="/") { $fileurl = $fileurl.$qstr; }
427 if($exqstr!=null) { 
428 if($qvarstr==null&&$prexqstr==null) {
429 if($qstr!="/") { $fileurl = $file."?"; }
430 if($qstr=="/") { $fileurl = $file."/"; } }
431 $sand1 = explode("&",$exqstr);
432 $sanum=count($sand1);
433 $sandi=0;
434 $sandqstr = "index.php?";
435 while ($sandi < $sanum) {
436         $sand2 = explode("=",$sand1[$sandi]);
437         if(!isset($sand2[0])) { $sand2[0] = null; }
438         $sand2[0] = urlencode($sand2[0]);
439         if(!isset($sand2[1])) { $sand2[1] = null; }
440         $sand2[1] = urlencode($sand2[1]);
441         if($qstr!="/") {
442         $fileurl = $fileurl.$sand2[0].$qsep.$sand2[1]; }
443         if($qstr=="/") {
444         $fileurl = $fileurl.$sand2[0]."/".$sand2[1]."/"; }
445         $sandis = $sandi + 1;
446         if($qstr!="/") {
447         if($sandis < $sanum) { $fileurl = $fileurl.$qstr; } }
448         ++$sandi; } }
449 return $fileurl; }
450 $thisdir = dirname(realpath("Preindex.php"))."/";
451 // Get the Query String
452 function GetQueryStr($qstr=";",$qsep="=",$fixhtml=true)
453 { $pregqstr = preg_quote($qstr,"/");
454 $pregqsep = preg_quote($qsep,"/");
455 $oqstr = $qstr; $oqsep = $qsep;
456 if($fixhtml===true||$fixhtml==null) {
457 $qstr = htmlentities($qstr, ENT_QUOTES, $icharset);
458 $qsep = htmlentities($qsep, ENT_QUOTES, $icharset); }
459 $OldBoardQuery = preg_replace("/".$pregqstr."/isxS", $qstr, $_SERVER['QUERY_STRING']);
460 $BoardQuery = "?".$OldBoardQuery;
461 return $BoardQuery; }
462 function log_fix_quotes($logtxt) {
463         $logtxt = str_replace("\"", "\\\"", $logtxt);
464         $logtxt = str_replace("'", "", $logtxt);
465         return $logtxt; }
466 function get_server_values($matches) {
467         $return_text = "-";
468         if(isset($_SERVER[$matches[1]])) { $return_text = $_SERVER[$matches[1]]; }
469         if(!isset($_SERVER[$matches[1]])) { $return_text = "-"; }
470         return $return_text; }
471 function get_cookie_values($matches) {
472         $return_text = null;
473         if(isset($_COOKIE[$matches[1]])) { $return_text = $_COOKIE[$matches[1]]; }
474         if(!isset($_COOKIE[$matches[1]])) { $return_text = null; }
475         return $return_text; }
476 function get_env_values($matches) {
477         $return_text = getenv($matches[1]);
478         if(!isset($return_text)) { $return_text = "-"; }
479         return $return_text; }
480 function get_setting_values($matches) {
481         global $Settings;
482         $return_text = null;
483         $matches[1] = str_replace("sqlpass", "sqluser", $matches[1]);
484         if(isset($Settings[$matches[1]])) { $return_text = $Settings[$matches[1]]; }
485         if(!isset($Settings[$matches[1]])) { $return_text = null; }
486         return $return_text; }
487 function log_fix_get_server_values($matches) {
488         return log_fix_quotes(get_server_values($matches)); }
489 function log_fix_get_cookie_values($matches) {
490         return log_fix_quotes(get_cookie_values($matches)); }
491 function log_fix_get_env_values($matches) {
492         return log_fix_quotes(get_env_values($matches)); }
493 function log_fix_get_setting_values($matches) {
494         return log_fix_quotes(get_setting_values($matches)); }
495 function get_time($matches) {
496         return date(convert_strftime($matches[1])); }
497 function convert_strftime($strftime) {
498 $strftime = str_replace("%%", "{percent\}p", $strftime);
499 $strftime = str_replace("%a", "D", $strftime);
500 $strftime = str_replace("%A", "l", $strftime);
501 $strftime = str_replace("%d", "d", $strftime);
502 $strftime = str_replace("%e", "j", $strftime);
503 $strftime = str_replace("%j", "z", $strftime);
504 $strftime = str_replace("%u", "w", $strftime);
505 $strftime = str_replace("%w", "w", $strftime);
506 $strftime = str_replace("%U", "W", $strftime);
507 $strftime = str_replace("%V", "W", $strftime);
508 $strftime = str_replace("%W", "W", $strftime);
509 $strftime = str_replace("%b", "M", $strftime);
510 $strftime = str_replace("%B", "F", $strftime);
511 $strftime = str_replace("%h", "M", $strftime);
512 $strftime = str_replace("%m", "m", $strftime);
513 $strftime = str_replace("%g", "y", $strftime);
514 $strftime = str_replace("%G", "Y", $strftime);
515 $strftime = str_replace("%y", "y", $strftime);
516 $strftime = str_replace("%Y", "Y", $strftime);
517 $strftime = str_replace("%H", "H", $strftime);
518 $strftime = str_replace("%I", "h", $strftime);
519 $strftime = str_replace("%l", "g", $strftime);
520 $strftime = str_replace("%M", "i", $strftime);
521 $strftime = str_replace("%p", "A", $strftime);
522 $strftime = str_replace("%P", "a", $strftime);
523 $strftime = str_replace("%r", "h:i:s A", $strftime);
524 $strftime = str_replace("%R", "H:i", $strftime);
525 $strftime = str_replace("%S", "s", $strftime);
526 $strftime = str_replace("%T", "H:i:s", $strftime);
527 $strftime = str_replace("%X", "H:i:s", $strftime);
528 $strftime = str_replace("%z", "O", $strftime);
529 $strftime = str_replace("%Z", "O", $strftime);
530 $strftime = str_replace("%c", "D M j H:i:s Y", $strftime);
531 $strftime = str_replace("%D", "m/d/y", $strftime);
532 $strftime = str_replace("%F", "Y-m-d", $strftime);
533 $strftime = str_replace("%x", "m/d/y", $strftime);
534 $strftime = str_replace("%n", "\n", $strftime);
535 $strftime = str_replace("%t", "\t", $strftime);
536 $strftime = preg_replace("/\{percent\}p/s", "%", $strftime);
537 return $strftime; }
538 function apache_log_maker($logtxt,$logfile=null,$status=200,$contentsize="-",$headersize=0) {
539 global $Settings;
540 if(isset($Settings['DefaultTimeZone'])) {
541 $servtz = new DateTimeZone($Settings['DefaultTimeZone']); }
542 if(!isset($Settings['DefaultTimeZone'])) {
543 $servtz = new DateTimeZone(date_default_timezone_get()); }
544 $servcurtime = new DateTime();
545 $servcurtime->setTimezone($servtz);
546 if(!isset($_SERVER['HTTP_REFERER'])) { $LOG_URL_REFERER = "-"; }
547 if(isset($_SERVER['HTTP_REFERER'])) { $LOG_URL_REFERER = $_SERVER['HTTP_REFERER']; }
548 if($LOG_URL_REFERER==""||$LOG_URL_REFERER==null) { $LOG_URL_REFERER = "-"; }
549 if(trim($LOG_URL_REFERER, "\x00..\x1F") == "") { $LOG_URL_REFERER = "-"; }
550 $LOG_URL_REFERER = log_fix_quotes($LOG_URL_REFERER);
551 if(!isset($_SERVER['PHP_AUTH_USER'])) { $LOG_AUTH_USER = "-"; }
552 if(isset($_SERVER['PHP_AUTH_USER'])) { $LOG_AUTH_USER = $_SERVER['PHP_AUTH_USER']; }
553 if($LOG_AUTH_USER==""||$LOG_AUTH_USER==null) { $LOG_AUTH_USER = "-"; }
554 if(trim($LOG_AUTH_USER, "\x00..\x1F") == "") { $LOG_AUTH_USER = "-"; }
555 $LOG_AUTH_USER = log_fix_quotes($LOG_AUTH_USER);
556 if(!isset($_SERVER["HTTP_USER_AGENT"])) { $LOG_USER_AGENT = "-"; }
557 if(isset($_SERVER["HTTP_USER_AGENT"])) { $LOG_USER_AGENT = $_SERVER["HTTP_USER_AGENT"]; }
558 if($LOG_USER_AGENT==""||$LOG_USER_AGENT==null) { $LOG_USER_AGENT = "-"; }
559 if(trim($LOG_USER_AGENT, "\x00..\x1F") == "") { $LOG_USER_AGENT = "-"; }
560 $LOG_USER_AGENT = log_fix_quotes($LOG_USER_AGENT);
561 $LogMemName = "-";
562 if(!isset($_SESSION['MemberName'])) {
563         $_SESSION['MemberName'] = null; }
564 if($_SESSION['MemberName']===null) {
565         $LogMemName = "-"; }
566 if(isset($_SESSION['MemberName'])&&$_SESSION['MemberName']!==null) {
567         $LogMemName = $_SESSION['MemberName']; }
568 if(trim($LogMemName, "\x00..\x1F") == "") { $LogMemName = "-"; }
569 $LogMemName = log_fix_quotes($LogMemName);
570 $LogMemID = "-";
571 if(!isset($_SESSION['UserID'])) {
572         $_SESSION['UserID'] = 0; }
573 if($_SESSION['UserID']===null||$_SESSION['UserID']===0) {
574         $LogMemID = "-"; }
575 if(isset($_SESSION['UserID'])&&$_SESSION['UserID']!==null&&$_SESSION['UserID']!==0) {
576         $LogMemID = $_SESSION['UserID']; }
577 if(trim($LogMemID, "\x00..\x1F") == "") { $LogMemID = "-"; }
578 $LogMemID = log_fix_quotes($LogMemID);
579 $LogGroupName = "-";
580 if(!isset($_SESSION['UserGroup'])) {
581         $LogGroupName = "-"; }
582 if(isset($_SESSION['UserGroup'])&&$_SESSION['UserGroup']===null) {
583         $LogGroupName = "-"; }
584 if(isset($_SESSION['UserGroup'])&&$_SESSION['UserGroup']!==null) {
585         $LogGroupName = $_SESSION['UserGroup']; }
586 if(trim($LogGroupName, "\x00..\x1F") == "") { $LogGroupName = "-"; }
587 $LogGroupName = log_fix_quotes($LogGroupName);
588 $LogGroupID = "-";
589 if(!isset($_SESSION['UserGroupID'])) {
590         $LogGroupID = "-"; }
591 if(isset($_SESSION['UserGroupID'])&&$_SESSION['UserGroupID']===null) {
592         $LogGroupID = "-"; }
593 if(isset($_SESSION['UserGroupID'])&&$_SESSION['UserGroupID']!==null) {
594         $LogGroupID = $_SESSION['UserGroupID']; }
595 if(trim($LogGroupID, "\x00..\x1F") == "") { $LogGroupID = "-"; }
596 $LogGroupID = log_fix_quotes($LogGroupID);
597 $LOG_QUERY_STRING = "";
598 if($_SERVER["QUERY_STRING"]!=="") {
599 $LOG_QUERY_STRING = "?".$_SERVER["QUERY_STRING"]; }
600 if(trim($LOG_QUERY_STRING, "\x00..\x1F") == "") { $LOG_QUERY_STRING = ""; }
601 $LOG_QUERY_STRING = log_fix_quotes($LOG_QUERY_STRING);
602 $oldcontentsize = $contentsize;
603 if($oldcontentsize=="-") { $oldcontentsize = 0; }
604 if($contentsize===0) { $contentsize = "-"; }
605 if($contentsize=="-"&&$headersize!==0) { $fullsitesize = $headersize; }
606 if($contentsize!="-"&&$headersize!==0) { $fullsitesize = $contentsize + $headersize; }
607 if($status=="302") { $contentsize = "-"; }
608 $HTTP_REQUEST_LINE = $_SERVER["REQUEST_METHOD"]." ".$_SERVER["REQUEST_URI"]." ".$_SERVER["SERVER_PROTOCOL"];
609 $HTTP_REQUEST_LINE = log_fix_quotes($HTTP_REQUEST_LINE);
610 $logtxt = preg_replace("/%%/s", "{percent}p", $logtxt);
611 $logtxt = preg_replace("/%([\<\>]*?)a/s", $_SERVER['REMOTE_ADDR'], $logtxt);
612 $logtxt = preg_replace("/%([\<\>]*?)A/s", $_SERVER["SERVER_ADDR"], $logtxt);
613 $logtxt = preg_replace("/%([\<\>]*?)B/s", $oldcontentsize, $logtxt);
614 $logtxt = preg_replace("/%([\<\>]*?)b/s", $contentsize, $logtxt);
615 $logtxt = preg_replace_callback("/%([\<\>]*?)\{([^\}]*)\}C/s", "get_cookie_values", $logtxt);
616 $logtxt = preg_replace_callback("/%([\<\>]*?)\{([^\}]*)\}e/s", "get_env_values", $logtxt);
617 $logtxt = preg_replace("/%([\<\>]*?)f/s", log_fix_quotes($_SERVER["SCRIPT_FILENAME"]), $logtxt);
618 $logtxt = preg_replace("/%([\<\>]*?)h/s", $_SERVER['REMOTE_ADDR'], $logtxt);
619 $logtxt = preg_replace("/%([\<\>]*?)H/s", $_SERVER["SERVER_PROTOCOL"], $logtxt);
620 $logtxt = preg_replace("/%([\<\>]*?)\{Referer\}i/s", $LOG_URL_REFERER, $logtxt);
621 $logtxt = preg_replace("/%([\<\>]*?)\{User-Agent\}i/s", $LOG_USER_AGENT, $logtxt);
622 $logtxt = preg_replace_callback("/%([\<\>]*?)\{([^\}]*)\}i/s", "get_server_values", $logtxt);
623 $logtxt = preg_replace("/%([\<\>]*?)l/s", "-", $logtxt);
624 $logtxt = preg_replace("/%([\<\>]*?)m/s", $_SERVER["REQUEST_METHOD"], $logtxt);
625 $logtxt = preg_replace("/%([\<\>]*?)p/s", $_SERVER["SERVER_PORT"], $logtxt);
626 $logtxt = preg_replace("/%([\<\>]*?)q/s", $LOG_QUERY_STRING, $logtxt);
627 $logtxt = preg_replace("/%([\<\>]*?)r/s", $HTTP_REQUEST_LINE, $logtxt);
628 $logtxt = preg_replace("/%([\<\>]*?)s/s", $status, $logtxt);
629 $logtxt = preg_replace("/%([\<\>]*?)t/s", "[".$servcurtime->format("d/M/Y:H:i:s O")."]", $logtxt);
630 $logtxt = preg_replace_callback("/%([\<\>]*?)\{([^\}]*)\}t/s", "get_time", $logtxt);
631 $logtxt = preg_replace("/%([\<\>]*?)u/s", $LOG_AUTH_USER, $logtxt);
632 $logtxt = preg_replace("/%([\<\>]*?)U/s", log_fix_quotes($_SERVER["PHP_SELF"]), $logtxt);
633 $logtxt = preg_replace("/%([\<\>]*?)v/s", $_SERVER["SERVER_NAME"], $logtxt);
634 $logtxt = preg_replace("/%([\<\>]*?)V/s", $_SERVER["SERVER_NAME"], $logtxt);
635 // Not what it should be but PHP dose not have variable to get Apache ServerName config value. :( 
636 $logtxt = preg_replace("/%([\<\>]*?)O/s", $fullsitesize, $logtxt);
637 $logtxt = preg_replace_callback("/%([\<\>]*?)\{([^\}]*)\}s/s", "get_setting_values", $logtxt);
638 $logtxt = preg_replace("/\%\{UserName\}m/s", $LogMemName, $logtxt);
639 $logtxt = preg_replace("/\%\{MemberName\}m/s", $LogMemName, $logtxt);
640 $logtxt = preg_replace("/\%\{UserID\}m/s", $LogMemID, $logtxt);
641 $logtxt = preg_replace("/\%\{MemberID\}m/s", $LogMemID, $logtxt);
642 $logtxt = preg_replace("/\%\{UserGroup\}m/s", $LogGroupName, $logtxt);
643 $logtxt = preg_replace("/\%\{MemberGroup\}m/s", $LogGroupName, $logtxt);
644 $logtxt = preg_replace("/\%\{UserGroupID\}m/s", $LogGroupID, $logtxt);
645 $logtxt = preg_replace("/\%\{MemberGroupID\}m/s", $LogGroupID, $logtxt);
646 $logtxt = preg_replace("/\{percent\}p/s", "%", $logtxt);
647 if(isset($logfile)&&$logfile!==null) {
648         $fp = fopen($logfile, "a+");
649         if (flock($fp, LOCK_EX)) {
650                 $logtxtnew = $logtxt."\r\n";
651                 fwrite($fp, $logtxtnew, strlen($logtxtnew)); 
652                 flock($fp, LOCK_UN); }
653         fclose($fp); 
654         @chmod($logfile, 0666); }
655 return $logtxt; }
656 function idb_log_maker($status=200,$contentsize="-") {
657 global $Settings,$SettDir;
658 if(isset($Settings['DefaultTimeZone'])) {
659 $servtz = new DateTimeZone($Settings['DefaultTimeZone']); }
660 if(!isset($Settings['DefaultTimeZone'])) {
661 $servtz = new DateTimeZone(date_default_timezone_get()); }
662 $servcurtime = new DateTime();
663 $servcurtime->setTimezone($servtz);
664 if(!isset($Settings['log_http_request'])) {
665         $Settings['log_http_request'] = "off"; }
666 if(!isset($Settings['log_config_format'])) {
667         $Settings['log_config_format'] = "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\""; }
668 if(isset($Settings['log_http_request'])&&$Settings['log_http_request']=="on"&&
669         $Settings['log_http_request']!==null&&$Settings['log_http_request']!="off") {
670 return apache_log_maker($Settings['log_config_format'], $SettDir['logs'].$Settings['sqltable'].$servcurtime->format("Ym").".log", $status, $contentsize, strlen(implode("\r\n",headers_list())."\r\n\r\n")); }
671 if(isset($Settings['log_http_request'])&&$Settings['log_http_request']!="on"&&
672         $Settings['log_http_request']!==null&&$Settings['log_http_request']!="off") {
673 $Settings['log_http_request'] = preg_replace_callback("/".preg_quote("%{", "/")."([^\}]*)".preg_quote("}t", "/")."/s", "get_time", $Settings['log_http_request']);
674 $Settings['log_http_request'] = preg_replace_callback("/".preg_quote("%{", "/")."([^\}]*)".preg_quote("}s", "/")."/s", "get_setting_values", $Settings['log_http_request']);
675 return apache_log_maker($Settings['log_config_format'], $SettDir['logs'].$Settings['log_http_request'], $status, $contentsize, strlen(implode("\r\n",headers_list())."\r\n\r\n")); } }
676 ?>