OSDN Git Service

BugTrack/579 Move language settings into language resources together
[pukiwiki/pukiwiki.git] / lib / init.php
1 <?php
2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
4 //
5 // $Id: init.php,v 1.14 2004/10/16 05:56:24 henoheno Exp $
6 //
7
8 /////////////////////////////////////////////////
9 // ¥Ð¡¼¥¸¥ç¥ó / Ãøºî¸¢
10 define('S_VERSION', '1.4.4');
11 define('S_COPYRIGHT',
12         '<strong>PukiWiki ' . S_VERSION . '</strong>' .
13         ' Copyright &copy; 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>'
17 );
18
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]);
25 }
26
27 /////////////////////////////////////////////////
28 // ½é´üÀßÄê (¥°¥í¡¼¥Ð¥ëÊÑ¿ô)
29
30 $foot_explain = array();        // µÓÃí
31 $related      = array();        // ´ØÏ¢¤¹¤ë¥Ú¡¼¥¸
32 $head_tags    = array();        // <head>Æâ¤ËÄɲ乤륿¥°
33
34 /////////////////////////////////////////////////
35 // ½é´üÀßÄê(»þ´Ö)
36 define('LOCALZONE', date('Z'));
37 define('UTIME', time() - LOCALZONE);
38 define('MUTIME', getmicrotime());
39
40 /////////////////////////////////////////////////
41 // ½é´üÀßÄê(ÀßÄê¥Õ¥¡¥¤¥ë¤Î¾ì½ê)
42 define('INI_FILE',  DATA_HOME . 'pukiwiki.ini.php');
43
44 // ¥Õ¥¡¥¤¥ëÆɤ߹þ¤ß
45 $die = '';
46 if (! file_exists(INI_FILE) || ! is_readable(INI_FILE)) {
47         $die = "${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 define('LANG_FILE', DATA_HOME . LANG . '.lng');
56
57 // ¥Õ¥¡¥¤¥ëÆɤ߹þ¤ß
58 $die = '';
59 if (! file_exists(LANG_FILE) || ! is_readable(LANG_FILE)) {
60         $die = "${die}File is not found. (LANG_FILE)\n";
61 } else {
62         require(LANG_FILE);
63 }
64 if ($die) die_message(nl2br("\n\n" . $die));
65
66 /////////////////////////////////////////////////
67 // LANG_FILE: mbstring extension ´ØÏ¢
68
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');
74
75 /////////////////////////////////////////////////
76 // LANG_FILE: ÍËÆüÇÛÎó
77
78 $weeklabels = $_msg_week;
79
80 /////////////////////////////////////////////////
81 // INI_FILE: $script: ½é´üÀßÄê
82
83 if (isset($script)) {
84         // Init manually
85         get_script_uri($script);
86 } else {
87         // Init automatically
88         $script = get_script_uri();
89 }
90
91 /////////////////////////////////////////////////
92 // INI_FILE: $agents:  UserAgent¤Î¼±ÊÌ
93
94 $ua = 'HTTP_USER_AGENT';
95 $user_agent = $matches = array();
96
97 $user_agent['agent'] = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
98 unset(${$ua}, $_SERVER[$ua], $HTTP_SERVER_VARS[$ua], $ua);      // safety
99
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
105                 break;
106         }
107 }
108 unset($agents, $matches);
109
110 // Profile-related init and setting
111 define('UA_PROFILE', isset($user_agent['profile']) ? $user_agent['profile'] : '');
112
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.');
116 } else {
117         require(UA_INI_FILE); // Also manually
118 }
119
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
123
124 /////////////////////////////////////////////////
125 // ¥Ç¥£¥ì¥¯¥È¥ê¤Î¥Á¥§¥Ã¥¯
126
127 $die = '';
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";
131 }
132
133 // ÀßÄê¥Õ¥¡¥¤¥ë¤ÎÊÑ¿ô¥Á¥§¥Ã¥¯
134 $temp = '';
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";
138 }
139 if ($temp) {
140         if ($die) $die .= "\n"; // A breath
141         $die .= "Variable(s) not found: (Maybe the old *.ini.php?)\n" . $temp;
142 }
143
144 $temp = '';
145 foreach(array('LANG', 'PLUGIN_DIR') as $def){
146         if (! defined($def)) $temp .= "$def\n";
147 }
148 if ($temp) {
149         if ($die) $die .= "\n"; // A breath
150         $die .= "Define(s) not found: (Maybe the old *.ini.php?)\n" . $temp;
151 }
152
153 if($die) die_message(nl2br("\n\n" . $die));
154 unset($die, $temp);
155
156 /////////////////////////////////////////////////
157 // É¬¿Ü¤Î¥Ú¡¼¥¸¤¬Â¸ºß¤·¤Ê¤±¤ì¤Ð¡¢¶õ¤Î¥Õ¥¡¥¤¥ë¤òºîÀ®¤¹¤ë
158
159 foreach(array($defaultpage, $whatsnew, $interwiki) as $page){
160         if (! is_page($page)) touch(get_filename($page));
161 }
162
163 /////////////////////////////////////////////////
164 // ³°Éô¤«¤é¤¯¤ëÊÑ¿ô¤Î¥Á¥§¥Ã¥¯
165
166 // Prohibit $_GET attack
167 foreach (array('msg', 'pass') as $key) {
168         if (isset($_GET[$key])) die_message("Sorry, already reserved: $key=");
169 }
170
171 // Expire risk
172 unset($HTTP_GET_VARS, $HTTP_POST_VARS); //, 'SERVER', 'ENV', 'SESSION', ...
173 unset($_REQUEST);       // Considered harmful
174
175 // Remove null character etc.
176 $_GET    = input_filter($_GET);
177 $_POST   = input_filter($_POST);
178 $_COOKIE = input_filter($_COOKIE);
179
180 // Ê¸»ú¥³¡¼¥ÉÊÑ´¹ ($_POST)
181 // <form> ¤ÇÁ÷¿®¤µ¤ì¤¿Ê¸»ú (¥Ö¥é¥¦¥¶¤¬¥¨¥ó¥³¡¼¥É¤·¤¿¥Ç¡¼¥¿) ¤Î¥³¡¼¥É¤òÊÑ´¹
182 // POST method ¤Ï¾ï¤Ë form ·Ðͳ¤Ê¤Î¤Ç¡¢É¬¤ºÊÑ´¹¤¹¤ë
183 //
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);
191
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);
198         }
199
200 } else if (! empty($_POST)) {
201         // Á´Éô¤Þ¤È¤á¤Æ¡¢¼«Æ°¸¡½Ð¡¿ÊÑ´¹
202         mb_convert_variables(SOURCE_ENCODING, 'auto', $_POST);
203 }
204
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'] != '')
209 {
210         // form ·Ðͳ¤Î¾ì¹ç¤Ï¡¢¥Ö¥é¥¦¥¶¤¬¥¨¥ó¥³¡¼¥É¤·¤Æ¤¤¤ë¤Î¤Ç¡¢¥³¡¼¥É¸¡½Ð¡¦ÊÑ´¹¤¬É¬Íס£
211         // encode_hint ¤¬´Þ¤Þ¤ì¤Æ¤¤¤ë¤Ï¤º¤Ê¤Î¤Ç¡¢¤½¤ì¤ò¸«¤Æ¡¢¥³¡¼¥É¸¡½Ð¤·¤¿¸å¡¢ÊÑ´¹¤¹¤ë¡£
212         // Íýͳ¤Ï¡¢post ¤ÈƱÍÍ
213         $encode = mb_detect_encoding($_GET['encode_hint']);
214         mb_convert_variables(SOURCE_ENCODING, $encode, $_GET);
215 }
216
217
218 /////////////////////////////////////////////////
219 // QUERY_STRING¤ò¼èÆÀ
220
221 // cmd¤âplugin¤â»ØÄꤵ¤ì¤Æ¤¤¤Ê¤¤¾ì¹ç¤Ï¡¢QUERY_STRING¤ò
222 // ¥Ú¡¼¥¸Ì¾¤«InterWikiName¤Ç¤¢¤ë¤È¤ß¤Ê¤¹
223 $arg = '';
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];
228 }
229 $arg = input_filter($arg); // \0 ½üµî
230
231 // unset QUERY_STRINGs
232 foreach (array('QUERY_STRING', 'argv', 'argc') as $key) {
233         unset(${$key}, $_SERVER[$key], $HTTP_SERVER_VARS[$key]);
234 }
235 // $_SERVER['REQUEST_URI'] is used at func.php NOW
236 unset($REQUEST_URI, $HTTP_SERVER_VARS['REQUEST_URI']);
237
238 // mb_convert_variables¤Î¥Ð¥°(?)Âкö: ÇÛÎó¤ÇÅϤµ¤Ê¤¤¤ÈÍî¤Á¤ë
239 $arg = array($arg);
240 mb_convert_variables(SOURCE_ENCODING, 'auto', $arg);
241 $arg = $arg[0];
242
243 /////////////////////////////////////////////////
244 // QUERY_STRING¤òʬ²ò¤·¤Æ¥³¡¼¥ÉÊÑ´¹¤·¡¢$_GET ¤Ë¾å½ñ¤­
245
246 // URI ¤ò urlencode ¤»¤º¤ËÆþÎϤ·¤¿¾ì¹ç¤ËÂн褹¤ë
247 $matches = array();
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];
252         }
253 }
254 unset($matches);
255
256 /////////////////////////////////////////////////
257 // GET, POST, COOKIE
258
259 $get    = & $_GET;
260 $post   = & $_POST;
261 $cookie = & $_COOKIE;
262
263 // GET + POST = $vars
264 if (empty($_POST)) {
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.
268 } else {
269         $vars = array_merge($_GET, $_POST); // Considered reliable than $_REQUEST
270 }
271
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]);
276 }
277
278 // À°·Á: page, strip_bracket()
279 if (isset($vars['page'])) {
280         $get['page'] = $post['page'] = $vars['page']  = strip_bracket($vars['page']);
281 } else {
282         $get['page'] = $post['page'] = $vars['page'] = '';
283 }
284
285 // À°·Á: msg, ²þ¹Ô¤ò¼è¤ê½ü¤¯
286 if (isset($vars['msg'])) {
287         $get['msg'] = $post['msg'] = $vars['msg'] = str_replace("\r", '', $vars['msg']);
288 }
289
290 // ¸åÊý¸ß´¹À­ (?md5=...)
291 if (isset($vars['md5']) && $vars['md5'] != '') {
292         $get['cmd'] = $post['cmd'] = $vars['cmd'] = 'md5';
293 }
294
295 // TrackBack Ping
296 if (isset($vars['tb_id']) && $vars['tb_id'] != '') {
297         $get['cmd'] = $post['cmd'] = $vars['cmd'] = 'tb';
298 }
299
300 // cmd¤âplugin¤â»ØÄꤵ¤ì¤Æ¤¤¤Ê¤¤¾ì¹ç¤Ï¡¢QUERY_STRING¤ò¥Ú¡¼¥¸Ì¾¤«InterWikiName¤Ç¤¢¤ë¤È¤ß¤Ê¤¹
301 if (! isset($vars['cmd']) && ! isset($vars['plugin'])) {
302
303         $get['cmd']  = $post['cmd']  = $vars['cmd']  = 'read';
304
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;
310 }
311
312 // ÆþÎÏ¥Á¥§¥Ã¥¯: 'cmd=' prohibits nasty 'plugin='
313 if (isset($vars['cmd']) && isset($vars['plugin']))
314         unset($get['plugin'], $post['plugin'], $vars['plugin']);
315
316
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)';
323
324 // BugTrack/304»ÃÄêÂнè
325 $WikiName = '(?:[A-Z][a-z]+){2,}(?!\w)';
326
327 // $BracketName = ':?[^\s\]#&<>":]+:?';
328 $BracketName = '(?!\s):?[^\r\n\t\f\[\]<>#&":]+:?(?<!\s)';
329
330 // InterWiki
331 $InterWikiName = "(\[\[)?((?:(?!\s|:|\]\]).)+):(.+)(?(1)\]\])";
332
333 // Ãí¼á
334 $NotePattern = '/\(\(((?:(?>(?:(?!\(\()(?!\)\)(?:[^\)]|$)).)+)|(?R))*)\)\)/ex';
335
336 /////////////////////////////////////////////////
337 // ½é´üÀßÄê(¥æ¡¼¥¶ÄêµÁ¥ë¡¼¥ëÆɤ߹þ¤ß)
338 require(DATA_HOME . 'rules.ini.php');
339
340 /////////////////////////////////////////////////
341 // ½é´üÀßÄê(¤½¤Î¾¤Î¥°¥í¡¼¥Ð¥ëÊÑ¿ô)
342
343 // ¸½ºß»þ¹ï
344 $now = format_date(UTIME);
345
346 // skinÆâ¤ÇDTDÀë¸À¤òÀÚ¤êÂؤ¨¤ë¤Î¤Ë»ÈÍÑ¡£paint.inc.phpÂкö
347 $html_transitional = FALSE;
348 // FALSE:XHTML 1.1
349 // TRUE :XHTML 1.0 Transitional
350
351 // ¥Õ¥§¥¤¥¹¥Þ¡¼¥¯¤ò$line_rules¤Ë²Ã¤¨¤ë
352 if ($usefacemark) $line_rules += $facemark_rules;
353 unset($facemark_rules);
354
355 // ¼ÂÂλ²¾È¥Ñ¥¿¡¼¥ó¤ª¤è¤Ó¥·¥¹¥Æ¥à¤Ç»ÈÍѤ¹¤ë¥Ñ¥¿¡¼¥ó¤ò$line_rules¤Ë²Ã¤¨¤ë
356 //$entity_pattern = '[a-zA-Z0-9]{2,8}';
357 $entity_pattern = trim(join('', file(CACHE_DIR . 'entities.dat')));
358
359 $line_rules = array_merge(array(
360         '&amp;(#[0-9]+|#x[0-9a-f]+|' . $entity_pattern . ');' => '&$1;',
361         "\r"          => "<br />\n",    /* ¹ÔËö¤Ë¥Á¥ë¥À¤Ï²þ¹Ô */
362         '#related$'   => '<del>#related</del>',
363         '^#contents$' => '<del>#contents</del>'
364 ), $line_rules);
365
366 ?>