comparison forum/Sources/Calendar.php @ 76:e3e11437ecea website

Add forum code
author Chris Cannam
date Sun, 07 Jul 2013 11:25:48 +0200
parents
children
comparison
equal deleted inserted replaced
75:72f59aa7e503 76:e3e11437ecea
1 <?php
2
3 /**
4 * Simple Machines Forum (SMF)
5 *
6 * @package SMF
7 * @author Simple Machines http://www.simplemachines.org
8 * @copyright 2011 Simple Machines
9 * @license http://www.simplemachines.org/about/smf/license.php BSD
10 *
11 * @version 2.0
12 */
13
14 // Original module by Aaron O'Neil - aaron@mud-master.com
15
16 if (!defined('SMF'))
17 die('Hacking attempt...');
18
19 /* This file has only one real task... showing the calendar. Posting is done
20 in Post.php - this just has the following functions:
21
22 void CalendarMain()
23 - loads the specified month's events, holidays, and birthdays.
24 - requires the calendar_view permission.
25 - depends on the cal_enabled setting, and many of the other cal_
26 settings.
27 - uses the calendar_start_day theme option. (Monday/Sunday)
28 - uses the main sub template in the Calendar template.
29 - goes to the month and year passed in 'month' and 'year' by
30 get or post.
31 - accessed through ?action=calendar.
32
33 void CalendarPost()
34 - processes posting/editing/deleting a calendar event.
35 - calls Post() function if event is linked to a post.
36 - calls insertEvent() to insert the event if not linked to post.
37 - requires the calendar_post permission to use.
38 - uses the event_post sub template in the Calendar template.
39 - is accessed with ?action=calendar;sa=post.
40
41 void iCalDownload()
42 - offers up a download of an event in iCal 2.0 format.
43 */
44
45 // Show the calendar.
46 function CalendarMain()
47 {
48 global $txt, $context, $modSettings, $scripturl, $options, $sourcedir;
49
50 // Permissions, permissions, permissions.
51 isAllowedTo('calendar_view');
52
53 // Doing something other than calendar viewing?
54 $subActions = array(
55 'ical' => 'iCalDownload',
56 'post' => 'CalendarPost',
57 );
58
59 if (isset($_GET['sa']) && isset($subActions[$_GET['sa']]) && !WIRELESS)
60 return $subActions[$_GET['sa']]();
61
62 // This is gonna be needed...
63 loadTemplate('Calendar');
64
65 // You can't do anything if the calendar is off.
66 if (empty($modSettings['cal_enabled']))
67 fatal_lang_error('calendar_off', false);
68
69 // Set the page title to mention the calendar ;).
70 $context['page_title'] = $txt['calendar'];
71
72 // Is this a week view?
73 $context['view_week'] = isset($_GET['viewweek']);
74
75 // Don't let search engines index weekly calendar pages.
76 if ($context['view_week'])
77 $context['robot_no_index'] = true;
78
79 // Get the current day of month...
80 require_once($sourcedir . '/Subs-Calendar.php');
81 $today = getTodayInfo();
82
83 // If the month and year are not passed in, use today's date as a starting point.
84 $curPage = array(
85 'day' => isset($_REQUEST['day']) ? (int) $_REQUEST['day'] : $today['day'],
86 'month' => isset($_REQUEST['month']) ? (int) $_REQUEST['month'] : $today['month'],
87 'year' => isset($_REQUEST['year']) ? (int) $_REQUEST['year'] : $today['year']
88 );
89
90 // Make sure the year and month are in valid ranges.
91 if ($curPage['month'] < 1 || $curPage['month'] > 12)
92 fatal_lang_error('invalid_month', false);
93 if ($curPage['year'] < $modSettings['cal_minyear'] || $curPage['year'] > $modSettings['cal_maxyear'])
94 fatal_lang_error('invalid_year', false);
95 // If we have a day clean that too.
96 if ($context['view_week'])
97 {
98 // Note $isValid is -1 < PHP 5.1
99 $isValid = mktime(0, 0, 0, $curPage['month'], $curPage['day'], $curPage['year']);
100 if ($curPage['day'] > 31 || !$isValid || $isValid == -1)
101 fatal_lang_error('invalid_day', false);
102 }
103
104 // Load all the context information needed to show the calendar grid.
105 $calendarOptions = array(
106 'start_day' => !empty($options['calendar_start_day']) ? $options['calendar_start_day'] : 0,
107 'show_birthdays' => in_array($modSettings['cal_showbdays'], array(1, 2)),
108 'show_events' => in_array($modSettings['cal_showevents'], array(1, 2)),
109 'show_holidays' => in_array($modSettings['cal_showholidays'], array(1, 2)),
110 'show_week_num' => true,
111 'short_day_titles' => false,
112 'show_next_prev' => true,
113 'show_week_links' => true,
114 'size' => 'large',
115 );
116
117 // Load up the main view.
118 if ($context['view_week'])
119 $context['calendar_grid_main'] = getCalendarWeek($curPage['month'], $curPage['year'], $curPage['day'], $calendarOptions);
120 else
121 $context['calendar_grid_main'] = getCalendarGrid($curPage['month'], $curPage['year'], $calendarOptions);
122
123 // Load up the previous and next months.
124 $calendarOptions['show_birthdays'] = $calendarOptions['show_events'] = $calendarOptions['show_holidays'] = false;
125 $calendarOptions['short_day_titles'] = true;
126 $calendarOptions['show_next_prev'] = false;
127 $calendarOptions['show_week_links'] = false;
128 $calendarOptions['size'] = 'small';
129 $context['calendar_grid_current'] = getCalendarGrid($curPage['month'], $curPage['year'], $calendarOptions);
130 // Only show previous month if it isn't pre-January of the min-year
131 if ($context['calendar_grid_current']['previous_calendar']['year'] > $modSettings['cal_minyear'] || $curPage['month'] != 1)
132 $context['calendar_grid_prev'] = getCalendarGrid($context['calendar_grid_current']['previous_calendar']['month'], $context['calendar_grid_current']['previous_calendar']['year'], $calendarOptions);
133 // Only show next month if it isn't post-December of the max-year
134 if ($context['calendar_grid_current']['next_calendar']['year'] < $modSettings['cal_maxyear'] || $curPage['month'] != 12)
135 $context['calendar_grid_next'] = getCalendarGrid($context['calendar_grid_current']['next_calendar']['month'], $context['calendar_grid_current']['next_calendar']['year'], $calendarOptions);
136
137 // Basic template stuff.
138 $context['can_post'] = allowedTo('calendar_post');
139 $context['current_day'] = $curPage['day'];
140 $context['current_month'] = $curPage['month'];
141 $context['current_year'] = $curPage['year'];
142 $context['show_all_birthdays'] = isset($_GET['showbd']);
143
144 // Set the page title to mention the month or week, too
145 $context['page_title'] .= ' - ' . ($context['view_week'] ? sprintf($txt['calendar_week_title'], $context['calendar_grid_main']['week_number'], ($context['calendar_grid_main']['week_number'] == 53 ? $context['current_year'] - 1 : $context['current_year'])) : $txt['months'][$context['current_month']] . ' ' . $context['current_year']);
146
147 // Load up the linktree!
148 $context['linktree'][] = array(
149 'url' => $scripturl . '?action=calendar',
150 'name' => $txt['calendar']
151 );
152 // Add the current month to the linktree.
153 $context['linktree'][] = array(
154 'url' => $scripturl . '?action=calendar;year=' . $context['current_year'] . ';month=' . $context['current_month'],
155 'name' => $txt['months'][$context['current_month']] . ' ' . $context['current_year']
156 );
157 // If applicable, add the current week to the linktree.
158 if ($context['view_week'])
159 $context['linktree'][] = array(
160 'url' => $scripturl . '?action=calendar;viewweek;year=' . $context['current_year'] . ';month=' . $context['current_month'] . ';day=' . $context['current_day'],
161 'name' => $txt['calendar_week'] . ' ' . $context['calendar_grid_main']['week_number']
162 );
163 }
164
165 function CalendarPost()
166 {
167 global $context, $txt, $user_info, $sourcedir, $scripturl;
168 global $modSettings, $topic, $smcFunc;
169
170 // Well - can they?
171 isAllowedTo('calendar_post');
172
173 // We need this for all kinds of useful functions.
174 require_once($sourcedir . '/Subs-Calendar.php');
175
176 // Cast this for safety...
177 if (isset($_REQUEST['eventid']))
178 $_REQUEST['eventid'] = (int) $_REQUEST['eventid'];
179
180 // Submitting?
181 if (isset($_POST[$context['session_var']], $_REQUEST['eventid']))
182 {
183 checkSession();
184
185 // Validate the post...
186 if (!isset($_POST['link_to_board']))
187 validateEventPost();
188
189 // If you're not allowed to edit any events, you have to be the poster.
190 if ($_REQUEST['eventid'] > 0 && !allowedTo('calendar_edit_any'))
191 isAllowedTo('calendar_edit_' . (!empty($user_info['id']) && getEventPoster($_REQUEST['eventid']) == $user_info['id'] ? 'own' : 'any'));
192
193 // New - and directing?
194 if ($_REQUEST['eventid'] == -1 && isset($_POST['link_to_board']))
195 {
196 $_REQUEST['calendar'] = 1;
197 require_once($sourcedir . '/Post.php');
198 return Post();
199 }
200 // New...
201 elseif ($_REQUEST['eventid'] == -1)
202 {
203 $eventOptions = array(
204 'board' => 0,
205 'topic' => 0,
206 'title' => substr($_REQUEST['evtitle'], 0, 60),
207 'member' => $user_info['id'],
208 'start_date' => sprintf('%04d-%02d-%02d', $_POST['year'], $_POST['month'], $_POST['day']),
209 'span' => isset($_POST['span']) && $_POST['span'] > 0 ? min((int) $modSettings['cal_maxspan'], (int) $_POST['span'] - 1) : 0,
210 );
211 insertEvent($eventOptions);
212 }
213
214 // Deleting...
215 elseif (isset($_REQUEST['deleteevent']))
216 removeEvent($_REQUEST['eventid']);
217
218 // ... or just update it?
219 else
220 {
221 $eventOptions = array(
222 'title' => substr($_REQUEST['evtitle'], 0, 60),
223 'span' => empty($modSettings['cal_allowspan']) || empty($_POST['span']) || $_POST['span'] == 1 || empty($modSettings['cal_maxspan']) || $_POST['span'] > $modSettings['cal_maxspan'] ? 0 : min((int) $modSettings['cal_maxspan'], (int) $_POST['span'] - 1),
224 'start_date' => strftime('%Y-%m-%d', mktime(0, 0, 0, (int) $_REQUEST['month'], (int) $_REQUEST['day'], (int) $_REQUEST['year'])),
225 );
226
227 modifyEvent($_REQUEST['eventid'], $eventOptions);
228 }
229
230 updateSettings(array(
231 'calendar_updated' => time(),
232 ));
233
234 // No point hanging around here now...
235 redirectexit($scripturl . '?action=calendar;month=' . $_POST['month'] . ';year=' . $_POST['year']);
236 }
237
238 // If we are not enabled... we are not enabled.
239 if (empty($modSettings['cal_allow_unlinked']) && empty($_REQUEST['eventid']))
240 {
241 $_REQUEST['calendar'] = 1;
242 require_once($sourcedir . '/Post.php');
243 return Post();
244 }
245
246 // New?
247 if (!isset($_REQUEST['eventid']))
248 {
249 $today = getdate();
250
251 $context['event'] = array(
252 'boards' => array(),
253 'board' => 0,
254 'new' => 1,
255 'eventid' => -1,
256 'year' => isset($_REQUEST['year']) ? $_REQUEST['year'] : $today['year'],
257 'month' => isset($_REQUEST['month']) ? $_REQUEST['month'] : $today['mon'],
258 'day' => isset($_REQUEST['day']) ? $_REQUEST['day'] : $today['mday'],
259 'title' => '',
260 'span' => 1,
261 );
262 $context['event']['last_day'] = (int) strftime('%d', mktime(0, 0, 0, $context['event']['month'] == 12 ? 1 : $context['event']['month'] + 1, 0, $context['event']['month'] == 12 ? $context['event']['year'] + 1 : $context['event']['year']));
263
264 // Get list of boards that can be posted in.
265 $boards = boardsAllowedTo('post_new');
266 if (empty($boards))
267 fatal_lang_error('cannot_post_new', 'permission');
268
269 // Load the list of boards and categories in the context.
270 require_once($sourcedir . '/Subs-MessageIndex.php');
271 $boardListOptions = array(
272 'included_boards' => in_array(0, $boards) ? null : $boards,
273 'not_redirection' => true,
274 'use_permissions' => true,
275 'selected_board' => $modSettings['cal_defaultboard'],
276 );
277 $context['event']['categories'] = getBoardList($boardListOptions);
278 }
279 else
280 {
281 $context['event'] = getEventProperties($_REQUEST['eventid']);
282
283 if ($context['event'] === false)
284 fatal_lang_error('no_access', false);
285
286 // If it has a board, then they should be editing it within the topic.
287 if (!empty($context['event']['topic']['id']) && !empty($context['event']['topic']['first_msg']))
288 {
289 // We load the board up, for a check on the board access rights...
290 $topic = $context['event']['topic']['id'];
291 loadBoard();
292 }
293
294 // Make sure the user is allowed to edit this event.
295 if ($context['event']['member'] != $user_info['id'])
296 isAllowedTo('calendar_edit_any');
297 elseif (!allowedTo('calendar_edit_any'))
298 isAllowedTo('calendar_edit_own');
299 }
300
301 // Template, sub template, etc.
302 loadTemplate('Calendar');
303 $context['sub_template'] = 'event_post';
304
305 $context['page_title'] = isset($_REQUEST['eventid']) ? $txt['calendar_edit'] : $txt['calendar_post_event'];
306 $context['linktree'][] = array(
307 'name' => $context['page_title'],
308 );
309 }
310
311 function iCalDownload()
312 {
313 global $smcFunc, $sourcedir, $forum_version, $context, $modSettings;
314
315 // Goes without saying that this is required.
316 if (!isset($_REQUEST['eventid']))
317 fatal_lang_error('no_access', false);
318
319 // This is kinda wanted.
320 require_once($sourcedir . '/Subs-Calendar.php');
321
322 // Load up the event in question and check it exists.
323 $event = getEventProperties($_REQUEST['eventid']);
324
325 if ($event === false)
326 fatal_lang_error('no_access', false);
327
328 // Check the title isn't too long - iCal requires some formatting if so.
329 $title = str_split($event['title'], 30);
330 foreach ($title as $id => $line)
331 {
332 if ($id != 0)
333 $title[$id] = ' ' . $title[$id];
334 $title[$id] .= "\n";
335 }
336
337 // Format the date.
338 $date = $event['year'] . '-' . ($event['month'] < 10 ? '0' . $event['month'] : $event['month']) . '-' . ($event['day'] < 10 ? '0' . $event['day'] : $event['day']) . 'T';
339 $date .= '1200:00:00Z';
340
341 // This is what we will be sending later.
342 $filecontents = '';
343 $filecontents .= 'BEGIN:VCALENDAR' . "\n";
344 $filecontents .= 'VERSION:2.0' . "\n";
345 $filecontents .= 'PRODID:-//SimpleMachines//SMF ' . (empty($forum_version) ? 1.0 : strtr($forum_version, array('SMF ' => ''))) . '//EN' . "\n";
346 $filecontents .= 'BEGIN:VEVENT' . "\n";
347 $filecontents .= 'DTSTART:' . $date . "\n";
348 $filecontents .= 'DTEND:' . $date . "\n";
349 $filecontents .= 'SUMMARY:' . implode('', $title);
350 $filecontents .= 'END:VEVENT' . "\n";
351 $filecontents .= 'END:VCALENDAR';
352
353 // Send some standard headers.
354 ob_end_clean();
355 if (!empty($modSettings['enableCompressedOutput']))
356 @ob_start('ob_gzhandler');
357 else
358 ob_start();
359
360 // Send the file headers
361 header('Pragma: ');
362 header('Cache-Control: no-cache');
363 if (!$context['browser']['is_gecko'])
364 header('Content-Transfer-Encoding: binary');
365 header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 525600 * 60) . ' GMT');
366 header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . 'GMT');
367 header('Accept-Ranges: bytes');
368 header('Connection: close');
369 header('Content-Disposition: attachment; filename=' . $event['title'] . '.ics');
370
371 // How big is it?
372 if (empty($modSettings['enableCompressedOutput']))
373 header('Content-Length: ' . $smcFunc['strlen']($filecontents));
374
375 // This is a calendar item!
376 header('Content-Type: text/calendar');
377
378 // Chuck out the card.
379 echo $filecontents;
380
381 // Off we pop - lovely!
382 obExit(false);
383 }
384
385 // This is not the code you are looking for.
386 function clock()
387 {
388 global $settings, $context;
389 $context['onimg'] = $settings['images_url'] . '/bbc/bbc_bg.gif';
390 $context['offimg'] = $settings['images_url'] . '/bbc/bbc_hoverbg.gif';
391
392 $context['page_title'] = 'Anyone know what time it is?';
393 $context['robot_no_index'] = true;
394
395 $omfg = isset($_REQUEST['omfg']);
396 $bcd = !isset($_REQUEST['rb']) && !isset($_REQUEST['omfg']) && !isset($_REQUEST['time']);
397
398 loadTemplate('Calendar');
399
400 if ($bcd && !$omfg)
401 {
402 $context['sub_template'] = 'bcd';
403 $context['clockicons'] = unserialize(base64_decode('YTo2OntzOjI6ImgxIjthOjI6e2k6MDtpOjI7aToxO2k6MTt9czoyOiJoMiI7YTo0OntpOjA7aTo4O2k6MTtpOjQ7aToyO2k6MjtpOjM7aToxO31zOjI6Im0xIjthOjM6e2k6MDtpOjQ7aToxO2k6MjtpOjI7aToxO31zOjI6Im0yIjthOjQ6e2k6MDtpOjg7aToxO2k6NDtpOjI7aToyO2k6MztpOjE7fXM6MjoiczEiO2E6Mzp7aTowO2k6NDtpOjE7aToyO2k6MjtpOjE7fXM6MjoiczIiO2E6NDp7aTowO2k6ODtpOjE7aTo0O2k6MjtpOjI7aTozO2k6MTt9fQ=='));
404 }
405 elseif (!$omfg && !isset($_REQUEST['time']))
406 {
407 $context['sub_template'] = 'hms';
408 $context['clockicons'] = unserialize(base64_decode('YTozOntzOjE6ImgiO2E6NTp7aTowO2k6MTY7aToxO2k6ODtpOjI7aTo0O2k6MztpOjI7aTo0O2k6MTt9czoxOiJtIjthOjY6e2k6MDtpOjMyO2k6MTtpOjE2O2k6MjtpOjg7aTozO2k6NDtpOjQ7aToyO2k6NTtpOjE7fXM6MToicyI7YTo2OntpOjA7aTozMjtpOjE7aToxNjtpOjI7aTo4O2k6MztpOjQ7aTo0O2k6MjtpOjU7aToxO319'));
409 }
410 elseif ($omfg)
411 {
412 $context['sub_template'] = 'omfg';
413 $context['clockicons'] = unserialize(base64_decode('YTo2OntzOjQ6InllYXIiO2E6Nzp7aTowO2k6NjQ7aToxO2k6MzI7aToyO2k6MTY7aTozO2k6ODtpOjQ7aTo0O2k6NTtpOjI7aTo2O2k6MTt9czo1OiJtb250aCI7YTo0OntpOjA7aTo4O2k6MTtpOjQ7aToyO2k6MjtpOjM7aToxO31zOjM6ImRheSI7YTo1OntpOjA7aToxNjtpOjE7aTo4O2k6MjtpOjQ7aTozO2k6MjtpOjQ7aToxO31zOjQ6ImhvdXIiO2E6NTp7aTowO2k6MTY7aToxO2k6ODtpOjI7aTo0O2k6MztpOjI7aTo0O2k6MTt9czozOiJtaW4iO2E6Njp7aTowO2k6MzI7aToxO2k6MTY7aToyO2k6ODtpOjM7aTo0O2k6NDtpOjI7aTo1O2k6MTt9czozOiJzZWMiO2E6Njp7aTowO2k6MzI7aToxO2k6MTY7aToyO2k6ODtpOjM7aTo0O2k6NDtpOjI7aTo1O2k6MTt9fQ=='));
414 }
415 elseif (isset($_REQUEST['time']))
416 {
417 $context['sub_template'] = 'thetime';
418 $time = getdate($_REQUEST['time'] == 'now' ? time() : (int) $_REQUEST['time']);
419
420 $context['clockicons'] = array(
421 'year' => array(
422 64 => false,
423 32 => false,
424 16 => false,
425 8 => false,
426 4 => false,
427 2 => false,
428 1 => false
429 ),
430 'month' => array(
431 8 => false,
432 4 => false,
433 2 => false,
434 1 => false
435 ),
436 'day' => array(
437 16 => false,
438 4 => false,
439 8 => false,
440 2 => false,
441 1 => false
442 ),
443 'hour' => array(
444 32 => false,
445 16 => false,
446 8 => false,
447 4 => false,
448 2 => false,
449 1 => false
450 ),
451 'min' => array(
452 32 => false,
453 16 => false,
454 8 => false,
455 4 => false,
456 2 => false,
457 1 => false
458 ),
459 'sec' => array(
460 32 => false,
461 16 => false,
462 8 => false,
463 4 => false,
464 2 => false,
465 1 => false
466 ),
467 );
468
469 $year = $time['year'] % 100;
470 $month = $time['mon'];
471 $day = $time['mday'];
472 $hour = $time['hours'];
473 $min = $time['minutes'];
474 $sec = $time['seconds'];
475
476 foreach ($context['clockicons'] as $t => $vs)
477 foreach ($vs as $v => $dumb)
478 {
479 if ($$t >= $v)
480 {
481 $$t -= $v;
482 $context['clockicons'][$t][$v] = true;
483 }
484 }
485 }
486 }
487 ?>