PageRenderTime 63ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/init.php

https://github.com/miya5n/pukiwiki
PHP | 420 lines | 237 code | 77 blank | 106 comment | 74 complexity | 44877af72f7fe37eed8ae381363f742f MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. // PukiWiki - Yet another WikiWikiWeb clone.
  3. // $Id: init.php,v 1.57 2011/01/25 15:01:01 henoheno Exp $
  4. // Copyright (C)
  5. // 2002-2007, 2009 PukiWiki Developers Team
  6. // 2001-2002 Originally written by yu-ji
  7. // License: GPL v2 or (at your option) any later version
  8. //
  9. // Init PukiWiki here
  10. // PukiWiki version / Copyright / Licence
  11. define('S_VERSION', '1.4.8_alpha2');
  12. define('S_COPYRIGHT',
  13. '<strong>PukiWiki ' . S_VERSION . '</strong>' .
  14. ' Copyright &copy; 2001-2006' .
  15. ' <a href="http://pukiwiki.sourceforge.jp/">PukiWiki Developers Team</a>.' .
  16. ' License is <a href="http://www.gnu.org/licenses/gpl.html">GPL</a>.<br />' .
  17. ' Based on "PukiWiki" 1.3 by <a href="http://factage.com/yu-ji/">yu-ji</a>'
  18. );
  19. /////////////////////////////////////////////////
  20. // Init server variables
  21. // Comapat and suppress notices
  22. if (!isset($HTTP_SERVER_VARS)) $HTTP_SERVER_VARS = array();
  23. foreach (array('SCRIPT_NAME', 'SERVER_ADMIN', 'SERVER_NAME',
  24. 'SERVER_PORT', 'SERVER_SOFTWARE') as $key) {
  25. define($key, isset($_SERVER[$key]) ? $_SERVER[$key] : '');
  26. unset(${$key}, $_SERVER[$key], $HTTP_SERVER_VARS[$key]);
  27. }
  28. /////////////////////////////////////////////////
  29. // Init grobal variables
  30. $foot_explain = array(); // Footnotes
  31. $related = array(); // Related pages
  32. $head_tags = array(); // XHTML tags in <head></head>
  33. /////////////////////////////////////////////////
  34. // Require INI_FILE
  35. define('INI_FILE', DATA_HOME . 'pukiwiki.ini.php');
  36. $die = '';
  37. if (! file_exists(INI_FILE) || ! is_readable(INI_FILE)) {
  38. $die .= 'File is not found. (INI_FILE)' . "\n";
  39. } else {
  40. require(INI_FILE);
  41. }
  42. if ($die) die_message(nl2br("\n\n" . $die));
  43. /////////////////////////////////////////////////
  44. // Time settings
  45. switch (ZONE) {
  46. case 'JST':
  47. ini_set('date.timezone', 'Asia/Tokyo');
  48. break;
  49. default:
  50. ini_set('date.timezone', 'UTC');
  51. break;
  52. }
  53. define('LOCALZONE', date('Z'));
  54. define('UTIME', time() - LOCALZONE);
  55. define('MUTIME', getmicrotime());
  56. /////////////////////////////////////////////////
  57. // INI_FILE: LANG に基づくエンコーディング設定
  58. // MB_LANGUAGE: mb_language (for mbstring extension)
  59. // 'uni'(means UTF-8), 'English', or 'Japanese'
  60. // SOURCE_ENCODING: Internal content encoding (for mbstring extension)
  61. // 'UTF-8', 'ASCII', or 'EUC-JP'
  62. // CONTENT_CHARSET: Internal content encoding = Output content charset
  63. // (for DTD, htmlsc())
  64. // 'UTF-8', 'iso-8859-1', 'EUC-JP' or ...
  65. switch (LANG){
  66. case 'en': define('MB_LANGUAGE', 'English' ); break;
  67. case 'ja': define('MB_LANGUAGE', 'Japanese'); break;
  68. //UTF-8:case 'ko': define('MB_LANGUAGE', 'Korean' ); break;
  69. //UTF-8: // See BugTrack2/13 for all hack about Korean support,
  70. //UTF-8: // and give us your report!
  71. default: die_message('No such language "' . LANG . '"'); break;
  72. }
  73. //UTF-8:define('PKWK_UTF8_ENABLE', 1);
  74. if (defined('PKWK_UTF8_ENABLE')) {
  75. define('SOURCE_ENCODING', 'UTF-8');
  76. define('CONTENT_CHARSET', 'UTF-8');
  77. } else {
  78. switch (LANG){
  79. case 'en':
  80. define('SOURCE_ENCODING', 'ASCII');
  81. define('CONTENT_CHARSET', 'iso-8859-1');
  82. break;
  83. case 'ja':
  84. define('SOURCE_ENCODING', 'EUC-JP');
  85. define('CONTENT_CHARSET', 'EUC-JP');
  86. break;
  87. }
  88. }
  89. mb_language(MB_LANGUAGE);
  90. mb_internal_encoding(SOURCE_ENCODING);
  91. ini_set('mbstring.http_input', 'pass');
  92. mb_http_output('pass');
  93. mb_detect_order('auto');
  94. /////////////////////////////////////////////////
  95. // INI_FILE: Require LANG_FILE
  96. define('LANG_FILE_HINT', DATA_HOME . LANG . '.lng.php'); // For encoding hint
  97. define('LANG_FILE', DATA_HOME . UI_LANG . '.lng.php'); // For UI resource
  98. $die = '';
  99. foreach (array('LANG_FILE_HINT', 'LANG_FILE') as $langfile) {
  100. if (! file_exists(constant($langfile)) || ! is_readable(constant($langfile))) {
  101. $die .= 'File is not found or not readable. (' . $langfile . ')' . "\n";
  102. } else {
  103. require_once(constant($langfile));
  104. }
  105. }
  106. if ($die) die_message(nl2br("\n\n" . $die));
  107. /////////////////////////////////////////////////
  108. // LANG_FILE: Init encoding hint
  109. define('PKWK_ENCODING_HINT', isset($_LANG['encode_hint'][LANG]) ? $_LANG['encode_hint'][LANG] : '');
  110. unset($_LANG['encode_hint']);
  111. /////////////////////////////////////////////////
  112. // LANG_FILE: Init severn days of the week
  113. $weeklabels = $_msg_week;
  114. /////////////////////////////////////////////////
  115. // INI_FILE: Init $script
  116. if (isset($script)) {
  117. get_script_uri($script); // Init manually
  118. } else {
  119. $script = get_script_uri(); // Init automatically
  120. }
  121. /////////////////////////////////////////////////
  122. // INI_FILE: $agents: UserAgentの識別
  123. $ua = 'HTTP_USER_AGENT';
  124. $user_agent = $matches = array();
  125. $user_agent['agent'] = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
  126. unset(${$ua}, $_SERVER[$ua], $HTTP_SERVER_VARS[$ua], $ua); // safety
  127. foreach ($agents as $agent) {
  128. if (preg_match($agent['pattern'], $user_agent['agent'], $matches)) {
  129. $user_agent['profile'] = isset($agent['profile']) ? $agent['profile'] : '';
  130. $user_agent['name'] = isset($matches[1]) ? $matches[1] : ''; // device or browser name
  131. $user_agent['vers'] = isset($matches[2]) ? $matches[2] : ''; // 's version
  132. break;
  133. }
  134. }
  135. unset($agents, $matches);
  136. // Profile-related init and setting
  137. define('UA_PROFILE', isset($user_agent['profile']) ? $user_agent['profile'] : '');
  138. define('UA_INI_FILE', DATA_HOME . UA_PROFILE . '.ini.php');
  139. if (! file_exists(UA_INI_FILE) || ! is_readable(UA_INI_FILE)) {
  140. die_message('UA_INI_FILE for "' . UA_PROFILE . '" not found.');
  141. } else {
  142. require(UA_INI_FILE); // Also manually
  143. }
  144. define('UA_NAME', isset($user_agent['name']) ? $user_agent['name'] : '');
  145. define('UA_VERS', isset($user_agent['vers']) ? $user_agent['vers'] : '');
  146. unset($user_agent); // Unset after reading UA_INI_FILE
  147. /////////////////////////////////////////////////
  148. // ディレクトリのチェック
  149. $die = '';
  150. foreach(array('DATA_DIR', 'DIFF_DIR', 'BACKUP_DIR', 'CACHE_DIR') as $dir){
  151. if (! is_writable(constant($dir)))
  152. $die .= 'Directory is not found or not writable (' . $dir . ')' . "\n";
  153. }
  154. // 設定ファイルの変数チェック
  155. $temp = '';
  156. foreach(array('rss_max', 'page_title', 'note_hr', 'related_link', 'show_passage',
  157. 'rule_related_str', 'load_template_func') as $var){
  158. if (! isset(${$var})) $temp .= '$' . $var . "\n";
  159. }
  160. if ($temp) {
  161. if ($die) $die .= "\n"; // A breath
  162. $die .= 'Variable(s) not found: (Maybe the old *.ini.php?)' . "\n" . $temp;
  163. }
  164. $temp = '';
  165. foreach(array('LANG', 'PLUGIN_DIR') as $def){
  166. if (! defined($def)) $temp .= $def . "\n";
  167. }
  168. if ($temp) {
  169. if ($die) $die .= "\n"; // A breath
  170. $die .= 'Define(s) not found: (Maybe the old *.ini.php?)' . "\n" . $temp;
  171. }
  172. if($die) die_message(nl2br("\n\n" . $die));
  173. unset($die, $temp);
  174. /////////////////////////////////////////////////
  175. // 必須のページが存在しなければ、空のファイルを作成する
  176. foreach(array($defaultpage, $whatsnew, $interwiki) as $page){
  177. if (! is_page($page)) pkwk_touch_file(get_filename($page));
  178. }
  179. /////////////////////////////////////////////////
  180. // 外部からくる変数のチェック
  181. // Prohibit $_GET attack
  182. foreach (array('msg', 'pass') as $key) {
  183. if (isset($_GET[$key])) die_message('Sorry, already reserved: ' . $key . '=');
  184. }
  185. // Expire risk
  186. unset($HTTP_GET_VARS, $HTTP_POST_VARS); //, 'SERVER', 'ENV', 'SESSION', ...
  187. unset($_REQUEST); // Considered harmful
  188. // Remove null character etc.
  189. $_GET = input_filter($_GET);
  190. $_POST = input_filter($_POST);
  191. $_COOKIE = input_filter($_COOKIE);
  192. // 文字コード変換 ($_POST)
  193. // <form> で送信された文字 (ブラウザがエンコードしたデータ) のコードを変換
  194. // POST method は常に form 経由なので、必ず変換する
  195. //
  196. if (isset($_POST['encode_hint']) && $_POST['encode_hint'] != '') {
  197. // do_plugin_xxx() の中で、<form> に encode_hint を仕込んでいるので、
  198. // encode_hint を用いてコード検出する。
  199. // 全体を見てコード検出すると、機種依存文字や、妙なバイナリ
  200. // コードが混入した場合に、コード検出に失敗する恐れがある。
  201. $encode = mb_detect_encoding($_POST['encode_hint']);
  202. mb_convert_variables(SOURCE_ENCODING, $encode, $_POST);
  203. } else if (isset($_POST['charset']) && $_POST['charset'] != '') {
  204. // TrackBack Ping で指定されていることがある
  205. // うまくいかない場合は自動検出に切り替え
  206. if (mb_convert_variables(SOURCE_ENCODING,
  207. $_POST['charset'], $_POST) !== $_POST['charset']) {
  208. mb_convert_variables(SOURCE_ENCODING, 'auto', $_POST);
  209. }
  210. } else if (! empty($_POST)) {
  211. // 全部まとめて、自動検出/変換
  212. mb_convert_variables(SOURCE_ENCODING, 'auto', $_POST);
  213. }
  214. // 文字コード変換 ($_GET)
  215. // GET method は form からの場合と、<a href="http://script/?key=value> の場合がある
  216. // <a href...> の場合は、サーバーが rawurlencode しているので、コード変換は不要
  217. if (isset($_GET['encode_hint']) && $_GET['encode_hint'] != '')
  218. {
  219. // form 経由の場合は、ブラウザがエンコードしているので、コード検出・変換が必要。
  220. // encode_hint が含まれているはずなので、それを見て、コード検出した後、変換する。
  221. // 理由は、post と同様
  222. $encode = mb_detect_encoding($_GET['encode_hint']);
  223. mb_convert_variables(SOURCE_ENCODING, $encode, $_GET);
  224. }
  225. /////////////////////////////////////////////////
  226. // QUERY_STRINGを取得
  227. $arg = '';
  228. if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != '') {
  229. $arg = & $_SERVER['QUERY_STRING'];
  230. } else if (isset($_SERVER['argv']) && ! empty($_SERVER['argv'])) {
  231. $arg = & $_SERVER['argv'][0];
  232. }
  233. if (PKWK_QUERY_STRING_MAX && strlen($arg) > PKWK_QUERY_STRING_MAX) {
  234. // Something nasty attack?
  235. pkwk_common_headers();
  236. echo('Query string too long');
  237. exit;
  238. }
  239. $arg = input_filter($arg); // \0 除去
  240. // unset QUERY_STRINGs
  241. foreach (array('QUERY_STRING', 'argv', 'argc') as $key) {
  242. unset(${$key}, $_SERVER[$key], $HTTP_SERVER_VARS[$key]);
  243. }
  244. // $_SERVER['REQUEST_URI'] is used at func.php NOW
  245. unset($REQUEST_URI, $HTTP_SERVER_VARS['REQUEST_URI']);
  246. // mb_convert_variablesのバグ(?)対策: 配列で渡さないと落ちる
  247. $arg = array($arg);
  248. mb_convert_variables(SOURCE_ENCODING, 'auto', $arg);
  249. $arg = $arg[0];
  250. /////////////////////////////////////////////////
  251. // QUERY_STRINGを分解してコード変換し、$_GET に上書き
  252. // URI を urlencode せずに入力した場合に対処する
  253. $matches = array();
  254. foreach (explode('&', $arg) as $key_and_value) {
  255. if (preg_match('/^([^=]+)=(.+)/', $key_and_value, $matches) &&
  256. mb_detect_encoding($matches[2]) != 'ASCII') {
  257. $_GET[$matches[1]] = $matches[2];
  258. }
  259. }
  260. unset($matches);
  261. /////////////////////////////////////////////////
  262. // GET, POST, COOKIE
  263. $get = & $_GET;
  264. $post = & $_POST;
  265. $cookie = & $_COOKIE;
  266. // GET + POST = $vars
  267. if (empty($_POST)) {
  268. $method = 'GET';
  269. $vars = & $_GET; // Major pattern: Read-only access via GET
  270. } else if (empty($_GET)) {
  271. $method = 'POST';
  272. $vars = & $_POST; // Minor pattern: Write access via POST etc.
  273. } else {
  274. $method = 'GET and POST';
  275. $vars = array_merge($_GET, $_POST); // Considered reliable than $_REQUEST
  276. }
  277. // 入力チェック: 'cmd=' and 'plugin=' can't live together
  278. if (isset($vars['cmd']) && isset($vars['plugin']))
  279. die('Using both cmd= and plugin= is not allowed');
  280. // 入力チェック: cmd, plugin の文字列は英数字以外ありえない
  281. foreach(array('cmd', 'plugin') as $var) {
  282. if (isset($vars[$var]) && ! preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $vars[$var]))
  283. unset($get[$var], $post[$var], $vars[$var]);
  284. }
  285. // 整形: page, strip_bracket()
  286. if (isset($vars['page'])) {
  287. $get['page'] = $post['page'] = $vars['page'] = strip_bracket($vars['page']);
  288. } else {
  289. $get['page'] = $post['page'] = $vars['page'] = '';
  290. }
  291. // 整形: msg, 改行を取り除く
  292. if (isset($vars['msg'])) {
  293. $get['msg'] = $post['msg'] = $vars['msg'] = str_replace("\r", '', $vars['msg']);
  294. }
  295. // 後方互換性 (?md5=...)
  296. if (isset($get['md5']) && $get['md5'] != '' &&
  297. ! isset($vars['cmd']) && ! isset($vars['plugin'])) {
  298. $get['cmd'] = $post['cmd'] = $vars['cmd'] = 'md5';
  299. }
  300. // cmdもpluginも指定されていない場合は、QUERY_STRINGをページ名かInterWikiNameであるとみなす
  301. if (! isset($vars['cmd']) && ! isset($vars['plugin'])) {
  302. $get['cmd'] = $post['cmd'] = $vars['cmd'] = 'read';
  303. if ($arg == '') $arg = $defaultpage;
  304. $arg = rawurldecode($arg);
  305. $arg = strip_bracket($arg);
  306. $arg = input_filter($arg);
  307. $get['page'] = $post['page'] = $vars['page'] = $arg;
  308. }
  309. /////////////////////////////////////////////////
  310. // 初期設定($WikiName,$BracketNameなど)
  311. // $WikiName = '[A-Z][a-z]+(?:[A-Z][a-z]+)+';
  312. // $WikiName = '\b[A-Z][a-z]+(?:[A-Z][a-z]+)+\b';
  313. // $WikiName = '(?<![[:alnum:]])(?:[[:upper:]][[:lower:]]+){2,}(?![[:alnum:]])';
  314. // $WikiName = '(?<!\w)(?:[A-Z][a-z]+){2,}(?!\w)';
  315. // BugTrack/304暫定対処
  316. $WikiName = '(?:[A-Z][a-z]+){2,}(?!\w)';
  317. // $BracketName = ':?[^\s\]#&<>":]+:?';
  318. $BracketName = '(?!\s):?[^\r\n\t\f\[\]<>#&":]+:?(?<!\s)';
  319. // InterWiki
  320. $InterWikiName = '(\[\[)?((?:(?!\s|:|\]\]).)+):(.+)(?(1)\]\])';
  321. // 注釈
  322. $NotePattern = '/\(\(((?:(?>(?:(?!\(\()(?!\)\)(?:[^\)]|$)).)+)|(?R))*)\)\)/ex';
  323. /////////////////////////////////////////////////
  324. // 初期設定(ユーザ定義ルール読み込み)
  325. require(DATA_HOME . 'rules.ini.php');
  326. /////////////////////////////////////////////////
  327. // 初期設定(その他のグローバル変数)
  328. // 現在時刻
  329. $now = format_date(UTIME);
  330. // 日時置換ルールを$line_rulesに加える
  331. if ($usedatetime) $line_rules += $datetime_rules;
  332. unset($datetime_rules);
  333. // フェイスマークを$line_rulesに加える
  334. if ($usefacemark) $line_rules += $facemark_rules;
  335. unset($facemark_rules);
  336. // 実体参照パターンおよびシステムで使用するパターンを$line_rulesに加える
  337. //$entity_pattern = '[a-zA-Z0-9]{2,8}';
  338. $entity_pattern = trim(join('', file(CACHE_DIR . PKWK_ENTITIES_REGEX_CACHE)));
  339. $line_rules = array_merge(array(
  340. '&amp;(#[0-9]+|#x[0-9a-f]+|' . $entity_pattern . ');' => '&$1;',
  341. "\r" => '<br />' . "\n", /* 行末にチルダは改行 */
  342. ), $line_rules);
  343. ?>