OSDN Git Service

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