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