OSDN Git Service

bf6d4d34cd2ba5f42eb93170b9b8af5fc464cb83
[pukiwiki/pukiwiki.git] / lib / func.php
1 <?php
2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
4 //
5 // $Id: func.php,v 1.19 2004/12/16 13:09:48 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, $force = FALSE)
74 {
75         global $function_freeze;
76         static $is_freeze = array();
77
78         if ($force === TRUE) $is_freeze = array();
79
80         if (isset($is_freeze[$page])) return $is_freeze[$page];
81         if (! $function_freeze || ! is_page($page)) {
82                 $is_freeze[$page] = FALSE;
83                 return FALSE;
84         }
85
86         list($lines) = get_source($page);
87         $is_freeze[$page] = (rtrim($lines) == '#freeze');
88
89         return $is_freeze[$page];
90 }
91
92 // ¼«Æ°¥Æ¥ó¥×¥ì¡¼¥È
93 function auto_template($page)
94 {
95         global $auto_template_func, $auto_template_rules;
96
97         if (! $auto_template_func) return '';
98
99         $body = '';
100         $matches = array();
101         foreach ($auto_template_rules as $rule => $template) {
102
103                 if (! preg_match("/$rule/", $page, $matches)) continue;
104
105                 $template_page = preg_replace("/$rule/", $template, $page);
106                 if (! is_page($template_page)) continue;
107
108                 $body = join('', get_source($template_page));
109
110                 // ¸«½Ð¤·¤Î¸ÇÍ­IDÉô¤òºï½ü
111                 $body = preg_replace('/^(\*{1,3}.*)\[#[A-Za-z][\w-]+\](.*)$/m', '$1$2', $body);
112
113                 // #freeze¤òºï½ü
114                 $body = preg_replace('/^#freeze\s*$/m', '', $body);
115
116                 $count = count($matches);
117                 for ($i = 0; $i < $count; $i++)
118                         $body = str_replace("\$$i", $matches[$i], $body);
119
120                 break;
121         }
122         return $body;
123 }
124
125 // ¸¡º÷¸ì¤òŸ³«¤¹¤ë
126 function get_search_words($words, $special = FALSE)
127 {
128         $retval = array();
129         // Perl¥á¥â - Àµ¤·¤¯¥Ñ¥¿¡¼¥ó¥Þ¥Ã¥Á¤µ¤»¤ë
130         // http://www.din.or.jp/~ohzaki/perl.htm#JP_Match
131         $eucpre = $eucpost = '';
132         if (SOURCE_ENCODING == 'EUC-JP') {
133                 $eucpre = '(?<!\x8F)';
134                 // # JIS X 0208 ¤¬ 0ʸ»ú°Ê¾å³¤¤¤Æ # ASCII, SS2, SS3 ¤Þ¤¿¤Ï½ªÃ¼
135                 $eucpost = '(?=(?:[\xA1-\xFE][\xA1-\xFE])*(?:[\x00-\x7F\x8E\x8F]|\z))';
136         }
137         $quote_func = create_function('$str', 'return preg_quote($str, "/");');
138
139         // LANG=='ja'¤Ç¡¢mb_convert_kana¤¬»È¤¨¤ë¾ì¹ç¤Ïmb_convert_kana¤ò»ÈÍÑ
140         $convert_kana = create_function('$str, $option',
141                 (LANG == 'ja' && function_exists('mb_convert_kana')) ?
142                         'return mb_convert_kana($str, $option);' : 'return $str;'
143         );
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 // ¸¡º÷
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                 if ($page == $whatsnew || (! $search_non_list && preg_match("/$non_list/", $page)))
191                         continue;
192
193                 // ¸¡º÷Âоݥڡ¼¥¸¤ÎÀ©¸Â¤ò¤«¤±¤ë¤«¤É¤¦¤« (¥Ú¡¼¥¸Ì¾¤ÏÀ©¸Â³°)
194                 if ($search_auth && ! check_readable($page, false, false)) {
195                         $source = get_source(); // ¸¡º÷Âоݥڡ¼¥¸ÆâÍƤò¶õ¤Ë¡£
196                 } else {
197                         $source = get_source($page);
198                 }
199                 if (! $non_format)
200                         array_unshift($source, $page); // ¥Ú¡¼¥¸Ì¾¤â¸¡º÷ÂоݤË
201
202                 $b_match = FALSE;
203                 foreach ($keys as $key) {
204                         $tmp     = preg_grep("/$key/", $source);
205                         $b_match = (! empty($tmp));
206                         if ($b_match xor $b_type) break;
207                 }
208                 if ($b_match) $pages[$page] = get_filetime($page);
209         }
210         if ($non_format) return array_keys($pages);
211
212         $r_word = rawurlencode($word);
213         $s_word = htmlspecialchars($word);
214         if (empty($pages))
215                 return str_replace('$1', $s_word, $_msg_notfoundresult);
216
217         ksort($pages);
218         $retval = "<ul>\n";
219         foreach ($pages as $page=>$time) {
220                 $r_page  = rawurlencode($page);
221                 $s_page  = htmlspecialchars($page);
222                 $passage = get_passage($time);
223                 $retval .= ' <li><a href="' . $script . '?cmd=read&amp;page=' .
224                         $r_page . '&amp;word=' . $r_word . '">' . $s_page .
225                         '</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         return isset($vars['cmd']) && (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;
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                 $r_page  = rawurlencode($page);
286                 $s_page  = htmlspecialchars($page, ENT_QUOTES);
287                 $passage = get_pg_passage($page);
288
289                 $str = '   <li><a href="' .
290                         $script . '?cmd=' . $cmd . '&amp;page=' . $r_page .
291                         '">' . $s_page . '</a>' . $passage;
292
293                 if ($withfilename) {
294                         $s_file = htmlspecialchars($file);
295                         $str .= "\n" . '    <ul><li>' . $s_file . '</li></ul>' .
296                                 "\n" . '   ';
297                 }
298                 $str .= '</li>';
299
300                 // WARNING: Japanese code hard-wired
301                 if($pagereading_enable) {
302                         if(mb_ereg('^([A-Za-z])', mb_convert_kana($page, 'a'), $matches)) {
303                                 $head = $matches[1];
304                         } elseif(mb_ereg('^([¥¡-¥ö])', $readings[$page], $matches)) { // here
305                                 $head = $matches[1];
306                         } elseif (mb_ereg('^[ -~]|[^¤¡-¤ó°¡-ô¦]', $page)) { // and here
307                                 $head = $symbol;
308                         } else {
309                                 $head = $other;
310                         }
311                 } else {
312                         $head = (preg_match('/^([A-Za-z])/', $page, $matches)) ? $matches[1] :
313                                 (preg_match('/^([ -~])/', $page, $matches) ? $symbol : $other);
314                 }
315
316                 $list[$head][$page] = $str;
317         }
318         ksort($list);
319
320         $cnt = 0;
321         $arr_index = array();
322         $retval .= '<ul>' . "\n";
323         foreach ($list as $head=>$pages) {
324                 if ($head === $symbol) {
325                         $head = $_msg_symbol;
326                 } else if ($head === $other) {
327                         $head = $_msg_other;
328                 }
329
330                 if ($list_index) {
331                         ++$cnt;
332                         $arr_index[] = '<a id="top_' . $cnt .
333                                 '" href="#head_' . $cnt . '"><strong>' .
334                                 $head . '</strong></a>';
335                         $retval .= ' <li><a id="head_' . $cnt . '" href="#top_' . $cnt .
336                                 '"><strong>' . $head . '</strong></a>' . "\n" .
337                                 '  <ul>' . "\n";
338                 }
339                 ksort($pages);
340                 $retval .= join("\n", $pages);
341                 if ($list_index)
342                         $retval .= "\n  </ul>\n </li>\n";
343         }
344         $retval .= '</ul>' . "\n";
345         if ($list_index && $cnt > 0) {
346                 $top = array();
347                 while (! empty($arr_index))
348                         $top[] = join(' | ' . "\n", array_splice($arr_index, 0, 16)) . "\n";
349
350                 $retval = '<div id="top" style="text-align:center">' . "\n" .
351                         join('<br />', $top) . '</div>' . "\n" . $retval;
352         }
353         return $retval;
354 }
355
356 // Show text formatting rules
357 function catrule()
358 {
359         global $rule_page;
360
361         if (! is_page($rule_page)) {
362                 return '<p>Sorry, page \'' . htmlspecialchars($rule_page) .
363                         '\' unavailable.</p>';
364         } else {
365                 return convert_html(get_source($rule_page));
366         }
367 }
368
369 // Show (critical) error message
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         $format = preg_replace('/(?<!\\\)T/',
411                 preg_replace('/(.)/', '\\\$1', ZONE), $format);
412
413         $time = ZONETIME + (($timestamp !== NULL) ? $timestamp : UTIME);
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         $date = date($date_format . ' ' . $time_format, $val) .
426                 ' ' . '(' . $weeklabels[date('w', $val)] . ')';
427
428         return $paren ? '(' . $date . ')' : $date;
429 }
430
431 // ·Ð²á»þ¹ïʸ»úÎó¤òºî¤ë
432 function get_passage($time, $paren = TRUE)
433 {
434         static $units = array('m'=>60, 'h'=>24, 'd'=>1);
435
436         $time = max(0, (UTIME - $time) / 60); // minutes
437
438         foreach ($units as $unit=>$card) {
439                 if ($time < $card) break;
440                 $time /= $card;
441         }
442         $time = floor($time) . $unit;
443
444         return $paren ? '(' . $time . ')' : $time;
445 }
446
447 // Hide <input type="(submit|button|image)"...>
448 function drop_submit($str)
449 {
450         return preg_replace('/<input([^>]+)type="(submit|button|image)"/i',
451                 '<input$1type="hidden"', $str);
452 }
453
454 // AutoLink¤Î¥Ñ¥¿¡¼¥ó¤òÀ¸À®¤¹¤ë
455 // thx for hirofummy
456 function get_autolink_pattern(& $pages)
457 {
458         global $WikiName, $autolink, $nowikiname;
459
460         $config = &new Config('AutoLink');
461         $config->read();
462         $ignorepages      = $config->get('IgnoreList');
463         $forceignorepages = $config->get('ForceIgnoreList');
464         unset($config);
465         $auto_pages = array_merge($ignorepages, $forceignorepages);
466
467         foreach ($pages as $page) {
468                 if (preg_match("/^$WikiName$/", $page) ?
469                     $nowikiname : strlen($page) >= $autolink)
470                         $auto_pages[] = $page;
471         }
472
473         if (empty($auto_pages))
474                 return $nowikiname ? '(?!)' : $WikiName;
475
476         $auto_pages = array_unique($auto_pages);
477         sort($auto_pages, SORT_STRING);
478
479         $auto_pages_a = array_values(preg_grep('/^[A-Z]+$/i', $auto_pages));
480         $auto_pages   = array_values(array_diff($auto_pages,  $auto_pages_a));
481
482         $result   = get_autolink_pattern_sub($auto_pages,   0, count($auto_pages),   0);
483         $result_a = get_autolink_pattern_sub($auto_pages_a, 0, count($auto_pages_a), 0);
484
485         return array($result, $result_a, $forceignorepages);
486 }
487
488 function get_autolink_pattern_sub(& $pages, $start, $end, $pos)
489 {
490         if ($end == 0) return '(?!)';
491
492         $result = '';
493         $count = $i = $j = 0;
494         $x = (mb_strlen($pages[$start]) <= $pos);
495         if ($x) ++$start;
496
497         for ($i = $start; $i < $end; $i = $j)
498         {
499                 $char = mb_substr($pages[$i], $pos, 1);
500                 for ($j = $i; $j < $end; $j++) {
501                         if (mb_substr($pages[$j], $pos, 1) != $char) break;
502                 }
503                 if ($i != $start) $result .= '|';
504                 if ($i >= ($j - 1)) {
505                         $result .= str_replace(' ', '\\ ', preg_quote(mb_substr($pages[$i], $pos), '/'));
506                 } else {
507                         $result .= str_replace(' ', '\\ ', preg_quote($char, '/')) .
508                                 get_autolink_pattern_sub($pages, $i, $j, $pos + 1);
509                 }
510                 ++$count;
511         }
512         if ($x || $count > 1) $result = '(?:' . $result . ')';
513         if ($x)               $result .= '?';
514
515         return $result;
516 }
517
518 // pukiwiki.php¥¹¥¯¥ê¥×¥È¤Îabsolute-uri¤òÀ¸À®
519 function get_script_uri($init_uri = '')
520 {
521         global $script_directory_index;
522         static $script;
523
524         if ($init_uri == '') {
525                 // Get
526                 if (isset($script)) return $script;
527
528                 // Set automatically
529                 $msg     = 'get_script_uri() failed: Please set $script at INI_FILE manually';
530
531                 $script  = (SERVER_PORT == 443 ? 'https://' : 'http://'); // scheme
532                 $script .= SERVER_NAME; // host
533                 $script .= (SERVER_PORT == 80 ? '' : ':' . SERVER_PORT);  // port
534
535                 // SCRIPT_NAME ¤¬'/'¤Ç»Ï¤Þ¤Ã¤Æ¤¤¤Ê¤¤¾ì¹ç(cgi¤Ê¤É) REQUEST_URI¤ò»È¤Ã¤Æ¤ß¤ë
536                 $path    = SCRIPT_NAME;
537                 if ($path{0} != '/') {
538                         if (! isset($_SERVER['REQUEST_URI']) || $_SERVER['REQUEST_URI']{0} != '/')
539                                 die_message($msg);
540
541                         // REQUEST_URI¤ò¥Ñ¡¼¥¹¤·¡¢pathÉôʬ¤À¤±¤ò¼è¤ê½Ð¤¹
542                         $parse_url = parse_url($script . $_SERVER['REQUEST_URI']);
543                         if (! isset($parse_url['path']) || $parse_url['path']{0} != '/')
544                                 die_message($msg);
545
546                         $path = $parse_url['path'];
547                 }
548                 $script .= $path;
549
550                 if (! is_url($script, TRUE) && php_sapi_name() == 'cgi')
551                         die_message($msg);
552                 unset($msg);
553
554         } else {
555                 // Set manually
556                 if (isset($script)) die_message('$script: Already init');
557                 if (! is_url($init_uri, TRUE)) die_message('$script: Invalid URI');
558                 $script = $init_uri;
559         }
560
561         // Cut filename or not
562         if (isset($script_directory_index)) {
563                 if (! file_exists($script_directory_index))
564                         die_message('Directory index file not found: ' .
565                                 htmlspecialchars($script_directory_index));
566                 $matches = array();
567                 if (preg_match('#^(.+/)' . preg_quote($script_directory_index, '#') . '$#',
568                         $script, $matches)) $script = $matches[1];
569         }
570
571         return $script;
572 }
573
574 /*
575 ÊÑ¿ôÆâ¤Înull(\0)¥Ð¥¤¥È¤òºï½ü¤¹¤ë
576 PHP¤Ïfopen("hoge.php\0.txt")¤Ç"hoge.php"¤ò³«¤¤¤Æ¤·¤Þ¤¦¤Ê¤É¤ÎÌäÂꤢ¤ê
577
578 http://ns1.php.gr.jp/pipermail/php-users/2003-January/012742.html
579 [PHP-users 12736] null byte attack
580
581 2003-05-16: magic quotes gpc¤ÎÉü¸µ½èÍý¤òÅý¹ç
582 2003-05-21: Ï¢ÁÛÇÛÎó¤Î¥­¡¼¤Ïbinary safe
583 */
584 function input_filter($param)
585 {
586         static $magic_quotes_gpc = NULL;
587         if ($magic_quotes_gpc === NULL)
588             $magic_quotes_gpc = get_magic_quotes_gpc();
589
590         if (is_array($param)) {
591                 return array_map('input_filter', $param);
592         } else {
593                 $result = str_replace("\0", '', $param);
594                 if ($magic_quotes_gpc) $result = stripslashes($result);
595                 return $result;
596         }
597 }
598
599 // Compat for 3rd party plugins. Remove this later
600 function sanitize($param) {
601         return input_filter($param);
602 }
603
604 // CSV·Á¼°¤Îʸ»úÎó¤òÇÛÎó¤Ë
605 function csv_explode($separator, $string)
606 {
607         $retval = $matches = array();
608
609         $_separator = preg_quote($separator, '/');
610         if (! preg_match_all('/("[^"]*(?:""[^"]*)*"|[^' . $_separator . ']*)' .
611             $_separator . '/', $string . $separator, $matches))
612                 return array();
613
614         foreach ($matches[1] as $str) {
615                 $len = strlen($str);
616                 if ($len > 1 && $str{0} == '"' && $str{$len - 1} == '"')
617                         $str = str_replace('""', '"', substr($str, 1, -1));
618                 $retval[] = $str;
619         }
620         return $retval;
621 }
622
623 // ÇÛÎó¤òCSV·Á¼°¤Îʸ»úÎó¤Ë
624 function csv_implode($glue, $pieces)
625 {
626         $_glue = ($glue != '') ? '\\' . $glue{0} : '';
627         $arr = array();
628         foreach ($pieces as $str) {
629                 if (ereg("[$_glue\"\n\r]", $str))
630                         $str = '"' . str_replace('"', '""', $str) . '"';
631                 $arr[] = $str;
632         }
633         return join($glue, $arr);
634 }
635
636 function pkwk_login($pass = '')
637 {
638         global $adminpass;
639
640         if ($pass != '' && md5($pass) == $adminpass) {
641                 return TRUE;
642         } else {
643                 sleep(2);       // Blocking brute force attack
644                 return FALSE;
645         }
646 }
647
648
649 //// Compat ////
650
651 // is_a --  Returns TRUE if the object is of this class or has this class as one of its parents
652 // (PHP 4 >= 4.2.0)
653 if (! function_exists('is_a'))
654 {
655         function is_a($class, $match)
656         {
657                 if (empty($class)) return FALSE; 
658
659                 $class = is_object($class) ? get_class($class) : $class;
660                 if (strtolower($class) == strtolower($match)) {
661                         return TRUE;
662                 } else {
663                         return is_a(get_parent_class($class), $match);  // Recurse
664                 }
665         }
666 }
667
668 // array_fill -- Fill an array with values
669 // (PHP 4 >= 4.2.0)
670 if (! function_exists('array_fill'))
671 {
672         function array_fill($start_index, $num, $value)
673         {
674                 $ret = array();
675                 while ($num-- > 0) $ret[$start_index++] = $value;
676                 return $ret;
677         }
678 }
679
680 // md5_file -- Calculates the md5 hash of a given filename
681 // (PHP 4 >= 4.2.0)
682 if (! function_exists('md5_file'))
683 {
684         function md5_file($filename)
685         {
686                 if (! file_exists($filename)) return FALSE;
687
688                 $fd = fopen($filename, 'rb');
689                 if ($fd === FALSE ) return FALSE;
690                 $data = fread($fd, filesize($filename));
691                 fclose($fd);
692                 return md5($data);
693         }
694 }
695 ?>