OSDN Git Service

PKWK_READONLY prohibits basic_auth
[pukiwiki/pukiwiki.git] / plugin / calendar_viewer.inc.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone
3 // $Id: calendar_viewer.inc.php,v 1.31 2005/01/26 13:09:58 henoheno Exp $
4 //
5 // Calendar viewer plugin - List pages that calendar/calnedar2 plugin created
6 // (Based on calendar and recent plugin)
7
8 define('PLUGIN_CALENDAR_VIEWER_USAGE',
9         '#calendar_viewer(pagename,this|yyyy-mm|n|x*y[,mode[,separater]])');
10 /*
11  ** pagename
12  * - A working root of calendar or calendar2 plugin
13  *   pagename/2004-12-30
14  *   pagename/2004-12-31
15  *   ...
16  *
17  ** (yyyy-mm|n|this)
18  * this    - Show 'this month'
19  * yyyy-mm - Show pages at year:yyyy and month:mm
20  * n       - Show first n pages
21  * x*n     - Show first n pages from x-th page (0 = first)
22  *
23  ** [mode]
24  * past   - Show today, and the past below. Recommended for ChangeLogs and diaries (default)
25  * future - Show today, and the future below. Recommended for event planning and scheduling
26  * view   - Show all, from the past to the future
27  *
28  ** [separater]
29  * - Specify separator of yyyy/mm/dd
30  * - Default: '-' (yyyy-mm-dd)
31  *
32  * TODO
33  *  Stop showing links 'next month' and 'previous month' with past/future mode for 'this month'
34  *    #calendar_viewer(pagename,this,past)
35  */
36
37 function plugin_calendar_viewer_convert()
38 {
39         global $vars, $get, $post, $script;
40         global $_msg_calendar_viewer_right, $_msg_calendar_viewer_left;
41         global $_msg_calendar_viewer_restrict, $_err_calendar_viewer_param2;
42
43         static $viewed = array();
44
45         if (func_num_args() < 2)
46                 return PLUGIN_CALENDAR_VIEWER_USAGE . '<br />' . "\n";
47
48         $func_args = func_get_args();
49
50         // Default values
51         $pagename    = $func_args[0];   // ´ð½à¤È¤Ê¤ë¥Ú¡¼¥¸Ì¾
52         $page_YM     = '';      // °ìÍ÷ɽ¼¨¤¹¤ëǯ·î
53         $limit_base  = 0;       // ÀèƬ¤«¤é¿ô¤¨¤Æ²¿¥Ú¡¼¥¸Ìܤ«¤éɽ¼¨¤¹¤ë¤« (ÀèƬ)
54         $limit_pitch = 0;       // ²¿·ï¤Å¤Äɽ¼¨¤¹¤ë¤«
55         $limit_page  = 0;       // ¥µ¡¼¥Á¤¹¤ë¥Ú¡¼¥¸¿ô
56         $mode        = 'past';  // Æ°ºî¥â¡¼¥É
57         $date_sep    = '-';     // ÆüÉդΥ»¥Ñ¥ì¡¼¥¿ calendar2¤Ê¤é '-', calendar¤Ê¤é ''
58
59         // Check $func_args[1]
60         $matches = array();
61         if (preg_match('/[0-9]{4}' . $date_sep . '[0-9]{2}/', $func_args[1])) {
62                 // »ØÄêǯ·î¤Î°ìÍ÷ɽ¼¨
63                 $page_YM     = $func_args[1];
64                 $limit_page  = 31;
65         } else if (preg_match('/this/si', $func_args[1])) {
66                 // º£·î¤Î°ìÍ÷ɽ¼¨
67                 $page_YM     = get_date('Y' . $date_sep . 'm');
68                 $limit_page  = 31;
69         } else if (preg_match('/^[0-9]+$/', $func_args[1])) {
70                 // nÆüʬɽ¼¨
71                 $limit_pitch = $func_args[1];
72                 $limit_page  = $func_args[1];
73         } else if (preg_match('/(-?[0-9]+)\*([0-9]+)/', $func_args[1], $matches)) {
74                 // ÀèƬ¤è¤ê¿ô¤¨¤Æ x ¥Ú¡¼¥¸Ìܤ«¤é¡¢y·ï¤Å¤Äɽ¼¨
75                 $limit_base  = $matches[1];
76                 $limit_pitch = $matches[2];
77                 $limit_page  = $matches[1] + $matches[2]; // ÆɤßÈô¤Ð¤¹ + É½¼¨¤¹¤ë
78         } else {
79                 return '#calendar_viewer(): ' . $_err_calendar_viewer_param2 . '<br />' . "\n";
80         }
81
82         // $func_args[2]: Mode setting
83         if (isset($func_args[2]) && preg_match('/^(past|view|future)$/si', $func_args[2]))
84                 $mode = $func_args[2];
85
86         // $func_args[3]: Change default delimiter
87         if (isset($func_args[3])) $date_sep = $func_args[3];
88
89         // Avoid Loop etc.
90         if (isset($viewed[$pagename])) {
91                 $s_page = htmlspecialchars($pagename);
92                 return "#calendar_viewer(): You already view: $s_page<br />";
93         } else {
94                 $viewed[$pagename] = TRUE; // Valid
95         }
96
97         // °ìÍ÷ɽ¼¨¤¹¤ë¥Ú¡¼¥¸Ì¾¤È¥Õ¥¡¥¤¥ë̾¤Î¥Ñ¥¿¡¼¥ó¡¡¥Õ¥¡¥¤¥ë̾¤Ë¤Ïǯ·î¤ò´Þ¤à
98         if ($pagename == '') {
99                 // pagename̵¤·¤Îyyyy-mm-dd¤ËÂбþ¤¹¤ë¤¿¤á¤Î½èÍý
100                 $pagepattern     = '';
101                 $pagepattern_len = 0;
102                 $filepattern     = encode($page_YM);
103                 $filepattern_len = strlen($filepattern);
104         } else {
105                 $pagepattern     = strip_bracket($pagename) . '/';
106                 $pagepattern_len = strlen($pagepattern);
107                 $filepattern     = encode($pagepattern . $page_YM);
108                 $filepattern_len = strlen($filepattern);
109         }
110
111         // ¥Ú¡¼¥¸¥ê¥¹¥È¤Î¼èÆÀ
112         $pagelist = array();
113         if ($dir = @opendir(DATA_DIR)) {
114                 $_date = get_date('Y' . $date_sep . 'm' . $date_sep . 'd');
115                 $page_date  = '';
116                 while($file = readdir($dir)) {
117                         if ($file == '..' || $file == '.') continue;
118                         if (substr($file, 0, $filepattern_len) != $filepattern) continue;
119
120                         $page      = decode(trim(preg_replace('/\.txt$/', ' ', $file)));
121                         $page_date = substr($page, $pagepattern_len);
122
123                         // Verify the $page_date pattern (Default: yyyy-mm-dd).
124                         // Past-mode hates the future, and
125                         // Future-mode hates the past.
126                         if ((plugin_calendar_viewer_isValidDate($page_date, $date_sep) == FALSE) || 
127                                 ($page_date > $_date && ($mode == 'past')) ||
128                                 ($page_date < $_date && ($mode == 'future')))
129                                         continue;
130
131                         $pagelist[] = $page;
132                 }
133         }
134         closedir($dir);
135
136         if ($mode == 'past') {
137                 rsort($pagelist);       // New => Old
138         } else {
139                 sort($pagelist);        // Old => New
140         }
141
142         // Include start
143         $tmppage     = $vars['page'];
144         $return_body = '';
145
146         // $limit_page ¤Î·ï¿ô¤Þ¤Ç¥¤¥ó¥¯¥ë¡¼¥É
147         $tmp = max($limit_base, 0); // Skip minus
148         while ($tmp < $limit_page) {
149                 if (! isset($pagelist[$tmp])) break;
150
151                 $page = $pagelist[$tmp];
152                 $get['page'] = $post['page'] = $vars['page'] = $page;
153
154                 // ¸½¾õ¤Ç±ÜÍ÷µö²Ä¤¬¤¢¤ë¾ì¹ç¤À¤±É½¼¨¤¹¤ë
155                 if (check_readable($page, FALSE, FALSE)) {
156                         $body = convert_html(get_source($page));
157                 } else {
158                         $body = str_replace('$1', $page, $_msg_calendar_viewer_restrict);
159                 }
160
161                 $r_page = rawurlencode($page);
162                 $s_page = htmlspecialchars($page);
163
164                 if (PKWK_READONLY) {
165                         $link   = $script . '?' . $r_page;
166                 } else {
167                         $link   = $script . '?cmd=edit&amp;page=' . $r_page;
168                 }
169                 $link   = '<a href="' . $link . '">' . $s_page . '</a>';
170
171                 $head   = '<h1>' . $link . '</h1>' . "\n";
172                 $return_body .= $head . $body;
173
174                 ++$tmp;
175         }
176
177         // ¤³¤³¤Ç¡¢Á°¸å¤Î¥ê¥ó¥¯¤òɽ¼¨
178         // ?plugin=calendar_viewer&file=¥Ú¡¼¥¸Ì¾&date=yyyy-mm
179         $enc_pagename = rawurlencode(substr($pagepattern, 0, $pagepattern_len - 1));
180
181         if ($page_YM != '') {
182                 // Ç¯·îɽ¼¨»þ
183                 $date_sep_len = strlen($date_sep);
184                 $this_year    = substr($page_YM, 0, 4);
185                 $this_month   = substr($page_YM, 4 + $date_sep_len, 2);
186
187                 // ¼¡·î
188                 $next_year  = $this_year;
189                 $next_month = $this_month + 1;
190                 if ($next_month > 12) {
191                         ++$next_year;
192                         $next_month = 1;
193                 }
194                 $next_YM = sprintf('%04d%s%02d', $next_year, $date_sep, $next_month);
195
196                 // Á°·î
197                 $prev_year  = $this_year;
198                 $prev_month = $this_month - 1;
199                 if ($prev_month < 1) {
200                         --$prev_year;
201                         $prev_month = 12;
202                 }
203                 $prev_YM = sprintf('%04d%s%02d', $prev_year, $date_sep, $prev_month);
204                 if ($mode == 'past') {
205                         $right_YM   = $prev_YM;
206                         $right_text = $prev_YM . '&gt;&gt;'; // >>
207                         $left_YM    = $next_YM;
208                         $left_text  = '&lt;&lt;' . $next_YM; // <<
209                 } else {
210                         $left_YM    = $prev_YM;
211                         $left_text  = '&lt;&lt;' . $prev_YM; // <<
212                         $right_YM   = $next_YM;
213                         $right_text = $next_YM . '&gt;&gt;'; // >>
214                 }
215         } else {
216                 // n·ïɽ¼¨»þ
217                 if ($limit_base <= 0) {
218                         $left_YM = ''; // É½¼¨¤·¤Ê¤¤ (¤½¤ì¤è¤êÁ°¤Î¹àÌܤϤʤ¤)
219                 } else {
220                         $left_YM   = $limit_base - $limit_pitch . '*' . $limit_pitch;
221                         $left_text = sprintf($_msg_calendar_viewer_left, $limit_pitch);
222
223                 }
224                 if ($limit_base + $limit_pitch >= count($pagelist)) {
225                         $right_YM = ''; // É½¼¨¤·¤Ê¤¤ (¤½¤ì¤è¤ê¸å¤Î¹àÌܤϤʤ¤)
226                 } else {
227                         $right_YM   = $limit_base + $limit_pitch . '*' . $limit_pitch;
228                         $right_text = sprintf($_msg_calendar_viewer_right, $limit_pitch);
229                 }
230         }
231
232         // ¥Ê¥Ó¥²¡¼¥ÈÍѤΥê¥ó¥¯¤òËöÈø¤ËÄɲÃ
233         if ($left_YM != '' || $right_YM != '') {
234                 $s_date_sep = htmlspecialchars($date_sep);
235                 $left_link = $right_link = '';
236                 $link = $script . '?plugin=calendar_viewer&amp;mode=' . $mode .
237                         '&amp;file=' . $enc_pagename . '&amp;date_sep=' . $s_date_sep . '&amp;';
238                 if ($left_YM != '')
239                         $left_link = '<a href="' . $link .
240                                 'date=' . $left_YM . '">' . $left_text . '</a>';
241                 if ($right_YM != '')
242                         $right_link = '<a href="' . $link .
243                                 'date=' . $right_YM . '">' . $right_text . '</a>';
244                 // past mode¤Ï<<¿· µì>> Â¾¤Ï<<µì ¿·>>
245                 $return_body .=
246                         '<div class="calendar_viewer">' .
247                         '<span class="calendar_viewer_left">'  . $left_link  . '</span>' .
248                         '<span class="calendar_viewer_right">' . $right_link . '</span>' .
249                         '</div>';
250         }
251
252         $get['page'] = $post['page'] = $vars['page'] = $tmppage;
253
254         return $return_body;
255 }
256
257 function plugin_calendar_viewer_action()
258 {
259         global $vars, $get, $post, $script;
260
261         $date_sep = '-';
262
263         $return_vars_array = array();
264
265         $page = strip_bracket($vars['page']);
266         $vars['page'] = '*';
267         if (isset($vars['file'])) $vars['page'] = $vars['file'];
268
269         $date_sep = $vars['date_sep'];
270
271         $page_YM = $vars['date'];
272         if ($page_YM == '') $page_YM = get_date('Y' . $date_sep . 'm');
273         $mode = $vars['mode'];
274
275         $args_array = array($vars['page'], $page_YM, $mode, $date_sep);
276         $return_vars_array['body'] = call_user_func_array('plugin_calendar_viewer_convert', $args_array);
277
278         //$return_vars_array['msg'] = 'calendar_viewer ' . $vars['page'] . '/' . $page_YM;
279         $return_vars_array['msg'] = 'calendar_viewer ' . htmlspecialchars($vars['page']);
280         if ($vars['page'] != '') $return_vars_array['msg'] .= '/';
281         if (preg_match('/\*/', $page_YM)) {
282                 // ¤¦¡¼¤ó¡¢n·ïɽ¼¨¤Î»þ¤Ï¤Ê¤ó¤Æ¥Ú¡¼¥¸Ì¾¤Ë¤·¤¿¤é¤¤¤¤¡©
283         } else {
284                 $return_vars_array['msg'] .= htmlspecialchars($page_YM);
285         }
286
287         $vars['page'] = $page;
288         return $return_vars_array;
289 }
290
291 function plugin_calendar_viewer_isValidDate($aStr, $aSepList = '-/ .')
292 {
293         $matches = array();
294         if ($aSepList == '') {
295                 // yyymmdd¤È¤·¤Æ¥Á¥§¥Ã¥¯¡Ê¼êÈ´¤­(^^;¡Ë
296                 return checkdate(substr($aStr, 4, 2), substr($aStr, 6, 2), substr($aStr, 0, 4));
297         } else if (ereg("^([0-9]{2,4})[$aSepList]([0-9]{1,2})[$aSepList]([0-9]{1,2})$", $aStr, $matches) ) {
298                 return checkdate($matches[2], $matches[3], $matches[1]);
299         } else {
300                 return FALSE;
301         }
302 }
303
304 ?>