2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
5 // $Id: file.php,v 1.22 2003/06/10 13:59:02 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,$notimestamp=FALSE)
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,$notimestamp);
45 // TrackBack Ping ¤ÎÁ÷¿®
46 tb_send($page,$postdata);
48 // is_page¤Î¥¥ã¥Ã¥·¥å¤ò¥¯¥ê¥¢¤¹¤ë¡£
51 // link¥Ç¡¼¥¿¥Ù¡¼¥¹¤ò¹¹¿·
55 // ¥æ¡¼¥¶ÄêµÁ¥ë¡¼¥ë(¥½¡¼¥¹¤òÃÖ´¹¤¹¤ë)
56 function make_str_rules($str)
58 global $str_rules,$fixed_heading_anchor;
60 $arr = explode("\n",$str);
62 foreach ($arr as $str)
64 if ($str != '' and $str{0} != ' ' and $str{0} != "\t")
66 foreach ($str_rules as $rule => $replace)
68 $str = preg_replace("/$rule/",$replace,$str);
71 // ¸«½Ð¤·¤Ë¸ÇÍID¤òÉÕÍ¿¤¹¤ë
72 if ($fixed_heading_anchor and
73 preg_match('/^(\*{1,3}(.(?!\[#[A-Za-z][\w-]+\]))+)$/',$str,$matches))
76 // ¥é¥ó¥À¥à¤Ê±Ñ»ú(1ʸ»ú)+md5¥Ï¥Ã¥·¥å¤Î¥é¥ó¥À¥à¤ÊÉôʬʸ»úÎó(7ʸ»ú)
77 $anchor = chr(mt_rand(ord('a'),ord('z'))).
78 substr(md5(uniqid(substr($matches[1],0,100),1)),mt_rand(0,24),7);
79 $str = rtrim($matches[1])." [#$anchor]";
84 return join("\n",$retvars);
88 function file_write($dir,$page,$str,$notimestamp=FALSE)
90 global $post,$update_exec;
91 global $_msg_invalidiwn;
93 if (!is_pagename($page))
96 str_replace('$1',htmlspecialchars($page),
97 str_replace('$2','WikiName',$_msg_invalidiwn)
101 $page = strip_bracket($page);
103 $file = $dir.encode($page).'.txt';
105 if ($dir == DATA_DIR and $str == '' and file_exists($file)) {
109 $str = preg_replace("/\r/",'',$str);
110 $str = rtrim($str)."\n";
112 if ($notimestamp and file_exists($file)) {
113 $timestamp = filemtime($file) - LOCALZONE;
116 $fp = fopen($file,'w')
117 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');
123 touch($file,$timestamp + LOCALZONE);
131 if ($update_exec and $dir == DATA_DIR) {
132 system($update_exec.' > /dev/null &');
136 // ºÇ½ª¹¹¿·¥Ú¡¼¥¸¤Î¹¹¿·
137 function put_lastmodified()
139 global $maxshow,$whatsnew,$non_list,$autolink;
141 $pages = get_existpages();
142 $recent_pages = array();
143 foreach($pages as $page)
145 if ($page != $whatsnew and !preg_match("/$non_list/",$page))
147 $recent_pages[$page] = get_filetime($page);
152 arsort($recent_pages,SORT_NUMERIC);
154 // create recent.dat (for recent.inc.php)
155 $fp = fopen(CACHE_DIR.'recent.dat','w')
156 or die_message('cannot write cache file '.CACHE_DIR.'recent.dat<br />maybe permission is not writable or filename is too long');
158 foreach ($recent_pages as $page=>$time)
160 fputs($fp,"$time\t$page\n");
165 // create RecentChanges
166 $fp = fopen(get_filename($whatsnew),'w')
167 or die_message('cannot write page file '.htmlspecialchars($whatsnew).'<br />maybe permission is not writable or filename is too long');
169 foreach (array_splice($recent_pages,0,$maxshow) as $page=>$time)
171 $s_lastmod = htmlspecialchars(format_date($time));
172 $s_page = htmlspecialchars($page);
173 fputs($fp, "-$s_lastmod - [[$s_page]]\n");
175 fputs($fp,"#norelated\n"); // :)
182 list($pattern,$forceignorelist) = get_autolink_pattern($pages);
184 $fp = fopen(CACHE_DIR.'autolink.dat','w')
185 or die_message('cannot write autolink file '.CACHE_DIR.'/autolink.dat<br />maybe permission is not writable');
187 fputs($fp,$pattern."\n");
188 fputs($fp,join("\t",$forceignorelist));
194 // »ØÄꤵ¤ì¤¿¥Ú¡¼¥¸¤Î·Ð²á»þ¹ï
195 function get_pg_passage($page,$sw=TRUE)
197 global $show_passage;
198 static $pg_passage = array();
205 if (!array_key_exists($page,$pg_passage))
207 $pg_passage[$page] = (is_page($page) and $time = get_filetime($page)) ?
208 get_passage($time) : '';
211 return $sw ? "<small>{$pg_passage[$page]}</small>" : " {$pg_passage[$page]}";
214 // Last-Modified ¥Ø¥Ã¥À
215 function header_lastmod()
219 if ($lastmod and is_page($page)) {
220 header('Last-Modified: '.date('D, d M Y H:i:s',get_filetime($page)).' GMT');
224 // Á´¥Ú¡¼¥¸Ì¾¤òÇÛÎó¤Ë
225 function get_existpages($dir=DATA_DIR,$ext='.txt')
229 $pattern = '^((?:[0-9A-F]{2})+)';
232 $pattern .= preg_quote($ext,'/').'$';
235 or die_message($dir. ' is not found or not readable.');
236 while ($file = readdir($dp))
238 if (preg_match("/$pattern/",$file,$matches))
240 $aryret[$file] = decode($matches[1]);
246 //¥Õ¥¡¥¤¥ë̾¤Î°ìÍ÷¤òÇÛÎó¤Ë(¥¨¥ó¥³¡¼¥ÉºÑ¤ß¡¢³ÈÄ¥»Ò¤ò»ØÄê)
247 function get_existfiles($dir,$ext)
251 $pattern = '^(?:[0-9A-F]{2})+'.preg_quote($ext,'/').'$';
253 or die_message($dir. ' is not found or not readable.');
254 while ($file = readdir($dp)) {
255 if (preg_match("/$pattern/",$file)) {
256 $aryret[] = $dir.$file;
262 //¤¢¤ë¥Ú¡¼¥¸¤Î´ØÏ¢¥Ú¡¼¥¸¤òÆÀ¤ë
263 function links_get_related($page)
265 global $vars,$related;
266 static $links = array();
268 if (array_key_exists($page,$links))
270 return $links[$page];
273 // ²Äǽ¤Ê¤émake_link()¤ÇÀ¸À®¤·¤¿´ØÏ¢¥Ú¡¼¥¸¤ò¼è¤ê¹þ¤à
274 $links[$page] = ($page == $vars['page']) ? $related : array();
276 // ¥Ç¡¼¥¿¥Ù¡¼¥¹¤«¤é´ØÏ¢¥Ú¡¼¥¸¤òÆÀ¤ë
277 $links[$page] += links_get_related_db($vars['page']);
279 return $links[$page];