OSDN Git Service

BugTrack/487: AutoLink対象のページ名がひとつもないときに、不正なパターンを生成していた
[pukiwiki/pukiwiki.git] / file.php
1 <?php
2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
4 //
5 // $Id: file.php,v 1.37 2003/11/05 10:58:39 arino Exp $
6 //
7
8 // ¥½¡¼¥¹¤ò¼èÆÀ
9 function get_source($page=NULL)
10 {
11         if (!is_page($page))
12         {
13                 return array();
14         }
15         return str_replace("\r",'',file(get_filename($page)));
16 }
17
18 // ¥Ú¡¼¥¸¤Î¹¹¿·»þ¹ï¤òÆÀ¤ë
19 function get_filetime($page)
20 {
21         if (!is_page($page))
22         {
23                 return 0;
24         }
25         return filemtime(get_filename($page)) - LOCALZONE;
26 }
27
28 // ¥Ú¡¼¥¸¤Î¥Õ¥¡¥¤¥ë̾¤òÆÀ¤ë
29 function get_filename($page)
30 {
31         return DATA_DIR.encode($page).'.txt';
32 }
33
34 // ¥Ú¡¼¥¸¤Î½ÐÎÏ
35 function page_write($page,$postdata,$notimestamp=FALSE)
36 {
37         $postdata = make_str_rules($postdata);
38         
39         // º¹Ê¬¥Õ¥¡¥¤¥ë¤ÎºîÀ®
40         $oldpostdata = is_page($page) ? join('',get_source($page)) : '';
41         $diffdata = do_diff($oldpostdata,$postdata);
42         file_write(DIFF_DIR,$page,$diffdata);
43         
44         // ¥Ð¥Ã¥¯¥¢¥Ã¥×¤ÎºîÀ®
45         make_backup($page,$postdata == '');
46         
47         // ¥Õ¥¡¥¤¥ë¤Î½ñ¤­¹þ¤ß
48         file_write(DATA_DIR,$page,$postdata,$notimestamp);
49         
50         // TrackBack Ping ¤ÎÁ÷¿®
51         // ¡ÖÄɲá׹ԤòÃê½Ð
52         $lines = join("\n",preg_replace('/^\+/','',preg_grep('/^\+/',explode("\n",$diffdata))));
53         tb_send($page,$lines);
54         
55         // link¥Ç¡¼¥¿¥Ù¡¼¥¹¤ò¹¹¿·
56         links_update($page);
57 }
58
59 // ¥æ¡¼¥¶ÄêµÁ¥ë¡¼¥ë(¥½¡¼¥¹¤òÃÖ´¹¤¹¤ë)
60 function make_str_rules($str)
61 {
62         global $str_rules,$fixed_heading_anchor;
63         
64         $arr = explode("\n",$str);
65         
66         $retvars = array();
67         foreach ($arr as $str)
68         {
69                 if ($str != '' and $str{0} != ' ' and $str{0} != "\t")
70                 {
71                         foreach ($str_rules as $rule => $replace)
72                         {
73                                 $str = preg_replace("/$rule/",$replace,$str);
74                         }
75                 }
76                 // ¸«½Ð¤·¤Ë¸ÇÍ­ID¤òÉÕÍ¿¤¹¤ë
77                 if ($fixed_heading_anchor and
78                         preg_match('/^(\*{1,3}(.(?!\[#[A-Za-z][\w-]+\]))+)$/',$str,$matches))
79                 {
80                         // ¸ÇÍ­ID¤òÀ¸À®¤¹¤ë
81                         // ¥é¥ó¥À¥à¤Ê±Ñ»ú(1ʸ»ú)+md5¥Ï¥Ã¥·¥å¤Î¥é¥ó¥À¥à¤ÊÉôʬʸ»úÎó(7ʸ»ú)
82                         $anchor = chr(mt_rand(ord('a'),ord('z'))).
83                                 substr(md5(uniqid(substr($matches[1],0,100),1)),mt_rand(0,24),7);
84                         $str = rtrim($matches[1])." [#$anchor]";
85                 }
86                 $retvars[] = $str;
87         }
88         
89         return join("\n",$retvars);
90 }
91
92 // ¥Õ¥¡¥¤¥ë¤Ø¤Î½ÐÎÏ
93 function file_write($dir,$page,$str,$notimestamp=FALSE)
94 {
95         global $post,$update_exec;
96         global $_msg_invalidiwn;
97         global $notify,$notify_diff_only,$notify_to,$notify_from,$notify_subject,$notify_header;
98         global $smtp_server,$smtp_auth;
99         
100         if (!is_pagename($page))
101         {
102                 die_message(
103                         str_replace('$1',htmlspecialchars($page),
104                                 str_replace('$2','WikiName',$_msg_invalidiwn)
105                         )
106                 );
107         }
108         $page = strip_bracket($page);
109         $timestamp = FALSE;
110         $file = $dir.encode($page).'.txt';
111         
112         if ($dir == DATA_DIR and $str == '' and file_exists($file))
113         {
114                 unlink($file);
115         }
116         if ($str != '')
117         {
118                 $str = preg_replace("/\r/",'',$str);
119                 $str = rtrim($str)."\n";
120                 
121                 if ($notimestamp and file_exists($file))
122                 {
123                         $timestamp = filemtime($file) - LOCALZONE;
124                 }
125                 
126                 $fp = fopen($file,'w')
127                         or die_message('cannot write page file or diff file or other'.htmlspecialchars($page).'<br />maybe permission is not writable or filename is too long');
128                 flock($fp,LOCK_EX);
129                 fputs($fp,$str);
130                 flock($fp,LOCK_UN);
131                 fclose($fp);
132                 if ($timestamp)
133                 {
134                         touch($file,$timestamp + LOCALZONE);
135                 }
136         }
137         
138         // is_page¤Î¥­¥ã¥Ã¥·¥å¤ò¥¯¥ê¥¢¤¹¤ë¡£
139         is_page($page,TRUE);
140         
141         if (!$timestamp and $dir == DATA_DIR)
142         {
143                 put_lastmodified();
144         }
145         
146         if ($update_exec and $dir == DATA_DIR)
147         {
148                 system($update_exec.' > /dev/null &');
149         }
150         
151         if ($notify and $dir == DIFF_DIR)
152         {
153                 if ($notify_diff_only)
154                 {
155                         // º¹Ê¬¤À¤±¤òÁ÷¿®¤¹¤ë
156                         $str = preg_replace('/^[^-+].*\n/m','',$str);
157                 }
158                 if ($smtp_auth)
159                 {
160                         pop_before_smtp();
161                 }
162                 $subject = str_replace('$page',$page,$notify_subject);
163                 ini_set('SMTP',$smtp_server);
164                 mb_language(LANG);
165                 mb_send_mail($notify_to,$subject,$str,$notify_header);
166         }
167 }
168
169 // ºÇ½ª¹¹¿·¥Ú¡¼¥¸¤Î¹¹¿·
170 function put_lastmodified()
171 {
172         global $maxshow,$whatsnew,$non_list,$autolink;
173
174         $pages = get_existpages();
175         $recent_pages = array();
176         foreach($pages as $page)
177         {
178                 if ($page != $whatsnew and !preg_match("/$non_list/",$page))
179                 {
180                         $recent_pages[$page] = get_filetime($page);
181                 }
182         }
183         
184         //»þ¹ï¹ß½ç¤Ç¥½¡¼¥È
185         arsort($recent_pages,SORT_NUMERIC);
186         
187         // create recent.dat (for recent.inc.php)
188         $fp = fopen(CACHE_DIR.'recent.dat','w')
189                 or die_message('cannot write cache file '.CACHE_DIR.'recent.dat<br />maybe permission is not writable or filename is too long');
190         flock($fp,LOCK_EX);
191         foreach ($recent_pages as $page=>$time)
192         {
193                 fputs($fp,"$time\t$page\n");
194         }
195         flock($fp,LOCK_UN);
196         fclose($fp);
197
198         // create RecentChanges
199         $fp = fopen(get_filename($whatsnew),'w')
200                 or die_message('cannot write page file '.htmlspecialchars($whatsnew).'<br />maybe permission is not writable or filename is too long');
201         flock($fp,LOCK_EX);
202         foreach (array_splice($recent_pages,0,$maxshow) as $page=>$time)
203         {
204                 $s_lastmod = htmlspecialchars(format_date($time));
205                 $s_page = htmlspecialchars($page);
206                 fputs($fp, "-$s_lastmod - [[$s_page]]\n");
207         }
208         fputs($fp,"#norelated\n"); // :)
209         flock($fp,LOCK_UN);
210         fclose($fp);
211         
212         // for autolink
213         if ($autolink)
214         {
215                 list($pattern,$pattern_a,$forceignorelist) = get_autolink_pattern($pages);
216                 
217                 $fp = fopen(CACHE_DIR.'autolink.dat','w')
218                         or die_message('cannot write autolink file '.CACHE_DIR.'/autolink.dat<br />maybe permission is not writable');
219                 flock($fp,LOCK_EX);
220                 fputs($fp,$pattern."\n");
221                 fputs($fp,$pattern_a."\n");
222                 fputs($fp,join("\t",$forceignorelist)."\n");
223                 flock($fp,LOCK_UN);
224                 fclose($fp);
225         }
226 }
227
228 // »ØÄꤵ¤ì¤¿¥Ú¡¼¥¸¤Î·Ð²á»þ¹ï
229 function get_pg_passage($page,$sw=TRUE)
230 {
231         global $show_passage;
232         static $pg_passage = array();
233         
234         if (!$show_passage)
235         {
236                 return '';
237         }
238         
239         if (!array_key_exists($page,$pg_passage))
240         {
241                 $pg_passage[$page] = (is_page($page) and $time = get_filetime($page)) ?
242                         get_passage($time) : '';
243         }
244         
245         return $sw ? "<small>{$pg_passage[$page]}</small>" : " {$pg_passage[$page]}";
246 }
247
248 // Last-Modified ¥Ø¥Ã¥À
249 function header_lastmod($page=NULL)
250 {
251         global $lastmod;
252         
253         if ($lastmod and is_page($page))
254         {
255                 header('Last-Modified: '.date('D, d M Y H:i:s',get_filetime($page)).' GMT');
256         }
257 }
258
259 // Á´¥Ú¡¼¥¸Ì¾¤òÇÛÎó¤Ë
260 function get_existpages($dir=DATA_DIR,$ext='.txt')
261 {
262         $aryret = array();
263         
264         $pattern = '^((?:[0-9A-F]{2})+)';
265         if ($ext != '')
266         {
267                 $pattern .= preg_quote($ext,'/').'$';
268         }
269         $dp = @opendir($dir)
270                 or die_message($dir. ' is not found or not readable.');
271         while ($file = readdir($dp))
272         {
273                 if (preg_match("/$pattern/",$file,$matches))
274                 {
275                         $aryret[$file] = decode($matches[1]);
276                 }
277         }
278         closedir($dp);
279         return $aryret;
280 }
281 // ¥Ú¡¼¥¸Ì¾¤ÎÆɤߤòÇÛÎó¤Ë
282 function get_readings()
283 {
284         global $pagereading_enable, $pagereading_kanji2kana_converter;
285         global $pagereading_kanji2kana_encoding, $pagereading_chasen_path;
286         global $pagereading_kakasi_path, $pagereading_config_page;
287         
288         $pages = get_existpages();
289         
290         $readings = array();
291         foreach ($pages as $page) {
292                 $readings[$page] = '';
293         }
294         foreach (get_source($pagereading_config_page) as $line) {
295                 $line = preg_replace('/[\s\r\n]+$/', '', $line);
296                 if(preg_match('/^-\[\[([^]]+)\]\]\s(.+)$/', $line, $matches)
297                    and isset($readings[$matches[1]])) {
298                         $readings[$matches[1]] = $matches[2];
299                 }
300         }
301         if($pagereading_enable) {
302                 // ChaSen/KAKASI ¸Æ¤Ó½Ð¤·¤¬Í­¸ú¤ËÀßÄꤵ¤ì¤Æ¤¤¤ë¾ì¹ç
303                 $unknownPage = FALSE;
304                 // Æɤߤ¬ÉÔÌÀ¤Î¥Ú¡¼¥¸¤¬¤¢¤ë¤«¥Á¥§¥Ã¥¯
305                 foreach ($readings as $page => $reading) {
306                         if($reading=='') {
307                                 $unknownPage = TRUE;
308                                 break;
309                         }
310                 }
311                 if($unknownPage) {
312                         // Æɤߤ¬ÉÔÌÀ¤Î¥Ú¡¼¥¸¤¬¤¢¤ë¾ì¹ç
313                         //                      $tmpfname = tempnam(CACHE_DIR, 'PageReading');
314                         $tmpfname = tempnam(CACHE_DIR, 'PageReading');
315                         $fp = fopen($tmpfname, "w")
316                                 or die_message("cannot write temporary file '$tmpfname'.\n");
317                         foreach ($readings as $page => $reading) {
318                                 if($reading=='') {
319                                         fputs($fp, mb_convert_encoding("$page\n", $pagereading_kanji2kana_encoding, SOURCE_ENCODING));
320                                 }
321                         }
322                         fclose($fp);
323                         // ChaSen/KAKASI ¤ò¼Â¹Ô
324                         switch(strtolower($pagereading_kanji2kana_converter)) {
325                         case 'chasen':
326                                 if(!file_exists($pagereading_chasen_path)) {
327                                         unlink($tmpfname);
328                                         die_message("CHASEN not found: $pagereading_chasen_path");
329                                 }
330                                 $fp = popen("$pagereading_chasen_path -F %y $tmpfname", "r");
331                                 if(!$fp) {
332                                         unlink($tmpfname);
333                                         die_message("ChaSen execution failed: $pagereading_chasen_path -F %y $tmpfname");
334                                 }
335                                 break;
336                         case 'kakasi':
337                         case 'kakashi':
338                                 if(!file_exists($pagereading_kakasi_path)) {
339                                         unlink($tmpfname);
340                                         die_message("KAKASI not found: $pagereading_kakasi_path");
341                                 }                                       
342                                 $fp = popen("$pagereading_kakasi_path -kK -HK -JK <$tmpfname", "r");
343                                 if(!$fp) {
344                                         unlink($tmpfname);
345                                         die_message("KAKASI execution failed: $pagereading_kakasi_path -kK -HK -JK <$tmpfname");
346                                 }
347                                 break;
348                         default:
349                                 die_message("unknown kanji-kana converter: $pagereading_kanji2kana_converter.");
350                                 break;
351                         }
352                         foreach ($readings as $page => $reading) {
353                                 if($reading=='') {
354                                         $line = fgets($fp);
355                                         $line = mb_convert_encoding($line, SOURCE_ENCODING, $pagereading_kanji2kana_encoding);
356                                         $line = preg_replace('/[\s\r\n]+$/', '', $line);
357                                         $readings[$page] = $line;
358                                 }
359                         }
360                         pclose($fp);
361                         unlink($tmpfname) or die_message("temporary file can not be removed: $tmpfname");
362                         // Æɤߤǥ½¡¼¥È
363                         asort($readings);
364                         
365                         // ¥Ú¡¼¥¸¤ò½ñ¤­¹þ¤ß
366                         $body = '';
367                         foreach ($readings as $page => $reading) {
368                                 $body .= "-[[$page]] $reading\n";
369                         }
370                         page_write($pagereading_config_page, $body);
371                 }
372         }
373         
374         // ÆɤßÉÔÌÀ¤Î¥Ú¡¼¥¸¤Ï¡¢¤½¤Î¤Þ¤Þ¥Ú¡¼¥¸Ì¾¤òÊÖ¤¹ (ChaSen/KAKASI ¸Æ
375         // ¤Ó½Ð¤·¤¬Ìµ¸ú¤ËÀßÄꤵ¤ì¤Æ¤¤¤ë¾ì¹ç¤ä¡¢ChaSen/KAKASI ¸Æ¤Ó½Ð¤·¤Ë
376         // ¼ºÇÔ¤·¤¿»þ¤Î°Ù)
377         foreach ($pages as $page) {
378                 if($readings[$page]=='') {
379                         $readings[$page] = $page;
380                 }
381         }
382         
383         return $readings;
384 }
385 //¥Õ¥¡¥¤¥ë̾¤Î°ìÍ÷¤òÇÛÎó¤Ë(¥¨¥ó¥³¡¼¥ÉºÑ¤ß¡¢³ÈÄ¥»Ò¤ò»ØÄê)
386 function get_existfiles($dir,$ext)
387 {
388         $aryret = array();
389         
390         $pattern = '^(?:[0-9A-F]{2})+'.preg_quote($ext,'/').'$';
391         $dp = @opendir($dir)
392                 or die_message($dir. ' is not found or not readable.');
393         while ($file = readdir($dp)) {
394                 if (preg_match("/$pattern/",$file)) {
395                         $aryret[] = $dir.$file;
396                 }
397         }
398         closedir($dp);
399         return $aryret;
400 }
401 //¤¢¤ë¥Ú¡¼¥¸¤Î´ØÏ¢¥Ú¡¼¥¸¤òÆÀ¤ë
402 function links_get_related($page)
403 {
404         global $vars,$related;
405         static $links = array();
406         
407         if (array_key_exists($page,$links))
408         {
409                 return $links[$page];
410         }
411         
412         // ²Äǽ¤Ê¤émake_link()¤ÇÀ¸À®¤·¤¿´ØÏ¢¥Ú¡¼¥¸¤ò¼è¤ê¹þ¤à
413         $links[$page] = ($page == $vars['page']) ? $related : array();
414         
415         // ¥Ç¡¼¥¿¥Ù¡¼¥¹¤«¤é´ØÏ¢¥Ú¡¼¥¸¤òÆÀ¤ë
416         $links[$page] += links_get_related_db($vars['page']);
417         
418         return $links[$page];
419 }
420 ?>