OSDN Git Service

A little cleanup:
[pukiwiki/pukiwiki.git] / lib / init.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // $Id: init.php,v 1.57 2011/01/25 15:01:01 henoheno Exp $
4 // Copyright (C)
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
8 //
9 // Init PukiWiki here
10
11 // PukiWiki version / Copyright / Licence
12
13 define('S_VERSION', '1.4.7');
14 define('S_COPYRIGHT',
15         '<strong>PukiWiki ' . S_VERSION . '</strong>' .
16         ' Copyright &copy; 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>'
20 );
21
22 /////////////////////////////////////////////////
23 // Init server variables
24
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]);
29 }
30
31 /////////////////////////////////////////////////
32 // Init grobal variables
33
34 $foot_explain = array();        // Footnotes
35 $related      = array();        // Related pages
36 $head_tags    = array();        // XHTML tags in <head></head>
37
38 /////////////////////////////////////////////////
39 // Time settings
40
41 define('LOCALZONE', date('Z'));
42 define('UTIME', time() - LOCALZONE);
43 define('MUTIME', getmicrotime());
44
45 /////////////////////////////////////////////////
46 // Require INI_FILE
47
48 define('INI_FILE',  DATA_HOME . 'pukiwiki.ini.php');
49 $die = '';
50 if (! file_exists(INI_FILE) || ! is_readable(INI_FILE)) {
51         $die .= 'File is not found. (INI_FILE)' . "\n";
52 } else {
53         require(INI_FILE);
54 }
55 if ($die) die_message(nl2br("\n\n" . $die));
56
57 /////////////////////////////////////////////////
58 // INI_FILE: LANG ¤Ë´ð¤Å¤¯¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°ÀßÄê
59
60 // MB_LANGUAGE: mb_language (for mbstring extension)
61 //   'uni'(means UTF-8), 'English', or 'Japanese'
62 // SOURCE_ENCODING: Internal content encoding (for mbstring extension)
63 //   'UTF-8', 'ASCII', or 'EUC-JP'
64 // CONTENT_CHARSET: Internal content encoding = Output content charset
65 //    (for DTD, htmlsc())
66 //   'UTF-8', 'iso-8859-1', 'EUC-JP' or ...
67
68 switch (LANG){
69 case 'en': define('MB_LANGUAGE', 'English' ); break;
70 case 'ja': define('MB_LANGUAGE', 'Japanese'); break;
71 //UTF-8:case 'ko': define('MB_LANGUAGE', 'Korean'  ); break;
72 //UTF-8:        // See BugTrack2/13 for all hack about Korean support,
73 //UTF-8:        // and give us your report!
74 default: die_message('No such language "' . LANG . '"'); break;
75 }
76
77 //UTF-8:define('PKWK_UTF8_ENABLE', 1);
78 if (defined('PKWK_UTF8_ENABLE')) {
79         define('SOURCE_ENCODING', 'UTF-8');
80         define('CONTENT_CHARSET', 'UTF-8');
81 } else {
82         switch (LANG){
83         case 'en':
84                 define('SOURCE_ENCODING', 'ASCII');
85                 define('CONTENT_CHARSET', 'iso-8859-1');
86                 break;
87         case 'ja':
88                 define('SOURCE_ENCODING', 'EUC-JP');
89                 define('CONTENT_CHARSET', 'EUC-JP');
90                 break;
91         }
92 }
93
94 mb_language(MB_LANGUAGE);
95 mb_internal_encoding(SOURCE_ENCODING);
96 ini_set('mbstring.http_input', 'pass');
97 mb_http_output('pass');
98 mb_detect_order('auto');
99
100 /////////////////////////////////////////////////
101 // INI_FILE: Require LANG_FILE
102
103 define('LANG_FILE_HINT', DATA_HOME . LANG . '.lng.php');        // For encoding hint
104 define('LANG_FILE',      DATA_HOME . UI_LANG . '.lng.php');     // For UI resource
105 $die = '';
106 foreach (array('LANG_FILE_HINT', 'LANG_FILE') as $langfile) {
107         if (! file_exists(constant($langfile)) || ! is_readable(constant($langfile))) {
108                 $die .= 'File is not found or not readable. (' . $langfile . ')' . "\n";
109         } else {
110                 require_once(constant($langfile));
111         }
112 }
113 if ($die) die_message(nl2br("\n\n" . $die));
114
115 /////////////////////////////////////////////////
116 // LANG_FILE: Init encoding hint
117
118 define('PKWK_ENCODING_HINT', isset($_LANG['encode_hint'][LANG]) ? $_LANG['encode_hint'][LANG] : '');
119 unset($_LANG['encode_hint']);
120
121 /////////////////////////////////////////////////
122 // LANG_FILE: Init severn days of the week
123
124 $weeklabels = $_msg_week;
125
126 /////////////////////////////////////////////////
127 // INI_FILE: Init $script
128
129 if (isset($script)) {
130         get_script_uri($script); // Init manually
131 } else {
132         $script = get_script_uri(); // Init automatically
133 }
134
135 /////////////////////////////////////////////////
136 // INI_FILE: $agents:  UserAgent¤Î¼±ÊÌ
137
138 $ua = 'HTTP_USER_AGENT';
139 $user_agent = $matches = array();
140
141 $user_agent['agent'] = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
142 unset(${$ua}, $_SERVER[$ua], $HTTP_SERVER_VARS[$ua], $ua);      // safety
143
144 foreach ($agents as $agent) {
145         if (preg_match($agent['pattern'], $user_agent['agent'], $matches)) {
146                 $user_agent['profile'] = isset($agent['profile']) ? $agent['profile'] : '';
147                 $user_agent['name']    = isset($matches[1]) ? $matches[1] : ''; // device or browser name
148                 $user_agent['vers']    = isset($matches[2]) ? $matches[2] : ''; // 's version
149                 break;
150         }
151 }
152 unset($agents, $matches);
153
154 // Profile-related init and setting
155 define('UA_PROFILE', isset($user_agent['profile']) ? $user_agent['profile'] : '');
156
157 define('UA_INI_FILE', DATA_HOME . UA_PROFILE . '.ini.php');
158 if (! file_exists(UA_INI_FILE) || ! is_readable(UA_INI_FILE)) {
159         die_message('UA_INI_FILE for "' . UA_PROFILE . '" not found.');
160 } else {
161         require(UA_INI_FILE); // Also manually
162 }
163
164 define('UA_NAME', isset($user_agent['name']) ? $user_agent['name'] : '');
165 define('UA_VERS', isset($user_agent['vers']) ? $user_agent['vers'] : '');
166 unset($user_agent);     // Unset after reading UA_INI_FILE
167
168 /////////////////////////////////////////////////
169 // ¥Ç¥£¥ì¥¯¥È¥ê¤Î¥Á¥§¥Ã¥¯
170
171 $die = '';
172 foreach(array('DATA_DIR', 'DIFF_DIR', 'BACKUP_DIR', 'CACHE_DIR') as $dir){
173         if (! is_writable(constant($dir)))
174                 $die .= 'Directory is not found or not writable (' . $dir . ')' . "\n";
175 }
176
177 // ÀßÄê¥Õ¥¡¥¤¥ë¤ÎÊÑ¿ô¥Á¥§¥Ã¥¯
178 $temp = '';
179 foreach(array('rss_max', 'page_title', 'note_hr', 'related_link', 'show_passage',
180         'rule_related_str', 'load_template_func') as $var){
181         if (! isset(${$var})) $temp .= '$' . $var . "\n";
182 }
183 if ($temp) {
184         if ($die) $die .= "\n"; // A breath
185         $die .= 'Variable(s) not found: (Maybe the old *.ini.php?)' . "\n" . $temp;
186 }
187
188 $temp = '';
189 foreach(array('LANG', 'PLUGIN_DIR') as $def){
190         if (! defined($def)) $temp .= $def . "\n";
191 }
192 if ($temp) {
193         if ($die) $die .= "\n"; // A breath
194         $die .= 'Define(s) not found: (Maybe the old *.ini.php?)' . "\n" . $temp;
195 }
196
197 if($die) die_message(nl2br("\n\n" . $die));
198 unset($die, $temp);
199
200 /////////////////////////////////////////////////
201 // É¬¿Ü¤Î¥Ú¡¼¥¸¤¬Â¸ºß¤·¤Ê¤±¤ì¤Ð¡¢¶õ¤Î¥Õ¥¡¥¤¥ë¤òºîÀ®¤¹¤ë
202
203 foreach(array($defaultpage, $whatsnew, $interwiki) as $page){
204         if (! is_page($page)) touch(get_filename($page));
205 }
206
207 /////////////////////////////////////////////////
208 // ³°Éô¤«¤é¤¯¤ëÊÑ¿ô¤Î¥Á¥§¥Ã¥¯
209
210 // Prohibit $_GET attack
211 foreach (array('msg', 'pass') as $key) {
212         if (isset($_GET[$key])) die_message('Sorry, already reserved: ' . $key . '=');
213 }
214
215 // Expire risk
216 unset($HTTP_GET_VARS, $HTTP_POST_VARS); //, 'SERVER', 'ENV', 'SESSION', ...
217 unset($_REQUEST);       // Considered harmful
218
219 // Remove null character etc.
220 $_GET    = input_filter($_GET);
221 $_POST   = input_filter($_POST);
222 $_COOKIE = input_filter($_COOKIE);
223
224 // Ê¸»ú¥³¡¼¥ÉÊÑ´¹ ($_POST)
225 // <form> ¤ÇÁ÷¿®¤µ¤ì¤¿Ê¸»ú (¥Ö¥é¥¦¥¶¤¬¥¨¥ó¥³¡¼¥É¤·¤¿¥Ç¡¼¥¿) ¤Î¥³¡¼¥É¤òÊÑ´¹
226 // POST method ¤Ï¾ï¤Ë form ·Ðͳ¤Ê¤Î¤Ç¡¢É¬¤ºÊÑ´¹¤¹¤ë
227 //
228 if (isset($_POST['encode_hint']) && $_POST['encode_hint'] != '') {
229         // do_plugin_xxx() ¤ÎÃæ¤Ç¡¢<form> ¤Ë encode_hint ¤ò»Å¹þ¤ó¤Ç¤¤¤ë¤Î¤Ç¡¢
230         // encode_hint ¤òÍѤ¤¤Æ¥³¡¼¥É¸¡½Ð¤¹¤ë¡£
231         // Á´ÂΤò¸«¤Æ¥³¡¼¥É¸¡½Ð¤¹¤ë¤È¡¢µ¡¼ï°Í¸ʸ»ú¤ä¡¢Ì¯¤Ê¥Ð¥¤¥Ê¥ê
232         // ¥³¡¼¥É¤¬º®Æþ¤·¤¿¾ì¹ç¤Ë¡¢¥³¡¼¥É¸¡½Ð¤Ë¼ºÇÔ¤¹¤ë¶²¤ì¤¬¤¢¤ë¡£
233         $encode = mb_detect_encoding($_POST['encode_hint']);
234         mb_convert_variables(SOURCE_ENCODING, $encode, $_POST);
235
236 } else if (isset($_POST['charset']) && $_POST['charset'] != '') {
237         // TrackBack Ping ¤Ç»ØÄꤵ¤ì¤Æ¤¤¤ë¤³¤È¤¬¤¢¤ë
238         // ¤¦¤Þ¤¯¤¤¤«¤Ê¤¤¾ì¹ç¤Ï¼«Æ°¸¡½Ð¤ËÀÚ¤êÂؤ¨
239         if (mb_convert_variables(SOURCE_ENCODING,
240             $_POST['charset'], $_POST) !== $_POST['charset']) {
241                 mb_convert_variables(SOURCE_ENCODING, 'auto', $_POST);
242         }
243
244 } else if (! empty($_POST)) {
245         // Á´Éô¤Þ¤È¤á¤Æ¡¢¼«Æ°¸¡½Ð¡¿ÊÑ´¹
246         mb_convert_variables(SOURCE_ENCODING, 'auto', $_POST);
247 }
248
249 // Ê¸»ú¥³¡¼¥ÉÊÑ´¹ ($_GET)
250 // GET method ¤Ï form ¤«¤é¤Î¾ì¹ç¤È¡¢<a href="http://script/?key=value> ¤Î¾ì¹ç¤¬¤¢¤ë
251 // <a href...> ¤Î¾ì¹ç¤Ï¡¢¥µ¡¼¥Ð¡¼¤¬ rawurlencode ¤·¤Æ¤¤¤ë¤Î¤Ç¡¢¥³¡¼¥ÉÊÑ´¹¤ÏÉÔÍ×
252 if (isset($_GET['encode_hint']) && $_GET['encode_hint'] != '')
253 {
254         // form ·Ðͳ¤Î¾ì¹ç¤Ï¡¢¥Ö¥é¥¦¥¶¤¬¥¨¥ó¥³¡¼¥É¤·¤Æ¤¤¤ë¤Î¤Ç¡¢¥³¡¼¥É¸¡½Ð¡¦ÊÑ´¹¤¬É¬Íס£
255         // encode_hint ¤¬´Þ¤Þ¤ì¤Æ¤¤¤ë¤Ï¤º¤Ê¤Î¤Ç¡¢¤½¤ì¤ò¸«¤Æ¡¢¥³¡¼¥É¸¡½Ð¤·¤¿¸å¡¢ÊÑ´¹¤¹¤ë¡£
256         // Íýͳ¤Ï¡¢post ¤ÈƱÍÍ
257         $encode = mb_detect_encoding($_GET['encode_hint']);
258         mb_convert_variables(SOURCE_ENCODING, $encode, $_GET);
259 }
260
261
262 /////////////////////////////////////////////////
263 // QUERY_STRING¤ò¼èÆÀ
264
265 // cmd¤âplugin¤â»ØÄꤵ¤ì¤Æ¤¤¤Ê¤¤¾ì¹ç¤Ï¡¢QUERY_STRING¤ò
266 // ¥Ú¡¼¥¸Ì¾¤«InterWikiName¤Ç¤¢¤ë¤È¤ß¤Ê¤¹
267 $arg = '';
268 if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING']) {
269         $arg = & $_SERVER['QUERY_STRING'];
270 } else if (isset($_SERVER['argv']) && ! empty($_SERVER['argv'])) {
271         $arg = & $_SERVER['argv'][0];
272 }
273 if (PKWK_QUERY_STRING_MAX && strlen($arg) > PKWK_QUERY_STRING_MAX) {
274         // Something nasty attack?
275         pkwk_common_headers();
276         sleep(1);       // Fake processing, and/or process other threads
277         echo('Query string too long');
278         exit;
279 }
280 $arg = input_filter($arg); // \0 ½üµî
281
282 // unset QUERY_STRINGs
283 foreach (array('QUERY_STRING', 'argv', 'argc') as $key) {
284         unset(${$key}, $_SERVER[$key], $HTTP_SERVER_VARS[$key]);
285 }
286 // $_SERVER['REQUEST_URI'] is used at func.php NOW
287 unset($REQUEST_URI, $HTTP_SERVER_VARS['REQUEST_URI']);
288
289 // mb_convert_variables¤Î¥Ð¥°(?)Âкö: ÇÛÎó¤ÇÅϤµ¤Ê¤¤¤ÈÍî¤Á¤ë
290 $arg = array($arg);
291 mb_convert_variables(SOURCE_ENCODING, 'auto', $arg);
292 $arg = $arg[0];
293
294 /////////////////////////////////////////////////
295 // QUERY_STRING¤òʬ²ò¤·¤Æ¥³¡¼¥ÉÊÑ´¹¤·¡¢$_GET ¤Ë¾å½ñ¤­
296
297 // URI ¤ò urlencode ¤»¤º¤ËÆþÎϤ·¤¿¾ì¹ç¤ËÂн褹¤ë
298 $matches = array();
299 foreach (explode('&', $arg) as $key_and_value) {
300         if (preg_match('/^([^=]+)=(.+)/', $key_and_value, $matches) &&
301             mb_detect_encoding($matches[2]) != 'ASCII') {
302                 $_GET[$matches[1]] = $matches[2];
303         }
304 }
305 unset($matches);
306
307 /////////////////////////////////////////////////
308 // GET, POST, COOKIE
309
310 $get    = & $_GET;
311 $post   = & $_POST;
312 $cookie = & $_COOKIE;
313
314 // GET + POST = $vars
315 if (empty($_POST)) {
316         $vars = & $_GET;  // Major pattern: Read-only access via GET
317 } else if (empty($_GET)) {
318         $vars = & $_POST; // Minor pattern: Write access via POST etc.
319 } else {
320         $vars = array_merge($_GET, $_POST); // Considered reliable than $_REQUEST
321 }
322
323 // ÆþÎÏ¥Á¥§¥Ã¥¯: 'cmd=' and 'plugin=' can't live together
324 if (isset($vars['cmd']) && isset($vars['plugin']))
325         die('Using both cmd= and plugin= is not allowed');
326
327 // ÆþÎÏ¥Á¥§¥Ã¥¯: cmd, plugin ¤Îʸ»úÎó¤Ï±Ñ¿ô»ú°Ê³°¤¢¤ê¤¨¤Ê¤¤
328 foreach(array('cmd', 'plugin') as $var) {
329         if (isset($vars[$var]) && ! preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $vars[$var]))
330                 unset($get[$var], $post[$var], $vars[$var]);
331 }
332
333 // À°·Á: page, strip_bracket()
334 if (isset($vars['page'])) {
335         $get['page'] = $post['page'] = $vars['page']  = strip_bracket($vars['page']);
336 } else {
337         $get['page'] = $post['page'] = $vars['page'] = '';
338 }
339
340 // À°·Á: msg, ²þ¹Ô¤ò¼è¤ê½ü¤¯
341 if (isset($vars['msg'])) {
342         $get['msg'] = $post['msg'] = $vars['msg'] = str_replace("\r", '', $vars['msg']);
343 }
344
345 // ¸åÊý¸ß´¹À­ (?md5=...)
346 if (isset($get['md5']) && $get['md5'] != '' &&
347     ! isset($vars['cmd']) && ! isset($vars['plugin'])) {
348         $get['cmd'] = $post['cmd'] = $vars['cmd'] = 'md5';
349 }
350
351 // TrackBack Ping
352 if (isset($vars['tb_id']) && $vars['tb_id'] != '') {
353         $get['cmd'] = $post['cmd'] = $vars['cmd'] = 'tb';
354 }
355
356 // cmd¤âplugin¤â»ØÄꤵ¤ì¤Æ¤¤¤Ê¤¤¾ì¹ç¤Ï¡¢QUERY_STRING¤ò¥Ú¡¼¥¸Ì¾¤«InterWikiName¤Ç¤¢¤ë¤È¤ß¤Ê¤¹
357 if (! isset($vars['cmd']) && ! isset($vars['plugin'])) {
358
359         $get['cmd']  = $post['cmd']  = $vars['cmd']  = 'read';
360
361         if ($arg == '') $arg = $defaultpage;
362         $arg = rawurldecode($arg);
363         $arg = strip_bracket($arg);
364         $arg = input_filter($arg);
365         $get['page'] = $post['page'] = $vars['page'] = $arg;
366 }
367
368 /////////////////////////////////////////////////
369 // ½é´üÀßÄê($WikiName,$BracketName¤Ê¤É)
370 // $WikiName = '[A-Z][a-z]+(?:[A-Z][a-z]+)+';
371 // $WikiName = '\b[A-Z][a-z]+(?:[A-Z][a-z]+)+\b';
372 // $WikiName = '(?<![[:alnum:]])(?:[[:upper:]][[:lower:]]+){2,}(?![[:alnum:]])';
373 // $WikiName = '(?<!\w)(?:[A-Z][a-z]+){2,}(?!\w)';
374
375 // BugTrack/304»ÃÄêÂнè
376 $WikiName = '(?:[A-Z][a-z]+){2,}(?!\w)';
377
378 // $BracketName = ':?[^\s\]#&<>":]+:?';
379 $BracketName = '(?!\s):?[^\r\n\t\f\[\]<>#&":]+:?(?<!\s)';
380
381 // InterWiki
382 $InterWikiName = '(\[\[)?((?:(?!\s|:|\]\]).)+):(.+)(?(1)\]\])';
383
384 // Ãí¼á
385 $NotePattern = '/\(\(((?:(?>(?:(?!\(\()(?!\)\)(?:[^\)]|$)).)+)|(?R))*)\)\)/ex';
386
387 /////////////////////////////////////////////////
388 // ½é´üÀßÄê(¥æ¡¼¥¶ÄêµÁ¥ë¡¼¥ëÆɤ߹þ¤ß)
389 require(DATA_HOME . 'rules.ini.php');
390
391 /////////////////////////////////////////////////
392 // ½é´üÀßÄê(¤½¤Î¾¤Î¥°¥í¡¼¥Ð¥ëÊÑ¿ô)
393
394 // ¸½ºß»þ¹ï
395 $now = format_date(UTIME);
396
397 // Æü»þÃÖ´¹¥ë¡¼¥ë¤ò$line_rules¤Ë²Ã¤¨¤ë
398 if ($usedatetime) $line_rules += $datetime_rules;
399 unset($datetime_rules);
400
401 // ¥Õ¥§¥¤¥¹¥Þ¡¼¥¯¤ò$line_rules¤Ë²Ã¤¨¤ë
402 if ($usefacemark) $line_rules += $facemark_rules;
403 unset($facemark_rules);
404
405 // ¼ÂÂλ²¾È¥Ñ¥¿¡¼¥ó¤ª¤è¤Ó¥·¥¹¥Æ¥à¤Ç»ÈÍѤ¹¤ë¥Ñ¥¿¡¼¥ó¤ò$line_rules¤Ë²Ã¤¨¤ë
406 //$entity_pattern = '[a-zA-Z0-9]{2,8}';
407 $entity_pattern = trim(join('', file(CACHE_DIR . 'entities.dat')));
408
409 $line_rules = array_merge(array(
410         '&amp;(#[0-9]+|#x[0-9a-f]+|' . $entity_pattern . ');' => '&$1;',
411         "\r"          => '<br />' . "\n",       /* ¹ÔËö¤Ë¥Á¥ë¥À¤Ï²þ¹Ô */
412 ), $line_rules);
413
414 ?>