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-2008 Cool Dude 2k - http://idb.berlios.de/
12 Copyright 2004-2008 Game Maker 2k - http://intdb.sourceforge.net/
14 $FileInfo: functions.php - Last Update: 12/10/2008 SVN 209 - Author: cooldude2k $
16 $File3Name = basename($_SERVER['SCRIPT_NAME']);
17 if ($File3Name=="functions.php"||$File3Name=="/functions.php") {
20 // Check the file names
21 function CheckFile($FileName) {
22 $File3Name = basename($_SERVER['SCRIPT_NAME']);
23 if ($File3Name==$FileName||$File3Name=="/".$FileName) {
27 function CheckFiles($FileName) {
28 $File3Name = basename($_SERVER['SCRIPT_NAME']);
29 if ($File3Name==$FileName||$File3Name=="/".$FileName) {
31 CheckFile("functions.php");
32 require($SettDir['misc']."compression.php");
34 if ($_GET['act']=="DeleteSession") { @session_destroy(); }
35 if ($_GET['act']=="ResetSession") { @session_unset(); }
36 if ($_GET['act']=="NewSessionID") { @session_regenerate_id(); }
37 if ($_GET['act']=="PHPInfo") { @phpinfo(); exit(); }
38 if ($_GET['act']=="phpinfo") { @phpinfo(); exit(); }
39 if ($_GET['act']=="PHPCredits") { @phpcredits(); exit(); }
40 if ($_GET['act']=="phpcredits") { @phpcredits(); exit(); }
41 */// Connect to mysql database
42 function ConnectMysql($sqlhost,$sqluser,$sqlpass,$sqldb,$retlink=false) {
43 if($retlink!==true) { $retlink = false; }
44 $StatSQL = @mysql_connect($sqlhost,$sqluser,$sqlpass);
45 $StatBase = @mysql_select_db($sqldb);
46 if (!$StatSQL) { return false; }
47 if (!$StatBase) { return false; }
48 if($retlink===true) { return $StatSQL; }
49 if($retlink===false) { return true; } }
50 $Names['RS'] = "Renee Sabonis";
51 define("_renee_", $Names['RS']);
52 // Change the title and gzip page
53 function change_title($new_title,$use_gzip="off",$gzip_type="gzip") {
55 if($gzip_type!="gzip") { if($gzip_type!="deflate") { $gzip_type = "gzip"; } }
56 $output = @ob_get_clean();
57 $output = preg_replace("/<title>(.*?)<\/title>/i", "<title>".$new_title."</title>", $output);
58 /* Change Some PHP Settings Fix the &PHPSESSID to &PHPSESSID */
59 $SessName = @session_name();
60 $output = preg_replace("/&PHPSESSID/", "&PHPSESSID", $output);
61 $qstrcode = htmlentities($Settings['qstr'], ENT_QUOTES, $Settings['charset']);
62 $output = str_replace($Settings['qstr'].$SessName, $qstrcode.$SessName, $output);
66 if($gzip_type=="gzip") {
67 $goutput = gzencode($output); }
68 if($gzip_type=="deflate") {
69 $goutput = gzcompress($output); }
71 // Fix amp => (&) to & and gzip page
72 function fix_amp($use_gzip="off",$gzip_type="gzip") {
74 if($gzip_type!="gzip") { if($gzip_type!="deflate") { $gzip_type = "gzip"; } }
75 $output = @ob_get_clean();
76 /* Change Some PHP Settings Fix the &PHPSESSID to &PHPSESSID */
77 $SessName = @session_name();
78 $output = preg_replace("/&PHPSESSID/", "&PHPSESSID", $output);
79 $qstrcode = htmlentities($Settings['qstr'], ENT_QUOTES, $Settings['charset']);
80 $output = str_replace($Settings['qstr'].$SessName, $qstrcode.$SessName, $output);
84 if($gzip_type=="gzip") {
85 $goutput = gzencode($output); }
86 if($gzip_type=="deflate") {
87 $goutput = gzcompress($output); }
89 $Names['RJ'] = "Kazuki Przyborowski";
90 define("_rene_", $Names['RJ']);
91 // GZip page for faster download
92 function gzip_page($use_gzip="off",$gzip_type="gzip") {
94 $output = @ob_get_clean();
95 if($gzip_type!="gzip") { if($gzip_type!="deflate") { $gzip_type = "gzip"; } }
99 if($gzip_type=="gzip") {
100 $goutput = gzencode($output); }
101 if($gzip_type=="deflate") {
102 $goutput = gzcompress($output); }
104 $foo="bar"; $$foo="foo";
105 // SafeSQL Lite Source Code by Cool Dude 2k
106 // Make SQL Query's safe
107 function query($query_string,$query_vars) {
108 $query_array = array(array("%i","%I","%F","%S"),array("%d","%d","%f","%s"));
109 $query_string = str_replace($query_array[0], $query_array[1], $query_string);
110 if (get_magic_quotes_gpc()) {
111 $query_vars = array_map("stripslashes", $query_vars); }
112 $query_vars = array_map("mysql_real_escape_string", $query_vars);
113 $query_val = $query_vars;
114 $query_num = count($query_val);
116 while ($query_i < $query_num) {
117 $query_is = $query_i+1;
118 $query_val[$query_is] = $query_vars[$query_i];
120 $query_val[0] = $query_string;
121 return call_user_func_array("sprintf",$query_val); }
122 $Names['KP'] = "Kazuki Przyborowski";
123 define("_kazuki_", $Names['KP']);
124 // Kill bad vars for some functions
125 function killbadvars($varname) {
126 $badphp1 = array('$'); $badphp2 = array(null);
127 $varname = str_replace($badphp1, $badphp2, $varname);
128 $varname = preg_replace("/(_SERVER|_ENV|_COOKIE|_SESSION)/i", null, $varname);
129 $varname = preg_replace("/(_GET|_POST|_FILES|_REQUEST|GLOBALS)/i", null, $varname);
130 $varname = preg_replace("/(HTTP_SERVER_VARS|HTTP_ENV_VARS)/i", null, $varname);
131 $varname = preg_replace("/(HTTP_COOKIE_VARS|HTTP_SESSION_VARS)/i", null, $varname);
132 $varname = preg_replace("/(HTTP_GET_VARS|HTTP_POST_VARS|HTTP_POST_FILES)/i", null, $varname);
134 // Change the text to icons(smileys)
135 function text2icons($Text,$sqlt) {
137 $reneequery=query("SELECT * FROM `".$sqlt."smileys`", array(null));
138 $reneeresult=mysql_query($reneequery);
139 $reneenum=mysql_num_rows($reneeresult);
141 while ($renees < $reneenum) {
142 $FileName=mysql_result($reneeresult,$renees,"FileName");
143 $SmileName=mysql_result($reneeresult,$renees,"SmileName");
144 $SmileText=mysql_result($reneeresult,$renees,"SmileText");
145 $SmileDirectory=mysql_result($reneeresult,$renees,"Directory");
146 $ShowSmile=mysql_result($reneeresult,$renees,"Show");
147 $ReplaceType=mysql_result($reneeresult,$renees,"ReplaceCI");
148 if($ReplaceType=="on") { $ReplaceType = "yes"; }
149 if($ReplaceType=="off") { $ReplaceType = "no"; }
150 if($ReplaceType!="yes"||$ReplaceType!="no") { $ReplaceType = "no"; }
151 $Smile1 = $SmileText;
152 $Smile2 = '<img src="'.$SmileDirectory.''.$FileName.'" style="vertical-align: middle; border: 0px;" title="'.$SmileName.'" alt="'.$SmileName.'" />';
153 if($ReplaceType=="no") {
154 $Text = str_replace($Smile1, $Smile2, $Text); }
155 if($ReplaceType=="yes") {
156 $Smile1 = preg_quote($SmileText,"/");
157 $Text = preg_replace("/".$Smile1."/i",$Smile2,$Text); }
158 ++$renees; } return $Text; }
159 // Removes the bad stuff
160 function remove_bad_entities($Text) {
161 //HTML Entities Dec Version
162 $Text = preg_replace("/‮/isU","",$Text);
163 $Text = preg_replace("/ /isU","",$Text);
164 $Text = preg_replace("/ /isU","",$Text);
165 $Text = preg_replace("/ /isU","",$Text);
166 $Text = preg_replace("/‌/isU","",$Text);
167 $Text = preg_replace("/‍/isU","",$Text);
168 $Text = preg_replace("/‎/isU","",$Text);
169 $Text = preg_replace("/‏/isU","",$Text);
170 //HTML Entities Hex Version
171 $Text = preg_replace("/‮/isU","",$Text);
172 $Text = preg_replace("/ /isU","",$Text);
173 $Text = preg_replace("/ /isU","",$Text);
174 $Text = preg_replace("/ /isU","",$Text);
175 $Text = preg_replace("/‌/isU","",$Text);
176 $Text = preg_replace("/‍/isU","",$Text);
177 $Text = preg_replace("/‎/isU","",$Text);
178 $Text = preg_replace("/‏/isU","",$Text);
179 //HTML Entities Name Version
180 $Text = preg_replace("/ /isU","",$Text);
181 $Text = preg_replace("/ /isU","",$Text);
182 $Text = preg_replace("/ /isU","",$Text);
183 $Text = preg_replace("/‌/isU","",$Text);
184 $Text = preg_replace("/‍/isU","",$Text);
185 $Text = preg_replace("/‎/isU","",$Text);
186 $Text = preg_replace("/‏/isU","",$Text);
188 // Remove the bad stuff
189 function remove_spaces($Text) {
190 $Text = preg_replace("/(^\t+|\t+$)/","",$Text);
191 $Text = preg_replace("/(^\n+|\n+$)/","",$Text);
192 $Text = preg_replace("/(^\r+|\r+$)/","",$Text);
193 $Text = preg_replace("/(\r|\n|\t)+/"," ",$Text);
194 $Text = preg_replace("/\s\s+/"," ",$Text);
195 $Text = preg_replace("/(^\s+|\s+$)/","",$Text);
196 $Text = @remove_bad_entities($Text);
199 function fixbamps($text) {
200 $fixamps1 = array("&copy;","&reg;","&trade;","&quot;","&amp;","&lt;","&gt;","&(a|e|i|o|u|y)acute;","&(a|e|i|o|u)grave;","&(a|e|i|o|u)circ;","&(a|e|i|o|u|y)uml;","&(a|o|n)tilde;","&aring;","&aelig;","&ccedil;","&eth;","&oslash;","&szlig;","&thorn;");
201 $fixamps2 = array("©","®","™",""","&","<",">","&\\1acute;","&\\1grave;","&\\1circ;","&\\1uml;","&\\1tilde;","å","æ","ç","ð","ø","ß","þ");
202 $ampnum = count($fixamps1); $ampi=0;
203 while ($ampi < $ampnum) {
204 $text = preg_replace("/".$fixamps1[$ampi]."/i", $fixamps2[$ampi], $text);
206 $text = preg_replace("/&#(x[a-f0-9]+|[0-9]+);/i", "&#$1;", $text);
208 // Get next id for stuff
209 function getnextid($tablepre,$table) {
210 $getnextidq = query("SHOW TABLE STATUS LIKE '".$tablepre.$table."'", array());
211 $getnextidr = mysql_query($getnextidq);
212 $getnextid = mysql_fetch_assoc($getnextidr);
213 return $getnextid['Auto_increment'];
214 @mysql_free_result($getnextidr); }
215 function getnumrows($tablepre,$table) {
216 $getnextidq = query("SHOW TABLE STATUS LIKE '".$tablepre.$table."'", array());
217 $getnextidr = mysql_query($getnextidq);
218 $getnextid = mysql_fetch_assoc($getnextidr);
219 return $getnextid['Rows'];
220 @mysql_free_result($getnextidr); }
221 $Names['K'] = "Katarzyna";
222 define("_katarzyna_", $Names['K']);
223 // Change Time Stamp to a readable time
224 function GMTimeChange($format,$timestamp,$offset,$minoffset=null,$dst=null) {
225 $TCHour = date("H",$timestamp);
226 $TCMinute = date("i",$timestamp);
227 $TCSecond = date("s",$timestamp);
228 $TCMonth = date("n",$timestamp);
229 $TCDay = date("d",$timestamp);
230 $TCYear = date("Y",$timestamp);
232 if(!is_numeric($minoffset)) { $minoffset = "00"; }
233 $ts_array = explode(":",$offset);
234 if(count($ts_array)!=2) {
235 if(!isset($ts_array[0])) { $ts_array[0] = "0"; }
236 if(!isset($ts_array[1])) { $ts_array[1] = "00"; }
237 $offset = $ts_array[0].":".$ts_array[1]; }
238 if(!is_numeric($ts_array[0])) { $ts_array[0] = "0"; }
239 if($ts_array[0]>12) { $ts_array[0] = "12"; $offset = $ts_array[0].":".$ts_array[1]; }
240 if($ts_array[0]<-12) { $ts_array[0] = "-12"; $offset = $ts_array[0].":".$ts_array[1]; }
241 if(!is_numeric($ts_array[1])) { $ts_array[1] = "00"; }
242 if($ts_array[1]>59) { $ts_array[1] = "59"; $offset = $ts_array[0].":".$ts_array[1]; }
243 if($ts_array[1]<0) { $ts_array[1] = "00"; $offset = $ts_array[0].":".$ts_array[1]; }
244 $tsa = array("offset" => $offset, "hour" => $ts_array[0], "minute" => $ts_array[1]);
245 //$tsa['minute'] = $tsa['minute'] + $minoffset;
246 if($dst!="on"&&$dst!="off") { $dst = "off"; }
247 if($dst=="on") { if($dstake!="done") {
248 $dstake = "done"; $tsa['hour'] = $tsa['hour']+1; } }
249 $TCHour = $TCHour + $tsa['hour'];
250 $TCMinute = $TCMinute + $tsa['minute'];
251 return date($format,mktime($TCHour,$TCMinute,$TCSecond,$TCMonth,$TCDay,$TCYear)); }
252 $Names['CK'] = "Christine";
253 define("_christine_", $Names['CK']);
254 // Change Time Stamp to a readable time
255 function TimeChange($format,$timestamp,$offset,$minoffset=null,$dst=null) {
256 return GMTimeChange($format,$timestamp,$offset,$minoffset,$dst); }
257 // Make a GMT Time Stamp
258 function GMTimeStamp() {
259 $GMTHour = gmdate("H");
260 $GMTMinute = gmdate("i");
261 $GMTSecond = gmdate("s");
262 $GMTMonth = gmdate("n");
263 $GMTDay = gmdate("d");
264 $GMTYear = gmdate("Y");
265 return mktime($GMTHour,$GMTMinute,$GMTSecond,$GMTMonth,$GMTDay,$GMTYear); }
266 function GMTimeStampS() { return time() - date('Z', time()); }
267 function GMTimeGet($format,$offset,$minoffset=null,$dst=null) {
268 return GMTimeChange($format,GMTimeStamp(),$offset,$minoffset,$dst); }
269 function GMTimeGetS($format,$offset,$minoffset=null,$dst=null) {
271 if(!is_numeric($offset)) { $offset = "0"; }
272 if(!is_numeric($minoffset)) { $minoffset = "00"; }
273 $ts_array = explode(":",$offset);
274 if(count($ts_array)!=2) {
275 if(!isset($ts_array[0])) { $ts_array[0] = "0"; }
276 if(!isset($ts_array[1])) { $ts_array[1] = "00"; }
277 $offset = $ts_array[0].":".$ts_array[1]; }
278 if(!is_numeric($ts_array[0])) { $ts_array[0] = "0"; }
279 if($ts_array[0]>12) { $ts_array[0] = "12"; $offset = $ts_array[0].":".$ts_array[1]; }
280 if($ts_array[0]<-12) { $ts_array[0] = "-12"; $offset = $ts_array[0].":".$ts_array[1]; }
281 if(!is_numeric($ts_array[1])) { $ts_array[1] = "00"; }
282 if($ts_array[1]>59) { $ts_array[1] = "59"; $offset = $ts_array[0].":".$ts_array[1]; }
283 if($ts_array[1]<0) { $ts_array[1] = "00"; $offset = $ts_array[0].":".$ts_array[1]; }
284 $tsa = array("offset" => $offset, "hour" => $ts_array[0], "minute" => $ts_array[1]);
285 //$tsa['minute'] = $tsa['minute'] + $minoffset;
286 if($dst!="on"&&$dst!="off") { $dst = "off"; }
287 if($dst=="on") { if($dstake!="done") {
288 $dstake = "done"; $tsa['hour'] = $tsa['hour']+1; } }
289 return date($format,mktime(gmdate('h')+$tsa['hour'],gmdate('i')+$tsa['minute'],gmdate('s'),gmdate('n'),gmdate('j'),gmdate('Y'))); }
291 function GetSeverZone() {
292 $TestHour1 = date("H");
293 @putenv("OTZ=".@getenv("TZ"));
295 $TestHour2 = date("H");
296 @putenv("TZ=".@getenv("OTZ"));
297 $TestHour3 = $TestHour1-$TestHour2;
299 function SeverOffSet() {
300 $TestHour1 = date("H");
301 $TestHour2 = gmdate("H");
302 $TestHour3 = $TestHour1-$TestHour2;
304 function SeverOffSetNew() {
305 return gmdate("g",mktime(0,date("Z"))); }
306 function gmtime() { return time() - (int) date('Z'); }
307 function file_get_source($filename,$return = FALSE) {
308 // Acts like highlight_file();
309 $phpsrc = file_get_contents($filename);
310 $phpsrcs = highlight_string($phpsrc,$return);
312 function valid_get_source($filename) {
313 $phpsrcs = file_get_source($filename,TRUE);
314 // Change font tag to span tag for valid xhtml
315 $phpsrcs = preg_replace("/\<font color=\"(.*?)\"\>/i", "<span style=\"color: \\1;\">", $phpsrcs);
316 $phpsrcs = preg_replace("/\<\/font>/i", "</span>", $phpsrcs);
318 function GetUserName($idu,$sqlt) {
319 $gunquery = query("SELECT * FROM `".$sqlt."members` WHERE `id`=%i", array($idu));
320 $gunresult=mysql_query($gunquery);
321 $gunnum=mysql_num_rows($gunresult);
323 $UsersName=mysql_result($gunresult,0,"Name"); }
324 @mysql_free_result($gunresult);
326 // hmac hash function
327 function hmac($data,$key,$hash='sha1',$blocksize=64) {
328 if (strlen($key)>$blocksize) {
329 $key=pack('H*',$hash($key)); }
330 $key=str_pad($key, $blocksize, chr(0x00));
331 $ipad=str_repeat(chr(0x36),$blocksize);
332 $opad=str_repeat(chr(0x5c),$blocksize);
333 return $hash(($key^$opad).pack('H*',$hash(($key^$ipad).$data))); }
334 // b64hmac hash function
335 function b64e_hmac($data,$key,$extdata,$hash='sha1',$blocksize=64) {
336 $extdata2 = hexdec($extdata); $key = $key.$extdata2;
337 return base64_encode(hmac($data,$key,$hash,$blocksize).$extdata); }
338 // salt hmac hash function
339 function salt_hmac($size1=4,$size2=6) {
340 $hprand = rand(4,6); $i = 0; $hpass = "";
341 while ($i < $hprand) {
342 $hspsrand = rand(1,2);
343 if($hspsrand!=1&&$hspsrand!=2) { $hspsrand=1; }
344 if($hspsrand==1) { $hpass .= chr(rand(48,57)); }
345 if($hspsrand==2) { $hpass .= chr(rand(65,70)); }
346 ++$i; } return $hpass; }
347 /* is_empty by M at http://us2.php.net/manual/en/function.empty.php#74093 */
348 function is_empty($var) {
349 if (((is_null($var) || rtrim($var) == "") &&
350 $var !== false) || (is_array($var) && empty($var))) {
351 return true; } else { return false; } }
352 // Hash two times with md5 and sha1
353 function PassHash2x($Text) {
357 // Hash two times with hmac-md5 and hmac-sha1
358 function PassHash2x2($data,$key,$extdata,$blocksize=64) {
359 $extdata2 = hexdec($extdata); $key = $key.$extdata2;
360 $Text = hmac($data,$key,"md5").$extdata;
361 $Text = hmac($Text,$key,"sha1").$extdata;
362 return base64_encode($Text); }
363 function cp($infile,$outfile,$mode="w") {
364 $contents = file_get_contents($infile);
365 $cpfp = fopen($outfile,$mode);
366 fwrite($cpfp, $contents);
369 /* str_ireplace for PHP below ver. 5 updated //
370 // by Kazuki Przyborowski - Cool Dude 2k //
371 // and upaded by Kazuki Przyborowski again */
372 if(!function_exists('str_ireplace')) {
373 function str_ireplace($search,$replace,$subject) {
374 if(!is_array($search)&&is_array($replace)) {
375 $search = array($search); }
376 if(is_array($search)&&!is_array($replace)) {
377 $replace = array($replace); }
378 if(is_array($search)&&is_array($replace)) {
379 $sc=count($search); $rc=count($replace); $sn=0;
380 if($sc!=$rc) { return false; }
382 $search[$sn] = preg_quote($search[$sn], "/");
383 $subject = preg_replace("/".$search[$sn]."/i", $replace[$sn], $subject);
385 if(!is_array($search)&&!is_array($replace)) {
386 $search = preg_quote($search, "/");
387 $subject = preg_replace("/".$search."/i", $replace, $subject); }
389 /* Adds mysql_set_charset to PHP below Ver. 5.2.3 - by: Janez R. //
390 // http://us.php.net/manual/en/function.mysql-set-charset.php#77565 */
391 if (!function_exists('mysql_set_charset')) {
392 function mysql_set_charset($charset) {
393 return mysql_query("set names $charset"); } }
394 /* Adds httponly to PHP below Ver. 5.2.0 //
395 // by Kazuki Przyborowski - Cool Dude 2k */
396 function http_set_cookie($name,$value=null,$expire=null,$path=null,$domain=null,$secure=false,$httponly=false) {
397 $mkcookie = null; $expireGMT = null;
398 if(!isset($name)) { echo "Error: You need to enter a name for cookie."; return false; }
399 if(!isset($expire)) { echo "Error: You need to enter a time for cookie to expire."; return false; }
400 $expireGMT = gmdate("D, d-M-Y H:i:s \G\M\T", $expire);
401 if(!isset($value)) { $value = null; }
402 if(!isset($httponly)||$httponly==false) {
403 setcookie($name, $value, $expire, $path, $domain, $secure); return true; }
404 if(version_compare(PHP_VERSION,"5.2.0",">=")&&$httponly==true) {
405 setcookie($name, $value, $expire, $path, $domain, $secure, $httponly); return true; }
406 if(version_compare(PHP_VERSION,"5.2.0","<")&&$httponly==true) {
407 $mkcookie = "Set-Cookie: ".rawurlencode($name)."=".rawurlencode($value);
408 $mkcookie = $mkcookie."; expires=".$expireGMT;
409 if(isset($path)&&$path!=null) { $mkcookie = $mkcookie."; path=".$path; }
410 if(isset($domain)&&$domain!=null) { $mkcookie = $mkcookie."; domain=".$domain; }
411 if(isset($secure)&&$secure===true) { $mkcookie = $mkcookie."; secure"; }
412 if(isset($httponly)&&$httponly===true) { $mkcookie = $mkcookie."; HttpOnly"; }
413 header($mkcookie, false); return true; } }
414 $foobar="fubar"; $$foobar="foobar";
416 function dump_included_files() { return var_dump(get_included_files()); }
417 function count_included_files() { return count(get_included_files()); }
418 function dump_extensions() { return var_dump(get_loaded_extensions()); }
419 function count_extensions() { return count(get_loaded_extensions()); }