2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // $Id: init.php,v 1.44 2006/05/15 14:47:42 henoheno Exp $
5 // 2002-2006 PukiWiki Developers Team
6 // 2001-2002 Originally written by yu-ji
7 // License: GPL v2 or (at your option) any later version
11 // PukiWiki version / Copyright / Licence
13 define('S_VERSION', '1.4.7_rc1');
15 '<strong>PukiWiki ' . S_VERSION . '</strong>' .
16 ' Copyright © 2001-2006' .
17 ' <a href="http://pukiwiki.sourceforge.jp/">PukiWiki Developers Team</a>.' .
18 ' License is <a href="http://www.gnu.org/licenses/gpl.html">GPL</a>.<br />' .
19 ' Based on "PukiWiki" 1.3 by <a href="http://factage.com/yu-ji/">yu-ji</a>'
22 /////////////////////////////////////////////////
23 // Init server variables
25 foreach (array('SCRIPT_NAME', 'SERVER_ADMIN', 'SERVER_NAME',
26 'SERVER_PORT', 'SERVER_SOFTWARE') as $key) {
27 define($key, isset($_SERVER[$key]) ? $_SERVER[$key] : '');
28 unset(${$key}, $_SERVER[$key], $HTTP_SERVER_VARS[$key]);
31 /////////////////////////////////////////////////
32 // Init grobal variables
34 $foot_explain = array(); // Footnotes
35 $related = array(); // Related pages
36 $head_tags = array(); // XHTML tags in <head></head>
38 /////////////////////////////////////////////////
41 define('LOCALZONE', date('Z'));
42 define('UTIME', time() - LOCALZONE);
43 define('MUTIME', getmicrotime());
45 /////////////////////////////////////////////////
48 define('INI_FILE', DATA_HOME . 'pukiwiki.ini.php');
50 if (! file_exists(INI_FILE) || ! is_readable(INI_FILE)) {
51 $die .= 'File is not found. (INI_FILE)' . "\n";
55 if ($die) die_message(nl2br("\n\n" . $die));
57 /////////////////////////////////////////////////
58 // INI_FILE: LANG ¤Ë´ð¤Å¤¯¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°ÀßÄê
60 // CONTENT_CHARSET: Internal content encoding = Output content charset (for skin)
61 // 'UTF-8', 'iso-8859-1', 'EUC-JP' or ...
63 // MB_LANGUAGE: mb_language (for mbstring extension)
64 // 'uni'(means UTF-8), 'English', or 'Japanese'
66 // SOURCE_ENCODING: Internal content encoding (for mbstring extension)
67 // 'UTF-8', 'ASCII', or 'EUC-JP'
72 define('CONTENT_CHARSET', 'iso-8859-1');
73 define('MB_LANGUAGE', 'English');
74 define('SOURCE_ENCODING', 'ASCII');
77 //define('CONTENT_CHARSET', 'UTF-8');
78 //define('MB_LANGUAGE', 'English');
79 //define('SOURCE_ENCODING', 'UTF-8');
85 define('CONTENT_CHARSET', 'EUC-JP');
86 define('MB_LANGUAGE', 'Japanese');
87 define('SOURCE_ENCODING', 'EUC-JP');
91 // UTF-8 (See BugTrack2/13 for all hack about Korean support, and give us your report!)
92 define('CONTENT_CHARSET', 'UTF-8');
93 define('MB_LANGUAGE', 'Korean');
94 define('SOURCE_ENCODING', 'UTF-8');
98 die_message('No such language "' . LANG . '"');
101 mb_language(MB_LANGUAGE);
102 mb_internal_encoding(SOURCE_ENCODING);
103 ini_set('mbstring.http_input', 'pass');
104 mb_http_output('pass');
105 mb_detect_order('auto');
107 /////////////////////////////////////////////////
108 // INI_FILE: Require LANG_FILE
110 define('LANG_FILE_HINT', DATA_HOME . LANG . '.lng.php'); // For encoding hint
111 define('LANG_FILE', DATA_HOME . UI_LANG . '.lng.php'); // For UI resource
113 foreach (array('LANG_FILE_HINT', 'LANG_FILE') as $langfile) {
114 if (! file_exists(constant($langfile)) || ! is_readable(constant($langfile))) {
115 $die .= 'File is not found or not readable. (' . $langfile . ')' . "\n";
117 require_once(constant($langfile));
120 if ($die) die_message(nl2br("\n\n" . $die));
122 /////////////////////////////////////////////////
123 // LANG_FILE: Init encoding hint
125 define('PKWK_ENCODING_HINT', isset($_LANG['encode_hint'][LANG]) ? $_LANG['encode_hint'][LANG] : '');
126 unset($_LANG['encode_hint']);
128 /////////////////////////////////////////////////
129 // LANG_FILE: Init severn days of the week
131 $weeklabels = $_msg_week;
133 /////////////////////////////////////////////////
134 // INI_FILE: Init $script
136 if (isset($script)) {
137 get_script_uri($script); // Init manually
139 $script = get_script_uri(); // Init automatically
142 /////////////////////////////////////////////////
143 // INI_FILE: $agents: UserAgent¤Î¼±ÊÌ
145 $ua = 'HTTP_USER_AGENT';
146 $user_agent = $matches = array();
148 $user_agent['agent'] = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
149 unset(${$ua}, $_SERVER[$ua], $HTTP_SERVER_VARS[$ua], $ua); // safety
151 foreach ($agents as $agent) {
152 if (preg_match($agent['pattern'], $user_agent['agent'], $matches)) {
153 $user_agent['profile'] = isset($agent['profile']) ? $agent['profile'] : '';
154 $user_agent['name'] = isset($matches[1]) ? $matches[1] : ''; // device or browser name
155 $user_agent['vers'] = isset($matches[2]) ? $matches[2] : ''; // 's version
159 unset($agents, $matches);
161 // Profile-related init and setting
162 define('UA_PROFILE', isset($user_agent['profile']) ? $user_agent['profile'] : '');
164 define('UA_INI_FILE', DATA_HOME . UA_PROFILE . '.ini.php');
165 if (! file_exists(UA_INI_FILE) || ! is_readable(UA_INI_FILE)) {
166 die_message('UA_INI_FILE for "' . UA_PROFILE . '" not found.');
168 require(UA_INI_FILE); // Also manually
171 define('UA_NAME', isset($user_agent['name']) ? $user_agent['name'] : '');
172 define('UA_VERS', isset($user_agent['vers']) ? $user_agent['vers'] : '');
173 unset($user_agent); // Unset after reading UA_INI_FILE
175 /////////////////////////////////////////////////
176 // ¥Ç¥£¥ì¥¯¥È¥ê¤Î¥Á¥§¥Ã¥¯
179 foreach(array('DATA_DIR', 'DIFF_DIR', 'BACKUP_DIR', 'CACHE_DIR') as $dir){
180 if (! is_writable(constant($dir)))
181 $die .= 'Directory is not found or not writable (' . $dir . ')' . "\n";
184 // ÀßÄê¥Õ¥¡¥¤¥ë¤ÎÊÑ¿ô¥Á¥§¥Ã¥¯
186 foreach(array('rss_max', 'page_title', 'note_hr', 'related_link', 'show_passage',
187 'rule_related_str', 'load_template_func') as $var){
188 if (! isset(${$var})) $temp .= '$' . $var . "\n";
191 if ($die) $die .= "\n"; // A breath
192 $die .= 'Variable(s) not found: (Maybe the old *.ini.php?)' . "\n" . $temp;
196 foreach(array('LANG', 'PLUGIN_DIR') as $def){
197 if (! defined($def)) $temp .= $def . "\n";
200 if ($die) $die .= "\n"; // A breath
201 $die .= 'Define(s) not found: (Maybe the old *.ini.php?)' . "\n" . $temp;
204 if($die) die_message(nl2br("\n\n" . $die));
207 /////////////////////////////////////////////////
208 // ɬ¿Ü¤Î¥Ú¡¼¥¸¤¬Â¸ºß¤·¤Ê¤±¤ì¤Ð¡¢¶õ¤Î¥Õ¥¡¥¤¥ë¤òºîÀ®¤¹¤ë
210 foreach(array($defaultpage, $whatsnew, $interwiki) as $page){
211 if (! is_page($page)) touch(get_filename($page));
214 /////////////////////////////////////////////////
215 // ³°Éô¤«¤é¤¯¤ëÊÑ¿ô¤Î¥Á¥§¥Ã¥¯
217 // Prohibit $_GET attack
218 foreach (array('msg', 'pass') as $key) {
219 if (isset($_GET[$key])) die_message('Sorry, already reserved: ' . $key . '=');
223 unset($HTTP_GET_VARS, $HTTP_POST_VARS); //, 'SERVER', 'ENV', 'SESSION', ...
224 unset($_REQUEST); // Considered harmful
226 // Remove null character etc.
227 $_GET = input_filter($_GET);
228 $_POST = input_filter($_POST);
229 $_COOKIE = input_filter($_COOKIE);
231 // ʸ»ú¥³¡¼¥ÉÊÑ´¹ ($_POST)
232 // <form> ¤ÇÁ÷¿®¤µ¤ì¤¿Ê¸»ú (¥Ö¥é¥¦¥¶¤¬¥¨¥ó¥³¡¼¥É¤·¤¿¥Ç¡¼¥¿) ¤Î¥³¡¼¥É¤òÊÑ´¹
233 // POST method ¤Ï¾ï¤Ë form ·Ðͳ¤Ê¤Î¤Ç¡¢É¬¤ºÊÑ´¹¤¹¤ë
235 if (isset($_POST['encode_hint']) && $_POST['encode_hint'] != '') {
236 // do_plugin_xxx() ¤ÎÃæ¤Ç¡¢<form> ¤Ë encode_hint ¤ò»Å¹þ¤ó¤Ç¤¤¤ë¤Î¤Ç¡¢
237 // encode_hint ¤òÍѤ¤¤Æ¥³¡¼¥É¸¡½Ð¤¹¤ë¡£
238 // Á´ÂΤò¸«¤Æ¥³¡¼¥É¸¡½Ð¤¹¤ë¤È¡¢µ¡¼ï°Í¸ʸ»ú¤ä¡¢Ì¯¤Ê¥Ð¥¤¥Ê¥ê
239 // ¥³¡¼¥É¤¬º®Æþ¤·¤¿¾ì¹ç¤Ë¡¢¥³¡¼¥É¸¡½Ð¤Ë¼ºÇÔ¤¹¤ë¶²¤ì¤¬¤¢¤ë¡£
240 $encode = mb_detect_encoding($_POST['encode_hint']);
241 mb_convert_variables(SOURCE_ENCODING, $encode, $_POST);
243 } else if (isset($_POST['charset']) && $_POST['charset'] != '') {
244 // TrackBack Ping ¤Ç»ØÄꤵ¤ì¤Æ¤¤¤ë¤³¤È¤¬¤¢¤ë
245 // ¤¦¤Þ¤¯¤¤¤«¤Ê¤¤¾ì¹ç¤Ï¼«Æ°¸¡½Ð¤ËÀÚ¤êÂؤ¨
246 if (mb_convert_variables(SOURCE_ENCODING,
247 $_POST['charset'], $_POST) !== $_POST['charset']) {
248 mb_convert_variables(SOURCE_ENCODING, 'auto', $_POST);
251 } else if (! empty($_POST)) {
252 // Á´Éô¤Þ¤È¤á¤Æ¡¢¼«Æ°¸¡½Ð¡¿ÊÑ´¹
253 mb_convert_variables(SOURCE_ENCODING, 'auto', $_POST);
256 // ʸ»ú¥³¡¼¥ÉÊÑ´¹ ($_GET)
257 // GET method ¤Ï form ¤«¤é¤Î¾ì¹ç¤È¡¢<a href="http://script/?key=value> ¤Î¾ì¹ç¤¬¤¢¤ë
258 // <a href...> ¤Î¾ì¹ç¤Ï¡¢¥µ¡¼¥Ð¡¼¤¬ rawurlencode ¤·¤Æ¤¤¤ë¤Î¤Ç¡¢¥³¡¼¥ÉÊÑ´¹¤ÏÉÔÍ×
259 if (isset($_GET['encode_hint']) && $_GET['encode_hint'] != '')
261 // form ·Ðͳ¤Î¾ì¹ç¤Ï¡¢¥Ö¥é¥¦¥¶¤¬¥¨¥ó¥³¡¼¥É¤·¤Æ¤¤¤ë¤Î¤Ç¡¢¥³¡¼¥É¸¡½Ð¡¦ÊÑ´¹¤¬É¬Íס£
262 // encode_hint ¤¬´Þ¤Þ¤ì¤Æ¤¤¤ë¤Ï¤º¤Ê¤Î¤Ç¡¢¤½¤ì¤ò¸«¤Æ¡¢¥³¡¼¥É¸¡½Ð¤·¤¿¸å¡¢ÊÑ´¹¤¹¤ë¡£
263 // Íýͳ¤Ï¡¢post ¤ÈƱÍÍ
264 $encode = mb_detect_encoding($_GET['encode_hint']);
265 mb_convert_variables(SOURCE_ENCODING, $encode, $_GET);
269 /////////////////////////////////////////////////
270 // QUERY_STRING¤ò¼èÆÀ
272 // cmd¤âplugin¤â»ØÄꤵ¤ì¤Æ¤¤¤Ê¤¤¾ì¹ç¤Ï¡¢QUERY_STRING¤ò
273 // ¥Ú¡¼¥¸Ì¾¤«InterWikiName¤Ç¤¢¤ë¤È¤ß¤Ê¤¹
275 if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING']) {
276 $arg = & $_SERVER['QUERY_STRING'];
277 } else if (isset($_SERVER['argv']) && ! empty($_SERVER['argv'])) {
278 $arg = & $_SERVER['argv'][0];
280 if (PKWK_QUERY_STRING_MAX && strlen($arg) > PKWK_QUERY_STRING_MAX) {
281 // Something nasty attack?
282 pkwk_common_headers();
283 sleep(1); // Fake processing, and/or process other threads
284 echo('Query string too long');
287 $arg = input_filter($arg); // \0 ½üµî
289 // unset QUERY_STRINGs
290 foreach (array('QUERY_STRING', 'argv', 'argc') as $key) {
291 unset(${$key}, $_SERVER[$key], $HTTP_SERVER_VARS[$key]);
293 // $_SERVER['REQUEST_URI'] is used at func.php NOW
294 unset($REQUEST_URI, $HTTP_SERVER_VARS['REQUEST_URI']);
296 // mb_convert_variables¤Î¥Ð¥°(?)Âкö: ÇÛÎó¤ÇÅϤµ¤Ê¤¤¤ÈÍî¤Á¤ë
298 mb_convert_variables(SOURCE_ENCODING, 'auto', $arg);
301 /////////////////////////////////////////////////
302 // QUERY_STRING¤òʬ²ò¤·¤Æ¥³¡¼¥ÉÊÑ´¹¤·¡¢$_GET ¤Ë¾å½ñ¤
304 // URI ¤ò urlencode ¤»¤º¤ËÆþÎϤ·¤¿¾ì¹ç¤ËÂн褹¤ë
306 foreach (explode('&', $arg) as $key_and_value) {
307 if (preg_match('/^([^=]+)=(.+)/', $key_and_value, $matches) &&
308 mb_detect_encoding($matches[2]) != 'ASCII') {
309 $_GET[$matches[1]] = $matches[2];
314 /////////////////////////////////////////////////
319 $cookie = & $_COOKIE;
321 // GET + POST = $vars
323 $vars = & $_GET; // Major pattern: Read-only access via GET
324 } else if (empty($_GET)) {
325 $vars = & $_POST; // Minor pattern: Write access via POST etc.
327 $vars = array_merge($_GET, $_POST); // Considered reliable than $_REQUEST
330 // ÆþÎÏ¥Á¥§¥Ã¥¯: 'cmd=' and 'plugin=' can't live together
331 if (isset($vars['cmd']) && isset($vars['plugin']))
332 die('Using both cmd= and plugin= is not allowed');
334 // ÆþÎÏ¥Á¥§¥Ã¥¯: cmd, plugin ¤Îʸ»úÎó¤Ï±Ñ¿ô»ú°Ê³°¤¢¤ê¤¨¤Ê¤¤
335 foreach(array('cmd', 'plugin') as $var) {
336 if (isset($vars[$var]) && ! preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $vars[$var]))
337 unset($get[$var], $post[$var], $vars[$var]);
340 // À°·Á: page, strip_bracket()
341 if (isset($vars['page'])) {
342 $get['page'] = $post['page'] = $vars['page'] = strip_bracket($vars['page']);
344 $get['page'] = $post['page'] = $vars['page'] = '';
347 // À°·Á: msg, ²þ¹Ô¤ò¼è¤ê½ü¤¯
348 if (isset($vars['msg'])) {
349 $get['msg'] = $post['msg'] = $vars['msg'] = str_replace("\r", '', $vars['msg']);
352 // ¸åÊý¸ß´¹À (?md5=...)
353 if (isset($get['md5']) && $get['md5'] != '' &&
354 ! isset($vars['cmd']) && ! isset($vars['plugin'])) {
355 $get['cmd'] = $post['cmd'] = $vars['cmd'] = 'md5';
359 if (isset($vars['tb_id']) && $vars['tb_id'] != '') {
360 $get['cmd'] = $post['cmd'] = $vars['cmd'] = 'tb';
363 // cmd¤âplugin¤â»ØÄꤵ¤ì¤Æ¤¤¤Ê¤¤¾ì¹ç¤Ï¡¢QUERY_STRING¤ò¥Ú¡¼¥¸Ì¾¤«InterWikiName¤Ç¤¢¤ë¤È¤ß¤Ê¤¹
364 if (! isset($vars['cmd']) && ! isset($vars['plugin'])) {
366 $get['cmd'] = $post['cmd'] = $vars['cmd'] = 'read';
368 if ($arg == '') $arg = $defaultpage;
369 $arg = rawurldecode($arg);
370 $arg = strip_bracket($arg);
371 $arg = input_filter($arg);
372 $get['page'] = $post['page'] = $vars['page'] = $arg;
375 /////////////////////////////////////////////////
376 // ½é´üÀßÄê($WikiName,$BracketName¤Ê¤É)
377 // $WikiName = '[A-Z][a-z]+(?:[A-Z][a-z]+)+';
378 // $WikiName = '\b[A-Z][a-z]+(?:[A-Z][a-z]+)+\b';
379 // $WikiName = '(?<![[:alnum:]])(?:[[:upper:]][[:lower:]]+){2,}(?![[:alnum:]])';
380 // $WikiName = '(?<!\w)(?:[A-Z][a-z]+){2,}(?!\w)';
382 // BugTrack/304»ÃÄêÂнè
383 $WikiName = '(?:[A-Z][a-z]+){2,}(?!\w)';
385 // $BracketName = ':?[^\s\]#&<>":]+:?';
386 $BracketName = '(?!\s):?[^\r\n\t\f\[\]<>#&":]+:?(?<!\s)';
389 $InterWikiName = '(\[\[)?((?:(?!\s|:|\]\]).)+):(.+)(?(1)\]\])';
392 $NotePattern = '/\(\(((?:(?>(?:(?!\(\()(?!\)\)(?:[^\)]|$)).)+)|(?R))*)\)\)/ex';
394 /////////////////////////////////////////////////
395 // ½é´üÀßÄê(¥æ¡¼¥¶ÄêµÁ¥ë¡¼¥ëÆɤ߹þ¤ß)
396 require(DATA_HOME . 'rules.ini.php');
398 /////////////////////////////////////////////////
399 // ½é´üÀßÄê(¤½¤Î¾¤Î¥°¥í¡¼¥Ð¥ëÊÑ¿ô)
402 $now = format_date(UTIME);
404 // Æü»þÃÖ´¹¥ë¡¼¥ë¤ò$line_rules¤Ë²Ã¤¨¤ë
405 if ($usedatetime) $line_rules += $datetime_rules;
406 unset($datetime_rules);
408 // ¥Õ¥§¥¤¥¹¥Þ¡¼¥¯¤ò$line_rules¤Ë²Ã¤¨¤ë
409 if ($usefacemark) $line_rules += $facemark_rules;
410 unset($facemark_rules);
412 // ¼ÂÂλ²¾È¥Ñ¥¿¡¼¥ó¤ª¤è¤Ó¥·¥¹¥Æ¥à¤Ç»ÈÍѤ¹¤ë¥Ñ¥¿¡¼¥ó¤ò$line_rules¤Ë²Ã¤¨¤ë
413 //$entity_pattern = '[a-zA-Z0-9]{2,8}';
414 $entity_pattern = trim(join('', file(CACHE_DIR . 'entities.dat')));
416 $line_rules = array_merge(array(
417 '&(#[0-9]+|#x[0-9a-f]+|' . $entity_pattern . ');' => '&$1;',
418 "\r" => '<br />' . "\n", /* ¹ÔËö¤Ë¥Á¥ë¥À¤Ï²þ¹Ô */