OSDN Git Service

UTF-8 supporting hack
[pukiwiki/pukiwiki.git] / lib / init.php
index 069ebd8..aed2233 100644 (file)
@@ -1,23 +1,27 @@
 <?php
-/////////////////////////////////////////////////
 // PukiWiki - Yet another WikiWikiWeb clone.
+// $Id: init.php,v 1.45 2006/05/30 14:53:39 henoheno Exp $
+// Copyright (C)
+//   2002-2006 PukiWiki Developers Team
+//   2001-2002 Originally written by yu-ji
+// License: GPL v2 or (at your option) any later version
 //
-// $Id: init.php,v 1.16 2004/10/17 13:29:52 henoheno Exp $
-//
+// Init PukiWiki here
 
-/////////////////////////////////////////////////
-// ¥Ð¡¼¥¸¥ç¥ó / Ãøºî¸¢
-define('S_VERSION', '1.4.4');
+// PukiWiki version / Copyright / Licence
+
+define('S_VERSION', '1.4.7_rc1');
 define('S_COPYRIGHT',
        '<strong>PukiWiki ' . S_VERSION . '</strong>' .
-       ' Copyright &copy; 2001-2004' .
-       ' <a href="http://pukiwiki.org">PukiWiki Developers Team</a>.' .
+       ' Copyright &copy; 2001-2006' .
+       ' <a href="http://pukiwiki.sourceforge.jp/">PukiWiki Developers Team</a>.' .
        ' License is <a href="http://www.gnu.org/licenses/gpl.html">GPL</a>.<br />' .
        ' Based on "PukiWiki" 1.3 by <a href="http://factage.com/yu-ji/">yu-ji</a>'
 );
 
 /////////////////////////////////////////////////
-// ½é´üÀßÄê (¥µ¡¼¥ÐÊÑ¿ô)
+// Init server variables
+
 foreach (array('SCRIPT_NAME', 'SERVER_ADMIN', 'SERVER_NAME',
        'SERVER_PORT', 'SERVER_SOFTWARE') as $key) {
        define($key, isset($_SERVER[$key]) ? $_SERVER[$key] : '');
@@ -25,26 +29,26 @@ foreach (array('SCRIPT_NAME', 'SERVER_ADMIN', 'SERVER_NAME',
 }
 
 /////////////////////////////////////////////////
-// ½é´üÀßÄê (¥°¥í¡¼¥Ð¥ëÊÑ¿ô)
+// Init grobal variables
 
-$foot_explain = array();       // µÓÃí
-$related      = array();       // ´ØÏ¢¤¹¤ë¥Ú¡¼¥¸
-$head_tags    = array();       // <head>Æâ¤ËÄɲ乤륿¥°
+$foot_explain = array();       // Footnotes
+$related      = array();       // Related pages
+$head_tags    = array();       // XHTML tags in <head></head>
 
 /////////////////////////////////////////////////
-// ½é´üÀßÄê(»þ´Ö)
+// Time settings
+
 define('LOCALZONE', date('Z'));
 define('UTIME', time() - LOCALZONE);
 define('MUTIME', getmicrotime());
 
 /////////////////////////////////////////////////
-// ½é´üÀßÄê(ÀßÄê¥Õ¥¡¥¤¥ë¤Î¾ì½ê)
-define('INI_FILE',  DATA_HOME . 'pukiwiki.ini.php');
+// Require INI_FILE
 
-// ¥Õ¥¡¥¤¥ëÆɤ߹þ¤ß
+define('INI_FILE',  DATA_HOME . 'pukiwiki.ini.php');
 $die = '';
 if (! file_exists(INI_FILE) || ! is_readable(INI_FILE)) {
-       $die = "${die}File is not found. (INI_FILE)\n";
+       $die .= 'File is not found. (INI_FILE)' . "\n";
 } else {
        require(INI_FILE);
 }
@@ -53,24 +57,37 @@ if ($die) die_message(nl2br("\n\n" . $die));
 /////////////////////////////////////////////////
 // INI_FILE: LANG ¤Ë´ð¤Å¤¯¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°ÀßÄê
 
+// MB_LANGUAGE: mb_language (for mbstring extension)
+//   'uni'(means UTF-8), 'English', or 'Japanese'
+// SOURCE_ENCODING: Internal content encoding (for mbstring extension)
+//   'UTF-8', 'ASCII', or 'EUC-JP'
+// CONTENT_CHARSET: Internal content encoding = Output content charset (for skin)
+//   'UTF-8', 'iso-8859-1', 'EUC-JP' or ...
+
 switch (LANG){
-case 'en':
-       // Internal content encoding = Output content charset (for skin)
-       define('CONTENT_CHARSET', 'iso-8859-1'); // 'UTF-8', 'iso-8859-1', 'EUC-JP' or ...
-       // mb_language (for mbstring extension)
-       define('MB_LANGUAGE',   'English');     // 'uni'(means UTF-8), 'English', or 'Japanese'
-       // Internal content encoding (for mbstring extension)
-       define('SOURCE_ENCODING', 'ASCII');     // 'UTF-8', 'ASCII', or 'EUC-JP'
-       break;
-       
-case 'ja': // EUC-JP
-       define('CONTENT_CHARSET', 'EUC-JP');
-       define('MB_LANGUAGE',   'Japanese');
-       define('SOURCE_ENCODING', 'EUC-JP');
-       break;
-
-default:
-       die_message('No such language "' . LANG . '"');
+case 'en': define('MB_LANGUAGE', 'English' ); break;
+case 'ja': define('MB_LANGUAGE', 'Japanese'); break;
+//UTF-8:case 'ko': define('MB_LANGUAGE', 'Korean'  ); break;
+//UTF-8:       // See BugTrack2/13 for all hack about Korean support,
+//UTF-8:       // and give us your report!
+default: die_message('No such language "' . LANG . '"'); break;
+}
+
+//UTF-8:define('PKWK_UTF8_ENABLE', 1);
+if (defined('PKWK_UTF8_ENABLE')) {
+       define('SOURCE_ENCODING', 'UTF-8');
+       define('CONTENT_CHARSET', 'UTF-8');
+} else {
+       switch (LANG){
+       case 'en':
+               define('SOURCE_ENCODING', 'ASCII');
+               define('CONTENT_CHARSET', 'iso-8859-1');
+               break;
+       case 'ja':
+               define('SOURCE_ENCODING', 'EUC-JP');
+               define('CONTENT_CHARSET', 'EUC-JP');
+               break;
+       }
 }
 
 mb_language(MB_LANGUAGE);
@@ -80,32 +97,38 @@ mb_http_output('pass');
 mb_detect_order('auto');
 
 /////////////////////////////////////////////////
-// INI_FILE: UI_LANG ½é´üÀßÄê(¸À¸ì¥Õ¥¡¥¤¥ë¤Î¾ì½ê)
-define('LANG_FILE', DATA_HOME . UI_LANG . '.lng');
+// INI_FILE: Require LANG_FILE
 
-// ¥Õ¥¡¥¤¥ëÆɤ߹þ¤ß
+define('LANG_FILE_HINT', DATA_HOME . LANG . '.lng.php');       // For encoding hint
+define('LANG_FILE',      DATA_HOME . UI_LANG . '.lng.php');    // For UI resource
 $die = '';
-if (! file_exists(LANG_FILE) || ! is_readable(LANG_FILE)) {
-       $die = "${die}File is not found. (LANG_FILE)\n";
-} else {
-       require(LANG_FILE);
+foreach (array('LANG_FILE_HINT', 'LANG_FILE') as $langfile) {
+       if (! file_exists(constant($langfile)) || ! is_readable(constant($langfile))) {
+               $die .= 'File is not found or not readable. (' . $langfile . ')' . "\n";
+       } else {
+               require_once(constant($langfile));
+       }
 }
 if ($die) die_message(nl2br("\n\n" . $die));
 
 /////////////////////////////////////////////////
-// LANG_FILE: ÍËÆüÇÛÎó
+// LANG_FILE: Init encoding hint
+
+define('PKWK_ENCODING_HINT', isset($_LANG['encode_hint'][LANG]) ? $_LANG['encode_hint'][LANG] : '');
+unset($_LANG['encode_hint']);
+
+/////////////////////////////////////////////////
+// LANG_FILE: Init severn days of the week
 
 $weeklabels = $_msg_week;
 
 /////////////////////////////////////////////////
-// INI_FILE: $script: ½é´üÀßÄê
+// INI_FILE: Init $script
 
 if (isset($script)) {
-       // Init manually
-       get_script_uri($script);
+       get_script_uri($script); // Init manually
 } else {
-       // Init automatically
-       $script = get_script_uri();
+       $script = get_script_uri(); // Init automatically
 }
 
 /////////////////////////////////////////////////
@@ -147,27 +170,27 @@ unset($user_agent);       // Unset after reading UA_INI_FILE
 $die = '';
 foreach(array('DATA_DIR', 'DIFF_DIR', 'BACKUP_DIR', 'CACHE_DIR') as $dir){
        if (! is_writable(constant($dir)))
-               $die = "${die}Directory is not found or not writable ($dir)\n";
+               $die .= 'Directory is not found or not writable (' . $dir . ')' . "\n";
 }
 
 // ÀßÄê¥Õ¥¡¥¤¥ë¤ÎÊÑ¿ô¥Á¥§¥Ã¥¯
 $temp = '';
 foreach(array('rss_max', 'page_title', 'note_hr', 'related_link', 'show_passage',
        'rule_related_str', 'load_template_func') as $var){
-       if (! isset(${$var})) $temp .= "\$$var\n";
+       if (! isset(${$var})) $temp .= '$' . $var . "\n";
 }
 if ($temp) {
        if ($die) $die .= "\n"; // A breath
-       $die .= "Variable(s) not found: (Maybe the old *.ini.php?)\n" . $temp;
+       $die .= 'Variable(s) not found: (Maybe the old *.ini.php?)' . "\n" . $temp;
 }
 
 $temp = '';
 foreach(array('LANG', 'PLUGIN_DIR') as $def){
-       if (! defined($def)) $temp .= "$def\n";
+       if (! defined($def)) $temp .= $def . "\n";
 }
 if ($temp) {
        if ($die) $die .= "\n"; // A breath
-       $die .= "Define(s) not found: (Maybe the old *.ini.php?)\n" . $temp;
+       $die .= 'Define(s) not found: (Maybe the old *.ini.php?)' . "\n" . $temp;
 }
 
 if($die) die_message(nl2br("\n\n" . $die));
@@ -185,7 +208,7 @@ foreach(array($defaultpage, $whatsnew, $interwiki) as $page){
 
 // Prohibit $_GET attack
 foreach (array('msg', 'pass') as $key) {
-       if (isset($_GET[$key])) die_message("Sorry, already reserved: $key=");
+       if (isset($_GET[$key])) die_message('Sorry, already reserved: ' . $key . '=');
 }
 
 // Expire risk
@@ -202,7 +225,7 @@ $_COOKIE = input_filter($_COOKIE);
 // POST method ¤Ï¾ï¤Ë form ·Ðͳ¤Ê¤Î¤Ç¡¢É¬¤ºÊÑ´¹¤¹¤ë
 //
 if (isset($_POST['encode_hint']) && $_POST['encode_hint'] != '') {
-       // html.php ¤ÎÃæ¤Ç¡¢<form> ¤Ë encode_hint ¤ò»Å¹þ¤ó¤Ç¤¤¤ë¤Î¤Ç¡¢
+       // do_plugin_xxx() ¤ÎÃæ¤Ç¡¢<form> ¤Ë encode_hint ¤ò»Å¹þ¤ó¤Ç¤¤¤ë¤Î¤Ç¡¢
        // encode_hint ¤òÍѤ¤¤Æ¥³¡¼¥É¸¡½Ð¤¹¤ë¡£
        // Á´ÂΤò¸«¤Æ¥³¡¼¥É¸¡½Ð¤¹¤ë¤È¡¢µ¡¼ï°Í¸ʸ»ú¤ä¡¢Ì¯¤Ê¥Ð¥¤¥Ê¥ê
        // ¥³¡¼¥É¤¬º®Æþ¤·¤¿¾ì¹ç¤Ë¡¢¥³¡¼¥É¸¡½Ð¤Ë¼ºÇÔ¤¹¤ë¶²¤ì¤¬¤¢¤ë¡£
@@ -242,9 +265,16 @@ if (isset($_GET['encode_hint']) && $_GET['encode_hint'] != '')
 // ¥Ú¡¼¥¸Ì¾¤«InterWikiName¤Ç¤¢¤ë¤È¤ß¤Ê¤¹
 $arg = '';
 if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING']) {
-       $arg = $_SERVER['QUERY_STRING'];
-} else if (isset($_SERVER['argv']) && count($_SERVER['argv'])) {
-       $arg = $_SERVER['argv'][0];
+       $arg = & $_SERVER['QUERY_STRING'];
+} else if (isset($_SERVER['argv']) && ! empty($_SERVER['argv'])) {
+       $arg = & $_SERVER['argv'][0];
+}
+if (PKWK_QUERY_STRING_MAX && strlen($arg) > PKWK_QUERY_STRING_MAX) {
+       // Something nasty attack?
+       pkwk_common_headers();
+       sleep(1);       // Fake processing, and/or process other threads
+       echo('Query string too long');
+       exit;
 }
 $arg = input_filter($arg); // \0 ½üµî
 
@@ -289,6 +319,10 @@ if (empty($_POST)) {
        $vars = array_merge($_GET, $_POST); // Considered reliable than $_REQUEST
 }
 
+// ÆþÎÏ¥Á¥§¥Ã¥¯: 'cmd=' and 'plugin=' can't live together
+if (isset($vars['cmd']) && isset($vars['plugin']))
+       die('Using both cmd= and plugin= is not allowed');
+
 // ÆþÎÏ¥Á¥§¥Ã¥¯: cmd, plugin ¤Îʸ»úÎó¤Ï±Ñ¿ô»ú°Ê³°¤¢¤ê¤¨¤Ê¤¤
 foreach(array('cmd', 'plugin') as $var) {
        if (isset($vars[$var]) && ! preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $vars[$var]))
@@ -308,7 +342,8 @@ if (isset($vars['msg'])) {
 }
 
 // ¸åÊý¸ß´¹À­ (?md5=...)
-if (isset($vars['md5']) && $vars['md5'] != '') {
+if (isset($get['md5']) && $get['md5'] != '' &&
+    ! isset($vars['cmd']) && ! isset($vars['plugin'])) {
        $get['cmd'] = $post['cmd'] = $vars['cmd'] = 'md5';
 }
 
@@ -329,11 +364,6 @@ if (! isset($vars['cmd']) && ! isset($vars['plugin'])) {
        $get['page'] = $post['page'] = $vars['page'] = $arg;
 }
 
-// ÆþÎÏ¥Á¥§¥Ã¥¯: 'cmd=' prohibits nasty 'plugin='
-if (isset($vars['cmd']) && isset($vars['plugin']))
-       unset($get['plugin'], $post['plugin'], $vars['plugin']);
-
-
 /////////////////////////////////////////////////
 // ½é´üÀßÄê($WikiName,$BracketName¤Ê¤É)
 // $WikiName = '[A-Z][a-z]+(?:[A-Z][a-z]+)+';
@@ -348,7 +378,7 @@ $WikiName = '(?:[A-Z][a-z]+){2,}(?!\w)';
 $BracketName = '(?!\s):?[^\r\n\t\f\[\]<>#&":]+:?(?<!\s)';
 
 // InterWiki
-$InterWikiName = "(\[\[)?((?:(?!\s|:|\]\]).)+):(.+)(?(1)\]\])";
+$InterWikiName = '(\[\[)?((?:(?!\s|:|\]\]).)+):(.+)(?(1)\]\])';
 
 // Ãí¼á
 $NotePattern = '/\(\(((?:(?>(?:(?!\(\()(?!\)\)(?:[^\)]|$)).)+)|(?R))*)\)\)/ex';
@@ -363,10 +393,9 @@ require(DATA_HOME . 'rules.ini.php');
 // ¸½ºß»þ¹ï
 $now = format_date(UTIME);
 
-// skinÆâ¤ÇDTDÀë¸À¤òÀÚ¤êÂؤ¨¤ë¤Î¤Ë»ÈÍÑ¡£paint.inc.phpÂкö
-$html_transitional = FALSE;
-// FALSE:XHTML 1.1
-// TRUE :XHTML 1.0 Transitional
+// Æü»þÃÖ´¹¥ë¡¼¥ë¤ò$line_rules¤Ë²Ã¤¨¤ë
+if ($usedatetime) $line_rules += $datetime_rules;
+unset($datetime_rules);
 
 // ¥Õ¥§¥¤¥¹¥Þ¡¼¥¯¤ò$line_rules¤Ë²Ã¤¨¤ë
 if ($usefacemark) $line_rules += $facemark_rules;
@@ -378,9 +407,7 @@ $entity_pattern = trim(join('', file(CACHE_DIR . 'entities.dat')));
 
 $line_rules = array_merge(array(
        '&amp;(#[0-9]+|#x[0-9a-f]+|' . $entity_pattern . ');' => '&$1;',
-       "\r"          => "<br />\n",    /* ¹ÔËö¤Ë¥Á¥ë¥À¤Ï²þ¹Ô */
-       '#related$'   => '<del>#related</del>',
-       '^#contents$' => '<del>#contents</del>'
+       "\r"          => '<br />' . "\n",       /* ¹ÔËö¤Ë¥Á¥ë¥À¤Ï²þ¹Ô */
 ), $line_rules);
 
 ?>