OSDN Git Service

Added new function xml_tag_make and modify function xml_doc_start a little.
[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-2007 Cool Dude 2k - http://intdb.sourceforge.net/
12     Copyright 2004-2007 Game Maker 2k - http://upload.idb.s1.jcink.com/
13
14     $FileInfo: functions.php - Last Update: 07/02/2007 SVN 36 - Author: cooldude2k $
15 */
16 $File3Name = basename($_SERVER['SCRIPT_NAME']);
17 if ($File3Name=="functions.php"||$File3Name=="/functions.php") {
18         require('index.php');
19         exit(); }
20 function CheckFile($FileName) {
21 $File3Name = basename($_SERVER['SCRIPT_NAME']);
22 if ($File3Name==$FileName||$File3Name=="/".$FileName) {
23         require('index.php');
24         exit(); }
25 return null; }
26 function CheckFiles($FileName) {
27 $File3Name = basename($_SERVER['SCRIPT_NAME']);
28 if ($File3Name==$FileName||$File3Name=="/".$FileName) {
29         return true; } }
30 CheckFile("functions.php");
31 require($SettDir['misc']."compression.php");
32 if ($_GET['act']=="DeleteSession") { @session_destroy(); }
33 if ($_GET['act']=="ResetSession") { @session_unset(); }
34 if ($_GET['act']=="NewSessionID") { @session_regenerate_id(); }
35 if ($_GET['act']=="PHPInfo") { @phpinfo(); exit(); }
36 if ($_GET['act']=="phpinfo") { @phpinfo(); exit(); }
37 if ($_GET['act']=="PHPCredits") { @phpcredits(); exit(); }
38 if ($_GET['act']=="phpcredits") { @phpcredits(); exit(); }
39 function ConnectMysql($sqlhost,$sqluser,$sqlpass,$sqldb) {
40 $StatSQL = @mysql_connect($sqlhost,$sqluser,$sqlpass);
41 $StatBase = @mysql_select_db($sqldb);
42 if (!$StatSQL) { return false; }
43 if (!$StatBase) { return false; }
44 return true; }
45         $Names['RS'] = "Renee Sabonis";
46 define("_renee_", $Names['RS']);
47 function change_title($new_title,$use_gzip="off",$gzip_type="gzip") {
48 global $Settings;
49 if($gzip_type!="gzip") { if($gzip_type!="deflate") { $gzip_type = "gzip"; } }
50 $output = @ob_get_clean();
51 $output = preg_replace("/<title>(.*?)<\/title>/i", "<title>".$new_title."</title>", $output);
52 /* Change Some PHP Settings Fix the &PHPSESSID to &amp;PHPSESSID */
53 $SessName = @session_name();
54 $output = preg_replace("/&PHPSESSID/", "&amp;PHPSESSID", $output);
55 $qstrcode = htmlentities($Settings['qstr']);
56 $output = str_replace($Settings['qstr'].$SessName, $qstrcode.$SessName, $output);
57 if($use_gzip!="on") {
58         echo $output; }
59 if($use_gzip=="on") {
60         if($gzip_type=="gzip") {
61         $goutput = gzencode($output); }
62         if($gzip_type=="deflate") {
63         $goutput = gzcompress($output); }
64         echo $goutput; } }
65 function fix_amp($use_gzip="off",$gzip_type="gzip") {
66 global $Settings;
67 if($gzip_type!="gzip") { if($gzip_type!="deflate") { $gzip_type = "gzip"; } }
68 $output = @ob_get_clean();
69 /* Change Some PHP Settings Fix the &PHPSESSID to &amp;PHPSESSID */
70 $SessName = @session_name();
71 $output = preg_replace("/&PHPSESSID/", "&amp;PHPSESSID", $output);
72 $qstrcode = htmlentities($Settings['qstr']);
73 $output = str_replace($Settings['qstr'].$SessName, $qstrcode.$SessName, $output);
74 if($use_gzip!="on") {
75         echo $output; }
76 if($use_gzip=="on") {
77         if($gzip_type=="gzip") {
78         $goutput = gzencode($output); }
79         if($gzip_type=="deflate") {
80         $goutput = gzcompress($output); }
81         echo $goutput; } }
82 $output = @ob_get_clean();
83         $Names['RJ'] = "René Johnson";
84 define("_rene_", $Names['RJ']);
85 function gzip_page($use_gzip="off",$gzip_type="gzip") {
86 global $Settings;
87 if($gzip_type!="gzip") { if($gzip_type!="deflate") { $gzip_type = "gzip"; } }
88 if($use_gzip!="on") {
89         echo $output; }
90 if($use_gzip=="on") {
91         if($gzip_type=="gzip") {
92         $goutput = gzencode($output); }
93         if($gzip_type=="deflate") {
94         $goutput = gzcompress($output); }
95         echo $goutput; } }
96 $foo="bar"; $$foo="foo";
97 // SafeSQL Lite Source Code by Cool Dude 2k
98 function query($query_string,$query_vars) {
99    $query_array = array(array("%i","%I","%F","%S"),array("%d","%d","%f","%s"));
100    $query_string = str_replace($query_array[0], $query_array[1], $query_string);
101    if (get_magic_quotes_gpc()) {
102        $query_vars  = array_map("stripslashes", $query_vars); }
103    $query_vars = array_map("mysql_real_escape_string", $query_vars);
104    $query_val = $query_vars;
105 $query_num = count($query_val);
106 $query_i = 0;
107 while ($query_i < $query_num) {
108 $query_is = $query_i+1;
109 $query_val[$query_is] = $query_vars[$query_i];
110 ++$query_i; }
111    $query_val[0] = $query_string;
112    return call_user_func_array("sprintf",$query_val); }
113 function killbadvars($varname) {
114 $badphp1 = array('$'); $badphp2 = array(null);
115 $varname = str_replace($badphp1, $badphp2, $varname);
116 $varname = preg_replace("/(_SERVER|_ENV|_COOKIE|_SESSION)/i", null, $varname);
117 $varname = preg_replace("/(_GET|_POST|_FILES|_REQUEST|GLOBALS)/i", null, $varname);
118 $varname = preg_replace("/(HTTP_SERVER_VARS|HTTP_ENV_VARS)/i", null, $varname);
119 $varname = preg_replace("/(HTTP_COOKIE_VARS|HTTP_SESSION_VARS)/i", null, $varname);
120 $varname = preg_replace("/(HTTP_GET_VARS|HTTP_POST_VARS|HTTP_POST_FILES)/i", null, $varname);
121         return $varname; }
122 function text2icons($Text,$sqlt) {
123 global $Settings;
124 $reneequery="SELECT * FROM ".$sqlt."smileys";
125 $reneeresult=mysql_query($reneequery);
126 $reneenum=mysql_num_rows($reneeresult);
127 $renees=0;
128 while ($renees < $reneenum) {
129 $FileName=mysql_result($reneeresult,$renees,"FileName");
130 $SmileName=mysql_result($reneeresult,$renees,"SmileName");
131 $SmileText=mysql_result($reneeresult,$renees,"SmileText");
132 $SmileDirectory=mysql_result($reneeresult,$renees,"Directory");
133 $ShowSmile=mysql_result($reneeresult,$renees,"Show");
134 $ReplaceType=mysql_result($reneeresult,$renees,"ReplaceCI");
135 if($ReplaceType=="on") { $ReplaceType = "yes"; }
136 if($ReplaceType=="off") { $ReplaceType = "no"; }
137 if($ReplaceType!="yes"||$ReplaceType!="no") { $ReplaceType = "no"; }
138 $Smile1 = $SmileText;
139 $Smile2 = '<img src="'.$SmileDirectory.''.$FileName.'" style="vertical-align: middle; border: 0px;" title="'.$SmileName.'" alt="'.$SmileName.'" />';
140 if($ReplaceType=="no") {
141 $Text = str_replace($Smile1, $Smile2, $Text); }
142 if($ReplaceType=="yes") {
143         $Smile1 = preg_quote($SmileText,"/");
144 $Text = preg_replace("/".$Smile1."/i",$Smile2,$Text); }
145 ++$renees; } return $Text; }
146 function remove_spaces($Text) {
147 $Text = preg_replace("/(^\t+|\t+$)/","",$Text);
148 $Text = preg_replace("/(^\n+|\n+$)/","",$Text);
149 $Text = preg_replace("/(^\r+|\r+$)/","",$Text);
150 $Text = preg_replace("/(\r|\n|\t)+/"," ",$Text);
151 $Text = preg_replace("/\s\s+/"," ",$Text);
152 $Text = preg_replace("/(^\s+|\s+$)/","",$Text);
153 return $Text; }
154 function fixbamps($text) {
155 $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;");
156 $fixamps2 = array("&copy;","&reg;","&trade;","&quot;","&amp;","&lt;","&gt;","&\\1acute;","&\\1grave;","&\\1circ;","&\\1uml;","&\\1tilde;","&aring;","&aelig;","&ccedil;","&eth;","&oslash;","&szlig;","&thorn;");
157 $ampnum = count($fixamps1); $ampi=0;
158 while ($ampi < $ampnum) {
159 $text = preg_replace("/".$fixamps1[$ampi]."/i", $fixamps2[$ampi], $text);
160 ++$ampi; }
161 $text = preg_replace("/&amp;#(x[a-f0-9]+|[0-9]+);/i", "&#$1;", $text);
162 return $text; }
163 function getnextid($tablepre,$table) {
164    $getnextidq = query("SHOW TABLE STATUS LIKE '".$tablepre.$table."'", array());
165    $getnextidr = mysql_query($getnextidq);
166    $getnextid = mysql_fetch_assoc($getnextidr);
167    return $getnextid['Auto_increment'];
168    @mysql_free_result($getnextidr); }
169         $Names['RSA'] = "Rachel Sabonis";
170 define("_rachel_", $Names['RSA']);
171 function redirects($type,$url,$time=0) {
172 if($type!="location"&&
173         $type!="refresh") {
174         $type=="location"; }
175 if($type=="refresh") {
176 header("Refresh: ".$time."; URL=".$url); }
177 if($type=="location") {
178 header("Location: ".$url); }
179 return true; }
180 function xml_tag_make($type,$attbs,$retval=false) {
181         $renee1 = explode("&",$attbs);
182         $reneenum=count($renee1);
183         $reneei=0; $attblist = null;
184         while ($reneei < $reneenum) {
185         $renee2 = explode("=",$renee1[$reneei]);
186         if($renee2[0]!=null||$renee2[1]!=null) {
187         $attblist = $attblist.' '.$renee2[0].'="'.$renee2[1].'"'; }
188         ++$reneei; }
189         if($retval!=false&&$retval!=true) { $retval=false; }
190         if($retval==false) {
191         echo '<?'.$type.$attblist.'?>'."\n"; }
192         if($retval==true) {
193         return '<?'.$type.$attblist.'?>'."\n"; } }
194 function xml_doc_start($ver,$encode,$retval=false) {
195         if($retval==false) {
196         echo xml_tag_make('xml','version='.$ver.'&encoding='.$encode,true); }
197         if($retval==true) {
198         return xml_tag_make('xml','version='.$ver.'&encoding='.$encode,true); } }
199 function GMTimeChange($format,$timestamp,$offset,$minoffset=null,$dst=null) {
200 $TCHour = date("H",$timestamp);
201 $TCMinute = date("i",$timestamp);
202 $TCSecond = date("s",$timestamp);
203 $TCMonth = date("n",$timestamp);
204 $TCDay = date("d",$timestamp);
205 $TCYear = date("Y",$timestamp);
206 unset($dstake); $dstake = null;
207 if(!is_numeric($minoffset)) { $minoffset = "00"; }
208 $ts_array = explode(":",$offset);
209 if(count($ts_array)!=2) {
210         if(!isset($ts_array[0])) { $ts_array[0] = "0"; }
211         if(!isset($ts_array[1])) { $ts_array[1] = "00"; }
212         $offset = $ts_array[0].":".$ts_array[1]; }
213 if(!is_numeric($ts_array[0])) { $ts_array[0] = "0"; }
214 if($ts_array[0]>12) { $ts_array[0] = "12"; $offset = $ts_array[0].":".$ts_array[1]; }
215 if($ts_array[0]<-12) { $ts_array[0] = "-12"; $offset = $ts_array[0].":".$ts_array[1]; }
216 if(!is_numeric($ts_array[1])) { $ts_array[1] = "00"; }
217 if($ts_array[1]>59) { $ts_array[1] = "59"; $offset = $ts_array[0].":".$ts_array[1]; }
218 if($ts_array[1]<0) { $ts_array[1] = "00"; $offset = $ts_array[0].":".$ts_array[1]; }
219 $tsa = array("offset" => $offset, "hour" => $ts_array[0], "minute" => $ts_array[1]);
220 //$tsa['minute'] = $tsa['minute'] + $minoffset;
221 if($dst!="on"&&$dst!="off") { $dst = "off"; }
222 if($dst=="on") { if($dstake!="done") { 
223         $dstake = "done"; $tsa['hour'] = $tsa['hour']+1; } }
224 $TCHour = $TCHour + $tsa['hour'];
225 $TCMinute = $TCMinute + $tsa['minute'];
226 return date($format,mktime($TCHour,$TCMinute,$TCSecond,$TCMonth,$TCDay,$TCYear)); }
227         $Names['CK'] = "Christine";
228 define("_christine_", $Names['CK']);
229 function TimeChange($format,$timestamp,$offset,$minoffset=null,$dst=null) {
230 return GMTimeChange($format,$timestamp,$offset,$minoffset,$dst); }
231 function GMTimeStamp() {
232 $GMTHour = gmdate("H");
233 $GMTMinute = gmdate("i");
234 $GMTSecond = gmdate("s");
235 $GMTMonth = gmdate("n");
236 $GMTDay = gmdate("d");
237 $GMTYear = gmdate("Y");
238 return mktime($GMTHour,$GMTMinute,$GMTSecond,$GMTMonth,$GMTDay,$GMTYear); }
239 function GMTimeStampS() { return time() - date('Z', time()); }
240 function GMTimeGet($format,$offset,$minoffset=null,$dst=null) { 
241         return GMTimeChange($format,GMTimeStamp(),$offset,$minoffset,$dst); }
242 function GMTimeGetS($format,$offset,$minoffset=null,$dst=null) {
243 unset($dstake); $dstake = null;
244 if(!is_numeric($offset)) { $offset = "0"; }
245 if(!is_numeric($minoffset)) { $minoffset = "00"; }
246 $ts_array = explode(":",$offset);
247 if(count($ts_array)!=2) {
248         if(!isset($ts_array[0])) { $ts_array[0] = "0"; }
249         if(!isset($ts_array[1])) { $ts_array[1] = "00"; }
250         $offset = $ts_array[0].":".$ts_array[1]; }
251 if(!is_numeric($ts_array[0])) { $ts_array[0] = "0"; }
252 if($ts_array[0]>12) { $ts_array[0] = "12"; $offset = $ts_array[0].":".$ts_array[1]; }
253 if($ts_array[0]<-12) { $ts_array[0] = "-12"; $offset = $ts_array[0].":".$ts_array[1]; }
254 if(!is_numeric($ts_array[1])) { $ts_array[1] = "00"; }
255 if($ts_array[1]>59) { $ts_array[1] = "59"; $offset = $ts_array[0].":".$ts_array[1]; }
256 if($ts_array[1]<0) { $ts_array[1] = "00"; $offset = $ts_array[0].":".$ts_array[1]; }
257 $tsa = array("offset" => $offset, "hour" => $ts_array[0], "minute" => $ts_array[1]);
258 //$tsa['minute'] = $tsa['minute'] + $minoffset;
259 if($dst!="on"&&$dst!="off") { $dst = "off"; }
260 if($dst=="on") { if($dstake!="done") { 
261         $dstake = "done"; $tsa['hour'] = $tsa['hour']+1; } }
262 return date($format,mktime(gmdate('h')+$tsa['hour'],gmdate('i')+$tsa['minute'],gmdate('s'),gmdate('n'),gmdate('j'),gmdate('Y'))); }
263 function GetSeverZone() {
264 $TestHour1 = date("H");
265 @putenv("OTZ=".@getenv("TZ"));
266 @putenv("TZ=GMT");
267 $TestHour2 = date("H");
268 @putenv("TZ=".@getenv("OTZ"));
269 $TestHour3 = $TestHour1-$TestHour2;
270 return $TestHour3; }
271 function SeverOffSet() {
272 $TestHour1 = date("H");
273 $TestHour2 = gmdate("H");
274 $TestHour3 = $TestHour1-$TestHour2;
275 return $TestHour3; }
276 function SeverOffSetNew() {
277 return gmdate("g",mktime(0,date("Z"))); }
278 function gmtime() { return time() - (int) date('Z'); }
279 function file_get_source($filename,$return = FALSE) {
280 // Acts like highlight_file();
281 $phpsrc = file_get_contents($filename);
282 $phpsrcs = highlight_string($phpsrc,$return);
283 return $phpsrcs; }
284 function valid_get_source($filename) {
285 $phpsrcs = file_get_source($filename,TRUE);
286 // Change font tag to span tag for valid xhtml
287 $phpsrcs = preg_replace("/\<font color=\"(.*?)\"\>/i", "<span style=\"color: \\1;\">", $phpsrcs);
288 $phpsrcs = preg_replace("/\<\/font>/i", "</span>", $phpsrcs);
289 return $phpsrcs; }
290 function GetUserName($idu,$sqlt) {
291 $gunquery = query("select * from ".$sqlt."members where `id`=%i", array($idu));
292 $gunresult=mysql_query($gunquery);
293 $gunnum=mysql_num_rows($gunresult);
294 if($gunnum>0){
295 $UsersName=mysql_result($gunresult,$gunnum-1,"Name"); }
296 @mysql_free_result($gunresult);
297 return $UsersName; }
298 function hmac($data,$key,$hash='sha1',$blocksize=64) {
299   if (strlen($key)>$blocksize) {
300   $key=pack('H*',$hash($key)); }
301   $key=str_pad($key, $blocksize, chr(0x00));
302   $ipad=str_repeat(chr(0x36),$blocksize);
303   $opad=str_repeat(chr(0x5c),$blocksize);
304   return $hash(($key^$opad).pack('H*',$hash(($key^$ipad).$data))); }
305 function b64e_hmac($data,$key,$extdata,$hash='sha1',$blocksize=64) {
306         $extdata2 = hexdec($extdata); $key = $key.$extdata2;
307   return base64_encode(hmac($data,$key,$hash,$blocksize).$extdata); }
308 function salt_hmac($size1=4,$size2=6) {
309 $hprand = rand(4,6); $i = 0; $hpass = "";
310 while ($i < $hprand) {
311 $hspsrand = rand(1,2);
312 if($hspsrand!=1&&$hspsrand!=2) { $hspsrand=1; }
313 if($hspsrand==1) { $hpass .= chr(rand(48,57)); }
314 if($hspsrand==2) { $hpass .= chr(rand(65,70)); }
315 ++$i; } return $hpass; }
316 /* is_empty by M at http://us2.php.net/manual/en/function.empty.php#74093 */
317 function is_empty($var) {
318     if (((is_null($var) || rtrim($var) == "") &&
319                 $var !== false) || (is_array($var) && empty($var))) {
320         return true; } else {
321         return false; } }
322 function PassHash2x($Text) {
323 $Text = md5($Text);
324 $Text = sha1($Text);
325 return $Text; }
326 function PassHash2x2($data,$key,$extdata,$blocksize=64) {
327 $extdata2 = hexdec($extdata); $key = $key.$extdata2;
328 $Text = hmac($data,$key,"md5").$extdata; 
329 $Text = hmac($Text,$key,"sha1").$extdata;
330 return base64_encode($Text); }
331 function cp($infile,$outfile,$mode="w") { 
332    $contents = file_get_contents($infile);
333    $cpfp = fopen($outfile,$mode);
334    fwrite($cpfp, $contents);
335    fclose($cpfp);
336    return true; }
337 /* str_ireplace for PHP below ver. 5 updated // 
338 //       by René Johnson - Cool Dude 2k      //
339 //      and upaded by René Johnson again     */
340 if(!function_exists('str_ireplace')) {
341 function str_ireplace($search,$replace,$subject) {
342 if(!is_array($search)&&is_array($replace)) {
343         $search = array($search); }
344 if(is_array($search)&&!is_array($replace)) {
345         $replace = array($replace); }
346 if(is_array($search)&&is_array($replace)) {
347         $sc=count($search); $rc=count($replace); $sn=0;
348         if($sc!=$rc) { return false; }
349 while ($sc > $sn) {
350         $search[$sn] = preg_quote($search[$sn], "/");
351         $subject = preg_replace("/".$search[$sn]."/i", $replace[$sn], $subject);
352         ++$sn; } }
353 if(!is_array($search)&&!is_array($replace)) {
354 $search = preg_quote($search, "/");
355 $subject = preg_replace("/".$search."/i", $replace, $subject); }
356 return $subject; } }
357 $foobar="fubar"; $$foobar="foobar";
358 function dump_included_files() {        return var_dump(get_included_files()); }
359 function count_included_files() {       return count(get_included_files()); }
360 function dump_extensions() {    return var_dump(get_loaded_extensions()); }
361 function count_extensions() {   return count(get_loaded_extensions()); }
362 ?>