OSDN Git Service

Moved libraries from root to lib/
[pukiwiki/pukiwiki.git] / lib / func.php
1 <?php
2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
4 //
5 // $Id: func.php,v 1.1 2004/08/01 01:54:35 henoheno Exp $
6 //
7
8 // Ê¸»úÎó¤¬InterWikiName¤«¤É¤¦¤«
9 function is_interwiki($str)
10 {
11         global $InterWikiName;
12
13         return preg_match("/^$InterWikiName$/",$str);
14 }
15
16 // Ê¸»úÎ󤬥ڡ¼¥¸Ì¾¤«¤É¤¦¤«
17 function is_pagename($str)
18 {
19         global $BracketName,$WikiName;
20
21         $is_pagename = (!is_interwiki($str) and preg_match("/^(?!\/)$BracketName$(?<!\/$)/",$str)
22                 and !preg_match('/(^|\/)\.{1,2}(\/|$)/',$str));
23
24         if (defined('SOURCE_ENCODING'))
25         {
26                 if (SOURCE_ENCODING == 'UTF-8')
27                 {
28                         $is_pagename = ($is_pagename and preg_match('/^(?:[\x00-\x7F]|(?:[\xC0-\xDF][\x80-\xBF])|(?:[\xE0-\xEF][\x80-\xBF][\x80-\xBF]))+$/',$str)); // UTF-8
29                 }
30                 else if (SOURCE_ENCODING == 'EUC-JP')
31                 {
32                         $is_pagename = ($is_pagename and preg_match('/^(?:[\x00-\x7F]|(?:[\x8E\xA1-\xFE][\xA1-\xFE])|(?:\x8F[\xA1-\xFE][\xA1-\xFE]))+$/',$str)); // EUC-JP
33                 }
34         }
35
36         return $is_pagename;
37 }
38
39 // Ê¸»úÎó¤¬URL¤«¤É¤¦¤«
40 function is_url($str,$only_http=FALSE)
41 {
42         $scheme = $only_http ? 'https?' : 'https?|ftp|news';
43         return preg_match('/^('.$scheme.')(:\/\/[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]*)$/', $str);
44 }
45
46 // ¥Ú¡¼¥¸¤¬Â¸ºß¤¹¤ë¤«
47 function is_page($page,$reload=FALSE)
48 {
49         return file_exists(get_filename($page));
50 }
51
52 // ¥Ú¡¼¥¸¤¬ÊÔ½¸²Äǽ¤«
53 function is_editable($page)
54 {
55         global $cantedit;
56         static $is_editable = array();
57
58         if (!array_key_exists($page,$is_editable))
59         {
60                 $is_editable[$page] = (
61                         is_pagename($page) and
62                         !is_freeze($page) and
63                         !in_array($page,$cantedit)
64                 );
65         }
66
67         return $is_editable[$page];
68 }
69
70 // ¥Ú¡¼¥¸¤¬Åà·ë¤µ¤ì¤Æ¤¤¤ë¤«
71 function is_freeze($page)
72 {
73         global $function_freeze;
74
75         if (!$function_freeze or !is_page($page))
76         {
77                 return FALSE;
78         }
79
80         list($lines) = get_source($page);
81         return (rtrim($lines) == '#freeze');
82 }
83
84 // ¼«Æ°¥Æ¥ó¥×¥ì¡¼¥È
85 function auto_template($page)
86 {
87         global $auto_template_func,$auto_template_rules;
88
89         if (!$auto_template_func)
90         {
91                 return '';
92         }
93
94         $body = '';
95         foreach ($auto_template_rules as $rule => $template)
96         {
97                 if (preg_match("/$rule/",$page,$matches))
98                 {
99                         $template_page = preg_replace("/$rule/",$template,$page);
100                         if (is_page($template_page))
101                         {
102                                 $body = join('',get_source($template_page));
103                                 // ¸«½Ð¤·¤Î¸ÇÍ­IDÉô¤òºï½ü
104                                 $body = preg_replace('/^(\*{1,3}.*)\[#[A-Za-z][\w-]+\](.*)$/m','$1$2',$body);
105                                 // #freeze¤òºï½ü
106                                 $body = preg_replace('/^#freeze\s*$/m','',$body);
107
108                                 for ($i = 0; $i < count($matches); $i++)
109                                 {
110                                         $body = str_replace("\$$i",$matches[$i],$body);
111                                 }
112                                 break;
113                         }
114                 }
115         }
116         return $body;
117 }
118
119 // ¸¡º÷¸ì¤òŸ³«¤¹¤ë
120 function get_search_words($words,$special=FALSE)
121 {
122         $retval = array();
123         // Perl¥á¥â - Àµ¤·¤¯¥Ñ¥¿¡¼¥ó¥Þ¥Ã¥Á¤µ¤»¤ë
124         // http://www.din.or.jp/~ohzaki/perl.htm#JP_Match
125         $eucpre = $eucpost = '';
126         if (SOURCE_ENCODING == 'EUC-JP')
127         {
128                 $eucpre = '(?<!\x8F)';
129                 // # JIS X 0208 ¤¬ 0ʸ»ú°Ê¾å³¤¤¤Æ # ASCII, SS2, SS3 ¤Þ¤¿¤Ï½ªÃ¼
130                 $eucpost = '(?=(?:[\xA1-\xFE][\xA1-\xFE])*(?:[\x00-\x7F\x8E\x8F]|\z))';
131         }
132         $quote_func = create_function('$str','return preg_quote($str,"/");');
133         // LANG=='ja'¤Ç¡¢mb_convert_kana¤¬»È¤¨¤ë¾ì¹ç¤Ïmb_convert_kana¤ò»ÈÍÑ
134         $convert_kana = create_function('$str,$option',
135                 (LANG == 'ja' and function_exists('mb_convert_kana')) ?
136                         'return mb_convert_kana($str,$option);' : 'return $str;'
137         );
138
139         foreach ($words as $word)
140         {
141                 // ±Ñ¿ô»ú¤ÏȾ³Ñ,¥«¥¿¥«¥Ê¤ÏÁ´³Ñ,¤Ò¤é¤¬¤Ê¤Ï¥«¥¿¥«¥Ê¤Ë
142                 $word_zk = $convert_kana($word,'aKCV');
143                 $chars = array();
144                 for ($pos = 0; $pos < mb_strlen($word_zk);$pos++)
145                 {
146                         $char = mb_substr($word_zk,$pos,1);
147                         // $special : htmlspecialchars()¤òÄ̤¹¤«
148                         $arr = array($quote_func($special ? htmlspecialchars($char) : $char));
149                         if (strlen($char) == 1) // ±Ñ¿ô»ú
150                         {
151                                 foreach (array(strtoupper($char),strtolower($char)) as $_char)
152                                 {
153                                         if ($char != '&')
154                                         {
155                                                 $arr[] = $quote_func($_char);
156                                         }
157                                         $ord = ord($_char);
158                                         $arr[] = sprintf('&#(?:%d|x%x);',$ord,$ord); // ¼ÂÂλ²¾È
159                                         $arr[] = $quote_func($convert_kana($_char,"A")); // Á´³Ñ
160                                 }
161                         }
162                         else // ¥Þ¥ë¥Á¥Ð¥¤¥Èʸ»ú
163                         {
164                                 $arr[] = $quote_func($convert_kana($char,"c")); // ¤Ò¤é¤¬¤Ê
165                                 $arr[] = $quote_func($convert_kana($char,"k")); // È¾³Ñ¥«¥¿¥«¥Ê
166                         }
167                         $chars[] = '(?:'.join('|',array_unique($arr)).')';
168                 }
169                 $retval[$word] = $eucpre.join('',$chars).$eucpost;
170         }
171         return $retval;
172 }
173
174 // ¸¡º÷
175 function do_search($word,$type='AND',$non_format=FALSE)
176 {
177         global $script,$vars,$whatsnew,$non_list,$search_non_list;
178         global $_msg_andresult,$_msg_orresult,$_msg_notfoundresult;
179         global $search_auth;
180
181         $database = array();
182         $retval = array();
183
184         $b_type = ($type == 'AND'); // AND:TRUE OR:FALSE
185         $keys = get_search_words(preg_split('/\s+/',$word,-1,PREG_SPLIT_NO_EMPTY));
186
187         $_pages = get_existpages();
188         $pages = array();
189
190         foreach ($_pages as $page)
191         {
192                 if ($page == $whatsnew
193                         or (!$search_non_list and preg_match("/$non_list/",$page)))
194                 {
195                         continue;
196                 }
197
198                 // ¸¡º÷Âоݥڡ¼¥¸¤ÎÀ©¸Â¤ò¤«¤±¤ë¤«¤É¤¦¤« (¥Ú¡¼¥¸Ì¾¤ÏÀ©¸Â³°)
199                 if ($search_auth and !check_readable($page,false,false)) {
200                         $source = get_source(); // ¸¡º÷Âоݥڡ¼¥¸ÆâÍƤò¶õ¤Ë¡£
201                 } else {
202                         $source = get_source($page);
203                 }
204                 if (!$non_format)
205                 {
206                         array_unshift($source,$page); // ¥Ú¡¼¥¸Ì¾¤â¸¡º÷ÂоݤË
207                 }
208
209                 $b_match = FALSE;
210                 foreach ($keys as $key)
211                 {
212                         $tmp = preg_grep("/$key/",$source);
213                         $b_match = (count($tmp) > 0);
214                         if ($b_match xor $b_type)
215                         {
216                                 break;
217                         }
218                 }
219                 if ($b_match)
220                 {
221                         $pages[$page] = get_filetime($page);
222                 }
223         }
224         if ($non_format)
225         {
226                 return array_keys($pages);
227         }
228         $r_word = rawurlencode($word);
229         $s_word = htmlspecialchars($word);
230         if (count($pages) == 0)
231         {
232                 return str_replace('$1',$s_word,$_msg_notfoundresult);
233         }
234         ksort($pages);
235         $retval = "<ul>\n";
236         foreach ($pages as $page=>$time)
237         {
238                 $r_page = rawurlencode($page);
239                 $s_page = htmlspecialchars($page);
240                 $passage = get_passage($time);
241                 $retval .= " <li><a href=\"$script?cmd=read&amp;page=$r_page&amp;word=$r_word\">$s_page</a>$passage</li>\n";
242         }
243         $retval .= "</ul>\n";
244
245         $retval .= str_replace('$1',$s_word,str_replace('$2',count($pages),
246                 str_replace('$3',count($_pages),$b_type ? $_msg_andresult : $_msg_orresult)));
247
248         return $retval;
249 }
250
251 // ¥×¥í¥°¥é¥à¤Ø¤Î°ú¿ô¤Î¥Á¥§¥Ã¥¯
252 function arg_check($str)
253 {
254         global $vars;
255
256         return array_key_exists('cmd',$vars) and (strpos($vars['cmd'],$str) === 0);
257 }
258
259 // ¥Ú¡¼¥¸Ì¾¤Î¥¨¥ó¥³¡¼¥É
260 function encode($key)
261 {
262         return ($key == '') ? '' : strtoupper(join('',unpack('H*0',$key)));
263 }
264
265 // ¥Ú¡¼¥¸Ì¾¤Î¥Ç¥³¡¼¥É
266 function decode($key)
267 {
268         return ($key == '') ? '' : substr(pack('H*','20202020'.$key),4);
269 }
270
271 // [[ ]] ¤ò¼è¤ê½ü¤¯
272 function strip_bracket($str)
273 {
274         if (preg_match('/^\[\[(.*)\]\]$/',$str,$match))
275         {
276                 $str = $match[1];
277         }
278         return $str;
279 }
280
281 // ¥Ú¡¼¥¸°ìÍ÷¤ÎºîÀ®
282 function page_list($pages, $cmd = 'read', $withfilename=FALSE)
283 {
284         global $script,$list_index,$top;
285         global $_msg_symbol,$_msg_other;
286         global $pagereading_enable;
287
288         // ¥½¡¼¥È¥­¡¼¤ò·èÄꤹ¤ë¡£ ' ' < '[a-zA-Z]' < 'zz'¤È¤¤¤¦Á°Äó¡£
289         $symbol = ' ';
290         $other = 'zz';
291
292         $retval = '';
293
294         if($pagereading_enable) {
295                 mb_regex_encoding(SOURCE_ENCODING);
296                 $readings = get_readings($pages);
297         }
298
299         $list = array();
300         foreach($pages as $file=>$page)
301         {
302                 $r_page = rawurlencode($page);
303                 $s_page = htmlspecialchars($page,ENT_QUOTES);
304                 $passage = get_pg_passage($page);
305
306                 $str = "   <li><a href=\"$script?cmd=$cmd&amp;page=$r_page\">$s_page</a>$passage";
307
308                 if ($withfilename)
309                 {
310                         $s_file = htmlspecialchars($file);
311                         $str .= "\n    <ul><li>$s_file</li></ul>\n   ";
312                 }
313                 $str .= "</li>";
314
315                 if($pagereading_enable) {
316                         if(mb_ereg('^([A-Za-z])',mb_convert_kana($page,'a'),$matches)) {
317                                 $head = $matches[1];
318                         }
319                         elseif(mb_ereg('^([¥¡-¥ö])',$readings[$page],$matches)) {
320                                 $head = $matches[1];
321                         }
322                         elseif (mb_ereg('^[ -~]|[^¤¡-¤ó°¡-ô¦]',$page)) {
323                                 $head = $symbol;
324                         }
325                         else {
326                                 $head = $other;
327                         }
328                 }
329                 else {
330                         $head = (preg_match('/^([A-Za-z])/',$page,$matches)) ? $matches[1] :
331                                 (preg_match('/^([ -~])/',$page,$matches) ? $symbol : $other);
332                 }
333
334                 $list[$head][$page] = $str;
335         }
336         ksort($list);
337
338         $cnt = 0;
339         $arr_index = array();
340         $retval .= "<ul>\n";
341         foreach ($list as $head=>$pages)
342         {
343                 if ($head === $symbol)
344                 {
345                         $head = $_msg_symbol;
346                 }
347                 else if ($head === $other)
348                 {
349                         $head = $_msg_other;
350                 }
351
352                 if ($list_index)
353                 {
354                         $cnt++;
355                         $arr_index[] = "<a id=\"top_$cnt\" href=\"#head_$cnt\"><strong>$head</strong></a>";
356                         $retval .= " <li><a id=\"head_$cnt\" href=\"#top_$cnt\"><strong>$head</strong></a>\n  <ul>\n";
357                 }
358                 ksort($pages);
359                 $retval .= join("\n",$pages);
360                 if ($list_index)
361                 {
362                         $retval .= "\n  </ul>\n </li>\n";
363                 }
364         }
365         $retval .= "</ul>\n";
366         if ($list_index and $cnt > 0)
367         {
368                 $top = array();
369                 while (count($arr_index) > 0)
370                 {
371                         $top[] = join(" | \n",array_splice($arr_index,0,16))."\n";
372                 }
373                 $retval = "<div id=\"top\" style=\"text-align:center\">\n".
374                         join("<br />",$top)."</div>\n".$retval;
375         }
376         return $retval;
377 }
378
379 // ¥Æ¥­¥¹¥ÈÀ°·Á¥ë¡¼¥ë¤òɽ¼¨¤¹¤ë
380 function catrule()
381 {
382         global $rule_page;
383
384         if (!is_page($rule_page))
385         {
386                 return "<p>sorry, $rule_page unavailable.</p>";
387         }
388         return convert_html(get_source($rule_page));
389 }
390
391 // ¥¨¥é¡¼¥á¥Ã¥»¡¼¥¸¤òɽ¼¨¤¹¤ë
392 function die_message($msg)
393 {
394         $title = $page = 'Runtime error';
395
396         $body = <<<EOD
397 <h3>Runtime error</h3>
398 <strong>Error message : $msg</strong>
399 EOD;
400
401         if(defined('SKIN_FILE') && file_exists(SKIN_FILE) && is_readable(SKIN_FILE))
402         {
403           catbody($title,$page,$body);
404         }
405         else
406         {
407                 header('Content-Type: text/html; charset=euc-jp');
408                 print <<<EOD
409 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
410 <html>
411  <head>
412   <title>$title</title>
413   <meta http-equiv="content-type" content="text/html; charset=euc-jp">
414  </head>
415  <body>
416  $body
417  </body>
418 </html>
419 EOD;
420         }
421         die();
422 }
423
424 // ¸½ºß»þ¹ï¤ò¥Þ¥¤¥¯¥íÉäǼèÆÀ
425 function getmicrotime()
426 {
427         list($usec, $sec) = explode(' ',microtime());
428         return ((float)$sec + (float)$usec);
429 }
430
431 // Æü»þ¤òÆÀ¤ë
432 function get_date($format,$timestamp = NULL)
433 {
434         $time = ($timestamp === NULL) ? UTIME : $timestamp;
435         $time += ZONETIME;
436
437         $format = preg_replace('/(?<!\\\)T/',preg_replace('/(.)/','\\\$1',ZONE),$format);
438
439         return date($format,$time);
440 }
441
442 // Æü»þʸ»úÎó¤òºî¤ë
443 function format_date($val, $paren = FALSE)
444 {
445         global $date_format,$time_format,$weeklabels;
446
447         $val += ZONETIME;
448
449         $ins_date = date($date_format,$val);
450         $ins_time = date($time_format,$val);
451         $ins_week = '('.$weeklabels[date('w',$val)].')';
452
453         $ins = "$ins_date $ins_week $ins_time";
454         return $paren ? "($ins)" : $ins;
455 }
456
457 // ·Ð²á»þ¹ïʸ»úÎó¤òºî¤ë
458 function get_passage($time, $paren = TRUE)
459 {
460         static $units = array('m'=>60,'h'=>24,'d'=>1);
461
462         $time = max(0,(UTIME - $time) / 60); //minutes
463
464         foreach ($units as $unit=>$card)
465         {
466                 if ($time < $card)
467                 {
468                         break;
469                 }
470                 $time /= $card;
471         }
472         $time = floor($time).$unit;
473
474         return $paren ? "($time)" : $time;
475 }
476
477 //<input type="(submit|button|image)"...>¤ò±£¤¹
478 function drop_submit($str)
479 {
480         return preg_replace(
481                 '/<input([^>]+)type="(submit|button|image)"/i',
482                 '<input$1type="hidden"',
483                 $str
484         );
485 }
486
487 // AutoLink¤Î¥Ñ¥¿¡¼¥ó¤òÀ¸À®¤¹¤ë
488 // thx for hirofummy
489 function get_autolink_pattern(&$pages)
490 {
491         global $WikiName,$autolink,$nowikiname;
492
493         $config = &new Config('AutoLink');
494         $config->read();
495         $ignorepages = $config->get('IgnoreList');
496         $forceignorepages = $config->get('ForceIgnoreList');
497         unset($config);
498         $auto_pages = array_merge($ignorepages,$forceignorepages);
499
500         foreach ($pages as $page)
501         {
502                 if (preg_match("/^$WikiName$/",$page) ?
503                         $nowikiname : strlen($page) >= $autolink)
504                 {
505                         $auto_pages[] = $page;
506                 }
507         }
508
509         if (count($auto_pages) == 0)
510         {
511                 return $nowikiname ? '(?!)' : $WikiName;
512         }
513
514         $auto_pages = array_unique($auto_pages);
515         sort($auto_pages,SORT_STRING);
516         $auto_pages_a = array_values(preg_grep('/^[A-Z]+$/i',$auto_pages));
517         $auto_pages = array_values(array_diff($auto_pages,$auto_pages_a));
518
519         $result = get_autolink_pattern_sub($auto_pages,0,count($auto_pages),0);
520         $result_a = get_autolink_pattern_sub($auto_pages_a,0,count($auto_pages_a),0);
521
522         return array($result,$result_a,$forceignorepages);
523 }
524
525 function get_autolink_pattern_sub(&$pages,$start,$end,$pos)
526 {
527         if ($end == 0)
528         {
529                 return '(?!)';
530         }
531
532         $result = '';
533         $count = 0;
534         $x = (mb_strlen($pages[$start]) <= $pos);
535
536         if ($x)
537         {
538                 $start++;
539         }
540         for ($i = $start; $i < $end; $i = $j)
541         {
542                 $char = mb_substr($pages[$i],$pos,1);
543                 for ($j = $i; $j < $end; $j++)
544                 {
545                         if (mb_substr($pages[$j],$pos,1) != $char)
546                         {
547                                 break;
548                         }
549                 }
550                 if ($i != $start)
551                 {
552                         $result .= '|';
553                 }
554                 if ($i >= ($j - 1))
555                 {
556                         $result .= str_replace(' ','\\ ',preg_quote(mb_substr($pages[$i],$pos),'/'));
557                 }
558                 else
559                 {
560                         $result .=
561                                 str_replace(' ','\\ ',preg_quote($char,'/')).
562                                 get_autolink_pattern_sub($pages,$i,$j,$pos + 1);
563                 }
564                 $count++;
565         }
566         if ($x or $count > 1)
567         {
568                 $result = '(?:'.$result.')';
569         }
570         if ($x)
571         {
572                 $result .= '?';
573         }
574         return $result;
575 }
576
577 // pukiwiki.php¥¹¥¯¥ê¥×¥È¤Îabsolute-uri¤òÀ¸À®
578 function get_script_uri()
579 {
580         $script  = (SERVER_PORT == 443 ? 'https://' : 'http://');       // scheme
581         $script .= SERVER_NAME; // host
582         $script .= (SERVER_PORT == 80 ? '' : ':' . SERVER_PORT); // port
583
584         // SCRIPT_NAME ¤¬'/'¤Ç»Ï¤Þ¤Ã¤Æ¤¤¤Ê¤¤¾ì¹ç(cgi¤Ê¤É) REQUEST_URI¤ò»È¤Ã¤Æ¤ß¤ë
585         $path    = SCRIPT_NAME;
586         if ($path{0} != '/')
587         {
588                 if (!isset($_SERVER['REQUEST_URI']) or $_SERVER['REQUEST_URI']{0} != '/')
589                         return FALSE;
590
591                 // REQUEST_URI¤ò¥Ñ¡¼¥¹¤·¡¢pathÉôʬ¤À¤±¤ò¼è¤ê½Ð¤¹
592                 $parse_url = parse_url($script.$_SERVER['REQUEST_URI']);
593                 if (!isset($parse_url['path']) or $parse_url['path']{0} != '/')
594                         return FALSE;
595
596                 $path = $parse_url['path'];
597         }
598         $script .= $path;       // path
599
600         return $script;
601 }
602
603 /*
604 ÊÑ¿ôÆâ¤Înull(\0)¥Ð¥¤¥È¤òºï½ü¤¹¤ë
605 PHP¤Ïfopen("hoge.php\0.txt")¤Ç"hoge.php"¤ò³«¤¤¤Æ¤·¤Þ¤¦¤Ê¤É¤ÎÌäÂꤢ¤ê
606
607 http://ns1.php.gr.jp/pipermail/php-users/2003-January/012742.html
608 [PHP-users 12736] null byte attack
609
610 2003-05-16: magic quotes gpc¤ÎÉü¸µ½èÍý¤òÅý¹ç
611 2003-05-21: Ï¢ÁÛÇÛÎó¤Î¥­¡¼¤Ïbinary safe
612 */
613 function input_filter($param)
614 {
615         static $magic_quotes_gpc = NULL;
616         if ($magic_quotes_gpc === NULL)
617             $magic_quotes_gpc = get_magic_quotes_gpc();
618
619         if (is_array($param)) {
620                 return array_map('input_filter', $param);
621         } else {
622                 $result = str_replace("\0", '', $param);
623                 if ($magic_quotes_gpc) $result = stripslashes($result);
624                 return $result;
625         }
626 }
627
628 // Compat for 3rd party plugins. Remove this later
629 function sanitize($param) {
630         return input_filter($param);
631 }
632
633 // CSV·Á¼°¤Îʸ»úÎó¤òÇÛÎó¤Ë
634 function csv_explode($separator, $string)
635 {
636         $_separator = preg_quote($separator,'/');
637         if (!preg_match_all('/("[^"]*(?:""[^"]*)*"|[^'.$_separator.']*)'.$_separator.'/', $string.$separator, $matches))
638         {
639                 return array();
640         }
641
642         $retval = array();
643         foreach ($matches[1] as $str)
644         {
645                 $len = strlen($str);
646                 if ($len > 1 and $str{0} == '"' and $str{$len - 1} == '"')
647                 {
648                         $str = str_replace('""', '"', substr($str, 1, -1));
649                 }
650                 $retval[] = $str;
651         }
652         return $retval;
653 }
654
655 // ÇÛÎó¤òCSV·Á¼°¤Îʸ»úÎó¤Ë
656 function csv_implode($glue, $pieces)
657 {
658         $_glue = ($glue != '') ? '\\'.$glue{0} : '';
659         $arr = array();
660         foreach ($pieces as $str)
661         {
662                 if (ereg("[$_glue\"\n\r]",$str))
663                 {
664                         $str = '"'.str_replace('"', '""', $str).'"';
665                 }
666                 $arr[] = $str;
667         }
668         return join($glue, $arr);
669 }
670
671 function pkwk_login($pass = '')
672 {
673         global $adminpass;
674
675         if ($pass != '' && md5($pass) == $adminpass) {
676                 return TRUE;
677         } else {
678                 sleep (2);      // Blocking brute force attack
679                 return FALSE;
680         }
681 }
682
683 //is_a
684 //(PHP 4 >= 4.2.0)
685 //
686 //is_a --  Returns TRUE if the object is of this class or has this class as one of its parents
687
688 if (!function_exists('is_a'))
689 {
690         function is_a($class, $match)
691         {
692                 if (empty($class))
693                 {
694                         return false;
695                 }
696                 $class = is_object($class) ? get_class($class) : $class;
697                 if (strtolower($class) == strtolower($match))
698                 {
699                         return true;
700                 }
701                 return is_a(get_parent_class($class), $match);
702         }
703 }
704
705 //array_fill
706 //(PHP 4 >= 4.2.0)
707 //
708 //array_fill -- Fill an array with values
709
710 if (!function_exists('array_fill'))
711 {
712         function array_fill($start_index,$num,$value)
713         {
714                 $ret = array();
715
716                 while ($num-- > 0)
717                 {
718                         $ret[$start_index++] = $value;
719                 }
720                 return $ret;
721         }
722 }
723
724 //md5_file
725 //(PHP 4 >= 4.2.0)
726 //
727 //md5_file -- Calculates the md5 hash of a given filename
728
729 if (!function_exists('md5_file'))
730 {
731         function md5_file($filename)
732         {
733                 if (!file_exists($filename))
734                 {
735                         return FALSE;
736                 }
737                 $fd = fopen($filename, 'rb');
738                 $data = fread($fd, filesize($filename));
739                 fclose($fd);
740                 return md5($data);
741         }
742 }
743 ?>