OSDN Git Service

BugTrack/577 Fixed rename plugin's XTML parse error
[pukiwiki/pukiwiki.git] / init.php
1 <?php
2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
4 //
5 // $Id: init.php,v 1.88 2004/07/05 12:13:56 henoheno Exp $
6 //
7
8 /////////////////////////////////////////////////
9 // ½é´üÀßÄê (¥¨¥é¡¼½ÐÎÏ¥ì¥Ù¥ë)
10 error_reporting(E_ERROR | E_PARSE);     // (E_WARNING | E_NOTICE)¤ò½ü³°¤·¤Æ¤¤¤Þ¤¹
11 //error_reporting(E_ALL);
12
13 /////////////////////////////////////////////////
14 // ½é´üÀßÄê (ʸ»ú¥¨¥ó¥³¡¼¥É¡¢¸À¸ì)
15 define('LANG','ja');    // Select 'ja' or 'en'
16 define('SOURCE_ENCODING','EUC-JP');
17
18 // mbstring extension ´ØÏ¢
19 mb_language('Japanese');
20 mb_internal_encoding(SOURCE_ENCODING);
21 ini_set('mbstring.http_input', 'pass');
22 mb_http_output('pass');
23 mb_detect_order('auto');
24
25 /////////////////////////////////////////////////
26 // ½é´üÀßÄê(ÀßÄê¥Õ¥¡¥¤¥ë¤Î¾ì½ê)
27 define('LANG_FILE', DATA_HOME . LANG . '.lng');
28 define('INI_FILE',  DATA_HOME . './pukiwiki.ini.php');
29
30 /////////////////////////////////////////////////
31 // ½é´üÀßÄê (¥Ð¡¼¥¸¥ç¥ó/Ãøºî¸¢)
32 define('S_VERSION','1.4.3');
33 define('S_COPYRIGHT','
34 <strong>"PukiWiki" '.S_VERSION.'</strong> Copyright &copy; 2001-2004
35 <a href="http://pukiwiki.org">PukiWiki Developers Team</a>.
36 License is <a href="http://www.gnu.org/">GNU/GPL</a>.<br />
37 Based on "PukiWiki" 1.3 by <a href="http://factage.com/sng/">sng</a>
38 ');
39
40 /////////////////////////////////////////////////
41 // ½é´üÀßÄê (¥µ¡¼¥ÐÊÑ¿ô)
42 foreach (array('SCRIPT_NAME', 'SERVER_ADMIN', 'SERVER_NAME',
43         'SERVER_PORT', 'SERVER_SOFTWARE') as $key) {
44         define($key, isset($_SERVER[$key]) ? $_SERVER[$key] : '');
45         unset(${$key}, $_SERVER[$key], $HTTP_SERVER_VARS[$key]);
46 }
47
48 /////////////////////////////////////////////////
49 // ½é´üÀßÄê (¥°¥í¡¼¥Ð¥ëÊÑ¿ô)
50 // ¥µ¡¼¥Ð¤«¤éÍè¤ëÊÑ¿ô
51 $vars = array();
52 // µÓÃí
53 $foot_explain = array();
54 // ´ØÏ¢¤¹¤ë¥Ú¡¼¥¸
55 $related = array();
56 // <head>Æâ¤ËÄɲ乤륿¥°
57 $head_tags = array();
58
59 /////////////////////////////////////////////////
60 // ½é´üÀßÄê(»þ´Ö)
61 define('LOCALZONE',date('Z'));
62 define('UTIME',time() - LOCALZONE);
63 define('MUTIME',getmicrotime());
64
65 /////////////////////////////////////////////////
66 // ¥Õ¥¡¥¤¥ëÆɤ߹þ¤ß
67 $die = '';
68 foreach(array('LANG_FILE', 'INI_FILE') as $file){
69         if (!file_exists(constant($file)) || !is_readable(constant($file))) {
70                 $die = "${die}File is not found. ($file)\n";
71         } else {
72                 require(constant($file));
73         }
74 }
75 if ($die) { die_message(nl2br("\n\n" . $die)); }
76
77 /////////////////////////////////////////////////
78 // INI_FILE: $script: ½é´üÀßÄê
79 if (!isset($script) or $script == '') {
80         $script = get_script_uri();
81         if ($script === FALSE or (php_sapi_name() == 'cgi' and !is_url($script,TRUE))) {
82                 die_message('get_script_uri() failed: Please set $script at INI_FILE manually.');
83         }
84 }
85
86 /////////////////////////////////////////////////
87 // INI_FILE: $agents:  UserAgent¤Î¼±ÊÌ
88
89 $ua = 'HTTP_USER_AGENT';
90 $user_agent = $matches = array();
91
92 $user_agent['agent'] = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
93 unset(${$ua}, $_SERVER[$ua], $HTTP_SERVER_VARS[$ua], $ua);      // safety
94
95 foreach ($agents as $agent) {
96         if (preg_match($agent['pattern'], $user_agent['agent'], $matches)) {
97                 $user_agent['profile'] = isset($agent['profile']) ? $agent['profile'] : '';
98                 $user_agent['name']    = isset($matches[1]) ? $matches[1] : ''; // device or browser name
99                 $user_agent['vers']    = isset($matches[2]) ? $matches[2] : ''; // 's version
100                 break;
101         }
102 }
103 unset($agents, $matches);
104
105 // Profile-related init and setting
106 define('UA_PROFILE', isset($user_agent['profile']) ? $user_agent['profile'] : '');
107
108 //// Not needed now
109 // define('INI_PRO_FILE', SUB_DIR . 'init.' . UA_PROFILE . '.php');
110 // if (file_exists(INI_PRO_FILE) && is_readable(INI_PRO_FILE)) {
111 //      require(INI_PRO_FILE);  // A chance to set or rewrite $user_agent['name'] and ['vers'] automatically
112 // }
113
114 define('UA_INI_FILE', UA_PROFILE . '.ini.php');
115 if (!file_exists(UA_INI_FILE) || !is_readable(UA_INI_FILE)) {
116         die_message('UA_INI_FILE for "' . UA_PROFILE . '" not found.');
117 } else {
118         require(UA_INI_FILE); // Also manually
119 }
120
121 define('UA_NAME', isset($user_agent['name']) ? $user_agent['name'] : '');
122 define('UA_VERS', isset($user_agent['vers']) ? $user_agent['vers'] : '');
123 unset($user_agent);     // Unset after reading UA_INI_FILE
124
125 /////////////////////////////////////////////////
126 // ¥Ç¥£¥ì¥¯¥È¥ê¤Î¥Á¥§¥Ã¥¯
127
128 $die = '';
129 foreach(array('DATA_DIR', 'DIFF_DIR', 'BACKUP_DIR', 'CACHE_DIR') as $dir){
130         if(!is_writable(constant($dir))) {
131                 $die = "${die}Directory is not found or not writable ($dir)\n";
132         }
133 }
134
135 // ÀßÄê¥Õ¥¡¥¤¥ë¤ÎÊÑ¿ô¥Á¥§¥Ã¥¯
136 $temp = '';
137 foreach(array('rss_max', 'page_title', 'note_hr', 'related_link', 'show_passage',
138         'rule_related_str', 'load_template_func') as $var){
139         if (!isset(${$var})) { $temp .= "\$$var\n"; }
140 }
141 if ($temp) {
142         if ($die) { $die .= "\n"; }     // A breath
143         $die = "${die}Variable(s) not found: (Maybe the old *.ini.php?)\n" . $temp;
144 }
145
146 $temp = '';
147 foreach(array('LANG', 'PLUGIN_DIR') as $def){
148         if (!defined($def)) $temp .= "$def\n";
149 }
150 if ($temp) {
151         if ($die) { $die .= "\n"; }     // A breath
152         $die = "${die}Define(s) not found: (Maybe the old *.ini.php?)\n" . $temp;
153 }
154
155 if($die){ die_message(nl2br("\n\n" . $die)); }
156 unset($die, $temp);
157
158 /////////////////////////////////////////////////
159 // É¬¿Ü¤Î¥Ú¡¼¥¸¤¬Â¸ºß¤·¤Ê¤±¤ì¤Ð¡¢¶õ¤Î¥Õ¥¡¥¤¥ë¤òºîÀ®¤¹¤ë
160
161 foreach(array($defaultpage, $whatsnew, $interwiki) as $page){
162         if (!is_page($page)) { touch(get_filename($page)); }
163 }
164
165 /////////////////////////////////////////////////
166 // ³°Éô¤«¤é¤¯¤ëÊÑ¿ô¤ò¥Á¥§¥Ã¥¯
167
168 // Prohibit $_GET['msg'] attack
169 if (isset($_GET['msg'])) die_message('Sorry, already reserved: msg=');
170
171 $get    = sanitize($_GET);
172 $post   = sanitize($_POST);
173 $cookie = sanitize($_COOKIE);
174
175 // Expire risk
176 unset($_GET, $_POST, $HTTP_GET_VARS, $HTTP_POST_VARS, $_REQUEST, $_COOKIE);     //, 'SERVER', 'ENV', 'SESSION', ...
177
178 /////////////////////////////////////////////////
179 // Ê¸»ú¥³¡¼¥É¤òÊÑ´¹
180
181 // <form> ¤ÇÁ÷¿®¤µ¤ì¤¿Ê¸»ú (¥Ö¥é¥¦¥¶¤¬¥¨¥ó¥³¡¼¥É¤·¤¿¥Ç¡¼¥¿) ¤Î¥³¡¼¥É¤òÊÑ´¹
182 // post ¤Ï¾ï¤Ë <form> ¤Ê¤Î¤Ç¡¢É¬¤ºÊÑ´¹
183 if (array_key_exists('encode_hint',$post))
184 {
185         // html.php ¤ÎÃæ¤Ç¡¢<form> ¤Ë encode_hint ¤ò»Å¹þ¤ó¤Ç¤¤¤ë¤Î¤Ç¡¢É¬¤º encode_hint ¤¬¤¢¤ë¤Ï¤º¡£
186         // encode_hint ¤Î¤ß¤òÍѤ¤¤Æ¥³¡¼¥É¸¡½Ð¤¹¤ë¡£
187         // Á´ÂΤò¸«¤Æ¥³¡¼¥É¸¡½Ð¤¹¤ë¤È¡¢µ¡¼ï°Í¸ʸ»ú¤ä¡¢Ì¯¤Ê¥Ð¥¤¥Ê¥ê¥³¡¼¥É¤¬º®Æþ¤·¤¿¾ì¹ç¤Ë¡¢
188         // ¥³¡¼¥É¸¡½Ð¤Ë¼ºÇÔ¤¹¤ë¶²¤ì¤¬¤¢¤ë¤¿¤á¡£
189         $encode = mb_detect_encoding($post['encode_hint']);
190         mb_convert_variables(SOURCE_ENCODING,$encode,$post);
191 }
192 else if (array_key_exists('charset',$post))
193 {
194         // TrackBack Ping¤Ë´Þ¤Þ¤ì¤Æ¤¤¤ë¤³¤È¤¬¤¢¤ë
195         // »ØÄꤵ¤ì¤¿¾ì¹ç¤Ï¡¢¤½¤ÎÆâÍƤÇÊÑ´¹¤ò»î¤ß¤ë
196         if (mb_convert_variables(SOURCE_ENCODING,$post['charset'],$post) !== $post['charset'])
197         {
198                 // ¤¦¤Þ¤¯¤¤¤«¤Ê¤«¤Ã¤¿¾ì¹ç¤Ï¥³¡¼¥É¸¡½Ð¤ÎÀßÄê¤ÇÊÑ´¹¤·¤Ê¤ª¤·
199                 mb_convert_variables(SOURCE_ENCODING,'auto',$post);
200         }
201 }
202 else if (count($post) > 0)
203 {
204         // encode_hint ¤¬Ìµ¤¤¤È¤¤¤¦¤³¤È¤Ï¡¢Ìµ¤¤¤Ï¤º¡£
205         // ¥Ç¥Ð¥Ã¥°ÍѤˡ¢¼è¤ê¤¢¤¨¤º¡¢·Ù¹ð¥á¥Ã¥»¡¼¥¸¤ò½Ð¤·¤Æ¤ª¤­¤Þ¤¹¡£
206 //      echo "<p>Warning: 'encode_hint' field is not found in the posted data.</p>\n";
207         // Á´Éô¤Þ¤È¤á¤Æ¡¢¥³¡¼¥É¸¡½Ð¡¢ÊÑ´¹
208         mb_convert_variables(SOURCE_ENCODING,'auto',$post);
209 }
210
211 // get ¤Ï <form> ¤«¤é¤Î¾ì¹ç¤È¡¢<a href="http;//script/?query> ¤Î¾ì¹ç¤¬¤¢¤ë
212 if (array_key_exists('encode_hint',$get))
213 {
214         // <form> ¤Î¾ì¹ç¤Ï¡¢¥Ö¥é¥¦¥¶¤¬¥¨¥ó¥³¡¼¥É¤·¤Æ¤¤¤ë¤Î¤Ç¡¢¥³¡¼¥É¸¡½Ð¡¦ÊÑ´¹¤¬É¬Íס£
215         // encode_hint ¤¬´Þ¤Þ¤ì¤Æ¤¤¤ë¤Ï¤º¤Ê¤Î¤Ç¡¢¤½¤ì¤ò¸«¤Æ¡¢¥³¡¼¥É¸¡½Ð¤·¤¿¸å¡¢ÊÑ´¹¤¹¤ë¡£
216         // Íýͳ¤Ï¡¢post ¤ÈƱÍÍ
217         $encode = mb_detect_encoding($get['encode_hint']);
218         mb_convert_variables(SOURCE_ENCODING,$encode,$get);
219 }       
220 // <a href...> ¤Î¾ì¹ç¤Ï¡¢¥µ¡¼¥Ð¡¼¤¬ rawurlencode ¤·¤Æ¤¤¤ë¤Î¤Ç¡¢¥³¡¼¥ÉÊÑ´¹¤ÏÉÔÍ×
221
222 // QUERY_STRING¤ò¼èÆÀ
223 // cmd¤âplugin¤â»ØÄꤵ¤ì¤Æ¤¤¤Ê¤¤¾ì¹ç¤Ï¡¢QUERY_STRING¤ò¥Ú¡¼¥¸Ì¾¤«InterWikiName¤Ç¤¢¤ë¤È¤ß¤Ê¤¹°Ù
224 // ¤Þ¤¿¡¢URI ¤ò urlencode ¤»¤º¤Ë¼êÂǤÁ¤ÇÆþÎϤ·¤¿¾ì¹ç¤ËÂн褹¤ë°Ù
225 $arg = '';
226 if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING']) {
227         $arg = $_SERVER['QUERY_STRING'];
228 } else if (isset($_SERVER['argv']) && count($_SERVER['argv'])) {
229         $arg = $_SERVER['argv'][0];
230 }
231
232 // unset QUERY_STRINGs
233 foreach (array('QUERY_STRING', 'argv', 'argc') as $key) {
234         unset(${$key}, $_SERVER[$key], $HTTP_SERVER_VARS[$key]);
235 }
236 // $_SERVER['REQUEST_URI'] is used by func.php NOW
237 unset($REQUEST_URI, $HTTP_SERVER_VARS['REQUEST_URI']);
238
239 // ¥µ¥Ë¥¿¥¤¥º (\0 ½üµî)
240 $arg = sanitize($arg);
241
242 // URI ¼êÂǤξì¹ç¡¢¥³¡¼¥ÉÊÑ´¹¤·¡¢get[] ¤Ë¾å½ñ¤­
243 // mb_convert_variables¤Î¥Ð¥°(?)Âкö ÇÛÎó¤ÇÅϤµ¤Ê¤¤¤ÈÍî¤Á¤ë
244 $arg = array($arg);
245 mb_convert_variables(SOURCE_ENCODING,'auto',$arg);
246 $arg = $arg[0];
247
248 foreach (explode('&',$arg) as $tmp_string)
249 {
250         if (preg_match('/^([^=]+)=(.+)/',$tmp_string,$matches)
251                 and mb_detect_encoding($matches[2]) != 'ASCII')
252         {
253                 $get[$matches[1]] = $matches[2];
254         }
255 }
256
257 /////////////////////////////////////////////////
258 // GET + POST = $vars
259
260 $vars = array_merge($get, $post);
261
262 // ÆþÎÏ¥Á¥§¥Ã¥¯: cmd, plugin ¤Îʸ»úÎó¤Ï±Ñ¿ô»ú°Ê³°¤¢¤ê¤¨¤Ê¤¤
263 foreach(array('cmd', 'plugin') as $var){
264         if (array_key_exists($var, $vars) &&
265             ! preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $vars[$var])) {
266                 unset($get[$var], $post[$var], $vars[$var]);
267         }
268 }
269
270 // À°·Á: page, strip_bracket()
271 if (array_key_exists('page', $vars)) {
272         $get['page'] = $post['page'] = $vars['page']  = strip_bracket($vars['page']);
273 } else {
274         $get['page'] = $post['page'] = $vars['page'] = '';
275 }
276
277 // À°·Á: msg, ²þ¹Ô¤ò¼è¤ê½ü¤¯
278 if (isset($vars['msg'])) {
279         $get['msg'] = $post['msg'] = $vars['msg'] = str_replace("\r",'',$vars['msg']);
280 }
281
282 // ¸åÊý¸ß´¹À­ (?md5=...)
283 if (array_key_exists('md5', $vars) and $vars['md5'] != '') {
284         $get['cmd'] = $post['cmd'] = $vars['cmd'] = 'md5';
285 }
286
287 // TrackBack Ping
288 if (array_key_exists('tb_id', $vars) and $vars['tb_id'] != '') {
289         $get['cmd'] = $post['cmd'] = $vars['cmd'] = 'tb';
290 }
291
292 // cmd¤âplugin¤â»ØÄꤵ¤ì¤Æ¤¤¤Ê¤¤¾ì¹ç¤Ï¡¢QUERY_STRING¤ò¥Ú¡¼¥¸Ì¾¤«InterWikiName¤Ç¤¢¤ë¤È¤ß¤Ê¤¹
293 if (!array_key_exists('cmd',$vars)  and !array_key_exists('plugin',$vars))
294 {
295         if ($arg == '')
296         {
297                 //¤Ê¤Ë¤â»ØÄꤵ¤ì¤Æ¤¤¤Ê¤«¤Ã¤¿¾ì¹ç¤Ï$defaultpage¤òɽ¼¨
298                 $arg = $defaultpage;
299         }               
300         $arg = rawurldecode($arg);
301         $arg = strip_bracket($arg);
302         $arg = sanitize($arg);
303
304         $get['cmd']  = $post['cmd']  = $vars['cmd']  = 'read';
305         $get['page'] = $post['page'] = $vars['page'] = $arg;
306 }
307
308 // ÆþÎÏ¥Á¥§¥Ã¥¯: 'cmd=' prohibits nasty 'plugin='
309 if (isset($vars['cmd']) && isset($vars['plugin']))
310         unset($get['plugin'], $post['plugin'], $vars['plugin']);
311
312
313 /////////////////////////////////////////////////
314 // ½é´üÀßÄê($WikiName,$BracketName¤Ê¤É)
315 // $WikiName = '[A-Z][a-z]+(?:[A-Z][a-z]+)+';
316 // $WikiName = '\b[A-Z][a-z]+(?:[A-Z][a-z]+)+\b';
317 // $WikiName = '(?<![[:alnum:]])(?:[[:upper:]][[:lower:]]+){2,}(?![[:alnum:]])';
318 // $WikiName = '(?<!\w)(?:[A-Z][a-z]+){2,}(?!\w)';
319
320 // BugTrack/304»ÃÄêÂнè
321 $WikiName = '(?:[A-Z][a-z]+){2,}(?!\w)';
322
323 // $BracketName = ':?[^\s\]#&<>":]+:?';
324 $BracketName = '(?!\s):?[^\r\n\t\f\[\]<>#&":]+:?(?<!\s)';
325
326 // InterWiki
327 $InterWikiName = "(\[\[)?((?:(?!\s|:|\]\]).)+):(.+)(?(1)\]\])";
328
329 // Ãí¼á
330 $NotePattern = '/\(\(((?:(?>(?:(?!\(\()(?!\)\)(?:[^\)]|$)).)+)|(?R))*)\)\)/ex';
331
332 /////////////////////////////////////////////////
333 // ½é´üÀßÄê(¥æ¡¼¥¶ÄêµÁ¥ë¡¼¥ëÆɤ߹þ¤ß)
334 require(DATA_HOME . 'rules.ini.php');
335
336 /////////////////////////////////////////////////
337 // ½é´üÀßÄê(¤½¤Î¾¤Î¥°¥í¡¼¥Ð¥ëÊÑ¿ô)
338
339 // ¸½ºß»þ¹ï
340 $now = format_date(UTIME);
341
342 // skinÆâ¤ÇDTDÀë¸À¤òÀÚ¤êÂؤ¨¤ë¤Î¤Ë»ÈÍÑ¡£paint.inc.phpÂкö
343 $html_transitional = FALSE;
344 // FALSE:XHTML 1.1
345 // TRUE :XHTML 1.0 Transitional
346
347 // ¥Õ¥§¥¤¥¹¥Þ¡¼¥¯¤ò$line_rules¤Ë²Ã¤¨¤ë
348 if ($usefacemark) { $line_rules += $facemark_rules; }
349 unset($facemark_rules);
350
351 // ¼ÂÂλ²¾È¥Ñ¥¿¡¼¥ó¤ª¤è¤Ó¥·¥¹¥Æ¥à¤Ç»ÈÍѤ¹¤ë¥Ñ¥¿¡¼¥ó¤ò$line_rules¤Ë²Ã¤¨¤ë
352 //$entity_pattern = '[a-zA-Z0-9]{2,8}';
353 $entity_pattern = trim(join('',file(CACHE_DIR.'entities.dat')));
354
355 $line_rules = array_merge(array(
356         '&amp;(#[0-9]+|#x[0-9a-f]+|' . $entity_pattern . ');' => '&$1;',
357         "\r"          => "<br />\n",    /* ¹ÔËö¤Ë¥Á¥ë¥À¤Ï²þ¹Ô */
358         '#related$'   => '<del>#related</del>',
359         '^#contents$' => '<del>#contents</del>'
360 ), $line_rules);
361
362 ?>