-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathinit.php
More file actions
440 lines (356 loc) · 14.6 KB
/
Copy pathinit.php
File metadata and controls
440 lines (356 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
<?php
// PukiWiki - Yet another WikiWikiWeb clone.
// $Id: init.php,v 1.46 2006/06/11 15:04:27 henoheno Exp $
// Copyright (C)
// 2002-2006 PukiWiki Developers Team
// 2001-2002 Originally written by yu-ji
// License: GPL v2 or (at your option) any later version
//
// Init PukiWiki here
// PukiWiki version / Copyright / Licence
define('S_VERSION', '1.4.7');
define('QHM_VERSION', '7.1.7'); //絶対に編集しないで下さい
define('QHM_OPTIONS', 'update=download; support=false; banner=true');
define('S_COPYRIGHT',
'powered by <strong><a href="http://www.open-qhm.net/">HAIK</a> ' . QHM_VERSION . '</strong><br />' .
' based on <a href="http://pukiwiki.sourceforge.jp/">PukiWiki</a> ' . S_VERSION . ' ' .
' License is <a href="http://www.gnu.org/licenses/gpl.html">GPL</a>.'
);
// SWFU Library
define('SWFU_DIR', './swfu/');
define('SWFU_TEXTSQL_PATH', SWFU_DIR.'cheetan/db/textsql.php');
define('SWFU_IMAGEDB_PATH', SWFU_DIR.'data/image.txt');
define('SWFU_IMAGE_DIR', SWFU_DIR.'d/');
// URLs
define('QHM_HOME', 'http://www.open-qhm.net/');
/////////////////////////////////////////////////
// Init server variables
foreach (array('SCRIPT_NAME', 'SERVER_ADMIN', 'SERVER_NAME',
'SERVER_PORT', 'SERVER_SOFTWARE') as $key) {
define($key, isset($_SERVER[$key]) ? $_SERVER[$key] : '');
if(isset(${$key})) unset(${$key});
if(isset($_SERVER[$key])) unset($_SERVER[$key]);
if(isset($HTTP_SERVER_VARS[$key])) unset($HTTP_SERVER_VARS[$key]);
}
/////////////////////////////////////////////////
// Init grobal variables
$foot_explain = array(); // Footnotes
$related = array(); // Related pages
$head_tags = array(); // XHTML tags in <head></head>
/////////////////////////////////////////////////
// Time settings
define('LOCALZONE', date('Z'));
define('UTIME', time() - LOCALZONE);
define('MUTIME', getmicrotime());
/////////////////////////////////////////////////
// Require INI_FILE
define('INI_FILE', DATA_HOME . 'pukiwiki.ini.php');
$die = '';
if (! file_exists(INI_FILE) || ! is_readable(INI_FILE)) {
$die .= 'File is not found. (INI_FILE)' . "\n";
} else {
require(INI_FILE);
}
if ($die) die_message(nl2br("\n\n" . $die));
/////////////////////////////////////////////////
//load QHM Messages
//Load QHM Template
require_once(LIB_DIR. 'qhm_message.php');
require_once(LIB_DIR. 'qhm_template.php');
$qm = QHM_Message::get_instance();
$qt = QHM_Template::get_instance();
$qt->create_cache = false;
/////////////////////////////////////////////////
// INI_FILE: 言語設定
define('SOURCE_ENCODING', 'UTF-8');
define('CONTENT_CHARSET', 'UTF-8');
// おかしな設定のサーバー対策
if( ini_get('mbstring.encoding_translation')
&& ini_get('mbstring.http_input') != 'pass'
&& ini_get('mbstring.http_input') != 'auto'
&& ( strtoupper( ini_get('mbstring.internal_encoding') ) != SOURCE_ENCODING )
){
define('WARNING_OF_ENCODING', 1);
}
define('MB_LANGUAGE', $qm->m['mb_language']);
mb_language(MB_LANGUAGE);
mb_internal_encoding(SOURCE_ENCODING);
mb_regex_encoding(SOURCE_ENCODING);
ini_set('mbstring.http_input', 'pass');
mb_http_output('pass');
mb_detect_order('auto');
/////////////////////////////////////////////////
// INI_FILE: Require LANG_FILE
define('LANG_FILE_HINT', DATA_HOME . LANG . '.lng.php'); // For encoding hint
define('LANG_FILE', DATA_HOME . UI_LANG . '.lng.php'); // For UI resource
$die = '';
foreach (array('LANG_FILE_HINT', 'LANG_FILE') as $langfile) {
if (! file_exists(constant($langfile)) || ! is_readable(constant($langfile))) {
$die .= 'File is not found or not readable. (' . $langfile . ')' . "\n";
} else {
require_once(constant($langfile));
}
}
if ($die) die_message(nl2br("\n\n" . $die));
/////////////////////////////////////////////////
// LANG_FILE: Init encoding hint
define('PKWK_ENCODING_HINT', isset($_LANG['encode_hint'][LANG]) ? $_LANG['encode_hint'][LANG] : '');
unset($_LANG['encode_hint']);
/////////////////////////////////////////////////
// LANG_FILE: Init severn days of the week
$weeklabels = $_msg_week;
/////////////////////////////////////////////////
// INI_FILE: Init $script
$default_script = $script;
if ( !isset($script) || $script == '') {
$script = get_script_uri(); // Init automanually
} else {
get_script_uri($script); // Init matically
}
//ssl接続用の$scriptを生成
if( ! isset($script_ssl) || $script_ssl == '' ){
$script_ssl = preg_replace('/^http:/', 'https:', $script);
}
//is_httpsメソッド用など
$init_scripts = array('normal'=>$script, 'ssl'=>$script_ssl);
/////////////////////////////////////////////////
// INI_FILE: $agents: UserAgentの識別
$ua = 'HTTP_USER_AGENT';
$user_agent = $matches = array();
$user_agent['agent'] = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
if(isset(${$ua})) unset(${$ua});
if(isset($_SERVER[$ua])) unset($_SERVER[$ua]);
if(isset($HTTP_SERVER_VARS[$ua])) unset($HTTP_SERVER_VARS[$ua]);
unset($ua);
foreach ($agents as $agent) {
if (preg_match($agent['pattern'], $user_agent['agent'], $matches)) {
$user_agent['profile'] = isset($agent['profile']) ? $agent['profile'] : '';
$user_agent['name'] = isset($matches[1]) ? $matches[1] : ''; // device or browser name
$user_agent['vers'] = isset($matches[2]) ? $matches[2] : ''; // 's version
break;
}
}
unset($agents, $matches);
// Profile-related init and setting
define('UA_PROFILE', isset($user_agent['profile']) ? $user_agent['profile'] : '');
define('UA_INI_FILE', DATA_HOME . UA_PROFILE . '.ini.php');
if (! file_exists(UA_INI_FILE) || ! is_readable(UA_INI_FILE)) {
die_message('UA_INI_FILE for "' . UA_PROFILE . '" not found.');
} else {
require(UA_INI_FILE); // Also manually
}
define('UA_NAME', isset($user_agent['name']) ? $user_agent['name'] : '');
define('UA_VERS', isset($user_agent['vers']) ? $user_agent['vers'] : '');
unset($user_agent); // Unset after reading UA_INI_FILE
/////////////////////////////////////////////////
// ディレクトリのチェック
$die = '';
foreach(array('DATA_DIR', 'DIFF_DIR', 'BACKUP_DIR', 'CACHE_DIR') as $dir){
if (! is_writable(constant($dir)))
$die .= 'Directory is not found or not writable (' . $dir . ')' . "\n";
}
// 設定ファイルの変数チェック
$temp = '';
foreach(array('rss_max', 'page_title', 'note_hr', 'related_link', 'show_passage',
'rule_related_str', 'load_template_func') as $var){
if (! isset(${$var})) $temp .= '$' . $var . "\n";
}
if ($temp) {
if ($die) $die .= "\n"; // A breath
$die .= 'Variable(s) not found: (Maybe the old *.ini.php?)' . "\n" . $temp;
}
$temp = '';
foreach(array('LANG', 'PLUGIN_DIR') as $def){
if (! defined($def)) $temp .= $def . "\n";
}
if ($temp) {
if ($die) $die .= "\n"; // A breath
$die .= 'Define(s) not found: (Maybe the old *.ini.php?)' . "\n" . $temp;
}
if($die) die_message(nl2br("\n\n" . $die));
unset($die, $temp);
/////////////////////////////////////////////////
// 必須のページが存在しなければ、空のファイルを作成する
foreach(array($defaultpage, $whatsnew, $interwiki) as $page){
if (! is_page($page)) touch(get_filename($page));
}
/////////////////////////////////////////////////
// 外部からくる変数のチェック
// Prohibit $_GET attack
foreach (array('msg', 'pass') as $key) {
if (isset($_GET[$key])) die_message('Sorry, already reserved: ' . $key . '=');
}
// Expire risk
unset($HTTP_GET_VARS, $HTTP_POST_VARS); //, 'SERVER', 'ENV', 'SESSION', ...
unset($_REQUEST); // Considered harmful
// Remove null character etc.
$_GET = input_filter($_GET);
$_POST = input_filter($_POST);
$_COOKIE = input_filter($_COOKIE);
// 文字コード変換 ($_POST)
// <form> で送信された文字 (ブラウザがエンコードしたデータ) のコードを変換
// POST method は常に form 経由なので、必ず変換する
//
if (isset($_POST['encode_hint']) && $_POST['encode_hint'] != '') {
// do_plugin_xxx() の中で、<form> に encode_hint を仕込んでいるので、
// encode_hint を用いてコード検出する。
// 全体を見てコード検出すると、機種依存文字や、妙なバイナリ
// コードが混入した場合に、コード検出に失敗する恐れがある。
$encode = mb_detect_encoding($_POST['encode_hint']);
mb_convert_variables(SOURCE_ENCODING, $encode, $_POST);
} else if (isset($_POST['charset']) && $_POST['charset'] != '') {
// TrackBack Ping で指定されていることがある
// うまくいかない場合は自動検出に切り替え
if (mb_convert_variables(SOURCE_ENCODING,
$_POST['charset'], $_POST) !== $_POST['charset']) {
mb_convert_variables(SOURCE_ENCODING, 'auto', $_POST);
}
} else if (! empty($_POST)) {
// 全部まとめて、自動検出/変換
mb_convert_variables(SOURCE_ENCODING, 'auto', $_POST);
}
// 文字コード変換 ($_GET)
// GET method は form からの場合と、<a href="http://script/?key=value> の場合がある
// <a href...> の場合は、サーバーが rawurlencode しているので、コード変換は不要
if (isset($_GET['encode_hint']) && $_GET['encode_hint'] != '')
{
// form 経由の場合は、ブラウザがエンコードしているので、コード検出・変換が必要。
// encode_hint が含まれているはずなので、それを見て、コード検出した後、変換する。
// 理由は、post と同様
$encode = mb_detect_encoding($_GET['encode_hint']);
mb_convert_variables(SOURCE_ENCODING, $encode, $_GET);
}
/////////////////////////////////////////////////
// QUERY_STRINGを取得
// cmdもpluginも指定されていない場合は、QUERY_STRINGを
// ページ名かInterWikiNameであるとみなす
$arg = '';
if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING']) {
$arg = & $_SERVER['QUERY_STRING'];
} else if (isset($_SERVER['argv']) && ! empty($_SERVER['argv'])) {
$arg = & $_SERVER['argv'][0];
}
if (PKWK_QUERY_STRING_MAX && strlen($arg) > PKWK_QUERY_STRING_MAX) {
// Something nasty attack?
pkwk_common_headers();
sleep(1); // Fake processing, and/or process other threads
echo('Query string too long');
exit;
}
$arg = input_filter($arg); // \0 除去
// unset QUERY_STRINGs
foreach (array('QUERY_STRING', 'argv', 'argc') as $key) {
if(isset(${$key})) unset(${$key});
if(isset($_SERVER[$key])) unset($_SERVER[$key]);
if(isset($HTTP_SERVER_VARS[$key])) unset($HTTP_SERVER_VARS[$key]);
}
// $_SERVER['REQUEST_URI'] is used at func.php NOW
if(isset($REQUEST_URI)) unset($REQUEST_URI);
if(isset($HTTP_SERVER_VARS['REQUEST_URI'])) unset($HTTP_SERVER_VARS['REQUEST_URI']);
// mb_convert_variablesのバグ(?)対策: 配列で渡さないと落ちる
$arg = array($arg);
mb_convert_variables(SOURCE_ENCODING, 'auto', $arg);
$arg = $arg[0];
/////////////////////////////////////////////////
// QUERY_STRINGを分解してコード変換し、$_GET に上書き
// URI を urlencode せずに入力した場合に対処する
$matches = array();
foreach (explode('&', $arg) as $key_and_value) {
if (preg_match('/^([^=]+)=(.+)/', $key_and_value, $matches) &&
mb_detect_encoding($matches[2]) != 'ASCII') {
$_GET[$matches[1]] = $matches[2];
}
}
unset($matches);
/////////////////////////////////////////////////
// GET, POST, COOKIE
$get = & $_GET;
$post = & $_POST;
$cookie = & $_COOKIE;
// GET + POST = $vars
if (empty($_POST)) {
$vars = & $_GET; // Major pattern: Read-only access via GET
} else if (empty($_GET)) {
$vars = & $_POST; // Minor pattern: Write access via POST etc.
} else {
$vars = array_merge($_GET, $_POST); // Considered reliable than $_REQUEST
}
// 入力チェック: 'cmd=' and 'plugin=' can't live together
if (isset($vars['cmd']) && isset($vars['plugin']))
die('Using both cmd= and plugin= is not allowed');
// 入力チェック: cmd, plugin の文字列は英数字以外ありえない
foreach(array('cmd', 'plugin') as $var) {
$regex = version_compare(PHP_VERSION, 5.3 , '<')
? '/^\w+$/'
: '/^\w+(?:\/\w+)?$/';
if (isset($vars[$var]) && ! preg_match($regex, $vars[$var]))
unset($get[$var], $post[$var], $vars[$var]);
}
// 整形: page, strip_bracket()
if (isset($vars['page'])) {
$get['page'] = $post['page'] = $vars['page'] = strip_bracket($vars['page']);
} else {
$get['page'] = $post['page'] = $vars['page'] = '';
}
// 整形: msg, 改行を取り除く
if (isset($vars['msg'])) {
$get['msg'] = $post['msg'] = $vars['msg'] = str_replace("\r", '', $vars['msg']);
}
// 後方互換性 (?md5=...)
if (isset($get['md5']) && $get['md5'] != '' &&
! isset($vars['cmd']) && ! isset($vars['plugin'])) {
$get['cmd'] = $post['cmd'] = $vars['cmd'] = 'md5';
}
// TrackBack Ping
if (isset($vars['tb_id']) && $vars['tb_id'] != '') {
$get['cmd'] = $post['cmd'] = $vars['cmd'] = 'tb';
}
// cmdもpluginも指定されていない場合は、QUERY_STRINGをページ名かInterWikiNameであるとみなす
if (! isset($vars['cmd']) && ! isset($vars['plugin'])) {
if(isset($vars['go'])){ // by HOKUKEN.COM
$t = get_tiny_page($vars['go']);
header('Location: '.$script.'?'.rawurlencode($t));
exit;
}
$get['cmd'] = $post['cmd'] = $vars['cmd'] = 'read';
if ($arg == '') $arg = $defaultpage;
$arg = rawurldecode($arg);
$arg = strip_bracket($arg);
$arg = input_filter($arg);
$arg = strip_adcode($arg);
$get['page'] = $post['page'] = $vars['page'] = $arg;
}
/////////////////////////////////////////////////
// 初期設定($WikiName,$BracketNameなど)
// $WikiName = '[A-Z][a-z]+(?:[A-Z][a-z]+)+';
// $WikiName = '\b[A-Z][a-z]+(?:[A-Z][a-z]+)+\b';
// $WikiName = '(?<![[:alnum:]])(?:[[:upper:]][[:lower:]]+){2,}(?![[:alnum:]])';
// $WikiName = '(?<!\w)(?:[A-Z][a-z]+){2,}(?!\w)';
// BugTrack/304暫定対処
$WikiName = '(?:[A-Z][a-z]+){2,}(?!\w)';
// $BracketName = ':?[^\s\]#&<>":]+:?';
$BracketName = '(?!\s):?[^\r\n\t\f\[\]<>#&":]+:?(?<!\s)';
// InterWiki
$InterWikiName = '(\[\[)?((?:(?!\s|:|\]\]).)+):(.+)(?(1)\]\])';
// 注釈
$NotePattern = '/\(\(((?:(?>(?:(?!\(\()(?!\)\)(?:[^\)]|$)).)+)|(?R))*)\)\)/x';
/////////////////////////////////////////////////
// 初期設定(ユーザ定義ルール読み込み)
require(DATA_HOME . 'rules.ini.php');
/////////////////////////////////////////////////
// 初期設定(その他のグローバル変数)
// 現在時刻
$now = format_date(UTIME);
// 日時置換ルールを$line_rulesに加える
if ($usedatetime) $line_rules += $datetime_rules;
unset($datetime_rules);
// フェイスマークを$line_rulesに加える
if ($usefacemark) $line_rules += $facemark_rules;
unset($facemark_rules);
// 実体参照パターンおよびシステムで使用するパターンを$line_rulesに加える
//$entity_pattern = '[a-zA-Z0-9]{2,8}';
$entity_pattern = trim(join('', file(CACHE_DIR . 'entities.dat')));
$line_rules = array_merge(array(
'&(#[0-9]+|#x[0-9a-f]+|' . $entity_pattern . ');' => '&$1;',
"\r" => '<br />' . "\n", /* 行末にチルダは改行 */
), $line_rules);