OSDN Git Service

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