OSDN Git Service

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