xref: /plugin/davcal/helper.php (revision e86c8dd3b7d861e975adf1d4642cdbd30e06f157)
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  /**
171*e86c8dd3SAndreas Boehler   * Get the user's principal URL for iOS sync
172*e86c8dd3SAndreas Boehler   * @param string $user the user name
173*e86c8dd3SAndreas Boehler   * @return the URL to the principal sync
174*e86c8dd3SAndreas Boehler   */
175*e86c8dd3SAndreas Boehler  public function getPrincipalUrlForUser($user)
176*e86c8dd3SAndreas Boehler  {
177*e86c8dd3SAndreas Boehler      if(is_null($user))
178*e86c8dd3SAndreas Boehler        return false;
179*e86c8dd3SAndreas Boehler      $url = DOKU_URL.'lib/plugins/davcal/calendarserver.php/principals/'.$user;
180*e86c8dd3SAndreas Boehler      return $url;
181*e86c8dd3SAndreas Boehler  }
182*e86c8dd3SAndreas Boehler
183*e86c8dd3SAndreas Boehler  /**
184185e2535SAndreas Boehler   * Set the calendar color for a given page.
185185e2535SAndreas Boehler   *
186185e2535SAndreas Boehler   * @param string $color The color definition
187185e2535SAndreas Boehler   * @param string $id optional The page ID
188185e2535SAndreas Boehler   * @return boolean True on success, otherwise false
189185e2535SAndreas Boehler   */
190185e2535SAndreas Boehler  public function setCalendarColorForPage($color, $id = null)
191185e2535SAndreas Boehler  {
192185e2535SAndreas Boehler      if(is_null($id))
193185e2535SAndreas Boehler      {
194185e2535SAndreas Boehler          global $ID;
195185e2535SAndreas Boehler          $id = $ID;
196185e2535SAndreas Boehler      }
197185e2535SAndreas Boehler      $calid = $this->getCalendarIdForPage($id);
198185e2535SAndreas Boehler      if($calid === false)
199185e2535SAndreas Boehler        return false;
200185e2535SAndreas Boehler
201185e2535SAndreas Boehler      $query = "UPDATE calendars SET calendarcolor=".$this->sqlite->quote_string($color).
202185e2535SAndreas Boehler               " WHERE id=".$this->sqlite->quote_string($calid);
203185e2535SAndreas Boehler      $res = $this->sqlite->query($query);
204185e2535SAndreas Boehler      if($res !== false)
205185e2535SAndreas Boehler      {
206185e2535SAndreas Boehler        $this->cachedValues['calendarcolor'][$calid] = $color;
207185e2535SAndreas Boehler        return true;
208185e2535SAndreas Boehler      }
209185e2535SAndreas Boehler      return false;
210185e2535SAndreas Boehler  }
211185e2535SAndreas Boehler
212185e2535SAndreas Boehler  /**
213cb71a62aSAndreas Boehler   * Set the calendar name and description for a given page with a given
214cb71a62aSAndreas Boehler   * page id.
215cb71a62aSAndreas Boehler   * If the calendar doesn't exist, the calendar is created!
216cb71a62aSAndreas Boehler   *
217cb71a62aSAndreas Boehler   * @param string  $name The name of the new calendar
218cb71a62aSAndreas Boehler   * @param string  $description The description of the new calendar
219cb71a62aSAndreas Boehler   * @param string  $id (optional) The ID of the page
220cb71a62aSAndreas Boehler   * @param string  $userid The userid of the creating user
221cb71a62aSAndreas Boehler   *
222cb71a62aSAndreas Boehler   * @return boolean True on success, otherwise false.
223cb71a62aSAndreas Boehler   */
224a1a3b679SAndreas Boehler  public function setCalendarNameForPage($name, $description, $id = null, $userid = null)
225a1a3b679SAndreas Boehler  {
226a1a3b679SAndreas Boehler      if(is_null($id))
227a1a3b679SAndreas Boehler      {
228a1a3b679SAndreas Boehler          global $ID;
229a1a3b679SAndreas Boehler          $id = $ID;
230a1a3b679SAndreas Boehler      }
231a1a3b679SAndreas Boehler      if(is_null($userid))
23234a47953SAndreas Boehler      {
23334a47953SAndreas Boehler        if(isset($_SERVER['REMOTE_USER']) && !is_null($_SERVER['REMOTE_USER']))
23434a47953SAndreas Boehler        {
235a1a3b679SAndreas Boehler          $userid = $_SERVER['REMOTE_USER'];
23634a47953SAndreas Boehler        }
23734a47953SAndreas Boehler        else
23834a47953SAndreas Boehler        {
23934a47953SAndreas Boehler          $userid = uniqid('davcal-');
24034a47953SAndreas Boehler        }
24134a47953SAndreas Boehler      }
242a1a3b679SAndreas Boehler      $calid = $this->getCalendarIdForPage($id);
243a1a3b679SAndreas Boehler      if($calid === false)
244a1a3b679SAndreas Boehler        return $this->createCalendarForPage($name, $description, $id, $userid);
245a1a3b679SAndreas Boehler
246b269830cSAndreas Boehler      $query = "UPDATE calendars SET displayname=".$this->sqlite->quote_string($name).", ".
247b269830cSAndreas Boehler               "description=".$this->sqlite->quote_string($description)." WHERE ".
248b269830cSAndreas Boehler               "id=".$this->sqlite->quote_string($calid);
249b269830cSAndreas Boehler      $res = $this->sqlite->query($query);
250b269830cSAndreas Boehler      if($res !== false)
251b269830cSAndreas Boehler        return true;
252b269830cSAndreas Boehler      return false;
253a1a3b679SAndreas Boehler  }
254a1a3b679SAndreas Boehler
255cb71a62aSAndreas Boehler  /**
256cb71a62aSAndreas Boehler   * Save the personal settings to the SQLite database 'calendarsettings'.
257cb71a62aSAndreas Boehler   *
258cb71a62aSAndreas Boehler   * @param array  $settings The settings array to store
259cb71a62aSAndreas Boehler   * @param string $userid (optional) The userid to store
260cb71a62aSAndreas Boehler   *
261cb71a62aSAndreas Boehler   * @param boolean True on success, otherwise false
262cb71a62aSAndreas Boehler   */
263a495d34cSAndreas Boehler  public function savePersonalSettings($settings, $userid = null)
264a495d34cSAndreas Boehler  {
265a495d34cSAndreas Boehler      if(is_null($userid))
26634a47953SAndreas Boehler      {
26734a47953SAndreas Boehler          if(isset($_SERVER['REMOTE_USER']) && !is_null($_SERVER['REMOTE_USER']))
26834a47953SAndreas Boehler          {
269a495d34cSAndreas Boehler            $userid = $_SERVER['REMOTE_USER'];
27034a47953SAndreas Boehler          }
27134a47953SAndreas Boehler          else
27234a47953SAndreas Boehler          {
27334a47953SAndreas Boehler              return false;
27434a47953SAndreas Boehler          }
27534a47953SAndreas Boehler      }
276a495d34cSAndreas Boehler      $this->sqlite->query("BEGIN TRANSACTION");
277a495d34cSAndreas Boehler
278bd883736SAndreas Boehler      $query = "DELETE FROM calendarsettings WHERE userid=".$this->sqlite->quote_string($userid);
279bd883736SAndreas Boehler      $this->sqlite->query($query);
280bd883736SAndreas Boehler
281a495d34cSAndreas Boehler      foreach($settings as $key => $value)
282a495d34cSAndreas Boehler      {
283bd883736SAndreas Boehler          $query = "INSERT INTO calendarsettings (userid, key, value) VALUES (".
284a495d34cSAndreas Boehler                   $this->sqlite->quote_string($userid).", ".
285a495d34cSAndreas Boehler                   $this->sqlite->quote_string($key).", ".
286a495d34cSAndreas Boehler                   $this->sqlite->quote_string($value).")";
287a495d34cSAndreas Boehler          $res = $this->sqlite->query($query);
288a495d34cSAndreas Boehler          if($res === false)
289a495d34cSAndreas Boehler              return false;
290a495d34cSAndreas Boehler      }
291a495d34cSAndreas Boehler      $this->sqlite->query("COMMIT TRANSACTION");
292185e2535SAndreas Boehler      $this->cachedValues['settings'][$userid] = $settings;
293a495d34cSAndreas Boehler      return true;
294a495d34cSAndreas Boehler  }
295a495d34cSAndreas Boehler
296cb71a62aSAndreas Boehler  /**
297cb71a62aSAndreas Boehler   * Retrieve the settings array for a given user id.
298cb71a62aSAndreas Boehler   * Some sane defaults are returned, currently:
299cb71a62aSAndreas Boehler   *
300cb71a62aSAndreas Boehler   *    timezone    => local
301cb71a62aSAndreas Boehler   *    weeknumbers => 0
302cb71a62aSAndreas Boehler   *    workweek    => 0
303cb71a62aSAndreas Boehler   *
304cb71a62aSAndreas Boehler   * @param string $userid (optional) The user id to retrieve
305cb71a62aSAndreas Boehler   *
306cb71a62aSAndreas Boehler   * @return array The settings array
307cb71a62aSAndreas Boehler   */
308a495d34cSAndreas Boehler  public function getPersonalSettings($userid = null)
309a495d34cSAndreas Boehler  {
310bd883736SAndreas Boehler      // Some sane default settings
311bd883736SAndreas Boehler      $settings = array(
312fb813b30SAndreas Boehler        'timezone' => $this->getConf('timezone'),
313fb813b30SAndreas Boehler        'weeknumbers' => $this->getConf('weeknumbers'),
314fb813b30SAndreas Boehler        'workweek' => $this->getConf('workweek'),
3151d5bdcd0SAndreas Boehler        'monday' => $this->getConf('monday'),
3161d5bdcd0SAndreas Boehler        'timeformat' => $this->getConf('timeformat')
317bd883736SAndreas Boehler      );
31834a47953SAndreas Boehler      if(is_null($userid))
31934a47953SAndreas Boehler      {
32034a47953SAndreas Boehler          if(isset($_SERVER['REMOTE_USER']) && !is_null($_SERVER['REMOTE_USER']))
32134a47953SAndreas Boehler          {
32234a47953SAndreas Boehler            $userid = $_SERVER['REMOTE_USER'];
32334a47953SAndreas Boehler          }
32434a47953SAndreas Boehler          else
32534a47953SAndreas Boehler          {
32634a47953SAndreas Boehler            return $settings;
32734a47953SAndreas Boehler          }
32834a47953SAndreas Boehler      }
32934a47953SAndreas Boehler
33034a47953SAndreas Boehler      if(isset($this->cachedValues['settings'][$userid]))
33134a47953SAndreas Boehler        return $this->cachedValues['settings'][$userid];
332a495d34cSAndreas Boehler      $query = "SELECT key, value FROM calendarsettings WHERE userid=".$this->sqlite->quote_string($userid);
333a495d34cSAndreas Boehler      $res = $this->sqlite->query($query);
334a495d34cSAndreas Boehler      $arr = $this->sqlite->res2arr($res);
335a495d34cSAndreas Boehler      foreach($arr as $row)
336a495d34cSAndreas Boehler      {
337a495d34cSAndreas Boehler          $settings[$row['key']] = $row['value'];
338a495d34cSAndreas Boehler      }
339185e2535SAndreas Boehler      $this->cachedValues['settings'][$userid] = $settings;
340a495d34cSAndreas Boehler      return $settings;
341a495d34cSAndreas Boehler  }
342a495d34cSAndreas Boehler
343cb71a62aSAndreas Boehler  /**
344cb71a62aSAndreas Boehler   * Retrieve the calendar ID based on a page ID from the SQLite table
345cb71a62aSAndreas Boehler   * 'pagetocalendarmapping'.
346cb71a62aSAndreas Boehler   *
347cb71a62aSAndreas Boehler   * @param string $id (optional) The page ID to retrieve the corresponding calendar
348cb71a62aSAndreas Boehler   *
349cb71a62aSAndreas Boehler   * @return mixed the ID on success, otherwise false
350cb71a62aSAndreas Boehler   */
351a1a3b679SAndreas Boehler  public function getCalendarIdForPage($id = null)
352a1a3b679SAndreas Boehler  {
353a1a3b679SAndreas Boehler      if(is_null($id))
354a1a3b679SAndreas Boehler      {
355a1a3b679SAndreas Boehler          global $ID;
356a1a3b679SAndreas Boehler          $id = $ID;
357a1a3b679SAndreas Boehler      }
358a1a3b679SAndreas Boehler
359185e2535SAndreas Boehler      if(isset($this->cachedValues['calid'][$id]))
360185e2535SAndreas Boehler        return $this->cachedValues['calid'][$id];
361185e2535SAndreas Boehler
362a1a3b679SAndreas Boehler      $query = "SELECT calid FROM pagetocalendarmapping WHERE page=".$this->sqlite->quote_string($id);
363a1a3b679SAndreas Boehler      $res = $this->sqlite->query($query);
364a1a3b679SAndreas Boehler      $row = $this->sqlite->res2row($res);
365a1a3b679SAndreas Boehler      if(isset($row['calid']))
366185e2535SAndreas Boehler      {
367185e2535SAndreas Boehler        $calid = $row['calid'];
368185e2535SAndreas Boehler        $this->cachedValues['calid'] = $calid;
369185e2535SAndreas Boehler        return $calid;
370185e2535SAndreas Boehler      }
371a1a3b679SAndreas Boehler      return false;
372a1a3b679SAndreas Boehler  }
373a1a3b679SAndreas Boehler
374cb71a62aSAndreas Boehler  /**
375cb71a62aSAndreas Boehler   * Retrieve the complete calendar id to page mapping.
376cb71a62aSAndreas Boehler   * This is necessary to be able to retrieve a list of
377cb71a62aSAndreas Boehler   * calendars for a given user and check the access rights.
378cb71a62aSAndreas Boehler   *
379cb71a62aSAndreas Boehler   * @return array The mapping array
380cb71a62aSAndreas Boehler   */
381a1a3b679SAndreas Boehler  public function getCalendarIdToPageMapping()
382a1a3b679SAndreas Boehler  {
383a1a3b679SAndreas Boehler      $query = "SELECT calid, page FROM pagetocalendarmapping";
384a1a3b679SAndreas Boehler      $res = $this->sqlite->query($query);
385a1a3b679SAndreas Boehler      $arr = $this->sqlite->res2arr($res);
386a1a3b679SAndreas Boehler      return $arr;
387a1a3b679SAndreas Boehler  }
388a1a3b679SAndreas Boehler
389cb71a62aSAndreas Boehler  /**
390cb71a62aSAndreas Boehler   * Retrieve all calendar IDs a given user has access to.
391cb71a62aSAndreas Boehler   * The user is specified by the principalUri, so the
392cb71a62aSAndreas Boehler   * user name is actually split from the URI component.
393cb71a62aSAndreas Boehler   *
394cb71a62aSAndreas Boehler   * Access rights are checked against DokuWiki's ACL
395cb71a62aSAndreas Boehler   * and applied accordingly.
396cb71a62aSAndreas Boehler   *
397cb71a62aSAndreas Boehler   * @param string $principalUri The principal URI to work on
398cb71a62aSAndreas Boehler   *
399cb71a62aSAndreas Boehler   * @return array An associative array of calendar IDs
400cb71a62aSAndreas Boehler   */
401a1a3b679SAndreas Boehler  public function getCalendarIdsForUser($principalUri)
402a1a3b679SAndreas Boehler  {
40334a47953SAndreas Boehler      global $auth;
404a1a3b679SAndreas Boehler      $user = explode('/', $principalUri);
405a1a3b679SAndreas Boehler      $user = end($user);
406a1a3b679SAndreas Boehler      $mapping = $this->getCalendarIdToPageMapping();
407a1a3b679SAndreas Boehler      $calids = array();
40834a47953SAndreas Boehler      $ud = $auth->getUserData($user);
40934a47953SAndreas Boehler      $groups = $ud['grps'];
410a1a3b679SAndreas Boehler      foreach($mapping as $row)
411a1a3b679SAndreas Boehler      {
412a1a3b679SAndreas Boehler          $id = $row['calid'];
413a1a3b679SAndreas Boehler          $page = $row['page'];
41434a47953SAndreas Boehler          $acl = auth_aclcheck($page, $user, $groups);
415a1a3b679SAndreas Boehler          if($acl >= AUTH_READ)
416a1a3b679SAndreas Boehler          {
417a1a3b679SAndreas Boehler              $write = $acl > AUTH_READ;
418a1a3b679SAndreas Boehler              $calids[$id] = array('readonly' => !$write);
419a1a3b679SAndreas Boehler          }
420a1a3b679SAndreas Boehler      }
421a1a3b679SAndreas Boehler      return $calids;
422a1a3b679SAndreas Boehler  }
423a1a3b679SAndreas Boehler
424cb71a62aSAndreas Boehler  /**
425cb71a62aSAndreas Boehler   * Create a new calendar for a given page ID and set name and description
426cb71a62aSAndreas Boehler   * accordingly. Also update the pagetocalendarmapping table on success.
427cb71a62aSAndreas Boehler   *
428cb71a62aSAndreas Boehler   * @param string $name The calendar's name
429cb71a62aSAndreas Boehler   * @param string $description The calendar's description
430cb71a62aSAndreas Boehler   * @param string $id (optional) The page ID to work on
431cb71a62aSAndreas Boehler   * @param string $userid (optional) The user ID that created the calendar
432cb71a62aSAndreas Boehler   *
433cb71a62aSAndreas Boehler   * @return boolean True on success, otherwise false
434cb71a62aSAndreas Boehler   */
435a1a3b679SAndreas Boehler  public function createCalendarForPage($name, $description, $id = null, $userid = null)
436a1a3b679SAndreas Boehler  {
437a1a3b679SAndreas Boehler      if(is_null($id))
438a1a3b679SAndreas Boehler      {
439a1a3b679SAndreas Boehler          global $ID;
440a1a3b679SAndreas Boehler          $id = $ID;
441a1a3b679SAndreas Boehler      }
442a1a3b679SAndreas Boehler      if(is_null($userid))
44334a47953SAndreas Boehler      {
44434a47953SAndreas Boehler        if(isset($_SERVER['REMOTE_USER']) && !is_null($_SERVER['REMOTE_USER']))
44534a47953SAndreas Boehler        {
446a1a3b679SAndreas Boehler          $userid = $_SERVER['REMOTE_USER'];
44734a47953SAndreas Boehler        }
44834a47953SAndreas Boehler        else
44934a47953SAndreas Boehler        {
45034a47953SAndreas Boehler          $userid = uniqid('davcal-');
45134a47953SAndreas Boehler        }
45234a47953SAndreas Boehler      }
453a1a3b679SAndreas Boehler      $values = array('principals/'.$userid,
454a1a3b679SAndreas Boehler                      $name,
455a1a3b679SAndreas Boehler                      str_replace(array('/', ' ', ':'), '_', $id),
456a1a3b679SAndreas Boehler                      $description,
457a1a3b679SAndreas Boehler                      'VEVENT,VTODO',
45855a741c0SAndreas Boehler                      0,
45955a741c0SAndreas Boehler                      1);
46055a741c0SAndreas Boehler      $query = "INSERT INTO calendars (principaluri, displayname, uri, description, components, transparent, synctoken) VALUES (".$this->sqlite->quote_and_join($values, ',').");";
461a1a3b679SAndreas Boehler      $res = $this->sqlite->query($query);
46255a741c0SAndreas Boehler      if($res === false)
46355a741c0SAndreas Boehler        return false;
464cb71a62aSAndreas Boehler
465cb71a62aSAndreas Boehler      // Get the new calendar ID
466a1a3b679SAndreas Boehler      $query = "SELECT id FROM calendars WHERE principaluri=".$this->sqlite->quote_string($values[0])." AND ".
467a1a3b679SAndreas Boehler               "displayname=".$this->sqlite->quote_string($values[1])." AND ".
468a1a3b679SAndreas Boehler               "uri=".$this->sqlite->quote_string($values[2])." AND ".
469a1a3b679SAndreas Boehler               "description=".$this->sqlite->quote_string($values[3]);
470a1a3b679SAndreas Boehler      $res = $this->sqlite->query($query);
471a1a3b679SAndreas Boehler      $row = $this->sqlite->res2row($res);
472cb71a62aSAndreas Boehler
473cb71a62aSAndreas Boehler      // Update the pagetocalendarmapping table with the new calendar ID
474a1a3b679SAndreas Boehler      if(isset($row['id']))
475a1a3b679SAndreas Boehler      {
476a1a3b679SAndreas Boehler          $values = array($id, $row['id']);
477a1a3b679SAndreas Boehler          $query = "INSERT INTO pagetocalendarmapping (page, calid) VALUES (".$this->sqlite->quote_and_join($values, ',').")";
478a1a3b679SAndreas Boehler          $res = $this->sqlite->query($query);
47955a741c0SAndreas Boehler          return ($res !== false);
480a1a3b679SAndreas Boehler      }
481a1a3b679SAndreas Boehler
482a1a3b679SAndreas Boehler      return false;
483a1a3b679SAndreas Boehler  }
484a1a3b679SAndreas Boehler
485cb71a62aSAndreas Boehler  /**
486cb71a62aSAndreas Boehler   * Add a new iCal entry for a given page, i.e. a given calendar.
487cb71a62aSAndreas Boehler   *
488cb71a62aSAndreas Boehler   * The parameter array needs to contain
489cb71a62aSAndreas Boehler   *   detectedtz       => The timezone as detected by the browser
49082a48dfbSAndreas Boehler   *   currenttz        => The timezone in use by the calendar
491cb71a62aSAndreas Boehler   *   eventfrom        => The event's start date
492cb71a62aSAndreas Boehler   *   eventfromtime    => The event's start time
493cb71a62aSAndreas Boehler   *   eventto          => The event's end date
494cb71a62aSAndreas Boehler   *   eventtotime      => The event's end time
495cb71a62aSAndreas Boehler   *   eventname        => The event's name
496cb71a62aSAndreas Boehler   *   eventdescription => The event's description
497cb71a62aSAndreas Boehler   *
498cb71a62aSAndreas Boehler   * @param string $id The page ID to work on
499cb71a62aSAndreas Boehler   * @param string $user The user who created the calendar
500cb71a62aSAndreas Boehler   * @param string $params A parameter array with values to create
501cb71a62aSAndreas Boehler   *
502cb71a62aSAndreas Boehler   * @return boolean True on success, otherwise false
503cb71a62aSAndreas Boehler   */
504a1a3b679SAndreas Boehler  public function addCalendarEntryToCalendarForPage($id, $user, $params)
505a1a3b679SAndreas Boehler  {
50682a48dfbSAndreas Boehler      if($params['currenttz'] !== '' && $params['currenttz'] !== 'local')
50782a48dfbSAndreas Boehler          $timezone = new \DateTimeZone($params['currenttz']);
50882a48dfbSAndreas Boehler      elseif($params['currenttz'] === 'local')
509a25c89eaSAndreas Boehler          $timezone = new \DateTimeZone($params['detectedtz']);
510bd883736SAndreas Boehler      else
511bd883736SAndreas Boehler          $timezone = new \DateTimeZone('UTC');
512cb71a62aSAndreas Boehler
513cb71a62aSAndreas Boehler      // Retrieve dates from settings
514b269830cSAndreas Boehler      $startDate = explode('-', $params['eventfrom']);
515b269830cSAndreas Boehler      $startTime = explode(':', $params['eventfromtime']);
516b269830cSAndreas Boehler      $endDate = explode('-', $params['eventto']);
517b269830cSAndreas Boehler      $endTime = explode(':', $params['eventtotime']);
518cb71a62aSAndreas Boehler
519cb71a62aSAndreas Boehler      // Load SabreDAV
5209bef4ad8SAndreas Boehler      require_once(DOKU_PLUGIN.'davcal/vendor/autoload.php');
521a1a3b679SAndreas Boehler      $vcalendar = new \Sabre\VObject\Component\VCalendar();
522cb71a62aSAndreas Boehler
523cb71a62aSAndreas Boehler      // Add VCalendar, UID and Event Name
524a1a3b679SAndreas Boehler      $event = $vcalendar->add('VEVENT');
525b269830cSAndreas Boehler      $uuid = \Sabre\VObject\UUIDUtil::getUUID();
526b269830cSAndreas Boehler      $event->add('UID', $uuid);
527a1a3b679SAndreas Boehler      $event->summary = $params['eventname'];
528cb71a62aSAndreas Boehler
529cb71a62aSAndreas Boehler      // Add a description if requested
5300eebc909SAndreas Boehler      $description = $params['eventdescription'];
5310eebc909SAndreas Boehler      if($description !== '')
5320eebc909SAndreas Boehler        $event->add('DESCRIPTION', $description);
533cb71a62aSAndreas Boehler
5344ecb526cSAndreas Boehler      // Add attachments
5354ecb526cSAndreas Boehler      $attachments = $params['attachments'];
53682a48dfbSAndreas Boehler      if(!is_null($attachments))
5374ecb526cSAndreas Boehler        foreach($attachments as $attachment)
5384ecb526cSAndreas Boehler          $event->add('ATTACH', $attachment);
5394ecb526cSAndreas Boehler
540cb71a62aSAndreas Boehler      // Create a timestamp for last modified, created and dtstamp values in UTC
541b269830cSAndreas Boehler      $dtStamp = new \DateTime(null, new \DateTimeZone('UTC'));
542b269830cSAndreas Boehler      $event->add('DTSTAMP', $dtStamp);
543b269830cSAndreas Boehler      $event->add('CREATED', $dtStamp);
544b269830cSAndreas Boehler      $event->add('LAST-MODIFIED', $dtStamp);
545cb71a62aSAndreas Boehler
546cb71a62aSAndreas Boehler      // Adjust the start date, based on the given timezone information
547b269830cSAndreas Boehler      $dtStart = new \DateTime();
548a25c89eaSAndreas Boehler      $dtStart->setTimezone($timezone);
549b269830cSAndreas Boehler      $dtStart->setDate(intval($startDate[0]), intval($startDate[1]), intval($startDate[2]));
550cb71a62aSAndreas Boehler
551cb71a62aSAndreas Boehler      // Only add the time values if it's not an allday event
552b269830cSAndreas Boehler      if($params['allday'] != '1')
553b269830cSAndreas Boehler        $dtStart->setTime(intval($startTime[0]), intval($startTime[1]), 0);
554cb71a62aSAndreas Boehler
555cb71a62aSAndreas Boehler      // Adjust the end date, based on the given timezone information
556b269830cSAndreas Boehler      $dtEnd = new \DateTime();
557a25c89eaSAndreas Boehler      $dtEnd->setTimezone($timezone);
558b269830cSAndreas Boehler      $dtEnd->setDate(intval($endDate[0]), intval($endDate[1]), intval($endDate[2]));
559cb71a62aSAndreas Boehler
560cb71a62aSAndreas Boehler      // Only add the time values if it's not an allday event
561b269830cSAndreas Boehler      if($params['allday'] != '1')
562b269830cSAndreas Boehler        $dtEnd->setTime(intval($endTime[0]), intval($endTime[1]), 0);
563cb71a62aSAndreas Boehler
564b269830cSAndreas Boehler      // According to the VCal spec, we need to add a whole day here
565b269830cSAndreas Boehler      if($params['allday'] == '1')
566b269830cSAndreas Boehler          $dtEnd->add(new \DateInterval('P1D'));
567cb71a62aSAndreas Boehler
568cb71a62aSAndreas Boehler      // Really add Start and End events
569b269830cSAndreas Boehler      $dtStartEv = $event->add('DTSTART', $dtStart);
570b269830cSAndreas Boehler      $dtEndEv = $event->add('DTEND', $dtEnd);
571cb71a62aSAndreas Boehler
572cb71a62aSAndreas Boehler      // Adjust the DATE format for allday events
573b269830cSAndreas Boehler      if($params['allday'] == '1')
574b269830cSAndreas Boehler      {
575b269830cSAndreas Boehler          $dtStartEv['VALUE'] = 'DATE';
576b269830cSAndreas Boehler          $dtEndEv['VALUE'] = 'DATE';
577b269830cSAndreas Boehler      }
578cb71a62aSAndreas Boehler
579cb71a62aSAndreas Boehler      // Actually add the values to the database
580a1a3b679SAndreas Boehler      $calid = $this->getCalendarIdForPage($id);
581a1a3b679SAndreas Boehler      $uri = uniqid('dokuwiki-').'.ics';
582a1a3b679SAndreas Boehler      $now = new DateTime();
583a1a3b679SAndreas Boehler      $eventStr = $vcalendar->serialize();
584a1a3b679SAndreas Boehler
585a1a3b679SAndreas Boehler      $values = array($calid,
586a1a3b679SAndreas Boehler                      $uri,
587a1a3b679SAndreas Boehler                      $eventStr,
588a1a3b679SAndreas Boehler                      $now->getTimestamp(),
589a1a3b679SAndreas Boehler                      'VEVENT',
590a1a3b679SAndreas Boehler                      $event->DTSTART->getDateTime()->getTimeStamp(),
591a1a3b679SAndreas Boehler                      $event->DTEND->getDateTime()->getTimeStamp(),
592a1a3b679SAndreas Boehler                      strlen($eventStr),
593a1a3b679SAndreas Boehler                      md5($eventStr),
594cb71a62aSAndreas Boehler                      $uuid
595a1a3b679SAndreas Boehler      );
596a1a3b679SAndreas Boehler
597a1a3b679SAndreas Boehler      $query = "INSERT INTO calendarobjects (calendarid, uri, calendardata, lastmodified, componenttype, firstoccurence, lastoccurence, size, etag, uid) VALUES (".$this->sqlite->quote_and_join($values, ',').")";
598a1a3b679SAndreas Boehler      $res = $this->sqlite->query($query);
599cb71a62aSAndreas Boehler
600cb71a62aSAndreas Boehler      // If successfully, update the sync token database
60155a741c0SAndreas Boehler      if($res !== false)
60255a741c0SAndreas Boehler      {
60355a741c0SAndreas Boehler          $this->updateSyncTokenLog($calid, $uri, 'added');
604a1a3b679SAndreas Boehler          return true;
605a1a3b679SAndreas Boehler      }
60655a741c0SAndreas Boehler      return false;
60755a741c0SAndreas Boehler  }
608a1a3b679SAndreas Boehler
609cb71a62aSAndreas Boehler  /**
610cb71a62aSAndreas Boehler   * Retrieve the calendar settings of a given calendar id
611cb71a62aSAndreas Boehler   *
612cb71a62aSAndreas Boehler   * @param string $calid The calendar ID
613cb71a62aSAndreas Boehler   *
614cb71a62aSAndreas Boehler   * @return array The calendar settings array
615cb71a62aSAndreas Boehler   */
616b269830cSAndreas Boehler  public function getCalendarSettings($calid)
617b269830cSAndreas Boehler  {
618185e2535SAndreas Boehler      $query = "SELECT principaluri, calendarcolor, displayname, uri, description, components, transparent, synctoken FROM calendars WHERE id=".$this->sqlite->quote_string($calid);
619b269830cSAndreas Boehler      $res = $this->sqlite->query($query);
620b269830cSAndreas Boehler      $row = $this->sqlite->res2row($res);
621b269830cSAndreas Boehler      return $row;
622b269830cSAndreas Boehler  }
623b269830cSAndreas Boehler
624cb71a62aSAndreas Boehler  /**
625cb71a62aSAndreas Boehler   * Retrieve all events that are within a given date range,
626cb71a62aSAndreas Boehler   * based on the timezone setting.
627cb71a62aSAndreas Boehler   *
628cb71a62aSAndreas Boehler   * There is also support for retrieving recurring events,
629cb71a62aSAndreas Boehler   * using Sabre's VObject Iterator. Recurring events are represented
630cb71a62aSAndreas Boehler   * as individual calendar entries with the same UID.
631cb71a62aSAndreas Boehler   *
632cb71a62aSAndreas Boehler   * @param string $id The page ID to work with
633cb71a62aSAndreas Boehler   * @param string $user The user ID to work with
634cb71a62aSAndreas Boehler   * @param string $startDate The start date as a string
635cb71a62aSAndreas Boehler   * @param string $endDate The end date as a string
636cb71a62aSAndreas Boehler   *
637cb71a62aSAndreas Boehler   * @return array An array containing the calendar entries.
638cb71a62aSAndreas Boehler   */
63982a48dfbSAndreas Boehler  public function getEventsWithinDateRange($id, $user, $startDate, $endDate, $timezone)
640a1a3b679SAndreas Boehler  {
64182a48dfbSAndreas Boehler      if($timezone !== '' && $timezone !== 'local')
64282a48dfbSAndreas Boehler          $timezone = new \DateTimeZone($timezone);
643bd883736SAndreas Boehler      else
644bd883736SAndreas Boehler          $timezone = new \DateTimeZone('UTC');
645a1a3b679SAndreas Boehler      $data = array();
646cb71a62aSAndreas Boehler
647cb71a62aSAndreas Boehler      // Load SabreDAV
6489bef4ad8SAndreas Boehler      require_once(DOKU_PLUGIN.'davcal/vendor/autoload.php');
649a1a3b679SAndreas Boehler      $calid = $this->getCalendarIdForPage($id);
650185e2535SAndreas Boehler      $color = $this->getCalendarColorForCalendar($calid);
651a1a3b679SAndreas Boehler      $startTs = new \DateTime($startDate);
652a1a3b679SAndreas Boehler      $endTs = new \DateTime($endDate);
653cb71a62aSAndreas Boehler
654cb71a62aSAndreas Boehler      // Retrieve matching calendar objects
655ebc4eb57SAndreas Boehler      $query = "SELECT calendardata, componenttype, uid FROM calendarobjects WHERE calendarid=".
656ebc4eb57SAndreas Boehler                $this->sqlite->quote_string($calid)." AND firstoccurence < ".
657ebc4eb57SAndreas Boehler                $this->sqlite->quote_string($endTs->getTimestamp())." AND lastoccurence > ".
658ebc4eb57SAndreas Boehler                $this->sqlite->quote_string($startTs->getTimestamp());
659a1a3b679SAndreas Boehler      $res = $this->sqlite->query($query);
660a1a3b679SAndreas Boehler      $arr = $this->sqlite->res2arr($res);
661cb71a62aSAndreas Boehler
662cb71a62aSAndreas Boehler      // Parse individual calendar entries
663a1a3b679SAndreas Boehler      foreach($arr as $row)
664a1a3b679SAndreas Boehler      {
665a1a3b679SAndreas Boehler          if(isset($row['calendardata']))
666a1a3b679SAndreas Boehler          {
667b269830cSAndreas Boehler              $entry = array();
668a1a3b679SAndreas Boehler              $vcal = \Sabre\VObject\Reader::read($row['calendardata']);
669ebc4eb57SAndreas Boehler              $recurrence = $vcal->VEVENT->RRULE;
670cb71a62aSAndreas Boehler              // If it is a recurring event, pass it through Sabre's EventIterator
671ebc4eb57SAndreas Boehler              if($recurrence != null)
672ebc4eb57SAndreas Boehler              {
673ebc4eb57SAndreas Boehler                  $rEvents = new \Sabre\VObject\Recur\EventIterator(array($vcal->VEVENT));
674ebc4eb57SAndreas Boehler                  $rEvents->rewind();
675ebc4eb57SAndreas Boehler                  $done = false;
676ebc4eb57SAndreas Boehler                  while($rEvents->valid() && !$done)
677ebc4eb57SAndreas Boehler                  {
678ebc4eb57SAndreas Boehler                      $event = $rEvents->getEventObject();
679cb71a62aSAndreas Boehler                      // If we are after the given time range, exit
680ebc4eb57SAndreas Boehler                      if(($rEvents->getDtStart()->getTimestamp() > $endTs->getTimestamp()) &&
681ebc4eb57SAndreas Boehler                         ($rEvents->getDtEnd()->getTimestamp() > $endTs->getTimestamp()))
682ebc4eb57SAndreas Boehler                        $done = true;
683cb71a62aSAndreas Boehler
684cb71a62aSAndreas Boehler                      // If we are before the given time range, continue
685ebc4eb57SAndreas Boehler                      if($rEvents->getDtEnd()->getTimestamp() < $startTs->getTimestamp())
686ebc4eb57SAndreas Boehler                      {
687ebc4eb57SAndreas Boehler                          $rEvents->next();
688ebc4eb57SAndreas Boehler                          continue;
689ebc4eb57SAndreas Boehler                      }
690cb71a62aSAndreas Boehler
691cb71a62aSAndreas Boehler                      // If we are within the given time range, parse the event
692185e2535SAndreas Boehler                      $data[] = $this->convertIcalDataToEntry($event, $id, $timezone, $row['uid'], $color, true);
693ebc4eb57SAndreas Boehler                      $rEvents->next();
694ebc4eb57SAndreas Boehler                  }
695ebc4eb57SAndreas Boehler              }
696ebc4eb57SAndreas Boehler              else
697185e2535SAndreas Boehler                $data[] = $this->convertIcalDataToEntry($vcal->VEVENT, $id, $timezone, $row['uid'], $color);
698ebc4eb57SAndreas Boehler          }
699ebc4eb57SAndreas Boehler      }
700ebc4eb57SAndreas Boehler      return $data;
701ebc4eb57SAndreas Boehler  }
702ebc4eb57SAndreas Boehler
703cb71a62aSAndreas Boehler  /**
704cb71a62aSAndreas Boehler   * Helper function that parses the iCal data of a VEVENT to a calendar entry.
705cb71a62aSAndreas Boehler   *
706cb71a62aSAndreas Boehler   * @param \Sabre\VObject\VEvent $event The event to parse
707cb71a62aSAndreas Boehler   * @param \DateTimeZone $timezone The timezone object
708cb71a62aSAndreas Boehler   * @param string $uid The entry's UID
7093c86dda8SAndreas Boehler   * @param boolean $recurring (optional) Set to true to define a recurring event
710cb71a62aSAndreas Boehler   *
711cb71a62aSAndreas Boehler   * @return array The parse calendar entry
712cb71a62aSAndreas Boehler   */
713185e2535SAndreas Boehler  private function convertIcalDataToEntry($event, $page, $timezone, $uid, $color, $recurring = false)
714ebc4eb57SAndreas Boehler  {
715ebc4eb57SAndreas Boehler      $entry = array();
716ebc4eb57SAndreas Boehler      $start = $event->DTSTART;
717cb71a62aSAndreas Boehler      // Parse only if the start date/time is present
718b269830cSAndreas Boehler      if($start !== null)
719b269830cSAndreas Boehler      {
720b269830cSAndreas Boehler        $dtStart = $start->getDateTime();
721b269830cSAndreas Boehler        $dtStart->setTimezone($timezone);
722b269830cSAndreas Boehler        $entry['start'] = $dtStart->format(\DateTime::ATOM);
723b269830cSAndreas Boehler        if($start['VALUE'] == 'DATE')
724b269830cSAndreas Boehler          $entry['allDay'] = true;
725b269830cSAndreas Boehler        else
726b269830cSAndreas Boehler          $entry['allDay'] = false;
727b269830cSAndreas Boehler      }
728ebc4eb57SAndreas Boehler      $end = $event->DTEND;
729cb71a62aSAndreas Boehler      // Parse onlyl if the end date/time is present
730b269830cSAndreas Boehler      if($end !== null)
731b269830cSAndreas Boehler      {
732b269830cSAndreas Boehler        $dtEnd = $end->getDateTime();
733b269830cSAndreas Boehler        $dtEnd->setTimezone($timezone);
734b269830cSAndreas Boehler        $entry['end'] = $dtEnd->format(\DateTime::ATOM);
735b269830cSAndreas Boehler      }
736ebc4eb57SAndreas Boehler      $description = $event->DESCRIPTION;
7370eebc909SAndreas Boehler      if($description !== null)
7380eebc909SAndreas Boehler        $entry['description'] = (string)$description;
7390eebc909SAndreas Boehler      else
7400eebc909SAndreas Boehler        $entry['description'] = '';
7414ecb526cSAndreas Boehler      $attachments = $event->ATTACH;
7424ecb526cSAndreas Boehler      if($attachments !== null)
7434ecb526cSAndreas Boehler      {
7444ecb526cSAndreas Boehler        $entry['attachments'] = array();
7454ecb526cSAndreas Boehler        foreach($attachments as $attachment)
7464ecb526cSAndreas Boehler          $entry['attachments'][] = (string)$attachment;
7474ecb526cSAndreas Boehler      }
748ebc4eb57SAndreas Boehler      $entry['title'] = (string)$event->summary;
749ebc4eb57SAndreas Boehler      $entry['id'] = $uid;
750185e2535SAndreas Boehler      $entry['page'] = $page;
751185e2535SAndreas Boehler      $entry['color'] = $color;
7523c86dda8SAndreas Boehler      $entry['recurring'] = $recurring;
753185e2535SAndreas Boehler
754ebc4eb57SAndreas Boehler      return $entry;
755a1a3b679SAndreas Boehler  }
756a1a3b679SAndreas Boehler
757cb71a62aSAndreas Boehler  /**
758cb71a62aSAndreas Boehler   * Retrieve an event by its UID
759cb71a62aSAndreas Boehler   *
760cb71a62aSAndreas Boehler   * @param string $uid The event's UID
761cb71a62aSAndreas Boehler   *
762cb71a62aSAndreas Boehler   * @return mixed The table row with the given event
763cb71a62aSAndreas Boehler   */
764a1a3b679SAndreas Boehler  public function getEventWithUid($uid)
765a1a3b679SAndreas Boehler  {
76655a741c0SAndreas Boehler      $query = "SELECT calendardata, calendarid, componenttype, uri FROM calendarobjects WHERE uid=".
767a1a3b679SAndreas Boehler                $this->sqlite->quote_string($uid);
768a1a3b679SAndreas Boehler      $res = $this->sqlite->query($query);
769a1a3b679SAndreas Boehler      $row = $this->sqlite->res2row($res);
770a1a3b679SAndreas Boehler      return $row;
771a1a3b679SAndreas Boehler  }
772a1a3b679SAndreas Boehler
773cb71a62aSAndreas Boehler  /**
774cb71a62aSAndreas Boehler   * Retrieve all calendar events for a given calendar ID
775cb71a62aSAndreas Boehler   *
776cb71a62aSAndreas Boehler   * @param string $calid The calendar's ID
777cb71a62aSAndreas Boehler   *
778cb71a62aSAndreas Boehler   * @return array An array containing all calendar data
779cb71a62aSAndreas Boehler   */
780f69bb449SAndreas Boehler  public function getAllCalendarEvents($calid)
781f69bb449SAndreas Boehler  {
782f69bb449SAndreas Boehler      $query = "SELECT calendardata, uid, componenttype, uri FROM calendarobjects WHERE calendarid=".
783f69bb449SAndreas Boehler               $this->sqlite->quote_string($calid);
784f69bb449SAndreas Boehler      $res = $this->sqlite->query($query);
785f69bb449SAndreas Boehler      $arr = $this->sqlite->res2arr($res);
786f69bb449SAndreas Boehler      return $arr;
787f69bb449SAndreas Boehler  }
788f69bb449SAndreas Boehler
789cb71a62aSAndreas Boehler  /**
790cb71a62aSAndreas Boehler   * Edit a calendar entry for a page, given by its parameters.
791cb71a62aSAndreas Boehler   * The params array has the same format as @see addCalendarEntryForPage
792cb71a62aSAndreas Boehler   *
793cb71a62aSAndreas Boehler   * @param string $id The page's ID to work on
794cb71a62aSAndreas Boehler   * @param string $user The user's ID to work on
795cb71a62aSAndreas Boehler   * @param array $params The parameter array for the edited calendar event
796cb71a62aSAndreas Boehler   *
797cb71a62aSAndreas Boehler   * @return boolean True on success, otherwise false
798cb71a62aSAndreas Boehler   */
799a1a3b679SAndreas Boehler  public function editCalendarEntryForPage($id, $user, $params)
800a1a3b679SAndreas Boehler  {
80182a48dfbSAndreas Boehler      if($params['currenttz'] !== '' && $params['currenttz'] !== 'local')
80282a48dfbSAndreas Boehler          $timezone = new \DateTimeZone($params['currenttz']);
80382a48dfbSAndreas Boehler      elseif($params['currenttz'] === 'local')
804a25c89eaSAndreas Boehler          $timezone = new \DateTimeZone($params['detectedtz']);
805bd883736SAndreas Boehler      else
806bd883736SAndreas Boehler          $timezone = new \DateTimeZone('UTC');
807cb71a62aSAndreas Boehler
808cb71a62aSAndreas Boehler      // Parse dates
809b269830cSAndreas Boehler      $startDate = explode('-', $params['eventfrom']);
810b269830cSAndreas Boehler      $startTime = explode(':', $params['eventfromtime']);
811b269830cSAndreas Boehler      $endDate = explode('-', $params['eventto']);
812b269830cSAndreas Boehler      $endTime = explode(':', $params['eventtotime']);
813cb71a62aSAndreas Boehler
814cb71a62aSAndreas Boehler      // Retrieve the existing event based on the UID
81555a741c0SAndreas Boehler      $uid = $params['uid'];
81655a741c0SAndreas Boehler      $event = $this->getEventWithUid($uid);
817cb71a62aSAndreas Boehler
818cb71a62aSAndreas Boehler      // Load SabreDAV
8199bef4ad8SAndreas Boehler      require_once(DOKU_PLUGIN.'davcal/vendor/autoload.php');
820a1a3b679SAndreas Boehler      if(!isset($event['calendardata']))
821a1a3b679SAndreas Boehler        return false;
82255a741c0SAndreas Boehler      $uri = $event['uri'];
82355a741c0SAndreas Boehler      $calid = $event['calendarid'];
824cb71a62aSAndreas Boehler
825cb71a62aSAndreas Boehler      // Parse the existing event
826a1a3b679SAndreas Boehler      $vcal = \Sabre\VObject\Reader::read($event['calendardata']);
827b269830cSAndreas Boehler      $vevent = $vcal->VEVENT;
828cb71a62aSAndreas Boehler
829cb71a62aSAndreas Boehler      // Set the new event values
830b269830cSAndreas Boehler      $vevent->summary = $params['eventname'];
831b269830cSAndreas Boehler      $dtStamp = new \DateTime(null, new \DateTimeZone('UTC'));
8320eebc909SAndreas Boehler      $description = $params['eventdescription'];
833cb71a62aSAndreas Boehler
834cb71a62aSAndreas Boehler      // Remove existing timestamps to overwrite them
8350eebc909SAndreas Boehler      $vevent->remove('DESCRIPTION');
836b269830cSAndreas Boehler      $vevent->remove('DTSTAMP');
837b269830cSAndreas Boehler      $vevent->remove('LAST-MODIFIED');
8384ecb526cSAndreas Boehler      $vevent->remove('ATTACH');
839cb71a62aSAndreas Boehler
840cb71a62aSAndreas Boehler      // Add new time stamps and description
841b269830cSAndreas Boehler      $vevent->add('DTSTAMP', $dtStamp);
842b269830cSAndreas Boehler      $vevent->add('LAST-MODIFIED', $dtStamp);
8430eebc909SAndreas Boehler      if($description !== '')
8440eebc909SAndreas Boehler        $vevent->add('DESCRIPTION', $description);
845cb71a62aSAndreas Boehler
8464ecb526cSAndreas Boehler      // Add attachments
8474ecb526cSAndreas Boehler      $attachments = $params['attachments'];
84882a48dfbSAndreas Boehler      if(!is_null($attachments))
8494ecb526cSAndreas Boehler        foreach($attachments as $attachment)
8504ecb526cSAndreas Boehler          $vevent->add('ATTACH', $attachment);
8514ecb526cSAndreas Boehler
852cb71a62aSAndreas Boehler      // Setup DTSTART
853b269830cSAndreas Boehler      $dtStart = new \DateTime();
854a25c89eaSAndreas Boehler      $dtStart->setTimezone($timezone);
855b269830cSAndreas Boehler      $dtStart->setDate(intval($startDate[0]), intval($startDate[1]), intval($startDate[2]));
856b269830cSAndreas Boehler      if($params['allday'] != '1')
857b269830cSAndreas Boehler        $dtStart->setTime(intval($startTime[0]), intval($startTime[1]), 0);
858cb71a62aSAndreas Boehler
8594ecb526cSAndreas Boehler      // Setup DTEND
860b269830cSAndreas Boehler      $dtEnd = new \DateTime();
861a25c89eaSAndreas Boehler      $dtEnd->setTimezone($timezone);
862b269830cSAndreas Boehler      $dtEnd->setDate(intval($endDate[0]), intval($endDate[1]), intval($endDate[2]));
863b269830cSAndreas Boehler      if($params['allday'] != '1')
864b269830cSAndreas Boehler        $dtEnd->setTime(intval($endTime[0]), intval($endTime[1]), 0);
865cb71a62aSAndreas Boehler
866b269830cSAndreas Boehler      // According to the VCal spec, we need to add a whole day here
867b269830cSAndreas Boehler      if($params['allday'] == '1')
868b269830cSAndreas Boehler          $dtEnd->add(new \DateInterval('P1D'));
869b269830cSAndreas Boehler      $vevent->remove('DTSTART');
870b269830cSAndreas Boehler      $vevent->remove('DTEND');
871b269830cSAndreas Boehler      $dtStartEv = $vevent->add('DTSTART', $dtStart);
872b269830cSAndreas Boehler      $dtEndEv = $vevent->add('DTEND', $dtEnd);
873cb71a62aSAndreas Boehler
874cb71a62aSAndreas Boehler      // Remove the time for allday events
875b269830cSAndreas Boehler      if($params['allday'] == '1')
876b269830cSAndreas Boehler      {
877b269830cSAndreas Boehler          $dtStartEv['VALUE'] = 'DATE';
878b269830cSAndreas Boehler          $dtEndEv['VALUE'] = 'DATE';
879b269830cSAndreas Boehler      }
880a1a3b679SAndreas Boehler      $now = new DateTime();
881a1a3b679SAndreas Boehler      $eventStr = $vcal->serialize();
882a1a3b679SAndreas Boehler
883cb71a62aSAndreas Boehler      // Actually write to the database
884a1a3b679SAndreas Boehler      $query = "UPDATE calendarobjects SET calendardata=".$this->sqlite->quote_string($eventStr).
885a1a3b679SAndreas Boehler               ", lastmodified=".$this->sqlite->quote_string($now->getTimestamp()).
886a1a3b679SAndreas Boehler               ", firstoccurence=".$this->sqlite->quote_string($dtStart->getTimestamp()).
887a1a3b679SAndreas Boehler               ", lastoccurence=".$this->sqlite->quote_string($dtEnd->getTimestamp()).
888a1a3b679SAndreas Boehler               ", size=".strlen($eventStr).
889a1a3b679SAndreas Boehler               ", etag=".$this->sqlite->quote_string(md5($eventStr)).
89055a741c0SAndreas Boehler               " WHERE uid=".$this->sqlite->quote_string($uid);
891a1a3b679SAndreas Boehler      $res = $this->sqlite->query($query);
89255a741c0SAndreas Boehler      if($res !== false)
89355a741c0SAndreas Boehler      {
89455a741c0SAndreas Boehler          $this->updateSyncTokenLog($calid, $uri, 'modified');
895a1a3b679SAndreas Boehler          return true;
896a1a3b679SAndreas Boehler      }
89755a741c0SAndreas Boehler      return false;
89855a741c0SAndreas Boehler  }
899a1a3b679SAndreas Boehler
900cb71a62aSAndreas Boehler  /**
901cb71a62aSAndreas Boehler   * Delete a calendar entry for a given page. Actually, the event is removed
902cb71a62aSAndreas Boehler   * based on the entry's UID, so that page ID is no used.
903cb71a62aSAndreas Boehler   *
904cb71a62aSAndreas Boehler   * @param string $id The page's ID (unused)
905cb71a62aSAndreas Boehler   * @param array $params The parameter array to work with
906cb71a62aSAndreas Boehler   *
907cb71a62aSAndreas Boehler   * @return boolean True
908cb71a62aSAndreas Boehler   */
909a1a3b679SAndreas Boehler  public function deleteCalendarEntryForPage($id, $params)
910a1a3b679SAndreas Boehler  {
911a1a3b679SAndreas Boehler      $uid = $params['uid'];
91255a741c0SAndreas Boehler      $event = $this->getEventWithUid($uid);
9132c14b82bSAndreas Boehler      $calid = $event['calendarid'];
91455a741c0SAndreas Boehler      $uri = $event['uri'];
915a1a3b679SAndreas Boehler      $query = "DELETE FROM calendarobjects WHERE uid=".$this->sqlite->quote_string($uid);
916a1a3b679SAndreas Boehler      $res = $this->sqlite->query($query);
91755a741c0SAndreas Boehler      if($res !== false)
91855a741c0SAndreas Boehler      {
91955a741c0SAndreas Boehler          $this->updateSyncTokenLog($calid, $uri, 'deleted');
92055a741c0SAndreas Boehler      }
921a1a3b679SAndreas Boehler      return true;
922a1a3b679SAndreas Boehler  }
923a1a3b679SAndreas Boehler
924cb71a62aSAndreas Boehler  /**
925cb71a62aSAndreas Boehler   * Retrieve the current sync token for a calendar
926cb71a62aSAndreas Boehler   *
927cb71a62aSAndreas Boehler   * @param string $calid The calendar id
928cb71a62aSAndreas Boehler   *
929cb71a62aSAndreas Boehler   * @return mixed The synctoken or false
930cb71a62aSAndreas Boehler   */
93155a741c0SAndreas Boehler  public function getSyncTokenForCalendar($calid)
93255a741c0SAndreas Boehler  {
933b269830cSAndreas Boehler      $row = $this->getCalendarSettings($calid);
93455a741c0SAndreas Boehler      if(isset($row['synctoken']))
93555a741c0SAndreas Boehler          return $row['synctoken'];
93655a741c0SAndreas Boehler      return false;
93755a741c0SAndreas Boehler  }
93855a741c0SAndreas Boehler
939cb71a62aSAndreas Boehler  /**
940cb71a62aSAndreas Boehler   * Helper function to convert the operation name to
941cb71a62aSAndreas Boehler   * an operation code as stored in the database
942cb71a62aSAndreas Boehler   *
943cb71a62aSAndreas Boehler   * @param string $operationName The operation name
944cb71a62aSAndreas Boehler   *
945cb71a62aSAndreas Boehler   * @return mixed The operation code or false
946cb71a62aSAndreas Boehler   */
94755a741c0SAndreas Boehler  public function operationNameToOperation($operationName)
94855a741c0SAndreas Boehler  {
94955a741c0SAndreas Boehler      switch($operationName)
95055a741c0SAndreas Boehler      {
95155a741c0SAndreas Boehler          case 'added':
95255a741c0SAndreas Boehler              return 1;
95355a741c0SAndreas Boehler          break;
95455a741c0SAndreas Boehler          case 'modified':
95555a741c0SAndreas Boehler              return 2;
95655a741c0SAndreas Boehler          break;
95755a741c0SAndreas Boehler          case 'deleted':
95855a741c0SAndreas Boehler              return 3;
95955a741c0SAndreas Boehler          break;
96055a741c0SAndreas Boehler      }
96155a741c0SAndreas Boehler      return false;
96255a741c0SAndreas Boehler  }
96355a741c0SAndreas Boehler
964cb71a62aSAndreas Boehler  /**
965cb71a62aSAndreas Boehler   * Update the sync token log based on the calendar id and the
966cb71a62aSAndreas Boehler   * operation that was performed.
967cb71a62aSAndreas Boehler   *
968cb71a62aSAndreas Boehler   * @param string $calid The calendar ID that was modified
969cb71a62aSAndreas Boehler   * @param string $uri The calendar URI that was modified
970cb71a62aSAndreas Boehler   * @param string $operation The operation that was performed
971cb71a62aSAndreas Boehler   *
972cb71a62aSAndreas Boehler   * @return boolean True on success, otherwise false
973cb71a62aSAndreas Boehler   */
97455a741c0SAndreas Boehler  private function updateSyncTokenLog($calid, $uri, $operation)
97555a741c0SAndreas Boehler  {
97655a741c0SAndreas Boehler      $currentToken = $this->getSyncTokenForCalendar($calid);
97755a741c0SAndreas Boehler      $operationCode = $this->operationNameToOperation($operation);
97855a741c0SAndreas Boehler      if(($operationCode === false) || ($currentToken === false))
97955a741c0SAndreas Boehler          return false;
98055a741c0SAndreas Boehler      $values = array($uri,
98155a741c0SAndreas Boehler                      $currentToken,
98255a741c0SAndreas Boehler                      $calid,
98355a741c0SAndreas Boehler                      $operationCode
98455a741c0SAndreas Boehler      );
98555a741c0SAndreas Boehler      $query = "INSERT INTO calendarchanges (uri, synctoken, calendarid, operation) VALUES(".
98655a741c0SAndreas Boehler               $this->sqlite->quote_and_join($values, ',').")";
98755a741c0SAndreas Boehler      $res = $this->sqlite->query($query);
98855a741c0SAndreas Boehler      if($res === false)
98955a741c0SAndreas Boehler        return false;
99055a741c0SAndreas Boehler      $currentToken++;
99155a741c0SAndreas Boehler      $query = "UPDATE calendars SET synctoken=".$this->sqlite->quote_string($currentToken)." WHERE id=".
99255a741c0SAndreas Boehler               $this->sqlite->quote_string($calid);
99355a741c0SAndreas Boehler      $res = $this->sqlite->query($query);
99455a741c0SAndreas Boehler      return ($res !== false);
99555a741c0SAndreas Boehler  }
99655a741c0SAndreas Boehler
997cb71a62aSAndreas Boehler  /**
998cb71a62aSAndreas Boehler   * Return the sync URL for a given Page, i.e. a calendar
999cb71a62aSAndreas Boehler   *
1000cb71a62aSAndreas Boehler   * @param string $id The page's ID
1001cb71a62aSAndreas Boehler   * @param string $user (optional) The user's ID
1002cb71a62aSAndreas Boehler   *
1003cb71a62aSAndreas Boehler   * @return mixed The sync url or false
1004cb71a62aSAndreas Boehler   */
1005b269830cSAndreas Boehler  public function getSyncUrlForPage($id, $user = null)
1006b269830cSAndreas Boehler  {
100734a47953SAndreas Boehler      if(is_null($userid))
100834a47953SAndreas Boehler      {
100934a47953SAndreas Boehler        if(isset($_SERVER['REMOTE_USER']) && !is_null($_SERVER['REMOTE_USER']))
101034a47953SAndreas Boehler        {
101134a47953SAndreas Boehler          $userid = $_SERVER['REMOTE_USER'];
101234a47953SAndreas Boehler        }
101334a47953SAndreas Boehler        else
101434a47953SAndreas Boehler        {
101534a47953SAndreas Boehler          return false;
101634a47953SAndreas Boehler        }
101734a47953SAndreas Boehler      }
1018b269830cSAndreas Boehler
1019b269830cSAndreas Boehler      $calid = $this->getCalendarIdForPage($id);
1020b269830cSAndreas Boehler      if($calid === false)
1021b269830cSAndreas Boehler        return false;
1022b269830cSAndreas Boehler
1023b269830cSAndreas Boehler      $calsettings = $this->getCalendarSettings($calid);
1024b269830cSAndreas Boehler      if(!isset($calsettings['uri']))
1025b269830cSAndreas Boehler        return false;
1026b269830cSAndreas Boehler
1027b269830cSAndreas Boehler      $syncurl = DOKU_URL.'lib/plugins/davcal/calendarserver.php/calendars/'.$user.'/'.$calsettings['uri'];
1028b269830cSAndreas Boehler      return $syncurl;
1029b269830cSAndreas Boehler  }
1030b269830cSAndreas Boehler
1031cb71a62aSAndreas Boehler  /**
1032cb71a62aSAndreas Boehler   * Return the private calendar's URL for a given page
1033cb71a62aSAndreas Boehler   *
1034cb71a62aSAndreas Boehler   * @param string $id the page ID
1035cb71a62aSAndreas Boehler   *
1036cb71a62aSAndreas Boehler   * @return mixed The private URL or false
1037cb71a62aSAndreas Boehler   */
1038f69bb449SAndreas Boehler  public function getPrivateURLForPage($id)
1039f69bb449SAndreas Boehler  {
1040f69bb449SAndreas Boehler      $calid = $this->getCalendarIdForPage($id);
1041f69bb449SAndreas Boehler      if($calid === false)
1042f69bb449SAndreas Boehler        return false;
1043f69bb449SAndreas Boehler
1044f69bb449SAndreas Boehler      return $this->getPrivateURLForCalendar($calid);
1045f69bb449SAndreas Boehler  }
1046f69bb449SAndreas Boehler
1047cb71a62aSAndreas Boehler  /**
1048cb71a62aSAndreas Boehler   * Return the private calendar's URL for a given calendar ID
1049cb71a62aSAndreas Boehler   *
1050cb71a62aSAndreas Boehler   * @param string $calid The calendar's ID
1051cb71a62aSAndreas Boehler   *
1052cb71a62aSAndreas Boehler   * @return mixed The private URL or false
1053cb71a62aSAndreas Boehler   */
1054f69bb449SAndreas Boehler  public function getPrivateURLForCalendar($calid)
1055f69bb449SAndreas Boehler  {
1056185e2535SAndreas Boehler      if(isset($this->cachedValues['privateurl'][$calid]))
1057185e2535SAndreas Boehler        return $this->cachedValues['privateurl'][$calid];
1058f69bb449SAndreas Boehler      $query = "SELECT url FROM calendartoprivateurlmapping WHERE calid=".$this->sqlite->quote_string($calid);
1059f69bb449SAndreas Boehler      $res = $this->sqlite->query($query);
1060f69bb449SAndreas Boehler      $row = $this->sqlite->res2row($res);
1061f69bb449SAndreas Boehler      if(!isset($row['url']))
1062f69bb449SAndreas Boehler      {
1063f69bb449SAndreas Boehler          $url = uniqid("dokuwiki-").".ics";
1064f69bb449SAndreas Boehler          $values = array(
1065f69bb449SAndreas Boehler                $url,
1066f69bb449SAndreas Boehler                $calid
1067f69bb449SAndreas Boehler          );
1068f69bb449SAndreas Boehler          $query = "INSERT INTO calendartoprivateurlmapping (url, calid) VALUES(".
1069f69bb449SAndreas Boehler                $this->sqlite->quote_and_join($values, ", ").")";
1070f69bb449SAndreas Boehler          $res = $this->sqlite->query($query);
1071f69bb449SAndreas Boehler          if($res === false)
1072f69bb449SAndreas Boehler            return false;
1073f69bb449SAndreas Boehler      }
1074f69bb449SAndreas Boehler      else
1075f69bb449SAndreas Boehler      {
1076f69bb449SAndreas Boehler          $url = $row['url'];
1077f69bb449SAndreas Boehler      }
1078185e2535SAndreas Boehler
1079185e2535SAndreas Boehler      $url = DOKU_URL.'lib/plugins/davcal/ics.php/'.$url;
1080185e2535SAndreas Boehler      $this->cachedValues['privateurl'][$calid] = $url;
1081185e2535SAndreas Boehler      return $url;
1082f69bb449SAndreas Boehler  }
1083f69bb449SAndreas Boehler
1084cb71a62aSAndreas Boehler  /**
1085cb71a62aSAndreas Boehler   * Retrieve the calendar ID for a given private calendar URL
1086cb71a62aSAndreas Boehler   *
1087cb71a62aSAndreas Boehler   * @param string $url The private URL
1088cb71a62aSAndreas Boehler   *
1089cb71a62aSAndreas Boehler   * @return mixed The calendar ID or false
1090cb71a62aSAndreas Boehler   */
1091f69bb449SAndreas Boehler  public function getCalendarForPrivateURL($url)
1092f69bb449SAndreas Boehler  {
1093f69bb449SAndreas Boehler      $query = "SELECT calid FROM calendartoprivateurlmapping WHERE url=".$this->sqlite->quote_string($url);
1094f69bb449SAndreas Boehler      $res = $this->sqlite->query($query);
1095f69bb449SAndreas Boehler      $row = $this->sqlite->res2row($res);
1096f69bb449SAndreas Boehler      if(!isset($row['calid']))
1097f69bb449SAndreas Boehler        return false;
1098f69bb449SAndreas Boehler      return $row['calid'];
1099f69bb449SAndreas Boehler  }
1100f69bb449SAndreas Boehler
1101cb71a62aSAndreas Boehler  /**
1102cb71a62aSAndreas Boehler   * Return a given calendar as ICS feed, i.e. all events in one ICS file.
1103cb71a62aSAndreas Boehler   *
1104cb71a62aSAndreas Boehler   * @param string $caldi The calendar ID to retrieve
1105cb71a62aSAndreas Boehler   *
1106cb71a62aSAndreas Boehler   * @return mixed The calendar events as string or false
1107cb71a62aSAndreas Boehler   */
1108f69bb449SAndreas Boehler  public function getCalendarAsICSFeed($calid)
1109f69bb449SAndreas Boehler  {
1110f69bb449SAndreas Boehler      $calSettings = $this->getCalendarSettings($calid);
1111f69bb449SAndreas Boehler      if($calSettings === false)
1112f69bb449SAndreas Boehler        return false;
1113f69bb449SAndreas Boehler      $events = $this->getAllCalendarEvents($calid);
1114f69bb449SAndreas Boehler      if($events === false)
1115f69bb449SAndreas Boehler        return false;
1116f69bb449SAndreas Boehler
1117f69bb449SAndreas Boehler      $out = "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//DAVCal//DAVCal for DokuWiki//EN\nCALSCALE:GREGORIAN\nX-WR-CALNAME:";
1118f69bb449SAndreas Boehler      $out .= $calSettings['displayname']."\n";
1119f69bb449SAndreas Boehler      foreach($events as $event)
1120f69bb449SAndreas Boehler      {
1121f69bb449SAndreas Boehler          $out .= rtrim($event['calendardata']);
1122f69bb449SAndreas Boehler          $out .= "\n";
1123f69bb449SAndreas Boehler      }
1124f69bb449SAndreas Boehler      $out .= "END:VCALENDAR\n";
1125f69bb449SAndreas Boehler      return $out;
1126f69bb449SAndreas Boehler  }
1127f69bb449SAndreas Boehler
11287c7c6b0bSAndreas Boehler  /**
11297c7c6b0bSAndreas Boehler   * Retrieve a configuration option for the plugin
11307c7c6b0bSAndreas Boehler   *
11317c7c6b0bSAndreas Boehler   * @param string $key The key to query
113221d04f73SAndreas Boehler   * @return mixed The option set, null if not found
11337c7c6b0bSAndreas Boehler   */
11347c7c6b0bSAndreas Boehler  public function getConfig($key)
11357c7c6b0bSAndreas Boehler  {
11367c7c6b0bSAndreas Boehler      return $this->getConf($key);
11377c7c6b0bSAndreas Boehler  }
11387c7c6b0bSAndreas Boehler
1139a1a3b679SAndreas Boehler}
1140