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() { 2021d04f73SAndreas Boehler dbglog('---- DAVCAL helper.php init'); 215f2c3e2dSAndreas Boehler } 225f2c3e2dSAndreas Boehler 235f2c3e2dSAndreas Boehler /** Establish and initialize the database if not already done 245f2c3e2dSAndreas Boehler * @return sqlite interface or false 255f2c3e2dSAndreas Boehler */ 265f2c3e2dSAndreas Boehler private function getDB() 275f2c3e2dSAndreas Boehler { 285f2c3e2dSAndreas Boehler if($this->sqlite === null) 295f2c3e2dSAndreas Boehler { 305f2c3e2dSAndreas Boehler $this->sqlite = plugin_load('helper', 'sqlite'); 31a1a3b679SAndreas Boehler if(!$this->sqlite) 32a1a3b679SAndreas Boehler { 3321d04f73SAndreas Boehler dbglog('This plugin requires the sqlite plugin. Please install it.'); 345f2c3e2dSAndreas Boehler msg('This plugin requires the sqlite plugin. Please install it.', -1); 355f2c3e2dSAndreas Boehler return false; 36a1a3b679SAndreas Boehler } 37a1a3b679SAndreas Boehler if(!$this->sqlite->init('davcal', DOKU_PLUGIN.'davcal/db/')) 38a1a3b679SAndreas Boehler { 395f2c3e2dSAndreas Boehler $this->sqlite = null; 4021d04f73SAndreas Boehler dbglog('Error initialising the SQLite DB for DAVCal'); 415f2c3e2dSAndreas Boehler return false; 42a1a3b679SAndreas Boehler } 43a1a3b679SAndreas Boehler } 445f2c3e2dSAndreas Boehler return $this->sqlite; 455f2c3e2dSAndreas Boehler } 46a1a3b679SAndreas Boehler 47cb71a62aSAndreas Boehler /** 48185e2535SAndreas Boehler * Retrieve meta data for a given page 49185e2535SAndreas Boehler * 50185e2535SAndreas Boehler * @param string $id optional The page ID 51185e2535SAndreas Boehler * @return array The metadata 52185e2535SAndreas Boehler */ 53185e2535SAndreas Boehler private function getMeta($id = null) { 54185e2535SAndreas Boehler global $ID; 55185e2535SAndreas Boehler global $INFO; 56185e2535SAndreas Boehler 57185e2535SAndreas Boehler if ($id === null) $id = $ID; 58185e2535SAndreas Boehler 59185e2535SAndreas Boehler if($ID === $id && $INFO['meta']) { 60185e2535SAndreas Boehler $meta = $INFO['meta']; 61185e2535SAndreas Boehler } else { 62185e2535SAndreas Boehler $meta = p_get_metadata($id); 63185e2535SAndreas Boehler } 64185e2535SAndreas Boehler 65185e2535SAndreas Boehler return $meta; 66185e2535SAndreas Boehler } 67185e2535SAndreas Boehler 68185e2535SAndreas Boehler /** 69185e2535SAndreas Boehler * Retrieve the meta data for a given page 70185e2535SAndreas Boehler * 71185e2535SAndreas Boehler * @param string $id optional The page ID 72185e2535SAndreas Boehler * @return array with meta data 73185e2535SAndreas Boehler */ 74185e2535SAndreas Boehler public function getCalendarMetaForPage($id = null) 75185e2535SAndreas Boehler { 76185e2535SAndreas Boehler if(is_null($id)) 77185e2535SAndreas Boehler { 78185e2535SAndreas Boehler global $ID; 79185e2535SAndreas Boehler $id = $ID; 80185e2535SAndreas Boehler } 81185e2535SAndreas Boehler 82185e2535SAndreas Boehler $meta = $this->getMeta($id); 83185e2535SAndreas Boehler if(isset($meta['plugin_davcal'])) 84185e2535SAndreas Boehler return $meta['plugin_davcal']; 85185e2535SAndreas Boehler else 86185e2535SAndreas Boehler return array(); 87185e2535SAndreas Boehler } 88185e2535SAndreas Boehler 89185e2535SAndreas Boehler /** 90d71c9934SAndreas Boehler * Check the permission of a user for a given calendar ID 91d71c9934SAndreas Boehler * 92d71c9934SAndreas Boehler * @param string $id The calendar ID to check 93d71c9934SAndreas Boehler * @return int AUTH_* constants 94d71c9934SAndreas Boehler */ 95d71c9934SAndreas Boehler public function checkCalendarPermission($id) 96d71c9934SAndreas Boehler { 97d4992453SAndreas Boehler if(strpos($id, 'webdav://') === 0) 98d71c9934SAndreas Boehler { 99d71c9934SAndreas Boehler $wdc =& plugin_load('helper', 'webdavclient'); 100d71c9934SAndreas Boehler if(is_null($wdc)) 101d71c9934SAndreas Boehler return AUTH_NONE; 102d4992453SAndreas Boehler $connectionId = str_replace('webdav://', '', $id); 103d71c9934SAndreas Boehler $settings = $wdc->getConnection($connectionId); 104d71c9934SAndreas Boehler if($settings === false) 105d71c9934SAndreas Boehler return AUTH_NONE; 106*58d0e54eSAndreas Boehler // Check if webdavclient has permissions attached to a page 107*58d0e54eSAndreas Boehler if(!empty($settings['permission'])) 108*58d0e54eSAndreas Boehler { 109*58d0e54eSAndreas Boehler $perm = auth_quickaclcheck($settings['permission']); 110*58d0e54eSAndreas Boehler // In case the page has more than read permission, but the 111*58d0e54eSAndreas Boehler // calendar is read-only, we need to modify the permission here. 112*58d0e54eSAndreas Boehler if($perm > AUTH_READ && $settings['write'] == 0) 113*58d0e54eSAndreas Boehler $perm = AUTH_READ; 114*58d0e54eSAndreas Boehler return $perm; 115*58d0e54eSAndreas Boehler } 116d71c9934SAndreas Boehler if($settings['write'] === '1') 117d71c9934SAndreas Boehler return AUTH_CREATE; 118d71c9934SAndreas Boehler return AUTH_READ; 119d71c9934SAndreas Boehler } 120d71c9934SAndreas Boehler else 121d71c9934SAndreas Boehler { 122d71c9934SAndreas Boehler $calid = $this->getCalendarIdForPage($id); 123d71c9934SAndreas Boehler // We return AUTH_READ if the calendar does not exist. This makes 124d71c9934SAndreas Boehler // davcal happy when there are just included calendars 125d71c9934SAndreas Boehler if($calid === false) 126d71c9934SAndreas Boehler return AUTH_READ; 127d71c9934SAndreas Boehler return auth_quickaclcheck($id); 128d71c9934SAndreas Boehler } 129d71c9934SAndreas Boehler } 130d71c9934SAndreas Boehler 131d71c9934SAndreas Boehler /** 13280e1ddf7SAndreas Boehler * Filter calendar pages and return only those where the current 13380e1ddf7SAndreas Boehler * user has at least read permission. 13480e1ddf7SAndreas Boehler * 13580e1ddf7SAndreas Boehler * @param array $calendarPages Array with calendar pages to check 13680e1ddf7SAndreas Boehler * @return array with filtered calendar pages 13780e1ddf7SAndreas Boehler */ 13880e1ddf7SAndreas Boehler public function filterCalendarPagesByUserPermission($calendarPages) 13980e1ddf7SAndreas Boehler { 14080e1ddf7SAndreas Boehler $retList = array(); 14180e1ddf7SAndreas Boehler foreach($calendarPages as $page => $data) 14280e1ddf7SAndreas Boehler { 143*58d0e54eSAndreas Boehler if($this->checkCalendarPermission($page) >= AUTH_READ) 14480e1ddf7SAndreas Boehler { 14580e1ddf7SAndreas Boehler $retList[$page] = $data; 14680e1ddf7SAndreas Boehler } 14780e1ddf7SAndreas Boehler } 14880e1ddf7SAndreas Boehler return $retList; 14980e1ddf7SAndreas Boehler } 15080e1ddf7SAndreas Boehler 15180e1ddf7SAndreas Boehler /** 152185e2535SAndreas Boehler * Get all calendar pages used by a given page 153185e2535SAndreas Boehler * based on the stored metadata 154185e2535SAndreas Boehler * 155185e2535SAndreas Boehler * @param string $id optional The page id 156185e2535SAndreas Boehler * @return mixed The pages as array or false 157185e2535SAndreas Boehler */ 158185e2535SAndreas Boehler public function getCalendarPagesByMeta($id = null) 159185e2535SAndreas Boehler { 160185e2535SAndreas Boehler if(is_null($id)) 161185e2535SAndreas Boehler { 162185e2535SAndreas Boehler global $ID; 163185e2535SAndreas Boehler $id = $ID; 164185e2535SAndreas Boehler } 165185e2535SAndreas Boehler 166185e2535SAndreas Boehler $meta = $this->getCalendarMetaForPage($id); 1670b805092SAndreas Boehler 168185e2535SAndreas Boehler if(isset($meta['id'])) 169ed764890SAndreas Boehler { 170ed764890SAndreas Boehler // Filter the list of pages by permission 17180e1ddf7SAndreas Boehler $pages = $this->filterCalendarPagesByUserPermission($meta['id']); 17280e1ddf7SAndreas Boehler if(empty($pages)) 173ed764890SAndreas Boehler return false; 17480e1ddf7SAndreas Boehler return $pages; 175ed764890SAndreas Boehler } 176185e2535SAndreas Boehler return false; 177185e2535SAndreas Boehler } 178185e2535SAndreas Boehler 179185e2535SAndreas Boehler /** 180185e2535SAndreas Boehler * Get a list of calendar names/pages/ids/colors 181185e2535SAndreas Boehler * for an array of page ids 182185e2535SAndreas Boehler * 183185e2535SAndreas Boehler * @param array $calendarPages The calendar pages to retrieve 184185e2535SAndreas Boehler * @return array The list 185185e2535SAndreas Boehler */ 186185e2535SAndreas Boehler public function getCalendarMapForIDs($calendarPages) 187185e2535SAndreas Boehler { 188185e2535SAndreas Boehler $data = array(); 1894a2bf5eeSAndreas Boehler foreach($calendarPages as $page => $color) 190185e2535SAndreas Boehler { 1910b805092SAndreas Boehler if(strpos($page, 'webdav://') === 0) 1920b805092SAndreas Boehler { 1930b805092SAndreas Boehler $wdc =& plugin_load('helper', 'webdavclient'); 1940b805092SAndreas Boehler if(is_null($wdc)) 1950b805092SAndreas Boehler continue; 1960b805092SAndreas Boehler $connectionId = str_replace('webdav://', '', $page); 1970b805092SAndreas Boehler $settings = $wdc->getConnection($connectionId); 1982393a702SAndreas Boehler if($settings === false) 1992393a702SAndreas Boehler continue; 2000b805092SAndreas Boehler $name = $settings['displayname']; 201d71c9934SAndreas Boehler $write = ($settings['write'] === '1'); 2020b805092SAndreas Boehler $calid = $connectionId; 203cd2f100dSAndreas Boehler $color = '#3a87ad'; 2040b805092SAndreas Boehler } 2050b805092SAndreas Boehler else 2060b805092SAndreas Boehler { 207185e2535SAndreas Boehler $calid = $this->getCalendarIdForPage($page); 208185e2535SAndreas Boehler if($calid !== false) 209185e2535SAndreas Boehler { 210185e2535SAndreas Boehler $settings = $this->getCalendarSettings($calid); 211185e2535SAndreas Boehler $name = $settings['displayname']; 212cd2f100dSAndreas Boehler $color = $settings['calendarcolor']; 213ed764890SAndreas Boehler $write = (auth_quickaclcheck($page) > AUTH_READ); 2140b805092SAndreas Boehler } 2150b805092SAndreas Boehler else 2160b805092SAndreas Boehler { 2170b805092SAndreas Boehler continue; 2180b805092SAndreas Boehler } 2190b805092SAndreas Boehler } 220185e2535SAndreas Boehler $data[] = array('name' => $name, 'page' => $page, 'calid' => $calid, 221ed764890SAndreas Boehler 'color' => $color, 'write' => $write); 222185e2535SAndreas Boehler } 223185e2535SAndreas Boehler return $data; 224185e2535SAndreas Boehler } 225185e2535SAndreas Boehler 226185e2535SAndreas Boehler /** 227185e2535SAndreas Boehler * Get the saved calendar color for a given page. 228185e2535SAndreas Boehler * 229185e2535SAndreas Boehler * @param string $id optional The page ID 230185e2535SAndreas Boehler * @return mixed The color on success, otherwise false 231185e2535SAndreas Boehler */ 232185e2535SAndreas Boehler public function getCalendarColorForPage($id = null) 233185e2535SAndreas Boehler { 234185e2535SAndreas Boehler if(is_null($id)) 235185e2535SAndreas Boehler { 236185e2535SAndreas Boehler global $ID; 237185e2535SAndreas Boehler $id = $ID; 238185e2535SAndreas Boehler } 239185e2535SAndreas Boehler 240185e2535SAndreas Boehler $calid = $this->getCalendarIdForPage($id); 241185e2535SAndreas Boehler if($calid === false) 242185e2535SAndreas Boehler return false; 243185e2535SAndreas Boehler 244185e2535SAndreas Boehler return $this->getCalendarColorForCalendar($calid); 245185e2535SAndreas Boehler } 246185e2535SAndreas Boehler 247185e2535SAndreas Boehler /** 248185e2535SAndreas Boehler * Get the saved calendar color for a given calendar ID. 249185e2535SAndreas Boehler * 250185e2535SAndreas Boehler * @param string $id optional The calendar ID 251185e2535SAndreas Boehler * @return mixed The color on success, otherwise false 252185e2535SAndreas Boehler */ 253185e2535SAndreas Boehler public function getCalendarColorForCalendar($calid) 254185e2535SAndreas Boehler { 255185e2535SAndreas Boehler if(isset($this->cachedValues['calendarcolor'][$calid])) 256185e2535SAndreas Boehler return $this->cachedValues['calendarcolor'][$calid]; 257185e2535SAndreas Boehler 258185e2535SAndreas Boehler $row = $this->getCalendarSettings($calid); 259185e2535SAndreas Boehler 260185e2535SAndreas Boehler if(!isset($row['calendarcolor'])) 261185e2535SAndreas Boehler return false; 262185e2535SAndreas Boehler 263185e2535SAndreas Boehler $color = $row['calendarcolor']; 264185e2535SAndreas Boehler $this->cachedValues['calendarcolor'][$calid] = $color; 265185e2535SAndreas Boehler return $color; 266185e2535SAndreas Boehler } 267185e2535SAndreas Boehler 268185e2535SAndreas Boehler /** 269e86c8dd3SAndreas Boehler * Get the user's principal URL for iOS sync 270e86c8dd3SAndreas Boehler * @param string $user the user name 271e86c8dd3SAndreas Boehler * @return the URL to the principal sync 272e86c8dd3SAndreas Boehler */ 273e86c8dd3SAndreas Boehler public function getPrincipalUrlForUser($user) 274e86c8dd3SAndreas Boehler { 275e86c8dd3SAndreas Boehler if(is_null($user)) 276e86c8dd3SAndreas Boehler return false; 277e86c8dd3SAndreas Boehler $url = DOKU_URL.'lib/plugins/davcal/calendarserver.php/principals/'.$user; 278e86c8dd3SAndreas Boehler return $url; 279e86c8dd3SAndreas Boehler } 280e86c8dd3SAndreas Boehler 281e86c8dd3SAndreas Boehler /** 282185e2535SAndreas Boehler * Set the calendar color for a given page. 283185e2535SAndreas Boehler * 284185e2535SAndreas Boehler * @param string $color The color definition 285185e2535SAndreas Boehler * @param string $id optional The page ID 286185e2535SAndreas Boehler * @return boolean True on success, otherwise false 287185e2535SAndreas Boehler */ 288185e2535SAndreas Boehler public function setCalendarColorForPage($color, $id = null) 289185e2535SAndreas Boehler { 290185e2535SAndreas Boehler if(is_null($id)) 291185e2535SAndreas Boehler { 292185e2535SAndreas Boehler global $ID; 293185e2535SAndreas Boehler $id = $ID; 294185e2535SAndreas Boehler } 295185e2535SAndreas Boehler $calid = $this->getCalendarIdForPage($id); 296185e2535SAndreas Boehler if($calid === false) 297185e2535SAndreas Boehler return false; 298185e2535SAndreas Boehler 2995f2c3e2dSAndreas Boehler $sqlite = $this->getDB(); 3005f2c3e2dSAndreas Boehler if(!$sqlite) 3015f2c3e2dSAndreas Boehler return false; 30251f4febbSAndreas Boehler $query = "UPDATE calendars SET calendarcolor = ? ". 30351f4febbSAndreas Boehler " WHERE id = ?"; 3045f2c3e2dSAndreas Boehler $res = $sqlite->query($query, $color, $calid); 305185e2535SAndreas Boehler if($res !== false) 306185e2535SAndreas Boehler { 307185e2535SAndreas Boehler $this->cachedValues['calendarcolor'][$calid] = $color; 308185e2535SAndreas Boehler return true; 309185e2535SAndreas Boehler } 310185e2535SAndreas Boehler return false; 311185e2535SAndreas Boehler } 312185e2535SAndreas Boehler 313185e2535SAndreas Boehler /** 314cb71a62aSAndreas Boehler * Set the calendar name and description for a given page with a given 315cb71a62aSAndreas Boehler * page id. 316cb71a62aSAndreas Boehler * If the calendar doesn't exist, the calendar is created! 317cb71a62aSAndreas Boehler * 318cb71a62aSAndreas Boehler * @param string $name The name of the new calendar 319cb71a62aSAndreas Boehler * @param string $description The description of the new calendar 320cb71a62aSAndreas Boehler * @param string $id (optional) The ID of the page 321cb71a62aSAndreas Boehler * @param string $userid The userid of the creating user 322cb71a62aSAndreas Boehler * 323cb71a62aSAndreas Boehler * @return boolean True on success, otherwise false. 324cb71a62aSAndreas Boehler */ 325a1a3b679SAndreas Boehler public function setCalendarNameForPage($name, $description, $id = null, $userid = null) 326a1a3b679SAndreas Boehler { 327a1a3b679SAndreas Boehler if(is_null($id)) 328a1a3b679SAndreas Boehler { 329a1a3b679SAndreas Boehler global $ID; 330a1a3b679SAndreas Boehler $id = $ID; 331a1a3b679SAndreas Boehler } 332a1a3b679SAndreas Boehler if(is_null($userid)) 33334a47953SAndreas Boehler { 33434a47953SAndreas Boehler if(isset($_SERVER['REMOTE_USER']) && !is_null($_SERVER['REMOTE_USER'])) 33534a47953SAndreas Boehler { 336a1a3b679SAndreas Boehler $userid = $_SERVER['REMOTE_USER']; 33734a47953SAndreas Boehler } 33834a47953SAndreas Boehler else 33934a47953SAndreas Boehler { 34034a47953SAndreas Boehler $userid = uniqid('davcal-'); 34134a47953SAndreas Boehler } 34234a47953SAndreas Boehler } 343a1a3b679SAndreas Boehler $calid = $this->getCalendarIdForPage($id); 344a1a3b679SAndreas Boehler if($calid === false) 345a1a3b679SAndreas Boehler return $this->createCalendarForPage($name, $description, $id, $userid); 346a1a3b679SAndreas Boehler 3475f2c3e2dSAndreas Boehler $sqlite = $this->getDB(); 3485f2c3e2dSAndreas Boehler if(!$sqlite) 3495f2c3e2dSAndreas Boehler return false; 35051f4febbSAndreas Boehler $query = "UPDATE calendars SET displayname = ?, description = ? WHERE id = ?"; 3515f2c3e2dSAndreas Boehler $res = $sqlite->query($query, $name, $description, $calid); 352b269830cSAndreas Boehler if($res !== false) 353b269830cSAndreas Boehler return true; 354b269830cSAndreas Boehler return false; 355a1a3b679SAndreas Boehler } 356a1a3b679SAndreas Boehler 357cb71a62aSAndreas Boehler /** 358d5703f5aSAndreas Boehler * Update a calendar's displayname 359d5703f5aSAndreas Boehler * 360d5703f5aSAndreas Boehler * @param int $calid The calendar's ID 361d5703f5aSAndreas Boehler * @param string $name The new calendar name 362d5703f5aSAndreas Boehler * 363d5703f5aSAndreas Boehler * @return boolean True on success, otherwise false 364d5703f5aSAndreas Boehler */ 365d5703f5aSAndreas Boehler public function updateCalendarName($calid, $name) 366d5703f5aSAndreas Boehler { 3675f2c3e2dSAndreas Boehler $sqlite = $this->getDB(); 3685f2c3e2dSAndreas Boehler if(!$sqlite) 3695f2c3e2dSAndreas Boehler return false; 370d5703f5aSAndreas Boehler $query = "UPDATE calendars SET displayname = ? WHERE id = ?"; 3715f2c3e2dSAndreas Boehler $res = $sqlite->query($query, $calid, $name); 372d5703f5aSAndreas Boehler if($res !== false) 373d5703f5aSAndreas Boehler { 374d5703f5aSAndreas Boehler $this->updateSyncTokenLog($calid, '', 'modified'); 375d5703f5aSAndreas Boehler return true; 376d5703f5aSAndreas Boehler } 377d5703f5aSAndreas Boehler return false; 378d5703f5aSAndreas Boehler } 379d5703f5aSAndreas Boehler 380d5703f5aSAndreas Boehler /** 381d5703f5aSAndreas Boehler * Update the calendar description 382d5703f5aSAndreas Boehler * 383d5703f5aSAndreas Boehler * @param int $calid The calendar's ID 384d5703f5aSAndreas Boehler * @param string $description The new calendar's description 385d5703f5aSAndreas Boehler * 386d5703f5aSAndreas Boehler * @return boolean True on success, otherwise false 387d5703f5aSAndreas Boehler */ 388d5703f5aSAndreas Boehler public function updateCalendarDescription($calid, $description) 389d5703f5aSAndreas Boehler { 3905f2c3e2dSAndreas Boehler $sqlite = $this->getDB(); 3915f2c3e2dSAndreas Boehler if(!$sqlite) 3925f2c3e2dSAndreas Boehler return false; 393d5703f5aSAndreas Boehler $query = "UPDATE calendars SET description = ? WHERE id = ?"; 3945f2c3e2dSAndreas Boehler $res = $sqlite->query($query, $calid, $description); 395d5703f5aSAndreas Boehler if($res !== false) 396d5703f5aSAndreas Boehler { 397d5703f5aSAndreas Boehler $this->updateSyncTokenLog($calid, '', 'modified'); 398d5703f5aSAndreas Boehler return true; 399d5703f5aSAndreas Boehler } 400d5703f5aSAndreas Boehler return false; 401d5703f5aSAndreas Boehler } 402d5703f5aSAndreas Boehler 403d5703f5aSAndreas Boehler /** 404d5703f5aSAndreas Boehler * Update a calendar's timezone information 405d5703f5aSAndreas Boehler * 406d5703f5aSAndreas Boehler * @param int $calid The calendar's ID 407d5703f5aSAndreas Boehler * @param string $timezone The new timezone to set 408d5703f5aSAndreas Boehler * 409d5703f5aSAndreas Boehler * @return boolean True on success, otherwise false 410d5703f5aSAndreas Boehler */ 411d5703f5aSAndreas Boehler public function updateCalendarTimezone($calid, $timezone) 412d5703f5aSAndreas Boehler { 4135f2c3e2dSAndreas Boehler $sqlite = $this->getDB(); 4145f2c3e2dSAndreas Boehler if(!$sqlite) 4155f2c3e2dSAndreas Boehler return false; 416d5703f5aSAndreas Boehler $query = "UPDATE calendars SET timezone = ? WHERE id = ?"; 4175f2c3e2dSAndreas Boehler $res = $sqlite->query($query, $calid, $timezone); 418d5703f5aSAndreas Boehler if($res !== false) 419d5703f5aSAndreas Boehler { 420d5703f5aSAndreas Boehler $this->updateSyncTokenLog($calid, '', 'modified'); 421d5703f5aSAndreas Boehler return true; 422d5703f5aSAndreas Boehler } 423d5703f5aSAndreas Boehler return false; 424d5703f5aSAndreas Boehler } 425d5703f5aSAndreas Boehler 426d5703f5aSAndreas Boehler /** 427cb71a62aSAndreas Boehler * Save the personal settings to the SQLite database 'calendarsettings'. 428cb71a62aSAndreas Boehler * 429cb71a62aSAndreas Boehler * @param array $settings The settings array to store 430cb71a62aSAndreas Boehler * @param string $userid (optional) The userid to store 431cb71a62aSAndreas Boehler * 432cb71a62aSAndreas Boehler * @param boolean True on success, otherwise false 433cb71a62aSAndreas Boehler */ 434a495d34cSAndreas Boehler public function savePersonalSettings($settings, $userid = null) 435a495d34cSAndreas Boehler { 436a495d34cSAndreas Boehler if(is_null($userid)) 43734a47953SAndreas Boehler { 43834a47953SAndreas Boehler if(isset($_SERVER['REMOTE_USER']) && !is_null($_SERVER['REMOTE_USER'])) 43934a47953SAndreas Boehler { 440a495d34cSAndreas Boehler $userid = $_SERVER['REMOTE_USER']; 44134a47953SAndreas Boehler } 44234a47953SAndreas Boehler else 44334a47953SAndreas Boehler { 44434a47953SAndreas Boehler return false; 44534a47953SAndreas Boehler } 44634a47953SAndreas Boehler } 4475f2c3e2dSAndreas Boehler $sqlite = $this->getDB(); 4485f2c3e2dSAndreas Boehler if(!$sqlite) 4495f2c3e2dSAndreas Boehler return false; 4505f2c3e2dSAndreas Boehler $sqlite->query("BEGIN TRANSACTION"); 451a495d34cSAndreas Boehler 45251f4febbSAndreas Boehler $query = "DELETE FROM calendarsettings WHERE userid = ?"; 4535f2c3e2dSAndreas Boehler $sqlite->query($query, $userid); 454bd883736SAndreas Boehler 455a495d34cSAndreas Boehler foreach($settings as $key => $value) 456a495d34cSAndreas Boehler { 45751f4febbSAndreas Boehler $query = "INSERT INTO calendarsettings (userid, key, value) VALUES (?, ?, ?)"; 4585f2c3e2dSAndreas Boehler $res = $sqlite->query($query, $userid, $key, $value); 459a495d34cSAndreas Boehler if($res === false) 460a495d34cSAndreas Boehler return false; 461a495d34cSAndreas Boehler } 4625f2c3e2dSAndreas Boehler $sqlite->query("COMMIT TRANSACTION"); 463185e2535SAndreas Boehler $this->cachedValues['settings'][$userid] = $settings; 464a495d34cSAndreas Boehler return true; 465a495d34cSAndreas Boehler } 466a495d34cSAndreas Boehler 467cb71a62aSAndreas Boehler /** 468cb71a62aSAndreas Boehler * Retrieve the settings array for a given user id. 469cb71a62aSAndreas Boehler * Some sane defaults are returned, currently: 470cb71a62aSAndreas Boehler * 471cb71a62aSAndreas Boehler * timezone => local 472cb71a62aSAndreas Boehler * weeknumbers => 0 473cb71a62aSAndreas Boehler * workweek => 0 474cb71a62aSAndreas Boehler * 475cb71a62aSAndreas Boehler * @param string $userid (optional) The user id to retrieve 476cb71a62aSAndreas Boehler * 477cb71a62aSAndreas Boehler * @return array The settings array 478cb71a62aSAndreas Boehler */ 479a495d34cSAndreas Boehler public function getPersonalSettings($userid = null) 480a495d34cSAndreas Boehler { 481bd883736SAndreas Boehler // Some sane default settings 482bd883736SAndreas Boehler $settings = array( 483fb813b30SAndreas Boehler 'timezone' => $this->getConf('timezone'), 484fb813b30SAndreas Boehler 'weeknumbers' => $this->getConf('weeknumbers'), 485fb813b30SAndreas Boehler 'workweek' => $this->getConf('workweek'), 4861d5bdcd0SAndreas Boehler 'monday' => $this->getConf('monday'), 4871d5bdcd0SAndreas Boehler 'timeformat' => $this->getConf('timeformat') 488bd883736SAndreas Boehler ); 48934a47953SAndreas Boehler if(is_null($userid)) 49034a47953SAndreas Boehler { 49134a47953SAndreas Boehler if(isset($_SERVER['REMOTE_USER']) && !is_null($_SERVER['REMOTE_USER'])) 49234a47953SAndreas Boehler { 49334a47953SAndreas Boehler $userid = $_SERVER['REMOTE_USER']; 49434a47953SAndreas Boehler } 49534a47953SAndreas Boehler else 49634a47953SAndreas Boehler { 49734a47953SAndreas Boehler return $settings; 49834a47953SAndreas Boehler } 49934a47953SAndreas Boehler } 50034a47953SAndreas Boehler 5015f2c3e2dSAndreas Boehler $sqlite = $this->getDB(); 5025f2c3e2dSAndreas Boehler if(!$sqlite) 5035f2c3e2dSAndreas Boehler return false; 50434a47953SAndreas Boehler if(isset($this->cachedValues['settings'][$userid])) 50534a47953SAndreas Boehler return $this->cachedValues['settings'][$userid]; 50651f4febbSAndreas Boehler $query = "SELECT key, value FROM calendarsettings WHERE userid = ?"; 5075f2c3e2dSAndreas Boehler $res = $sqlite->query($query, $userid); 5085f2c3e2dSAndreas Boehler $arr = $sqlite->res2arr($res); 509a495d34cSAndreas Boehler foreach($arr as $row) 510a495d34cSAndreas Boehler { 511a495d34cSAndreas Boehler $settings[$row['key']] = $row['value']; 512a495d34cSAndreas Boehler } 513185e2535SAndreas Boehler $this->cachedValues['settings'][$userid] = $settings; 514a495d34cSAndreas Boehler return $settings; 515a495d34cSAndreas Boehler } 516a495d34cSAndreas Boehler 517cb71a62aSAndreas Boehler /** 518cb71a62aSAndreas Boehler * Retrieve the calendar ID based on a page ID from the SQLite table 519cb71a62aSAndreas Boehler * 'pagetocalendarmapping'. 520cb71a62aSAndreas Boehler * 521cb71a62aSAndreas Boehler * @param string $id (optional) The page ID to retrieve the corresponding calendar 522cb71a62aSAndreas Boehler * 523cb71a62aSAndreas Boehler * @return mixed the ID on success, otherwise false 524cb71a62aSAndreas Boehler */ 525a1a3b679SAndreas Boehler public function getCalendarIdForPage($id = null) 526a1a3b679SAndreas Boehler { 527a1a3b679SAndreas Boehler if(is_null($id)) 528a1a3b679SAndreas Boehler { 529a1a3b679SAndreas Boehler global $ID; 530a1a3b679SAndreas Boehler $id = $ID; 531a1a3b679SAndreas Boehler } 532a1a3b679SAndreas Boehler 533185e2535SAndreas Boehler if(isset($this->cachedValues['calid'][$id])) 534185e2535SAndreas Boehler return $this->cachedValues['calid'][$id]; 535185e2535SAndreas Boehler 5365f2c3e2dSAndreas Boehler $sqlite = $this->getDB(); 5375f2c3e2dSAndreas Boehler if(!$sqlite) 5385f2c3e2dSAndreas Boehler return false; 53951f4febbSAndreas Boehler $query = "SELECT calid FROM pagetocalendarmapping WHERE page = ?"; 5405f2c3e2dSAndreas Boehler $res = $sqlite->query($query, $id); 5415f2c3e2dSAndreas Boehler $row = $sqlite->res2row($res); 542a1a3b679SAndreas Boehler if(isset($row['calid'])) 543185e2535SAndreas Boehler { 544185e2535SAndreas Boehler $calid = $row['calid']; 545185e2535SAndreas Boehler $this->cachedValues['calid'] = $calid; 546185e2535SAndreas Boehler return $calid; 547185e2535SAndreas Boehler } 548a1a3b679SAndreas Boehler return false; 549a1a3b679SAndreas Boehler } 550a1a3b679SAndreas Boehler 551cb71a62aSAndreas Boehler /** 552cb71a62aSAndreas Boehler * Retrieve the complete calendar id to page mapping. 553cb71a62aSAndreas Boehler * This is necessary to be able to retrieve a list of 554cb71a62aSAndreas Boehler * calendars for a given user and check the access rights. 555cb71a62aSAndreas Boehler * 556cb71a62aSAndreas Boehler * @return array The mapping array 557cb71a62aSAndreas Boehler */ 558a1a3b679SAndreas Boehler public function getCalendarIdToPageMapping() 559a1a3b679SAndreas Boehler { 5605f2c3e2dSAndreas Boehler $sqlite = $this->getDB(); 5615f2c3e2dSAndreas Boehler if(!$sqlite) 5625f2c3e2dSAndreas Boehler return false; 563a1a3b679SAndreas Boehler $query = "SELECT calid, page FROM pagetocalendarmapping"; 5645f2c3e2dSAndreas Boehler $res = $sqlite->query($query); 5655f2c3e2dSAndreas Boehler $arr = $sqlite->res2arr($res); 566a1a3b679SAndreas Boehler return $arr; 567a1a3b679SAndreas Boehler } 568a1a3b679SAndreas Boehler 569cb71a62aSAndreas Boehler /** 570cb71a62aSAndreas Boehler * Retrieve all calendar IDs a given user has access to. 571cb71a62aSAndreas Boehler * The user is specified by the principalUri, so the 572cb71a62aSAndreas Boehler * user name is actually split from the URI component. 573cb71a62aSAndreas Boehler * 574cb71a62aSAndreas Boehler * Access rights are checked against DokuWiki's ACL 575cb71a62aSAndreas Boehler * and applied accordingly. 576cb71a62aSAndreas Boehler * 577cb71a62aSAndreas Boehler * @param string $principalUri The principal URI to work on 578cb71a62aSAndreas Boehler * 579cb71a62aSAndreas Boehler * @return array An associative array of calendar IDs 580cb71a62aSAndreas Boehler */ 581a1a3b679SAndreas Boehler public function getCalendarIdsForUser($principalUri) 582a1a3b679SAndreas Boehler { 58334a47953SAndreas Boehler global $auth; 584a1a3b679SAndreas Boehler $user = explode('/', $principalUri); 585a1a3b679SAndreas Boehler $user = end($user); 586a1a3b679SAndreas Boehler $mapping = $this->getCalendarIdToPageMapping(); 587a1a3b679SAndreas Boehler $calids = array(); 58834a47953SAndreas Boehler $ud = $auth->getUserData($user); 58934a47953SAndreas Boehler $groups = $ud['grps']; 590a1a3b679SAndreas Boehler foreach($mapping as $row) 591a1a3b679SAndreas Boehler { 592a1a3b679SAndreas Boehler $id = $row['calid']; 59313b16484SAndreas Boehler $enabled = $this->getCalendarStatus($id); 59413b16484SAndreas Boehler if($enabled == false) 59513b16484SAndreas Boehler continue; 596a1a3b679SAndreas Boehler $page = $row['page']; 59734a47953SAndreas Boehler $acl = auth_aclcheck($page, $user, $groups); 598a1a3b679SAndreas Boehler if($acl >= AUTH_READ) 599a1a3b679SAndreas Boehler { 600a1a3b679SAndreas Boehler $write = $acl > AUTH_READ; 601a1a3b679SAndreas Boehler $calids[$id] = array('readonly' => !$write); 602a1a3b679SAndreas Boehler } 603a1a3b679SAndreas Boehler } 604a1a3b679SAndreas Boehler return $calids; 605a1a3b679SAndreas Boehler } 606a1a3b679SAndreas Boehler 607cb71a62aSAndreas Boehler /** 608cb71a62aSAndreas Boehler * Create a new calendar for a given page ID and set name and description 609cb71a62aSAndreas Boehler * accordingly. Also update the pagetocalendarmapping table on success. 610cb71a62aSAndreas Boehler * 611cb71a62aSAndreas Boehler * @param string $name The calendar's name 612cb71a62aSAndreas Boehler * @param string $description The calendar's description 613cb71a62aSAndreas Boehler * @param string $id (optional) The page ID to work on 614cb71a62aSAndreas Boehler * @param string $userid (optional) The user ID that created the calendar 615cb71a62aSAndreas Boehler * 616cb71a62aSAndreas Boehler * @return boolean True on success, otherwise false 617cb71a62aSAndreas Boehler */ 618a1a3b679SAndreas Boehler public function createCalendarForPage($name, $description, $id = null, $userid = null) 619a1a3b679SAndreas Boehler { 620a1a3b679SAndreas Boehler if(is_null($id)) 621a1a3b679SAndreas Boehler { 622a1a3b679SAndreas Boehler global $ID; 623a1a3b679SAndreas Boehler $id = $ID; 624a1a3b679SAndreas Boehler } 625a1a3b679SAndreas Boehler if(is_null($userid)) 62634a47953SAndreas Boehler { 62734a47953SAndreas Boehler if(isset($_SERVER['REMOTE_USER']) && !is_null($_SERVER['REMOTE_USER'])) 62834a47953SAndreas Boehler { 629a1a3b679SAndreas Boehler $userid = $_SERVER['REMOTE_USER']; 63034a47953SAndreas Boehler } 63134a47953SAndreas Boehler else 63234a47953SAndreas Boehler { 63334a47953SAndreas Boehler $userid = uniqid('davcal-'); 63434a47953SAndreas Boehler } 63534a47953SAndreas Boehler } 636a1a3b679SAndreas Boehler $values = array('principals/'.$userid, 637a1a3b679SAndreas Boehler $name, 638a1a3b679SAndreas Boehler str_replace(array('/', ' ', ':'), '_', $id), 639a1a3b679SAndreas Boehler $description, 640a1a3b679SAndreas Boehler 'VEVENT,VTODO', 64155a741c0SAndreas Boehler 0, 64255a741c0SAndreas Boehler 1); 6435f2c3e2dSAndreas Boehler 6445f2c3e2dSAndreas Boehler $sqlite = $this->getDB(); 6455f2c3e2dSAndreas Boehler if(!$sqlite) 6465f2c3e2dSAndreas Boehler return false; 64751f4febbSAndreas Boehler $query = "INSERT INTO calendars (principaluri, displayname, uri, description, components, transparent, synctoken) ". 64851f4febbSAndreas Boehler "VALUES (?, ?, ?, ?, ?, ?, ?)"; 6495f2c3e2dSAndreas Boehler $res = $sqlite->query($query, $values[0], $values[1], $values[2], $values[3], $values[4], $values[5], $values[6]); 65055a741c0SAndreas Boehler if($res === false) 65155a741c0SAndreas Boehler return false; 652cb71a62aSAndreas Boehler 653cb71a62aSAndreas Boehler // Get the new calendar ID 65451f4febbSAndreas Boehler $query = "SELECT id FROM calendars WHERE principaluri = ? AND displayname = ? AND ". 65551f4febbSAndreas Boehler "uri = ? AND description = ?"; 6565f2c3e2dSAndreas Boehler $res = $sqlite->query($query, $values[0], $values[1], $values[2], $values[3]); 6575f2c3e2dSAndreas Boehler $row = $sqlite->res2row($res); 658cb71a62aSAndreas Boehler 659cb71a62aSAndreas Boehler // Update the pagetocalendarmapping table with the new calendar ID 660a1a3b679SAndreas Boehler if(isset($row['id'])) 661a1a3b679SAndreas Boehler { 66251f4febbSAndreas Boehler $query = "INSERT INTO pagetocalendarmapping (page, calid) VALUES (?, ?)"; 6635f2c3e2dSAndreas Boehler $res = $sqlite->query($query, $id, $row['id']); 66455a741c0SAndreas Boehler return ($res !== false); 665a1a3b679SAndreas Boehler } 666a1a3b679SAndreas Boehler 667a1a3b679SAndreas Boehler return false; 668a1a3b679SAndreas Boehler } 669a1a3b679SAndreas Boehler 670cb71a62aSAndreas Boehler /** 671d5703f5aSAndreas Boehler * Add a new calendar entry to the given calendar. Calendar data is 672d5703f5aSAndreas Boehler * specified as ICS file, thus it needs to be parsed first. 673d5703f5aSAndreas Boehler * 674d5703f5aSAndreas Boehler * This is mainly needed for the sync support. 675d5703f5aSAndreas Boehler * 676d5703f5aSAndreas Boehler * @param int $calid The calendar's ID 677d5703f5aSAndreas Boehler * @param string $uri The new object URI 678d5703f5aSAndreas Boehler * @param string $ics The ICS file 679d5703f5aSAndreas Boehler * 680d5703f5aSAndreas Boehler * @return mixed The etag. 681d5703f5aSAndreas Boehler */ 682d5703f5aSAndreas Boehler public function addCalendarEntryToCalendarByICS($calid, $uri, $ics) 683d5703f5aSAndreas Boehler { 684d5703f5aSAndreas Boehler $extraData = $this->getDenormalizedData($ics); 685d5703f5aSAndreas Boehler 6865f2c3e2dSAndreas Boehler $sqlite = $this->getDB(); 6875f2c3e2dSAndreas Boehler if(!$sqlite) 6885f2c3e2dSAndreas Boehler return false; 689d5703f5aSAndreas Boehler $query = "INSERT INTO calendarobjects (calendarid, uri, calendardata, lastmodified, etag, size, componenttype, firstoccurence, lastoccurence, uid) VALUES (?,?,?,?,?,?,?,?,?,?)"; 6905f2c3e2dSAndreas Boehler $res = $sqlite->query($query, 691d5703f5aSAndreas Boehler $calid, 692d5703f5aSAndreas Boehler $uri, 693d5703f5aSAndreas Boehler $ics, 694d5703f5aSAndreas Boehler time(), 695d5703f5aSAndreas Boehler $extraData['etag'], 696d5703f5aSAndreas Boehler $extraData['size'], 697d5703f5aSAndreas Boehler $extraData['componentType'], 698d5703f5aSAndreas Boehler $extraData['firstOccurence'], 699d5703f5aSAndreas Boehler $extraData['lastOccurence'], 700d5703f5aSAndreas Boehler $extraData['uid']); 701d5703f5aSAndreas Boehler // If successfully, update the sync token database 702d5703f5aSAndreas Boehler if($res !== false) 703d5703f5aSAndreas Boehler { 704d5703f5aSAndreas Boehler $this->updateSyncTokenLog($calid, $uri, 'added'); 705d5703f5aSAndreas Boehler } 706d5703f5aSAndreas Boehler return $extraData['etag']; 707d5703f5aSAndreas Boehler } 708d5703f5aSAndreas Boehler 709d5703f5aSAndreas Boehler /** 710d5703f5aSAndreas Boehler * Edit a calendar entry by providing a new ICS file. This is mainly 711d5703f5aSAndreas Boehler * needed for the sync support. 712d5703f5aSAndreas Boehler * 713d5703f5aSAndreas Boehler * @param int $calid The calendar's IS 714d5703f5aSAndreas Boehler * @param string $uri The object's URI to modify 715d5703f5aSAndreas Boehler * @param string $ics The new object's ICS file 716d5703f5aSAndreas Boehler */ 717d5703f5aSAndreas Boehler public function editCalendarEntryToCalendarByICS($calid, $uri, $ics) 718d5703f5aSAndreas Boehler { 719d5703f5aSAndreas Boehler $extraData = $this->getDenormalizedData($ics); 720d5703f5aSAndreas Boehler 7215f2c3e2dSAndreas Boehler $sqlite = $this->getDB(); 7225f2c3e2dSAndreas Boehler if(!$sqlite) 7235f2c3e2dSAndreas Boehler return false; 724d5703f5aSAndreas Boehler $query = "UPDATE calendarobjects SET calendardata = ?, lastmodified = ?, etag = ?, size = ?, componenttype = ?, firstoccurence = ?, lastoccurence = ?, uid = ? WHERE calendarid = ? AND uri = ?"; 7255f2c3e2dSAndreas Boehler $res = $sqlite->query($query, 726d5703f5aSAndreas Boehler $ics, 727d5703f5aSAndreas Boehler time(), 728d5703f5aSAndreas Boehler $extraData['etag'], 729d5703f5aSAndreas Boehler $extraData['size'], 730d5703f5aSAndreas Boehler $extraData['componentType'], 731d5703f5aSAndreas Boehler $extraData['firstOccurence'], 732d5703f5aSAndreas Boehler $extraData['lastOccurence'], 733d5703f5aSAndreas Boehler $extraData['uid'], 734d5703f5aSAndreas Boehler $calid, 735d5703f5aSAndreas Boehler $uri 736d5703f5aSAndreas Boehler ); 737d5703f5aSAndreas Boehler if($res !== false) 738d5703f5aSAndreas Boehler { 739d5703f5aSAndreas Boehler $this->updateSyncTokenLog($calid, $uri, 'modified'); 740d5703f5aSAndreas Boehler } 741d5703f5aSAndreas Boehler return $extraData['etag']; 742d5703f5aSAndreas Boehler } 743d5703f5aSAndreas Boehler 744d5703f5aSAndreas Boehler /** 745cb71a62aSAndreas Boehler * Add a new iCal entry for a given page, i.e. a given calendar. 746cb71a62aSAndreas Boehler * 747cb71a62aSAndreas Boehler * The parameter array needs to contain 748cb71a62aSAndreas Boehler * detectedtz => The timezone as detected by the browser 74982a48dfbSAndreas Boehler * currenttz => The timezone in use by the calendar 750cb71a62aSAndreas Boehler * eventfrom => The event's start date 751cb71a62aSAndreas Boehler * eventfromtime => The event's start time 752cb71a62aSAndreas Boehler * eventto => The event's end date 753cb71a62aSAndreas Boehler * eventtotime => The event's end time 754cb71a62aSAndreas Boehler * eventname => The event's name 755cb71a62aSAndreas Boehler * eventdescription => The event's description 756cb71a62aSAndreas Boehler * 757cb71a62aSAndreas Boehler * @param string $id The page ID to work on 758cb71a62aSAndreas Boehler * @param string $user The user who created the calendar 759cb71a62aSAndreas Boehler * @param string $params A parameter array with values to create 760cb71a62aSAndreas Boehler * 761cb71a62aSAndreas Boehler * @return boolean True on success, otherwise false 762cb71a62aSAndreas Boehler */ 763a1a3b679SAndreas Boehler public function addCalendarEntryToCalendarForPage($id, $user, $params) 764a1a3b679SAndreas Boehler { 76582a48dfbSAndreas Boehler if($params['currenttz'] !== '' && $params['currenttz'] !== 'local') 76682a48dfbSAndreas Boehler $timezone = new \DateTimeZone($params['currenttz']); 76782a48dfbSAndreas Boehler elseif($params['currenttz'] === 'local') 768a25c89eaSAndreas Boehler $timezone = new \DateTimeZone($params['detectedtz']); 769bd883736SAndreas Boehler else 770bd883736SAndreas Boehler $timezone = new \DateTimeZone('UTC'); 771cb71a62aSAndreas Boehler 772cb71a62aSAndreas Boehler // Retrieve dates from settings 773b269830cSAndreas Boehler $startDate = explode('-', $params['eventfrom']); 774b269830cSAndreas Boehler $startTime = explode(':', $params['eventfromtime']); 775b269830cSAndreas Boehler $endDate = explode('-', $params['eventto']); 776b269830cSAndreas Boehler $endTime = explode(':', $params['eventtotime']); 777cb71a62aSAndreas Boehler 778cb71a62aSAndreas Boehler // Load SabreDAV 7799bef4ad8SAndreas Boehler require_once(DOKU_PLUGIN.'davcal/vendor/autoload.php'); 780a1a3b679SAndreas Boehler $vcalendar = new \Sabre\VObject\Component\VCalendar(); 781cb71a62aSAndreas Boehler 782cb71a62aSAndreas Boehler // Add VCalendar, UID and Event Name 783a1a3b679SAndreas Boehler $event = $vcalendar->add('VEVENT'); 784b269830cSAndreas Boehler $uuid = \Sabre\VObject\UUIDUtil::getUUID(); 785b269830cSAndreas Boehler $event->add('UID', $uuid); 786a1a3b679SAndreas Boehler $event->summary = $params['eventname']; 787cb71a62aSAndreas Boehler 788cb71a62aSAndreas Boehler // Add a description if requested 7890eebc909SAndreas Boehler $description = $params['eventdescription']; 7900eebc909SAndreas Boehler if($description !== '') 7910eebc909SAndreas Boehler $event->add('DESCRIPTION', $description); 792cb71a62aSAndreas Boehler 7932b7be5bdSAndreas Boehler // Add a location if requested 7942b7be5bdSAndreas Boehler $location = $params['eventlocation']; 7952b7be5bdSAndreas Boehler if($location !== '') 7962b7be5bdSAndreas Boehler $event->add('LOCATION', $location); 7972b7be5bdSAndreas Boehler 7984ecb526cSAndreas Boehler // Add attachments 7994ecb526cSAndreas Boehler $attachments = $params['attachments']; 80082a48dfbSAndreas Boehler if(!is_null($attachments)) 8014ecb526cSAndreas Boehler foreach($attachments as $attachment) 8024ecb526cSAndreas Boehler $event->add('ATTACH', $attachment); 8034ecb526cSAndreas Boehler 804cb71a62aSAndreas Boehler // Create a timestamp for last modified, created and dtstamp values in UTC 805b269830cSAndreas Boehler $dtStamp = new \DateTime(null, new \DateTimeZone('UTC')); 806b269830cSAndreas Boehler $event->add('DTSTAMP', $dtStamp); 807b269830cSAndreas Boehler $event->add('CREATED', $dtStamp); 808b269830cSAndreas Boehler $event->add('LAST-MODIFIED', $dtStamp); 809cb71a62aSAndreas Boehler 810cb71a62aSAndreas Boehler // Adjust the start date, based on the given timezone information 811b269830cSAndreas Boehler $dtStart = new \DateTime(); 812a25c89eaSAndreas Boehler $dtStart->setTimezone($timezone); 813b269830cSAndreas Boehler $dtStart->setDate(intval($startDate[0]), intval($startDate[1]), intval($startDate[2])); 814cb71a62aSAndreas Boehler 815cb71a62aSAndreas Boehler // Only add the time values if it's not an allday event 816b269830cSAndreas Boehler if($params['allday'] != '1') 817b269830cSAndreas Boehler $dtStart->setTime(intval($startTime[0]), intval($startTime[1]), 0); 818cb71a62aSAndreas Boehler 819cb71a62aSAndreas Boehler // Adjust the end date, based on the given timezone information 820b269830cSAndreas Boehler $dtEnd = new \DateTime(); 821a25c89eaSAndreas Boehler $dtEnd->setTimezone($timezone); 822b269830cSAndreas Boehler $dtEnd->setDate(intval($endDate[0]), intval($endDate[1]), intval($endDate[2])); 823cb71a62aSAndreas Boehler 824cb71a62aSAndreas Boehler // Only add the time values if it's not an allday event 825b269830cSAndreas Boehler if($params['allday'] != '1') 826b269830cSAndreas Boehler $dtEnd->setTime(intval($endTime[0]), intval($endTime[1]), 0); 827cb71a62aSAndreas Boehler 828b269830cSAndreas Boehler // According to the VCal spec, we need to add a whole day here 829b269830cSAndreas Boehler if($params['allday'] == '1') 830b269830cSAndreas Boehler $dtEnd->add(new \DateInterval('P1D')); 831cb71a62aSAndreas Boehler 832cb71a62aSAndreas Boehler // Really add Start and End events 833b269830cSAndreas Boehler $dtStartEv = $event->add('DTSTART', $dtStart); 834b269830cSAndreas Boehler $dtEndEv = $event->add('DTEND', $dtEnd); 835cb71a62aSAndreas Boehler 836cb71a62aSAndreas Boehler // Adjust the DATE format for allday events 837b269830cSAndreas Boehler if($params['allday'] == '1') 838b269830cSAndreas Boehler { 839b269830cSAndreas Boehler $dtStartEv['VALUE'] = 'DATE'; 840b269830cSAndreas Boehler $dtEndEv['VALUE'] = 'DATE'; 841b269830cSAndreas Boehler } 842cb71a62aSAndreas Boehler 843809cb0faSAndreas Boehler $eventStr = $vcalendar->serialize(); 844809cb0faSAndreas Boehler 845809cb0faSAndreas Boehler if(strpos($id, 'webdav://') === 0) 846809cb0faSAndreas Boehler { 847809cb0faSAndreas Boehler $wdc =& plugin_load('helper', 'webdavclient'); 848809cb0faSAndreas Boehler if(is_null($wdc)) 849809cb0faSAndreas Boehler return false; 850809cb0faSAndreas Boehler $connectionId = str_replace('webdav://', '', $id); 851809cb0faSAndreas Boehler return $wdc->addCalendarEntry($connectionId, $eventStr); 852809cb0faSAndreas Boehler } 853809cb0faSAndreas Boehler else 854809cb0faSAndreas Boehler { 855cb71a62aSAndreas Boehler // Actually add the values to the database 856a1a3b679SAndreas Boehler $calid = $this->getCalendarIdForPage($id); 85702eea9b8SAndreas Boehler $uri = $uri = 'dokuwiki-' . bin2hex(random_bytes(16)) . '.ics'; 8581bb22c2bSAndreas Boehler $now = new \DateTime(); 859a1a3b679SAndreas Boehler 8605f2c3e2dSAndreas Boehler $sqlite = $this->getDB(); 8615f2c3e2dSAndreas Boehler if(!$sqlite) 8625f2c3e2dSAndreas Boehler return false; 86351f4febbSAndreas Boehler $query = "INSERT INTO calendarobjects (calendarid, uri, calendardata, lastmodified, componenttype, firstoccurence, lastoccurence, size, etag, uid) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; 8645f2c3e2dSAndreas Boehler $res = $sqlite->query($query, $calid, $uri, $eventStr, $now->getTimestamp(), 'VEVENT', 86551f4febbSAndreas Boehler $event->DTSTART->getDateTime()->getTimeStamp(), $event->DTEND->getDateTime()->getTimeStamp(), 86651f4febbSAndreas Boehler strlen($eventStr), md5($eventStr), $uuid); 867cb71a62aSAndreas Boehler 868cb71a62aSAndreas Boehler // If successfully, update the sync token database 86955a741c0SAndreas Boehler if($res !== false) 87055a741c0SAndreas Boehler { 87155a741c0SAndreas Boehler $this->updateSyncTokenLog($calid, $uri, 'added'); 872a1a3b679SAndreas Boehler return true; 873a1a3b679SAndreas Boehler } 874809cb0faSAndreas Boehler } 87555a741c0SAndreas Boehler return false; 87655a741c0SAndreas Boehler } 877a1a3b679SAndreas Boehler 878cb71a62aSAndreas Boehler /** 879cb71a62aSAndreas Boehler * Retrieve the calendar settings of a given calendar id 880cb71a62aSAndreas Boehler * 881cb71a62aSAndreas Boehler * @param string $calid The calendar ID 882cb71a62aSAndreas Boehler * 883cb71a62aSAndreas Boehler * @return array The calendar settings array 884cb71a62aSAndreas Boehler */ 885b269830cSAndreas Boehler public function getCalendarSettings($calid) 886b269830cSAndreas Boehler { 8875f2c3e2dSAndreas Boehler $sqlite = $this->getDB(); 8885f2c3e2dSAndreas Boehler if(!$sqlite) 8895f2c3e2dSAndreas Boehler return false; 89013b16484SAndreas Boehler $query = "SELECT id, principaluri, calendarcolor, displayname, uri, description, components, transparent, synctoken, disabled FROM calendars WHERE id= ? "; 8915f2c3e2dSAndreas Boehler $res = $sqlite->query($query, $calid); 8925f2c3e2dSAndreas Boehler $row = $sqlite->res2row($res); 893b269830cSAndreas Boehler return $row; 894b269830cSAndreas Boehler } 895b269830cSAndreas Boehler 896cb71a62aSAndreas Boehler /** 89713b16484SAndreas Boehler * Retrieve the calendar status of a given calendar id 89813b16484SAndreas Boehler * 89913b16484SAndreas Boehler * @param string $calid The calendar ID 90013b16484SAndreas Boehler * @return boolean True if calendar is enabled, otherwise false 90113b16484SAndreas Boehler */ 90213b16484SAndreas Boehler public function getCalendarStatus($calid) 90313b16484SAndreas Boehler { 9045f2c3e2dSAndreas Boehler $sqlite = $this->getDB(); 9055f2c3e2dSAndreas Boehler if(!$sqlite) 9065f2c3e2dSAndreas Boehler return false; 90713b16484SAndreas Boehler $query = "SELECT disabled FROM calendars WHERE id = ?"; 9085f2c3e2dSAndreas Boehler $res = $sqlite->query($query, $calid); 9095f2c3e2dSAndreas Boehler $row = $sqlite->res2row($res); 91013b16484SAndreas Boehler if($row['disabled'] == 1) 91113b16484SAndreas Boehler return false; 91213b16484SAndreas Boehler else 91313b16484SAndreas Boehler return true; 91413b16484SAndreas Boehler } 91513b16484SAndreas Boehler 91613b16484SAndreas Boehler /** 91713b16484SAndreas Boehler * Disable a calendar for a given page 91813b16484SAndreas Boehler * 91913b16484SAndreas Boehler * @param string $id The page ID 92013b16484SAndreas Boehler * 92113b16484SAndreas Boehler * @return boolean true on success, otherwise false 92213b16484SAndreas Boehler */ 92313b16484SAndreas Boehler public function disableCalendarForPage($id) 92413b16484SAndreas Boehler { 92513b16484SAndreas Boehler $calid = $this->getCalendarIdForPage($id); 92613b16484SAndreas Boehler if($calid === false) 92713b16484SAndreas Boehler return false; 9285f2c3e2dSAndreas Boehler 9295f2c3e2dSAndreas Boehler $sqlite = $this->getDB(); 9305f2c3e2dSAndreas Boehler if(!$sqlite) 9315f2c3e2dSAndreas Boehler return false; 93213b16484SAndreas Boehler $query = "UPDATE calendars SET disabled = 1 WHERE id = ?"; 9335f2c3e2dSAndreas Boehler $res = $sqlite->query($query, $calid); 93413b16484SAndreas Boehler if($res !== false) 93513b16484SAndreas Boehler return true; 93613b16484SAndreas Boehler return false; 93713b16484SAndreas Boehler } 93813b16484SAndreas Boehler 93913b16484SAndreas Boehler /** 94013b16484SAndreas Boehler * Enable a calendar for a given page 94113b16484SAndreas Boehler * 94213b16484SAndreas Boehler * @param string $id The page ID 94313b16484SAndreas Boehler * 94413b16484SAndreas Boehler * @return boolean true on success, otherwise false 94513b16484SAndreas Boehler */ 94613b16484SAndreas Boehler public function enableCalendarForPage($id) 94713b16484SAndreas Boehler { 94813b16484SAndreas Boehler $calid = $this->getCalendarIdForPage($id); 94913b16484SAndreas Boehler if($calid === false) 95013b16484SAndreas Boehler return false; 9515f2c3e2dSAndreas Boehler $sqlite = $this->getDB(); 9525f2c3e2dSAndreas Boehler if(!$sqlite) 9535f2c3e2dSAndreas Boehler return false; 95413b16484SAndreas Boehler $query = "UPDATE calendars SET disabled = 0 WHERE id = ?"; 9555f2c3e2dSAndreas Boehler $res = $sqlite->query($query, $calid); 95613b16484SAndreas Boehler if($res !== false) 95713b16484SAndreas Boehler return true; 95813b16484SAndreas Boehler return false; 95913b16484SAndreas Boehler } 96013b16484SAndreas Boehler 96113b16484SAndreas Boehler /** 962cb71a62aSAndreas Boehler * Retrieve all events that are within a given date range, 963cb71a62aSAndreas Boehler * based on the timezone setting. 964cb71a62aSAndreas Boehler * 965cb71a62aSAndreas Boehler * There is also support for retrieving recurring events, 966cb71a62aSAndreas Boehler * using Sabre's VObject Iterator. Recurring events are represented 967cb71a62aSAndreas Boehler * as individual calendar entries with the same UID. 968cb71a62aSAndreas Boehler * 969cb71a62aSAndreas Boehler * @param string $id The page ID to work with 970cb71a62aSAndreas Boehler * @param string $user The user ID to work with 971cb71a62aSAndreas Boehler * @param string $startDate The start date as a string 972cb71a62aSAndreas Boehler * @param string $endDate The end date as a string 9734a2bf5eeSAndreas Boehler * @param string $color (optional) The calendar's color 974cb71a62aSAndreas Boehler * 975cb71a62aSAndreas Boehler * @return array An array containing the calendar entries. 976cb71a62aSAndreas Boehler */ 9774a2bf5eeSAndreas Boehler public function getEventsWithinDateRange($id, $user, $startDate, $endDate, $timezone, $color = null) 978a1a3b679SAndreas Boehler { 97982a48dfbSAndreas Boehler if($timezone !== '' && $timezone !== 'local') 98082a48dfbSAndreas Boehler $timezone = new \DateTimeZone($timezone); 981bd883736SAndreas Boehler else 982bd883736SAndreas Boehler $timezone = new \DateTimeZone('UTC'); 983a1a3b679SAndreas Boehler $data = array(); 984cb71a62aSAndreas Boehler 9855f2c3e2dSAndreas Boehler $sqlite = $this->getDB(); 9865f2c3e2dSAndreas Boehler if(!$sqlite) 9875f2c3e2dSAndreas Boehler return false; 9885f2c3e2dSAndreas Boehler 989a469597cSAndreas Boehler $query = "SELECT calendardata, componenttype, uid FROM calendarobjects WHERE calendarid = ?"; 990a469597cSAndreas Boehler $startTs = null; 991a469597cSAndreas Boehler $endTs = null; 992a469597cSAndreas Boehler if($startDate !== null) 993a469597cSAndreas Boehler { 994a1a3b679SAndreas Boehler $startTs = new \DateTime($startDate); 9955f2c3e2dSAndreas Boehler $query .= " AND lastoccurence > ".$sqlite->quote_string($startTs->getTimestamp()); 996a469597cSAndreas Boehler } 997a469597cSAndreas Boehler if($endDate !== null) 998a469597cSAndreas Boehler { 999a1a3b679SAndreas Boehler $endTs = new \DateTime($endDate); 10005f2c3e2dSAndreas Boehler $query .= " AND firstoccurence < ".$sqlite->quote_string($endTs->getTimestamp()); 1001a469597cSAndreas Boehler } 1002cb71a62aSAndreas Boehler 10030b805092SAndreas Boehler // Load SabreDAV 10040b805092SAndreas Boehler require_once(DOKU_PLUGIN.'davcal/vendor/autoload.php'); 10050b805092SAndreas Boehler 10060b805092SAndreas Boehler if(strpos($id, 'webdav://') === 0) 10070b805092SAndreas Boehler { 10080b805092SAndreas Boehler $wdc =& plugin_load('helper', 'webdavclient'); 10090b805092SAndreas Boehler if(is_null($wdc)) 10100b805092SAndreas Boehler return $data; 10110b805092SAndreas Boehler $connectionId = str_replace('webdav://', '', $id); 10120b805092SAndreas Boehler $arr = $wdc->getCalendarEntries($connectionId, $startDate, $endDate); 10130b805092SAndreas Boehler } 10140b805092SAndreas Boehler else 10150b805092SAndreas Boehler { 10160b805092SAndreas Boehler $calid = $this->getCalendarIdForPage($id); 10170b805092SAndreas Boehler if(is_null($color)) 10180b805092SAndreas Boehler $color = $this->getCalendarColorForCalendar($calid); 10190b805092SAndreas Boehler 102059b68239SAndreas Boehler $enabled = $this->getCalendarStatus($calid); 102159b68239SAndreas Boehler if($enabled === false) 102259b68239SAndreas Boehler return $data; 102359b68239SAndreas Boehler 1024cb71a62aSAndreas Boehler // Retrieve matching calendar objects 10255f2c3e2dSAndreas Boehler $res = $sqlite->query($query, $calid); 10265f2c3e2dSAndreas Boehler $arr = $sqlite->res2arr($res); 10270b805092SAndreas Boehler } 1028cb71a62aSAndreas Boehler 1029cb71a62aSAndreas Boehler // Parse individual calendar entries 1030a1a3b679SAndreas Boehler foreach($arr as $row) 1031a1a3b679SAndreas Boehler { 1032a1a3b679SAndreas Boehler if(isset($row['calendardata'])) 1033a1a3b679SAndreas Boehler { 1034b269830cSAndreas Boehler $entry = array(); 1035a1a3b679SAndreas Boehler $vcal = \Sabre\VObject\Reader::read($row['calendardata']); 1036ebc4eb57SAndreas Boehler $recurrence = $vcal->VEVENT->RRULE; 1037cb71a62aSAndreas Boehler // If it is a recurring event, pass it through Sabre's EventIterator 1038ebc4eb57SAndreas Boehler if($recurrence != null) 1039ebc4eb57SAndreas Boehler { 1040ebc4eb57SAndreas Boehler $rEvents = new \Sabre\VObject\Recur\EventIterator(array($vcal->VEVENT)); 1041ebc4eb57SAndreas Boehler $rEvents->rewind(); 1042e9b7d302SAndreas Boehler while($rEvents->valid()) 1043ebc4eb57SAndreas Boehler { 1044ebc4eb57SAndreas Boehler $event = $rEvents->getEventObject(); 1045cb71a62aSAndreas Boehler // If we are after the given time range, exit 1046a469597cSAndreas Boehler if(($endTs !== null) && ($rEvents->getDtStart()->getTimestamp() > $endTs->getTimestamp())) 1047e9b7d302SAndreas Boehler break; 1048cb71a62aSAndreas Boehler 1049cb71a62aSAndreas Boehler // If we are before the given time range, continue 1050a469597cSAndreas Boehler if(($startTs != null) && ($rEvents->getDtEnd()->getTimestamp() < $startTs->getTimestamp())) 1051ebc4eb57SAndreas Boehler { 1052ebc4eb57SAndreas Boehler $rEvents->next(); 1053ebc4eb57SAndreas Boehler continue; 1054ebc4eb57SAndreas Boehler } 1055cb71a62aSAndreas Boehler 1056cb71a62aSAndreas Boehler // If we are within the given time range, parse the event 1057185e2535SAndreas Boehler $data[] = $this->convertIcalDataToEntry($event, $id, $timezone, $row['uid'], $color, true); 1058ebc4eb57SAndreas Boehler $rEvents->next(); 1059ebc4eb57SAndreas Boehler } 1060ebc4eb57SAndreas Boehler } 1061ebc4eb57SAndreas Boehler else 1062185e2535SAndreas Boehler $data[] = $this->convertIcalDataToEntry($vcal->VEVENT, $id, $timezone, $row['uid'], $color); 1063ebc4eb57SAndreas Boehler } 1064ebc4eb57SAndreas Boehler } 1065ebc4eb57SAndreas Boehler return $data; 1066ebc4eb57SAndreas Boehler } 1067ebc4eb57SAndreas Boehler 1068cb71a62aSAndreas Boehler /** 1069cb71a62aSAndreas Boehler * Helper function that parses the iCal data of a VEVENT to a calendar entry. 1070cb71a62aSAndreas Boehler * 1071cb71a62aSAndreas Boehler * @param \Sabre\VObject\VEvent $event The event to parse 1072cb71a62aSAndreas Boehler * @param \DateTimeZone $timezone The timezone object 1073cb71a62aSAndreas Boehler * @param string $uid The entry's UID 10743c86dda8SAndreas Boehler * @param boolean $recurring (optional) Set to true to define a recurring event 1075cb71a62aSAndreas Boehler * 1076cb71a62aSAndreas Boehler * @return array The parse calendar entry 1077cb71a62aSAndreas Boehler */ 1078185e2535SAndreas Boehler private function convertIcalDataToEntry($event, $page, $timezone, $uid, $color, $recurring = false) 1079ebc4eb57SAndreas Boehler { 1080ebc4eb57SAndreas Boehler $entry = array(); 1081ebc4eb57SAndreas Boehler $start = $event->DTSTART; 1082cb71a62aSAndreas Boehler // Parse only if the start date/time is present 1083b269830cSAndreas Boehler if($start !== null) 1084b269830cSAndreas Boehler { 1085b269830cSAndreas Boehler $dtStart = $start->getDateTime(); 1086b269830cSAndreas Boehler $dtStart->setTimezone($timezone); 1087bf0ad2b4SAndreas Boehler 1088bf0ad2b4SAndreas Boehler // moment.js doesn't like times be given even if 1089bf0ad2b4SAndreas Boehler // allDay is set to true 1090bf0ad2b4SAndreas Boehler // This should fix T23 1091b269830cSAndreas Boehler if($start['VALUE'] == 'DATE') 1092bf0ad2b4SAndreas Boehler { 1093b269830cSAndreas Boehler $entry['allDay'] = true; 1094bf0ad2b4SAndreas Boehler $entry['start'] = $dtStart->format("Y-m-d"); 1095bf0ad2b4SAndreas Boehler } 1096b269830cSAndreas Boehler else 1097bf0ad2b4SAndreas Boehler { 1098b269830cSAndreas Boehler $entry['allDay'] = false; 1099bf0ad2b4SAndreas Boehler $entry['start'] = $dtStart->format(\DateTime::ATOM); 1100bf0ad2b4SAndreas Boehler } 1101b269830cSAndreas Boehler } 1102ebc4eb57SAndreas Boehler $end = $event->DTEND; 1103bf0ad2b4SAndreas Boehler // Parse only if the end date/time is present 1104b269830cSAndreas Boehler if($end !== null) 1105b269830cSAndreas Boehler { 1106b269830cSAndreas Boehler $dtEnd = $end->getDateTime(); 1107b269830cSAndreas Boehler $dtEnd->setTimezone($timezone); 1108bf0ad2b4SAndreas Boehler if($end['VALUE'] == 'DATE') 1109bf0ad2b4SAndreas Boehler $entry['end'] = $dtEnd->format("Y-m-d"); 1110bf0ad2b4SAndreas Boehler else 1111b269830cSAndreas Boehler $entry['end'] = $dtEnd->format(\DateTime::ATOM); 1112b269830cSAndreas Boehler } 1113ebc4eb57SAndreas Boehler $description = $event->DESCRIPTION; 11140eebc909SAndreas Boehler if($description !== null) 11150eebc909SAndreas Boehler $entry['description'] = (string)$description; 11160eebc909SAndreas Boehler else 11170eebc909SAndreas Boehler $entry['description'] = ''; 11184ecb526cSAndreas Boehler $attachments = $event->ATTACH; 11194ecb526cSAndreas Boehler if($attachments !== null) 11204ecb526cSAndreas Boehler { 11214ecb526cSAndreas Boehler $entry['attachments'] = array(); 11224ecb526cSAndreas Boehler foreach($attachments as $attachment) 11234ecb526cSAndreas Boehler $entry['attachments'][] = (string)$attachment; 11244ecb526cSAndreas Boehler } 1125ebc4eb57SAndreas Boehler $entry['title'] = (string)$event->summary; 11262b7be5bdSAndreas Boehler $entry['location'] = (string)$event->location; 1127ebc4eb57SAndreas Boehler $entry['id'] = $uid; 1128185e2535SAndreas Boehler $entry['page'] = $page; 1129185e2535SAndreas Boehler $entry['color'] = $color; 11303c86dda8SAndreas Boehler $entry['recurring'] = $recurring; 1131185e2535SAndreas Boehler 1132ebc4eb57SAndreas Boehler return $entry; 1133a1a3b679SAndreas Boehler } 1134a1a3b679SAndreas Boehler 1135cb71a62aSAndreas Boehler /** 1136cb71a62aSAndreas Boehler * Retrieve an event by its UID 1137cb71a62aSAndreas Boehler * 1138cb71a62aSAndreas Boehler * @param string $uid The event's UID 1139cb71a62aSAndreas Boehler * 1140cb71a62aSAndreas Boehler * @return mixed The table row with the given event 1141cb71a62aSAndreas Boehler */ 1142a1a3b679SAndreas Boehler public function getEventWithUid($uid) 1143a1a3b679SAndreas Boehler { 11445f2c3e2dSAndreas Boehler $sqlite = $this->getDB(); 11455f2c3e2dSAndreas Boehler if(!$sqlite) 11465f2c3e2dSAndreas Boehler return false; 114751f4febbSAndreas Boehler $query = "SELECT calendardata, calendarid, componenttype, uri FROM calendarobjects WHERE uid = ?"; 11485f2c3e2dSAndreas Boehler $res = $sqlite->query($query, $uid); 11495f2c3e2dSAndreas Boehler $row = $sqlite->res2row($res); 1150a1a3b679SAndreas Boehler return $row; 1151a1a3b679SAndreas Boehler } 1152a1a3b679SAndreas Boehler 1153cb71a62aSAndreas Boehler /** 1154d5703f5aSAndreas Boehler * Retrieve information of a calendar's object, not including the actual 115559b68239SAndreas Boehler * calendar data! This is mainly needed for the sync support. 1156d5703f5aSAndreas Boehler * 1157d5703f5aSAndreas Boehler * @param int $calid The calendar ID 1158d5703f5aSAndreas Boehler * 1159d5703f5aSAndreas Boehler * @return mixed The result 1160d5703f5aSAndreas Boehler */ 1161d5703f5aSAndreas Boehler public function getCalendarObjects($calid) 1162d5703f5aSAndreas Boehler { 11635f2c3e2dSAndreas Boehler $sqlite = $this->getDB(); 11645f2c3e2dSAndreas Boehler if(!$sqlite) 11655f2c3e2dSAndreas Boehler return false; 1166d5703f5aSAndreas Boehler $query = "SELECT id, uri, lastmodified, etag, calendarid, size, componenttype FROM calendarobjects WHERE calendarid = ?"; 11675f2c3e2dSAndreas Boehler $res = $sqlite->query($query, $calid); 11685f2c3e2dSAndreas Boehler $arr = $sqlite->res2arr($res); 1169d5703f5aSAndreas Boehler return $arr; 1170d5703f5aSAndreas Boehler } 1171d5703f5aSAndreas Boehler 1172d5703f5aSAndreas Boehler /** 1173d5703f5aSAndreas Boehler * Retrieve a single calendar object by calendar ID and URI 1174d5703f5aSAndreas Boehler * 1175d5703f5aSAndreas Boehler * @param int $calid The calendar's ID 1176d5703f5aSAndreas Boehler * @param string $uri The object's URI 1177d5703f5aSAndreas Boehler * 1178d5703f5aSAndreas Boehler * @return mixed The result 1179d5703f5aSAndreas Boehler */ 1180d5703f5aSAndreas Boehler public function getCalendarObjectByUri($calid, $uri) 1181d5703f5aSAndreas Boehler { 11825f2c3e2dSAndreas Boehler $sqlite = $this->getDB(); 11835f2c3e2dSAndreas Boehler if(!$sqlite) 11845f2c3e2dSAndreas Boehler return false; 1185d5703f5aSAndreas Boehler $query = "SELECT id, uri, lastmodified, etag, calendarid, size, calendardata, componenttype FROM calendarobjects WHERE calendarid = ? AND uri = ?"; 11865f2c3e2dSAndreas Boehler $res = $sqlite->query($query, $calid, $uri); 11875f2c3e2dSAndreas Boehler $row = $sqlite->res2row($res); 1188d5703f5aSAndreas Boehler return $row; 1189d5703f5aSAndreas Boehler } 1190d5703f5aSAndreas Boehler 1191d5703f5aSAndreas Boehler /** 1192d5703f5aSAndreas Boehler * Retrieve several calendar objects by specifying an array of URIs. 1193d5703f5aSAndreas Boehler * This is mainly neede for sync. 1194d5703f5aSAndreas Boehler * 1195d5703f5aSAndreas Boehler * @param int $calid The calendar's ID 1196d5703f5aSAndreas Boehler * @param array $uris An array of URIs 1197d5703f5aSAndreas Boehler * 1198d5703f5aSAndreas Boehler * @return mixed The result 1199d5703f5aSAndreas Boehler */ 1200d5703f5aSAndreas Boehler public function getMultipleCalendarObjectsByUri($calid, $uris) 1201d5703f5aSAndreas Boehler { 12025f2c3e2dSAndreas Boehler $sqlite = $this->getDB(); 12035f2c3e2dSAndreas Boehler if(!$sqlite) 12045f2c3e2dSAndreas Boehler return false; 1205d5703f5aSAndreas Boehler $query = "SELECT id, uri, lastmodified, etag, calendarid, size, calendardata, componenttype FROM calendarobjects WHERE calendarid = ? AND uri IN ("; 1206d5703f5aSAndreas Boehler // Inserting a whole bunch of question marks 1207d5703f5aSAndreas Boehler $query .= implode(',', array_fill(0, count($uris), '?')); 1208d5703f5aSAndreas Boehler $query .= ')'; 1209d5703f5aSAndreas Boehler $vals = array_merge(array($calid), $uris); 1210d5703f5aSAndreas Boehler 12115f2c3e2dSAndreas Boehler $res = $sqlite->query($query, $vals); 12125f2c3e2dSAndreas Boehler $arr = $sqlite->res2arr($res); 1213d5703f5aSAndreas Boehler return $arr; 1214d5703f5aSAndreas Boehler } 1215d5703f5aSAndreas Boehler 1216d5703f5aSAndreas Boehler /** 1217cb71a62aSAndreas Boehler * Retrieve all calendar events for a given calendar ID 1218cb71a62aSAndreas Boehler * 1219cb71a62aSAndreas Boehler * @param string $calid The calendar's ID 1220cb71a62aSAndreas Boehler * 1221cb71a62aSAndreas Boehler * @return array An array containing all calendar data 1222cb71a62aSAndreas Boehler */ 1223f69bb449SAndreas Boehler public function getAllCalendarEvents($calid) 1224f69bb449SAndreas Boehler { 122559b68239SAndreas Boehler $enabled = $this->getCalendarStatus($calid); 122659b68239SAndreas Boehler if($enabled === false) 122759b68239SAndreas Boehler return false; 12285f2c3e2dSAndreas Boehler 12295f2c3e2dSAndreas Boehler $sqlite = $this->getDB(); 12305f2c3e2dSAndreas Boehler if(!$sqlite) 12315f2c3e2dSAndreas Boehler return false; 12327e0b8590SAndreas Boehler $query = "SELECT calendardata, uid, componenttype, uri FROM calendarobjects WHERE calendarid = ?"; 12335f2c3e2dSAndreas Boehler $res = $sqlite->query($query, $calid); 12345f2c3e2dSAndreas Boehler $arr = $sqlite->res2arr($res); 1235f69bb449SAndreas Boehler return $arr; 1236f69bb449SAndreas Boehler } 1237f69bb449SAndreas Boehler 1238cb71a62aSAndreas Boehler /** 1239cb71a62aSAndreas Boehler * Edit a calendar entry for a page, given by its parameters. 1240cb71a62aSAndreas Boehler * The params array has the same format as @see addCalendarEntryForPage 1241cb71a62aSAndreas Boehler * 1242cb71a62aSAndreas Boehler * @param string $id The page's ID to work on 1243cb71a62aSAndreas Boehler * @param string $user The user's ID to work on 1244cb71a62aSAndreas Boehler * @param array $params The parameter array for the edited calendar event 1245cb71a62aSAndreas Boehler * 1246cb71a62aSAndreas Boehler * @return boolean True on success, otherwise false 1247cb71a62aSAndreas Boehler */ 1248a1a3b679SAndreas Boehler public function editCalendarEntryForPage($id, $user, $params) 1249a1a3b679SAndreas Boehler { 125082a48dfbSAndreas Boehler if($params['currenttz'] !== '' && $params['currenttz'] !== 'local') 125182a48dfbSAndreas Boehler $timezone = new \DateTimeZone($params['currenttz']); 125282a48dfbSAndreas Boehler elseif($params['currenttz'] === 'local') 1253a25c89eaSAndreas Boehler $timezone = new \DateTimeZone($params['detectedtz']); 1254bd883736SAndreas Boehler else 1255bd883736SAndreas Boehler $timezone = new \DateTimeZone('UTC'); 1256cb71a62aSAndreas Boehler 1257cb71a62aSAndreas Boehler // Parse dates 1258b269830cSAndreas Boehler $startDate = explode('-', $params['eventfrom']); 1259b269830cSAndreas Boehler $startTime = explode(':', $params['eventfromtime']); 1260b269830cSAndreas Boehler $endDate = explode('-', $params['eventto']); 1261b269830cSAndreas Boehler $endTime = explode(':', $params['eventtotime']); 1262cb71a62aSAndreas Boehler 1263cb71a62aSAndreas Boehler // Retrieve the existing event based on the UID 126455a741c0SAndreas Boehler $uid = $params['uid']; 1265809cb0faSAndreas Boehler 1266809cb0faSAndreas Boehler if(strpos($id, 'webdav://') === 0) 1267809cb0faSAndreas Boehler { 1268809cb0faSAndreas Boehler $wdc =& plugin_load('helper', 'webdavclient'); 1269809cb0faSAndreas Boehler if(is_null($wdc)) 1270809cb0faSAndreas Boehler return false; 1271809cb0faSAndreas Boehler $event = $wdc->getCalendarEntryByUid($uid); 1272809cb0faSAndreas Boehler } 1273809cb0faSAndreas Boehler else 1274809cb0faSAndreas Boehler { 127555a741c0SAndreas Boehler $event = $this->getEventWithUid($uid); 1276809cb0faSAndreas Boehler } 1277cb71a62aSAndreas Boehler 1278cb71a62aSAndreas Boehler // Load SabreDAV 12799bef4ad8SAndreas Boehler require_once(DOKU_PLUGIN.'davcal/vendor/autoload.php'); 1280a1a3b679SAndreas Boehler if(!isset($event['calendardata'])) 1281a1a3b679SAndreas Boehler return false; 128255a741c0SAndreas Boehler $uri = $event['uri']; 128355a741c0SAndreas Boehler $calid = $event['calendarid']; 1284cb71a62aSAndreas Boehler 1285cb71a62aSAndreas Boehler // Parse the existing event 1286a1a3b679SAndreas Boehler $vcal = \Sabre\VObject\Reader::read($event['calendardata']); 1287b269830cSAndreas Boehler $vevent = $vcal->VEVENT; 1288cb71a62aSAndreas Boehler 1289cb71a62aSAndreas Boehler // Set the new event values 1290b269830cSAndreas Boehler $vevent->summary = $params['eventname']; 1291b269830cSAndreas Boehler $dtStamp = new \DateTime(null, new \DateTimeZone('UTC')); 12920eebc909SAndreas Boehler $description = $params['eventdescription']; 12932b7be5bdSAndreas Boehler $location = $params['eventlocation']; 1294cb71a62aSAndreas Boehler 1295cb71a62aSAndreas Boehler // Remove existing timestamps to overwrite them 12960eebc909SAndreas Boehler $vevent->remove('DESCRIPTION'); 1297b269830cSAndreas Boehler $vevent->remove('DTSTAMP'); 1298b269830cSAndreas Boehler $vevent->remove('LAST-MODIFIED'); 12994ecb526cSAndreas Boehler $vevent->remove('ATTACH'); 13002b7be5bdSAndreas Boehler $vevent->remove('LOCATION'); 1301cb71a62aSAndreas Boehler 13022b7be5bdSAndreas Boehler // Add new time stamps, description and location 1303b269830cSAndreas Boehler $vevent->add('DTSTAMP', $dtStamp); 1304b269830cSAndreas Boehler $vevent->add('LAST-MODIFIED', $dtStamp); 13050eebc909SAndreas Boehler if($description !== '') 13060eebc909SAndreas Boehler $vevent->add('DESCRIPTION', $description); 13072b7be5bdSAndreas Boehler if($location !== '') 13082b7be5bdSAndreas Boehler $vevent->add('LOCATION', $location); 1309cb71a62aSAndreas Boehler 13104ecb526cSAndreas Boehler // Add attachments 13114ecb526cSAndreas Boehler $attachments = $params['attachments']; 131282a48dfbSAndreas Boehler if(!is_null($attachments)) 13134ecb526cSAndreas Boehler foreach($attachments as $attachment) 13144ecb526cSAndreas Boehler $vevent->add('ATTACH', $attachment); 13154ecb526cSAndreas Boehler 1316cb71a62aSAndreas Boehler // Setup DTSTART 1317b269830cSAndreas Boehler $dtStart = new \DateTime(); 1318a25c89eaSAndreas Boehler $dtStart->setTimezone($timezone); 1319b269830cSAndreas Boehler $dtStart->setDate(intval($startDate[0]), intval($startDate[1]), intval($startDate[2])); 1320b269830cSAndreas Boehler if($params['allday'] != '1') 1321b269830cSAndreas Boehler $dtStart->setTime(intval($startTime[0]), intval($startTime[1]), 0); 1322cb71a62aSAndreas Boehler 13234ecb526cSAndreas Boehler // Setup DTEND 1324b269830cSAndreas Boehler $dtEnd = new \DateTime(); 1325a25c89eaSAndreas Boehler $dtEnd->setTimezone($timezone); 1326b269830cSAndreas Boehler $dtEnd->setDate(intval($endDate[0]), intval($endDate[1]), intval($endDate[2])); 1327b269830cSAndreas Boehler if($params['allday'] != '1') 1328b269830cSAndreas Boehler $dtEnd->setTime(intval($endTime[0]), intval($endTime[1]), 0); 1329cb71a62aSAndreas Boehler 1330b269830cSAndreas Boehler // According to the VCal spec, we need to add a whole day here 1331b269830cSAndreas Boehler if($params['allday'] == '1') 1332b269830cSAndreas Boehler $dtEnd->add(new \DateInterval('P1D')); 1333b269830cSAndreas Boehler $vevent->remove('DTSTART'); 1334b269830cSAndreas Boehler $vevent->remove('DTEND'); 1335b269830cSAndreas Boehler $dtStartEv = $vevent->add('DTSTART', $dtStart); 1336b269830cSAndreas Boehler $dtEndEv = $vevent->add('DTEND', $dtEnd); 1337cb71a62aSAndreas Boehler 1338cb71a62aSAndreas Boehler // Remove the time for allday events 1339b269830cSAndreas Boehler if($params['allday'] == '1') 1340b269830cSAndreas Boehler { 1341b269830cSAndreas Boehler $dtStartEv['VALUE'] = 'DATE'; 1342b269830cSAndreas Boehler $dtEndEv['VALUE'] = 'DATE'; 1343b269830cSAndreas Boehler } 1344a1a3b679SAndreas Boehler $eventStr = $vcal->serialize(); 1345809cb0faSAndreas Boehler if(strpos($id, 'webdav://') === 0) 1346809cb0faSAndreas Boehler { 1347809cb0faSAndreas Boehler $connectionId = str_replace('webdav://', '', $id); 1348809cb0faSAndreas Boehler return $wdc->editCalendarEntry($connectionId, $uid, $eventStr); 1349809cb0faSAndreas Boehler } 1350809cb0faSAndreas Boehler else 1351809cb0faSAndreas Boehler { 13525f2c3e2dSAndreas Boehler $sqlite = $this->getDB(); 13535f2c3e2dSAndreas Boehler if(!$sqlite) 13545f2c3e2dSAndreas Boehler return false; 1355809cb0faSAndreas Boehler $now = new DateTime(); 1356cb71a62aSAndreas Boehler // Actually write to the database 135751f4febbSAndreas Boehler $query = "UPDATE calendarobjects SET calendardata = ?, lastmodified = ?, ". 135851f4febbSAndreas Boehler "firstoccurence = ?, lastoccurence = ?, size = ?, etag = ? WHERE uid = ?"; 13595f2c3e2dSAndreas Boehler $res = $sqlite->query($query, $eventStr, $now->getTimestamp(), $dtStart->getTimestamp(), 136051f4febbSAndreas Boehler $dtEnd->getTimestamp(), strlen($eventStr), md5($eventStr), $uid); 136155a741c0SAndreas Boehler if($res !== false) 136255a741c0SAndreas Boehler { 136355a741c0SAndreas Boehler $this->updateSyncTokenLog($calid, $uri, 'modified'); 1364a1a3b679SAndreas Boehler return true; 1365a1a3b679SAndreas Boehler } 1366809cb0faSAndreas Boehler } 136755a741c0SAndreas Boehler return false; 136855a741c0SAndreas Boehler } 1369a1a3b679SAndreas Boehler 1370cb71a62aSAndreas Boehler /** 1371d5703f5aSAndreas Boehler * Delete an event from a calendar by calendar ID and URI 1372d5703f5aSAndreas Boehler * 1373d5703f5aSAndreas Boehler * @param int $calid The calendar's ID 1374d5703f5aSAndreas Boehler * @param string $uri The object's URI 1375d5703f5aSAndreas Boehler * 1376d5703f5aSAndreas Boehler * @return true 1377d5703f5aSAndreas Boehler */ 1378d5703f5aSAndreas Boehler public function deleteCalendarEntryForCalendarByUri($calid, $uri) 1379d5703f5aSAndreas Boehler { 13805f2c3e2dSAndreas Boehler $sqlite = $this->getDB(); 13815f2c3e2dSAndreas Boehler if(!$sqlite) 13825f2c3e2dSAndreas Boehler return false; 1383d5703f5aSAndreas Boehler $query = "DELETE FROM calendarobjects WHERE calendarid = ? AND uri = ?"; 13845f2c3e2dSAndreas Boehler $res = $sqlite->query($query, $calid, $uri); 1385d5703f5aSAndreas Boehler if($res !== false) 1386d5703f5aSAndreas Boehler { 1387d5703f5aSAndreas Boehler $this->updateSyncTokenLog($calid, $uri, 'deleted'); 1388d5703f5aSAndreas Boehler } 1389d5703f5aSAndreas Boehler return true; 1390d5703f5aSAndreas Boehler } 1391d5703f5aSAndreas Boehler 1392d5703f5aSAndreas Boehler /** 1393cb71a62aSAndreas Boehler * Delete a calendar entry for a given page. Actually, the event is removed 1394cb71a62aSAndreas Boehler * based on the entry's UID, so that page ID is no used. 1395cb71a62aSAndreas Boehler * 1396cb71a62aSAndreas Boehler * @param string $id The page's ID (unused) 1397cb71a62aSAndreas Boehler * @param array $params The parameter array to work with 1398cb71a62aSAndreas Boehler * 1399cb71a62aSAndreas Boehler * @return boolean True 1400cb71a62aSAndreas Boehler */ 1401a1a3b679SAndreas Boehler public function deleteCalendarEntryForPage($id, $params) 1402a1a3b679SAndreas Boehler { 1403a1a3b679SAndreas Boehler $uid = $params['uid']; 1404809cb0faSAndreas Boehler if(strpos($id, 'webdav://') === 0) 1405809cb0faSAndreas Boehler { 1406809cb0faSAndreas Boehler $wdc =& plugin_load('helper', 'webdavclient'); 1407809cb0faSAndreas Boehler if(is_null($wdc)) 1408809cb0faSAndreas Boehler return false; 1409809cb0faSAndreas Boehler $connectionId = str_replace('webdav://', '', $id); 1410809cb0faSAndreas Boehler $result = $wdc->deleteCalendarEntry($connectionId, $uid); 1411809cb0faSAndreas Boehler return $result; 1412809cb0faSAndreas Boehler } 14135f2c3e2dSAndreas Boehler $sqlite = $this->getDB(); 14145f2c3e2dSAndreas Boehler if(!$sqlite) 14155f2c3e2dSAndreas Boehler return false; 141655a741c0SAndreas Boehler $event = $this->getEventWithUid($uid); 14172c14b82bSAndreas Boehler $calid = $event['calendarid']; 141855a741c0SAndreas Boehler $uri = $event['uri']; 141951f4febbSAndreas Boehler $query = "DELETE FROM calendarobjects WHERE uid = ?"; 14205f2c3e2dSAndreas Boehler $res = $sqlite->query($query, $uid); 142155a741c0SAndreas Boehler if($res !== false) 142255a741c0SAndreas Boehler { 142355a741c0SAndreas Boehler $this->updateSyncTokenLog($calid, $uri, 'deleted'); 142455a741c0SAndreas Boehler } 1425a1a3b679SAndreas Boehler return true; 1426a1a3b679SAndreas Boehler } 1427a1a3b679SAndreas Boehler 1428cb71a62aSAndreas Boehler /** 1429cb71a62aSAndreas Boehler * Retrieve the current sync token for a calendar 1430cb71a62aSAndreas Boehler * 1431cb71a62aSAndreas Boehler * @param string $calid The calendar id 1432cb71a62aSAndreas Boehler * 1433cb71a62aSAndreas Boehler * @return mixed The synctoken or false 1434cb71a62aSAndreas Boehler */ 143555a741c0SAndreas Boehler public function getSyncTokenForCalendar($calid) 143655a741c0SAndreas Boehler { 1437b269830cSAndreas Boehler $row = $this->getCalendarSettings($calid); 143855a741c0SAndreas Boehler if(isset($row['synctoken'])) 143955a741c0SAndreas Boehler return $row['synctoken']; 144055a741c0SAndreas Boehler return false; 144155a741c0SAndreas Boehler } 144255a741c0SAndreas Boehler 1443cb71a62aSAndreas Boehler /** 1444cb71a62aSAndreas Boehler * Helper function to convert the operation name to 1445cb71a62aSAndreas Boehler * an operation code as stored in the database 1446cb71a62aSAndreas Boehler * 1447cb71a62aSAndreas Boehler * @param string $operationName The operation name 1448cb71a62aSAndreas Boehler * 1449cb71a62aSAndreas Boehler * @return mixed The operation code or false 1450cb71a62aSAndreas Boehler */ 145155a741c0SAndreas Boehler public function operationNameToOperation($operationName) 145255a741c0SAndreas Boehler { 145355a741c0SAndreas Boehler switch($operationName) 145455a741c0SAndreas Boehler { 145555a741c0SAndreas Boehler case 'added': 145655a741c0SAndreas Boehler return 1; 145755a741c0SAndreas Boehler break; 145855a741c0SAndreas Boehler case 'modified': 145955a741c0SAndreas Boehler return 2; 146055a741c0SAndreas Boehler break; 146155a741c0SAndreas Boehler case 'deleted': 146255a741c0SAndreas Boehler return 3; 146355a741c0SAndreas Boehler break; 146455a741c0SAndreas Boehler } 146555a741c0SAndreas Boehler return false; 146655a741c0SAndreas Boehler } 146755a741c0SAndreas Boehler 1468cb71a62aSAndreas Boehler /** 1469cb71a62aSAndreas Boehler * Update the sync token log based on the calendar id and the 1470cb71a62aSAndreas Boehler * operation that was performed. 1471cb71a62aSAndreas Boehler * 1472cb71a62aSAndreas Boehler * @param string $calid The calendar ID that was modified 1473cb71a62aSAndreas Boehler * @param string $uri The calendar URI that was modified 1474cb71a62aSAndreas Boehler * @param string $operation The operation that was performed 1475cb71a62aSAndreas Boehler * 1476cb71a62aSAndreas Boehler * @return boolean True on success, otherwise false 1477cb71a62aSAndreas Boehler */ 147855a741c0SAndreas Boehler private function updateSyncTokenLog($calid, $uri, $operation) 147955a741c0SAndreas Boehler { 148055a741c0SAndreas Boehler $currentToken = $this->getSyncTokenForCalendar($calid); 148155a741c0SAndreas Boehler $operationCode = $this->operationNameToOperation($operation); 148255a741c0SAndreas Boehler if(($operationCode === false) || ($currentToken === false)) 148355a741c0SAndreas Boehler return false; 148455a741c0SAndreas Boehler $values = array($uri, 148555a741c0SAndreas Boehler $currentToken, 148655a741c0SAndreas Boehler $calid, 148755a741c0SAndreas Boehler $operationCode 148855a741c0SAndreas Boehler ); 14895f2c3e2dSAndreas Boehler $sqlite = $this->getDB(); 14905f2c3e2dSAndreas Boehler if(!$sqlite) 14915f2c3e2dSAndreas Boehler return false; 149251f4febbSAndreas Boehler $query = "INSERT INTO calendarchanges (uri, synctoken, calendarid, operation) VALUES(?, ?, ?, ?)"; 14935f2c3e2dSAndreas Boehler $res = $sqlite->query($query, $uri, $currentToken, $calid, $operationCode); 149455a741c0SAndreas Boehler if($res === false) 149555a741c0SAndreas Boehler return false; 149655a741c0SAndreas Boehler $currentToken++; 149751f4febbSAndreas Boehler $query = "UPDATE calendars SET synctoken = ? WHERE id = ?"; 14985f2c3e2dSAndreas Boehler $res = $sqlite->query($query, $currentToken, $calid); 149955a741c0SAndreas Boehler return ($res !== false); 150055a741c0SAndreas Boehler } 150155a741c0SAndreas Boehler 1502cb71a62aSAndreas Boehler /** 1503cb71a62aSAndreas Boehler * Return the sync URL for a given Page, i.e. a calendar 1504cb71a62aSAndreas Boehler * 1505cb71a62aSAndreas Boehler * @param string $id The page's ID 1506cb71a62aSAndreas Boehler * @param string $user (optional) The user's ID 1507cb71a62aSAndreas Boehler * 1508cb71a62aSAndreas Boehler * @return mixed The sync url or false 1509cb71a62aSAndreas Boehler */ 1510b269830cSAndreas Boehler public function getSyncUrlForPage($id, $user = null) 1511b269830cSAndreas Boehler { 151234a47953SAndreas Boehler if(is_null($userid)) 151334a47953SAndreas Boehler { 151434a47953SAndreas Boehler if(isset($_SERVER['REMOTE_USER']) && !is_null($_SERVER['REMOTE_USER'])) 151534a47953SAndreas Boehler { 151634a47953SAndreas Boehler $userid = $_SERVER['REMOTE_USER']; 151734a47953SAndreas Boehler } 151834a47953SAndreas Boehler else 151934a47953SAndreas Boehler { 152034a47953SAndreas Boehler return false; 152134a47953SAndreas Boehler } 152234a47953SAndreas Boehler } 1523b269830cSAndreas Boehler 1524b269830cSAndreas Boehler $calid = $this->getCalendarIdForPage($id); 1525b269830cSAndreas Boehler if($calid === false) 1526b269830cSAndreas Boehler return false; 1527b269830cSAndreas Boehler 1528b269830cSAndreas Boehler $calsettings = $this->getCalendarSettings($calid); 1529b269830cSAndreas Boehler if(!isset($calsettings['uri'])) 1530b269830cSAndreas Boehler return false; 1531b269830cSAndreas Boehler 1532b269830cSAndreas Boehler $syncurl = DOKU_URL.'lib/plugins/davcal/calendarserver.php/calendars/'.$user.'/'.$calsettings['uri']; 1533b269830cSAndreas Boehler return $syncurl; 1534b269830cSAndreas Boehler } 1535b269830cSAndreas Boehler 1536cb71a62aSAndreas Boehler /** 1537cb71a62aSAndreas Boehler * Return the private calendar's URL for a given page 1538cb71a62aSAndreas Boehler * 1539cb71a62aSAndreas Boehler * @param string $id the page ID 1540cb71a62aSAndreas Boehler * 1541cb71a62aSAndreas Boehler * @return mixed The private URL or false 1542cb71a62aSAndreas Boehler */ 1543f69bb449SAndreas Boehler public function getPrivateURLForPage($id) 1544f69bb449SAndreas Boehler { 1545f69bb449SAndreas Boehler $calid = $this->getCalendarIdForPage($id); 1546f69bb449SAndreas Boehler if($calid === false) 1547f69bb449SAndreas Boehler return false; 1548f69bb449SAndreas Boehler 1549f69bb449SAndreas Boehler return $this->getPrivateURLForCalendar($calid); 1550f69bb449SAndreas Boehler } 1551f69bb449SAndreas Boehler 1552cb71a62aSAndreas Boehler /** 1553cb71a62aSAndreas Boehler * Return the private calendar's URL for a given calendar ID 1554cb71a62aSAndreas Boehler * 1555cb71a62aSAndreas Boehler * @param string $calid The calendar's ID 1556cb71a62aSAndreas Boehler * 1557cb71a62aSAndreas Boehler * @return mixed The private URL or false 1558cb71a62aSAndreas Boehler */ 1559f69bb449SAndreas Boehler public function getPrivateURLForCalendar($calid) 1560f69bb449SAndreas Boehler { 1561185e2535SAndreas Boehler if(isset($this->cachedValues['privateurl'][$calid])) 1562185e2535SAndreas Boehler return $this->cachedValues['privateurl'][$calid]; 15635f2c3e2dSAndreas Boehler $sqlite = $this->getDB(); 15645f2c3e2dSAndreas Boehler if(!$sqlite) 15655f2c3e2dSAndreas Boehler return false; 156651f4febbSAndreas Boehler $query = "SELECT url FROM calendartoprivateurlmapping WHERE calid = ?"; 15675f2c3e2dSAndreas Boehler $res = $sqlite->query($query, $calid); 15685f2c3e2dSAndreas Boehler $row = $sqlite->res2row($res); 1569f69bb449SAndreas Boehler if(!isset($row['url'])) 1570f69bb449SAndreas Boehler { 157102eea9b8SAndreas Boehler $url = 'dokuwiki-' . bin2hex(random_bytes(16)) . '.ics'; 157251f4febbSAndreas Boehler $query = "INSERT INTO calendartoprivateurlmapping (url, calid) VALUES(?, ?)"; 15735f2c3e2dSAndreas Boehler $res = $sqlite->query($query, $url, $calid); 1574f69bb449SAndreas Boehler if($res === false) 1575f69bb449SAndreas Boehler return false; 1576f69bb449SAndreas Boehler } 1577f69bb449SAndreas Boehler else 1578f69bb449SAndreas Boehler { 1579f69bb449SAndreas Boehler $url = $row['url']; 1580f69bb449SAndreas Boehler } 1581185e2535SAndreas Boehler 1582185e2535SAndreas Boehler $url = DOKU_URL.'lib/plugins/davcal/ics.php/'.$url; 1583185e2535SAndreas Boehler $this->cachedValues['privateurl'][$calid] = $url; 1584185e2535SAndreas Boehler return $url; 1585f69bb449SAndreas Boehler } 1586f69bb449SAndreas Boehler 1587cb71a62aSAndreas Boehler /** 1588cb71a62aSAndreas Boehler * Retrieve the calendar ID for a given private calendar URL 1589cb71a62aSAndreas Boehler * 1590cb71a62aSAndreas Boehler * @param string $url The private URL 1591cb71a62aSAndreas Boehler * 1592cb71a62aSAndreas Boehler * @return mixed The calendar ID or false 1593cb71a62aSAndreas Boehler */ 1594f69bb449SAndreas Boehler public function getCalendarForPrivateURL($url) 1595f69bb449SAndreas Boehler { 15965f2c3e2dSAndreas Boehler $sqlite = $this->getDB(); 15975f2c3e2dSAndreas Boehler if(!$sqlite) 15985f2c3e2dSAndreas Boehler return false; 159951f4febbSAndreas Boehler $query = "SELECT calid FROM calendartoprivateurlmapping WHERE url = ?"; 16005f2c3e2dSAndreas Boehler $res = $sqlite->query($query, $url); 16015f2c3e2dSAndreas Boehler $row = $sqlite->res2row($res); 1602f69bb449SAndreas Boehler if(!isset($row['calid'])) 1603f69bb449SAndreas Boehler return false; 1604f69bb449SAndreas Boehler return $row['calid']; 1605f69bb449SAndreas Boehler } 1606f69bb449SAndreas Boehler 1607cb71a62aSAndreas Boehler /** 1608cb71a62aSAndreas Boehler * Return a given calendar as ICS feed, i.e. all events in one ICS file. 1609cb71a62aSAndreas Boehler * 16107e0b8590SAndreas Boehler * @param string $calid The calendar ID to retrieve 1611cb71a62aSAndreas Boehler * 1612cb71a62aSAndreas Boehler * @return mixed The calendar events as string or false 1613cb71a62aSAndreas Boehler */ 1614f69bb449SAndreas Boehler public function getCalendarAsICSFeed($calid) 1615f69bb449SAndreas Boehler { 1616f69bb449SAndreas Boehler $calSettings = $this->getCalendarSettings($calid); 1617f69bb449SAndreas Boehler if($calSettings === false) 1618f69bb449SAndreas Boehler return false; 1619f69bb449SAndreas Boehler $events = $this->getAllCalendarEvents($calid); 1620f69bb449SAndreas Boehler if($events === false) 1621f69bb449SAndreas Boehler return false; 1622f69bb449SAndreas Boehler 16237e0b8590SAndreas Boehler // Load SabreDAV 16247e0b8590SAndreas Boehler require_once(DOKU_PLUGIN.'davcal/vendor/autoload.php'); 16257e0b8590SAndreas Boehler $out = "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//DAVCal//DAVCal for DokuWiki//EN\r\nCALSCALE:GREGORIAN\r\nX-WR-CALNAME:"; 16267e0b8590SAndreas Boehler $out .= $calSettings['displayname']."\r\n"; 1627f69bb449SAndreas Boehler foreach($events as $event) 1628f69bb449SAndreas Boehler { 16297e0b8590SAndreas Boehler $vcal = \Sabre\VObject\Reader::read($event['calendardata']); 16307e0b8590SAndreas Boehler $evt = $vcal->VEVENT; 16317e0b8590SAndreas Boehler $out .= $evt->serialize(); 1632f69bb449SAndreas Boehler } 16337e0b8590SAndreas Boehler $out .= "END:VCALENDAR\r\n"; 1634f69bb449SAndreas Boehler return $out; 1635f69bb449SAndreas Boehler } 1636f69bb449SAndreas Boehler 16377c7c6b0bSAndreas Boehler /** 16387c7c6b0bSAndreas Boehler * Retrieve a configuration option for the plugin 16397c7c6b0bSAndreas Boehler * 16407c7c6b0bSAndreas Boehler * @param string $key The key to query 164121d04f73SAndreas Boehler * @return mixed The option set, null if not found 16427c7c6b0bSAndreas Boehler */ 16437c7c6b0bSAndreas Boehler public function getConfig($key) 16447c7c6b0bSAndreas Boehler { 16457c7c6b0bSAndreas Boehler return $this->getConf($key); 16467c7c6b0bSAndreas Boehler } 16477c7c6b0bSAndreas Boehler 1648d5703f5aSAndreas Boehler /** 1649d5703f5aSAndreas Boehler * Parses some information from calendar objects, used for optimized 1650d5703f5aSAndreas Boehler * calendar-queries. Taken nearly unmodified from Sabre's PDO backend 1651d5703f5aSAndreas Boehler * 1652d5703f5aSAndreas Boehler * Returns an array with the following keys: 1653d5703f5aSAndreas Boehler * * etag - An md5 checksum of the object without the quotes. 1654d5703f5aSAndreas Boehler * * size - Size of the object in bytes 1655d5703f5aSAndreas Boehler * * componentType - VEVENT, VTODO or VJOURNAL 1656d5703f5aSAndreas Boehler * * firstOccurence 1657d5703f5aSAndreas Boehler * * lastOccurence 1658d5703f5aSAndreas Boehler * * uid - value of the UID property 1659d5703f5aSAndreas Boehler * 1660d5703f5aSAndreas Boehler * @param string $calendarData 1661d5703f5aSAndreas Boehler * @return array 1662d5703f5aSAndreas Boehler */ 1663d5703f5aSAndreas Boehler protected function getDenormalizedData($calendarData) 1664d5703f5aSAndreas Boehler { 1665d5703f5aSAndreas Boehler require_once(DOKU_PLUGIN.'davcal/vendor/autoload.php'); 1666d5703f5aSAndreas Boehler 1667d5703f5aSAndreas Boehler $vObject = \Sabre\VObject\Reader::read($calendarData); 1668d5703f5aSAndreas Boehler $componentType = null; 1669d5703f5aSAndreas Boehler $component = null; 1670d5703f5aSAndreas Boehler $firstOccurence = null; 1671d5703f5aSAndreas Boehler $lastOccurence = null; 1672d5703f5aSAndreas Boehler $uid = null; 1673d5703f5aSAndreas Boehler foreach ($vObject->getComponents() as $component) 1674d5703f5aSAndreas Boehler { 1675d5703f5aSAndreas Boehler if ($component->name !== 'VTIMEZONE') 1676d5703f5aSAndreas Boehler { 1677d5703f5aSAndreas Boehler $componentType = $component->name; 1678d5703f5aSAndreas Boehler $uid = (string)$component->UID; 1679d5703f5aSAndreas Boehler break; 1680d5703f5aSAndreas Boehler } 1681d5703f5aSAndreas Boehler } 1682d5703f5aSAndreas Boehler if (!$componentType) 1683d5703f5aSAndreas Boehler { 1684d5703f5aSAndreas Boehler return false; 1685d5703f5aSAndreas Boehler } 1686d5703f5aSAndreas Boehler if ($componentType === 'VEVENT') 1687d5703f5aSAndreas Boehler { 1688d5703f5aSAndreas Boehler $firstOccurence = $component->DTSTART->getDateTime()->getTimeStamp(); 1689d5703f5aSAndreas Boehler // Finding the last occurence is a bit harder 1690d5703f5aSAndreas Boehler if (!isset($component->RRULE)) 1691d5703f5aSAndreas Boehler { 1692d5703f5aSAndreas Boehler if (isset($component->DTEND)) 1693d5703f5aSAndreas Boehler { 1694d5703f5aSAndreas Boehler $lastOccurence = $component->DTEND->getDateTime()->getTimeStamp(); 1695d5703f5aSAndreas Boehler } 1696d5703f5aSAndreas Boehler elseif (isset($component->DURATION)) 1697d5703f5aSAndreas Boehler { 1698d5703f5aSAndreas Boehler $endDate = clone $component->DTSTART->getDateTime(); 1699d5703f5aSAndreas Boehler $endDate->add(\Sabre\VObject\DateTimeParser::parse($component->DURATION->getValue())); 1700d5703f5aSAndreas Boehler $lastOccurence = $endDate->getTimeStamp(); 1701d5703f5aSAndreas Boehler } 1702d5703f5aSAndreas Boehler elseif (!$component->DTSTART->hasTime()) 1703d5703f5aSAndreas Boehler { 1704d5703f5aSAndreas Boehler $endDate = clone $component->DTSTART->getDateTime(); 1705d5703f5aSAndreas Boehler $endDate->modify('+1 day'); 1706d5703f5aSAndreas Boehler $lastOccurence = $endDate->getTimeStamp(); 1707d5703f5aSAndreas Boehler } 1708d5703f5aSAndreas Boehler else 1709d5703f5aSAndreas Boehler { 1710d5703f5aSAndreas Boehler $lastOccurence = $firstOccurence; 1711d5703f5aSAndreas Boehler } 1712d5703f5aSAndreas Boehler } 1713d5703f5aSAndreas Boehler else 1714d5703f5aSAndreas Boehler { 1715d5703f5aSAndreas Boehler $it = new \Sabre\VObject\Recur\EventIterator($vObject, (string)$component->UID); 1716d5703f5aSAndreas Boehler $maxDate = new \DateTime('2038-01-01'); 1717d5703f5aSAndreas Boehler if ($it->isInfinite()) 1718d5703f5aSAndreas Boehler { 1719d5703f5aSAndreas Boehler $lastOccurence = $maxDate->getTimeStamp(); 1720d5703f5aSAndreas Boehler } 1721d5703f5aSAndreas Boehler else 1722d5703f5aSAndreas Boehler { 1723d5703f5aSAndreas Boehler $end = $it->getDtEnd(); 1724d5703f5aSAndreas Boehler while ($it->valid() && $end < $maxDate) 1725d5703f5aSAndreas Boehler { 1726d5703f5aSAndreas Boehler $end = $it->getDtEnd(); 1727d5703f5aSAndreas Boehler $it->next(); 1728d5703f5aSAndreas Boehler } 1729d5703f5aSAndreas Boehler $lastOccurence = $end->getTimeStamp(); 1730d5703f5aSAndreas Boehler } 1731d5703f5aSAndreas Boehler } 1732d5703f5aSAndreas Boehler } 1733d5703f5aSAndreas Boehler 1734d5703f5aSAndreas Boehler return array( 1735d5703f5aSAndreas Boehler 'etag' => md5($calendarData), 1736d5703f5aSAndreas Boehler 'size' => strlen($calendarData), 1737d5703f5aSAndreas Boehler 'componentType' => $componentType, 1738d5703f5aSAndreas Boehler 'firstOccurence' => $firstOccurence, 1739d5703f5aSAndreas Boehler 'lastOccurence' => $lastOccurence, 1740d5703f5aSAndreas Boehler 'uid' => $uid, 1741d5703f5aSAndreas Boehler ); 1742d5703f5aSAndreas Boehler 1743d5703f5aSAndreas Boehler } 1744d5703f5aSAndreas Boehler 1745d5703f5aSAndreas Boehler /** 1746d5703f5aSAndreas Boehler * Query a calendar by ID and taking several filters into account. 1747d5703f5aSAndreas Boehler * This is heavily based on Sabre's PDO backend. 1748d5703f5aSAndreas Boehler * 1749d5703f5aSAndreas Boehler * @param int $calendarId The calendar's ID 1750d5703f5aSAndreas Boehler * @param array $filters The filter array to apply 1751d5703f5aSAndreas Boehler * 1752d5703f5aSAndreas Boehler * @return mixed The result 1753d5703f5aSAndreas Boehler */ 1754d5703f5aSAndreas Boehler public function calendarQuery($calendarId, $filters) 1755d5703f5aSAndreas Boehler { 175639787131SAndreas Boehler dbglog('davcal::helper::calendarQuery'); 1757d5703f5aSAndreas Boehler $componentType = null; 1758d5703f5aSAndreas Boehler $requirePostFilter = true; 1759d5703f5aSAndreas Boehler $timeRange = null; 17605f2c3e2dSAndreas Boehler $sqlite = $this->getDB(); 17615f2c3e2dSAndreas Boehler if(!$sqlite) 17625f2c3e2dSAndreas Boehler return false; 1763d5703f5aSAndreas Boehler 1764d5703f5aSAndreas Boehler // if no filters were specified, we don't need to filter after a query 1765d5703f5aSAndreas Boehler if (!$filters['prop-filters'] && !$filters['comp-filters']) 1766d5703f5aSAndreas Boehler { 1767d5703f5aSAndreas Boehler $requirePostFilter = false; 1768d5703f5aSAndreas Boehler } 1769d5703f5aSAndreas Boehler 1770d5703f5aSAndreas Boehler // Figuring out if there's a component filter 1771d5703f5aSAndreas Boehler if (count($filters['comp-filters']) > 0 && !$filters['comp-filters'][0]['is-not-defined']) 1772d5703f5aSAndreas Boehler { 1773d5703f5aSAndreas Boehler $componentType = $filters['comp-filters'][0]['name']; 1774d5703f5aSAndreas Boehler 1775d5703f5aSAndreas Boehler // Checking if we need post-filters 1776d5703f5aSAndreas Boehler if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['time-range'] && !$filters['comp-filters'][0]['prop-filters']) 1777d5703f5aSAndreas Boehler { 1778d5703f5aSAndreas Boehler $requirePostFilter = false; 1779d5703f5aSAndreas Boehler } 1780d5703f5aSAndreas Boehler // There was a time-range filter 1781d5703f5aSAndreas Boehler if ($componentType == 'VEVENT' && isset($filters['comp-filters'][0]['time-range'])) 1782d5703f5aSAndreas Boehler { 1783d5703f5aSAndreas Boehler $timeRange = $filters['comp-filters'][0]['time-range']; 1784d5703f5aSAndreas Boehler 1785d5703f5aSAndreas Boehler // If start time OR the end time is not specified, we can do a 1786d5703f5aSAndreas Boehler // 100% accurate mysql query. 1787d5703f5aSAndreas Boehler if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['prop-filters'] && (!$timeRange['start'] || !$timeRange['end'])) 1788d5703f5aSAndreas Boehler { 1789d5703f5aSAndreas Boehler $requirePostFilter = false; 1790d5703f5aSAndreas Boehler } 1791d5703f5aSAndreas Boehler } 1792d5703f5aSAndreas Boehler 1793d5703f5aSAndreas Boehler } 1794d5703f5aSAndreas Boehler 1795d5703f5aSAndreas Boehler if ($requirePostFilter) 1796d5703f5aSAndreas Boehler { 1797d5703f5aSAndreas Boehler $query = "SELECT uri, calendardata FROM calendarobjects WHERE calendarid = ?"; 1798d5703f5aSAndreas Boehler } 1799d5703f5aSAndreas Boehler else 1800d5703f5aSAndreas Boehler { 1801d5703f5aSAndreas Boehler $query = "SELECT uri FROM calendarobjects WHERE calendarid = ?"; 1802d5703f5aSAndreas Boehler } 1803d5703f5aSAndreas Boehler 1804d5703f5aSAndreas Boehler $values = array( 1805d5703f5aSAndreas Boehler $calendarId 1806d5703f5aSAndreas Boehler ); 1807d5703f5aSAndreas Boehler 1808d5703f5aSAndreas Boehler if ($componentType) 1809d5703f5aSAndreas Boehler { 1810d5703f5aSAndreas Boehler $query .= " AND componenttype = ?"; 1811d5703f5aSAndreas Boehler $values[] = $componentType; 1812d5703f5aSAndreas Boehler } 1813d5703f5aSAndreas Boehler 1814d5703f5aSAndreas Boehler if ($timeRange && $timeRange['start']) 1815d5703f5aSAndreas Boehler { 1816d5703f5aSAndreas Boehler $query .= " AND lastoccurence > ?"; 1817d5703f5aSAndreas Boehler $values[] = $timeRange['start']->getTimeStamp(); 1818d5703f5aSAndreas Boehler } 1819d5703f5aSAndreas Boehler if ($timeRange && $timeRange['end']) 1820d5703f5aSAndreas Boehler { 1821d5703f5aSAndreas Boehler $query .= " AND firstoccurence < ?"; 1822d5703f5aSAndreas Boehler $values[] = $timeRange['end']->getTimeStamp(); 1823d5703f5aSAndreas Boehler } 1824d5703f5aSAndreas Boehler 18255f2c3e2dSAndreas Boehler $res = $sqlite->query($query, $values); 18265f2c3e2dSAndreas Boehler $arr = $sqlite->res2arr($res); 1827d5703f5aSAndreas Boehler 1828d5703f5aSAndreas Boehler $result = array(); 1829d5703f5aSAndreas Boehler foreach($arr as $row) 1830d5703f5aSAndreas Boehler { 1831d5703f5aSAndreas Boehler if ($requirePostFilter) 1832d5703f5aSAndreas Boehler { 1833d5703f5aSAndreas Boehler if (!$this->validateFilterForObject($row, $filters)) 1834d5703f5aSAndreas Boehler { 1835d5703f5aSAndreas Boehler continue; 1836d5703f5aSAndreas Boehler } 1837d5703f5aSAndreas Boehler } 1838d5703f5aSAndreas Boehler $result[] = $row['uri']; 1839d5703f5aSAndreas Boehler 1840d5703f5aSAndreas Boehler } 1841d5703f5aSAndreas Boehler 1842d5703f5aSAndreas Boehler return $result; 1843d5703f5aSAndreas Boehler } 1844d5703f5aSAndreas Boehler 1845d5703f5aSAndreas Boehler /** 1846d5703f5aSAndreas Boehler * This method validates if a filter (as passed to calendarQuery) matches 1847d5703f5aSAndreas Boehler * the given object. Taken from Sabre's PDO backend 1848d5703f5aSAndreas Boehler * 1849d5703f5aSAndreas Boehler * @param array $object 1850d5703f5aSAndreas Boehler * @param array $filters 1851d5703f5aSAndreas Boehler * @return bool 1852d5703f5aSAndreas Boehler */ 1853d5703f5aSAndreas Boehler protected function validateFilterForObject($object, $filters) 1854d5703f5aSAndreas Boehler { 1855d5703f5aSAndreas Boehler require_once(DOKU_PLUGIN.'davcal/vendor/autoload.php'); 1856d5703f5aSAndreas Boehler // Unfortunately, setting the 'calendardata' here is optional. If 1857d5703f5aSAndreas Boehler // it was excluded, we actually need another call to get this as 1858d5703f5aSAndreas Boehler // well. 1859d5703f5aSAndreas Boehler if (!isset($object['calendardata'])) 1860d5703f5aSAndreas Boehler { 1861d5703f5aSAndreas Boehler $object = $this->getCalendarObjectByUri($object['calendarid'], $object['uri']); 1862d5703f5aSAndreas Boehler } 1863d5703f5aSAndreas Boehler 1864d5703f5aSAndreas Boehler $vObject = \Sabre\VObject\Reader::read($object['calendardata']); 1865d5703f5aSAndreas Boehler $validator = new \Sabre\CalDAV\CalendarQueryValidator(); 1866d5703f5aSAndreas Boehler 186739787131SAndreas Boehler $res = $validator->validate($vObject, $filters); 186839787131SAndreas Boehler return $res; 1869d5703f5aSAndreas Boehler 1870d5703f5aSAndreas Boehler } 1871d5703f5aSAndreas Boehler 1872d5703f5aSAndreas Boehler /** 1873d5703f5aSAndreas Boehler * Retrieve changes for a given calendar based on the given syncToken. 1874d5703f5aSAndreas Boehler * 1875d5703f5aSAndreas Boehler * @param int $calid The calendar's ID 1876d5703f5aSAndreas Boehler * @param int $syncToken The supplied sync token 1877d5703f5aSAndreas Boehler * @param int $syncLevel The sync level 1878d5703f5aSAndreas Boehler * @param int $limit The limit of changes 1879d5703f5aSAndreas Boehler * 1880d5703f5aSAndreas Boehler * @return array The result 1881d5703f5aSAndreas Boehler */ 1882d5703f5aSAndreas Boehler public function getChangesForCalendar($calid, $syncToken, $syncLevel, $limit = null) 1883d5703f5aSAndreas Boehler { 1884d5703f5aSAndreas Boehler // Current synctoken 1885d5703f5aSAndreas Boehler $currentToken = $this->getSyncTokenForCalendar($calid); 1886d5703f5aSAndreas Boehler 1887d5703f5aSAndreas Boehler if ($currentToken === false) return null; 1888d5703f5aSAndreas Boehler 1889d5703f5aSAndreas Boehler $result = array( 1890d5703f5aSAndreas Boehler 'syncToken' => $currentToken, 1891d5703f5aSAndreas Boehler 'added' => array(), 1892d5703f5aSAndreas Boehler 'modified' => array(), 1893d5703f5aSAndreas Boehler 'deleted' => array(), 1894d5703f5aSAndreas Boehler ); 18955f2c3e2dSAndreas Boehler $sqlite = $this->getDB(); 18965f2c3e2dSAndreas Boehler if(!$sqlite) 18975f2c3e2dSAndreas Boehler return false; 1898d5703f5aSAndreas Boehler 1899d5703f5aSAndreas Boehler if ($syncToken) 1900d5703f5aSAndreas Boehler { 1901d5703f5aSAndreas Boehler 1902d5703f5aSAndreas Boehler $query = "SELECT uri, operation FROM calendarchanges WHERE synctoken >= ? AND synctoken < ? AND calendarid = ? ORDER BY synctoken"; 1903d5703f5aSAndreas Boehler if ($limit > 0) $query .= " LIMIT " . (int)$limit; 1904d5703f5aSAndreas Boehler 1905d5703f5aSAndreas Boehler // Fetching all changes 19065f2c3e2dSAndreas Boehler $res = $sqlite->query($query, $syncToken, $currentToken, $calid); 1907d5703f5aSAndreas Boehler if($res === false) 1908d5703f5aSAndreas Boehler return null; 1909d5703f5aSAndreas Boehler 19105f2c3e2dSAndreas Boehler $arr = $sqlite->res2arr($res); 1911d5703f5aSAndreas Boehler $changes = array(); 1912d5703f5aSAndreas Boehler 1913d5703f5aSAndreas Boehler // This loop ensures that any duplicates are overwritten, only the 1914d5703f5aSAndreas Boehler // last change on a node is relevant. 1915d5703f5aSAndreas Boehler foreach($arr as $row) 1916d5703f5aSAndreas Boehler { 1917d5703f5aSAndreas Boehler $changes[$row['uri']] = $row['operation']; 1918d5703f5aSAndreas Boehler } 1919d5703f5aSAndreas Boehler 1920d5703f5aSAndreas Boehler foreach ($changes as $uri => $operation) 1921d5703f5aSAndreas Boehler { 1922d5703f5aSAndreas Boehler switch ($operation) 1923d5703f5aSAndreas Boehler { 1924d5703f5aSAndreas Boehler case 1 : 1925d5703f5aSAndreas Boehler $result['added'][] = $uri; 1926d5703f5aSAndreas Boehler break; 1927d5703f5aSAndreas Boehler case 2 : 1928d5703f5aSAndreas Boehler $result['modified'][] = $uri; 1929d5703f5aSAndreas Boehler break; 1930d5703f5aSAndreas Boehler case 3 : 1931d5703f5aSAndreas Boehler $result['deleted'][] = $uri; 1932d5703f5aSAndreas Boehler break; 1933d5703f5aSAndreas Boehler } 1934d5703f5aSAndreas Boehler 1935d5703f5aSAndreas Boehler } 1936d5703f5aSAndreas Boehler } 1937d5703f5aSAndreas Boehler else 1938d5703f5aSAndreas Boehler { 1939d5703f5aSAndreas Boehler // No synctoken supplied, this is the initial sync. 1940d5703f5aSAndreas Boehler $query = "SELECT uri FROM calendarobjects WHERE calendarid = ?"; 19415f2c3e2dSAndreas Boehler $res = $sqlite->query($query); 19425f2c3e2dSAndreas Boehler $arr = $sqlite->res2arr($res); 1943d5703f5aSAndreas Boehler 1944d5703f5aSAndreas Boehler $result['added'] = $arr; 1945d5703f5aSAndreas Boehler } 1946d5703f5aSAndreas Boehler return $result; 1947d5703f5aSAndreas Boehler } 1948d5703f5aSAndreas Boehler 1949a1a3b679SAndreas Boehler} 1950