OSDN Git Service

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