1<?php
2/**
3 * Plugin iCalEvents: Renders an iCalendar file, e.g., as a table.
4 *
5 * Copyright (C) 2010-2012, 2015-2016
6 * Tim Ruffing, Robert Rackl, Elan Ruusamäe, Jannes Drost-Tenfelde
7 *
8 * This file is part of the DokuWiki iCalEvents plugin.
9 *
10 * The DokuWiki iCalEvents plugin program is free software:
11 * you can redistribute it and/or modify it under the terms of the
12 * GNU General Public License version 2 as published by the Free
13 * Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * version 2 along with the DokuWiki iCalEvents plugin program.  If
22 * not, see <http://www.gnu.org/licenses/gpl-2.0.html>.
23 *
24 * @license    https://www.gnu.org/licenses/gpl-2.0.html GPL2
25 * @author     Tim Ruffing <tim@timruffing.de>
26 * @author     Robert Rackl <wiki@doogie.de>
27 * @author     Elan Ruusamäe <glen@delfi.ee>
28 * @author     Jannes Drost-Tenfelde <info@drost-tenfelde.de>
29 *
30 */
31
32// We require at least PHP 5.5.
33if (PHP_VERSION_ID < 50500)
34    return;
35
36
37// must be run within Dokuwiki
38if (!defined('DOKU_INC'))
39    die();
40
41require_once DOKU_INC . 'inc/parser/renderer.php';
42require_once __DIR__ . '/vendor/autoload.php';
43
44// This is a minimal renderer plugin. It ignores everything except
45// invocations of our syntax plugin component. These invocations are
46// handled to output VEVENTs.
47
48class renderer_plugin_icalevents extends Doku_Renderer {
49    public $info = array(
50        // no caching, because the cache does not honor the UID parameter
51        'cache' => false,
52        // no table of contents
53        'toc'   => false
54    );
55
56    private $eventId = false;
57
58    function getFormat() {
59        return 'icalevents';
60    }
61
62    function plugin($name, $data, $state = '', $match = '') {
63        // Filter syntax plugins that are not our own syntax plugin,
64        // and stop processing if we have exported an event already.
65        // This is necessary to avoid duplicate VEVENTs in the output,
66        // which arise when displaying the same calendar multiple times on the same page.
67        if ($name == 'icalevents' && $this->eventId === false) {
68            return parent::plugin($name, $data, $state, $match);
69        }
70    }
71
72    function document_start() {
73        $this->doc = "BEGIN:VCALENDAR\r\n";
74        $this->doc .= "PRODID: -//DokuWiki//NONSGML Plugin iCalEvents//EN" . "\r\n";
75        $this->doc .= "VERSION:2.0\r\n";
76    }
77
78    function document_end() {
79        global $ID;
80
81        if ($this->eventId === false) {
82            http_status(404);
83            echo "Error: UID not found";
84            exit;
85        }
86
87        $filename =  SafeFN::encode(strtr($this->eventId, '/:', '--')) . '.ics';
88        $headers = array(
89            'Content-Type' => 'text/calendar',
90            'Content-Disposition' => 'attachment; filename=' . $filename
91        );
92        p_set_metadata($ID, array('format' => array('icalevents' => $headers)));
93
94        $this->doc .= "END:VCALENDAR\r\n";
95    }
96
97    function setEventId($eventId) {
98        $this->eventId = $eventId;
99    }
100
101    function reset() {
102        $this->eventId = false;
103    }
104
105    // Instantiating the class several times is not necessary,
106    // because we have implemented reset();
107    function isSingleton() {
108        return true;
109    }
110}
111
112