2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
5 // $Id: file.php,v 1.19 2003/05/26 13:46:31 arino Exp $
9 function get_source($page)
11 if (!is_page($page)) {
14 return str_replace("\r",'',file(get_filename($page)));
17 // ¥Ú¡¼¥¸¤Î¹¹¿·»þ¹ï¤òÆÀ¤ë
18 function get_filetime($page)
20 return filemtime(get_filename($page)) - LOCALZONE;
23 // ¥Ú¡¼¥¸¤Î¥Õ¥¡¥¤¥ë̾¤òÆÀ¤ë
24 function get_filename($page)
26 return DATA_DIR.encode($page).'.txt';
30 function page_write($page,$postdata)
32 $postdata = make_str_rules($postdata);
35 $oldpostdata = is_page($page) ? join('',get_source($page)) : '';
36 $diffdata = do_diff($oldpostdata,$postdata);
37 file_write(DIFF_DIR,$page,$diffdata);
40 make_backup($page,$postdata == '');
43 file_write(DATA_DIR,$page,$postdata);
45 // is_page¤Î¥¥ã¥Ã¥·¥å¤ò¥¯¥ê¥¢¤¹¤ë¡£
48 // link¥Ç¡¼¥¿¥Ù¡¼¥¹¤ò¹¹¿·
52 // ¥æ¡¼¥¶ÄêµÁ¥ë¡¼¥ë(¥½¡¼¥¹¤òÃÖ´¹¤¹¤ë)
53 function make_str_rules($str)
55 global $str_rules,$fixed_heading_anchor;
57 $arr = explode("\n",$str);
59 foreach ($arr as $str)
61 if ($str != '' and $str{0} != ' ' and $str{0} != "\t")
63 foreach ($str_rules as $rule => $replace)
65 $str = preg_replace("/$rule/",$replace,$str);
68 // ¸«½Ð¤·¤Ë¸ÇÍID¤òÉÕÍ¿¤¹¤ë
69 if ($fixed_heading_anchor and
70 preg_match('/^(\*{1,3}(.(?!\[#[A-Za-z][\w-]+\]))+)$/',$str,$matches))
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]";
81 return join("\n",$retvars);
85 function file_write($dir,$page,$str)
87 global $post,$update_exec;
88 global $_msg_invalidiwn;
90 if (!is_pagename($page))
93 str_replace('$1',htmlspecialchars($page),
94 str_replace('$2','WikiName',$_msg_invalidiwn)
98 $page = strip_bracket($page);
100 $file = $dir.encode($page).'.txt';
102 if ($dir == DATA_DIR and $str == '' and file_exists($file)) {
106 $str = preg_replace("/\r/",'',$str);
107 $str = rtrim($str)."\n";
109 if (!empty($post['notimestamp']) and file_exists($file)) {
110 $timestamp = filemtime($file) - LOCALZONE;
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');
120 touch($file,$timestamp + LOCALZONE);
128 if ($update_exec and $dir == DATA_DIR) {
129 system($update_exec.' > /dev/null &');
133 // ºÇ½ª¹¹¿·¥Ú¡¼¥¸¤Î¹¹¿·
134 function put_lastmodified()
136 global $script,$post,$maxshow,$whatsnew,$non_list,$autolink;
138 $pages = get_existpages();
139 $recent_pages = array();
140 foreach($pages as $page)
142 if ($page != $whatsnew and !preg_match("/$non_list/",$page))
144 $recent_pages[$page] = get_filetime($page);
149 arsort($recent_pages,SORT_NUMERIC);
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');
155 foreach ($recent_pages as $page=>$time)
157 fputs($fp,"$time\t$page\n");
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');
166 foreach (array_splice($recent_pages,0,$maxshow) as $page=>$time)
168 $s_lastmod = htmlspecialchars(format_date($time));
169 $s_page = htmlspecialchars($page);
170 fputs($fp, "-$s_lastmod - [[$s_page]]\n");
172 fputs($fp,"#norelated\n"); // :)
179 list($pattern,$forceignorelist) = get_autolink_pattern($pages);
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');
184 fputs($fp,$pattern."\n");
185 fputs($fp,join("\t",$forceignorelist));
191 // »ØÄꤵ¤ì¤¿¥Ú¡¼¥¸¤Î·Ð²á»þ¹ï
192 function get_pg_passage($page,$sw=TRUE)
194 global $show_passage;
195 static $pg_passage = array();
202 if (!array_key_exists($page,$pg_passage))
204 $pg_passage[$page] = (is_page($page) and $time = get_filetime($page)) ?
205 get_passage($time) : '';
208 return $sw ? "<small>{$pg_passage[$page]}</small>" : " {$pg_passage[$page]}";
211 // Last-Modified ¥Ø¥Ã¥À
212 function header_lastmod()
216 if ($lastmod and is_page($page)) {
217 header('Last-Modified: '.date('D, d M Y H:i:s',get_filetime($page)).' GMT');
221 // Á´¥Ú¡¼¥¸Ì¾¤òÇÛÎó¤Ë
222 function get_existpages($dir=DATA_DIR,$ext='.txt')
226 $pattern = '^([0-9A-F]+)';
229 $pattern .= preg_quote($ext,'/').'$';
232 or die_message($dir. ' is not found or not readable.');
233 while ($file = readdir($dp))
235 if (preg_match("/$pattern/",$file,$matches))
237 $aryret[$file] = decode($matches[1]);
243 //¥Õ¥¡¥¤¥ë̾¤Î°ìÍ÷¤òÇÛÎó¤Ë(¥¨¥ó¥³¡¼¥ÉºÑ¤ß¡¢³ÈÄ¥»Ò¤ò»ØÄê)
244 function get_existfiles($dir,$ext)
248 $pattern = '^[0-9A-F]+'.preg_quote($ext,'/').'$';
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;
259 //¤¢¤ë¥Ú¡¼¥¸¤Î´ØÏ¢¥Ú¡¼¥¸¤òÆÀ¤ë
260 function links_get_related($page)
262 global $vars,$related;
263 static $links = array();
265 if (array_key_exists($page,$links))
267 return $links[$page];
270 // ²Äǽ¤Ê¤émake_link()¤ÇÀ¸À®¤·¤¿´ØÏ¢¥Ú¡¼¥¸¤ò¼è¤ê¹þ¤à
271 $links[$page] = ($page == $vars['page']) ? $related : array();
273 // ¥Ç¡¼¥¿¥Ù¡¼¥¹¤«¤é´ØÏ¢¥Ú¡¼¥¸¤òÆÀ¤ë
274 $links[$page] += links_get_related_db($vars['page']);
276 return $links[$page];