OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / widgets / wiki_main / include / lib / init.php
1 <?php
2 /**
3  * 初期化ライブラリ
4  *
5  * PHP versions 5
6  *
7  * LICENSE: This source file is licensed under the terms of the GNU General Public License.
8  *
9  * @package    Magic3 Framework
10  * @author     平田直毅(Naoki Hirata) <naoki@aplo.co.jp>
11  * @copyright  Copyright 2006-2010 Magic3 Project.
12  * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
13  * @version    SVN: $Id: init.php 3474 2010-08-13 10:36:48Z fishbone $
14  * @link       http://www.magic3.org
15  */
16 // Copyright (C)
17 //   2002-2006 PukiWiki Developers Team
18 //   2001-2002 Originally written by yu-ji
19 // License: GPL v2 or (at your option) any later version
20 //
21 // Init PukiWiki here
22 // PukiWiki用グローバル変数             // add for Magic3 by naoki on 2008/9/28
23 global $foot_explain;   // Footnotes
24 global $related;        // Related pages
25 global $head_tags;      // XHTML tags in <head></head>
26 global $vars;
27 global $line_rules;
28 global $WikiName;
29 global $BracketName;
30 global $InterWikiName;
31 global $NotePattern;
32 global $weeklabels;
33 global $script;
34 global $now;
35
36 // PukiWiki version / Copyright / Licence
37 define('S_VERSION', '1.4.7');
38 define('S_COPYRIGHT',
39         '<strong>PukiWiki ' . S_VERSION . '</strong>' .
40         ' Copyright &copy; 2001-2006' .
41         ' <a href="http://pukiwiki.sourceforge.jp/">PukiWiki Developers Team</a>.' .
42         ' License is <a href="http://www.gnu.org/licenses/gpl.html">GPL</a>.<br />' .
43         ' Based on "PukiWiki" 1.3 by <a href="http://factage.com/yu-ji/">yu-ji</a>'
44 );
45
46 /////////////////////////////////////////////////
47 // Init server variables
48
49 foreach (array('SCRIPT_NAME', 'SERVER_ADMIN', 'SERVER_NAME',
50         'SERVER_PORT', 'SERVER_SOFTWARE') as $key) {
51         define($key, isset($_SERVER[$key]) ? $_SERVER[$key] : '');
52 // removed for Magic3 by naoki on 2008/9/26
53 //      unset(${$key}, $_SERVER[$key], $HTTP_SERVER_VARS[$key]);
54 }
55
56 /////////////////////////////////////////////////
57 // Init grobal variables
58
59 $foot_explain = array();        // Footnotes
60 $related      = array();        // Related pages
61 $head_tags    = array();        // XHTML tags in <head></head>
62
63 /////////////////////////////////////////////////
64 // Time settings
65
66 define('LOCALZONE', date('Z'));
67 define('UTIME', time() - LOCALZONE);
68 define('MUTIME', getmicrotime());
69
70 /////////////////////////////////////////////////
71 // Require INI_FILE
72
73 //define('INI_FILE',  DATA_HOME . 'pukiwiki.ini.php');
74 // modified for Magic3 by naoki on 2008/9/22
75 define('INI_FILE',  $gEnvManager->getCurrentWidgetIncludePath() . '/conf/pukiwiki.ini.php');
76 $die = '';
77 if (! file_exists(INI_FILE) || ! is_readable(INI_FILE)) {
78         $die .= 'File is not found. (INI_FILE)' . "\n";
79 } else {
80         require(INI_FILE);
81 }
82 if ($die) die_message(nl2br("\n\n" . $die));
83
84 /////////////////////////////////////////////////
85 // INI_FILE: LANG に基づくエンコーディング設定
86
87 // MB_LANGUAGE: mb_language (for mbstring extension)
88 //   'uni'(means UTF-8), 'English', or 'Japanese'
89 // SOURCE_ENCODING: Internal content encoding (for mbstring extension)
90 //   'UTF-8', 'ASCII', or 'EUC-JP'
91 // CONTENT_CHARSET: Internal content encoding = Output content charset (for skin)
92 //   'UTF-8', 'iso-8859-1', 'EUC-JP' or ...
93
94 switch (LANG){
95 case 'en': define('MB_LANGUAGE', 'English' ); break;
96 case 'ja': define('MB_LANGUAGE', 'Japanese'); break;
97 case 'ko': define('MB_LANGUAGE', 'Korean'  ); break;
98         // See BugTrack2/13 for all hack about Korean support,
99         // and give us your report!
100 default: die_message('No such language "' . LANG . '"'); break;
101 }
102
103 // removed for Magic3 by naoki on 2008/9/22
104 //define('PKWK_UTF8_ENABLE', 1);
105 //if (defined('PKWK_UTF8_ENABLE')) {
106         define('SOURCE_ENCODING', 'UTF-8');
107         define('CONTENT_CHARSET', 'UTF-8');
108 /*} else {
109         switch (LANG){
110         case 'en':
111                 define('SOURCE_ENCODING', 'ASCII');
112                 define('CONTENT_CHARSET', 'iso-8859-1');
113                 break;
114         case 'ja':
115                 define('SOURCE_ENCODING', 'EUC-JP');
116                 define('CONTENT_CHARSET', 'EUC-JP');
117                 break;
118         }
119 }
120 mb_language(MB_LANGUAGE);
121 mb_internal_encoding(SOURCE_ENCODING);
122 ini_set('mbstring.http_input', 'pass');
123 mb_http_output('pass');
124 mb_detect_order('auto');
125 */
126
127 /////////////////////////////////////////////////
128 // INI_FILE: Require LANG_FILE
129 //define('LANG_FILE_HINT', DATA_HOME . LANG . '.lng.php');      // For encoding hint
130 //define('LANG_FILE',      DATA_HOME . UI_LANG . '.lng.php');   // For UI resource
131 // modified for Magic3 by naoki on 2008/9/22
132 define('LANG_FILE_HINT', $gEnvManager->getCurrentWidgetIncludePath() . '/lang/' . LANG . '.lng.php');   // For encoding hint
133 define('LANG_FILE',      $gEnvManager->getCurrentWidgetIncludePath() . '/lang/' . UI_LANG . '.lng.php');        // For UI resource
134 $die = '';
135 foreach (array('LANG_FILE_HINT', 'LANG_FILE') as $langfile) {
136         if (! file_exists(constant($langfile)) || ! is_readable(constant($langfile))) {
137                 $die .= 'File is not found or not readable. (' . $langfile . ')' . "\n";
138         } else {
139                 require_once(constant($langfile));
140         }
141 }
142 if ($die) die_message(nl2br("\n\n" . $die));
143
144 /////////////////////////////////////////////////
145 // LANG_FILE: Init encoding hint
146
147 define('PKWK_ENCODING_HINT', isset($_LANG['encode_hint'][LANG]) ? $_LANG['encode_hint'][LANG] : '');
148 unset($_LANG['encode_hint']);
149
150 /////////////////////////////////////////////////
151 // LANG_FILE: Init severn days of the week
152
153 $weeklabels = $_msg_week;
154
155 /////////////////////////////////////////////////
156 // INI_FILE: Init $script
157 if (isset($script)) {
158         get_script_uri($script); // Init manually
159 } else {
160         $script = get_script_uri(); // Init automatically
161 }
162
163 /////////////////////////////////////////////////
164 // INI_FILE: $agents:  UserAgentの識別
165
166 $ua = 'HTTP_USER_AGENT';
167 $user_agent = $matches = array();
168
169 $user_agent['agent'] = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
170 unset(${$ua}, $_SERVER[$ua], $HTTP_SERVER_VARS[$ua], $ua);      // safety
171
172 foreach ($agents as $agent) {
173         if (preg_match($agent['pattern'], $user_agent['agent'], $matches)) {
174                 $user_agent['profile'] = isset($agent['profile']) ? $agent['profile'] : '';
175                 $user_agent['name']    = isset($matches[1]) ? $matches[1] : ''; // device or browser name
176                 $user_agent['vers']    = isset($matches[2]) ? $matches[2] : ''; // 's version
177                 break;
178         }
179 }
180 unset($agents, $matches);
181
182 // Profile-related init and setting
183 define('UA_PROFILE', isset($user_agent['profile']) ? $user_agent['profile'] : '');
184 //define('UA_INI_FILE', DATA_HOME . UA_PROFILE . '.ini.php');
185 // modified for Magic3 by naoki on 2008/9/22
186 define('UA_INI_FILE', $gEnvManager->getCurrentWidgetIncludePath() . '/conf/' . UA_PROFILE . '.ini.php');
187 if (! file_exists(UA_INI_FILE) || ! is_readable(UA_INI_FILE)) {
188         die_message('UA_INI_FILE for "' . UA_PROFILE . '" not found.');
189 } else {
190         require(UA_INI_FILE); // Also manually
191 }
192
193 define('UA_NAME', isset($user_agent['name']) ? $user_agent['name'] : '');
194 define('UA_VERS', isset($user_agent['vers']) ? $user_agent['vers'] : '');
195 unset($user_agent);     // Unset after reading UA_INI_FILE
196
197 /////////////////////////////////////////////////
198 // ディレクトリのチェック
199
200 $die = '';
201 /*
202 remove temporary by naoki
203 foreach(array('DATA_DIR', 'DIFF_DIR', 'BACKUP_DIR', 'CACHE_DIR') as $dir){
204         if (! is_writable(constant($dir)))
205                 $die .= 'Directory is not found or not writable (' . $dir . ')' . "\n";
206 }*/
207
208 // 設定ファイルの変数チェック
209 $temp = '';
210 foreach(array('rss_max', 'page_title', 'note_hr', 'related_link', 'show_passage',
211         'rule_related_str', 'load_template_func') as $var){
212         if (! isset(${$var})) $temp .= '$' . $var . "\n";
213 }
214 if ($temp) {
215         if ($die) $die .= "\n"; // A breath
216         $die .= 'Variable(s) not found: (Maybe the old *.ini.php?)' . "\n" . $temp;
217 }
218
219 $temp = '';
220 foreach(array('LANG', 'PLUGIN_DIR') as $def){
221         if (! defined($def)) $temp .= $def . "\n";
222 }
223 if ($temp) {
224         if ($die) $die .= "\n"; // A breath
225         $die .= 'Define(s) not found: (Maybe the old *.ini.php?)' . "\n" . $temp;
226 }
227
228 if($die) die_message(nl2br("\n\n" . $die));
229 unset($die, $temp);
230
231 /////////////////////////////////////////////////
232 // 必須のページが存在しなければ、空のファイルを作成する
233 //foreach(array($defaultpage, $whatsnew, $interwiki) as $page){
234 foreach(array(WikiConfig::getDefaultPage(), $whatsnew, $interwiki) as $page){
235         //if (! is_page($page)) touch(get_filename($page));
236         // modified for Magic3 by naoki on 2008/9/22
237         if (!WikiPage::isPage($page)){
238                 //WikiPage::getPageFile($page, $data);          // ファイルから初期データを読み込む
239                 //$ret = WikiPage::initPage($page, $data);
240                 //if (!$ret) die_message('Cannot create init pages.');
241                 WikiPage::initPage($page, '');
242         }
243 }
244 // removed for Magic3 by naoki on 2008/9/22
245 /*
246 /////////////////////////////////////////////////
247 // 外部からくる変数のチェック
248 // Prohibit $_GET attack
249 foreach (array('msg', 'pass') as $key) {
250         if (isset($_GET[$key])) die_message('Sorry, already reserved: ' . $key . '=');
251 }
252 // Expire risk
253 unset($HTTP_GET_VARS, $HTTP_POST_VARS); //, 'SERVER', 'ENV', 'SESSION', ...
254 unset($_REQUEST);       // Considered harmful
255
256 // Remove null character etc.
257 $_GET    = input_filter($_GET);
258 $_POST   = input_filter($_POST);
259 $_COOKIE = input_filter($_COOKIE);
260
261 // 文字コード変換 ($_POST)
262 // <form> で送信された文字 (ブラウザがエンコードしたデータ) のコードを変換
263 // POST method は常に form 経由なので、必ず変換する
264 //
265 if (isset($_POST['encode_hint']) && $_POST['encode_hint'] != '') {
266         // do_plugin_xxx() の中で、<form> に encode_hint を仕込んでいるので、
267         // encode_hint を用いてコード検出する。
268         // 全体を見てコード検出すると、機種依存文字や、妙なバイナリ
269         // コードが混入した場合に、コード検出に失敗する恐れがある。
270         $encode = mb_detect_encoding($_POST['encode_hint']);
271         mb_convert_variables(SOURCE_ENCODING, $encode, $_POST);
272
273 } else if (isset($_POST['charset']) && $_POST['charset'] != '') {
274         // TrackBack Ping で指定されていることがある
275         // うまくいかない場合は自動検出に切り替え
276         if (mb_convert_variables(SOURCE_ENCODING,
277             $_POST['charset'], $_POST) !== $_POST['charset']) {
278                 mb_convert_variables(SOURCE_ENCODING, 'auto', $_POST);
279         }
280
281 } else if (! empty($_POST)) {
282         // 全部まとめて、自動検出/変換
283         mb_convert_variables(SOURCE_ENCODING, 'auto', $_POST);
284 }
285
286 // 文字コード変換 ($_GET)
287 // GET method は form からの場合と、<a href="http://script/?key=value> の場合がある
288 // <a href...> の場合は、サーバーが rawurlencode しているので、コード変換は不要
289 if (isset($_GET['encode_hint']) && $_GET['encode_hint'] != '')
290 {
291         // form 経由の場合は、ブラウザがエンコードしているので、コード検出・変換が必要。
292         // encode_hint が含まれているはずなので、それを見て、コード検出した後、変換する。
293         // 理由は、post と同様
294         $encode = mb_detect_encoding($_GET['encode_hint']);
295         mb_convert_variables(SOURCE_ENCODING, $encode, $_GET);
296 }
297 */
298 /////////////////////////////////////////////////
299 // QUERY_STRINGを取得
300
301 // cmdもpluginも指定されていない場合は、QUERY_STRINGを
302 // ページ名かInterWikiNameであるとみなす
303 // modified for Magic3 by naoki on 2008/9/26
304 /*$arg = '';
305 if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING']) {
306         $arg = $_SERVER['QUERY_STRING'];
307 } else if (isset($_SERVER['argv']) && ! empty($_SERVER['argv'])) {
308         $arg = $_SERVER['argv'][0];
309 }
310 if (PKWK_QUERY_STRING_MAX && strlen($arg) > PKWK_QUERY_STRING_MAX) {
311         // Something nasty attack?
312         pkwk_common_headers();
313         sleep(1);       // Fake processing, and/or process other threads
314         echo('Query string too long');
315         exit;
316 }
317 $arg = input_filter($arg); // \0 除去
318
319 // unset QUERY_STRINGs
320 foreach (array('QUERY_STRING', 'argv', 'argc') as $key) {
321         unset(${$key}, $_SERVER[$key], $HTTP_SERVER_VARS[$key]);
322 }
323 // $_SERVER['REQUEST_URI'] is used at func.php NOW
324 unset($REQUEST_URI, $HTTP_SERVER_VARS['REQUEST_URI']);
325
326 // mb_convert_variablesのバグ(?)対策: 配列で渡さないと落ちる
327 $arg = array($arg);
328 mb_convert_variables(SOURCE_ENCODING, 'auto', $arg);
329 $arg = $arg[0];
330
331 /////////////////////////////////////////////////
332 // QUERY_STRINGを分解してコード変換し、$_GET に上書き
333
334 // URI を urlencode せずに入力した場合に対処する
335 $matches = array();
336 foreach (explode('&', $arg) as $key_and_value) {
337         if (preg_match('/^([^=]+)=(.+)/', $key_and_value, $matches) &&
338             mb_detect_encoding($matches[2]) != 'ASCII') {
339                 $_GET[$matches[1]] = $matches[2];
340         }
341 }
342 unset($matches);
343
344 /////////////////////////////////////////////////
345 // GET, POST, COOKIE
346 $get    = $_GET;
347 $post   = $_POST;
348 $cookie = $_COOKIE;
349
350 // GET + POST = $vars
351 if (empty($_POST)) {
352         $vars = $_GET;  // Major pattern: Read-only access via GET
353 } else if (empty($_GET)) {
354         $vars = $_POST; // Minor pattern: Write access via POST etc.
355 } else {
356         $vars = array_merge($_GET, $_POST); // Considered reliable than $_REQUEST
357 }
358 */
359 // 入力チェック: 'cmd=' and 'plugin=' can't live together
360 // modified for Magic3 by naoki on 2008/9/22
361 //if (isset($vars['cmd']) && isset($vars['plugin']))
362 //die('Using both cmd= and plugin= is not allowed');
363 if (!WikiParam::checkParam()) die('Using both cmd= and plugin= is not allowed');
364
365 // 入力チェック: cmd, plugin の文字列は英数字以外ありえない
366 // removed for Magic3 by naoki on 2008/9/22
367 /*foreach(array('cmd', 'plugin') as $var) {
368         if (isset($vars[$var]) && ! preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $vars[$var]))
369                 unset($get[$var], $post[$var], $vars[$var]);
370 }*/
371
372 // removed for Magic3 by naoki on 2008/9/22
373 /*
374 // 整形: page, strip_bracket()
375 if (isset($vars['page'])) {
376         $get['page'] = $post['page'] = $vars['page']  = strip_bracket($vars['page']);
377 } else {
378         $get['page'] = $post['page'] = $vars['page'] = '';
379 }
380
381 // 整形: msg, 改行を取り除く
382 if (isset($vars['msg'])) {
383         $get['msg'] = $post['msg'] = $vars['msg'] = str_replace("\r", '', $vars['msg']);
384 }
385
386 // 後方互換性 (?md5=...)
387 if (isset($get['md5']) && $get['md5'] != '' &&
388     ! isset($vars['cmd']) && ! isset($vars['plugin'])) {
389         $get['cmd'] = $post['cmd'] = $vars['cmd'] = 'md5';
390 }
391 */
392 // TrackBack Ping
393 if (isset($vars['tb_id']) && $vars['tb_id'] != '') {
394         $get['cmd'] = $post['cmd'] = $vars['cmd'] = 'tb';
395 }
396
397 // cmdもpluginも指定されていない場合は、QUERY_STRINGをページ名かInterWikiNameであるとみなす
398 // modified for Magic3 by naoki on 2008/9/22
399 //if (! isset($vars['cmd']) && ! isset($vars['plugin'])) {
400 /*$cmd = WikiParam::getCmd();
401 $plugin = WikiParam::getPlugin();
402 if (empty($cmd) && empty($plugin)){
403         //$get['cmd']  = $post['cmd']  = $vars['cmd']  = 'read';
404         WikiParam::setCmd('read');
405         
406         $arg = WikiParam::getUnbraketArg();
407         if ($arg == '') $arg = $defaultpage;
408         
409         //$get['page'] = $post['page'] = $vars['page'] = $arg;
410         WikiParam::setPage($arg);
411         $vars['page'] = $arg;
412 } else {
413         $vars['page'] = WikiParam::getPage();
414 }*/
415
416 /////////////////////////////////////////////////
417 // 初期設定($WikiName,$BracketNameなど)
418 // $WikiName = '[A-Z][a-z]+(?:[A-Z][a-z]+)+';
419 // $WikiName = '\b[A-Z][a-z]+(?:[A-Z][a-z]+)+\b';
420 // $WikiName = '(?<![[:alnum:]])(?:[[:upper:]][[:lower:]]+){2,}(?![[:alnum:]])';
421 // $WikiName = '(?<!\w)(?:[A-Z][a-z]+){2,}(?!\w)';
422
423 // BugTrack/304暫定対処
424 $WikiName = '(?:[A-Z][a-z]+){2,}(?!\w)';
425
426 // $BracketName = ':?[^\s\]#&<>":]+:?';
427 $BracketName = '(?!\s):?[^\r\n\t\f\[\]<>#&":]+:?(?<!\s)';
428
429 // InterWiki
430 $InterWikiName = '(\[\[)?((?:(?!\s|:|\]\]).)+):(.+)(?(1)\]\])';
431
432 // 注釈
433 $NotePattern = '/\(\(((?:(?>(?:(?!\(\()(?!\)\)(?:[^\)]|$)).)+)|(?R))*)\)\)/ex';
434
435 /////////////////////////////////////////////////
436 // 初期設定(ユーザ定義ルール読み込み)
437 //require(DATA_HOME . 'rules.ini.php');
438 // modified for Magic3 by naoki on 2008/9/22
439 require($gEnvManager->getCurrentWidgetIncludePath() . '/conf/rules.ini.php');
440
441 /////////////////////////////////////////////////
442 // 初期設定(その他のグローバル変数)
443
444 // 現在時刻
445 $now = format_date(UTIME);
446
447 // 日時置換ルールを$line_rulesに加える
448 if ($usedatetime) $line_rules += $datetime_rules;
449 unset($datetime_rules);
450
451 // フェイスマークを$line_rulesに加える
452 if ($usefacemark) $line_rules += $facemark_rules;
453 unset($facemark_rules);
454
455 // 実体参照パターンおよびシステムで使用するパターンを$line_rulesに加える
456 //$entity_pattern = '[a-zA-Z0-9]{2,8}';
457 //$entity_pattern = trim(join('', file(CACHE_DIR . 'entities.dat')));
458 // modified for Magic3 by naoki on 2008/9/22
459 $entity_pattern = trim(WikiPage::getEntityData());
460
461 $line_rules = array_merge(array(
462         '&amp;(#[0-9]+|#x[0-9a-f]+|' . $entity_pattern . ');' => '&$1;',
463         "\r"          => '<br />' . "\n",       /* 行末にチルダは改行 */
464 ), $line_rules);
465 ?>