2 // PukiWiki - Yet another WikiWikiWeb clone.
5 // 2002-2017 PukiWiki Development 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.5.1');
15 '<strong>PukiWiki ' . S_VERSION . '</strong>' .
17 ' <a href="http://pukiwiki.osdn.jp/">PukiWiki Development Team</a>'
20 /////////////////////////////////////////////////
21 // Session security options
23 ini_set('session.use_strict_mode', 1);
24 ini_set('session.use_cookies', 1);
25 ini_set('session.use_only_cookies', 1);
27 /////////////////////////////////////////////////
28 // Init server variables
30 // Comapat and suppress notices
31 if (!isset($HTTP_SERVER_VARS)) $HTTP_SERVER_VARS = array();
33 foreach (array('SCRIPT_NAME', 'SERVER_ADMIN', 'SERVER_NAME',
34 'SERVER_PORT', 'SERVER_SOFTWARE') as $key) {
35 define($key, isset($_SERVER[$key]) ? $_SERVER[$key] : '');
36 unset(${$key}, $_SERVER[$key], $HTTP_SERVER_VARS[$key]);
39 /////////////////////////////////////////////////
40 // Init grobal variables
42 $foot_explain = array(); // Footnotes
43 $related = array(); // Related pages
44 $head_tags = array(); // XHTML tags in <head></head>
46 /////////////////////////////////////////////////
49 define('LOCALZONE', date('Z'));
50 define('UTIME', time() - LOCALZONE);
51 define('MUTIME', getmicrotime());
53 /////////////////////////////////////////////////
56 define('INI_FILE', DATA_HOME . 'pukiwiki.ini.php');
58 if (! file_exists(INI_FILE) || ! is_readable(INI_FILE)) {
59 $die .= 'File is not found. (INI_FILE)' . "\n";
63 if ($die) die_message(nl2br("\n\n" . $die));
65 /////////////////////////////////////////////////
66 // INI_FILE: LANG に基づくエンコーディング設定
68 // MB_LANGUAGE: mb_language (for mbstring extension)
69 // 'uni'(means UTF-8), 'English', or 'Japanese'
70 // SOURCE_ENCODING: Internal content encoding (for mbstring extension)
71 // 'UTF-8', 'ASCII', or 'EUC-JP'
72 // CONTENT_CHARSET: Internal content encoding = Output content charset
73 // (for DTD, htmlsc())
74 // 'UTF-8', 'iso-8859-1', 'EUC-JP' or ...
77 case 'en': define('MB_LANGUAGE', 'English' ); break;
78 case 'ja': define('MB_LANGUAGE', 'Japanese'); break;
79 case 'ko': define('MB_LANGUAGE', 'Korean' ); break; //UTF-8 only
80 // See BugTrack2/13 for all hack about Korean support, //UTF-8 only
81 // and give us your report! //UTF-8 only
82 default: die_message('No such language "' . LANG . '"'); break;
85 define('PKWK_UTF8_ENABLE', 1); //UTF-8 only
86 if (defined('PKWK_UTF8_ENABLE')) {
87 define('SOURCE_ENCODING', 'UTF-8');
88 define('CONTENT_CHARSET', 'UTF-8');
92 define('SOURCE_ENCODING', 'ASCII');
93 define('CONTENT_CHARSET', 'iso-8859-1');
96 define('SOURCE_ENCODING', 'EUC-JP');
97 define('CONTENT_CHARSET', 'EUC-JP');
102 mb_language(MB_LANGUAGE);
103 mb_internal_encoding(SOURCE_ENCODING);
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)) {
138 pkwk_script_uri_base(PKWK_URI_ABSOLUTE, true, $script);
140 // Init automatically
141 $__uri_absolute = pkwk_script_uri_base(PKWK_URI_ABSOLUTE, true);
142 $script = $__uri_absolute;
145 // INI_FILE: Auth settings
146 if (isset($auth_type) && $auth_type === AUTH_TYPE_SAML) {
147 $auth_external_login_url_base = get_script_uri() . '?//cmd.saml//sso';
151 /////////////////////////////////////////////////
152 // INI_FILE: $agents: UserAgentの識別
154 $ua = 'HTTP_USER_AGENT';
155 $user_agent = $matches = array();
157 $user_agent['agent'] = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
159 foreach ($agents as $agent) {
160 if (preg_match($agent['pattern'], $user_agent['agent'], $matches)) {
161 $user_agent['profile'] = isset($agent['profile']) ? $agent['profile'] : '';
162 $user_agent['name'] = isset($matches[1]) ? $matches[1] : ''; // device or browser name
163 $user_agent['vers'] = isset($matches[2]) ? $matches[2] : ''; // 's version
167 unset($agents, $matches);
169 // Profile-related init and setting
170 define('UA_PROFILE', isset($user_agent['profile']) ? $user_agent['profile'] : '');
172 define('UA_INI_FILE', DATA_HOME . UA_PROFILE . '.ini.php');
173 if (! file_exists(UA_INI_FILE) || ! is_readable(UA_INI_FILE)) {
174 die_message('UA_INI_FILE for "' . UA_PROFILE . '" not found.');
176 require(UA_INI_FILE); // Also manually
179 define('UA_NAME', isset($user_agent['name']) ? $user_agent['name'] : '');
180 define('UA_VERS', isset($user_agent['vers']) ? $user_agent['vers'] : '');
181 unset($user_agent); // Unset after reading UA_INI_FILE
183 /////////////////////////////////////////////////
187 foreach(array('DATA_DIR', 'DIFF_DIR', 'BACKUP_DIR', 'CACHE_DIR') as $dir){
188 if (! is_writable(constant($dir)))
189 $die .= 'Directory is not found or not writable (' . $dir . ')' . "\n";
194 foreach(array('rss_max', 'page_title', 'note_hr', 'related_link', 'show_passage',
195 'rule_related_str', 'load_template_func') as $var){
196 if (! isset(${$var})) $temp .= '$' . $var . "\n";
199 if ($die) $die .= "\n"; // A breath
200 $die .= 'Variable(s) not found: (Maybe the old *.ini.php?)' . "\n" . $temp;
204 foreach(array('LANG', 'PLUGIN_DIR') as $def){
205 if (! defined($def)) $temp .= $def . "\n";
208 if ($die) $die .= "\n"; // A breath
209 $die .= 'Define(s) not found: (Maybe the old *.ini.php?)' . "\n" . $temp;
212 if($die) die_message(nl2br("\n\n" . $die));
215 /////////////////////////////////////////////////
216 // 必須のページが存在しなければ、空のファイルを作成する
218 foreach(array($defaultpage, $whatsnew, $interwiki) as $page){
219 if (! is_page($page)) pkwk_touch_file(get_filename($page));
222 /////////////////////////////////////////////////
225 // Prohibit $_GET attack
226 foreach (array('msg', 'pass') as $key) {
227 if (isset($_GET[$key])) die_message('Sorry, already reserved: ' . $key . '=');
231 unset($HTTP_GET_VARS, $HTTP_POST_VARS); //, 'SERVER', 'ENV', 'SESSION', ...
232 unset($_REQUEST); // Considered harmful
234 // Remove null character etc.
235 $_GET = input_filter($_GET);
236 $_POST = input_filter($_POST);
237 $_COOKIE = input_filter($_COOKIE);
240 // <form> で送信された文字 (ブラウザがエンコードしたデータ) のコードを変換
241 // POST method は常に form 経由なので、必ず変換する
243 if (isset($_POST['encode_hint']) && $_POST['encode_hint'] != '') {
244 // do_plugin_xxx() の中で、<form> に encode_hint を仕込んでいるので、
245 // encode_hint を用いてコード検出する。
246 // 全体を見てコード検出すると、機種依存文字や、妙なバイナリ
247 // コードが混入した場合に、コード検出に失敗する恐れがある。
248 $encode = mb_detect_encoding($_POST['encode_hint']);
249 mb_convert_variables(SOURCE_ENCODING, $encode, $_POST);
251 } else if (isset($_POST['charset']) && $_POST['charset'] != '') {
252 // TrackBack Ping で指定されていることがある
253 // うまくいかない場合は自動検出に切り替え
254 if (mb_convert_variables(SOURCE_ENCODING,
255 $_POST['charset'], $_POST) !== $_POST['charset']) {
256 mb_convert_variables(SOURCE_ENCODING, 'auto', $_POST);
259 } else if (! empty($_POST)) {
261 mb_convert_variables(SOURCE_ENCODING, 'auto', $_POST);
265 // GET method は form からの場合と、<a href="http://script/?key=value> の場合がある
266 // <a href...> の場合は、サーバーが rawurlencode しているので、コード変換は不要
267 if (isset($_GET['encode_hint']) && $_GET['encode_hint'] != '')
269 // form 経由の場合は、ブラウザがエンコードしているので、コード検出・変換が必要。
270 // encode_hint が含まれているはずなので、それを見て、コード検出した後、変換する。
272 $encode = mb_detect_encoding($_GET['encode_hint']);
273 mb_convert_variables(SOURCE_ENCODING, $encode, $_GET);
277 /////////////////////////////////////////////////
280 // cmdもpluginも指定されていない場合は、QUERY_STRINGを
281 // ページ名かInterWikiNameであるとみなす
283 if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != '') {
284 global $g_query_string;
285 $g_query_string = $_SERVER['QUERY_STRING'];
286 $arg = & $_SERVER['QUERY_STRING'];
287 } else if (isset($_SERVER['argv']) && ! empty($_SERVER['argv'])) {
288 $arg = & $_SERVER['argv'][0];
290 if (PKWK_QUERY_STRING_MAX && strlen($arg) > PKWK_QUERY_STRING_MAX) {
291 // Something nasty attack?
292 pkwk_common_headers();
293 sleep(1); // Fake processing, and/or process other threads
294 echo('Query string too long');
297 $arg = input_filter($arg); // \0 除去
299 // unset QUERY_STRINGs
300 foreach (array('QUERY_STRING', 'argv', 'argc') as $key) {
301 unset(${$key}, $_SERVER[$key], $HTTP_SERVER_VARS[$key]);
303 // $_SERVER['REQUEST_URI'] is used at func.php NOW
304 unset($REQUEST_URI, $HTTP_SERVER_VARS['REQUEST_URI']);
306 // mb_convert_variablesのバグ(?)対策: 配列で渡さないと落ちる
308 mb_convert_variables(SOURCE_ENCODING, 'auto', $arg);
311 /////////////////////////////////////////////////
312 // QUERY_STRINGを分解してコード変換し、$_GET に上書き
314 // URI を urlencode せずに入力した場合に対処する
316 foreach (explode('&', $arg) as $key_and_value) {
317 if (preg_match('/^([^=]+)=(.+)/', $key_and_value, $matches) &&
318 mb_detect_encoding($matches[2]) != 'ASCII') {
319 $_GET[$matches[1]] = $matches[2];
324 /////////////////////////////////////////////////
329 $cookie = & $_COOKIE;
331 // GET + POST = $vars
333 $vars = & $_GET; // Major pattern: Read-only access via GET
334 } else if (empty($_GET)) {
335 $vars = & $_POST; // Minor pattern: Write access via POST etc.
337 $vars = array_merge($_GET, $_POST); // Considered reliable than $_REQUEST
341 * Parse specified format query_string as params.
343 * For example: ?//key1.value2//key2.value2
345 function parse_query_string_ext($query_string) {
348 if (preg_match('#^//[^&]*#', $query_string, $m)) {
349 foreach (explode('//', $m[0]) as $item) {
350 $sp = explode('.', $item, 2);
353 $vars[$sp[0]] = $sp[1];
362 if (isset($g_query_string) && $g_query_string) {
363 if (substr($g_query_string, 0, 2) === '//') {
364 // Parse ?//key.value//key.value format query string
365 $vars = array_merge($vars, parse_query_string_ext($g_query_string));
370 // 入力チェック: 'cmd=' and 'plugin=' can't live together
371 if (isset($vars['cmd']) && isset($vars['plugin']))
372 die('Using both cmd= and plugin= is not allowed');
374 // 入力チェック: cmd, plugin の文字列は英数字以外ありえない
375 foreach(array('cmd', 'plugin') as $var) {
376 if (isset($vars[$var]) && ! preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $vars[$var]))
377 unset($get[$var], $post[$var], $vars[$var]);
380 // 整形: page, strip_bracket()
381 if (isset($vars['page'])) {
382 $get['page'] = $post['page'] = $vars['page'] = strip_bracket($vars['page']);
384 $get['page'] = $post['page'] = $vars['page'] = '';
388 if (isset($vars['msg'])) {
389 $get['msg'] = $post['msg'] = $vars['msg'] = str_replace("\r", '', $vars['msg']);
393 if (isset($get['md5']) && $get['md5'] != '' &&
394 ! isset($vars['cmd']) && ! isset($vars['plugin'])) {
395 $get['cmd'] = $post['cmd'] = $vars['cmd'] = 'md5';
398 // cmdもpluginも指定されていない場合は、QUERY_STRINGをページ名かInterWikiNameであるとみなす
399 if (! isset($vars['cmd']) && ! isset($vars['plugin'])) {
401 $get['cmd'] = $post['cmd'] = $vars['cmd'] = 'read';
403 $arg = preg_replace("#^([^&]*)&.*$#", "$1", $arg);
404 if ($arg == '') $arg = $defaultpage;
405 $arg = rawurldecode($arg);
406 $arg = strip_bracket($arg);
407 $arg = input_filter($arg);
408 $get['page'] = $post['page'] = $vars['page'] = $arg;
411 /////////////////////////////////////////////////
412 // 初期設定($WikiName,$BracketNameなど)
413 // $WikiName = '[A-Z][a-z]+(?:[A-Z][a-z]+)+';
414 // $WikiName = '\b[A-Z][a-z]+(?:[A-Z][a-z]+)+\b';
415 // $WikiName = '(?<![[:alnum:]])(?:[[:upper:]][[:lower:]]+){2,}(?![[:alnum:]])';
416 // $WikiName = '(?<!\w)(?:[A-Z][a-z]+){2,}(?!\w)';
419 $WikiName = '(?:[A-Z][a-z]+){2,}(?!\w)';
421 // $BracketName = ':?[^\s\]#&<>":]+:?';
422 $BracketName = '(?!\s):?[^\r\n\t\f\[\]<>#&":]+:?(?<!\s)';
425 $InterWikiName = '(\[\[)?((?:(?!\s|:|\]\]).)+):(.+)(?(1)\]\])';
428 $NotePattern = '/\(\(((?:(?>(?:(?!\(\()(?!\)\)(?:[^\)]|$)).)+)|(?R))*)\)\)/x';
430 /////////////////////////////////////////////////
431 // 初期設定(ユーザ定義ルール読み込み)
432 require(DATA_HOME . 'rules.ini.php');
434 /////////////////////////////////////////////////
435 // Load HTML Entity pattern
436 // This pattern is created by 'plugin/update_entities.inc.php'
437 require(LIB_DIR . 'html_entities.php');
439 /////////////////////////////////////////////////
443 $now = format_date(UTIME);
445 // 日時置換ルールを$line_rulesに加える
446 if ($usedatetime) $line_rules += $datetime_rules;
447 unset($datetime_rules);
449 // フェイスマークを$line_rulesに加える
450 if ($usefacemark) $line_rules += $facemark_rules;
451 unset($facemark_rules);
453 // 実体参照パターンおよびシステムで使用するパターンを$line_rulesに加える
454 $line_rules = array_merge(array(
455 '&(#[0-9]+|#x[0-9a-f]+|' . get_html_entity_pattern() . ');' => '&$1;',
456 "\r" => '<br />' . "\n", /* 行末にチルダは改行 */