@@ -398,7 +398,9 @@ discard block |
||
| 398 | 398 | $stmt->execute([$calendarId, $objectUri]); |
| 399 | 399 | $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
| 400 | 400 | |
| 401 | - if (!$row) return null; |
|
| 401 | + if (!$row) { |
|
| 402 | + return null; |
|
| 403 | + } |
|
| 402 | 404 | |
| 403 | 405 | return [ |
| 404 | 406 | 'id' => $row['id'], |
@@ -862,7 +864,9 @@ discard block |
||
| 862 | 864 | $stmt->execute([ $calendarId ]); |
| 863 | 865 | $currentToken = $stmt->fetchColumn(0); |
| 864 | 866 | |
| 865 | - if (is_null($currentToken)) return null; |
|
| 867 | + if (is_null($currentToken)) { |
|
| 868 | + return null; |
|
| 869 | + } |
|
| 866 | 870 | |
| 867 | 871 | $result = [ |
| 868 | 872 | 'syncToken' => $currentToken, |
@@ -874,7 +878,9 @@ discard block |
||
| 874 | 878 | if ($syncToken) { |
| 875 | 879 | |
| 876 | 880 | $query = sprintf('SELECT uri, operation FROM %s WHERE synctoken >= ? && synctoken < ? && calendarid = ? ORDER BY synctoken', $this->calendarChangesTableName); |
| 877 | - if ($limit > 0) $query .= " LIMIT " . (int)$limit; |
|
| 881 | + if ($limit > 0) { |
|
| 882 | + $query .= " LIMIT " . (int)$limit; |
|
| 883 | + } |
|
| 878 | 884 | |
| 879 | 885 | // Fetching all changes |
| 880 | 886 | $stmt = $this->pdo->prepare($query); |
@@ -1150,7 +1156,9 @@ discard block |
||
| 1150 | 1156 | $stmt->execute([$principalUri, $objectUri]); |
| 1151 | 1157 | $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
| 1152 | 1158 | |
| 1153 | - if (!$row) return null; |
|
| 1159 | + if (!$row) { |
|
| 1160 | + return null; |
|
| 1161 | + } |
|
| 1154 | 1162 | |
| 1155 | 1163 | return [ |
| 1156 | 1164 | 'uri' => $row['uri'], |
@@ -19,763 +19,763 @@ discard block |
||
| 19 | 19 | */ |
| 20 | 20 | class PDO extends AbstractBackend implements SyncSupport, SubscriptionSupport, SchedulingSupport { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * We need to specify a max date, because we need to stop *somewhere* |
|
| 24 | - * |
|
| 25 | - * On 32 bit system the maximum for a signed integer is 2147483647, so |
|
| 26 | - * MAX_DATE cannot be higher than date('Y-m-d', 2147483647) which results |
|
| 27 | - * in 2038-01-19 to avoid problems when the date is converted |
|
| 28 | - * to a unix timestamp. |
|
| 29 | - */ |
|
| 30 | - const MAX_DATE = '2038-01-01'; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * pdo |
|
| 34 | - * |
|
| 35 | - * @var \PDO |
|
| 36 | - */ |
|
| 37 | - protected $pdo; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * The table name that will be used for calendars |
|
| 41 | - * |
|
| 42 | - * @var string |
|
| 43 | - */ |
|
| 44 | - public $calendarTableName = 'calendars'; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * The table name that will be used for calendar objects |
|
| 48 | - * |
|
| 49 | - * @var string |
|
| 50 | - */ |
|
| 51 | - public $calendarObjectTableName = 'calendarobjects'; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * The table name that will be used for tracking changes in calendars. |
|
| 55 | - * |
|
| 56 | - * @var string |
|
| 57 | - */ |
|
| 58 | - public $calendarChangesTableName = 'calendarchanges'; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * The table name that will be used inbox items. |
|
| 62 | - * |
|
| 63 | - * @var string |
|
| 64 | - */ |
|
| 65 | - public $schedulingObjectTableName = 'schedulingobjects'; |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * The table name that will be used for calendar subscriptions. |
|
| 69 | - * |
|
| 70 | - * @var string |
|
| 71 | - */ |
|
| 72 | - public $calendarSubscriptionsTableName = 'calendarsubscriptions'; |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * List of CalDAV properties, and how they map to database fieldnames |
|
| 76 | - * Add your own properties by simply adding on to this array. |
|
| 77 | - * |
|
| 78 | - * Note that only string-based properties are supported here. |
|
| 79 | - * |
|
| 80 | - * @var array |
|
| 81 | - */ |
|
| 82 | - public $propertyMap = [ |
|
| 83 | - '{DAV:}displayname' => 'displayname', |
|
| 84 | - '{urn:ietf:params:xml:ns:caldav}calendar-description' => 'description', |
|
| 85 | - '{urn:ietf:params:xml:ns:caldav}calendar-timezone' => 'timezone', |
|
| 86 | - '{http://apple.com/ns/ical/}calendar-order' => 'calendarorder', |
|
| 87 | - '{http://apple.com/ns/ical/}calendar-color' => 'calendarcolor', |
|
| 88 | - ]; |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * List of subscription properties, and how they map to database fieldnames. |
|
| 92 | - * |
|
| 93 | - * @var array |
|
| 94 | - */ |
|
| 95 | - public $subscriptionPropertyMap = [ |
|
| 96 | - '{DAV:}displayname' => 'displayname', |
|
| 97 | - '{http://apple.com/ns/ical/}refreshrate' => 'refreshrate', |
|
| 98 | - '{http://apple.com/ns/ical/}calendar-order' => 'calendarorder', |
|
| 99 | - '{http://apple.com/ns/ical/}calendar-color' => 'calendarcolor', |
|
| 100 | - '{http://calendarserver.org/ns/}subscribed-strip-todos' => 'striptodos', |
|
| 101 | - '{http://calendarserver.org/ns/}subscribed-strip-alarms' => 'stripalarms', |
|
| 102 | - '{http://calendarserver.org/ns/}subscribed-strip-attachments' => 'stripattachments', |
|
| 103 | - ]; |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * Creates the backend |
|
| 107 | - * |
|
| 108 | - * @param \PDO $pdo |
|
| 109 | - */ |
|
| 110 | - public function __construct(\PDO $pdo) { |
|
| 111 | - |
|
| 112 | - $this->pdo = $pdo; |
|
| 113 | - |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Returns a list of calendars for a principal. |
|
| 118 | - * |
|
| 119 | - * Every project is an array with the following keys: |
|
| 120 | - * * id, a unique id that will be used by other functions to modify the |
|
| 121 | - * calendar. This can be the same as the uri or a database key. |
|
| 122 | - * * uri. This is just the 'base uri' or 'filename' of the calendar. |
|
| 123 | - * * principaluri. The owner of the calendar. Almost always the same as |
|
| 124 | - * principalUri passed to this method. |
|
| 125 | - * |
|
| 126 | - * Furthermore it can contain webdav properties in clark notation. A very |
|
| 127 | - * common one is '{DAV:}displayname'. |
|
| 128 | - * |
|
| 129 | - * Many clients also require: |
|
| 130 | - * {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
|
| 131 | - * For this property, you can just return an instance of |
|
| 132 | - * Sabre\CalDAV\Xml\Property\SupportedCalendarComponentSet. |
|
| 133 | - * |
|
| 134 | - * If you return {http://sabredav.org/ns}read-only and set the value to 1, |
|
| 135 | - * ACL will automatically be put in read-only mode. |
|
| 136 | - * |
|
| 137 | - * @param string $principalUri |
|
| 138 | - * @return array |
|
| 139 | - */ |
|
| 140 | - public function getCalendarsForUser($principalUri) { |
|
| 141 | - |
|
| 142 | - $fields = array_values($this->propertyMap); |
|
| 143 | - $fields[] = 'id'; |
|
| 144 | - $fields[] = 'uri'; |
|
| 145 | - $fields[] = 'synctoken'; |
|
| 146 | - $fields[] = 'components'; |
|
| 147 | - $fields[] = 'principaluri'; |
|
| 148 | - $fields[] = 'transparent'; |
|
| 149 | - |
|
| 150 | - // Making fields a comma-delimited list |
|
| 151 | - $fields = implode(', ', $fields); |
|
| 22 | + /** |
|
| 23 | + * We need to specify a max date, because we need to stop *somewhere* |
|
| 24 | + * |
|
| 25 | + * On 32 bit system the maximum for a signed integer is 2147483647, so |
|
| 26 | + * MAX_DATE cannot be higher than date('Y-m-d', 2147483647) which results |
|
| 27 | + * in 2038-01-19 to avoid problems when the date is converted |
|
| 28 | + * to a unix timestamp. |
|
| 29 | + */ |
|
| 30 | + const MAX_DATE = '2038-01-01'; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * pdo |
|
| 34 | + * |
|
| 35 | + * @var \PDO |
|
| 36 | + */ |
|
| 37 | + protected $pdo; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * The table name that will be used for calendars |
|
| 41 | + * |
|
| 42 | + * @var string |
|
| 43 | + */ |
|
| 44 | + public $calendarTableName = 'calendars'; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * The table name that will be used for calendar objects |
|
| 48 | + * |
|
| 49 | + * @var string |
|
| 50 | + */ |
|
| 51 | + public $calendarObjectTableName = 'calendarobjects'; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * The table name that will be used for tracking changes in calendars. |
|
| 55 | + * |
|
| 56 | + * @var string |
|
| 57 | + */ |
|
| 58 | + public $calendarChangesTableName = 'calendarchanges'; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * The table name that will be used inbox items. |
|
| 62 | + * |
|
| 63 | + * @var string |
|
| 64 | + */ |
|
| 65 | + public $schedulingObjectTableName = 'schedulingobjects'; |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * The table name that will be used for calendar subscriptions. |
|
| 69 | + * |
|
| 70 | + * @var string |
|
| 71 | + */ |
|
| 72 | + public $calendarSubscriptionsTableName = 'calendarsubscriptions'; |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * List of CalDAV properties, and how they map to database fieldnames |
|
| 76 | + * Add your own properties by simply adding on to this array. |
|
| 77 | + * |
|
| 78 | + * Note that only string-based properties are supported here. |
|
| 79 | + * |
|
| 80 | + * @var array |
|
| 81 | + */ |
|
| 82 | + public $propertyMap = [ |
|
| 83 | + '{DAV:}displayname' => 'displayname', |
|
| 84 | + '{urn:ietf:params:xml:ns:caldav}calendar-description' => 'description', |
|
| 85 | + '{urn:ietf:params:xml:ns:caldav}calendar-timezone' => 'timezone', |
|
| 86 | + '{http://apple.com/ns/ical/}calendar-order' => 'calendarorder', |
|
| 87 | + '{http://apple.com/ns/ical/}calendar-color' => 'calendarcolor', |
|
| 88 | + ]; |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * List of subscription properties, and how they map to database fieldnames. |
|
| 92 | + * |
|
| 93 | + * @var array |
|
| 94 | + */ |
|
| 95 | + public $subscriptionPropertyMap = [ |
|
| 96 | + '{DAV:}displayname' => 'displayname', |
|
| 97 | + '{http://apple.com/ns/ical/}refreshrate' => 'refreshrate', |
|
| 98 | + '{http://apple.com/ns/ical/}calendar-order' => 'calendarorder', |
|
| 99 | + '{http://apple.com/ns/ical/}calendar-color' => 'calendarcolor', |
|
| 100 | + '{http://calendarserver.org/ns/}subscribed-strip-todos' => 'striptodos', |
|
| 101 | + '{http://calendarserver.org/ns/}subscribed-strip-alarms' => 'stripalarms', |
|
| 102 | + '{http://calendarserver.org/ns/}subscribed-strip-attachments' => 'stripattachments', |
|
| 103 | + ]; |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * Creates the backend |
|
| 107 | + * |
|
| 108 | + * @param \PDO $pdo |
|
| 109 | + */ |
|
| 110 | + public function __construct(\PDO $pdo) { |
|
| 111 | + |
|
| 112 | + $this->pdo = $pdo; |
|
| 113 | + |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * Returns a list of calendars for a principal. |
|
| 118 | + * |
|
| 119 | + * Every project is an array with the following keys: |
|
| 120 | + * * id, a unique id that will be used by other functions to modify the |
|
| 121 | + * calendar. This can be the same as the uri or a database key. |
|
| 122 | + * * uri. This is just the 'base uri' or 'filename' of the calendar. |
|
| 123 | + * * principaluri. The owner of the calendar. Almost always the same as |
|
| 124 | + * principalUri passed to this method. |
|
| 125 | + * |
|
| 126 | + * Furthermore it can contain webdav properties in clark notation. A very |
|
| 127 | + * common one is '{DAV:}displayname'. |
|
| 128 | + * |
|
| 129 | + * Many clients also require: |
|
| 130 | + * {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
|
| 131 | + * For this property, you can just return an instance of |
|
| 132 | + * Sabre\CalDAV\Xml\Property\SupportedCalendarComponentSet. |
|
| 133 | + * |
|
| 134 | + * If you return {http://sabredav.org/ns}read-only and set the value to 1, |
|
| 135 | + * ACL will automatically be put in read-only mode. |
|
| 136 | + * |
|
| 137 | + * @param string $principalUri |
|
| 138 | + * @return array |
|
| 139 | + */ |
|
| 140 | + public function getCalendarsForUser($principalUri) { |
|
| 141 | + |
|
| 142 | + $fields = array_values($this->propertyMap); |
|
| 143 | + $fields[] = 'id'; |
|
| 144 | + $fields[] = 'uri'; |
|
| 145 | + $fields[] = 'synctoken'; |
|
| 146 | + $fields[] = 'components'; |
|
| 147 | + $fields[] = 'principaluri'; |
|
| 148 | + $fields[] = 'transparent'; |
|
| 149 | + |
|
| 150 | + // Making fields a comma-delimited list |
|
| 151 | + $fields = implode(', ', $fields); |
|
| 152 | 152 | $stmt = $this->pdo->prepare( |
| 153 | 153 | <<<SQL |
| 154 | 154 | SELECT $fields FROM {$this->calendarTableName} |
| 155 | 155 | WHERE principaluri = ? ORDER BY calendarorder ASC |
| 156 | 156 | SQL |
| 157 | 157 | ); |
| 158 | - $stmt->execute([$principalUri]); |
|
| 159 | - |
|
| 160 | - $calendars = []; |
|
| 161 | - while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 162 | - |
|
| 163 | - $components = []; |
|
| 164 | - if ($row['components']) { |
|
| 165 | - $components = explode(',', $row['components']); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - $calendar = [ |
|
| 169 | - 'id' => $row['id'], |
|
| 170 | - 'uri' => $row['uri'], |
|
| 171 | - 'principaluri' => $row['principaluri'], |
|
| 172 | - '{' . CalDAV\Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken'] ? $row['synctoken'] : '0'), |
|
| 173 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ? $row['synctoken'] : '0', |
|
| 174 | - '{' . CalDAV\Plugin::NS_CALDAV . '}supported-calendar-component-set' => new CalDAV\Xml\Property\SupportedCalendarComponentSet($components), |
|
| 175 | - '{' . CalDAV\Plugin::NS_CALDAV . '}schedule-calendar-transp' => new CalDAV\Xml\Property\ScheduleCalendarTransp($row['transparent'] ? 'transparent' : 'opaque'), |
|
| 176 | - ]; |
|
| 177 | - |
|
| 178 | - |
|
| 179 | - foreach ($this->propertyMap as $xmlName => $dbName) { |
|
| 180 | - $calendar[$xmlName] = $row[$dbName]; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - $calendars[] = $calendar; |
|
| 184 | - |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - return $calendars; |
|
| 188 | - |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * Creates a new calendar for a principal. |
|
| 193 | - * |
|
| 194 | - * If the creation was a success, an id must be returned that can be used |
|
| 195 | - * to reference this calendar in other methods, such as updateCalendar. |
|
| 196 | - * |
|
| 197 | - * @param string $principalUri |
|
| 198 | - * @param string $calendarUri |
|
| 199 | - * @param array $properties |
|
| 200 | - * @return string |
|
| 201 | - */ |
|
| 202 | - public function createCalendar($principalUri, $calendarUri, array $properties) { |
|
| 203 | - |
|
| 204 | - $fieldNames = [ |
|
| 205 | - 'principaluri', |
|
| 206 | - 'uri', |
|
| 207 | - 'synctoken', |
|
| 208 | - 'transparent', |
|
| 209 | - ]; |
|
| 210 | - $values = [ |
|
| 211 | - ':principaluri' => $principalUri, |
|
| 212 | - ':uri' => $calendarUri, |
|
| 213 | - ':synctoken' => 1, |
|
| 214 | - ':transparent' => 0, |
|
| 215 | - ]; |
|
| 216 | - |
|
| 217 | - // Default value |
|
| 218 | - $sccs = '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set'; |
|
| 219 | - $fieldNames[] = 'components'; |
|
| 220 | - if (!isset($properties[$sccs])) { |
|
| 221 | - $values[':components'] = 'VEVENT,VTODO'; |
|
| 222 | - } else { |
|
| 223 | - if (!($properties[$sccs] instanceof CalDAV\Xml\Property\SupportedCalendarComponentSet)) { |
|
| 224 | - throw new DAV\Exception('The ' . $sccs . ' property must be of type: \Sabre\CalDAV\Xml\Property\SupportedCalendarComponentSet'); |
|
| 225 | - } |
|
| 226 | - $values[':components'] = implode(',', $properties[$sccs]->getValue()); |
|
| 227 | - } |
|
| 228 | - $transp = '{' . CalDAV\Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
| 229 | - if (isset($properties[$transp])) { |
|
| 230 | - $values[':transparent'] = $properties[$transp]->getValue() === 'transparent'; |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - foreach ($this->propertyMap as $xmlName => $dbName) { |
|
| 234 | - if (isset($properties[$xmlName])) { |
|
| 235 | - |
|
| 236 | - $values[':' . $dbName] = $properties[$xmlName]; |
|
| 237 | - $fieldNames[] = $dbName; |
|
| 238 | - } |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - $stmt = $this->pdo->prepare("INSERT INTO " . $this->calendarTableName . " (" . implode(', ', $fieldNames) . ") VALUES (" . implode(', ', array_keys($values)) . ")"); |
|
| 242 | - $stmt->execute($values); |
|
| 243 | - |
|
| 244 | - return $this->pdo->lastInsertId(); |
|
| 245 | - |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - /** |
|
| 249 | - * Updates properties for a calendar. |
|
| 250 | - * |
|
| 251 | - * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
| 252 | - * To do the actual updates, you must tell this object which properties |
|
| 253 | - * you're going to process with the handle() method. |
|
| 254 | - * |
|
| 255 | - * Calling the handle method is like telling the PropPatch object "I |
|
| 256 | - * promise I can handle updating this property". |
|
| 257 | - * |
|
| 258 | - * Read the PropPatch documenation for more info and examples. |
|
| 259 | - * |
|
| 260 | - * @param string $calendarId |
|
| 261 | - * @param \Sabre\DAV\PropPatch $propPatch |
|
| 262 | - * @return void |
|
| 263 | - */ |
|
| 264 | - public function updateCalendar($calendarId, \Sabre\DAV\PropPatch $propPatch) { |
|
| 265 | - |
|
| 266 | - $supportedProperties = array_keys($this->propertyMap); |
|
| 267 | - $supportedProperties[] = '{' . CalDAV\Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
| 268 | - |
|
| 269 | - $propPatch->handle($supportedProperties, function($mutations) use ($calendarId) { |
|
| 270 | - $newValues = []; |
|
| 271 | - foreach ($mutations as $propertyName => $propertyValue) { |
|
| 272 | - |
|
| 273 | - switch ($propertyName) { |
|
| 274 | - case '{' . CalDAV\Plugin::NS_CALDAV . '}schedule-calendar-transp' : |
|
| 275 | - $fieldName = 'transparent'; |
|
| 276 | - $newValues[$fieldName] = $propertyValue->getValue() === 'transparent'; |
|
| 277 | - break; |
|
| 278 | - default : |
|
| 279 | - $fieldName = $this->propertyMap[$propertyName]; |
|
| 280 | - $newValues[$fieldName] = $propertyValue; |
|
| 281 | - break; |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - } |
|
| 285 | - $valuesSql = []; |
|
| 286 | - foreach ($newValues as $fieldName => $value) { |
|
| 287 | - $valuesSql[] = $fieldName . ' = ?'; |
|
| 288 | - } |
|
| 158 | + $stmt->execute([$principalUri]); |
|
| 159 | + |
|
| 160 | + $calendars = []; |
|
| 161 | + while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 162 | + |
|
| 163 | + $components = []; |
|
| 164 | + if ($row['components']) { |
|
| 165 | + $components = explode(',', $row['components']); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + $calendar = [ |
|
| 169 | + 'id' => $row['id'], |
|
| 170 | + 'uri' => $row['uri'], |
|
| 171 | + 'principaluri' => $row['principaluri'], |
|
| 172 | + '{' . CalDAV\Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken'] ? $row['synctoken'] : '0'), |
|
| 173 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ? $row['synctoken'] : '0', |
|
| 174 | + '{' . CalDAV\Plugin::NS_CALDAV . '}supported-calendar-component-set' => new CalDAV\Xml\Property\SupportedCalendarComponentSet($components), |
|
| 175 | + '{' . CalDAV\Plugin::NS_CALDAV . '}schedule-calendar-transp' => new CalDAV\Xml\Property\ScheduleCalendarTransp($row['transparent'] ? 'transparent' : 'opaque'), |
|
| 176 | + ]; |
|
| 177 | + |
|
| 178 | + |
|
| 179 | + foreach ($this->propertyMap as $xmlName => $dbName) { |
|
| 180 | + $calendar[$xmlName] = $row[$dbName]; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + $calendars[] = $calendar; |
|
| 184 | + |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + return $calendars; |
|
| 188 | + |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * Creates a new calendar for a principal. |
|
| 193 | + * |
|
| 194 | + * If the creation was a success, an id must be returned that can be used |
|
| 195 | + * to reference this calendar in other methods, such as updateCalendar. |
|
| 196 | + * |
|
| 197 | + * @param string $principalUri |
|
| 198 | + * @param string $calendarUri |
|
| 199 | + * @param array $properties |
|
| 200 | + * @return string |
|
| 201 | + */ |
|
| 202 | + public function createCalendar($principalUri, $calendarUri, array $properties) { |
|
| 203 | + |
|
| 204 | + $fieldNames = [ |
|
| 205 | + 'principaluri', |
|
| 206 | + 'uri', |
|
| 207 | + 'synctoken', |
|
| 208 | + 'transparent', |
|
| 209 | + ]; |
|
| 210 | + $values = [ |
|
| 211 | + ':principaluri' => $principalUri, |
|
| 212 | + ':uri' => $calendarUri, |
|
| 213 | + ':synctoken' => 1, |
|
| 214 | + ':transparent' => 0, |
|
| 215 | + ]; |
|
| 216 | + |
|
| 217 | + // Default value |
|
| 218 | + $sccs = '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set'; |
|
| 219 | + $fieldNames[] = 'components'; |
|
| 220 | + if (!isset($properties[$sccs])) { |
|
| 221 | + $values[':components'] = 'VEVENT,VTODO'; |
|
| 222 | + } else { |
|
| 223 | + if (!($properties[$sccs] instanceof CalDAV\Xml\Property\SupportedCalendarComponentSet)) { |
|
| 224 | + throw new DAV\Exception('The ' . $sccs . ' property must be of type: \Sabre\CalDAV\Xml\Property\SupportedCalendarComponentSet'); |
|
| 225 | + } |
|
| 226 | + $values[':components'] = implode(',', $properties[$sccs]->getValue()); |
|
| 227 | + } |
|
| 228 | + $transp = '{' . CalDAV\Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
| 229 | + if (isset($properties[$transp])) { |
|
| 230 | + $values[':transparent'] = $properties[$transp]->getValue() === 'transparent'; |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + foreach ($this->propertyMap as $xmlName => $dbName) { |
|
| 234 | + if (isset($properties[$xmlName])) { |
|
| 235 | + |
|
| 236 | + $values[':' . $dbName] = $properties[$xmlName]; |
|
| 237 | + $fieldNames[] = $dbName; |
|
| 238 | + } |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + $stmt = $this->pdo->prepare("INSERT INTO " . $this->calendarTableName . " (" . implode(', ', $fieldNames) . ") VALUES (" . implode(', ', array_keys($values)) . ")"); |
|
| 242 | + $stmt->execute($values); |
|
| 243 | + |
|
| 244 | + return $this->pdo->lastInsertId(); |
|
| 245 | + |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + /** |
|
| 249 | + * Updates properties for a calendar. |
|
| 250 | + * |
|
| 251 | + * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
| 252 | + * To do the actual updates, you must tell this object which properties |
|
| 253 | + * you're going to process with the handle() method. |
|
| 254 | + * |
|
| 255 | + * Calling the handle method is like telling the PropPatch object "I |
|
| 256 | + * promise I can handle updating this property". |
|
| 257 | + * |
|
| 258 | + * Read the PropPatch documenation for more info and examples. |
|
| 259 | + * |
|
| 260 | + * @param string $calendarId |
|
| 261 | + * @param \Sabre\DAV\PropPatch $propPatch |
|
| 262 | + * @return void |
|
| 263 | + */ |
|
| 264 | + public function updateCalendar($calendarId, \Sabre\DAV\PropPatch $propPatch) { |
|
| 265 | + |
|
| 266 | + $supportedProperties = array_keys($this->propertyMap); |
|
| 267 | + $supportedProperties[] = '{' . CalDAV\Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
| 268 | + |
|
| 269 | + $propPatch->handle($supportedProperties, function($mutations) use ($calendarId) { |
|
| 270 | + $newValues = []; |
|
| 271 | + foreach ($mutations as $propertyName => $propertyValue) { |
|
| 272 | + |
|
| 273 | + switch ($propertyName) { |
|
| 274 | + case '{' . CalDAV\Plugin::NS_CALDAV . '}schedule-calendar-transp' : |
|
| 275 | + $fieldName = 'transparent'; |
|
| 276 | + $newValues[$fieldName] = $propertyValue->getValue() === 'transparent'; |
|
| 277 | + break; |
|
| 278 | + default : |
|
| 279 | + $fieldName = $this->propertyMap[$propertyName]; |
|
| 280 | + $newValues[$fieldName] = $propertyValue; |
|
| 281 | + break; |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + } |
|
| 285 | + $valuesSql = []; |
|
| 286 | + foreach ($newValues as $fieldName => $value) { |
|
| 287 | + $valuesSql[] = $fieldName . ' = ?'; |
|
| 288 | + } |
|
| 289 | 289 | $query = sprintf('UPDATE %s SET %s WHERE id = ?', $this->calendarTableName, implode(', ', $valuesSql)); |
| 290 | - $stmt = $this->pdo->prepare($query); |
|
| 291 | - $newValues['id'] = $calendarId; |
|
| 292 | - $stmt->execute(array_values($newValues)); |
|
| 290 | + $stmt = $this->pdo->prepare($query); |
|
| 291 | + $newValues['id'] = $calendarId; |
|
| 292 | + $stmt->execute(array_values($newValues)); |
|
| 293 | 293 | |
| 294 | - $this->addChange($calendarId, "", 2); |
|
| 294 | + $this->addChange($calendarId, "", 2); |
|
| 295 | 295 | |
| 296 | - return true; |
|
| 296 | + return true; |
|
| 297 | 297 | |
| 298 | - }); |
|
| 298 | + }); |
|
| 299 | 299 | |
| 300 | - } |
|
| 300 | + } |
|
| 301 | 301 | |
| 302 | - /** |
|
| 303 | - * Delete a calendar and all it's objects |
|
| 304 | - * |
|
| 305 | - * @param string $calendarId |
|
| 306 | - * @return void |
|
| 307 | - */ |
|
| 308 | - public function deleteCalendar($calendarId) { |
|
| 302 | + /** |
|
| 303 | + * Delete a calendar and all it's objects |
|
| 304 | + * |
|
| 305 | + * @param string $calendarId |
|
| 306 | + * @return void |
|
| 307 | + */ |
|
| 308 | + public function deleteCalendar($calendarId) { |
|
| 309 | 309 | |
| 310 | 310 | $query = sprintf('DELETE FROM %s WHERE calendarid = ?', $this->calendarObjectTableName); |
| 311 | - $stmt = $this->pdo->prepare($query); |
|
| 312 | - $stmt->execute([$calendarId]); |
|
| 311 | + $stmt = $this->pdo->prepare($query); |
|
| 312 | + $stmt->execute([$calendarId]); |
|
| 313 | 313 | |
| 314 | 314 | $query = sprintf('DELETE FROM %s WHERE id = ?', $this->calendarTableName); |
| 315 | - $stmt = $this->pdo->prepare($query); |
|
| 316 | - $stmt->execute([$calendarId]); |
|
| 315 | + $stmt = $this->pdo->prepare($query); |
|
| 316 | + $stmt->execute([$calendarId]); |
|
| 317 | 317 | |
| 318 | - $query = sprintf('DELETE FROM %s WHERE calendarid = ?', $this->calendarChangesTableName); |
|
| 318 | + $query = sprintf('DELETE FROM %s WHERE calendarid = ?', $this->calendarChangesTableName); |
|
| 319 | 319 | $stmt = $this->pdo->prepare($query); |
| 320 | - $stmt->execute([$calendarId]); |
|
| 321 | - |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - /** |
|
| 325 | - * Returns all calendar objects within a calendar. |
|
| 326 | - * |
|
| 327 | - * Every item contains an array with the following keys: |
|
| 328 | - * * calendardata - The iCalendar-compatible calendar data |
|
| 329 | - * * uri - a unique key which will be used to construct the uri. This can |
|
| 330 | - * be any arbitrary string, but making sure it ends with '.ics' is a |
|
| 331 | - * good idea. This is only the basename, or filename, not the full |
|
| 332 | - * path. |
|
| 333 | - * * lastmodified - a timestamp of the last modification time |
|
| 334 | - * * etag - An arbitrary string, surrounded by double-quotes. (e.g.: |
|
| 335 | - * ' "abcdef"') |
|
| 336 | - * * size - The size of the calendar objects, in bytes. |
|
| 337 | - * * component - optional, a string containing the type of object, such |
|
| 338 | - * as 'vevent' or 'vtodo'. If specified, this will be used to populate |
|
| 339 | - * the Content-Type header. |
|
| 340 | - * |
|
| 341 | - * Note that the etag is optional, but it's highly encouraged to return for |
|
| 342 | - * speed reasons. |
|
| 343 | - * |
|
| 344 | - * The calendardata is also optional. If it's not returned |
|
| 345 | - * 'getCalendarObject' will be called later, which *is* expected to return |
|
| 346 | - * calendardata. |
|
| 347 | - * |
|
| 348 | - * If neither etag or size are specified, the calendardata will be |
|
| 349 | - * used/fetched to determine these numbers. If both are specified the |
|
| 350 | - * amount of times this is needed is reduced by a great degree. |
|
| 351 | - * |
|
| 352 | - * @param string $calendarId |
|
| 353 | - * @return array |
|
| 354 | - */ |
|
| 355 | - public function getCalendarObjects($calendarId) { |
|
| 320 | + $stmt->execute([$calendarId]); |
|
| 321 | + |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + /** |
|
| 325 | + * Returns all calendar objects within a calendar. |
|
| 326 | + * |
|
| 327 | + * Every item contains an array with the following keys: |
|
| 328 | + * * calendardata - The iCalendar-compatible calendar data |
|
| 329 | + * * uri - a unique key which will be used to construct the uri. This can |
|
| 330 | + * be any arbitrary string, but making sure it ends with '.ics' is a |
|
| 331 | + * good idea. This is only the basename, or filename, not the full |
|
| 332 | + * path. |
|
| 333 | + * * lastmodified - a timestamp of the last modification time |
|
| 334 | + * * etag - An arbitrary string, surrounded by double-quotes. (e.g.: |
|
| 335 | + * ' "abcdef"') |
|
| 336 | + * * size - The size of the calendar objects, in bytes. |
|
| 337 | + * * component - optional, a string containing the type of object, such |
|
| 338 | + * as 'vevent' or 'vtodo'. If specified, this will be used to populate |
|
| 339 | + * the Content-Type header. |
|
| 340 | + * |
|
| 341 | + * Note that the etag is optional, but it's highly encouraged to return for |
|
| 342 | + * speed reasons. |
|
| 343 | + * |
|
| 344 | + * The calendardata is also optional. If it's not returned |
|
| 345 | + * 'getCalendarObject' will be called later, which *is* expected to return |
|
| 346 | + * calendardata. |
|
| 347 | + * |
|
| 348 | + * If neither etag or size are specified, the calendardata will be |
|
| 349 | + * used/fetched to determine these numbers. If both are specified the |
|
| 350 | + * amount of times this is needed is reduced by a great degree. |
|
| 351 | + * |
|
| 352 | + * @param string $calendarId |
|
| 353 | + * @return array |
|
| 354 | + */ |
|
| 355 | + public function getCalendarObjects($calendarId) { |
|
| 356 | 356 | |
| 357 | 357 | $query = sprintf('SELECT id, uri, lastmodified, etag, calendarid, size, componenttype FROM %s WHERE calendarid = ?', $this->calendarObjectTableName); |
| 358 | - $stmt = $this->pdo->prepare($query); |
|
| 359 | - $stmt->execute([$calendarId]); |
|
| 360 | - |
|
| 361 | - $result = []; |
|
| 362 | - foreach ($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) { |
|
| 363 | - $result[] = [ |
|
| 364 | - 'id' => $row['id'], |
|
| 365 | - 'uri' => $row['uri'], |
|
| 366 | - 'lastmodified' => $row['lastmodified'], |
|
| 367 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 368 | - 'calendarid' => $row['calendarid'], |
|
| 369 | - 'size' => (int)$row['size'], |
|
| 370 | - 'component' => strtolower($row['componenttype']), |
|
| 371 | - ]; |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - return $result; |
|
| 375 | - |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - /** |
|
| 379 | - * Returns information from a single calendar object, based on it's object |
|
| 380 | - * uri. |
|
| 381 | - * |
|
| 382 | - * The object uri is only the basename, or filename and not a full path. |
|
| 383 | - * |
|
| 384 | - * The returned array must have the same keys as getCalendarObjects. The |
|
| 385 | - * 'calendardata' object is required here though, while it's not required |
|
| 386 | - * for getCalendarObjects. |
|
| 387 | - * |
|
| 388 | - * This method must return null if the object did not exist. |
|
| 389 | - * |
|
| 390 | - * @param string $calendarId |
|
| 391 | - * @param string $objectUri |
|
| 392 | - * @return array|null |
|
| 393 | - */ |
|
| 394 | - public function getCalendarObject($calendarId, $objectUri) { |
|
| 358 | + $stmt = $this->pdo->prepare($query); |
|
| 359 | + $stmt->execute([$calendarId]); |
|
| 360 | + |
|
| 361 | + $result = []; |
|
| 362 | + foreach ($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) { |
|
| 363 | + $result[] = [ |
|
| 364 | + 'id' => $row['id'], |
|
| 365 | + 'uri' => $row['uri'], |
|
| 366 | + 'lastmodified' => $row['lastmodified'], |
|
| 367 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 368 | + 'calendarid' => $row['calendarid'], |
|
| 369 | + 'size' => (int)$row['size'], |
|
| 370 | + 'component' => strtolower($row['componenttype']), |
|
| 371 | + ]; |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + return $result; |
|
| 375 | + |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + /** |
|
| 379 | + * Returns information from a single calendar object, based on it's object |
|
| 380 | + * uri. |
|
| 381 | + * |
|
| 382 | + * The object uri is only the basename, or filename and not a full path. |
|
| 383 | + * |
|
| 384 | + * The returned array must have the same keys as getCalendarObjects. The |
|
| 385 | + * 'calendardata' object is required here though, while it's not required |
|
| 386 | + * for getCalendarObjects. |
|
| 387 | + * |
|
| 388 | + * This method must return null if the object did not exist. |
|
| 389 | + * |
|
| 390 | + * @param string $calendarId |
|
| 391 | + * @param string $objectUri |
|
| 392 | + * @return array|null |
|
| 393 | + */ |
|
| 394 | + public function getCalendarObject($calendarId, $objectUri) { |
|
| 395 | 395 | |
| 396 | 396 | $query = sprintf('SELECT id, uri, lastmodified, etag, calendarid, size, calendardata, componenttype FROM %s WHERE calendarid = ? && uri = ?', $this->calendarObjectTableName); |
| 397 | - $stmt = $this->pdo->prepare($query); |
|
| 398 | - $stmt->execute([$calendarId, $objectUri]); |
|
| 399 | - $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
| 400 | - |
|
| 401 | - if (!$row) return null; |
|
| 402 | - |
|
| 403 | - return [ |
|
| 404 | - 'id' => $row['id'], |
|
| 405 | - 'uri' => $row['uri'], |
|
| 406 | - 'lastmodified' => $row['lastmodified'], |
|
| 407 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 408 | - 'calendarid' => $row['calendarid'], |
|
| 409 | - 'size' => (int)$row['size'], |
|
| 410 | - 'calendardata' => $row['calendardata'], |
|
| 411 | - 'component' => strtolower($row['componenttype']), |
|
| 412 | - ]; |
|
| 413 | - |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - /** |
|
| 417 | - * Returns a list of calendar objects. |
|
| 418 | - * |
|
| 419 | - * This method should work identical to getCalendarObject, but instead |
|
| 420 | - * return all the calendar objects in the list as an array. |
|
| 421 | - * |
|
| 422 | - * If the backend supports this, it may allow for some speed-ups. |
|
| 423 | - * |
|
| 424 | - * @param mixed $calendarId |
|
| 425 | - * @param array $uris |
|
| 426 | - * @return array |
|
| 427 | - */ |
|
| 428 | - public function getMultipleCalendarObjects($calendarId, array $uris) { |
|
| 429 | - |
|
| 430 | - $query =sprintf('SELECT id, uri, lastmodified, etag, calendarid, size, calendardata, componenttype FROM %s WHERE calendarid = ? && uri IN (', $this->calendarObjectTableName); |
|
| 431 | - // Inserting a whole bunch of question marks |
|
| 432 | - $query .= implode(',', array_fill(0, count($uris), '?')); |
|
| 433 | - $query .= ')'; |
|
| 434 | - |
|
| 435 | - $stmt = $this->pdo->prepare($query); |
|
| 436 | - $stmt->execute(array_merge([$calendarId], $uris)); |
|
| 437 | - |
|
| 438 | - $result = []; |
|
| 439 | - while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 440 | - |
|
| 441 | - $result[] = [ |
|
| 442 | - 'id' => $row['id'], |
|
| 443 | - 'uri' => $row['uri'], |
|
| 444 | - 'lastmodified' => $row['lastmodified'], |
|
| 445 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 446 | - 'calendarid' => $row['calendarid'], |
|
| 447 | - 'size' => (int)$row['size'], |
|
| 448 | - 'calendardata' => $row['calendardata'], |
|
| 449 | - 'component' => strtolower($row['componenttype']), |
|
| 450 | - ]; |
|
| 451 | - |
|
| 452 | - } |
|
| 453 | - return $result; |
|
| 454 | - |
|
| 455 | - } |
|
| 456 | - |
|
| 457 | - |
|
| 458 | - /** |
|
| 459 | - * Creates a new calendar object. |
|
| 460 | - * |
|
| 461 | - * The object uri is only the basename, or filename and not a full path. |
|
| 462 | - * |
|
| 463 | - * It is possible return an etag from this function, which will be used in |
|
| 464 | - * the response to this PUT request. Note that the ETag must be surrounded |
|
| 465 | - * by double-quotes. |
|
| 466 | - * |
|
| 467 | - * However, you should only really return this ETag if you don't mangle the |
|
| 468 | - * calendar-data. If the result of a subsequent GET to this object is not |
|
| 469 | - * the exact same as this request body, you should omit the ETag. |
|
| 470 | - * |
|
| 471 | - * @param mixed $calendarId |
|
| 472 | - * @param string $objectUri |
|
| 473 | - * @param string $calendarData |
|
| 474 | - * @return string|null |
|
| 475 | - */ |
|
| 476 | - public function createCalendarObject($calendarId, $objectUri, $calendarData) { |
|
| 477 | - |
|
| 478 | - $extraData = $this->getDenormalizedData($calendarData); |
|
| 479 | - |
|
| 480 | - $stmt = $this->pdo->prepare('INSERT INTO ' . $this->calendarObjectTableName . ' (calendarid, uri, calendardata, lastmodified, etag, size, componenttype, firstoccurence, lastoccurence, uid) VALUES (?,?,?,?,?,?,?,?,?,?)'); |
|
| 481 | - $stmt->execute([ |
|
| 482 | - $calendarId, |
|
| 483 | - $objectUri, |
|
| 484 | - $calendarData, |
|
| 485 | - time(), |
|
| 486 | - $extraData['etag'], |
|
| 487 | - $extraData['size'], |
|
| 488 | - $extraData['componentType'], |
|
| 489 | - $extraData['firstOccurence'], |
|
| 490 | - $extraData['lastOccurence'], |
|
| 491 | - $extraData['uid'], |
|
| 492 | - ]); |
|
| 493 | - $this->addChange($calendarId, $objectUri, 1); |
|
| 494 | - |
|
| 495 | - return '"' . $extraData['etag'] . '"'; |
|
| 496 | - |
|
| 497 | - } |
|
| 498 | - |
|
| 499 | - /** |
|
| 500 | - * Updates an existing calendarobject, based on it's uri. |
|
| 501 | - * |
|
| 502 | - * The object uri is only the basename, or filename and not a full path. |
|
| 503 | - * |
|
| 504 | - * It is possible return an etag from this function, which will be used in |
|
| 505 | - * the response to this PUT request. Note that the ETag must be surrounded |
|
| 506 | - * by double-quotes. |
|
| 507 | - * |
|
| 508 | - * However, you should only really return this ETag if you don't mangle the |
|
| 509 | - * calendar-data. If the result of a subsequent GET to this object is not |
|
| 510 | - * the exact same as this request body, you should omit the ETag. |
|
| 511 | - * |
|
| 512 | - * @param mixed $calendarId |
|
| 513 | - * @param string $objectUri |
|
| 514 | - * @param string $calendarData |
|
| 515 | - * @return string|null |
|
| 516 | - */ |
|
| 517 | - public function updateCalendarObject($calendarId, $objectUri, $calendarData) { |
|
| 518 | - |
|
| 519 | - $extraData = $this->getDenormalizedData($calendarData); |
|
| 397 | + $stmt = $this->pdo->prepare($query); |
|
| 398 | + $stmt->execute([$calendarId, $objectUri]); |
|
| 399 | + $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
| 400 | + |
|
| 401 | + if (!$row) return null; |
|
| 402 | + |
|
| 403 | + return [ |
|
| 404 | + 'id' => $row['id'], |
|
| 405 | + 'uri' => $row['uri'], |
|
| 406 | + 'lastmodified' => $row['lastmodified'], |
|
| 407 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 408 | + 'calendarid' => $row['calendarid'], |
|
| 409 | + 'size' => (int)$row['size'], |
|
| 410 | + 'calendardata' => $row['calendardata'], |
|
| 411 | + 'component' => strtolower($row['componenttype']), |
|
| 412 | + ]; |
|
| 413 | + |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + /** |
|
| 417 | + * Returns a list of calendar objects. |
|
| 418 | + * |
|
| 419 | + * This method should work identical to getCalendarObject, but instead |
|
| 420 | + * return all the calendar objects in the list as an array. |
|
| 421 | + * |
|
| 422 | + * If the backend supports this, it may allow for some speed-ups. |
|
| 423 | + * |
|
| 424 | + * @param mixed $calendarId |
|
| 425 | + * @param array $uris |
|
| 426 | + * @return array |
|
| 427 | + */ |
|
| 428 | + public function getMultipleCalendarObjects($calendarId, array $uris) { |
|
| 429 | + |
|
| 430 | + $query =sprintf('SELECT id, uri, lastmodified, etag, calendarid, size, calendardata, componenttype FROM %s WHERE calendarid = ? && uri IN (', $this->calendarObjectTableName); |
|
| 431 | + // Inserting a whole bunch of question marks |
|
| 432 | + $query .= implode(',', array_fill(0, count($uris), '?')); |
|
| 433 | + $query .= ')'; |
|
| 434 | + |
|
| 435 | + $stmt = $this->pdo->prepare($query); |
|
| 436 | + $stmt->execute(array_merge([$calendarId], $uris)); |
|
| 437 | + |
|
| 438 | + $result = []; |
|
| 439 | + while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 440 | + |
|
| 441 | + $result[] = [ |
|
| 442 | + 'id' => $row['id'], |
|
| 443 | + 'uri' => $row['uri'], |
|
| 444 | + 'lastmodified' => $row['lastmodified'], |
|
| 445 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 446 | + 'calendarid' => $row['calendarid'], |
|
| 447 | + 'size' => (int)$row['size'], |
|
| 448 | + 'calendardata' => $row['calendardata'], |
|
| 449 | + 'component' => strtolower($row['componenttype']), |
|
| 450 | + ]; |
|
| 451 | + |
|
| 452 | + } |
|
| 453 | + return $result; |
|
| 454 | + |
|
| 455 | + } |
|
| 456 | + |
|
| 457 | + |
|
| 458 | + /** |
|
| 459 | + * Creates a new calendar object. |
|
| 460 | + * |
|
| 461 | + * The object uri is only the basename, or filename and not a full path. |
|
| 462 | + * |
|
| 463 | + * It is possible return an etag from this function, which will be used in |
|
| 464 | + * the response to this PUT request. Note that the ETag must be surrounded |
|
| 465 | + * by double-quotes. |
|
| 466 | + * |
|
| 467 | + * However, you should only really return this ETag if you don't mangle the |
|
| 468 | + * calendar-data. If the result of a subsequent GET to this object is not |
|
| 469 | + * the exact same as this request body, you should omit the ETag. |
|
| 470 | + * |
|
| 471 | + * @param mixed $calendarId |
|
| 472 | + * @param string $objectUri |
|
| 473 | + * @param string $calendarData |
|
| 474 | + * @return string|null |
|
| 475 | + */ |
|
| 476 | + public function createCalendarObject($calendarId, $objectUri, $calendarData) { |
|
| 477 | + |
|
| 478 | + $extraData = $this->getDenormalizedData($calendarData); |
|
| 479 | + |
|
| 480 | + $stmt = $this->pdo->prepare('INSERT INTO ' . $this->calendarObjectTableName . ' (calendarid, uri, calendardata, lastmodified, etag, size, componenttype, firstoccurence, lastoccurence, uid) VALUES (?,?,?,?,?,?,?,?,?,?)'); |
|
| 481 | + $stmt->execute([ |
|
| 482 | + $calendarId, |
|
| 483 | + $objectUri, |
|
| 484 | + $calendarData, |
|
| 485 | + time(), |
|
| 486 | + $extraData['etag'], |
|
| 487 | + $extraData['size'], |
|
| 488 | + $extraData['componentType'], |
|
| 489 | + $extraData['firstOccurence'], |
|
| 490 | + $extraData['lastOccurence'], |
|
| 491 | + $extraData['uid'], |
|
| 492 | + ]); |
|
| 493 | + $this->addChange($calendarId, $objectUri, 1); |
|
| 494 | + |
|
| 495 | + return '"' . $extraData['etag'] . '"'; |
|
| 496 | + |
|
| 497 | + } |
|
| 498 | + |
|
| 499 | + /** |
|
| 500 | + * Updates an existing calendarobject, based on it's uri. |
|
| 501 | + * |
|
| 502 | + * The object uri is only the basename, or filename and not a full path. |
|
| 503 | + * |
|
| 504 | + * It is possible return an etag from this function, which will be used in |
|
| 505 | + * the response to this PUT request. Note that the ETag must be surrounded |
|
| 506 | + * by double-quotes. |
|
| 507 | + * |
|
| 508 | + * However, you should only really return this ETag if you don't mangle the |
|
| 509 | + * calendar-data. If the result of a subsequent GET to this object is not |
|
| 510 | + * the exact same as this request body, you should omit the ETag. |
|
| 511 | + * |
|
| 512 | + * @param mixed $calendarId |
|
| 513 | + * @param string $objectUri |
|
| 514 | + * @param string $calendarData |
|
| 515 | + * @return string|null |
|
| 516 | + */ |
|
| 517 | + public function updateCalendarObject($calendarId, $objectUri, $calendarData) { |
|
| 518 | + |
|
| 519 | + $extraData = $this->getDenormalizedData($calendarData); |
|
| 520 | 520 | $query = sprintf('UPDATE %s SET calendardata = ?, lastmodified = ?, etag = ?, size = ?, componenttype = ?, firstoccurence = ?, lastoccurence = ?, uid = ? WHERE calendarid = ? && uri = ?', $this->calendarObjectTableName); |
| 521 | - $stmt = $this->pdo->prepare($query); |
|
| 522 | - $stmt->execute([$calendarData, time(), $extraData['etag'], $extraData['size'], $extraData['componentType'], $extraData['firstOccurence'], $extraData['lastOccurence'], $extraData['uid'], $calendarId, $objectUri]); |
|
| 523 | - |
|
| 524 | - $this->addChange($calendarId, $objectUri, 2); |
|
| 525 | - |
|
| 526 | - return '"' . $extraData['etag'] . '"'; |
|
| 527 | - |
|
| 528 | - } |
|
| 529 | - |
|
| 530 | - /** |
|
| 531 | - * Parses some information from calendar objects, used for optimized |
|
| 532 | - * calendar-queries. |
|
| 533 | - * |
|
| 534 | - * Returns an array with the following keys: |
|
| 535 | - * * etag - An md5 checksum of the object without the quotes. |
|
| 536 | - * * size - Size of the object in bytes |
|
| 537 | - * * componentType - VEVENT, VTODO or VJOURNAL |
|
| 538 | - * * firstOccurence |
|
| 539 | - * * lastOccurence |
|
| 540 | - * * uid - value of the UID property |
|
| 541 | - * |
|
| 542 | - * @param string $calendarData |
|
| 543 | - * @return array |
|
| 544 | - */ |
|
| 545 | - protected function getDenormalizedData($calendarData) { |
|
| 546 | - |
|
| 547 | - $vObject = VObject\Reader::read($calendarData); |
|
| 548 | - $componentType = null; |
|
| 549 | - $component = null; |
|
| 550 | - $firstOccurence = null; |
|
| 551 | - $lastOccurence = null; |
|
| 552 | - $uid = null; |
|
| 553 | - foreach ($vObject->getComponents() as $component) { |
|
| 554 | - if ($component->name !== 'VTIMEZONE') { |
|
| 555 | - $componentType = $component->name; |
|
| 556 | - $uid = (string)$component->UID; |
|
| 557 | - break; |
|
| 558 | - } |
|
| 559 | - } |
|
| 560 | - if (!$componentType) { |
|
| 561 | - throw new \Sabre\DAV\Exception\BadRequest('Calendar objects must have a VJOURNAL, VEVENT or VTODO component'); |
|
| 562 | - } |
|
| 563 | - if ($componentType === 'VEVENT') { |
|
| 564 | - $firstOccurence = $component->DTSTART->getDateTime()->getTimeStamp(); |
|
| 565 | - // Finding the last occurence is a bit harder |
|
| 566 | - if (!isset($component->RRULE)) { |
|
| 567 | - if (isset($component->DTEND)) { |
|
| 568 | - $lastOccurence = $component->DTEND->getDateTime()->getTimeStamp(); |
|
| 569 | - } elseif (isset($component->DURATION)) { |
|
| 570 | - $endDate = clone $component->DTSTART->getDateTime(); |
|
| 571 | - $endDate = $endDate->add(VObject\DateTimeParser::parse($component->DURATION->getValue())); |
|
| 572 | - $lastOccurence = $endDate->getTimeStamp(); |
|
| 573 | - } elseif (!$component->DTSTART->hasTime()) { |
|
| 574 | - $endDate = clone $component->DTSTART->getDateTime(); |
|
| 575 | - $endDate = $endDate->modify('+1 day'); |
|
| 576 | - $lastOccurence = $endDate->getTimeStamp(); |
|
| 577 | - } else { |
|
| 578 | - $lastOccurence = $firstOccurence; |
|
| 579 | - } |
|
| 580 | - } else { |
|
| 581 | - $it = new VObject\Recur\EventIterator($vObject, (string)$component->UID); |
|
| 582 | - $maxDate = new \DateTime(self::MAX_DATE); |
|
| 583 | - if ($it->isInfinite()) { |
|
| 584 | - $lastOccurence = $maxDate->getTimeStamp(); |
|
| 585 | - } else { |
|
| 586 | - $end = $it->getDtEnd(); |
|
| 587 | - while ($it->valid() && $end < $maxDate) { |
|
| 588 | - $end = $it->getDtEnd(); |
|
| 589 | - $it->next(); |
|
| 590 | - |
|
| 591 | - } |
|
| 592 | - $lastOccurence = $end->getTimeStamp(); |
|
| 593 | - } |
|
| 594 | - |
|
| 595 | - } |
|
| 596 | - } |
|
| 597 | - |
|
| 598 | - // Destroy circular references to PHP will GC the object. |
|
| 599 | - $vObject->destroy(); |
|
| 600 | - |
|
| 601 | - return [ |
|
| 602 | - 'etag' => md5($calendarData), |
|
| 603 | - 'size' => strlen($calendarData), |
|
| 604 | - 'componentType' => $componentType, |
|
| 605 | - 'firstOccurence' => $firstOccurence, |
|
| 606 | - 'lastOccurence' => $lastOccurence, |
|
| 607 | - 'uid' => $uid, |
|
| 608 | - ]; |
|
| 609 | - |
|
| 610 | - } |
|
| 611 | - |
|
| 612 | - /** |
|
| 613 | - * Deletes an existing calendar object. |
|
| 614 | - * |
|
| 615 | - * The object uri is only the basename, or filename and not a full path. |
|
| 616 | - * |
|
| 617 | - * @param string $calendarId |
|
| 618 | - * @param string $objectUri |
|
| 619 | - * @return void |
|
| 620 | - */ |
|
| 621 | - public function deleteCalendarObject($calendarId, $objectUri) { |
|
| 521 | + $stmt = $this->pdo->prepare($query); |
|
| 522 | + $stmt->execute([$calendarData, time(), $extraData['etag'], $extraData['size'], $extraData['componentType'], $extraData['firstOccurence'], $extraData['lastOccurence'], $extraData['uid'], $calendarId, $objectUri]); |
|
| 523 | + |
|
| 524 | + $this->addChange($calendarId, $objectUri, 2); |
|
| 525 | + |
|
| 526 | + return '"' . $extraData['etag'] . '"'; |
|
| 527 | + |
|
| 528 | + } |
|
| 529 | + |
|
| 530 | + /** |
|
| 531 | + * Parses some information from calendar objects, used for optimized |
|
| 532 | + * calendar-queries. |
|
| 533 | + * |
|
| 534 | + * Returns an array with the following keys: |
|
| 535 | + * * etag - An md5 checksum of the object without the quotes. |
|
| 536 | + * * size - Size of the object in bytes |
|
| 537 | + * * componentType - VEVENT, VTODO or VJOURNAL |
|
| 538 | + * * firstOccurence |
|
| 539 | + * * lastOccurence |
|
| 540 | + * * uid - value of the UID property |
|
| 541 | + * |
|
| 542 | + * @param string $calendarData |
|
| 543 | + * @return array |
|
| 544 | + */ |
|
| 545 | + protected function getDenormalizedData($calendarData) { |
|
| 546 | + |
|
| 547 | + $vObject = VObject\Reader::read($calendarData); |
|
| 548 | + $componentType = null; |
|
| 549 | + $component = null; |
|
| 550 | + $firstOccurence = null; |
|
| 551 | + $lastOccurence = null; |
|
| 552 | + $uid = null; |
|
| 553 | + foreach ($vObject->getComponents() as $component) { |
|
| 554 | + if ($component->name !== 'VTIMEZONE') { |
|
| 555 | + $componentType = $component->name; |
|
| 556 | + $uid = (string)$component->UID; |
|
| 557 | + break; |
|
| 558 | + } |
|
| 559 | + } |
|
| 560 | + if (!$componentType) { |
|
| 561 | + throw new \Sabre\DAV\Exception\BadRequest('Calendar objects must have a VJOURNAL, VEVENT or VTODO component'); |
|
| 562 | + } |
|
| 563 | + if ($componentType === 'VEVENT') { |
|
| 564 | + $firstOccurence = $component->DTSTART->getDateTime()->getTimeStamp(); |
|
| 565 | + // Finding the last occurence is a bit harder |
|
| 566 | + if (!isset($component->RRULE)) { |
|
| 567 | + if (isset($component->DTEND)) { |
|
| 568 | + $lastOccurence = $component->DTEND->getDateTime()->getTimeStamp(); |
|
| 569 | + } elseif (isset($component->DURATION)) { |
|
| 570 | + $endDate = clone $component->DTSTART->getDateTime(); |
|
| 571 | + $endDate = $endDate->add(VObject\DateTimeParser::parse($component->DURATION->getValue())); |
|
| 572 | + $lastOccurence = $endDate->getTimeStamp(); |
|
| 573 | + } elseif (!$component->DTSTART->hasTime()) { |
|
| 574 | + $endDate = clone $component->DTSTART->getDateTime(); |
|
| 575 | + $endDate = $endDate->modify('+1 day'); |
|
| 576 | + $lastOccurence = $endDate->getTimeStamp(); |
|
| 577 | + } else { |
|
| 578 | + $lastOccurence = $firstOccurence; |
|
| 579 | + } |
|
| 580 | + } else { |
|
| 581 | + $it = new VObject\Recur\EventIterator($vObject, (string)$component->UID); |
|
| 582 | + $maxDate = new \DateTime(self::MAX_DATE); |
|
| 583 | + if ($it->isInfinite()) { |
|
| 584 | + $lastOccurence = $maxDate->getTimeStamp(); |
|
| 585 | + } else { |
|
| 586 | + $end = $it->getDtEnd(); |
|
| 587 | + while ($it->valid() && $end < $maxDate) { |
|
| 588 | + $end = $it->getDtEnd(); |
|
| 589 | + $it->next(); |
|
| 590 | + |
|
| 591 | + } |
|
| 592 | + $lastOccurence = $end->getTimeStamp(); |
|
| 593 | + } |
|
| 594 | + |
|
| 595 | + } |
|
| 596 | + } |
|
| 597 | + |
|
| 598 | + // Destroy circular references to PHP will GC the object. |
|
| 599 | + $vObject->destroy(); |
|
| 600 | + |
|
| 601 | + return [ |
|
| 602 | + 'etag' => md5($calendarData), |
|
| 603 | + 'size' => strlen($calendarData), |
|
| 604 | + 'componentType' => $componentType, |
|
| 605 | + 'firstOccurence' => $firstOccurence, |
|
| 606 | + 'lastOccurence' => $lastOccurence, |
|
| 607 | + 'uid' => $uid, |
|
| 608 | + ]; |
|
| 609 | + |
|
| 610 | + } |
|
| 611 | + |
|
| 612 | + /** |
|
| 613 | + * Deletes an existing calendar object. |
|
| 614 | + * |
|
| 615 | + * The object uri is only the basename, or filename and not a full path. |
|
| 616 | + * |
|
| 617 | + * @param string $calendarId |
|
| 618 | + * @param string $objectUri |
|
| 619 | + * @return void |
|
| 620 | + */ |
|
| 621 | + public function deleteCalendarObject($calendarId, $objectUri) { |
|
| 622 | 622 | |
| 623 | 623 | $query = sprintf('DELETE FROM %s WHERE calendarid = ? && uri = ?', $this->calendarObjectTableName); |
| 624 | - $stmt = $this->pdo->prepare($query); |
|
| 625 | - $stmt->execute([$calendarId, $objectUri]); |
|
| 626 | - |
|
| 627 | - $this->addChange($calendarId, $objectUri, 3); |
|
| 628 | - |
|
| 629 | - } |
|
| 630 | - |
|
| 631 | - /** |
|
| 632 | - * Performs a calendar-query on the contents of this calendar. |
|
| 633 | - * |
|
| 634 | - * The calendar-query is defined in RFC4791 : CalDAV. Using the |
|
| 635 | - * calendar-query it is possible for a client to request a specific set of |
|
| 636 | - * object, based on contents of iCalendar properties, date-ranges and |
|
| 637 | - * iCalendar component types (VTODO, VEVENT). |
|
| 638 | - * |
|
| 639 | - * This method should just return a list of (relative) urls that match this |
|
| 640 | - * query. |
|
| 641 | - * |
|
| 642 | - * The list of filters are specified as an array. The exact array is |
|
| 643 | - * documented by \Sabre\CalDAV\CalendarQueryParser. |
|
| 644 | - * |
|
| 645 | - * Note that it is extremely likely that getCalendarObject for every path |
|
| 646 | - * returned from this method will be called almost immediately after. You |
|
| 647 | - * may want to anticipate this to speed up these requests. |
|
| 648 | - * |
|
| 649 | - * This method provides a default implementation, which parses *all* the |
|
| 650 | - * iCalendar objects in the specified calendar. |
|
| 651 | - * |
|
| 652 | - * This default may well be good enough for personal use, and calendars |
|
| 653 | - * that aren't very large. But if you anticipate high usage, big calendars |
|
| 654 | - * or high loads, you are strongly adviced to optimize certain paths. |
|
| 655 | - * |
|
| 656 | - * The best way to do so is override this method and to optimize |
|
| 657 | - * specifically for 'common filters'. |
|
| 658 | - * |
|
| 659 | - * Requests that are extremely common are: |
|
| 660 | - * * requests for just VEVENTS |
|
| 661 | - * * requests for just VTODO |
|
| 662 | - * * requests with a time-range-filter on a VEVENT. |
|
| 663 | - * |
|
| 664 | - * ..and combinations of these requests. It may not be worth it to try to |
|
| 665 | - * handle every possible situation and just rely on the (relatively |
|
| 666 | - * easy to use) CalendarQueryValidator to handle the rest. |
|
| 667 | - * |
|
| 668 | - * Note that especially time-range-filters may be difficult to parse. A |
|
| 669 | - * time-range filter specified on a VEVENT must for instance also handle |
|
| 670 | - * recurrence rules correctly. |
|
| 671 | - * A good example of how to interprete all these filters can also simply |
|
| 672 | - * be found in \Sabre\CalDAV\CalendarQueryFilter. This class is as correct |
|
| 673 | - * as possible, so it gives you a good idea on what type of stuff you need |
|
| 674 | - * to think of. |
|
| 675 | - * |
|
| 676 | - * This specific implementation (for the PDO) backend optimizes filters on |
|
| 677 | - * specific components, and VEVENT time-ranges. |
|
| 678 | - * |
|
| 679 | - * @param string $calendarId |
|
| 680 | - * @param array $filters |
|
| 681 | - * @return array |
|
| 682 | - */ |
|
| 683 | - public function calendarQuery($calendarId, array $filters) { |
|
| 684 | - |
|
| 685 | - $componentType = null; |
|
| 686 | - $requirePostFilter = true; |
|
| 687 | - $timeRange = null; |
|
| 688 | - |
|
| 689 | - // if no filters were specified, we don't need to filter after a query |
|
| 690 | - if (!$filters['prop-filters'] && !$filters['comp-filters']) { |
|
| 691 | - $requirePostFilter = false; |
|
| 692 | - } |
|
| 693 | - |
|
| 694 | - // Figuring out if there's a component filter |
|
| 695 | - if (count($filters['comp-filters']) > 0 && !$filters['comp-filters'][0]['is-not-defined']) { |
|
| 696 | - $componentType = $filters['comp-filters'][0]['name']; |
|
| 697 | - |
|
| 698 | - // Checking if we need post-filters |
|
| 699 | - if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['time-range'] && !$filters['comp-filters'][0]['prop-filters']) { |
|
| 700 | - $requirePostFilter = false; |
|
| 701 | - } |
|
| 702 | - // There was a time-range filter |
|
| 703 | - if ($componentType == 'VEVENT' && isset($filters['comp-filters'][0]['time-range'])) { |
|
| 704 | - $timeRange = $filters['comp-filters'][0]['time-range']; |
|
| 705 | - |
|
| 706 | - // If start time OR the end time is not specified, we can do a |
|
| 707 | - // 100% accurate mysql query. |
|
| 708 | - if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['prop-filters'] && (!$timeRange['start'] || !$timeRange['end'])) { |
|
| 709 | - $requirePostFilter = false; |
|
| 710 | - } |
|
| 711 | - } |
|
| 712 | - |
|
| 713 | - } |
|
| 714 | - |
|
| 715 | - if ($requirePostFilter) { |
|
| 716 | - $query = sprintf("SELECT uri, calendardata FROM %s WHERE calendarid = :calendarid", $this->calendarObjectTableName); |
|
| 717 | - } else { |
|
| 718 | - $query = sprintf("SELECT uri FROM %s WHERE calendarid = :calendarid", $this->calendarObjectTableName); |
|
| 719 | - } |
|
| 720 | - |
|
| 721 | - $values = [ |
|
| 722 | - 'calendarid' => $calendarId, |
|
| 723 | - ]; |
|
| 724 | - |
|
| 725 | - if ($componentType) { |
|
| 726 | - $query .= " && componenttype = :componenttype"; |
|
| 727 | - $values['componenttype'] = $componentType; |
|
| 728 | - } |
|
| 729 | - |
|
| 730 | - if ($timeRange && $timeRange['start']) { |
|
| 731 | - $query .= " && lastoccurence > :startdate"; |
|
| 732 | - $values['startdate'] = $timeRange['start']->getTimeStamp(); |
|
| 733 | - } |
|
| 734 | - if ($timeRange && $timeRange['end']) { |
|
| 735 | - $query .= " && firstoccurence < :enddate"; |
|
| 736 | - $values['enddate'] = $timeRange['end']->getTimeStamp(); |
|
| 737 | - } |
|
| 738 | - |
|
| 739 | - $stmt = $this->pdo->prepare($query); |
|
| 740 | - $stmt->execute($values); |
|
| 741 | - |
|
| 742 | - $result = []; |
|
| 743 | - while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 744 | - if ($requirePostFilter) { |
|
| 745 | - if (!$this->validateFilterForObject($row, $filters)) { |
|
| 746 | - continue; |
|
| 747 | - } |
|
| 748 | - } |
|
| 749 | - $result[] = $row['uri']; |
|
| 750 | - |
|
| 751 | - } |
|
| 752 | - |
|
| 753 | - return $result; |
|
| 754 | - |
|
| 755 | - } |
|
| 756 | - |
|
| 757 | - /** |
|
| 758 | - * Searches through all of a users calendars and calendar objects to find |
|
| 759 | - * an object with a specific UID. |
|
| 760 | - * |
|
| 761 | - * This method should return the path to this object, relative to the |
|
| 762 | - * calendar home, so this path usually only contains two parts: |
|
| 763 | - * |
|
| 764 | - * calendarpath/objectpath.ics |
|
| 765 | - * |
|
| 766 | - * If the uid is not found, return null. |
|
| 767 | - * |
|
| 768 | - * This method should only consider * objects that the principal owns, so |
|
| 769 | - * any calendars owned by other principals that also appear in this |
|
| 770 | - * collection should be ignored. |
|
| 771 | - * |
|
| 772 | - * @param string $principalUri |
|
| 773 | - * @param string $uid |
|
| 774 | - * @return string|null |
|
| 775 | - */ |
|
| 776 | - public function getCalendarObjectByUID($principalUri, $uid) { |
|
| 777 | - |
|
| 778 | - $query = <<<SQL |
|
| 624 | + $stmt = $this->pdo->prepare($query); |
|
| 625 | + $stmt->execute([$calendarId, $objectUri]); |
|
| 626 | + |
|
| 627 | + $this->addChange($calendarId, $objectUri, 3); |
|
| 628 | + |
|
| 629 | + } |
|
| 630 | + |
|
| 631 | + /** |
|
| 632 | + * Performs a calendar-query on the contents of this calendar. |
|
| 633 | + * |
|
| 634 | + * The calendar-query is defined in RFC4791 : CalDAV. Using the |
|
| 635 | + * calendar-query it is possible for a client to request a specific set of |
|
| 636 | + * object, based on contents of iCalendar properties, date-ranges and |
|
| 637 | + * iCalendar component types (VTODO, VEVENT). |
|
| 638 | + * |
|
| 639 | + * This method should just return a list of (relative) urls that match this |
|
| 640 | + * query. |
|
| 641 | + * |
|
| 642 | + * The list of filters are specified as an array. The exact array is |
|
| 643 | + * documented by \Sabre\CalDAV\CalendarQueryParser. |
|
| 644 | + * |
|
| 645 | + * Note that it is extremely likely that getCalendarObject for every path |
|
| 646 | + * returned from this method will be called almost immediately after. You |
|
| 647 | + * may want to anticipate this to speed up these requests. |
|
| 648 | + * |
|
| 649 | + * This method provides a default implementation, which parses *all* the |
|
| 650 | + * iCalendar objects in the specified calendar. |
|
| 651 | + * |
|
| 652 | + * This default may well be good enough for personal use, and calendars |
|
| 653 | + * that aren't very large. But if you anticipate high usage, big calendars |
|
| 654 | + * or high loads, you are strongly adviced to optimize certain paths. |
|
| 655 | + * |
|
| 656 | + * The best way to do so is override this method and to optimize |
|
| 657 | + * specifically for 'common filters'. |
|
| 658 | + * |
|
| 659 | + * Requests that are extremely common are: |
|
| 660 | + * * requests for just VEVENTS |
|
| 661 | + * * requests for just VTODO |
|
| 662 | + * * requests with a time-range-filter on a VEVENT. |
|
| 663 | + * |
|
| 664 | + * ..and combinations of these requests. It may not be worth it to try to |
|
| 665 | + * handle every possible situation and just rely on the (relatively |
|
| 666 | + * easy to use) CalendarQueryValidator to handle the rest. |
|
| 667 | + * |
|
| 668 | + * Note that especially time-range-filters may be difficult to parse. A |
|
| 669 | + * time-range filter specified on a VEVENT must for instance also handle |
|
| 670 | + * recurrence rules correctly. |
|
| 671 | + * A good example of how to interprete all these filters can also simply |
|
| 672 | + * be found in \Sabre\CalDAV\CalendarQueryFilter. This class is as correct |
|
| 673 | + * as possible, so it gives you a good idea on what type of stuff you need |
|
| 674 | + * to think of. |
|
| 675 | + * |
|
| 676 | + * This specific implementation (for the PDO) backend optimizes filters on |
|
| 677 | + * specific components, and VEVENT time-ranges. |
|
| 678 | + * |
|
| 679 | + * @param string $calendarId |
|
| 680 | + * @param array $filters |
|
| 681 | + * @return array |
|
| 682 | + */ |
|
| 683 | + public function calendarQuery($calendarId, array $filters) { |
|
| 684 | + |
|
| 685 | + $componentType = null; |
|
| 686 | + $requirePostFilter = true; |
|
| 687 | + $timeRange = null; |
|
| 688 | + |
|
| 689 | + // if no filters were specified, we don't need to filter after a query |
|
| 690 | + if (!$filters['prop-filters'] && !$filters['comp-filters']) { |
|
| 691 | + $requirePostFilter = false; |
|
| 692 | + } |
|
| 693 | + |
|
| 694 | + // Figuring out if there's a component filter |
|
| 695 | + if (count($filters['comp-filters']) > 0 && !$filters['comp-filters'][0]['is-not-defined']) { |
|
| 696 | + $componentType = $filters['comp-filters'][0]['name']; |
|
| 697 | + |
|
| 698 | + // Checking if we need post-filters |
|
| 699 | + if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['time-range'] && !$filters['comp-filters'][0]['prop-filters']) { |
|
| 700 | + $requirePostFilter = false; |
|
| 701 | + } |
|
| 702 | + // There was a time-range filter |
|
| 703 | + if ($componentType == 'VEVENT' && isset($filters['comp-filters'][0]['time-range'])) { |
|
| 704 | + $timeRange = $filters['comp-filters'][0]['time-range']; |
|
| 705 | + |
|
| 706 | + // If start time OR the end time is not specified, we can do a |
|
| 707 | + // 100% accurate mysql query. |
|
| 708 | + if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['prop-filters'] && (!$timeRange['start'] || !$timeRange['end'])) { |
|
| 709 | + $requirePostFilter = false; |
|
| 710 | + } |
|
| 711 | + } |
|
| 712 | + |
|
| 713 | + } |
|
| 714 | + |
|
| 715 | + if ($requirePostFilter) { |
|
| 716 | + $query = sprintf("SELECT uri, calendardata FROM %s WHERE calendarid = :calendarid", $this->calendarObjectTableName); |
|
| 717 | + } else { |
|
| 718 | + $query = sprintf("SELECT uri FROM %s WHERE calendarid = :calendarid", $this->calendarObjectTableName); |
|
| 719 | + } |
|
| 720 | + |
|
| 721 | + $values = [ |
|
| 722 | + 'calendarid' => $calendarId, |
|
| 723 | + ]; |
|
| 724 | + |
|
| 725 | + if ($componentType) { |
|
| 726 | + $query .= " && componenttype = :componenttype"; |
|
| 727 | + $values['componenttype'] = $componentType; |
|
| 728 | + } |
|
| 729 | + |
|
| 730 | + if ($timeRange && $timeRange['start']) { |
|
| 731 | + $query .= " && lastoccurence > :startdate"; |
|
| 732 | + $values['startdate'] = $timeRange['start']->getTimeStamp(); |
|
| 733 | + } |
|
| 734 | + if ($timeRange && $timeRange['end']) { |
|
| 735 | + $query .= " && firstoccurence < :enddate"; |
|
| 736 | + $values['enddate'] = $timeRange['end']->getTimeStamp(); |
|
| 737 | + } |
|
| 738 | + |
|
| 739 | + $stmt = $this->pdo->prepare($query); |
|
| 740 | + $stmt->execute($values); |
|
| 741 | + |
|
| 742 | + $result = []; |
|
| 743 | + while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 744 | + if ($requirePostFilter) { |
|
| 745 | + if (!$this->validateFilterForObject($row, $filters)) { |
|
| 746 | + continue; |
|
| 747 | + } |
|
| 748 | + } |
|
| 749 | + $result[] = $row['uri']; |
|
| 750 | + |
|
| 751 | + } |
|
| 752 | + |
|
| 753 | + return $result; |
|
| 754 | + |
|
| 755 | + } |
|
| 756 | + |
|
| 757 | + /** |
|
| 758 | + * Searches through all of a users calendars and calendar objects to find |
|
| 759 | + * an object with a specific UID. |
|
| 760 | + * |
|
| 761 | + * This method should return the path to this object, relative to the |
|
| 762 | + * calendar home, so this path usually only contains two parts: |
|
| 763 | + * |
|
| 764 | + * calendarpath/objectpath.ics |
|
| 765 | + * |
|
| 766 | + * If the uid is not found, return null. |
|
| 767 | + * |
|
| 768 | + * This method should only consider * objects that the principal owns, so |
|
| 769 | + * any calendars owned by other principals that also appear in this |
|
| 770 | + * collection should be ignored. |
|
| 771 | + * |
|
| 772 | + * @param string $principalUri |
|
| 773 | + * @param string $uid |
|
| 774 | + * @return string|null |
|
| 775 | + */ |
|
| 776 | + public function getCalendarObjectByUID($principalUri, $uid) { |
|
| 777 | + |
|
| 778 | + $query = <<<SQL |
|
| 779 | 779 | SELECT |
| 780 | 780 | calendars.uri AS calendaruri, calendarobjects.uri as objecturi |
| 781 | 781 | FROM |
@@ -789,439 +789,439 @@ discard block |
||
| 789 | 789 | calendarobjects.uid = ? |
| 790 | 790 | SQL; |
| 791 | 791 | |
| 792 | - $stmt = $this->pdo->prepare($query); |
|
| 793 | - $stmt->execute([$principalUri, $uid]); |
|
| 794 | - |
|
| 795 | - if ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 796 | - return $row['calendaruri'] . '/' . $row['objecturi']; |
|
| 797 | - } |
|
| 798 | - |
|
| 799 | - } |
|
| 800 | - |
|
| 801 | - /** |
|
| 802 | - * The getChanges method returns all the changes that have happened, since |
|
| 803 | - * the specified syncToken in the specified calendar. |
|
| 804 | - * |
|
| 805 | - * This function should return an array, such as the following: |
|
| 806 | - * |
|
| 807 | - * [ |
|
| 808 | - * 'syncToken' => 'The current synctoken', |
|
| 809 | - * 'added' => [ |
|
| 810 | - * 'new.txt', |
|
| 811 | - * ], |
|
| 812 | - * 'modified' => [ |
|
| 813 | - * 'modified.txt', |
|
| 814 | - * ], |
|
| 815 | - * 'deleted' => [ |
|
| 816 | - * 'foo.php.bak', |
|
| 817 | - * 'old.txt' |
|
| 818 | - * ] |
|
| 819 | - * ]; |
|
| 820 | - * |
|
| 821 | - * The returned syncToken property should reflect the *current* syncToken |
|
| 822 | - * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
|
| 823 | - * property this is needed here too, to ensure the operation is atomic. |
|
| 824 | - * |
|
| 825 | - * If the $syncToken argument is specified as null, this is an initial |
|
| 826 | - * sync, and all members should be reported. |
|
| 827 | - * |
|
| 828 | - * The modified property is an array of nodenames that have changed since |
|
| 829 | - * the last token. |
|
| 830 | - * |
|
| 831 | - * The deleted property is an array with nodenames, that have been deleted |
|
| 832 | - * from collection. |
|
| 833 | - * |
|
| 834 | - * The $syncLevel argument is basically the 'depth' of the report. If it's |
|
| 835 | - * 1, you only have to report changes that happened only directly in |
|
| 836 | - * immediate descendants. If it's 2, it should also include changes from |
|
| 837 | - * the nodes below the child collections. (grandchildren) |
|
| 838 | - * |
|
| 839 | - * The $limit argument allows a client to specify how many results should |
|
| 840 | - * be returned at most. If the limit is not specified, it should be treated |
|
| 841 | - * as infinite. |
|
| 842 | - * |
|
| 843 | - * If the limit (infinite or not) is higher than you're willing to return, |
|
| 844 | - * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
|
| 845 | - * |
|
| 846 | - * If the syncToken is expired (due to data cleanup) or unknown, you must |
|
| 847 | - * return null. |
|
| 848 | - * |
|
| 849 | - * The limit is 'suggestive'. You are free to ignore it. |
|
| 850 | - * |
|
| 851 | - * @param string $calendarId |
|
| 852 | - * @param string $syncToken |
|
| 853 | - * @param int $syncLevel |
|
| 854 | - * @param int $limit |
|
| 855 | - * @return array |
|
| 856 | - */ |
|
| 857 | - public function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null) { |
|
| 858 | - |
|
| 859 | - // Current synctoken |
|
| 792 | + $stmt = $this->pdo->prepare($query); |
|
| 793 | + $stmt->execute([$principalUri, $uid]); |
|
| 794 | + |
|
| 795 | + if ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 796 | + return $row['calendaruri'] . '/' . $row['objecturi']; |
|
| 797 | + } |
|
| 798 | + |
|
| 799 | + } |
|
| 800 | + |
|
| 801 | + /** |
|
| 802 | + * The getChanges method returns all the changes that have happened, since |
|
| 803 | + * the specified syncToken in the specified calendar. |
|
| 804 | + * |
|
| 805 | + * This function should return an array, such as the following: |
|
| 806 | + * |
|
| 807 | + * [ |
|
| 808 | + * 'syncToken' => 'The current synctoken', |
|
| 809 | + * 'added' => [ |
|
| 810 | + * 'new.txt', |
|
| 811 | + * ], |
|
| 812 | + * 'modified' => [ |
|
| 813 | + * 'modified.txt', |
|
| 814 | + * ], |
|
| 815 | + * 'deleted' => [ |
|
| 816 | + * 'foo.php.bak', |
|
| 817 | + * 'old.txt' |
|
| 818 | + * ] |
|
| 819 | + * ]; |
|
| 820 | + * |
|
| 821 | + * The returned syncToken property should reflect the *current* syncToken |
|
| 822 | + * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
|
| 823 | + * property this is needed here too, to ensure the operation is atomic. |
|
| 824 | + * |
|
| 825 | + * If the $syncToken argument is specified as null, this is an initial |
|
| 826 | + * sync, and all members should be reported. |
|
| 827 | + * |
|
| 828 | + * The modified property is an array of nodenames that have changed since |
|
| 829 | + * the last token. |
|
| 830 | + * |
|
| 831 | + * The deleted property is an array with nodenames, that have been deleted |
|
| 832 | + * from collection. |
|
| 833 | + * |
|
| 834 | + * The $syncLevel argument is basically the 'depth' of the report. If it's |
|
| 835 | + * 1, you only have to report changes that happened only directly in |
|
| 836 | + * immediate descendants. If it's 2, it should also include changes from |
|
| 837 | + * the nodes below the child collections. (grandchildren) |
|
| 838 | + * |
|
| 839 | + * The $limit argument allows a client to specify how many results should |
|
| 840 | + * be returned at most. If the limit is not specified, it should be treated |
|
| 841 | + * as infinite. |
|
| 842 | + * |
|
| 843 | + * If the limit (infinite or not) is higher than you're willing to return, |
|
| 844 | + * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
|
| 845 | + * |
|
| 846 | + * If the syncToken is expired (due to data cleanup) or unknown, you must |
|
| 847 | + * return null. |
|
| 848 | + * |
|
| 849 | + * The limit is 'suggestive'. You are free to ignore it. |
|
| 850 | + * |
|
| 851 | + * @param string $calendarId |
|
| 852 | + * @param string $syncToken |
|
| 853 | + * @param int $syncLevel |
|
| 854 | + * @param int $limit |
|
| 855 | + * @return array |
|
| 856 | + */ |
|
| 857 | + public function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null) { |
|
| 858 | + |
|
| 859 | + // Current synctoken |
|
| 860 | 860 | $query = sprintf('SELECT synctoken FROM %s WHERE id = ?', $this->calendarTableName); |
| 861 | - $stmt = $this->pdo->prepare($query); |
|
| 862 | - $stmt->execute([ $calendarId ]); |
|
| 863 | - $currentToken = $stmt->fetchColumn(0); |
|
| 864 | - |
|
| 865 | - if (is_null($currentToken)) return null; |
|
| 866 | - |
|
| 867 | - $result = [ |
|
| 868 | - 'syncToken' => $currentToken, |
|
| 869 | - 'added' => [], |
|
| 870 | - 'modified' => [], |
|
| 871 | - 'deleted' => [], |
|
| 872 | - ]; |
|
| 873 | - |
|
| 874 | - if ($syncToken) { |
|
| 875 | - |
|
| 876 | - $query = sprintf('SELECT uri, operation FROM %s WHERE synctoken >= ? && synctoken < ? && calendarid = ? ORDER BY synctoken', $this->calendarChangesTableName); |
|
| 877 | - if ($limit > 0) $query .= " LIMIT " . (int)$limit; |
|
| 878 | - |
|
| 879 | - // Fetching all changes |
|
| 880 | - $stmt = $this->pdo->prepare($query); |
|
| 881 | - $stmt->execute([$syncToken, $currentToken, $calendarId]); |
|
| 882 | - |
|
| 883 | - $changes = []; |
|
| 884 | - |
|
| 885 | - // This loop ensures that any duplicates are overwritten, only the |
|
| 886 | - // last change on a node is relevant. |
|
| 887 | - while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 888 | - |
|
| 889 | - $changes[$row['uri']] = $row['operation']; |
|
| 890 | - |
|
| 891 | - } |
|
| 892 | - |
|
| 893 | - foreach ($changes as $uri => $operation) { |
|
| 894 | - |
|
| 895 | - switch ($operation) { |
|
| 896 | - case 1 : |
|
| 897 | - $result['added'][] = $uri; |
|
| 898 | - break; |
|
| 899 | - case 2 : |
|
| 900 | - $result['modified'][] = $uri; |
|
| 901 | - break; |
|
| 902 | - case 3 : |
|
| 903 | - $result['deleted'][] = $uri; |
|
| 904 | - break; |
|
| 905 | - } |
|
| 906 | - |
|
| 907 | - } |
|
| 908 | - } else { |
|
| 909 | - // No synctoken supplied, this is the initial sync. |
|
| 910 | - $query = sprintf('SELECT uri FROM %s WHERE calendarid = ?', $this->calendarObjectTableName); |
|
| 911 | - $stmt = $this->pdo->prepare($query); |
|
| 912 | - $stmt->execute([$calendarId]); |
|
| 913 | - |
|
| 914 | - $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
| 915 | - } |
|
| 916 | - return $result; |
|
| 917 | - |
|
| 918 | - } |
|
| 919 | - |
|
| 920 | - /** |
|
| 921 | - * Adds a change record to the calendarchanges table. |
|
| 922 | - * |
|
| 923 | - * @param mixed $calendarId |
|
| 924 | - * @param string $objectUri |
|
| 925 | - * @param int $operation 1 = add, 2 = modify, 3 = delete. |
|
| 926 | - * @return void |
|
| 927 | - */ |
|
| 928 | - protected function addChange($calendarId, $objectUri, $operation) { |
|
| 929 | - |
|
| 930 | - $stmt = $this->pdo->prepare('INSERT INTO ' . $this->calendarChangesTableName . ' (uri, synctoken, calendarid, operation) SELECT ?, synctoken, ?, ? FROM ' . $this->calendarTableName . ' WHERE id = ?'); |
|
| 931 | - $stmt->execute([ |
|
| 932 | - $objectUri, |
|
| 933 | - $calendarId, |
|
| 934 | - $operation, |
|
| 935 | - $calendarId |
|
| 936 | - ]); |
|
| 861 | + $stmt = $this->pdo->prepare($query); |
|
| 862 | + $stmt->execute([ $calendarId ]); |
|
| 863 | + $currentToken = $stmt->fetchColumn(0); |
|
| 864 | + |
|
| 865 | + if (is_null($currentToken)) return null; |
|
| 866 | + |
|
| 867 | + $result = [ |
|
| 868 | + 'syncToken' => $currentToken, |
|
| 869 | + 'added' => [], |
|
| 870 | + 'modified' => [], |
|
| 871 | + 'deleted' => [], |
|
| 872 | + ]; |
|
| 873 | + |
|
| 874 | + if ($syncToken) { |
|
| 875 | + |
|
| 876 | + $query = sprintf('SELECT uri, operation FROM %s WHERE synctoken >= ? && synctoken < ? && calendarid = ? ORDER BY synctoken', $this->calendarChangesTableName); |
|
| 877 | + if ($limit > 0) $query .= " LIMIT " . (int)$limit; |
|
| 878 | + |
|
| 879 | + // Fetching all changes |
|
| 880 | + $stmt = $this->pdo->prepare($query); |
|
| 881 | + $stmt->execute([$syncToken, $currentToken, $calendarId]); |
|
| 882 | + |
|
| 883 | + $changes = []; |
|
| 884 | + |
|
| 885 | + // This loop ensures that any duplicates are overwritten, only the |
|
| 886 | + // last change on a node is relevant. |
|
| 887 | + while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 888 | + |
|
| 889 | + $changes[$row['uri']] = $row['operation']; |
|
| 890 | + |
|
| 891 | + } |
|
| 892 | + |
|
| 893 | + foreach ($changes as $uri => $operation) { |
|
| 894 | + |
|
| 895 | + switch ($operation) { |
|
| 896 | + case 1 : |
|
| 897 | + $result['added'][] = $uri; |
|
| 898 | + break; |
|
| 899 | + case 2 : |
|
| 900 | + $result['modified'][] = $uri; |
|
| 901 | + break; |
|
| 902 | + case 3 : |
|
| 903 | + $result['deleted'][] = $uri; |
|
| 904 | + break; |
|
| 905 | + } |
|
| 906 | + |
|
| 907 | + } |
|
| 908 | + } else { |
|
| 909 | + // No synctoken supplied, this is the initial sync. |
|
| 910 | + $query = sprintf('SELECT uri FROM %s WHERE calendarid = ?', $this->calendarObjectTableName); |
|
| 911 | + $stmt = $this->pdo->prepare($query); |
|
| 912 | + $stmt->execute([$calendarId]); |
|
| 913 | + |
|
| 914 | + $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
| 915 | + } |
|
| 916 | + return $result; |
|
| 917 | + |
|
| 918 | + } |
|
| 919 | + |
|
| 920 | + /** |
|
| 921 | + * Adds a change record to the calendarchanges table. |
|
| 922 | + * |
|
| 923 | + * @param mixed $calendarId |
|
| 924 | + * @param string $objectUri |
|
| 925 | + * @param int $operation 1 = add, 2 = modify, 3 = delete. |
|
| 926 | + * @return void |
|
| 927 | + */ |
|
| 928 | + protected function addChange($calendarId, $objectUri, $operation) { |
|
| 929 | + |
|
| 930 | + $stmt = $this->pdo->prepare('INSERT INTO ' . $this->calendarChangesTableName . ' (uri, synctoken, calendarid, operation) SELECT ?, synctoken, ?, ? FROM ' . $this->calendarTableName . ' WHERE id = ?'); |
|
| 931 | + $stmt->execute([ |
|
| 932 | + $objectUri, |
|
| 933 | + $calendarId, |
|
| 934 | + $operation, |
|
| 935 | + $calendarId |
|
| 936 | + ]); |
|
| 937 | 937 | $query = sprintf('UPDATE %s SET synctoken = synctoken + 1 WHERE id = ?', $this->calendarTableName); |
| 938 | - $stmt = $this->pdo->prepare($query); |
|
| 939 | - $stmt->execute([ |
|
| 940 | - $calendarId |
|
| 941 | - ]); |
|
| 942 | - |
|
| 943 | - } |
|
| 944 | - |
|
| 945 | - /** |
|
| 946 | - * Returns a list of subscriptions for a principal. |
|
| 947 | - * |
|
| 948 | - * Every subscription is an array with the following keys: |
|
| 949 | - * * id, a unique id that will be used by other functions to modify the |
|
| 950 | - * subscription. This can be the same as the uri or a database key. |
|
| 951 | - * * uri. This is just the 'base uri' or 'filename' of the subscription. |
|
| 952 | - * * principaluri. The owner of the subscription. Almost always the same as |
|
| 953 | - * principalUri passed to this method. |
|
| 954 | - * * source. Url to the actual feed |
|
| 955 | - * |
|
| 956 | - * Furthermore, all the subscription info must be returned too: |
|
| 957 | - * |
|
| 958 | - * 1. {DAV:}displayname |
|
| 959 | - * 2. {http://apple.com/ns/ical/}refreshrate |
|
| 960 | - * 3. {http://calendarserver.org/ns/}subscribed-strip-todos (omit if todos |
|
| 961 | - * should not be stripped). |
|
| 962 | - * 4. {http://calendarserver.org/ns/}subscribed-strip-alarms (omit if alarms |
|
| 963 | - * should not be stripped). |
|
| 964 | - * 5. {http://calendarserver.org/ns/}subscribed-strip-attachments (omit if |
|
| 965 | - * attachments should not be stripped). |
|
| 966 | - * 7. {http://apple.com/ns/ical/}calendar-color |
|
| 967 | - * 8. {http://apple.com/ns/ical/}calendar-order |
|
| 968 | - * 9. {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
|
| 969 | - * (should just be an instance of |
|
| 970 | - * Sabre\CalDAV\Property\SupportedCalendarComponentSet, with a bunch of |
|
| 971 | - * default components). |
|
| 972 | - * |
|
| 973 | - * @param string $principalUri |
|
| 974 | - * @return array |
|
| 975 | - */ |
|
| 976 | - public function getSubscriptionsForUser($principalUri) { |
|
| 977 | - |
|
| 978 | - $fields = array_values($this->subscriptionPropertyMap); |
|
| 979 | - $fields[] = 'id'; |
|
| 980 | - $fields[] = 'uri'; |
|
| 981 | - $fields[] = 'source'; |
|
| 982 | - $fields[] = 'principaluri'; |
|
| 983 | - $fields[] = 'lastmodified'; |
|
| 984 | - |
|
| 985 | - // Making fields a comma-delimited list |
|
| 986 | - $fields = implode(', ', $fields); |
|
| 938 | + $stmt = $this->pdo->prepare($query); |
|
| 939 | + $stmt->execute([ |
|
| 940 | + $calendarId |
|
| 941 | + ]); |
|
| 942 | + |
|
| 943 | + } |
|
| 944 | + |
|
| 945 | + /** |
|
| 946 | + * Returns a list of subscriptions for a principal. |
|
| 947 | + * |
|
| 948 | + * Every subscription is an array with the following keys: |
|
| 949 | + * * id, a unique id that will be used by other functions to modify the |
|
| 950 | + * subscription. This can be the same as the uri or a database key. |
|
| 951 | + * * uri. This is just the 'base uri' or 'filename' of the subscription. |
|
| 952 | + * * principaluri. The owner of the subscription. Almost always the same as |
|
| 953 | + * principalUri passed to this method. |
|
| 954 | + * * source. Url to the actual feed |
|
| 955 | + * |
|
| 956 | + * Furthermore, all the subscription info must be returned too: |
|
| 957 | + * |
|
| 958 | + * 1. {DAV:}displayname |
|
| 959 | + * 2. {http://apple.com/ns/ical/}refreshrate |
|
| 960 | + * 3. {http://calendarserver.org/ns/}subscribed-strip-todos (omit if todos |
|
| 961 | + * should not be stripped). |
|
| 962 | + * 4. {http://calendarserver.org/ns/}subscribed-strip-alarms (omit if alarms |
|
| 963 | + * should not be stripped). |
|
| 964 | + * 5. {http://calendarserver.org/ns/}subscribed-strip-attachments (omit if |
|
| 965 | + * attachments should not be stripped). |
|
| 966 | + * 7. {http://apple.com/ns/ical/}calendar-color |
|
| 967 | + * 8. {http://apple.com/ns/ical/}calendar-order |
|
| 968 | + * 9. {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
|
| 969 | + * (should just be an instance of |
|
| 970 | + * Sabre\CalDAV\Property\SupportedCalendarComponentSet, with a bunch of |
|
| 971 | + * default components). |
|
| 972 | + * |
|
| 973 | + * @param string $principalUri |
|
| 974 | + * @return array |
|
| 975 | + */ |
|
| 976 | + public function getSubscriptionsForUser($principalUri) { |
|
| 977 | + |
|
| 978 | + $fields = array_values($this->subscriptionPropertyMap); |
|
| 979 | + $fields[] = 'id'; |
|
| 980 | + $fields[] = 'uri'; |
|
| 981 | + $fields[] = 'source'; |
|
| 982 | + $fields[] = 'principaluri'; |
|
| 983 | + $fields[] = 'lastmodified'; |
|
| 984 | + |
|
| 985 | + // Making fields a comma-delimited list |
|
| 986 | + $fields = implode(', ', $fields); |
|
| 987 | 987 | $query = sprintf('SELECT %s FROM %s WHERE principaluri = ? ORDER BY calendarorder ASC', $fields, $this->calendarSubscriptionsTableName); |
| 988 | - $stmt = $this->pdo->prepare($query); |
|
| 989 | - $stmt->execute([$principalUri]); |
|
| 990 | - |
|
| 991 | - $subscriptions = []; |
|
| 992 | - while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 993 | - |
|
| 994 | - $subscription = [ |
|
| 995 | - 'id' => $row['id'], |
|
| 996 | - 'uri' => $row['uri'], |
|
| 997 | - 'principaluri' => $row['principaluri'], |
|
| 998 | - 'source' => $row['source'], |
|
| 999 | - 'lastmodified' => $row['lastmodified'], |
|
| 1000 | - |
|
| 1001 | - '{' . CalDAV\Plugin::NS_CALDAV . '}supported-calendar-component-set' => new CalDAV\Xml\Property\SupportedCalendarComponentSet(['VTODO', 'VEVENT']), |
|
| 1002 | - ]; |
|
| 1003 | - |
|
| 1004 | - foreach ($this->subscriptionPropertyMap as $xmlName => $dbName) { |
|
| 1005 | - if (!is_null($row[$dbName])) { |
|
| 1006 | - $subscription[$xmlName] = $row[$dbName]; |
|
| 1007 | - } |
|
| 1008 | - } |
|
| 1009 | - |
|
| 1010 | - $subscriptions[] = $subscription; |
|
| 1011 | - |
|
| 1012 | - } |
|
| 1013 | - |
|
| 1014 | - return $subscriptions; |
|
| 1015 | - |
|
| 1016 | - } |
|
| 1017 | - |
|
| 1018 | - /** |
|
| 1019 | - * Creates a new subscription for a principal. |
|
| 1020 | - * |
|
| 1021 | - * If the creation was a success, an id must be returned that can be used to reference |
|
| 1022 | - * this subscription in other methods, such as updateSubscription. |
|
| 1023 | - * |
|
| 1024 | - * @param string $principalUri |
|
| 1025 | - * @param string $uri |
|
| 1026 | - * @param array $properties |
|
| 1027 | - * @return mixed |
|
| 1028 | - */ |
|
| 1029 | - public function createSubscription($principalUri, $uri, array $properties) { |
|
| 1030 | - |
|
| 1031 | - $fieldNames = [ |
|
| 1032 | - 'principaluri', |
|
| 1033 | - 'uri', |
|
| 1034 | - 'source', |
|
| 1035 | - 'lastmodified', |
|
| 1036 | - ]; |
|
| 1037 | - |
|
| 1038 | - if (!isset($properties['{http://calendarserver.org/ns/}source'])) { |
|
| 1039 | - throw new Forbidden('The {http://calendarserver.org/ns/}source property is required when creating subscriptions'); |
|
| 1040 | - } |
|
| 1041 | - |
|
| 1042 | - $values = [ |
|
| 1043 | - ':principaluri' => $principalUri, |
|
| 1044 | - ':uri' => $uri, |
|
| 1045 | - ':source' => $properties['{http://calendarserver.org/ns/}source']->getHref(), |
|
| 1046 | - ':lastmodified' => time(), |
|
| 1047 | - ]; |
|
| 1048 | - |
|
| 1049 | - foreach ($this->subscriptionPropertyMap as $xmlName => $dbName) { |
|
| 1050 | - if (isset($properties[$xmlName])) { |
|
| 1051 | - |
|
| 1052 | - $values[':' . $dbName] = $properties[$xmlName]; |
|
| 1053 | - $fieldNames[] = $dbName; |
|
| 1054 | - } |
|
| 1055 | - } |
|
| 1056 | - |
|
| 1057 | - $stmt = $this->pdo->prepare("INSERT INTO " . $this->calendarSubscriptionsTableName . " (" . implode(', ', $fieldNames) . ") VALUES (" . implode(', ', array_keys($values)) . ")"); |
|
| 1058 | - $stmt->execute($values); |
|
| 1059 | - |
|
| 1060 | - return $this->pdo->lastInsertId(); |
|
| 1061 | - |
|
| 1062 | - } |
|
| 1063 | - |
|
| 1064 | - /** |
|
| 1065 | - * Updates a subscription |
|
| 1066 | - * |
|
| 1067 | - * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
| 1068 | - * To do the actual updates, you must tell this object which properties |
|
| 1069 | - * you're going to process with the handle() method. |
|
| 1070 | - * |
|
| 1071 | - * Calling the handle method is like telling the PropPatch object "I |
|
| 1072 | - * promise I can handle updating this property". |
|
| 1073 | - * |
|
| 1074 | - * Read the PropPatch documenation for more info and examples. |
|
| 1075 | - * |
|
| 1076 | - * @param mixed $subscriptionId |
|
| 1077 | - * @param \Sabre\DAV\PropPatch $propPatch |
|
| 1078 | - * @return void |
|
| 1079 | - */ |
|
| 1080 | - public function updateSubscription($subscriptionId, DAV\PropPatch $propPatch) { |
|
| 1081 | - |
|
| 1082 | - $supportedProperties = array_keys($this->subscriptionPropertyMap); |
|
| 1083 | - $supportedProperties[] = '{http://calendarserver.org/ns/}source'; |
|
| 1084 | - |
|
| 1085 | - $propPatch->handle($supportedProperties, function($mutations) use ($subscriptionId) { |
|
| 1086 | - |
|
| 1087 | - $newValues = []; |
|
| 1088 | - |
|
| 1089 | - foreach ($mutations as $propertyName => $propertyValue) { |
|
| 1090 | - |
|
| 1091 | - if ($propertyName === '{http://calendarserver.org/ns/}source') { |
|
| 1092 | - $newValues['source'] = $propertyValue->getHref(); |
|
| 1093 | - } else { |
|
| 1094 | - $fieldName = $this->subscriptionPropertyMap[$propertyName]; |
|
| 1095 | - $newValues[$fieldName] = $propertyValue; |
|
| 1096 | - } |
|
| 1097 | - |
|
| 1098 | - } |
|
| 1099 | - |
|
| 1100 | - // Now we're generating the sql query. |
|
| 1101 | - $valuesSql = []; |
|
| 1102 | - foreach ($newValues as $fieldName => $value) { |
|
| 1103 | - $valuesSql[] = $fieldName . ' = ?'; |
|
| 1104 | - } |
|
| 988 | + $stmt = $this->pdo->prepare($query); |
|
| 989 | + $stmt->execute([$principalUri]); |
|
| 990 | + |
|
| 991 | + $subscriptions = []; |
|
| 992 | + while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 993 | + |
|
| 994 | + $subscription = [ |
|
| 995 | + 'id' => $row['id'], |
|
| 996 | + 'uri' => $row['uri'], |
|
| 997 | + 'principaluri' => $row['principaluri'], |
|
| 998 | + 'source' => $row['source'], |
|
| 999 | + 'lastmodified' => $row['lastmodified'], |
|
| 1000 | + |
|
| 1001 | + '{' . CalDAV\Plugin::NS_CALDAV . '}supported-calendar-component-set' => new CalDAV\Xml\Property\SupportedCalendarComponentSet(['VTODO', 'VEVENT']), |
|
| 1002 | + ]; |
|
| 1003 | + |
|
| 1004 | + foreach ($this->subscriptionPropertyMap as $xmlName => $dbName) { |
|
| 1005 | + if (!is_null($row[$dbName])) { |
|
| 1006 | + $subscription[$xmlName] = $row[$dbName]; |
|
| 1007 | + } |
|
| 1008 | + } |
|
| 1009 | + |
|
| 1010 | + $subscriptions[] = $subscription; |
|
| 1011 | + |
|
| 1012 | + } |
|
| 1013 | + |
|
| 1014 | + return $subscriptions; |
|
| 1015 | + |
|
| 1016 | + } |
|
| 1017 | + |
|
| 1018 | + /** |
|
| 1019 | + * Creates a new subscription for a principal. |
|
| 1020 | + * |
|
| 1021 | + * If the creation was a success, an id must be returned that can be used to reference |
|
| 1022 | + * this subscription in other methods, such as updateSubscription. |
|
| 1023 | + * |
|
| 1024 | + * @param string $principalUri |
|
| 1025 | + * @param string $uri |
|
| 1026 | + * @param array $properties |
|
| 1027 | + * @return mixed |
|
| 1028 | + */ |
|
| 1029 | + public function createSubscription($principalUri, $uri, array $properties) { |
|
| 1030 | + |
|
| 1031 | + $fieldNames = [ |
|
| 1032 | + 'principaluri', |
|
| 1033 | + 'uri', |
|
| 1034 | + 'source', |
|
| 1035 | + 'lastmodified', |
|
| 1036 | + ]; |
|
| 1037 | + |
|
| 1038 | + if (!isset($properties['{http://calendarserver.org/ns/}source'])) { |
|
| 1039 | + throw new Forbidden('The {http://calendarserver.org/ns/}source property is required when creating subscriptions'); |
|
| 1040 | + } |
|
| 1041 | + |
|
| 1042 | + $values = [ |
|
| 1043 | + ':principaluri' => $principalUri, |
|
| 1044 | + ':uri' => $uri, |
|
| 1045 | + ':source' => $properties['{http://calendarserver.org/ns/}source']->getHref(), |
|
| 1046 | + ':lastmodified' => time(), |
|
| 1047 | + ]; |
|
| 1048 | + |
|
| 1049 | + foreach ($this->subscriptionPropertyMap as $xmlName => $dbName) { |
|
| 1050 | + if (isset($properties[$xmlName])) { |
|
| 1051 | + |
|
| 1052 | + $values[':' . $dbName] = $properties[$xmlName]; |
|
| 1053 | + $fieldNames[] = $dbName; |
|
| 1054 | + } |
|
| 1055 | + } |
|
| 1056 | + |
|
| 1057 | + $stmt = $this->pdo->prepare("INSERT INTO " . $this->calendarSubscriptionsTableName . " (" . implode(', ', $fieldNames) . ") VALUES (" . implode(', ', array_keys($values)) . ")"); |
|
| 1058 | + $stmt->execute($values); |
|
| 1059 | + |
|
| 1060 | + return $this->pdo->lastInsertId(); |
|
| 1061 | + |
|
| 1062 | + } |
|
| 1063 | + |
|
| 1064 | + /** |
|
| 1065 | + * Updates a subscription |
|
| 1066 | + * |
|
| 1067 | + * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
| 1068 | + * To do the actual updates, you must tell this object which properties |
|
| 1069 | + * you're going to process with the handle() method. |
|
| 1070 | + * |
|
| 1071 | + * Calling the handle method is like telling the PropPatch object "I |
|
| 1072 | + * promise I can handle updating this property". |
|
| 1073 | + * |
|
| 1074 | + * Read the PropPatch documenation for more info and examples. |
|
| 1075 | + * |
|
| 1076 | + * @param mixed $subscriptionId |
|
| 1077 | + * @param \Sabre\DAV\PropPatch $propPatch |
|
| 1078 | + * @return void |
|
| 1079 | + */ |
|
| 1080 | + public function updateSubscription($subscriptionId, DAV\PropPatch $propPatch) { |
|
| 1081 | + |
|
| 1082 | + $supportedProperties = array_keys($this->subscriptionPropertyMap); |
|
| 1083 | + $supportedProperties[] = '{http://calendarserver.org/ns/}source'; |
|
| 1084 | + |
|
| 1085 | + $propPatch->handle($supportedProperties, function($mutations) use ($subscriptionId) { |
|
| 1086 | + |
|
| 1087 | + $newValues = []; |
|
| 1088 | + |
|
| 1089 | + foreach ($mutations as $propertyName => $propertyValue) { |
|
| 1090 | + |
|
| 1091 | + if ($propertyName === '{http://calendarserver.org/ns/}source') { |
|
| 1092 | + $newValues['source'] = $propertyValue->getHref(); |
|
| 1093 | + } else { |
|
| 1094 | + $fieldName = $this->subscriptionPropertyMap[$propertyName]; |
|
| 1095 | + $newValues[$fieldName] = $propertyValue; |
|
| 1096 | + } |
|
| 1097 | + |
|
| 1098 | + } |
|
| 1099 | + |
|
| 1100 | + // Now we're generating the sql query. |
|
| 1101 | + $valuesSql = []; |
|
| 1102 | + foreach ($newValues as $fieldName => $value) { |
|
| 1103 | + $valuesSql[] = $fieldName . ' = ?'; |
|
| 1104 | + } |
|
| 1105 | 1105 | $query = sprintf('UPDATE %s SET %s , lastmodified = ? WHERE id = ?', $this->calendarSubscriptionsTableName, implode(', ', $valuesSql)); |
| 1106 | - $stmt = $this->pdo->prepare($query); |
|
| 1107 | - $newValues['lastmodified'] = time(); |
|
| 1108 | - $newValues['id'] = $subscriptionId; |
|
| 1109 | - $stmt->execute(array_values($newValues)); |
|
| 1106 | + $stmt = $this->pdo->prepare($query); |
|
| 1107 | + $newValues['lastmodified'] = time(); |
|
| 1108 | + $newValues['id'] = $subscriptionId; |
|
| 1109 | + $stmt->execute(array_values($newValues)); |
|
| 1110 | 1110 | |
| 1111 | - return true; |
|
| 1111 | + return true; |
|
| 1112 | 1112 | |
| 1113 | - }); |
|
| 1113 | + }); |
|
| 1114 | 1114 | |
| 1115 | - } |
|
| 1115 | + } |
|
| 1116 | 1116 | |
| 1117 | - /** |
|
| 1118 | - * Deletes a subscription |
|
| 1119 | - * |
|
| 1120 | - * @param mixed $subscriptionId |
|
| 1121 | - * @return void |
|
| 1122 | - */ |
|
| 1123 | - public function deleteSubscription($subscriptionId) { |
|
| 1117 | + /** |
|
| 1118 | + * Deletes a subscription |
|
| 1119 | + * |
|
| 1120 | + * @param mixed $subscriptionId |
|
| 1121 | + * @return void |
|
| 1122 | + */ |
|
| 1123 | + public function deleteSubscription($subscriptionId) { |
|
| 1124 | 1124 | $query = sprintf('DELETE FROM %s WHERE id = ?', $this->calendarSubscriptionsTableName); |
| 1125 | - $stmt = $this->pdo->prepare($query); |
|
| 1126 | - $stmt->execute([$subscriptionId]); |
|
| 1127 | - |
|
| 1128 | - } |
|
| 1129 | - |
|
| 1130 | - /** |
|
| 1131 | - * Returns a single scheduling object. |
|
| 1132 | - * |
|
| 1133 | - * The returned array should contain the following elements: |
|
| 1134 | - * * uri - A unique basename for the object. This will be used to |
|
| 1135 | - * construct a full uri. |
|
| 1136 | - * * calendardata - The iCalendar object |
|
| 1137 | - * * lastmodified - The last modification date. Can be an int for a unix |
|
| 1138 | - * timestamp, or a PHP DateTime object. |
|
| 1139 | - * * etag - A unique token that must change if the object changed. |
|
| 1140 | - * * size - The size of the object, in bytes. |
|
| 1141 | - * |
|
| 1142 | - * @param string $principalUri |
|
| 1143 | - * @param string $objectUri |
|
| 1144 | - * @return array |
|
| 1145 | - */ |
|
| 1146 | - public function getSchedulingObject($principalUri, $objectUri) { |
|
| 1125 | + $stmt = $this->pdo->prepare($query); |
|
| 1126 | + $stmt->execute([$subscriptionId]); |
|
| 1127 | + |
|
| 1128 | + } |
|
| 1129 | + |
|
| 1130 | + /** |
|
| 1131 | + * Returns a single scheduling object. |
|
| 1132 | + * |
|
| 1133 | + * The returned array should contain the following elements: |
|
| 1134 | + * * uri - A unique basename for the object. This will be used to |
|
| 1135 | + * construct a full uri. |
|
| 1136 | + * * calendardata - The iCalendar object |
|
| 1137 | + * * lastmodified - The last modification date. Can be an int for a unix |
|
| 1138 | + * timestamp, or a PHP DateTime object. |
|
| 1139 | + * * etag - A unique token that must change if the object changed. |
|
| 1140 | + * * size - The size of the object, in bytes. |
|
| 1141 | + * |
|
| 1142 | + * @param string $principalUri |
|
| 1143 | + * @param string $objectUri |
|
| 1144 | + * @return array |
|
| 1145 | + */ |
|
| 1146 | + public function getSchedulingObject($principalUri, $objectUri) { |
|
| 1147 | 1147 | |
| 1148 | 1148 | $query = sprintf('SELECT uri, calendardata, lastmodified, etag, size FROM %s WHERE principaluri = ? && uri = ?', $this->schedulingObjectTableName); |
| 1149 | - $stmt = $this->pdo->prepare($query); |
|
| 1150 | - $stmt->execute([$principalUri, $objectUri]); |
|
| 1151 | - $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
| 1152 | - |
|
| 1153 | - if (!$row) return null; |
|
| 1154 | - |
|
| 1155 | - return [ |
|
| 1156 | - 'uri' => $row['uri'], |
|
| 1157 | - 'calendardata' => $row['calendardata'], |
|
| 1158 | - 'lastmodified' => $row['lastmodified'], |
|
| 1159 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 1160 | - 'size' => (int)$row['size'], |
|
| 1161 | - ]; |
|
| 1162 | - |
|
| 1163 | - } |
|
| 1164 | - |
|
| 1165 | - /** |
|
| 1166 | - * Returns all scheduling objects for the inbox collection. |
|
| 1167 | - * |
|
| 1168 | - * These objects should be returned as an array. Every item in the array |
|
| 1169 | - * should follow the same structure as returned from getSchedulingObject. |
|
| 1170 | - * |
|
| 1171 | - * The main difference is that 'calendardata' is optional. |
|
| 1172 | - * |
|
| 1173 | - * @param string $principalUri |
|
| 1174 | - * @return array |
|
| 1175 | - */ |
|
| 1176 | - public function getSchedulingObjects($principalUri) { |
|
| 1149 | + $stmt = $this->pdo->prepare($query); |
|
| 1150 | + $stmt->execute([$principalUri, $objectUri]); |
|
| 1151 | + $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
| 1152 | + |
|
| 1153 | + if (!$row) return null; |
|
| 1154 | + |
|
| 1155 | + return [ |
|
| 1156 | + 'uri' => $row['uri'], |
|
| 1157 | + 'calendardata' => $row['calendardata'], |
|
| 1158 | + 'lastmodified' => $row['lastmodified'], |
|
| 1159 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 1160 | + 'size' => (int)$row['size'], |
|
| 1161 | + ]; |
|
| 1162 | + |
|
| 1163 | + } |
|
| 1164 | + |
|
| 1165 | + /** |
|
| 1166 | + * Returns all scheduling objects for the inbox collection. |
|
| 1167 | + * |
|
| 1168 | + * These objects should be returned as an array. Every item in the array |
|
| 1169 | + * should follow the same structure as returned from getSchedulingObject. |
|
| 1170 | + * |
|
| 1171 | + * The main difference is that 'calendardata' is optional. |
|
| 1172 | + * |
|
| 1173 | + * @param string $principalUri |
|
| 1174 | + * @return array |
|
| 1175 | + */ |
|
| 1176 | + public function getSchedulingObjects($principalUri) { |
|
| 1177 | 1177 | |
| 1178 | 1178 | $query = sprintf('SELECT id, calendardata, uri, lastmodified, etag, size FROM %s WHERE principaluri = ?', $this->schedulingObjectTableName); |
| 1179 | - $stmt = $this->pdo->prepare($query); |
|
| 1180 | - $stmt->execute([$principalUri]); |
|
| 1181 | - |
|
| 1182 | - $result = []; |
|
| 1183 | - foreach ($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) { |
|
| 1184 | - $result[] = [ |
|
| 1185 | - 'calendardata' => $row['calendardata'], |
|
| 1186 | - 'uri' => $row['uri'], |
|
| 1187 | - 'lastmodified' => $row['lastmodified'], |
|
| 1188 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 1189 | - 'size' => (int)$row['size'], |
|
| 1190 | - ]; |
|
| 1191 | - } |
|
| 1192 | - |
|
| 1193 | - return $result; |
|
| 1194 | - |
|
| 1195 | - } |
|
| 1196 | - |
|
| 1197 | - /** |
|
| 1198 | - * Deletes a scheduling object |
|
| 1199 | - * |
|
| 1200 | - * @param string $principalUri |
|
| 1201 | - * @param string $objectUri |
|
| 1202 | - * @return void |
|
| 1203 | - */ |
|
| 1204 | - public function deleteSchedulingObject($principalUri, $objectUri) { |
|
| 1179 | + $stmt = $this->pdo->prepare($query); |
|
| 1180 | + $stmt->execute([$principalUri]); |
|
| 1181 | + |
|
| 1182 | + $result = []; |
|
| 1183 | + foreach ($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) { |
|
| 1184 | + $result[] = [ |
|
| 1185 | + 'calendardata' => $row['calendardata'], |
|
| 1186 | + 'uri' => $row['uri'], |
|
| 1187 | + 'lastmodified' => $row['lastmodified'], |
|
| 1188 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 1189 | + 'size' => (int)$row['size'], |
|
| 1190 | + ]; |
|
| 1191 | + } |
|
| 1192 | + |
|
| 1193 | + return $result; |
|
| 1194 | + |
|
| 1195 | + } |
|
| 1196 | + |
|
| 1197 | + /** |
|
| 1198 | + * Deletes a scheduling object |
|
| 1199 | + * |
|
| 1200 | + * @param string $principalUri |
|
| 1201 | + * @param string $objectUri |
|
| 1202 | + * @return void |
|
| 1203 | + */ |
|
| 1204 | + public function deleteSchedulingObject($principalUri, $objectUri) { |
|
| 1205 | 1205 | |
| 1206 | 1206 | $query = sprintf('DELETE FROM %s WHERE principaluri = ? && uri = ?', $this->schedulingObjectTableName); |
| 1207 | - $stmt = $this->pdo->prepare($query); |
|
| 1208 | - $stmt->execute([$principalUri, $objectUri]); |
|
| 1207 | + $stmt = $this->pdo->prepare($query); |
|
| 1208 | + $stmt->execute([$principalUri, $objectUri]); |
|
| 1209 | 1209 | |
| 1210 | - } |
|
| 1210 | + } |
|
| 1211 | 1211 | |
| 1212 | - /** |
|
| 1213 | - * Creates a new scheduling object. This should land in a users' inbox. |
|
| 1214 | - * |
|
| 1215 | - * @param string $principalUri |
|
| 1216 | - * @param string $objectUri |
|
| 1217 | - * @param string $objectData |
|
| 1218 | - * @return void |
|
| 1219 | - */ |
|
| 1220 | - public function createSchedulingObject($principalUri, $objectUri, $objectData) { |
|
| 1212 | + /** |
|
| 1213 | + * Creates a new scheduling object. This should land in a users' inbox. |
|
| 1214 | + * |
|
| 1215 | + * @param string $principalUri |
|
| 1216 | + * @param string $objectUri |
|
| 1217 | + * @param string $objectData |
|
| 1218 | + * @return void |
|
| 1219 | + */ |
|
| 1220 | + public function createSchedulingObject($principalUri, $objectUri, $objectData) { |
|
| 1221 | 1221 | |
| 1222 | - $stmt = $this->pdo->prepare('INSERT INTO ' . $this->schedulingObjectTableName . ' (principaluri, calendardata, uri, lastmodified, etag, size) VALUES (?, ?, ?, ?, ?, ?)'); |
|
| 1223 | - $stmt->execute([$principalUri, $objectData, $objectUri, time(), md5($objectData), strlen($objectData) ]); |
|
| 1222 | + $stmt = $this->pdo->prepare('INSERT INTO ' . $this->schedulingObjectTableName . ' (principaluri, calendardata, uri, lastmodified, etag, size) VALUES (?, ?, ?, ?, ?, ?)'); |
|
| 1223 | + $stmt->execute([$principalUri, $objectData, $objectUri, time(), md5($objectData), strlen($objectData) ]); |
|
| 1224 | 1224 | |
| 1225 | - } |
|
| 1225 | + } |
|
| 1226 | 1226 | |
| 1227 | 1227 | } |
@@ -366,7 +366,7 @@ discard block |
||
| 366 | 366 | 'lastmodified' => $row['lastmodified'], |
| 367 | 367 | 'etag' => '"' . $row['etag'] . '"', |
| 368 | 368 | 'calendarid' => $row['calendarid'], |
| 369 | - 'size' => (int)$row['size'], |
|
| 369 | + 'size' => (int) $row['size'], |
|
| 370 | 370 | 'component' => strtolower($row['componenttype']), |
| 371 | 371 | ]; |
| 372 | 372 | } |
@@ -406,7 +406,7 @@ discard block |
||
| 406 | 406 | 'lastmodified' => $row['lastmodified'], |
| 407 | 407 | 'etag' => '"' . $row['etag'] . '"', |
| 408 | 408 | 'calendarid' => $row['calendarid'], |
| 409 | - 'size' => (int)$row['size'], |
|
| 409 | + 'size' => (int) $row['size'], |
|
| 410 | 410 | 'calendardata' => $row['calendardata'], |
| 411 | 411 | 'component' => strtolower($row['componenttype']), |
| 412 | 412 | ]; |
@@ -427,7 +427,7 @@ discard block |
||
| 427 | 427 | */ |
| 428 | 428 | public function getMultipleCalendarObjects($calendarId, array $uris) { |
| 429 | 429 | |
| 430 | - $query =sprintf('SELECT id, uri, lastmodified, etag, calendarid, size, calendardata, componenttype FROM %s WHERE calendarid = ? && uri IN (', $this->calendarObjectTableName); |
|
| 430 | + $query = sprintf('SELECT id, uri, lastmodified, etag, calendarid, size, calendardata, componenttype FROM %s WHERE calendarid = ? && uri IN (', $this->calendarObjectTableName); |
|
| 431 | 431 | // Inserting a whole bunch of question marks |
| 432 | 432 | $query .= implode(',', array_fill(0, count($uris), '?')); |
| 433 | 433 | $query .= ')'; |
@@ -444,7 +444,7 @@ discard block |
||
| 444 | 444 | 'lastmodified' => $row['lastmodified'], |
| 445 | 445 | 'etag' => '"' . $row['etag'] . '"', |
| 446 | 446 | 'calendarid' => $row['calendarid'], |
| 447 | - 'size' => (int)$row['size'], |
|
| 447 | + 'size' => (int) $row['size'], |
|
| 448 | 448 | 'calendardata' => $row['calendardata'], |
| 449 | 449 | 'component' => strtolower($row['componenttype']), |
| 450 | 450 | ]; |
@@ -553,7 +553,7 @@ discard block |
||
| 553 | 553 | foreach ($vObject->getComponents() as $component) { |
| 554 | 554 | if ($component->name !== 'VTIMEZONE') { |
| 555 | 555 | $componentType = $component->name; |
| 556 | - $uid = (string)$component->UID; |
|
| 556 | + $uid = (string) $component->UID; |
|
| 557 | 557 | break; |
| 558 | 558 | } |
| 559 | 559 | } |
@@ -578,7 +578,7 @@ discard block |
||
| 578 | 578 | $lastOccurence = $firstOccurence; |
| 579 | 579 | } |
| 580 | 580 | } else { |
| 581 | - $it = new VObject\Recur\EventIterator($vObject, (string)$component->UID); |
|
| 581 | + $it = new VObject\Recur\EventIterator($vObject, (string) $component->UID); |
|
| 582 | 582 | $maxDate = new \DateTime(self::MAX_DATE); |
| 583 | 583 | if ($it->isInfinite()) { |
| 584 | 584 | $lastOccurence = $maxDate->getTimeStamp(); |
@@ -859,7 +859,7 @@ discard block |
||
| 859 | 859 | // Current synctoken |
| 860 | 860 | $query = sprintf('SELECT synctoken FROM %s WHERE id = ?', $this->calendarTableName); |
| 861 | 861 | $stmt = $this->pdo->prepare($query); |
| 862 | - $stmt->execute([ $calendarId ]); |
|
| 862 | + $stmt->execute([$calendarId]); |
|
| 863 | 863 | $currentToken = $stmt->fetchColumn(0); |
| 864 | 864 | |
| 865 | 865 | if (is_null($currentToken)) return null; |
@@ -874,7 +874,7 @@ discard block |
||
| 874 | 874 | if ($syncToken) { |
| 875 | 875 | |
| 876 | 876 | $query = sprintf('SELECT uri, operation FROM %s WHERE synctoken >= ? && synctoken < ? && calendarid = ? ORDER BY synctoken', $this->calendarChangesTableName); |
| 877 | - if ($limit > 0) $query .= " LIMIT " . (int)$limit; |
|
| 877 | + if ($limit > 0) $query .= " LIMIT " . (int) $limit; |
|
| 878 | 878 | |
| 879 | 879 | // Fetching all changes |
| 880 | 880 | $stmt = $this->pdo->prepare($query); |
@@ -1157,7 +1157,7 @@ discard block |
||
| 1157 | 1157 | 'calendardata' => $row['calendardata'], |
| 1158 | 1158 | 'lastmodified' => $row['lastmodified'], |
| 1159 | 1159 | 'etag' => '"' . $row['etag'] . '"', |
| 1160 | - 'size' => (int)$row['size'], |
|
| 1160 | + 'size' => (int) $row['size'], |
|
| 1161 | 1161 | ]; |
| 1162 | 1162 | |
| 1163 | 1163 | } |
@@ -1186,7 +1186,7 @@ discard block |
||
| 1186 | 1186 | 'uri' => $row['uri'], |
| 1187 | 1187 | 'lastmodified' => $row['lastmodified'], |
| 1188 | 1188 | 'etag' => '"' . $row['etag'] . '"', |
| 1189 | - 'size' => (int)$row['size'], |
|
| 1189 | + 'size' => (int) $row['size'], |
|
| 1190 | 1190 | ]; |
| 1191 | 1191 | } |
| 1192 | 1192 | |
@@ -1220,7 +1220,7 @@ discard block |
||
| 1220 | 1220 | public function createSchedulingObject($principalUri, $objectUri, $objectData) { |
| 1221 | 1221 | |
| 1222 | 1222 | $stmt = $this->pdo->prepare('INSERT INTO ' . $this->schedulingObjectTableName . ' (principaluri, calendardata, uri, lastmodified, etag, size) VALUES (?, ?, ?, ?, ?, ?)'); |
| 1223 | - $stmt->execute([$principalUri, $objectData, $objectUri, time(), md5($objectData), strlen($objectData) ]); |
|
| 1223 | + $stmt->execute([$principalUri, $objectData, $objectUri, time(), md5($objectData), strlen($objectData)]); |
|
| 1224 | 1224 | |
| 1225 | 1225 | } |
| 1226 | 1226 | |
@@ -67,7 +67,7 @@ |
||
| 67 | 67 | |
| 68 | 68 | $this->server = $server; |
| 69 | 69 | $server->on('method:GET', [$this, 'httpGet'], 90); |
| 70 | - $server->on('propFind', [$this, 'propFind']); |
|
| 70 | + $server->on('propFind', [$this, 'propFind']); |
|
| 71 | 71 | |
| 72 | 72 | $server->xml->namespaceMap[self::NS_CALENDARSERVER] = 'cs'; |
| 73 | 73 | $server->resourceTypeMapping['\\Sabre\\CalDAV\\Notifications\\ICollection'] = '{' . self::NS_CALENDARSERVER . '}notification'; |
@@ -135,8 +135,9 @@ |
||
| 135 | 135 | return; |
| 136 | 136 | } |
| 137 | 137 | |
| 138 | - if (!$node instanceof INode) |
|
| 139 | - return; |
|
| 138 | + if (!$node instanceof INode) { |
|
| 139 | + return; |
|
| 140 | + } |
|
| 140 | 141 | |
| 141 | 142 | $writer = $this->server->xml->getWriter(); |
| 142 | 143 | $writer->contextUri = $this->server->getBaseUri(); |
@@ -26,155 +26,155 @@ |
||
| 26 | 26 | */ |
| 27 | 27 | class Plugin extends ServerPlugin { |
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * This is the namespace for the proprietary calendarserver extensions |
|
| 31 | - */ |
|
| 32 | - const NS_CALENDARSERVER = 'http://calendarserver.org/ns/'; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Reference to the main server object. |
|
| 36 | - * |
|
| 37 | - * @var Server |
|
| 38 | - */ |
|
| 39 | - protected $server; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Returns a plugin name. |
|
| 43 | - * |
|
| 44 | - * Using this name other plugins will be able to access other plugins |
|
| 45 | - * using \Sabre\DAV\Server::getPlugin |
|
| 46 | - * |
|
| 47 | - * @return string |
|
| 48 | - */ |
|
| 49 | - public function getPluginName() { |
|
| 50 | - |
|
| 51 | - return 'notifications'; |
|
| 52 | - |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * This initializes the plugin. |
|
| 57 | - * |
|
| 58 | - * This function is called by Sabre\DAV\Server, after |
|
| 59 | - * addPlugin is called. |
|
| 60 | - * |
|
| 61 | - * This method should set up the required event subscriptions. |
|
| 62 | - * |
|
| 63 | - * @param Server $server |
|
| 64 | - * @return void |
|
| 65 | - */ |
|
| 66 | - public function initialize(Server $server) { |
|
| 67 | - |
|
| 68 | - $this->server = $server; |
|
| 69 | - $server->on('method:GET', [$this, 'httpGet'], 90); |
|
| 70 | - $server->on('propFind', [$this, 'propFind']); |
|
| 71 | - |
|
| 72 | - $server->xml->namespaceMap[self::NS_CALENDARSERVER] = 'cs'; |
|
| 73 | - $server->resourceTypeMapping['\\Sabre\\CalDAV\\Notifications\\ICollection'] = '{' . self::NS_CALENDARSERVER . '}notification'; |
|
| 74 | - |
|
| 75 | - array_push($server->protectedProperties, |
|
| 76 | - '{' . self::NS_CALENDARSERVER . '}notification-URL', |
|
| 77 | - '{' . self::NS_CALENDARSERVER . '}notificationtype' |
|
| 78 | - ); |
|
| 79 | - |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * PropFind |
|
| 84 | - * |
|
| 85 | - * @param PropFind $propFind |
|
| 86 | - * @param BaseINode $node |
|
| 87 | - * @return void |
|
| 88 | - */ |
|
| 89 | - public function propFind(PropFind $propFind, BaseINode $node) { |
|
| 90 | - |
|
| 91 | - $caldavPlugin = $this->server->getPlugin('caldav'); |
|
| 92 | - |
|
| 93 | - if ($node instanceof DAVACL\IPrincipal) { |
|
| 94 | - |
|
| 95 | - $principalUrl = $node->getPrincipalUrl(); |
|
| 96 | - |
|
| 97 | - // notification-URL property |
|
| 98 | - $propFind->handle('{' . self::NS_CALENDARSERVER . '}notification-URL', function() use ($principalUrl, $caldavPlugin) { |
|
| 99 | - |
|
| 100 | - $notificationPath = $caldavPlugin->getCalendarHomeForPrincipal($principalUrl) . '/notifications/'; |
|
| 101 | - return new DAV\Xml\Property\Href($notificationPath); |
|
| 102 | - |
|
| 103 | - }); |
|
| 104 | - |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - if ($node instanceof INode) { |
|
| 108 | - |
|
| 109 | - $propFind->handle( |
|
| 110 | - '{' . self::NS_CALENDARSERVER . '}notificationtype', |
|
| 111 | - [$node, 'getNotificationType'] |
|
| 112 | - ); |
|
| 113 | - |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * This event is triggered before the usual GET request handler. |
|
| 120 | - * |
|
| 121 | - * We use this to intercept GET calls to notification nodes, and return the |
|
| 122 | - * proper response. |
|
| 123 | - * |
|
| 124 | - * @param RequestInterface $request |
|
| 125 | - * @param ResponseInterface $response |
|
| 126 | - * @return void |
|
| 127 | - */ |
|
| 128 | - public function httpGet(RequestInterface $request, ResponseInterface $response) { |
|
| 129 | - |
|
| 130 | - $path = $request->getPath(); |
|
| 131 | - |
|
| 132 | - try { |
|
| 133 | - $node = $this->server->tree->getNodeForPath($path); |
|
| 134 | - } catch (DAV\Exception\NotFound $e) { |
|
| 135 | - return; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - if (!$node instanceof INode) |
|
| 139 | - return; |
|
| 140 | - |
|
| 141 | - $writer = $this->server->xml->getWriter(); |
|
| 142 | - $writer->contextUri = $this->server->getBaseUri(); |
|
| 143 | - $writer->openMemory(); |
|
| 144 | - $writer->startDocument('1.0', 'UTF-8'); |
|
| 145 | - $writer->startElement('{http://calendarserver.org/ns/}notification'); |
|
| 146 | - $node->getNotificationType()->xmlSerializeFull($writer); |
|
| 147 | - $writer->endElement(); |
|
| 148 | - |
|
| 149 | - $response->setHeader('Content-Type', 'application/xml'); |
|
| 150 | - $response->setHeader('ETag', $node->getETag()); |
|
| 151 | - $response->setStatus(200); |
|
| 152 | - $response->setBody($writer->outputMemory()); |
|
| 153 | - |
|
| 154 | - // Return false to break the event chain. |
|
| 155 | - return false; |
|
| 156 | - |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * Returns a bunch of meta-data about the plugin. |
|
| 161 | - * |
|
| 162 | - * Providing this information is optional, and is mainly displayed by the |
|
| 163 | - * Browser plugin. |
|
| 164 | - * |
|
| 165 | - * The description key in the returned array may contain html and will not |
|
| 166 | - * be sanitized. |
|
| 167 | - * |
|
| 168 | - * @return array |
|
| 169 | - */ |
|
| 170 | - public function getPluginInfo() { |
|
| 171 | - |
|
| 172 | - return [ |
|
| 173 | - 'name' => $this->getPluginName(), |
|
| 174 | - 'description' => 'Adds support for caldav-notifications, which is required to enable caldav-sharing.', |
|
| 175 | - 'link' => 'http://sabre.io/dav/caldav-sharing/', |
|
| 176 | - ]; |
|
| 177 | - |
|
| 178 | - } |
|
| 29 | + /** |
|
| 30 | + * This is the namespace for the proprietary calendarserver extensions |
|
| 31 | + */ |
|
| 32 | + const NS_CALENDARSERVER = 'http://calendarserver.org/ns/'; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Reference to the main server object. |
|
| 36 | + * |
|
| 37 | + * @var Server |
|
| 38 | + */ |
|
| 39 | + protected $server; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Returns a plugin name. |
|
| 43 | + * |
|
| 44 | + * Using this name other plugins will be able to access other plugins |
|
| 45 | + * using \Sabre\DAV\Server::getPlugin |
|
| 46 | + * |
|
| 47 | + * @return string |
|
| 48 | + */ |
|
| 49 | + public function getPluginName() { |
|
| 50 | + |
|
| 51 | + return 'notifications'; |
|
| 52 | + |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * This initializes the plugin. |
|
| 57 | + * |
|
| 58 | + * This function is called by Sabre\DAV\Server, after |
|
| 59 | + * addPlugin is called. |
|
| 60 | + * |
|
| 61 | + * This method should set up the required event subscriptions. |
|
| 62 | + * |
|
| 63 | + * @param Server $server |
|
| 64 | + * @return void |
|
| 65 | + */ |
|
| 66 | + public function initialize(Server $server) { |
|
| 67 | + |
|
| 68 | + $this->server = $server; |
|
| 69 | + $server->on('method:GET', [$this, 'httpGet'], 90); |
|
| 70 | + $server->on('propFind', [$this, 'propFind']); |
|
| 71 | + |
|
| 72 | + $server->xml->namespaceMap[self::NS_CALENDARSERVER] = 'cs'; |
|
| 73 | + $server->resourceTypeMapping['\\Sabre\\CalDAV\\Notifications\\ICollection'] = '{' . self::NS_CALENDARSERVER . '}notification'; |
|
| 74 | + |
|
| 75 | + array_push($server->protectedProperties, |
|
| 76 | + '{' . self::NS_CALENDARSERVER . '}notification-URL', |
|
| 77 | + '{' . self::NS_CALENDARSERVER . '}notificationtype' |
|
| 78 | + ); |
|
| 79 | + |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * PropFind |
|
| 84 | + * |
|
| 85 | + * @param PropFind $propFind |
|
| 86 | + * @param BaseINode $node |
|
| 87 | + * @return void |
|
| 88 | + */ |
|
| 89 | + public function propFind(PropFind $propFind, BaseINode $node) { |
|
| 90 | + |
|
| 91 | + $caldavPlugin = $this->server->getPlugin('caldav'); |
|
| 92 | + |
|
| 93 | + if ($node instanceof DAVACL\IPrincipal) { |
|
| 94 | + |
|
| 95 | + $principalUrl = $node->getPrincipalUrl(); |
|
| 96 | + |
|
| 97 | + // notification-URL property |
|
| 98 | + $propFind->handle('{' . self::NS_CALENDARSERVER . '}notification-URL', function() use ($principalUrl, $caldavPlugin) { |
|
| 99 | + |
|
| 100 | + $notificationPath = $caldavPlugin->getCalendarHomeForPrincipal($principalUrl) . '/notifications/'; |
|
| 101 | + return new DAV\Xml\Property\Href($notificationPath); |
|
| 102 | + |
|
| 103 | + }); |
|
| 104 | + |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + if ($node instanceof INode) { |
|
| 108 | + |
|
| 109 | + $propFind->handle( |
|
| 110 | + '{' . self::NS_CALENDARSERVER . '}notificationtype', |
|
| 111 | + [$node, 'getNotificationType'] |
|
| 112 | + ); |
|
| 113 | + |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * This event is triggered before the usual GET request handler. |
|
| 120 | + * |
|
| 121 | + * We use this to intercept GET calls to notification nodes, and return the |
|
| 122 | + * proper response. |
|
| 123 | + * |
|
| 124 | + * @param RequestInterface $request |
|
| 125 | + * @param ResponseInterface $response |
|
| 126 | + * @return void |
|
| 127 | + */ |
|
| 128 | + public function httpGet(RequestInterface $request, ResponseInterface $response) { |
|
| 129 | + |
|
| 130 | + $path = $request->getPath(); |
|
| 131 | + |
|
| 132 | + try { |
|
| 133 | + $node = $this->server->tree->getNodeForPath($path); |
|
| 134 | + } catch (DAV\Exception\NotFound $e) { |
|
| 135 | + return; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + if (!$node instanceof INode) |
|
| 139 | + return; |
|
| 140 | + |
|
| 141 | + $writer = $this->server->xml->getWriter(); |
|
| 142 | + $writer->contextUri = $this->server->getBaseUri(); |
|
| 143 | + $writer->openMemory(); |
|
| 144 | + $writer->startDocument('1.0', 'UTF-8'); |
|
| 145 | + $writer->startElement('{http://calendarserver.org/ns/}notification'); |
|
| 146 | + $node->getNotificationType()->xmlSerializeFull($writer); |
|
| 147 | + $writer->endElement(); |
|
| 148 | + |
|
| 149 | + $response->setHeader('Content-Type', 'application/xml'); |
|
| 150 | + $response->setHeader('ETag', $node->getETag()); |
|
| 151 | + $response->setStatus(200); |
|
| 152 | + $response->setBody($writer->outputMemory()); |
|
| 153 | + |
|
| 154 | + // Return false to break the event chain. |
|
| 155 | + return false; |
|
| 156 | + |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * Returns a bunch of meta-data about the plugin. |
|
| 161 | + * |
|
| 162 | + * Providing this information is optional, and is mainly displayed by the |
|
| 163 | + * Browser plugin. |
|
| 164 | + * |
|
| 165 | + * The description key in the returned array may contain html and will not |
|
| 166 | + * be sanitized. |
|
| 167 | + * |
|
| 168 | + * @return array |
|
| 169 | + */ |
|
| 170 | + public function getPluginInfo() { |
|
| 171 | + |
|
| 172 | + return [ |
|
| 173 | + 'name' => $this->getPluginName(), |
|
| 174 | + 'description' => 'Adds support for caldav-notifications, which is required to enable caldav-sharing.', |
|
| 175 | + 'link' => 'http://sabre.io/dav/caldav-sharing/', |
|
| 176 | + ]; |
|
| 177 | + |
|
| 178 | + } |
|
| 179 | 179 | |
| 180 | 180 | } |
@@ -179,13 +179,13 @@ discard block |
||
| 179 | 179 | |
| 180 | 180 | $this->server = $server; |
| 181 | 181 | |
| 182 | - $server->on('method:MKCALENDAR', [$this, 'httpMkCalendar']); |
|
| 183 | - $server->on('report', [$this, 'report']); |
|
| 184 | - $server->on('propFind', [$this, 'propFind']); |
|
| 185 | - $server->on('onHTMLActionsPanel', [$this, 'htmlActionsPanel']); |
|
| 186 | - $server->on('beforeCreateFile', [$this, 'beforeCreateFile']); |
|
| 187 | - $server->on('beforeWriteContent', [$this, 'beforeWriteContent']); |
|
| 188 | - $server->on('afterMethod:GET', [$this, 'httpAfterGET']); |
|
| 182 | + $server->on('method:MKCALENDAR', [$this, 'httpMkCalendar']); |
|
| 183 | + $server->on('report', [$this, 'report']); |
|
| 184 | + $server->on('propFind', [$this, 'propFind']); |
|
| 185 | + $server->on('onHTMLActionsPanel', [$this, 'htmlActionsPanel']); |
|
| 186 | + $server->on('beforeCreateFile', [$this, 'beforeCreateFile']); |
|
| 187 | + $server->on('beforeWriteContent', [$this, 'beforeWriteContent']); |
|
| 188 | + $server->on('afterMethod:GET', [$this, 'httpAfterGET']); |
|
| 189 | 189 | |
| 190 | 190 | $server->xml->namespaceMap[self::NS_CALDAV] = 'cal'; |
| 191 | 191 | $server->xml->namespaceMap[self::NS_CALENDARSERVER] = 'cs'; |
@@ -294,7 +294,7 @@ discard block |
||
| 294 | 294 | if (isset($properties['{DAV:}resourcetype'])) { |
| 295 | 295 | $resourceType = $properties['{DAV:}resourcetype']->getValue(); |
| 296 | 296 | } else { |
| 297 | - $resourceType = ['{DAV:}collection','{urn:ietf:params:xml:ns:caldav}calendar']; |
|
| 297 | + $resourceType = ['{DAV:}collection', '{urn:ietf:params:xml:ns:caldav}calendar']; |
|
| 298 | 298 | } |
| 299 | 299 | |
| 300 | 300 | $this->server->createCollection($path, new MkCol($resourceType, $properties)); |
@@ -881,12 +881,12 @@ discard block |
||
| 881 | 881 | if (!isset($component->UID)) { |
| 882 | 882 | throw new DAV\Exception\BadRequest('Every ' . $component->name . ' component must have an UID'); |
| 883 | 883 | } |
| 884 | - $foundUID = (string)$component->UID; |
|
| 884 | + $foundUID = (string) $component->UID; |
|
| 885 | 885 | } else { |
| 886 | 886 | if ($foundType !== $component->name) { |
| 887 | 887 | throw new DAV\Exception\BadRequest('A calendar object must only contain 1 component. We found a ' . $component->name . ' as well as a ' . $foundType); |
| 888 | 888 | } |
| 889 | - if ($foundUID !== (string)$component->UID) { |
|
| 889 | + if ($foundUID !== (string) $component->UID) { |
|
| 890 | 890 | throw new DAV\Exception\BadRequest('Every ' . $component->name . ' in this object must have identical UIDs'); |
| 891 | 891 | } |
| 892 | 892 | } |
@@ -26,929 +26,929 @@ discard block |
||
| 26 | 26 | */ |
| 27 | 27 | class Plugin extends DAV\ServerPlugin { |
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * This is the official CalDAV namespace |
|
| 31 | - */ |
|
| 32 | - const NS_CALDAV = 'urn:ietf:params:xml:ns:caldav'; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * This is the namespace for the proprietary calendarserver extensions |
|
| 36 | - */ |
|
| 37 | - const NS_CALENDARSERVER = 'http://calendarserver.org/ns/'; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * The hardcoded root for calendar objects. It is unfortunate |
|
| 41 | - * that we're stuck with it, but it will have to do for now |
|
| 42 | - */ |
|
| 43 | - const CALENDAR_ROOT = 'calendars'; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Reference to server object |
|
| 47 | - * |
|
| 48 | - * @var DAV\Server |
|
| 49 | - */ |
|
| 50 | - protected $server; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * The default PDO storage uses a MySQL MEDIUMBLOB for iCalendar data, |
|
| 54 | - * which can hold up to 2^24 = 16777216 bytes. This is plenty. We're |
|
| 55 | - * capping it to 10M here. |
|
| 56 | - */ |
|
| 57 | - protected $maxResourceSize = 10000000; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Use this method to tell the server this plugin defines additional |
|
| 61 | - * HTTP methods. |
|
| 62 | - * |
|
| 63 | - * This method is passed a uri. It should only return HTTP methods that are |
|
| 64 | - * available for the specified uri. |
|
| 65 | - * |
|
| 66 | - * @param string $uri |
|
| 67 | - * @return array |
|
| 68 | - */ |
|
| 69 | - public function getHTTPMethods($uri) { |
|
| 70 | - |
|
| 71 | - // The MKCALENDAR is only available on unmapped uri's, whose |
|
| 72 | - // parents extend IExtendedCollection |
|
| 73 | - list($parent, $name) = Uri\split($uri); |
|
| 74 | - |
|
| 75 | - $node = $this->server->tree->getNodeForPath($parent); |
|
| 76 | - |
|
| 77 | - if ($node instanceof DAV\IExtendedCollection) { |
|
| 78 | - try { |
|
| 79 | - $node->getChild($name); |
|
| 80 | - } catch (DAV\Exception\NotFound $e) { |
|
| 81 | - return ['MKCALENDAR']; |
|
| 82 | - } |
|
| 83 | - } |
|
| 84 | - return []; |
|
| 85 | - |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Returns the path to a principal's calendar home. |
|
| 90 | - * |
|
| 91 | - * The return url must not end with a slash. |
|
| 92 | - * This function should return null in case a principal did not have |
|
| 93 | - * a calendar home. |
|
| 94 | - * |
|
| 95 | - * @param string $principalUrl |
|
| 96 | - * @return string |
|
| 97 | - */ |
|
| 98 | - public function getCalendarHomeForPrincipal($principalUrl) { |
|
| 99 | - |
|
| 100 | - // The default behavior for most sabre/dav servers is that there is a |
|
| 101 | - // principals root node, which contains users directly under it. |
|
| 102 | - // |
|
| 103 | - // This function assumes that there are two components in a principal |
|
| 104 | - // path. If there's more, we don't return a calendar home. This |
|
| 105 | - // excludes things like the calendar-proxy-read principal (which it |
|
| 106 | - // should). |
|
| 107 | - $parts = explode('/', trim($principalUrl, '/')); |
|
| 108 | - if (count($parts) !== 2) return; |
|
| 109 | - if ($parts[0] !== 'principals') return; |
|
| 110 | - |
|
| 111 | - return self::CALENDAR_ROOT . '/' . $parts[1]; |
|
| 112 | - |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Returns a list of features for the DAV: HTTP header. |
|
| 117 | - * |
|
| 118 | - * @return array |
|
| 119 | - */ |
|
| 120 | - public function getFeatures() { |
|
| 121 | - |
|
| 122 | - return ['calendar-access', 'calendar-proxy']; |
|
| 123 | - |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Returns a plugin name. |
|
| 128 | - * |
|
| 129 | - * Using this name other plugins will be able to access other plugins |
|
| 130 | - * using DAV\Server::getPlugin |
|
| 131 | - * |
|
| 132 | - * @return string |
|
| 133 | - */ |
|
| 134 | - public function getPluginName() { |
|
| 135 | - |
|
| 136 | - return 'caldav'; |
|
| 137 | - |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Returns a list of reports this plugin supports. |
|
| 142 | - * |
|
| 143 | - * This will be used in the {DAV:}supported-report-set property. |
|
| 144 | - * Note that you still need to subscribe to the 'report' event to actually |
|
| 145 | - * implement them |
|
| 146 | - * |
|
| 147 | - * @param string $uri |
|
| 148 | - * @return array |
|
| 149 | - */ |
|
| 150 | - public function getSupportedReportSet($uri) { |
|
| 151 | - |
|
| 152 | - $node = $this->server->tree->getNodeForPath($uri); |
|
| 153 | - |
|
| 154 | - $reports = []; |
|
| 155 | - if ($node instanceof ICalendarObjectContainer || $node instanceof ICalendarObject) { |
|
| 156 | - $reports[] = '{' . self::NS_CALDAV . '}calendar-multiget'; |
|
| 157 | - $reports[] = '{' . self::NS_CALDAV . '}calendar-query'; |
|
| 158 | - } |
|
| 159 | - if ($node instanceof ICalendar) { |
|
| 160 | - $reports[] = '{' . self::NS_CALDAV . '}free-busy-query'; |
|
| 161 | - } |
|
| 162 | - // iCal has a bug where it assumes that sync support is enabled, only |
|
| 163 | - // if we say we support it on the calendar-home, even though this is |
|
| 164 | - // not actually the case. |
|
| 165 | - if ($node instanceof CalendarHome && $this->server->getPlugin('sync')) { |
|
| 166 | - $reports[] = '{DAV:}sync-collection'; |
|
| 167 | - } |
|
| 168 | - return $reports; |
|
| 169 | - |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * Initializes the plugin |
|
| 174 | - * |
|
| 175 | - * @param DAV\Server $server |
|
| 176 | - * @return void |
|
| 177 | - */ |
|
| 178 | - public function initialize(DAV\Server $server) { |
|
| 179 | - |
|
| 180 | - $this->server = $server; |
|
| 181 | - |
|
| 182 | - $server->on('method:MKCALENDAR', [$this, 'httpMkCalendar']); |
|
| 183 | - $server->on('report', [$this, 'report']); |
|
| 184 | - $server->on('propFind', [$this, 'propFind']); |
|
| 185 | - $server->on('onHTMLActionsPanel', [$this, 'htmlActionsPanel']); |
|
| 186 | - $server->on('beforeCreateFile', [$this, 'beforeCreateFile']); |
|
| 187 | - $server->on('beforeWriteContent', [$this, 'beforeWriteContent']); |
|
| 188 | - $server->on('afterMethod:GET', [$this, 'httpAfterGET']); |
|
| 189 | - |
|
| 190 | - $server->xml->namespaceMap[self::NS_CALDAV] = 'cal'; |
|
| 191 | - $server->xml->namespaceMap[self::NS_CALENDARSERVER] = 'cs'; |
|
| 192 | - |
|
| 193 | - $server->xml->elementMap['{' . self::NS_CALDAV . '}supported-calendar-component-set'] = 'Sabre\\CalDAV\\Xml\\Property\\SupportedCalendarComponentSet'; |
|
| 194 | - $server->xml->elementMap['{' . self::NS_CALDAV . '}calendar-query'] = 'Sabre\\CalDAV\\Xml\\Request\\CalendarQueryReport'; |
|
| 195 | - $server->xml->elementMap['{' . self::NS_CALDAV . '}calendar-multiget'] = 'Sabre\\CalDAV\\Xml\\Request\\CalendarMultiGetReport'; |
|
| 196 | - $server->xml->elementMap['{' . self::NS_CALDAV . '}free-busy-query'] = 'Sabre\\CalDAV\\Xml\\Request\\FreeBusyQueryReport'; |
|
| 197 | - $server->xml->elementMap['{' . self::NS_CALDAV . '}mkcalendar'] = 'Sabre\\CalDAV\\Xml\\Request\\MkCalendar'; |
|
| 198 | - $server->xml->elementMap['{' . self::NS_CALDAV . '}schedule-calendar-transp'] = 'Sabre\\CalDAV\\Xml\\Property\\ScheduleCalendarTransp'; |
|
| 199 | - $server->xml->elementMap['{' . self::NS_CALDAV . '}supported-calendar-component-set'] = 'Sabre\\CalDAV\\Xml\\Property\\SupportedCalendarComponentSet'; |
|
| 200 | - |
|
| 201 | - $server->resourceTypeMapping['\\Sabre\\CalDAV\\ICalendar'] = '{urn:ietf:params:xml:ns:caldav}calendar'; |
|
| 202 | - |
|
| 203 | - $server->resourceTypeMapping['\\Sabre\\CalDAV\\Principal\\IProxyRead'] = '{http://calendarserver.org/ns/}calendar-proxy-read'; |
|
| 204 | - $server->resourceTypeMapping['\\Sabre\\CalDAV\\Principal\\IProxyWrite'] = '{http://calendarserver.org/ns/}calendar-proxy-write'; |
|
| 205 | - |
|
| 206 | - array_push($server->protectedProperties, |
|
| 207 | - |
|
| 208 | - '{' . self::NS_CALDAV . '}supported-calendar-component-set', |
|
| 209 | - '{' . self::NS_CALDAV . '}supported-calendar-data', |
|
| 210 | - '{' . self::NS_CALDAV . '}max-resource-size', |
|
| 211 | - '{' . self::NS_CALDAV . '}min-date-time', |
|
| 212 | - '{' . self::NS_CALDAV . '}max-date-time', |
|
| 213 | - '{' . self::NS_CALDAV . '}max-instances', |
|
| 214 | - '{' . self::NS_CALDAV . '}max-attendees-per-instance', |
|
| 215 | - '{' . self::NS_CALDAV . '}calendar-home-set', |
|
| 216 | - '{' . self::NS_CALDAV . '}supported-collation-set', |
|
| 217 | - '{' . self::NS_CALDAV . '}calendar-data', |
|
| 218 | - |
|
| 219 | - // CalendarServer extensions |
|
| 220 | - '{' . self::NS_CALENDARSERVER . '}getctag', |
|
| 221 | - '{' . self::NS_CALENDARSERVER . '}calendar-proxy-read-for', |
|
| 222 | - '{' . self::NS_CALENDARSERVER . '}calendar-proxy-write-for' |
|
| 223 | - |
|
| 224 | - ); |
|
| 225 | - |
|
| 226 | - if ($aclPlugin = $server->getPlugin('acl')) { |
|
| 227 | - $aclPlugin->principalSearchPropertySet['{' . self::NS_CALDAV . '}calendar-user-address-set'] = 'Calendar address'; |
|
| 228 | - } |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - /** |
|
| 232 | - * This functions handles REPORT requests specific to CalDAV |
|
| 233 | - * |
|
| 234 | - * @param string $reportName |
|
| 235 | - * @param mixed $report |
|
| 236 | - * @return bool |
|
| 237 | - */ |
|
| 238 | - public function report($reportName, $report) { |
|
| 239 | - |
|
| 240 | - switch ($reportName) { |
|
| 241 | - case '{' . self::NS_CALDAV . '}calendar-multiget' : |
|
| 242 | - $this->server->transactionType = 'report-calendar-multiget'; |
|
| 243 | - $this->calendarMultiGetReport($report); |
|
| 244 | - return false; |
|
| 245 | - case '{' . self::NS_CALDAV . '}calendar-query' : |
|
| 246 | - $this->server->transactionType = 'report-calendar-query'; |
|
| 247 | - $this->calendarQueryReport($report); |
|
| 248 | - return false; |
|
| 249 | - case '{' . self::NS_CALDAV . '}free-busy-query' : |
|
| 250 | - $this->server->transactionType = 'report-free-busy-query'; |
|
| 251 | - $this->freeBusyQueryReport($report); |
|
| 252 | - return false; |
|
| 253 | - |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - /** |
|
| 260 | - * This function handles the MKCALENDAR HTTP method, which creates |
|
| 261 | - * a new calendar. |
|
| 262 | - * |
|
| 263 | - * @param RequestInterface $request |
|
| 264 | - * @param ResponseInterface $response |
|
| 265 | - * @return bool |
|
| 266 | - */ |
|
| 267 | - public function httpMkCalendar(RequestInterface $request, ResponseInterface $response) { |
|
| 268 | - |
|
| 269 | - $body = $request->getBodyAsString(); |
|
| 270 | - $path = $request->getPath(); |
|
| 271 | - |
|
| 272 | - $properties = []; |
|
| 273 | - |
|
| 274 | - if ($body) { |
|
| 275 | - |
|
| 276 | - try { |
|
| 277 | - $mkcalendar = $this->server->xml->expect( |
|
| 278 | - '{urn:ietf:params:xml:ns:caldav}mkcalendar', |
|
| 279 | - $body |
|
| 280 | - ); |
|
| 281 | - } catch (\Sabre\Xml\ParseException $e) { |
|
| 282 | - throw new BadRequest($e->getMessage(), null, $e); |
|
| 283 | - } |
|
| 284 | - $properties = $mkcalendar->getProperties(); |
|
| 285 | - |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - // iCal abuses MKCALENDAR since iCal 10.9.2 to create server-stored |
|
| 289 | - // subscriptions. Before that it used MKCOL which was the correct way |
|
| 290 | - // to do this. |
|
| 291 | - // |
|
| 292 | - // If the body had a {DAV:}resourcetype, it means we stumbled upon this |
|
| 293 | - // request, and we simply use it instead of the pre-defined list. |
|
| 294 | - if (isset($properties['{DAV:}resourcetype'])) { |
|
| 295 | - $resourceType = $properties['{DAV:}resourcetype']->getValue(); |
|
| 296 | - } else { |
|
| 297 | - $resourceType = ['{DAV:}collection','{urn:ietf:params:xml:ns:caldav}calendar']; |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - $this->server->createCollection($path, new MkCol($resourceType, $properties)); |
|
| 301 | - |
|
| 302 | - $this->server->httpResponse->setStatus(201); |
|
| 303 | - $this->server->httpResponse->setHeader('Content-Length', 0); |
|
| 304 | - |
|
| 305 | - // This breaks the method chain. |
|
| 306 | - return false; |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * PropFind |
|
| 311 | - * |
|
| 312 | - * This method handler is invoked before any after properties for a |
|
| 313 | - * resource are fetched. This allows us to add in any CalDAV specific |
|
| 314 | - * properties. |
|
| 315 | - * |
|
| 316 | - * @param DAV\PropFind $propFind |
|
| 317 | - * @param DAV\INode $node |
|
| 318 | - * @return void |
|
| 319 | - */ |
|
| 320 | - public function propFind(DAV\PropFind $propFind, DAV\INode $node) { |
|
| 321 | - |
|
| 322 | - $ns = '{' . self::NS_CALDAV . '}'; |
|
| 323 | - |
|
| 324 | - if ($node instanceof ICalendarObjectContainer) { |
|
| 325 | - |
|
| 326 | - $propFind->handle($ns . 'max-resource-size', $this->maxResourceSize); |
|
| 327 | - $propFind->handle($ns . 'supported-calendar-data', function() { |
|
| 328 | - return new Xml\Property\SupportedCalendarData(); |
|
| 329 | - }); |
|
| 330 | - $propFind->handle($ns . 'supported-collation-set', function() { |
|
| 331 | - return new Xml\Property\SupportedCollationSet(); |
|
| 332 | - }); |
|
| 333 | - |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - if ($node instanceof DAVACL\IPrincipal) { |
|
| 337 | - |
|
| 338 | - $principalUrl = $node->getPrincipalUrl(); |
|
| 339 | - |
|
| 340 | - $propFind->handle('{' . self::NS_CALDAV . '}calendar-home-set', function() use ($principalUrl) { |
|
| 341 | - |
|
| 342 | - $calendarHomePath = $this->getCalendarHomeForPrincipal($principalUrl); |
|
| 343 | - if (is_null($calendarHomePath)) return null; |
|
| 344 | - return new Href($calendarHomePath . '/'); |
|
| 345 | - |
|
| 346 | - }); |
|
| 347 | - // The calendar-user-address-set property is basically mapped to |
|
| 348 | - // the {DAV:}alternate-URI-set property. |
|
| 349 | - $propFind->handle('{' . self::NS_CALDAV . '}calendar-user-address-set', function() use ($node) { |
|
| 350 | - $addresses = $node->getAlternateUriSet(); |
|
| 351 | - $addresses[] = $this->server->getBaseUri() . $node->getPrincipalUrl() . '/'; |
|
| 352 | - return new Href($addresses, false); |
|
| 353 | - }); |
|
| 354 | - // For some reason somebody thought it was a good idea to add |
|
| 355 | - // another one of these properties. We're supporting it too. |
|
| 356 | - $propFind->handle('{' . self::NS_CALENDARSERVER . '}email-address-set', function() use ($node) { |
|
| 357 | - $addresses = $node->getAlternateUriSet(); |
|
| 358 | - $emails = []; |
|
| 359 | - foreach ($addresses as $address) { |
|
| 360 | - if (substr($address, 0, 7) === 'mailto:') { |
|
| 361 | - $emails[] = substr($address, 7); |
|
| 362 | - } |
|
| 363 | - } |
|
| 364 | - return new Xml\Property\EmailAddressSet($emails); |
|
| 365 | - }); |
|
| 366 | - |
|
| 367 | - // These two properties are shortcuts for ical to easily find |
|
| 368 | - // other principals this principal has access to. |
|
| 369 | - $propRead = '{' . self::NS_CALENDARSERVER . '}calendar-proxy-read-for'; |
|
| 370 | - $propWrite = '{' . self::NS_CALENDARSERVER . '}calendar-proxy-write-for'; |
|
| 371 | - |
|
| 372 | - if ($propFind->getStatus($propRead) === 404 || $propFind->getStatus($propWrite) === 404) { |
|
| 373 | - |
|
| 374 | - $aclPlugin = $this->server->getPlugin('acl'); |
|
| 375 | - $membership = $aclPlugin->getPrincipalMembership($propFind->getPath()); |
|
| 376 | - $readList = []; |
|
| 377 | - $writeList = []; |
|
| 378 | - |
|
| 379 | - foreach ($membership as $group) { |
|
| 380 | - |
|
| 381 | - $groupNode = $this->server->tree->getNodeForPath($group); |
|
| 382 | - |
|
| 383 | - $listItem = Uri\split($group)[0] . '/'; |
|
| 384 | - |
|
| 385 | - // If the node is either ap proxy-read or proxy-write |
|
| 386 | - // group, we grab the parent principal and add it to the |
|
| 387 | - // list. |
|
| 388 | - if ($groupNode instanceof Principal\IProxyRead) { |
|
| 389 | - $readList[] = $listItem; |
|
| 390 | - } |
|
| 391 | - if ($groupNode instanceof Principal\IProxyWrite) { |
|
| 392 | - $writeList[] = $listItem; |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - $propFind->set($propRead, new Href($readList)); |
|
| 398 | - $propFind->set($propWrite, new Href($writeList)); |
|
| 399 | - |
|
| 400 | - } |
|
| 401 | - |
|
| 402 | - } // instanceof IPrincipal |
|
| 403 | - |
|
| 404 | - if ($node instanceof ICalendarObject) { |
|
| 405 | - |
|
| 406 | - // The calendar-data property is not supposed to be a 'real' |
|
| 407 | - // property, but in large chunks of the spec it does act as such. |
|
| 408 | - // Therefore we simply expose it as a property. |
|
| 409 | - $propFind->handle('{' . self::NS_CALDAV . '}calendar-data', function() use ($node) { |
|
| 410 | - $val = $node->get(); |
|
| 411 | - if (is_resource($val)) |
|
| 412 | - $val = stream_get_contents($val); |
|
| 413 | - |
|
| 414 | - // Taking out \r to not screw up the xml output |
|
| 415 | - return str_replace("\r", "", $val); |
|
| 416 | - |
|
| 417 | - }); |
|
| 418 | - |
|
| 419 | - } |
|
| 420 | - |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - /** |
|
| 424 | - * This function handles the calendar-multiget REPORT. |
|
| 425 | - * |
|
| 426 | - * This report is used by the client to fetch the content of a series |
|
| 427 | - * of urls. Effectively avoiding a lot of redundant requests. |
|
| 428 | - * |
|
| 429 | - * @param CalendarMultiGetReport $report |
|
| 430 | - * @return void |
|
| 431 | - */ |
|
| 432 | - public function calendarMultiGetReport($report) { |
|
| 433 | - |
|
| 434 | - $needsJson = $report->contentType === 'application/calendar+json'; |
|
| 435 | - |
|
| 436 | - $timeZones = []; |
|
| 437 | - $propertyList = []; |
|
| 438 | - |
|
| 439 | - $paths = array_map( |
|
| 440 | - [$this->server, 'calculateUri'], |
|
| 441 | - $report->hrefs |
|
| 442 | - ); |
|
| 443 | - |
|
| 444 | - foreach ($this->server->getPropertiesForMultiplePaths($paths, $report->properties) as $uri => $objProps) { |
|
| 445 | - |
|
| 446 | - if (($needsJson || $report->expand) && isset($objProps[200]['{' . self::NS_CALDAV . '}calendar-data'])) { |
|
| 447 | - $vObject = VObject\Reader::read($objProps[200]['{' . self::NS_CALDAV . '}calendar-data']); |
|
| 448 | - |
|
| 449 | - if ($report->expand) { |
|
| 450 | - // We're expanding, and for that we need to figure out the |
|
| 451 | - // calendar's timezone. |
|
| 452 | - list($calendarPath) = Uri\split($uri); |
|
| 453 | - if (!isset($timeZones[$calendarPath])) { |
|
| 454 | - // Checking the calendar-timezone property. |
|
| 455 | - $tzProp = '{' . self::NS_CALDAV . '}calendar-timezone'; |
|
| 456 | - $tzResult = $this->server->getProperties($calendarPath, [$tzProp]); |
|
| 457 | - if (isset($tzResult[$tzProp])) { |
|
| 458 | - // This property contains a VCALENDAR with a single |
|
| 459 | - // VTIMEZONE. |
|
| 460 | - $vtimezoneObj = VObject\Reader::read($tzResult[$tzProp]); |
|
| 461 | - $timeZone = $vtimezoneObj->VTIMEZONE->getTimeZone(); |
|
| 462 | - } else { |
|
| 463 | - // Defaulting to UTC. |
|
| 464 | - $timeZone = new DateTimeZone('UTC'); |
|
| 465 | - } |
|
| 466 | - $timeZones[$calendarPath] = $timeZone; |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - $vObject = $vObject->expand($report->expand['start'], $report->expand['end'], $timeZones[$calendarPath]); |
|
| 470 | - } |
|
| 471 | - if ($needsJson) { |
|
| 472 | - $objProps[200]['{' . self::NS_CALDAV . '}calendar-data'] = json_encode($vObject->jsonSerialize()); |
|
| 473 | - } else { |
|
| 474 | - $objProps[200]['{' . self::NS_CALDAV . '}calendar-data'] = $vObject->serialize(); |
|
| 475 | - } |
|
| 476 | - // Destroy circular references so PHP will garbage collect the |
|
| 477 | - // object. |
|
| 478 | - $vObject->destroy(); |
|
| 479 | - } |
|
| 480 | - |
|
| 481 | - $propertyList[] = $objProps; |
|
| 482 | - |
|
| 483 | - } |
|
| 484 | - |
|
| 485 | - $prefer = $this->server->getHTTPPrefer(); |
|
| 486 | - |
|
| 487 | - $this->server->httpResponse->setStatus(207); |
|
| 488 | - $this->server->httpResponse->setHeader('Content-Type', 'application/xml; charset=utf-8'); |
|
| 489 | - $this->server->httpResponse->setHeader('Vary', 'Brief,Prefer'); |
|
| 490 | - $this->server->httpResponse->setBody($this->server->generateMultiStatus($propertyList, $prefer['return'] === 'minimal')); |
|
| 491 | - |
|
| 492 | - } |
|
| 493 | - |
|
| 494 | - /** |
|
| 495 | - * This function handles the calendar-query REPORT |
|
| 496 | - * |
|
| 497 | - * This report is used by clients to request calendar objects based on |
|
| 498 | - * complex conditions. |
|
| 499 | - * |
|
| 500 | - * @param Xml\Request\CalendarQueryReport $report |
|
| 501 | - * @return void |
|
| 502 | - */ |
|
| 503 | - public function calendarQueryReport($report) { |
|
| 504 | - |
|
| 505 | - $path = $this->server->getRequestUri(); |
|
| 506 | - |
|
| 507 | - $needsJson = $report->contentType === 'application/calendar+json'; |
|
| 508 | - |
|
| 509 | - $node = $this->server->tree->getNodeForPath($this->server->getRequestUri()); |
|
| 510 | - $depth = $this->server->getHTTPDepth(0); |
|
| 511 | - |
|
| 512 | - // The default result is an empty array |
|
| 513 | - $result = []; |
|
| 514 | - |
|
| 515 | - $calendarTimeZone = null; |
|
| 516 | - if ($report->expand) { |
|
| 517 | - // We're expanding, and for that we need to figure out the |
|
| 518 | - // calendar's timezone. |
|
| 519 | - $tzProp = '{' . self::NS_CALDAV . '}calendar-timezone'; |
|
| 520 | - $tzResult = $this->server->getProperties($path, [$tzProp]); |
|
| 521 | - if (isset($tzResult[$tzProp])) { |
|
| 522 | - // This property contains a VCALENDAR with a single |
|
| 523 | - // VTIMEZONE. |
|
| 524 | - $vtimezoneObj = VObject\Reader::read($tzResult[$tzProp]); |
|
| 525 | - $calendarTimeZone = $vtimezoneObj->VTIMEZONE->getTimeZone(); |
|
| 526 | - |
|
| 527 | - // Destroy circular references so PHP will garbage collect the |
|
| 528 | - // object. |
|
| 529 | - $vtimezoneObj->destroy(); |
|
| 530 | - } else { |
|
| 531 | - // Defaulting to UTC. |
|
| 532 | - $calendarTimeZone = new DateTimeZone('UTC'); |
|
| 533 | - } |
|
| 534 | - } |
|
| 535 | - |
|
| 536 | - // The calendarobject was requested directly. In this case we handle |
|
| 537 | - // this locally. |
|
| 538 | - if ($depth == 0 && $node instanceof ICalendarObject) { |
|
| 539 | - |
|
| 540 | - $requestedCalendarData = true; |
|
| 541 | - $requestedProperties = $report->properties; |
|
| 542 | - |
|
| 543 | - if (!in_array('{urn:ietf:params:xml:ns:caldav}calendar-data', $requestedProperties)) { |
|
| 544 | - |
|
| 545 | - // We always retrieve calendar-data, as we need it for filtering. |
|
| 546 | - $requestedProperties[] = '{urn:ietf:params:xml:ns:caldav}calendar-data'; |
|
| 547 | - |
|
| 548 | - // If calendar-data wasn't explicitly requested, we need to remove |
|
| 549 | - // it after processing. |
|
| 550 | - $requestedCalendarData = false; |
|
| 551 | - } |
|
| 552 | - |
|
| 553 | - $properties = $this->server->getPropertiesForPath( |
|
| 554 | - $path, |
|
| 555 | - $requestedProperties, |
|
| 556 | - 0 |
|
| 557 | - ); |
|
| 558 | - |
|
| 559 | - // This array should have only 1 element, the first calendar |
|
| 560 | - // object. |
|
| 561 | - $properties = current($properties); |
|
| 562 | - |
|
| 563 | - // If there wasn't any calendar-data returned somehow, we ignore |
|
| 564 | - // this. |
|
| 565 | - if (isset($properties[200]['{urn:ietf:params:xml:ns:caldav}calendar-data'])) { |
|
| 566 | - |
|
| 567 | - $validator = new CalendarQueryValidator(); |
|
| 568 | - |
|
| 569 | - $vObject = VObject\Reader::read($properties[200]['{urn:ietf:params:xml:ns:caldav}calendar-data']); |
|
| 570 | - if ($validator->validate($vObject, $report->filters)) { |
|
| 571 | - |
|
| 572 | - // If the client didn't require the calendar-data property, |
|
| 573 | - // we won't give it back. |
|
| 574 | - if (!$requestedCalendarData) { |
|
| 575 | - unset($properties[200]['{urn:ietf:params:xml:ns:caldav}calendar-data']); |
|
| 576 | - } else { |
|
| 577 | - |
|
| 578 | - |
|
| 579 | - if ($report->expand) { |
|
| 580 | - $vObject = $vObject->expand($report->expand['start'], $report->expand['end'], $calendarTimeZone); |
|
| 581 | - } |
|
| 582 | - if ($needsJson) { |
|
| 583 | - $properties[200]['{' . self::NS_CALDAV . '}calendar-data'] = json_encode($vObject->jsonSerialize()); |
|
| 584 | - } elseif ($report->expand) { |
|
| 585 | - $properties[200]['{' . self::NS_CALDAV . '}calendar-data'] = $vObject->serialize(); |
|
| 586 | - } |
|
| 587 | - } |
|
| 588 | - |
|
| 589 | - $result = [$properties]; |
|
| 590 | - |
|
| 591 | - } |
|
| 592 | - // Destroy circular references so PHP will garbage collect the |
|
| 593 | - // object. |
|
| 594 | - $vObject->destroy(); |
|
| 595 | - |
|
| 596 | - } |
|
| 597 | - |
|
| 598 | - } |
|
| 599 | - |
|
| 600 | - if ($node instanceof ICalendarObjectContainer && $depth === 0) { |
|
| 601 | - |
|
| 602 | - if (strpos($this->server->httpRequest->getHeader('User-Agent'), 'MSFT-') === 0) { |
|
| 603 | - // Microsoft clients incorrectly supplied depth as 0, when it actually |
|
| 604 | - // should have set depth to 1. We're implementing a workaround here |
|
| 605 | - // to deal with this. |
|
| 606 | - // |
|
| 607 | - // This targets at least the following clients: |
|
| 608 | - // Windows 10 |
|
| 609 | - // Windows Phone 8, 10 |
|
| 610 | - $depth = 1; |
|
| 611 | - } else { |
|
| 612 | - throw new BadRequest('A calendar-query REPORT on a calendar with a Depth: 0 is undefined. Set Depth to 1'); |
|
| 613 | - } |
|
| 614 | - |
|
| 615 | - } |
|
| 616 | - |
|
| 617 | - // If we're dealing with a calendar, the calendar itself is responsible |
|
| 618 | - // for the calendar-query. |
|
| 619 | - if ($node instanceof ICalendarObjectContainer && $depth == 1) { |
|
| 620 | - |
|
| 621 | - $nodePaths = $node->calendarQuery($report->filters); |
|
| 622 | - |
|
| 623 | - foreach ($nodePaths as $path) { |
|
| 624 | - |
|
| 625 | - list($properties) = |
|
| 626 | - $this->server->getPropertiesForPath($this->server->getRequestUri() . '/' . $path, $report->properties); |
|
| 627 | - |
|
| 628 | - if (($needsJson || $report->expand)) { |
|
| 629 | - $vObject = VObject\Reader::read($properties[200]['{' . self::NS_CALDAV . '}calendar-data']); |
|
| 630 | - |
|
| 631 | - if ($report->expand) { |
|
| 632 | - $vObject = $vObject->expand($report->expand['start'], $report->expand['end'], $calendarTimeZone); |
|
| 633 | - } |
|
| 634 | - |
|
| 635 | - if ($needsJson) { |
|
| 636 | - $properties[200]['{' . self::NS_CALDAV . '}calendar-data'] = json_encode($vObject->jsonSerialize()); |
|
| 637 | - } else { |
|
| 638 | - $properties[200]['{' . self::NS_CALDAV . '}calendar-data'] = $vObject->serialize(); |
|
| 639 | - } |
|
| 640 | - |
|
| 641 | - // Destroy circular references so PHP will garbage collect the |
|
| 642 | - // object. |
|
| 643 | - $vObject->destroy(); |
|
| 644 | - } |
|
| 645 | - $result[] = $properties; |
|
| 646 | - |
|
| 647 | - } |
|
| 648 | - |
|
| 649 | - } |
|
| 650 | - |
|
| 651 | - $prefer = $this->server->getHTTPPrefer(); |
|
| 652 | - |
|
| 653 | - $this->server->httpResponse->setStatus(207); |
|
| 654 | - $this->server->httpResponse->setHeader('Content-Type', 'application/xml; charset=utf-8'); |
|
| 655 | - $this->server->httpResponse->setHeader('Vary', 'Brief,Prefer'); |
|
| 656 | - $this->server->httpResponse->setBody($this->server->generateMultiStatus($result, $prefer['return'] === 'minimal')); |
|
| 657 | - |
|
| 658 | - } |
|
| 659 | - |
|
| 660 | - /** |
|
| 661 | - * This method is responsible for parsing the request and generating the |
|
| 662 | - * response for the CALDAV:free-busy-query REPORT. |
|
| 663 | - * |
|
| 664 | - * @param Xml\Request\FreeBusyQueryReport $report |
|
| 665 | - * @return void |
|
| 666 | - */ |
|
| 667 | - protected function freeBusyQueryReport(Xml\Request\FreeBusyQueryReport $report) { |
|
| 668 | - |
|
| 669 | - $uri = $this->server->getRequestUri(); |
|
| 670 | - |
|
| 671 | - $acl = $this->server->getPlugin('acl'); |
|
| 672 | - if ($acl) { |
|
| 673 | - $acl->checkPrivileges($uri, '{' . self::NS_CALDAV . '}read-free-busy'); |
|
| 674 | - } |
|
| 675 | - |
|
| 676 | - $calendar = $this->server->tree->getNodeForPath($uri); |
|
| 677 | - if (!$calendar instanceof ICalendar) { |
|
| 678 | - throw new DAV\Exception\NotImplemented('The free-busy-query REPORT is only implemented on calendars'); |
|
| 679 | - } |
|
| 680 | - |
|
| 681 | - $tzProp = '{' . self::NS_CALDAV . '}calendar-timezone'; |
|
| 682 | - |
|
| 683 | - // Figuring out the default timezone for the calendar, for floating |
|
| 684 | - // times. |
|
| 685 | - $calendarProps = $this->server->getProperties($uri, [$tzProp]); |
|
| 686 | - |
|
| 687 | - if (isset($calendarProps[$tzProp])) { |
|
| 688 | - $vtimezoneObj = VObject\Reader::read($calendarProps[$tzProp]); |
|
| 689 | - $calendarTimeZone = $vtimezoneObj->VTIMEZONE->getTimeZone(); |
|
| 690 | - // Destroy circular references so PHP will garbage collect the object. |
|
| 691 | - $vtimezoneObj->destroy(); |
|
| 692 | - } else { |
|
| 693 | - $calendarTimeZone = new DateTimeZone('UTC'); |
|
| 694 | - } |
|
| 695 | - |
|
| 696 | - // Doing a calendar-query first, to make sure we get the most |
|
| 697 | - // performance. |
|
| 698 | - $urls = $calendar->calendarQuery([ |
|
| 699 | - 'name' => 'VCALENDAR', |
|
| 700 | - 'comp-filters' => [ |
|
| 701 | - [ |
|
| 702 | - 'name' => 'VEVENT', |
|
| 703 | - 'comp-filters' => [], |
|
| 704 | - 'prop-filters' => [], |
|
| 705 | - 'is-not-defined' => false, |
|
| 706 | - 'time-range' => [ |
|
| 707 | - 'start' => $report->start, |
|
| 708 | - 'end' => $report->end, |
|
| 709 | - ], |
|
| 710 | - ], |
|
| 711 | - ], |
|
| 712 | - 'prop-filters' => [], |
|
| 713 | - 'is-not-defined' => false, |
|
| 714 | - 'time-range' => null, |
|
| 715 | - ]); |
|
| 716 | - |
|
| 717 | - $objects = array_map(function($url) use ($calendar) { |
|
| 718 | - $obj = $calendar->getChild($url)->get(); |
|
| 719 | - return $obj; |
|
| 720 | - }, $urls); |
|
| 721 | - |
|
| 722 | - $generator = new VObject\FreeBusyGenerator(); |
|
| 723 | - $generator->setObjects($objects); |
|
| 724 | - $generator->setTimeRange($report->start, $report->end); |
|
| 725 | - $generator->setTimeZone($calendarTimeZone); |
|
| 726 | - $result = $generator->getResult(); |
|
| 727 | - $result = $result->serialize(); |
|
| 728 | - |
|
| 729 | - $this->server->httpResponse->setStatus(200); |
|
| 730 | - $this->server->httpResponse->setHeader('Content-Type', 'text/calendar'); |
|
| 731 | - $this->server->httpResponse->setHeader('Content-Length', strlen($result)); |
|
| 732 | - $this->server->httpResponse->setBody($result); |
|
| 733 | - |
|
| 734 | - } |
|
| 735 | - |
|
| 736 | - /** |
|
| 737 | - * This method is triggered before a file gets updated with new content. |
|
| 738 | - * |
|
| 739 | - * This plugin uses this method to ensure that CalDAV objects receive |
|
| 740 | - * valid calendar data. |
|
| 741 | - * |
|
| 742 | - * @param string $path |
|
| 743 | - * @param DAV\IFile $node |
|
| 744 | - * @param resource $data |
|
| 745 | - * @param bool $modified Should be set to true, if this event handler |
|
| 746 | - * changed &$data. |
|
| 747 | - * @return void |
|
| 748 | - */ |
|
| 749 | - public function beforeWriteContent($path, DAV\IFile $node, &$data, &$modified) { |
|
| 750 | - |
|
| 751 | - if (!$node instanceof ICalendarObject) |
|
| 752 | - return; |
|
| 753 | - |
|
| 754 | - // We're onyl interested in ICalendarObject nodes that are inside of a |
|
| 755 | - // real calendar. This is to avoid triggering validation and scheduling |
|
| 756 | - // for non-calendars (such as an inbox). |
|
| 757 | - list($parent) = Uri\split($path); |
|
| 758 | - $parentNode = $this->server->tree->getNodeForPath($parent); |
|
| 759 | - |
|
| 760 | - if (!$parentNode instanceof ICalendar) |
|
| 761 | - return; |
|
| 762 | - |
|
| 763 | - $this->validateICalendar( |
|
| 764 | - $data, |
|
| 765 | - $path, |
|
| 766 | - $modified, |
|
| 767 | - $this->server->httpRequest, |
|
| 768 | - $this->server->httpResponse, |
|
| 769 | - false |
|
| 770 | - ); |
|
| 771 | - |
|
| 772 | - } |
|
| 773 | - |
|
| 774 | - /** |
|
| 775 | - * This method is triggered before a new file is created. |
|
| 776 | - * |
|
| 777 | - * This plugin uses this method to ensure that newly created calendar |
|
| 778 | - * objects contain valid calendar data. |
|
| 779 | - * |
|
| 780 | - * @param string $path |
|
| 781 | - * @param resource $data |
|
| 782 | - * @param DAV\ICollection $parentNode |
|
| 783 | - * @param bool $modified Should be set to true, if this event handler |
|
| 784 | - * changed &$data. |
|
| 785 | - * @return void |
|
| 786 | - */ |
|
| 787 | - public function beforeCreateFile($path, &$data, DAV\ICollection $parentNode, &$modified) { |
|
| 788 | - |
|
| 789 | - if (!$parentNode instanceof ICalendar) |
|
| 790 | - return; |
|
| 791 | - |
|
| 792 | - $this->validateICalendar( |
|
| 793 | - $data, |
|
| 794 | - $path, |
|
| 795 | - $modified, |
|
| 796 | - $this->server->httpRequest, |
|
| 797 | - $this->server->httpResponse, |
|
| 798 | - true |
|
| 799 | - ); |
|
| 800 | - |
|
| 801 | - } |
|
| 802 | - |
|
| 803 | - /** |
|
| 804 | - * Checks if the submitted iCalendar data is in fact, valid. |
|
| 805 | - * |
|
| 806 | - * An exception is thrown if it's not. |
|
| 807 | - * |
|
| 808 | - * @param resource|string $data |
|
| 809 | - * @param string $path |
|
| 810 | - * @param bool $modified Should be set to true, if this event handler |
|
| 811 | - * changed &$data. |
|
| 812 | - * @param RequestInterface $request The http request. |
|
| 813 | - * @param ResponseInterface $response The http response. |
|
| 814 | - * @param bool $isNew Is the item a new one, or an update. |
|
| 815 | - * @return void |
|
| 816 | - */ |
|
| 817 | - protected function validateICalendar(&$data, $path, &$modified, RequestInterface $request, ResponseInterface $response, $isNew) { |
|
| 818 | - |
|
| 819 | - // If it's a stream, we convert it to a string first. |
|
| 820 | - if (is_resource($data)) { |
|
| 821 | - $data = stream_get_contents($data); |
|
| 822 | - } |
|
| 823 | - |
|
| 824 | - $before = md5($data); |
|
| 825 | - // Converting the data to unicode, if needed. |
|
| 826 | - $data = DAV\StringUtil::ensureUTF8($data); |
|
| 827 | - |
|
| 828 | - if ($before !== md5($data)) $modified = true; |
|
| 829 | - |
|
| 830 | - try { |
|
| 831 | - |
|
| 832 | - // If the data starts with a [, we can reasonably assume we're dealing |
|
| 833 | - // with a jCal object. |
|
| 834 | - if (substr($data, 0, 1) === '[') { |
|
| 835 | - $vobj = VObject\Reader::readJson($data); |
|
| 836 | - |
|
| 837 | - // Converting $data back to iCalendar, as that's what we |
|
| 838 | - // technically support everywhere. |
|
| 839 | - $data = $vobj->serialize(); |
|
| 840 | - $modified = true; |
|
| 841 | - } else { |
|
| 842 | - $vobj = VObject\Reader::read($data); |
|
| 843 | - } |
|
| 844 | - |
|
| 845 | - } catch (VObject\ParseException $e) { |
|
| 846 | - |
|
| 847 | - throw new DAV\Exception\UnsupportedMediaType('This resource only supports valid iCalendar 2.0 data. Parse error: ' . $e->getMessage()); |
|
| 848 | - |
|
| 849 | - } |
|
| 850 | - |
|
| 851 | - if ($vobj->name !== 'VCALENDAR') { |
|
| 852 | - throw new DAV\Exception\UnsupportedMediaType('This collection can only support iCalendar objects.'); |
|
| 853 | - } |
|
| 854 | - |
|
| 855 | - $sCCS = '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set'; |
|
| 856 | - |
|
| 857 | - // Get the Supported Components for the target calendar |
|
| 858 | - list($parentPath) = Uri\split($path); |
|
| 859 | - $calendarProperties = $this->server->getProperties($parentPath, [$sCCS]); |
|
| 860 | - |
|
| 861 | - if (isset($calendarProperties[$sCCS])) { |
|
| 862 | - $supportedComponents = $calendarProperties[$sCCS]->getValue(); |
|
| 863 | - } else { |
|
| 864 | - $supportedComponents = ['VJOURNAL', 'VTODO', 'VEVENT']; |
|
| 865 | - } |
|
| 866 | - |
|
| 867 | - $foundType = null; |
|
| 868 | - $foundUID = null; |
|
| 869 | - foreach ($vobj->getComponents() as $component) { |
|
| 870 | - switch ($component->name) { |
|
| 871 | - case 'VTIMEZONE' : |
|
| 872 | - continue 2; |
|
| 873 | - case 'VEVENT' : |
|
| 874 | - case 'VTODO' : |
|
| 875 | - case 'VJOURNAL' : |
|
| 876 | - if (is_null($foundType)) { |
|
| 877 | - $foundType = $component->name; |
|
| 878 | - if (!in_array($foundType, $supportedComponents)) { |
|
| 879 | - throw new Exception\InvalidComponentType('This calendar only supports ' . implode(', ', $supportedComponents) . '. We found a ' . $foundType); |
|
| 880 | - } |
|
| 881 | - if (!isset($component->UID)) { |
|
| 882 | - throw new DAV\Exception\BadRequest('Every ' . $component->name . ' component must have an UID'); |
|
| 883 | - } |
|
| 884 | - $foundUID = (string)$component->UID; |
|
| 885 | - } else { |
|
| 886 | - if ($foundType !== $component->name) { |
|
| 887 | - throw new DAV\Exception\BadRequest('A calendar object must only contain 1 component. We found a ' . $component->name . ' as well as a ' . $foundType); |
|
| 888 | - } |
|
| 889 | - if ($foundUID !== (string)$component->UID) { |
|
| 890 | - throw new DAV\Exception\BadRequest('Every ' . $component->name . ' in this object must have identical UIDs'); |
|
| 891 | - } |
|
| 892 | - } |
|
| 893 | - break; |
|
| 894 | - default : |
|
| 895 | - throw new DAV\Exception\BadRequest('You are not allowed to create components of type: ' . $component->name . ' here'); |
|
| 896 | - |
|
| 897 | - } |
|
| 898 | - } |
|
| 899 | - if (!$foundType) |
|
| 900 | - throw new DAV\Exception\BadRequest('iCalendar object must contain at least 1 of VEVENT, VTODO or VJOURNAL'); |
|
| 901 | - |
|
| 902 | - // We use an extra variable to allow event handles to tell us wether |
|
| 903 | - // the object was modified or not. |
|
| 904 | - // |
|
| 905 | - // This helps us determine if we need to re-serialize the object. |
|
| 906 | - $subModified = false; |
|
| 907 | - |
|
| 908 | - $this->server->emit( |
|
| 909 | - 'calendarObjectChange', |
|
| 910 | - [ |
|
| 911 | - $request, |
|
| 912 | - $response, |
|
| 913 | - $vobj, |
|
| 914 | - $parentPath, |
|
| 915 | - &$subModified, |
|
| 916 | - $isNew |
|
| 917 | - ] |
|
| 918 | - ); |
|
| 919 | - |
|
| 920 | - if ($subModified) { |
|
| 921 | - // An event handler told us that it modified the object. |
|
| 922 | - $data = $vobj->serialize(); |
|
| 923 | - |
|
| 924 | - // Using md5 to figure out if there was an *actual* change. |
|
| 925 | - if (!$modified && $before !== md5($data)) { |
|
| 926 | - $modified = true; |
|
| 927 | - } |
|
| 928 | - |
|
| 929 | - } |
|
| 930 | - |
|
| 931 | - // Destroy circular references so PHP will garbage collect the object. |
|
| 932 | - $vobj->destroy(); |
|
| 933 | - |
|
| 934 | - } |
|
| 935 | - |
|
| 936 | - |
|
| 937 | - /** |
|
| 938 | - * This method is used to generate HTML output for the |
|
| 939 | - * DAV\Browser\Plugin. This allows us to generate an interface users |
|
| 940 | - * can use to create new calendars. |
|
| 941 | - * |
|
| 942 | - * @param DAV\INode $node |
|
| 943 | - * @param string $output |
|
| 944 | - * @return bool |
|
| 945 | - */ |
|
| 946 | - public function htmlActionsPanel(DAV\INode $node, &$output) { |
|
| 947 | - |
|
| 948 | - if (!$node instanceof CalendarHome) |
|
| 949 | - return; |
|
| 950 | - |
|
| 951 | - $output .= '<tr><td colspan="2"><form method="post" action=""> |
|
| 29 | + /** |
|
| 30 | + * This is the official CalDAV namespace |
|
| 31 | + */ |
|
| 32 | + const NS_CALDAV = 'urn:ietf:params:xml:ns:caldav'; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * This is the namespace for the proprietary calendarserver extensions |
|
| 36 | + */ |
|
| 37 | + const NS_CALENDARSERVER = 'http://calendarserver.org/ns/'; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * The hardcoded root for calendar objects. It is unfortunate |
|
| 41 | + * that we're stuck with it, but it will have to do for now |
|
| 42 | + */ |
|
| 43 | + const CALENDAR_ROOT = 'calendars'; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Reference to server object |
|
| 47 | + * |
|
| 48 | + * @var DAV\Server |
|
| 49 | + */ |
|
| 50 | + protected $server; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * The default PDO storage uses a MySQL MEDIUMBLOB for iCalendar data, |
|
| 54 | + * which can hold up to 2^24 = 16777216 bytes. This is plenty. We're |
|
| 55 | + * capping it to 10M here. |
|
| 56 | + */ |
|
| 57 | + protected $maxResourceSize = 10000000; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Use this method to tell the server this plugin defines additional |
|
| 61 | + * HTTP methods. |
|
| 62 | + * |
|
| 63 | + * This method is passed a uri. It should only return HTTP methods that are |
|
| 64 | + * available for the specified uri. |
|
| 65 | + * |
|
| 66 | + * @param string $uri |
|
| 67 | + * @return array |
|
| 68 | + */ |
|
| 69 | + public function getHTTPMethods($uri) { |
|
| 70 | + |
|
| 71 | + // The MKCALENDAR is only available on unmapped uri's, whose |
|
| 72 | + // parents extend IExtendedCollection |
|
| 73 | + list($parent, $name) = Uri\split($uri); |
|
| 74 | + |
|
| 75 | + $node = $this->server->tree->getNodeForPath($parent); |
|
| 76 | + |
|
| 77 | + if ($node instanceof DAV\IExtendedCollection) { |
|
| 78 | + try { |
|
| 79 | + $node->getChild($name); |
|
| 80 | + } catch (DAV\Exception\NotFound $e) { |
|
| 81 | + return ['MKCALENDAR']; |
|
| 82 | + } |
|
| 83 | + } |
|
| 84 | + return []; |
|
| 85 | + |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * Returns the path to a principal's calendar home. |
|
| 90 | + * |
|
| 91 | + * The return url must not end with a slash. |
|
| 92 | + * This function should return null in case a principal did not have |
|
| 93 | + * a calendar home. |
|
| 94 | + * |
|
| 95 | + * @param string $principalUrl |
|
| 96 | + * @return string |
|
| 97 | + */ |
|
| 98 | + public function getCalendarHomeForPrincipal($principalUrl) { |
|
| 99 | + |
|
| 100 | + // The default behavior for most sabre/dav servers is that there is a |
|
| 101 | + // principals root node, which contains users directly under it. |
|
| 102 | + // |
|
| 103 | + // This function assumes that there are two components in a principal |
|
| 104 | + // path. If there's more, we don't return a calendar home. This |
|
| 105 | + // excludes things like the calendar-proxy-read principal (which it |
|
| 106 | + // should). |
|
| 107 | + $parts = explode('/', trim($principalUrl, '/')); |
|
| 108 | + if (count($parts) !== 2) return; |
|
| 109 | + if ($parts[0] !== 'principals') return; |
|
| 110 | + |
|
| 111 | + return self::CALENDAR_ROOT . '/' . $parts[1]; |
|
| 112 | + |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Returns a list of features for the DAV: HTTP header. |
|
| 117 | + * |
|
| 118 | + * @return array |
|
| 119 | + */ |
|
| 120 | + public function getFeatures() { |
|
| 121 | + |
|
| 122 | + return ['calendar-access', 'calendar-proxy']; |
|
| 123 | + |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Returns a plugin name. |
|
| 128 | + * |
|
| 129 | + * Using this name other plugins will be able to access other plugins |
|
| 130 | + * using DAV\Server::getPlugin |
|
| 131 | + * |
|
| 132 | + * @return string |
|
| 133 | + */ |
|
| 134 | + public function getPluginName() { |
|
| 135 | + |
|
| 136 | + return 'caldav'; |
|
| 137 | + |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Returns a list of reports this plugin supports. |
|
| 142 | + * |
|
| 143 | + * This will be used in the {DAV:}supported-report-set property. |
|
| 144 | + * Note that you still need to subscribe to the 'report' event to actually |
|
| 145 | + * implement them |
|
| 146 | + * |
|
| 147 | + * @param string $uri |
|
| 148 | + * @return array |
|
| 149 | + */ |
|
| 150 | + public function getSupportedReportSet($uri) { |
|
| 151 | + |
|
| 152 | + $node = $this->server->tree->getNodeForPath($uri); |
|
| 153 | + |
|
| 154 | + $reports = []; |
|
| 155 | + if ($node instanceof ICalendarObjectContainer || $node instanceof ICalendarObject) { |
|
| 156 | + $reports[] = '{' . self::NS_CALDAV . '}calendar-multiget'; |
|
| 157 | + $reports[] = '{' . self::NS_CALDAV . '}calendar-query'; |
|
| 158 | + } |
|
| 159 | + if ($node instanceof ICalendar) { |
|
| 160 | + $reports[] = '{' . self::NS_CALDAV . '}free-busy-query'; |
|
| 161 | + } |
|
| 162 | + // iCal has a bug where it assumes that sync support is enabled, only |
|
| 163 | + // if we say we support it on the calendar-home, even though this is |
|
| 164 | + // not actually the case. |
|
| 165 | + if ($node instanceof CalendarHome && $this->server->getPlugin('sync')) { |
|
| 166 | + $reports[] = '{DAV:}sync-collection'; |
|
| 167 | + } |
|
| 168 | + return $reports; |
|
| 169 | + |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * Initializes the plugin |
|
| 174 | + * |
|
| 175 | + * @param DAV\Server $server |
|
| 176 | + * @return void |
|
| 177 | + */ |
|
| 178 | + public function initialize(DAV\Server $server) { |
|
| 179 | + |
|
| 180 | + $this->server = $server; |
|
| 181 | + |
|
| 182 | + $server->on('method:MKCALENDAR', [$this, 'httpMkCalendar']); |
|
| 183 | + $server->on('report', [$this, 'report']); |
|
| 184 | + $server->on('propFind', [$this, 'propFind']); |
|
| 185 | + $server->on('onHTMLActionsPanel', [$this, 'htmlActionsPanel']); |
|
| 186 | + $server->on('beforeCreateFile', [$this, 'beforeCreateFile']); |
|
| 187 | + $server->on('beforeWriteContent', [$this, 'beforeWriteContent']); |
|
| 188 | + $server->on('afterMethod:GET', [$this, 'httpAfterGET']); |
|
| 189 | + |
|
| 190 | + $server->xml->namespaceMap[self::NS_CALDAV] = 'cal'; |
|
| 191 | + $server->xml->namespaceMap[self::NS_CALENDARSERVER] = 'cs'; |
|
| 192 | + |
|
| 193 | + $server->xml->elementMap['{' . self::NS_CALDAV . '}supported-calendar-component-set'] = 'Sabre\\CalDAV\\Xml\\Property\\SupportedCalendarComponentSet'; |
|
| 194 | + $server->xml->elementMap['{' . self::NS_CALDAV . '}calendar-query'] = 'Sabre\\CalDAV\\Xml\\Request\\CalendarQueryReport'; |
|
| 195 | + $server->xml->elementMap['{' . self::NS_CALDAV . '}calendar-multiget'] = 'Sabre\\CalDAV\\Xml\\Request\\CalendarMultiGetReport'; |
|
| 196 | + $server->xml->elementMap['{' . self::NS_CALDAV . '}free-busy-query'] = 'Sabre\\CalDAV\\Xml\\Request\\FreeBusyQueryReport'; |
|
| 197 | + $server->xml->elementMap['{' . self::NS_CALDAV . '}mkcalendar'] = 'Sabre\\CalDAV\\Xml\\Request\\MkCalendar'; |
|
| 198 | + $server->xml->elementMap['{' . self::NS_CALDAV . '}schedule-calendar-transp'] = 'Sabre\\CalDAV\\Xml\\Property\\ScheduleCalendarTransp'; |
|
| 199 | + $server->xml->elementMap['{' . self::NS_CALDAV . '}supported-calendar-component-set'] = 'Sabre\\CalDAV\\Xml\\Property\\SupportedCalendarComponentSet'; |
|
| 200 | + |
|
| 201 | + $server->resourceTypeMapping['\\Sabre\\CalDAV\\ICalendar'] = '{urn:ietf:params:xml:ns:caldav}calendar'; |
|
| 202 | + |
|
| 203 | + $server->resourceTypeMapping['\\Sabre\\CalDAV\\Principal\\IProxyRead'] = '{http://calendarserver.org/ns/}calendar-proxy-read'; |
|
| 204 | + $server->resourceTypeMapping['\\Sabre\\CalDAV\\Principal\\IProxyWrite'] = '{http://calendarserver.org/ns/}calendar-proxy-write'; |
|
| 205 | + |
|
| 206 | + array_push($server->protectedProperties, |
|
| 207 | + |
|
| 208 | + '{' . self::NS_CALDAV . '}supported-calendar-component-set', |
|
| 209 | + '{' . self::NS_CALDAV . '}supported-calendar-data', |
|
| 210 | + '{' . self::NS_CALDAV . '}max-resource-size', |
|
| 211 | + '{' . self::NS_CALDAV . '}min-date-time', |
|
| 212 | + '{' . self::NS_CALDAV . '}max-date-time', |
|
| 213 | + '{' . self::NS_CALDAV . '}max-instances', |
|
| 214 | + '{' . self::NS_CALDAV . '}max-attendees-per-instance', |
|
| 215 | + '{' . self::NS_CALDAV . '}calendar-home-set', |
|
| 216 | + '{' . self::NS_CALDAV . '}supported-collation-set', |
|
| 217 | + '{' . self::NS_CALDAV . '}calendar-data', |
|
| 218 | + |
|
| 219 | + // CalendarServer extensions |
|
| 220 | + '{' . self::NS_CALENDARSERVER . '}getctag', |
|
| 221 | + '{' . self::NS_CALENDARSERVER . '}calendar-proxy-read-for', |
|
| 222 | + '{' . self::NS_CALENDARSERVER . '}calendar-proxy-write-for' |
|
| 223 | + |
|
| 224 | + ); |
|
| 225 | + |
|
| 226 | + if ($aclPlugin = $server->getPlugin('acl')) { |
|
| 227 | + $aclPlugin->principalSearchPropertySet['{' . self::NS_CALDAV . '}calendar-user-address-set'] = 'Calendar address'; |
|
| 228 | + } |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + /** |
|
| 232 | + * This functions handles REPORT requests specific to CalDAV |
|
| 233 | + * |
|
| 234 | + * @param string $reportName |
|
| 235 | + * @param mixed $report |
|
| 236 | + * @return bool |
|
| 237 | + */ |
|
| 238 | + public function report($reportName, $report) { |
|
| 239 | + |
|
| 240 | + switch ($reportName) { |
|
| 241 | + case '{' . self::NS_CALDAV . '}calendar-multiget' : |
|
| 242 | + $this->server->transactionType = 'report-calendar-multiget'; |
|
| 243 | + $this->calendarMultiGetReport($report); |
|
| 244 | + return false; |
|
| 245 | + case '{' . self::NS_CALDAV . '}calendar-query' : |
|
| 246 | + $this->server->transactionType = 'report-calendar-query'; |
|
| 247 | + $this->calendarQueryReport($report); |
|
| 248 | + return false; |
|
| 249 | + case '{' . self::NS_CALDAV . '}free-busy-query' : |
|
| 250 | + $this->server->transactionType = 'report-free-busy-query'; |
|
| 251 | + $this->freeBusyQueryReport($report); |
|
| 252 | + return false; |
|
| 253 | + |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + /** |
|
| 260 | + * This function handles the MKCALENDAR HTTP method, which creates |
|
| 261 | + * a new calendar. |
|
| 262 | + * |
|
| 263 | + * @param RequestInterface $request |
|
| 264 | + * @param ResponseInterface $response |
|
| 265 | + * @return bool |
|
| 266 | + */ |
|
| 267 | + public function httpMkCalendar(RequestInterface $request, ResponseInterface $response) { |
|
| 268 | + |
|
| 269 | + $body = $request->getBodyAsString(); |
|
| 270 | + $path = $request->getPath(); |
|
| 271 | + |
|
| 272 | + $properties = []; |
|
| 273 | + |
|
| 274 | + if ($body) { |
|
| 275 | + |
|
| 276 | + try { |
|
| 277 | + $mkcalendar = $this->server->xml->expect( |
|
| 278 | + '{urn:ietf:params:xml:ns:caldav}mkcalendar', |
|
| 279 | + $body |
|
| 280 | + ); |
|
| 281 | + } catch (\Sabre\Xml\ParseException $e) { |
|
| 282 | + throw new BadRequest($e->getMessage(), null, $e); |
|
| 283 | + } |
|
| 284 | + $properties = $mkcalendar->getProperties(); |
|
| 285 | + |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + // iCal abuses MKCALENDAR since iCal 10.9.2 to create server-stored |
|
| 289 | + // subscriptions. Before that it used MKCOL which was the correct way |
|
| 290 | + // to do this. |
|
| 291 | + // |
|
| 292 | + // If the body had a {DAV:}resourcetype, it means we stumbled upon this |
|
| 293 | + // request, and we simply use it instead of the pre-defined list. |
|
| 294 | + if (isset($properties['{DAV:}resourcetype'])) { |
|
| 295 | + $resourceType = $properties['{DAV:}resourcetype']->getValue(); |
|
| 296 | + } else { |
|
| 297 | + $resourceType = ['{DAV:}collection','{urn:ietf:params:xml:ns:caldav}calendar']; |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + $this->server->createCollection($path, new MkCol($resourceType, $properties)); |
|
| 301 | + |
|
| 302 | + $this->server->httpResponse->setStatus(201); |
|
| 303 | + $this->server->httpResponse->setHeader('Content-Length', 0); |
|
| 304 | + |
|
| 305 | + // This breaks the method chain. |
|
| 306 | + return false; |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * PropFind |
|
| 311 | + * |
|
| 312 | + * This method handler is invoked before any after properties for a |
|
| 313 | + * resource are fetched. This allows us to add in any CalDAV specific |
|
| 314 | + * properties. |
|
| 315 | + * |
|
| 316 | + * @param DAV\PropFind $propFind |
|
| 317 | + * @param DAV\INode $node |
|
| 318 | + * @return void |
|
| 319 | + */ |
|
| 320 | + public function propFind(DAV\PropFind $propFind, DAV\INode $node) { |
|
| 321 | + |
|
| 322 | + $ns = '{' . self::NS_CALDAV . '}'; |
|
| 323 | + |
|
| 324 | + if ($node instanceof ICalendarObjectContainer) { |
|
| 325 | + |
|
| 326 | + $propFind->handle($ns . 'max-resource-size', $this->maxResourceSize); |
|
| 327 | + $propFind->handle($ns . 'supported-calendar-data', function() { |
|
| 328 | + return new Xml\Property\SupportedCalendarData(); |
|
| 329 | + }); |
|
| 330 | + $propFind->handle($ns . 'supported-collation-set', function() { |
|
| 331 | + return new Xml\Property\SupportedCollationSet(); |
|
| 332 | + }); |
|
| 333 | + |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + if ($node instanceof DAVACL\IPrincipal) { |
|
| 337 | + |
|
| 338 | + $principalUrl = $node->getPrincipalUrl(); |
|
| 339 | + |
|
| 340 | + $propFind->handle('{' . self::NS_CALDAV . '}calendar-home-set', function() use ($principalUrl) { |
|
| 341 | + |
|
| 342 | + $calendarHomePath = $this->getCalendarHomeForPrincipal($principalUrl); |
|
| 343 | + if (is_null($calendarHomePath)) return null; |
|
| 344 | + return new Href($calendarHomePath . '/'); |
|
| 345 | + |
|
| 346 | + }); |
|
| 347 | + // The calendar-user-address-set property is basically mapped to |
|
| 348 | + // the {DAV:}alternate-URI-set property. |
|
| 349 | + $propFind->handle('{' . self::NS_CALDAV . '}calendar-user-address-set', function() use ($node) { |
|
| 350 | + $addresses = $node->getAlternateUriSet(); |
|
| 351 | + $addresses[] = $this->server->getBaseUri() . $node->getPrincipalUrl() . '/'; |
|
| 352 | + return new Href($addresses, false); |
|
| 353 | + }); |
|
| 354 | + // For some reason somebody thought it was a good idea to add |
|
| 355 | + // another one of these properties. We're supporting it too. |
|
| 356 | + $propFind->handle('{' . self::NS_CALENDARSERVER . '}email-address-set', function() use ($node) { |
|
| 357 | + $addresses = $node->getAlternateUriSet(); |
|
| 358 | + $emails = []; |
|
| 359 | + foreach ($addresses as $address) { |
|
| 360 | + if (substr($address, 0, 7) === 'mailto:') { |
|
| 361 | + $emails[] = substr($address, 7); |
|
| 362 | + } |
|
| 363 | + } |
|
| 364 | + return new Xml\Property\EmailAddressSet($emails); |
|
| 365 | + }); |
|
| 366 | + |
|
| 367 | + // These two properties are shortcuts for ical to easily find |
|
| 368 | + // other principals this principal has access to. |
|
| 369 | + $propRead = '{' . self::NS_CALENDARSERVER . '}calendar-proxy-read-for'; |
|
| 370 | + $propWrite = '{' . self::NS_CALENDARSERVER . '}calendar-proxy-write-for'; |
|
| 371 | + |
|
| 372 | + if ($propFind->getStatus($propRead) === 404 || $propFind->getStatus($propWrite) === 404) { |
|
| 373 | + |
|
| 374 | + $aclPlugin = $this->server->getPlugin('acl'); |
|
| 375 | + $membership = $aclPlugin->getPrincipalMembership($propFind->getPath()); |
|
| 376 | + $readList = []; |
|
| 377 | + $writeList = []; |
|
| 378 | + |
|
| 379 | + foreach ($membership as $group) { |
|
| 380 | + |
|
| 381 | + $groupNode = $this->server->tree->getNodeForPath($group); |
|
| 382 | + |
|
| 383 | + $listItem = Uri\split($group)[0] . '/'; |
|
| 384 | + |
|
| 385 | + // If the node is either ap proxy-read or proxy-write |
|
| 386 | + // group, we grab the parent principal and add it to the |
|
| 387 | + // list. |
|
| 388 | + if ($groupNode instanceof Principal\IProxyRead) { |
|
| 389 | + $readList[] = $listItem; |
|
| 390 | + } |
|
| 391 | + if ($groupNode instanceof Principal\IProxyWrite) { |
|
| 392 | + $writeList[] = $listItem; |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + $propFind->set($propRead, new Href($readList)); |
|
| 398 | + $propFind->set($propWrite, new Href($writeList)); |
|
| 399 | + |
|
| 400 | + } |
|
| 401 | + |
|
| 402 | + } // instanceof IPrincipal |
|
| 403 | + |
|
| 404 | + if ($node instanceof ICalendarObject) { |
|
| 405 | + |
|
| 406 | + // The calendar-data property is not supposed to be a 'real' |
|
| 407 | + // property, but in large chunks of the spec it does act as such. |
|
| 408 | + // Therefore we simply expose it as a property. |
|
| 409 | + $propFind->handle('{' . self::NS_CALDAV . '}calendar-data', function() use ($node) { |
|
| 410 | + $val = $node->get(); |
|
| 411 | + if (is_resource($val)) |
|
| 412 | + $val = stream_get_contents($val); |
|
| 413 | + |
|
| 414 | + // Taking out \r to not screw up the xml output |
|
| 415 | + return str_replace("\r", "", $val); |
|
| 416 | + |
|
| 417 | + }); |
|
| 418 | + |
|
| 419 | + } |
|
| 420 | + |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + /** |
|
| 424 | + * This function handles the calendar-multiget REPORT. |
|
| 425 | + * |
|
| 426 | + * This report is used by the client to fetch the content of a series |
|
| 427 | + * of urls. Effectively avoiding a lot of redundant requests. |
|
| 428 | + * |
|
| 429 | + * @param CalendarMultiGetReport $report |
|
| 430 | + * @return void |
|
| 431 | + */ |
|
| 432 | + public function calendarMultiGetReport($report) { |
|
| 433 | + |
|
| 434 | + $needsJson = $report->contentType === 'application/calendar+json'; |
|
| 435 | + |
|
| 436 | + $timeZones = []; |
|
| 437 | + $propertyList = []; |
|
| 438 | + |
|
| 439 | + $paths = array_map( |
|
| 440 | + [$this->server, 'calculateUri'], |
|
| 441 | + $report->hrefs |
|
| 442 | + ); |
|
| 443 | + |
|
| 444 | + foreach ($this->server->getPropertiesForMultiplePaths($paths, $report->properties) as $uri => $objProps) { |
|
| 445 | + |
|
| 446 | + if (($needsJson || $report->expand) && isset($objProps[200]['{' . self::NS_CALDAV . '}calendar-data'])) { |
|
| 447 | + $vObject = VObject\Reader::read($objProps[200]['{' . self::NS_CALDAV . '}calendar-data']); |
|
| 448 | + |
|
| 449 | + if ($report->expand) { |
|
| 450 | + // We're expanding, and for that we need to figure out the |
|
| 451 | + // calendar's timezone. |
|
| 452 | + list($calendarPath) = Uri\split($uri); |
|
| 453 | + if (!isset($timeZones[$calendarPath])) { |
|
| 454 | + // Checking the calendar-timezone property. |
|
| 455 | + $tzProp = '{' . self::NS_CALDAV . '}calendar-timezone'; |
|
| 456 | + $tzResult = $this->server->getProperties($calendarPath, [$tzProp]); |
|
| 457 | + if (isset($tzResult[$tzProp])) { |
|
| 458 | + // This property contains a VCALENDAR with a single |
|
| 459 | + // VTIMEZONE. |
|
| 460 | + $vtimezoneObj = VObject\Reader::read($tzResult[$tzProp]); |
|
| 461 | + $timeZone = $vtimezoneObj->VTIMEZONE->getTimeZone(); |
|
| 462 | + } else { |
|
| 463 | + // Defaulting to UTC. |
|
| 464 | + $timeZone = new DateTimeZone('UTC'); |
|
| 465 | + } |
|
| 466 | + $timeZones[$calendarPath] = $timeZone; |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + $vObject = $vObject->expand($report->expand['start'], $report->expand['end'], $timeZones[$calendarPath]); |
|
| 470 | + } |
|
| 471 | + if ($needsJson) { |
|
| 472 | + $objProps[200]['{' . self::NS_CALDAV . '}calendar-data'] = json_encode($vObject->jsonSerialize()); |
|
| 473 | + } else { |
|
| 474 | + $objProps[200]['{' . self::NS_CALDAV . '}calendar-data'] = $vObject->serialize(); |
|
| 475 | + } |
|
| 476 | + // Destroy circular references so PHP will garbage collect the |
|
| 477 | + // object. |
|
| 478 | + $vObject->destroy(); |
|
| 479 | + } |
|
| 480 | + |
|
| 481 | + $propertyList[] = $objProps; |
|
| 482 | + |
|
| 483 | + } |
|
| 484 | + |
|
| 485 | + $prefer = $this->server->getHTTPPrefer(); |
|
| 486 | + |
|
| 487 | + $this->server->httpResponse->setStatus(207); |
|
| 488 | + $this->server->httpResponse->setHeader('Content-Type', 'application/xml; charset=utf-8'); |
|
| 489 | + $this->server->httpResponse->setHeader('Vary', 'Brief,Prefer'); |
|
| 490 | + $this->server->httpResponse->setBody($this->server->generateMultiStatus($propertyList, $prefer['return'] === 'minimal')); |
|
| 491 | + |
|
| 492 | + } |
|
| 493 | + |
|
| 494 | + /** |
|
| 495 | + * This function handles the calendar-query REPORT |
|
| 496 | + * |
|
| 497 | + * This report is used by clients to request calendar objects based on |
|
| 498 | + * complex conditions. |
|
| 499 | + * |
|
| 500 | + * @param Xml\Request\CalendarQueryReport $report |
|
| 501 | + * @return void |
|
| 502 | + */ |
|
| 503 | + public function calendarQueryReport($report) { |
|
| 504 | + |
|
| 505 | + $path = $this->server->getRequestUri(); |
|
| 506 | + |
|
| 507 | + $needsJson = $report->contentType === 'application/calendar+json'; |
|
| 508 | + |
|
| 509 | + $node = $this->server->tree->getNodeForPath($this->server->getRequestUri()); |
|
| 510 | + $depth = $this->server->getHTTPDepth(0); |
|
| 511 | + |
|
| 512 | + // The default result is an empty array |
|
| 513 | + $result = []; |
|
| 514 | + |
|
| 515 | + $calendarTimeZone = null; |
|
| 516 | + if ($report->expand) { |
|
| 517 | + // We're expanding, and for that we need to figure out the |
|
| 518 | + // calendar's timezone. |
|
| 519 | + $tzProp = '{' . self::NS_CALDAV . '}calendar-timezone'; |
|
| 520 | + $tzResult = $this->server->getProperties($path, [$tzProp]); |
|
| 521 | + if (isset($tzResult[$tzProp])) { |
|
| 522 | + // This property contains a VCALENDAR with a single |
|
| 523 | + // VTIMEZONE. |
|
| 524 | + $vtimezoneObj = VObject\Reader::read($tzResult[$tzProp]); |
|
| 525 | + $calendarTimeZone = $vtimezoneObj->VTIMEZONE->getTimeZone(); |
|
| 526 | + |
|
| 527 | + // Destroy circular references so PHP will garbage collect the |
|
| 528 | + // object. |
|
| 529 | + $vtimezoneObj->destroy(); |
|
| 530 | + } else { |
|
| 531 | + // Defaulting to UTC. |
|
| 532 | + $calendarTimeZone = new DateTimeZone('UTC'); |
|
| 533 | + } |
|
| 534 | + } |
|
| 535 | + |
|
| 536 | + // The calendarobject was requested directly. In this case we handle |
|
| 537 | + // this locally. |
|
| 538 | + if ($depth == 0 && $node instanceof ICalendarObject) { |
|
| 539 | + |
|
| 540 | + $requestedCalendarData = true; |
|
| 541 | + $requestedProperties = $report->properties; |
|
| 542 | + |
|
| 543 | + if (!in_array('{urn:ietf:params:xml:ns:caldav}calendar-data', $requestedProperties)) { |
|
| 544 | + |
|
| 545 | + // We always retrieve calendar-data, as we need it for filtering. |
|
| 546 | + $requestedProperties[] = '{urn:ietf:params:xml:ns:caldav}calendar-data'; |
|
| 547 | + |
|
| 548 | + // If calendar-data wasn't explicitly requested, we need to remove |
|
| 549 | + // it after processing. |
|
| 550 | + $requestedCalendarData = false; |
|
| 551 | + } |
|
| 552 | + |
|
| 553 | + $properties = $this->server->getPropertiesForPath( |
|
| 554 | + $path, |
|
| 555 | + $requestedProperties, |
|
| 556 | + 0 |
|
| 557 | + ); |
|
| 558 | + |
|
| 559 | + // This array should have only 1 element, the first calendar |
|
| 560 | + // object. |
|
| 561 | + $properties = current($properties); |
|
| 562 | + |
|
| 563 | + // If there wasn't any calendar-data returned somehow, we ignore |
|
| 564 | + // this. |
|
| 565 | + if (isset($properties[200]['{urn:ietf:params:xml:ns:caldav}calendar-data'])) { |
|
| 566 | + |
|
| 567 | + $validator = new CalendarQueryValidator(); |
|
| 568 | + |
|
| 569 | + $vObject = VObject\Reader::read($properties[200]['{urn:ietf:params:xml:ns:caldav}calendar-data']); |
|
| 570 | + if ($validator->validate($vObject, $report->filters)) { |
|
| 571 | + |
|
| 572 | + // If the client didn't require the calendar-data property, |
|
| 573 | + // we won't give it back. |
|
| 574 | + if (!$requestedCalendarData) { |
|
| 575 | + unset($properties[200]['{urn:ietf:params:xml:ns:caldav}calendar-data']); |
|
| 576 | + } else { |
|
| 577 | + |
|
| 578 | + |
|
| 579 | + if ($report->expand) { |
|
| 580 | + $vObject = $vObject->expand($report->expand['start'], $report->expand['end'], $calendarTimeZone); |
|
| 581 | + } |
|
| 582 | + if ($needsJson) { |
|
| 583 | + $properties[200]['{' . self::NS_CALDAV . '}calendar-data'] = json_encode($vObject->jsonSerialize()); |
|
| 584 | + } elseif ($report->expand) { |
|
| 585 | + $properties[200]['{' . self::NS_CALDAV . '}calendar-data'] = $vObject->serialize(); |
|
| 586 | + } |
|
| 587 | + } |
|
| 588 | + |
|
| 589 | + $result = [$properties]; |
|
| 590 | + |
|
| 591 | + } |
|
| 592 | + // Destroy circular references so PHP will garbage collect the |
|
| 593 | + // object. |
|
| 594 | + $vObject->destroy(); |
|
| 595 | + |
|
| 596 | + } |
|
| 597 | + |
|
| 598 | + } |
|
| 599 | + |
|
| 600 | + if ($node instanceof ICalendarObjectContainer && $depth === 0) { |
|
| 601 | + |
|
| 602 | + if (strpos($this->server->httpRequest->getHeader('User-Agent'), 'MSFT-') === 0) { |
|
| 603 | + // Microsoft clients incorrectly supplied depth as 0, when it actually |
|
| 604 | + // should have set depth to 1. We're implementing a workaround here |
|
| 605 | + // to deal with this. |
|
| 606 | + // |
|
| 607 | + // This targets at least the following clients: |
|
| 608 | + // Windows 10 |
|
| 609 | + // Windows Phone 8, 10 |
|
| 610 | + $depth = 1; |
|
| 611 | + } else { |
|
| 612 | + throw new BadRequest('A calendar-query REPORT on a calendar with a Depth: 0 is undefined. Set Depth to 1'); |
|
| 613 | + } |
|
| 614 | + |
|
| 615 | + } |
|
| 616 | + |
|
| 617 | + // If we're dealing with a calendar, the calendar itself is responsible |
|
| 618 | + // for the calendar-query. |
|
| 619 | + if ($node instanceof ICalendarObjectContainer && $depth == 1) { |
|
| 620 | + |
|
| 621 | + $nodePaths = $node->calendarQuery($report->filters); |
|
| 622 | + |
|
| 623 | + foreach ($nodePaths as $path) { |
|
| 624 | + |
|
| 625 | + list($properties) = |
|
| 626 | + $this->server->getPropertiesForPath($this->server->getRequestUri() . '/' . $path, $report->properties); |
|
| 627 | + |
|
| 628 | + if (($needsJson || $report->expand)) { |
|
| 629 | + $vObject = VObject\Reader::read($properties[200]['{' . self::NS_CALDAV . '}calendar-data']); |
|
| 630 | + |
|
| 631 | + if ($report->expand) { |
|
| 632 | + $vObject = $vObject->expand($report->expand['start'], $report->expand['end'], $calendarTimeZone); |
|
| 633 | + } |
|
| 634 | + |
|
| 635 | + if ($needsJson) { |
|
| 636 | + $properties[200]['{' . self::NS_CALDAV . '}calendar-data'] = json_encode($vObject->jsonSerialize()); |
|
| 637 | + } else { |
|
| 638 | + $properties[200]['{' . self::NS_CALDAV . '}calendar-data'] = $vObject->serialize(); |
|
| 639 | + } |
|
| 640 | + |
|
| 641 | + // Destroy circular references so PHP will garbage collect the |
|
| 642 | + // object. |
|
| 643 | + $vObject->destroy(); |
|
| 644 | + } |
|
| 645 | + $result[] = $properties; |
|
| 646 | + |
|
| 647 | + } |
|
| 648 | + |
|
| 649 | + } |
|
| 650 | + |
|
| 651 | + $prefer = $this->server->getHTTPPrefer(); |
|
| 652 | + |
|
| 653 | + $this->server->httpResponse->setStatus(207); |
|
| 654 | + $this->server->httpResponse->setHeader('Content-Type', 'application/xml; charset=utf-8'); |
|
| 655 | + $this->server->httpResponse->setHeader('Vary', 'Brief,Prefer'); |
|
| 656 | + $this->server->httpResponse->setBody($this->server->generateMultiStatus($result, $prefer['return'] === 'minimal')); |
|
| 657 | + |
|
| 658 | + } |
|
| 659 | + |
|
| 660 | + /** |
|
| 661 | + * This method is responsible for parsing the request and generating the |
|
| 662 | + * response for the CALDAV:free-busy-query REPORT. |
|
| 663 | + * |
|
| 664 | + * @param Xml\Request\FreeBusyQueryReport $report |
|
| 665 | + * @return void |
|
| 666 | + */ |
|
| 667 | + protected function freeBusyQueryReport(Xml\Request\FreeBusyQueryReport $report) { |
|
| 668 | + |
|
| 669 | + $uri = $this->server->getRequestUri(); |
|
| 670 | + |
|
| 671 | + $acl = $this->server->getPlugin('acl'); |
|
| 672 | + if ($acl) { |
|
| 673 | + $acl->checkPrivileges($uri, '{' . self::NS_CALDAV . '}read-free-busy'); |
|
| 674 | + } |
|
| 675 | + |
|
| 676 | + $calendar = $this->server->tree->getNodeForPath($uri); |
|
| 677 | + if (!$calendar instanceof ICalendar) { |
|
| 678 | + throw new DAV\Exception\NotImplemented('The free-busy-query REPORT is only implemented on calendars'); |
|
| 679 | + } |
|
| 680 | + |
|
| 681 | + $tzProp = '{' . self::NS_CALDAV . '}calendar-timezone'; |
|
| 682 | + |
|
| 683 | + // Figuring out the default timezone for the calendar, for floating |
|
| 684 | + // times. |
|
| 685 | + $calendarProps = $this->server->getProperties($uri, [$tzProp]); |
|
| 686 | + |
|
| 687 | + if (isset($calendarProps[$tzProp])) { |
|
| 688 | + $vtimezoneObj = VObject\Reader::read($calendarProps[$tzProp]); |
|
| 689 | + $calendarTimeZone = $vtimezoneObj->VTIMEZONE->getTimeZone(); |
|
| 690 | + // Destroy circular references so PHP will garbage collect the object. |
|
| 691 | + $vtimezoneObj->destroy(); |
|
| 692 | + } else { |
|
| 693 | + $calendarTimeZone = new DateTimeZone('UTC'); |
|
| 694 | + } |
|
| 695 | + |
|
| 696 | + // Doing a calendar-query first, to make sure we get the most |
|
| 697 | + // performance. |
|
| 698 | + $urls = $calendar->calendarQuery([ |
|
| 699 | + 'name' => 'VCALENDAR', |
|
| 700 | + 'comp-filters' => [ |
|
| 701 | + [ |
|
| 702 | + 'name' => 'VEVENT', |
|
| 703 | + 'comp-filters' => [], |
|
| 704 | + 'prop-filters' => [], |
|
| 705 | + 'is-not-defined' => false, |
|
| 706 | + 'time-range' => [ |
|
| 707 | + 'start' => $report->start, |
|
| 708 | + 'end' => $report->end, |
|
| 709 | + ], |
|
| 710 | + ], |
|
| 711 | + ], |
|
| 712 | + 'prop-filters' => [], |
|
| 713 | + 'is-not-defined' => false, |
|
| 714 | + 'time-range' => null, |
|
| 715 | + ]); |
|
| 716 | + |
|
| 717 | + $objects = array_map(function($url) use ($calendar) { |
|
| 718 | + $obj = $calendar->getChild($url)->get(); |
|
| 719 | + return $obj; |
|
| 720 | + }, $urls); |
|
| 721 | + |
|
| 722 | + $generator = new VObject\FreeBusyGenerator(); |
|
| 723 | + $generator->setObjects($objects); |
|
| 724 | + $generator->setTimeRange($report->start, $report->end); |
|
| 725 | + $generator->setTimeZone($calendarTimeZone); |
|
| 726 | + $result = $generator->getResult(); |
|
| 727 | + $result = $result->serialize(); |
|
| 728 | + |
|
| 729 | + $this->server->httpResponse->setStatus(200); |
|
| 730 | + $this->server->httpResponse->setHeader('Content-Type', 'text/calendar'); |
|
| 731 | + $this->server->httpResponse->setHeader('Content-Length', strlen($result)); |
|
| 732 | + $this->server->httpResponse->setBody($result); |
|
| 733 | + |
|
| 734 | + } |
|
| 735 | + |
|
| 736 | + /** |
|
| 737 | + * This method is triggered before a file gets updated with new content. |
|
| 738 | + * |
|
| 739 | + * This plugin uses this method to ensure that CalDAV objects receive |
|
| 740 | + * valid calendar data. |
|
| 741 | + * |
|
| 742 | + * @param string $path |
|
| 743 | + * @param DAV\IFile $node |
|
| 744 | + * @param resource $data |
|
| 745 | + * @param bool $modified Should be set to true, if this event handler |
|
| 746 | + * changed &$data. |
|
| 747 | + * @return void |
|
| 748 | + */ |
|
| 749 | + public function beforeWriteContent($path, DAV\IFile $node, &$data, &$modified) { |
|
| 750 | + |
|
| 751 | + if (!$node instanceof ICalendarObject) |
|
| 752 | + return; |
|
| 753 | + |
|
| 754 | + // We're onyl interested in ICalendarObject nodes that are inside of a |
|
| 755 | + // real calendar. This is to avoid triggering validation and scheduling |
|
| 756 | + // for non-calendars (such as an inbox). |
|
| 757 | + list($parent) = Uri\split($path); |
|
| 758 | + $parentNode = $this->server->tree->getNodeForPath($parent); |
|
| 759 | + |
|
| 760 | + if (!$parentNode instanceof ICalendar) |
|
| 761 | + return; |
|
| 762 | + |
|
| 763 | + $this->validateICalendar( |
|
| 764 | + $data, |
|
| 765 | + $path, |
|
| 766 | + $modified, |
|
| 767 | + $this->server->httpRequest, |
|
| 768 | + $this->server->httpResponse, |
|
| 769 | + false |
|
| 770 | + ); |
|
| 771 | + |
|
| 772 | + } |
|
| 773 | + |
|
| 774 | + /** |
|
| 775 | + * This method is triggered before a new file is created. |
|
| 776 | + * |
|
| 777 | + * This plugin uses this method to ensure that newly created calendar |
|
| 778 | + * objects contain valid calendar data. |
|
| 779 | + * |
|
| 780 | + * @param string $path |
|
| 781 | + * @param resource $data |
|
| 782 | + * @param DAV\ICollection $parentNode |
|
| 783 | + * @param bool $modified Should be set to true, if this event handler |
|
| 784 | + * changed &$data. |
|
| 785 | + * @return void |
|
| 786 | + */ |
|
| 787 | + public function beforeCreateFile($path, &$data, DAV\ICollection $parentNode, &$modified) { |
|
| 788 | + |
|
| 789 | + if (!$parentNode instanceof ICalendar) |
|
| 790 | + return; |
|
| 791 | + |
|
| 792 | + $this->validateICalendar( |
|
| 793 | + $data, |
|
| 794 | + $path, |
|
| 795 | + $modified, |
|
| 796 | + $this->server->httpRequest, |
|
| 797 | + $this->server->httpResponse, |
|
| 798 | + true |
|
| 799 | + ); |
|
| 800 | + |
|
| 801 | + } |
|
| 802 | + |
|
| 803 | + /** |
|
| 804 | + * Checks if the submitted iCalendar data is in fact, valid. |
|
| 805 | + * |
|
| 806 | + * An exception is thrown if it's not. |
|
| 807 | + * |
|
| 808 | + * @param resource|string $data |
|
| 809 | + * @param string $path |
|
| 810 | + * @param bool $modified Should be set to true, if this event handler |
|
| 811 | + * changed &$data. |
|
| 812 | + * @param RequestInterface $request The http request. |
|
| 813 | + * @param ResponseInterface $response The http response. |
|
| 814 | + * @param bool $isNew Is the item a new one, or an update. |
|
| 815 | + * @return void |
|
| 816 | + */ |
|
| 817 | + protected function validateICalendar(&$data, $path, &$modified, RequestInterface $request, ResponseInterface $response, $isNew) { |
|
| 818 | + |
|
| 819 | + // If it's a stream, we convert it to a string first. |
|
| 820 | + if (is_resource($data)) { |
|
| 821 | + $data = stream_get_contents($data); |
|
| 822 | + } |
|
| 823 | + |
|
| 824 | + $before = md5($data); |
|
| 825 | + // Converting the data to unicode, if needed. |
|
| 826 | + $data = DAV\StringUtil::ensureUTF8($data); |
|
| 827 | + |
|
| 828 | + if ($before !== md5($data)) $modified = true; |
|
| 829 | + |
|
| 830 | + try { |
|
| 831 | + |
|
| 832 | + // If the data starts with a [, we can reasonably assume we're dealing |
|
| 833 | + // with a jCal object. |
|
| 834 | + if (substr($data, 0, 1) === '[') { |
|
| 835 | + $vobj = VObject\Reader::readJson($data); |
|
| 836 | + |
|
| 837 | + // Converting $data back to iCalendar, as that's what we |
|
| 838 | + // technically support everywhere. |
|
| 839 | + $data = $vobj->serialize(); |
|
| 840 | + $modified = true; |
|
| 841 | + } else { |
|
| 842 | + $vobj = VObject\Reader::read($data); |
|
| 843 | + } |
|
| 844 | + |
|
| 845 | + } catch (VObject\ParseException $e) { |
|
| 846 | + |
|
| 847 | + throw new DAV\Exception\UnsupportedMediaType('This resource only supports valid iCalendar 2.0 data. Parse error: ' . $e->getMessage()); |
|
| 848 | + |
|
| 849 | + } |
|
| 850 | + |
|
| 851 | + if ($vobj->name !== 'VCALENDAR') { |
|
| 852 | + throw new DAV\Exception\UnsupportedMediaType('This collection can only support iCalendar objects.'); |
|
| 853 | + } |
|
| 854 | + |
|
| 855 | + $sCCS = '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set'; |
|
| 856 | + |
|
| 857 | + // Get the Supported Components for the target calendar |
|
| 858 | + list($parentPath) = Uri\split($path); |
|
| 859 | + $calendarProperties = $this->server->getProperties($parentPath, [$sCCS]); |
|
| 860 | + |
|
| 861 | + if (isset($calendarProperties[$sCCS])) { |
|
| 862 | + $supportedComponents = $calendarProperties[$sCCS]->getValue(); |
|
| 863 | + } else { |
|
| 864 | + $supportedComponents = ['VJOURNAL', 'VTODO', 'VEVENT']; |
|
| 865 | + } |
|
| 866 | + |
|
| 867 | + $foundType = null; |
|
| 868 | + $foundUID = null; |
|
| 869 | + foreach ($vobj->getComponents() as $component) { |
|
| 870 | + switch ($component->name) { |
|
| 871 | + case 'VTIMEZONE' : |
|
| 872 | + continue 2; |
|
| 873 | + case 'VEVENT' : |
|
| 874 | + case 'VTODO' : |
|
| 875 | + case 'VJOURNAL' : |
|
| 876 | + if (is_null($foundType)) { |
|
| 877 | + $foundType = $component->name; |
|
| 878 | + if (!in_array($foundType, $supportedComponents)) { |
|
| 879 | + throw new Exception\InvalidComponentType('This calendar only supports ' . implode(', ', $supportedComponents) . '. We found a ' . $foundType); |
|
| 880 | + } |
|
| 881 | + if (!isset($component->UID)) { |
|
| 882 | + throw new DAV\Exception\BadRequest('Every ' . $component->name . ' component must have an UID'); |
|
| 883 | + } |
|
| 884 | + $foundUID = (string)$component->UID; |
|
| 885 | + } else { |
|
| 886 | + if ($foundType !== $component->name) { |
|
| 887 | + throw new DAV\Exception\BadRequest('A calendar object must only contain 1 component. We found a ' . $component->name . ' as well as a ' . $foundType); |
|
| 888 | + } |
|
| 889 | + if ($foundUID !== (string)$component->UID) { |
|
| 890 | + throw new DAV\Exception\BadRequest('Every ' . $component->name . ' in this object must have identical UIDs'); |
|
| 891 | + } |
|
| 892 | + } |
|
| 893 | + break; |
|
| 894 | + default : |
|
| 895 | + throw new DAV\Exception\BadRequest('You are not allowed to create components of type: ' . $component->name . ' here'); |
|
| 896 | + |
|
| 897 | + } |
|
| 898 | + } |
|
| 899 | + if (!$foundType) |
|
| 900 | + throw new DAV\Exception\BadRequest('iCalendar object must contain at least 1 of VEVENT, VTODO or VJOURNAL'); |
|
| 901 | + |
|
| 902 | + // We use an extra variable to allow event handles to tell us wether |
|
| 903 | + // the object was modified or not. |
|
| 904 | + // |
|
| 905 | + // This helps us determine if we need to re-serialize the object. |
|
| 906 | + $subModified = false; |
|
| 907 | + |
|
| 908 | + $this->server->emit( |
|
| 909 | + 'calendarObjectChange', |
|
| 910 | + [ |
|
| 911 | + $request, |
|
| 912 | + $response, |
|
| 913 | + $vobj, |
|
| 914 | + $parentPath, |
|
| 915 | + &$subModified, |
|
| 916 | + $isNew |
|
| 917 | + ] |
|
| 918 | + ); |
|
| 919 | + |
|
| 920 | + if ($subModified) { |
|
| 921 | + // An event handler told us that it modified the object. |
|
| 922 | + $data = $vobj->serialize(); |
|
| 923 | + |
|
| 924 | + // Using md5 to figure out if there was an *actual* change. |
|
| 925 | + if (!$modified && $before !== md5($data)) { |
|
| 926 | + $modified = true; |
|
| 927 | + } |
|
| 928 | + |
|
| 929 | + } |
|
| 930 | + |
|
| 931 | + // Destroy circular references so PHP will garbage collect the object. |
|
| 932 | + $vobj->destroy(); |
|
| 933 | + |
|
| 934 | + } |
|
| 935 | + |
|
| 936 | + |
|
| 937 | + /** |
|
| 938 | + * This method is used to generate HTML output for the |
|
| 939 | + * DAV\Browser\Plugin. This allows us to generate an interface users |
|
| 940 | + * can use to create new calendars. |
|
| 941 | + * |
|
| 942 | + * @param DAV\INode $node |
|
| 943 | + * @param string $output |
|
| 944 | + * @return bool |
|
| 945 | + */ |
|
| 946 | + public function htmlActionsPanel(DAV\INode $node, &$output) { |
|
| 947 | + |
|
| 948 | + if (!$node instanceof CalendarHome) |
|
| 949 | + return; |
|
| 950 | + |
|
| 951 | + $output .= '<tr><td colspan="2"><form method="post" action=""> |
|
| 952 | 952 | <h3>Create new calendar</h3> |
| 953 | 953 | <input type="hidden" name="sabreAction" value="mkcol" /> |
| 954 | 954 | <input type="hidden" name="resourceType" value="{DAV:}collection,{' . self::NS_CALDAV . '}calendar" /> |
@@ -958,68 +958,68 @@ discard block |
||
| 958 | 958 | </form> |
| 959 | 959 | </td></tr>'; |
| 960 | 960 | |
| 961 | - return false; |
|
| 962 | - |
|
| 963 | - } |
|
| 964 | - |
|
| 965 | - /** |
|
| 966 | - * This event is triggered after GET requests. |
|
| 967 | - * |
|
| 968 | - * This is used to transform data into jCal, if this was requested. |
|
| 969 | - * |
|
| 970 | - * @param RequestInterface $request |
|
| 971 | - * @param ResponseInterface $response |
|
| 972 | - * @return void |
|
| 973 | - */ |
|
| 974 | - public function httpAfterGet(RequestInterface $request, ResponseInterface $response) { |
|
| 975 | - |
|
| 976 | - if (strpos($response->getHeader('Content-Type'), 'text/calendar') === false) { |
|
| 977 | - return; |
|
| 978 | - } |
|
| 979 | - |
|
| 980 | - $result = HTTP\Util::negotiate( |
|
| 981 | - $request->getHeader('Accept'), |
|
| 982 | - ['text/calendar', 'application/calendar+json'] |
|
| 983 | - ); |
|
| 984 | - |
|
| 985 | - if ($result !== 'application/calendar+json') { |
|
| 986 | - // Do nothing |
|
| 987 | - return; |
|
| 988 | - } |
|
| 989 | - |
|
| 990 | - // Transforming. |
|
| 991 | - $vobj = VObject\Reader::read($response->getBody()); |
|
| 992 | - |
|
| 993 | - $jsonBody = json_encode($vobj->jsonSerialize()); |
|
| 994 | - $response->setBody($jsonBody); |
|
| 995 | - |
|
| 996 | - // Destroy circular references so PHP will garbage collect the object. |
|
| 997 | - $vobj->destroy(); |
|
| 998 | - |
|
| 999 | - $response->setHeader('Content-Type', 'application/calendar+json'); |
|
| 1000 | - $response->setHeader('Content-Length', strlen($jsonBody)); |
|
| 1001 | - |
|
| 1002 | - } |
|
| 1003 | - |
|
| 1004 | - /** |
|
| 1005 | - * Returns a bunch of meta-data about the plugin. |
|
| 1006 | - * |
|
| 1007 | - * Providing this information is optional, and is mainly displayed by the |
|
| 1008 | - * Browser plugin. |
|
| 1009 | - * |
|
| 1010 | - * The description key in the returned array may contain html and will not |
|
| 1011 | - * be sanitized. |
|
| 1012 | - * |
|
| 1013 | - * @return array |
|
| 1014 | - */ |
|
| 1015 | - public function getPluginInfo() { |
|
| 1016 | - |
|
| 1017 | - return [ |
|
| 1018 | - 'name' => $this->getPluginName(), |
|
| 1019 | - 'description' => 'Adds support for CalDAV (rfc4791)', |
|
| 1020 | - 'link' => 'http://sabre.io/dav/caldav/', |
|
| 1021 | - ]; |
|
| 1022 | - |
|
| 1023 | - } |
|
| 961 | + return false; |
|
| 962 | + |
|
| 963 | + } |
|
| 964 | + |
|
| 965 | + /** |
|
| 966 | + * This event is triggered after GET requests. |
|
| 967 | + * |
|
| 968 | + * This is used to transform data into jCal, if this was requested. |
|
| 969 | + * |
|
| 970 | + * @param RequestInterface $request |
|
| 971 | + * @param ResponseInterface $response |
|
| 972 | + * @return void |
|
| 973 | + */ |
|
| 974 | + public function httpAfterGet(RequestInterface $request, ResponseInterface $response) { |
|
| 975 | + |
|
| 976 | + if (strpos($response->getHeader('Content-Type'), 'text/calendar') === false) { |
|
| 977 | + return; |
|
| 978 | + } |
|
| 979 | + |
|
| 980 | + $result = HTTP\Util::negotiate( |
|
| 981 | + $request->getHeader('Accept'), |
|
| 982 | + ['text/calendar', 'application/calendar+json'] |
|
| 983 | + ); |
|
| 984 | + |
|
| 985 | + if ($result !== 'application/calendar+json') { |
|
| 986 | + // Do nothing |
|
| 987 | + return; |
|
| 988 | + } |
|
| 989 | + |
|
| 990 | + // Transforming. |
|
| 991 | + $vobj = VObject\Reader::read($response->getBody()); |
|
| 992 | + |
|
| 993 | + $jsonBody = json_encode($vobj->jsonSerialize()); |
|
| 994 | + $response->setBody($jsonBody); |
|
| 995 | + |
|
| 996 | + // Destroy circular references so PHP will garbage collect the object. |
|
| 997 | + $vobj->destroy(); |
|
| 998 | + |
|
| 999 | + $response->setHeader('Content-Type', 'application/calendar+json'); |
|
| 1000 | + $response->setHeader('Content-Length', strlen($jsonBody)); |
|
| 1001 | + |
|
| 1002 | + } |
|
| 1003 | + |
|
| 1004 | + /** |
|
| 1005 | + * Returns a bunch of meta-data about the plugin. |
|
| 1006 | + * |
|
| 1007 | + * Providing this information is optional, and is mainly displayed by the |
|
| 1008 | + * Browser plugin. |
|
| 1009 | + * |
|
| 1010 | + * The description key in the returned array may contain html and will not |
|
| 1011 | + * be sanitized. |
|
| 1012 | + * |
|
| 1013 | + * @return array |
|
| 1014 | + */ |
|
| 1015 | + public function getPluginInfo() { |
|
| 1016 | + |
|
| 1017 | + return [ |
|
| 1018 | + 'name' => $this->getPluginName(), |
|
| 1019 | + 'description' => 'Adds support for CalDAV (rfc4791)', |
|
| 1020 | + 'link' => 'http://sabre.io/dav/caldav/', |
|
| 1021 | + ]; |
|
| 1022 | + |
|
| 1023 | + } |
|
| 1024 | 1024 | |
| 1025 | 1025 | } |
@@ -105,8 +105,12 @@ discard block |
||
| 105 | 105 | // excludes things like the calendar-proxy-read principal (which it |
| 106 | 106 | // should). |
| 107 | 107 | $parts = explode('/', trim($principalUrl, '/')); |
| 108 | - if (count($parts) !== 2) return; |
|
| 109 | - if ($parts[0] !== 'principals') return; |
|
| 108 | + if (count($parts) !== 2) { |
|
| 109 | + return; |
|
| 110 | + } |
|
| 111 | + if ($parts[0] !== 'principals') { |
|
| 112 | + return; |
|
| 113 | + } |
|
| 110 | 114 | |
| 111 | 115 | return self::CALENDAR_ROOT . '/' . $parts[1]; |
| 112 | 116 | |
@@ -340,7 +344,9 @@ discard block |
||
| 340 | 344 | $propFind->handle('{' . self::NS_CALDAV . '}calendar-home-set', function() use ($principalUrl) { |
| 341 | 345 | |
| 342 | 346 | $calendarHomePath = $this->getCalendarHomeForPrincipal($principalUrl); |
| 343 | - if (is_null($calendarHomePath)) return null; |
|
| 347 | + if (is_null($calendarHomePath)) { |
|
| 348 | + return null; |
|
| 349 | + } |
|
| 344 | 350 | return new Href($calendarHomePath . '/'); |
| 345 | 351 | |
| 346 | 352 | }); |
@@ -408,8 +414,9 @@ discard block |
||
| 408 | 414 | // Therefore we simply expose it as a property. |
| 409 | 415 | $propFind->handle('{' . self::NS_CALDAV . '}calendar-data', function() use ($node) { |
| 410 | 416 | $val = $node->get(); |
| 411 | - if (is_resource($val)) |
|
| 412 | - $val = stream_get_contents($val); |
|
| 417 | + if (is_resource($val)) { |
|
| 418 | + $val = stream_get_contents($val); |
|
| 419 | + } |
|
| 413 | 420 | |
| 414 | 421 | // Taking out \r to not screw up the xml output |
| 415 | 422 | return str_replace("\r", "", $val); |
@@ -748,8 +755,9 @@ discard block |
||
| 748 | 755 | */ |
| 749 | 756 | public function beforeWriteContent($path, DAV\IFile $node, &$data, &$modified) { |
| 750 | 757 | |
| 751 | - if (!$node instanceof ICalendarObject) |
|
| 752 | - return; |
|
| 758 | + if (!$node instanceof ICalendarObject) { |
|
| 759 | + return; |
|
| 760 | + } |
|
| 753 | 761 | |
| 754 | 762 | // We're onyl interested in ICalendarObject nodes that are inside of a |
| 755 | 763 | // real calendar. This is to avoid triggering validation and scheduling |
@@ -757,8 +765,9 @@ discard block |
||
| 757 | 765 | list($parent) = Uri\split($path); |
| 758 | 766 | $parentNode = $this->server->tree->getNodeForPath($parent); |
| 759 | 767 | |
| 760 | - if (!$parentNode instanceof ICalendar) |
|
| 761 | - return; |
|
| 768 | + if (!$parentNode instanceof ICalendar) { |
|
| 769 | + return; |
|
| 770 | + } |
|
| 762 | 771 | |
| 763 | 772 | $this->validateICalendar( |
| 764 | 773 | $data, |
@@ -786,8 +795,9 @@ discard block |
||
| 786 | 795 | */ |
| 787 | 796 | public function beforeCreateFile($path, &$data, DAV\ICollection $parentNode, &$modified) { |
| 788 | 797 | |
| 789 | - if (!$parentNode instanceof ICalendar) |
|
| 790 | - return; |
|
| 798 | + if (!$parentNode instanceof ICalendar) { |
|
| 799 | + return; |
|
| 800 | + } |
|
| 791 | 801 | |
| 792 | 802 | $this->validateICalendar( |
| 793 | 803 | $data, |
@@ -825,7 +835,9 @@ discard block |
||
| 825 | 835 | // Converting the data to unicode, if needed. |
| 826 | 836 | $data = DAV\StringUtil::ensureUTF8($data); |
| 827 | 837 | |
| 828 | - if ($before !== md5($data)) $modified = true; |
|
| 838 | + if ($before !== md5($data)) { |
|
| 839 | + $modified = true; |
|
| 840 | + } |
|
| 829 | 841 | |
| 830 | 842 | try { |
| 831 | 843 | |
@@ -896,8 +908,9 @@ discard block |
||
| 896 | 908 | |
| 897 | 909 | } |
| 898 | 910 | } |
| 899 | - if (!$foundType) |
|
| 900 | - throw new DAV\Exception\BadRequest('iCalendar object must contain at least 1 of VEVENT, VTODO or VJOURNAL'); |
|
| 911 | + if (!$foundType) { |
|
| 912 | + throw new DAV\Exception\BadRequest('iCalendar object must contain at least 1 of VEVENT, VTODO or VJOURNAL'); |
|
| 913 | + } |
|
| 901 | 914 | |
| 902 | 915 | // We use an extra variable to allow event handles to tell us wether |
| 903 | 916 | // the object was modified or not. |
@@ -945,8 +958,9 @@ discard block |
||
| 945 | 958 | */ |
| 946 | 959 | public function htmlActionsPanel(DAV\INode $node, &$output) { |
| 947 | 960 | |
| 948 | - if (!$node instanceof CalendarHome) |
|
| 949 | - return; |
|
| 961 | + if (!$node instanceof CalendarHome) { |
|
| 962 | + return; |
|
| 963 | + } |
|
| 950 | 964 | |
| 951 | 965 | $output .= '<tr><td colspan="2"><form method="post" action=""> |
| 952 | 966 | <h3>Create new calendar</h3> |
@@ -301,7 +301,7 @@ discard block |
||
| 301 | 301 | if ($component->parent->name === 'VEVENT' && $component->parent->RRULE) { |
| 302 | 302 | |
| 303 | 303 | // Fire up the iterator! |
| 304 | - $it = new VObject\Recur\EventIterator($component->parent->parent, (string)$component->parent->UID); |
|
| 304 | + $it = new VObject\Recur\EventIterator($component->parent->parent, (string) $component->parent->UID); |
|
| 305 | 305 | while ($it->valid()) { |
| 306 | 306 | $expandedEvent = $it->getEventObject(); |
| 307 | 307 | |
@@ -317,7 +317,7 @@ discard block |
||
| 317 | 317 | return true; |
| 318 | 318 | } |
| 319 | 319 | |
| 320 | - if ((string)$expandedAlarm->TRIGGER['VALUE'] === 'DATE-TIME') { |
|
| 320 | + if ((string) $expandedAlarm->TRIGGER['VALUE'] === 'DATE-TIME') { |
|
| 321 | 321 | // This is an alarm with a non-relative trigger |
| 322 | 322 | // time, likely created by a buggy client. The |
| 323 | 323 | // implication is that every alarm in this |
@@ -20,356 +20,356 @@ |
||
| 20 | 20 | */ |
| 21 | 21 | class CalendarQueryValidator { |
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Verify if a list of filters applies to the calendar data object |
|
| 25 | - * |
|
| 26 | - * The list of filters must be formatted as parsed by \Sabre\CalDAV\CalendarQueryParser |
|
| 27 | - * |
|
| 28 | - * @param VObject\Component $vObject |
|
| 29 | - * @param array $filters |
|
| 30 | - * @return bool |
|
| 31 | - */ |
|
| 32 | - public function validate(VObject\Component\VCalendar $vObject, array $filters) { |
|
| 33 | - |
|
| 34 | - // The top level object is always a component filter. |
|
| 35 | - // We'll parse it manually, as it's pretty simple. |
|
| 36 | - if ($vObject->name !== $filters['name']) { |
|
| 37 | - return false; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - return |
|
| 41 | - $this->validateCompFilters($vObject, $filters['comp-filters']) && |
|
| 42 | - $this->validatePropFilters($vObject, $filters['prop-filters']); |
|
| 43 | - |
|
| 44 | - |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * This method checks the validity of comp-filters. |
|
| 49 | - * |
|
| 50 | - * A list of comp-filters needs to be specified. Also the parent of the |
|
| 51 | - * component we're checking should be specified, not the component to check |
|
| 52 | - * itself. |
|
| 53 | - * |
|
| 54 | - * @param VObject\Component $parent |
|
| 55 | - * @param array $filters |
|
| 56 | - * @return bool |
|
| 57 | - */ |
|
| 58 | - protected function validateCompFilters(VObject\Component $parent, array $filters) { |
|
| 59 | - |
|
| 60 | - foreach ($filters as $filter) { |
|
| 61 | - |
|
| 62 | - $isDefined = isset($parent->{$filter['name']}); |
|
| 63 | - |
|
| 64 | - if ($filter['is-not-defined']) { |
|
| 65 | - |
|
| 66 | - if ($isDefined) { |
|
| 67 | - return false; |
|
| 68 | - } else { |
|
| 69 | - continue; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - } |
|
| 73 | - if (!$isDefined) { |
|
| 74 | - return false; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - if ($filter['time-range']) { |
|
| 78 | - foreach ($parent->{$filter['name']} as $subComponent) { |
|
| 79 | - if ($this->validateTimeRange($subComponent, $filter['time-range']['start'], $filter['time-range']['end'])) { |
|
| 80 | - continue 2; |
|
| 81 | - } |
|
| 82 | - } |
|
| 83 | - return false; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - if (!$filter['comp-filters'] && !$filter['prop-filters']) { |
|
| 87 | - continue; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - // If there are sub-filters, we need to find at least one component |
|
| 91 | - // for which the subfilters hold true. |
|
| 92 | - foreach ($parent->{$filter['name']} as $subComponent) { |
|
| 93 | - |
|
| 94 | - if ( |
|
| 95 | - $this->validateCompFilters($subComponent, $filter['comp-filters']) && |
|
| 96 | - $this->validatePropFilters($subComponent, $filter['prop-filters'])) { |
|
| 97 | - // We had a match, so this comp-filter succeeds |
|
| 98 | - continue 2; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - // If we got here it means there were sub-comp-filters or |
|
| 104 | - // sub-prop-filters and there was no match. This means this filter |
|
| 105 | - // needs to return false. |
|
| 106 | - return false; |
|
| 107 | - |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - // If we got here it means we got through all comp-filters alive so the |
|
| 111 | - // filters were all true. |
|
| 112 | - return true; |
|
| 113 | - |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * This method checks the validity of prop-filters. |
|
| 118 | - * |
|
| 119 | - * A list of prop-filters needs to be specified. Also the parent of the |
|
| 120 | - * property we're checking should be specified, not the property to check |
|
| 121 | - * itself. |
|
| 122 | - * |
|
| 123 | - * @param VObject\Component $parent |
|
| 124 | - * @param array $filters |
|
| 125 | - * @return bool |
|
| 126 | - */ |
|
| 127 | - protected function validatePropFilters(VObject\Component $parent, array $filters) { |
|
| 128 | - |
|
| 129 | - foreach ($filters as $filter) { |
|
| 130 | - |
|
| 131 | - $isDefined = isset($parent->{$filter['name']}); |
|
| 132 | - |
|
| 133 | - if ($filter['is-not-defined']) { |
|
| 134 | - |
|
| 135 | - if ($isDefined) { |
|
| 136 | - return false; |
|
| 137 | - } else { |
|
| 138 | - continue; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - } |
|
| 142 | - if (!$isDefined) { |
|
| 143 | - return false; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - if ($filter['time-range']) { |
|
| 147 | - foreach ($parent->{$filter['name']} as $subComponent) { |
|
| 148 | - if ($this->validateTimeRange($subComponent, $filter['time-range']['start'], $filter['time-range']['end'])) { |
|
| 149 | - continue 2; |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - return false; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - if (!$filter['param-filters'] && !$filter['text-match']) { |
|
| 156 | - continue; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - // If there are sub-filters, we need to find at least one property |
|
| 160 | - // for which the subfilters hold true. |
|
| 161 | - foreach ($parent->{$filter['name']} as $subComponent) { |
|
| 162 | - |
|
| 163 | - if ( |
|
| 164 | - $this->validateParamFilters($subComponent, $filter['param-filters']) && |
|
| 165 | - (!$filter['text-match'] || $this->validateTextMatch($subComponent, $filter['text-match'])) |
|
| 166 | - ) { |
|
| 167 | - // We had a match, so this prop-filter succeeds |
|
| 168 | - continue 2; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - // If we got here it means there were sub-param-filters or |
|
| 174 | - // text-match filters and there was no match. This means the |
|
| 175 | - // filter needs to return false. |
|
| 176 | - return false; |
|
| 177 | - |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - // If we got here it means we got through all prop-filters alive so the |
|
| 181 | - // filters were all true. |
|
| 182 | - return true; |
|
| 183 | - |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - /** |
|
| 187 | - * This method checks the validity of param-filters. |
|
| 188 | - * |
|
| 189 | - * A list of param-filters needs to be specified. Also the parent of the |
|
| 190 | - * parameter we're checking should be specified, not the parameter to check |
|
| 191 | - * itself. |
|
| 192 | - * |
|
| 193 | - * @param VObject\Property $parent |
|
| 194 | - * @param array $filters |
|
| 195 | - * @return bool |
|
| 196 | - */ |
|
| 197 | - protected function validateParamFilters(VObject\Property $parent, array $filters) { |
|
| 198 | - |
|
| 199 | - foreach ($filters as $filter) { |
|
| 200 | - |
|
| 201 | - $isDefined = isset($parent[$filter['name']]); |
|
| 202 | - |
|
| 203 | - if ($filter['is-not-defined']) { |
|
| 204 | - |
|
| 205 | - if ($isDefined) { |
|
| 206 | - return false; |
|
| 207 | - } else { |
|
| 208 | - continue; |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - } |
|
| 212 | - if (!$isDefined) { |
|
| 213 | - return false; |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - if (!$filter['text-match']) { |
|
| 217 | - continue; |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - // If there are sub-filters, we need to find at least one parameter |
|
| 221 | - // for which the subfilters hold true. |
|
| 222 | - foreach ($parent[$filter['name']]->getParts() as $paramPart) { |
|
| 223 | - |
|
| 224 | - if ($this->validateTextMatch($paramPart, $filter['text-match'])) { |
|
| 225 | - // We had a match, so this param-filter succeeds |
|
| 226 | - continue 2; |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - // If we got here it means there was a text-match filter and there |
|
| 232 | - // were no matches. This means the filter needs to return false. |
|
| 233 | - return false; |
|
| 234 | - |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - // If we got here it means we got through all param-filters alive so the |
|
| 238 | - // filters were all true. |
|
| 239 | - return true; |
|
| 240 | - |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - /** |
|
| 244 | - * This method checks the validity of a text-match. |
|
| 245 | - * |
|
| 246 | - * A single text-match should be specified as well as the specific property |
|
| 247 | - * or parameter we need to validate. |
|
| 248 | - * |
|
| 249 | - * @param VObject\Node|string $check Value to check against. |
|
| 250 | - * @param array $textMatch |
|
| 251 | - * @return bool |
|
| 252 | - */ |
|
| 253 | - protected function validateTextMatch($check, array $textMatch) { |
|
| 254 | - |
|
| 255 | - if ($check instanceof VObject\Node) { |
|
| 256 | - $check = $check->getValue(); |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - $isMatching = \Sabre\DAV\StringUtil::textMatch($check, $textMatch['value'], $textMatch['collation']); |
|
| 260 | - |
|
| 261 | - return ($textMatch['negate-condition'] xor $isMatching); |
|
| 262 | - |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - /** |
|
| 266 | - * Validates if a component matches the given time range. |
|
| 267 | - * |
|
| 268 | - * This is all based on the rules specified in rfc4791, which are quite |
|
| 269 | - * complex. |
|
| 270 | - * |
|
| 271 | - * @param VObject\Node $component |
|
| 272 | - * @param DateTime $start |
|
| 273 | - * @param DateTime $end |
|
| 274 | - * @return bool |
|
| 275 | - */ |
|
| 276 | - protected function validateTimeRange(VObject\Node $component, $start, $end) { |
|
| 277 | - |
|
| 278 | - if (is_null($start)) { |
|
| 279 | - $start = new DateTime('1900-01-01'); |
|
| 280 | - } |
|
| 281 | - if (is_null($end)) { |
|
| 282 | - $end = new DateTime('3000-01-01'); |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - switch ($component->name) { |
|
| 286 | - |
|
| 287 | - case 'VEVENT' : |
|
| 288 | - case 'VTODO' : |
|
| 289 | - case 'VJOURNAL' : |
|
| 290 | - |
|
| 291 | - return $component->isInTimeRange($start, $end); |
|
| 292 | - |
|
| 293 | - case 'VALARM' : |
|
| 294 | - |
|
| 295 | - // If the valarm is wrapped in a recurring event, we need to |
|
| 296 | - // expand the recursions, and validate each. |
|
| 297 | - // |
|
| 298 | - // Our datamodel doesn't easily allow us to do this straight |
|
| 299 | - // in the VALARM component code, so this is a hack, and an |
|
| 300 | - // expensive one too. |
|
| 301 | - if ($component->parent->name === 'VEVENT' && $component->parent->RRULE) { |
|
| 302 | - |
|
| 303 | - // Fire up the iterator! |
|
| 304 | - $it = new VObject\Recur\EventIterator($component->parent->parent, (string)$component->parent->UID); |
|
| 305 | - while ($it->valid()) { |
|
| 306 | - $expandedEvent = $it->getEventObject(); |
|
| 307 | - |
|
| 308 | - // We need to check from these expanded alarms, which |
|
| 309 | - // one is the first to trigger. Based on this, we can |
|
| 310 | - // determine if we can 'give up' expanding events. |
|
| 311 | - $firstAlarm = null; |
|
| 312 | - if ($expandedEvent->VALARM !== null) { |
|
| 313 | - foreach ($expandedEvent->VALARM as $expandedAlarm) { |
|
| 314 | - |
|
| 315 | - $effectiveTrigger = $expandedAlarm->getEffectiveTriggerTime(); |
|
| 316 | - if ($expandedAlarm->isInTimeRange($start, $end)) { |
|
| 317 | - return true; |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - if ((string)$expandedAlarm->TRIGGER['VALUE'] === 'DATE-TIME') { |
|
| 321 | - // This is an alarm with a non-relative trigger |
|
| 322 | - // time, likely created by a buggy client. The |
|
| 323 | - // implication is that every alarm in this |
|
| 324 | - // recurring event trigger at the exact same |
|
| 325 | - // time. It doesn't make sense to traverse |
|
| 326 | - // further. |
|
| 327 | - } else { |
|
| 328 | - // We store the first alarm as a means to |
|
| 329 | - // figure out when we can stop traversing. |
|
| 330 | - if (!$firstAlarm || $effectiveTrigger < $firstAlarm) { |
|
| 331 | - $firstAlarm = $effectiveTrigger; |
|
| 332 | - } |
|
| 333 | - } |
|
| 334 | - } |
|
| 335 | - } |
|
| 336 | - if (is_null($firstAlarm)) { |
|
| 337 | - // No alarm was found. |
|
| 338 | - // |
|
| 339 | - // Or technically: No alarm that will change for |
|
| 340 | - // every instance of the recurrence was found, |
|
| 341 | - // which means we can assume there was no match. |
|
| 342 | - return false; |
|
| 343 | - } |
|
| 344 | - if ($firstAlarm > $end) { |
|
| 345 | - return false; |
|
| 346 | - } |
|
| 347 | - $it->next(); |
|
| 348 | - } |
|
| 349 | - return false; |
|
| 350 | - } else { |
|
| 351 | - return $component->isInTimeRange($start, $end); |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - case 'VFREEBUSY' : |
|
| 355 | - throw new \Sabre\DAV\Exception\NotImplemented('time-range filters are currently not supported on ' . $component->name . ' components'); |
|
| 356 | - |
|
| 357 | - case 'COMPLETED' : |
|
| 358 | - case 'CREATED' : |
|
| 359 | - case 'DTEND' : |
|
| 360 | - case 'DTSTAMP' : |
|
| 361 | - case 'DTSTART' : |
|
| 362 | - case 'DUE' : |
|
| 363 | - case 'LAST-MODIFIED' : |
|
| 364 | - return ($start <= $component->getDateTime() && $end >= $component->getDateTime()); |
|
| 365 | - |
|
| 366 | - |
|
| 367 | - |
|
| 368 | - default : |
|
| 369 | - throw new \Sabre\DAV\Exception\BadRequest('You cannot create a time-range filter on a ' . $component->name . ' component'); |
|
| 370 | - |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - } |
|
| 23 | + /** |
|
| 24 | + * Verify if a list of filters applies to the calendar data object |
|
| 25 | + * |
|
| 26 | + * The list of filters must be formatted as parsed by \Sabre\CalDAV\CalendarQueryParser |
|
| 27 | + * |
|
| 28 | + * @param VObject\Component $vObject |
|
| 29 | + * @param array $filters |
|
| 30 | + * @return bool |
|
| 31 | + */ |
|
| 32 | + public function validate(VObject\Component\VCalendar $vObject, array $filters) { |
|
| 33 | + |
|
| 34 | + // The top level object is always a component filter. |
|
| 35 | + // We'll parse it manually, as it's pretty simple. |
|
| 36 | + if ($vObject->name !== $filters['name']) { |
|
| 37 | + return false; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + return |
|
| 41 | + $this->validateCompFilters($vObject, $filters['comp-filters']) && |
|
| 42 | + $this->validatePropFilters($vObject, $filters['prop-filters']); |
|
| 43 | + |
|
| 44 | + |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * This method checks the validity of comp-filters. |
|
| 49 | + * |
|
| 50 | + * A list of comp-filters needs to be specified. Also the parent of the |
|
| 51 | + * component we're checking should be specified, not the component to check |
|
| 52 | + * itself. |
|
| 53 | + * |
|
| 54 | + * @param VObject\Component $parent |
|
| 55 | + * @param array $filters |
|
| 56 | + * @return bool |
|
| 57 | + */ |
|
| 58 | + protected function validateCompFilters(VObject\Component $parent, array $filters) { |
|
| 59 | + |
|
| 60 | + foreach ($filters as $filter) { |
|
| 61 | + |
|
| 62 | + $isDefined = isset($parent->{$filter['name']}); |
|
| 63 | + |
|
| 64 | + if ($filter['is-not-defined']) { |
|
| 65 | + |
|
| 66 | + if ($isDefined) { |
|
| 67 | + return false; |
|
| 68 | + } else { |
|
| 69 | + continue; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + } |
|
| 73 | + if (!$isDefined) { |
|
| 74 | + return false; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + if ($filter['time-range']) { |
|
| 78 | + foreach ($parent->{$filter['name']} as $subComponent) { |
|
| 79 | + if ($this->validateTimeRange($subComponent, $filter['time-range']['start'], $filter['time-range']['end'])) { |
|
| 80 | + continue 2; |
|
| 81 | + } |
|
| 82 | + } |
|
| 83 | + return false; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + if (!$filter['comp-filters'] && !$filter['prop-filters']) { |
|
| 87 | + continue; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + // If there are sub-filters, we need to find at least one component |
|
| 91 | + // for which the subfilters hold true. |
|
| 92 | + foreach ($parent->{$filter['name']} as $subComponent) { |
|
| 93 | + |
|
| 94 | + if ( |
|
| 95 | + $this->validateCompFilters($subComponent, $filter['comp-filters']) && |
|
| 96 | + $this->validatePropFilters($subComponent, $filter['prop-filters'])) { |
|
| 97 | + // We had a match, so this comp-filter succeeds |
|
| 98 | + continue 2; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + // If we got here it means there were sub-comp-filters or |
|
| 104 | + // sub-prop-filters and there was no match. This means this filter |
|
| 105 | + // needs to return false. |
|
| 106 | + return false; |
|
| 107 | + |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + // If we got here it means we got through all comp-filters alive so the |
|
| 111 | + // filters were all true. |
|
| 112 | + return true; |
|
| 113 | + |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * This method checks the validity of prop-filters. |
|
| 118 | + * |
|
| 119 | + * A list of prop-filters needs to be specified. Also the parent of the |
|
| 120 | + * property we're checking should be specified, not the property to check |
|
| 121 | + * itself. |
|
| 122 | + * |
|
| 123 | + * @param VObject\Component $parent |
|
| 124 | + * @param array $filters |
|
| 125 | + * @return bool |
|
| 126 | + */ |
|
| 127 | + protected function validatePropFilters(VObject\Component $parent, array $filters) { |
|
| 128 | + |
|
| 129 | + foreach ($filters as $filter) { |
|
| 130 | + |
|
| 131 | + $isDefined = isset($parent->{$filter['name']}); |
|
| 132 | + |
|
| 133 | + if ($filter['is-not-defined']) { |
|
| 134 | + |
|
| 135 | + if ($isDefined) { |
|
| 136 | + return false; |
|
| 137 | + } else { |
|
| 138 | + continue; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + } |
|
| 142 | + if (!$isDefined) { |
|
| 143 | + return false; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + if ($filter['time-range']) { |
|
| 147 | + foreach ($parent->{$filter['name']} as $subComponent) { |
|
| 148 | + if ($this->validateTimeRange($subComponent, $filter['time-range']['start'], $filter['time-range']['end'])) { |
|
| 149 | + continue 2; |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + return false; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + if (!$filter['param-filters'] && !$filter['text-match']) { |
|
| 156 | + continue; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + // If there are sub-filters, we need to find at least one property |
|
| 160 | + // for which the subfilters hold true. |
|
| 161 | + foreach ($parent->{$filter['name']} as $subComponent) { |
|
| 162 | + |
|
| 163 | + if ( |
|
| 164 | + $this->validateParamFilters($subComponent, $filter['param-filters']) && |
|
| 165 | + (!$filter['text-match'] || $this->validateTextMatch($subComponent, $filter['text-match'])) |
|
| 166 | + ) { |
|
| 167 | + // We had a match, so this prop-filter succeeds |
|
| 168 | + continue 2; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + // If we got here it means there were sub-param-filters or |
|
| 174 | + // text-match filters and there was no match. This means the |
|
| 175 | + // filter needs to return false. |
|
| 176 | + return false; |
|
| 177 | + |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + // If we got here it means we got through all prop-filters alive so the |
|
| 181 | + // filters were all true. |
|
| 182 | + return true; |
|
| 183 | + |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + /** |
|
| 187 | + * This method checks the validity of param-filters. |
|
| 188 | + * |
|
| 189 | + * A list of param-filters needs to be specified. Also the parent of the |
|
| 190 | + * parameter we're checking should be specified, not the parameter to check |
|
| 191 | + * itself. |
|
| 192 | + * |
|
| 193 | + * @param VObject\Property $parent |
|
| 194 | + * @param array $filters |
|
| 195 | + * @return bool |
|
| 196 | + */ |
|
| 197 | + protected function validateParamFilters(VObject\Property $parent, array $filters) { |
|
| 198 | + |
|
| 199 | + foreach ($filters as $filter) { |
|
| 200 | + |
|
| 201 | + $isDefined = isset($parent[$filter['name']]); |
|
| 202 | + |
|
| 203 | + if ($filter['is-not-defined']) { |
|
| 204 | + |
|
| 205 | + if ($isDefined) { |
|
| 206 | + return false; |
|
| 207 | + } else { |
|
| 208 | + continue; |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + } |
|
| 212 | + if (!$isDefined) { |
|
| 213 | + return false; |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + if (!$filter['text-match']) { |
|
| 217 | + continue; |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + // If there are sub-filters, we need to find at least one parameter |
|
| 221 | + // for which the subfilters hold true. |
|
| 222 | + foreach ($parent[$filter['name']]->getParts() as $paramPart) { |
|
| 223 | + |
|
| 224 | + if ($this->validateTextMatch($paramPart, $filter['text-match'])) { |
|
| 225 | + // We had a match, so this param-filter succeeds |
|
| 226 | + continue 2; |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + // If we got here it means there was a text-match filter and there |
|
| 232 | + // were no matches. This means the filter needs to return false. |
|
| 233 | + return false; |
|
| 234 | + |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + // If we got here it means we got through all param-filters alive so the |
|
| 238 | + // filters were all true. |
|
| 239 | + return true; |
|
| 240 | + |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * This method checks the validity of a text-match. |
|
| 245 | + * |
|
| 246 | + * A single text-match should be specified as well as the specific property |
|
| 247 | + * or parameter we need to validate. |
|
| 248 | + * |
|
| 249 | + * @param VObject\Node|string $check Value to check against. |
|
| 250 | + * @param array $textMatch |
|
| 251 | + * @return bool |
|
| 252 | + */ |
|
| 253 | + protected function validateTextMatch($check, array $textMatch) { |
|
| 254 | + |
|
| 255 | + if ($check instanceof VObject\Node) { |
|
| 256 | + $check = $check->getValue(); |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + $isMatching = \Sabre\DAV\StringUtil::textMatch($check, $textMatch['value'], $textMatch['collation']); |
|
| 260 | + |
|
| 261 | + return ($textMatch['negate-condition'] xor $isMatching); |
|
| 262 | + |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + /** |
|
| 266 | + * Validates if a component matches the given time range. |
|
| 267 | + * |
|
| 268 | + * This is all based on the rules specified in rfc4791, which are quite |
|
| 269 | + * complex. |
|
| 270 | + * |
|
| 271 | + * @param VObject\Node $component |
|
| 272 | + * @param DateTime $start |
|
| 273 | + * @param DateTime $end |
|
| 274 | + * @return bool |
|
| 275 | + */ |
|
| 276 | + protected function validateTimeRange(VObject\Node $component, $start, $end) { |
|
| 277 | + |
|
| 278 | + if (is_null($start)) { |
|
| 279 | + $start = new DateTime('1900-01-01'); |
|
| 280 | + } |
|
| 281 | + if (is_null($end)) { |
|
| 282 | + $end = new DateTime('3000-01-01'); |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + switch ($component->name) { |
|
| 286 | + |
|
| 287 | + case 'VEVENT' : |
|
| 288 | + case 'VTODO' : |
|
| 289 | + case 'VJOURNAL' : |
|
| 290 | + |
|
| 291 | + return $component->isInTimeRange($start, $end); |
|
| 292 | + |
|
| 293 | + case 'VALARM' : |
|
| 294 | + |
|
| 295 | + // If the valarm is wrapped in a recurring event, we need to |
|
| 296 | + // expand the recursions, and validate each. |
|
| 297 | + // |
|
| 298 | + // Our datamodel doesn't easily allow us to do this straight |
|
| 299 | + // in the VALARM component code, so this is a hack, and an |
|
| 300 | + // expensive one too. |
|
| 301 | + if ($component->parent->name === 'VEVENT' && $component->parent->RRULE) { |
|
| 302 | + |
|
| 303 | + // Fire up the iterator! |
|
| 304 | + $it = new VObject\Recur\EventIterator($component->parent->parent, (string)$component->parent->UID); |
|
| 305 | + while ($it->valid()) { |
|
| 306 | + $expandedEvent = $it->getEventObject(); |
|
| 307 | + |
|
| 308 | + // We need to check from these expanded alarms, which |
|
| 309 | + // one is the first to trigger. Based on this, we can |
|
| 310 | + // determine if we can 'give up' expanding events. |
|
| 311 | + $firstAlarm = null; |
|
| 312 | + if ($expandedEvent->VALARM !== null) { |
|
| 313 | + foreach ($expandedEvent->VALARM as $expandedAlarm) { |
|
| 314 | + |
|
| 315 | + $effectiveTrigger = $expandedAlarm->getEffectiveTriggerTime(); |
|
| 316 | + if ($expandedAlarm->isInTimeRange($start, $end)) { |
|
| 317 | + return true; |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + if ((string)$expandedAlarm->TRIGGER['VALUE'] === 'DATE-TIME') { |
|
| 321 | + // This is an alarm with a non-relative trigger |
|
| 322 | + // time, likely created by a buggy client. The |
|
| 323 | + // implication is that every alarm in this |
|
| 324 | + // recurring event trigger at the exact same |
|
| 325 | + // time. It doesn't make sense to traverse |
|
| 326 | + // further. |
|
| 327 | + } else { |
|
| 328 | + // We store the first alarm as a means to |
|
| 329 | + // figure out when we can stop traversing. |
|
| 330 | + if (!$firstAlarm || $effectiveTrigger < $firstAlarm) { |
|
| 331 | + $firstAlarm = $effectiveTrigger; |
|
| 332 | + } |
|
| 333 | + } |
|
| 334 | + } |
|
| 335 | + } |
|
| 336 | + if (is_null($firstAlarm)) { |
|
| 337 | + // No alarm was found. |
|
| 338 | + // |
|
| 339 | + // Or technically: No alarm that will change for |
|
| 340 | + // every instance of the recurrence was found, |
|
| 341 | + // which means we can assume there was no match. |
|
| 342 | + return false; |
|
| 343 | + } |
|
| 344 | + if ($firstAlarm > $end) { |
|
| 345 | + return false; |
|
| 346 | + } |
|
| 347 | + $it->next(); |
|
| 348 | + } |
|
| 349 | + return false; |
|
| 350 | + } else { |
|
| 351 | + return $component->isInTimeRange($start, $end); |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + case 'VFREEBUSY' : |
|
| 355 | + throw new \Sabre\DAV\Exception\NotImplemented('time-range filters are currently not supported on ' . $component->name . ' components'); |
|
| 356 | + |
|
| 357 | + case 'COMPLETED' : |
|
| 358 | + case 'CREATED' : |
|
| 359 | + case 'DTEND' : |
|
| 360 | + case 'DTSTAMP' : |
|
| 361 | + case 'DTSTART' : |
|
| 362 | + case 'DUE' : |
|
| 363 | + case 'LAST-MODIFIED' : |
|
| 364 | + return ($start <= $component->getDateTime() && $end >= $component->getDateTime()); |
|
| 365 | + |
|
| 366 | + |
|
| 367 | + |
|
| 368 | + default : |
|
| 369 | + throw new \Sabre\DAV\Exception\BadRequest('You cannot create a time-range filter on a ' . $component->name . ' component'); |
|
| 370 | + |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + } |
|
| 374 | 374 | |
| 375 | 375 | } |
@@ -21,71 +21,71 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class FreeBusyQueryReport implements XmlDeserializable { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Starttime of report |
|
| 26 | - * |
|
| 27 | - * @var DateTime|null |
|
| 28 | - */ |
|
| 29 | - public $start; |
|
| 24 | + /** |
|
| 25 | + * Starttime of report |
|
| 26 | + * |
|
| 27 | + * @var DateTime|null |
|
| 28 | + */ |
|
| 29 | + public $start; |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * End time of report |
|
| 33 | - * |
|
| 34 | - * @var DateTime|null |
|
| 35 | - */ |
|
| 36 | - public $end; |
|
| 31 | + /** |
|
| 32 | + * End time of report |
|
| 33 | + * |
|
| 34 | + * @var DateTime|null |
|
| 35 | + */ |
|
| 36 | + public $end; |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * The deserialize method is called during xml parsing. |
|
| 40 | - * |
|
| 41 | - * This method is called statictly, this is because in theory this method |
|
| 42 | - * may be used as a type of constructor, or factory method. |
|
| 43 | - * |
|
| 44 | - * Often you want to return an instance of the current class, but you are |
|
| 45 | - * free to return other data as well. |
|
| 46 | - * |
|
| 47 | - * You are responsible for advancing the reader to the next element. Not |
|
| 48 | - * doing anything will result in a never-ending loop. |
|
| 49 | - * |
|
| 50 | - * If you just want to skip parsing for this element altogether, you can |
|
| 51 | - * just call $reader->next(); |
|
| 52 | - * |
|
| 53 | - * $reader->parseInnerTree() will parse the entire sub-tree, and advance to |
|
| 54 | - * the next element. |
|
| 55 | - * |
|
| 56 | - * @param Reader $reader |
|
| 57 | - * @return mixed |
|
| 58 | - */ |
|
| 59 | - static function xmlDeserialize(Reader $reader) { |
|
| 38 | + /** |
|
| 39 | + * The deserialize method is called during xml parsing. |
|
| 40 | + * |
|
| 41 | + * This method is called statictly, this is because in theory this method |
|
| 42 | + * may be used as a type of constructor, or factory method. |
|
| 43 | + * |
|
| 44 | + * Often you want to return an instance of the current class, but you are |
|
| 45 | + * free to return other data as well. |
|
| 46 | + * |
|
| 47 | + * You are responsible for advancing the reader to the next element. Not |
|
| 48 | + * doing anything will result in a never-ending loop. |
|
| 49 | + * |
|
| 50 | + * If you just want to skip parsing for this element altogether, you can |
|
| 51 | + * just call $reader->next(); |
|
| 52 | + * |
|
| 53 | + * $reader->parseInnerTree() will parse the entire sub-tree, and advance to |
|
| 54 | + * the next element. |
|
| 55 | + * |
|
| 56 | + * @param Reader $reader |
|
| 57 | + * @return mixed |
|
| 58 | + */ |
|
| 59 | + static function xmlDeserialize(Reader $reader) { |
|
| 60 | 60 | |
| 61 | - $timeRange = '{' . Plugin::NS_CALDAV . '}time-range'; |
|
| 61 | + $timeRange = '{' . Plugin::NS_CALDAV . '}time-range'; |
|
| 62 | 62 | |
| 63 | - $start = null; |
|
| 64 | - $end = null; |
|
| 63 | + $start = null; |
|
| 64 | + $end = null; |
|
| 65 | 65 | |
| 66 | - foreach ((array)$reader->parseInnerTree([]) as $elem) { |
|
| 66 | + foreach ((array)$reader->parseInnerTree([]) as $elem) { |
|
| 67 | 67 | |
| 68 | - if ($elem['name'] !== $timeRange) continue; |
|
| 68 | + if ($elem['name'] !== $timeRange) continue; |
|
| 69 | 69 | |
| 70 | - $start = empty($elem['attributes']['start']) ?: $elem['attributes']['start']; |
|
| 71 | - $end = empty($elem['attributes']['end']) ?: $elem['attributes']['end']; |
|
| 70 | + $start = empty($elem['attributes']['start']) ?: $elem['attributes']['start']; |
|
| 71 | + $end = empty($elem['attributes']['end']) ?: $elem['attributes']['end']; |
|
| 72 | 72 | |
| 73 | - } |
|
| 74 | - if (!$start && !$end) { |
|
| 75 | - throw new BadRequest('The freebusy report must have a time-range element'); |
|
| 76 | - } |
|
| 77 | - if ($start) { |
|
| 78 | - $start = DateTimeParser::parseDateTime($start); |
|
| 79 | - } |
|
| 80 | - if ($end) { |
|
| 81 | - $end = DateTimeParser::parseDateTime($end); |
|
| 82 | - } |
|
| 83 | - $result = new self(); |
|
| 84 | - $result->start = $start; |
|
| 85 | - $result->end = $end; |
|
| 73 | + } |
|
| 74 | + if (!$start && !$end) { |
|
| 75 | + throw new BadRequest('The freebusy report must have a time-range element'); |
|
| 76 | + } |
|
| 77 | + if ($start) { |
|
| 78 | + $start = DateTimeParser::parseDateTime($start); |
|
| 79 | + } |
|
| 80 | + if ($end) { |
|
| 81 | + $end = DateTimeParser::parseDateTime($end); |
|
| 82 | + } |
|
| 83 | + $result = new self(); |
|
| 84 | + $result->start = $start; |
|
| 85 | + $result->end = $end; |
|
| 86 | 86 | |
| 87 | - return $result; |
|
| 87 | + return $result; |
|
| 88 | 88 | |
| 89 | - } |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | 91 | } |
@@ -63,7 +63,7 @@ |
||
| 63 | 63 | $start = null; |
| 64 | 64 | $end = null; |
| 65 | 65 | |
| 66 | - foreach ((array)$reader->parseInnerTree([]) as $elem) { |
|
| 66 | + foreach ((array) $reader->parseInnerTree([]) as $elem) { |
|
| 67 | 67 | |
| 68 | 68 | if ($elem['name'] !== $timeRange) continue; |
| 69 | 69 | |
@@ -65,7 +65,9 @@ |
||
| 65 | 65 | |
| 66 | 66 | foreach ((array)$reader->parseInnerTree([]) as $elem) { |
| 67 | 67 | |
| 68 | - if ($elem['name'] !== $timeRange) continue; |
|
| 68 | + if ($elem['name'] !== $timeRange) { |
|
| 69 | + continue; |
|
| 70 | + } |
|
| 69 | 71 | |
| 70 | 72 | $start = empty($elem['attributes']['start']) ?: $elem['attributes']['start']; |
| 71 | 73 | $end = empty($elem['attributes']['end']) ?: $elem['attributes']['end']; |
@@ -21,104 +21,104 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class CalendarMultiGetReport implements XmlDeserializable { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * An array with requested properties. |
|
| 26 | - * |
|
| 27 | - * @var array |
|
| 28 | - */ |
|
| 29 | - public $properties; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * This is an array with the urls that are being requested. |
|
| 33 | - * |
|
| 34 | - * @var array |
|
| 35 | - */ |
|
| 36 | - public $hrefs; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * If the calendar data must be expanded, this will contain an array with 2 |
|
| 40 | - * elements: start and end. |
|
| 41 | - * |
|
| 42 | - * Each may be a DateTime or null. |
|
| 43 | - * |
|
| 44 | - * @var array|null |
|
| 45 | - */ |
|
| 46 | - public $expand = null; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * The mimetype of the content that should be returend. Usually |
|
| 50 | - * text/calendar. |
|
| 51 | - * |
|
| 52 | - * @var string |
|
| 53 | - */ |
|
| 54 | - public $contentType = null; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * The version of calendar-data that should be returned. Usually '2.0', |
|
| 58 | - * referring to iCalendar 2.0. |
|
| 59 | - * |
|
| 60 | - * @var string |
|
| 61 | - */ |
|
| 62 | - public $version = null; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * The deserialize method is called during xml parsing. |
|
| 66 | - * |
|
| 67 | - * This method is called statictly, this is because in theory this method |
|
| 68 | - * may be used as a type of constructor, or factory method. |
|
| 69 | - * |
|
| 70 | - * Often you want to return an instance of the current class, but you are |
|
| 71 | - * free to return other data as well. |
|
| 72 | - * |
|
| 73 | - * You are responsible for advancing the reader to the next element. Not |
|
| 74 | - * doing anything will result in a never-ending loop. |
|
| 75 | - * |
|
| 76 | - * If you just want to skip parsing for this element altogether, you can |
|
| 77 | - * just call $reader->next(); |
|
| 78 | - * |
|
| 79 | - * $reader->parseInnerTree() will parse the entire sub-tree, and advance to |
|
| 80 | - * the next element. |
|
| 81 | - * |
|
| 82 | - * @param Reader $reader |
|
| 83 | - * @return mixed |
|
| 84 | - */ |
|
| 85 | - static function xmlDeserialize(Reader $reader) { |
|
| 86 | - |
|
| 87 | - $elems = $reader->parseInnerTree([ |
|
| 88 | - '{urn:ietf:params:xml:ns:caldav}calendar-data' => 'Sabre\\CalDAV\\Xml\\Filter\\CalendarData', |
|
| 89 | - '{DAV:}prop' => 'Sabre\\Xml\\Element\\KeyValue', |
|
| 90 | - ]); |
|
| 91 | - |
|
| 92 | - $newProps = [ |
|
| 93 | - 'hrefs' => [], |
|
| 94 | - 'properties' => [], |
|
| 95 | - ]; |
|
| 96 | - |
|
| 97 | - foreach ($elems as $elem) { |
|
| 98 | - |
|
| 99 | - switch ($elem['name']) { |
|
| 100 | - |
|
| 101 | - case '{DAV:}prop' : |
|
| 102 | - $newProps['properties'] = array_keys($elem['value']); |
|
| 103 | - if (isset($elem['value']['{' . Plugin::NS_CALDAV . '}calendar-data'])) { |
|
| 104 | - $newProps += $elem['value']['{' . Plugin::NS_CALDAV . '}calendar-data']; |
|
| 105 | - } |
|
| 106 | - break; |
|
| 107 | - case '{DAV:}href' : |
|
| 108 | - $newProps['hrefs'][] = Uri\resolve($reader->contextUri, $elem['value']); |
|
| 109 | - break; |
|
| 110 | - |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - $obj = new self(); |
|
| 116 | - foreach ($newProps as $key => $value) { |
|
| 117 | - $obj->$key = $value; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - return $obj; |
|
| 121 | - |
|
| 122 | - } |
|
| 24 | + /** |
|
| 25 | + * An array with requested properties. |
|
| 26 | + * |
|
| 27 | + * @var array |
|
| 28 | + */ |
|
| 29 | + public $properties; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * This is an array with the urls that are being requested. |
|
| 33 | + * |
|
| 34 | + * @var array |
|
| 35 | + */ |
|
| 36 | + public $hrefs; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * If the calendar data must be expanded, this will contain an array with 2 |
|
| 40 | + * elements: start and end. |
|
| 41 | + * |
|
| 42 | + * Each may be a DateTime or null. |
|
| 43 | + * |
|
| 44 | + * @var array|null |
|
| 45 | + */ |
|
| 46 | + public $expand = null; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * The mimetype of the content that should be returend. Usually |
|
| 50 | + * text/calendar. |
|
| 51 | + * |
|
| 52 | + * @var string |
|
| 53 | + */ |
|
| 54 | + public $contentType = null; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * The version of calendar-data that should be returned. Usually '2.0', |
|
| 58 | + * referring to iCalendar 2.0. |
|
| 59 | + * |
|
| 60 | + * @var string |
|
| 61 | + */ |
|
| 62 | + public $version = null; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * The deserialize method is called during xml parsing. |
|
| 66 | + * |
|
| 67 | + * This method is called statictly, this is because in theory this method |
|
| 68 | + * may be used as a type of constructor, or factory method. |
|
| 69 | + * |
|
| 70 | + * Often you want to return an instance of the current class, but you are |
|
| 71 | + * free to return other data as well. |
|
| 72 | + * |
|
| 73 | + * You are responsible for advancing the reader to the next element. Not |
|
| 74 | + * doing anything will result in a never-ending loop. |
|
| 75 | + * |
|
| 76 | + * If you just want to skip parsing for this element altogether, you can |
|
| 77 | + * just call $reader->next(); |
|
| 78 | + * |
|
| 79 | + * $reader->parseInnerTree() will parse the entire sub-tree, and advance to |
|
| 80 | + * the next element. |
|
| 81 | + * |
|
| 82 | + * @param Reader $reader |
|
| 83 | + * @return mixed |
|
| 84 | + */ |
|
| 85 | + static function xmlDeserialize(Reader $reader) { |
|
| 86 | + |
|
| 87 | + $elems = $reader->parseInnerTree([ |
|
| 88 | + '{urn:ietf:params:xml:ns:caldav}calendar-data' => 'Sabre\\CalDAV\\Xml\\Filter\\CalendarData', |
|
| 89 | + '{DAV:}prop' => 'Sabre\\Xml\\Element\\KeyValue', |
|
| 90 | + ]); |
|
| 91 | + |
|
| 92 | + $newProps = [ |
|
| 93 | + 'hrefs' => [], |
|
| 94 | + 'properties' => [], |
|
| 95 | + ]; |
|
| 96 | + |
|
| 97 | + foreach ($elems as $elem) { |
|
| 98 | + |
|
| 99 | + switch ($elem['name']) { |
|
| 100 | + |
|
| 101 | + case '{DAV:}prop' : |
|
| 102 | + $newProps['properties'] = array_keys($elem['value']); |
|
| 103 | + if (isset($elem['value']['{' . Plugin::NS_CALDAV . '}calendar-data'])) { |
|
| 104 | + $newProps += $elem['value']['{' . Plugin::NS_CALDAV . '}calendar-data']; |
|
| 105 | + } |
|
| 106 | + break; |
|
| 107 | + case '{DAV:}href' : |
|
| 108 | + $newProps['hrefs'][] = Uri\resolve($reader->contextUri, $elem['value']); |
|
| 109 | + break; |
|
| 110 | + |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + $obj = new self(); |
|
| 116 | + foreach ($newProps as $key => $value) { |
|
| 117 | + $obj->$key = $value; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + return $obj; |
|
| 121 | + |
|
| 122 | + } |
|
| 123 | 123 | |
| 124 | 124 | } |
@@ -21,119 +21,119 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class CalendarQueryReport implements XmlDeserializable { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * An array with requested properties. |
|
| 26 | - * |
|
| 27 | - * @var array |
|
| 28 | - */ |
|
| 29 | - public $properties; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * List of property/component filters. |
|
| 33 | - * |
|
| 34 | - * @var array |
|
| 35 | - */ |
|
| 36 | - public $filters; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * If the calendar data must be expanded, this will contain an array with 2 |
|
| 40 | - * elements: start and end. |
|
| 41 | - * |
|
| 42 | - * Each may be a DateTime or null. |
|
| 43 | - * |
|
| 44 | - * @var array|null |
|
| 45 | - */ |
|
| 46 | - public $expand = null; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * The mimetype of the content that should be returend. Usually |
|
| 50 | - * text/calendar. |
|
| 51 | - * |
|
| 52 | - * @var string |
|
| 53 | - */ |
|
| 54 | - public $contentType = null; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * The version of calendar-data that should be returned. Usually '2.0', |
|
| 58 | - * referring to iCalendar 2.0. |
|
| 59 | - * |
|
| 60 | - * @var string |
|
| 61 | - */ |
|
| 62 | - public $version = null; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * The deserialize method is called during xml parsing. |
|
| 66 | - * |
|
| 67 | - * This method is called statictly, this is because in theory this method |
|
| 68 | - * may be used as a type of constructor, or factory method. |
|
| 69 | - * |
|
| 70 | - * Often you want to return an instance of the current class, but you are |
|
| 71 | - * free to return other data as well. |
|
| 72 | - * |
|
| 73 | - * You are responsible for advancing the reader to the next element. Not |
|
| 74 | - * doing anything will result in a never-ending loop. |
|
| 75 | - * |
|
| 76 | - * If you just want to skip parsing for this element altogether, you can |
|
| 77 | - * just call $reader->next(); |
|
| 78 | - * |
|
| 79 | - * $reader->parseInnerTree() will parse the entire sub-tree, and advance to |
|
| 80 | - * the next element. |
|
| 81 | - * |
|
| 82 | - * @param Reader $reader |
|
| 83 | - * @return mixed |
|
| 84 | - */ |
|
| 85 | - static function xmlDeserialize(Reader $reader) { |
|
| 86 | - |
|
| 87 | - $elems = $reader->parseInnerTree([ |
|
| 88 | - '{urn:ietf:params:xml:ns:caldav}comp-filter' => 'Sabre\\CalDAV\\Xml\\Filter\\CompFilter', |
|
| 89 | - '{urn:ietf:params:xml:ns:caldav}prop-filter' => 'Sabre\\CalDAV\\Xml\\Filter\\PropFilter', |
|
| 90 | - '{urn:ietf:params:xml:ns:caldav}param-filter' => 'Sabre\\CalDAV\\Xml\\Filter\\ParamFilter', |
|
| 91 | - '{urn:ietf:params:xml:ns:caldav}calendar-data' => 'Sabre\\CalDAV\\Xml\\Filter\\CalendarData', |
|
| 92 | - '{DAV:}prop' => 'Sabre\\Xml\\Element\\KeyValue', |
|
| 93 | - ]); |
|
| 94 | - |
|
| 95 | - $newProps = [ |
|
| 96 | - 'filters' => null, |
|
| 97 | - 'properties' => [], |
|
| 98 | - ]; |
|
| 99 | - |
|
| 100 | - if (!is_array($elems)) $elems = []; |
|
| 101 | - |
|
| 102 | - foreach ($elems as $elem) { |
|
| 103 | - |
|
| 104 | - switch ($elem['name']) { |
|
| 105 | - |
|
| 106 | - case '{DAV:}prop' : |
|
| 107 | - $newProps['properties'] = array_keys($elem['value']); |
|
| 108 | - if (isset($elem['value']['{' . Plugin::NS_CALDAV . '}calendar-data'])) { |
|
| 109 | - $newProps += $elem['value']['{' . Plugin::NS_CALDAV . '}calendar-data']; |
|
| 110 | - } |
|
| 111 | - break; |
|
| 112 | - case '{' . Plugin::NS_CALDAV . '}filter' : |
|
| 113 | - foreach ($elem['value'] as $subElem) { |
|
| 114 | - if ($subElem['name'] === '{' . Plugin::NS_CALDAV . '}comp-filter') { |
|
| 115 | - if (!is_null($newProps['filters'])) { |
|
| 116 | - throw new BadRequest('Only one top-level comp-filter may be defined'); |
|
| 117 | - } |
|
| 118 | - $newProps['filters'] = $subElem['value']; |
|
| 119 | - } |
|
| 120 | - } |
|
| 121 | - break; |
|
| 122 | - |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - if (is_null($newProps['filters'])) { |
|
| 128 | - throw new BadRequest('The {' . Plugin::NS_CALDAV . '}filter element is required for this request'); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - $obj = new self(); |
|
| 132 | - foreach ($newProps as $key => $value) { |
|
| 133 | - $obj->$key = $value; |
|
| 134 | - } |
|
| 135 | - return $obj; |
|
| 136 | - |
|
| 137 | - } |
|
| 24 | + /** |
|
| 25 | + * An array with requested properties. |
|
| 26 | + * |
|
| 27 | + * @var array |
|
| 28 | + */ |
|
| 29 | + public $properties; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * List of property/component filters. |
|
| 33 | + * |
|
| 34 | + * @var array |
|
| 35 | + */ |
|
| 36 | + public $filters; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * If the calendar data must be expanded, this will contain an array with 2 |
|
| 40 | + * elements: start and end. |
|
| 41 | + * |
|
| 42 | + * Each may be a DateTime or null. |
|
| 43 | + * |
|
| 44 | + * @var array|null |
|
| 45 | + */ |
|
| 46 | + public $expand = null; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * The mimetype of the content that should be returend. Usually |
|
| 50 | + * text/calendar. |
|
| 51 | + * |
|
| 52 | + * @var string |
|
| 53 | + */ |
|
| 54 | + public $contentType = null; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * The version of calendar-data that should be returned. Usually '2.0', |
|
| 58 | + * referring to iCalendar 2.0. |
|
| 59 | + * |
|
| 60 | + * @var string |
|
| 61 | + */ |
|
| 62 | + public $version = null; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * The deserialize method is called during xml parsing. |
|
| 66 | + * |
|
| 67 | + * This method is called statictly, this is because in theory this method |
|
| 68 | + * may be used as a type of constructor, or factory method. |
|
| 69 | + * |
|
| 70 | + * Often you want to return an instance of the current class, but you are |
|
| 71 | + * free to return other data as well. |
|
| 72 | + * |
|
| 73 | + * You are responsible for advancing the reader to the next element. Not |
|
| 74 | + * doing anything will result in a never-ending loop. |
|
| 75 | + * |
|
| 76 | + * If you just want to skip parsing for this element altogether, you can |
|
| 77 | + * just call $reader->next(); |
|
| 78 | + * |
|
| 79 | + * $reader->parseInnerTree() will parse the entire sub-tree, and advance to |
|
| 80 | + * the next element. |
|
| 81 | + * |
|
| 82 | + * @param Reader $reader |
|
| 83 | + * @return mixed |
|
| 84 | + */ |
|
| 85 | + static function xmlDeserialize(Reader $reader) { |
|
| 86 | + |
|
| 87 | + $elems = $reader->parseInnerTree([ |
|
| 88 | + '{urn:ietf:params:xml:ns:caldav}comp-filter' => 'Sabre\\CalDAV\\Xml\\Filter\\CompFilter', |
|
| 89 | + '{urn:ietf:params:xml:ns:caldav}prop-filter' => 'Sabre\\CalDAV\\Xml\\Filter\\PropFilter', |
|
| 90 | + '{urn:ietf:params:xml:ns:caldav}param-filter' => 'Sabre\\CalDAV\\Xml\\Filter\\ParamFilter', |
|
| 91 | + '{urn:ietf:params:xml:ns:caldav}calendar-data' => 'Sabre\\CalDAV\\Xml\\Filter\\CalendarData', |
|
| 92 | + '{DAV:}prop' => 'Sabre\\Xml\\Element\\KeyValue', |
|
| 93 | + ]); |
|
| 94 | + |
|
| 95 | + $newProps = [ |
|
| 96 | + 'filters' => null, |
|
| 97 | + 'properties' => [], |
|
| 98 | + ]; |
|
| 99 | + |
|
| 100 | + if (!is_array($elems)) $elems = []; |
|
| 101 | + |
|
| 102 | + foreach ($elems as $elem) { |
|
| 103 | + |
|
| 104 | + switch ($elem['name']) { |
|
| 105 | + |
|
| 106 | + case '{DAV:}prop' : |
|
| 107 | + $newProps['properties'] = array_keys($elem['value']); |
|
| 108 | + if (isset($elem['value']['{' . Plugin::NS_CALDAV . '}calendar-data'])) { |
|
| 109 | + $newProps += $elem['value']['{' . Plugin::NS_CALDAV . '}calendar-data']; |
|
| 110 | + } |
|
| 111 | + break; |
|
| 112 | + case '{' . Plugin::NS_CALDAV . '}filter' : |
|
| 113 | + foreach ($elem['value'] as $subElem) { |
|
| 114 | + if ($subElem['name'] === '{' . Plugin::NS_CALDAV . '}comp-filter') { |
|
| 115 | + if (!is_null($newProps['filters'])) { |
|
| 116 | + throw new BadRequest('Only one top-level comp-filter may be defined'); |
|
| 117 | + } |
|
| 118 | + $newProps['filters'] = $subElem['value']; |
|
| 119 | + } |
|
| 120 | + } |
|
| 121 | + break; |
|
| 122 | + |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + if (is_null($newProps['filters'])) { |
|
| 128 | + throw new BadRequest('The {' . Plugin::NS_CALDAV . '}filter element is required for this request'); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + $obj = new self(); |
|
| 132 | + foreach ($newProps as $key => $value) { |
|
| 133 | + $obj->$key = $value; |
|
| 134 | + } |
|
| 135 | + return $obj; |
|
| 136 | + |
|
| 137 | + } |
|
| 138 | 138 | |
| 139 | 139 | } |
@@ -97,7 +97,9 @@ |
||
| 97 | 97 | 'properties' => [], |
| 98 | 98 | ]; |
| 99 | 99 | |
| 100 | - if (!is_array($elems)) $elems = []; |
|
| 100 | + if (!is_array($elems)) { |
|
| 101 | + $elems = []; |
|
| 102 | + } |
|
| 101 | 103 | |
| 102 | 104 | foreach ($elems as $elem) { |
| 103 | 105 | |
@@ -24,75 +24,75 @@ |
||
| 24 | 24 | */ |
| 25 | 25 | class PropFilter implements XmlDeserializable { |
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * The deserialize method is called during xml parsing. |
|
| 29 | - * |
|
| 30 | - * This method is called statictly, this is because in theory this method |
|
| 31 | - * may be used as a type of constructor, or factory method. |
|
| 32 | - * |
|
| 33 | - * Often you want to return an instance of the current class, but you are |
|
| 34 | - * free to return other data as well. |
|
| 35 | - * |
|
| 36 | - * You are responsible for advancing the reader to the next element. Not |
|
| 37 | - * doing anything will result in a never-ending loop. |
|
| 38 | - * |
|
| 39 | - * If you just want to skip parsing for this element altogether, you can |
|
| 40 | - * just call $reader->next(); |
|
| 41 | - * |
|
| 42 | - * $reader->parseInnerTree() will parse the entire sub-tree, and advance to |
|
| 43 | - * the next element. |
|
| 44 | - * |
|
| 45 | - * @param Reader $reader |
|
| 46 | - * @return mixed |
|
| 47 | - */ |
|
| 48 | - static function xmlDeserialize(Reader $reader) { |
|
| 27 | + /** |
|
| 28 | + * The deserialize method is called during xml parsing. |
|
| 29 | + * |
|
| 30 | + * This method is called statictly, this is because in theory this method |
|
| 31 | + * may be used as a type of constructor, or factory method. |
|
| 32 | + * |
|
| 33 | + * Often you want to return an instance of the current class, but you are |
|
| 34 | + * free to return other data as well. |
|
| 35 | + * |
|
| 36 | + * You are responsible for advancing the reader to the next element. Not |
|
| 37 | + * doing anything will result in a never-ending loop. |
|
| 38 | + * |
|
| 39 | + * If you just want to skip parsing for this element altogether, you can |
|
| 40 | + * just call $reader->next(); |
|
| 41 | + * |
|
| 42 | + * $reader->parseInnerTree() will parse the entire sub-tree, and advance to |
|
| 43 | + * the next element. |
|
| 44 | + * |
|
| 45 | + * @param Reader $reader |
|
| 46 | + * @return mixed |
|
| 47 | + */ |
|
| 48 | + static function xmlDeserialize(Reader $reader) { |
|
| 49 | 49 | |
| 50 | - $result = [ |
|
| 51 | - 'name' => null, |
|
| 52 | - 'is-not-defined' => false, |
|
| 53 | - 'param-filters' => [], |
|
| 54 | - 'text-match' => null, |
|
| 55 | - 'time-range' => false, |
|
| 56 | - ]; |
|
| 50 | + $result = [ |
|
| 51 | + 'name' => null, |
|
| 52 | + 'is-not-defined' => false, |
|
| 53 | + 'param-filters' => [], |
|
| 54 | + 'text-match' => null, |
|
| 55 | + 'time-range' => false, |
|
| 56 | + ]; |
|
| 57 | 57 | |
| 58 | - $att = $reader->parseAttributes(); |
|
| 59 | - $result['name'] = $att['name']; |
|
| 58 | + $att = $reader->parseAttributes(); |
|
| 59 | + $result['name'] = $att['name']; |
|
| 60 | 60 | |
| 61 | - $elems = $reader->parseInnerTree(); |
|
| 61 | + $elems = $reader->parseInnerTree(); |
|
| 62 | 62 | |
| 63 | - if (is_array($elems)) foreach ($elems as $elem) { |
|
| 63 | + if (is_array($elems)) foreach ($elems as $elem) { |
|
| 64 | 64 | |
| 65 | - switch ($elem['name']) { |
|
| 65 | + switch ($elem['name']) { |
|
| 66 | 66 | |
| 67 | - case '{' . Plugin::NS_CALDAV . '}param-filter' : |
|
| 68 | - $result['param-filters'][] = $elem['value']; |
|
| 69 | - break; |
|
| 70 | - case '{' . Plugin::NS_CALDAV . '}is-not-defined' : |
|
| 71 | - $result['is-not-defined'] = true; |
|
| 72 | - break; |
|
| 73 | - case '{' . Plugin::NS_CALDAV . '}time-range' : |
|
| 74 | - $result['time-range'] = [ |
|
| 75 | - 'start' => isset($elem['attributes']['start']) ? DateTimeParser::parseDateTime($elem['attributes']['start']) : null, |
|
| 76 | - 'end' => isset($elem['attributes']['end']) ? DateTimeParser::parseDateTime($elem['attributes']['end']) : null, |
|
| 77 | - ]; |
|
| 78 | - if ($result['time-range']['start'] && $result['time-range']['end'] && $result['time-range']['end'] <= $result['time-range']['start']) { |
|
| 79 | - throw new BadRequest('The end-date must be larger than the start-date'); |
|
| 80 | - } |
|
| 81 | - break; |
|
| 82 | - case '{' . Plugin::NS_CALDAV . '}text-match' : |
|
| 83 | - $result['text-match'] = [ |
|
| 84 | - 'negate-condition' => isset($elem['attributes']['negate-condition']) && $elem['attributes']['negate-condition'] === 'yes', |
|
| 85 | - 'collation' => isset($elem['attributes']['collation']) ? $elem['attributes']['collation'] : 'i;ascii-casemap', |
|
| 86 | - 'value' => $elem['value'], |
|
| 87 | - ]; |
|
| 88 | - break; |
|
| 67 | + case '{' . Plugin::NS_CALDAV . '}param-filter' : |
|
| 68 | + $result['param-filters'][] = $elem['value']; |
|
| 69 | + break; |
|
| 70 | + case '{' . Plugin::NS_CALDAV . '}is-not-defined' : |
|
| 71 | + $result['is-not-defined'] = true; |
|
| 72 | + break; |
|
| 73 | + case '{' . Plugin::NS_CALDAV . '}time-range' : |
|
| 74 | + $result['time-range'] = [ |
|
| 75 | + 'start' => isset($elem['attributes']['start']) ? DateTimeParser::parseDateTime($elem['attributes']['start']) : null, |
|
| 76 | + 'end' => isset($elem['attributes']['end']) ? DateTimeParser::parseDateTime($elem['attributes']['end']) : null, |
|
| 77 | + ]; |
|
| 78 | + if ($result['time-range']['start'] && $result['time-range']['end'] && $result['time-range']['end'] <= $result['time-range']['start']) { |
|
| 79 | + throw new BadRequest('The end-date must be larger than the start-date'); |
|
| 80 | + } |
|
| 81 | + break; |
|
| 82 | + case '{' . Plugin::NS_CALDAV . '}text-match' : |
|
| 83 | + $result['text-match'] = [ |
|
| 84 | + 'negate-condition' => isset($elem['attributes']['negate-condition']) && $elem['attributes']['negate-condition'] === 'yes', |
|
| 85 | + 'collation' => isset($elem['attributes']['collation']) ? $elem['attributes']['collation'] : 'i;ascii-casemap', |
|
| 86 | + 'value' => $elem['value'], |
|
| 87 | + ]; |
|
| 88 | + break; |
|
| 89 | 89 | |
| 90 | - } |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - } |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - return $result; |
|
| 94 | + return $result; |
|
| 95 | 95 | |
| 96 | - } |
|
| 96 | + } |
|
| 97 | 97 | |
| 98 | 98 | } |
@@ -60,12 +60,14 @@ |
||
| 60 | 60 | |
| 61 | 61 | $elems = $reader->parseInnerTree(); |
| 62 | 62 | |
| 63 | - if (is_array($elems)) foreach ($elems as $elem) { |
|
| 63 | + if (is_array($elems)) { |
|
| 64 | + foreach ($elems as $elem) { |
|
| 64 | 65 | |
| 65 | 66 | switch ($elem['name']) { |
| 66 | 67 | |
| 67 | 68 | case '{' . Plugin::NS_CALDAV . '}param-filter' : |
| 68 | 69 | $result['param-filters'][] = $elem['value']; |
| 70 | + } |
|
| 69 | 71 | break; |
| 70 | 72 | case '{' . Plugin::NS_CALDAV . '}is-not-defined' : |
| 71 | 73 | $result['is-not-defined'] = true; |
@@ -22,61 +22,61 @@ |
||
| 22 | 22 | */ |
| 23 | 23 | class ParamFilter implements XmlDeserializable { |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * The deserialize method is called during xml parsing. |
|
| 27 | - * |
|
| 28 | - * This method is called statictly, this is because in theory this method |
|
| 29 | - * may be used as a type of constructor, or factory method. |
|
| 30 | - * |
|
| 31 | - * Often you want to return an instance of the current class, but you are |
|
| 32 | - * free to return other data as well. |
|
| 33 | - * |
|
| 34 | - * Important note 2: You are responsible for advancing the reader to the |
|
| 35 | - * next element. Not doing anything will result in a never-ending loop. |
|
| 36 | - * |
|
| 37 | - * If you just want to skip parsing for this element altogether, you can |
|
| 38 | - * just call $reader->next(); |
|
| 39 | - * |
|
| 40 | - * $reader->parseInnerTree() will parse the entire sub-tree, and advance to |
|
| 41 | - * the next element. |
|
| 42 | - * |
|
| 43 | - * @param Reader $reader |
|
| 44 | - * @return mixed |
|
| 45 | - */ |
|
| 46 | - static function xmlDeserialize(Reader $reader) { |
|
| 25 | + /** |
|
| 26 | + * The deserialize method is called during xml parsing. |
|
| 27 | + * |
|
| 28 | + * This method is called statictly, this is because in theory this method |
|
| 29 | + * may be used as a type of constructor, or factory method. |
|
| 30 | + * |
|
| 31 | + * Often you want to return an instance of the current class, but you are |
|
| 32 | + * free to return other data as well. |
|
| 33 | + * |
|
| 34 | + * Important note 2: You are responsible for advancing the reader to the |
|
| 35 | + * next element. Not doing anything will result in a never-ending loop. |
|
| 36 | + * |
|
| 37 | + * If you just want to skip parsing for this element altogether, you can |
|
| 38 | + * just call $reader->next(); |
|
| 39 | + * |
|
| 40 | + * $reader->parseInnerTree() will parse the entire sub-tree, and advance to |
|
| 41 | + * the next element. |
|
| 42 | + * |
|
| 43 | + * @param Reader $reader |
|
| 44 | + * @return mixed |
|
| 45 | + */ |
|
| 46 | + static function xmlDeserialize(Reader $reader) { |
|
| 47 | 47 | |
| 48 | - $result = [ |
|
| 49 | - 'name' => null, |
|
| 50 | - 'is-not-defined' => false, |
|
| 51 | - 'text-match' => null, |
|
| 52 | - ]; |
|
| 48 | + $result = [ |
|
| 49 | + 'name' => null, |
|
| 50 | + 'is-not-defined' => false, |
|
| 51 | + 'text-match' => null, |
|
| 52 | + ]; |
|
| 53 | 53 | |
| 54 | - $att = $reader->parseAttributes(); |
|
| 55 | - $result['name'] = $att['name']; |
|
| 54 | + $att = $reader->parseAttributes(); |
|
| 55 | + $result['name'] = $att['name']; |
|
| 56 | 56 | |
| 57 | - $elems = $reader->parseInnerTree(); |
|
| 57 | + $elems = $reader->parseInnerTree(); |
|
| 58 | 58 | |
| 59 | - if (is_array($elems)) foreach ($elems as $elem) { |
|
| 59 | + if (is_array($elems)) foreach ($elems as $elem) { |
|
| 60 | 60 | |
| 61 | - switch ($elem['name']) { |
|
| 61 | + switch ($elem['name']) { |
|
| 62 | 62 | |
| 63 | - case '{' . Plugin::NS_CALDAV . '}is-not-defined' : |
|
| 64 | - $result['is-not-defined'] = true; |
|
| 65 | - break; |
|
| 66 | - case '{' . Plugin::NS_CALDAV . '}text-match' : |
|
| 67 | - $result['text-match'] = [ |
|
| 68 | - 'negate-condition' => isset($elem['attributes']['negate-condition']) && $elem['attributes']['negate-condition'] === 'yes', |
|
| 69 | - 'collation' => isset($elem['attributes']['collation']) ? $elem['attributes']['collation'] : 'i;ascii-casemap', |
|
| 70 | - 'value' => $elem['value'], |
|
| 71 | - ]; |
|
| 72 | - break; |
|
| 63 | + case '{' . Plugin::NS_CALDAV . '}is-not-defined' : |
|
| 64 | + $result['is-not-defined'] = true; |
|
| 65 | + break; |
|
| 66 | + case '{' . Plugin::NS_CALDAV . '}text-match' : |
|
| 67 | + $result['text-match'] = [ |
|
| 68 | + 'negate-condition' => isset($elem['attributes']['negate-condition']) && $elem['attributes']['negate-condition'] === 'yes', |
|
| 69 | + 'collation' => isset($elem['attributes']['collation']) ? $elem['attributes']['collation'] : 'i;ascii-casemap', |
|
| 70 | + 'value' => $elem['value'], |
|
| 71 | + ]; |
|
| 72 | + break; |
|
| 73 | 73 | |
| 74 | - } |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - } |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - return $result; |
|
| 78 | + return $result; |
|
| 79 | 79 | |
| 80 | - } |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | 82 | } |
@@ -56,12 +56,14 @@ |
||
| 56 | 56 | |
| 57 | 57 | $elems = $reader->parseInnerTree(); |
| 58 | 58 | |
| 59 | - if (is_array($elems)) foreach ($elems as $elem) { |
|
| 59 | + if (is_array($elems)) { |
|
| 60 | + foreach ($elems as $elem) { |
|
| 60 | 61 | |
| 61 | 62 | switch ($elem['name']) { |
| 62 | 63 | |
| 63 | 64 | case '{' . Plugin::NS_CALDAV . '}is-not-defined' : |
| 64 | 65 | $result['is-not-defined'] = true; |
| 66 | + } |
|
| 65 | 67 | break; |
| 66 | 68 | case '{' . Plugin::NS_CALDAV . '}text-match' : |
| 67 | 69 | $result['text-match'] = [ |