OSDN Git Service

BugTrack/487: AutoLink対象のページ名がひとつもないときに、不正なパターンを生成していた
[pukiwiki/pukiwiki.git] / init.php
1 <?php
2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
4 //
5 // $Id: init.php,v 1.67 2003/11/10 02:44:59 arino Exp $
6 //
7
8 /////////////////////////////////////////////////
9 // ½é´üÀßÄê (¥¨¥é¡¼½ÐÎÏ¥ì¥Ù¥ë)
10 // (E_WARNING | E_NOTICE)¤ò½ü³°¤·¤Æ¤¤¤Þ¤¹¡£
11 error_reporting(E_ERROR | E_PARSE);
12
13 /////////////////////////////////////////////////
14 // ½é´üÀßÄê (ʸ»ú¥¨¥ó¥³¡¼¥É¡¢¸À¸ì)
15 define('SOURCE_ENCODING','EUC-JP');
16 define('LANG','ja');
17 mb_internal_encoding(SOURCE_ENCODING);
18 mb_http_output(SOURCE_ENCODING);
19
20 /////////////////////////////////////////////////
21 // ½é´üÀßÄê(ÀßÄê¥Õ¥¡¥¤¥ë¤Î¾ì½ê)
22 define('INI_FILE','./pukiwiki.ini.php');
23
24 /////////////////////////////////////////////////
25 // ½é´üÀßÄê (¥Ð¡¼¥¸¥ç¥ó/Ãøºî¸¢)
26 define('S_VERSION','1.4.1');
27 define('S_COPYRIGHT','
28 <strong>"PukiWiki" '.S_VERSION.'</strong> Copyright &copy; 2001,2002,2003
29 <a href="http://pukiwiki.org">PukiWiki Developers Team</a>.
30 License is <a href="http://www.gnu.org/">GNU/GPL</a>.<br />
31 Based on "PukiWiki" 1.3 by <a href="http://factage.com/sng/">sng</a>
32 ');
33
34 /////////////////////////////////////////////////
35 // ½é´üÀßÄê (¥µ¡¼¥ÐÊÑ¿ô)
36 foreach (array('HTTP_USER_AGENT','PHP_SELF','SERVER_NAME','SERVER_SOFTWARE','SERVER_ADMIN') as $key) {
37         define($key,array_key_exists($key,$_SERVER) ? $_SERVER[$key] : '');
38 }
39
40 /////////////////////////////////////////////////
41 // ½é´üÀßÄê (¥°¥í¡¼¥Ð¥ëÊÑ¿ô)
42 // ¥µ¡¼¥Ð¤«¤éÍè¤ëÊÑ¿ô
43 $vars = array();
44 // µÓÃí
45 $foot_explain = array();
46 // ´ØÏ¢¤¹¤ë¥Ú¡¼¥¸
47 $related = array();
48 // <head>Æâ¤ËÄɲ乤륿¥°
49 $head_tags = array();
50
51 /////////////////////////////////////////////////
52 // ½é´üÀßÄê(»þ´Ö)
53 define('LOCALZONE',date('Z'));
54 define('UTIME',time() - LOCALZONE);
55 define('MUTIME',getmicrotime());
56
57 /////////////////////////////////////////////////
58 // ¸À¸ì¥Õ¥¡¥¤¥ëÆɤ߹þ¤ß
59 if (!file_exists(LANG.'.lng')||!is_readable(LANG.'.lng')) {
60         die_message(LANG.'.lng(language file) is not found.');
61 }
62 require(LANG.'.lng');
63
64 /////////////////////////////////////////////////
65 // ÀßÄê¥Õ¥¡¥¤¥ëÆɤ߹þ¤ß
66 if (!file_exists(INI_FILE)||!is_readable(INI_FILE)) {
67         die_message(INI_FILE.' is not found.');
68 }
69 require(INI_FILE);
70
71 /////////////////////////////////////////////////
72 // ½é´üÀßÄê($script)
73 if (!isset($script) or $script == '') {
74         $script = get_script_uri();
75 }
76 if ($script === FALSE or
77         (php_sapi_name() == 'cgi' and
78                 !preg_match('/^https?:\/\/[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]*$/',$script))) {
79         die_message("please set '\$script' in ".INI_FILE);
80 }
81
82 /////////////////////////////////////////////////
83 // ÀßÄê¥Õ¥¡¥¤¥ëÆɤ߹þ¤ß(UserAgent)
84 foreach ($agents as $agent) {
85         if (preg_match($agent['pattern'],HTTP_USER_AGENT,$matches)) {
86                 $agent['matches'] = $matches;
87                 $user_agent = $agent;
88                 break;
89         }
90 }
91 define('UA_INI_FILE',$user_agent['name'].'.ini.php');
92
93 if (!file_exists(UA_INI_FILE)||!is_readable(UA_INI_FILE)) {
94         die_message(UA_INI_FILE.' is not found.');
95 }
96 require(UA_INI_FILE);
97
98 /////////////////////////////////////////////////
99 // ÀßÄê¥Õ¥¡¥¤¥ë¤ÎÊÑ¿ô¥Á¥§¥Ã¥¯
100 if(!is_writable(DATA_DIR)) {
101         die_message('DATA_DIR is not found or not writable.');
102 }
103 if(!is_writable(DIFF_DIR)) {
104         die_message('DIFF_DIR is not found or not writable.');
105 }
106 if($do_backup && !is_writable(BACKUP_DIR)) {
107         die_message('BACKUP_DIR is not found or not writable.');
108 }
109 if(!is_writable(CACHE_DIR)) {
110         die_message('CACHE_DIR is not found or not writable.');
111 }
112 $wrong_ini_file = '';
113 if (!isset($rss_max)) $wrong_ini_file .= '$rss_max ';
114 if (!isset($page_title)) $wrong_ini_file .= '$page_title ';
115 if (!isset($note_hr)) $wrong_ini_file .= '$note_hr ';
116 if (!isset($related_link)) $wrong_ini_file .= '$related_link ';
117 if (!isset($show_passage)) $wrong_ini_file .= '$show_passage ';
118 if (!isset($rule_related_str)) $wrong_ini_file .= '$rule_related_str ';
119 if (!isset($load_template_func)) $wrong_ini_file .= '$load_template_func ';
120 if (!defined('LANG')) $wrong_ini_file .= 'LANG ';
121 if (!defined('PLUGIN_DIR')) $wrong_ini_file .= 'PLUGIN_DIR ';
122 if ($wrong_ini_file) {
123         die_message('The setting file runs short of information.<br />The version of a setting file may be old.<br /><br />These option are not found : '.$wrong_ini_file);
124 }
125 if (!is_page($defaultpage)) {
126         touch(get_filename($defaultpage));
127 }
128 if (!is_page($whatsnew)) {
129         touch(get_filename($whatsnew));
130 }
131 if (!is_page($interwiki)) {
132         touch(get_filename($interwiki));
133 }
134
135 /////////////////////////////////////////////////
136 // ³°Éô¤«¤é¤¯¤ëÊÑ¿ô¤ò¥µ¥Ë¥¿¥¤¥º
137 $get    = sanitize($_GET);
138 $post   = sanitize($_POST);
139 $cookie = sanitize($_COOKIE);
140
141 /////////////////////////////////////////////////
142 // Ê¸»ú¥³¡¼¥É¤òÊÑ´¹
143
144 // <form> ¤ÇÁ÷¿®¤µ¤ì¤¿Ê¸»ú (¥Ö¥é¥¦¥¶¤¬¥¨¥ó¥³¡¼¥É¤·¤¿¥Ç¡¼¥¿) ¤Î¥³¡¼¥É¤òÊÑ´¹
145 // post ¤Ï¾ï¤Ë <form> ¤Ê¤Î¤Ç¡¢É¬¤ºÊÑ´¹
146 if (array_key_exists('encode_hint',$post))
147 {
148         // html.php ¤ÎÃæ¤Ç¡¢<form> ¤Ë encode_hint ¤ò»Å¹þ¤ó¤Ç¤¤¤ë¤Î¤Ç¡¢É¬¤º encode_hint ¤¬¤¢¤ë¤Ï¤º¡£
149         // encode_hint ¤Î¤ß¤òÍѤ¤¤Æ¥³¡¼¥É¸¡½Ð¤¹¤ë¡£
150         // Á´ÂΤò¸«¤Æ¥³¡¼¥É¸¡½Ð¤¹¤ë¤È¡¢µ¡¼ï°Í¸ʸ»ú¤ä¡¢Ì¯¤Ê¥Ð¥¤¥Ê¥ê¥³¡¼¥É¤¬º®Æþ¤·¤¿¾ì¹ç¤Ë¡¢
151         // ¥³¡¼¥É¸¡½Ð¤Ë¼ºÇÔ¤¹¤ë¶²¤ì¤¬¤¢¤ë¤¿¤á¡£
152         $encode = mb_detect_encoding($post['encode_hint']);
153         mb_convert_variables(SOURCE_ENCODING,$encode,$post);
154 }
155 else if (array_key_exists('charset',$post))
156 {
157         // TrackBack Ping¤Ë´Þ¤Þ¤ì¤Æ¤¤¤ë¤³¤È¤¬¤¢¤ë
158         // »ØÄꤵ¤ì¤¿¾ì¹ç¤Ï¡¢¤½¤ÎÆâÍƤÇÊÑ´¹¤ò»î¤ß¤ë
159         if (mb_convert_variables(SOURCE_ENCODING,$post['charset'],$post) !== $post['charset'])
160         {
161                 // ¤¦¤Þ¤¯¤¤¤«¤Ê¤«¤Ã¤¿¾ì¹ç¤Ï¥³¡¼¥É¸¡½Ð¤ÎÀßÄê¤ÇÊÑ´¹¤·¤Ê¤ª¤·
162                 mb_convert_variables(SOURCE_ENCODING,'auto',$post);
163         }
164 }
165 else if (count($post) > 0)
166 {
167         // encode_hint ¤¬Ìµ¤¤¤È¤¤¤¦¤³¤È¤Ï¡¢Ìµ¤¤¤Ï¤º¡£
168         // ¥Ç¥Ð¥Ã¥°ÍѤˡ¢¼è¤ê¤¢¤¨¤º¡¢·Ù¹ð¥á¥Ã¥»¡¼¥¸¤ò½Ð¤·¤Æ¤ª¤­¤Þ¤¹¡£
169 //      echo "<p>Warning: 'encode_hint' field is not found in the posted data.</p>\n";
170         // Á´Éô¤Þ¤È¤á¤Æ¡¢¥³¡¼¥É¸¡½Ð¡¢ÊÑ´¹
171         mb_convert_variables(SOURCE_ENCODING,'auto',$post);
172 }
173
174 // get ¤Ï <form> ¤«¤é¤Î¾ì¹ç¤È¡¢<a href="http;//script/?query> ¤Î¾ì¹ç¤¬¤¢¤ë
175 if (array_key_exists('encode_hint',$get))
176 {
177         // <form> ¤Î¾ì¹ç¤Ï¡¢¥Ö¥é¥¦¥¶¤¬¥¨¥ó¥³¡¼¥É¤·¤Æ¤¤¤ë¤Î¤Ç¡¢¥³¡¼¥É¸¡½Ð¡¦ÊÑ´¹¤¬É¬Íס£
178         // encode_hint ¤¬´Þ¤Þ¤ì¤Æ¤¤¤ë¤Ï¤º¤Ê¤Î¤Ç¡¢¤½¤ì¤ò¸«¤Æ¡¢¥³¡¼¥É¸¡½Ð¤·¤¿¸å¡¢ÊÑ´¹¤¹¤ë¡£
179         // Íýͳ¤Ï¡¢post ¤ÈƱÍÍ
180         $encode = mb_detect_encoding($get['encode_hint']);
181         mb_convert_variables(SOURCE_ENCODING,$encode,$get);
182 }       
183 // <a href...> ¤Î¾ì¹ç¤Ï¡¢¥µ¡¼¥Ð¡¼¤¬ rawurlencode ¤·¤Æ¤¤¤ë¤Î¤Ç¡¢¥³¡¼¥ÉÊÑ´¹¤ÏÉÔÍ×
184
185 // QUERY_STRING¤ò¼èÆÀ
186 // cmd¤âplugin¤â»ØÄꤵ¤ì¤Æ¤¤¤Ê¤¤¾ì¹ç¤Ï¡¢QUERY_STRING¤ò¥Ú¡¼¥¸Ì¾¤«InterWikiName¤Ç¤¢¤ë¤È¤ß¤Ê¤¹°Ù
187 // ¤Þ¤¿¡¢URI ¤ò urlencode ¤»¤º¤Ë¼êÂǤÁ¤ÇÆþÎϤ·¤¿¾ì¹ç¤ËÂн褹¤ë°Ù
188 $arg = '';
189 if ($_SERVER['QUERY_STRING'] != '')
190 {
191         $arg = $_SERVER['QUERY_STRING'];
192 }
193 else if (array_key_exists(0,$_SERVER['argv']))
194 {
195         $arg = $_SERVER['argv'][0];
196 }
197
198 // ¥µ¥Ë¥¿¥¤¥º (\0 ½üµî)
199 $arg = sanitize($arg);
200
201 // URI ¼êÂǤξì¹ç¡¢¥³¡¼¥ÉÊÑ´¹¤·¡¢get[] ¤Ë¾å½ñ¤­
202 // mb_convert_variables¤Î¥Ð¥°(?)Âкö ÇÛÎó¤ÇÅϤµ¤Ê¤¤¤ÈÍî¤Á¤ë
203 $arg = array($arg);
204 mb_convert_variables(SOURCE_ENCODING,'auto',$arg);
205 $arg = $arg[0];
206
207 foreach (explode('&',$arg) as $tmp_string)
208 {
209         if (preg_match('/^([^=]+)=(.+)/',$tmp_string,$matches)
210                 and mb_detect_encoding($matches[2]) != 'ASCII')
211         {
212                 $get[$matches[1]] = $matches[2];
213         }
214 }
215
216 if (!empty($get['page']))
217 {
218         $get['page']  = strip_bracket($get['page']);
219 }
220 if (!empty($post['page']))
221 {
222         $post['page'] = strip_bracket($post['page']);
223 }
224 if (!empty($post['msg']))
225 {
226         $post['msg']  = str_replace("\r",'',$post['msg']);
227 }
228
229 $vars = array_merge($post,$get);
230 if (!array_key_exists('page',$vars))
231 {
232         $get['page'] = $post['page'] = $vars['page'] = '';
233 }
234
235 // ¸åÊý¸ß´¹À­ (?md5=...)
236 if (array_key_exists('md5',$vars) and $vars['md5'] != '')
237 {
238         $vars['cmd'] = 'md5';
239 }
240
241 // cmd¤âplugin¤â»ØÄꤵ¤ì¤Æ¤¤¤Ê¤¤¾ì¹ç¤Ï¡¢QUERY_STRING¤ò¥Ú¡¼¥¸Ì¾¤«InterWikiName¤Ç¤¢¤ë¤È¤ß¤Ê¤¹
242 if (!array_key_exists('cmd',$vars)  and !array_key_exists('plugin',$vars))
243 {
244         if ($arg == '')
245         {
246                 //¤Ê¤Ë¤â»ØÄꤵ¤ì¤Æ¤¤¤Ê¤«¤Ã¤¿¾ì¹ç¤Ï$defaultpage¤òɽ¼¨
247                 $arg = $defaultpage;
248         }               
249         $arg = rawurldecode($arg);
250         $arg = strip_bracket($arg);
251         $arg = sanitize($arg);
252
253         $get['cmd'] = $post['cmd'] = $vars['cmd'] = 'read';
254         $get['page'] = $post['page'] = $vars['page'] = $arg;
255 }
256
257 /////////////////////////////////////////////////
258 // ½é´üÀßÄê($WikiName,$BracketName¤Ê¤É)
259 // $WikiName = '[A-Z][a-z]+(?:[A-Z][a-z]+)+';
260 // $WikiName = '\b[A-Z][a-z]+(?:[A-Z][a-z]+)+\b';
261 // $WikiName = '(?<![[:alnum:]])(?:[[:upper:]][[:lower:]]+){2,}(?![[:alnum:]])';
262 // $WikiName = '(?<!\w)(?:[A-Z][a-z]+){2,}(?!\w)';
263 // BugTrack/304»ÃÄêÂнè
264 $WikiName = '(?:[A-Z][a-z]+){2,}(?!\w)';
265 // $BracketName = ':?[^\s\]#&<>":]+:?';
266 $BracketName = '(?!\s):?[^\r\n\t\f\[\]<>#&":]+:?(?<!\s)';
267 // InterWiki
268 $InterWikiName = "(\[\[)?((?:(?!\s|:|\]\]).)+):(.+)(?(1)\]\])";
269 // Ãí¼á
270 $NotePattern = '/\(\(((?:(?>(?:(?!\(\()(?!\)\)(?:[^\)]|$)).)+)|(?R))*)\)\)/ex';
271
272 /////////////////////////////////////////////////
273 // ½é´üÀßÄê(¥æ¡¼¥¶ÄêµÁ¥ë¡¼¥ëÆɤ߹þ¤ß)
274 require('rules.ini.php');
275
276 /////////////////////////////////////////////////
277 // ½é´üÀßÄê(¤½¤Î¾¤Î¥°¥í¡¼¥Ð¥ëÊÑ¿ô)
278 // ¸½ºß»þ¹ï
279 $now = format_date(UTIME);
280 // skinÆâ¤ÇDTDÀë¸À¤òÀÚ¤êÂؤ¨¤ë¤Î¤Ë»ÈÍÑ¡£paint.inc.phpÂкö
281 // FALSE:XHTML 1.1
282 // TRUE :XHTML 1.0 Transitional
283 $html_transitional = FALSE;
284 // ¥Õ¥§¥¤¥¹¥Þ¡¼¥¯¤ò$line_rules¤Ë²Ã¤¨¤ë
285 if ($usefacemark)
286 {
287         $line_rules += $facemark_rules;
288 }
289 unset($facemark_rules);
290 // ¼ÂÂλ²¾È¥Ñ¥¿¡¼¥ó¤ª¤è¤Ó¥·¥¹¥Æ¥à¤Ç»ÈÍѤ¹¤ë¥Ñ¥¿¡¼¥ó¤ò$line_rules¤Ë²Ã¤¨¤ë
291 //$entity_pattern = '[a-zA-Z0-9]{2,8}';
292 $entity_pattern = trim(join('',file(CACHE_DIR.'entities.dat')));
293 $line_rules = array_merge(array(
294         '&amp;(#[0-9]+|#x[0-9a-f]+|'.$entity_pattern.');'=>'&$1;',
295         "\r"=>"<br />\n", /* ¹ÔËö¤Ë¥Á¥ë¥À¤Ï²þ¹Ô */
296         '#related$'=>'<del>#related</del>',
297         '^#contents$'=>'<del>#contents</del>'
298 ),$line_rules);
299 ?>