OSDN Git Service

BugTrack/344: 見出し行が長いときに固有IDの生成に失敗する問題を修理
[pukiwiki/pukiwiki.git] / file.php
1 <?php
2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
4 //
5 // $Id: file.php,v 1.19 2003/05/26 13:46:31 arino Exp $
6 //
7
8 // ¥½¡¼¥¹¤ò¼èÆÀ
9 function get_source($page)
10 {
11         if (!is_page($page)) {
12                 return array();
13         }
14         return str_replace("\r",'',file(get_filename($page)));
15 }
16
17 // ¥Ú¡¼¥¸¤Î¹¹¿·»þ¹ï¤òÆÀ¤ë
18 function get_filetime($page)
19 {
20         return filemtime(get_filename($page)) - LOCALZONE;
21 }
22
23 // ¥Ú¡¼¥¸¤Î¥Õ¥¡¥¤¥ë̾¤òÆÀ¤ë
24 function get_filename($page)
25 {
26         return DATA_DIR.encode($page).'.txt';
27 }
28
29 // ¥Ú¡¼¥¸¤Î½ÐÎÏ
30 function page_write($page,$postdata)
31 {
32         $postdata = make_str_rules($postdata);
33         
34         // º¹Ê¬¥Õ¥¡¥¤¥ë¤ÎºîÀ®
35         $oldpostdata = is_page($page) ? join('',get_source($page)) : '';
36         $diffdata = do_diff($oldpostdata,$postdata);
37         file_write(DIFF_DIR,$page,$diffdata);
38         
39         // ¥Ð¥Ã¥¯¥¢¥Ã¥×¤ÎºîÀ®
40         make_backup($page,$postdata == '');
41         
42         // ¥Õ¥¡¥¤¥ë¤Î½ñ¤­¹þ¤ß
43         file_write(DATA_DIR,$page,$postdata);
44         
45         // is_page¤Î¥­¥ã¥Ã¥·¥å¤ò¥¯¥ê¥¢¤¹¤ë¡£
46         is_page($page,TRUE);
47         
48         // link¥Ç¡¼¥¿¥Ù¡¼¥¹¤ò¹¹¿·
49         links_update($page);
50 }
51
52 // ¥æ¡¼¥¶ÄêµÁ¥ë¡¼¥ë(¥½¡¼¥¹¤òÃÖ´¹¤¹¤ë)
53 function make_str_rules($str)
54 {
55         global $str_rules,$fixed_heading_anchor;
56         
57         $arr = explode("\n",$str);
58         
59         foreach ($arr as $str)
60         {
61                 if ($str != '' and $str{0} != ' ' and $str{0} != "\t")
62                 {
63                         foreach ($str_rules as $rule => $replace)
64                         {
65                                 $str = preg_replace("/$rule/",$replace,$str);
66                         }
67                 }
68                 // ¸«½Ð¤·¤Ë¸ÇÍ­ID¤òÉÕÍ¿¤¹¤ë
69                 if ($fixed_heading_anchor and
70                         preg_match('/^(\*{1,3}(.(?!\[#[A-Za-z][\w-]+\]))+)$/',$str,$matches))
71                 {
72                         // ¸ÇÍ­ID¤òÀ¸À®¤¹¤ë
73                         // ¥é¥ó¥À¥à¤Ê±Ñ»ú(1ʸ»ú)+md5¥Ï¥Ã¥·¥å¤Î¥é¥ó¥À¥à¤ÊÉôʬʸ»úÎó(7ʸ»ú)
74                         $anchor = chr(mt_rand(ord('a'),ord('z'))).
75                                 substr(md5(uniqid(substr($matches[1],0,100),1)),mt_rand(0,24),7);
76                         $str = rtrim($matches[1])." [#$anchor]";
77                 }
78                 $retvars[] = $str;
79         }
80         
81         return join("\n",$retvars);
82 }
83
84 // ¥Õ¥¡¥¤¥ë¤Ø¤Î½ÐÎÏ
85 function file_write($dir,$page,$str)
86 {
87         global $post,$update_exec;
88         global $_msg_invalidiwn;
89         
90         if (!is_pagename($page))
91         {
92                 die_message(
93                         str_replace('$1',htmlspecialchars($page),
94                                 str_replace('$2','WikiName',$_msg_invalidiwn)
95                         )
96                 );
97         }
98         $page = strip_bracket($page);
99         $timestamp = FALSE;
100         $file = $dir.encode($page).'.txt';
101         
102         if ($dir == DATA_DIR and $str == '' and file_exists($file)) {
103                 unlink($file);
104         }
105         if ($str != '') {
106                 $str = preg_replace("/\r/",'',$str);
107                 $str = rtrim($str)."\n";
108                 
109                 if (!empty($post['notimestamp']) and file_exists($file)) {
110                         $timestamp = filemtime($file) - LOCALZONE;
111                 }
112                 
113                 $fp = fopen($file,'w')
114                         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');
115                 flock($fp,LOCK_EX);
116                 fputs($fp,$str);
117                 flock($fp,LOCK_UN);
118                 fclose($fp);
119                 if ($timestamp) {
120                         touch($file,$timestamp + LOCALZONE);
121                 }
122         }
123         
124         if (!$timestamp) {
125                 put_lastmodified();
126         }
127         
128         if ($update_exec and $dir == DATA_DIR) {
129                 system($update_exec.' > /dev/null &');
130         }
131 }
132
133 // ºÇ½ª¹¹¿·¥Ú¡¼¥¸¤Î¹¹¿·
134 function put_lastmodified()
135 {
136         global $script,$post,$maxshow,$whatsnew,$non_list,$autolink;
137
138         $pages = get_existpages();
139         $recent_pages = array();
140         foreach($pages as $page)
141         {
142                 if ($page != $whatsnew and !preg_match("/$non_list/",$page))
143                 {
144                         $recent_pages[$page] = get_filetime($page);
145                 }
146         }
147         
148         //»þ¹ï¹ß½ç¤Ç¥½¡¼¥È
149         arsort($recent_pages,SORT_NUMERIC);
150         
151         // create recent.dat (for recent.inc.php)
152         $fp = fopen(CACHE_DIR.'recent.dat','w')
153                 or die_message('cannot write cache file '.CACHE_DIR.'recent.dat<br>maybe permission is not writable or filename is too long');
154         flock($fp,LOCK_EX);
155         foreach ($recent_pages as $page=>$time)
156         {
157                 fputs($fp,"$time\t$page\n");
158         }
159         flock($fp,LOCK_UN);
160         fclose($fp);
161
162         // create RecentChanges
163         $fp = fopen(get_filename($whatsnew),'w')
164                 or die_message('cannot write page file '.htmlspecialchars($whatsnew).'<br>maybe permission is not writable or filename is too long');
165         flock($fp,LOCK_EX);
166         foreach (array_splice($recent_pages,0,$maxshow) as $page=>$time)
167         {
168                 $s_lastmod = htmlspecialchars(format_date($time));
169                 $s_page = htmlspecialchars($page);
170                 fputs($fp, "-$s_lastmod - [[$s_page]]\n");
171         }
172         fputs($fp,"#norelated\n"); // :)
173         flock($fp,LOCK_UN);
174         fclose($fp);
175         
176         // for autolink
177         if ($autolink)
178         {
179                 list($pattern,$forceignorelist) = get_autolink_pattern($pages);
180                 
181                 $fp = fopen(CACHE_DIR.'autolink.dat','w')
182                         or die_message('cannot write autolink file '.CACHE_DIR.'/autolink.dat<br>maybe permission is not writable');
183                 flock($fp,LOCK_EX);
184                 fputs($fp,$pattern."\n");
185                 fputs($fp,join("\t",$forceignorelist));
186                 flock($fp,LOCK_UN);
187                 fclose($fp);
188         }
189 }
190
191 // »ØÄꤵ¤ì¤¿¥Ú¡¼¥¸¤Î·Ð²á»þ¹ï
192 function get_pg_passage($page,$sw=TRUE)
193 {
194         global $show_passage;
195         static $pg_passage = array();
196         
197         if (!$show_passage)
198         {
199                 return '';
200         }
201         
202         if (!array_key_exists($page,$pg_passage))
203         {
204                 $pg_passage[$page] = (is_page($page) and $time = get_filetime($page)) ?
205                         get_passage($time) : '';
206         }
207         
208         return $sw ? "<small>{$pg_passage[$page]}</small>" : " {$pg_passage[$page]}";
209 }
210
211 // Last-Modified ¥Ø¥Ã¥À
212 function header_lastmod()
213 {
214         global $lastmod;
215         
216         if ($lastmod and is_page($page)) {
217                 header('Last-Modified: '.date('D, d M Y H:i:s',get_filetime($page)).' GMT');
218         }
219 }
220
221 // Á´¥Ú¡¼¥¸Ì¾¤òÇÛÎó¤Ë
222 function get_existpages($dir=DATA_DIR,$ext='.txt')
223 {
224         $aryret = array();
225         
226         $pattern = '^([0-9A-F]+)';
227         if ($ext != '')
228         {
229                 $pattern .= preg_quote($ext,'/').'$';
230         }
231         $dp = @opendir($dir)
232                 or die_message($dir. ' is not found or not readable.');
233         while ($file = readdir($dp))
234         {
235                 if (preg_match("/$pattern/",$file,$matches))
236                 {
237                         $aryret[$file] = decode($matches[1]);
238                 }
239         }
240         closedir($dp);
241         return $aryret;
242 }
243 //¥Õ¥¡¥¤¥ë̾¤Î°ìÍ÷¤òÇÛÎó¤Ë(¥¨¥ó¥³¡¼¥ÉºÑ¤ß¡¢³ÈÄ¥»Ò¤ò»ØÄê)
244 function get_existfiles($dir,$ext)
245 {
246         $aryret = array();
247         
248         $pattern = '^[0-9A-F]+'.preg_quote($ext,'/').'$';
249         $dp = @opendir($dir)
250                 or die_message($dir. ' is not found or not readable.');
251         while ($file = readdir($dp)) {
252                 if (preg_match("/$pattern/",$file)) {
253                         $aryret[] = $dir.$file;
254                 }
255         }
256         closedir($dp);
257         return $aryret;
258 }
259 //¤¢¤ë¥Ú¡¼¥¸¤Î´ØÏ¢¥Ú¡¼¥¸¤òÆÀ¤ë
260 function links_get_related($page)
261 {
262         global $vars,$related;
263         static $links = array();
264         
265         if (array_key_exists($page,$links))
266         {
267                 return $links[$page];
268         }
269         
270         // ²Äǽ¤Ê¤émake_link()¤ÇÀ¸À®¤·¤¿´ØÏ¢¥Ú¡¼¥¸¤ò¼è¤ê¹þ¤à
271         $links[$page] = ($page == $vars['page']) ? $related : array();
272         
273         // ¥Ç¡¼¥¿¥Ù¡¼¥¹¤«¤é´ØÏ¢¥Ú¡¼¥¸¤òÆÀ¤ë
274         $links[$page] += links_get_related_db($vars['page']);
275         
276         return $links[$page];
277 }
278 ?>