OSDN Git Service

9190f0474638cf5fbf2d961445401b87d9437ff6
[pukiwiki/pukiwiki.git] / file.php
1 <?php
2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
4 //
5 // $Id: file.php,v 1.4 2003/01/27 05:38:41 panda Exp $
6 //
7
8 // ¥½¡¼¥¹¤ò¼èÆÀ
9 function get_source($page)
10 {
11         return is_page($page) ?
12                 crlf_rtrim(file(get_filename($page))) : array();
13 }
14
15 // ¥Ú¡¼¥¸¤Î¹¹¿·»þ¹ï¤òÆÀ¤ë
16 function get_filetime($page)
17 {
18         return filemtime(get_filename($page)) - LOCALZONE;
19 }
20
21 // ¥Ú¡¼¥¸¤Î¥Õ¥¡¥¤¥ë̾¤òÆÀ¤ë
22 function get_filename($page)
23 {
24         return DATA_DIR.encode($page).'.txt';
25 }
26
27 // ¥Ú¡¼¥¸¤Î½ÐÎÏ
28 function page_write($page,$postdata)
29 {
30         $postdata = user_rules_str($postdata);
31         
32         // º¹Ê¬¥Õ¥¡¥¤¥ë¤ÎºîÀ®
33         $oldpostdata = is_page($page) ? join('',get_source($page)) : "\n";
34         $diffdata = do_diff($oldpostdata,$postdata);
35         file_write(DIFF_DIR,$page,$diffdata);
36         
37         // ¥Ð¥Ã¥¯¥¢¥Ã¥×¤ÎºîÀ®
38         make_backup($page,$postdata == '');
39         
40         // ¥Õ¥¡¥¤¥ë¤Î½ñ¤­¹þ¤ß
41         file_write(DATA_DIR,$page,$postdata);
42         
43         // is_page¤Î¥­¥ã¥Ã¥·¥å¤ò¥¯¥ê¥¢¤¹¤ë¡£
44         is_page($page,true);
45         
46         // link¥Ç¡¼¥¿¥Ù¡¼¥¹¤ò¹¹¿·
47         links_update($page);
48 }
49
50 // ¥Õ¥¡¥¤¥ë¤Ø¤Î½ÐÎÏ
51 function file_write($dir,$page,$str)
52 {
53         global $post,$update_exec;
54         
55         $page = strip_bracket($page);
56         $timestamp = FALSE;
57         $file = $dir.encode($page).'.txt';
58         
59         if ($dir == DATA_DIR and $str == '' and file_exists($file)) {
60                 unlink($file);
61         }
62         else {
63                 $str = preg_replace("/\r/",'',$str);
64                 $str = rtrim($str)."\n";
65                 
66                 if (!empty($post['notimestamp']) and file_exists($file)) {
67                         $timestamp = filemtime($file) - LOCALZONE;
68                 }
69                 
70                 $fp = fopen($file,'w')
71                         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');
72                 flock($fp,LOCK_EX);
73                 fputs($fp,$str);
74                 flock($fp,LOCK_UN);
75                 fclose($fp);
76                 if ($timestamp) {
77                         touch($file,$timestamp + LOCALZONE);
78                 }
79         }
80         
81         if (!$timestamp) {
82                 put_lastmodified();
83         }
84         
85         if ($update_exec and $dir == DATA_DIR) {
86                 system($update_exec.' > /dev/null &');
87         }
88 }
89
90 // ºÇ½ª¹¹¿·¥Ú¡¼¥¸¤Î¹¹¿·
91 function put_lastmodified()
92 {
93         global $script,$post,$maxshow,$whatsnew,$non_list;
94
95         $pages = array();
96         foreach(get_existpages() as $page) {
97                 if ($page == $whatsnew or preg_match("/$non_list/",$page)) {
98                         continue;
99                 }
100                 
101                 $time = get_filetime($page);
102                 $s_page = htmlspecialchars($page);
103                 $pages[$s_page] = $time;
104         }
105         
106         arsort($pages); //»þ¹ï¹ß½ç¤Ç¥½¡¼¥È
107         
108         $fp = fopen(get_filename($whatsnew),'w')
109                 or die_message('cannot write page file '.htmlspecialchars($whatsnew).'<br>maybe permission is not writable or filename is too long');
110         
111         flock($fp,LOCK_EX);
112         
113         foreach($pages as $s_page => $time) {
114                 fputs($fp, "//$time $s_page\n");
115         }
116         
117         $pages = array_splice($pages,0,$maxshow);
118         
119         foreach($pages as $s_page => $time) {
120                 $lastmod = format_date($time);
121                 fputs($fp, "-$lastmod - [[$s_page]]\n");
122         }
123         
124         fputs($fp,"#norelated\n"); // :)
125         flock($fp,LOCK_UN);
126         fclose($fp);
127 }
128
129 // »ØÄꤵ¤ì¤¿¥Ú¡¼¥¸¤Î·Ð²á»þ¹ï
130 function get_pg_passage($page,$sw=TRUE)
131 {
132         global $show_passage;
133         static $pg_passage;
134         
135         if (!$show_passage) {
136                 return '';
137         }
138         
139         if (!isset($pg_passage)) {
140                 $pg_passage = array();
141         }
142         
143         if (!array_key_exists($page,$pg_passage)) {
144                 $pg_passage[$page] = (is_page($page) and $time = get_filetime($page)) ? get_passage($time) : '';
145         }
146         
147         return $sw ? "<small>{$pg_passage[$page]}</small>" : $pg_passage[$page];
148 }
149
150 // Last-Modified ¥Ø¥Ã¥À
151 function header_lastmod()
152 {
153         global $lastmod;
154         
155         if ($lastmod and is_page($page)) {
156                 header('Last-Modified: '.date('D, d M Y H:i:s',get_filetime($page)).' GMT');
157         }
158 }
159
160 // Á´¥Ú¡¼¥¸Ì¾¤òÇÛÎó¤Ë
161 function get_existpages($dir = DATA_DIR)
162 {
163         $aryret = array();
164         
165         $dir = @opendir($dir) or die();
166         
167         while ($file = readdir($dir)) {
168                 if (preg_match('/^([0-9A-F]+)/',$file,$matches)) {
169                         $aryret[] = decode($matches[1]);
170                 }
171         }
172         
173         closedir($dir);
174         
175         return $aryret;
176 }
177
178 function links_update($page)
179 {
180         global $vars;
181
182         // link¥Ç¡¼¥¿¥Ù¡¼¥¹¤ò¹¹¿·
183         if (defined('LINK_DB') and exist_plugin_action('links')) {
184                 // ¤Á¤ç¤Ã¤È¸È©
185                 $tmp = $vars['page'];
186                 $vars['page'] = $page;
187                 do_plugin_action('links');
188                 $vars['page'] = $tmp;
189         }
190 }
191 //¤¢¤ë¥Ú¡¼¥¸¤Î´ØÏ¢¥Ú¡¼¥¸¤òÆÀ¤ë
192 function links_get_related($page)
193 {
194         global $vars,$related;
195         static $links;
196         
197         if (!isset($links)) {
198                 $links = array();
199         }
200         
201         if (array_key_exists($page,$links)) {
202                 return $links[$page];
203         }
204         
205         // ²Äǽ¤Ê¤émake_link()¤ÇÀ¸À®¤·¤¿´ØÏ¢¥Ú¡¼¥¸¤ò¼è¤ê¹þ¤à
206         $links[$page] = ($page == $vars['page']) ? $related : array();
207         
208         $a_page = addslashes($page);
209         
210         if (defined('LINK_DB')) {
211                 $sql = <<<EOD
212 SELECT refpage.name,refpage.lastmod FROM page left join link on page.id = page_id left join page as refpage on ref_id = refpage.id where page.name = '$a_page'
213 UNION
214 SELECT DISTINCT refpage.name,refpage.lastmod FROM page left join link on page.id = ref_id left join page as refpage on page_id = refpage.id where page.name = '$a_page';
215 EOD;
216                 $rows = db_query($sql);
217                 
218                 foreach ($rows as $row) {
219                         if (empty($row['name']) or substr($row['name'],0,1) == ':') {
220                                 continue;
221                         }
222                         $links[$page][$row['name']] = $row['lastmod'];
223                 }
224         }
225         else {
226                 $links[$page] = array_merge($links[$page],do_search($page,'OR',1));
227         }
228         
229         return $links[$page];
230 }
231 ?>