2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
5 // $Id: file.php,v 1.37 2003/11/05 10:58:39 arino Exp $
9 function get_source($page=NULL)
15 return str_replace("\r",'',file(get_filename($page)));
18 // ¥Ú¡¼¥¸¤Î¹¹¿·»þ¹ï¤òÆÀ¤ë
19 function get_filetime($page)
25 return filemtime(get_filename($page)) - LOCALZONE;
28 // ¥Ú¡¼¥¸¤Î¥Õ¥¡¥¤¥ë̾¤òÆÀ¤ë
29 function get_filename($page)
31 return DATA_DIR.encode($page).'.txt';
35 function page_write($page,$postdata,$notimestamp=FALSE)
37 $postdata = make_str_rules($postdata);
40 $oldpostdata = is_page($page) ? join('',get_source($page)) : '';
41 $diffdata = do_diff($oldpostdata,$postdata);
42 file_write(DIFF_DIR,$page,$diffdata);
45 make_backup($page,$postdata == '');
48 file_write(DATA_DIR,$page,$postdata,$notimestamp);
50 // TrackBack Ping ¤ÎÁ÷¿®
52 $lines = join("\n",preg_replace('/^\+/','',preg_grep('/^\+/',explode("\n",$diffdata))));
53 tb_send($page,$lines);
55 // link¥Ç¡¼¥¿¥Ù¡¼¥¹¤ò¹¹¿·
59 // ¥æ¡¼¥¶ÄêµÁ¥ë¡¼¥ë(¥½¡¼¥¹¤òÃÖ´¹¤¹¤ë)
60 function make_str_rules($str)
62 global $str_rules,$fixed_heading_anchor;
64 $arr = explode("\n",$str);
67 foreach ($arr as $str)
69 if ($str != '' and $str{0} != ' ' and $str{0} != "\t")
71 foreach ($str_rules as $rule => $replace)
73 $str = preg_replace("/$rule/",$replace,$str);
76 // ¸«½Ð¤·¤Ë¸ÇÍID¤òÉÕÍ¿¤¹¤ë
77 if ($fixed_heading_anchor and
78 preg_match('/^(\*{1,3}(.(?!\[#[A-Za-z][\w-]+\]))+)$/',$str,$matches))
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]";
89 return join("\n",$retvars);
93 function file_write($dir,$page,$str,$notimestamp=FALSE)
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;
100 if (!is_pagename($page))
103 str_replace('$1',htmlspecialchars($page),
104 str_replace('$2','WikiName',$_msg_invalidiwn)
108 $page = strip_bracket($page);
110 $file = $dir.encode($page).'.txt';
112 if ($dir == DATA_DIR and $str == '' and file_exists($file))
118 $str = preg_replace("/\r/",'',$str);
119 $str = rtrim($str)."\n";
121 if ($notimestamp and file_exists($file))
123 $timestamp = filemtime($file) - LOCALZONE;
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');
134 touch($file,$timestamp + LOCALZONE);
138 // is_page¤Î¥¥ã¥Ã¥·¥å¤ò¥¯¥ê¥¢¤¹¤ë¡£
141 if (!$timestamp and $dir == DATA_DIR)
146 if ($update_exec and $dir == DATA_DIR)
148 system($update_exec.' > /dev/null &');
151 if ($notify and $dir == DIFF_DIR)
153 if ($notify_diff_only)
155 // º¹Ê¬¤À¤±¤òÁ÷¿®¤¹¤ë
156 $str = preg_replace('/^[^-+].*\n/m','',$str);
162 $subject = str_replace('$page',$page,$notify_subject);
163 ini_set('SMTP',$smtp_server);
165 mb_send_mail($notify_to,$subject,$str,$notify_header);
169 // ºÇ½ª¹¹¿·¥Ú¡¼¥¸¤Î¹¹¿·
170 function put_lastmodified()
172 global $maxshow,$whatsnew,$non_list,$autolink;
174 $pages = get_existpages();
175 $recent_pages = array();
176 foreach($pages as $page)
178 if ($page != $whatsnew and !preg_match("/$non_list/",$page))
180 $recent_pages[$page] = get_filetime($page);
185 arsort($recent_pages,SORT_NUMERIC);
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');
191 foreach ($recent_pages as $page=>$time)
193 fputs($fp,"$time\t$page\n");
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');
202 foreach (array_splice($recent_pages,0,$maxshow) as $page=>$time)
204 $s_lastmod = htmlspecialchars(format_date($time));
205 $s_page = htmlspecialchars($page);
206 fputs($fp, "-$s_lastmod - [[$s_page]]\n");
208 fputs($fp,"#norelated\n"); // :)
215 list($pattern,$pattern_a,$forceignorelist) = get_autolink_pattern($pages);
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');
220 fputs($fp,$pattern."\n");
221 fputs($fp,$pattern_a."\n");
222 fputs($fp,join("\t",$forceignorelist)."\n");
228 // »ØÄꤵ¤ì¤¿¥Ú¡¼¥¸¤Î·Ð²á»þ¹ï
229 function get_pg_passage($page,$sw=TRUE)
231 global $show_passage;
232 static $pg_passage = array();
239 if (!array_key_exists($page,$pg_passage))
241 $pg_passage[$page] = (is_page($page) and $time = get_filetime($page)) ?
242 get_passage($time) : '';
245 return $sw ? "<small>{$pg_passage[$page]}</small>" : " {$pg_passage[$page]}";
248 // Last-Modified ¥Ø¥Ã¥À
249 function header_lastmod($page=NULL)
253 if ($lastmod and is_page($page))
255 header('Last-Modified: '.date('D, d M Y H:i:s',get_filetime($page)).' GMT');
259 // Á´¥Ú¡¼¥¸Ì¾¤òÇÛÎó¤Ë
260 function get_existpages($dir=DATA_DIR,$ext='.txt')
264 $pattern = '^((?:[0-9A-F]{2})+)';
267 $pattern .= preg_quote($ext,'/').'$';
270 or die_message($dir. ' is not found or not readable.');
271 while ($file = readdir($dp))
273 if (preg_match("/$pattern/",$file,$matches))
275 $aryret[$file] = decode($matches[1]);
281 // ¥Ú¡¼¥¸Ì¾¤ÎÆɤߤòÇÛÎó¤Ë
282 function get_readings()
284 global $pagereading_enable, $pagereading_kanji2kana_converter;
285 global $pagereading_kanji2kana_encoding, $pagereading_chasen_path;
286 global $pagereading_kakasi_path, $pagereading_config_page;
288 $pages = get_existpages();
291 foreach ($pages as $page) {
292 $readings[$page] = '';
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];
301 if($pagereading_enable) {
302 // ChaSen/KAKASI ¸Æ¤Ó½Ð¤·¤¬Í¸ú¤ËÀßÄꤵ¤ì¤Æ¤¤¤ë¾ì¹ç
303 $unknownPage = FALSE;
304 // Æɤߤ¬ÉÔÌÀ¤Î¥Ú¡¼¥¸¤¬¤¢¤ë¤«¥Á¥§¥Ã¥¯
305 foreach ($readings as $page => $reading) {
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) {
319 fputs($fp, mb_convert_encoding("$page\n", $pagereading_kanji2kana_encoding, SOURCE_ENCODING));
323 // ChaSen/KAKASI ¤ò¼Â¹Ô
324 switch(strtolower($pagereading_kanji2kana_converter)) {
326 if(!file_exists($pagereading_chasen_path)) {
328 die_message("CHASEN not found: $pagereading_chasen_path");
330 $fp = popen("$pagereading_chasen_path -F %y $tmpfname", "r");
333 die_message("ChaSen execution failed: $pagereading_chasen_path -F %y $tmpfname");
338 if(!file_exists($pagereading_kakasi_path)) {
340 die_message("KAKASI not found: $pagereading_kakasi_path");
342 $fp = popen("$pagereading_kakasi_path -kK -HK -JK <$tmpfname", "r");
345 die_message("KAKASI execution failed: $pagereading_kakasi_path -kK -HK -JK <$tmpfname");
349 die_message("unknown kanji-kana converter: $pagereading_kanji2kana_converter.");
352 foreach ($readings as $page => $reading) {
355 $line = mb_convert_encoding($line, SOURCE_ENCODING, $pagereading_kanji2kana_encoding);
356 $line = preg_replace('/[\s\r\n]+$/', '', $line);
357 $readings[$page] = $line;
361 unlink($tmpfname) or die_message("temporary file can not be removed: $tmpfname");
367 foreach ($readings as $page => $reading) {
368 $body .= "-[[$page]] $reading\n";
370 page_write($pagereading_config_page, $body);
374 // ÆɤßÉÔÌÀ¤Î¥Ú¡¼¥¸¤Ï¡¢¤½¤Î¤Þ¤Þ¥Ú¡¼¥¸Ì¾¤òÊÖ¤¹ (ChaSen/KAKASI ¸Æ
375 // ¤Ó½Ð¤·¤¬Ìµ¸ú¤ËÀßÄꤵ¤ì¤Æ¤¤¤ë¾ì¹ç¤ä¡¢ChaSen/KAKASI ¸Æ¤Ó½Ð¤·¤Ë
377 foreach ($pages as $page) {
378 if($readings[$page]=='') {
379 $readings[$page] = $page;
385 //¥Õ¥¡¥¤¥ë̾¤Î°ìÍ÷¤òÇÛÎó¤Ë(¥¨¥ó¥³¡¼¥ÉºÑ¤ß¡¢³ÈÄ¥»Ò¤ò»ØÄê)
386 function get_existfiles($dir,$ext)
390 $pattern = '^(?:[0-9A-F]{2})+'.preg_quote($ext,'/').'$';
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;
401 //¤¢¤ë¥Ú¡¼¥¸¤Î´ØÏ¢¥Ú¡¼¥¸¤òÆÀ¤ë
402 function links_get_related($page)
404 global $vars,$related;
405 static $links = array();
407 if (array_key_exists($page,$links))
409 return $links[$page];
412 // ²Äǽ¤Ê¤émake_link()¤ÇÀ¸À®¤·¤¿´ØÏ¢¥Ú¡¼¥¸¤ò¼è¤ê¹þ¤à
413 $links[$page] = ($page == $vars['page']) ? $related : array();
415 // ¥Ç¡¼¥¿¥Ù¡¼¥¹¤«¤é´ØÏ¢¥Ú¡¼¥¸¤òÆÀ¤ë
416 $links[$page] += links_get_related_db($vars['page']);
418 return $links[$page];