OSDN Git Service

ef59bd2ded4b1a4cf854f6cdf593f32e0ecced7d
[idb/iDB.git.git] / inc / misc / functions.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-2019 iDB Support - https://idb.osdn.jp/support/category.php?act=view&id=1
12     Copyright 2004-2019 Game Maker 2k - https://idb.osdn.jp/support/category.php?act=view&id=2
13
14     $FileInfo: functions.php - Last Update: 11/27/2020 SVN 925 - Author: cooldude2k $
15 */
16 $File3Name = basename($_SERVER['SCRIPT_NAME']);
17 if ($File3Name=="functions.php"||$File3Name=="/functions.php") {
18         require('index.php');
19         exit(); }
20 // Check the file names
21 function CheckFile($FileName) {
22 $File3Name = basename($_SERVER['SCRIPT_NAME']);
23 if ($File3Name==$FileName||$File3Name=="/".$FileName) {
24         require('index.php');
25         exit(); }
26 return null; }
27 function CheckFiles($FileName) {
28 $File3Name = basename($_SERVER['SCRIPT_NAME']);
29 if ($File3Name==$FileName||$File3Name=="/".$FileName) {
30         return true; } }
31 CheckFile("functions.php");
32 require($SettDir['misc']."compression.php");
33 if($Settings['sqltype']=="mysql") {
34 if(!in_array("ini_set", $disfunc)) {
35 @ini_set("mysql.default_host",$Settings['sqlhost']);
36 @ini_set("mysql.default_user",$Settings['sqluser']);
37 @ini_set("mysql.default_password",$Settings['sqlpass']); }
38 require($SettDir['sql']."mysql.php"); }
39 if($Settings['sqltype']=="mysqli") {
40 if(!in_array("ini_set", $disfunc)) {
41 @ini_set("mysqli.default_host",$Settings['sqlhost']);
42 @ini_set("mysqli.default_user",$Settings['sqluser']);
43 @ini_set("mysqli.default_pw",$Settings['sqlpass']); }
44 require($SettDir['sql']."mysqli.php"); }
45 if($Settings['sqltype']=="pgsql") {
46 require($SettDir['sql']."pgsql.php"); }
47 if($Settings['sqltype']=="sqlite") {
48 require($SettDir['sql']."sqlite.php"); }
49 if($Settings['sqltype']=="sqlite3") {
50 require($SettDir['sql']."sqlite3.php"); }
51 if($Settings['sqltype']=="cubrid") {
52 require($SettDir['sql']."cubrid.php"); }
53 require($SettDir['misc']."useragents.php");
54 require($SettDir['misc']."password.php");
55 /* 
56 if ($_GET['act']=="DeleteSession") { session_destroy(); }
57 if ($_GET['act']=="ResetSession") { session_unset(); }
58 if ($_GET['act']=="NewSessionID") { session_regenerate_id(); }
59 if ($_GET['act']=="PHPInfo") { phpinfo(); exit(); }
60 if ($_GET['act']=="phpinfo") { phpinfo(); exit(); }
61 if ($_GET['act']=="PHPCredits") { phpcredits(); exit(); }
62 if ($_GET['act']=="phpcredits") { phpcredits(); exit(); } 
63 */
64 function header_protocol($header) {
65 if(isset($_SERVER['SERVER_PROTOCOL'])&&strstr($_SERVER['SERVER_PROTOCOL'],"/1.0")) {
66         return "HTTP/1.0 ".$header; }
67 else {
68         return "HTTP/1.1 ".$header; } }
69 // Change the title and gzip page
70 function change_title($new_title,$use_gzip="off",$gzip_type="gzip") {
71 global $Settings,$urlstatus;
72 if(!isset($urlstatus)||!is_numeric($urlstatus)) { $urlstatus = 200; }
73 if($gzip_type!="gzip") { if($gzip_type!="deflate") { $gzip_type = "gzip"; } }
74 $output = trim(ob_get_clean());
75 $output = preg_replace("/<title>(.*?)<\/title>/i", "<title>".$new_title."</title>", $output);
76 $new_title_html = htmlentities($new_title, ENT_QUOTES, $Settings['charset']);
77 $output = preg_replace("/<meta itemprop=\"title\" property=\"og:title\" content=\"(.*?)\" \/>/i", "<meta itemprop=\"title\" property=\"og:title\" content=\"".$new_title_html."\" />", $output);
78 $output = preg_replace("/<meta itemprop=\"title\" property=\"twitter:title\" content=\"(.*?)\" \/>/i", "<meta itemprop=\"title\" property=\"twitter:title\" content=\"".$new_title_html."\" />", $output);
79 $output = preg_replace("/<meta name=\"title\" content=\"(.*?)\" \/>/i", "<meta name=\"title\" content=\"".$new_title_html."\" />", $output);
80 /* Change Some PHP Settings Fix the &PHPSESSID to &amp;PHPSESSID */
81 $SessName = session_name();
82 $output = preg_replace("/&PHPSESSID/", "&amp;PHPSESSID", $output);
83 $qstrcode = htmlentities($Settings['qstr'], ENT_QUOTES, $Settings['charset']);
84 $output = str_replace($Settings['qstr'].$SessName, $qstrcode.$SessName, $output);
85 if($use_gzip!="on") {
86         if($Settings['send_pagesize']=="on") {
87         @header("Content-Length: ".decoct(strlen($output))); 
88         @header("Content-MD5: ".base64_encode(md5($output))); }
89         idb_log_maker($urlstatus,strlen($output));
90         echo $output; }
91 if($use_gzip=="on") {
92         if($gzip_type=="gzip") {
93         $goutput = gzencode($output); }
94         if($gzip_type=="deflate") {
95         $goutput = gzcompress($output); }
96         if($Settings['send_pagesize']=="on") {
97         @header("Content-Length: ".decoct(strlen($goutput))); 
98         @header("Content-MD5: ".base64_encode(md5($goutput))); }
99         idb_log_maker($urlstatus,strlen($goutput));
100         echo $goutput; } }
101 // Fix amp => (&) to &amp; and gzip page
102 function fix_amp($use_gzip="off",$gzip_type="gzip") {
103 global $Settings,$urlstatus;
104 if(!isset($urlstatus)||!is_numeric($urlstatus)) { $urlstatus = 200; }
105 if($gzip_type!="gzip") { if($gzip_type!="deflate") { $gzip_type = "gzip"; } }
106 $output = trim(ob_get_clean());
107 /* Change Some PHP Settings Fix the &PHPSESSID to &amp;PHPSESSID */
108 $SessName = session_name();
109 $output = preg_replace("/&PHPSESSID/", "&amp;PHPSESSID", $output);
110 $qstrcode = htmlentities($Settings['qstr'], ENT_QUOTES, $Settings['charset']);
111 $output = str_replace($Settings['qstr'].$SessName, $qstrcode.$SessName, $output);
112 if($use_gzip!="on") {
113         if($Settings['send_pagesize']=="on") {
114         @header("Content-Length: ".decoct(strlen($output))); 
115         @header("Content-MD5: ".base64_encode(md5($output))); }
116         idb_log_maker($urlstatus,strlen($output));
117         echo $output; }
118 if($use_gzip=="on") {
119         if($gzip_type=="gzip") {
120         $goutput = gzencode($output); }
121         if($gzip_type=="deflate") {
122         $goutput = gzcompress($output); }
123         if($Settings['send_pagesize']=="on") {
124         @header("Content-Length: ".decoct(strlen($goutput))); 
125         @header("Content-MD5: ".base64_encode(md5($goutput))); }
126         idb_log_maker($urlstatus,strlen($goutput));
127         echo $goutput; } }
128 // GZip page for faster download
129 function gzip_page($use_gzip="off",$gzip_type="gzip") {
130 global $Settings,$urlstatus;
131 if(!isset($urlstatus)||!is_numeric($urlstatus)) { $urlstatus = 200; }
132 $output = trim(ob_get_clean());
133 if($gzip_type!="gzip") { if($gzip_type!="deflate") { $gzip_type = "gzip"; } }
134 if($use_gzip!="on") {
135         if($Settings['send_pagesize']=="on") {
136         @header("Content-Length: ".decoct(strlen($output))); 
137         @header("Content-MD5: ".base64_encode(md5($output))); }
138         idb_log_maker($urlstatus,strlen($output));
139         echo $output; }
140 if($use_gzip=="on") {
141         if($gzip_type=="gzip") {
142         $goutput = gzencode($output); }
143         if($gzip_type=="deflate") {
144         $goutput = gzcompress($output); }
145         if($Settings['send_pagesize']=="on") {
146         @header("Content-Length: ".decoct(strlen($goutput))); 
147         @header("Content-MD5: ".base64_encode(md5($goutput))); }
148         idb_log_maker($urlstatus,strlen($goutput));
149         echo $goutput; } }
150 $foo="bar"; $$foo="foo";
151 // Kill bad vars for some functions
152 function killbadvars($varname) {
153 $badphp1 = array('$'); $badphp2 = array(null);
154 $varname = str_replace($badphp1, $badphp2, $varname);
155 $varname = preg_replace("/(_SERVER|_ENV|_COOKIE|_SESSION)/i", null, $varname);
156 $varname = preg_replace("/(_GET|_POST|_FILES|_REQUEST|GLOBALS)/i", null, $varname);
157 $varname = preg_replace("/(HTTP_SERVER_VARS|HTTP_ENV_VARS)/i", null, $varname);
158 $varname = preg_replace("/(HTTP_COOKIE_VARS|HTTP_SESSION_VARS)/i", null, $varname);
159 $varname = preg_replace("/(HTTP_GET_VARS|HTTP_POST_VARS|HTTP_POST_FILES)/i", null, $varname);
160         return $varname; }
161 // Trying to fix this bug. ^_^
162 // http://xforce.iss.net/xforce/xfdb/49697
163 if(!isset($Settings['DefaultTheme'])) {
164         $Settings['DefaultTheme'] = "iDB"; }
165 // Change the text to icons(smileys)
166 function text2icons($Text,$sqlt,$link=null) {
167 global $SQLStat;
168 if(!isset($link)) { $link = $SQLStat; }
169 $melaniequery=sql_pre_query("SELECT * FROM \"".$sqlt."smileys\"", array(null));
170 $melanieresult=sql_query($melaniequery,$link);
171 $melanienum=sql_num_rows($melanieresult);
172 $melanies=0;
173 while ($melanies < $melanienum) {
174 $FileName=sql_result($melanieresult,$melanies,"FileName");
175 $SmileName=sql_result($melanieresult,$melanies,"SmileName");
176 $SmileText=sql_result($melanieresult,$melanies,"SmileText");
177 $SmileDirectory=sql_result($melanieresult,$melanies,"Directory");
178 $ShowSmile=sql_result($melanieresult,$melanies,"Display");
179 $ReplaceType=sql_result($melanieresult,$melanies,"ReplaceCI");
180 if($ReplaceType=="on") { $ReplaceType = "yes"; }
181 if($ReplaceType=="off") { $ReplaceType = "no"; }
182 if($ReplaceType!="yes"||$ReplaceType!="no") { $ReplaceType = "no"; }
183 $Smile1 = $SmileText;
184 $Smile2 = '<img src="'.$SmileDirectory.''.$FileName.'" style="vertical-align: middle; border: 0px;" title="'.$SmileName.'" alt="'.$SmileName.'" />';
185 if($ReplaceType=="no") {
186 $Text = str_replace($Smile1, $Smile2, $Text); }
187 if($ReplaceType=="yes") {
188         $Smile1 = preg_quote($SmileText,"/");
189 $Text = preg_replace("/".$Smile1."/i",$Smile2,$Text); }
190 ++$melanies; } return $Text; }
191 // Removes the bad stuff
192 function remove_bad_entities($Text) {
193 //HTML Entities Dec Version
194 $Text = preg_replace("/&#8238;/isU","",$Text);
195 $Text = preg_replace("/&#8194;/isU","",$Text);
196 $Text = preg_replace("/&#8195;/isU","",$Text);
197 $Text = preg_replace("/&#8201;/isU","",$Text);
198 $Text = preg_replace("/&#8204;/isU","",$Text);
199 $Text = preg_replace("/&#8205;/isU","",$Text);
200 $Text = preg_replace("/&#8206;/isU","",$Text);
201 $Text = preg_replace("/&#8207;/isU","",$Text);
202 //HTML Entities Hex Version
203 $Text = preg_replace("/&#x202e;/isU","",$Text);
204 $Text = preg_replace("/&#x2002;/isU","",$Text);
205 $Text = preg_replace("/&#x2003;/isU","",$Text);
206 $Text = preg_replace("/&#x2009;/isU","",$Text);
207 $Text = preg_replace("/&#x200c;/isU","",$Text);
208 $Text = preg_replace("/&#x200d;/isU","",$Text);
209 $Text = preg_replace("/&#x200e;/isU","",$Text);
210 $Text = preg_replace("/&#x200f;/isU","",$Text);
211 //HTML Entities Name Version
212 $Text = preg_replace("/&ensp;/isU","",$Text);
213 $Text = preg_replace("/&emsp;/isU","",$Text);
214 $Text = preg_replace("/&thinsp;/isU","",$Text);
215 $Text = preg_replace("/&zwnj;/isU","",$Text);
216 $Text = preg_replace("/&zwj;/isU","",$Text);
217 $Text = preg_replace("/&lrm;/isU","",$Text);
218 $Text = preg_replace("/&rlm;/isU","",$Text);
219 return $Text; }
220 // Remove the bad stuff
221 function remove_spaces($Text) {
222 $Text = preg_replace("/(^\t+|\t+$)/","",$Text);
223 $Text = preg_replace("/(^\n+|\n+$)/","",$Text);
224 $Text = preg_replace("/(^\r+|\r+$)/","",$Text);
225 $Text = preg_replace("/(\r|\n|\t)+/"," ",$Text);
226 $Text = preg_replace("/\s\s+/"," ",$Text);
227 $Text = preg_replace("/(^\s+|\s+$)/","",$Text);
228 $Text = trim($Text, "\x00..\x1F");
229 $Text = remove_bad_entities($Text);
230 return $Text; }
231 // Fix some chars
232 function fixbamps($text) {
233 $fixamps1 = array("&amp;copy;","&amp;reg;","&amp;trade;","&amp;quot;","&amp;amp;","&amp;lt;","&amp;gt;","&amp;(a|e|i|o|u|y)acute;","&amp;(a|e|i|o|u)grave;","&amp;(a|e|i|o|u)circ;","&amp;(a|e|i|o|u|y)uml;","&amp;(a|o|n)tilde;","&amp;aring;","&amp;aelig;","&amp;ccedil;","&amp;eth;","&amp;oslash;","&amp;szlig;","&amp;thorn;");
234 $fixamps2 = array("&copy;","&reg;","&trade;","&quot;","&amp;","&lt;","&gt;","&\\1acute;","&\\1grave;","&\\1circ;","&\\1uml;","&\\1tilde;","&aring;","&aelig;","&ccedil;","&eth;","&oslash;","&szlig;","&thorn;");
235 $ampnum = count($fixamps1); $ampi=0;
236 while ($ampi < $ampnum) {
237 $text = preg_replace("/".$fixamps1[$ampi]."/i", $fixamps2[$ampi], $text);
238 ++$ampi; }
239 $text = preg_replace("/&amp;#(x[a-f0-9]+|[0-9]+);/i", "&#$1;", $text);
240 return $text; }
241 $utshour = $dayconv['hour'];
242 $utsminute = $dayconv['minute'];
243 // Change Time Stamp to a readable time
244 function GMTimeChange($format,$timestamp,$offset,$minoffset=null,$dst=null) {
245 global $utshour,$utsminute;
246 $dstake = null;
247 if(!is_numeric($minoffset)) { $minoffset = "00"; }
248 $ts_array = explode(":",$offset);
249 if(count($ts_array)!=2) {
250         if(!isset($ts_array[0])) { $ts_array[0] = "0"; }
251         if(!isset($ts_array[1])) { $ts_array[1] = "00"; }
252         $offset = $ts_array[0].":".$ts_array[1]; }
253 if(!is_numeric($ts_array[0])) { $ts_array[0] = "0"; }
254 if(!is_numeric($ts_array[1])) { $ts_array[1] = "00"; }
255 if($ts_array[1]<0) { $ts_array[1] = "00"; $offset = $ts_array[0].":".$ts_array[1]; }
256 $tsa = array("offset" => $offset, "hour" => $ts_array[0], "minute" => $ts_array[1]);
257 //$tsa['minute'] = $tsa['minute'] + $minoffset;
258 if($dst!="on"&&$dst!="off") { $dst = "off"; }
259 if($dst=="on") { if($dstake!="done") { 
260         $dstake = "done"; $tsa['hour'] = $tsa['hour']+1; } }
261 $utimestamp = $tsa['hour'] * $utshour;
262 $utimestamp = $utimestamp + $tsa['minute'] * $utsminute;
263 $utimestamp = $utimestamp + $minoffset * $utsminute;
264 $timestamp = $timestamp + $utimestamp;
265 return date($format,$timestamp); }
266 // Change Time Stamp to a readable time
267 function TimeChange($format,$timestamp,$offset,$minoffset=null,$dst=null) {
268 return GMTimeChange($format,$timestamp,$offset,$minoffset,$dst); }
269 // Make a GMT Time Stamp
270 function GMTimeStamp() {
271 $GMTHour = gmdate("H");
272 $GMTMinute = gmdate("i");
273 $GMTSecond = gmdate("s");
274 $GMTMonth = gmdate("n");
275 $GMTDay = gmdate("d");
276 $GMTYear = gmdate("Y");
277 return mktime($GMTHour,$GMTMinute,$GMTSecond,$GMTMonth,$GMTDay,$GMTYear); }
278 // Make a GMT Time Stamp alt version
279 function GMTimeStampS() { return time() - date('Z', time()); }
280 // Get GMT Time
281 function GMTimeGet($format,$offset,$minoffset=null,$dst=null,$taddon=null) {
282         if(!is_numeric($taddon)) { $taddon = null; }
283         if($taddon!==null) {
284         return GMTimeChange($format,GMTimeStamp()+$taddon,$offset,$minoffset,$dst); }
285         if($taddon===null) {
286         return GMTimeChange($format,GMTimeStamp(),$offset,$minoffset,$dst); } }
287 // Get GMT Time alt version
288 function GMTimeGetS($format,$offset,$minoffset=null,$dst=null) {
289 global $utshour,$utsminute;
290 $dstake = null;
291 if(!is_numeric($offset)) { $offset = "0"; }
292 if(!is_numeric($minoffset)) { $minoffset = "00"; }
293 $ts_array = explode(":",$offset);
294 if(count($ts_array)!=2) {
295         if(!isset($ts_array[0])) { $ts_array[0] = "0"; }
296         if(!isset($ts_array[1])) { $ts_array[1] = "00"; }
297         $offset = $ts_array[0].":".$ts_array[1]; }
298 if(!is_numeric($ts_array[0])) { $ts_array[0] = "0"; }
299 if(!is_numeric($ts_array[1])) { $ts_array[1] = "00"; }
300 if($ts_array[1]<0) { $ts_array[1] = "00"; $offset = $ts_array[0].":".$ts_array[1]; }
301 $tsa = array("offset" => $offset, "hour" => $ts_array[0], "minute" => $ts_array[1]);
302 //$tsa['minute'] = $tsa['minute'] + $minoffset;
303 if($dst!="on"&&$dst!="off") { $dst = "off"; }
304 if($dst=="on") { if($dstake!="done") { 
305         $dstake = "done"; $tsa['hour'] = $tsa['hour']+1; } }
306 $utimestamp = $tsa['hour'] * $utshour;
307 $utimestamp = $utimestamp + $tsa['minute'] * $utsminute;
308 $utimestamp = $utimestamp + $minoffset * $utsminute;
309 $timestamp = $timestamp + $utimestamp;
310 return date($format,mktime()+$timestamp); }
311 // Get Server offset
312 function GetSeverZone() {
313 $TestHour1 = date("H");
314 @putenv("OTZ=".getenv("TZ"));
315 @putenv("TZ=GMT");
316 $TestHour2 = date("H");
317 @putenv("TZ=".getenv("OTZ"));
318 $TestHour3 = $TestHour1-$TestHour2;
319 return $TestHour3; }
320 // Get Server offset alt version
321 function SeverOffSet() {
322 $TestHour1 = date("H");
323 $TestHour2 = gmdate("H");
324 $TestHour3 = $TestHour1-$TestHour2;
325 return $TestHour3; }
326 // Get Server offset new version
327 function SeverOffSetNew() {
328 return gmdate("g",mktime(0,date("Z"))); }
329 function gmtime() { return time() - (int) date('Z'); }
330 // Acts like highlight_file();
331 function file_get_source($filename,$return = FALSE) {
332 $phpsrc = file_get_contents($filename);
333 $phpsrcs = highlight_string($phpsrc,$return);
334 return $phpsrcs; }
335 // Also acts like highlight_file(); but valid xhtml
336 function valid_get_source($filename) {
337 $phpsrcs = file_get_source($filename,TRUE);
338 // Change font tag to span tag for valid xhtml
339 $phpsrcs = preg_replace("/\<font color=\"(.*?)\"\>/i", "<span style=\"color: \\1;\">", $phpsrcs);
340 $phpsrcs = preg_replace("/\<\/font>/i", "</span>", $phpsrcs);
341 return $phpsrcs; }
342 // Check to see if the user is hidden/shy/timid. >_> | ^_^ | <_<
343 function GetUserName($idu,$sqlt,$link=null) { $UsersName = null;
344 global $SQLStat;
345 if(!isset($link)) { $link = $SQLStat; }
346 $gunquery = sql_pre_query("SELECT * FROM \"".$sqlt."members\" WHERE \"id\"=%i LIMIT 1", array($idu));
347 $gunresult=sql_query($gunquery,$link);
348 $gunnum=sql_num_rows($gunresult);
349 // I'm now hidden from you. ^_^ | <_< I cant find you.
350 $UsersHidden = "yes";
351 if($gunnum>0){
352 $UsersName=sql_result($gunresult,0,"Name");
353 // Am i still hidden. o_O <_< I can see you.
354 $UsersHidden=sql_result($gunresult,0,"HiddenMember"); }
355 sql_free_result($gunresult);
356 $UsersInfo['Name'] = $UsersName;
357 $UsersInfo['Hidden'] = $UsersHidden;
358 return $UsersInfo; }
359 if(!function_exists('hash')) {
360 function hash($algo, $data, $raw_output = false) {
361 if($algo!="md5"&&$algo!="sha1") { $algo = "md5"; }
362 return $algo($data); } }
363 if(!function_exists('hash_algos')) {
364 function hash_algos() {
365 return array(0 => "md5", 1 => "sha1"); } }
366 if(!function_exists('hash_hmac')) {
367 function hash_hmac($hash, $data, $key, $raw_output = false) {
368   if($hash=="sha3-224") { $hash = "sha3224"; }
369   if($hash=="sha3-256") { $hash = "sha3256"; }
370   if($hash=="sha3-384") { $hash = "sha3384"; }
371   if($hash=="sha3-512") { $hash = "sha3512"; }
372   $blocksize = 64;
373   if (strlen($key)>$blocksize) {
374   if (function_exists('hash')) {
375   if($hash=="sha3224") { $hash = "sha3-224"; }
376   if($hash=="sha3256") { $hash = "sha3-256"; }
377   if($hash=="sha3384") { $hash = "sha3-384"; }
378   if($hash=="sha3512") { $hash = "sha3-512"; }
379   $key=pack('H*',hash($hash, $key)); }
380   if (!function_exists('hash')) {
381   $key=pack('H*',$hash($key)); } }
382   $key=str_pad($key, $blocksize, chr(0x00));
383   $ipad=str_repeat(chr(0x36),$blocksize);
384   $opad=str_repeat(chr(0x5c),$blocksize);
385   if($hash=="sha3224") { $hash = "sha3-224"; }
386   if($hash=="sha3256") { $hash = "sha3-256"; }
387   if($hash=="sha3384") { $hash = "sha3-384"; }
388   if($hash=="sha3512") { $hash = "sha3-512"; }
389   return hash($hash, ($key^$opad).pack('H*',hash($hash, ($key^$ipad).$data))); } }
390 // hmac hash function
391 function hmac($data,$key,$hash='sha1',$blocksize=64) {
392   if($hash=="sha3-224") { $hash = "sha3224"; }
393   if($hash=="sha3-256") { $hash = "sha3256"; }
394   if($hash=="sha3-384") { $hash = "sha3384"; }
395   if($hash=="sha3-512") { $hash = "sha3512"; }
396   if (!function_exists('hash_hmac')) {
397   if (strlen($key)>$blocksize) {
398   if (function_exists('hash')) {
399   $key=pack('H*',hash($hash, $key)); }
400   if (!function_exists('hash')) {
401   $key=pack('H*',$hash($key)); } }
402   $key=str_pad($key, $blocksize, chr(0x00));
403   $ipad=str_repeat(chr(0x36),$blocksize);
404   $opad=str_repeat(chr(0x5c),$blocksize);
405   if (function_exists('hash')) {
406   if($hash=="sha3224") { $hash = "sha3-224"; }
407   if($hash=="sha3256") { $hash = "sha3-256"; }
408   if($hash=="sha3384") { $hash = "sha3-384"; }
409   if($hash=="sha3512") { $hash = "sha3-512"; }
410   return hash($hash, ($key^$opad).pack('H*',hash($hash, ($key^$ipad).$data))); }
411   if (!function_exists('hash')) {
412   return $hash(($key^$opad).pack('H*',$hash(($key^$ipad).$data))); } }
413   if (function_exists('hash_hmac')) { 
414   if($hash=="sha3224") { $hash = "sha3-224"; }
415   if($hash=="sha3256") { $hash = "sha3-256"; }
416   if($hash=="sha3384") { $hash = "sha3-384"; }
417   if($hash=="sha3512") { $hash = "sha3-512"; }
418   return hash_hmac($hash,$data,$key); } }
419 // b64hmac hash function
420 function b64e_hmac($data,$key,$extdata,$hash='sha1',$blocksize=64) {
421         $extdata2 = hexdec($extdata); $key = $key.$extdata2;
422   return base64_encode(hmac($data,$key,$hash,$blocksize).$extdata); }
423 // b64hmac rot13 hash function
424 function b64e_rot13_hmac($data,$key,$extdata,$hash='sha1',$blocksize=64) {
425         $data = str_rot13($data);
426         $extdata2 = hexdec($extdata); $key = $key.$extdata2;
427   return base64_encode(hmac($data,$key,$hash,$blocksize).$extdata); }
428 // salt hmac hash function
429 function salt_hmac($size1=6,$size2=12) {
430 $hprand = rand($size1,$size2); $i = 0; $hpass = "";
431 while ($i < $hprand) {
432 $hspsrand = rand(1,2);
433 if($hspsrand!=1&&$hspsrand!=2) { $hspsrand=1; }
434 if($hspsrand==1) { $hpass .= chr(rand(48,57)); }
435 /* if($hspsrand==2) { $hpass .= chr(rand(65,70)); } */
436 if($hspsrand==2) { $hpass .= chr(rand(97,102)); }
437 ++$i; } return $hpass; }
438 /* is_empty by M at http://us2.php.net/manual/en/function.empty.php#74093 */
439 function is_empty($var) {
440     if (((is_null($var) || rtrim($var) == "") &&
441                 $var !== false) || (is_array($var) && empty($var))) {
442         return true; } else { return false; } }
443 // PHP 5 hash algorithms to functions :o 
444 if(function_exists('hash')&&function_exists('hash_algos')) {
445 if(in_array("md2",hash_algos())&&!function_exists("md2")) { 
446 function md2($data) { return hash("md2",$data); } } 
447 if(in_array("md4",hash_algos())&&!function_exists("md4")) { 
448 function md4($data) { return hash("md4",$data); } }
449 if(in_array("md5",hash_algos())&&!function_exists("md5")) { 
450 function md5($data) { return hash("md5",$data); } }
451 if(in_array("sha1",hash_algos())&&!function_exists("sha1")) { 
452 function sha1($data) { return hash("sha1",$data); } }
453 if(in_array("sha224",hash_algos())&&!function_exists("sha224")) { 
454 function sha224($data) { return hash("sha224",$data); } }
455 if(in_array("sha256",hash_algos())&&!function_exists("sha256")) { 
456 function sha256($data) { return hash("sha256",$data); } }
457 if(in_array("sha384",hash_algos())&&!function_exists("sha384")) { 
458 function sha384($data) { return hash("sha384",$data); } }
459 if(in_array("sha512",hash_algos())&&!function_exists("sha512")) { 
460 function sha512($data) { return hash("sha512",$data); } }
461 if(in_array("sha3-224",hash_algos())&&!function_exists("sha3224")) { 
462 function sha3224($data) { return hash("sha3-224",$data); } }
463 if(in_array("sha3-256",hash_algos())&&!function_exists("sha3256")) { 
464 function sha3256($data) { return hash("sha3-256",$data); } }
465 if(in_array("sha3-384",hash_algos())&&!function_exists("sha3384")) { 
466 function sha3384($data) { return hash("sha3-384",$data); } }
467 if(in_array("sha3-512",hash_algos())&&!function_exists("sha3512")) { 
468 function sha3512($data) { return hash("sha3-512",$data); } }
469 if(in_array("ripemd128",hash_algos())&&!function_exists("ripemd128")) { 
470 function ripemd128($data) { return hash("ripemd128",$data); } }
471 if(in_array("ripemd160",hash_algos())&&!function_exists("ripemd160")) { 
472 function ripemd160($data) { return hash("ripemd160",$data); } }
473 if(in_array("ripemd256",hash_algos())&&!function_exists("ripemd256")) { 
474 function ripemd256($data) { return hash("ripemd256",$data); } }
475 if(in_array("ripemd512",hash_algos())&&!function_exists("ripemd512")) { 
476 function ripemd320($data) { return hash("ripemd320",$data); } } }
477 // Try and convert IPB 2.0.0 style passwords to iDB style passwords
478 function hash2xkey($data,$key,$hash1='md5',$hash2='md5') {
479   return $hash1($hash2($key).$hash2($data)); }
480 // Hash two times with md5 and sha1 for DF2k
481 function PassHash2x($Text) {
482 $Text = md5($Text);
483 $Text = sha1($Text);
484 return $Text; }
485 // Hash two times with hmac-md5 and hmac-sha1
486 function PassHash2x2($data,$key,$extdata,$blocksize=64) {
487 $extdata2 = hexdec($extdata); $key = $key.$extdata2;
488 $Text = hmac($data,$key,"md5").$extdata; 
489 $Text = hmac($Text,$key,"sha1").$extdata;
490 return base64_encode($Text); }
491 function cp($infile,$outfile,$mode="w") { 
492    $contents = file_get_contents($infile);
493    $cpfp = fopen($outfile,$mode);
494    fwrite($cpfp, $contents);
495    fclose($cpfp);
496    return true; }
497
498 // b64hmac hash function
499 function neo_b64e_hmac($data,$key,$extdata,$hash='sha1',$blocksize=64) {
500         $extdata2 = hexdec($extdata); $key = $key.$extdata2;
501   return base64_encode(password_hash($data.$extdata, PASSWORD_BCRYPT)); }
502 // b64hmac rot13 hash function
503 function neo_b64e_rot13_hmac($data,$key,$extdata,$hash='sha1',$blocksize=64) {
504         $data = str_rot13($data);
505         $extdata2 = hexdec($extdata); $key = $key.$extdata2;
506   return base64_encode(password_hash($data.$extdata, PASSWORD_BCRYPT)); }
507
508 if(!function_exists('password_hash')) { 
509 function bcrypt($data) { return password_hash($data,PASSWORD_BCRYPT); } }
510
511 /* is_empty by s rotondo90 at gmail com at https://www.php.net/manual/en/function.hash-equals.php#119576*/
512 if(!function_exists('hash_equals')) {
513     function hash_equals($known_string, $user_string) {
514         $ret = 0;
515        
516         if (strlen($known_string) !== strlen($user_string)) {
517             $user_string = $known_string;
518             $ret = 1;
519         }
520        
521         $res = $known_string ^ $user_string;
522        
523         for ($i = strlen($res) - 1; $i >= 0; --$i) {
524             $ret |= ord($res[$i]);
525         }
526        
527         return !$ret;
528     }
529 }
530
531 /* str_ireplace for PHP below ver. 5 updated // 
532 //       by Kazuki Przyborowski - Cool Dude 2k      //
533 //      and upaded by Kazuki Przyborowski again     */
534 if(!function_exists('str_ireplace')) {
535 function str_ireplace($search,$replace,$subject) {
536 if(!is_array($search)&&is_array($replace)) {
537         $search = array($search); }
538 if(is_array($search)&&!is_array($replace)) {
539         $replace = array($replace); }
540 if(is_array($search)&&is_array($replace)) {
541         $sc=count($search); $rc=count($replace); $sn=0;
542         if($sc!=$rc) { return false; }
543 while ($sc > $sn) {
544         $search[$sn] = preg_quote($search[$sn], "/");
545         $subject = preg_replace("/".$search[$sn]."/i", $replace[$sn], $subject);
546         ++$sn; } }
547 if(!is_array($search)&&!is_array($replace)) {
548 $search = preg_quote($search, "/");
549 $subject = preg_replace("/".$search."/i", $replace, $subject); }
550 return $subject; } }
551 /*   Adds httponly to PHP below Ver. 5.2.0   // 
552 //       by Kazuki Przyborowski - Cool Dude 2k      */
553 function http_set_cookie($name,$value=null,$expire=null,$path=null,$domain=null,$secure=false,$httponly=false) {
554         $mkcookie = null; $expireGMT = null;
555         if(!isset($name)) { 
556         output_error("Error: You need to enter a name for cookie.",E_USER_ERROR); 
557         return false; }
558         if(!isset($expire)) { 
559         output_error("Error: You need to enter a time for cookie to expire.",E_USER_ERROR); 
560         return false; }
561         $expireGMT = gmdate("D, d-M-Y H:i:s \G\M\T", $expire);
562         if(!isset($value)) { $value = null; }
563         if(!isset($httponly)||$httponly==false) {
564         setcookie($name, $value, $expire, $path, $domain, $secure); return true; }
565         if(version_compare(PHP_VERSION,"5.2.0",">=")&&$httponly==true) {
566         setcookie($name, $value, $expire, $path, $domain, $secure, $httponly); return true; }
567         if(version_compare(PHP_VERSION,"5.2.0","<")&&$httponly==true) {
568         $mkcookie = "Set-Cookie: ".rawurlencode($name)."=".rawurlencode($value);
569         $mkcookie = $mkcookie."; expires=".$expireGMT;
570         if(isset($path)&&$path!=null) { $mkcookie = $mkcookie."; path=".$path; }
571         if(isset($domain)&&$domain!=null) { $mkcookie = $mkcookie."; domain=".$domain; }
572         if(isset($secure)&&$secure===true) { $mkcookie = $mkcookie."; secure"; }
573         if(isset($httponly)&&$httponly===true) { $mkcookie = $mkcookie."; HttpOnly"; }
574 header($mkcookie, false); return true; } }
575 $foobar="fubar"; $$foobar="foobar";
576 // Debug info
577 function dump_included_files($type="var_dump") {
578         if(!isset($type)) { $type = "var_dump"; }
579         if($type=="print_r") { return print_r(get_included_files()); }
580         if($type=="var_dump") { return var_dump(get_included_files()); }
581         if($type=="var_export") { return var_export(get_included_files()); } }
582 function count_included_files() {       return count(get_included_files()); }
583 function dump_extensions($type="var_dump") {
584         if(!isset($type)) { $type = "var_dump"; }
585         if($type=="print_r") { return print_r(get_loaded_extensions()); }
586         if($type=="var_dump") { return var_dump(get_loaded_extensions()); }
587         if($type=="var_export") { return var_export(get_loaded_extensions()); } }
588 function count_extensions() {   return count(get_loaded_extensions()); }
589 // human_filesize by evgenij at kostanay dot kz 
590 // URL: https://www.php.net/manual/en/function.filesize.php#120250
591 function human_filesize($bytes, $decimals = 2) {
592     $factor = floor((strlen($bytes) - 1) / 3);
593     if ($factor > 0) $sz = 'KMGT';
594     return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . ' ' . @$sz[$factor - 1] . 'B';
595 }
596 ?>