2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
5 // $Id: init.php,v 1.14 2004/10/16 05:56:24 henoheno Exp $
8 /////////////////////////////////////////////////
10 define('S_VERSION', '1.4.4');
12 '<strong>PukiWiki ' . S_VERSION . '</strong>' .
13 ' Copyright © 2001-2004' .
14 ' <a href="http://pukiwiki.org">PukiWiki Developers Team</a>.' .
15 ' License is <a href="http://www.gnu.org/licenses/gpl.html">GPL</a>.<br />' .
16 ' Based on "PukiWiki" 1.3 by <a href="http://factage.com/yu-ji/">yu-ji</a>'
19 /////////////////////////////////////////////////
20 // ½é´üÀßÄê (¥µ¡¼¥ÐÊÑ¿ô)
21 foreach (array('SCRIPT_NAME', 'SERVER_ADMIN', 'SERVER_NAME',
22 'SERVER_PORT', 'SERVER_SOFTWARE') as $key) {
23 define($key, isset($_SERVER[$key]) ? $_SERVER[$key] : '');
24 unset(${$key}, $_SERVER[$key], $HTTP_SERVER_VARS[$key]);
27 /////////////////////////////////////////////////
28 // ½é´üÀßÄê (¥°¥í¡¼¥Ð¥ëÊÑ¿ô)
30 $foot_explain = array(); // µÓÃí
31 $related = array(); // ´ØÏ¢¤¹¤ë¥Ú¡¼¥¸
32 $head_tags = array(); // <head>Æâ¤ËÄɲ乤륿¥°
34 /////////////////////////////////////////////////
36 define('LOCALZONE', date('Z'));
37 define('UTIME', time() - LOCALZONE);
38 define('MUTIME', getmicrotime());
40 /////////////////////////////////////////////////
41 // ½é´üÀßÄê(ÀßÄê¥Õ¥¡¥¤¥ë¤Î¾ì½ê)
42 define('INI_FILE', DATA_HOME . 'pukiwiki.ini.php');
46 if (! file_exists(INI_FILE) || ! is_readable(INI_FILE)) {
47 $die = "${die}File is not found. (INI_FILE)\n";
51 if ($die) die_message(nl2br("\n\n" . $die));
53 /////////////////////////////////////////////////
54 // INI_FILE: LANG ½é´üÀßÄê(¸À¸ì¥Õ¥¡¥¤¥ë¤Î¾ì½ê)
55 define('LANG_FILE', DATA_HOME . LANG . '.lng');
59 if (! file_exists(LANG_FILE) || ! is_readable(LANG_FILE)) {
60 $die = "${die}File is not found. (LANG_FILE)\n";
64 if ($die) die_message(nl2br("\n\n" . $die));
66 /////////////////////////////////////////////////
67 // LANG_FILE: mbstring extension ´ØÏ¢
69 mb_language(MB_LANGUAGE);
70 mb_internal_encoding(SOURCE_ENCODING);
71 ini_set('mbstring.http_input', 'pass');
72 mb_http_output('pass');
73 mb_detect_order('auto');
75 /////////////////////////////////////////////////
76 // LANG_FILE: ÍËÆüÇÛÎó
78 $weeklabels = $_msg_week;
80 /////////////////////////////////////////////////
81 // INI_FILE: $script: ½é´üÀßÄê
85 get_script_uri($script);
88 $script = get_script_uri();
91 /////////////////////////////////////////////////
92 // INI_FILE: $agents: UserAgent¤Î¼±ÊÌ
94 $ua = 'HTTP_USER_AGENT';
95 $user_agent = $matches = array();
97 $user_agent['agent'] = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
98 unset(${$ua}, $_SERVER[$ua], $HTTP_SERVER_VARS[$ua], $ua); // safety
100 foreach ($agents as $agent) {
101 if (preg_match($agent['pattern'], $user_agent['agent'], $matches)) {
102 $user_agent['profile'] = isset($agent['profile']) ? $agent['profile'] : '';
103 $user_agent['name'] = isset($matches[1]) ? $matches[1] : ''; // device or browser name
104 $user_agent['vers'] = isset($matches[2]) ? $matches[2] : ''; // 's version
108 unset($agents, $matches);
110 // Profile-related init and setting
111 define('UA_PROFILE', isset($user_agent['profile']) ? $user_agent['profile'] : '');
113 define('UA_INI_FILE', DATA_HOME . UA_PROFILE . '.ini.php');
114 if (! file_exists(UA_INI_FILE) || ! is_readable(UA_INI_FILE)) {
115 die_message('UA_INI_FILE for "' . UA_PROFILE . '" not found.');
117 require(UA_INI_FILE); // Also manually
120 define('UA_NAME', isset($user_agent['name']) ? $user_agent['name'] : '');
121 define('UA_VERS', isset($user_agent['vers']) ? $user_agent['vers'] : '');
122 unset($user_agent); // Unset after reading UA_INI_FILE
124 /////////////////////////////////////////////////
125 // ¥Ç¥£¥ì¥¯¥È¥ê¤Î¥Á¥§¥Ã¥¯
128 foreach(array('DATA_DIR', 'DIFF_DIR', 'BACKUP_DIR', 'CACHE_DIR') as $dir){
129 if (! is_writable(constant($dir)))
130 $die = "${die}Directory is not found or not writable ($dir)\n";
133 // ÀßÄê¥Õ¥¡¥¤¥ë¤ÎÊÑ¿ô¥Á¥§¥Ã¥¯
135 foreach(array('rss_max', 'page_title', 'note_hr', 'related_link', 'show_passage',
136 'rule_related_str', 'load_template_func') as $var){
137 if (! isset(${$var})) $temp .= "\$$var\n";
140 if ($die) $die .= "\n"; // A breath
141 $die .= "Variable(s) not found: (Maybe the old *.ini.php?)\n" . $temp;
145 foreach(array('LANG', 'PLUGIN_DIR') as $def){
146 if (! defined($def)) $temp .= "$def\n";
149 if ($die) $die .= "\n"; // A breath
150 $die .= "Define(s) not found: (Maybe the old *.ini.php?)\n" . $temp;
153 if($die) die_message(nl2br("\n\n" . $die));
156 /////////////////////////////////////////////////
157 // ɬ¿Ü¤Î¥Ú¡¼¥¸¤¬Â¸ºß¤·¤Ê¤±¤ì¤Ð¡¢¶õ¤Î¥Õ¥¡¥¤¥ë¤òºîÀ®¤¹¤ë
159 foreach(array($defaultpage, $whatsnew, $interwiki) as $page){
160 if (! is_page($page)) touch(get_filename($page));
163 /////////////////////////////////////////////////
164 // ³°Éô¤«¤é¤¯¤ëÊÑ¿ô¤Î¥Á¥§¥Ã¥¯
166 // Prohibit $_GET attack
167 foreach (array('msg', 'pass') as $key) {
168 if (isset($_GET[$key])) die_message("Sorry, already reserved: $key=");
172 unset($HTTP_GET_VARS, $HTTP_POST_VARS); //, 'SERVER', 'ENV', 'SESSION', ...
173 unset($_REQUEST); // Considered harmful
175 // Remove null character etc.
176 $_GET = input_filter($_GET);
177 $_POST = input_filter($_POST);
178 $_COOKIE = input_filter($_COOKIE);
180 // ʸ»ú¥³¡¼¥ÉÊÑ´¹ ($_POST)
181 // <form> ¤ÇÁ÷¿®¤µ¤ì¤¿Ê¸»ú (¥Ö¥é¥¦¥¶¤¬¥¨¥ó¥³¡¼¥É¤·¤¿¥Ç¡¼¥¿) ¤Î¥³¡¼¥É¤òÊÑ´¹
182 // POST method ¤Ï¾ï¤Ë form ·Ðͳ¤Ê¤Î¤Ç¡¢É¬¤ºÊÑ´¹¤¹¤ë
184 if (isset($_POST['encode_hint']) && $_POST['encode_hint'] != '') {
185 // html.php ¤ÎÃæ¤Ç¡¢<form> ¤Ë encode_hint ¤ò»Å¹þ¤ó¤Ç¤¤¤ë¤Î¤Ç¡¢
186 // encode_hint ¤òÍѤ¤¤Æ¥³¡¼¥É¸¡½Ð¤¹¤ë¡£
187 // Á´ÂΤò¸«¤Æ¥³¡¼¥É¸¡½Ð¤¹¤ë¤È¡¢µ¡¼ï°Í¸ʸ»ú¤ä¡¢Ì¯¤Ê¥Ð¥¤¥Ê¥ê
188 // ¥³¡¼¥É¤¬º®Æþ¤·¤¿¾ì¹ç¤Ë¡¢¥³¡¼¥É¸¡½Ð¤Ë¼ºÇÔ¤¹¤ë¶²¤ì¤¬¤¢¤ë¡£
189 $encode = mb_detect_encoding($_POST['encode_hint']);
190 mb_convert_variables(SOURCE_ENCODING, $encode, $_POST);
192 } else if (isset($_POST['charset']) && $_POST['charset'] != '') {
193 // TrackBack Ping ¤Ç»ØÄꤵ¤ì¤Æ¤¤¤ë¤³¤È¤¬¤¢¤ë
194 // ¤¦¤Þ¤¯¤¤¤«¤Ê¤¤¾ì¹ç¤Ï¼«Æ°¸¡½Ð¤ËÀÚ¤êÂؤ¨
195 if (mb_convert_variables(SOURCE_ENCODING,
196 $_POST['charset'], $_POST) !== $_POST['charset']) {
197 mb_convert_variables(SOURCE_ENCODING, 'auto', $_POST);
200 } else if (! empty($_POST)) {
201 // Á´Éô¤Þ¤È¤á¤Æ¡¢¼«Æ°¸¡½Ð¡¿ÊÑ´¹
202 mb_convert_variables(SOURCE_ENCODING, 'auto', $_POST);
205 // ʸ»ú¥³¡¼¥ÉÊÑ´¹ ($_GET)
206 // GET method ¤Ï form ¤«¤é¤Î¾ì¹ç¤È¡¢<a href="http://script/?key=value> ¤Î¾ì¹ç¤¬¤¢¤ë
207 // <a href...> ¤Î¾ì¹ç¤Ï¡¢¥µ¡¼¥Ð¡¼¤¬ rawurlencode ¤·¤Æ¤¤¤ë¤Î¤Ç¡¢¥³¡¼¥ÉÊÑ´¹¤ÏÉÔÍ×
208 if (isset($_GET['encode_hint']) && $_GET['encode_hint'] != '')
210 // form ·Ðͳ¤Î¾ì¹ç¤Ï¡¢¥Ö¥é¥¦¥¶¤¬¥¨¥ó¥³¡¼¥É¤·¤Æ¤¤¤ë¤Î¤Ç¡¢¥³¡¼¥É¸¡½Ð¡¦ÊÑ´¹¤¬É¬Íס£
211 // encode_hint ¤¬´Þ¤Þ¤ì¤Æ¤¤¤ë¤Ï¤º¤Ê¤Î¤Ç¡¢¤½¤ì¤ò¸«¤Æ¡¢¥³¡¼¥É¸¡½Ð¤·¤¿¸å¡¢ÊÑ´¹¤¹¤ë¡£
212 // Íýͳ¤Ï¡¢post ¤ÈƱÍÍ
213 $encode = mb_detect_encoding($_GET['encode_hint']);
214 mb_convert_variables(SOURCE_ENCODING, $encode, $_GET);
218 /////////////////////////////////////////////////
219 // QUERY_STRING¤ò¼èÆÀ
221 // cmd¤âplugin¤â»ØÄꤵ¤ì¤Æ¤¤¤Ê¤¤¾ì¹ç¤Ï¡¢QUERY_STRING¤ò
222 // ¥Ú¡¼¥¸Ì¾¤«InterWikiName¤Ç¤¢¤ë¤È¤ß¤Ê¤¹
224 if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING']) {
225 $arg = $_SERVER['QUERY_STRING'];
226 } else if (isset($_SERVER['argv']) && count($_SERVER['argv'])) {
227 $arg = $_SERVER['argv'][0];
229 $arg = input_filter($arg); // \0 ½üµî
231 // unset QUERY_STRINGs
232 foreach (array('QUERY_STRING', 'argv', 'argc') as $key) {
233 unset(${$key}, $_SERVER[$key], $HTTP_SERVER_VARS[$key]);
235 // $_SERVER['REQUEST_URI'] is used at func.php NOW
236 unset($REQUEST_URI, $HTTP_SERVER_VARS['REQUEST_URI']);
238 // mb_convert_variables¤Î¥Ð¥°(?)Âкö: ÇÛÎó¤ÇÅϤµ¤Ê¤¤¤ÈÍî¤Á¤ë
240 mb_convert_variables(SOURCE_ENCODING, 'auto', $arg);
243 /////////////////////////////////////////////////
244 // QUERY_STRING¤òʬ²ò¤·¤Æ¥³¡¼¥ÉÊÑ´¹¤·¡¢$_GET ¤Ë¾å½ñ¤
246 // URI ¤ò urlencode ¤»¤º¤ËÆþÎϤ·¤¿¾ì¹ç¤ËÂн褹¤ë
248 foreach (explode('&', $arg) as $key_and_value) {
249 if (preg_match('/^([^=]+)=(.+)/', $key_and_value, $matches) &&
250 mb_detect_encoding($matches[2]) != 'ASCII') {
251 $_GET[$matches[1]] = $matches[2];
256 /////////////////////////////////////////////////
261 $cookie = & $_COOKIE;
263 // GET + POST = $vars
265 $vars = & $_GET; // Major pattern: Read-only access via GET
266 } else if (empty($_GET)) {
267 $vars = & $_POST; // Minor pattern: Write access via POST etc.
269 $vars = array_merge($_GET, $_POST); // Considered reliable than $_REQUEST
272 // ÆþÎÏ¥Á¥§¥Ã¥¯: cmd, plugin ¤Îʸ»úÎó¤Ï±Ñ¿ô»ú°Ê³°¤¢¤ê¤¨¤Ê¤¤
273 foreach(array('cmd', 'plugin') as $var) {
274 if (isset($vars[$var]) && ! preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $vars[$var]))
275 unset($get[$var], $post[$var], $vars[$var]);
278 // À°·Á: page, strip_bracket()
279 if (isset($vars['page'])) {
280 $get['page'] = $post['page'] = $vars['page'] = strip_bracket($vars['page']);
282 $get['page'] = $post['page'] = $vars['page'] = '';
285 // À°·Á: msg, ²þ¹Ô¤ò¼è¤ê½ü¤¯
286 if (isset($vars['msg'])) {
287 $get['msg'] = $post['msg'] = $vars['msg'] = str_replace("\r", '', $vars['msg']);
290 // ¸åÊý¸ß´¹À (?md5=...)
291 if (isset($vars['md5']) && $vars['md5'] != '') {
292 $get['cmd'] = $post['cmd'] = $vars['cmd'] = 'md5';
296 if (isset($vars['tb_id']) && $vars['tb_id'] != '') {
297 $get['cmd'] = $post['cmd'] = $vars['cmd'] = 'tb';
300 // cmd¤âplugin¤â»ØÄꤵ¤ì¤Æ¤¤¤Ê¤¤¾ì¹ç¤Ï¡¢QUERY_STRING¤ò¥Ú¡¼¥¸Ì¾¤«InterWikiName¤Ç¤¢¤ë¤È¤ß¤Ê¤¹
301 if (! isset($vars['cmd']) && ! isset($vars['plugin'])) {
303 $get['cmd'] = $post['cmd'] = $vars['cmd'] = 'read';
305 if ($arg == '') $arg = $defaultpage;
306 $arg = rawurldecode($arg);
307 $arg = strip_bracket($arg);
308 $arg = input_filter($arg);
309 $get['page'] = $post['page'] = $vars['page'] = $arg;
312 // ÆþÎÏ¥Á¥§¥Ã¥¯: 'cmd=' prohibits nasty 'plugin='
313 if (isset($vars['cmd']) && isset($vars['plugin']))
314 unset($get['plugin'], $post['plugin'], $vars['plugin']);
317 /////////////////////////////////////////////////
318 // ½é´üÀßÄê($WikiName,$BracketName¤Ê¤É)
319 // $WikiName = '[A-Z][a-z]+(?:[A-Z][a-z]+)+';
320 // $WikiName = '\b[A-Z][a-z]+(?:[A-Z][a-z]+)+\b';
321 // $WikiName = '(?<![[:alnum:]])(?:[[:upper:]][[:lower:]]+){2,}(?![[:alnum:]])';
322 // $WikiName = '(?<!\w)(?:[A-Z][a-z]+){2,}(?!\w)';
324 // BugTrack/304»ÃÄêÂнè
325 $WikiName = '(?:[A-Z][a-z]+){2,}(?!\w)';
327 // $BracketName = ':?[^\s\]#&<>":]+:?';
328 $BracketName = '(?!\s):?[^\r\n\t\f\[\]<>#&":]+:?(?<!\s)';
331 $InterWikiName = "(\[\[)?((?:(?!\s|:|\]\]).)+):(.+)(?(1)\]\])";
334 $NotePattern = '/\(\(((?:(?>(?:(?!\(\()(?!\)\)(?:[^\)]|$)).)+)|(?R))*)\)\)/ex';
336 /////////////////////////////////////////////////
337 // ½é´üÀßÄê(¥æ¡¼¥¶ÄêµÁ¥ë¡¼¥ëÆɤ߹þ¤ß)
338 require(DATA_HOME . 'rules.ini.php');
340 /////////////////////////////////////////////////
341 // ½é´üÀßÄê(¤½¤Î¾¤Î¥°¥í¡¼¥Ð¥ëÊÑ¿ô)
344 $now = format_date(UTIME);
346 // skinÆâ¤ÇDTDÀë¸À¤òÀÚ¤êÂؤ¨¤ë¤Î¤Ë»ÈÍÑ¡£paint.inc.phpÂкö
347 $html_transitional = FALSE;
349 // TRUE :XHTML 1.0 Transitional
351 // ¥Õ¥§¥¤¥¹¥Þ¡¼¥¯¤ò$line_rules¤Ë²Ã¤¨¤ë
352 if ($usefacemark) $line_rules += $facemark_rules;
353 unset($facemark_rules);
355 // ¼ÂÂλ²¾È¥Ñ¥¿¡¼¥ó¤ª¤è¤Ó¥·¥¹¥Æ¥à¤Ç»ÈÍѤ¹¤ë¥Ñ¥¿¡¼¥ó¤ò$line_rules¤Ë²Ã¤¨¤ë
356 //$entity_pattern = '[a-zA-Z0-9]{2,8}';
357 $entity_pattern = trim(join('', file(CACHE_DIR . 'entities.dat')));
359 $line_rules = array_merge(array(
360 '&(#[0-9]+|#x[0-9a-f]+|' . $entity_pattern . ');' => '&$1;',
361 "\r" => "<br />\n", /* ¹ÔËö¤Ë¥Á¥ë¥À¤Ï²þ¹Ô */
362 '#related$' => '<del>#related</del>',
363 '^#contents$' => '<del>#contents</del>'