OSDN Git Service

Added is_freeze() caching
[pukiwiki/pukiwiki.git] / lib / func.php
1 <?php
2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
4 //
5 // $Id: func.php,v 1.15 2004/12/04 05:55:42 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         static $is_freeze = array();
77
78         if (isset($is_freeze[$page])) return $is_freeze[$page];
79         if (! $function_freeze || ! is_page($page)) return FALSE;
80
81         list($lines) = get_source($page);
82         $is_freeze[$page] = (rtrim($lines) == '#freeze');
83
84         return $is_freeze[$page];
85 }
86
87 // ¼«Æ°¥Æ¥ó¥×¥ì¡¼¥È
88 function auto_template($page)
89 {
90         global $auto_template_func, $auto_template_rules;
91
92         if (! $auto_template_func) return '';
93
94         $body = '';
95         $matches = array();
96         foreach ($auto_template_rules as $rule => $template) {
97
98                 if (! preg_match("/$rule/", $page, $matches)) continue;
99
100                 $template_page = preg_replace("/$rule/", $template, $page);
101                 if (! is_page($template_page)) continue;
102
103                 $body = join('', get_source($template_page));
104
105                 // ¸«½Ð¤·¤Î¸ÇÍ­IDÉô¤òºï½ü
106                 $body = preg_replace('/^(\*{1,3}.*)\[#[A-Za-z][\w-]+\](.*)$/m', '$1$2', $body);
107
108                 // #freeze¤òºï½ü
109                 $body = preg_replace('/^#freeze\s*$/m', '', $body);
110
111                 for ($i = 0; $i < count($matches); $i++) {
112                         $body = str_replace("\$$i", $matches[$i], $body);
113                 }
114                 break;
115         }
116         return $body;
117 }
118
119 // ¸¡º÷¸ì¤òŸ³«¤¹¤ë
120 function get_search_words($words, $special = FALSE)
121 {
122         $retval = array();
123         // Perl¥á¥â - Àµ¤·¤¯¥Ñ¥¿¡¼¥ó¥Þ¥Ã¥Á¤µ¤»¤ë
124         // http://www.din.or.jp/~ohzaki/perl.htm#JP_Match
125         $eucpre = $eucpost = '';
126         if (SOURCE_ENCODING == 'EUC-JP')
127         {
128                 $eucpre = '(?<!\x8F)';
129                 // # JIS X 0208 ¤¬ 0ʸ»ú°Ê¾å³¤¤¤Æ # ASCII, SS2, SS3 ¤Þ¤¿¤Ï½ªÃ¼
130                 $eucpost = '(?=(?:[\xA1-\xFE][\xA1-\xFE])*(?:[\x00-\x7F\x8E\x8F]|\z))';
131         }
132         $quote_func = create_function('$str', 'return preg_quote($str, "/");');
133
134         // LANG=='ja'¤Ç¡¢mb_convert_kana¤¬»È¤¨¤ë¾ì¹ç¤Ïmb_convert_kana¤ò»ÈÍÑ
135         $convert_kana = create_function('$str, $option',
136                 (LANG == 'ja' && function_exists('mb_convert_kana')) ?
137                         'return mb_convert_kana($str, $option);' : 'return $str;'
138         );
139
140         foreach ($words as $word)
141         {
142                 // ±Ñ¿ô»ú¤ÏȾ³Ñ,¥«¥¿¥«¥Ê¤ÏÁ´³Ñ,¤Ò¤é¤¬¤Ê¤Ï¥«¥¿¥«¥Ê¤Ë
143                 $word_zk = $convert_kana($word, 'aKCV');
144                 $chars = array();
145                 for ($pos = 0; $pos < mb_strlen($word_zk); $pos++)
146                 {
147                         $char = mb_substr($word_zk, $pos,1);
148                         // $special : htmlspecialchars()¤òÄ̤¹¤«
149                         $arr = array($quote_func($special ? htmlspecialchars($char) : $char));
150                         if (strlen($char) == 1) // ±Ñ¿ô»ú
151                         {
152                                 foreach (array(strtoupper($char), strtolower($char)) as $_char)
153                                 {
154                                         if ($char != '&') {
155                                                 $arr[] = $quote_func($_char);
156                                         }
157                                         $ord = ord($_char);
158                                         $arr[] = sprintf('&#(?:%d|x%x);', $ord, $ord); // ¼ÂÂλ²¾È
159                                         $arr[] = $quote_func($convert_kana($_char, 'A')); // Á´³Ñ
160                                 }
161                         }
162                         else // ¥Þ¥ë¥Á¥Ð¥¤¥Èʸ»ú
163                         {
164                                 $arr[] = $quote_func($convert_kana($char, 'c')); // ¤Ò¤é¤¬¤Ê
165                                 $arr[] = $quote_func($convert_kana($char, 'k')); // È¾³Ñ¥«¥¿¥«¥Ê
166                         }
167                         $chars[] = '(?:' . join('|', array_unique($arr)) . ')';
168                 }
169                 $retval[$word] = $eucpre.join('', $chars) . $eucpost;
170         }
171         return $retval;
172 }
173
174 // ¸¡º÷
175 function do_search($word, $type = 'AND', $non_format = FALSE)
176 {
177         global $script, $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         foreach ($_pages as $page)
190         {
191                 if ($page == $whatsnew || (! $search_non_list && preg_match("/$non_list/", $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 = (count($tmp) > 0);
207                         if ($b_match xor $b_type)
208                                 break;
209                 }
210                 if ($b_match)
211                         $pages[$page] = get_filetime($page);
212         }
213         if ($non_format)
214                 return array_keys($pages);
215
216         $r_word = rawurlencode($word);
217         $s_word = htmlspecialchars($word);
218         if (empty($pages))
219                 return str_replace('$1', $s_word, $_msg_notfoundresult);
220
221         ksort($pages);
222         $retval = "<ul>\n";
223         foreach ($pages as $page=>$time) {
224                 $r_page  = rawurlencode($page);
225                 $s_page  = htmlspecialchars($page);
226                 $passage = get_passage($time);
227                 $retval .= " <li><a href=\"$script?cmd=read&amp;page=$r_page&amp;word=$r_word\">$s_page</a>$passage</li>\n";
228         }
229         $retval .= "</ul>\n";
230
231         $retval .= str_replace('$1', $s_word, str_replace('$2', count($pages),
232                 str_replace('$3', count($_pages), $b_type ? $_msg_andresult : $_msg_orresult)));
233
234         return $retval;
235 }
236
237 // ¥×¥í¥°¥é¥à¤Ø¤Î°ú¿ô¤Î¥Á¥§¥Ã¥¯
238 function arg_check($str)
239 {
240         global $vars;
241
242         return isset($vars['cmd']) && (strpos($vars['cmd'], $str) === 0);
243 }
244
245 // ¥Ú¡¼¥¸Ì¾¤Î¥¨¥ó¥³¡¼¥É
246 function encode($key)
247 {
248         return ($key == '') ? '' : strtoupper(join('', unpack('H*0', $key)));
249 }
250
251 // ¥Ú¡¼¥¸Ì¾¤Î¥Ç¥³¡¼¥É
252 function decode($key)
253 {
254         return ($key == '') ? '' : substr(pack('H*', '20202020' . $key), 4);
255 }
256
257 // [[ ]] ¤ò¼è¤ê½ü¤¯
258 function strip_bracket($str)
259 {
260         $match = array();
261         if (preg_match('/^\[\[(.*)\]\]$/', $str, $match)) {
262                 return $match[1];
263         } else {
264                 return $str;
265         }
266 }
267
268 // ¥Ú¡¼¥¸°ìÍ÷¤ÎºîÀ®
269 function page_list($pages, $cmd = 'read', $withfilename = FALSE)
270 {
271         global $script, $list_index, $top;
272         global $_msg_symbol, $_msg_other;
273         global $pagereading_enable;
274
275         // ¥½¡¼¥È¥­¡¼¤ò·èÄꤹ¤ë¡£ ' ' < '[a-zA-Z]' < 'zz'¤È¤¤¤¦Á°Äó¡£
276         $symbol = ' ';
277         $other = 'zz';
278
279         $retval = '';
280
281         if($pagereading_enable) {
282                 mb_regex_encoding(SOURCE_ENCODING);
283                 $readings = get_readings($pages);
284         }
285
286         $list = $matches = array();
287         foreach($pages as $file=>$page)
288         {
289                 $r_page  = rawurlencode($page);
290                 $s_page  = htmlspecialchars($page, ENT_QUOTES);
291                 $passage = get_pg_passage($page);
292
293                 $str = "   <li><a href=\"$script?cmd=$cmd&amp;page=$r_page\">$s_page</a>$passage";
294
295                 if ($withfilename) {
296                         $s_file = htmlspecialchars($file);
297                         $str .= "\n    <ul><li>$s_file</li></ul>\n   ";
298                 }
299                 $str .= "</li>";
300
301                 if($pagereading_enable) {
302                         if(mb_ereg('^([A-Za-z])', mb_convert_kana($page, 'a'), $matches)) {
303                                 $head = $matches[1];
304                         }
305                         elseif(mb_ereg('^([¥¡-¥ö])', $readings[$page], $matches)) {
306                                 $head = $matches[1];
307                         }
308                         elseif (mb_ereg('^[ -~]|[^¤¡-¤ó°¡-ô¦]', $page)) {
309                                 $head = $symbol;
310                         }
311                         else {
312                                 $head = $other;
313                         }
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         {
329                 if ($head === $symbol) {
330                         $head = $_msg_symbol;
331                 } else if ($head === $other) {
332                         $head = $_msg_other;
333                 }
334
335                 if ($list_index) {
336                         ++$cnt;
337                         $arr_index[] = "<a id=\"top_$cnt\" href=\"#head_$cnt\"><strong>$head</strong></a>";
338                         $retval .= " <li><a id=\"head_$cnt\" href=\"#top_$cnt\"><strong>$head</strong></a>\n  <ul>\n";
339                 }
340                 ksort($pages);
341                 $retval .= join("\n", $pages);
342                 if ($list_index)
343                         $retval .= "\n  </ul>\n </li>\n";
344         }
345         $retval .= "</ul>\n";
346         if ($list_index && $cnt > 0) {
347                 $top = array();
348                 while (count($arr_index) > 0) {
349                         $top[] = join(" | \n", array_splice($arr_index, 0, 16)) . "\n";
350                 }
351                 $retval = "<div id=\"top\" style=\"text-align:center\">\n" .
352                         join('<br />', $top) . "</div>\n" . $retval;
353         }
354         return $retval;
355 }
356
357 // ¥Æ¥­¥¹¥ÈÀ°·Á¥ë¡¼¥ë¤òɽ¼¨¤¹¤ë
358 function catrule()
359 {
360         global $rule_page;
361
362         if (! is_page($rule_page)) {
363                 return "<p>Sorry, $rule_page unavailable.</p>";
364         } else {
365                 return convert_html(get_source($rule_page));
366         }
367 }
368
369 // ¥¨¥é¡¼¥á¥Ã¥»¡¼¥¸¤òɽ¼¨¤¹¤ë
370 function die_message($msg)
371 {
372         $title = $page = 'Runtime error';
373
374         $body = <<<EOD
375 <h3>Runtime error</h3>
376 <strong>Error message : $msg</strong>
377 EOD;
378
379         if(defined('SKIN_FILE') && file_exists(SKIN_FILE) && is_readable(SKIN_FILE)) {
380                 catbody($title, $page, $body);
381         } else {
382                 pkwk_headers_sent();
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 (empty($auto_pages))
479                 return $nowikiname ? '(?!)' : $WikiName;
480
481         $auto_pages = array_unique($auto_pages);
482         sort($auto_pages, SORT_STRING);
483
484         $auto_pages_a = array_values(preg_grep('/^[A-Z]+$/i', $auto_pages));
485         $auto_pages   = array_values(array_diff($auto_pages,  $auto_pages_a));
486
487         $result   = get_autolink_pattern_sub($auto_pages,   0, count($auto_pages),   0);
488         $result_a = get_autolink_pattern_sub($auto_pages_a, 0, count($auto_pages_a), 0);
489
490         return array($result, $result_a, $forceignorepages);
491 }
492
493 function get_autolink_pattern_sub(& $pages, $start, $end, $pos)
494 {
495         if ($end == 0) return '(?!)';
496
497         $result = '';
498         $count = $i = $j = 0;
499         $x = (mb_strlen($pages[$start]) <= $pos);
500         if ($x) ++$start;
501
502         for ($i = $start; $i < $end; $i = $j)
503         {
504                 $char = mb_substr($pages[$i], $pos, 1);
505                 for ($j = $i; $j < $end; $j++) {
506                         if (mb_substr($pages[$j], $pos, 1) != $char) break;
507                 }
508                 if ($i != $start) $result .= '|';
509                 if ($i >= ($j - 1)) {
510                         $result .= str_replace(' ', '\\ ', preg_quote(mb_substr($pages[$i], $pos), '/'));
511                 } else {
512                         $result .= str_replace(' ', '\\ ', preg_quote($char, '/')) .
513                                 get_autolink_pattern_sub($pages, $i, $j, $pos + 1);
514                 }
515                 ++$count;
516         }
517         if ($x || $count > 1) $result = '(?:' . $result . ')';
518         if ($x)               $result .= '?';
519
520         return $result;
521 }
522
523 // pukiwiki.php¥¹¥¯¥ê¥×¥È¤Îabsolute-uri¤òÀ¸À®
524 function get_script_uri($init_uri = '')
525 {
526         global $script_directory_index;
527         static $script;
528
529         if ($init_uri == '') {
530                 // Get
531                 if (isset($script)) return $script;
532
533                 // Set automatically
534                 $msg     = 'get_script_uri() failed: Please set $script at INI_FILE manually';
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                         if (! isset($_SERVER['REQUEST_URI']) || $_SERVER['REQUEST_URI']{0} != '/')
544                                 die_message($msg);
545
546                         // REQUEST_URI¤ò¥Ñ¡¼¥¹¤·¡¢pathÉôʬ¤À¤±¤ò¼è¤ê½Ð¤¹
547                         $parse_url = parse_url($script . $_SERVER['REQUEST_URI']);
548                         if (! isset($parse_url['path']) || $parse_url['path']{0} != '/')
549                                 die_message($msg);
550
551                         $path = $parse_url['path'];
552                 }
553                 $script .= $path;
554
555                 if (! is_url($script, TRUE) && php_sapi_name() == 'cgi')
556                         die_message($msg);
557                 unset($msg);
558
559         } else {
560                 // Set manually
561                 if (isset($script)) die_message('$script: Already init');
562                 if (! is_url($init_uri, TRUE)) die_message('$script: Invalid URI');
563                 $script = $init_uri;
564         }
565
566         // Cut filename or not
567         if (isset($script_directory_index)) {
568                 if (! file_exists($script_directory_index))
569                         die_message('Directory index file not found: ' .
570                                 htmlspecialchars($script_directory_index));
571                 $matches = array();
572                 if (preg_match('#^(.+/)' . preg_quote($script_directory_index, '#') . '$#',
573                         $script, $matches)) $script = $matches[1];
574         }
575
576         return $script;
577 }
578
579 /*
580 ÊÑ¿ôÆâ¤Înull(\0)¥Ð¥¤¥È¤òºï½ü¤¹¤ë
581 PHP¤Ïfopen("hoge.php\0.txt")¤Ç"hoge.php"¤ò³«¤¤¤Æ¤·¤Þ¤¦¤Ê¤É¤ÎÌäÂꤢ¤ê
582
583 http://ns1.php.gr.jp/pipermail/php-users/2003-January/012742.html
584 [PHP-users 12736] null byte attack
585
586 2003-05-16: magic quotes gpc¤ÎÉü¸µ½èÍý¤òÅý¹ç
587 2003-05-21: Ï¢ÁÛÇÛÎó¤Î¥­¡¼¤Ïbinary safe
588 */
589 function input_filter($param)
590 {
591         static $magic_quotes_gpc = NULL;
592         if ($magic_quotes_gpc === NULL)
593             $magic_quotes_gpc = get_magic_quotes_gpc();
594
595         if (is_array($param)) {
596                 return array_map('input_filter', $param);
597         } else {
598                 $result = str_replace("\0", '', $param);
599                 if ($magic_quotes_gpc) $result = stripslashes($result);
600                 return $result;
601         }
602 }
603
604 // Compat for 3rd party plugins. Remove this later
605 function sanitize($param) {
606         return input_filter($param);
607 }
608
609 // CSV·Á¼°¤Îʸ»úÎó¤òÇÛÎó¤Ë
610 function csv_explode($separator, $string)
611 {
612         $retval = $matches = array();
613
614         $_separator = preg_quote($separator, '/');
615         if (! preg_match_all('/("[^"]*(?:""[^"]*)*"|[^' . $_separator . ']*)' .
616             $_separator . '/', $string . $separator, $matches))
617                 return array();
618
619         foreach ($matches[1] as $str) {
620                 $len = strlen($str);
621                 if ($len > 1 && $str{0} == '"' && $str{$len - 1} == '"')
622                         $str = str_replace('""', '"', substr($str, 1, -1));
623                 $retval[] = $str;
624         }
625         return $retval;
626 }
627
628 // ÇÛÎó¤òCSV·Á¼°¤Îʸ»úÎó¤Ë
629 function csv_implode($glue, $pieces)
630 {
631         $_glue = ($glue != '') ? '\\' . $glue{0} : '';
632         $arr = array();
633         foreach ($pieces as $str) {
634                 if (ereg("[$_glue\"\n\r]", $str))
635                         $str = '"' . str_replace('"', '""', $str) . '"';
636                 $arr[] = $str;
637         }
638         return join($glue, $arr);
639 }
640
641 function pkwk_login($pass = '')
642 {
643         global $adminpass;
644
645         if ($pass != '' && md5($pass) == $adminpass) {
646                 return TRUE;
647         } else {
648                 sleep (2);      // Blocking brute force attack
649                 return FALSE;
650         }
651 }
652
653
654 //// Compat ////
655
656 // is_a --  Returns TRUE if the object is of this class or has this class as one of its parents
657 // (PHP 4 >= 4.2.0)
658 if (! function_exists('is_a'))
659 {
660         function is_a($class, $match)
661         {
662                 if (empty($class)) return false;
663
664                 $class = is_object($class) ? get_class($class) : $class;
665                 if (strtolower($class) == strtolower($match)) {
666                         return true;
667                 } else {
668                         return is_a(get_parent_class($class), $match);  // Recurse
669                 }
670         }
671 }
672
673 // array_fill -- Fill an array with values
674 // (PHP 4 >= 4.2.0)
675 if (! function_exists('array_fill'))
676 {
677         function array_fill($start_index, $num, $value)
678         {
679                 $ret = array();
680                 while ($num-- > 0) $ret[$start_index++] = $value;
681                 return $ret;
682         }
683 }
684
685 // md5_file -- Calculates the md5 hash of a given filename
686 // (PHP 4 >= 4.2.0)
687 if (! function_exists('md5_file'))
688 {
689         function md5_file($filename)
690         {
691                 if (! file_exists($filename)) return FALSE;
692
693                 $fd = fopen($filename, 'rb');
694                 if ($fd === FALSE ) return FALSE;
695                 $data = fread($fd, filesize($filename));
696                 fclose($fd);
697                 return md5($data);
698         }
699 }
700 ?>