xref: /plugin/davcal/helper.php (revision 1d5bdcd0daf69d42a648436e4034819d35b1879d)
1a1a3b679SAndreas Boehler<?php
2a1a3b679SAndreas Boehler/**
3cb71a62aSAndreas Boehler  * Helper Class for the DAVCal plugin
4a1a3b679SAndreas Boehler  * This helper does the actual work.
5a1a3b679SAndreas Boehler  *
6a1a3b679SAndreas Boehler  */
7a1a3b679SAndreas Boehler
8a1a3b679SAndreas Boehler// must be run within Dokuwiki
9a1a3b679SAndreas Boehlerif(!defined('DOKU_INC')) die();
10a1a3b679SAndreas Boehler
11a1a3b679SAndreas Boehlerclass helper_plugin_davcal extends DokuWiki_Plugin {
12a1a3b679SAndreas Boehler
13a1a3b679SAndreas Boehler  protected $sqlite = null;
14185e2535SAndreas Boehler  protected $cachedValues = array();
15a1a3b679SAndreas Boehler
16a1a3b679SAndreas Boehler  /**
17cb71a62aSAndreas Boehler    * Constructor to load the configuration and the SQLite plugin
18a1a3b679SAndreas Boehler    */
19a1a3b679SAndreas Boehler  public function helper_plugin_davcal() {
20a1a3b679SAndreas Boehler    $this->sqlite =& plugin_load('helper', 'sqlite');
2121d04f73SAndreas Boehler    global $conf;
2221d04f73SAndreas Boehler    if($conf['allowdebug'])
2321d04f73SAndreas Boehler        dbglog('---- DAVCAL helper.php init');
24a1a3b679SAndreas Boehler    if(!$this->sqlite)
25a1a3b679SAndreas Boehler    {
2621d04f73SAndreas Boehler        if($conf['allowdebug'])
2721d04f73SAndreas Boehler            dbglog('This plugin requires the sqlite plugin. Please install it.');
28a1a3b679SAndreas Boehler        msg('This plugin requires the sqlite plugin. Please install it.');
29a1a3b679SAndreas Boehler        return;
30a1a3b679SAndreas Boehler    }
31a1a3b679SAndreas Boehler
32a1a3b679SAndreas Boehler    if(!$this->sqlite->init('davcal', DOKU_PLUGIN.'davcal/db/'))
33a1a3b679SAndreas Boehler    {
3421d04f73SAndreas Boehler        if($conf['allowdebug'])
3521d04f73SAndreas Boehler            dbglog('Error initialising the SQLite DB for DAVCal');
36a1a3b679SAndreas Boehler        return;
37a1a3b679SAndreas Boehler    }
38a1a3b679SAndreas Boehler  }
39a1a3b679SAndreas Boehler
40cb71a62aSAndreas Boehler  /**
41185e2535SAndreas Boehler   * Retrieve meta data for a given page
42185e2535SAndreas Boehler   *
43185e2535SAndreas Boehler   * @param string $id optional The page ID
44185e2535SAndreas Boehler   * @return array The metadata
45185e2535SAndreas Boehler   */
46185e2535SAndreas Boehler  private function getMeta($id = null) {
47185e2535SAndreas Boehler    global $ID;
48185e2535SAndreas Boehler    global $INFO;
49185e2535SAndreas Boehler
50185e2535SAndreas Boehler    if ($id === null) $id = $ID;
51185e2535SAndreas Boehler
52185e2535SAndreas Boehler    if($ID === $id && $INFO['meta']) {
53185e2535SAndreas Boehler        $meta = $INFO['meta'];
54185e2535SAndreas Boehler    } else {
55185e2535SAndreas Boehler        $meta = p_get_metadata($id);
56185e2535SAndreas Boehler    }
57185e2535SAndreas Boehler
58185e2535SAndreas Boehler    return $meta;
59185e2535SAndreas Boehler  }
60185e2535SAndreas Boehler
61185e2535SAndreas Boehler  /**
62185e2535SAndreas Boehler   * Retrieve the meta data for a given page
63185e2535SAndreas Boehler   *
64185e2535SAndreas Boehler   * @param string $id optional The page ID
65185e2535SAndreas Boehler   * @return array with meta data
66185e2535SAndreas Boehler   */
67185e2535SAndreas Boehler  public function getCalendarMetaForPage($id = null)
68185e2535SAndreas Boehler  {
69185e2535SAndreas Boehler      if(is_null($id))
70185e2535SAndreas Boehler      {
71185e2535SAndreas Boehler          global $ID;
72185e2535SAndreas Boehler          $id = $ID;
73185e2535SAndreas Boehler      }
74185e2535SAndreas Boehler
75185e2535SAndreas Boehler      $meta = $this->getMeta($id);
76185e2535SAndreas Boehler      if(isset($meta['plugin_davcal']))
77185e2535SAndreas Boehler        return $meta['plugin_davcal'];
78185e2535SAndreas Boehler      else
79185e2535SAndreas Boehler        return array();
80185e2535SAndreas Boehler  }
81185e2535SAndreas Boehler
82185e2535SAndreas Boehler  /**
83185e2535SAndreas Boehler   * Get all calendar pages used by a given page
84185e2535SAndreas Boehler   * based on the stored metadata
85185e2535SAndreas Boehler   *
86185e2535SAndreas Boehler   * @param string $id optional The page id
87185e2535SAndreas Boehler   * @return mixed The pages as array or false
88185e2535SAndreas Boehler   */
89185e2535SAndreas Boehler  public function getCalendarPagesByMeta($id = null)
90185e2535SAndreas Boehler  {
91185e2535SAndreas Boehler      if(is_null($id))
92185e2535SAndreas Boehler      {
93185e2535SAndreas Boehler          global $ID;
94185e2535SAndreas Boehler          $id = $ID;
95185e2535SAndreas Boehler      }
96185e2535SAndreas Boehler
97185e2535SAndreas Boehler      $meta = $this->getCalendarMetaForPage($id);
98185e2535SAndreas Boehler      if(isset($meta['id']))
99185e2535SAndreas Boehler          return array_keys($meta['id']);
100185e2535SAndreas Boehler      return false;
101185e2535SAndreas Boehler  }
102185e2535SAndreas Boehler
103185e2535SAndreas Boehler  /**
104185e2535SAndreas Boehler   * Get a list of calendar names/pages/ids/colors
105185e2535SAndreas Boehler   * for an array of page ids
106185e2535SAndreas Boehler   *
107185e2535SAndreas Boehler   * @param array $calendarPages The calendar pages to retrieve
108185e2535SAndreas Boehler   * @return array The list
109185e2535SAndreas Boehler   */
110185e2535SAndreas Boehler  public function getCalendarMapForIDs($calendarPages)
111185e2535SAndreas Boehler  {
112185e2535SAndreas Boehler      $data = array();
113185e2535SAndreas Boehler      foreach($calendarPages as $page)
114185e2535SAndreas Boehler      {
115185e2535SAndreas Boehler          $calid = $this->getCalendarIdForPage($page);
116185e2535SAndreas Boehler          if($calid !== false)
117185e2535SAndreas Boehler          {
118185e2535SAndreas Boehler            $settings = $this->getCalendarSettings($calid);
119185e2535SAndreas Boehler            $name = $settings['displayname'];
120185e2535SAndreas Boehler            $color = $settings['calendarcolor'];
121185e2535SAndreas Boehler            $data[] = array('name' => $name, 'page' => $page, 'calid' => $calid,
122185e2535SAndreas Boehler                            'color' => $color);
123185e2535SAndreas Boehler          }
124185e2535SAndreas Boehler      }
125185e2535SAndreas Boehler      return $data;
126185e2535SAndreas Boehler  }
127185e2535SAndreas Boehler
128185e2535SAndreas Boehler  /**
129185e2535SAndreas Boehler   * Get the saved calendar color for a given page.
130185e2535SAndreas Boehler   *
131185e2535SAndreas Boehler   * @param string $id optional The page ID
132185e2535SAndreas Boehler   * @return mixed The color on success, otherwise false
133185e2535SAndreas Boehler   */
134185e2535SAndreas Boehler  public function getCalendarColorForPage($id = null)
135185e2535SAndreas Boehler  {
136185e2535SAndreas Boehler      if(is_null($id))
137185e2535SAndreas Boehler      {
138185e2535SAndreas Boehler          global $ID;
139185e2535SAndreas Boehler          $id = $ID;
140185e2535SAndreas Boehler      }
141185e2535SAndreas Boehler
142185e2535SAndreas Boehler      $calid = $this->getCalendarIdForPage($id);
143185e2535SAndreas Boehler      if($calid === false)
144185e2535SAndreas Boehler        return false;
145185e2535SAndreas Boehler
146185e2535SAndreas Boehler      return $this->getCalendarColorForCalendar($calid);
147185e2535SAndreas Boehler  }
148185e2535SAndreas Boehler
149185e2535SAndreas Boehler  /**
150185e2535SAndreas Boehler   * Get the saved calendar color for a given calendar ID.
151185e2535SAndreas Boehler   *
152185e2535SAndreas Boehler   * @param string $id optional The calendar ID
153185e2535SAndreas Boehler   * @return mixed The color on success, otherwise false
154185e2535SAndreas Boehler   */
155185e2535SAndreas Boehler  public function getCalendarColorForCalendar($calid)
156185e2535SAndreas Boehler  {
157185e2535SAndreas Boehler      if(isset($this->cachedValues['calendarcolor'][$calid]))
158185e2535SAndreas Boehler        return $this->cachedValues['calendarcolor'][$calid];
159185e2535SAndreas Boehler
160185e2535SAndreas Boehler      $row = $this->getCalendarSettings($calid);
161185e2535SAndreas Boehler
162185e2535SAndreas Boehler      if(!isset($row['calendarcolor']))
163185e2535SAndreas Boehler        return false;
164185e2535SAndreas Boehler
165185e2535SAndreas Boehler      $color = $row['calendarcolor'];
166185e2535SAndreas Boehler      $this->cachedValues['calendarcolor'][$calid] = $color;
167185e2535SAndreas Boehler      return $color;
168185e2535SAndreas Boehler  }
169185e2535SAndreas Boehler
170185e2535SAndreas Boehler  /**
171185e2535SAndreas Boehler   * Set the calendar color for a given page.
172185e2535SAndreas Boehler   *
173185e2535SAndreas Boehler   * @param string $color The color definition
174185e2535SAndreas Boehler   * @param string $id optional The page ID
175185e2535SAndreas Boehler   * @return boolean True on success, otherwise false
176185e2535SAndreas Boehler   */
177185e2535SAndreas Boehler  public function setCalendarColorForPage($color, $id = null)
178185e2535SAndreas Boehler  {
179185e2535SAndreas Boehler      if(is_null($id))
180185e2535SAndreas Boehler      {
181185e2535SAndreas Boehler          global $ID;
182185e2535SAndreas Boehler          $id = $ID;
183185e2535SAndreas Boehler      }
184185e2535SAndreas Boehler      $calid = $this->getCalendarIdForPage($id);
185185e2535SAndreas Boehler      if($calid === false)
186185e2535SAndreas Boehler        return false;
187185e2535SAndreas Boehler
188185e2535SAndreas Boehler      $query = "UPDATE calendars SET calendarcolor=".$this->sqlite->quote_string($color).
189185e2535SAndreas Boehler               " WHERE id=".$this->sqlite->quote_string($calid);
190185e2535SAndreas Boehler      $res = $this->sqlite->query($query);
191185e2535SAndreas Boehler      if($res !== false)
192185e2535SAndreas Boehler      {
193185e2535SAndreas Boehler        $this->cachedValues['calendarcolor'][$calid] = $color;
194185e2535SAndreas Boehler        return true;
195185e2535SAndreas Boehler      }
196185e2535SAndreas Boehler      return false;
197185e2535SAndreas Boehler  }
198185e2535SAndreas Boehler
199185e2535SAndreas Boehler  /**
200cb71a62aSAndreas Boehler   * Set the calendar name and description for a given page with a given
201cb71a62aSAndreas Boehler   * page id.
202cb71a62aSAndreas Boehler   * If the calendar doesn't exist, the calendar is created!
203cb71a62aSAndreas Boehler   *
204cb71a62aSAndreas Boehler   * @param string  $name The name of the new calendar
205cb71a62aSAndreas Boehler   * @param string  $description The description of the new calendar
206cb71a62aSAndreas Boehler   * @param string  $id (optional) The ID of the page
207cb71a62aSAndreas Boehler   * @param string  $userid The userid of the creating user
208cb71a62aSAndreas Boehler   *
209cb71a62aSAndreas Boehler   * @return boolean True on success, otherwise false.
210cb71a62aSAndreas Boehler   */
211a1a3b679SAndreas Boehler  public function setCalendarNameForPage($name, $description, $id = null, $userid = null)
212a1a3b679SAndreas Boehler  {
213a1a3b679SAndreas Boehler      if(is_null($id))
214a1a3b679SAndreas Boehler      {
215a1a3b679SAndreas Boehler          global $ID;
216a1a3b679SAndreas Boehler          $id = $ID;
217a1a3b679SAndreas Boehler      }
218a1a3b679SAndreas Boehler      if(is_null($userid))
21934a47953SAndreas Boehler      {
22034a47953SAndreas Boehler        if(isset($_SERVER['REMOTE_USER']) && !is_null($_SERVER['REMOTE_USER']))
22134a47953SAndreas Boehler        {
222a1a3b679SAndreas Boehler          $userid = $_SERVER['REMOTE_USER'];
22334a47953SAndreas Boehler        }
22434a47953SAndreas Boehler        else
22534a47953SAndreas Boehler        {
22634a47953SAndreas Boehler          $userid = uniqid('davcal-');
22734a47953SAndreas Boehler        }
22834a47953SAndreas Boehler      }
229a1a3b679SAndreas Boehler      $calid = $this->getCalendarIdForPage($id);
230a1a3b679SAndreas Boehler      if($calid === false)
231a1a3b679SAndreas Boehler        return $this->createCalendarForPage($name, $description, $id, $userid);
232a1a3b679SAndreas Boehler
233b269830cSAndreas Boehler      $query = "UPDATE calendars SET displayname=".$this->sqlite->quote_string($name).", ".
234b269830cSAndreas Boehler               "description=".$this->sqlite->quote_string($description)." WHERE ".
235b269830cSAndreas Boehler               "id=".$this->sqlite->quote_string($calid);
236b269830cSAndreas Boehler      $res = $this->sqlite->query($query);
237b269830cSAndreas Boehler      if($res !== false)
238b269830cSAndreas Boehler        return true;
239b269830cSAndreas Boehler      return false;
240a1a3b679SAndreas Boehler  }
241a1a3b679SAndreas Boehler
242cb71a62aSAndreas Boehler  /**
243cb71a62aSAndreas Boehler   * Save the personal settings to the SQLite database 'calendarsettings'.
244cb71a62aSAndreas Boehler   *
245cb71a62aSAndreas Boehler   * @param array  $settings The settings array to store
246cb71a62aSAndreas Boehler   * @param string $userid (optional) The userid to store
247cb71a62aSAndreas Boehler   *
248cb71a62aSAndreas Boehler   * @param boolean True on success, otherwise false
249cb71a62aSAndreas Boehler   */
250a495d34cSAndreas Boehler  public function savePersonalSettings($settings, $userid = null)
251a495d34cSAndreas Boehler  {
252a495d34cSAndreas Boehler      if(is_null($userid))
25334a47953SAndreas Boehler      {
25434a47953SAndreas Boehler          if(isset($_SERVER['REMOTE_USER']) && !is_null($_SERVER['REMOTE_USER']))
25534a47953SAndreas Boehler          {
256a495d34cSAndreas Boehler            $userid = $_SERVER['REMOTE_USER'];
25734a47953SAndreas Boehler          }
25834a47953SAndreas Boehler          else
25934a47953SAndreas Boehler          {
26034a47953SAndreas Boehler              return false;
26134a47953SAndreas Boehler          }
26234a47953SAndreas Boehler      }
263a495d34cSAndreas Boehler      $this->sqlite->query("BEGIN TRANSACTION");
264a495d34cSAndreas Boehler
265bd883736SAndreas Boehler      $query = "DELETE FROM calendarsettings WHERE userid=".$this->sqlite->quote_string($userid);
266bd883736SAndreas Boehler      $this->sqlite->query($query);
267bd883736SAndreas Boehler
268a495d34cSAndreas Boehler      foreach($settings as $key => $value)
269a495d34cSAndreas Boehler      {
270bd883736SAndreas Boehler          $query = "INSERT INTO calendarsettings (userid, key, value) VALUES (".
271a495d34cSAndreas Boehler                   $this->sqlite->quote_string($userid).", ".
272a495d34cSAndreas Boehler                   $this->sqlite->quote_string($key).", ".
273a495d34cSAndreas Boehler                   $this->sqlite->quote_string($value).")";
274a495d34cSAndreas Boehler          $res = $this->sqlite->query($query);
275a495d34cSAndreas Boehler          if($res === false)
276a495d34cSAndreas Boehler              return false;
277a495d34cSAndreas Boehler      }
278a495d34cSAndreas Boehler      $this->sqlite->query("COMMIT TRANSACTION");
279185e2535SAndreas Boehler      $this->cachedValues['settings'][$userid] = $settings;
280a495d34cSAndreas Boehler      return true;
281a495d34cSAndreas Boehler  }
282a495d34cSAndreas Boehler
283cb71a62aSAndreas Boehler  /**
284cb71a62aSAndreas Boehler   * Retrieve the settings array for a given user id.
285cb71a62aSAndreas Boehler   * Some sane defaults are returned, currently:
286cb71a62aSAndreas Boehler   *
287cb71a62aSAndreas Boehler   *    timezone    => local
288cb71a62aSAndreas Boehler   *    weeknumbers => 0
289cb71a62aSAndreas Boehler   *    workweek    => 0
290cb71a62aSAndreas Boehler   *
291cb71a62aSAndreas Boehler   * @param string $userid (optional) The user id to retrieve
292cb71a62aSAndreas Boehler   *
293cb71a62aSAndreas Boehler   * @return array The settings array
294cb71a62aSAndreas Boehler   */
295a495d34cSAndreas Boehler  public function getPersonalSettings($userid = null)
296a495d34cSAndreas Boehler  {
297bd883736SAndreas Boehler      // Some sane default settings
298bd883736SAndreas Boehler      $settings = array(
299fb813b30SAndreas Boehler        'timezone' => $this->getConf('timezone'),
300fb813b30SAndreas Boehler        'weeknumbers' => $this->getConf('weeknumbers'),
301fb813b30SAndreas Boehler        'workweek' => $this->getConf('workweek'),
302*1d5bdcd0SAndreas Boehler        'monday' => $this->getConf('monday'),
303*1d5bdcd0SAndreas Boehler        'timeformat' => $this->getConf('timeformat')
304bd883736SAndreas Boehler      );
30534a47953SAndreas Boehler      if(is_null($userid))
30634a47953SAndreas Boehler      {
30734a47953SAndreas Boehler          if(isset($_SERVER['REMOTE_USER']) && !is_null($_SERVER['REMOTE_USER']))
30834a47953SAndreas Boehler          {
30934a47953SAndreas Boehler            $userid = $_SERVER['REMOTE_USER'];
31034a47953SAndreas Boehler          }
31134a47953SAndreas Boehler          else
31234a47953SAndreas Boehler          {
31334a47953SAndreas Boehler            return $settings;
31434a47953SAndreas Boehler          }
31534a47953SAndreas Boehler      }
31634a47953SAndreas Boehler
31734a47953SAndreas Boehler      if(isset($this->cachedValues['settings'][$userid]))
31834a47953SAndreas Boehler        return $this->cachedValues['settings'][$userid];
319a495d34cSAndreas Boehler      $query = "SELECT key, value FROM calendarsettings WHERE userid=".$this->sqlite->quote_string($userid);
320a495d34cSAndreas Boehler      $res = $this->sqlite->query($query);
321a495d34cSAndreas Boehler      $arr = $this->sqlite->res2arr($res);
322a495d34cSAndreas Boehler      foreach($arr as $row)
323a495d34cSAndreas Boehler      {
324a495d34cSAndreas Boehler          $settings[$row['key']] = $row['value'];
325a495d34cSAndreas Boehler      }
326185e2535SAndreas Boehler      $this->cachedValues['settings'][$userid] = $settings;
327a495d34cSAndreas Boehler      return $settings;
328a495d34cSAndreas Boehler  }
329a495d34cSAndreas Boehler
330cb71a62aSAndreas Boehler  /**
331cb71a62aSAndreas Boehler   * Retrieve the calendar ID based on a page ID from the SQLite table
332cb71a62aSAndreas Boehler   * 'pagetocalendarmapping'.
333cb71a62aSAndreas Boehler   *
334cb71a62aSAndreas Boehler   * @param string $id (optional) The page ID to retrieve the corresponding calendar
335cb71a62aSAndreas Boehler   *
336cb71a62aSAndreas Boehler   * @return mixed the ID on success, otherwise false
337cb71a62aSAndreas Boehler   */
338a1a3b679SAndreas Boehler  public function getCalendarIdForPage($id = null)
339a1a3b679SAndreas Boehler  {
340a1a3b679SAndreas Boehler      if(is_null($id))
341a1a3b679SAndreas Boehler      {
342a1a3b679SAndreas Boehler          global $ID;
343a1a3b679SAndreas Boehler          $id = $ID;
344a1a3b679SAndreas Boehler      }
345a1a3b679SAndreas Boehler
346185e2535SAndreas Boehler      if(isset($this->cachedValues['calid'][$id]))
347185e2535SAndreas Boehler        return $this->cachedValues['calid'][$id];
348185e2535SAndreas Boehler
349a1a3b679SAndreas Boehler      $query = "SELECT calid FROM pagetocalendarmapping WHERE page=".$this->sqlite->quote_string($id);
350a1a3b679SAndreas Boehler      $res = $this->sqlite->query($query);
351a1a3b679SAndreas Boehler      $row = $this->sqlite->res2row($res);
352a1a3b679SAndreas Boehler      if(isset($row['calid']))
353185e2535SAndreas Boehler      {
354185e2535SAndreas Boehler        $calid = $row['calid'];
355185e2535SAndreas Boehler        $this->cachedValues['calid'] = $calid;
356185e2535SAndreas Boehler        return $calid;
357185e2535SAndreas Boehler      }
358a1a3b679SAndreas Boehler      return false;
359a1a3b679SAndreas Boehler  }
360a1a3b679SAndreas Boehler
361cb71a62aSAndreas Boehler  /**
362cb71a62aSAndreas Boehler   * Retrieve the complete calendar id to page mapping.
363cb71a62aSAndreas Boehler   * This is necessary to be able to retrieve a list of
364cb71a62aSAndreas Boehler   * calendars for a given user and check the access rights.
365cb71a62aSAndreas Boehler   *
366cb71a62aSAndreas Boehler   * @return array The mapping array
367cb71a62aSAndreas Boehler   */
368a1a3b679SAndreas Boehler  public function getCalendarIdToPageMapping()
369a1a3b679SAndreas Boehler  {
370a1a3b679SAndreas Boehler      $query = "SELECT calid, page FROM pagetocalendarmapping";
371a1a3b679SAndreas Boehler      $res = $this->sqlite->query($query);
372a1a3b679SAndreas Boehler      $arr = $this->sqlite->res2arr($res);
373a1a3b679SAndreas Boehler      return $arr;
374a1a3b679SAndreas Boehler  }
375a1a3b679SAndreas Boehler
376cb71a62aSAndreas Boehler  /**
377cb71a62aSAndreas Boehler   * Retrieve all calendar IDs a given user has access to.
378cb71a62aSAndreas Boehler   * The user is specified by the principalUri, so the
379cb71a62aSAndreas Boehler   * user name is actually split from the URI component.
380cb71a62aSAndreas Boehler   *
381cb71a62aSAndreas Boehler   * Access rights are checked against DokuWiki's ACL
382cb71a62aSAndreas Boehler   * and applied accordingly.
383cb71a62aSAndreas Boehler   *
384cb71a62aSAndreas Boehler   * @param string $principalUri The principal URI to work on
385cb71a62aSAndreas Boehler   *
386cb71a62aSAndreas Boehler   * @return array An associative array of calendar IDs
387cb71a62aSAndreas Boehler   */
388a1a3b679SAndreas Boehler  public function getCalendarIdsForUser($principalUri)
389a1a3b679SAndreas Boehler  {
39034a47953SAndreas Boehler      global $auth;
391a1a3b679SAndreas Boehler      $user = explode('/', $principalUri);
392a1a3b679SAndreas Boehler      $user = end($user);
393a1a3b679SAndreas Boehler      $mapping = $this->getCalendarIdToPageMapping();
394a1a3b679SAndreas Boehler      $calids = array();
39534a47953SAndreas Boehler      $ud = $auth->getUserData($user);
39634a47953SAndreas Boehler      $groups = $ud['grps'];
397a1a3b679SAndreas Boehler      foreach($mapping as $row)
398a1a3b679SAndreas Boehler      {
399a1a3b679SAndreas Boehler          $id = $row['calid'];
400a1a3b679SAndreas Boehler          $page = $row['page'];
40134a47953SAndreas Boehler          $acl = auth_aclcheck($page, $user, $groups);
402a1a3b679SAndreas Boehler          if($acl >= AUTH_READ)
403a1a3b679SAndreas Boehler          {
404a1a3b679SAndreas Boehler              $write = $acl > AUTH_READ;
405a1a3b679SAndreas Boehler              $calids[$id] = array('readonly' => !$write);
406a1a3b679SAndreas Boehler          }
407a1a3b679SAndreas Boehler      }
408a1a3b679SAndreas Boehler      return $calids;
409a1a3b679SAndreas Boehler  }
410a1a3b679SAndreas Boehler
411cb71a62aSAndreas Boehler  /**
412cb71a62aSAndreas Boehler   * Create a new calendar for a given page ID and set name and description
413cb71a62aSAndreas Boehler   * accordingly. Also update the pagetocalendarmapping table on success.
414cb71a62aSAndreas Boehler   *
415cb71a62aSAndreas Boehler   * @param string $name The calendar's name
416cb71a62aSAndreas Boehler   * @param string $description The calendar's description
417cb71a62aSAndreas Boehler   * @param string $id (optional) The page ID to work on
418cb71a62aSAndreas Boehler   * @param string $userid (optional) The user ID that created the calendar
419cb71a62aSAndreas Boehler   *
420cb71a62aSAndreas Boehler   * @return boolean True on success, otherwise false
421cb71a62aSAndreas Boehler   */
422a1a3b679SAndreas Boehler  public function createCalendarForPage($name, $description, $id = null, $userid = null)
423a1a3b679SAndreas Boehler  {
424a1a3b679SAndreas Boehler      if(is_null($id))
425a1a3b679SAndreas Boehler      {
426a1a3b679SAndreas Boehler          global $ID;
427a1a3b679SAndreas Boehler          $id = $ID;
428a1a3b679SAndreas Boehler      }
429a1a3b679SAndreas Boehler      if(is_null($userid))
43034a47953SAndreas Boehler      {
43134a47953SAndreas Boehler        if(isset($_SERVER['REMOTE_USER']) && !is_null($_SERVER['REMOTE_USER']))
43234a47953SAndreas Boehler        {
433a1a3b679SAndreas Boehler          $userid = $_SERVER['REMOTE_USER'];
43434a47953SAndreas Boehler        }
43534a47953SAndreas Boehler        else
43634a47953SAndreas Boehler        {
43734a47953SAndreas Boehler          $userid = uniqid('davcal-');
43834a47953SAndreas Boehler        }
43934a47953SAndreas Boehler      }
440a1a3b679SAndreas Boehler      $values = array('principals/'.$userid,
441a1a3b679SAndreas Boehler                      $name,
442a1a3b679SAndreas Boehler                      str_replace(array('/', ' ', ':'), '_', $id),
443a1a3b679SAndreas Boehler                      $description,
444a1a3b679SAndreas Boehler                      'VEVENT,VTODO',
44555a741c0SAndreas Boehler                      0,
44655a741c0SAndreas Boehler                      1);
44755a741c0SAndreas Boehler      $query = "INSERT INTO calendars (principaluri, displayname, uri, description, components, transparent, synctoken) VALUES (".$this->sqlite->quote_and_join($values, ',').");";
448a1a3b679SAndreas Boehler      $res = $this->sqlite->query($query);
44955a741c0SAndreas Boehler      if($res === false)
45055a741c0SAndreas Boehler        return false;
451cb71a62aSAndreas Boehler
452cb71a62aSAndreas Boehler      // Get the new calendar ID
453a1a3b679SAndreas Boehler      $query = "SELECT id FROM calendars WHERE principaluri=".$this->sqlite->quote_string($values[0])." AND ".
454a1a3b679SAndreas Boehler               "displayname=".$this->sqlite->quote_string($values[1])." AND ".
455a1a3b679SAndreas Boehler               "uri=".$this->sqlite->quote_string($values[2])." AND ".
456a1a3b679SAndreas Boehler               "description=".$this->sqlite->quote_string($values[3]);
457a1a3b679SAndreas Boehler      $res = $this->sqlite->query($query);
458a1a3b679SAndreas Boehler      $row = $this->sqlite->res2row($res);
459cb71a62aSAndreas Boehler
460cb71a62aSAndreas Boehler      // Update the pagetocalendarmapping table with the new calendar ID
461a1a3b679SAndreas Boehler      if(isset($row['id']))
462a1a3b679SAndreas Boehler      {
463a1a3b679SAndreas Boehler          $values = array($id, $row['id']);
464a1a3b679SAndreas Boehler          $query = "INSERT INTO pagetocalendarmapping (page, calid) VALUES (".$this->sqlite->quote_and_join($values, ',').")";
465a1a3b679SAndreas Boehler          $res = $this->sqlite->query($query);
46655a741c0SAndreas Boehler          return ($res !== false);
467a1a3b679SAndreas Boehler      }
468a1a3b679SAndreas Boehler
469a1a3b679SAndreas Boehler      return false;
470a1a3b679SAndreas Boehler  }
471a1a3b679SAndreas Boehler
472cb71a62aSAndreas Boehler  /**
473cb71a62aSAndreas Boehler   * Add a new iCal entry for a given page, i.e. a given calendar.
474cb71a62aSAndreas Boehler   *
475cb71a62aSAndreas Boehler   * The parameter array needs to contain
476cb71a62aSAndreas Boehler   *   detectedtz       => The timezone as detected by the browser
47782a48dfbSAndreas Boehler   *   currenttz        => The timezone in use by the calendar
478cb71a62aSAndreas Boehler   *   eventfrom        => The event's start date
479cb71a62aSAndreas Boehler   *   eventfromtime    => The event's start time
480cb71a62aSAndreas Boehler   *   eventto          => The event's end date
481cb71a62aSAndreas Boehler   *   eventtotime      => The event's end time
482cb71a62aSAndreas Boehler   *   eventname        => The event's name
483cb71a62aSAndreas Boehler   *   eventdescription => The event's description
484cb71a62aSAndreas Boehler   *
485cb71a62aSAndreas Boehler   * @param string $id The page ID to work on
486cb71a62aSAndreas Boehler   * @param string $user The user who created the calendar
487cb71a62aSAndreas Boehler   * @param string $params A parameter array with values to create
488cb71a62aSAndreas Boehler   *
489cb71a62aSAndreas Boehler   * @return boolean True on success, otherwise false
490cb71a62aSAndreas Boehler   */
491a1a3b679SAndreas Boehler  public function addCalendarEntryToCalendarForPage($id, $user, $params)
492a1a3b679SAndreas Boehler  {
49382a48dfbSAndreas Boehler      if($params['currenttz'] !== '' && $params['currenttz'] !== 'local')
49482a48dfbSAndreas Boehler          $timezone = new \DateTimeZone($params['currenttz']);
49582a48dfbSAndreas Boehler      elseif($params['currenttz'] === 'local')
496a25c89eaSAndreas Boehler          $timezone = new \DateTimeZone($params['detectedtz']);
497bd883736SAndreas Boehler      else
498bd883736SAndreas Boehler          $timezone = new \DateTimeZone('UTC');
499cb71a62aSAndreas Boehler
500cb71a62aSAndreas Boehler      // Retrieve dates from settings
501b269830cSAndreas Boehler      $startDate = explode('-', $params['eventfrom']);
502b269830cSAndreas Boehler      $startTime = explode(':', $params['eventfromtime']);
503b269830cSAndreas Boehler      $endDate = explode('-', $params['eventto']);
504b269830cSAndreas Boehler      $endTime = explode(':', $params['eventtotime']);
505cb71a62aSAndreas Boehler
506cb71a62aSAndreas Boehler      // Load SabreDAV
5079bef4ad8SAndreas Boehler      require_once(DOKU_PLUGIN.'davcal/vendor/autoload.php');
508a1a3b679SAndreas Boehler      $vcalendar = new \Sabre\VObject\Component\VCalendar();
509cb71a62aSAndreas Boehler
510cb71a62aSAndreas Boehler      // Add VCalendar, UID and Event Name
511a1a3b679SAndreas Boehler      $event = $vcalendar->add('VEVENT');
512b269830cSAndreas Boehler      $uuid = \Sabre\VObject\UUIDUtil::getUUID();
513b269830cSAndreas Boehler      $event->add('UID', $uuid);
514a1a3b679SAndreas Boehler      $event->summary = $params['eventname'];
515cb71a62aSAndreas Boehler
516cb71a62aSAndreas Boehler      // Add a description if requested
5170eebc909SAndreas Boehler      $description = $params['eventdescription'];
5180eebc909SAndreas Boehler      if($description !== '')
5190eebc909SAndreas Boehler        $event->add('DESCRIPTION', $description);
520cb71a62aSAndreas Boehler
5214ecb526cSAndreas Boehler      // Add attachments
5224ecb526cSAndreas Boehler      $attachments = $params['attachments'];
52382a48dfbSAndreas Boehler      if(!is_null($attachments))
5244ecb526cSAndreas Boehler        foreach($attachments as $attachment)
5254ecb526cSAndreas Boehler          $event->add('ATTACH', $attachment);
5264ecb526cSAndreas Boehler
527cb71a62aSAndreas Boehler      // Create a timestamp for last modified, created and dtstamp values in UTC
528b269830cSAndreas Boehler      $dtStamp = new \DateTime(null, new \DateTimeZone('UTC'));
529b269830cSAndreas Boehler      $event->add('DTSTAMP', $dtStamp);
530b269830cSAndreas Boehler      $event->add('CREATED', $dtStamp);
531b269830cSAndreas Boehler      $event->add('LAST-MODIFIED', $dtStamp);
532cb71a62aSAndreas Boehler
533cb71a62aSAndreas Boehler      // Adjust the start date, based on the given timezone information
534b269830cSAndreas Boehler      $dtStart = new \DateTime();
535a25c89eaSAndreas Boehler      $dtStart->setTimezone($timezone);
536b269830cSAndreas Boehler      $dtStart->setDate(intval($startDate[0]), intval($startDate[1]), intval($startDate[2]));
537cb71a62aSAndreas Boehler
538cb71a62aSAndreas Boehler      // Only add the time values if it's not an allday event
539b269830cSAndreas Boehler      if($params['allday'] != '1')
540b269830cSAndreas Boehler        $dtStart->setTime(intval($startTime[0]), intval($startTime[1]), 0);
541cb71a62aSAndreas Boehler
542cb71a62aSAndreas Boehler      // Adjust the end date, based on the given timezone information
543b269830cSAndreas Boehler      $dtEnd = new \DateTime();
544a25c89eaSAndreas Boehler      $dtEnd->setTimezone($timezone);
545b269830cSAndreas Boehler      $dtEnd->setDate(intval($endDate[0]), intval($endDate[1]), intval($endDate[2]));
546cb71a62aSAndreas Boehler
547cb71a62aSAndreas Boehler      // Only add the time values if it's not an allday event
548b269830cSAndreas Boehler      if($params['allday'] != '1')
549b269830cSAndreas Boehler        $dtEnd->setTime(intval($endTime[0]), intval($endTime[1]), 0);
550cb71a62aSAndreas Boehler
551b269830cSAndreas Boehler      // According to the VCal spec, we need to add a whole day here
552b269830cSAndreas Boehler      if($params['allday'] == '1')
553b269830cSAndreas Boehler          $dtEnd->add(new \DateInterval('P1D'));
554cb71a62aSAndreas Boehler
555cb71a62aSAndreas Boehler      // Really add Start and End events
556b269830cSAndreas Boehler      $dtStartEv = $event->add('DTSTART', $dtStart);
557b269830cSAndreas Boehler      $dtEndEv = $event->add('DTEND', $dtEnd);
558cb71a62aSAndreas Boehler
559cb71a62aSAndreas Boehler      // Adjust the DATE format for allday events
560b269830cSAndreas Boehler      if($params['allday'] == '1')
561b269830cSAndreas Boehler      {
562b269830cSAndreas Boehler          $dtStartEv['VALUE'] = 'DATE';
563b269830cSAndreas Boehler          $dtEndEv['VALUE'] = 'DATE';
564b269830cSAndreas Boehler      }
565cb71a62aSAndreas Boehler
566cb71a62aSAndreas Boehler      // Actually add the values to the database
567a1a3b679SAndreas Boehler      $calid = $this->getCalendarIdForPage($id);
568a1a3b679SAndreas Boehler      $uri = uniqid('dokuwiki-').'.ics';
569a1a3b679SAndreas Boehler      $now = new DateTime();
570a1a3b679SAndreas Boehler      $eventStr = $vcalendar->serialize();
571a1a3b679SAndreas Boehler
572a1a3b679SAndreas Boehler      $values = array($calid,
573a1a3b679SAndreas Boehler                      $uri,
574a1a3b679SAndreas Boehler                      $eventStr,
575a1a3b679SAndreas Boehler                      $now->getTimestamp(),
576a1a3b679SAndreas Boehler                      'VEVENT',
577a1a3b679SAndreas Boehler                      $event->DTSTART->getDateTime()->getTimeStamp(),
578a1a3b679SAndreas Boehler                      $event->DTEND->getDateTime()->getTimeStamp(),
579a1a3b679SAndreas Boehler                      strlen($eventStr),
580a1a3b679SAndreas Boehler                      md5($eventStr),
581cb71a62aSAndreas Boehler                      $uuid
582a1a3b679SAndreas Boehler      );
583a1a3b679SAndreas Boehler
584a1a3b679SAndreas Boehler      $query = "INSERT INTO calendarobjects (calendarid, uri, calendardata, lastmodified, componenttype, firstoccurence, lastoccurence, size, etag, uid) VALUES (".$this->sqlite->quote_and_join($values, ',').")";
585a1a3b679SAndreas Boehler      $res = $this->sqlite->query($query);
586cb71a62aSAndreas Boehler
587cb71a62aSAndreas Boehler      // If successfully, update the sync token database
58855a741c0SAndreas Boehler      if($res !== false)
58955a741c0SAndreas Boehler      {
59055a741c0SAndreas Boehler          $this->updateSyncTokenLog($calid, $uri, 'added');
591a1a3b679SAndreas Boehler          return true;
592a1a3b679SAndreas Boehler      }
59355a741c0SAndreas Boehler      return false;
59455a741c0SAndreas Boehler  }
595a1a3b679SAndreas Boehler
596cb71a62aSAndreas Boehler  /**
597cb71a62aSAndreas Boehler   * Retrieve the calendar settings of a given calendar id
598cb71a62aSAndreas Boehler   *
599cb71a62aSAndreas Boehler   * @param string $calid The calendar ID
600cb71a62aSAndreas Boehler   *
601cb71a62aSAndreas Boehler   * @return array The calendar settings array
602cb71a62aSAndreas Boehler   */
603b269830cSAndreas Boehler  public function getCalendarSettings($calid)
604b269830cSAndreas Boehler  {
605185e2535SAndreas Boehler      $query = "SELECT principaluri, calendarcolor, displayname, uri, description, components, transparent, synctoken FROM calendars WHERE id=".$this->sqlite->quote_string($calid);
606b269830cSAndreas Boehler      $res = $this->sqlite->query($query);
607b269830cSAndreas Boehler      $row = $this->sqlite->res2row($res);
608b269830cSAndreas Boehler      return $row;
609b269830cSAndreas Boehler  }
610b269830cSAndreas Boehler
611cb71a62aSAndreas Boehler  /**
612cb71a62aSAndreas Boehler   * Retrieve all events that are within a given date range,
613cb71a62aSAndreas Boehler   * based on the timezone setting.
614cb71a62aSAndreas Boehler   *
615cb71a62aSAndreas Boehler   * There is also support for retrieving recurring events,
616cb71a62aSAndreas Boehler   * using Sabre's VObject Iterator. Recurring events are represented
617cb71a62aSAndreas Boehler   * as individual calendar entries with the same UID.
618cb71a62aSAndreas Boehler   *
619cb71a62aSAndreas Boehler   * @param string $id The page ID to work with
620cb71a62aSAndreas Boehler   * @param string $user The user ID to work with
621cb71a62aSAndreas Boehler   * @param string $startDate The start date as a string
622cb71a62aSAndreas Boehler   * @param string $endDate The end date as a string
623cb71a62aSAndreas Boehler   *
624cb71a62aSAndreas Boehler   * @return array An array containing the calendar entries.
625cb71a62aSAndreas Boehler   */
62682a48dfbSAndreas Boehler  public function getEventsWithinDateRange($id, $user, $startDate, $endDate, $timezone)
627a1a3b679SAndreas Boehler  {
62882a48dfbSAndreas Boehler      if($timezone !== '' && $timezone !== 'local')
62982a48dfbSAndreas Boehler          $timezone = new \DateTimeZone($timezone);
630bd883736SAndreas Boehler      else
631bd883736SAndreas Boehler          $timezone = new \DateTimeZone('UTC');
632a1a3b679SAndreas Boehler      $data = array();
633cb71a62aSAndreas Boehler
634cb71a62aSAndreas Boehler      // Load SabreDAV
6359bef4ad8SAndreas Boehler      require_once(DOKU_PLUGIN.'davcal/vendor/autoload.php');
636a1a3b679SAndreas Boehler      $calid = $this->getCalendarIdForPage($id);
637185e2535SAndreas Boehler      $color = $this->getCalendarColorForCalendar($calid);
638a1a3b679SAndreas Boehler      $startTs = new \DateTime($startDate);
639a1a3b679SAndreas Boehler      $endTs = new \DateTime($endDate);
640cb71a62aSAndreas Boehler
641cb71a62aSAndreas Boehler      // Retrieve matching calendar objects
642ebc4eb57SAndreas Boehler      $query = "SELECT calendardata, componenttype, uid FROM calendarobjects WHERE calendarid=".
643ebc4eb57SAndreas Boehler                $this->sqlite->quote_string($calid)." AND firstoccurence < ".
644ebc4eb57SAndreas Boehler                $this->sqlite->quote_string($endTs->getTimestamp())." AND lastoccurence > ".
645ebc4eb57SAndreas Boehler                $this->sqlite->quote_string($startTs->getTimestamp());
646a1a3b679SAndreas Boehler      $res = $this->sqlite->query($query);
647a1a3b679SAndreas Boehler      $arr = $this->sqlite->res2arr($res);
648cb71a62aSAndreas Boehler
649cb71a62aSAndreas Boehler      // Parse individual calendar entries
650a1a3b679SAndreas Boehler      foreach($arr as $row)
651a1a3b679SAndreas Boehler      {
652a1a3b679SAndreas Boehler          if(isset($row['calendardata']))
653a1a3b679SAndreas Boehler          {
654b269830cSAndreas Boehler              $entry = array();
655a1a3b679SAndreas Boehler              $vcal = \Sabre\VObject\Reader::read($row['calendardata']);
656ebc4eb57SAndreas Boehler              $recurrence = $vcal->VEVENT->RRULE;
657cb71a62aSAndreas Boehler              // If it is a recurring event, pass it through Sabre's EventIterator
658ebc4eb57SAndreas Boehler              if($recurrence != null)
659ebc4eb57SAndreas Boehler              {
660ebc4eb57SAndreas Boehler                  $rEvents = new \Sabre\VObject\Recur\EventIterator(array($vcal->VEVENT));
661ebc4eb57SAndreas Boehler                  $rEvents->rewind();
662ebc4eb57SAndreas Boehler                  $done = false;
663ebc4eb57SAndreas Boehler                  while($rEvents->valid() && !$done)
664ebc4eb57SAndreas Boehler                  {
665ebc4eb57SAndreas Boehler                      $event = $rEvents->getEventObject();
666cb71a62aSAndreas Boehler                      // If we are after the given time range, exit
667ebc4eb57SAndreas Boehler                      if(($rEvents->getDtStart()->getTimestamp() > $endTs->getTimestamp()) &&
668ebc4eb57SAndreas Boehler                         ($rEvents->getDtEnd()->getTimestamp() > $endTs->getTimestamp()))
669ebc4eb57SAndreas Boehler                        $done = true;
670cb71a62aSAndreas Boehler
671cb71a62aSAndreas Boehler                      // If we are before the given time range, continue
672ebc4eb57SAndreas Boehler                      if($rEvents->getDtEnd()->getTimestamp() < $startTs->getTimestamp())
673ebc4eb57SAndreas Boehler                      {
674ebc4eb57SAndreas Boehler                          $rEvents->next();
675ebc4eb57SAndreas Boehler                          continue;
676ebc4eb57SAndreas Boehler                      }
677cb71a62aSAndreas Boehler
678cb71a62aSAndreas Boehler                      // If we are within the given time range, parse the event
679185e2535SAndreas Boehler                      $data[] = $this->convertIcalDataToEntry($event, $id, $timezone, $row['uid'], $color, true);
680ebc4eb57SAndreas Boehler                      $rEvents->next();
681ebc4eb57SAndreas Boehler                  }
682ebc4eb57SAndreas Boehler              }
683ebc4eb57SAndreas Boehler              else
684185e2535SAndreas Boehler                $data[] = $this->convertIcalDataToEntry($vcal->VEVENT, $id, $timezone, $row['uid'], $color);
685ebc4eb57SAndreas Boehler          }
686ebc4eb57SAndreas Boehler      }
687ebc4eb57SAndreas Boehler      return $data;
688ebc4eb57SAndreas Boehler  }
689ebc4eb57SAndreas Boehler
690cb71a62aSAndreas Boehler  /**
691cb71a62aSAndreas Boehler   * Helper function that parses the iCal data of a VEVENT to a calendar entry.
692cb71a62aSAndreas Boehler   *
693cb71a62aSAndreas Boehler   * @param \Sabre\VObject\VEvent $event The event to parse
694cb71a62aSAndreas Boehler   * @param \DateTimeZone $timezone The timezone object
695cb71a62aSAndreas Boehler   * @param string $uid The entry's UID
6963c86dda8SAndreas Boehler   * @param boolean $recurring (optional) Set to true to define a recurring event
697cb71a62aSAndreas Boehler   *
698cb71a62aSAndreas Boehler   * @return array The parse calendar entry
699cb71a62aSAndreas Boehler   */
700185e2535SAndreas Boehler  private function convertIcalDataToEntry($event, $page, $timezone, $uid, $color, $recurring = false)
701ebc4eb57SAndreas Boehler  {
702ebc4eb57SAndreas Boehler      $entry = array();
703ebc4eb57SAndreas Boehler      $start = $event->DTSTART;
704cb71a62aSAndreas Boehler      // Parse only if the start date/time is present
705b269830cSAndreas Boehler      if($start !== null)
706b269830cSAndreas Boehler      {
707b269830cSAndreas Boehler        $dtStart = $start->getDateTime();
708b269830cSAndreas Boehler        $dtStart->setTimezone($timezone);
709b269830cSAndreas Boehler        $entry['start'] = $dtStart->format(\DateTime::ATOM);
710b269830cSAndreas Boehler        if($start['VALUE'] == 'DATE')
711b269830cSAndreas Boehler          $entry['allDay'] = true;
712b269830cSAndreas Boehler        else
713b269830cSAndreas Boehler          $entry['allDay'] = false;
714b269830cSAndreas Boehler      }
715ebc4eb57SAndreas Boehler      $end = $event->DTEND;
716cb71a62aSAndreas Boehler      // Parse onlyl if the end date/time is present
717b269830cSAndreas Boehler      if($end !== null)
718b269830cSAndreas Boehler      {
719b269830cSAndreas Boehler        $dtEnd = $end->getDateTime();
720b269830cSAndreas Boehler        $dtEnd->setTimezone($timezone);
721b269830cSAndreas Boehler        $entry['end'] = $dtEnd->format(\DateTime::ATOM);
722b269830cSAndreas Boehler      }
723ebc4eb57SAndreas Boehler      $description = $event->DESCRIPTION;
7240eebc909SAndreas Boehler      if($description !== null)
7250eebc909SAndreas Boehler        $entry['description'] = (string)$description;
7260eebc909SAndreas Boehler      else
7270eebc909SAndreas Boehler        $entry['description'] = '';
7284ecb526cSAndreas Boehler      $attachments = $event->ATTACH;
7294ecb526cSAndreas Boehler      if($attachments !== null)
7304ecb526cSAndreas Boehler      {
7314ecb526cSAndreas Boehler        $entry['attachments'] = array();
7324ecb526cSAndreas Boehler        foreach($attachments as $attachment)
7334ecb526cSAndreas Boehler          $entry['attachments'][] = (string)$attachment;
7344ecb526cSAndreas Boehler      }
735ebc4eb57SAndreas Boehler      $entry['title'] = (string)$event->summary;
736ebc4eb57SAndreas Boehler      $entry['id'] = $uid;
737185e2535SAndreas Boehler      $entry['page'] = $page;
738185e2535SAndreas Boehler      $entry['color'] = $color;
7393c86dda8SAndreas Boehler      $entry['recurring'] = $recurring;
740185e2535SAndreas Boehler
741ebc4eb57SAndreas Boehler      return $entry;
742a1a3b679SAndreas Boehler  }
743a1a3b679SAndreas Boehler
744cb71a62aSAndreas Boehler  /**
745cb71a62aSAndreas Boehler   * Retrieve an event by its UID
746cb71a62aSAndreas Boehler   *
747cb71a62aSAndreas Boehler   * @param string $uid The event's UID
748cb71a62aSAndreas Boehler   *
749cb71a62aSAndreas Boehler   * @return mixed The table row with the given event
750cb71a62aSAndreas Boehler   */
751a1a3b679SAndreas Boehler  public function getEventWithUid($uid)
752a1a3b679SAndreas Boehler  {
75355a741c0SAndreas Boehler      $query = "SELECT calendardata, calendarid, componenttype, uri FROM calendarobjects WHERE uid=".
754a1a3b679SAndreas Boehler                $this->sqlite->quote_string($uid);
755a1a3b679SAndreas Boehler      $res = $this->sqlite->query($query);
756a1a3b679SAndreas Boehler      $row = $this->sqlite->res2row($res);
757a1a3b679SAndreas Boehler      return $row;
758a1a3b679SAndreas Boehler  }
759a1a3b679SAndreas Boehler
760cb71a62aSAndreas Boehler  /**
761cb71a62aSAndreas Boehler   * Retrieve all calendar events for a given calendar ID
762cb71a62aSAndreas Boehler   *
763cb71a62aSAndreas Boehler   * @param string $calid The calendar's ID
764cb71a62aSAndreas Boehler   *
765cb71a62aSAndreas Boehler   * @return array An array containing all calendar data
766cb71a62aSAndreas Boehler   */
767f69bb449SAndreas Boehler  public function getAllCalendarEvents($calid)
768f69bb449SAndreas Boehler  {
769f69bb449SAndreas Boehler      $query = "SELECT calendardata, uid, componenttype, uri FROM calendarobjects WHERE calendarid=".
770f69bb449SAndreas Boehler               $this->sqlite->quote_string($calid);
771f69bb449SAndreas Boehler      $res = $this->sqlite->query($query);
772f69bb449SAndreas Boehler      $arr = $this->sqlite->res2arr($res);
773f69bb449SAndreas Boehler      return $arr;
774f69bb449SAndreas Boehler  }
775f69bb449SAndreas Boehler
776cb71a62aSAndreas Boehler  /**
777cb71a62aSAndreas Boehler   * Edit a calendar entry for a page, given by its parameters.
778cb71a62aSAndreas Boehler   * The params array has the same format as @see addCalendarEntryForPage
779cb71a62aSAndreas Boehler   *
780cb71a62aSAndreas Boehler   * @param string $id The page's ID to work on
781cb71a62aSAndreas Boehler   * @param string $user The user's ID to work on
782cb71a62aSAndreas Boehler   * @param array $params The parameter array for the edited calendar event
783cb71a62aSAndreas Boehler   *
784cb71a62aSAndreas Boehler   * @return boolean True on success, otherwise false
785cb71a62aSAndreas Boehler   */
786a1a3b679SAndreas Boehler  public function editCalendarEntryForPage($id, $user, $params)
787a1a3b679SAndreas Boehler  {
78882a48dfbSAndreas Boehler      if($params['currenttz'] !== '' && $params['currenttz'] !== 'local')
78982a48dfbSAndreas Boehler          $timezone = new \DateTimeZone($params['currenttz']);
79082a48dfbSAndreas Boehler      elseif($params['currenttz'] === 'local')
791a25c89eaSAndreas Boehler          $timezone = new \DateTimeZone($params['detectedtz']);
792bd883736SAndreas Boehler      else
793bd883736SAndreas Boehler          $timezone = new \DateTimeZone('UTC');
794cb71a62aSAndreas Boehler
795cb71a62aSAndreas Boehler      // Parse dates
796b269830cSAndreas Boehler      $startDate = explode('-', $params['eventfrom']);
797b269830cSAndreas Boehler      $startTime = explode(':', $params['eventfromtime']);
798b269830cSAndreas Boehler      $endDate = explode('-', $params['eventto']);
799b269830cSAndreas Boehler      $endTime = explode(':', $params['eventtotime']);
800cb71a62aSAndreas Boehler
801cb71a62aSAndreas Boehler      // Retrieve the existing event based on the UID
80255a741c0SAndreas Boehler      $uid = $params['uid'];
80355a741c0SAndreas Boehler      $event = $this->getEventWithUid($uid);
804cb71a62aSAndreas Boehler
805cb71a62aSAndreas Boehler      // Load SabreDAV
8069bef4ad8SAndreas Boehler      require_once(DOKU_PLUGIN.'davcal/vendor/autoload.php');
807a1a3b679SAndreas Boehler      if(!isset($event['calendardata']))
808a1a3b679SAndreas Boehler        return false;
80955a741c0SAndreas Boehler      $uri = $event['uri'];
81055a741c0SAndreas Boehler      $calid = $event['calendarid'];
811cb71a62aSAndreas Boehler
812cb71a62aSAndreas Boehler      // Parse the existing event
813a1a3b679SAndreas Boehler      $vcal = \Sabre\VObject\Reader::read($event['calendardata']);
814b269830cSAndreas Boehler      $vevent = $vcal->VEVENT;
815cb71a62aSAndreas Boehler
816cb71a62aSAndreas Boehler      // Set the new event values
817b269830cSAndreas Boehler      $vevent->summary = $params['eventname'];
818b269830cSAndreas Boehler      $dtStamp = new \DateTime(null, new \DateTimeZone('UTC'));
8190eebc909SAndreas Boehler      $description = $params['eventdescription'];
820cb71a62aSAndreas Boehler
821cb71a62aSAndreas Boehler      // Remove existing timestamps to overwrite them
8220eebc909SAndreas Boehler      $vevent->remove('DESCRIPTION');
823b269830cSAndreas Boehler      $vevent->remove('DTSTAMP');
824b269830cSAndreas Boehler      $vevent->remove('LAST-MODIFIED');
8254ecb526cSAndreas Boehler      $vevent->remove('ATTACH');
826cb71a62aSAndreas Boehler
827cb71a62aSAndreas Boehler      // Add new time stamps and description
828b269830cSAndreas Boehler      $vevent->add('DTSTAMP', $dtStamp);
829b269830cSAndreas Boehler      $vevent->add('LAST-MODIFIED', $dtStamp);
8300eebc909SAndreas Boehler      if($description !== '')
8310eebc909SAndreas Boehler        $vevent->add('DESCRIPTION', $description);
832cb71a62aSAndreas Boehler
8334ecb526cSAndreas Boehler      // Add attachments
8344ecb526cSAndreas Boehler      $attachments = $params['attachments'];
83582a48dfbSAndreas Boehler      if(!is_null($attachments))
8364ecb526cSAndreas Boehler        foreach($attachments as $attachment)
8374ecb526cSAndreas Boehler          $vevent->add('ATTACH', $attachment);
8384ecb526cSAndreas Boehler
839cb71a62aSAndreas Boehler      // Setup DTSTART
840b269830cSAndreas Boehler      $dtStart = new \DateTime();
841a25c89eaSAndreas Boehler      $dtStart->setTimezone($timezone);
842b269830cSAndreas Boehler      $dtStart->setDate(intval($startDate[0]), intval($startDate[1]), intval($startDate[2]));
843b269830cSAndreas Boehler      if($params['allday'] != '1')
844b269830cSAndreas Boehler        $dtStart->setTime(intval($startTime[0]), intval($startTime[1]), 0);
845cb71a62aSAndreas Boehler
8464ecb526cSAndreas Boehler      // Setup DTEND
847b269830cSAndreas Boehler      $dtEnd = new \DateTime();
848a25c89eaSAndreas Boehler      $dtEnd->setTimezone($timezone);
849b269830cSAndreas Boehler      $dtEnd->setDate(intval($endDate[0]), intval($endDate[1]), intval($endDate[2]));
850b269830cSAndreas Boehler      if($params['allday'] != '1')
851b269830cSAndreas Boehler        $dtEnd->setTime(intval($endTime[0]), intval($endTime[1]), 0);
852cb71a62aSAndreas Boehler
853b269830cSAndreas Boehler      // According to the VCal spec, we need to add a whole day here
854b269830cSAndreas Boehler      if($params['allday'] == '1')
855b269830cSAndreas Boehler          $dtEnd->add(new \DateInterval('P1D'));
856b269830cSAndreas Boehler      $vevent->remove('DTSTART');
857b269830cSAndreas Boehler      $vevent->remove('DTEND');
858b269830cSAndreas Boehler      $dtStartEv = $vevent->add('DTSTART', $dtStart);
859b269830cSAndreas Boehler      $dtEndEv = $vevent->add('DTEND', $dtEnd);
860cb71a62aSAndreas Boehler
861cb71a62aSAndreas Boehler      // Remove the time for allday events
862b269830cSAndreas Boehler      if($params['allday'] == '1')
863b269830cSAndreas Boehler      {
864b269830cSAndreas Boehler          $dtStartEv['VALUE'] = 'DATE';
865b269830cSAndreas Boehler          $dtEndEv['VALUE'] = 'DATE';
866b269830cSAndreas Boehler      }
867a1a3b679SAndreas Boehler      $now = new DateTime();
868a1a3b679SAndreas Boehler      $eventStr = $vcal->serialize();
869a1a3b679SAndreas Boehler
870cb71a62aSAndreas Boehler      // Actually write to the database
871a1a3b679SAndreas Boehler      $query = "UPDATE calendarobjects SET calendardata=".$this->sqlite->quote_string($eventStr).
872a1a3b679SAndreas Boehler               ", lastmodified=".$this->sqlite->quote_string($now->getTimestamp()).
873a1a3b679SAndreas Boehler               ", firstoccurence=".$this->sqlite->quote_string($dtStart->getTimestamp()).
874a1a3b679SAndreas Boehler               ", lastoccurence=".$this->sqlite->quote_string($dtEnd->getTimestamp()).
875a1a3b679SAndreas Boehler               ", size=".strlen($eventStr).
876a1a3b679SAndreas Boehler               ", etag=".$this->sqlite->quote_string(md5($eventStr)).
87755a741c0SAndreas Boehler               " WHERE uid=".$this->sqlite->quote_string($uid);
878a1a3b679SAndreas Boehler      $res = $this->sqlite->query($query);
87955a741c0SAndreas Boehler      if($res !== false)
88055a741c0SAndreas Boehler      {
88155a741c0SAndreas Boehler          $this->updateSyncTokenLog($calid, $uri, 'modified');
882a1a3b679SAndreas Boehler          return true;
883a1a3b679SAndreas Boehler      }
88455a741c0SAndreas Boehler      return false;
88555a741c0SAndreas Boehler  }
886a1a3b679SAndreas Boehler
887cb71a62aSAndreas Boehler  /**
888cb71a62aSAndreas Boehler   * Delete a calendar entry for a given page. Actually, the event is removed
889cb71a62aSAndreas Boehler   * based on the entry's UID, so that page ID is no used.
890cb71a62aSAndreas Boehler   *
891cb71a62aSAndreas Boehler   * @param string $id The page's ID (unused)
892cb71a62aSAndreas Boehler   * @param array $params The parameter array to work with
893cb71a62aSAndreas Boehler   *
894cb71a62aSAndreas Boehler   * @return boolean True
895cb71a62aSAndreas Boehler   */
896a1a3b679SAndreas Boehler  public function deleteCalendarEntryForPage($id, $params)
897a1a3b679SAndreas Boehler  {
898a1a3b679SAndreas Boehler      $uid = $params['uid'];
89955a741c0SAndreas Boehler      $event = $this->getEventWithUid($uid);
9002c14b82bSAndreas Boehler      $calid = $event['calendarid'];
90155a741c0SAndreas Boehler      $uri = $event['uri'];
902a1a3b679SAndreas Boehler      $query = "DELETE FROM calendarobjects WHERE uid=".$this->sqlite->quote_string($uid);
903a1a3b679SAndreas Boehler      $res = $this->sqlite->query($query);
90455a741c0SAndreas Boehler      if($res !== false)
90555a741c0SAndreas Boehler      {
90655a741c0SAndreas Boehler          $this->updateSyncTokenLog($calid, $uri, 'deleted');
90755a741c0SAndreas Boehler      }
908a1a3b679SAndreas Boehler      return true;
909a1a3b679SAndreas Boehler  }
910a1a3b679SAndreas Boehler
911cb71a62aSAndreas Boehler  /**
912cb71a62aSAndreas Boehler   * Retrieve the current sync token for a calendar
913cb71a62aSAndreas Boehler   *
914cb71a62aSAndreas Boehler   * @param string $calid The calendar id
915cb71a62aSAndreas Boehler   *
916cb71a62aSAndreas Boehler   * @return mixed The synctoken or false
917cb71a62aSAndreas Boehler   */
91855a741c0SAndreas Boehler  public function getSyncTokenForCalendar($calid)
91955a741c0SAndreas Boehler  {
920b269830cSAndreas Boehler      $row = $this->getCalendarSettings($calid);
92155a741c0SAndreas Boehler      if(isset($row['synctoken']))
92255a741c0SAndreas Boehler          return $row['synctoken'];
92355a741c0SAndreas Boehler      return false;
92455a741c0SAndreas Boehler  }
92555a741c0SAndreas Boehler
926cb71a62aSAndreas Boehler  /**
927cb71a62aSAndreas Boehler   * Helper function to convert the operation name to
928cb71a62aSAndreas Boehler   * an operation code as stored in the database
929cb71a62aSAndreas Boehler   *
930cb71a62aSAndreas Boehler   * @param string $operationName The operation name
931cb71a62aSAndreas Boehler   *
932cb71a62aSAndreas Boehler   * @return mixed The operation code or false
933cb71a62aSAndreas Boehler   */
93455a741c0SAndreas Boehler  public function operationNameToOperation($operationName)
93555a741c0SAndreas Boehler  {
93655a741c0SAndreas Boehler      switch($operationName)
93755a741c0SAndreas Boehler      {
93855a741c0SAndreas Boehler          case 'added':
93955a741c0SAndreas Boehler              return 1;
94055a741c0SAndreas Boehler          break;
94155a741c0SAndreas Boehler          case 'modified':
94255a741c0SAndreas Boehler              return 2;
94355a741c0SAndreas Boehler          break;
94455a741c0SAndreas Boehler          case 'deleted':
94555a741c0SAndreas Boehler              return 3;
94655a741c0SAndreas Boehler          break;
94755a741c0SAndreas Boehler      }
94855a741c0SAndreas Boehler      return false;
94955a741c0SAndreas Boehler  }
95055a741c0SAndreas Boehler
951cb71a62aSAndreas Boehler  /**
952cb71a62aSAndreas Boehler   * Update the sync token log based on the calendar id and the
953cb71a62aSAndreas Boehler   * operation that was performed.
954cb71a62aSAndreas Boehler   *
955cb71a62aSAndreas Boehler   * @param string $calid The calendar ID that was modified
956cb71a62aSAndreas Boehler   * @param string $uri The calendar URI that was modified
957cb71a62aSAndreas Boehler   * @param string $operation The operation that was performed
958cb71a62aSAndreas Boehler   *
959cb71a62aSAndreas Boehler   * @return boolean True on success, otherwise false
960cb71a62aSAndreas Boehler   */
96155a741c0SAndreas Boehler  private function updateSyncTokenLog($calid, $uri, $operation)
96255a741c0SAndreas Boehler  {
96355a741c0SAndreas Boehler      $currentToken = $this->getSyncTokenForCalendar($calid);
96455a741c0SAndreas Boehler      $operationCode = $this->operationNameToOperation($operation);
96555a741c0SAndreas Boehler      if(($operationCode === false) || ($currentToken === false))
96655a741c0SAndreas Boehler          return false;
96755a741c0SAndreas Boehler      $values = array($uri,
96855a741c0SAndreas Boehler                      $currentToken,
96955a741c0SAndreas Boehler                      $calid,
97055a741c0SAndreas Boehler                      $operationCode
97155a741c0SAndreas Boehler      );
97255a741c0SAndreas Boehler      $query = "INSERT INTO calendarchanges (uri, synctoken, calendarid, operation) VALUES(".
97355a741c0SAndreas Boehler               $this->sqlite->quote_and_join($values, ',').")";
97455a741c0SAndreas Boehler      $res = $this->sqlite->query($query);
97555a741c0SAndreas Boehler      if($res === false)
97655a741c0SAndreas Boehler        return false;
97755a741c0SAndreas Boehler      $currentToken++;
97855a741c0SAndreas Boehler      $query = "UPDATE calendars SET synctoken=".$this->sqlite->quote_string($currentToken)." WHERE id=".
97955a741c0SAndreas Boehler               $this->sqlite->quote_string($calid);
98055a741c0SAndreas Boehler      $res = $this->sqlite->query($query);
98155a741c0SAndreas Boehler      return ($res !== false);
98255a741c0SAndreas Boehler  }
98355a741c0SAndreas Boehler
984cb71a62aSAndreas Boehler  /**
985cb71a62aSAndreas Boehler   * Return the sync URL for a given Page, i.e. a calendar
986cb71a62aSAndreas Boehler   *
987cb71a62aSAndreas Boehler   * @param string $id The page's ID
988cb71a62aSAndreas Boehler   * @param string $user (optional) The user's ID
989cb71a62aSAndreas Boehler   *
990cb71a62aSAndreas Boehler   * @return mixed The sync url or false
991cb71a62aSAndreas Boehler   */
992b269830cSAndreas Boehler  public function getSyncUrlForPage($id, $user = null)
993b269830cSAndreas Boehler  {
99434a47953SAndreas Boehler      if(is_null($userid))
99534a47953SAndreas Boehler      {
99634a47953SAndreas Boehler        if(isset($_SERVER['REMOTE_USER']) && !is_null($_SERVER['REMOTE_USER']))
99734a47953SAndreas Boehler        {
99834a47953SAndreas Boehler          $userid = $_SERVER['REMOTE_USER'];
99934a47953SAndreas Boehler        }
100034a47953SAndreas Boehler        else
100134a47953SAndreas Boehler        {
100234a47953SAndreas Boehler          return false;
100334a47953SAndreas Boehler        }
100434a47953SAndreas Boehler      }
1005b269830cSAndreas Boehler
1006b269830cSAndreas Boehler      $calid = $this->getCalendarIdForPage($id);
1007b269830cSAndreas Boehler      if($calid === false)
1008b269830cSAndreas Boehler        return false;
1009b269830cSAndreas Boehler
1010b269830cSAndreas Boehler      $calsettings = $this->getCalendarSettings($calid);
1011b269830cSAndreas Boehler      if(!isset($calsettings['uri']))
1012b269830cSAndreas Boehler        return false;
1013b269830cSAndreas Boehler
1014b269830cSAndreas Boehler      $syncurl = DOKU_URL.'lib/plugins/davcal/calendarserver.php/calendars/'.$user.'/'.$calsettings['uri'];
1015b269830cSAndreas Boehler      return $syncurl;
1016b269830cSAndreas Boehler  }
1017b269830cSAndreas Boehler
1018cb71a62aSAndreas Boehler  /**
1019cb71a62aSAndreas Boehler   * Return the private calendar's URL for a given page
1020cb71a62aSAndreas Boehler   *
1021cb71a62aSAndreas Boehler   * @param string $id the page ID
1022cb71a62aSAndreas Boehler   *
1023cb71a62aSAndreas Boehler   * @return mixed The private URL or false
1024cb71a62aSAndreas Boehler   */
1025f69bb449SAndreas Boehler  public function getPrivateURLForPage($id)
1026f69bb449SAndreas Boehler  {
1027f69bb449SAndreas Boehler      $calid = $this->getCalendarIdForPage($id);
1028f69bb449SAndreas Boehler      if($calid === false)
1029f69bb449SAndreas Boehler        return false;
1030f69bb449SAndreas Boehler
1031f69bb449SAndreas Boehler      return $this->getPrivateURLForCalendar($calid);
1032f69bb449SAndreas Boehler  }
1033f69bb449SAndreas Boehler
1034cb71a62aSAndreas Boehler  /**
1035cb71a62aSAndreas Boehler   * Return the private calendar's URL for a given calendar ID
1036cb71a62aSAndreas Boehler   *
1037cb71a62aSAndreas Boehler   * @param string $calid The calendar's ID
1038cb71a62aSAndreas Boehler   *
1039cb71a62aSAndreas Boehler   * @return mixed The private URL or false
1040cb71a62aSAndreas Boehler   */
1041f69bb449SAndreas Boehler  public function getPrivateURLForCalendar($calid)
1042f69bb449SAndreas Boehler  {
1043185e2535SAndreas Boehler      if(isset($this->cachedValues['privateurl'][$calid]))
1044185e2535SAndreas Boehler        return $this->cachedValues['privateurl'][$calid];
1045f69bb449SAndreas Boehler      $query = "SELECT url FROM calendartoprivateurlmapping WHERE calid=".$this->sqlite->quote_string($calid);
1046f69bb449SAndreas Boehler      $res = $this->sqlite->query($query);
1047f69bb449SAndreas Boehler      $row = $this->sqlite->res2row($res);
1048f69bb449SAndreas Boehler      if(!isset($row['url']))
1049f69bb449SAndreas Boehler      {
1050f69bb449SAndreas Boehler          $url = uniqid("dokuwiki-").".ics";
1051f69bb449SAndreas Boehler          $values = array(
1052f69bb449SAndreas Boehler                $url,
1053f69bb449SAndreas Boehler                $calid
1054f69bb449SAndreas Boehler          );
1055f69bb449SAndreas Boehler          $query = "INSERT INTO calendartoprivateurlmapping (url, calid) VALUES(".
1056f69bb449SAndreas Boehler                $this->sqlite->quote_and_join($values, ", ").")";
1057f69bb449SAndreas Boehler          $res = $this->sqlite->query($query);
1058f69bb449SAndreas Boehler          if($res === false)
1059f69bb449SAndreas Boehler            return false;
1060f69bb449SAndreas Boehler      }
1061f69bb449SAndreas Boehler      else
1062f69bb449SAndreas Boehler      {
1063f69bb449SAndreas Boehler          $url = $row['url'];
1064f69bb449SAndreas Boehler      }
1065185e2535SAndreas Boehler
1066185e2535SAndreas Boehler      $url = DOKU_URL.'lib/plugins/davcal/ics.php/'.$url;
1067185e2535SAndreas Boehler      $this->cachedValues['privateurl'][$calid] = $url;
1068185e2535SAndreas Boehler      return $url;
1069f69bb449SAndreas Boehler  }
1070f69bb449SAndreas Boehler
1071cb71a62aSAndreas Boehler  /**
1072cb71a62aSAndreas Boehler   * Retrieve the calendar ID for a given private calendar URL
1073cb71a62aSAndreas Boehler   *
1074cb71a62aSAndreas Boehler   * @param string $url The private URL
1075cb71a62aSAndreas Boehler   *
1076cb71a62aSAndreas Boehler   * @return mixed The calendar ID or false
1077cb71a62aSAndreas Boehler   */
1078f69bb449SAndreas Boehler  public function getCalendarForPrivateURL($url)
1079f69bb449SAndreas Boehler  {
1080f69bb449SAndreas Boehler      $query = "SELECT calid FROM calendartoprivateurlmapping WHERE url=".$this->sqlite->quote_string($url);
1081f69bb449SAndreas Boehler      $res = $this->sqlite->query($query);
1082f69bb449SAndreas Boehler      $row = $this->sqlite->res2row($res);
1083f69bb449SAndreas Boehler      if(!isset($row['calid']))
1084f69bb449SAndreas Boehler        return false;
1085f69bb449SAndreas Boehler      return $row['calid'];
1086f69bb449SAndreas Boehler  }
1087f69bb449SAndreas Boehler
1088cb71a62aSAndreas Boehler  /**
1089cb71a62aSAndreas Boehler   * Return a given calendar as ICS feed, i.e. all events in one ICS file.
1090cb71a62aSAndreas Boehler   *
1091cb71a62aSAndreas Boehler   * @param string $caldi The calendar ID to retrieve
1092cb71a62aSAndreas Boehler   *
1093cb71a62aSAndreas Boehler   * @return mixed The calendar events as string or false
1094cb71a62aSAndreas Boehler   */
1095f69bb449SAndreas Boehler  public function getCalendarAsICSFeed($calid)
1096f69bb449SAndreas Boehler  {
1097f69bb449SAndreas Boehler      $calSettings = $this->getCalendarSettings($calid);
1098f69bb449SAndreas Boehler      if($calSettings === false)
1099f69bb449SAndreas Boehler        return false;
1100f69bb449SAndreas Boehler      $events = $this->getAllCalendarEvents($calid);
1101f69bb449SAndreas Boehler      if($events === false)
1102f69bb449SAndreas Boehler        return false;
1103f69bb449SAndreas Boehler
1104f69bb449SAndreas Boehler      $out = "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//DAVCal//DAVCal for DokuWiki//EN\nCALSCALE:GREGORIAN\nX-WR-CALNAME:";
1105f69bb449SAndreas Boehler      $out .= $calSettings['displayname']."\n";
1106f69bb449SAndreas Boehler      foreach($events as $event)
1107f69bb449SAndreas Boehler      {
1108f69bb449SAndreas Boehler          $out .= rtrim($event['calendardata']);
1109f69bb449SAndreas Boehler          $out .= "\n";
1110f69bb449SAndreas Boehler      }
1111f69bb449SAndreas Boehler      $out .= "END:VCALENDAR\n";
1112f69bb449SAndreas Boehler      return $out;
1113f69bb449SAndreas Boehler  }
1114f69bb449SAndreas Boehler
11157c7c6b0bSAndreas Boehler  /**
11167c7c6b0bSAndreas Boehler   * Retrieve a configuration option for the plugin
11177c7c6b0bSAndreas Boehler   *
11187c7c6b0bSAndreas Boehler   * @param string $key The key to query
111921d04f73SAndreas Boehler   * @return mixed The option set, null if not found
11207c7c6b0bSAndreas Boehler   */
11217c7c6b0bSAndreas Boehler  public function getConfig($key)
11227c7c6b0bSAndreas Boehler  {
11237c7c6b0bSAndreas Boehler      return $this->getConf($key);
11247c7c6b0bSAndreas Boehler  }
11257c7c6b0bSAndreas Boehler
1126a1a3b679SAndreas Boehler}
1127