OSDN Git Service

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