1a1a3b679SAndreas Boehler<?php 2a1a3b679SAndreas Boehler/** 3a1a3b679SAndreas Boehler * Helper Class for the tagrevisions plugin 4a1a3b679SAndreas Boehler * This helper does the actual work. 5a1a3b679SAndreas Boehler * 6a1a3b679SAndreas Boehler * Configurable in DokuWiki's configuration 7a1a3b679SAndreas Boehler */ 8a1a3b679SAndreas Boehler 9a1a3b679SAndreas Boehler// must be run within Dokuwiki 10a1a3b679SAndreas Boehlerif(!defined('DOKU_INC')) die(); 11a1a3b679SAndreas Boehler 12a1a3b679SAndreas Boehlerclass helper_plugin_davcal extends DokuWiki_Plugin { 13a1a3b679SAndreas Boehler 14a1a3b679SAndreas Boehler protected $sqlite = null; 15a1a3b679SAndreas Boehler 16a1a3b679SAndreas Boehler /** 17a1a3b679SAndreas Boehler * Constructor to load the configuration 18a1a3b679SAndreas Boehler */ 19a1a3b679SAndreas Boehler public function helper_plugin_davcal() { 20a1a3b679SAndreas Boehler $this->sqlite =& plugin_load('helper', 'sqlite'); 21a1a3b679SAndreas Boehler if(!$this->sqlite) 22a1a3b679SAndreas Boehler { 23a1a3b679SAndreas Boehler msg('This plugin requires the sqlite plugin. Please install it.'); 24a1a3b679SAndreas Boehler return; 25a1a3b679SAndreas Boehler } 26a1a3b679SAndreas Boehler 27a1a3b679SAndreas Boehler if(!$this->sqlite->init('davcal', DOKU_PLUGIN.'davcal/db/')) 28a1a3b679SAndreas Boehler { 29a1a3b679SAndreas Boehler return; 30a1a3b679SAndreas Boehler } 31a1a3b679SAndreas Boehler } 32a1a3b679SAndreas Boehler 33a1a3b679SAndreas Boehler public function setCalendarNameForPage($name, $description, $id = null, $userid = null) 34a1a3b679SAndreas Boehler { 35a1a3b679SAndreas Boehler if(is_null($id)) 36a1a3b679SAndreas Boehler { 37a1a3b679SAndreas Boehler global $ID; 38a1a3b679SAndreas Boehler $id = $ID; 39a1a3b679SAndreas Boehler } 40a1a3b679SAndreas Boehler if(is_null($userid)) 41a1a3b679SAndreas Boehler $userid = $_SERVER['REMOTE_USER']; 42a1a3b679SAndreas Boehler $calid = $this->getCalendarIdForPage($id); 43a1a3b679SAndreas Boehler if($calid === false) 44a1a3b679SAndreas Boehler return $this->createCalendarForPage($name, $description, $id, $userid); 45a1a3b679SAndreas Boehler 46b269830cSAndreas Boehler $query = "UPDATE calendars SET displayname=".$this->sqlite->quote_string($name).", ". 47b269830cSAndreas Boehler "description=".$this->sqlite->quote_string($description)." WHERE ". 48b269830cSAndreas Boehler "id=".$this->sqlite->quote_string($calid); 49b269830cSAndreas Boehler $res = $this->sqlite->query($query); 50b269830cSAndreas Boehler if($res !== false) 51b269830cSAndreas Boehler return true; 52b269830cSAndreas Boehler return false; 53a1a3b679SAndreas Boehler } 54a1a3b679SAndreas Boehler 55a495d34cSAndreas Boehler public function savePersonalSettings($settings, $userid = null) 56a495d34cSAndreas Boehler { 57a495d34cSAndreas Boehler if(is_null($userid)) 58a495d34cSAndreas Boehler $userid = $_SERVER['REMOTE_USER']; 59a495d34cSAndreas Boehler $this->sqlite->query("BEGIN TRANSACTION"); 60a495d34cSAndreas Boehler 61bd883736SAndreas Boehler $query = "DELETE FROM calendarsettings WHERE userid=".$this->sqlite->quote_string($userid); 62bd883736SAndreas Boehler $this->sqlite->query($query); 63bd883736SAndreas Boehler 64a495d34cSAndreas Boehler foreach($settings as $key => $value) 65a495d34cSAndreas Boehler { 66bd883736SAndreas Boehler $query = "INSERT INTO calendarsettings (userid, key, value) VALUES (". 67a495d34cSAndreas Boehler $this->sqlite->quote_string($userid).", ". 68a495d34cSAndreas Boehler $this->sqlite->quote_string($key).", ". 69a495d34cSAndreas Boehler $this->sqlite->quote_string($value).")"; 70a495d34cSAndreas Boehler $res = $this->sqlite->query($query); 71a495d34cSAndreas Boehler if($res === false) 72a495d34cSAndreas Boehler return false; 73a495d34cSAndreas Boehler } 74a495d34cSAndreas Boehler $this->sqlite->query("COMMIT TRANSACTION"); 75a495d34cSAndreas Boehler return true; 76a495d34cSAndreas Boehler } 77a495d34cSAndreas Boehler 78a495d34cSAndreas Boehler public function getPersonalSettings($userid = null) 79a495d34cSAndreas Boehler { 80a495d34cSAndreas Boehler if(is_null($userid)) 81a495d34cSAndreas Boehler $userid = $_SERVER['REMOTE_USER']; 82bd883736SAndreas Boehler // Some sane default settings 83bd883736SAndreas Boehler $settings = array( 84bd883736SAndreas Boehler 'timezone' => 'local', 85bd883736SAndreas Boehler 'weeknumbers' => '0', 86bd883736SAndreas Boehler 'workweek' => '0' 87bd883736SAndreas Boehler ); 88a495d34cSAndreas Boehler $query = "SELECT key, value FROM calendarsettings WHERE userid=".$this->sqlite->quote_string($userid); 89a495d34cSAndreas Boehler $res = $this->sqlite->query($query); 90a495d34cSAndreas Boehler $arr = $this->sqlite->res2arr($res); 91a495d34cSAndreas Boehler foreach($arr as $row) 92a495d34cSAndreas Boehler { 93a495d34cSAndreas Boehler $settings[$row['key']] = $row['value']; 94a495d34cSAndreas Boehler } 95a495d34cSAndreas Boehler return $settings; 96a495d34cSAndreas Boehler } 97a495d34cSAndreas Boehler 98a1a3b679SAndreas Boehler public function getCalendarIdForPage($id = null) 99a1a3b679SAndreas Boehler { 100a1a3b679SAndreas Boehler if(is_null($id)) 101a1a3b679SAndreas Boehler { 102a1a3b679SAndreas Boehler global $ID; 103a1a3b679SAndreas Boehler $id = $ID; 104a1a3b679SAndreas Boehler } 105a1a3b679SAndreas Boehler 106a1a3b679SAndreas Boehler $query = "SELECT calid FROM pagetocalendarmapping WHERE page=".$this->sqlite->quote_string($id); 107a1a3b679SAndreas Boehler $res = $this->sqlite->query($query); 108a1a3b679SAndreas Boehler $row = $this->sqlite->res2row($res); 109a1a3b679SAndreas Boehler if(isset($row['calid'])) 110a1a3b679SAndreas Boehler return $row['calid']; 111a1a3b679SAndreas Boehler else 112a1a3b679SAndreas Boehler return false; 113a1a3b679SAndreas Boehler } 114a1a3b679SAndreas Boehler 115a1a3b679SAndreas Boehler public function getCalendarIdToPageMapping() 116a1a3b679SAndreas Boehler { 117a1a3b679SAndreas Boehler $query = "SELECT calid, page FROM pagetocalendarmapping"; 118a1a3b679SAndreas Boehler $res = $this->sqlite->query($query); 119a1a3b679SAndreas Boehler $arr = $this->sqlite->res2arr($res); 120a1a3b679SAndreas Boehler return $arr; 121a1a3b679SAndreas Boehler } 122a1a3b679SAndreas Boehler 123a1a3b679SAndreas Boehler public function getCalendarIdsForUser($principalUri) 124a1a3b679SAndreas Boehler { 125a1a3b679SAndreas Boehler $user = explode('/', $principalUri); 126a1a3b679SAndreas Boehler $user = end($user); 127a1a3b679SAndreas Boehler $mapping = $this->getCalendarIdToPageMapping(); 128a1a3b679SAndreas Boehler $calids = array(); 129a1a3b679SAndreas Boehler foreach($mapping as $row) 130a1a3b679SAndreas Boehler { 131a1a3b679SAndreas Boehler $id = $row['calid']; 132a1a3b679SAndreas Boehler $page = $row['page']; 133a1a3b679SAndreas Boehler $acl = auth_quickaclcheck($page); 134a1a3b679SAndreas Boehler if($acl >= AUTH_READ) 135a1a3b679SAndreas Boehler { 136a1a3b679SAndreas Boehler $write = $acl > AUTH_READ; 137a1a3b679SAndreas Boehler $calids[$id] = array('readonly' => !$write); 138a1a3b679SAndreas Boehler } 139a1a3b679SAndreas Boehler } 140a1a3b679SAndreas Boehler return $calids; 141a1a3b679SAndreas Boehler } 142a1a3b679SAndreas Boehler 143a1a3b679SAndreas Boehler public function createCalendarForPage($name, $description, $id = null, $userid = null) 144a1a3b679SAndreas Boehler { 145a1a3b679SAndreas Boehler if(is_null($id)) 146a1a3b679SAndreas Boehler { 147a1a3b679SAndreas Boehler global $ID; 148a1a3b679SAndreas Boehler $id = $ID; 149a1a3b679SAndreas Boehler } 150a1a3b679SAndreas Boehler if(is_null($userid)) 151a1a3b679SAndreas Boehler $userid = $_SERVER['REMOTE_USER']; 152a1a3b679SAndreas Boehler $values = array('principals/'.$userid, 153a1a3b679SAndreas Boehler $name, 154a1a3b679SAndreas Boehler str_replace(array('/', ' ', ':'), '_', $id), 155a1a3b679SAndreas Boehler $description, 156a1a3b679SAndreas Boehler 'VEVENT,VTODO', 15755a741c0SAndreas Boehler 0, 15855a741c0SAndreas Boehler 1); 15955a741c0SAndreas Boehler $query = "INSERT INTO calendars (principaluri, displayname, uri, description, components, transparent, synctoken) VALUES (".$this->sqlite->quote_and_join($values, ',').");"; 160a1a3b679SAndreas Boehler $res = $this->sqlite->query($query); 16155a741c0SAndreas Boehler if($res === false) 16255a741c0SAndreas Boehler return false; 163a1a3b679SAndreas Boehler $query = "SELECT id FROM calendars WHERE principaluri=".$this->sqlite->quote_string($values[0])." AND ". 164a1a3b679SAndreas Boehler "displayname=".$this->sqlite->quote_string($values[1])." AND ". 165a1a3b679SAndreas Boehler "uri=".$this->sqlite->quote_string($values[2])." AND ". 166a1a3b679SAndreas Boehler "description=".$this->sqlite->quote_string($values[3]); 167a1a3b679SAndreas Boehler $res = $this->sqlite->query($query); 168a1a3b679SAndreas Boehler $row = $this->sqlite->res2row($res); 169a1a3b679SAndreas Boehler if(isset($row['id'])) 170a1a3b679SAndreas Boehler { 171a1a3b679SAndreas Boehler $values = array($id, $row['id']); 172a1a3b679SAndreas Boehler $query = "INSERT INTO pagetocalendarmapping (page, calid) VALUES (".$this->sqlite->quote_and_join($values, ',').")"; 173a1a3b679SAndreas Boehler $res = $this->sqlite->query($query); 17455a741c0SAndreas Boehler return ($res !== false); 175a1a3b679SAndreas Boehler } 176a1a3b679SAndreas Boehler 177a1a3b679SAndreas Boehler return false; 178a1a3b679SAndreas Boehler } 179a1a3b679SAndreas Boehler 180a1a3b679SAndreas Boehler public function addCalendarEntryToCalendarForPage($id, $user, $params) 181a1a3b679SAndreas Boehler { 182bd883736SAndreas Boehler $settings = $this->getPersonalSettings($user); 183bd883736SAndreas Boehler if($settings['timezone'] !== '' && $settings['timezone'] !== 'local') 184bd883736SAndreas Boehler $timezone = new \DateTimeZone($settings['timezone']); 185a25c89eaSAndreas Boehler elseif($settings['timezone'] === 'local') 186a25c89eaSAndreas Boehler $timezone = new \DateTimeZone($params['detectedtz']); 187bd883736SAndreas Boehler else 188bd883736SAndreas Boehler $timezone = new \DateTimeZone('UTC'); 189b269830cSAndreas Boehler $startDate = explode('-', $params['eventfrom']); 190b269830cSAndreas Boehler $startTime = explode(':', $params['eventfromtime']); 191b269830cSAndreas Boehler $endDate = explode('-', $params['eventto']); 192b269830cSAndreas Boehler $endTime = explode(':', $params['eventtotime']); 193a1a3b679SAndreas Boehler require_once('vendor/autoload.php'); 194a1a3b679SAndreas Boehler $vcalendar = new \Sabre\VObject\Component\VCalendar(); 195a1a3b679SAndreas Boehler $event = $vcalendar->add('VEVENT'); 196b269830cSAndreas Boehler $uuid = \Sabre\VObject\UUIDUtil::getUUID(); 197b269830cSAndreas Boehler $event->add('UID', $uuid); 198a1a3b679SAndreas Boehler $event->summary = $params['eventname']; 1990eebc909SAndreas Boehler $description = $params['eventdescription']; 2000eebc909SAndreas Boehler if($description !== '') 2010eebc909SAndreas Boehler $event->add('DESCRIPTION', $description); 202b269830cSAndreas Boehler $dtStamp = new \DateTime(null, new \DateTimeZone('UTC')); 203b269830cSAndreas Boehler $event->add('DTSTAMP', $dtStamp); 204b269830cSAndreas Boehler $event->add('CREATED', $dtStamp); 205b269830cSAndreas Boehler $event->add('LAST-MODIFIED', $dtStamp); 206b269830cSAndreas Boehler $dtStart = new \DateTime(); 207a25c89eaSAndreas Boehler $dtStart->setTimezone($timezone); 208b269830cSAndreas Boehler $dtStart->setDate(intval($startDate[0]), intval($startDate[1]), intval($startDate[2])); 209b269830cSAndreas Boehler if($params['allday'] != '1') 210b269830cSAndreas Boehler $dtStart->setTime(intval($startTime[0]), intval($startTime[1]), 0); 211b269830cSAndreas Boehler $dtEnd = new \DateTime(); 212a25c89eaSAndreas Boehler $dtEnd->setTimezone($timezone); 213b269830cSAndreas Boehler $dtEnd->setDate(intval($endDate[0]), intval($endDate[1]), intval($endDate[2])); 214b269830cSAndreas Boehler if($params['allday'] != '1') 215b269830cSAndreas Boehler $dtEnd->setTime(intval($endTime[0]), intval($endTime[1]), 0); 216b269830cSAndreas Boehler // According to the VCal spec, we need to add a whole day here 217b269830cSAndreas Boehler if($params['allday'] == '1') 218b269830cSAndreas Boehler $dtEnd->add(new \DateInterval('P1D')); 219b269830cSAndreas Boehler $dtStartEv = $event->add('DTSTART', $dtStart); 220b269830cSAndreas Boehler $dtEndEv = $event->add('DTEND', $dtEnd); 221b269830cSAndreas Boehler if($params['allday'] == '1') 222b269830cSAndreas Boehler { 223b269830cSAndreas Boehler $dtStartEv['VALUE'] = 'DATE'; 224b269830cSAndreas Boehler $dtEndEv['VALUE'] = 'DATE'; 225b269830cSAndreas Boehler } 226a1a3b679SAndreas Boehler $calid = $this->getCalendarIdForPage($id); 227a1a3b679SAndreas Boehler $uri = uniqid('dokuwiki-').'.ics'; 228a1a3b679SAndreas Boehler $now = new DateTime(); 229a1a3b679SAndreas Boehler $eventStr = $vcalendar->serialize(); 230a1a3b679SAndreas Boehler 231a1a3b679SAndreas Boehler $values = array($calid, 232a1a3b679SAndreas Boehler $uri, 233a1a3b679SAndreas Boehler $eventStr, 234a1a3b679SAndreas Boehler $now->getTimestamp(), 235a1a3b679SAndreas Boehler 'VEVENT', 236a1a3b679SAndreas Boehler $event->DTSTART->getDateTime()->getTimeStamp(), 237a1a3b679SAndreas Boehler $event->DTEND->getDateTime()->getTimeStamp(), 238a1a3b679SAndreas Boehler strlen($eventStr), 239a1a3b679SAndreas Boehler md5($eventStr), 240a1a3b679SAndreas Boehler uniqid() 241a1a3b679SAndreas Boehler ); 242a1a3b679SAndreas Boehler 243a1a3b679SAndreas Boehler $query = "INSERT INTO calendarobjects (calendarid, uri, calendardata, lastmodified, componenttype, firstoccurence, lastoccurence, size, etag, uid) VALUES (".$this->sqlite->quote_and_join($values, ',').")"; 244a1a3b679SAndreas Boehler $res = $this->sqlite->query($query); 24555a741c0SAndreas Boehler if($res !== false) 24655a741c0SAndreas Boehler { 24755a741c0SAndreas Boehler $this->updateSyncTokenLog($calid, $uri, 'added'); 248a1a3b679SAndreas Boehler return true; 249a1a3b679SAndreas Boehler } 25055a741c0SAndreas Boehler return false; 25155a741c0SAndreas Boehler } 252a1a3b679SAndreas Boehler 253b269830cSAndreas Boehler public function getCalendarSettings($calid) 254b269830cSAndreas Boehler { 255b269830cSAndreas Boehler $query = "SELECT principaluri, displayname, uri, description, components, transparent, synctoken FROM calendars WHERE id=".$this->sqlite->quote_string($calid); 256b269830cSAndreas Boehler $res = $this->sqlite->query($query); 257b269830cSAndreas Boehler $row = $this->sqlite->res2row($res); 258b269830cSAndreas Boehler return $row; 259b269830cSAndreas Boehler } 260b269830cSAndreas Boehler 261a1a3b679SAndreas Boehler public function getEventsWithinDateRange($id, $user, $startDate, $endDate) 262a1a3b679SAndreas Boehler { 263bd883736SAndreas Boehler $settings = $this->getPersonalSettings($user); 264bd883736SAndreas Boehler if($settings['timezone'] !== '' && $settings['timezone'] !== 'local') 265bd883736SAndreas Boehler $timezone = new \DateTimeZone($settings['timezone']); 266bd883736SAndreas Boehler else 267bd883736SAndreas Boehler $timezone = new \DateTimeZone('UTC'); 268a1a3b679SAndreas Boehler $data = array(); 269a1a3b679SAndreas Boehler require_once('vendor/autoload.php'); 270a1a3b679SAndreas Boehler $calid = $this->getCalendarIdForPage($id); 271a1a3b679SAndreas Boehler $startTs = new \DateTime($startDate); 272a1a3b679SAndreas Boehler $endTs = new \DateTime($endDate); 273b269830cSAndreas Boehler $query = "SELECT calendardata, componenttype, uid FROM calendarobjects WHERE (calendarid=". 274a1a3b679SAndreas Boehler $this->sqlite->quote_string($calid)." AND firstoccurence > ". 275a1a3b679SAndreas Boehler $this->sqlite->quote_string($startTs->getTimestamp())." AND firstoccurence < ". 276b269830cSAndreas Boehler $this->sqlite->quote_string($endTs->getTimestamp()).") OR (calendarid=". 277b269830cSAndreas Boehler $this->sqlite->quote_string($calid)." AND lastoccurence > ". 278b269830cSAndreas Boehler $this->sqlite->quote_string($startTs->getTimestamp())." AND lastoccurence < ". 279b269830cSAndreas Boehler $this->sqlite->quote_string($endTs->getTimestamp()).")"; 280a1a3b679SAndreas Boehler $res = $this->sqlite->query($query); 281a1a3b679SAndreas Boehler $arr = $this->sqlite->res2arr($res); 282a1a3b679SAndreas Boehler foreach($arr as $row) 283a1a3b679SAndreas Boehler { 284a1a3b679SAndreas Boehler if(isset($row['calendardata'])) 285a1a3b679SAndreas Boehler { 286b269830cSAndreas Boehler $entry = array(); 287a1a3b679SAndreas Boehler $vcal = \Sabre\VObject\Reader::read($row['calendardata']); 288b269830cSAndreas Boehler $start = $vcal->VEVENT->DTSTART; 289b269830cSAndreas Boehler if($start !== null) 290b269830cSAndreas Boehler { 291b269830cSAndreas Boehler $dtStart = $start->getDateTime(); 292b269830cSAndreas Boehler $dtStart->setTimezone($timezone); 293b269830cSAndreas Boehler $entry['start'] = $dtStart->format(\DateTime::ATOM); 294b269830cSAndreas Boehler if($start['VALUE'] == 'DATE') 295b269830cSAndreas Boehler $entry['allDay'] = true; 296b269830cSAndreas Boehler else 297b269830cSAndreas Boehler $entry['allDay'] = false; 298b269830cSAndreas Boehler } 299b269830cSAndreas Boehler $end = $vcal->VEVENT->DTEND; 300b269830cSAndreas Boehler if($end !== null) 301b269830cSAndreas Boehler { 302b269830cSAndreas Boehler $dtEnd = $end->getDateTime(); 303b269830cSAndreas Boehler $dtEnd->setTimezone($timezone); 304b269830cSAndreas Boehler $entry['end'] = $dtEnd->format(\DateTime::ATOM); 305b269830cSAndreas Boehler } 3060eebc909SAndreas Boehler $description = $vcal->VEVENT->DESCRIPTION; 3070eebc909SAndreas Boehler if($description !== null) 3080eebc909SAndreas Boehler $entry['description'] = (string)$description; 3090eebc909SAndreas Boehler else 3100eebc909SAndreas Boehler $entry['description'] = ''; 311b269830cSAndreas Boehler $entry['title'] = (string)$vcal->VEVENT->summary; 312b269830cSAndreas Boehler $entry['id'] = $row['uid']; 313b269830cSAndreas Boehler $data[] = $entry; 314a1a3b679SAndreas Boehler } 315a1a3b679SAndreas Boehler } 316a1a3b679SAndreas Boehler return $data; 317a1a3b679SAndreas Boehler } 318a1a3b679SAndreas Boehler 319a1a3b679SAndreas Boehler public function getEventWithUid($uid) 320a1a3b679SAndreas Boehler { 32155a741c0SAndreas Boehler $query = "SELECT calendardata, calendarid, componenttype, uri FROM calendarobjects WHERE uid=". 322a1a3b679SAndreas Boehler $this->sqlite->quote_string($uid); 323a1a3b679SAndreas Boehler $res = $this->sqlite->query($query); 324a1a3b679SAndreas Boehler $row = $this->sqlite->res2row($res); 325a1a3b679SAndreas Boehler return $row; 326a1a3b679SAndreas Boehler } 327a1a3b679SAndreas Boehler 328*f69bb449SAndreas Boehler public function getAllCalendarEvents($calid) 329*f69bb449SAndreas Boehler { 330*f69bb449SAndreas Boehler $query = "SELECT calendardata, uid, componenttype, uri FROM calendarobjects WHERE calendarid=". 331*f69bb449SAndreas Boehler $this->sqlite->quote_string($calid); 332*f69bb449SAndreas Boehler $res = $this->sqlite->query($query); 333*f69bb449SAndreas Boehler $arr = $this->sqlite->res2arr($res); 334*f69bb449SAndreas Boehler return $arr; 335*f69bb449SAndreas Boehler } 336*f69bb449SAndreas Boehler 337a1a3b679SAndreas Boehler public function editCalendarEntryForPage($id, $user, $params) 338a1a3b679SAndreas Boehler { 339bd883736SAndreas Boehler $settings = $this->getPersonalSettings($user); 340bd883736SAndreas Boehler if($settings['timezone'] !== '' && $settings['timezone'] !== 'local') 341bd883736SAndreas Boehler $timezone = new \DateTimeZone($settings['timezone']); 342a25c89eaSAndreas Boehler elseif($settings['timezone'] === 'local') 343a25c89eaSAndreas Boehler $timezone = new \DateTimeZone($params['detectedtz']); 344bd883736SAndreas Boehler else 345bd883736SAndreas Boehler $timezone = new \DateTimeZone('UTC'); 346b269830cSAndreas Boehler $startDate = explode('-', $params['eventfrom']); 347b269830cSAndreas Boehler $startTime = explode(':', $params['eventfromtime']); 348b269830cSAndreas Boehler $endDate = explode('-', $params['eventto']); 349b269830cSAndreas Boehler $endTime = explode(':', $params['eventtotime']); 35055a741c0SAndreas Boehler $uid = $params['uid']; 35155a741c0SAndreas Boehler $event = $this->getEventWithUid($uid); 352a1a3b679SAndreas Boehler require_once('vendor/autoload.php'); 353a1a3b679SAndreas Boehler if(!isset($event['calendardata'])) 354a1a3b679SAndreas Boehler return false; 35555a741c0SAndreas Boehler $uri = $event['uri']; 35655a741c0SAndreas Boehler $calid = $event['calendarid']; 357a1a3b679SAndreas Boehler $vcal = \Sabre\VObject\Reader::read($event['calendardata']); 358b269830cSAndreas Boehler $vevent = $vcal->VEVENT; 359b269830cSAndreas Boehler $vevent->summary = $params['eventname']; 360b269830cSAndreas Boehler $dtStamp = new \DateTime(null, new \DateTimeZone('UTC')); 3610eebc909SAndreas Boehler $description = $params['eventdescription']; 3620eebc909SAndreas Boehler $vevent->remove('DESCRIPTION'); 363b269830cSAndreas Boehler $vevent->remove('DTSTAMP'); 364b269830cSAndreas Boehler $vevent->remove('LAST-MODIFIED'); 365b269830cSAndreas Boehler $vevent->add('DTSTAMP', $dtStamp); 366b269830cSAndreas Boehler $vevent->add('LAST-MODIFIED', $dtStamp); 3670eebc909SAndreas Boehler if($description !== '') 3680eebc909SAndreas Boehler $vevent->add('DESCRIPTION', $description); 369b269830cSAndreas Boehler $dtStart = new \DateTime(); 370a25c89eaSAndreas Boehler $dtStart->setTimezone($timezone); 371b269830cSAndreas Boehler $dtStart->setDate(intval($startDate[0]), intval($startDate[1]), intval($startDate[2])); 372b269830cSAndreas Boehler if($params['allday'] != '1') 373b269830cSAndreas Boehler $dtStart->setTime(intval($startTime[0]), intval($startTime[1]), 0); 374b269830cSAndreas Boehler $dtEnd = new \DateTime(); 375a25c89eaSAndreas Boehler $dtEnd->setTimezone($timezone); 376b269830cSAndreas Boehler $dtEnd->setDate(intval($endDate[0]), intval($endDate[1]), intval($endDate[2])); 377b269830cSAndreas Boehler if($params['allday'] != '1') 378b269830cSAndreas Boehler $dtEnd->setTime(intval($endTime[0]), intval($endTime[1]), 0); 379b269830cSAndreas Boehler // According to the VCal spec, we need to add a whole day here 380b269830cSAndreas Boehler if($params['allday'] == '1') 381b269830cSAndreas Boehler $dtEnd->add(new \DateInterval('P1D')); 382b269830cSAndreas Boehler $vevent->remove('DTSTART'); 383b269830cSAndreas Boehler $vevent->remove('DTEND'); 384b269830cSAndreas Boehler $dtStartEv = $vevent->add('DTSTART', $dtStart); 385b269830cSAndreas Boehler $dtEndEv = $vevent->add('DTEND', $dtEnd); 386b269830cSAndreas Boehler if($params['allday'] == '1') 387b269830cSAndreas Boehler { 388b269830cSAndreas Boehler $dtStartEv['VALUE'] = 'DATE'; 389b269830cSAndreas Boehler $dtEndEv['VALUE'] = 'DATE'; 390b269830cSAndreas Boehler } 391a1a3b679SAndreas Boehler $now = new DateTime(); 392a1a3b679SAndreas Boehler $eventStr = $vcal->serialize(); 393a1a3b679SAndreas Boehler 394a1a3b679SAndreas Boehler $query = "UPDATE calendarobjects SET calendardata=".$this->sqlite->quote_string($eventStr). 395a1a3b679SAndreas Boehler ", lastmodified=".$this->sqlite->quote_string($now->getTimestamp()). 396a1a3b679SAndreas Boehler ", firstoccurence=".$this->sqlite->quote_string($dtStart->getTimestamp()). 397a1a3b679SAndreas Boehler ", lastoccurence=".$this->sqlite->quote_string($dtEnd->getTimestamp()). 398a1a3b679SAndreas Boehler ", size=".strlen($eventStr). 399a1a3b679SAndreas Boehler ", etag=".$this->sqlite->quote_string(md5($eventStr)). 40055a741c0SAndreas Boehler " WHERE uid=".$this->sqlite->quote_string($uid); 401a1a3b679SAndreas Boehler $res = $this->sqlite->query($query); 40255a741c0SAndreas Boehler if($res !== false) 40355a741c0SAndreas Boehler { 40455a741c0SAndreas Boehler $this->updateSyncTokenLog($calid, $uri, 'modified'); 405a1a3b679SAndreas Boehler return true; 406a1a3b679SAndreas Boehler } 40755a741c0SAndreas Boehler return false; 40855a741c0SAndreas Boehler } 409a1a3b679SAndreas Boehler 410a1a3b679SAndreas Boehler public function deleteCalendarEntryForPage($id, $params) 411a1a3b679SAndreas Boehler { 412a1a3b679SAndreas Boehler $uid = $params['uid']; 41355a741c0SAndreas Boehler $event = $this->getEventWithUid($uid); 4142c14b82bSAndreas Boehler $calid = $event['calendarid']; 41555a741c0SAndreas Boehler $uri = $event['uri']; 416a1a3b679SAndreas Boehler $query = "DELETE FROM calendarobjects WHERE uid=".$this->sqlite->quote_string($uid); 417a1a3b679SAndreas Boehler $res = $this->sqlite->query($query); 41855a741c0SAndreas Boehler if($res !== false) 41955a741c0SAndreas Boehler { 42055a741c0SAndreas Boehler $this->updateSyncTokenLog($calid, $uri, 'deleted'); 42155a741c0SAndreas Boehler } 422a1a3b679SAndreas Boehler return true; 423a1a3b679SAndreas Boehler } 424a1a3b679SAndreas Boehler 42555a741c0SAndreas Boehler public function getSyncTokenForCalendar($calid) 42655a741c0SAndreas Boehler { 427b269830cSAndreas Boehler $row = $this->getCalendarSettings($calid); 42855a741c0SAndreas Boehler if(isset($row['synctoken'])) 42955a741c0SAndreas Boehler return $row['synctoken']; 43055a741c0SAndreas Boehler return false; 43155a741c0SAndreas Boehler } 43255a741c0SAndreas Boehler 43355a741c0SAndreas Boehler public function operationNameToOperation($operationName) 43455a741c0SAndreas Boehler { 43555a741c0SAndreas Boehler switch($operationName) 43655a741c0SAndreas Boehler { 43755a741c0SAndreas Boehler case 'added': 43855a741c0SAndreas Boehler return 1; 43955a741c0SAndreas Boehler break; 44055a741c0SAndreas Boehler case 'modified': 44155a741c0SAndreas Boehler return 2; 44255a741c0SAndreas Boehler break; 44355a741c0SAndreas Boehler case 'deleted': 44455a741c0SAndreas Boehler return 3; 44555a741c0SAndreas Boehler break; 44655a741c0SAndreas Boehler } 44755a741c0SAndreas Boehler return false; 44855a741c0SAndreas Boehler } 44955a741c0SAndreas Boehler 45055a741c0SAndreas Boehler private function updateSyncTokenLog($calid, $uri, $operation) 45155a741c0SAndreas Boehler { 45255a741c0SAndreas Boehler $currentToken = $this->getSyncTokenForCalendar($calid); 45355a741c0SAndreas Boehler $operationCode = $this->operationNameToOperation($operation); 45455a741c0SAndreas Boehler if(($operationCode === false) || ($currentToken === false)) 45555a741c0SAndreas Boehler return false; 45655a741c0SAndreas Boehler $values = array($uri, 45755a741c0SAndreas Boehler $currentToken, 45855a741c0SAndreas Boehler $calid, 45955a741c0SAndreas Boehler $operationCode 46055a741c0SAndreas Boehler ); 46155a741c0SAndreas Boehler $query = "INSERT INTO calendarchanges (uri, synctoken, calendarid, operation) VALUES(". 46255a741c0SAndreas Boehler $this->sqlite->quote_and_join($values, ',').")"; 46355a741c0SAndreas Boehler $res = $this->sqlite->query($query); 46455a741c0SAndreas Boehler if($res === false) 46555a741c0SAndreas Boehler return false; 46655a741c0SAndreas Boehler $currentToken++; 46755a741c0SAndreas Boehler $query = "UPDATE calendars SET synctoken=".$this->sqlite->quote_string($currentToken)." WHERE id=". 46855a741c0SAndreas Boehler $this->sqlite->quote_string($calid); 46955a741c0SAndreas Boehler $res = $this->sqlite->query($query); 47055a741c0SAndreas Boehler return ($res !== false); 47155a741c0SAndreas Boehler } 47255a741c0SAndreas Boehler 473b269830cSAndreas Boehler public function getSyncUrlForPage($id, $user = null) 474b269830cSAndreas Boehler { 475b269830cSAndreas Boehler if(is_null($user)) 476b269830cSAndreas Boehler $user = $_SERVER['REMOTE_USER']; 477b269830cSAndreas Boehler 478b269830cSAndreas Boehler $calid = $this->getCalendarIdForPage($id); 479b269830cSAndreas Boehler if($calid === false) 480b269830cSAndreas Boehler return false; 481b269830cSAndreas Boehler 482b269830cSAndreas Boehler $calsettings = $this->getCalendarSettings($calid); 483b269830cSAndreas Boehler if(!isset($calsettings['uri'])) 484b269830cSAndreas Boehler return false; 485b269830cSAndreas Boehler 486b269830cSAndreas Boehler $syncurl = DOKU_URL.'lib/plugins/davcal/calendarserver.php/calendars/'.$user.'/'.$calsettings['uri']; 487b269830cSAndreas Boehler return $syncurl; 488b269830cSAndreas Boehler } 489b269830cSAndreas Boehler 490*f69bb449SAndreas Boehler public function getPrivateURLForPage($id) 491*f69bb449SAndreas Boehler { 492*f69bb449SAndreas Boehler $calid = $this->getCalendarIdForPage($id); 493*f69bb449SAndreas Boehler if($calid === false) 494*f69bb449SAndreas Boehler return false; 495*f69bb449SAndreas Boehler 496*f69bb449SAndreas Boehler return $this->getPrivateURLForCalendar($calid); 497*f69bb449SAndreas Boehler } 498*f69bb449SAndreas Boehler 499*f69bb449SAndreas Boehler public function getPrivateURLForCalendar($calid) 500*f69bb449SAndreas Boehler { 501*f69bb449SAndreas Boehler $query = "SELECT url FROM calendartoprivateurlmapping WHERE calid=".$this->sqlite->quote_string($calid); 502*f69bb449SAndreas Boehler $res = $this->sqlite->query($query); 503*f69bb449SAndreas Boehler $row = $this->sqlite->res2row($res); 504*f69bb449SAndreas Boehler if(!isset($row['url'])) 505*f69bb449SAndreas Boehler { 506*f69bb449SAndreas Boehler $url = uniqid("dokuwiki-").".ics"; 507*f69bb449SAndreas Boehler $values = array( 508*f69bb449SAndreas Boehler $url, 509*f69bb449SAndreas Boehler $calid 510*f69bb449SAndreas Boehler ); 511*f69bb449SAndreas Boehler $query = "INSERT INTO calendartoprivateurlmapping (url, calid) VALUES(". 512*f69bb449SAndreas Boehler $this->sqlite->quote_and_join($values, ", ").")"; 513*f69bb449SAndreas Boehler $res = $this->sqlite->query($query); 514*f69bb449SAndreas Boehler if($res === false) 515*f69bb449SAndreas Boehler return false; 516*f69bb449SAndreas Boehler } 517*f69bb449SAndreas Boehler else 518*f69bb449SAndreas Boehler { 519*f69bb449SAndreas Boehler $url = $row['url']; 520*f69bb449SAndreas Boehler } 521*f69bb449SAndreas Boehler return DOKU_URL.'lib/plugins/davcal/calendarserver.php/calendars/'.$url; 522*f69bb449SAndreas Boehler } 523*f69bb449SAndreas Boehler 524*f69bb449SAndreas Boehler public function getCalendarForPrivateURL($url) 525*f69bb449SAndreas Boehler { 526*f69bb449SAndreas Boehler $query = "SELECT calid FROM calendartoprivateurlmapping WHERE url=".$this->sqlite->quote_string($url); 527*f69bb449SAndreas Boehler $res = $this->sqlite->query($query); 528*f69bb449SAndreas Boehler $row = $this->sqlite->res2row($res); 529*f69bb449SAndreas Boehler if(!isset($row['calid'])) 530*f69bb449SAndreas Boehler return false; 531*f69bb449SAndreas Boehler return $row['calid']; 532*f69bb449SAndreas Boehler } 533*f69bb449SAndreas Boehler 534*f69bb449SAndreas Boehler public function getCalendarAsICSFeed($calid) 535*f69bb449SAndreas Boehler { 536*f69bb449SAndreas Boehler $calSettings = $this->getCalendarSettings($calid); 537*f69bb449SAndreas Boehler if($calSettings === false) 538*f69bb449SAndreas Boehler return false; 539*f69bb449SAndreas Boehler $events = $this->getAllCalendarEvents($calid); 540*f69bb449SAndreas Boehler if($events === false) 541*f69bb449SAndreas Boehler return false; 542*f69bb449SAndreas Boehler 543*f69bb449SAndreas Boehler $out = "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//DAVCal//DAVCal for DokuWiki//EN\nCALSCALE:GREGORIAN\nX-WR-CALNAME:"; 544*f69bb449SAndreas Boehler $out .= $calSettings['displayname']."\n"; 545*f69bb449SAndreas Boehler foreach($events as $event) 546*f69bb449SAndreas Boehler { 547*f69bb449SAndreas Boehler $out .= rtrim($event['calendardata']); 548*f69bb449SAndreas Boehler $out .= "\n"; 549*f69bb449SAndreas Boehler } 550*f69bb449SAndreas Boehler $out .= "END:VCALENDAR\n"; 551*f69bb449SAndreas Boehler return $out; 552*f69bb449SAndreas Boehler } 553*f69bb449SAndreas Boehler 554a1a3b679SAndreas Boehler} 555