2 // PukiWiki - Yet another WikiWikiWeb clone.
5 // 2002-2016 PukiWiki Development Team
6 // 2001-2002 Originally written by yu-ji
7 // License: GPL v2 or (at your option) any later version
11 function pkwk_log($message)
13 $log_filepath = 'log/error.log.php';
14 static $dateTimeExists;
15 if (!isset($dateTimeExists)) {
16 $dateTimeExists = class_exists('DateTime');
17 error_log("<?php\n", 3, $log_filepath);
19 if ($dateTimeExists) {
21 $d = \DateTime::createFromFormat('U.u', sprintf('%6F', microtime(true)));
22 $timestamp = substr($d->format('Y-m-d H:i:s.u'), 0, 23);
24 $timestamp = date('Y-m-d H:i:s');
26 error_log($timestamp . ' ' . $message . "\n", 3, $log_filepath);
30 * ctype_digit that supports PHP4+.
32 * PHP official document says PHP4 has ctype_digit() function.
33 * But sometimes it doen't exists on PHP 4.1.
35 function pkwk_ctype_digit($s) {
36 static $ctype_digit_exists;
37 if (!isset($ctype_digit_exists)) {
38 $ctype_digit_exists = function_exists('ctype_digit');
40 if ($ctype_digit_exists) {
41 return ctype_digit($s);
43 return preg_match('/^[0-9]+$/', $s) ? true : false;
46 function is_interwiki($str)
48 global $InterWikiName;
49 return preg_match('/^' . $InterWikiName . '$/', $str);
52 function is_pagename($str)
56 $is_pagename = (! is_interwiki($str) &&
57 preg_match('/^(?!\/)' . $BracketName . '$(?<!\/$)/', $str) &&
58 ! preg_match('#(^|/)\.{1,2}(/|$)#', $str));
60 if (defined('SOURCE_ENCODING')) {
61 switch(SOURCE_ENCODING){
62 case 'UTF-8': $pattern =
63 '/^(?:[\x00-\x7F]|(?:[\xC0-\xDF][\x80-\xBF])|(?:[\xE0-\xEF][\x80-\xBF][\x80-\xBF]))+$/';
65 case 'EUC-JP': $pattern =
66 '/^(?:[\x00-\x7F]|(?:[\x8E\xA1-\xFE][\xA1-\xFE])|(?:\x8F[\xA1-\xFE][\xA1-\xFE]))+$/';
69 if (isset($pattern) && $pattern != '')
70 $is_pagename = ($is_pagename && preg_match($pattern, $str));
76 function is_url($str, $only_http = FALSE)
78 $scheme = $only_http ? 'https?' : 'https?|ftp|news';
79 return preg_match('/^(' . $scheme . ')(:\/\/[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]*)$/', $str);
83 function is_page($page, $clearcache = FALSE)
85 if ($clearcache) clearstatcache();
86 return file_exists(get_filename($page));
89 function is_editable($page)
92 static $is_editable = array();
94 if (! isset($is_editable[$page])) {
95 $is_editable[$page] = (
98 ! in_array($page, $cantedit)
102 return $is_editable[$page];
105 function is_freeze($page, $clearcache = FALSE)
107 global $function_freeze;
108 static $is_freeze = array();
110 if ($clearcache === TRUE) $is_freeze = array();
111 if (isset($is_freeze[$page])) return $is_freeze[$page];
113 if (! $function_freeze || ! is_page($page)) {
114 $is_freeze[$page] = FALSE;
117 $fp = fopen(get_filename($page), 'rb') or
118 die('is_freeze(): fopen() failed: ' . htmlsc($page));
119 flock($fp, LOCK_SH) or die('is_freeze(): flock() failed');
121 $buffer = fread($fp, 1000);
122 flock($fp, LOCK_UN) or die('is_freeze(): flock() failed');
123 fclose($fp) or die('is_freeze(): fclose() failed: ' . htmlsc($page));
124 $is_freeze[$page] = (bool) preg_match('/^#freeze$/m', $buffer);
125 return $is_freeze[$page];
129 // Handling $non_list
130 // $non_list will be preg_quote($str, '/') later.
131 function check_non_list($page = '')
136 if (! isset($regex)) $regex = '/' . $non_list . '/';
138 return preg_match($regex, $page);
142 function auto_template($page)
144 global $auto_template_func, $auto_template_rules;
146 if (! $auto_template_func) return '';
150 foreach ($auto_template_rules as $rule => $template) {
151 $rule_pattrn = '/' . $rule . '/';
153 if (! preg_match($rule_pattrn, $page, $matches)) continue;
155 $template_page = preg_replace($rule_pattrn, $template, $page);
156 if (! is_page($template_page)) continue;
158 $body = join('', get_source($template_page));
160 // Remove fixed-heading anchors
161 $body = preg_replace('/^(\*{1,3}.*)\[#[A-Za-z][\w-]+\](.*)$/m', '$1$2', $body);
164 $body = preg_replace('/^#freeze\s*$/m', '', $body);
166 $count = count($matches);
167 for ($i = 0; $i < $count; $i++)
168 $body = str_replace('$' . $i, $matches[$i], $body);
175 // Expand all search-words to regexes and push them into an array
176 function get_search_words($words = array(), $do_escape = FALSE)
178 static $init, $mb_convert_kana, $pre, $post, $quote = '/';
180 if (! isset($init)) {
181 // function: mb_convert_kana() is for Japanese code only
182 if (LANG == 'ja' && function_exists('mb_convert_kana')) {
183 $mb_convert_kana = create_function('$str, $option',
184 'return mb_convert_kana($str, $option, SOURCE_ENCODING);');
186 $mb_convert_kana = create_function('$str, $option',
189 if (SOURCE_ENCODING == 'EUC-JP') {
190 // Perl memo - Correct pattern-matching with EUC-JP
191 // http://www.din.or.jp/~ohzaki/perl.htm#JP_Match (Japanese)
193 $post = '(?=(?:[\xA1-\xFE][\xA1-\xFE])*' . // JIS X 0208
194 '(?:[\x00-\x7F\x8E\x8F]|\z))'; // ASCII, SS2, SS3, or the last
201 if (! is_array($words)) $words = array($words);
203 // Generate regex for the words
205 foreach ($words as $word) {
207 if ($word == '') continue;
209 // Normalize: ASCII letters = to single-byte. Others = to Zenkaku and Katakana
210 $word_nm = $mb_convert_kana($word, 'aKCV');
211 $nmlen = mb_strlen($word_nm, SOURCE_ENCODING);
213 // Each chars may be served ...
215 for ($pos = 0; $pos < $nmlen; $pos++) {
216 $char = mb_substr($word_nm, $pos, 1, SOURCE_ENCODING);
218 // Just normalized one? (ASCII char or Zenkaku-Katakana?)
219 $or = array(preg_quote($do_escape ? htmlsc($char) : $char, $quote));
220 if (strlen($char) == 1) {
221 // An ASCII (single-byte) character
222 foreach (array(strtoupper($char), strtolower($char)) as $_char) {
223 if ($char != '&') $or[] = preg_quote($_char, $quote); // As-is?
224 $ascii = ord($_char);
225 $or[] = sprintf('&#(?:%d|x%x);', $ascii, $ascii); // As an entity reference?
226 $or[] = preg_quote($mb_convert_kana($_char, 'A'), $quote); // As Zenkaku?
229 // NEVER COME HERE with mb_substr(string, start, length, 'ASCII')
230 // A multi-byte character
231 $or[] = preg_quote($mb_convert_kana($char, 'c'), $quote); // As Hiragana?
232 $or[] = preg_quote($mb_convert_kana($char, 'k'), $quote); // As Hankaku-Katakana?
234 $chars[] = '(?:' . join('|', array_unique($or)) . ')'; // Regex for the character
237 $regex[$word] = $pre . join('', $chars) . $post; // For the word
240 return $regex; // For all words
243 // 'Search' main function
244 function do_search($word, $type = 'AND', $non_format = FALSE, $base = '')
246 global $script, $whatsnew, $non_list, $search_non_list;
247 global $_msg_andresult, $_msg_orresult, $_msg_notfoundresult;
248 global $search_auth, $show_passage;
252 $b_type = ($type == 'AND'); // AND:TRUE OR:FALSE
253 $keys = get_search_words(preg_split('/\s+/', $word, -1, PREG_SPLIT_NO_EMPTY));
254 foreach ($keys as $key=>$value)
255 $keys[$key] = '/' . $value . '/S';
257 $pages = get_existpages();
261 $pages = preg_grep('/^' . preg_quote($base, '/') . '/S', $pages);
263 if (! $search_non_list) {
264 $pages = array_diff($pages, preg_grep('/' . $non_list . '/S', $pages));
266 $pages = array_flip($pages);
267 unset($pages[$whatsnew]);
269 $count = count($pages);
270 foreach (array_keys($pages) as $page) {
273 // Search for page name
275 foreach ($keys as $key) {
276 $b_match = preg_match($key, $page);
277 if ($b_type xor $b_match) break; // OR
279 if ($b_match) continue;
282 // Search auth for page contents
283 if ($search_auth && ! check_readable($page, false, false)) {
284 unset($pages[$page]);
288 // Search for page contents
289 foreach ($keys as $key) {
290 $b_match = preg_match($key, get_source($page, TRUE, TRUE));
291 if ($b_type xor $b_match) break; // OR
293 if ($b_match) continue;
295 unset($pages[$page]); // Miss
297 if ($non_format) return array_keys($pages);
299 $r_word = rawurlencode($word);
300 $s_word = htmlsc($word);
302 return str_replace('$1', $s_word, $_msg_notfoundresult);
304 ksort($pages, SORT_STRING);
306 $retval = '<ul>' . "\n";
307 foreach (array_keys($pages) as $page) {
308 $r_page = rawurlencode($page);
309 $s_page = htmlsc($page);
310 $passage = $show_passage ? ' ' . get_passage(get_filetime($page)) : '';
311 $retval .= ' <li><a href="' . $script . '?cmd=read&page=' .
312 $r_page . '&word=' . $r_word . '">' . $s_page .
313 '</a>' . $passage . '</li>' . "\n";
315 $retval .= '</ul>' . "\n";
317 $retval .= str_replace('$1', $s_word, str_replace('$2', count($pages),
318 str_replace('$3', $count, $b_type ? $_msg_andresult : $_msg_orresult)));
323 // Argument check for program
324 function arg_check($str)
327 return isset($vars['cmd']) && (strpos($vars['cmd'], $str) === 0);
330 function _pagename_urlencode_callback($matches)
332 return rawurlencode($matches[0]);
335 function pagename_urlencode($page)
337 return preg_replace_callback('|[^/:]+|', '_pagename_urlencode_callback', $page);
341 function encode($str)
344 return ($str == '') ? '' : strtoupper(bin2hex($str));
345 // Equal to strtoupper(join('', unpack('H*0', $key)));
346 // But PHP 4.3.10 says 'Warning: unpack(): Type H: outside of string in ...'
350 function decode($str)
352 return pkwk_hex2bin($str);
355 // Inversion of bin2hex()
356 function pkwk_hex2bin($hex_string)
358 // preg_match : Avoid warning : pack(): Type H: illegal hex digit ...
359 // (string) : Always treat as string (not int etc). See BugTrack2/31
360 return preg_match('/^[0-9a-f]+$/i', $hex_string) ?
361 pack('H*', (string)$hex_string) : $hex_string;
364 // Remove [[ ]] (brackets)
365 function strip_bracket($str)
368 if (preg_match('/^\[\[(.*)\]\]$/', $str, $match)) {
375 // Create list of pages
376 function page_list($pages, $cmd = 'read', $withfilename = FALSE)
378 global $script, $list_index;
379 global $_msg_symbol, $_msg_other;
380 global $pagereading_enable;
382 // ソートキーを決定する。 ' ' < '[a-zA-Z]' < 'zz'という前提。
388 if($pagereading_enable) {
389 mb_regex_encoding(SOURCE_ENCODING);
390 $readings = get_readings($pages);
393 $list = $matches = array();
395 // Shrink URI for read
396 if ($cmd == 'read') {
397 $href = $script . '?';
399 $href = $script . '?cmd=' . $cmd . '&page=';
402 foreach($pages as $file=>$page) {
403 $r_page = pagename_urlencode($page);
404 $s_page = htmlsc($page, ENT_QUOTES);
405 $passage = get_pg_passage($page);
407 $str = ' <li><a href="' . $href . $r_page . '">' .
408 $s_page . '</a>' . $passage;
411 $s_file = htmlsc($file);
412 $str .= "\n" . ' <ul><li>' . $s_file . '</li></ul>' .
417 // WARNING: Japanese code hard-wired
418 if($pagereading_enable) {
419 if(mb_ereg('^([A-Za-z])', mb_convert_kana($page, 'a'), $matches)) {
420 $head = strtoupper($matches[1]);
421 } elseif (isset($readings[$page]) && mb_ereg('^([ァ-ヶ])', $readings[$page], $matches)) { // here
423 } elseif (mb_ereg('^[ -~]|[^ぁ-ん亜-熙]', $page)) { // and here
429 $head = (preg_match('/^([A-Za-z])/', $page, $matches)) ? strtoupper($matches[1]) :
430 (preg_match('/^([ -~])/', $page) ? $symbol : $other);
433 $list[$head][$page] = $str;
435 uksort($pages, 'strnatcmp');
438 $arr_index = array();
439 $retval .= '<ul>' . "\n";
440 foreach ($list as $head=>$pages) {
441 if ($head === $symbol) {
442 $head = $_msg_symbol;
443 } else if ($head === $other) {
449 $arr_index[] = '<a id="top_' . $cnt .
450 '" href="#head_' . $cnt . '"><strong>' .
451 $head . '</strong></a>';
452 $retval .= ' <li><a id="head_' . $cnt . '" href="#top_' . $cnt .
453 '"><strong>' . $head . '</strong></a>' . "\n" .
456 ksort($pages, SORT_STRING);
457 $retval .= join("\n", $pages);
459 $retval .= "\n </ul>\n </li>\n";
461 $retval .= '</ul>' . "\n";
462 if ($list_index && $cnt > 0) {
464 while (! empty($arr_index))
465 $top[] = join(' | ' . "\n", array_splice($arr_index, 0, 16)) . "\n";
467 $retval = '<div id="top" style="text-align:center">' . "\n" .
468 join('<br />', $top) . '</div>' . "\n" . $retval;
473 // Show text formatting rules
478 if (! is_page($rule_page)) {
479 return '<p>Sorry, page \'' . htmlsc($rule_page) .
480 '\' unavailable.</p>';
482 return convert_html(get_source($rule_page));
486 // Show (critical) error message
487 function die_message($msg)
489 $title = $page = 'Runtime error';
491 <h3>Runtime error</h3>
492 <strong>Error message : $msg</strong>
495 pkwk_common_headers();
496 if(defined('SKIN_FILE') && file_exists(SKIN_FILE) && is_readable(SKIN_FILE)) {
497 catbody($title, $page, $body);
500 if(defined('CONTENT_CHARSET')) {
501 $charset = CONTENT_CHARSET;
503 header("Content-Type: text/html; charset=$charset");
508 <meta http-equiv="content-type" content="text/html; charset=$charset">
509 <title>$title</title>
520 // Have the time (as microtime)
521 function getmicrotime()
523 list($usec, $sec) = explode(' ', microtime());
524 return ((float)$sec + (float)$usec);
527 // Elapsed time by second
528 //define('MUTIME', getmicrotime());
529 function elapsedtime()
531 $at_the_microtime = MUTIME;
532 return sprintf('%01.03f', getmicrotime() - $at_the_microtime);
536 function get_date($format, $timestamp = NULL)
538 $format = preg_replace('/(?<!\\\)T/',
539 preg_replace('/(.)/', '\\\$1', ZONE), $format);
541 $time = ZONETIME + (($timestamp !== NULL) ? $timestamp : UTIME);
543 return date($format, $time);
546 // Format date string
547 function format_date($val, $paren = FALSE)
549 global $date_format, $time_format, $weeklabels;
553 $date = date($date_format, $val) .
554 ' (' . $weeklabels[date('w', $val)] . ') ' .
555 date($time_format, $val);
557 return $paren ? '(' . $date . ')' : $date;
560 // Get short string of the passage, 'N seconds/minutes/hours/days/years ago'
561 function get_passage($time, $paren = TRUE)
563 static $units = array('m'=>60, 'h'=>24, 'd'=>1);
565 $time = max(0, (UTIME - $time) / 60); // minutes
567 foreach ($units as $unit=>$card) {
568 if ($time < $card) break;
571 $time = floor($time) . $unit;
573 return $paren ? '(' . $time . ')' : $time;
576 // Hide <input type="(submit|button|image)"...>
577 function drop_submit($str)
579 return preg_replace('/<input([^>]+)type="(submit|button|image)"/i',
580 '<input$1type="hidden"', $str);
583 // Generate AutoLink patterns (thx to hirofummy)
584 function get_autolink_pattern(& $pages)
586 global $WikiName, $autolink, $nowikiname;
588 $config = new Config('AutoLink');
590 $ignorepages = $config->get('IgnoreList');
591 $forceignorepages = $config->get('ForceIgnoreList');
593 $auto_pages = array_merge($ignorepages, $forceignorepages);
595 foreach ($pages as $page)
596 if (preg_match('/^' . $WikiName . '$/', $page) ?
597 $nowikiname : strlen($page) >= $autolink)
598 $auto_pages[] = $page;
600 if (empty($auto_pages)) {
601 $result = $result_a = $nowikiname ? '(?!)' : $WikiName;
603 $auto_pages = array_unique($auto_pages);
604 sort($auto_pages, SORT_STRING);
606 $auto_pages_a = array_values(preg_grep('/^[A-Z]+$/i', $auto_pages));
607 $auto_pages = array_values(array_diff($auto_pages, $auto_pages_a));
609 $result = get_autolink_pattern_sub($auto_pages, 0, count($auto_pages), 0);
610 $result_a = get_autolink_pattern_sub($auto_pages_a, 0, count($auto_pages_a), 0);
612 return array($result, $result_a, $forceignorepages);
615 function get_autolink_pattern_sub(& $pages, $start, $end, $pos)
617 if ($end == 0) return '(?!)';
620 $count = $i = $j = 0;
621 $x = (mb_strlen($pages[$start]) <= $pos);
624 for ($i = $start; $i < $end; $i = $j) {
625 $char = mb_substr($pages[$i], $pos, 1);
626 for ($j = $i; $j < $end; $j++)
627 if (mb_substr($pages[$j], $pos, 1) != $char) break;
629 if ($i != $start) $result .= '|';
630 if ($i >= ($j - 1)) {
631 $result .= str_replace(' ', '\\ ', preg_quote(mb_substr($pages[$i], $pos), '/'));
633 $result .= str_replace(' ', '\\ ', preg_quote($char, '/')) .
634 get_autolink_pattern_sub($pages, $i, $j, $pos + 1);
638 if ($x || $count > 1) $result = '(?:' . $result . ')';
639 if ($x) $result .= '?';
644 // Get absolute-URI of this script
645 function get_script_uri($init_uri = '')
647 global $script_directory_index;
650 if ($init_uri == '') {
652 if (isset($script)) return $script;
655 $msg = 'get_script_uri() failed: Please set $script at INI_FILE manually';
657 $script = (SERVER_PORT == 443 ? 'https://' : 'http://'); // scheme
658 $script .= SERVER_NAME; // host
659 $script .= (SERVER_PORT == 80 ? '' : ':' . SERVER_PORT); // port
661 // SCRIPT_NAME が'/'で始まっていない場合(cgiなど) REQUEST_URIを使ってみる
663 if ($path{0} != '/') {
664 if (! isset($_SERVER['REQUEST_URI']) || $_SERVER['REQUEST_URI']{0} != '/')
667 // REQUEST_URIをパースし、path部分だけを取り出す
668 $parse_url = parse_url($script . $_SERVER['REQUEST_URI']);
669 if (! isset($parse_url['path']) || $parse_url['path']{0} != '/')
672 $path = $parse_url['path'];
676 if (! is_url($script, TRUE) && php_sapi_name() == 'cgi')
682 if (isset($script)) die_message('$script: Already init');
683 if (! is_url($init_uri, TRUE)) die_message('$script: Invalid URI');
687 // Cut filename or not
688 if (isset($script_directory_index)) {
689 if (! file_exists($script_directory_index))
690 die_message('Directory index file not found: ' .
691 htmlsc($script_directory_index));
693 if (preg_match('#^(.+/)' . preg_quote($script_directory_index, '#') . '$#',
694 $script, $matches)) $script = $matches[1];
700 // Remove null(\0) bytes from variables
702 // NOTE: PHP had vulnerabilities that opens "hoge.php" via fopen("hoge.php\0.txt") etc.
703 // [PHP-users 12736] null byte attack
704 // http://ns1.php.gr.jp/pipermail/php-users/2003-January/012742.html
706 // 2003-05-16: magic quotes gpcの復元処理を統合
707 // 2003-05-21: 連想配列のキーはbinary safe
709 function input_filter($param)
711 static $magic_quotes_gpc = NULL;
712 if ($magic_quotes_gpc === NULL)
713 $magic_quotes_gpc = get_magic_quotes_gpc();
715 if (is_array($param)) {
716 return array_map('input_filter', $param);
718 $result = str_replace("\0", '', $param);
719 if ($magic_quotes_gpc) $result = stripslashes($result);
724 // Compat for 3rd party plugins. Remove this later
725 function sanitize($param) {
726 return input_filter($param);
729 // Explode Comma-Separated Values to an array
730 function csv_explode($separator, $string)
732 $retval = $matches = array();
734 $_separator = preg_quote($separator, '/');
735 if (! preg_match_all('/("[^"]*(?:""[^"]*)*"|[^' . $_separator . ']*)' .
736 $_separator . '/', $string . $separator, $matches))
739 foreach ($matches[1] as $str) {
741 if ($len > 1 && $str{0} == '"' && $str{$len - 1} == '"')
742 $str = str_replace('""', '"', substr($str, 1, -1));
748 // Implode an array with CSV data format (escape double quotes)
749 function csv_implode($glue, $pieces)
751 $_glue = ($glue != '') ? '\\' . $glue{0} : '';
753 foreach ($pieces as $str) {
754 if (preg_match('/[' . '"' . "\n\r" . $_glue . ']/', $str))
755 $str = '"' . str_replace('"', '""', $str) . '"';
758 return join($glue, $arr);
761 // Sugar with default settings
762 function htmlsc($string = '', $flags = ENT_COMPAT, $charset = CONTENT_CHARSET)
764 return htmlspecialchars($string, $flags, $charset); // htmlsc()
768 * Get redirect page name on Page Redirect Rules
770 * This function returns exactly false if it doesn't need redirection.
771 * So callers need check return value is false or not.
773 * @param $page page name
774 * @return new page name or false
776 function get_pagename_on_redirect($page) {
777 global $page_redirect_rules;
778 foreach ($page_redirect_rules as $rule=>$replace) {
779 if (preg_match($rule, $page)) {
780 if (is_string($replace)) {
781 $new_page = preg_replace($rule, $replace, $page);
782 } elseif (is_object($replace) && is_callable($replace)) {
783 $new_page = preg_replace_callback($rule, $replace, $page);
785 die_message('Invalid redirect rule: ' . $rule . '=>' . $replace);
787 if ($page !== $new_page) {
796 * Redirect from an old page to new page
798 * This function returns true when a redirection occurs.
799 * So callers need check return value is false or true.
800 * And if it is true, then you have to exit PHP script.
802 * @return bool Inticates a redirection occurred or not
804 function manage_page_redirect() {
806 if (isset($vars['page'])) {
807 $page = $vars['page'];
809 $new_page = get_pagename_on_redirect($page);
810 if ($new_page != false) {
811 header('Location: ' . get_script_uri() . '?' .
812 pagename_urlencode($new_page));
820 // is_a -- Returns TRUE if the object is of this class or has this class as one of its parents
822 if (! function_exists('is_a')) {
824 function is_a($class, $match)
826 if (empty($class)) return FALSE;
828 $class = is_object($class) ? get_class($class) : $class;
829 if (strtolower($class) == strtolower($match)) {
832 return is_a(get_parent_class($class), $match); // Recurse
837 // array_fill -- Fill an array with values
839 if (! function_exists('array_fill')) {
841 function array_fill($start_index, $num, $value)
844 while ($num-- > 0) $ret[$start_index++] = $value;
849 // md5_file -- Calculates the md5 hash of a given filename
851 if (! function_exists('md5_file')) {
853 function md5_file($filename)
855 if (! file_exists($filename)) return FALSE;
857 $fd = fopen($filename, 'rb');
858 if ($fd === FALSE ) return FALSE;
859 $data = fread($fd, filesize($filename));
865 // sha1 -- Compute SHA-1 hash
866 // (PHP 4 >= 4.3.0, PHP5)
867 if (! function_exists('sha1')) {
868 if (extension_loaded('mhash')) {
871 return bin2hex(mhash(MHASH_SHA1, $str));