OSDN Git Service

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