OSDN Git Service

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