@@ -1037,7 +1037,7 @@ discard block |
||
| 1037 | 1037 | * @param string $principalUri |
| 1038 | 1038 | * @param string $uri |
| 1039 | 1039 | * @param array $properties |
| 1040 | - * @return mixed |
|
| 1040 | + * @return integer |
|
| 1041 | 1041 | */ |
| 1042 | 1042 | function createSubscription($principalUri, $uri, array $properties) { |
| 1043 | 1043 | |
@@ -1378,6 +1378,9 @@ discard block |
||
| 1378 | 1378 | return $this->sharingBackend->applyShareAcl($resourceId, $acl); |
| 1379 | 1379 | } |
| 1380 | 1380 | |
| 1381 | + /** |
|
| 1382 | + * @param boolean $toV2 |
|
| 1383 | + */ |
|
| 1381 | 1384 | private function convertPrincipal($principalUri, $toV2) { |
| 1382 | 1385 | if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
| 1383 | 1386 | list(, $name) = URLUtil::splitPath($principalUri); |
@@ -54,1392 +54,1392 @@ |
||
| 54 | 54 | */ |
| 55 | 55 | class CalDavBackend extends AbstractBackend implements SyncSupport, SubscriptionSupport, SchedulingSupport { |
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * We need to specify a max date, because we need to stop *somewhere* |
|
| 59 | - * |
|
| 60 | - * On 32 bit system the maximum for a signed integer is 2147483647, so |
|
| 61 | - * MAX_DATE cannot be higher than date('Y-m-d', 2147483647) which results |
|
| 62 | - * in 2038-01-19 to avoid problems when the date is converted |
|
| 63 | - * to a unix timestamp. |
|
| 64 | - */ |
|
| 65 | - const MAX_DATE = '2038-01-01'; |
|
| 66 | - |
|
| 67 | - const CLASSIFICATION_PUBLIC = 0; |
|
| 68 | - const CLASSIFICATION_PRIVATE = 1; |
|
| 69 | - const CLASSIFICATION_CONFIDENTIAL = 2; |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * List of CalDAV properties, and how they map to database field names |
|
| 73 | - * Add your own properties by simply adding on to this array. |
|
| 74 | - * |
|
| 75 | - * Note that only string-based properties are supported here. |
|
| 76 | - * |
|
| 77 | - * @var array |
|
| 78 | - */ |
|
| 79 | - public $propertyMap = [ |
|
| 80 | - '{DAV:}displayname' => 'displayname', |
|
| 81 | - '{urn:ietf:params:xml:ns:caldav}calendar-description' => 'description', |
|
| 82 | - '{urn:ietf:params:xml:ns:caldav}calendar-timezone' => 'timezone', |
|
| 83 | - '{http://apple.com/ns/ical/}calendar-order' => 'calendarorder', |
|
| 84 | - '{http://apple.com/ns/ical/}calendar-color' => 'calendarcolor', |
|
| 85 | - ]; |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * List of subscription properties, and how they map to database field names. |
|
| 89 | - * |
|
| 90 | - * @var array |
|
| 91 | - */ |
|
| 92 | - public $subscriptionPropertyMap = [ |
|
| 93 | - '{DAV:}displayname' => 'displayname', |
|
| 94 | - '{http://apple.com/ns/ical/}refreshrate' => 'refreshrate', |
|
| 95 | - '{http://apple.com/ns/ical/}calendar-order' => 'calendarorder', |
|
| 96 | - '{http://apple.com/ns/ical/}calendar-color' => 'calendarcolor', |
|
| 97 | - '{http://calendarserver.org/ns/}subscribed-strip-todos' => 'striptodos', |
|
| 98 | - '{http://calendarserver.org/ns/}subscribed-strip-alarms' => 'stripalarms', |
|
| 99 | - '{http://calendarserver.org/ns/}subscribed-strip-attachments' => 'stripattachments', |
|
| 100 | - ]; |
|
| 101 | - |
|
| 102 | - /** @var IDBConnection */ |
|
| 103 | - private $db; |
|
| 104 | - |
|
| 105 | - /** @var Backend */ |
|
| 106 | - private $sharingBackend; |
|
| 107 | - |
|
| 108 | - /** @var Principal */ |
|
| 109 | - private $principalBackend; |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * CalDavBackend constructor. |
|
| 113 | - * |
|
| 114 | - * @param IDBConnection $db |
|
| 115 | - * @param Principal $principalBackend |
|
| 116 | - */ |
|
| 117 | - public function __construct(IDBConnection $db, Principal $principalBackend) { |
|
| 118 | - $this->db = $db; |
|
| 119 | - $this->principalBackend = $principalBackend; |
|
| 120 | - $this->sharingBackend = new Backend($this->db, $principalBackend, 'calendar'); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Returns a list of calendars for a principal. |
|
| 125 | - * |
|
| 126 | - * Every project is an array with the following keys: |
|
| 127 | - * * id, a unique id that will be used by other functions to modify the |
|
| 128 | - * calendar. This can be the same as the uri or a database key. |
|
| 129 | - * * uri, which the basename of the uri with which the calendar is |
|
| 130 | - * accessed. |
|
| 131 | - * * principaluri. The owner of the calendar. Almost always the same as |
|
| 132 | - * principalUri passed to this method. |
|
| 133 | - * |
|
| 134 | - * Furthermore it can contain webdav properties in clark notation. A very |
|
| 135 | - * common one is '{DAV:}displayname'. |
|
| 136 | - * |
|
| 137 | - * Many clients also require: |
|
| 138 | - * {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
|
| 139 | - * For this property, you can just return an instance of |
|
| 140 | - * Sabre\CalDAV\Property\SupportedCalendarComponentSet. |
|
| 141 | - * |
|
| 142 | - * If you return {http://sabredav.org/ns}read-only and set the value to 1, |
|
| 143 | - * ACL will automatically be put in read-only mode. |
|
| 144 | - * |
|
| 145 | - * @param string $principalUri |
|
| 146 | - * @return array |
|
| 147 | - */ |
|
| 148 | - function getCalendarsForUser($principalUri) { |
|
| 149 | - $principalUriOriginal = $principalUri; |
|
| 150 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 151 | - $fields = array_values($this->propertyMap); |
|
| 152 | - $fields[] = 'id'; |
|
| 153 | - $fields[] = 'uri'; |
|
| 154 | - $fields[] = 'synctoken'; |
|
| 155 | - $fields[] = 'components'; |
|
| 156 | - $fields[] = 'principaluri'; |
|
| 157 | - $fields[] = 'transparent'; |
|
| 158 | - |
|
| 159 | - // Making fields a comma-delimited list |
|
| 160 | - $query = $this->db->getQueryBuilder(); |
|
| 161 | - $query->select($fields)->from('calendars') |
|
| 162 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 163 | - ->orderBy('calendarorder', 'ASC'); |
|
| 164 | - $stmt = $query->execute(); |
|
| 165 | - |
|
| 166 | - $calendars = []; |
|
| 167 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 168 | - |
|
| 169 | - $components = []; |
|
| 170 | - if ($row['components']) { |
|
| 171 | - $components = explode(',',$row['components']); |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - $calendar = [ |
|
| 175 | - 'id' => $row['id'], |
|
| 176 | - 'uri' => $row['uri'], |
|
| 177 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
|
| 178 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 179 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 180 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 181 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 182 | - ]; |
|
| 183 | - |
|
| 184 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
| 185 | - $calendar[$xmlName] = $row[$dbName]; |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - if (!isset($calendars[$calendar['id']])) { |
|
| 189 | - $calendars[$calendar['id']] = $calendar; |
|
| 190 | - } |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - $stmt->closeCursor(); |
|
| 194 | - |
|
| 195 | - // query for shared calendars |
|
| 196 | - $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); |
|
| 197 | - $principals[]= $principalUri; |
|
| 198 | - |
|
| 199 | - $fields = array_values($this->propertyMap); |
|
| 200 | - $fields[] = 'a.id'; |
|
| 201 | - $fields[] = 'a.uri'; |
|
| 202 | - $fields[] = 'a.synctoken'; |
|
| 203 | - $fields[] = 'a.components'; |
|
| 204 | - $fields[] = 'a.principaluri'; |
|
| 205 | - $fields[] = 'a.transparent'; |
|
| 206 | - $fields[] = 's.access'; |
|
| 207 | - $query = $this->db->getQueryBuilder(); |
|
| 208 | - $result = $query->select($fields) |
|
| 209 | - ->from('dav_shares', 's') |
|
| 210 | - ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
| 211 | - ->where($query->expr()->in('s.principaluri', $query->createParameter('principaluri'))) |
|
| 212 | - ->andWhere($query->expr()->eq('s.type', $query->createParameter('type'))) |
|
| 213 | - ->setParameter('type', 'calendar') |
|
| 214 | - ->setParameter('principaluri', $principals, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY) |
|
| 215 | - ->execute(); |
|
| 216 | - |
|
| 217 | - while($row = $result->fetch()) { |
|
| 218 | - list(, $name) = URLUtil::splitPath($row['principaluri']); |
|
| 219 | - $uri = $row['uri'] . '_shared_by_' . $name; |
|
| 220 | - $row['displayname'] = $row['displayname'] . "($name)"; |
|
| 221 | - $components = []; |
|
| 222 | - if ($row['components']) { |
|
| 223 | - $components = explode(',',$row['components']); |
|
| 224 | - } |
|
| 225 | - $calendar = [ |
|
| 226 | - 'id' => $row['id'], |
|
| 227 | - 'uri' => $uri, |
|
| 228 | - 'principaluri' => $principalUri, |
|
| 229 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 230 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 231 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 232 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 233 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $row['principaluri'], |
|
| 234 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ, |
|
| 235 | - ]; |
|
| 236 | - |
|
| 237 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
| 238 | - $calendar[$xmlName] = $row[$dbName]; |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - if (!isset($calendars[$calendar['id']])) { |
|
| 242 | - $calendars[$calendar['id']] = $calendar; |
|
| 243 | - } |
|
| 244 | - } |
|
| 245 | - $result->closeCursor(); |
|
| 246 | - |
|
| 247 | - return array_values($calendars); |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * @param string $principal |
|
| 252 | - * @param string $uri |
|
| 253 | - * @return array|null |
|
| 254 | - */ |
|
| 255 | - public function getCalendarByUri($principal, $uri) { |
|
| 256 | - $fields = array_values($this->propertyMap); |
|
| 257 | - $fields[] = 'id'; |
|
| 258 | - $fields[] = 'uri'; |
|
| 259 | - $fields[] = 'synctoken'; |
|
| 260 | - $fields[] = 'components'; |
|
| 261 | - $fields[] = 'principaluri'; |
|
| 262 | - $fields[] = 'transparent'; |
|
| 263 | - |
|
| 264 | - // Making fields a comma-delimited list |
|
| 265 | - $query = $this->db->getQueryBuilder(); |
|
| 266 | - $query->select($fields)->from('calendars') |
|
| 267 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 268 | - ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal))) |
|
| 269 | - ->setMaxResults(1); |
|
| 270 | - $stmt = $query->execute(); |
|
| 271 | - |
|
| 272 | - $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
| 273 | - $stmt->closeCursor(); |
|
| 274 | - if ($row === false) { |
|
| 275 | - return null; |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - $components = []; |
|
| 279 | - if ($row['components']) { |
|
| 280 | - $components = explode(',',$row['components']); |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - $calendar = [ |
|
| 284 | - 'id' => $row['id'], |
|
| 285 | - 'uri' => $row['uri'], |
|
| 286 | - 'principaluri' => $row['principaluri'], |
|
| 287 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 288 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 289 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 290 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 291 | - ]; |
|
| 292 | - |
|
| 293 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
| 294 | - $calendar[$xmlName] = $row[$dbName]; |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - return $calendar; |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - public function getCalendarById($calendarId) { |
|
| 301 | - $fields = array_values($this->propertyMap); |
|
| 302 | - $fields[] = 'id'; |
|
| 303 | - $fields[] = 'uri'; |
|
| 304 | - $fields[] = 'synctoken'; |
|
| 305 | - $fields[] = 'components'; |
|
| 306 | - $fields[] = 'principaluri'; |
|
| 307 | - $fields[] = 'transparent'; |
|
| 308 | - |
|
| 309 | - // Making fields a comma-delimited list |
|
| 310 | - $query = $this->db->getQueryBuilder(); |
|
| 311 | - $query->select($fields)->from('calendars') |
|
| 312 | - ->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))) |
|
| 313 | - ->setMaxResults(1); |
|
| 314 | - $stmt = $query->execute(); |
|
| 315 | - |
|
| 316 | - $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
| 317 | - $stmt->closeCursor(); |
|
| 318 | - if ($row === false) { |
|
| 319 | - return null; |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - $components = []; |
|
| 323 | - if ($row['components']) { |
|
| 324 | - $components = explode(',',$row['components']); |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - $calendar = [ |
|
| 328 | - 'id' => $row['id'], |
|
| 329 | - 'uri' => $row['uri'], |
|
| 330 | - 'principaluri' => $row['principaluri'], |
|
| 331 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 332 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 333 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 334 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 335 | - ]; |
|
| 336 | - |
|
| 337 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
| 338 | - $calendar[$xmlName] = $row[$dbName]; |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - return $calendar; |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - /** |
|
| 345 | - * Creates a new calendar for a principal. |
|
| 346 | - * |
|
| 347 | - * If the creation was a success, an id must be returned that can be used to reference |
|
| 348 | - * this calendar in other methods, such as updateCalendar. |
|
| 349 | - * |
|
| 350 | - * @param string $principalUri |
|
| 351 | - * @param string $calendarUri |
|
| 352 | - * @param array $properties |
|
| 353 | - * @return int |
|
| 354 | - */ |
|
| 355 | - function createCalendar($principalUri, $calendarUri, array $properties) { |
|
| 356 | - $values = [ |
|
| 357 | - 'principaluri' => $this->convertPrincipal($principalUri, true), |
|
| 358 | - 'uri' => $calendarUri, |
|
| 359 | - 'synctoken' => 1, |
|
| 360 | - 'transparent' => 0, |
|
| 361 | - 'components' => 'VEVENT,VTODO', |
|
| 362 | - 'displayname' => $calendarUri |
|
| 363 | - ]; |
|
| 364 | - |
|
| 365 | - // Default value |
|
| 366 | - $sccs = '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set'; |
|
| 367 | - if (isset($properties[$sccs])) { |
|
| 368 | - if (!($properties[$sccs] instanceof SupportedCalendarComponentSet)) { |
|
| 369 | - throw new DAV\Exception('The ' . $sccs . ' property must be of type: \Sabre\CalDAV\Property\SupportedCalendarComponentSet'); |
|
| 370 | - } |
|
| 371 | - $values['components'] = implode(',',$properties[$sccs]->getValue()); |
|
| 372 | - } |
|
| 373 | - $transp = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
| 374 | - if (isset($properties[$transp])) { |
|
| 375 | - $values['transparent'] = (int) ($properties[$transp]->getValue() === 'transparent'); |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
| 379 | - if (isset($properties[$xmlName])) { |
|
| 380 | - $values[$dbName] = $properties[$xmlName]; |
|
| 381 | - } |
|
| 382 | - } |
|
| 383 | - |
|
| 384 | - $query = $this->db->getQueryBuilder(); |
|
| 385 | - $query->insert('calendars'); |
|
| 386 | - foreach($values as $column => $value) { |
|
| 387 | - $query->setValue($column, $query->createNamedParameter($value)); |
|
| 388 | - } |
|
| 389 | - $query->execute(); |
|
| 390 | - return $query->getLastInsertId(); |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - /** |
|
| 394 | - * Updates properties for a calendar. |
|
| 395 | - * |
|
| 396 | - * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
| 397 | - * To do the actual updates, you must tell this object which properties |
|
| 398 | - * you're going to process with the handle() method. |
|
| 399 | - * |
|
| 400 | - * Calling the handle method is like telling the PropPatch object "I |
|
| 401 | - * promise I can handle updating this property". |
|
| 402 | - * |
|
| 403 | - * Read the PropPatch documentation for more info and examples. |
|
| 404 | - * |
|
| 405 | - * @param PropPatch $propPatch |
|
| 406 | - * @return void |
|
| 407 | - */ |
|
| 408 | - function updateCalendar($calendarId, PropPatch $propPatch) { |
|
| 409 | - $supportedProperties = array_keys($this->propertyMap); |
|
| 410 | - $supportedProperties[] = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
| 411 | - |
|
| 412 | - $propPatch->handle($supportedProperties, function($mutations) use ($calendarId) { |
|
| 413 | - $newValues = []; |
|
| 414 | - foreach ($mutations as $propertyName => $propertyValue) { |
|
| 415 | - |
|
| 416 | - switch ($propertyName) { |
|
| 417 | - case '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' : |
|
| 418 | - $fieldName = 'transparent'; |
|
| 419 | - $newValues[$fieldName] = (int) ($propertyValue->getValue() === 'transparent'); |
|
| 420 | - break; |
|
| 421 | - default : |
|
| 422 | - $fieldName = $this->propertyMap[$propertyName]; |
|
| 423 | - $newValues[$fieldName] = $propertyValue; |
|
| 424 | - break; |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - } |
|
| 428 | - $query = $this->db->getQueryBuilder(); |
|
| 429 | - $query->update('calendars'); |
|
| 430 | - foreach ($newValues as $fieldName => $value) { |
|
| 431 | - $query->set($fieldName, $query->createNamedParameter($value)); |
|
| 432 | - } |
|
| 433 | - $query->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))); |
|
| 434 | - $query->execute(); |
|
| 435 | - |
|
| 436 | - $this->addChange($calendarId, "", 2); |
|
| 437 | - |
|
| 438 | - return true; |
|
| 439 | - }); |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - /** |
|
| 443 | - * Delete a calendar and all it's objects |
|
| 444 | - * |
|
| 445 | - * @param mixed $calendarId |
|
| 446 | - * @return void |
|
| 447 | - */ |
|
| 448 | - function deleteCalendar($calendarId) { |
|
| 449 | - $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ?'); |
|
| 450 | - $stmt->execute([$calendarId]); |
|
| 451 | - |
|
| 452 | - $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendars` WHERE `id` = ?'); |
|
| 453 | - $stmt->execute([$calendarId]); |
|
| 454 | - |
|
| 455 | - $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarchanges` WHERE `calendarid` = ?'); |
|
| 456 | - $stmt->execute([$calendarId]); |
|
| 457 | - |
|
| 458 | - $this->sharingBackend->deleteAllShares($calendarId); |
|
| 459 | - } |
|
| 460 | - |
|
| 461 | - /** |
|
| 462 | - * Returns all calendar objects within a calendar. |
|
| 463 | - * |
|
| 464 | - * Every item contains an array with the following keys: |
|
| 465 | - * * calendardata - The iCalendar-compatible calendar data |
|
| 466 | - * * uri - a unique key which will be used to construct the uri. This can |
|
| 467 | - * be any arbitrary string, but making sure it ends with '.ics' is a |
|
| 468 | - * good idea. This is only the basename, or filename, not the full |
|
| 469 | - * path. |
|
| 470 | - * * lastmodified - a timestamp of the last modification time |
|
| 471 | - * * etag - An arbitrary string, surrounded by double-quotes. (e.g.: |
|
| 472 | - * '"abcdef"') |
|
| 473 | - * * size - The size of the calendar objects, in bytes. |
|
| 474 | - * * component - optional, a string containing the type of object, such |
|
| 475 | - * as 'vevent' or 'vtodo'. If specified, this will be used to populate |
|
| 476 | - * the Content-Type header. |
|
| 477 | - * |
|
| 478 | - * Note that the etag is optional, but it's highly encouraged to return for |
|
| 479 | - * speed reasons. |
|
| 480 | - * |
|
| 481 | - * The calendardata is also optional. If it's not returned |
|
| 482 | - * 'getCalendarObject' will be called later, which *is* expected to return |
|
| 483 | - * calendardata. |
|
| 484 | - * |
|
| 485 | - * If neither etag or size are specified, the calendardata will be |
|
| 486 | - * used/fetched to determine these numbers. If both are specified the |
|
| 487 | - * amount of times this is needed is reduced by a great degree. |
|
| 488 | - * |
|
| 489 | - * @param mixed $calendarId |
|
| 490 | - * @return array |
|
| 491 | - */ |
|
| 492 | - function getCalendarObjects($calendarId) { |
|
| 493 | - $query = $this->db->getQueryBuilder(); |
|
| 494 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'componenttype', 'classification']) |
|
| 495 | - ->from('calendarobjects') |
|
| 496 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))); |
|
| 497 | - $stmt = $query->execute(); |
|
| 498 | - |
|
| 499 | - $result = []; |
|
| 500 | - foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) { |
|
| 501 | - $result[] = [ |
|
| 502 | - 'id' => $row['id'], |
|
| 503 | - 'uri' => $row['uri'], |
|
| 504 | - 'lastmodified' => $row['lastmodified'], |
|
| 505 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 506 | - 'calendarid' => $row['calendarid'], |
|
| 507 | - 'size' => (int)$row['size'], |
|
| 508 | - 'component' => strtolower($row['componenttype']), |
|
| 509 | - 'classification'=> (int)$row['classification'] |
|
| 510 | - ]; |
|
| 511 | - } |
|
| 512 | - |
|
| 513 | - return $result; |
|
| 514 | - } |
|
| 515 | - |
|
| 516 | - /** |
|
| 517 | - * Returns information from a single calendar object, based on it's object |
|
| 518 | - * uri. |
|
| 519 | - * |
|
| 520 | - * The object uri is only the basename, or filename and not a full path. |
|
| 521 | - * |
|
| 522 | - * The returned array must have the same keys as getCalendarObjects. The |
|
| 523 | - * 'calendardata' object is required here though, while it's not required |
|
| 524 | - * for getCalendarObjects. |
|
| 525 | - * |
|
| 526 | - * This method must return null if the object did not exist. |
|
| 527 | - * |
|
| 528 | - * @param mixed $calendarId |
|
| 529 | - * @param string $objectUri |
|
| 530 | - * @return array|null |
|
| 531 | - */ |
|
| 532 | - function getCalendarObject($calendarId, $objectUri) { |
|
| 533 | - |
|
| 534 | - $query = $this->db->getQueryBuilder(); |
|
| 535 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification']) |
|
| 536 | - ->from('calendarobjects') |
|
| 537 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 538 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))); |
|
| 539 | - $stmt = $query->execute(); |
|
| 540 | - $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
| 541 | - |
|
| 542 | - if(!$row) return null; |
|
| 543 | - |
|
| 544 | - return [ |
|
| 545 | - 'id' => $row['id'], |
|
| 546 | - 'uri' => $row['uri'], |
|
| 547 | - 'lastmodified' => $row['lastmodified'], |
|
| 548 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 549 | - 'calendarid' => $row['calendarid'], |
|
| 550 | - 'size' => (int)$row['size'], |
|
| 551 | - 'calendardata' => $this->readBlob($row['calendardata']), |
|
| 552 | - 'component' => strtolower($row['componenttype']), |
|
| 553 | - 'classification'=> (int)$row['classification'] |
|
| 554 | - ]; |
|
| 555 | - } |
|
| 556 | - |
|
| 557 | - /** |
|
| 558 | - * Returns a list of calendar objects. |
|
| 559 | - * |
|
| 560 | - * This method should work identical to getCalendarObject, but instead |
|
| 561 | - * return all the calendar objects in the list as an array. |
|
| 562 | - * |
|
| 563 | - * If the backend supports this, it may allow for some speed-ups. |
|
| 564 | - * |
|
| 565 | - * @param mixed $calendarId |
|
| 566 | - * @param string[] $uris |
|
| 567 | - * @return array |
|
| 568 | - */ |
|
| 569 | - function getMultipleCalendarObjects($calendarId, array $uris) { |
|
| 570 | - if (empty($uris)) { |
|
| 571 | - return []; |
|
| 572 | - } |
|
| 573 | - |
|
| 574 | - $chunks = array_chunk($uris, 100); |
|
| 575 | - $objects = []; |
|
| 576 | - |
|
| 577 | - $query = $this->db->getQueryBuilder(); |
|
| 578 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification']) |
|
| 579 | - ->from('calendarobjects') |
|
| 580 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 581 | - ->andWhere($query->expr()->in('uri', $query->createParameter('uri'))); |
|
| 582 | - |
|
| 583 | - foreach ($chunks as $uris) { |
|
| 584 | - $query->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY); |
|
| 585 | - $result = $query->execute(); |
|
| 586 | - |
|
| 587 | - while ($row = $result->fetch()) { |
|
| 588 | - $objects[] = [ |
|
| 589 | - 'id' => $row['id'], |
|
| 590 | - 'uri' => $row['uri'], |
|
| 591 | - 'lastmodified' => $row['lastmodified'], |
|
| 592 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 593 | - 'calendarid' => $row['calendarid'], |
|
| 594 | - 'size' => (int)$row['size'], |
|
| 595 | - 'calendardata' => $this->readBlob($row['calendardata']), |
|
| 596 | - 'component' => strtolower($row['componenttype']), |
|
| 597 | - 'classification' => (int)$row['classification'] |
|
| 598 | - ]; |
|
| 599 | - } |
|
| 600 | - $result->closeCursor(); |
|
| 601 | - } |
|
| 602 | - return $objects; |
|
| 603 | - } |
|
| 604 | - |
|
| 605 | - /** |
|
| 606 | - * Creates a new calendar object. |
|
| 607 | - * |
|
| 608 | - * The object uri is only the basename, or filename and not a full path. |
|
| 609 | - * |
|
| 610 | - * It is possible return an etag from this function, which will be used in |
|
| 611 | - * the response to this PUT request. Note that the ETag must be surrounded |
|
| 612 | - * by double-quotes. |
|
| 613 | - * |
|
| 614 | - * However, you should only really return this ETag if you don't mangle the |
|
| 615 | - * calendar-data. If the result of a subsequent GET to this object is not |
|
| 616 | - * the exact same as this request body, you should omit the ETag. |
|
| 617 | - * |
|
| 618 | - * @param mixed $calendarId |
|
| 619 | - * @param string $objectUri |
|
| 620 | - * @param string $calendarData |
|
| 621 | - * @return string |
|
| 622 | - */ |
|
| 623 | - function createCalendarObject($calendarId, $objectUri, $calendarData) { |
|
| 624 | - $extraData = $this->getDenormalizedData($calendarData); |
|
| 625 | - |
|
| 626 | - $query = $this->db->getQueryBuilder(); |
|
| 627 | - $query->insert('calendarobjects') |
|
| 628 | - ->values([ |
|
| 629 | - 'calendarid' => $query->createNamedParameter($calendarId), |
|
| 630 | - 'uri' => $query->createNamedParameter($objectUri), |
|
| 631 | - 'calendardata' => $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB), |
|
| 632 | - 'lastmodified' => $query->createNamedParameter(time()), |
|
| 633 | - 'etag' => $query->createNamedParameter($extraData['etag']), |
|
| 634 | - 'size' => $query->createNamedParameter($extraData['size']), |
|
| 635 | - 'componenttype' => $query->createNamedParameter($extraData['componentType']), |
|
| 636 | - 'firstoccurence' => $query->createNamedParameter($extraData['firstOccurence']), |
|
| 637 | - 'lastoccurence' => $query->createNamedParameter($extraData['lastOccurence']), |
|
| 638 | - 'classification' => $query->createNamedParameter($extraData['classification']), |
|
| 639 | - 'uid' => $query->createNamedParameter($extraData['uid']), |
|
| 640 | - ]) |
|
| 641 | - ->execute(); |
|
| 642 | - |
|
| 643 | - $this->addChange($calendarId, $objectUri, 1); |
|
| 644 | - |
|
| 645 | - return '"' . $extraData['etag'] . '"'; |
|
| 646 | - } |
|
| 647 | - |
|
| 648 | - /** |
|
| 649 | - * Updates an existing calendarobject, based on it's uri. |
|
| 650 | - * |
|
| 651 | - * The object uri is only the basename, or filename and not a full path. |
|
| 652 | - * |
|
| 653 | - * It is possible return an etag from this function, which will be used in |
|
| 654 | - * the response to this PUT request. Note that the ETag must be surrounded |
|
| 655 | - * by double-quotes. |
|
| 656 | - * |
|
| 657 | - * However, you should only really return this ETag if you don't mangle the |
|
| 658 | - * calendar-data. If the result of a subsequent GET to this object is not |
|
| 659 | - * the exact same as this request body, you should omit the ETag. |
|
| 660 | - * |
|
| 661 | - * @param mixed $calendarId |
|
| 662 | - * @param string $objectUri |
|
| 663 | - * @param string $calendarData |
|
| 664 | - * @return string |
|
| 665 | - */ |
|
| 666 | - function updateCalendarObject($calendarId, $objectUri, $calendarData) { |
|
| 667 | - $extraData = $this->getDenormalizedData($calendarData); |
|
| 668 | - |
|
| 669 | - $query = $this->db->getQueryBuilder(); |
|
| 670 | - $query->update('calendarobjects') |
|
| 671 | - ->set('calendardata', $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB)) |
|
| 672 | - ->set('lastmodified', $query->createNamedParameter(time())) |
|
| 673 | - ->set('etag', $query->createNamedParameter($extraData['etag'])) |
|
| 674 | - ->set('size', $query->createNamedParameter($extraData['size'])) |
|
| 675 | - ->set('componenttype', $query->createNamedParameter($extraData['componentType'])) |
|
| 676 | - ->set('firstoccurence', $query->createNamedParameter($extraData['firstOccurence'])) |
|
| 677 | - ->set('lastoccurence', $query->createNamedParameter($extraData['lastOccurence'])) |
|
| 678 | - ->set('classification', $query->createNamedParameter($extraData['classification'])) |
|
| 679 | - ->set('uid', $query->createNamedParameter($extraData['uid'])) |
|
| 680 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 681 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
| 682 | - ->execute(); |
|
| 683 | - |
|
| 684 | - $this->addChange($calendarId, $objectUri, 2); |
|
| 685 | - |
|
| 686 | - return '"' . $extraData['etag'] . '"'; |
|
| 687 | - } |
|
| 688 | - |
|
| 689 | - /** |
|
| 690 | - * @param int $calendarObjectId |
|
| 691 | - * @param int $classification |
|
| 692 | - */ |
|
| 693 | - public function setClassification($calendarObjectId, $classification) { |
|
| 694 | - if (!in_array($classification, [ |
|
| 695 | - self::CLASSIFICATION_PUBLIC, self::CLASSIFICATION_PRIVATE, self::CLASSIFICATION_CONFIDENTIAL |
|
| 696 | - ])) { |
|
| 697 | - throw new \InvalidArgumentException(); |
|
| 698 | - } |
|
| 699 | - $query = $this->db->getQueryBuilder(); |
|
| 700 | - $query->update('calendarobjects') |
|
| 701 | - ->set('classification', $query->createNamedParameter($classification)) |
|
| 702 | - ->where($query->expr()->eq('id', $query->createNamedParameter($calendarObjectId))) |
|
| 703 | - ->execute(); |
|
| 704 | - } |
|
| 705 | - |
|
| 706 | - /** |
|
| 707 | - * Deletes an existing calendar object. |
|
| 708 | - * |
|
| 709 | - * The object uri is only the basename, or filename and not a full path. |
|
| 710 | - * |
|
| 711 | - * @param mixed $calendarId |
|
| 712 | - * @param string $objectUri |
|
| 713 | - * @return void |
|
| 714 | - */ |
|
| 715 | - function deleteCalendarObject($calendarId, $objectUri) { |
|
| 716 | - $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ? AND `uri` = ?'); |
|
| 717 | - $stmt->execute([$calendarId, $objectUri]); |
|
| 718 | - |
|
| 719 | - $this->addChange($calendarId, $objectUri, 3); |
|
| 720 | - } |
|
| 721 | - |
|
| 722 | - /** |
|
| 723 | - * Performs a calendar-query on the contents of this calendar. |
|
| 724 | - * |
|
| 725 | - * The calendar-query is defined in RFC4791 : CalDAV. Using the |
|
| 726 | - * calendar-query it is possible for a client to request a specific set of |
|
| 727 | - * object, based on contents of iCalendar properties, date-ranges and |
|
| 728 | - * iCalendar component types (VTODO, VEVENT). |
|
| 729 | - * |
|
| 730 | - * This method should just return a list of (relative) urls that match this |
|
| 731 | - * query. |
|
| 732 | - * |
|
| 733 | - * The list of filters are specified as an array. The exact array is |
|
| 734 | - * documented by Sabre\CalDAV\CalendarQueryParser. |
|
| 735 | - * |
|
| 736 | - * Note that it is extremely likely that getCalendarObject for every path |
|
| 737 | - * returned from this method will be called almost immediately after. You |
|
| 738 | - * may want to anticipate this to speed up these requests. |
|
| 739 | - * |
|
| 740 | - * This method provides a default implementation, which parses *all* the |
|
| 741 | - * iCalendar objects in the specified calendar. |
|
| 742 | - * |
|
| 743 | - * This default may well be good enough for personal use, and calendars |
|
| 744 | - * that aren't very large. But if you anticipate high usage, big calendars |
|
| 745 | - * or high loads, you are strongly advised to optimize certain paths. |
|
| 746 | - * |
|
| 747 | - * The best way to do so is override this method and to optimize |
|
| 748 | - * specifically for 'common filters'. |
|
| 749 | - * |
|
| 750 | - * Requests that are extremely common are: |
|
| 751 | - * * requests for just VEVENTS |
|
| 752 | - * * requests for just VTODO |
|
| 753 | - * * requests with a time-range-filter on either VEVENT or VTODO. |
|
| 754 | - * |
|
| 755 | - * ..and combinations of these requests. It may not be worth it to try to |
|
| 756 | - * handle every possible situation and just rely on the (relatively |
|
| 757 | - * easy to use) CalendarQueryValidator to handle the rest. |
|
| 758 | - * |
|
| 759 | - * Note that especially time-range-filters may be difficult to parse. A |
|
| 760 | - * time-range filter specified on a VEVENT must for instance also handle |
|
| 761 | - * recurrence rules correctly. |
|
| 762 | - * A good example of how to interprete all these filters can also simply |
|
| 763 | - * be found in Sabre\CalDAV\CalendarQueryFilter. This class is as correct |
|
| 764 | - * as possible, so it gives you a good idea on what type of stuff you need |
|
| 765 | - * to think of. |
|
| 766 | - * |
|
| 767 | - * @param mixed $calendarId |
|
| 768 | - * @param array $filters |
|
| 769 | - * @return array |
|
| 770 | - */ |
|
| 771 | - function calendarQuery($calendarId, array $filters) { |
|
| 772 | - $componentType = null; |
|
| 773 | - $requirePostFilter = true; |
|
| 774 | - $timeRange = null; |
|
| 775 | - |
|
| 776 | - // if no filters were specified, we don't need to filter after a query |
|
| 777 | - if (!$filters['prop-filters'] && !$filters['comp-filters']) { |
|
| 778 | - $requirePostFilter = false; |
|
| 779 | - } |
|
| 780 | - |
|
| 781 | - // Figuring out if there's a component filter |
|
| 782 | - if (count($filters['comp-filters']) > 0 && !$filters['comp-filters'][0]['is-not-defined']) { |
|
| 783 | - $componentType = $filters['comp-filters'][0]['name']; |
|
| 784 | - |
|
| 785 | - // Checking if we need post-filters |
|
| 786 | - if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['time-range'] && !$filters['comp-filters'][0]['prop-filters']) { |
|
| 787 | - $requirePostFilter = false; |
|
| 788 | - } |
|
| 789 | - // There was a time-range filter |
|
| 790 | - if ($componentType == 'VEVENT' && isset($filters['comp-filters'][0]['time-range'])) { |
|
| 791 | - $timeRange = $filters['comp-filters'][0]['time-range']; |
|
| 792 | - |
|
| 793 | - // If start time OR the end time is not specified, we can do a |
|
| 794 | - // 100% accurate mysql query. |
|
| 795 | - if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['prop-filters'] && (!$timeRange['start'] || !$timeRange['end'])) { |
|
| 796 | - $requirePostFilter = false; |
|
| 797 | - } |
|
| 798 | - } |
|
| 799 | - |
|
| 800 | - } |
|
| 801 | - $columns = ['uri']; |
|
| 802 | - if ($requirePostFilter) { |
|
| 803 | - $columns = ['uri', 'calendardata']; |
|
| 804 | - } |
|
| 805 | - $query = $this->db->getQueryBuilder(); |
|
| 806 | - $query->select($columns) |
|
| 807 | - ->from('calendarobjects') |
|
| 808 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))); |
|
| 809 | - |
|
| 810 | - if ($componentType) { |
|
| 811 | - $query->andWhere($query->expr()->eq('componenttype', $query->createNamedParameter($componentType))); |
|
| 812 | - } |
|
| 813 | - |
|
| 814 | - if ($timeRange && $timeRange['start']) { |
|
| 815 | - $query->andWhere($query->expr()->gt('lastoccurence', $query->createNamedParameter($timeRange['start']->getTimeStamp()))); |
|
| 816 | - } |
|
| 817 | - if ($timeRange && $timeRange['end']) { |
|
| 818 | - $query->andWhere($query->expr()->lt('firstoccurence', $query->createNamedParameter($timeRange['end']->getTimeStamp()))); |
|
| 819 | - } |
|
| 820 | - |
|
| 821 | - $stmt = $query->execute(); |
|
| 822 | - |
|
| 823 | - $result = []; |
|
| 824 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 825 | - if ($requirePostFilter) { |
|
| 826 | - if (!$this->validateFilterForObject($row, $filters)) { |
|
| 827 | - continue; |
|
| 828 | - } |
|
| 829 | - } |
|
| 830 | - $result[] = $row['uri']; |
|
| 831 | - } |
|
| 832 | - |
|
| 833 | - return $result; |
|
| 834 | - } |
|
| 835 | - |
|
| 836 | - /** |
|
| 837 | - * Searches through all of a users calendars and calendar objects to find |
|
| 838 | - * an object with a specific UID. |
|
| 839 | - * |
|
| 840 | - * This method should return the path to this object, relative to the |
|
| 841 | - * calendar home, so this path usually only contains two parts: |
|
| 842 | - * |
|
| 843 | - * calendarpath/objectpath.ics |
|
| 844 | - * |
|
| 845 | - * If the uid is not found, return null. |
|
| 846 | - * |
|
| 847 | - * This method should only consider * objects that the principal owns, so |
|
| 848 | - * any calendars owned by other principals that also appear in this |
|
| 849 | - * collection should be ignored. |
|
| 850 | - * |
|
| 851 | - * @param string $principalUri |
|
| 852 | - * @param string $uid |
|
| 853 | - * @return string|null |
|
| 854 | - */ |
|
| 855 | - function getCalendarObjectByUID($principalUri, $uid) { |
|
| 856 | - |
|
| 857 | - $query = $this->db->getQueryBuilder(); |
|
| 858 | - $query->selectAlias('c.uri', 'calendaruri')->selectAlias('co.uri', 'objecturi') |
|
| 859 | - ->from('calendarobjects', 'co') |
|
| 860 | - ->leftJoin('co', 'calendars', 'c', $query->expr()->eq('co.calendarid', 'c.id')) |
|
| 861 | - ->where($query->expr()->eq('c.principaluri', $query->createNamedParameter($principalUri))) |
|
| 862 | - ->andWhere($query->expr()->eq('co.uid', $query->createNamedParameter($uid))); |
|
| 863 | - |
|
| 864 | - $stmt = $query->execute(); |
|
| 865 | - |
|
| 866 | - if ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 867 | - return $row['calendaruri'] . '/' . $row['objecturi']; |
|
| 868 | - } |
|
| 869 | - |
|
| 870 | - return null; |
|
| 871 | - } |
|
| 872 | - |
|
| 873 | - /** |
|
| 874 | - * The getChanges method returns all the changes that have happened, since |
|
| 875 | - * the specified syncToken in the specified calendar. |
|
| 876 | - * |
|
| 877 | - * This function should return an array, such as the following: |
|
| 878 | - * |
|
| 879 | - * [ |
|
| 880 | - * 'syncToken' => 'The current synctoken', |
|
| 881 | - * 'added' => [ |
|
| 882 | - * 'new.txt', |
|
| 883 | - * ], |
|
| 884 | - * 'modified' => [ |
|
| 885 | - * 'modified.txt', |
|
| 886 | - * ], |
|
| 887 | - * 'deleted' => [ |
|
| 888 | - * 'foo.php.bak', |
|
| 889 | - * 'old.txt' |
|
| 890 | - * ] |
|
| 891 | - * ); |
|
| 892 | - * |
|
| 893 | - * The returned syncToken property should reflect the *current* syncToken |
|
| 894 | - * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
|
| 895 | - * property This is * needed here too, to ensure the operation is atomic. |
|
| 896 | - * |
|
| 897 | - * If the $syncToken argument is specified as null, this is an initial |
|
| 898 | - * sync, and all members should be reported. |
|
| 899 | - * |
|
| 900 | - * The modified property is an array of nodenames that have changed since |
|
| 901 | - * the last token. |
|
| 902 | - * |
|
| 903 | - * The deleted property is an array with nodenames, that have been deleted |
|
| 904 | - * from collection. |
|
| 905 | - * |
|
| 906 | - * The $syncLevel argument is basically the 'depth' of the report. If it's |
|
| 907 | - * 1, you only have to report changes that happened only directly in |
|
| 908 | - * immediate descendants. If it's 2, it should also include changes from |
|
| 909 | - * the nodes below the child collections. (grandchildren) |
|
| 910 | - * |
|
| 911 | - * The $limit argument allows a client to specify how many results should |
|
| 912 | - * be returned at most. If the limit is not specified, it should be treated |
|
| 913 | - * as infinite. |
|
| 914 | - * |
|
| 915 | - * If the limit (infinite or not) is higher than you're willing to return, |
|
| 916 | - * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
|
| 917 | - * |
|
| 918 | - * If the syncToken is expired (due to data cleanup) or unknown, you must |
|
| 919 | - * return null. |
|
| 920 | - * |
|
| 921 | - * The limit is 'suggestive'. You are free to ignore it. |
|
| 922 | - * |
|
| 923 | - * @param string $calendarId |
|
| 924 | - * @param string $syncToken |
|
| 925 | - * @param int $syncLevel |
|
| 926 | - * @param int $limit |
|
| 927 | - * @return array |
|
| 928 | - */ |
|
| 929 | - function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null) { |
|
| 930 | - // Current synctoken |
|
| 931 | - $stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*calendars` WHERE `id` = ?'); |
|
| 932 | - $stmt->execute([ $calendarId ]); |
|
| 933 | - $currentToken = $stmt->fetchColumn(0); |
|
| 934 | - |
|
| 935 | - if (is_null($currentToken)) { |
|
| 936 | - return null; |
|
| 937 | - } |
|
| 938 | - |
|
| 939 | - $result = [ |
|
| 940 | - 'syncToken' => $currentToken, |
|
| 941 | - 'added' => [], |
|
| 942 | - 'modified' => [], |
|
| 943 | - 'deleted' => [], |
|
| 944 | - ]; |
|
| 945 | - |
|
| 946 | - if ($syncToken) { |
|
| 947 | - |
|
| 948 | - $query = "SELECT `uri`, `operation` FROM `*PREFIX*calendarchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `calendarid` = ? ORDER BY `synctoken`"; |
|
| 949 | - if ($limit>0) { |
|
| 950 | - $query.= " `LIMIT` " . (int)$limit; |
|
| 951 | - } |
|
| 952 | - |
|
| 953 | - // Fetching all changes |
|
| 954 | - $stmt = $this->db->prepare($query); |
|
| 955 | - $stmt->execute([$syncToken, $currentToken, $calendarId]); |
|
| 956 | - |
|
| 957 | - $changes = []; |
|
| 958 | - |
|
| 959 | - // This loop ensures that any duplicates are overwritten, only the |
|
| 960 | - // last change on a node is relevant. |
|
| 961 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 962 | - |
|
| 963 | - $changes[$row['uri']] = $row['operation']; |
|
| 964 | - |
|
| 965 | - } |
|
| 966 | - |
|
| 967 | - foreach($changes as $uri => $operation) { |
|
| 968 | - |
|
| 969 | - switch($operation) { |
|
| 970 | - case 1 : |
|
| 971 | - $result['added'][] = $uri; |
|
| 972 | - break; |
|
| 973 | - case 2 : |
|
| 974 | - $result['modified'][] = $uri; |
|
| 975 | - break; |
|
| 976 | - case 3 : |
|
| 977 | - $result['deleted'][] = $uri; |
|
| 978 | - break; |
|
| 979 | - } |
|
| 980 | - |
|
| 981 | - } |
|
| 982 | - } else { |
|
| 983 | - // No synctoken supplied, this is the initial sync. |
|
| 984 | - $query = "SELECT `uri` FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ?"; |
|
| 985 | - $stmt = $this->db->prepare($query); |
|
| 986 | - $stmt->execute([$calendarId]); |
|
| 987 | - |
|
| 988 | - $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
| 989 | - } |
|
| 990 | - return $result; |
|
| 991 | - |
|
| 992 | - } |
|
| 993 | - |
|
| 994 | - /** |
|
| 995 | - * Returns a list of subscriptions for a principal. |
|
| 996 | - * |
|
| 997 | - * Every subscription is an array with the following keys: |
|
| 998 | - * * id, a unique id that will be used by other functions to modify the |
|
| 999 | - * subscription. This can be the same as the uri or a database key. |
|
| 1000 | - * * uri. This is just the 'base uri' or 'filename' of the subscription. |
|
| 1001 | - * * principaluri. The owner of the subscription. Almost always the same as |
|
| 1002 | - * principalUri passed to this method. |
|
| 1003 | - * |
|
| 1004 | - * Furthermore, all the subscription info must be returned too: |
|
| 1005 | - * |
|
| 1006 | - * 1. {DAV:}displayname |
|
| 1007 | - * 2. {http://apple.com/ns/ical/}refreshrate |
|
| 1008 | - * 3. {http://calendarserver.org/ns/}subscribed-strip-todos (omit if todos |
|
| 1009 | - * should not be stripped). |
|
| 1010 | - * 4. {http://calendarserver.org/ns/}subscribed-strip-alarms (omit if alarms |
|
| 1011 | - * should not be stripped). |
|
| 1012 | - * 5. {http://calendarserver.org/ns/}subscribed-strip-attachments (omit if |
|
| 1013 | - * attachments should not be stripped). |
|
| 1014 | - * 6. {http://calendarserver.org/ns/}source (Must be a |
|
| 1015 | - * Sabre\DAV\Property\Href). |
|
| 1016 | - * 7. {http://apple.com/ns/ical/}calendar-color |
|
| 1017 | - * 8. {http://apple.com/ns/ical/}calendar-order |
|
| 1018 | - * 9. {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
|
| 1019 | - * (should just be an instance of |
|
| 1020 | - * Sabre\CalDAV\Property\SupportedCalendarComponentSet, with a bunch of |
|
| 1021 | - * default components). |
|
| 1022 | - * |
|
| 1023 | - * @param string $principalUri |
|
| 1024 | - * @return array |
|
| 1025 | - */ |
|
| 1026 | - function getSubscriptionsForUser($principalUri) { |
|
| 1027 | - $fields = array_values($this->subscriptionPropertyMap); |
|
| 1028 | - $fields[] = 'id'; |
|
| 1029 | - $fields[] = 'uri'; |
|
| 1030 | - $fields[] = 'source'; |
|
| 1031 | - $fields[] = 'principaluri'; |
|
| 1032 | - $fields[] = 'lastmodified'; |
|
| 1033 | - |
|
| 1034 | - $query = $this->db->getQueryBuilder(); |
|
| 1035 | - $query->select($fields) |
|
| 1036 | - ->from('calendarsubscriptions') |
|
| 1037 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 1038 | - ->orderBy('calendarorder', 'asc'); |
|
| 1039 | - $stmt =$query->execute(); |
|
| 1040 | - |
|
| 1041 | - $subscriptions = []; |
|
| 1042 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 1043 | - |
|
| 1044 | - $subscription = [ |
|
| 1045 | - 'id' => $row['id'], |
|
| 1046 | - 'uri' => $row['uri'], |
|
| 1047 | - 'principaluri' => $row['principaluri'], |
|
| 1048 | - 'source' => $row['source'], |
|
| 1049 | - 'lastmodified' => $row['lastmodified'], |
|
| 1050 | - |
|
| 1051 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), |
|
| 1052 | - ]; |
|
| 1053 | - |
|
| 1054 | - foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) { |
|
| 1055 | - if (!is_null($row[$dbName])) { |
|
| 1056 | - $subscription[$xmlName] = $row[$dbName]; |
|
| 1057 | - } |
|
| 1058 | - } |
|
| 1059 | - |
|
| 1060 | - $subscriptions[] = $subscription; |
|
| 1061 | - |
|
| 1062 | - } |
|
| 1063 | - |
|
| 1064 | - return $subscriptions; |
|
| 1065 | - } |
|
| 1066 | - |
|
| 1067 | - /** |
|
| 1068 | - * Creates a new subscription for a principal. |
|
| 1069 | - * |
|
| 1070 | - * If the creation was a success, an id must be returned that can be used to reference |
|
| 1071 | - * this subscription in other methods, such as updateSubscription. |
|
| 1072 | - * |
|
| 1073 | - * @param string $principalUri |
|
| 1074 | - * @param string $uri |
|
| 1075 | - * @param array $properties |
|
| 1076 | - * @return mixed |
|
| 1077 | - */ |
|
| 1078 | - function createSubscription($principalUri, $uri, array $properties) { |
|
| 1079 | - |
|
| 1080 | - if (!isset($properties['{http://calendarserver.org/ns/}source'])) { |
|
| 1081 | - throw new Forbidden('The {http://calendarserver.org/ns/}source property is required when creating subscriptions'); |
|
| 1082 | - } |
|
| 1083 | - |
|
| 1084 | - $values = [ |
|
| 1085 | - 'principaluri' => $principalUri, |
|
| 1086 | - 'uri' => $uri, |
|
| 1087 | - 'source' => $properties['{http://calendarserver.org/ns/}source']->getHref(), |
|
| 1088 | - 'lastmodified' => time(), |
|
| 1089 | - ]; |
|
| 1090 | - |
|
| 1091 | - $propertiesBoolean = ['striptodos', 'stripalarms', 'stripattachments']; |
|
| 1092 | - |
|
| 1093 | - foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) { |
|
| 1094 | - if (array_key_exists($xmlName, $properties)) { |
|
| 1095 | - $values[$dbName] = $properties[$xmlName]; |
|
| 1096 | - if (in_array($dbName, $propertiesBoolean)) { |
|
| 1097 | - $values[$dbName] = true; |
|
| 1098 | - } |
|
| 1099 | - } |
|
| 1100 | - } |
|
| 1101 | - |
|
| 1102 | - $valuesToInsert = array(); |
|
| 1103 | - |
|
| 1104 | - $query = $this->db->getQueryBuilder(); |
|
| 1105 | - |
|
| 1106 | - foreach (array_keys($values) as $name) { |
|
| 1107 | - $valuesToInsert[$name] = $query->createNamedParameter($values[$name]); |
|
| 1108 | - } |
|
| 1109 | - |
|
| 1110 | - $query->insert('calendarsubscriptions') |
|
| 1111 | - ->values($valuesToInsert) |
|
| 1112 | - ->execute(); |
|
| 1113 | - |
|
| 1114 | - return $this->db->lastInsertId('*PREFIX*calendarsubscriptions'); |
|
| 1115 | - } |
|
| 1116 | - |
|
| 1117 | - /** |
|
| 1118 | - * Updates a subscription |
|
| 1119 | - * |
|
| 1120 | - * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
| 1121 | - * To do the actual updates, you must tell this object which properties |
|
| 1122 | - * you're going to process with the handle() method. |
|
| 1123 | - * |
|
| 1124 | - * Calling the handle method is like telling the PropPatch object "I |
|
| 1125 | - * promise I can handle updating this property". |
|
| 1126 | - * |
|
| 1127 | - * Read the PropPatch documentation for more info and examples. |
|
| 1128 | - * |
|
| 1129 | - * @param mixed $subscriptionId |
|
| 1130 | - * @param PropPatch $propPatch |
|
| 1131 | - * @return void |
|
| 1132 | - */ |
|
| 1133 | - function updateSubscription($subscriptionId, PropPatch $propPatch) { |
|
| 1134 | - $supportedProperties = array_keys($this->subscriptionPropertyMap); |
|
| 1135 | - $supportedProperties[] = '{http://calendarserver.org/ns/}source'; |
|
| 1136 | - |
|
| 1137 | - $propPatch->handle($supportedProperties, function($mutations) use ($subscriptionId) { |
|
| 1138 | - |
|
| 1139 | - $newValues = []; |
|
| 1140 | - |
|
| 1141 | - foreach($mutations as $propertyName=>$propertyValue) { |
|
| 1142 | - if ($propertyName === '{http://calendarserver.org/ns/}source') { |
|
| 1143 | - $newValues['source'] = $propertyValue->getHref(); |
|
| 1144 | - } else { |
|
| 1145 | - $fieldName = $this->subscriptionPropertyMap[$propertyName]; |
|
| 1146 | - $newValues[$fieldName] = $propertyValue; |
|
| 1147 | - } |
|
| 1148 | - } |
|
| 1149 | - |
|
| 1150 | - $query = $this->db->getQueryBuilder(); |
|
| 1151 | - $query->update('calendarsubscriptions') |
|
| 1152 | - ->set('lastmodified', $query->createNamedParameter(time())); |
|
| 1153 | - foreach($newValues as $fieldName=>$value) { |
|
| 1154 | - $query->set($fieldName, $query->createNamedParameter($value)); |
|
| 1155 | - } |
|
| 1156 | - $query->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
| 1157 | - ->execute(); |
|
| 1158 | - |
|
| 1159 | - return true; |
|
| 1160 | - |
|
| 1161 | - }); |
|
| 1162 | - } |
|
| 1163 | - |
|
| 1164 | - /** |
|
| 1165 | - * Deletes a subscription. |
|
| 1166 | - * |
|
| 1167 | - * @param mixed $subscriptionId |
|
| 1168 | - * @return void |
|
| 1169 | - */ |
|
| 1170 | - function deleteSubscription($subscriptionId) { |
|
| 1171 | - $query = $this->db->getQueryBuilder(); |
|
| 1172 | - $query->delete('calendarsubscriptions') |
|
| 1173 | - ->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
| 1174 | - ->execute(); |
|
| 1175 | - } |
|
| 1176 | - |
|
| 1177 | - /** |
|
| 1178 | - * Returns a single scheduling object for the inbox collection. |
|
| 1179 | - * |
|
| 1180 | - * The returned array should contain the following elements: |
|
| 1181 | - * * uri - A unique basename for the object. This will be used to |
|
| 1182 | - * construct a full uri. |
|
| 1183 | - * * calendardata - The iCalendar object |
|
| 1184 | - * * lastmodified - The last modification date. Can be an int for a unix |
|
| 1185 | - * timestamp, or a PHP DateTime object. |
|
| 1186 | - * * etag - A unique token that must change if the object changed. |
|
| 1187 | - * * size - The size of the object, in bytes. |
|
| 1188 | - * |
|
| 1189 | - * @param string $principalUri |
|
| 1190 | - * @param string $objectUri |
|
| 1191 | - * @return array |
|
| 1192 | - */ |
|
| 1193 | - function getSchedulingObject($principalUri, $objectUri) { |
|
| 1194 | - $query = $this->db->getQueryBuilder(); |
|
| 1195 | - $stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size']) |
|
| 1196 | - ->from('schedulingobjects') |
|
| 1197 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 1198 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
| 1199 | - ->execute(); |
|
| 1200 | - |
|
| 1201 | - $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
| 1202 | - |
|
| 1203 | - if(!$row) { |
|
| 1204 | - return null; |
|
| 1205 | - } |
|
| 1206 | - |
|
| 1207 | - return [ |
|
| 1208 | - 'uri' => $row['uri'], |
|
| 1209 | - 'calendardata' => $row['calendardata'], |
|
| 1210 | - 'lastmodified' => $row['lastmodified'], |
|
| 1211 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 1212 | - 'size' => (int)$row['size'], |
|
| 1213 | - ]; |
|
| 1214 | - } |
|
| 1215 | - |
|
| 1216 | - /** |
|
| 1217 | - * Returns all scheduling objects for the inbox collection. |
|
| 1218 | - * |
|
| 1219 | - * These objects should be returned as an array. Every item in the array |
|
| 1220 | - * should follow the same structure as returned from getSchedulingObject. |
|
| 1221 | - * |
|
| 1222 | - * The main difference is that 'calendardata' is optional. |
|
| 1223 | - * |
|
| 1224 | - * @param string $principalUri |
|
| 1225 | - * @return array |
|
| 1226 | - */ |
|
| 1227 | - function getSchedulingObjects($principalUri) { |
|
| 1228 | - $query = $this->db->getQueryBuilder(); |
|
| 1229 | - $stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size']) |
|
| 1230 | - ->from('schedulingobjects') |
|
| 1231 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 1232 | - ->execute(); |
|
| 1233 | - |
|
| 1234 | - $result = []; |
|
| 1235 | - foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) { |
|
| 1236 | - $result[] = [ |
|
| 1237 | - 'calendardata' => $row['calendardata'], |
|
| 1238 | - 'uri' => $row['uri'], |
|
| 1239 | - 'lastmodified' => $row['lastmodified'], |
|
| 1240 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 1241 | - 'size' => (int)$row['size'], |
|
| 1242 | - ]; |
|
| 1243 | - } |
|
| 1244 | - |
|
| 1245 | - return $result; |
|
| 1246 | - } |
|
| 1247 | - |
|
| 1248 | - /** |
|
| 1249 | - * Deletes a scheduling object from the inbox collection. |
|
| 1250 | - * |
|
| 1251 | - * @param string $principalUri |
|
| 1252 | - * @param string $objectUri |
|
| 1253 | - * @return void |
|
| 1254 | - */ |
|
| 1255 | - function deleteSchedulingObject($principalUri, $objectUri) { |
|
| 1256 | - $query = $this->db->getQueryBuilder(); |
|
| 1257 | - $query->delete('schedulingobjects') |
|
| 1258 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 1259 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
| 1260 | - ->execute(); |
|
| 1261 | - } |
|
| 1262 | - |
|
| 1263 | - /** |
|
| 1264 | - * Creates a new scheduling object. This should land in a users' inbox. |
|
| 1265 | - * |
|
| 1266 | - * @param string $principalUri |
|
| 1267 | - * @param string $objectUri |
|
| 1268 | - * @param string $objectData |
|
| 1269 | - * @return void |
|
| 1270 | - */ |
|
| 1271 | - function createSchedulingObject($principalUri, $objectUri, $objectData) { |
|
| 1272 | - $query = $this->db->getQueryBuilder(); |
|
| 1273 | - $query->insert('schedulingobjects') |
|
| 1274 | - ->values([ |
|
| 1275 | - 'principaluri' => $query->createNamedParameter($principalUri), |
|
| 1276 | - 'calendardata' => $query->createNamedParameter($objectData), |
|
| 1277 | - 'uri' => $query->createNamedParameter($objectUri), |
|
| 1278 | - 'lastmodified' => $query->createNamedParameter(time()), |
|
| 1279 | - 'etag' => $query->createNamedParameter(md5($objectData)), |
|
| 1280 | - 'size' => $query->createNamedParameter(strlen($objectData)) |
|
| 1281 | - ]) |
|
| 1282 | - ->execute(); |
|
| 1283 | - } |
|
| 1284 | - |
|
| 1285 | - /** |
|
| 1286 | - * Adds a change record to the calendarchanges table. |
|
| 1287 | - * |
|
| 1288 | - * @param mixed $calendarId |
|
| 1289 | - * @param string $objectUri |
|
| 1290 | - * @param int $operation 1 = add, 2 = modify, 3 = delete. |
|
| 1291 | - * @return void |
|
| 1292 | - */ |
|
| 1293 | - protected function addChange($calendarId, $objectUri, $operation) { |
|
| 1294 | - |
|
| 1295 | - $stmt = $this->db->prepare('INSERT INTO `*PREFIX*calendarchanges` (`uri`, `synctoken`, `calendarid`, `operation`) SELECT ?, `synctoken`, ?, ? FROM `*PREFIX*calendars` WHERE `id` = ?'); |
|
| 1296 | - $stmt->execute([ |
|
| 1297 | - $objectUri, |
|
| 1298 | - $calendarId, |
|
| 1299 | - $operation, |
|
| 1300 | - $calendarId |
|
| 1301 | - ]); |
|
| 1302 | - $stmt = $this->db->prepare('UPDATE `*PREFIX*calendars` SET `synctoken` = `synctoken` + 1 WHERE `id` = ?'); |
|
| 1303 | - $stmt->execute([ |
|
| 1304 | - $calendarId |
|
| 1305 | - ]); |
|
| 1306 | - |
|
| 1307 | - } |
|
| 1308 | - |
|
| 1309 | - /** |
|
| 1310 | - * Parses some information from calendar objects, used for optimized |
|
| 1311 | - * calendar-queries. |
|
| 1312 | - * |
|
| 1313 | - * Returns an array with the following keys: |
|
| 1314 | - * * etag - An md5 checksum of the object without the quotes. |
|
| 1315 | - * * size - Size of the object in bytes |
|
| 1316 | - * * componentType - VEVENT, VTODO or VJOURNAL |
|
| 1317 | - * * firstOccurence |
|
| 1318 | - * * lastOccurence |
|
| 1319 | - * * uid - value of the UID property |
|
| 1320 | - * |
|
| 1321 | - * @param string $calendarData |
|
| 1322 | - * @return array |
|
| 1323 | - */ |
|
| 1324 | - public function getDenormalizedData($calendarData) { |
|
| 1325 | - |
|
| 1326 | - $vObject = Reader::read($calendarData); |
|
| 1327 | - $componentType = null; |
|
| 1328 | - $component = null; |
|
| 1329 | - $firstOccurrence = null; |
|
| 1330 | - $lastOccurrence = null; |
|
| 1331 | - $uid = null; |
|
| 1332 | - $classification = self::CLASSIFICATION_PUBLIC; |
|
| 1333 | - foreach($vObject->getComponents() as $component) { |
|
| 1334 | - if ($component->name!=='VTIMEZONE') { |
|
| 1335 | - $componentType = $component->name; |
|
| 1336 | - $uid = (string)$component->UID; |
|
| 1337 | - break; |
|
| 1338 | - } |
|
| 1339 | - } |
|
| 1340 | - if (!$componentType) { |
|
| 1341 | - throw new \Sabre\DAV\Exception\BadRequest('Calendar objects must have a VJOURNAL, VEVENT or VTODO component'); |
|
| 1342 | - } |
|
| 1343 | - if ($componentType === 'VEVENT' && $component->DTSTART) { |
|
| 1344 | - $firstOccurrence = $component->DTSTART->getDateTime()->getTimeStamp(); |
|
| 1345 | - // Finding the last occurrence is a bit harder |
|
| 1346 | - if (!isset($component->RRULE)) { |
|
| 1347 | - if (isset($component->DTEND)) { |
|
| 1348 | - $lastOccurrence = $component->DTEND->getDateTime()->getTimeStamp(); |
|
| 1349 | - } elseif (isset($component->DURATION)) { |
|
| 1350 | - $endDate = clone $component->DTSTART->getDateTime(); |
|
| 1351 | - $endDate->add(DateTimeParser::parse($component->DURATION->getValue())); |
|
| 1352 | - $lastOccurrence = $endDate->getTimeStamp(); |
|
| 1353 | - } elseif (!$component->DTSTART->hasTime()) { |
|
| 1354 | - $endDate = clone $component->DTSTART->getDateTime(); |
|
| 1355 | - $endDate->modify('+1 day'); |
|
| 1356 | - $lastOccurrence = $endDate->getTimeStamp(); |
|
| 1357 | - } else { |
|
| 1358 | - $lastOccurrence = $firstOccurrence; |
|
| 1359 | - } |
|
| 1360 | - } else { |
|
| 1361 | - $it = new EventIterator($vObject, (string)$component->UID); |
|
| 1362 | - $maxDate = new \DateTime(self::MAX_DATE); |
|
| 1363 | - if ($it->isInfinite()) { |
|
| 1364 | - $lastOccurrence = $maxDate->getTimeStamp(); |
|
| 1365 | - } else { |
|
| 1366 | - $end = $it->getDtEnd(); |
|
| 1367 | - while($it->valid() && $end < $maxDate) { |
|
| 1368 | - $end = $it->getDtEnd(); |
|
| 1369 | - $it->next(); |
|
| 1370 | - |
|
| 1371 | - } |
|
| 1372 | - $lastOccurrence = $end->getTimeStamp(); |
|
| 1373 | - } |
|
| 1374 | - |
|
| 1375 | - } |
|
| 1376 | - } |
|
| 1377 | - |
|
| 1378 | - if ($component->CLASS) { |
|
| 1379 | - $classification = CalDavBackend::CLASSIFICATION_PRIVATE; |
|
| 1380 | - switch ($component->CLASS->getValue()) { |
|
| 1381 | - case 'PUBLIC': |
|
| 1382 | - $classification = CalDavBackend::CLASSIFICATION_PUBLIC; |
|
| 1383 | - break; |
|
| 1384 | - case 'CONFIDENTIAL': |
|
| 1385 | - $classification = CalDavBackend::CLASSIFICATION_CONFIDENTIAL; |
|
| 1386 | - break; |
|
| 1387 | - } |
|
| 1388 | - } |
|
| 1389 | - return [ |
|
| 1390 | - 'etag' => md5($calendarData), |
|
| 1391 | - 'size' => strlen($calendarData), |
|
| 1392 | - 'componentType' => $componentType, |
|
| 1393 | - 'firstOccurence' => is_null($firstOccurrence) ? null : max(0, $firstOccurrence), |
|
| 1394 | - 'lastOccurence' => $lastOccurrence, |
|
| 1395 | - 'uid' => $uid, |
|
| 1396 | - 'classification' => $classification |
|
| 1397 | - ]; |
|
| 1398 | - |
|
| 1399 | - } |
|
| 1400 | - |
|
| 1401 | - private function readBlob($cardData) { |
|
| 1402 | - if (is_resource($cardData)) { |
|
| 1403 | - return stream_get_contents($cardData); |
|
| 1404 | - } |
|
| 1405 | - |
|
| 1406 | - return $cardData; |
|
| 1407 | - } |
|
| 1408 | - |
|
| 1409 | - /** |
|
| 1410 | - * @param IShareable $shareable |
|
| 1411 | - * @param array $add |
|
| 1412 | - * @param array $remove |
|
| 1413 | - */ |
|
| 1414 | - public function updateShares($shareable, $add, $remove) { |
|
| 1415 | - $this->sharingBackend->updateShares($shareable, $add, $remove); |
|
| 1416 | - } |
|
| 1417 | - |
|
| 1418 | - /** |
|
| 1419 | - * @param int $resourceId |
|
| 1420 | - * @return array |
|
| 1421 | - */ |
|
| 1422 | - public function getShares($resourceId) { |
|
| 1423 | - return $this->sharingBackend->getShares($resourceId); |
|
| 1424 | - } |
|
| 1425 | - |
|
| 1426 | - /** |
|
| 1427 | - * @param int $resourceId |
|
| 1428 | - * @param array $acl |
|
| 1429 | - * @return array |
|
| 1430 | - */ |
|
| 1431 | - public function applyShareAcl($resourceId, $acl) { |
|
| 1432 | - return $this->sharingBackend->applyShareAcl($resourceId, $acl); |
|
| 1433 | - } |
|
| 1434 | - |
|
| 1435 | - private function convertPrincipal($principalUri, $toV2) { |
|
| 1436 | - if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
|
| 1437 | - list(, $name) = URLUtil::splitPath($principalUri); |
|
| 1438 | - if ($toV2 === true) { |
|
| 1439 | - return "principals/users/$name"; |
|
| 1440 | - } |
|
| 1441 | - return "principals/$name"; |
|
| 1442 | - } |
|
| 1443 | - return $principalUri; |
|
| 1444 | - } |
|
| 57 | + /** |
|
| 58 | + * We need to specify a max date, because we need to stop *somewhere* |
|
| 59 | + * |
|
| 60 | + * On 32 bit system the maximum for a signed integer is 2147483647, so |
|
| 61 | + * MAX_DATE cannot be higher than date('Y-m-d', 2147483647) which results |
|
| 62 | + * in 2038-01-19 to avoid problems when the date is converted |
|
| 63 | + * to a unix timestamp. |
|
| 64 | + */ |
|
| 65 | + const MAX_DATE = '2038-01-01'; |
|
| 66 | + |
|
| 67 | + const CLASSIFICATION_PUBLIC = 0; |
|
| 68 | + const CLASSIFICATION_PRIVATE = 1; |
|
| 69 | + const CLASSIFICATION_CONFIDENTIAL = 2; |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * List of CalDAV properties, and how they map to database field names |
|
| 73 | + * Add your own properties by simply adding on to this array. |
|
| 74 | + * |
|
| 75 | + * Note that only string-based properties are supported here. |
|
| 76 | + * |
|
| 77 | + * @var array |
|
| 78 | + */ |
|
| 79 | + public $propertyMap = [ |
|
| 80 | + '{DAV:}displayname' => 'displayname', |
|
| 81 | + '{urn:ietf:params:xml:ns:caldav}calendar-description' => 'description', |
|
| 82 | + '{urn:ietf:params:xml:ns:caldav}calendar-timezone' => 'timezone', |
|
| 83 | + '{http://apple.com/ns/ical/}calendar-order' => 'calendarorder', |
|
| 84 | + '{http://apple.com/ns/ical/}calendar-color' => 'calendarcolor', |
|
| 85 | + ]; |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * List of subscription properties, and how they map to database field names. |
|
| 89 | + * |
|
| 90 | + * @var array |
|
| 91 | + */ |
|
| 92 | + public $subscriptionPropertyMap = [ |
|
| 93 | + '{DAV:}displayname' => 'displayname', |
|
| 94 | + '{http://apple.com/ns/ical/}refreshrate' => 'refreshrate', |
|
| 95 | + '{http://apple.com/ns/ical/}calendar-order' => 'calendarorder', |
|
| 96 | + '{http://apple.com/ns/ical/}calendar-color' => 'calendarcolor', |
|
| 97 | + '{http://calendarserver.org/ns/}subscribed-strip-todos' => 'striptodos', |
|
| 98 | + '{http://calendarserver.org/ns/}subscribed-strip-alarms' => 'stripalarms', |
|
| 99 | + '{http://calendarserver.org/ns/}subscribed-strip-attachments' => 'stripattachments', |
|
| 100 | + ]; |
|
| 101 | + |
|
| 102 | + /** @var IDBConnection */ |
|
| 103 | + private $db; |
|
| 104 | + |
|
| 105 | + /** @var Backend */ |
|
| 106 | + private $sharingBackend; |
|
| 107 | + |
|
| 108 | + /** @var Principal */ |
|
| 109 | + private $principalBackend; |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * CalDavBackend constructor. |
|
| 113 | + * |
|
| 114 | + * @param IDBConnection $db |
|
| 115 | + * @param Principal $principalBackend |
|
| 116 | + */ |
|
| 117 | + public function __construct(IDBConnection $db, Principal $principalBackend) { |
|
| 118 | + $this->db = $db; |
|
| 119 | + $this->principalBackend = $principalBackend; |
|
| 120 | + $this->sharingBackend = new Backend($this->db, $principalBackend, 'calendar'); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Returns a list of calendars for a principal. |
|
| 125 | + * |
|
| 126 | + * Every project is an array with the following keys: |
|
| 127 | + * * id, a unique id that will be used by other functions to modify the |
|
| 128 | + * calendar. This can be the same as the uri or a database key. |
|
| 129 | + * * uri, which the basename of the uri with which the calendar is |
|
| 130 | + * accessed. |
|
| 131 | + * * principaluri. The owner of the calendar. Almost always the same as |
|
| 132 | + * principalUri passed to this method. |
|
| 133 | + * |
|
| 134 | + * Furthermore it can contain webdav properties in clark notation. A very |
|
| 135 | + * common one is '{DAV:}displayname'. |
|
| 136 | + * |
|
| 137 | + * Many clients also require: |
|
| 138 | + * {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
|
| 139 | + * For this property, you can just return an instance of |
|
| 140 | + * Sabre\CalDAV\Property\SupportedCalendarComponentSet. |
|
| 141 | + * |
|
| 142 | + * If you return {http://sabredav.org/ns}read-only and set the value to 1, |
|
| 143 | + * ACL will automatically be put in read-only mode. |
|
| 144 | + * |
|
| 145 | + * @param string $principalUri |
|
| 146 | + * @return array |
|
| 147 | + */ |
|
| 148 | + function getCalendarsForUser($principalUri) { |
|
| 149 | + $principalUriOriginal = $principalUri; |
|
| 150 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 151 | + $fields = array_values($this->propertyMap); |
|
| 152 | + $fields[] = 'id'; |
|
| 153 | + $fields[] = 'uri'; |
|
| 154 | + $fields[] = 'synctoken'; |
|
| 155 | + $fields[] = 'components'; |
|
| 156 | + $fields[] = 'principaluri'; |
|
| 157 | + $fields[] = 'transparent'; |
|
| 158 | + |
|
| 159 | + // Making fields a comma-delimited list |
|
| 160 | + $query = $this->db->getQueryBuilder(); |
|
| 161 | + $query->select($fields)->from('calendars') |
|
| 162 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 163 | + ->orderBy('calendarorder', 'ASC'); |
|
| 164 | + $stmt = $query->execute(); |
|
| 165 | + |
|
| 166 | + $calendars = []; |
|
| 167 | + while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 168 | + |
|
| 169 | + $components = []; |
|
| 170 | + if ($row['components']) { |
|
| 171 | + $components = explode(',',$row['components']); |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + $calendar = [ |
|
| 175 | + 'id' => $row['id'], |
|
| 176 | + 'uri' => $row['uri'], |
|
| 177 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
|
| 178 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 179 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 180 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 181 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 182 | + ]; |
|
| 183 | + |
|
| 184 | + foreach($this->propertyMap as $xmlName=>$dbName) { |
|
| 185 | + $calendar[$xmlName] = $row[$dbName]; |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + if (!isset($calendars[$calendar['id']])) { |
|
| 189 | + $calendars[$calendar['id']] = $calendar; |
|
| 190 | + } |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + $stmt->closeCursor(); |
|
| 194 | + |
|
| 195 | + // query for shared calendars |
|
| 196 | + $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); |
|
| 197 | + $principals[]= $principalUri; |
|
| 198 | + |
|
| 199 | + $fields = array_values($this->propertyMap); |
|
| 200 | + $fields[] = 'a.id'; |
|
| 201 | + $fields[] = 'a.uri'; |
|
| 202 | + $fields[] = 'a.synctoken'; |
|
| 203 | + $fields[] = 'a.components'; |
|
| 204 | + $fields[] = 'a.principaluri'; |
|
| 205 | + $fields[] = 'a.transparent'; |
|
| 206 | + $fields[] = 's.access'; |
|
| 207 | + $query = $this->db->getQueryBuilder(); |
|
| 208 | + $result = $query->select($fields) |
|
| 209 | + ->from('dav_shares', 's') |
|
| 210 | + ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
| 211 | + ->where($query->expr()->in('s.principaluri', $query->createParameter('principaluri'))) |
|
| 212 | + ->andWhere($query->expr()->eq('s.type', $query->createParameter('type'))) |
|
| 213 | + ->setParameter('type', 'calendar') |
|
| 214 | + ->setParameter('principaluri', $principals, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY) |
|
| 215 | + ->execute(); |
|
| 216 | + |
|
| 217 | + while($row = $result->fetch()) { |
|
| 218 | + list(, $name) = URLUtil::splitPath($row['principaluri']); |
|
| 219 | + $uri = $row['uri'] . '_shared_by_' . $name; |
|
| 220 | + $row['displayname'] = $row['displayname'] . "($name)"; |
|
| 221 | + $components = []; |
|
| 222 | + if ($row['components']) { |
|
| 223 | + $components = explode(',',$row['components']); |
|
| 224 | + } |
|
| 225 | + $calendar = [ |
|
| 226 | + 'id' => $row['id'], |
|
| 227 | + 'uri' => $uri, |
|
| 228 | + 'principaluri' => $principalUri, |
|
| 229 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 230 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 231 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 232 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 233 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $row['principaluri'], |
|
| 234 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ, |
|
| 235 | + ]; |
|
| 236 | + |
|
| 237 | + foreach($this->propertyMap as $xmlName=>$dbName) { |
|
| 238 | + $calendar[$xmlName] = $row[$dbName]; |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + if (!isset($calendars[$calendar['id']])) { |
|
| 242 | + $calendars[$calendar['id']] = $calendar; |
|
| 243 | + } |
|
| 244 | + } |
|
| 245 | + $result->closeCursor(); |
|
| 246 | + |
|
| 247 | + return array_values($calendars); |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * @param string $principal |
|
| 252 | + * @param string $uri |
|
| 253 | + * @return array|null |
|
| 254 | + */ |
|
| 255 | + public function getCalendarByUri($principal, $uri) { |
|
| 256 | + $fields = array_values($this->propertyMap); |
|
| 257 | + $fields[] = 'id'; |
|
| 258 | + $fields[] = 'uri'; |
|
| 259 | + $fields[] = 'synctoken'; |
|
| 260 | + $fields[] = 'components'; |
|
| 261 | + $fields[] = 'principaluri'; |
|
| 262 | + $fields[] = 'transparent'; |
|
| 263 | + |
|
| 264 | + // Making fields a comma-delimited list |
|
| 265 | + $query = $this->db->getQueryBuilder(); |
|
| 266 | + $query->select($fields)->from('calendars') |
|
| 267 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 268 | + ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal))) |
|
| 269 | + ->setMaxResults(1); |
|
| 270 | + $stmt = $query->execute(); |
|
| 271 | + |
|
| 272 | + $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
| 273 | + $stmt->closeCursor(); |
|
| 274 | + if ($row === false) { |
|
| 275 | + return null; |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + $components = []; |
|
| 279 | + if ($row['components']) { |
|
| 280 | + $components = explode(',',$row['components']); |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + $calendar = [ |
|
| 284 | + 'id' => $row['id'], |
|
| 285 | + 'uri' => $row['uri'], |
|
| 286 | + 'principaluri' => $row['principaluri'], |
|
| 287 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 288 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 289 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 290 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 291 | + ]; |
|
| 292 | + |
|
| 293 | + foreach($this->propertyMap as $xmlName=>$dbName) { |
|
| 294 | + $calendar[$xmlName] = $row[$dbName]; |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + return $calendar; |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + public function getCalendarById($calendarId) { |
|
| 301 | + $fields = array_values($this->propertyMap); |
|
| 302 | + $fields[] = 'id'; |
|
| 303 | + $fields[] = 'uri'; |
|
| 304 | + $fields[] = 'synctoken'; |
|
| 305 | + $fields[] = 'components'; |
|
| 306 | + $fields[] = 'principaluri'; |
|
| 307 | + $fields[] = 'transparent'; |
|
| 308 | + |
|
| 309 | + // Making fields a comma-delimited list |
|
| 310 | + $query = $this->db->getQueryBuilder(); |
|
| 311 | + $query->select($fields)->from('calendars') |
|
| 312 | + ->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))) |
|
| 313 | + ->setMaxResults(1); |
|
| 314 | + $stmt = $query->execute(); |
|
| 315 | + |
|
| 316 | + $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
| 317 | + $stmt->closeCursor(); |
|
| 318 | + if ($row === false) { |
|
| 319 | + return null; |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + $components = []; |
|
| 323 | + if ($row['components']) { |
|
| 324 | + $components = explode(',',$row['components']); |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + $calendar = [ |
|
| 328 | + 'id' => $row['id'], |
|
| 329 | + 'uri' => $row['uri'], |
|
| 330 | + 'principaluri' => $row['principaluri'], |
|
| 331 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 332 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 333 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 334 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 335 | + ]; |
|
| 336 | + |
|
| 337 | + foreach($this->propertyMap as $xmlName=>$dbName) { |
|
| 338 | + $calendar[$xmlName] = $row[$dbName]; |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + return $calendar; |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + /** |
|
| 345 | + * Creates a new calendar for a principal. |
|
| 346 | + * |
|
| 347 | + * If the creation was a success, an id must be returned that can be used to reference |
|
| 348 | + * this calendar in other methods, such as updateCalendar. |
|
| 349 | + * |
|
| 350 | + * @param string $principalUri |
|
| 351 | + * @param string $calendarUri |
|
| 352 | + * @param array $properties |
|
| 353 | + * @return int |
|
| 354 | + */ |
|
| 355 | + function createCalendar($principalUri, $calendarUri, array $properties) { |
|
| 356 | + $values = [ |
|
| 357 | + 'principaluri' => $this->convertPrincipal($principalUri, true), |
|
| 358 | + 'uri' => $calendarUri, |
|
| 359 | + 'synctoken' => 1, |
|
| 360 | + 'transparent' => 0, |
|
| 361 | + 'components' => 'VEVENT,VTODO', |
|
| 362 | + 'displayname' => $calendarUri |
|
| 363 | + ]; |
|
| 364 | + |
|
| 365 | + // Default value |
|
| 366 | + $sccs = '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set'; |
|
| 367 | + if (isset($properties[$sccs])) { |
|
| 368 | + if (!($properties[$sccs] instanceof SupportedCalendarComponentSet)) { |
|
| 369 | + throw new DAV\Exception('The ' . $sccs . ' property must be of type: \Sabre\CalDAV\Property\SupportedCalendarComponentSet'); |
|
| 370 | + } |
|
| 371 | + $values['components'] = implode(',',$properties[$sccs]->getValue()); |
|
| 372 | + } |
|
| 373 | + $transp = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
| 374 | + if (isset($properties[$transp])) { |
|
| 375 | + $values['transparent'] = (int) ($properties[$transp]->getValue() === 'transparent'); |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + foreach($this->propertyMap as $xmlName=>$dbName) { |
|
| 379 | + if (isset($properties[$xmlName])) { |
|
| 380 | + $values[$dbName] = $properties[$xmlName]; |
|
| 381 | + } |
|
| 382 | + } |
|
| 383 | + |
|
| 384 | + $query = $this->db->getQueryBuilder(); |
|
| 385 | + $query->insert('calendars'); |
|
| 386 | + foreach($values as $column => $value) { |
|
| 387 | + $query->setValue($column, $query->createNamedParameter($value)); |
|
| 388 | + } |
|
| 389 | + $query->execute(); |
|
| 390 | + return $query->getLastInsertId(); |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + /** |
|
| 394 | + * Updates properties for a calendar. |
|
| 395 | + * |
|
| 396 | + * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
| 397 | + * To do the actual updates, you must tell this object which properties |
|
| 398 | + * you're going to process with the handle() method. |
|
| 399 | + * |
|
| 400 | + * Calling the handle method is like telling the PropPatch object "I |
|
| 401 | + * promise I can handle updating this property". |
|
| 402 | + * |
|
| 403 | + * Read the PropPatch documentation for more info and examples. |
|
| 404 | + * |
|
| 405 | + * @param PropPatch $propPatch |
|
| 406 | + * @return void |
|
| 407 | + */ |
|
| 408 | + function updateCalendar($calendarId, PropPatch $propPatch) { |
|
| 409 | + $supportedProperties = array_keys($this->propertyMap); |
|
| 410 | + $supportedProperties[] = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
| 411 | + |
|
| 412 | + $propPatch->handle($supportedProperties, function($mutations) use ($calendarId) { |
|
| 413 | + $newValues = []; |
|
| 414 | + foreach ($mutations as $propertyName => $propertyValue) { |
|
| 415 | + |
|
| 416 | + switch ($propertyName) { |
|
| 417 | + case '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' : |
|
| 418 | + $fieldName = 'transparent'; |
|
| 419 | + $newValues[$fieldName] = (int) ($propertyValue->getValue() === 'transparent'); |
|
| 420 | + break; |
|
| 421 | + default : |
|
| 422 | + $fieldName = $this->propertyMap[$propertyName]; |
|
| 423 | + $newValues[$fieldName] = $propertyValue; |
|
| 424 | + break; |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + } |
|
| 428 | + $query = $this->db->getQueryBuilder(); |
|
| 429 | + $query->update('calendars'); |
|
| 430 | + foreach ($newValues as $fieldName => $value) { |
|
| 431 | + $query->set($fieldName, $query->createNamedParameter($value)); |
|
| 432 | + } |
|
| 433 | + $query->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))); |
|
| 434 | + $query->execute(); |
|
| 435 | + |
|
| 436 | + $this->addChange($calendarId, "", 2); |
|
| 437 | + |
|
| 438 | + return true; |
|
| 439 | + }); |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + /** |
|
| 443 | + * Delete a calendar and all it's objects |
|
| 444 | + * |
|
| 445 | + * @param mixed $calendarId |
|
| 446 | + * @return void |
|
| 447 | + */ |
|
| 448 | + function deleteCalendar($calendarId) { |
|
| 449 | + $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ?'); |
|
| 450 | + $stmt->execute([$calendarId]); |
|
| 451 | + |
|
| 452 | + $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendars` WHERE `id` = ?'); |
|
| 453 | + $stmt->execute([$calendarId]); |
|
| 454 | + |
|
| 455 | + $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarchanges` WHERE `calendarid` = ?'); |
|
| 456 | + $stmt->execute([$calendarId]); |
|
| 457 | + |
|
| 458 | + $this->sharingBackend->deleteAllShares($calendarId); |
|
| 459 | + } |
|
| 460 | + |
|
| 461 | + /** |
|
| 462 | + * Returns all calendar objects within a calendar. |
|
| 463 | + * |
|
| 464 | + * Every item contains an array with the following keys: |
|
| 465 | + * * calendardata - The iCalendar-compatible calendar data |
|
| 466 | + * * uri - a unique key which will be used to construct the uri. This can |
|
| 467 | + * be any arbitrary string, but making sure it ends with '.ics' is a |
|
| 468 | + * good idea. This is only the basename, or filename, not the full |
|
| 469 | + * path. |
|
| 470 | + * * lastmodified - a timestamp of the last modification time |
|
| 471 | + * * etag - An arbitrary string, surrounded by double-quotes. (e.g.: |
|
| 472 | + * '"abcdef"') |
|
| 473 | + * * size - The size of the calendar objects, in bytes. |
|
| 474 | + * * component - optional, a string containing the type of object, such |
|
| 475 | + * as 'vevent' or 'vtodo'. If specified, this will be used to populate |
|
| 476 | + * the Content-Type header. |
|
| 477 | + * |
|
| 478 | + * Note that the etag is optional, but it's highly encouraged to return for |
|
| 479 | + * speed reasons. |
|
| 480 | + * |
|
| 481 | + * The calendardata is also optional. If it's not returned |
|
| 482 | + * 'getCalendarObject' will be called later, which *is* expected to return |
|
| 483 | + * calendardata. |
|
| 484 | + * |
|
| 485 | + * If neither etag or size are specified, the calendardata will be |
|
| 486 | + * used/fetched to determine these numbers. If both are specified the |
|
| 487 | + * amount of times this is needed is reduced by a great degree. |
|
| 488 | + * |
|
| 489 | + * @param mixed $calendarId |
|
| 490 | + * @return array |
|
| 491 | + */ |
|
| 492 | + function getCalendarObjects($calendarId) { |
|
| 493 | + $query = $this->db->getQueryBuilder(); |
|
| 494 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'componenttype', 'classification']) |
|
| 495 | + ->from('calendarobjects') |
|
| 496 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))); |
|
| 497 | + $stmt = $query->execute(); |
|
| 498 | + |
|
| 499 | + $result = []; |
|
| 500 | + foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) { |
|
| 501 | + $result[] = [ |
|
| 502 | + 'id' => $row['id'], |
|
| 503 | + 'uri' => $row['uri'], |
|
| 504 | + 'lastmodified' => $row['lastmodified'], |
|
| 505 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 506 | + 'calendarid' => $row['calendarid'], |
|
| 507 | + 'size' => (int)$row['size'], |
|
| 508 | + 'component' => strtolower($row['componenttype']), |
|
| 509 | + 'classification'=> (int)$row['classification'] |
|
| 510 | + ]; |
|
| 511 | + } |
|
| 512 | + |
|
| 513 | + return $result; |
|
| 514 | + } |
|
| 515 | + |
|
| 516 | + /** |
|
| 517 | + * Returns information from a single calendar object, based on it's object |
|
| 518 | + * uri. |
|
| 519 | + * |
|
| 520 | + * The object uri is only the basename, or filename and not a full path. |
|
| 521 | + * |
|
| 522 | + * The returned array must have the same keys as getCalendarObjects. The |
|
| 523 | + * 'calendardata' object is required here though, while it's not required |
|
| 524 | + * for getCalendarObjects. |
|
| 525 | + * |
|
| 526 | + * This method must return null if the object did not exist. |
|
| 527 | + * |
|
| 528 | + * @param mixed $calendarId |
|
| 529 | + * @param string $objectUri |
|
| 530 | + * @return array|null |
|
| 531 | + */ |
|
| 532 | + function getCalendarObject($calendarId, $objectUri) { |
|
| 533 | + |
|
| 534 | + $query = $this->db->getQueryBuilder(); |
|
| 535 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification']) |
|
| 536 | + ->from('calendarobjects') |
|
| 537 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 538 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))); |
|
| 539 | + $stmt = $query->execute(); |
|
| 540 | + $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
| 541 | + |
|
| 542 | + if(!$row) return null; |
|
| 543 | + |
|
| 544 | + return [ |
|
| 545 | + 'id' => $row['id'], |
|
| 546 | + 'uri' => $row['uri'], |
|
| 547 | + 'lastmodified' => $row['lastmodified'], |
|
| 548 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 549 | + 'calendarid' => $row['calendarid'], |
|
| 550 | + 'size' => (int)$row['size'], |
|
| 551 | + 'calendardata' => $this->readBlob($row['calendardata']), |
|
| 552 | + 'component' => strtolower($row['componenttype']), |
|
| 553 | + 'classification'=> (int)$row['classification'] |
|
| 554 | + ]; |
|
| 555 | + } |
|
| 556 | + |
|
| 557 | + /** |
|
| 558 | + * Returns a list of calendar objects. |
|
| 559 | + * |
|
| 560 | + * This method should work identical to getCalendarObject, but instead |
|
| 561 | + * return all the calendar objects in the list as an array. |
|
| 562 | + * |
|
| 563 | + * If the backend supports this, it may allow for some speed-ups. |
|
| 564 | + * |
|
| 565 | + * @param mixed $calendarId |
|
| 566 | + * @param string[] $uris |
|
| 567 | + * @return array |
|
| 568 | + */ |
|
| 569 | + function getMultipleCalendarObjects($calendarId, array $uris) { |
|
| 570 | + if (empty($uris)) { |
|
| 571 | + return []; |
|
| 572 | + } |
|
| 573 | + |
|
| 574 | + $chunks = array_chunk($uris, 100); |
|
| 575 | + $objects = []; |
|
| 576 | + |
|
| 577 | + $query = $this->db->getQueryBuilder(); |
|
| 578 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification']) |
|
| 579 | + ->from('calendarobjects') |
|
| 580 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 581 | + ->andWhere($query->expr()->in('uri', $query->createParameter('uri'))); |
|
| 582 | + |
|
| 583 | + foreach ($chunks as $uris) { |
|
| 584 | + $query->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY); |
|
| 585 | + $result = $query->execute(); |
|
| 586 | + |
|
| 587 | + while ($row = $result->fetch()) { |
|
| 588 | + $objects[] = [ |
|
| 589 | + 'id' => $row['id'], |
|
| 590 | + 'uri' => $row['uri'], |
|
| 591 | + 'lastmodified' => $row['lastmodified'], |
|
| 592 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 593 | + 'calendarid' => $row['calendarid'], |
|
| 594 | + 'size' => (int)$row['size'], |
|
| 595 | + 'calendardata' => $this->readBlob($row['calendardata']), |
|
| 596 | + 'component' => strtolower($row['componenttype']), |
|
| 597 | + 'classification' => (int)$row['classification'] |
|
| 598 | + ]; |
|
| 599 | + } |
|
| 600 | + $result->closeCursor(); |
|
| 601 | + } |
|
| 602 | + return $objects; |
|
| 603 | + } |
|
| 604 | + |
|
| 605 | + /** |
|
| 606 | + * Creates a new calendar object. |
|
| 607 | + * |
|
| 608 | + * The object uri is only the basename, or filename and not a full path. |
|
| 609 | + * |
|
| 610 | + * It is possible return an etag from this function, which will be used in |
|
| 611 | + * the response to this PUT request. Note that the ETag must be surrounded |
|
| 612 | + * by double-quotes. |
|
| 613 | + * |
|
| 614 | + * However, you should only really return this ETag if you don't mangle the |
|
| 615 | + * calendar-data. If the result of a subsequent GET to this object is not |
|
| 616 | + * the exact same as this request body, you should omit the ETag. |
|
| 617 | + * |
|
| 618 | + * @param mixed $calendarId |
|
| 619 | + * @param string $objectUri |
|
| 620 | + * @param string $calendarData |
|
| 621 | + * @return string |
|
| 622 | + */ |
|
| 623 | + function createCalendarObject($calendarId, $objectUri, $calendarData) { |
|
| 624 | + $extraData = $this->getDenormalizedData($calendarData); |
|
| 625 | + |
|
| 626 | + $query = $this->db->getQueryBuilder(); |
|
| 627 | + $query->insert('calendarobjects') |
|
| 628 | + ->values([ |
|
| 629 | + 'calendarid' => $query->createNamedParameter($calendarId), |
|
| 630 | + 'uri' => $query->createNamedParameter($objectUri), |
|
| 631 | + 'calendardata' => $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB), |
|
| 632 | + 'lastmodified' => $query->createNamedParameter(time()), |
|
| 633 | + 'etag' => $query->createNamedParameter($extraData['etag']), |
|
| 634 | + 'size' => $query->createNamedParameter($extraData['size']), |
|
| 635 | + 'componenttype' => $query->createNamedParameter($extraData['componentType']), |
|
| 636 | + 'firstoccurence' => $query->createNamedParameter($extraData['firstOccurence']), |
|
| 637 | + 'lastoccurence' => $query->createNamedParameter($extraData['lastOccurence']), |
|
| 638 | + 'classification' => $query->createNamedParameter($extraData['classification']), |
|
| 639 | + 'uid' => $query->createNamedParameter($extraData['uid']), |
|
| 640 | + ]) |
|
| 641 | + ->execute(); |
|
| 642 | + |
|
| 643 | + $this->addChange($calendarId, $objectUri, 1); |
|
| 644 | + |
|
| 645 | + return '"' . $extraData['etag'] . '"'; |
|
| 646 | + } |
|
| 647 | + |
|
| 648 | + /** |
|
| 649 | + * Updates an existing calendarobject, based on it's uri. |
|
| 650 | + * |
|
| 651 | + * The object uri is only the basename, or filename and not a full path. |
|
| 652 | + * |
|
| 653 | + * It is possible return an etag from this function, which will be used in |
|
| 654 | + * the response to this PUT request. Note that the ETag must be surrounded |
|
| 655 | + * by double-quotes. |
|
| 656 | + * |
|
| 657 | + * However, you should only really return this ETag if you don't mangle the |
|
| 658 | + * calendar-data. If the result of a subsequent GET to this object is not |
|
| 659 | + * the exact same as this request body, you should omit the ETag. |
|
| 660 | + * |
|
| 661 | + * @param mixed $calendarId |
|
| 662 | + * @param string $objectUri |
|
| 663 | + * @param string $calendarData |
|
| 664 | + * @return string |
|
| 665 | + */ |
|
| 666 | + function updateCalendarObject($calendarId, $objectUri, $calendarData) { |
|
| 667 | + $extraData = $this->getDenormalizedData($calendarData); |
|
| 668 | + |
|
| 669 | + $query = $this->db->getQueryBuilder(); |
|
| 670 | + $query->update('calendarobjects') |
|
| 671 | + ->set('calendardata', $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB)) |
|
| 672 | + ->set('lastmodified', $query->createNamedParameter(time())) |
|
| 673 | + ->set('etag', $query->createNamedParameter($extraData['etag'])) |
|
| 674 | + ->set('size', $query->createNamedParameter($extraData['size'])) |
|
| 675 | + ->set('componenttype', $query->createNamedParameter($extraData['componentType'])) |
|
| 676 | + ->set('firstoccurence', $query->createNamedParameter($extraData['firstOccurence'])) |
|
| 677 | + ->set('lastoccurence', $query->createNamedParameter($extraData['lastOccurence'])) |
|
| 678 | + ->set('classification', $query->createNamedParameter($extraData['classification'])) |
|
| 679 | + ->set('uid', $query->createNamedParameter($extraData['uid'])) |
|
| 680 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 681 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
| 682 | + ->execute(); |
|
| 683 | + |
|
| 684 | + $this->addChange($calendarId, $objectUri, 2); |
|
| 685 | + |
|
| 686 | + return '"' . $extraData['etag'] . '"'; |
|
| 687 | + } |
|
| 688 | + |
|
| 689 | + /** |
|
| 690 | + * @param int $calendarObjectId |
|
| 691 | + * @param int $classification |
|
| 692 | + */ |
|
| 693 | + public function setClassification($calendarObjectId, $classification) { |
|
| 694 | + if (!in_array($classification, [ |
|
| 695 | + self::CLASSIFICATION_PUBLIC, self::CLASSIFICATION_PRIVATE, self::CLASSIFICATION_CONFIDENTIAL |
|
| 696 | + ])) { |
|
| 697 | + throw new \InvalidArgumentException(); |
|
| 698 | + } |
|
| 699 | + $query = $this->db->getQueryBuilder(); |
|
| 700 | + $query->update('calendarobjects') |
|
| 701 | + ->set('classification', $query->createNamedParameter($classification)) |
|
| 702 | + ->where($query->expr()->eq('id', $query->createNamedParameter($calendarObjectId))) |
|
| 703 | + ->execute(); |
|
| 704 | + } |
|
| 705 | + |
|
| 706 | + /** |
|
| 707 | + * Deletes an existing calendar object. |
|
| 708 | + * |
|
| 709 | + * The object uri is only the basename, or filename and not a full path. |
|
| 710 | + * |
|
| 711 | + * @param mixed $calendarId |
|
| 712 | + * @param string $objectUri |
|
| 713 | + * @return void |
|
| 714 | + */ |
|
| 715 | + function deleteCalendarObject($calendarId, $objectUri) { |
|
| 716 | + $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ? AND `uri` = ?'); |
|
| 717 | + $stmt->execute([$calendarId, $objectUri]); |
|
| 718 | + |
|
| 719 | + $this->addChange($calendarId, $objectUri, 3); |
|
| 720 | + } |
|
| 721 | + |
|
| 722 | + /** |
|
| 723 | + * Performs a calendar-query on the contents of this calendar. |
|
| 724 | + * |
|
| 725 | + * The calendar-query is defined in RFC4791 : CalDAV. Using the |
|
| 726 | + * calendar-query it is possible for a client to request a specific set of |
|
| 727 | + * object, based on contents of iCalendar properties, date-ranges and |
|
| 728 | + * iCalendar component types (VTODO, VEVENT). |
|
| 729 | + * |
|
| 730 | + * This method should just return a list of (relative) urls that match this |
|
| 731 | + * query. |
|
| 732 | + * |
|
| 733 | + * The list of filters are specified as an array. The exact array is |
|
| 734 | + * documented by Sabre\CalDAV\CalendarQueryParser. |
|
| 735 | + * |
|
| 736 | + * Note that it is extremely likely that getCalendarObject for every path |
|
| 737 | + * returned from this method will be called almost immediately after. You |
|
| 738 | + * may want to anticipate this to speed up these requests. |
|
| 739 | + * |
|
| 740 | + * This method provides a default implementation, which parses *all* the |
|
| 741 | + * iCalendar objects in the specified calendar. |
|
| 742 | + * |
|
| 743 | + * This default may well be good enough for personal use, and calendars |
|
| 744 | + * that aren't very large. But if you anticipate high usage, big calendars |
|
| 745 | + * or high loads, you are strongly advised to optimize certain paths. |
|
| 746 | + * |
|
| 747 | + * The best way to do so is override this method and to optimize |
|
| 748 | + * specifically for 'common filters'. |
|
| 749 | + * |
|
| 750 | + * Requests that are extremely common are: |
|
| 751 | + * * requests for just VEVENTS |
|
| 752 | + * * requests for just VTODO |
|
| 753 | + * * requests with a time-range-filter on either VEVENT or VTODO. |
|
| 754 | + * |
|
| 755 | + * ..and combinations of these requests. It may not be worth it to try to |
|
| 756 | + * handle every possible situation and just rely on the (relatively |
|
| 757 | + * easy to use) CalendarQueryValidator to handle the rest. |
|
| 758 | + * |
|
| 759 | + * Note that especially time-range-filters may be difficult to parse. A |
|
| 760 | + * time-range filter specified on a VEVENT must for instance also handle |
|
| 761 | + * recurrence rules correctly. |
|
| 762 | + * A good example of how to interprete all these filters can also simply |
|
| 763 | + * be found in Sabre\CalDAV\CalendarQueryFilter. This class is as correct |
|
| 764 | + * as possible, so it gives you a good idea on what type of stuff you need |
|
| 765 | + * to think of. |
|
| 766 | + * |
|
| 767 | + * @param mixed $calendarId |
|
| 768 | + * @param array $filters |
|
| 769 | + * @return array |
|
| 770 | + */ |
|
| 771 | + function calendarQuery($calendarId, array $filters) { |
|
| 772 | + $componentType = null; |
|
| 773 | + $requirePostFilter = true; |
|
| 774 | + $timeRange = null; |
|
| 775 | + |
|
| 776 | + // if no filters were specified, we don't need to filter after a query |
|
| 777 | + if (!$filters['prop-filters'] && !$filters['comp-filters']) { |
|
| 778 | + $requirePostFilter = false; |
|
| 779 | + } |
|
| 780 | + |
|
| 781 | + // Figuring out if there's a component filter |
|
| 782 | + if (count($filters['comp-filters']) > 0 && !$filters['comp-filters'][0]['is-not-defined']) { |
|
| 783 | + $componentType = $filters['comp-filters'][0]['name']; |
|
| 784 | + |
|
| 785 | + // Checking if we need post-filters |
|
| 786 | + if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['time-range'] && !$filters['comp-filters'][0]['prop-filters']) { |
|
| 787 | + $requirePostFilter = false; |
|
| 788 | + } |
|
| 789 | + // There was a time-range filter |
|
| 790 | + if ($componentType == 'VEVENT' && isset($filters['comp-filters'][0]['time-range'])) { |
|
| 791 | + $timeRange = $filters['comp-filters'][0]['time-range']; |
|
| 792 | + |
|
| 793 | + // If start time OR the end time is not specified, we can do a |
|
| 794 | + // 100% accurate mysql query. |
|
| 795 | + if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['prop-filters'] && (!$timeRange['start'] || !$timeRange['end'])) { |
|
| 796 | + $requirePostFilter = false; |
|
| 797 | + } |
|
| 798 | + } |
|
| 799 | + |
|
| 800 | + } |
|
| 801 | + $columns = ['uri']; |
|
| 802 | + if ($requirePostFilter) { |
|
| 803 | + $columns = ['uri', 'calendardata']; |
|
| 804 | + } |
|
| 805 | + $query = $this->db->getQueryBuilder(); |
|
| 806 | + $query->select($columns) |
|
| 807 | + ->from('calendarobjects') |
|
| 808 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))); |
|
| 809 | + |
|
| 810 | + if ($componentType) { |
|
| 811 | + $query->andWhere($query->expr()->eq('componenttype', $query->createNamedParameter($componentType))); |
|
| 812 | + } |
|
| 813 | + |
|
| 814 | + if ($timeRange && $timeRange['start']) { |
|
| 815 | + $query->andWhere($query->expr()->gt('lastoccurence', $query->createNamedParameter($timeRange['start']->getTimeStamp()))); |
|
| 816 | + } |
|
| 817 | + if ($timeRange && $timeRange['end']) { |
|
| 818 | + $query->andWhere($query->expr()->lt('firstoccurence', $query->createNamedParameter($timeRange['end']->getTimeStamp()))); |
|
| 819 | + } |
|
| 820 | + |
|
| 821 | + $stmt = $query->execute(); |
|
| 822 | + |
|
| 823 | + $result = []; |
|
| 824 | + while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 825 | + if ($requirePostFilter) { |
|
| 826 | + if (!$this->validateFilterForObject($row, $filters)) { |
|
| 827 | + continue; |
|
| 828 | + } |
|
| 829 | + } |
|
| 830 | + $result[] = $row['uri']; |
|
| 831 | + } |
|
| 832 | + |
|
| 833 | + return $result; |
|
| 834 | + } |
|
| 835 | + |
|
| 836 | + /** |
|
| 837 | + * Searches through all of a users calendars and calendar objects to find |
|
| 838 | + * an object with a specific UID. |
|
| 839 | + * |
|
| 840 | + * This method should return the path to this object, relative to the |
|
| 841 | + * calendar home, so this path usually only contains two parts: |
|
| 842 | + * |
|
| 843 | + * calendarpath/objectpath.ics |
|
| 844 | + * |
|
| 845 | + * If the uid is not found, return null. |
|
| 846 | + * |
|
| 847 | + * This method should only consider * objects that the principal owns, so |
|
| 848 | + * any calendars owned by other principals that also appear in this |
|
| 849 | + * collection should be ignored. |
|
| 850 | + * |
|
| 851 | + * @param string $principalUri |
|
| 852 | + * @param string $uid |
|
| 853 | + * @return string|null |
|
| 854 | + */ |
|
| 855 | + function getCalendarObjectByUID($principalUri, $uid) { |
|
| 856 | + |
|
| 857 | + $query = $this->db->getQueryBuilder(); |
|
| 858 | + $query->selectAlias('c.uri', 'calendaruri')->selectAlias('co.uri', 'objecturi') |
|
| 859 | + ->from('calendarobjects', 'co') |
|
| 860 | + ->leftJoin('co', 'calendars', 'c', $query->expr()->eq('co.calendarid', 'c.id')) |
|
| 861 | + ->where($query->expr()->eq('c.principaluri', $query->createNamedParameter($principalUri))) |
|
| 862 | + ->andWhere($query->expr()->eq('co.uid', $query->createNamedParameter($uid))); |
|
| 863 | + |
|
| 864 | + $stmt = $query->execute(); |
|
| 865 | + |
|
| 866 | + if ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 867 | + return $row['calendaruri'] . '/' . $row['objecturi']; |
|
| 868 | + } |
|
| 869 | + |
|
| 870 | + return null; |
|
| 871 | + } |
|
| 872 | + |
|
| 873 | + /** |
|
| 874 | + * The getChanges method returns all the changes that have happened, since |
|
| 875 | + * the specified syncToken in the specified calendar. |
|
| 876 | + * |
|
| 877 | + * This function should return an array, such as the following: |
|
| 878 | + * |
|
| 879 | + * [ |
|
| 880 | + * 'syncToken' => 'The current synctoken', |
|
| 881 | + * 'added' => [ |
|
| 882 | + * 'new.txt', |
|
| 883 | + * ], |
|
| 884 | + * 'modified' => [ |
|
| 885 | + * 'modified.txt', |
|
| 886 | + * ], |
|
| 887 | + * 'deleted' => [ |
|
| 888 | + * 'foo.php.bak', |
|
| 889 | + * 'old.txt' |
|
| 890 | + * ] |
|
| 891 | + * ); |
|
| 892 | + * |
|
| 893 | + * The returned syncToken property should reflect the *current* syncToken |
|
| 894 | + * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
|
| 895 | + * property This is * needed here too, to ensure the operation is atomic. |
|
| 896 | + * |
|
| 897 | + * If the $syncToken argument is specified as null, this is an initial |
|
| 898 | + * sync, and all members should be reported. |
|
| 899 | + * |
|
| 900 | + * The modified property is an array of nodenames that have changed since |
|
| 901 | + * the last token. |
|
| 902 | + * |
|
| 903 | + * The deleted property is an array with nodenames, that have been deleted |
|
| 904 | + * from collection. |
|
| 905 | + * |
|
| 906 | + * The $syncLevel argument is basically the 'depth' of the report. If it's |
|
| 907 | + * 1, you only have to report changes that happened only directly in |
|
| 908 | + * immediate descendants. If it's 2, it should also include changes from |
|
| 909 | + * the nodes below the child collections. (grandchildren) |
|
| 910 | + * |
|
| 911 | + * The $limit argument allows a client to specify how many results should |
|
| 912 | + * be returned at most. If the limit is not specified, it should be treated |
|
| 913 | + * as infinite. |
|
| 914 | + * |
|
| 915 | + * If the limit (infinite or not) is higher than you're willing to return, |
|
| 916 | + * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
|
| 917 | + * |
|
| 918 | + * If the syncToken is expired (due to data cleanup) or unknown, you must |
|
| 919 | + * return null. |
|
| 920 | + * |
|
| 921 | + * The limit is 'suggestive'. You are free to ignore it. |
|
| 922 | + * |
|
| 923 | + * @param string $calendarId |
|
| 924 | + * @param string $syncToken |
|
| 925 | + * @param int $syncLevel |
|
| 926 | + * @param int $limit |
|
| 927 | + * @return array |
|
| 928 | + */ |
|
| 929 | + function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null) { |
|
| 930 | + // Current synctoken |
|
| 931 | + $stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*calendars` WHERE `id` = ?'); |
|
| 932 | + $stmt->execute([ $calendarId ]); |
|
| 933 | + $currentToken = $stmt->fetchColumn(0); |
|
| 934 | + |
|
| 935 | + if (is_null($currentToken)) { |
|
| 936 | + return null; |
|
| 937 | + } |
|
| 938 | + |
|
| 939 | + $result = [ |
|
| 940 | + 'syncToken' => $currentToken, |
|
| 941 | + 'added' => [], |
|
| 942 | + 'modified' => [], |
|
| 943 | + 'deleted' => [], |
|
| 944 | + ]; |
|
| 945 | + |
|
| 946 | + if ($syncToken) { |
|
| 947 | + |
|
| 948 | + $query = "SELECT `uri`, `operation` FROM `*PREFIX*calendarchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `calendarid` = ? ORDER BY `synctoken`"; |
|
| 949 | + if ($limit>0) { |
|
| 950 | + $query.= " `LIMIT` " . (int)$limit; |
|
| 951 | + } |
|
| 952 | + |
|
| 953 | + // Fetching all changes |
|
| 954 | + $stmt = $this->db->prepare($query); |
|
| 955 | + $stmt->execute([$syncToken, $currentToken, $calendarId]); |
|
| 956 | + |
|
| 957 | + $changes = []; |
|
| 958 | + |
|
| 959 | + // This loop ensures that any duplicates are overwritten, only the |
|
| 960 | + // last change on a node is relevant. |
|
| 961 | + while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 962 | + |
|
| 963 | + $changes[$row['uri']] = $row['operation']; |
|
| 964 | + |
|
| 965 | + } |
|
| 966 | + |
|
| 967 | + foreach($changes as $uri => $operation) { |
|
| 968 | + |
|
| 969 | + switch($operation) { |
|
| 970 | + case 1 : |
|
| 971 | + $result['added'][] = $uri; |
|
| 972 | + break; |
|
| 973 | + case 2 : |
|
| 974 | + $result['modified'][] = $uri; |
|
| 975 | + break; |
|
| 976 | + case 3 : |
|
| 977 | + $result['deleted'][] = $uri; |
|
| 978 | + break; |
|
| 979 | + } |
|
| 980 | + |
|
| 981 | + } |
|
| 982 | + } else { |
|
| 983 | + // No synctoken supplied, this is the initial sync. |
|
| 984 | + $query = "SELECT `uri` FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ?"; |
|
| 985 | + $stmt = $this->db->prepare($query); |
|
| 986 | + $stmt->execute([$calendarId]); |
|
| 987 | + |
|
| 988 | + $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
| 989 | + } |
|
| 990 | + return $result; |
|
| 991 | + |
|
| 992 | + } |
|
| 993 | + |
|
| 994 | + /** |
|
| 995 | + * Returns a list of subscriptions for a principal. |
|
| 996 | + * |
|
| 997 | + * Every subscription is an array with the following keys: |
|
| 998 | + * * id, a unique id that will be used by other functions to modify the |
|
| 999 | + * subscription. This can be the same as the uri or a database key. |
|
| 1000 | + * * uri. This is just the 'base uri' or 'filename' of the subscription. |
|
| 1001 | + * * principaluri. The owner of the subscription. Almost always the same as |
|
| 1002 | + * principalUri passed to this method. |
|
| 1003 | + * |
|
| 1004 | + * Furthermore, all the subscription info must be returned too: |
|
| 1005 | + * |
|
| 1006 | + * 1. {DAV:}displayname |
|
| 1007 | + * 2. {http://apple.com/ns/ical/}refreshrate |
|
| 1008 | + * 3. {http://calendarserver.org/ns/}subscribed-strip-todos (omit if todos |
|
| 1009 | + * should not be stripped). |
|
| 1010 | + * 4. {http://calendarserver.org/ns/}subscribed-strip-alarms (omit if alarms |
|
| 1011 | + * should not be stripped). |
|
| 1012 | + * 5. {http://calendarserver.org/ns/}subscribed-strip-attachments (omit if |
|
| 1013 | + * attachments should not be stripped). |
|
| 1014 | + * 6. {http://calendarserver.org/ns/}source (Must be a |
|
| 1015 | + * Sabre\DAV\Property\Href). |
|
| 1016 | + * 7. {http://apple.com/ns/ical/}calendar-color |
|
| 1017 | + * 8. {http://apple.com/ns/ical/}calendar-order |
|
| 1018 | + * 9. {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
|
| 1019 | + * (should just be an instance of |
|
| 1020 | + * Sabre\CalDAV\Property\SupportedCalendarComponentSet, with a bunch of |
|
| 1021 | + * default components). |
|
| 1022 | + * |
|
| 1023 | + * @param string $principalUri |
|
| 1024 | + * @return array |
|
| 1025 | + */ |
|
| 1026 | + function getSubscriptionsForUser($principalUri) { |
|
| 1027 | + $fields = array_values($this->subscriptionPropertyMap); |
|
| 1028 | + $fields[] = 'id'; |
|
| 1029 | + $fields[] = 'uri'; |
|
| 1030 | + $fields[] = 'source'; |
|
| 1031 | + $fields[] = 'principaluri'; |
|
| 1032 | + $fields[] = 'lastmodified'; |
|
| 1033 | + |
|
| 1034 | + $query = $this->db->getQueryBuilder(); |
|
| 1035 | + $query->select($fields) |
|
| 1036 | + ->from('calendarsubscriptions') |
|
| 1037 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 1038 | + ->orderBy('calendarorder', 'asc'); |
|
| 1039 | + $stmt =$query->execute(); |
|
| 1040 | + |
|
| 1041 | + $subscriptions = []; |
|
| 1042 | + while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 1043 | + |
|
| 1044 | + $subscription = [ |
|
| 1045 | + 'id' => $row['id'], |
|
| 1046 | + 'uri' => $row['uri'], |
|
| 1047 | + 'principaluri' => $row['principaluri'], |
|
| 1048 | + 'source' => $row['source'], |
|
| 1049 | + 'lastmodified' => $row['lastmodified'], |
|
| 1050 | + |
|
| 1051 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), |
|
| 1052 | + ]; |
|
| 1053 | + |
|
| 1054 | + foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) { |
|
| 1055 | + if (!is_null($row[$dbName])) { |
|
| 1056 | + $subscription[$xmlName] = $row[$dbName]; |
|
| 1057 | + } |
|
| 1058 | + } |
|
| 1059 | + |
|
| 1060 | + $subscriptions[] = $subscription; |
|
| 1061 | + |
|
| 1062 | + } |
|
| 1063 | + |
|
| 1064 | + return $subscriptions; |
|
| 1065 | + } |
|
| 1066 | + |
|
| 1067 | + /** |
|
| 1068 | + * Creates a new subscription for a principal. |
|
| 1069 | + * |
|
| 1070 | + * If the creation was a success, an id must be returned that can be used to reference |
|
| 1071 | + * this subscription in other methods, such as updateSubscription. |
|
| 1072 | + * |
|
| 1073 | + * @param string $principalUri |
|
| 1074 | + * @param string $uri |
|
| 1075 | + * @param array $properties |
|
| 1076 | + * @return mixed |
|
| 1077 | + */ |
|
| 1078 | + function createSubscription($principalUri, $uri, array $properties) { |
|
| 1079 | + |
|
| 1080 | + if (!isset($properties['{http://calendarserver.org/ns/}source'])) { |
|
| 1081 | + throw new Forbidden('The {http://calendarserver.org/ns/}source property is required when creating subscriptions'); |
|
| 1082 | + } |
|
| 1083 | + |
|
| 1084 | + $values = [ |
|
| 1085 | + 'principaluri' => $principalUri, |
|
| 1086 | + 'uri' => $uri, |
|
| 1087 | + 'source' => $properties['{http://calendarserver.org/ns/}source']->getHref(), |
|
| 1088 | + 'lastmodified' => time(), |
|
| 1089 | + ]; |
|
| 1090 | + |
|
| 1091 | + $propertiesBoolean = ['striptodos', 'stripalarms', 'stripattachments']; |
|
| 1092 | + |
|
| 1093 | + foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) { |
|
| 1094 | + if (array_key_exists($xmlName, $properties)) { |
|
| 1095 | + $values[$dbName] = $properties[$xmlName]; |
|
| 1096 | + if (in_array($dbName, $propertiesBoolean)) { |
|
| 1097 | + $values[$dbName] = true; |
|
| 1098 | + } |
|
| 1099 | + } |
|
| 1100 | + } |
|
| 1101 | + |
|
| 1102 | + $valuesToInsert = array(); |
|
| 1103 | + |
|
| 1104 | + $query = $this->db->getQueryBuilder(); |
|
| 1105 | + |
|
| 1106 | + foreach (array_keys($values) as $name) { |
|
| 1107 | + $valuesToInsert[$name] = $query->createNamedParameter($values[$name]); |
|
| 1108 | + } |
|
| 1109 | + |
|
| 1110 | + $query->insert('calendarsubscriptions') |
|
| 1111 | + ->values($valuesToInsert) |
|
| 1112 | + ->execute(); |
|
| 1113 | + |
|
| 1114 | + return $this->db->lastInsertId('*PREFIX*calendarsubscriptions'); |
|
| 1115 | + } |
|
| 1116 | + |
|
| 1117 | + /** |
|
| 1118 | + * Updates a subscription |
|
| 1119 | + * |
|
| 1120 | + * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
| 1121 | + * To do the actual updates, you must tell this object which properties |
|
| 1122 | + * you're going to process with the handle() method. |
|
| 1123 | + * |
|
| 1124 | + * Calling the handle method is like telling the PropPatch object "I |
|
| 1125 | + * promise I can handle updating this property". |
|
| 1126 | + * |
|
| 1127 | + * Read the PropPatch documentation for more info and examples. |
|
| 1128 | + * |
|
| 1129 | + * @param mixed $subscriptionId |
|
| 1130 | + * @param PropPatch $propPatch |
|
| 1131 | + * @return void |
|
| 1132 | + */ |
|
| 1133 | + function updateSubscription($subscriptionId, PropPatch $propPatch) { |
|
| 1134 | + $supportedProperties = array_keys($this->subscriptionPropertyMap); |
|
| 1135 | + $supportedProperties[] = '{http://calendarserver.org/ns/}source'; |
|
| 1136 | + |
|
| 1137 | + $propPatch->handle($supportedProperties, function($mutations) use ($subscriptionId) { |
|
| 1138 | + |
|
| 1139 | + $newValues = []; |
|
| 1140 | + |
|
| 1141 | + foreach($mutations as $propertyName=>$propertyValue) { |
|
| 1142 | + if ($propertyName === '{http://calendarserver.org/ns/}source') { |
|
| 1143 | + $newValues['source'] = $propertyValue->getHref(); |
|
| 1144 | + } else { |
|
| 1145 | + $fieldName = $this->subscriptionPropertyMap[$propertyName]; |
|
| 1146 | + $newValues[$fieldName] = $propertyValue; |
|
| 1147 | + } |
|
| 1148 | + } |
|
| 1149 | + |
|
| 1150 | + $query = $this->db->getQueryBuilder(); |
|
| 1151 | + $query->update('calendarsubscriptions') |
|
| 1152 | + ->set('lastmodified', $query->createNamedParameter(time())); |
|
| 1153 | + foreach($newValues as $fieldName=>$value) { |
|
| 1154 | + $query->set($fieldName, $query->createNamedParameter($value)); |
|
| 1155 | + } |
|
| 1156 | + $query->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
| 1157 | + ->execute(); |
|
| 1158 | + |
|
| 1159 | + return true; |
|
| 1160 | + |
|
| 1161 | + }); |
|
| 1162 | + } |
|
| 1163 | + |
|
| 1164 | + /** |
|
| 1165 | + * Deletes a subscription. |
|
| 1166 | + * |
|
| 1167 | + * @param mixed $subscriptionId |
|
| 1168 | + * @return void |
|
| 1169 | + */ |
|
| 1170 | + function deleteSubscription($subscriptionId) { |
|
| 1171 | + $query = $this->db->getQueryBuilder(); |
|
| 1172 | + $query->delete('calendarsubscriptions') |
|
| 1173 | + ->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
| 1174 | + ->execute(); |
|
| 1175 | + } |
|
| 1176 | + |
|
| 1177 | + /** |
|
| 1178 | + * Returns a single scheduling object for the inbox collection. |
|
| 1179 | + * |
|
| 1180 | + * The returned array should contain the following elements: |
|
| 1181 | + * * uri - A unique basename for the object. This will be used to |
|
| 1182 | + * construct a full uri. |
|
| 1183 | + * * calendardata - The iCalendar object |
|
| 1184 | + * * lastmodified - The last modification date. Can be an int for a unix |
|
| 1185 | + * timestamp, or a PHP DateTime object. |
|
| 1186 | + * * etag - A unique token that must change if the object changed. |
|
| 1187 | + * * size - The size of the object, in bytes. |
|
| 1188 | + * |
|
| 1189 | + * @param string $principalUri |
|
| 1190 | + * @param string $objectUri |
|
| 1191 | + * @return array |
|
| 1192 | + */ |
|
| 1193 | + function getSchedulingObject($principalUri, $objectUri) { |
|
| 1194 | + $query = $this->db->getQueryBuilder(); |
|
| 1195 | + $stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size']) |
|
| 1196 | + ->from('schedulingobjects') |
|
| 1197 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 1198 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
| 1199 | + ->execute(); |
|
| 1200 | + |
|
| 1201 | + $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
| 1202 | + |
|
| 1203 | + if(!$row) { |
|
| 1204 | + return null; |
|
| 1205 | + } |
|
| 1206 | + |
|
| 1207 | + return [ |
|
| 1208 | + 'uri' => $row['uri'], |
|
| 1209 | + 'calendardata' => $row['calendardata'], |
|
| 1210 | + 'lastmodified' => $row['lastmodified'], |
|
| 1211 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 1212 | + 'size' => (int)$row['size'], |
|
| 1213 | + ]; |
|
| 1214 | + } |
|
| 1215 | + |
|
| 1216 | + /** |
|
| 1217 | + * Returns all scheduling objects for the inbox collection. |
|
| 1218 | + * |
|
| 1219 | + * These objects should be returned as an array. Every item in the array |
|
| 1220 | + * should follow the same structure as returned from getSchedulingObject. |
|
| 1221 | + * |
|
| 1222 | + * The main difference is that 'calendardata' is optional. |
|
| 1223 | + * |
|
| 1224 | + * @param string $principalUri |
|
| 1225 | + * @return array |
|
| 1226 | + */ |
|
| 1227 | + function getSchedulingObjects($principalUri) { |
|
| 1228 | + $query = $this->db->getQueryBuilder(); |
|
| 1229 | + $stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size']) |
|
| 1230 | + ->from('schedulingobjects') |
|
| 1231 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 1232 | + ->execute(); |
|
| 1233 | + |
|
| 1234 | + $result = []; |
|
| 1235 | + foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) { |
|
| 1236 | + $result[] = [ |
|
| 1237 | + 'calendardata' => $row['calendardata'], |
|
| 1238 | + 'uri' => $row['uri'], |
|
| 1239 | + 'lastmodified' => $row['lastmodified'], |
|
| 1240 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 1241 | + 'size' => (int)$row['size'], |
|
| 1242 | + ]; |
|
| 1243 | + } |
|
| 1244 | + |
|
| 1245 | + return $result; |
|
| 1246 | + } |
|
| 1247 | + |
|
| 1248 | + /** |
|
| 1249 | + * Deletes a scheduling object from the inbox collection. |
|
| 1250 | + * |
|
| 1251 | + * @param string $principalUri |
|
| 1252 | + * @param string $objectUri |
|
| 1253 | + * @return void |
|
| 1254 | + */ |
|
| 1255 | + function deleteSchedulingObject($principalUri, $objectUri) { |
|
| 1256 | + $query = $this->db->getQueryBuilder(); |
|
| 1257 | + $query->delete('schedulingobjects') |
|
| 1258 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 1259 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
| 1260 | + ->execute(); |
|
| 1261 | + } |
|
| 1262 | + |
|
| 1263 | + /** |
|
| 1264 | + * Creates a new scheduling object. This should land in a users' inbox. |
|
| 1265 | + * |
|
| 1266 | + * @param string $principalUri |
|
| 1267 | + * @param string $objectUri |
|
| 1268 | + * @param string $objectData |
|
| 1269 | + * @return void |
|
| 1270 | + */ |
|
| 1271 | + function createSchedulingObject($principalUri, $objectUri, $objectData) { |
|
| 1272 | + $query = $this->db->getQueryBuilder(); |
|
| 1273 | + $query->insert('schedulingobjects') |
|
| 1274 | + ->values([ |
|
| 1275 | + 'principaluri' => $query->createNamedParameter($principalUri), |
|
| 1276 | + 'calendardata' => $query->createNamedParameter($objectData), |
|
| 1277 | + 'uri' => $query->createNamedParameter($objectUri), |
|
| 1278 | + 'lastmodified' => $query->createNamedParameter(time()), |
|
| 1279 | + 'etag' => $query->createNamedParameter(md5($objectData)), |
|
| 1280 | + 'size' => $query->createNamedParameter(strlen($objectData)) |
|
| 1281 | + ]) |
|
| 1282 | + ->execute(); |
|
| 1283 | + } |
|
| 1284 | + |
|
| 1285 | + /** |
|
| 1286 | + * Adds a change record to the calendarchanges table. |
|
| 1287 | + * |
|
| 1288 | + * @param mixed $calendarId |
|
| 1289 | + * @param string $objectUri |
|
| 1290 | + * @param int $operation 1 = add, 2 = modify, 3 = delete. |
|
| 1291 | + * @return void |
|
| 1292 | + */ |
|
| 1293 | + protected function addChange($calendarId, $objectUri, $operation) { |
|
| 1294 | + |
|
| 1295 | + $stmt = $this->db->prepare('INSERT INTO `*PREFIX*calendarchanges` (`uri`, `synctoken`, `calendarid`, `operation`) SELECT ?, `synctoken`, ?, ? FROM `*PREFIX*calendars` WHERE `id` = ?'); |
|
| 1296 | + $stmt->execute([ |
|
| 1297 | + $objectUri, |
|
| 1298 | + $calendarId, |
|
| 1299 | + $operation, |
|
| 1300 | + $calendarId |
|
| 1301 | + ]); |
|
| 1302 | + $stmt = $this->db->prepare('UPDATE `*PREFIX*calendars` SET `synctoken` = `synctoken` + 1 WHERE `id` = ?'); |
|
| 1303 | + $stmt->execute([ |
|
| 1304 | + $calendarId |
|
| 1305 | + ]); |
|
| 1306 | + |
|
| 1307 | + } |
|
| 1308 | + |
|
| 1309 | + /** |
|
| 1310 | + * Parses some information from calendar objects, used for optimized |
|
| 1311 | + * calendar-queries. |
|
| 1312 | + * |
|
| 1313 | + * Returns an array with the following keys: |
|
| 1314 | + * * etag - An md5 checksum of the object without the quotes. |
|
| 1315 | + * * size - Size of the object in bytes |
|
| 1316 | + * * componentType - VEVENT, VTODO or VJOURNAL |
|
| 1317 | + * * firstOccurence |
|
| 1318 | + * * lastOccurence |
|
| 1319 | + * * uid - value of the UID property |
|
| 1320 | + * |
|
| 1321 | + * @param string $calendarData |
|
| 1322 | + * @return array |
|
| 1323 | + */ |
|
| 1324 | + public function getDenormalizedData($calendarData) { |
|
| 1325 | + |
|
| 1326 | + $vObject = Reader::read($calendarData); |
|
| 1327 | + $componentType = null; |
|
| 1328 | + $component = null; |
|
| 1329 | + $firstOccurrence = null; |
|
| 1330 | + $lastOccurrence = null; |
|
| 1331 | + $uid = null; |
|
| 1332 | + $classification = self::CLASSIFICATION_PUBLIC; |
|
| 1333 | + foreach($vObject->getComponents() as $component) { |
|
| 1334 | + if ($component->name!=='VTIMEZONE') { |
|
| 1335 | + $componentType = $component->name; |
|
| 1336 | + $uid = (string)$component->UID; |
|
| 1337 | + break; |
|
| 1338 | + } |
|
| 1339 | + } |
|
| 1340 | + if (!$componentType) { |
|
| 1341 | + throw new \Sabre\DAV\Exception\BadRequest('Calendar objects must have a VJOURNAL, VEVENT or VTODO component'); |
|
| 1342 | + } |
|
| 1343 | + if ($componentType === 'VEVENT' && $component->DTSTART) { |
|
| 1344 | + $firstOccurrence = $component->DTSTART->getDateTime()->getTimeStamp(); |
|
| 1345 | + // Finding the last occurrence is a bit harder |
|
| 1346 | + if (!isset($component->RRULE)) { |
|
| 1347 | + if (isset($component->DTEND)) { |
|
| 1348 | + $lastOccurrence = $component->DTEND->getDateTime()->getTimeStamp(); |
|
| 1349 | + } elseif (isset($component->DURATION)) { |
|
| 1350 | + $endDate = clone $component->DTSTART->getDateTime(); |
|
| 1351 | + $endDate->add(DateTimeParser::parse($component->DURATION->getValue())); |
|
| 1352 | + $lastOccurrence = $endDate->getTimeStamp(); |
|
| 1353 | + } elseif (!$component->DTSTART->hasTime()) { |
|
| 1354 | + $endDate = clone $component->DTSTART->getDateTime(); |
|
| 1355 | + $endDate->modify('+1 day'); |
|
| 1356 | + $lastOccurrence = $endDate->getTimeStamp(); |
|
| 1357 | + } else { |
|
| 1358 | + $lastOccurrence = $firstOccurrence; |
|
| 1359 | + } |
|
| 1360 | + } else { |
|
| 1361 | + $it = new EventIterator($vObject, (string)$component->UID); |
|
| 1362 | + $maxDate = new \DateTime(self::MAX_DATE); |
|
| 1363 | + if ($it->isInfinite()) { |
|
| 1364 | + $lastOccurrence = $maxDate->getTimeStamp(); |
|
| 1365 | + } else { |
|
| 1366 | + $end = $it->getDtEnd(); |
|
| 1367 | + while($it->valid() && $end < $maxDate) { |
|
| 1368 | + $end = $it->getDtEnd(); |
|
| 1369 | + $it->next(); |
|
| 1370 | + |
|
| 1371 | + } |
|
| 1372 | + $lastOccurrence = $end->getTimeStamp(); |
|
| 1373 | + } |
|
| 1374 | + |
|
| 1375 | + } |
|
| 1376 | + } |
|
| 1377 | + |
|
| 1378 | + if ($component->CLASS) { |
|
| 1379 | + $classification = CalDavBackend::CLASSIFICATION_PRIVATE; |
|
| 1380 | + switch ($component->CLASS->getValue()) { |
|
| 1381 | + case 'PUBLIC': |
|
| 1382 | + $classification = CalDavBackend::CLASSIFICATION_PUBLIC; |
|
| 1383 | + break; |
|
| 1384 | + case 'CONFIDENTIAL': |
|
| 1385 | + $classification = CalDavBackend::CLASSIFICATION_CONFIDENTIAL; |
|
| 1386 | + break; |
|
| 1387 | + } |
|
| 1388 | + } |
|
| 1389 | + return [ |
|
| 1390 | + 'etag' => md5($calendarData), |
|
| 1391 | + 'size' => strlen($calendarData), |
|
| 1392 | + 'componentType' => $componentType, |
|
| 1393 | + 'firstOccurence' => is_null($firstOccurrence) ? null : max(0, $firstOccurrence), |
|
| 1394 | + 'lastOccurence' => $lastOccurrence, |
|
| 1395 | + 'uid' => $uid, |
|
| 1396 | + 'classification' => $classification |
|
| 1397 | + ]; |
|
| 1398 | + |
|
| 1399 | + } |
|
| 1400 | + |
|
| 1401 | + private function readBlob($cardData) { |
|
| 1402 | + if (is_resource($cardData)) { |
|
| 1403 | + return stream_get_contents($cardData); |
|
| 1404 | + } |
|
| 1405 | + |
|
| 1406 | + return $cardData; |
|
| 1407 | + } |
|
| 1408 | + |
|
| 1409 | + /** |
|
| 1410 | + * @param IShareable $shareable |
|
| 1411 | + * @param array $add |
|
| 1412 | + * @param array $remove |
|
| 1413 | + */ |
|
| 1414 | + public function updateShares($shareable, $add, $remove) { |
|
| 1415 | + $this->sharingBackend->updateShares($shareable, $add, $remove); |
|
| 1416 | + } |
|
| 1417 | + |
|
| 1418 | + /** |
|
| 1419 | + * @param int $resourceId |
|
| 1420 | + * @return array |
|
| 1421 | + */ |
|
| 1422 | + public function getShares($resourceId) { |
|
| 1423 | + return $this->sharingBackend->getShares($resourceId); |
|
| 1424 | + } |
|
| 1425 | + |
|
| 1426 | + /** |
|
| 1427 | + * @param int $resourceId |
|
| 1428 | + * @param array $acl |
|
| 1429 | + * @return array |
|
| 1430 | + */ |
|
| 1431 | + public function applyShareAcl($resourceId, $acl) { |
|
| 1432 | + return $this->sharingBackend->applyShareAcl($resourceId, $acl); |
|
| 1433 | + } |
|
| 1434 | + |
|
| 1435 | + private function convertPrincipal($principalUri, $toV2) { |
|
| 1436 | + if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
|
| 1437 | + list(, $name) = URLUtil::splitPath($principalUri); |
|
| 1438 | + if ($toV2 === true) { |
|
| 1439 | + return "principals/users/$name"; |
|
| 1440 | + } |
|
| 1441 | + return "principals/$name"; |
|
| 1442 | + } |
|
| 1443 | + return $principalUri; |
|
| 1444 | + } |
|
| 1445 | 1445 | } |
@@ -188,7 +188,8 @@ discard block |
||
| 188 | 188 | } |
| 189 | 189 | |
| 190 | 190 | /** |
| 191 | - * @param $addressBookUri |
|
| 191 | + * @param string $addressBookUri |
|
| 192 | + * @param string $principal |
|
| 192 | 193 | * @return array|null |
| 193 | 194 | */ |
| 194 | 195 | public function getAddressBooksByUri($principal, $addressBookUri) { |
@@ -868,6 +869,7 @@ discard block |
||
| 868 | 869 | * * readOnly - boolean |
| 869 | 870 | * * summary - Optional, a description for the share |
| 870 | 871 | * |
| 872 | + * @param integer $addressBookId |
|
| 871 | 873 | * @return array |
| 872 | 874 | */ |
| 873 | 875 | public function getShares($addressBookId) { |
@@ -967,7 +969,7 @@ discard block |
||
| 967 | 969 | |
| 968 | 970 | /** |
| 969 | 971 | * For shared address books the sharee is set in the ACL of the address book |
| 970 | - * @param $addressBookId |
|
| 972 | + * @param integer $addressBookId |
|
| 971 | 973 | * @param $acl |
| 972 | 974 | * @return array |
| 973 | 975 | */ |
@@ -975,6 +977,9 @@ discard block |
||
| 975 | 977 | return $this->sharingBackend->applyShareAcl($addressBookId, $acl); |
| 976 | 978 | } |
| 977 | 979 | |
| 980 | + /** |
|
| 981 | + * @param boolean $toV2 |
|
| 982 | + */ |
|
| 978 | 983 | private function convertPrincipal($principalUri, $toV2) { |
| 979 | 984 | if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
| 980 | 985 | list(, $name) = URLUtil::splitPath($principalUri); |
@@ -46,955 +46,955 @@ |
||
| 46 | 46 | |
| 47 | 47 | class CardDavBackend implements BackendInterface, SyncSupport { |
| 48 | 48 | |
| 49 | - /** @var Principal */ |
|
| 50 | - private $principalBackend; |
|
| 51 | - |
|
| 52 | - /** @var string */ |
|
| 53 | - private $dbCardsTable = 'cards'; |
|
| 54 | - |
|
| 55 | - /** @var string */ |
|
| 56 | - private $dbCardsPropertiesTable = 'cards_properties'; |
|
| 57 | - |
|
| 58 | - /** @var IDBConnection */ |
|
| 59 | - private $db; |
|
| 60 | - |
|
| 61 | - /** @var Backend */ |
|
| 62 | - private $sharingBackend; |
|
| 63 | - |
|
| 64 | - /** @var array properties to index */ |
|
| 65 | - public static $indexProperties = array( |
|
| 66 | - 'BDAY', 'UID', 'N', 'FN', 'TITLE', 'ROLE', 'NOTE', 'NICKNAME', |
|
| 67 | - 'ORG', 'CATEGORIES', 'EMAIL', 'TEL', 'IMPP', 'ADR', 'URL', 'GEO', 'CLOUD'); |
|
| 68 | - |
|
| 69 | - /** @var EventDispatcherInterface */ |
|
| 70 | - private $dispatcher; |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * CardDavBackend constructor. |
|
| 74 | - * |
|
| 75 | - * @param IDBConnection $db |
|
| 76 | - * @param Principal $principalBackend |
|
| 77 | - * @param EventDispatcherInterface $dispatcher |
|
| 78 | - */ |
|
| 79 | - public function __construct(IDBConnection $db, |
|
| 80 | - Principal $principalBackend, |
|
| 81 | - EventDispatcherInterface $dispatcher = null) { |
|
| 82 | - $this->db = $db; |
|
| 83 | - $this->principalBackend = $principalBackend; |
|
| 84 | - $this->dispatcher = $dispatcher; |
|
| 85 | - $this->sharingBackend = new Backend($this->db, $principalBackend, 'addressbook'); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Returns the list of address books for a specific user. |
|
| 90 | - * |
|
| 91 | - * Every addressbook should have the following properties: |
|
| 92 | - * id - an arbitrary unique id |
|
| 93 | - * uri - the 'basename' part of the url |
|
| 94 | - * principaluri - Same as the passed parameter |
|
| 95 | - * |
|
| 96 | - * Any additional clark-notation property may be passed besides this. Some |
|
| 97 | - * common ones are : |
|
| 98 | - * {DAV:}displayname |
|
| 99 | - * {urn:ietf:params:xml:ns:carddav}addressbook-description |
|
| 100 | - * {http://calendarserver.org/ns/}getctag |
|
| 101 | - * |
|
| 102 | - * @param string $principalUri |
|
| 103 | - * @return array |
|
| 104 | - */ |
|
| 105 | - function getAddressBooksForUser($principalUri) { |
|
| 106 | - $principalUriOriginal = $principalUri; |
|
| 107 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 108 | - $query = $this->db->getQueryBuilder(); |
|
| 109 | - $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 110 | - ->from('addressbooks') |
|
| 111 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 112 | - |
|
| 113 | - $addressBooks = []; |
|
| 114 | - |
|
| 115 | - $result = $query->execute(); |
|
| 116 | - while($row = $result->fetch()) { |
|
| 117 | - $addressBooks[$row['id']] = [ |
|
| 118 | - 'id' => $row['id'], |
|
| 119 | - 'uri' => $row['uri'], |
|
| 120 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
|
| 121 | - '{DAV:}displayname' => $row['displayname'], |
|
| 122 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 123 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 124 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 125 | - ]; |
|
| 126 | - } |
|
| 127 | - $result->closeCursor(); |
|
| 128 | - |
|
| 129 | - // query for shared calendars |
|
| 130 | - $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); |
|
| 131 | - $principals[]= $principalUri; |
|
| 132 | - |
|
| 133 | - $query = $this->db->getQueryBuilder(); |
|
| 134 | - $result = $query->select(['a.id', 'a.uri', 'a.displayname', 'a.principaluri', 'a.description', 'a.synctoken', 's.access']) |
|
| 135 | - ->from('dav_shares', 's') |
|
| 136 | - ->join('s', 'addressbooks', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
| 137 | - ->where($query->expr()->in('s.principaluri', $query->createParameter('principaluri'))) |
|
| 138 | - ->andWhere($query->expr()->eq('s.type', $query->createParameter('type'))) |
|
| 139 | - ->setParameter('type', 'addressbook') |
|
| 140 | - ->setParameter('principaluri', $principals, IQueryBuilder::PARAM_STR_ARRAY) |
|
| 141 | - ->execute(); |
|
| 142 | - |
|
| 143 | - while($row = $result->fetch()) { |
|
| 144 | - list(, $name) = URLUtil::splitPath($row['principaluri']); |
|
| 145 | - $uri = $row['uri'] . '_shared_by_' . $name; |
|
| 146 | - $displayName = $row['displayname'] . "($name)"; |
|
| 147 | - if (!isset($addressBooks[$row['id']])) { |
|
| 148 | - $addressBooks[$row['id']] = [ |
|
| 149 | - 'id' => $row['id'], |
|
| 150 | - 'uri' => $uri, |
|
| 151 | - 'principaluri' => $principalUri, |
|
| 152 | - '{DAV:}displayname' => $displayName, |
|
| 153 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 154 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 155 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 156 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $row['principaluri'], |
|
| 157 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ, |
|
| 158 | - ]; |
|
| 159 | - } |
|
| 160 | - } |
|
| 161 | - $result->closeCursor(); |
|
| 162 | - |
|
| 163 | - return array_values($addressBooks); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * @param int $addressBookId |
|
| 168 | - */ |
|
| 169 | - public function getAddressBookById($addressBookId) { |
|
| 170 | - $query = $this->db->getQueryBuilder(); |
|
| 171 | - $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 172 | - ->from('addressbooks') |
|
| 173 | - ->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) |
|
| 174 | - ->execute(); |
|
| 175 | - |
|
| 176 | - $row = $result->fetch(); |
|
| 177 | - $result->closeCursor(); |
|
| 178 | - if ($row === false) { |
|
| 179 | - return null; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - return [ |
|
| 183 | - 'id' => $row['id'], |
|
| 184 | - 'uri' => $row['uri'], |
|
| 185 | - 'principaluri' => $row['principaluri'], |
|
| 186 | - '{DAV:}displayname' => $row['displayname'], |
|
| 187 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 188 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 189 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 190 | - ]; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * @param $addressBookUri |
|
| 195 | - * @return array|null |
|
| 196 | - */ |
|
| 197 | - public function getAddressBooksByUri($principal, $addressBookUri) { |
|
| 198 | - $query = $this->db->getQueryBuilder(); |
|
| 199 | - $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 200 | - ->from('addressbooks') |
|
| 201 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($addressBookUri))) |
|
| 202 | - ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal))) |
|
| 203 | - ->setMaxResults(1) |
|
| 204 | - ->execute(); |
|
| 205 | - |
|
| 206 | - $row = $result->fetch(); |
|
| 207 | - $result->closeCursor(); |
|
| 208 | - if ($row === false) { |
|
| 209 | - return null; |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - return [ |
|
| 213 | - 'id' => $row['id'], |
|
| 214 | - 'uri' => $row['uri'], |
|
| 215 | - 'principaluri' => $row['principaluri'], |
|
| 216 | - '{DAV:}displayname' => $row['displayname'], |
|
| 217 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 218 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 219 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 220 | - ]; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * Updates properties for an address book. |
|
| 225 | - * |
|
| 226 | - * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
| 227 | - * To do the actual updates, you must tell this object which properties |
|
| 228 | - * you're going to process with the handle() method. |
|
| 229 | - * |
|
| 230 | - * Calling the handle method is like telling the PropPatch object "I |
|
| 231 | - * promise I can handle updating this property". |
|
| 232 | - * |
|
| 233 | - * Read the PropPatch documentation for more info and examples. |
|
| 234 | - * |
|
| 235 | - * @param string $addressBookId |
|
| 236 | - * @param \Sabre\DAV\PropPatch $propPatch |
|
| 237 | - * @return void |
|
| 238 | - */ |
|
| 239 | - function updateAddressBook($addressBookId, \Sabre\DAV\PropPatch $propPatch) { |
|
| 240 | - $supportedProperties = [ |
|
| 241 | - '{DAV:}displayname', |
|
| 242 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description', |
|
| 243 | - ]; |
|
| 244 | - |
|
| 245 | - $propPatch->handle($supportedProperties, function($mutations) use ($addressBookId) { |
|
| 246 | - |
|
| 247 | - $updates = []; |
|
| 248 | - foreach($mutations as $property=>$newValue) { |
|
| 249 | - |
|
| 250 | - switch($property) { |
|
| 251 | - case '{DAV:}displayname' : |
|
| 252 | - $updates['displayname'] = $newValue; |
|
| 253 | - break; |
|
| 254 | - case '{' . Plugin::NS_CARDDAV . '}addressbook-description' : |
|
| 255 | - $updates['description'] = $newValue; |
|
| 256 | - break; |
|
| 257 | - } |
|
| 258 | - } |
|
| 259 | - $query = $this->db->getQueryBuilder(); |
|
| 260 | - $query->update('addressbooks'); |
|
| 261 | - |
|
| 262 | - foreach($updates as $key=>$value) { |
|
| 263 | - $query->set($key, $query->createNamedParameter($value)); |
|
| 264 | - } |
|
| 265 | - $query->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) |
|
| 266 | - ->execute(); |
|
| 267 | - |
|
| 268 | - $this->addChange($addressBookId, "", 2); |
|
| 269 | - |
|
| 270 | - return true; |
|
| 271 | - |
|
| 272 | - }); |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * Creates a new address book |
|
| 277 | - * |
|
| 278 | - * @param string $principalUri |
|
| 279 | - * @param string $url Just the 'basename' of the url. |
|
| 280 | - * @param array $properties |
|
| 281 | - * @return int |
|
| 282 | - * @throws BadRequest |
|
| 283 | - */ |
|
| 284 | - function createAddressBook($principalUri, $url, array $properties) { |
|
| 285 | - $values = [ |
|
| 286 | - 'displayname' => null, |
|
| 287 | - 'description' => null, |
|
| 288 | - 'principaluri' => $principalUri, |
|
| 289 | - 'uri' => $url, |
|
| 290 | - 'synctoken' => 1 |
|
| 291 | - ]; |
|
| 292 | - |
|
| 293 | - foreach($properties as $property=>$newValue) { |
|
| 294 | - |
|
| 295 | - switch($property) { |
|
| 296 | - case '{DAV:}displayname' : |
|
| 297 | - $values['displayname'] = $newValue; |
|
| 298 | - break; |
|
| 299 | - case '{' . Plugin::NS_CARDDAV . '}addressbook-description' : |
|
| 300 | - $values['description'] = $newValue; |
|
| 301 | - break; |
|
| 302 | - default : |
|
| 303 | - throw new BadRequest('Unknown property: ' . $property); |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - // Fallback to make sure the displayname is set. Some clients may refuse |
|
| 309 | - // to work with addressbooks not having a displayname. |
|
| 310 | - if(is_null($values['displayname'])) { |
|
| 311 | - $values['displayname'] = $url; |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - $query = $this->db->getQueryBuilder(); |
|
| 315 | - $query->insert('addressbooks') |
|
| 316 | - ->values([ |
|
| 317 | - 'uri' => $query->createParameter('uri'), |
|
| 318 | - 'displayname' => $query->createParameter('displayname'), |
|
| 319 | - 'description' => $query->createParameter('description'), |
|
| 320 | - 'principaluri' => $query->createParameter('principaluri'), |
|
| 321 | - 'synctoken' => $query->createParameter('synctoken'), |
|
| 322 | - ]) |
|
| 323 | - ->setParameters($values) |
|
| 324 | - ->execute(); |
|
| 325 | - |
|
| 326 | - return $query->getLastInsertId(); |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - /** |
|
| 330 | - * Deletes an entire addressbook and all its contents |
|
| 331 | - * |
|
| 332 | - * @param mixed $addressBookId |
|
| 333 | - * @return void |
|
| 334 | - */ |
|
| 335 | - function deleteAddressBook($addressBookId) { |
|
| 336 | - $query = $this->db->getQueryBuilder(); |
|
| 337 | - $query->delete('cards') |
|
| 338 | - ->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid'))) |
|
| 339 | - ->setParameter('addressbookid', $addressBookId) |
|
| 340 | - ->execute(); |
|
| 341 | - |
|
| 342 | - $query->delete('addressbookchanges') |
|
| 343 | - ->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid'))) |
|
| 344 | - ->setParameter('addressbookid', $addressBookId) |
|
| 345 | - ->execute(); |
|
| 346 | - |
|
| 347 | - $query->delete('addressbooks') |
|
| 348 | - ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
| 349 | - ->setParameter('id', $addressBookId) |
|
| 350 | - ->execute(); |
|
| 351 | - |
|
| 352 | - $this->sharingBackend->deleteAllShares($addressBookId); |
|
| 353 | - |
|
| 354 | - $query->delete($this->dbCardsPropertiesTable) |
|
| 355 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 356 | - ->execute(); |
|
| 357 | - |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - /** |
|
| 361 | - * Returns all cards for a specific addressbook id. |
|
| 362 | - * |
|
| 363 | - * This method should return the following properties for each card: |
|
| 364 | - * * carddata - raw vcard data |
|
| 365 | - * * uri - Some unique url |
|
| 366 | - * * lastmodified - A unix timestamp |
|
| 367 | - * |
|
| 368 | - * It's recommended to also return the following properties: |
|
| 369 | - * * etag - A unique etag. This must change every time the card changes. |
|
| 370 | - * * size - The size of the card in bytes. |
|
| 371 | - * |
|
| 372 | - * If these last two properties are provided, less time will be spent |
|
| 373 | - * calculating them. If they are specified, you can also ommit carddata. |
|
| 374 | - * This may speed up certain requests, especially with large cards. |
|
| 375 | - * |
|
| 376 | - * @param mixed $addressBookId |
|
| 377 | - * @return array |
|
| 378 | - */ |
|
| 379 | - function getCards($addressBookId) { |
|
| 380 | - $query = $this->db->getQueryBuilder(); |
|
| 381 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata']) |
|
| 382 | - ->from('cards') |
|
| 383 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 384 | - |
|
| 385 | - $cards = []; |
|
| 386 | - |
|
| 387 | - $result = $query->execute(); |
|
| 388 | - while($row = $result->fetch()) { |
|
| 389 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 390 | - $row['carddata'] = $this->readBlob($row['carddata']); |
|
| 391 | - $cards[] = $row; |
|
| 392 | - } |
|
| 393 | - $result->closeCursor(); |
|
| 394 | - |
|
| 395 | - return $cards; |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - /** |
|
| 399 | - * Returns a specific card. |
|
| 400 | - * |
|
| 401 | - * The same set of properties must be returned as with getCards. The only |
|
| 402 | - * exception is that 'carddata' is absolutely required. |
|
| 403 | - * |
|
| 404 | - * If the card does not exist, you must return false. |
|
| 405 | - * |
|
| 406 | - * @param mixed $addressBookId |
|
| 407 | - * @param string $cardUri |
|
| 408 | - * @return array |
|
| 409 | - */ |
|
| 410 | - function getCard($addressBookId, $cardUri) { |
|
| 411 | - $query = $this->db->getQueryBuilder(); |
|
| 412 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata']) |
|
| 413 | - ->from('cards') |
|
| 414 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 415 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 416 | - ->setMaxResults(1); |
|
| 417 | - |
|
| 418 | - $result = $query->execute(); |
|
| 419 | - $row = $result->fetch(); |
|
| 420 | - if (!$row) { |
|
| 421 | - return false; |
|
| 422 | - } |
|
| 423 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 424 | - $row['carddata'] = $this->readBlob($row['carddata']); |
|
| 425 | - |
|
| 426 | - return $row; |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - /** |
|
| 430 | - * Returns a list of cards. |
|
| 431 | - * |
|
| 432 | - * This method should work identical to getCard, but instead return all the |
|
| 433 | - * cards in the list as an array. |
|
| 434 | - * |
|
| 435 | - * If the backend supports this, it may allow for some speed-ups. |
|
| 436 | - * |
|
| 437 | - * @param mixed $addressBookId |
|
| 438 | - * @param string[] $uris |
|
| 439 | - * @return array |
|
| 440 | - */ |
|
| 441 | - function getMultipleCards($addressBookId, array $uris) { |
|
| 442 | - if (empty($uris)) { |
|
| 443 | - return []; |
|
| 444 | - } |
|
| 445 | - |
|
| 446 | - $chunks = array_chunk($uris, 100); |
|
| 447 | - $cards = []; |
|
| 448 | - |
|
| 449 | - $query = $this->db->getQueryBuilder(); |
|
| 450 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata']) |
|
| 451 | - ->from('cards') |
|
| 452 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 453 | - ->andWhere($query->expr()->in('uri', $query->createParameter('uri'))); |
|
| 454 | - |
|
| 455 | - foreach ($chunks as $uris) { |
|
| 456 | - $query->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY); |
|
| 457 | - $result = $query->execute(); |
|
| 458 | - |
|
| 459 | - while ($row = $result->fetch()) { |
|
| 460 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 461 | - $row['carddata'] = $this->readBlob($row['carddata']); |
|
| 462 | - $cards[] = $row; |
|
| 463 | - } |
|
| 464 | - $result->closeCursor(); |
|
| 465 | - } |
|
| 466 | - return $cards; |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - /** |
|
| 470 | - * Creates a new card. |
|
| 471 | - * |
|
| 472 | - * The addressbook id will be passed as the first argument. This is the |
|
| 473 | - * same id as it is returned from the getAddressBooksForUser method. |
|
| 474 | - * |
|
| 475 | - * The cardUri is a base uri, and doesn't include the full path. The |
|
| 476 | - * cardData argument is the vcard body, and is passed as a string. |
|
| 477 | - * |
|
| 478 | - * It is possible to return an ETag from this method. This ETag is for the |
|
| 479 | - * newly created resource, and must be enclosed with double quotes (that |
|
| 480 | - * is, the string itself must contain the double quotes). |
|
| 481 | - * |
|
| 482 | - * You should only return the ETag if you store the carddata as-is. If a |
|
| 483 | - * subsequent GET request on the same card does not have the same body, |
|
| 484 | - * byte-by-byte and you did return an ETag here, clients tend to get |
|
| 485 | - * confused. |
|
| 486 | - * |
|
| 487 | - * If you don't return an ETag, you can just return null. |
|
| 488 | - * |
|
| 489 | - * @param mixed $addressBookId |
|
| 490 | - * @param string $cardUri |
|
| 491 | - * @param string $cardData |
|
| 492 | - * @return string |
|
| 493 | - */ |
|
| 494 | - function createCard($addressBookId, $cardUri, $cardData) { |
|
| 495 | - $etag = md5($cardData); |
|
| 496 | - |
|
| 497 | - $query = $this->db->getQueryBuilder(); |
|
| 498 | - $query->insert('cards') |
|
| 499 | - ->values([ |
|
| 500 | - 'carddata' => $query->createNamedParameter($cardData, IQueryBuilder::PARAM_LOB), |
|
| 501 | - 'uri' => $query->createNamedParameter($cardUri), |
|
| 502 | - 'lastmodified' => $query->createNamedParameter(time()), |
|
| 503 | - 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
| 504 | - 'size' => $query->createNamedParameter(strlen($cardData)), |
|
| 505 | - 'etag' => $query->createNamedParameter($etag), |
|
| 506 | - ]) |
|
| 507 | - ->execute(); |
|
| 508 | - |
|
| 509 | - $this->addChange($addressBookId, $cardUri, 1); |
|
| 510 | - $this->updateProperties($addressBookId, $cardUri, $cardData); |
|
| 511 | - |
|
| 512 | - if (!is_null($this->dispatcher)) { |
|
| 513 | - $this->dispatcher->dispatch('\OCA\DAV\CardDAV\CardDavBackend::createCard', |
|
| 514 | - new GenericEvent(null, [ |
|
| 515 | - 'addressBookId' => $addressBookId, |
|
| 516 | - 'cardUri' => $cardUri, |
|
| 517 | - 'cardData' => $cardData])); |
|
| 518 | - } |
|
| 519 | - |
|
| 520 | - return '"' . $etag . '"'; |
|
| 521 | - } |
|
| 522 | - |
|
| 523 | - /** |
|
| 524 | - * Updates a card. |
|
| 525 | - * |
|
| 526 | - * The addressbook id will be passed as the first argument. This is the |
|
| 527 | - * same id as it is returned from the getAddressBooksForUser method. |
|
| 528 | - * |
|
| 529 | - * The cardUri is a base uri, and doesn't include the full path. The |
|
| 530 | - * cardData argument is the vcard body, and is passed as a string. |
|
| 531 | - * |
|
| 532 | - * It is possible to return an ETag from this method. This ETag should |
|
| 533 | - * match that of the updated resource, and must be enclosed with double |
|
| 534 | - * quotes (that is: the string itself must contain the actual quotes). |
|
| 535 | - * |
|
| 536 | - * You should only return the ETag if you store the carddata as-is. If a |
|
| 537 | - * subsequent GET request on the same card does not have the same body, |
|
| 538 | - * byte-by-byte and you did return an ETag here, clients tend to get |
|
| 539 | - * confused. |
|
| 540 | - * |
|
| 541 | - * If you don't return an ETag, you can just return null. |
|
| 542 | - * |
|
| 543 | - * @param mixed $addressBookId |
|
| 544 | - * @param string $cardUri |
|
| 545 | - * @param string $cardData |
|
| 546 | - * @return string |
|
| 547 | - */ |
|
| 548 | - function updateCard($addressBookId, $cardUri, $cardData) { |
|
| 549 | - |
|
| 550 | - $etag = md5($cardData); |
|
| 551 | - $query = $this->db->getQueryBuilder(); |
|
| 552 | - $query->update('cards') |
|
| 553 | - ->set('carddata', $query->createNamedParameter($cardData, IQueryBuilder::PARAM_LOB)) |
|
| 554 | - ->set('lastmodified', $query->createNamedParameter(time())) |
|
| 555 | - ->set('size', $query->createNamedParameter(strlen($cardData))) |
|
| 556 | - ->set('etag', $query->createNamedParameter($etag)) |
|
| 557 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 558 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 559 | - ->execute(); |
|
| 560 | - |
|
| 561 | - $this->addChange($addressBookId, $cardUri, 2); |
|
| 562 | - $this->updateProperties($addressBookId, $cardUri, $cardData); |
|
| 563 | - |
|
| 564 | - if (!is_null($this->dispatcher)) { |
|
| 565 | - $this->dispatcher->dispatch('\OCA\DAV\CardDAV\CardDavBackend::updateCard', |
|
| 566 | - new GenericEvent(null, [ |
|
| 567 | - 'addressBookId' => $addressBookId, |
|
| 568 | - 'cardUri' => $cardUri, |
|
| 569 | - 'cardData' => $cardData])); |
|
| 570 | - } |
|
| 571 | - |
|
| 572 | - return '"' . $etag . '"'; |
|
| 573 | - } |
|
| 574 | - |
|
| 575 | - /** |
|
| 576 | - * Deletes a card |
|
| 577 | - * |
|
| 578 | - * @param mixed $addressBookId |
|
| 579 | - * @param string $cardUri |
|
| 580 | - * @return bool |
|
| 581 | - */ |
|
| 582 | - function deleteCard($addressBookId, $cardUri) { |
|
| 583 | - try { |
|
| 584 | - $cardId = $this->getCardId($addressBookId, $cardUri); |
|
| 585 | - } catch (\InvalidArgumentException $e) { |
|
| 586 | - $cardId = null; |
|
| 587 | - } |
|
| 588 | - $query = $this->db->getQueryBuilder(); |
|
| 589 | - $ret = $query->delete('cards') |
|
| 590 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 591 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 592 | - ->execute(); |
|
| 593 | - |
|
| 594 | - $this->addChange($addressBookId, $cardUri, 3); |
|
| 595 | - |
|
| 596 | - if (!is_null($this->dispatcher)) { |
|
| 597 | - $this->dispatcher->dispatch('\OCA\DAV\CardDAV\CardDavBackend::deleteCard', |
|
| 598 | - new GenericEvent(null, [ |
|
| 599 | - 'addressBookId' => $addressBookId, |
|
| 600 | - 'cardUri' => $cardUri])); |
|
| 601 | - } |
|
| 602 | - |
|
| 603 | - if ($ret === 1) { |
|
| 604 | - if ($cardId !== null) { |
|
| 605 | - $this->purgeProperties($addressBookId, $cardId); |
|
| 606 | - } |
|
| 607 | - return true; |
|
| 608 | - } |
|
| 609 | - |
|
| 610 | - return false; |
|
| 611 | - } |
|
| 612 | - |
|
| 613 | - /** |
|
| 614 | - * The getChanges method returns all the changes that have happened, since |
|
| 615 | - * the specified syncToken in the specified address book. |
|
| 616 | - * |
|
| 617 | - * This function should return an array, such as the following: |
|
| 618 | - * |
|
| 619 | - * [ |
|
| 620 | - * 'syncToken' => 'The current synctoken', |
|
| 621 | - * 'added' => [ |
|
| 622 | - * 'new.txt', |
|
| 623 | - * ], |
|
| 624 | - * 'modified' => [ |
|
| 625 | - * 'modified.txt', |
|
| 626 | - * ], |
|
| 627 | - * 'deleted' => [ |
|
| 628 | - * 'foo.php.bak', |
|
| 629 | - * 'old.txt' |
|
| 630 | - * ] |
|
| 631 | - * ]; |
|
| 632 | - * |
|
| 633 | - * The returned syncToken property should reflect the *current* syncToken |
|
| 634 | - * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
|
| 635 | - * property. This is needed here too, to ensure the operation is atomic. |
|
| 636 | - * |
|
| 637 | - * If the $syncToken argument is specified as null, this is an initial |
|
| 638 | - * sync, and all members should be reported. |
|
| 639 | - * |
|
| 640 | - * The modified property is an array of nodenames that have changed since |
|
| 641 | - * the last token. |
|
| 642 | - * |
|
| 643 | - * The deleted property is an array with nodenames, that have been deleted |
|
| 644 | - * from collection. |
|
| 645 | - * |
|
| 646 | - * The $syncLevel argument is basically the 'depth' of the report. If it's |
|
| 647 | - * 1, you only have to report changes that happened only directly in |
|
| 648 | - * immediate descendants. If it's 2, it should also include changes from |
|
| 649 | - * the nodes below the child collections. (grandchildren) |
|
| 650 | - * |
|
| 651 | - * The $limit argument allows a client to specify how many results should |
|
| 652 | - * be returned at most. If the limit is not specified, it should be treated |
|
| 653 | - * as infinite. |
|
| 654 | - * |
|
| 655 | - * If the limit (infinite or not) is higher than you're willing to return, |
|
| 656 | - * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
|
| 657 | - * |
|
| 658 | - * If the syncToken is expired (due to data cleanup) or unknown, you must |
|
| 659 | - * return null. |
|
| 660 | - * |
|
| 661 | - * The limit is 'suggestive'. You are free to ignore it. |
|
| 662 | - * |
|
| 663 | - * @param string $addressBookId |
|
| 664 | - * @param string $syncToken |
|
| 665 | - * @param int $syncLevel |
|
| 666 | - * @param int $limit |
|
| 667 | - * @return array |
|
| 668 | - */ |
|
| 669 | - function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel, $limit = null) { |
|
| 670 | - // Current synctoken |
|
| 671 | - $stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*addressbooks` WHERE `id` = ?'); |
|
| 672 | - $stmt->execute([ $addressBookId ]); |
|
| 673 | - $currentToken = $stmt->fetchColumn(0); |
|
| 674 | - |
|
| 675 | - if (is_null($currentToken)) return null; |
|
| 676 | - |
|
| 677 | - $result = [ |
|
| 678 | - 'syncToken' => $currentToken, |
|
| 679 | - 'added' => [], |
|
| 680 | - 'modified' => [], |
|
| 681 | - 'deleted' => [], |
|
| 682 | - ]; |
|
| 683 | - |
|
| 684 | - if ($syncToken) { |
|
| 685 | - |
|
| 686 | - $query = "SELECT `uri`, `operation` FROM `*PREFIX*addressbookchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `addressbookid` = ? ORDER BY `synctoken`"; |
|
| 687 | - if ($limit>0) { |
|
| 688 | - $query .= " `LIMIT` " . (int)$limit; |
|
| 689 | - } |
|
| 690 | - |
|
| 691 | - // Fetching all changes |
|
| 692 | - $stmt = $this->db->prepare($query); |
|
| 693 | - $stmt->execute([$syncToken, $currentToken, $addressBookId]); |
|
| 694 | - |
|
| 695 | - $changes = []; |
|
| 696 | - |
|
| 697 | - // This loop ensures that any duplicates are overwritten, only the |
|
| 698 | - // last change on a node is relevant. |
|
| 699 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 700 | - |
|
| 701 | - $changes[$row['uri']] = $row['operation']; |
|
| 702 | - |
|
| 703 | - } |
|
| 704 | - |
|
| 705 | - foreach($changes as $uri => $operation) { |
|
| 706 | - |
|
| 707 | - switch($operation) { |
|
| 708 | - case 1: |
|
| 709 | - $result['added'][] = $uri; |
|
| 710 | - break; |
|
| 711 | - case 2: |
|
| 712 | - $result['modified'][] = $uri; |
|
| 713 | - break; |
|
| 714 | - case 3: |
|
| 715 | - $result['deleted'][] = $uri; |
|
| 716 | - break; |
|
| 717 | - } |
|
| 718 | - |
|
| 719 | - } |
|
| 720 | - } else { |
|
| 721 | - // No synctoken supplied, this is the initial sync. |
|
| 722 | - $query = "SELECT `uri` FROM `*PREFIX*cards` WHERE `addressbookid` = ?"; |
|
| 723 | - $stmt = $this->db->prepare($query); |
|
| 724 | - $stmt->execute([$addressBookId]); |
|
| 725 | - |
|
| 726 | - $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
| 727 | - } |
|
| 728 | - return $result; |
|
| 729 | - } |
|
| 730 | - |
|
| 731 | - /** |
|
| 732 | - * Adds a change record to the addressbookchanges table. |
|
| 733 | - * |
|
| 734 | - * @param mixed $addressBookId |
|
| 735 | - * @param string $objectUri |
|
| 736 | - * @param int $operation 1 = add, 2 = modify, 3 = delete |
|
| 737 | - * @return void |
|
| 738 | - */ |
|
| 739 | - protected function addChange($addressBookId, $objectUri, $operation) { |
|
| 740 | - $sql = 'INSERT INTO `*PREFIX*addressbookchanges`(`uri`, `synctoken`, `addressbookid`, `operation`) SELECT ?, `synctoken`, ?, ? FROM `*PREFIX*addressbooks` WHERE `id` = ?'; |
|
| 741 | - $stmt = $this->db->prepare($sql); |
|
| 742 | - $stmt->execute([ |
|
| 743 | - $objectUri, |
|
| 744 | - $addressBookId, |
|
| 745 | - $operation, |
|
| 746 | - $addressBookId |
|
| 747 | - ]); |
|
| 748 | - $stmt = $this->db->prepare('UPDATE `*PREFIX*addressbooks` SET `synctoken` = `synctoken` + 1 WHERE `id` = ?'); |
|
| 749 | - $stmt->execute([ |
|
| 750 | - $addressBookId |
|
| 751 | - ]); |
|
| 752 | - } |
|
| 753 | - |
|
| 754 | - private function readBlob($cardData) { |
|
| 755 | - if (is_resource($cardData)) { |
|
| 756 | - return stream_get_contents($cardData); |
|
| 757 | - } |
|
| 758 | - |
|
| 759 | - return $cardData; |
|
| 760 | - } |
|
| 761 | - |
|
| 762 | - /** |
|
| 763 | - * @param IShareable $shareable |
|
| 764 | - * @param string[] $add |
|
| 765 | - * @param string[] $remove |
|
| 766 | - */ |
|
| 767 | - public function updateShares(IShareable $shareable, $add, $remove) { |
|
| 768 | - $this->sharingBackend->updateShares($shareable, $add, $remove); |
|
| 769 | - } |
|
| 770 | - |
|
| 771 | - /** |
|
| 772 | - * search contact |
|
| 773 | - * |
|
| 774 | - * @param int $addressBookId |
|
| 775 | - * @param string $pattern which should match within the $searchProperties |
|
| 776 | - * @param array $searchProperties defines the properties within the query pattern should match |
|
| 777 | - * @return array an array of contacts which are arrays of key-value-pairs |
|
| 778 | - */ |
|
| 779 | - public function search($addressBookId, $pattern, $searchProperties) { |
|
| 780 | - $query = $this->db->getQueryBuilder(); |
|
| 781 | - $query2 = $this->db->getQueryBuilder(); |
|
| 782 | - $query2->selectDistinct('cp.cardid')->from($this->dbCardsPropertiesTable, 'cp'); |
|
| 783 | - foreach ($searchProperties as $property) { |
|
| 784 | - $query2->orWhere( |
|
| 785 | - $query2->expr()->andX( |
|
| 786 | - $query2->expr()->eq('cp.name', $query->createNamedParameter($property)), |
|
| 787 | - $query2->expr()->ilike('cp.value', $query->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%')) |
|
| 788 | - ) |
|
| 789 | - ); |
|
| 790 | - } |
|
| 791 | - $query2->andWhere($query2->expr()->eq('cp.addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 792 | - |
|
| 793 | - $query->select('c.carddata', 'c.uri')->from($this->dbCardsTable, 'c') |
|
| 794 | - ->where($query->expr()->in('c.id', $query->createFunction($query2->getSQL()))); |
|
| 795 | - |
|
| 796 | - $result = $query->execute(); |
|
| 797 | - $cards = $result->fetchAll(); |
|
| 798 | - |
|
| 799 | - $result->closeCursor(); |
|
| 800 | - |
|
| 801 | - return array_map(function($array) { |
|
| 802 | - $array['carddata'] = $this->readBlob($array['carddata']); |
|
| 803 | - return $array; |
|
| 804 | - }, $cards); |
|
| 805 | - } |
|
| 806 | - |
|
| 807 | - /** |
|
| 808 | - * @param int $bookId |
|
| 809 | - * @param string $name |
|
| 810 | - * @return array |
|
| 811 | - */ |
|
| 812 | - public function collectCardProperties($bookId, $name) { |
|
| 813 | - $query = $this->db->getQueryBuilder(); |
|
| 814 | - $result = $query->selectDistinct('value') |
|
| 815 | - ->from($this->dbCardsPropertiesTable) |
|
| 816 | - ->where($query->expr()->eq('name', $query->createNamedParameter($name))) |
|
| 817 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($bookId))) |
|
| 818 | - ->execute(); |
|
| 819 | - |
|
| 820 | - $all = $result->fetchAll(PDO::FETCH_COLUMN); |
|
| 821 | - $result->closeCursor(); |
|
| 822 | - |
|
| 823 | - return $all; |
|
| 824 | - } |
|
| 825 | - |
|
| 826 | - /** |
|
| 827 | - * get URI from a given contact |
|
| 828 | - * |
|
| 829 | - * @param int $id |
|
| 830 | - * @return string |
|
| 831 | - */ |
|
| 832 | - public function getCardUri($id) { |
|
| 833 | - $query = $this->db->getQueryBuilder(); |
|
| 834 | - $query->select('uri')->from($this->dbCardsTable) |
|
| 835 | - ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
| 836 | - ->setParameter('id', $id); |
|
| 837 | - |
|
| 838 | - $result = $query->execute(); |
|
| 839 | - $uri = $result->fetch(); |
|
| 840 | - $result->closeCursor(); |
|
| 841 | - |
|
| 842 | - if (!isset($uri['uri'])) { |
|
| 843 | - throw new \InvalidArgumentException('Card does not exists: ' . $id); |
|
| 844 | - } |
|
| 845 | - |
|
| 846 | - return $uri['uri']; |
|
| 847 | - } |
|
| 848 | - |
|
| 849 | - /** |
|
| 850 | - * return contact with the given URI |
|
| 851 | - * |
|
| 852 | - * @param int $addressBookId |
|
| 853 | - * @param string $uri |
|
| 854 | - * @returns array |
|
| 855 | - */ |
|
| 856 | - public function getContact($addressBookId, $uri) { |
|
| 857 | - $result = []; |
|
| 858 | - $query = $this->db->getQueryBuilder(); |
|
| 859 | - $query->select('*')->from($this->dbCardsTable) |
|
| 860 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 861 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 862 | - $queryResult = $query->execute(); |
|
| 863 | - $contact = $queryResult->fetch(); |
|
| 864 | - $queryResult->closeCursor(); |
|
| 865 | - |
|
| 866 | - if (is_array($contact)) { |
|
| 867 | - $result = $contact; |
|
| 868 | - } |
|
| 869 | - |
|
| 870 | - return $result; |
|
| 871 | - } |
|
| 872 | - |
|
| 873 | - /** |
|
| 874 | - * Returns the list of people whom this address book is shared with. |
|
| 875 | - * |
|
| 876 | - * Every element in this array should have the following properties: |
|
| 877 | - * * href - Often a mailto: address |
|
| 878 | - * * commonName - Optional, for example a first + last name |
|
| 879 | - * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
|
| 880 | - * * readOnly - boolean |
|
| 881 | - * * summary - Optional, a description for the share |
|
| 882 | - * |
|
| 883 | - * @return array |
|
| 884 | - */ |
|
| 885 | - public function getShares($addressBookId) { |
|
| 886 | - return $this->sharingBackend->getShares($addressBookId); |
|
| 887 | - } |
|
| 888 | - |
|
| 889 | - /** |
|
| 890 | - * update properties table |
|
| 891 | - * |
|
| 892 | - * @param int $addressBookId |
|
| 893 | - * @param string $cardUri |
|
| 894 | - * @param string $vCardSerialized |
|
| 895 | - */ |
|
| 896 | - protected function updateProperties($addressBookId, $cardUri, $vCardSerialized) { |
|
| 897 | - $cardId = $this->getCardId($addressBookId, $cardUri); |
|
| 898 | - $vCard = $this->readCard($vCardSerialized); |
|
| 899 | - |
|
| 900 | - $this->purgeProperties($addressBookId, $cardId); |
|
| 901 | - |
|
| 902 | - $query = $this->db->getQueryBuilder(); |
|
| 903 | - $query->insert($this->dbCardsPropertiesTable) |
|
| 904 | - ->values( |
|
| 905 | - [ |
|
| 906 | - 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
| 907 | - 'cardid' => $query->createNamedParameter($cardId), |
|
| 908 | - 'name' => $query->createParameter('name'), |
|
| 909 | - 'value' => $query->createParameter('value'), |
|
| 910 | - 'preferred' => $query->createParameter('preferred') |
|
| 911 | - ] |
|
| 912 | - ); |
|
| 913 | - |
|
| 914 | - foreach ($vCard->children as $property) { |
|
| 915 | - if(!in_array($property->name, self::$indexProperties)) { |
|
| 916 | - continue; |
|
| 917 | - } |
|
| 918 | - $preferred = 0; |
|
| 919 | - foreach($property->parameters as $parameter) { |
|
| 920 | - if ($parameter->name == 'TYPE' && strtoupper($parameter->getValue()) == 'PREF') { |
|
| 921 | - $preferred = 1; |
|
| 922 | - break; |
|
| 923 | - } |
|
| 924 | - } |
|
| 925 | - $query->setParameter('name', $property->name); |
|
| 926 | - $query->setParameter('value', substr($property->getValue(), 0, 254)); |
|
| 927 | - $query->setParameter('preferred', $preferred); |
|
| 928 | - $query->execute(); |
|
| 929 | - } |
|
| 930 | - } |
|
| 931 | - |
|
| 932 | - /** |
|
| 933 | - * read vCard data into a vCard object |
|
| 934 | - * |
|
| 935 | - * @param string $cardData |
|
| 936 | - * @return VCard |
|
| 937 | - */ |
|
| 938 | - protected function readCard($cardData) { |
|
| 939 | - return Reader::read($cardData); |
|
| 940 | - } |
|
| 941 | - |
|
| 942 | - /** |
|
| 943 | - * delete all properties from a given card |
|
| 944 | - * |
|
| 945 | - * @param int $addressBookId |
|
| 946 | - * @param int $cardId |
|
| 947 | - */ |
|
| 948 | - protected function purgeProperties($addressBookId, $cardId) { |
|
| 949 | - $query = $this->db->getQueryBuilder(); |
|
| 950 | - $query->delete($this->dbCardsPropertiesTable) |
|
| 951 | - ->where($query->expr()->eq('cardid', $query->createNamedParameter($cardId))) |
|
| 952 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 953 | - $query->execute(); |
|
| 954 | - } |
|
| 955 | - |
|
| 956 | - /** |
|
| 957 | - * get ID from a given contact |
|
| 958 | - * |
|
| 959 | - * @param int $addressBookId |
|
| 960 | - * @param string $uri |
|
| 961 | - * @return int |
|
| 962 | - */ |
|
| 963 | - protected function getCardId($addressBookId, $uri) { |
|
| 964 | - $query = $this->db->getQueryBuilder(); |
|
| 965 | - $query->select('id')->from($this->dbCardsTable) |
|
| 966 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 967 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 968 | - |
|
| 969 | - $result = $query->execute(); |
|
| 970 | - $cardIds = $result->fetch(); |
|
| 971 | - $result->closeCursor(); |
|
| 972 | - |
|
| 973 | - if (!isset($cardIds['id'])) { |
|
| 974 | - throw new \InvalidArgumentException('Card does not exists: ' . $uri); |
|
| 975 | - } |
|
| 976 | - |
|
| 977 | - return (int)$cardIds['id']; |
|
| 978 | - } |
|
| 979 | - |
|
| 980 | - /** |
|
| 981 | - * For shared address books the sharee is set in the ACL of the address book |
|
| 982 | - * @param $addressBookId |
|
| 983 | - * @param $acl |
|
| 984 | - * @return array |
|
| 985 | - */ |
|
| 986 | - public function applyShareAcl($addressBookId, $acl) { |
|
| 987 | - return $this->sharingBackend->applyShareAcl($addressBookId, $acl); |
|
| 988 | - } |
|
| 989 | - |
|
| 990 | - private function convertPrincipal($principalUri, $toV2) { |
|
| 991 | - if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
|
| 992 | - list(, $name) = URLUtil::splitPath($principalUri); |
|
| 993 | - if ($toV2 === true) { |
|
| 994 | - return "principals/users/$name"; |
|
| 995 | - } |
|
| 996 | - return "principals/$name"; |
|
| 997 | - } |
|
| 998 | - return $principalUri; |
|
| 999 | - } |
|
| 49 | + /** @var Principal */ |
|
| 50 | + private $principalBackend; |
|
| 51 | + |
|
| 52 | + /** @var string */ |
|
| 53 | + private $dbCardsTable = 'cards'; |
|
| 54 | + |
|
| 55 | + /** @var string */ |
|
| 56 | + private $dbCardsPropertiesTable = 'cards_properties'; |
|
| 57 | + |
|
| 58 | + /** @var IDBConnection */ |
|
| 59 | + private $db; |
|
| 60 | + |
|
| 61 | + /** @var Backend */ |
|
| 62 | + private $sharingBackend; |
|
| 63 | + |
|
| 64 | + /** @var array properties to index */ |
|
| 65 | + public static $indexProperties = array( |
|
| 66 | + 'BDAY', 'UID', 'N', 'FN', 'TITLE', 'ROLE', 'NOTE', 'NICKNAME', |
|
| 67 | + 'ORG', 'CATEGORIES', 'EMAIL', 'TEL', 'IMPP', 'ADR', 'URL', 'GEO', 'CLOUD'); |
|
| 68 | + |
|
| 69 | + /** @var EventDispatcherInterface */ |
|
| 70 | + private $dispatcher; |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * CardDavBackend constructor. |
|
| 74 | + * |
|
| 75 | + * @param IDBConnection $db |
|
| 76 | + * @param Principal $principalBackend |
|
| 77 | + * @param EventDispatcherInterface $dispatcher |
|
| 78 | + */ |
|
| 79 | + public function __construct(IDBConnection $db, |
|
| 80 | + Principal $principalBackend, |
|
| 81 | + EventDispatcherInterface $dispatcher = null) { |
|
| 82 | + $this->db = $db; |
|
| 83 | + $this->principalBackend = $principalBackend; |
|
| 84 | + $this->dispatcher = $dispatcher; |
|
| 85 | + $this->sharingBackend = new Backend($this->db, $principalBackend, 'addressbook'); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * Returns the list of address books for a specific user. |
|
| 90 | + * |
|
| 91 | + * Every addressbook should have the following properties: |
|
| 92 | + * id - an arbitrary unique id |
|
| 93 | + * uri - the 'basename' part of the url |
|
| 94 | + * principaluri - Same as the passed parameter |
|
| 95 | + * |
|
| 96 | + * Any additional clark-notation property may be passed besides this. Some |
|
| 97 | + * common ones are : |
|
| 98 | + * {DAV:}displayname |
|
| 99 | + * {urn:ietf:params:xml:ns:carddav}addressbook-description |
|
| 100 | + * {http://calendarserver.org/ns/}getctag |
|
| 101 | + * |
|
| 102 | + * @param string $principalUri |
|
| 103 | + * @return array |
|
| 104 | + */ |
|
| 105 | + function getAddressBooksForUser($principalUri) { |
|
| 106 | + $principalUriOriginal = $principalUri; |
|
| 107 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 108 | + $query = $this->db->getQueryBuilder(); |
|
| 109 | + $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 110 | + ->from('addressbooks') |
|
| 111 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 112 | + |
|
| 113 | + $addressBooks = []; |
|
| 114 | + |
|
| 115 | + $result = $query->execute(); |
|
| 116 | + while($row = $result->fetch()) { |
|
| 117 | + $addressBooks[$row['id']] = [ |
|
| 118 | + 'id' => $row['id'], |
|
| 119 | + 'uri' => $row['uri'], |
|
| 120 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
|
| 121 | + '{DAV:}displayname' => $row['displayname'], |
|
| 122 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 123 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 124 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 125 | + ]; |
|
| 126 | + } |
|
| 127 | + $result->closeCursor(); |
|
| 128 | + |
|
| 129 | + // query for shared calendars |
|
| 130 | + $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); |
|
| 131 | + $principals[]= $principalUri; |
|
| 132 | + |
|
| 133 | + $query = $this->db->getQueryBuilder(); |
|
| 134 | + $result = $query->select(['a.id', 'a.uri', 'a.displayname', 'a.principaluri', 'a.description', 'a.synctoken', 's.access']) |
|
| 135 | + ->from('dav_shares', 's') |
|
| 136 | + ->join('s', 'addressbooks', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
| 137 | + ->where($query->expr()->in('s.principaluri', $query->createParameter('principaluri'))) |
|
| 138 | + ->andWhere($query->expr()->eq('s.type', $query->createParameter('type'))) |
|
| 139 | + ->setParameter('type', 'addressbook') |
|
| 140 | + ->setParameter('principaluri', $principals, IQueryBuilder::PARAM_STR_ARRAY) |
|
| 141 | + ->execute(); |
|
| 142 | + |
|
| 143 | + while($row = $result->fetch()) { |
|
| 144 | + list(, $name) = URLUtil::splitPath($row['principaluri']); |
|
| 145 | + $uri = $row['uri'] . '_shared_by_' . $name; |
|
| 146 | + $displayName = $row['displayname'] . "($name)"; |
|
| 147 | + if (!isset($addressBooks[$row['id']])) { |
|
| 148 | + $addressBooks[$row['id']] = [ |
|
| 149 | + 'id' => $row['id'], |
|
| 150 | + 'uri' => $uri, |
|
| 151 | + 'principaluri' => $principalUri, |
|
| 152 | + '{DAV:}displayname' => $displayName, |
|
| 153 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 154 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 155 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 156 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $row['principaluri'], |
|
| 157 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ, |
|
| 158 | + ]; |
|
| 159 | + } |
|
| 160 | + } |
|
| 161 | + $result->closeCursor(); |
|
| 162 | + |
|
| 163 | + return array_values($addressBooks); |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * @param int $addressBookId |
|
| 168 | + */ |
|
| 169 | + public function getAddressBookById($addressBookId) { |
|
| 170 | + $query = $this->db->getQueryBuilder(); |
|
| 171 | + $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 172 | + ->from('addressbooks') |
|
| 173 | + ->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) |
|
| 174 | + ->execute(); |
|
| 175 | + |
|
| 176 | + $row = $result->fetch(); |
|
| 177 | + $result->closeCursor(); |
|
| 178 | + if ($row === false) { |
|
| 179 | + return null; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + return [ |
|
| 183 | + 'id' => $row['id'], |
|
| 184 | + 'uri' => $row['uri'], |
|
| 185 | + 'principaluri' => $row['principaluri'], |
|
| 186 | + '{DAV:}displayname' => $row['displayname'], |
|
| 187 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 188 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 189 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 190 | + ]; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * @param $addressBookUri |
|
| 195 | + * @return array|null |
|
| 196 | + */ |
|
| 197 | + public function getAddressBooksByUri($principal, $addressBookUri) { |
|
| 198 | + $query = $this->db->getQueryBuilder(); |
|
| 199 | + $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 200 | + ->from('addressbooks') |
|
| 201 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($addressBookUri))) |
|
| 202 | + ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal))) |
|
| 203 | + ->setMaxResults(1) |
|
| 204 | + ->execute(); |
|
| 205 | + |
|
| 206 | + $row = $result->fetch(); |
|
| 207 | + $result->closeCursor(); |
|
| 208 | + if ($row === false) { |
|
| 209 | + return null; |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + return [ |
|
| 213 | + 'id' => $row['id'], |
|
| 214 | + 'uri' => $row['uri'], |
|
| 215 | + 'principaluri' => $row['principaluri'], |
|
| 216 | + '{DAV:}displayname' => $row['displayname'], |
|
| 217 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 218 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 219 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 220 | + ]; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * Updates properties for an address book. |
|
| 225 | + * |
|
| 226 | + * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
| 227 | + * To do the actual updates, you must tell this object which properties |
|
| 228 | + * you're going to process with the handle() method. |
|
| 229 | + * |
|
| 230 | + * Calling the handle method is like telling the PropPatch object "I |
|
| 231 | + * promise I can handle updating this property". |
|
| 232 | + * |
|
| 233 | + * Read the PropPatch documentation for more info and examples. |
|
| 234 | + * |
|
| 235 | + * @param string $addressBookId |
|
| 236 | + * @param \Sabre\DAV\PropPatch $propPatch |
|
| 237 | + * @return void |
|
| 238 | + */ |
|
| 239 | + function updateAddressBook($addressBookId, \Sabre\DAV\PropPatch $propPatch) { |
|
| 240 | + $supportedProperties = [ |
|
| 241 | + '{DAV:}displayname', |
|
| 242 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description', |
|
| 243 | + ]; |
|
| 244 | + |
|
| 245 | + $propPatch->handle($supportedProperties, function($mutations) use ($addressBookId) { |
|
| 246 | + |
|
| 247 | + $updates = []; |
|
| 248 | + foreach($mutations as $property=>$newValue) { |
|
| 249 | + |
|
| 250 | + switch($property) { |
|
| 251 | + case '{DAV:}displayname' : |
|
| 252 | + $updates['displayname'] = $newValue; |
|
| 253 | + break; |
|
| 254 | + case '{' . Plugin::NS_CARDDAV . '}addressbook-description' : |
|
| 255 | + $updates['description'] = $newValue; |
|
| 256 | + break; |
|
| 257 | + } |
|
| 258 | + } |
|
| 259 | + $query = $this->db->getQueryBuilder(); |
|
| 260 | + $query->update('addressbooks'); |
|
| 261 | + |
|
| 262 | + foreach($updates as $key=>$value) { |
|
| 263 | + $query->set($key, $query->createNamedParameter($value)); |
|
| 264 | + } |
|
| 265 | + $query->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) |
|
| 266 | + ->execute(); |
|
| 267 | + |
|
| 268 | + $this->addChange($addressBookId, "", 2); |
|
| 269 | + |
|
| 270 | + return true; |
|
| 271 | + |
|
| 272 | + }); |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * Creates a new address book |
|
| 277 | + * |
|
| 278 | + * @param string $principalUri |
|
| 279 | + * @param string $url Just the 'basename' of the url. |
|
| 280 | + * @param array $properties |
|
| 281 | + * @return int |
|
| 282 | + * @throws BadRequest |
|
| 283 | + */ |
|
| 284 | + function createAddressBook($principalUri, $url, array $properties) { |
|
| 285 | + $values = [ |
|
| 286 | + 'displayname' => null, |
|
| 287 | + 'description' => null, |
|
| 288 | + 'principaluri' => $principalUri, |
|
| 289 | + 'uri' => $url, |
|
| 290 | + 'synctoken' => 1 |
|
| 291 | + ]; |
|
| 292 | + |
|
| 293 | + foreach($properties as $property=>$newValue) { |
|
| 294 | + |
|
| 295 | + switch($property) { |
|
| 296 | + case '{DAV:}displayname' : |
|
| 297 | + $values['displayname'] = $newValue; |
|
| 298 | + break; |
|
| 299 | + case '{' . Plugin::NS_CARDDAV . '}addressbook-description' : |
|
| 300 | + $values['description'] = $newValue; |
|
| 301 | + break; |
|
| 302 | + default : |
|
| 303 | + throw new BadRequest('Unknown property: ' . $property); |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + // Fallback to make sure the displayname is set. Some clients may refuse |
|
| 309 | + // to work with addressbooks not having a displayname. |
|
| 310 | + if(is_null($values['displayname'])) { |
|
| 311 | + $values['displayname'] = $url; |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + $query = $this->db->getQueryBuilder(); |
|
| 315 | + $query->insert('addressbooks') |
|
| 316 | + ->values([ |
|
| 317 | + 'uri' => $query->createParameter('uri'), |
|
| 318 | + 'displayname' => $query->createParameter('displayname'), |
|
| 319 | + 'description' => $query->createParameter('description'), |
|
| 320 | + 'principaluri' => $query->createParameter('principaluri'), |
|
| 321 | + 'synctoken' => $query->createParameter('synctoken'), |
|
| 322 | + ]) |
|
| 323 | + ->setParameters($values) |
|
| 324 | + ->execute(); |
|
| 325 | + |
|
| 326 | + return $query->getLastInsertId(); |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + /** |
|
| 330 | + * Deletes an entire addressbook and all its contents |
|
| 331 | + * |
|
| 332 | + * @param mixed $addressBookId |
|
| 333 | + * @return void |
|
| 334 | + */ |
|
| 335 | + function deleteAddressBook($addressBookId) { |
|
| 336 | + $query = $this->db->getQueryBuilder(); |
|
| 337 | + $query->delete('cards') |
|
| 338 | + ->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid'))) |
|
| 339 | + ->setParameter('addressbookid', $addressBookId) |
|
| 340 | + ->execute(); |
|
| 341 | + |
|
| 342 | + $query->delete('addressbookchanges') |
|
| 343 | + ->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid'))) |
|
| 344 | + ->setParameter('addressbookid', $addressBookId) |
|
| 345 | + ->execute(); |
|
| 346 | + |
|
| 347 | + $query->delete('addressbooks') |
|
| 348 | + ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
| 349 | + ->setParameter('id', $addressBookId) |
|
| 350 | + ->execute(); |
|
| 351 | + |
|
| 352 | + $this->sharingBackend->deleteAllShares($addressBookId); |
|
| 353 | + |
|
| 354 | + $query->delete($this->dbCardsPropertiesTable) |
|
| 355 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 356 | + ->execute(); |
|
| 357 | + |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + /** |
|
| 361 | + * Returns all cards for a specific addressbook id. |
|
| 362 | + * |
|
| 363 | + * This method should return the following properties for each card: |
|
| 364 | + * * carddata - raw vcard data |
|
| 365 | + * * uri - Some unique url |
|
| 366 | + * * lastmodified - A unix timestamp |
|
| 367 | + * |
|
| 368 | + * It's recommended to also return the following properties: |
|
| 369 | + * * etag - A unique etag. This must change every time the card changes. |
|
| 370 | + * * size - The size of the card in bytes. |
|
| 371 | + * |
|
| 372 | + * If these last two properties are provided, less time will be spent |
|
| 373 | + * calculating them. If they are specified, you can also ommit carddata. |
|
| 374 | + * This may speed up certain requests, especially with large cards. |
|
| 375 | + * |
|
| 376 | + * @param mixed $addressBookId |
|
| 377 | + * @return array |
|
| 378 | + */ |
|
| 379 | + function getCards($addressBookId) { |
|
| 380 | + $query = $this->db->getQueryBuilder(); |
|
| 381 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata']) |
|
| 382 | + ->from('cards') |
|
| 383 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 384 | + |
|
| 385 | + $cards = []; |
|
| 386 | + |
|
| 387 | + $result = $query->execute(); |
|
| 388 | + while($row = $result->fetch()) { |
|
| 389 | + $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 390 | + $row['carddata'] = $this->readBlob($row['carddata']); |
|
| 391 | + $cards[] = $row; |
|
| 392 | + } |
|
| 393 | + $result->closeCursor(); |
|
| 394 | + |
|
| 395 | + return $cards; |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + /** |
|
| 399 | + * Returns a specific card. |
|
| 400 | + * |
|
| 401 | + * The same set of properties must be returned as with getCards. The only |
|
| 402 | + * exception is that 'carddata' is absolutely required. |
|
| 403 | + * |
|
| 404 | + * If the card does not exist, you must return false. |
|
| 405 | + * |
|
| 406 | + * @param mixed $addressBookId |
|
| 407 | + * @param string $cardUri |
|
| 408 | + * @return array |
|
| 409 | + */ |
|
| 410 | + function getCard($addressBookId, $cardUri) { |
|
| 411 | + $query = $this->db->getQueryBuilder(); |
|
| 412 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata']) |
|
| 413 | + ->from('cards') |
|
| 414 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 415 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 416 | + ->setMaxResults(1); |
|
| 417 | + |
|
| 418 | + $result = $query->execute(); |
|
| 419 | + $row = $result->fetch(); |
|
| 420 | + if (!$row) { |
|
| 421 | + return false; |
|
| 422 | + } |
|
| 423 | + $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 424 | + $row['carddata'] = $this->readBlob($row['carddata']); |
|
| 425 | + |
|
| 426 | + return $row; |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + /** |
|
| 430 | + * Returns a list of cards. |
|
| 431 | + * |
|
| 432 | + * This method should work identical to getCard, but instead return all the |
|
| 433 | + * cards in the list as an array. |
|
| 434 | + * |
|
| 435 | + * If the backend supports this, it may allow for some speed-ups. |
|
| 436 | + * |
|
| 437 | + * @param mixed $addressBookId |
|
| 438 | + * @param string[] $uris |
|
| 439 | + * @return array |
|
| 440 | + */ |
|
| 441 | + function getMultipleCards($addressBookId, array $uris) { |
|
| 442 | + if (empty($uris)) { |
|
| 443 | + return []; |
|
| 444 | + } |
|
| 445 | + |
|
| 446 | + $chunks = array_chunk($uris, 100); |
|
| 447 | + $cards = []; |
|
| 448 | + |
|
| 449 | + $query = $this->db->getQueryBuilder(); |
|
| 450 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata']) |
|
| 451 | + ->from('cards') |
|
| 452 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 453 | + ->andWhere($query->expr()->in('uri', $query->createParameter('uri'))); |
|
| 454 | + |
|
| 455 | + foreach ($chunks as $uris) { |
|
| 456 | + $query->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY); |
|
| 457 | + $result = $query->execute(); |
|
| 458 | + |
|
| 459 | + while ($row = $result->fetch()) { |
|
| 460 | + $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 461 | + $row['carddata'] = $this->readBlob($row['carddata']); |
|
| 462 | + $cards[] = $row; |
|
| 463 | + } |
|
| 464 | + $result->closeCursor(); |
|
| 465 | + } |
|
| 466 | + return $cards; |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + /** |
|
| 470 | + * Creates a new card. |
|
| 471 | + * |
|
| 472 | + * The addressbook id will be passed as the first argument. This is the |
|
| 473 | + * same id as it is returned from the getAddressBooksForUser method. |
|
| 474 | + * |
|
| 475 | + * The cardUri is a base uri, and doesn't include the full path. The |
|
| 476 | + * cardData argument is the vcard body, and is passed as a string. |
|
| 477 | + * |
|
| 478 | + * It is possible to return an ETag from this method. This ETag is for the |
|
| 479 | + * newly created resource, and must be enclosed with double quotes (that |
|
| 480 | + * is, the string itself must contain the double quotes). |
|
| 481 | + * |
|
| 482 | + * You should only return the ETag if you store the carddata as-is. If a |
|
| 483 | + * subsequent GET request on the same card does not have the same body, |
|
| 484 | + * byte-by-byte and you did return an ETag here, clients tend to get |
|
| 485 | + * confused. |
|
| 486 | + * |
|
| 487 | + * If you don't return an ETag, you can just return null. |
|
| 488 | + * |
|
| 489 | + * @param mixed $addressBookId |
|
| 490 | + * @param string $cardUri |
|
| 491 | + * @param string $cardData |
|
| 492 | + * @return string |
|
| 493 | + */ |
|
| 494 | + function createCard($addressBookId, $cardUri, $cardData) { |
|
| 495 | + $etag = md5($cardData); |
|
| 496 | + |
|
| 497 | + $query = $this->db->getQueryBuilder(); |
|
| 498 | + $query->insert('cards') |
|
| 499 | + ->values([ |
|
| 500 | + 'carddata' => $query->createNamedParameter($cardData, IQueryBuilder::PARAM_LOB), |
|
| 501 | + 'uri' => $query->createNamedParameter($cardUri), |
|
| 502 | + 'lastmodified' => $query->createNamedParameter(time()), |
|
| 503 | + 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
| 504 | + 'size' => $query->createNamedParameter(strlen($cardData)), |
|
| 505 | + 'etag' => $query->createNamedParameter($etag), |
|
| 506 | + ]) |
|
| 507 | + ->execute(); |
|
| 508 | + |
|
| 509 | + $this->addChange($addressBookId, $cardUri, 1); |
|
| 510 | + $this->updateProperties($addressBookId, $cardUri, $cardData); |
|
| 511 | + |
|
| 512 | + if (!is_null($this->dispatcher)) { |
|
| 513 | + $this->dispatcher->dispatch('\OCA\DAV\CardDAV\CardDavBackend::createCard', |
|
| 514 | + new GenericEvent(null, [ |
|
| 515 | + 'addressBookId' => $addressBookId, |
|
| 516 | + 'cardUri' => $cardUri, |
|
| 517 | + 'cardData' => $cardData])); |
|
| 518 | + } |
|
| 519 | + |
|
| 520 | + return '"' . $etag . '"'; |
|
| 521 | + } |
|
| 522 | + |
|
| 523 | + /** |
|
| 524 | + * Updates a card. |
|
| 525 | + * |
|
| 526 | + * The addressbook id will be passed as the first argument. This is the |
|
| 527 | + * same id as it is returned from the getAddressBooksForUser method. |
|
| 528 | + * |
|
| 529 | + * The cardUri is a base uri, and doesn't include the full path. The |
|
| 530 | + * cardData argument is the vcard body, and is passed as a string. |
|
| 531 | + * |
|
| 532 | + * It is possible to return an ETag from this method. This ETag should |
|
| 533 | + * match that of the updated resource, and must be enclosed with double |
|
| 534 | + * quotes (that is: the string itself must contain the actual quotes). |
|
| 535 | + * |
|
| 536 | + * You should only return the ETag if you store the carddata as-is. If a |
|
| 537 | + * subsequent GET request on the same card does not have the same body, |
|
| 538 | + * byte-by-byte and you did return an ETag here, clients tend to get |
|
| 539 | + * confused. |
|
| 540 | + * |
|
| 541 | + * If you don't return an ETag, you can just return null. |
|
| 542 | + * |
|
| 543 | + * @param mixed $addressBookId |
|
| 544 | + * @param string $cardUri |
|
| 545 | + * @param string $cardData |
|
| 546 | + * @return string |
|
| 547 | + */ |
|
| 548 | + function updateCard($addressBookId, $cardUri, $cardData) { |
|
| 549 | + |
|
| 550 | + $etag = md5($cardData); |
|
| 551 | + $query = $this->db->getQueryBuilder(); |
|
| 552 | + $query->update('cards') |
|
| 553 | + ->set('carddata', $query->createNamedParameter($cardData, IQueryBuilder::PARAM_LOB)) |
|
| 554 | + ->set('lastmodified', $query->createNamedParameter(time())) |
|
| 555 | + ->set('size', $query->createNamedParameter(strlen($cardData))) |
|
| 556 | + ->set('etag', $query->createNamedParameter($etag)) |
|
| 557 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 558 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 559 | + ->execute(); |
|
| 560 | + |
|
| 561 | + $this->addChange($addressBookId, $cardUri, 2); |
|
| 562 | + $this->updateProperties($addressBookId, $cardUri, $cardData); |
|
| 563 | + |
|
| 564 | + if (!is_null($this->dispatcher)) { |
|
| 565 | + $this->dispatcher->dispatch('\OCA\DAV\CardDAV\CardDavBackend::updateCard', |
|
| 566 | + new GenericEvent(null, [ |
|
| 567 | + 'addressBookId' => $addressBookId, |
|
| 568 | + 'cardUri' => $cardUri, |
|
| 569 | + 'cardData' => $cardData])); |
|
| 570 | + } |
|
| 571 | + |
|
| 572 | + return '"' . $etag . '"'; |
|
| 573 | + } |
|
| 574 | + |
|
| 575 | + /** |
|
| 576 | + * Deletes a card |
|
| 577 | + * |
|
| 578 | + * @param mixed $addressBookId |
|
| 579 | + * @param string $cardUri |
|
| 580 | + * @return bool |
|
| 581 | + */ |
|
| 582 | + function deleteCard($addressBookId, $cardUri) { |
|
| 583 | + try { |
|
| 584 | + $cardId = $this->getCardId($addressBookId, $cardUri); |
|
| 585 | + } catch (\InvalidArgumentException $e) { |
|
| 586 | + $cardId = null; |
|
| 587 | + } |
|
| 588 | + $query = $this->db->getQueryBuilder(); |
|
| 589 | + $ret = $query->delete('cards') |
|
| 590 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 591 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 592 | + ->execute(); |
|
| 593 | + |
|
| 594 | + $this->addChange($addressBookId, $cardUri, 3); |
|
| 595 | + |
|
| 596 | + if (!is_null($this->dispatcher)) { |
|
| 597 | + $this->dispatcher->dispatch('\OCA\DAV\CardDAV\CardDavBackend::deleteCard', |
|
| 598 | + new GenericEvent(null, [ |
|
| 599 | + 'addressBookId' => $addressBookId, |
|
| 600 | + 'cardUri' => $cardUri])); |
|
| 601 | + } |
|
| 602 | + |
|
| 603 | + if ($ret === 1) { |
|
| 604 | + if ($cardId !== null) { |
|
| 605 | + $this->purgeProperties($addressBookId, $cardId); |
|
| 606 | + } |
|
| 607 | + return true; |
|
| 608 | + } |
|
| 609 | + |
|
| 610 | + return false; |
|
| 611 | + } |
|
| 612 | + |
|
| 613 | + /** |
|
| 614 | + * The getChanges method returns all the changes that have happened, since |
|
| 615 | + * the specified syncToken in the specified address book. |
|
| 616 | + * |
|
| 617 | + * This function should return an array, such as the following: |
|
| 618 | + * |
|
| 619 | + * [ |
|
| 620 | + * 'syncToken' => 'The current synctoken', |
|
| 621 | + * 'added' => [ |
|
| 622 | + * 'new.txt', |
|
| 623 | + * ], |
|
| 624 | + * 'modified' => [ |
|
| 625 | + * 'modified.txt', |
|
| 626 | + * ], |
|
| 627 | + * 'deleted' => [ |
|
| 628 | + * 'foo.php.bak', |
|
| 629 | + * 'old.txt' |
|
| 630 | + * ] |
|
| 631 | + * ]; |
|
| 632 | + * |
|
| 633 | + * The returned syncToken property should reflect the *current* syncToken |
|
| 634 | + * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
|
| 635 | + * property. This is needed here too, to ensure the operation is atomic. |
|
| 636 | + * |
|
| 637 | + * If the $syncToken argument is specified as null, this is an initial |
|
| 638 | + * sync, and all members should be reported. |
|
| 639 | + * |
|
| 640 | + * The modified property is an array of nodenames that have changed since |
|
| 641 | + * the last token. |
|
| 642 | + * |
|
| 643 | + * The deleted property is an array with nodenames, that have been deleted |
|
| 644 | + * from collection. |
|
| 645 | + * |
|
| 646 | + * The $syncLevel argument is basically the 'depth' of the report. If it's |
|
| 647 | + * 1, you only have to report changes that happened only directly in |
|
| 648 | + * immediate descendants. If it's 2, it should also include changes from |
|
| 649 | + * the nodes below the child collections. (grandchildren) |
|
| 650 | + * |
|
| 651 | + * The $limit argument allows a client to specify how many results should |
|
| 652 | + * be returned at most. If the limit is not specified, it should be treated |
|
| 653 | + * as infinite. |
|
| 654 | + * |
|
| 655 | + * If the limit (infinite or not) is higher than you're willing to return, |
|
| 656 | + * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
|
| 657 | + * |
|
| 658 | + * If the syncToken is expired (due to data cleanup) or unknown, you must |
|
| 659 | + * return null. |
|
| 660 | + * |
|
| 661 | + * The limit is 'suggestive'. You are free to ignore it. |
|
| 662 | + * |
|
| 663 | + * @param string $addressBookId |
|
| 664 | + * @param string $syncToken |
|
| 665 | + * @param int $syncLevel |
|
| 666 | + * @param int $limit |
|
| 667 | + * @return array |
|
| 668 | + */ |
|
| 669 | + function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel, $limit = null) { |
|
| 670 | + // Current synctoken |
|
| 671 | + $stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*addressbooks` WHERE `id` = ?'); |
|
| 672 | + $stmt->execute([ $addressBookId ]); |
|
| 673 | + $currentToken = $stmt->fetchColumn(0); |
|
| 674 | + |
|
| 675 | + if (is_null($currentToken)) return null; |
|
| 676 | + |
|
| 677 | + $result = [ |
|
| 678 | + 'syncToken' => $currentToken, |
|
| 679 | + 'added' => [], |
|
| 680 | + 'modified' => [], |
|
| 681 | + 'deleted' => [], |
|
| 682 | + ]; |
|
| 683 | + |
|
| 684 | + if ($syncToken) { |
|
| 685 | + |
|
| 686 | + $query = "SELECT `uri`, `operation` FROM `*PREFIX*addressbookchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `addressbookid` = ? ORDER BY `synctoken`"; |
|
| 687 | + if ($limit>0) { |
|
| 688 | + $query .= " `LIMIT` " . (int)$limit; |
|
| 689 | + } |
|
| 690 | + |
|
| 691 | + // Fetching all changes |
|
| 692 | + $stmt = $this->db->prepare($query); |
|
| 693 | + $stmt->execute([$syncToken, $currentToken, $addressBookId]); |
|
| 694 | + |
|
| 695 | + $changes = []; |
|
| 696 | + |
|
| 697 | + // This loop ensures that any duplicates are overwritten, only the |
|
| 698 | + // last change on a node is relevant. |
|
| 699 | + while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 700 | + |
|
| 701 | + $changes[$row['uri']] = $row['operation']; |
|
| 702 | + |
|
| 703 | + } |
|
| 704 | + |
|
| 705 | + foreach($changes as $uri => $operation) { |
|
| 706 | + |
|
| 707 | + switch($operation) { |
|
| 708 | + case 1: |
|
| 709 | + $result['added'][] = $uri; |
|
| 710 | + break; |
|
| 711 | + case 2: |
|
| 712 | + $result['modified'][] = $uri; |
|
| 713 | + break; |
|
| 714 | + case 3: |
|
| 715 | + $result['deleted'][] = $uri; |
|
| 716 | + break; |
|
| 717 | + } |
|
| 718 | + |
|
| 719 | + } |
|
| 720 | + } else { |
|
| 721 | + // No synctoken supplied, this is the initial sync. |
|
| 722 | + $query = "SELECT `uri` FROM `*PREFIX*cards` WHERE `addressbookid` = ?"; |
|
| 723 | + $stmt = $this->db->prepare($query); |
|
| 724 | + $stmt->execute([$addressBookId]); |
|
| 725 | + |
|
| 726 | + $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
| 727 | + } |
|
| 728 | + return $result; |
|
| 729 | + } |
|
| 730 | + |
|
| 731 | + /** |
|
| 732 | + * Adds a change record to the addressbookchanges table. |
|
| 733 | + * |
|
| 734 | + * @param mixed $addressBookId |
|
| 735 | + * @param string $objectUri |
|
| 736 | + * @param int $operation 1 = add, 2 = modify, 3 = delete |
|
| 737 | + * @return void |
|
| 738 | + */ |
|
| 739 | + protected function addChange($addressBookId, $objectUri, $operation) { |
|
| 740 | + $sql = 'INSERT INTO `*PREFIX*addressbookchanges`(`uri`, `synctoken`, `addressbookid`, `operation`) SELECT ?, `synctoken`, ?, ? FROM `*PREFIX*addressbooks` WHERE `id` = ?'; |
|
| 741 | + $stmt = $this->db->prepare($sql); |
|
| 742 | + $stmt->execute([ |
|
| 743 | + $objectUri, |
|
| 744 | + $addressBookId, |
|
| 745 | + $operation, |
|
| 746 | + $addressBookId |
|
| 747 | + ]); |
|
| 748 | + $stmt = $this->db->prepare('UPDATE `*PREFIX*addressbooks` SET `synctoken` = `synctoken` + 1 WHERE `id` = ?'); |
|
| 749 | + $stmt->execute([ |
|
| 750 | + $addressBookId |
|
| 751 | + ]); |
|
| 752 | + } |
|
| 753 | + |
|
| 754 | + private function readBlob($cardData) { |
|
| 755 | + if (is_resource($cardData)) { |
|
| 756 | + return stream_get_contents($cardData); |
|
| 757 | + } |
|
| 758 | + |
|
| 759 | + return $cardData; |
|
| 760 | + } |
|
| 761 | + |
|
| 762 | + /** |
|
| 763 | + * @param IShareable $shareable |
|
| 764 | + * @param string[] $add |
|
| 765 | + * @param string[] $remove |
|
| 766 | + */ |
|
| 767 | + public function updateShares(IShareable $shareable, $add, $remove) { |
|
| 768 | + $this->sharingBackend->updateShares($shareable, $add, $remove); |
|
| 769 | + } |
|
| 770 | + |
|
| 771 | + /** |
|
| 772 | + * search contact |
|
| 773 | + * |
|
| 774 | + * @param int $addressBookId |
|
| 775 | + * @param string $pattern which should match within the $searchProperties |
|
| 776 | + * @param array $searchProperties defines the properties within the query pattern should match |
|
| 777 | + * @return array an array of contacts which are arrays of key-value-pairs |
|
| 778 | + */ |
|
| 779 | + public function search($addressBookId, $pattern, $searchProperties) { |
|
| 780 | + $query = $this->db->getQueryBuilder(); |
|
| 781 | + $query2 = $this->db->getQueryBuilder(); |
|
| 782 | + $query2->selectDistinct('cp.cardid')->from($this->dbCardsPropertiesTable, 'cp'); |
|
| 783 | + foreach ($searchProperties as $property) { |
|
| 784 | + $query2->orWhere( |
|
| 785 | + $query2->expr()->andX( |
|
| 786 | + $query2->expr()->eq('cp.name', $query->createNamedParameter($property)), |
|
| 787 | + $query2->expr()->ilike('cp.value', $query->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%')) |
|
| 788 | + ) |
|
| 789 | + ); |
|
| 790 | + } |
|
| 791 | + $query2->andWhere($query2->expr()->eq('cp.addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 792 | + |
|
| 793 | + $query->select('c.carddata', 'c.uri')->from($this->dbCardsTable, 'c') |
|
| 794 | + ->where($query->expr()->in('c.id', $query->createFunction($query2->getSQL()))); |
|
| 795 | + |
|
| 796 | + $result = $query->execute(); |
|
| 797 | + $cards = $result->fetchAll(); |
|
| 798 | + |
|
| 799 | + $result->closeCursor(); |
|
| 800 | + |
|
| 801 | + return array_map(function($array) { |
|
| 802 | + $array['carddata'] = $this->readBlob($array['carddata']); |
|
| 803 | + return $array; |
|
| 804 | + }, $cards); |
|
| 805 | + } |
|
| 806 | + |
|
| 807 | + /** |
|
| 808 | + * @param int $bookId |
|
| 809 | + * @param string $name |
|
| 810 | + * @return array |
|
| 811 | + */ |
|
| 812 | + public function collectCardProperties($bookId, $name) { |
|
| 813 | + $query = $this->db->getQueryBuilder(); |
|
| 814 | + $result = $query->selectDistinct('value') |
|
| 815 | + ->from($this->dbCardsPropertiesTable) |
|
| 816 | + ->where($query->expr()->eq('name', $query->createNamedParameter($name))) |
|
| 817 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($bookId))) |
|
| 818 | + ->execute(); |
|
| 819 | + |
|
| 820 | + $all = $result->fetchAll(PDO::FETCH_COLUMN); |
|
| 821 | + $result->closeCursor(); |
|
| 822 | + |
|
| 823 | + return $all; |
|
| 824 | + } |
|
| 825 | + |
|
| 826 | + /** |
|
| 827 | + * get URI from a given contact |
|
| 828 | + * |
|
| 829 | + * @param int $id |
|
| 830 | + * @return string |
|
| 831 | + */ |
|
| 832 | + public function getCardUri($id) { |
|
| 833 | + $query = $this->db->getQueryBuilder(); |
|
| 834 | + $query->select('uri')->from($this->dbCardsTable) |
|
| 835 | + ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
| 836 | + ->setParameter('id', $id); |
|
| 837 | + |
|
| 838 | + $result = $query->execute(); |
|
| 839 | + $uri = $result->fetch(); |
|
| 840 | + $result->closeCursor(); |
|
| 841 | + |
|
| 842 | + if (!isset($uri['uri'])) { |
|
| 843 | + throw new \InvalidArgumentException('Card does not exists: ' . $id); |
|
| 844 | + } |
|
| 845 | + |
|
| 846 | + return $uri['uri']; |
|
| 847 | + } |
|
| 848 | + |
|
| 849 | + /** |
|
| 850 | + * return contact with the given URI |
|
| 851 | + * |
|
| 852 | + * @param int $addressBookId |
|
| 853 | + * @param string $uri |
|
| 854 | + * @returns array |
|
| 855 | + */ |
|
| 856 | + public function getContact($addressBookId, $uri) { |
|
| 857 | + $result = []; |
|
| 858 | + $query = $this->db->getQueryBuilder(); |
|
| 859 | + $query->select('*')->from($this->dbCardsTable) |
|
| 860 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 861 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 862 | + $queryResult = $query->execute(); |
|
| 863 | + $contact = $queryResult->fetch(); |
|
| 864 | + $queryResult->closeCursor(); |
|
| 865 | + |
|
| 866 | + if (is_array($contact)) { |
|
| 867 | + $result = $contact; |
|
| 868 | + } |
|
| 869 | + |
|
| 870 | + return $result; |
|
| 871 | + } |
|
| 872 | + |
|
| 873 | + /** |
|
| 874 | + * Returns the list of people whom this address book is shared with. |
|
| 875 | + * |
|
| 876 | + * Every element in this array should have the following properties: |
|
| 877 | + * * href - Often a mailto: address |
|
| 878 | + * * commonName - Optional, for example a first + last name |
|
| 879 | + * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
|
| 880 | + * * readOnly - boolean |
|
| 881 | + * * summary - Optional, a description for the share |
|
| 882 | + * |
|
| 883 | + * @return array |
|
| 884 | + */ |
|
| 885 | + public function getShares($addressBookId) { |
|
| 886 | + return $this->sharingBackend->getShares($addressBookId); |
|
| 887 | + } |
|
| 888 | + |
|
| 889 | + /** |
|
| 890 | + * update properties table |
|
| 891 | + * |
|
| 892 | + * @param int $addressBookId |
|
| 893 | + * @param string $cardUri |
|
| 894 | + * @param string $vCardSerialized |
|
| 895 | + */ |
|
| 896 | + protected function updateProperties($addressBookId, $cardUri, $vCardSerialized) { |
|
| 897 | + $cardId = $this->getCardId($addressBookId, $cardUri); |
|
| 898 | + $vCard = $this->readCard($vCardSerialized); |
|
| 899 | + |
|
| 900 | + $this->purgeProperties($addressBookId, $cardId); |
|
| 901 | + |
|
| 902 | + $query = $this->db->getQueryBuilder(); |
|
| 903 | + $query->insert($this->dbCardsPropertiesTable) |
|
| 904 | + ->values( |
|
| 905 | + [ |
|
| 906 | + 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
| 907 | + 'cardid' => $query->createNamedParameter($cardId), |
|
| 908 | + 'name' => $query->createParameter('name'), |
|
| 909 | + 'value' => $query->createParameter('value'), |
|
| 910 | + 'preferred' => $query->createParameter('preferred') |
|
| 911 | + ] |
|
| 912 | + ); |
|
| 913 | + |
|
| 914 | + foreach ($vCard->children as $property) { |
|
| 915 | + if(!in_array($property->name, self::$indexProperties)) { |
|
| 916 | + continue; |
|
| 917 | + } |
|
| 918 | + $preferred = 0; |
|
| 919 | + foreach($property->parameters as $parameter) { |
|
| 920 | + if ($parameter->name == 'TYPE' && strtoupper($parameter->getValue()) == 'PREF') { |
|
| 921 | + $preferred = 1; |
|
| 922 | + break; |
|
| 923 | + } |
|
| 924 | + } |
|
| 925 | + $query->setParameter('name', $property->name); |
|
| 926 | + $query->setParameter('value', substr($property->getValue(), 0, 254)); |
|
| 927 | + $query->setParameter('preferred', $preferred); |
|
| 928 | + $query->execute(); |
|
| 929 | + } |
|
| 930 | + } |
|
| 931 | + |
|
| 932 | + /** |
|
| 933 | + * read vCard data into a vCard object |
|
| 934 | + * |
|
| 935 | + * @param string $cardData |
|
| 936 | + * @return VCard |
|
| 937 | + */ |
|
| 938 | + protected function readCard($cardData) { |
|
| 939 | + return Reader::read($cardData); |
|
| 940 | + } |
|
| 941 | + |
|
| 942 | + /** |
|
| 943 | + * delete all properties from a given card |
|
| 944 | + * |
|
| 945 | + * @param int $addressBookId |
|
| 946 | + * @param int $cardId |
|
| 947 | + */ |
|
| 948 | + protected function purgeProperties($addressBookId, $cardId) { |
|
| 949 | + $query = $this->db->getQueryBuilder(); |
|
| 950 | + $query->delete($this->dbCardsPropertiesTable) |
|
| 951 | + ->where($query->expr()->eq('cardid', $query->createNamedParameter($cardId))) |
|
| 952 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 953 | + $query->execute(); |
|
| 954 | + } |
|
| 955 | + |
|
| 956 | + /** |
|
| 957 | + * get ID from a given contact |
|
| 958 | + * |
|
| 959 | + * @param int $addressBookId |
|
| 960 | + * @param string $uri |
|
| 961 | + * @return int |
|
| 962 | + */ |
|
| 963 | + protected function getCardId($addressBookId, $uri) { |
|
| 964 | + $query = $this->db->getQueryBuilder(); |
|
| 965 | + $query->select('id')->from($this->dbCardsTable) |
|
| 966 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 967 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 968 | + |
|
| 969 | + $result = $query->execute(); |
|
| 970 | + $cardIds = $result->fetch(); |
|
| 971 | + $result->closeCursor(); |
|
| 972 | + |
|
| 973 | + if (!isset($cardIds['id'])) { |
|
| 974 | + throw new \InvalidArgumentException('Card does not exists: ' . $uri); |
|
| 975 | + } |
|
| 976 | + |
|
| 977 | + return (int)$cardIds['id']; |
|
| 978 | + } |
|
| 979 | + |
|
| 980 | + /** |
|
| 981 | + * For shared address books the sharee is set in the ACL of the address book |
|
| 982 | + * @param $addressBookId |
|
| 983 | + * @param $acl |
|
| 984 | + * @return array |
|
| 985 | + */ |
|
| 986 | + public function applyShareAcl($addressBookId, $acl) { |
|
| 987 | + return $this->sharingBackend->applyShareAcl($addressBookId, $acl); |
|
| 988 | + } |
|
| 989 | + |
|
| 990 | + private function convertPrincipal($principalUri, $toV2) { |
|
| 991 | + if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
|
| 992 | + list(, $name) = URLUtil::splitPath($principalUri); |
|
| 993 | + if ($toV2 === true) { |
|
| 994 | + return "principals/users/$name"; |
|
| 995 | + } |
|
| 996 | + return "principals/$name"; |
|
| 997 | + } |
|
| 998 | + return $principalUri; |
|
| 999 | + } |
|
| 1000 | 1000 | } |
@@ -246,7 +246,7 @@ |
||
| 246 | 246 | } |
| 247 | 247 | |
| 248 | 248 | /** |
| 249 | - * @return array|null |
|
| 249 | + * @return string |
|
| 250 | 250 | */ |
| 251 | 251 | public function getLocalSystemAddressBook() { |
| 252 | 252 | if (is_null($this->localSystemAddressBook)) { |
@@ -36,250 +36,250 @@ |
||
| 36 | 36 | |
| 37 | 37 | class SyncService { |
| 38 | 38 | |
| 39 | - /** @var CardDavBackend */ |
|
| 40 | - private $backend; |
|
| 41 | - |
|
| 42 | - /** @var IUserManager */ |
|
| 43 | - private $userManager; |
|
| 44 | - |
|
| 45 | - /** @var ILogger */ |
|
| 46 | - private $logger; |
|
| 47 | - |
|
| 48 | - /** @var array */ |
|
| 49 | - private $localSystemAddressBook; |
|
| 50 | - |
|
| 51 | - public function __construct(CardDavBackend $backend, IUserManager $userManager, ILogger $logger) { |
|
| 52 | - $this->backend = $backend; |
|
| 53 | - $this->userManager = $userManager; |
|
| 54 | - $this->logger = $logger; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @param string $url |
|
| 59 | - * @param string $userName |
|
| 60 | - * @param string $sharedSecret |
|
| 61 | - * @param string $syncToken |
|
| 62 | - * @param int $targetBookId |
|
| 63 | - * @param string $targetPrincipal |
|
| 64 | - * @param array $targetProperties |
|
| 65 | - * @return string |
|
| 66 | - * @throws \Exception |
|
| 67 | - */ |
|
| 68 | - public function syncRemoteAddressBook($url, $userName, $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetProperties) { |
|
| 69 | - // 1. create addressbook |
|
| 70 | - $book = $this->ensureSystemAddressBookExists($targetPrincipal, $targetBookId, $targetProperties); |
|
| 71 | - $addressBookId = $book['id']; |
|
| 72 | - |
|
| 73 | - // 2. query changes |
|
| 74 | - try { |
|
| 75 | - $response = $this->requestSyncReport($url, $userName, $sharedSecret, $syncToken); |
|
| 76 | - } catch (ClientHttpException $ex) { |
|
| 77 | - if ($ex->getCode() === Http::STATUS_UNAUTHORIZED) { |
|
| 78 | - // remote server revoked access to the address book, remove it |
|
| 79 | - $this->backend->deleteAddressBook($addressBookId); |
|
| 80 | - $this->logger->info('Authorization failed, remove address book: ' . $url, ['app' => 'dav']); |
|
| 81 | - throw $ex; |
|
| 82 | - } |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - // 3. apply changes |
|
| 86 | - // TODO: use multi-get for download |
|
| 87 | - foreach ($response['response'] as $resource => $status) { |
|
| 88 | - $cardUri = basename($resource); |
|
| 89 | - if (isset($status[200])) { |
|
| 90 | - $vCard = $this->download($url, $sharedSecret, $resource); |
|
| 91 | - $existingCard = $this->backend->getCard($addressBookId, $cardUri); |
|
| 92 | - if ($existingCard === false) { |
|
| 93 | - $this->backend->createCard($addressBookId, $cardUri, $vCard['body']); |
|
| 94 | - } else { |
|
| 95 | - $this->backend->updateCard($addressBookId, $cardUri, $vCard['body']); |
|
| 96 | - } |
|
| 97 | - } else { |
|
| 98 | - $this->backend->deleteCard($addressBookId, $cardUri); |
|
| 99 | - } |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - return $response['token']; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * @param string $principal |
|
| 107 | - * @param string $id |
|
| 108 | - * @param array $properties |
|
| 109 | - * @return array|null |
|
| 110 | - * @throws \Sabre\DAV\Exception\BadRequest |
|
| 111 | - */ |
|
| 112 | - public function ensureSystemAddressBookExists($principal, $id, $properties) { |
|
| 113 | - $book = $this->backend->getAddressBooksByUri($principal, $id); |
|
| 114 | - if (!is_null($book)) { |
|
| 115 | - return $book; |
|
| 116 | - } |
|
| 117 | - $this->backend->createAddressBook($principal, $id, $properties); |
|
| 118 | - |
|
| 119 | - return $this->backend->getAddressBooksByUri($principal, $id); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * @param string $url |
|
| 124 | - * @param string $userName |
|
| 125 | - * @param string $sharedSecret |
|
| 126 | - * @param string $syncToken |
|
| 127 | - * @return array |
|
| 128 | - */ |
|
| 129 | - protected function requestSyncReport($url, $userName, $sharedSecret, $syncToken) { |
|
| 130 | - $settings = [ |
|
| 131 | - 'baseUri' => $url . '/', |
|
| 132 | - 'userName' => $userName, |
|
| 133 | - 'password' => $sharedSecret, |
|
| 134 | - ]; |
|
| 135 | - $client = new Client($settings); |
|
| 136 | - $client->setThrowExceptions(true); |
|
| 137 | - |
|
| 138 | - $addressBookUrl = "remote.php/dav/addressbooks/system/system/system"; |
|
| 139 | - $body = $this->buildSyncCollectionRequestBody($syncToken); |
|
| 140 | - |
|
| 141 | - $response = $client->request('REPORT', $addressBookUrl, $body, [ |
|
| 142 | - 'Content-Type' => 'application/xml' |
|
| 143 | - ]); |
|
| 144 | - |
|
| 145 | - $result = $this->parseMultiStatus($response['body']); |
|
| 146 | - |
|
| 147 | - return $result; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * @param string $url |
|
| 152 | - * @param string $sharedSecret |
|
| 153 | - * @param string $resourcePath |
|
| 154 | - * @return array |
|
| 155 | - */ |
|
| 156 | - protected function download($url, $sharedSecret, $resourcePath) { |
|
| 157 | - $settings = [ |
|
| 158 | - 'baseUri' => $url, |
|
| 159 | - 'userName' => 'system', |
|
| 160 | - 'password' => $sharedSecret, |
|
| 161 | - ]; |
|
| 162 | - $client = new Client($settings); |
|
| 163 | - $client->setThrowExceptions(true); |
|
| 164 | - |
|
| 165 | - $response = $client->request('GET', $resourcePath); |
|
| 166 | - return $response; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * @param string|null $syncToken |
|
| 171 | - * @return string |
|
| 172 | - */ |
|
| 173 | - private function buildSyncCollectionRequestBody($syncToken) { |
|
| 174 | - |
|
| 175 | - $dom = new \DOMDocument('1.0', 'UTF-8'); |
|
| 176 | - $dom->formatOutput = true; |
|
| 177 | - $root = $dom->createElementNS('DAV:', 'd:sync-collection'); |
|
| 178 | - $sync = $dom->createElement('d:sync-token', $syncToken); |
|
| 179 | - $prop = $dom->createElement('d:prop'); |
|
| 180 | - $cont = $dom->createElement('d:getcontenttype'); |
|
| 181 | - $etag = $dom->createElement('d:getetag'); |
|
| 182 | - |
|
| 183 | - $prop->appendChild($cont); |
|
| 184 | - $prop->appendChild($etag); |
|
| 185 | - $root->appendChild($sync); |
|
| 186 | - $root->appendChild($prop); |
|
| 187 | - $dom->appendChild($root); |
|
| 188 | - $body = $dom->saveXML(); |
|
| 189 | - |
|
| 190 | - return $body; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * @param string $body |
|
| 195 | - * @return array |
|
| 196 | - * @throws \Sabre\Xml\ParseException |
|
| 197 | - */ |
|
| 198 | - private function parseMultiStatus($body) { |
|
| 199 | - $xml = new Service(); |
|
| 200 | - |
|
| 201 | - /** @var MultiStatus $multiStatus */ |
|
| 202 | - $multiStatus = $xml->expect('{DAV:}multistatus', $body); |
|
| 203 | - |
|
| 204 | - $result = []; |
|
| 205 | - foreach ($multiStatus->getResponses() as $response) { |
|
| 206 | - $result[$response->getHref()] = $response->getResponseProperties(); |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - return ['response' => $result, 'token' => $multiStatus->getSyncToken()]; |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * @param IUser $user |
|
| 214 | - */ |
|
| 215 | - public function updateUser($user) { |
|
| 216 | - $systemAddressBook = $this->getLocalSystemAddressBook(); |
|
| 217 | - $addressBookId = $systemAddressBook['id']; |
|
| 218 | - $converter = new Converter(); |
|
| 219 | - $name = $user->getBackendClassName(); |
|
| 220 | - $userId = $user->getUID(); |
|
| 221 | - |
|
| 222 | - $cardId = "$name:$userId.vcf"; |
|
| 223 | - $card = $this->backend->getCard($addressBookId, $cardId); |
|
| 224 | - if ($card === false) { |
|
| 225 | - $vCard = $converter->createCardFromUser($user); |
|
| 226 | - $this->backend->createCard($addressBookId, $cardId, $vCard->serialize()); |
|
| 227 | - } else { |
|
| 228 | - $vCard = Reader::read($card['carddata']); |
|
| 229 | - if ($converter->updateCard($vCard, $user)) { |
|
| 230 | - $this->backend->updateCard($addressBookId, $cardId, $vCard->serialize()); |
|
| 231 | - } |
|
| 232 | - } |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * @param IUser|string $userOrCardId |
|
| 237 | - */ |
|
| 238 | - public function deleteUser($userOrCardId) { |
|
| 239 | - $systemAddressBook = $this->getLocalSystemAddressBook(); |
|
| 240 | - if ($userOrCardId instanceof IUser){ |
|
| 241 | - $name = $userOrCardId->getBackendClassName(); |
|
| 242 | - $userId = $userOrCardId->getUID(); |
|
| 243 | - |
|
| 244 | - $userOrCardId = "$name:$userId.vcf"; |
|
| 245 | - } |
|
| 246 | - $this->backend->deleteCard($systemAddressBook['id'], $userOrCardId); |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - /** |
|
| 250 | - * @return array|null |
|
| 251 | - */ |
|
| 252 | - public function getLocalSystemAddressBook() { |
|
| 253 | - if (is_null($this->localSystemAddressBook)) { |
|
| 254 | - $systemPrincipal = "principals/system/system"; |
|
| 255 | - $this->localSystemAddressBook = $this->ensureSystemAddressBookExists($systemPrincipal, 'system', [ |
|
| 256 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => 'System addressbook which holds all users of this instance' |
|
| 257 | - ]); |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - return $this->localSystemAddressBook; |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - public function syncInstance(\Closure $progressCallback = null) { |
|
| 264 | - $systemAddressBook = $this->getLocalSystemAddressBook(); |
|
| 265 | - $this->userManager->callForAllUsers(function($user) use ($systemAddressBook, $progressCallback) { |
|
| 266 | - $this->updateUser($user); |
|
| 267 | - if (!is_null($progressCallback)) { |
|
| 268 | - $progressCallback(); |
|
| 269 | - } |
|
| 270 | - }); |
|
| 271 | - |
|
| 272 | - // remove no longer existing |
|
| 273 | - $allCards = $this->backend->getCards($systemAddressBook['id']); |
|
| 274 | - foreach($allCards as $card) { |
|
| 275 | - $vCard = Reader::read($card['carddata']); |
|
| 276 | - $uid = $vCard->UID->getValue(); |
|
| 277 | - // load backend and see if user exists |
|
| 278 | - if (!$this->userManager->userExists($uid)) { |
|
| 279 | - $this->deleteUser($card['uri']); |
|
| 280 | - } |
|
| 281 | - } |
|
| 282 | - } |
|
| 39 | + /** @var CardDavBackend */ |
|
| 40 | + private $backend; |
|
| 41 | + |
|
| 42 | + /** @var IUserManager */ |
|
| 43 | + private $userManager; |
|
| 44 | + |
|
| 45 | + /** @var ILogger */ |
|
| 46 | + private $logger; |
|
| 47 | + |
|
| 48 | + /** @var array */ |
|
| 49 | + private $localSystemAddressBook; |
|
| 50 | + |
|
| 51 | + public function __construct(CardDavBackend $backend, IUserManager $userManager, ILogger $logger) { |
|
| 52 | + $this->backend = $backend; |
|
| 53 | + $this->userManager = $userManager; |
|
| 54 | + $this->logger = $logger; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @param string $url |
|
| 59 | + * @param string $userName |
|
| 60 | + * @param string $sharedSecret |
|
| 61 | + * @param string $syncToken |
|
| 62 | + * @param int $targetBookId |
|
| 63 | + * @param string $targetPrincipal |
|
| 64 | + * @param array $targetProperties |
|
| 65 | + * @return string |
|
| 66 | + * @throws \Exception |
|
| 67 | + */ |
|
| 68 | + public function syncRemoteAddressBook($url, $userName, $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetProperties) { |
|
| 69 | + // 1. create addressbook |
|
| 70 | + $book = $this->ensureSystemAddressBookExists($targetPrincipal, $targetBookId, $targetProperties); |
|
| 71 | + $addressBookId = $book['id']; |
|
| 72 | + |
|
| 73 | + // 2. query changes |
|
| 74 | + try { |
|
| 75 | + $response = $this->requestSyncReport($url, $userName, $sharedSecret, $syncToken); |
|
| 76 | + } catch (ClientHttpException $ex) { |
|
| 77 | + if ($ex->getCode() === Http::STATUS_UNAUTHORIZED) { |
|
| 78 | + // remote server revoked access to the address book, remove it |
|
| 79 | + $this->backend->deleteAddressBook($addressBookId); |
|
| 80 | + $this->logger->info('Authorization failed, remove address book: ' . $url, ['app' => 'dav']); |
|
| 81 | + throw $ex; |
|
| 82 | + } |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + // 3. apply changes |
|
| 86 | + // TODO: use multi-get for download |
|
| 87 | + foreach ($response['response'] as $resource => $status) { |
|
| 88 | + $cardUri = basename($resource); |
|
| 89 | + if (isset($status[200])) { |
|
| 90 | + $vCard = $this->download($url, $sharedSecret, $resource); |
|
| 91 | + $existingCard = $this->backend->getCard($addressBookId, $cardUri); |
|
| 92 | + if ($existingCard === false) { |
|
| 93 | + $this->backend->createCard($addressBookId, $cardUri, $vCard['body']); |
|
| 94 | + } else { |
|
| 95 | + $this->backend->updateCard($addressBookId, $cardUri, $vCard['body']); |
|
| 96 | + } |
|
| 97 | + } else { |
|
| 98 | + $this->backend->deleteCard($addressBookId, $cardUri); |
|
| 99 | + } |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + return $response['token']; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * @param string $principal |
|
| 107 | + * @param string $id |
|
| 108 | + * @param array $properties |
|
| 109 | + * @return array|null |
|
| 110 | + * @throws \Sabre\DAV\Exception\BadRequest |
|
| 111 | + */ |
|
| 112 | + public function ensureSystemAddressBookExists($principal, $id, $properties) { |
|
| 113 | + $book = $this->backend->getAddressBooksByUri($principal, $id); |
|
| 114 | + if (!is_null($book)) { |
|
| 115 | + return $book; |
|
| 116 | + } |
|
| 117 | + $this->backend->createAddressBook($principal, $id, $properties); |
|
| 118 | + |
|
| 119 | + return $this->backend->getAddressBooksByUri($principal, $id); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * @param string $url |
|
| 124 | + * @param string $userName |
|
| 125 | + * @param string $sharedSecret |
|
| 126 | + * @param string $syncToken |
|
| 127 | + * @return array |
|
| 128 | + */ |
|
| 129 | + protected function requestSyncReport($url, $userName, $sharedSecret, $syncToken) { |
|
| 130 | + $settings = [ |
|
| 131 | + 'baseUri' => $url . '/', |
|
| 132 | + 'userName' => $userName, |
|
| 133 | + 'password' => $sharedSecret, |
|
| 134 | + ]; |
|
| 135 | + $client = new Client($settings); |
|
| 136 | + $client->setThrowExceptions(true); |
|
| 137 | + |
|
| 138 | + $addressBookUrl = "remote.php/dav/addressbooks/system/system/system"; |
|
| 139 | + $body = $this->buildSyncCollectionRequestBody($syncToken); |
|
| 140 | + |
|
| 141 | + $response = $client->request('REPORT', $addressBookUrl, $body, [ |
|
| 142 | + 'Content-Type' => 'application/xml' |
|
| 143 | + ]); |
|
| 144 | + |
|
| 145 | + $result = $this->parseMultiStatus($response['body']); |
|
| 146 | + |
|
| 147 | + return $result; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * @param string $url |
|
| 152 | + * @param string $sharedSecret |
|
| 153 | + * @param string $resourcePath |
|
| 154 | + * @return array |
|
| 155 | + */ |
|
| 156 | + protected function download($url, $sharedSecret, $resourcePath) { |
|
| 157 | + $settings = [ |
|
| 158 | + 'baseUri' => $url, |
|
| 159 | + 'userName' => 'system', |
|
| 160 | + 'password' => $sharedSecret, |
|
| 161 | + ]; |
|
| 162 | + $client = new Client($settings); |
|
| 163 | + $client->setThrowExceptions(true); |
|
| 164 | + |
|
| 165 | + $response = $client->request('GET', $resourcePath); |
|
| 166 | + return $response; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * @param string|null $syncToken |
|
| 171 | + * @return string |
|
| 172 | + */ |
|
| 173 | + private function buildSyncCollectionRequestBody($syncToken) { |
|
| 174 | + |
|
| 175 | + $dom = new \DOMDocument('1.0', 'UTF-8'); |
|
| 176 | + $dom->formatOutput = true; |
|
| 177 | + $root = $dom->createElementNS('DAV:', 'd:sync-collection'); |
|
| 178 | + $sync = $dom->createElement('d:sync-token', $syncToken); |
|
| 179 | + $prop = $dom->createElement('d:prop'); |
|
| 180 | + $cont = $dom->createElement('d:getcontenttype'); |
|
| 181 | + $etag = $dom->createElement('d:getetag'); |
|
| 182 | + |
|
| 183 | + $prop->appendChild($cont); |
|
| 184 | + $prop->appendChild($etag); |
|
| 185 | + $root->appendChild($sync); |
|
| 186 | + $root->appendChild($prop); |
|
| 187 | + $dom->appendChild($root); |
|
| 188 | + $body = $dom->saveXML(); |
|
| 189 | + |
|
| 190 | + return $body; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * @param string $body |
|
| 195 | + * @return array |
|
| 196 | + * @throws \Sabre\Xml\ParseException |
|
| 197 | + */ |
|
| 198 | + private function parseMultiStatus($body) { |
|
| 199 | + $xml = new Service(); |
|
| 200 | + |
|
| 201 | + /** @var MultiStatus $multiStatus */ |
|
| 202 | + $multiStatus = $xml->expect('{DAV:}multistatus', $body); |
|
| 203 | + |
|
| 204 | + $result = []; |
|
| 205 | + foreach ($multiStatus->getResponses() as $response) { |
|
| 206 | + $result[$response->getHref()] = $response->getResponseProperties(); |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + return ['response' => $result, 'token' => $multiStatus->getSyncToken()]; |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * @param IUser $user |
|
| 214 | + */ |
|
| 215 | + public function updateUser($user) { |
|
| 216 | + $systemAddressBook = $this->getLocalSystemAddressBook(); |
|
| 217 | + $addressBookId = $systemAddressBook['id']; |
|
| 218 | + $converter = new Converter(); |
|
| 219 | + $name = $user->getBackendClassName(); |
|
| 220 | + $userId = $user->getUID(); |
|
| 221 | + |
|
| 222 | + $cardId = "$name:$userId.vcf"; |
|
| 223 | + $card = $this->backend->getCard($addressBookId, $cardId); |
|
| 224 | + if ($card === false) { |
|
| 225 | + $vCard = $converter->createCardFromUser($user); |
|
| 226 | + $this->backend->createCard($addressBookId, $cardId, $vCard->serialize()); |
|
| 227 | + } else { |
|
| 228 | + $vCard = Reader::read($card['carddata']); |
|
| 229 | + if ($converter->updateCard($vCard, $user)) { |
|
| 230 | + $this->backend->updateCard($addressBookId, $cardId, $vCard->serialize()); |
|
| 231 | + } |
|
| 232 | + } |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * @param IUser|string $userOrCardId |
|
| 237 | + */ |
|
| 238 | + public function deleteUser($userOrCardId) { |
|
| 239 | + $systemAddressBook = $this->getLocalSystemAddressBook(); |
|
| 240 | + if ($userOrCardId instanceof IUser){ |
|
| 241 | + $name = $userOrCardId->getBackendClassName(); |
|
| 242 | + $userId = $userOrCardId->getUID(); |
|
| 243 | + |
|
| 244 | + $userOrCardId = "$name:$userId.vcf"; |
|
| 245 | + } |
|
| 246 | + $this->backend->deleteCard($systemAddressBook['id'], $userOrCardId); |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + /** |
|
| 250 | + * @return array|null |
|
| 251 | + */ |
|
| 252 | + public function getLocalSystemAddressBook() { |
|
| 253 | + if (is_null($this->localSystemAddressBook)) { |
|
| 254 | + $systemPrincipal = "principals/system/system"; |
|
| 255 | + $this->localSystemAddressBook = $this->ensureSystemAddressBookExists($systemPrincipal, 'system', [ |
|
| 256 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => 'System addressbook which holds all users of this instance' |
|
| 257 | + ]); |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + return $this->localSystemAddressBook; |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + public function syncInstance(\Closure $progressCallback = null) { |
|
| 264 | + $systemAddressBook = $this->getLocalSystemAddressBook(); |
|
| 265 | + $this->userManager->callForAllUsers(function($user) use ($systemAddressBook, $progressCallback) { |
|
| 266 | + $this->updateUser($user); |
|
| 267 | + if (!is_null($progressCallback)) { |
|
| 268 | + $progressCallback(); |
|
| 269 | + } |
|
| 270 | + }); |
|
| 271 | + |
|
| 272 | + // remove no longer existing |
|
| 273 | + $allCards = $this->backend->getCards($systemAddressBook['id']); |
|
| 274 | + foreach($allCards as $card) { |
|
| 275 | + $vCard = Reader::read($card['carddata']); |
|
| 276 | + $uid = $vCard->UID->getValue(); |
|
| 277 | + // load backend and see if user exists |
|
| 278 | + if (!$this->userManager->userExists($uid)) { |
|
| 279 | + $this->deleteUser($card['uri']); |
|
| 280 | + } |
|
| 281 | + } |
|
| 282 | + } |
|
| 283 | 283 | |
| 284 | 284 | |
| 285 | 285 | } |
@@ -93,7 +93,7 @@ |
||
| 93 | 93 | /** |
| 94 | 94 | * returns a list of all possible property names |
| 95 | 95 | * |
| 96 | - * @return array |
|
| 96 | + * @return string[] |
|
| 97 | 97 | */ |
| 98 | 98 | static public function getPropertyNames() { |
| 99 | 99 | return [ |
@@ -36,228 +36,228 @@ |
||
| 36 | 36 | use Sabre\DAV\PropPatch; |
| 37 | 37 | |
| 38 | 38 | class CommentNode implements \Sabre\DAV\INode, \Sabre\DAV\IProperties { |
| 39 | - const NS_OWNCLOUD = 'http://owncloud.org/ns'; |
|
| 39 | + const NS_OWNCLOUD = 'http://owncloud.org/ns'; |
|
| 40 | 40 | |
| 41 | - const PROPERTY_NAME_UNREAD = '{http://owncloud.org/ns}isUnread'; |
|
| 42 | - const PROPERTY_NAME_MESSAGE = '{http://owncloud.org/ns}message'; |
|
| 43 | - const PROPERTY_NAME_ACTOR_DISPLAYNAME = '{http://owncloud.org/ns}actorDisplayName'; |
|
| 41 | + const PROPERTY_NAME_UNREAD = '{http://owncloud.org/ns}isUnread'; |
|
| 42 | + const PROPERTY_NAME_MESSAGE = '{http://owncloud.org/ns}message'; |
|
| 43 | + const PROPERTY_NAME_ACTOR_DISPLAYNAME = '{http://owncloud.org/ns}actorDisplayName'; |
|
| 44 | 44 | |
| 45 | - /** @var IComment */ |
|
| 46 | - public $comment; |
|
| 45 | + /** @var IComment */ |
|
| 46 | + public $comment; |
|
| 47 | 47 | |
| 48 | - /** @var ICommentsManager */ |
|
| 49 | - protected $commentsManager; |
|
| 48 | + /** @var ICommentsManager */ |
|
| 49 | + protected $commentsManager; |
|
| 50 | 50 | |
| 51 | - /** @var ILogger */ |
|
| 52 | - protected $logger; |
|
| 51 | + /** @var ILogger */ |
|
| 52 | + protected $logger; |
|
| 53 | 53 | |
| 54 | - /** @var array list of properties with key being their name and value their setter */ |
|
| 55 | - protected $properties = []; |
|
| 54 | + /** @var array list of properties with key being their name and value their setter */ |
|
| 55 | + protected $properties = []; |
|
| 56 | 56 | |
| 57 | - /** @var IUserManager */ |
|
| 58 | - protected $userManager; |
|
| 57 | + /** @var IUserManager */ |
|
| 58 | + protected $userManager; |
|
| 59 | 59 | |
| 60 | - /** @var IUserSession */ |
|
| 61 | - protected $userSession; |
|
| 60 | + /** @var IUserSession */ |
|
| 61 | + protected $userSession; |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * CommentNode constructor. |
|
| 65 | - * |
|
| 66 | - * @param ICommentsManager $commentsManager |
|
| 67 | - * @param IComment $comment |
|
| 68 | - * @param IUserManager $userManager |
|
| 69 | - * @param IUserSession $userSession |
|
| 70 | - * @param ILogger $logger |
|
| 71 | - */ |
|
| 72 | - public function __construct( |
|
| 73 | - ICommentsManager $commentsManager, |
|
| 74 | - IComment $comment, |
|
| 75 | - IUserManager $userManager, |
|
| 76 | - IUserSession $userSession, |
|
| 77 | - ILogger $logger |
|
| 78 | - ) { |
|
| 79 | - $this->commentsManager = $commentsManager; |
|
| 80 | - $this->comment = $comment; |
|
| 81 | - $this->logger = $logger; |
|
| 63 | + /** |
|
| 64 | + * CommentNode constructor. |
|
| 65 | + * |
|
| 66 | + * @param ICommentsManager $commentsManager |
|
| 67 | + * @param IComment $comment |
|
| 68 | + * @param IUserManager $userManager |
|
| 69 | + * @param IUserSession $userSession |
|
| 70 | + * @param ILogger $logger |
|
| 71 | + */ |
|
| 72 | + public function __construct( |
|
| 73 | + ICommentsManager $commentsManager, |
|
| 74 | + IComment $comment, |
|
| 75 | + IUserManager $userManager, |
|
| 76 | + IUserSession $userSession, |
|
| 77 | + ILogger $logger |
|
| 78 | + ) { |
|
| 79 | + $this->commentsManager = $commentsManager; |
|
| 80 | + $this->comment = $comment; |
|
| 81 | + $this->logger = $logger; |
|
| 82 | 82 | |
| 83 | - $methods = get_class_methods($this->comment); |
|
| 84 | - $methods = array_filter($methods, function($name){ |
|
| 85 | - return strpos($name, 'get') === 0; |
|
| 86 | - }); |
|
| 87 | - foreach($methods as $getter) { |
|
| 88 | - $name = '{'.self::NS_OWNCLOUD.'}' . lcfirst(substr($getter, 3)); |
|
| 89 | - $this->properties[$name] = $getter; |
|
| 90 | - } |
|
| 91 | - $this->userManager = $userManager; |
|
| 92 | - $this->userSession = $userSession; |
|
| 93 | - } |
|
| 83 | + $methods = get_class_methods($this->comment); |
|
| 84 | + $methods = array_filter($methods, function($name){ |
|
| 85 | + return strpos($name, 'get') === 0; |
|
| 86 | + }); |
|
| 87 | + foreach($methods as $getter) { |
|
| 88 | + $name = '{'.self::NS_OWNCLOUD.'}' . lcfirst(substr($getter, 3)); |
|
| 89 | + $this->properties[$name] = $getter; |
|
| 90 | + } |
|
| 91 | + $this->userManager = $userManager; |
|
| 92 | + $this->userSession = $userSession; |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - /** |
|
| 96 | - * returns a list of all possible property names |
|
| 97 | - * |
|
| 98 | - * @return array |
|
| 99 | - */ |
|
| 100 | - static public function getPropertyNames() { |
|
| 101 | - return [ |
|
| 102 | - '{http://owncloud.org/ns}id', |
|
| 103 | - '{http://owncloud.org/ns}parentId', |
|
| 104 | - '{http://owncloud.org/ns}topmostParentId', |
|
| 105 | - '{http://owncloud.org/ns}childrenCount', |
|
| 106 | - '{http://owncloud.org/ns}verb', |
|
| 107 | - '{http://owncloud.org/ns}actorType', |
|
| 108 | - '{http://owncloud.org/ns}actorId', |
|
| 109 | - '{http://owncloud.org/ns}creationDateTime', |
|
| 110 | - '{http://owncloud.org/ns}latestChildDateTime', |
|
| 111 | - '{http://owncloud.org/ns}objectType', |
|
| 112 | - '{http://owncloud.org/ns}objectId', |
|
| 113 | - // re-used property names are defined as constants |
|
| 114 | - self::PROPERTY_NAME_MESSAGE, |
|
| 115 | - self::PROPERTY_NAME_ACTOR_DISPLAYNAME, |
|
| 116 | - self::PROPERTY_NAME_UNREAD |
|
| 117 | - ]; |
|
| 118 | - } |
|
| 95 | + /** |
|
| 96 | + * returns a list of all possible property names |
|
| 97 | + * |
|
| 98 | + * @return array |
|
| 99 | + */ |
|
| 100 | + static public function getPropertyNames() { |
|
| 101 | + return [ |
|
| 102 | + '{http://owncloud.org/ns}id', |
|
| 103 | + '{http://owncloud.org/ns}parentId', |
|
| 104 | + '{http://owncloud.org/ns}topmostParentId', |
|
| 105 | + '{http://owncloud.org/ns}childrenCount', |
|
| 106 | + '{http://owncloud.org/ns}verb', |
|
| 107 | + '{http://owncloud.org/ns}actorType', |
|
| 108 | + '{http://owncloud.org/ns}actorId', |
|
| 109 | + '{http://owncloud.org/ns}creationDateTime', |
|
| 110 | + '{http://owncloud.org/ns}latestChildDateTime', |
|
| 111 | + '{http://owncloud.org/ns}objectType', |
|
| 112 | + '{http://owncloud.org/ns}objectId', |
|
| 113 | + // re-used property names are defined as constants |
|
| 114 | + self::PROPERTY_NAME_MESSAGE, |
|
| 115 | + self::PROPERTY_NAME_ACTOR_DISPLAYNAME, |
|
| 116 | + self::PROPERTY_NAME_UNREAD |
|
| 117 | + ]; |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | - protected function checkWriteAccessOnComment() { |
|
| 121 | - $user = $this->userSession->getUser(); |
|
| 122 | - if( $this->comment->getActorType() !== 'users' |
|
| 123 | - || is_null($user) |
|
| 124 | - || $this->comment->getActorId() !== $user->getUID() |
|
| 125 | - ) { |
|
| 126 | - throw new Forbidden('Only authors are allowed to edit their comment.'); |
|
| 127 | - } |
|
| 128 | - } |
|
| 120 | + protected function checkWriteAccessOnComment() { |
|
| 121 | + $user = $this->userSession->getUser(); |
|
| 122 | + if( $this->comment->getActorType() !== 'users' |
|
| 123 | + || is_null($user) |
|
| 124 | + || $this->comment->getActorId() !== $user->getUID() |
|
| 125 | + ) { |
|
| 126 | + throw new Forbidden('Only authors are allowed to edit their comment.'); |
|
| 127 | + } |
|
| 128 | + } |
|
| 129 | 129 | |
| 130 | - /** |
|
| 131 | - * Deleted the current node |
|
| 132 | - * |
|
| 133 | - * @return void |
|
| 134 | - */ |
|
| 135 | - function delete() { |
|
| 136 | - $this->checkWriteAccessOnComment(); |
|
| 137 | - $this->commentsManager->delete($this->comment->getId()); |
|
| 138 | - } |
|
| 130 | + /** |
|
| 131 | + * Deleted the current node |
|
| 132 | + * |
|
| 133 | + * @return void |
|
| 134 | + */ |
|
| 135 | + function delete() { |
|
| 136 | + $this->checkWriteAccessOnComment(); |
|
| 137 | + $this->commentsManager->delete($this->comment->getId()); |
|
| 138 | + } |
|
| 139 | 139 | |
| 140 | - /** |
|
| 141 | - * Returns the name of the node. |
|
| 142 | - * |
|
| 143 | - * This is used to generate the url. |
|
| 144 | - * |
|
| 145 | - * @return string |
|
| 146 | - */ |
|
| 147 | - function getName() { |
|
| 148 | - return $this->comment->getId(); |
|
| 149 | - } |
|
| 140 | + /** |
|
| 141 | + * Returns the name of the node. |
|
| 142 | + * |
|
| 143 | + * This is used to generate the url. |
|
| 144 | + * |
|
| 145 | + * @return string |
|
| 146 | + */ |
|
| 147 | + function getName() { |
|
| 148 | + return $this->comment->getId(); |
|
| 149 | + } |
|
| 150 | 150 | |
| 151 | - /** |
|
| 152 | - * Renames the node |
|
| 153 | - * |
|
| 154 | - * @param string $name The new name |
|
| 155 | - * @throws MethodNotAllowed |
|
| 156 | - */ |
|
| 157 | - function setName($name) { |
|
| 158 | - throw new MethodNotAllowed(); |
|
| 159 | - } |
|
| 151 | + /** |
|
| 152 | + * Renames the node |
|
| 153 | + * |
|
| 154 | + * @param string $name The new name |
|
| 155 | + * @throws MethodNotAllowed |
|
| 156 | + */ |
|
| 157 | + function setName($name) { |
|
| 158 | + throw new MethodNotAllowed(); |
|
| 159 | + } |
|
| 160 | 160 | |
| 161 | - /** |
|
| 162 | - * Returns the last modification time, as a unix timestamp |
|
| 163 | - * |
|
| 164 | - * @return int |
|
| 165 | - */ |
|
| 166 | - function getLastModified() { |
|
| 167 | - return null; |
|
| 168 | - } |
|
| 161 | + /** |
|
| 162 | + * Returns the last modification time, as a unix timestamp |
|
| 163 | + * |
|
| 164 | + * @return int |
|
| 165 | + */ |
|
| 166 | + function getLastModified() { |
|
| 167 | + return null; |
|
| 168 | + } |
|
| 169 | 169 | |
| 170 | - /** |
|
| 171 | - * update the comment's message |
|
| 172 | - * |
|
| 173 | - * @param $propertyValue |
|
| 174 | - * @return bool |
|
| 175 | - * @throws BadRequest |
|
| 176 | - * @throws Forbidden |
|
| 177 | - */ |
|
| 178 | - public function updateComment($propertyValue) { |
|
| 179 | - $this->checkWriteAccessOnComment(); |
|
| 180 | - try { |
|
| 181 | - $this->comment->setMessage($propertyValue); |
|
| 182 | - $this->commentsManager->save($this->comment); |
|
| 183 | - return true; |
|
| 184 | - } catch (\Exception $e) { |
|
| 185 | - $this->logger->logException($e, ['app' => 'dav/comments']); |
|
| 186 | - if($e instanceof MessageTooLongException) { |
|
| 187 | - $msg = 'Message exceeds allowed character limit of '; |
|
| 188 | - throw new BadRequest($msg . IComment::MAX_MESSAGE_LENGTH, 0, $e); |
|
| 189 | - } |
|
| 190 | - throw $e; |
|
| 191 | - } |
|
| 192 | - } |
|
| 170 | + /** |
|
| 171 | + * update the comment's message |
|
| 172 | + * |
|
| 173 | + * @param $propertyValue |
|
| 174 | + * @return bool |
|
| 175 | + * @throws BadRequest |
|
| 176 | + * @throws Forbidden |
|
| 177 | + */ |
|
| 178 | + public function updateComment($propertyValue) { |
|
| 179 | + $this->checkWriteAccessOnComment(); |
|
| 180 | + try { |
|
| 181 | + $this->comment->setMessage($propertyValue); |
|
| 182 | + $this->commentsManager->save($this->comment); |
|
| 183 | + return true; |
|
| 184 | + } catch (\Exception $e) { |
|
| 185 | + $this->logger->logException($e, ['app' => 'dav/comments']); |
|
| 186 | + if($e instanceof MessageTooLongException) { |
|
| 187 | + $msg = 'Message exceeds allowed character limit of '; |
|
| 188 | + throw new BadRequest($msg . IComment::MAX_MESSAGE_LENGTH, 0, $e); |
|
| 189 | + } |
|
| 190 | + throw $e; |
|
| 191 | + } |
|
| 192 | + } |
|
| 193 | 193 | |
| 194 | - /** |
|
| 195 | - * Updates properties on this node. |
|
| 196 | - * |
|
| 197 | - * This method received a PropPatch object, which contains all the |
|
| 198 | - * information about the update. |
|
| 199 | - * |
|
| 200 | - * To update specific properties, call the 'handle' method on this object. |
|
| 201 | - * Read the PropPatch documentation for more information. |
|
| 202 | - * |
|
| 203 | - * @param PropPatch $propPatch |
|
| 204 | - * @return void |
|
| 205 | - */ |
|
| 206 | - function propPatch(PropPatch $propPatch) { |
|
| 207 | - // other properties than 'message' are read only |
|
| 208 | - $propPatch->handle(self::PROPERTY_NAME_MESSAGE, [$this, 'updateComment']); |
|
| 209 | - } |
|
| 194 | + /** |
|
| 195 | + * Updates properties on this node. |
|
| 196 | + * |
|
| 197 | + * This method received a PropPatch object, which contains all the |
|
| 198 | + * information about the update. |
|
| 199 | + * |
|
| 200 | + * To update specific properties, call the 'handle' method on this object. |
|
| 201 | + * Read the PropPatch documentation for more information. |
|
| 202 | + * |
|
| 203 | + * @param PropPatch $propPatch |
|
| 204 | + * @return void |
|
| 205 | + */ |
|
| 206 | + function propPatch(PropPatch $propPatch) { |
|
| 207 | + // other properties than 'message' are read only |
|
| 208 | + $propPatch->handle(self::PROPERTY_NAME_MESSAGE, [$this, 'updateComment']); |
|
| 209 | + } |
|
| 210 | 210 | |
| 211 | - /** |
|
| 212 | - * Returns a list of properties for this nodes. |
|
| 213 | - * |
|
| 214 | - * The properties list is a list of propertynames the client requested, |
|
| 215 | - * encoded in clark-notation {xmlnamespace}tagname |
|
| 216 | - * |
|
| 217 | - * If the array is empty, it means 'all properties' were requested. |
|
| 218 | - * |
|
| 219 | - * Note that it's fine to liberally give properties back, instead of |
|
| 220 | - * conforming to the list of requested properties. |
|
| 221 | - * The Server class will filter out the extra. |
|
| 222 | - * |
|
| 223 | - * @param array $properties |
|
| 224 | - * @return array |
|
| 225 | - */ |
|
| 226 | - function getProperties($properties) { |
|
| 227 | - $properties = array_keys($this->properties); |
|
| 211 | + /** |
|
| 212 | + * Returns a list of properties for this nodes. |
|
| 213 | + * |
|
| 214 | + * The properties list is a list of propertynames the client requested, |
|
| 215 | + * encoded in clark-notation {xmlnamespace}tagname |
|
| 216 | + * |
|
| 217 | + * If the array is empty, it means 'all properties' were requested. |
|
| 218 | + * |
|
| 219 | + * Note that it's fine to liberally give properties back, instead of |
|
| 220 | + * conforming to the list of requested properties. |
|
| 221 | + * The Server class will filter out the extra. |
|
| 222 | + * |
|
| 223 | + * @param array $properties |
|
| 224 | + * @return array |
|
| 225 | + */ |
|
| 226 | + function getProperties($properties) { |
|
| 227 | + $properties = array_keys($this->properties); |
|
| 228 | 228 | |
| 229 | - $result = []; |
|
| 230 | - foreach($properties as $property) { |
|
| 231 | - $getter = $this->properties[$property]; |
|
| 232 | - if(method_exists($this->comment, $getter)) { |
|
| 233 | - $result[$property] = $this->comment->$getter(); |
|
| 234 | - } |
|
| 235 | - } |
|
| 229 | + $result = []; |
|
| 230 | + foreach($properties as $property) { |
|
| 231 | + $getter = $this->properties[$property]; |
|
| 232 | + if(method_exists($this->comment, $getter)) { |
|
| 233 | + $result[$property] = $this->comment->$getter(); |
|
| 234 | + } |
|
| 235 | + } |
|
| 236 | 236 | |
| 237 | - if($this->comment->getActorType() === 'users') { |
|
| 238 | - $user = $this->userManager->get($this->comment->getActorId()); |
|
| 239 | - $displayName = is_null($user) ? null : $user->getDisplayName(); |
|
| 240 | - $result[self::PROPERTY_NAME_ACTOR_DISPLAYNAME] = $displayName; |
|
| 241 | - } |
|
| 237 | + if($this->comment->getActorType() === 'users') { |
|
| 238 | + $user = $this->userManager->get($this->comment->getActorId()); |
|
| 239 | + $displayName = is_null($user) ? null : $user->getDisplayName(); |
|
| 240 | + $result[self::PROPERTY_NAME_ACTOR_DISPLAYNAME] = $displayName; |
|
| 241 | + } |
|
| 242 | 242 | |
| 243 | - $unread = null; |
|
| 244 | - $user = $this->userSession->getUser(); |
|
| 245 | - if(!is_null($user)) { |
|
| 246 | - $readUntil = $this->commentsManager->getReadMark( |
|
| 247 | - $this->comment->getObjectType(), |
|
| 248 | - $this->comment->getObjectId(), |
|
| 249 | - $user |
|
| 250 | - ); |
|
| 251 | - if(is_null($readUntil)) { |
|
| 252 | - $unread = 'true'; |
|
| 253 | - } else { |
|
| 254 | - $unread = $this->comment->getCreationDateTime() > $readUntil; |
|
| 255 | - // re-format for output |
|
| 256 | - $unread = $unread ? 'true' : 'false'; |
|
| 257 | - } |
|
| 258 | - } |
|
| 259 | - $result[self::PROPERTY_NAME_UNREAD] = $unread; |
|
| 243 | + $unread = null; |
|
| 244 | + $user = $this->userSession->getUser(); |
|
| 245 | + if(!is_null($user)) { |
|
| 246 | + $readUntil = $this->commentsManager->getReadMark( |
|
| 247 | + $this->comment->getObjectType(), |
|
| 248 | + $this->comment->getObjectId(), |
|
| 249 | + $user |
|
| 250 | + ); |
|
| 251 | + if(is_null($readUntil)) { |
|
| 252 | + $unread = 'true'; |
|
| 253 | + } else { |
|
| 254 | + $unread = $this->comment->getCreationDateTime() > $readUntil; |
|
| 255 | + // re-format for output |
|
| 256 | + $unread = $unread ? 'true' : 'false'; |
|
| 257 | + } |
|
| 258 | + } |
|
| 259 | + $result[self::PROPERTY_NAME_UNREAD] = $unread; |
|
| 260 | 260 | |
| 261 | - return $result; |
|
| 262 | - } |
|
| 261 | + return $result; |
|
| 262 | + } |
|
| 263 | 263 | } |
@@ -138,7 +138,7 @@ |
||
| 138 | 138 | * This will be used in the {DAV:}supported-report-set property. |
| 139 | 139 | * |
| 140 | 140 | * @param string $uri |
| 141 | - * @return array |
|
| 141 | + * @return string[] |
|
| 142 | 142 | */ |
| 143 | 143 | public function getSupportedReportSet($uri) { |
| 144 | 144 | return [self::REPORT_NAME]; |
@@ -43,213 +43,213 @@ |
||
| 43 | 43 | * Sabre plugin to handle comments: |
| 44 | 44 | */ |
| 45 | 45 | class CommentsPlugin extends ServerPlugin { |
| 46 | - // namespace |
|
| 47 | - const NS_OWNCLOUD = 'http://owncloud.org/ns'; |
|
| 48 | - |
|
| 49 | - const REPORT_NAME = '{http://owncloud.org/ns}filter-comments'; |
|
| 50 | - const REPORT_PARAM_LIMIT = '{http://owncloud.org/ns}limit'; |
|
| 51 | - const REPORT_PARAM_OFFSET = '{http://owncloud.org/ns}offset'; |
|
| 52 | - const REPORT_PARAM_TIMESTAMP = '{http://owncloud.org/ns}datetime'; |
|
| 53 | - |
|
| 54 | - /** @var ICommentsManager */ |
|
| 55 | - protected $commentsManager; |
|
| 56 | - |
|
| 57 | - /** @var \Sabre\DAV\Server $server */ |
|
| 58 | - private $server; |
|
| 59 | - |
|
| 60 | - /** @var \OCP\IUserSession */ |
|
| 61 | - protected $userSession; |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Comments plugin |
|
| 65 | - * |
|
| 66 | - * @param ICommentsManager $commentsManager |
|
| 67 | - * @param IUserSession $userSession |
|
| 68 | - */ |
|
| 69 | - public function __construct(ICommentsManager $commentsManager, IUserSession $userSession) { |
|
| 70 | - $this->commentsManager = $commentsManager; |
|
| 71 | - $this->userSession = $userSession; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * This initializes the plugin. |
|
| 76 | - * |
|
| 77 | - * This function is called by Sabre\DAV\Server, after |
|
| 78 | - * addPlugin is called. |
|
| 79 | - * |
|
| 80 | - * This method should set up the required event subscriptions. |
|
| 81 | - * |
|
| 82 | - * @param Server $server |
|
| 83 | - * @return void |
|
| 84 | - */ |
|
| 85 | - function initialize(Server $server) { |
|
| 86 | - $this->server = $server; |
|
| 87 | - if(strpos($this->server->getRequestUri(), 'comments/') !== 0) { |
|
| 88 | - return; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - $this->server->xml->namespaceMap[self::NS_OWNCLOUD] = 'oc'; |
|
| 92 | - |
|
| 93 | - $this->server->xml->classMap['DateTime'] = function(Writer $writer, \DateTime $value) { |
|
| 94 | - $writer->write(\Sabre\HTTP\toDate($value)); |
|
| 95 | - }; |
|
| 96 | - |
|
| 97 | - $this->server->on('report', [$this, 'onReport']); |
|
| 98 | - $this->server->on('method:POST', [$this, 'httpPost']); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * POST operation on Comments collections |
|
| 103 | - * |
|
| 104 | - * @param RequestInterface $request request object |
|
| 105 | - * @param ResponseInterface $response response object |
|
| 106 | - * @return null|false |
|
| 107 | - */ |
|
| 108 | - public function httpPost(RequestInterface $request, ResponseInterface $response) { |
|
| 109 | - $path = $request->getPath(); |
|
| 110 | - $node = $this->server->tree->getNodeForPath($path); |
|
| 111 | - if (!$node instanceof EntityCollection) { |
|
| 112 | - return null; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - $data = $request->getBodyAsString(); |
|
| 116 | - $comment = $this->createComment( |
|
| 117 | - $node->getName(), |
|
| 118 | - $node->getId(), |
|
| 119 | - $data, |
|
| 120 | - $request->getHeader('Content-Type') |
|
| 121 | - ); |
|
| 122 | - |
|
| 123 | - // update read marker for the current user/poster to avoid |
|
| 124 | - // having their own comments marked as unread |
|
| 125 | - $node->setReadMarker(null); |
|
| 126 | - |
|
| 127 | - $url = rtrim($request->getUrl(), '/') . '/' . urlencode($comment->getId()); |
|
| 128 | - |
|
| 129 | - $response->setHeader('Content-Location', $url); |
|
| 130 | - |
|
| 131 | - // created |
|
| 132 | - $response->setStatus(201); |
|
| 133 | - return false; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Returns a list of reports this plugin supports. |
|
| 138 | - * |
|
| 139 | - * This will be used in the {DAV:}supported-report-set property. |
|
| 140 | - * |
|
| 141 | - * @param string $uri |
|
| 142 | - * @return array |
|
| 143 | - */ |
|
| 144 | - public function getSupportedReportSet($uri) { |
|
| 145 | - return [self::REPORT_NAME]; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * REPORT operations to look for comments |
|
| 150 | - * |
|
| 151 | - * @param string $reportName |
|
| 152 | - * @param [] $report |
|
| 153 | - * @param string $uri |
|
| 154 | - * @return bool |
|
| 155 | - * @throws NotFound |
|
| 156 | - * @throws ReportNotSupported |
|
| 157 | - */ |
|
| 158 | - public function onReport($reportName, $report, $uri) { |
|
| 159 | - $node = $this->server->tree->getNodeForPath($uri); |
|
| 160 | - if(!$node instanceof EntityCollection || $reportName !== self::REPORT_NAME) { |
|
| 161 | - throw new ReportNotSupported(); |
|
| 162 | - } |
|
| 163 | - $args = ['limit' => 0, 'offset' => 0, 'datetime' => null]; |
|
| 164 | - $acceptableParameters = [ |
|
| 165 | - $this::REPORT_PARAM_LIMIT, |
|
| 166 | - $this::REPORT_PARAM_OFFSET, |
|
| 167 | - $this::REPORT_PARAM_TIMESTAMP |
|
| 168 | - ]; |
|
| 169 | - $ns = '{' . $this::NS_OWNCLOUD . '}'; |
|
| 170 | - foreach($report as $parameter) { |
|
| 171 | - if(!in_array($parameter['name'], $acceptableParameters) || empty($parameter['value'])) { |
|
| 172 | - continue; |
|
| 173 | - } |
|
| 174 | - $args[str_replace($ns, '', $parameter['name'])] = $parameter['value']; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - if(!is_null($args['datetime'])) { |
|
| 178 | - $args['datetime'] = new \DateTime($args['datetime']); |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - $results = $node->findChildren($args['limit'], $args['offset'], $args['datetime']); |
|
| 182 | - |
|
| 183 | - $responses = []; |
|
| 184 | - foreach($results as $node) { |
|
| 185 | - $nodePath = $this->server->getRequestUri() . '/' . $node->comment->getId(); |
|
| 186 | - $resultSet = $this->server->getPropertiesForPath($nodePath, CommentNode::getPropertyNames()); |
|
| 187 | - if(isset($resultSet[0]) && isset($resultSet[0][200])) { |
|
| 188 | - $responses[] = new Response( |
|
| 189 | - $this->server->getBaseUri() . $nodePath, |
|
| 190 | - [200 => $resultSet[0][200]], |
|
| 191 | - 200 |
|
| 192 | - ); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - $xml = $this->server->xml->write( |
|
| 198 | - '{DAV:}multistatus', |
|
| 199 | - new MultiStatus($responses) |
|
| 200 | - ); |
|
| 201 | - |
|
| 202 | - $this->server->httpResponse->setStatus(207); |
|
| 203 | - $this->server->httpResponse->setHeader('Content-Type', 'application/xml; charset=utf-8'); |
|
| 204 | - $this->server->httpResponse->setBody($xml); |
|
| 205 | - |
|
| 206 | - return false; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * Creates a new comment |
|
| 211 | - * |
|
| 212 | - * @param string $objectType e.g. "files" |
|
| 213 | - * @param string $objectId e.g. the file id |
|
| 214 | - * @param string $data JSON encoded string containing the properties of the tag to create |
|
| 215 | - * @param string $contentType content type of the data |
|
| 216 | - * @return IComment newly created comment |
|
| 217 | - * |
|
| 218 | - * @throws BadRequest if a field was missing |
|
| 219 | - * @throws UnsupportedMediaType if the content type is not supported |
|
| 220 | - */ |
|
| 221 | - private function createComment($objectType, $objectId, $data, $contentType = 'application/json') { |
|
| 222 | - if (explode(';', $contentType)[0] === 'application/json') { |
|
| 223 | - $data = json_decode($data, true); |
|
| 224 | - } else { |
|
| 225 | - throw new UnsupportedMediaType(); |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - $actorType = $data['actorType']; |
|
| 229 | - $actorId = null; |
|
| 230 | - if($actorType === 'users') { |
|
| 231 | - $user = $this->userSession->getUser(); |
|
| 232 | - if(!is_null($user)) { |
|
| 233 | - $actorId = $user->getUID(); |
|
| 234 | - } |
|
| 235 | - } |
|
| 236 | - if(is_null($actorId)) { |
|
| 237 | - throw new BadRequest('Invalid actor "' . $actorType .'"'); |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - try { |
|
| 241 | - $comment = $this->commentsManager->create($actorType, $actorId, $objectType, $objectId); |
|
| 242 | - $comment->setMessage($data['message']); |
|
| 243 | - $comment->setVerb($data['verb']); |
|
| 244 | - $this->commentsManager->save($comment); |
|
| 245 | - return $comment; |
|
| 246 | - } catch (\InvalidArgumentException $e) { |
|
| 247 | - throw new BadRequest('Invalid input values', 0, $e); |
|
| 248 | - } catch (\OCP\Comments\MessageTooLongException $e) { |
|
| 249 | - $msg = 'Message exceeds allowed character limit of '; |
|
| 250 | - throw new BadRequest($msg . \OCP\Comments\IComment::MAX_MESSAGE_LENGTH, 0, $e); |
|
| 251 | - } |
|
| 252 | - } |
|
| 46 | + // namespace |
|
| 47 | + const NS_OWNCLOUD = 'http://owncloud.org/ns'; |
|
| 48 | + |
|
| 49 | + const REPORT_NAME = '{http://owncloud.org/ns}filter-comments'; |
|
| 50 | + const REPORT_PARAM_LIMIT = '{http://owncloud.org/ns}limit'; |
|
| 51 | + const REPORT_PARAM_OFFSET = '{http://owncloud.org/ns}offset'; |
|
| 52 | + const REPORT_PARAM_TIMESTAMP = '{http://owncloud.org/ns}datetime'; |
|
| 53 | + |
|
| 54 | + /** @var ICommentsManager */ |
|
| 55 | + protected $commentsManager; |
|
| 56 | + |
|
| 57 | + /** @var \Sabre\DAV\Server $server */ |
|
| 58 | + private $server; |
|
| 59 | + |
|
| 60 | + /** @var \OCP\IUserSession */ |
|
| 61 | + protected $userSession; |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Comments plugin |
|
| 65 | + * |
|
| 66 | + * @param ICommentsManager $commentsManager |
|
| 67 | + * @param IUserSession $userSession |
|
| 68 | + */ |
|
| 69 | + public function __construct(ICommentsManager $commentsManager, IUserSession $userSession) { |
|
| 70 | + $this->commentsManager = $commentsManager; |
|
| 71 | + $this->userSession = $userSession; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * This initializes the plugin. |
|
| 76 | + * |
|
| 77 | + * This function is called by Sabre\DAV\Server, after |
|
| 78 | + * addPlugin is called. |
|
| 79 | + * |
|
| 80 | + * This method should set up the required event subscriptions. |
|
| 81 | + * |
|
| 82 | + * @param Server $server |
|
| 83 | + * @return void |
|
| 84 | + */ |
|
| 85 | + function initialize(Server $server) { |
|
| 86 | + $this->server = $server; |
|
| 87 | + if(strpos($this->server->getRequestUri(), 'comments/') !== 0) { |
|
| 88 | + return; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + $this->server->xml->namespaceMap[self::NS_OWNCLOUD] = 'oc'; |
|
| 92 | + |
|
| 93 | + $this->server->xml->classMap['DateTime'] = function(Writer $writer, \DateTime $value) { |
|
| 94 | + $writer->write(\Sabre\HTTP\toDate($value)); |
|
| 95 | + }; |
|
| 96 | + |
|
| 97 | + $this->server->on('report', [$this, 'onReport']); |
|
| 98 | + $this->server->on('method:POST', [$this, 'httpPost']); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * POST operation on Comments collections |
|
| 103 | + * |
|
| 104 | + * @param RequestInterface $request request object |
|
| 105 | + * @param ResponseInterface $response response object |
|
| 106 | + * @return null|false |
|
| 107 | + */ |
|
| 108 | + public function httpPost(RequestInterface $request, ResponseInterface $response) { |
|
| 109 | + $path = $request->getPath(); |
|
| 110 | + $node = $this->server->tree->getNodeForPath($path); |
|
| 111 | + if (!$node instanceof EntityCollection) { |
|
| 112 | + return null; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + $data = $request->getBodyAsString(); |
|
| 116 | + $comment = $this->createComment( |
|
| 117 | + $node->getName(), |
|
| 118 | + $node->getId(), |
|
| 119 | + $data, |
|
| 120 | + $request->getHeader('Content-Type') |
|
| 121 | + ); |
|
| 122 | + |
|
| 123 | + // update read marker for the current user/poster to avoid |
|
| 124 | + // having their own comments marked as unread |
|
| 125 | + $node->setReadMarker(null); |
|
| 126 | + |
|
| 127 | + $url = rtrim($request->getUrl(), '/') . '/' . urlencode($comment->getId()); |
|
| 128 | + |
|
| 129 | + $response->setHeader('Content-Location', $url); |
|
| 130 | + |
|
| 131 | + // created |
|
| 132 | + $response->setStatus(201); |
|
| 133 | + return false; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Returns a list of reports this plugin supports. |
|
| 138 | + * |
|
| 139 | + * This will be used in the {DAV:}supported-report-set property. |
|
| 140 | + * |
|
| 141 | + * @param string $uri |
|
| 142 | + * @return array |
|
| 143 | + */ |
|
| 144 | + public function getSupportedReportSet($uri) { |
|
| 145 | + return [self::REPORT_NAME]; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * REPORT operations to look for comments |
|
| 150 | + * |
|
| 151 | + * @param string $reportName |
|
| 152 | + * @param [] $report |
|
| 153 | + * @param string $uri |
|
| 154 | + * @return bool |
|
| 155 | + * @throws NotFound |
|
| 156 | + * @throws ReportNotSupported |
|
| 157 | + */ |
|
| 158 | + public function onReport($reportName, $report, $uri) { |
|
| 159 | + $node = $this->server->tree->getNodeForPath($uri); |
|
| 160 | + if(!$node instanceof EntityCollection || $reportName !== self::REPORT_NAME) { |
|
| 161 | + throw new ReportNotSupported(); |
|
| 162 | + } |
|
| 163 | + $args = ['limit' => 0, 'offset' => 0, 'datetime' => null]; |
|
| 164 | + $acceptableParameters = [ |
|
| 165 | + $this::REPORT_PARAM_LIMIT, |
|
| 166 | + $this::REPORT_PARAM_OFFSET, |
|
| 167 | + $this::REPORT_PARAM_TIMESTAMP |
|
| 168 | + ]; |
|
| 169 | + $ns = '{' . $this::NS_OWNCLOUD . '}'; |
|
| 170 | + foreach($report as $parameter) { |
|
| 171 | + if(!in_array($parameter['name'], $acceptableParameters) || empty($parameter['value'])) { |
|
| 172 | + continue; |
|
| 173 | + } |
|
| 174 | + $args[str_replace($ns, '', $parameter['name'])] = $parameter['value']; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + if(!is_null($args['datetime'])) { |
|
| 178 | + $args['datetime'] = new \DateTime($args['datetime']); |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + $results = $node->findChildren($args['limit'], $args['offset'], $args['datetime']); |
|
| 182 | + |
|
| 183 | + $responses = []; |
|
| 184 | + foreach($results as $node) { |
|
| 185 | + $nodePath = $this->server->getRequestUri() . '/' . $node->comment->getId(); |
|
| 186 | + $resultSet = $this->server->getPropertiesForPath($nodePath, CommentNode::getPropertyNames()); |
|
| 187 | + if(isset($resultSet[0]) && isset($resultSet[0][200])) { |
|
| 188 | + $responses[] = new Response( |
|
| 189 | + $this->server->getBaseUri() . $nodePath, |
|
| 190 | + [200 => $resultSet[0][200]], |
|
| 191 | + 200 |
|
| 192 | + ); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + $xml = $this->server->xml->write( |
|
| 198 | + '{DAV:}multistatus', |
|
| 199 | + new MultiStatus($responses) |
|
| 200 | + ); |
|
| 201 | + |
|
| 202 | + $this->server->httpResponse->setStatus(207); |
|
| 203 | + $this->server->httpResponse->setHeader('Content-Type', 'application/xml; charset=utf-8'); |
|
| 204 | + $this->server->httpResponse->setBody($xml); |
|
| 205 | + |
|
| 206 | + return false; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * Creates a new comment |
|
| 211 | + * |
|
| 212 | + * @param string $objectType e.g. "files" |
|
| 213 | + * @param string $objectId e.g. the file id |
|
| 214 | + * @param string $data JSON encoded string containing the properties of the tag to create |
|
| 215 | + * @param string $contentType content type of the data |
|
| 216 | + * @return IComment newly created comment |
|
| 217 | + * |
|
| 218 | + * @throws BadRequest if a field was missing |
|
| 219 | + * @throws UnsupportedMediaType if the content type is not supported |
|
| 220 | + */ |
|
| 221 | + private function createComment($objectType, $objectId, $data, $contentType = 'application/json') { |
|
| 222 | + if (explode(';', $contentType)[0] === 'application/json') { |
|
| 223 | + $data = json_decode($data, true); |
|
| 224 | + } else { |
|
| 225 | + throw new UnsupportedMediaType(); |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + $actorType = $data['actorType']; |
|
| 229 | + $actorId = null; |
|
| 230 | + if($actorType === 'users') { |
|
| 231 | + $user = $this->userSession->getUser(); |
|
| 232 | + if(!is_null($user)) { |
|
| 233 | + $actorId = $user->getUID(); |
|
| 234 | + } |
|
| 235 | + } |
|
| 236 | + if(is_null($actorId)) { |
|
| 237 | + throw new BadRequest('Invalid actor "' . $actorType .'"'); |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + try { |
|
| 241 | + $comment = $this->commentsManager->create($actorType, $actorId, $objectType, $objectId); |
|
| 242 | + $comment->setMessage($data['message']); |
|
| 243 | + $comment->setVerb($data['verb']); |
|
| 244 | + $this->commentsManager->save($comment); |
|
| 245 | + return $comment; |
|
| 246 | + } catch (\InvalidArgumentException $e) { |
|
| 247 | + throw new BadRequest('Invalid input values', 0, $e); |
|
| 248 | + } catch (\OCP\Comments\MessageTooLongException $e) { |
|
| 249 | + $msg = 'Message exceeds allowed character limit of '; |
|
| 250 | + throw new BadRequest($msg . \OCP\Comments\IComment::MAX_MESSAGE_LENGTH, 0, $e); |
|
| 251 | + } |
|
| 252 | + } |
|
| 253 | 253 | |
| 254 | 254 | |
| 255 | 255 | |
@@ -115,7 +115,7 @@ |
||
| 115 | 115 | /** |
| 116 | 116 | * Returns an array with all the child nodes |
| 117 | 117 | * |
| 118 | - * @return \Sabre\DAV\INode[] |
|
| 118 | + * @return CommentNode[] |
|
| 119 | 119 | */ |
| 120 | 120 | function getChildren() { |
| 121 | 121 | return $this->findChildren(); |
@@ -41,156 +41,156 @@ |
||
| 41 | 41 | * @package OCA\DAV\Comments |
| 42 | 42 | */ |
| 43 | 43 | class EntityCollection extends RootCollection implements IProperties { |
| 44 | - const PROPERTY_NAME_READ_MARKER = '{http://owncloud.org/ns}readMarker'; |
|
| 44 | + const PROPERTY_NAME_READ_MARKER = '{http://owncloud.org/ns}readMarker'; |
|
| 45 | 45 | |
| 46 | - /** @var string */ |
|
| 47 | - protected $id; |
|
| 46 | + /** @var string */ |
|
| 47 | + protected $id; |
|
| 48 | 48 | |
| 49 | - /** @var ILogger */ |
|
| 50 | - protected $logger; |
|
| 49 | + /** @var ILogger */ |
|
| 50 | + protected $logger; |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * @param string $id |
|
| 54 | - * @param string $name |
|
| 55 | - * @param ICommentsManager $commentsManager |
|
| 56 | - * @param IUserManager $userManager |
|
| 57 | - * @param IUserSession $userSession |
|
| 58 | - * @param ILogger $logger |
|
| 59 | - */ |
|
| 60 | - public function __construct( |
|
| 61 | - $id, |
|
| 62 | - $name, |
|
| 63 | - ICommentsManager $commentsManager, |
|
| 64 | - IUserManager $userManager, |
|
| 65 | - IUserSession $userSession, |
|
| 66 | - ILogger $logger |
|
| 67 | - ) { |
|
| 68 | - foreach(['id', 'name'] as $property) { |
|
| 69 | - $$property = trim($$property); |
|
| 70 | - if(empty($$property) || !is_string($$property)) { |
|
| 71 | - throw new \InvalidArgumentException('"' . $property . '" parameter must be non-empty string'); |
|
| 72 | - } |
|
| 73 | - } |
|
| 74 | - $this->id = $id; |
|
| 75 | - $this->name = $name; |
|
| 76 | - $this->commentsManager = $commentsManager; |
|
| 77 | - $this->logger = $logger; |
|
| 78 | - $this->userManager = $userManager; |
|
| 79 | - $this->userSession = $userSession; |
|
| 80 | - } |
|
| 52 | + /** |
|
| 53 | + * @param string $id |
|
| 54 | + * @param string $name |
|
| 55 | + * @param ICommentsManager $commentsManager |
|
| 56 | + * @param IUserManager $userManager |
|
| 57 | + * @param IUserSession $userSession |
|
| 58 | + * @param ILogger $logger |
|
| 59 | + */ |
|
| 60 | + public function __construct( |
|
| 61 | + $id, |
|
| 62 | + $name, |
|
| 63 | + ICommentsManager $commentsManager, |
|
| 64 | + IUserManager $userManager, |
|
| 65 | + IUserSession $userSession, |
|
| 66 | + ILogger $logger |
|
| 67 | + ) { |
|
| 68 | + foreach(['id', 'name'] as $property) { |
|
| 69 | + $$property = trim($$property); |
|
| 70 | + if(empty($$property) || !is_string($$property)) { |
|
| 71 | + throw new \InvalidArgumentException('"' . $property . '" parameter must be non-empty string'); |
|
| 72 | + } |
|
| 73 | + } |
|
| 74 | + $this->id = $id; |
|
| 75 | + $this->name = $name; |
|
| 76 | + $this->commentsManager = $commentsManager; |
|
| 77 | + $this->logger = $logger; |
|
| 78 | + $this->userManager = $userManager; |
|
| 79 | + $this->userSession = $userSession; |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * returns the ID of this entity |
|
| 84 | - * |
|
| 85 | - * @return string |
|
| 86 | - */ |
|
| 87 | - public function getId() { |
|
| 88 | - return $this->id; |
|
| 89 | - } |
|
| 82 | + /** |
|
| 83 | + * returns the ID of this entity |
|
| 84 | + * |
|
| 85 | + * @return string |
|
| 86 | + */ |
|
| 87 | + public function getId() { |
|
| 88 | + return $this->id; |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - /** |
|
| 92 | - * Returns a specific child node, referenced by its name |
|
| 93 | - * |
|
| 94 | - * This method must throw Sabre\DAV\Exception\NotFound if the node does not |
|
| 95 | - * exist. |
|
| 96 | - * |
|
| 97 | - * @param string $name |
|
| 98 | - * @return \Sabre\DAV\INode |
|
| 99 | - * @throws NotFound |
|
| 100 | - */ |
|
| 101 | - function getChild($name) { |
|
| 102 | - try { |
|
| 103 | - $comment = $this->commentsManager->get($name); |
|
| 104 | - return new CommentNode( |
|
| 105 | - $this->commentsManager, |
|
| 106 | - $comment, |
|
| 107 | - $this->userManager, |
|
| 108 | - $this->userSession, |
|
| 109 | - $this->logger |
|
| 110 | - ); |
|
| 111 | - } catch (NotFoundException $e) { |
|
| 112 | - throw new NotFound(); |
|
| 113 | - } |
|
| 114 | - } |
|
| 91 | + /** |
|
| 92 | + * Returns a specific child node, referenced by its name |
|
| 93 | + * |
|
| 94 | + * This method must throw Sabre\DAV\Exception\NotFound if the node does not |
|
| 95 | + * exist. |
|
| 96 | + * |
|
| 97 | + * @param string $name |
|
| 98 | + * @return \Sabre\DAV\INode |
|
| 99 | + * @throws NotFound |
|
| 100 | + */ |
|
| 101 | + function getChild($name) { |
|
| 102 | + try { |
|
| 103 | + $comment = $this->commentsManager->get($name); |
|
| 104 | + return new CommentNode( |
|
| 105 | + $this->commentsManager, |
|
| 106 | + $comment, |
|
| 107 | + $this->userManager, |
|
| 108 | + $this->userSession, |
|
| 109 | + $this->logger |
|
| 110 | + ); |
|
| 111 | + } catch (NotFoundException $e) { |
|
| 112 | + throw new NotFound(); |
|
| 113 | + } |
|
| 114 | + } |
|
| 115 | 115 | |
| 116 | - /** |
|
| 117 | - * Returns an array with all the child nodes |
|
| 118 | - * |
|
| 119 | - * @return \Sabre\DAV\INode[] |
|
| 120 | - */ |
|
| 121 | - function getChildren() { |
|
| 122 | - return $this->findChildren(); |
|
| 123 | - } |
|
| 116 | + /** |
|
| 117 | + * Returns an array with all the child nodes |
|
| 118 | + * |
|
| 119 | + * @return \Sabre\DAV\INode[] |
|
| 120 | + */ |
|
| 121 | + function getChildren() { |
|
| 122 | + return $this->findChildren(); |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | - /** |
|
| 126 | - * Returns an array of comment nodes. Result can be influenced by offset, |
|
| 127 | - * limit and date time parameters. |
|
| 128 | - * |
|
| 129 | - * @param int $limit |
|
| 130 | - * @param int $offset |
|
| 131 | - * @param \DateTime|null $datetime |
|
| 132 | - * @return CommentNode[] |
|
| 133 | - */ |
|
| 134 | - function findChildren($limit = 0, $offset = 0, \DateTime $datetime = null) { |
|
| 135 | - $comments = $this->commentsManager->getForObject($this->name, $this->id, $limit, $offset, $datetime); |
|
| 136 | - $result = []; |
|
| 137 | - foreach($comments as $comment) { |
|
| 138 | - $result[] = new CommentNode( |
|
| 139 | - $this->commentsManager, |
|
| 140 | - $comment, |
|
| 141 | - $this->userManager, |
|
| 142 | - $this->userSession, |
|
| 143 | - $this->logger |
|
| 144 | - ); |
|
| 145 | - } |
|
| 146 | - return $result; |
|
| 147 | - } |
|
| 125 | + /** |
|
| 126 | + * Returns an array of comment nodes. Result can be influenced by offset, |
|
| 127 | + * limit and date time parameters. |
|
| 128 | + * |
|
| 129 | + * @param int $limit |
|
| 130 | + * @param int $offset |
|
| 131 | + * @param \DateTime|null $datetime |
|
| 132 | + * @return CommentNode[] |
|
| 133 | + */ |
|
| 134 | + function findChildren($limit = 0, $offset = 0, \DateTime $datetime = null) { |
|
| 135 | + $comments = $this->commentsManager->getForObject($this->name, $this->id, $limit, $offset, $datetime); |
|
| 136 | + $result = []; |
|
| 137 | + foreach($comments as $comment) { |
|
| 138 | + $result[] = new CommentNode( |
|
| 139 | + $this->commentsManager, |
|
| 140 | + $comment, |
|
| 141 | + $this->userManager, |
|
| 142 | + $this->userSession, |
|
| 143 | + $this->logger |
|
| 144 | + ); |
|
| 145 | + } |
|
| 146 | + return $result; |
|
| 147 | + } |
|
| 148 | 148 | |
| 149 | - /** |
|
| 150 | - * Checks if a child-node with the specified name exists |
|
| 151 | - * |
|
| 152 | - * @param string $name |
|
| 153 | - * @return bool |
|
| 154 | - */ |
|
| 155 | - function childExists($name) { |
|
| 156 | - try { |
|
| 157 | - $this->commentsManager->get($name); |
|
| 158 | - return true; |
|
| 159 | - } catch (NotFoundException $e) { |
|
| 160 | - return false; |
|
| 161 | - } |
|
| 162 | - } |
|
| 149 | + /** |
|
| 150 | + * Checks if a child-node with the specified name exists |
|
| 151 | + * |
|
| 152 | + * @param string $name |
|
| 153 | + * @return bool |
|
| 154 | + */ |
|
| 155 | + function childExists($name) { |
|
| 156 | + try { |
|
| 157 | + $this->commentsManager->get($name); |
|
| 158 | + return true; |
|
| 159 | + } catch (NotFoundException $e) { |
|
| 160 | + return false; |
|
| 161 | + } |
|
| 162 | + } |
|
| 163 | 163 | |
| 164 | - /** |
|
| 165 | - * Sets the read marker to the specified date for the logged in user |
|
| 166 | - * |
|
| 167 | - * @param \DateTime $value |
|
| 168 | - * @return bool |
|
| 169 | - */ |
|
| 170 | - public function setReadMarker($value) { |
|
| 171 | - $dateTime = new \DateTime($value); |
|
| 172 | - $user = $this->userSession->getUser(); |
|
| 173 | - $this->commentsManager->setReadMark($this->name, $this->id, $dateTime, $user); |
|
| 174 | - return true; |
|
| 175 | - } |
|
| 164 | + /** |
|
| 165 | + * Sets the read marker to the specified date for the logged in user |
|
| 166 | + * |
|
| 167 | + * @param \DateTime $value |
|
| 168 | + * @return bool |
|
| 169 | + */ |
|
| 170 | + public function setReadMarker($value) { |
|
| 171 | + $dateTime = new \DateTime($value); |
|
| 172 | + $user = $this->userSession->getUser(); |
|
| 173 | + $this->commentsManager->setReadMark($this->name, $this->id, $dateTime, $user); |
|
| 174 | + return true; |
|
| 175 | + } |
|
| 176 | 176 | |
| 177 | - /** |
|
| 178 | - * @inheritdoc |
|
| 179 | - */ |
|
| 180 | - function propPatch(PropPatch $propPatch) { |
|
| 181 | - $propPatch->handle(self::PROPERTY_NAME_READ_MARKER, [$this, 'setReadMarker']); |
|
| 182 | - } |
|
| 177 | + /** |
|
| 178 | + * @inheritdoc |
|
| 179 | + */ |
|
| 180 | + function propPatch(PropPatch $propPatch) { |
|
| 181 | + $propPatch->handle(self::PROPERTY_NAME_READ_MARKER, [$this, 'setReadMarker']); |
|
| 182 | + } |
|
| 183 | 183 | |
| 184 | - /** |
|
| 185 | - * @inheritdoc |
|
| 186 | - */ |
|
| 187 | - function getProperties($properties) { |
|
| 188 | - $marker = null; |
|
| 189 | - $user = $this->userSession->getUser(); |
|
| 190 | - if(!is_null($user)) { |
|
| 191 | - $marker = $this->commentsManager->getReadMark($this->name, $this->id, $user); |
|
| 192 | - } |
|
| 193 | - return [self::PROPERTY_NAME_READ_MARKER => $marker]; |
|
| 194 | - } |
|
| 184 | + /** |
|
| 185 | + * @inheritdoc |
|
| 186 | + */ |
|
| 187 | + function getProperties($properties) { |
|
| 188 | + $marker = null; |
|
| 189 | + $user = $this->userSession->getUser(); |
|
| 190 | + if(!is_null($user)) { |
|
| 191 | + $marker = $this->commentsManager->getReadMark($this->name, $this->id, $user); |
|
| 192 | + } |
|
| 193 | + return [self::PROPERTY_NAME_READ_MARKER => $marker]; |
|
| 194 | + } |
|
| 195 | 195 | } |
| 196 | 196 | |
@@ -64,6 +64,9 @@ |
||
| 64 | 64 | ); |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | + /** |
|
| 68 | + * @param boolean $toV2 |
|
| 69 | + */ |
|
| 67 | 70 | private function convertPrincipal($principal, $toV2) { |
| 68 | 71 | list(, $name) = URLUtil::splitPath($principal); |
| 69 | 72 | if ($toV2) { |
@@ -32,57 +32,57 @@ |
||
| 32 | 32 | |
| 33 | 33 | class LegacyDAVACL extends DavAclPlugin { |
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * Converts the v1 principal `principal/<username>` to the new v2 |
|
| 37 | - * `principal/users/<username>` which is required for permission checks |
|
| 38 | - * |
|
| 39 | - * @inheritdoc |
|
| 40 | - */ |
|
| 41 | - function getCurrentUserPrincipal() { |
|
| 42 | - $principalV1 = parent::getCurrentUserPrincipal(); |
|
| 43 | - if (is_null($principalV1)) { |
|
| 44 | - return $principalV1; |
|
| 45 | - } |
|
| 46 | - return $this->convertPrincipal($principalV1, true); |
|
| 47 | - } |
|
| 35 | + /** |
|
| 36 | + * Converts the v1 principal `principal/<username>` to the new v2 |
|
| 37 | + * `principal/users/<username>` which is required for permission checks |
|
| 38 | + * |
|
| 39 | + * @inheritdoc |
|
| 40 | + */ |
|
| 41 | + function getCurrentUserPrincipal() { |
|
| 42 | + $principalV1 = parent::getCurrentUserPrincipal(); |
|
| 43 | + if (is_null($principalV1)) { |
|
| 44 | + return $principalV1; |
|
| 45 | + } |
|
| 46 | + return $this->convertPrincipal($principalV1, true); |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * @inheritdoc |
|
| 52 | - */ |
|
| 53 | - function getCurrentUserPrincipals() { |
|
| 54 | - $principalV2 = $this->getCurrentUserPrincipal(); |
|
| 50 | + /** |
|
| 51 | + * @inheritdoc |
|
| 52 | + */ |
|
| 53 | + function getCurrentUserPrincipals() { |
|
| 54 | + $principalV2 = $this->getCurrentUserPrincipal(); |
|
| 55 | 55 | |
| 56 | - if (is_null($principalV2)) return []; |
|
| 56 | + if (is_null($principalV2)) return []; |
|
| 57 | 57 | |
| 58 | - $principalV1 = $this->convertPrincipal($principalV2, false); |
|
| 59 | - return array_merge( |
|
| 60 | - [ |
|
| 61 | - $principalV2, |
|
| 62 | - $principalV1 |
|
| 63 | - ], |
|
| 64 | - $this->getPrincipalMembership($principalV1) |
|
| 65 | - ); |
|
| 66 | - } |
|
| 58 | + $principalV1 = $this->convertPrincipal($principalV2, false); |
|
| 59 | + return array_merge( |
|
| 60 | + [ |
|
| 61 | + $principalV2, |
|
| 62 | + $principalV1 |
|
| 63 | + ], |
|
| 64 | + $this->getPrincipalMembership($principalV1) |
|
| 65 | + ); |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - private function convertPrincipal($principal, $toV2) { |
|
| 69 | - list(, $name) = URLUtil::splitPath($principal); |
|
| 70 | - if ($toV2) { |
|
| 71 | - return "principals/users/$name"; |
|
| 72 | - } |
|
| 73 | - return "principals/$name"; |
|
| 74 | - } |
|
| 68 | + private function convertPrincipal($principal, $toV2) { |
|
| 69 | + list(, $name) = URLUtil::splitPath($principal); |
|
| 70 | + if ($toV2) { |
|
| 71 | + return "principals/users/$name"; |
|
| 72 | + } |
|
| 73 | + return "principals/$name"; |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - function propFind(PropFind $propFind, INode $node) { |
|
| 77 | - /* Overload current-user-principal */ |
|
| 78 | - $propFind->handle('{DAV:}current-user-principal', function () { |
|
| 79 | - if ($url = parent::getCurrentUserPrincipal()) { |
|
| 80 | - return new Principal(Principal::HREF, $url . '/'); |
|
| 81 | - } else { |
|
| 82 | - return new Principal(Principal::UNAUTHENTICATED); |
|
| 83 | - } |
|
| 84 | - }); |
|
| 76 | + function propFind(PropFind $propFind, INode $node) { |
|
| 77 | + /* Overload current-user-principal */ |
|
| 78 | + $propFind->handle('{DAV:}current-user-principal', function () { |
|
| 79 | + if ($url = parent::getCurrentUserPrincipal()) { |
|
| 80 | + return new Principal(Principal::HREF, $url . '/'); |
|
| 81 | + } else { |
|
| 82 | + return new Principal(Principal::UNAUTHENTICATED); |
|
| 83 | + } |
|
| 84 | + }); |
|
| 85 | 85 | |
| 86 | - return parent::propFind($propFind, $node); |
|
| 87 | - } |
|
| 86 | + return parent::propFind($propFind, $node); |
|
| 87 | + } |
|
| 88 | 88 | } |
@@ -75,7 +75,7 @@ |
||
| 75 | 75 | private $cache = []; |
| 76 | 76 | |
| 77 | 77 | /** |
| 78 | - * @param Tree $tree node tree |
|
| 78 | + * @param ObjectTree $tree node tree |
|
| 79 | 79 | * @param IDBConnection $connection database connection |
| 80 | 80 | * @param IUser $user owner of the tree and properties |
| 81 | 81 | */ |
@@ -36,322 +36,322 @@ |
||
| 36 | 36 | |
| 37 | 37 | class CustomPropertiesBackend implements BackendInterface { |
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Ignored properties |
|
| 41 | - * |
|
| 42 | - * @var array |
|
| 43 | - */ |
|
| 44 | - private $ignoredProperties = array( |
|
| 45 | - '{DAV:}getcontentlength', |
|
| 46 | - '{DAV:}getcontenttype', |
|
| 47 | - '{DAV:}getetag', |
|
| 48 | - '{DAV:}quota-used-bytes', |
|
| 49 | - '{DAV:}quota-available-bytes', |
|
| 50 | - '{DAV:}quota-available-bytes', |
|
| 51 | - '{http://owncloud.org/ns}permissions', |
|
| 52 | - '{http://owncloud.org/ns}downloadURL', |
|
| 53 | - '{http://owncloud.org/ns}dDC', |
|
| 54 | - '{http://owncloud.org/ns}size', |
|
| 55 | - ); |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @var Tree |
|
| 59 | - */ |
|
| 60 | - private $tree; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * @var IDBConnection |
|
| 64 | - */ |
|
| 65 | - private $connection; |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * @var IUser |
|
| 69 | - */ |
|
| 70 | - private $user; |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Properties cache |
|
| 74 | - * |
|
| 75 | - * @var array |
|
| 76 | - */ |
|
| 77 | - private $cache = []; |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * @param Tree $tree node tree |
|
| 81 | - * @param IDBConnection $connection database connection |
|
| 82 | - * @param IUser $user owner of the tree and properties |
|
| 83 | - */ |
|
| 84 | - public function __construct( |
|
| 85 | - Tree $tree, |
|
| 86 | - IDBConnection $connection, |
|
| 87 | - IUser $user) { |
|
| 88 | - $this->tree = $tree; |
|
| 89 | - $this->connection = $connection; |
|
| 90 | - $this->user = $user->getUID(); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Fetches properties for a path. |
|
| 95 | - * |
|
| 96 | - * @param string $path |
|
| 97 | - * @param PropFind $propFind |
|
| 98 | - * @return void |
|
| 99 | - */ |
|
| 100 | - public function propFind($path, PropFind $propFind) { |
|
| 101 | - try { |
|
| 102 | - $node = $this->tree->getNodeForPath($path); |
|
| 103 | - if (!($node instanceof Node)) { |
|
| 104 | - return; |
|
| 105 | - } |
|
| 106 | - } catch (ServiceUnavailable $e) { |
|
| 107 | - // might happen for unavailable mount points, skip |
|
| 108 | - return; |
|
| 109 | - } catch (NotFound $e) { |
|
| 110 | - // in some rare (buggy) cases the node might not be found, |
|
| 111 | - // we catch the exception to prevent breaking the whole list with a 404 |
|
| 112 | - // (soft fail) |
|
| 113 | - \OC::$server->getLogger()->warning( |
|
| 114 | - 'Could not get node for path: \"' . $path . '\" : ' . $e->getMessage(), |
|
| 115 | - array('app' => 'files') |
|
| 116 | - ); |
|
| 117 | - return; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - $requestedProps = $propFind->get404Properties(); |
|
| 121 | - |
|
| 122 | - // these might appear |
|
| 123 | - $requestedProps = array_diff( |
|
| 124 | - $requestedProps, |
|
| 125 | - $this->ignoredProperties |
|
| 126 | - ); |
|
| 127 | - |
|
| 128 | - if (empty($requestedProps)) { |
|
| 129 | - return; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - if ($node instanceof Directory |
|
| 133 | - && $propFind->getDepth() !== 0 |
|
| 134 | - ) { |
|
| 135 | - // note: pre-fetching only supported for depth <= 1 |
|
| 136 | - $this->loadChildrenProperties($node, $requestedProps); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - $props = $this->getProperties($node, $requestedProps); |
|
| 140 | - foreach ($props as $propName => $propValue) { |
|
| 141 | - $propFind->set($propName, $propValue); |
|
| 142 | - } |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Updates properties for a path |
|
| 147 | - * |
|
| 148 | - * @param string $path |
|
| 149 | - * @param PropPatch $propPatch |
|
| 150 | - * |
|
| 151 | - * @return void |
|
| 152 | - */ |
|
| 153 | - public function propPatch($path, PropPatch $propPatch) { |
|
| 154 | - $node = $this->tree->getNodeForPath($path); |
|
| 155 | - if (!($node instanceof Node)) { |
|
| 156 | - return; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - $propPatch->handleRemaining(function($changedProps) use ($node) { |
|
| 160 | - return $this->updateProperties($node, $changedProps); |
|
| 161 | - }); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * This method is called after a node is deleted. |
|
| 166 | - * |
|
| 167 | - * @param string $path path of node for which to delete properties |
|
| 168 | - */ |
|
| 169 | - public function delete($path) { |
|
| 170 | - $statement = $this->connection->prepare( |
|
| 171 | - 'DELETE FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` = ?' |
|
| 172 | - ); |
|
| 173 | - $statement->execute(array($this->user, '/' . $path)); |
|
| 174 | - $statement->closeCursor(); |
|
| 175 | - |
|
| 176 | - unset($this->cache[$path]); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * This method is called after a successful MOVE |
|
| 181 | - * |
|
| 182 | - * @param string $source |
|
| 183 | - * @param string $destination |
|
| 184 | - * |
|
| 185 | - * @return void |
|
| 186 | - */ |
|
| 187 | - public function move($source, $destination) { |
|
| 188 | - $statement = $this->connection->prepare( |
|
| 189 | - 'UPDATE `*PREFIX*properties` SET `propertypath` = ?' . |
|
| 190 | - ' WHERE `userid` = ? AND `propertypath` = ?' |
|
| 191 | - ); |
|
| 192 | - $statement->execute(array('/' . $destination, $this->user, '/' . $source)); |
|
| 193 | - $statement->closeCursor(); |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - /** |
|
| 197 | - * Returns a list of properties for this nodes.; |
|
| 198 | - * @param Node $node |
|
| 199 | - * @param array $requestedProperties requested properties or empty array for "all" |
|
| 200 | - * @return array |
|
| 201 | - * @note The properties list is a list of propertynames the client |
|
| 202 | - * requested, encoded as xmlnamespace#tagName, for example: |
|
| 203 | - * http://www.example.org/namespace#author If the array is empty, all |
|
| 204 | - * properties should be returned |
|
| 205 | - */ |
|
| 206 | - private function getProperties(Node $node, array $requestedProperties) { |
|
| 207 | - $path = $node->getPath(); |
|
| 208 | - if (isset($this->cache[$path])) { |
|
| 209 | - return $this->cache[$path]; |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - // TODO: chunking if more than 1000 properties |
|
| 213 | - $sql = 'SELECT * FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` = ?'; |
|
| 214 | - |
|
| 215 | - $whereValues = array($this->user, $path); |
|
| 216 | - $whereTypes = array(null, null); |
|
| 217 | - |
|
| 218 | - if (!empty($requestedProperties)) { |
|
| 219 | - // request only a subset |
|
| 220 | - $sql .= ' AND `propertyname` in (?)'; |
|
| 221 | - $whereValues[] = $requestedProperties; |
|
| 222 | - $whereTypes[] = \Doctrine\DBAL\Connection::PARAM_STR_ARRAY; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - $result = $this->connection->executeQuery( |
|
| 226 | - $sql, |
|
| 227 | - $whereValues, |
|
| 228 | - $whereTypes |
|
| 229 | - ); |
|
| 230 | - |
|
| 231 | - $props = []; |
|
| 232 | - while ($row = $result->fetch()) { |
|
| 233 | - $props[$row['propertyname']] = $row['propertyvalue']; |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - $result->closeCursor(); |
|
| 237 | - |
|
| 238 | - $this->cache[$path] = $props; |
|
| 239 | - return $props; |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - /** |
|
| 243 | - * Update properties |
|
| 244 | - * |
|
| 245 | - * @param Node $node node for which to update properties |
|
| 246 | - * @param array $properties array of properties to update |
|
| 247 | - * |
|
| 248 | - * @return bool |
|
| 249 | - */ |
|
| 250 | - private function updateProperties($node, $properties) { |
|
| 251 | - $path = $node->getPath(); |
|
| 252 | - |
|
| 253 | - $deleteStatement = 'DELETE FROM `*PREFIX*properties`' . |
|
| 254 | - ' WHERE `userid` = ? AND `propertypath` = ? AND `propertyname` = ?'; |
|
| 255 | - |
|
| 256 | - $insertStatement = 'INSERT INTO `*PREFIX*properties`' . |
|
| 257 | - ' (`userid`,`propertypath`,`propertyname`,`propertyvalue`) VALUES(?,?,?,?)'; |
|
| 258 | - |
|
| 259 | - $updateStatement = 'UPDATE `*PREFIX*properties` SET `propertyvalue` = ?' . |
|
| 260 | - ' WHERE `userid` = ? AND `propertypath` = ? AND `propertyname` = ?'; |
|
| 261 | - |
|
| 262 | - // TODO: use "insert or update" strategy ? |
|
| 263 | - $existing = $this->getProperties($node, array()); |
|
| 264 | - $this->connection->beginTransaction(); |
|
| 265 | - foreach ($properties as $propertyName => $propertyValue) { |
|
| 266 | - // If it was null, we need to delete the property |
|
| 267 | - if (is_null($propertyValue)) { |
|
| 268 | - if (array_key_exists($propertyName, $existing)) { |
|
| 269 | - $this->connection->executeUpdate($deleteStatement, |
|
| 270 | - array( |
|
| 271 | - $this->user, |
|
| 272 | - $path, |
|
| 273 | - $propertyName |
|
| 274 | - ) |
|
| 275 | - ); |
|
| 276 | - } |
|
| 277 | - } else { |
|
| 278 | - if (!array_key_exists($propertyName, $existing)) { |
|
| 279 | - $this->connection->executeUpdate($insertStatement, |
|
| 280 | - array( |
|
| 281 | - $this->user, |
|
| 282 | - $path, |
|
| 283 | - $propertyName, |
|
| 284 | - $propertyValue |
|
| 285 | - ) |
|
| 286 | - ); |
|
| 287 | - } else { |
|
| 288 | - $this->connection->executeUpdate($updateStatement, |
|
| 289 | - array( |
|
| 290 | - $propertyValue, |
|
| 291 | - $this->user, |
|
| 292 | - $path, |
|
| 293 | - $propertyName |
|
| 294 | - ) |
|
| 295 | - ); |
|
| 296 | - } |
|
| 297 | - } |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - $this->connection->commit(); |
|
| 301 | - unset($this->cache[$path]); |
|
| 302 | - |
|
| 303 | - return true; |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * Bulk load properties for directory children |
|
| 308 | - * |
|
| 309 | - * @param Directory $node |
|
| 310 | - * @param array $requestedProperties requested properties |
|
| 311 | - * |
|
| 312 | - * @return void |
|
| 313 | - */ |
|
| 314 | - private function loadChildrenProperties(Directory $node, $requestedProperties) { |
|
| 315 | - $path = $node->getPath(); |
|
| 316 | - if (isset($this->cache[$path])) { |
|
| 317 | - // we already loaded them at some point |
|
| 318 | - return; |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - $childNodes = $node->getChildren(); |
|
| 322 | - // pre-fill cache |
|
| 323 | - foreach ($childNodes as $childNode) { |
|
| 324 | - $this->cache[$childNode->getPath()] = []; |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - $sql = 'SELECT * FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` LIKE ?'; |
|
| 328 | - $sql .= ' AND `propertyname` in (?) ORDER BY `propertypath`, `propertyname`'; |
|
| 329 | - |
|
| 330 | - $result = $this->connection->executeQuery( |
|
| 331 | - $sql, |
|
| 332 | - array($this->user, $this->connection->escapeLikeParameter(rtrim($path, '/')) . '/%', $requestedProperties), |
|
| 333 | - array(null, null, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY) |
|
| 334 | - ); |
|
| 335 | - |
|
| 336 | - $oldPath = null; |
|
| 337 | - $props = []; |
|
| 338 | - while ($row = $result->fetch()) { |
|
| 339 | - $path = $row['propertypath']; |
|
| 340 | - if ($oldPath !== $path) { |
|
| 341 | - // save previously gathered props |
|
| 342 | - $this->cache[$oldPath] = $props; |
|
| 343 | - $oldPath = $path; |
|
| 344 | - // prepare props for next path |
|
| 345 | - $props = []; |
|
| 346 | - } |
|
| 347 | - $props[$row['propertyname']] = $row['propertyvalue']; |
|
| 348 | - } |
|
| 349 | - if (!is_null($oldPath)) { |
|
| 350 | - // save props from last run |
|
| 351 | - $this->cache[$oldPath] = $props; |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - $result->closeCursor(); |
|
| 355 | - } |
|
| 39 | + /** |
|
| 40 | + * Ignored properties |
|
| 41 | + * |
|
| 42 | + * @var array |
|
| 43 | + */ |
|
| 44 | + private $ignoredProperties = array( |
|
| 45 | + '{DAV:}getcontentlength', |
|
| 46 | + '{DAV:}getcontenttype', |
|
| 47 | + '{DAV:}getetag', |
|
| 48 | + '{DAV:}quota-used-bytes', |
|
| 49 | + '{DAV:}quota-available-bytes', |
|
| 50 | + '{DAV:}quota-available-bytes', |
|
| 51 | + '{http://owncloud.org/ns}permissions', |
|
| 52 | + '{http://owncloud.org/ns}downloadURL', |
|
| 53 | + '{http://owncloud.org/ns}dDC', |
|
| 54 | + '{http://owncloud.org/ns}size', |
|
| 55 | + ); |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @var Tree |
|
| 59 | + */ |
|
| 60 | + private $tree; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * @var IDBConnection |
|
| 64 | + */ |
|
| 65 | + private $connection; |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * @var IUser |
|
| 69 | + */ |
|
| 70 | + private $user; |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Properties cache |
|
| 74 | + * |
|
| 75 | + * @var array |
|
| 76 | + */ |
|
| 77 | + private $cache = []; |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * @param Tree $tree node tree |
|
| 81 | + * @param IDBConnection $connection database connection |
|
| 82 | + * @param IUser $user owner of the tree and properties |
|
| 83 | + */ |
|
| 84 | + public function __construct( |
|
| 85 | + Tree $tree, |
|
| 86 | + IDBConnection $connection, |
|
| 87 | + IUser $user) { |
|
| 88 | + $this->tree = $tree; |
|
| 89 | + $this->connection = $connection; |
|
| 90 | + $this->user = $user->getUID(); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Fetches properties for a path. |
|
| 95 | + * |
|
| 96 | + * @param string $path |
|
| 97 | + * @param PropFind $propFind |
|
| 98 | + * @return void |
|
| 99 | + */ |
|
| 100 | + public function propFind($path, PropFind $propFind) { |
|
| 101 | + try { |
|
| 102 | + $node = $this->tree->getNodeForPath($path); |
|
| 103 | + if (!($node instanceof Node)) { |
|
| 104 | + return; |
|
| 105 | + } |
|
| 106 | + } catch (ServiceUnavailable $e) { |
|
| 107 | + // might happen for unavailable mount points, skip |
|
| 108 | + return; |
|
| 109 | + } catch (NotFound $e) { |
|
| 110 | + // in some rare (buggy) cases the node might not be found, |
|
| 111 | + // we catch the exception to prevent breaking the whole list with a 404 |
|
| 112 | + // (soft fail) |
|
| 113 | + \OC::$server->getLogger()->warning( |
|
| 114 | + 'Could not get node for path: \"' . $path . '\" : ' . $e->getMessage(), |
|
| 115 | + array('app' => 'files') |
|
| 116 | + ); |
|
| 117 | + return; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + $requestedProps = $propFind->get404Properties(); |
|
| 121 | + |
|
| 122 | + // these might appear |
|
| 123 | + $requestedProps = array_diff( |
|
| 124 | + $requestedProps, |
|
| 125 | + $this->ignoredProperties |
|
| 126 | + ); |
|
| 127 | + |
|
| 128 | + if (empty($requestedProps)) { |
|
| 129 | + return; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + if ($node instanceof Directory |
|
| 133 | + && $propFind->getDepth() !== 0 |
|
| 134 | + ) { |
|
| 135 | + // note: pre-fetching only supported for depth <= 1 |
|
| 136 | + $this->loadChildrenProperties($node, $requestedProps); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + $props = $this->getProperties($node, $requestedProps); |
|
| 140 | + foreach ($props as $propName => $propValue) { |
|
| 141 | + $propFind->set($propName, $propValue); |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Updates properties for a path |
|
| 147 | + * |
|
| 148 | + * @param string $path |
|
| 149 | + * @param PropPatch $propPatch |
|
| 150 | + * |
|
| 151 | + * @return void |
|
| 152 | + */ |
|
| 153 | + public function propPatch($path, PropPatch $propPatch) { |
|
| 154 | + $node = $this->tree->getNodeForPath($path); |
|
| 155 | + if (!($node instanceof Node)) { |
|
| 156 | + return; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + $propPatch->handleRemaining(function($changedProps) use ($node) { |
|
| 160 | + return $this->updateProperties($node, $changedProps); |
|
| 161 | + }); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * This method is called after a node is deleted. |
|
| 166 | + * |
|
| 167 | + * @param string $path path of node for which to delete properties |
|
| 168 | + */ |
|
| 169 | + public function delete($path) { |
|
| 170 | + $statement = $this->connection->prepare( |
|
| 171 | + 'DELETE FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` = ?' |
|
| 172 | + ); |
|
| 173 | + $statement->execute(array($this->user, '/' . $path)); |
|
| 174 | + $statement->closeCursor(); |
|
| 175 | + |
|
| 176 | + unset($this->cache[$path]); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * This method is called after a successful MOVE |
|
| 181 | + * |
|
| 182 | + * @param string $source |
|
| 183 | + * @param string $destination |
|
| 184 | + * |
|
| 185 | + * @return void |
|
| 186 | + */ |
|
| 187 | + public function move($source, $destination) { |
|
| 188 | + $statement = $this->connection->prepare( |
|
| 189 | + 'UPDATE `*PREFIX*properties` SET `propertypath` = ?' . |
|
| 190 | + ' WHERE `userid` = ? AND `propertypath` = ?' |
|
| 191 | + ); |
|
| 192 | + $statement->execute(array('/' . $destination, $this->user, '/' . $source)); |
|
| 193 | + $statement->closeCursor(); |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + /** |
|
| 197 | + * Returns a list of properties for this nodes.; |
|
| 198 | + * @param Node $node |
|
| 199 | + * @param array $requestedProperties requested properties or empty array for "all" |
|
| 200 | + * @return array |
|
| 201 | + * @note The properties list is a list of propertynames the client |
|
| 202 | + * requested, encoded as xmlnamespace#tagName, for example: |
|
| 203 | + * http://www.example.org/namespace#author If the array is empty, all |
|
| 204 | + * properties should be returned |
|
| 205 | + */ |
|
| 206 | + private function getProperties(Node $node, array $requestedProperties) { |
|
| 207 | + $path = $node->getPath(); |
|
| 208 | + if (isset($this->cache[$path])) { |
|
| 209 | + return $this->cache[$path]; |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + // TODO: chunking if more than 1000 properties |
|
| 213 | + $sql = 'SELECT * FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` = ?'; |
|
| 214 | + |
|
| 215 | + $whereValues = array($this->user, $path); |
|
| 216 | + $whereTypes = array(null, null); |
|
| 217 | + |
|
| 218 | + if (!empty($requestedProperties)) { |
|
| 219 | + // request only a subset |
|
| 220 | + $sql .= ' AND `propertyname` in (?)'; |
|
| 221 | + $whereValues[] = $requestedProperties; |
|
| 222 | + $whereTypes[] = \Doctrine\DBAL\Connection::PARAM_STR_ARRAY; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + $result = $this->connection->executeQuery( |
|
| 226 | + $sql, |
|
| 227 | + $whereValues, |
|
| 228 | + $whereTypes |
|
| 229 | + ); |
|
| 230 | + |
|
| 231 | + $props = []; |
|
| 232 | + while ($row = $result->fetch()) { |
|
| 233 | + $props[$row['propertyname']] = $row['propertyvalue']; |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + $result->closeCursor(); |
|
| 237 | + |
|
| 238 | + $this->cache[$path] = $props; |
|
| 239 | + return $props; |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + /** |
|
| 243 | + * Update properties |
|
| 244 | + * |
|
| 245 | + * @param Node $node node for which to update properties |
|
| 246 | + * @param array $properties array of properties to update |
|
| 247 | + * |
|
| 248 | + * @return bool |
|
| 249 | + */ |
|
| 250 | + private function updateProperties($node, $properties) { |
|
| 251 | + $path = $node->getPath(); |
|
| 252 | + |
|
| 253 | + $deleteStatement = 'DELETE FROM `*PREFIX*properties`' . |
|
| 254 | + ' WHERE `userid` = ? AND `propertypath` = ? AND `propertyname` = ?'; |
|
| 255 | + |
|
| 256 | + $insertStatement = 'INSERT INTO `*PREFIX*properties`' . |
|
| 257 | + ' (`userid`,`propertypath`,`propertyname`,`propertyvalue`) VALUES(?,?,?,?)'; |
|
| 258 | + |
|
| 259 | + $updateStatement = 'UPDATE `*PREFIX*properties` SET `propertyvalue` = ?' . |
|
| 260 | + ' WHERE `userid` = ? AND `propertypath` = ? AND `propertyname` = ?'; |
|
| 261 | + |
|
| 262 | + // TODO: use "insert or update" strategy ? |
|
| 263 | + $existing = $this->getProperties($node, array()); |
|
| 264 | + $this->connection->beginTransaction(); |
|
| 265 | + foreach ($properties as $propertyName => $propertyValue) { |
|
| 266 | + // If it was null, we need to delete the property |
|
| 267 | + if (is_null($propertyValue)) { |
|
| 268 | + if (array_key_exists($propertyName, $existing)) { |
|
| 269 | + $this->connection->executeUpdate($deleteStatement, |
|
| 270 | + array( |
|
| 271 | + $this->user, |
|
| 272 | + $path, |
|
| 273 | + $propertyName |
|
| 274 | + ) |
|
| 275 | + ); |
|
| 276 | + } |
|
| 277 | + } else { |
|
| 278 | + if (!array_key_exists($propertyName, $existing)) { |
|
| 279 | + $this->connection->executeUpdate($insertStatement, |
|
| 280 | + array( |
|
| 281 | + $this->user, |
|
| 282 | + $path, |
|
| 283 | + $propertyName, |
|
| 284 | + $propertyValue |
|
| 285 | + ) |
|
| 286 | + ); |
|
| 287 | + } else { |
|
| 288 | + $this->connection->executeUpdate($updateStatement, |
|
| 289 | + array( |
|
| 290 | + $propertyValue, |
|
| 291 | + $this->user, |
|
| 292 | + $path, |
|
| 293 | + $propertyName |
|
| 294 | + ) |
|
| 295 | + ); |
|
| 296 | + } |
|
| 297 | + } |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + $this->connection->commit(); |
|
| 301 | + unset($this->cache[$path]); |
|
| 302 | + |
|
| 303 | + return true; |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * Bulk load properties for directory children |
|
| 308 | + * |
|
| 309 | + * @param Directory $node |
|
| 310 | + * @param array $requestedProperties requested properties |
|
| 311 | + * |
|
| 312 | + * @return void |
|
| 313 | + */ |
|
| 314 | + private function loadChildrenProperties(Directory $node, $requestedProperties) { |
|
| 315 | + $path = $node->getPath(); |
|
| 316 | + if (isset($this->cache[$path])) { |
|
| 317 | + // we already loaded them at some point |
|
| 318 | + return; |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + $childNodes = $node->getChildren(); |
|
| 322 | + // pre-fill cache |
|
| 323 | + foreach ($childNodes as $childNode) { |
|
| 324 | + $this->cache[$childNode->getPath()] = []; |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + $sql = 'SELECT * FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` LIKE ?'; |
|
| 328 | + $sql .= ' AND `propertyname` in (?) ORDER BY `propertypath`, `propertyname`'; |
|
| 329 | + |
|
| 330 | + $result = $this->connection->executeQuery( |
|
| 331 | + $sql, |
|
| 332 | + array($this->user, $this->connection->escapeLikeParameter(rtrim($path, '/')) . '/%', $requestedProperties), |
|
| 333 | + array(null, null, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY) |
|
| 334 | + ); |
|
| 335 | + |
|
| 336 | + $oldPath = null; |
|
| 337 | + $props = []; |
|
| 338 | + while ($row = $result->fetch()) { |
|
| 339 | + $path = $row['propertypath']; |
|
| 340 | + if ($oldPath !== $path) { |
|
| 341 | + // save previously gathered props |
|
| 342 | + $this->cache[$oldPath] = $props; |
|
| 343 | + $oldPath = $path; |
|
| 344 | + // prepare props for next path |
|
| 345 | + $props = []; |
|
| 346 | + } |
|
| 347 | + $props[$row['propertyname']] = $row['propertyvalue']; |
|
| 348 | + } |
|
| 349 | + if (!is_null($oldPath)) { |
|
| 350 | + // save props from last run |
|
| 351 | + $this->cache[$oldPath] = $props; |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + $result->closeCursor(); |
|
| 355 | + } |
|
| 356 | 356 | |
| 357 | 357 | } |
@@ -232,6 +232,9 @@ |
||
| 232 | 232 | return '"' . $this->info->getEtag() . '"'; |
| 233 | 233 | } |
| 234 | 234 | |
| 235 | + /** |
|
| 236 | + * @param string $path |
|
| 237 | + */ |
|
| 235 | 238 | private function getPartFileBasePath($path) { |
| 236 | 239 | $partFileInStorage = \OC::$server->getConfig()->getSystemValue('part_file_in_storage', true); |
| 237 | 240 | if ($partFileInStorage) { |
@@ -57,512 +57,512 @@ |
||
| 57 | 57 | |
| 58 | 58 | class File extends Node implements IFile { |
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Updates the data |
|
| 62 | - * |
|
| 63 | - * The data argument is a readable stream resource. |
|
| 64 | - * |
|
| 65 | - * After a successful put operation, you may choose to return an ETag. The |
|
| 66 | - * etag must always be surrounded by double-quotes. These quotes must |
|
| 67 | - * appear in the actual string you're returning. |
|
| 68 | - * |
|
| 69 | - * Clients may use the ETag from a PUT request to later on make sure that |
|
| 70 | - * when they update the file, the contents haven't changed in the mean |
|
| 71 | - * time. |
|
| 72 | - * |
|
| 73 | - * If you don't plan to store the file byte-by-byte, and you return a |
|
| 74 | - * different object on a subsequent GET you are strongly recommended to not |
|
| 75 | - * return an ETag, and just return null. |
|
| 76 | - * |
|
| 77 | - * @param resource $data |
|
| 78 | - * |
|
| 79 | - * @throws Forbidden |
|
| 80 | - * @throws UnsupportedMediaType |
|
| 81 | - * @throws BadRequest |
|
| 82 | - * @throws Exception |
|
| 83 | - * @throws EntityTooLarge |
|
| 84 | - * @throws ServiceUnavailable |
|
| 85 | - * @throws FileLocked |
|
| 86 | - * @return string|null |
|
| 87 | - */ |
|
| 88 | - public function put($data) { |
|
| 89 | - try { |
|
| 90 | - $exists = $this->fileView->file_exists($this->path); |
|
| 91 | - if ($this->info && $exists && !$this->info->isUpdateable()) { |
|
| 92 | - throw new Forbidden(); |
|
| 93 | - } |
|
| 94 | - } catch (StorageNotAvailableException $e) { |
|
| 95 | - throw new ServiceUnavailable("File is not updatable: " . $e->getMessage()); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - // verify path of the target |
|
| 99 | - $this->verifyPath(); |
|
| 100 | - |
|
| 101 | - // chunked handling |
|
| 102 | - if (isset($_SERVER['HTTP_OC_CHUNKED'])) { |
|
| 103 | - try { |
|
| 104 | - return $this->createFileChunked($data); |
|
| 105 | - } catch (\Exception $e) { |
|
| 106 | - $this->convertToSabreException($e); |
|
| 107 | - } |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - list($partStorage) = $this->fileView->resolvePath($this->path); |
|
| 111 | - $needsPartFile = $this->needsPartFile($partStorage) && (strlen($this->path) > 1); |
|
| 112 | - |
|
| 113 | - if ($needsPartFile) { |
|
| 114 | - // mark file as partial while uploading (ignored by the scanner) |
|
| 115 | - $partFilePath = $this->getPartFileBasePath($this->path) . '.ocTransferId' . rand() . '.part'; |
|
| 116 | - } else { |
|
| 117 | - // upload file directly as the final path |
|
| 118 | - $partFilePath = $this->path; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - // the part file and target file might be on a different storage in case of a single file storage (e.g. single file share) |
|
| 122 | - /** @var \OC\Files\Storage\Storage $partStorage */ |
|
| 123 | - list($partStorage, $internalPartPath) = $this->fileView->resolvePath($partFilePath); |
|
| 124 | - /** @var \OC\Files\Storage\Storage $storage */ |
|
| 125 | - list($storage, $internalPath) = $this->fileView->resolvePath($this->path); |
|
| 126 | - try { |
|
| 127 | - $target = $partStorage->fopen($internalPartPath, 'wb'); |
|
| 128 | - if ($target === false) { |
|
| 129 | - \OCP\Util::writeLog('webdav', '\OC\Files\Filesystem::fopen() failed', \OCP\Util::ERROR); |
|
| 130 | - // because we have no clue about the cause we can only throw back a 500/Internal Server Error |
|
| 131 | - throw new Exception('Could not write file contents'); |
|
| 132 | - } |
|
| 133 | - list($count, $result) = \OC_Helper::streamCopy($data, $target); |
|
| 134 | - fclose($target); |
|
| 135 | - |
|
| 136 | - if ($result === false) { |
|
| 137 | - $expected = -1; |
|
| 138 | - if (isset($_SERVER['CONTENT_LENGTH'])) { |
|
| 139 | - $expected = $_SERVER['CONTENT_LENGTH']; |
|
| 140 | - } |
|
| 141 | - throw new Exception('Error while copying file to target location (copied bytes: ' . $count . ', expected filesize: ' . $expected . ' )'); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - // if content length is sent by client: |
|
| 145 | - // double check if the file was fully received |
|
| 146 | - // compare expected and actual size |
|
| 147 | - if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['REQUEST_METHOD'] === 'PUT') { |
|
| 148 | - $expected = $_SERVER['CONTENT_LENGTH']; |
|
| 149 | - if ($count != $expected) { |
|
| 150 | - throw new BadRequest('expected filesize ' . $expected . ' got ' . $count); |
|
| 151 | - } |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - } catch (\Exception $e) { |
|
| 155 | - if ($needsPartFile) { |
|
| 156 | - $partStorage->unlink($internalPartPath); |
|
| 157 | - } |
|
| 158 | - $this->convertToSabreException($e); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - try { |
|
| 162 | - $view = \OC\Files\Filesystem::getView(); |
|
| 163 | - if ($view) { |
|
| 164 | - $run = $this->emitPreHooks($exists); |
|
| 165 | - } else { |
|
| 166 | - $run = true; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - try { |
|
| 170 | - $this->changeLock(ILockingProvider::LOCK_EXCLUSIVE); |
|
| 171 | - } catch (LockedException $e) { |
|
| 172 | - if ($needsPartFile) { |
|
| 173 | - $partStorage->unlink($internalPartPath); |
|
| 174 | - } |
|
| 175 | - throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - if ($needsPartFile) { |
|
| 179 | - // rename to correct path |
|
| 180 | - try { |
|
| 181 | - if ($run) { |
|
| 182 | - $renameOkay = $storage->moveFromStorage($partStorage, $internalPartPath, $internalPath); |
|
| 183 | - $fileExists = $storage->file_exists($internalPath); |
|
| 184 | - } |
|
| 185 | - if (!$run || $renameOkay === false || $fileExists === false) { |
|
| 186 | - \OCP\Util::writeLog('webdav', 'renaming part file to final file failed', \OCP\Util::ERROR); |
|
| 187 | - throw new Exception('Could not rename part file to final file'); |
|
| 188 | - } |
|
| 189 | - } catch (ForbiddenException $ex) { |
|
| 190 | - throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry()); |
|
| 191 | - } catch (\Exception $e) { |
|
| 192 | - $partStorage->unlink($internalPartPath); |
|
| 193 | - $this->convertToSabreException($e); |
|
| 194 | - } |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - // since we skipped the view we need to scan and emit the hooks ourselves |
|
| 198 | - $storage->getUpdater()->update($internalPath); |
|
| 199 | - |
|
| 200 | - try { |
|
| 201 | - $this->changeLock(ILockingProvider::LOCK_SHARED); |
|
| 202 | - } catch (LockedException $e) { |
|
| 203 | - throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - if ($view) { |
|
| 207 | - $this->emitPostHooks($exists); |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - // allow sync clients to send the mtime along in a header |
|
| 211 | - $request = \OC::$server->getRequest(); |
|
| 212 | - if (isset($request->server['HTTP_X_OC_MTIME'])) { |
|
| 213 | - if ($this->fileView->touch($this->path, $request->server['HTTP_X_OC_MTIME'])) { |
|
| 214 | - header('X-OC-MTime: accepted'); |
|
| 215 | - } |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - $this->refreshInfo(); |
|
| 219 | - |
|
| 220 | - if (isset($request->server['HTTP_OC_CHECKSUM'])) { |
|
| 221 | - $checksum = trim($request->server['HTTP_OC_CHECKSUM']); |
|
| 222 | - $this->fileView->putFileInfo($this->path, ['checksum' => $checksum]); |
|
| 223 | - $this->refreshInfo(); |
|
| 224 | - } else if ($this->getChecksum() !== null && $this->getChecksum() !== '') { |
|
| 225 | - $this->fileView->putFileInfo($this->path, ['checksum' => '']); |
|
| 226 | - $this->refreshInfo(); |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - } catch (StorageNotAvailableException $e) { |
|
| 230 | - throw new ServiceUnavailable("Failed to check file size: " . $e->getMessage()); |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - return '"' . $this->info->getEtag() . '"'; |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - private function getPartFileBasePath($path) { |
|
| 237 | - $partFileInStorage = \OC::$server->getConfig()->getSystemValue('part_file_in_storage', true); |
|
| 238 | - if ($partFileInStorage) { |
|
| 239 | - return $path; |
|
| 240 | - } else { |
|
| 241 | - return md5($path); // will place it in the root of the view with a unique name |
|
| 242 | - } |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - /** |
|
| 246 | - * @param string $path |
|
| 247 | - */ |
|
| 248 | - private function emitPreHooks($exists, $path = null) { |
|
| 249 | - if (is_null($path)) { |
|
| 250 | - $path = $this->path; |
|
| 251 | - } |
|
| 252 | - $hookPath = Filesystem::getView()->getRelativePath($this->fileView->getAbsolutePath($path)); |
|
| 253 | - $run = true; |
|
| 254 | - |
|
| 255 | - if (!$exists) { |
|
| 256 | - \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_create, array( |
|
| 257 | - \OC\Files\Filesystem::signal_param_path => $hookPath, |
|
| 258 | - \OC\Files\Filesystem::signal_param_run => &$run, |
|
| 259 | - )); |
|
| 260 | - } else { |
|
| 261 | - \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_update, array( |
|
| 262 | - \OC\Files\Filesystem::signal_param_path => $hookPath, |
|
| 263 | - \OC\Files\Filesystem::signal_param_run => &$run, |
|
| 264 | - )); |
|
| 265 | - } |
|
| 266 | - \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_write, array( |
|
| 267 | - \OC\Files\Filesystem::signal_param_path => $hookPath, |
|
| 268 | - \OC\Files\Filesystem::signal_param_run => &$run, |
|
| 269 | - )); |
|
| 270 | - return $run; |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - /** |
|
| 274 | - * @param string $path |
|
| 275 | - */ |
|
| 276 | - private function emitPostHooks($exists, $path = null) { |
|
| 277 | - if (is_null($path)) { |
|
| 278 | - $path = $this->path; |
|
| 279 | - } |
|
| 280 | - $hookPath = Filesystem::getView()->getRelativePath($this->fileView->getAbsolutePath($path)); |
|
| 281 | - if (!$exists) { |
|
| 282 | - \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_post_create, array( |
|
| 283 | - \OC\Files\Filesystem::signal_param_path => $hookPath |
|
| 284 | - )); |
|
| 285 | - } else { |
|
| 286 | - \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_post_update, array( |
|
| 287 | - \OC\Files\Filesystem::signal_param_path => $hookPath |
|
| 288 | - )); |
|
| 289 | - } |
|
| 290 | - \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_post_write, array( |
|
| 291 | - \OC\Files\Filesystem::signal_param_path => $hookPath |
|
| 292 | - )); |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - /** |
|
| 296 | - * Returns the data |
|
| 297 | - * |
|
| 298 | - * @return resource |
|
| 299 | - * @throws Forbidden |
|
| 300 | - * @throws ServiceUnavailable |
|
| 301 | - */ |
|
| 302 | - public function get() { |
|
| 303 | - //throw exception if encryption is disabled but files are still encrypted |
|
| 304 | - try { |
|
| 305 | - $res = $this->fileView->fopen(ltrim($this->path, '/'), 'rb'); |
|
| 306 | - if ($res === false) { |
|
| 307 | - throw new ServiceUnavailable("Could not open file"); |
|
| 308 | - } |
|
| 309 | - return $res; |
|
| 310 | - } catch (GenericEncryptionException $e) { |
|
| 311 | - // returning 503 will allow retry of the operation at a later point in time |
|
| 312 | - throw new ServiceUnavailable("Encryption not ready: " . $e->getMessage()); |
|
| 313 | - } catch (StorageNotAvailableException $e) { |
|
| 314 | - throw new ServiceUnavailable("Failed to open file: " . $e->getMessage()); |
|
| 315 | - } catch (ForbiddenException $ex) { |
|
| 316 | - throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry()); |
|
| 317 | - } catch (LockedException $e) { |
|
| 318 | - throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 319 | - } |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - /** |
|
| 323 | - * Delete the current file |
|
| 324 | - * |
|
| 325 | - * @throws Forbidden |
|
| 326 | - * @throws ServiceUnavailable |
|
| 327 | - */ |
|
| 328 | - public function delete() { |
|
| 329 | - if (!$this->info->isDeletable()) { |
|
| 330 | - throw new Forbidden(); |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - try { |
|
| 334 | - if (!$this->fileView->unlink($this->path)) { |
|
| 335 | - // assume it wasn't possible to delete due to permissions |
|
| 336 | - throw new Forbidden(); |
|
| 337 | - } |
|
| 338 | - } catch (StorageNotAvailableException $e) { |
|
| 339 | - throw new ServiceUnavailable("Failed to unlink: " . $e->getMessage()); |
|
| 340 | - } catch (ForbiddenException $ex) { |
|
| 341 | - throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry()); |
|
| 342 | - } catch (LockedException $e) { |
|
| 343 | - throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 344 | - } |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - /** |
|
| 348 | - * Returns the mime-type for a file |
|
| 349 | - * |
|
| 350 | - * If null is returned, we'll assume application/octet-stream |
|
| 351 | - * |
|
| 352 | - * @return string |
|
| 353 | - */ |
|
| 354 | - public function getContentType() { |
|
| 355 | - $mimeType = $this->info->getMimetype(); |
|
| 356 | - |
|
| 357 | - // PROPFIND needs to return the correct mime type, for consistency with the web UI |
|
| 358 | - if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PROPFIND') { |
|
| 359 | - return $mimeType; |
|
| 360 | - } |
|
| 361 | - return \OC::$server->getMimeTypeDetector()->getSecureMimeType($mimeType); |
|
| 362 | - } |
|
| 363 | - |
|
| 364 | - /** |
|
| 365 | - * @return array|false |
|
| 366 | - */ |
|
| 367 | - public function getDirectDownload() { |
|
| 368 | - if (\OCP\App::isEnabled('encryption')) { |
|
| 369 | - return []; |
|
| 370 | - } |
|
| 371 | - /** @var \OCP\Files\Storage $storage */ |
|
| 372 | - list($storage, $internalPath) = $this->fileView->resolvePath($this->path); |
|
| 373 | - if (is_null($storage)) { |
|
| 374 | - return []; |
|
| 375 | - } |
|
| 376 | - |
|
| 377 | - return $storage->getDirectDownload($internalPath); |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - /** |
|
| 381 | - * @param resource $data |
|
| 382 | - * @return null|string |
|
| 383 | - * @throws Exception |
|
| 384 | - * @throws BadRequest |
|
| 385 | - * @throws NotImplemented |
|
| 386 | - * @throws ServiceUnavailable |
|
| 387 | - */ |
|
| 388 | - private function createFileChunked($data) { |
|
| 389 | - list($path, $name) = \Sabre\HTTP\URLUtil::splitPath($this->path); |
|
| 390 | - |
|
| 391 | - $info = \OC_FileChunking::decodeName($name); |
|
| 392 | - if (empty($info)) { |
|
| 393 | - throw new NotImplemented('Invalid chunk name'); |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - $chunk_handler = new \OC_FileChunking($info); |
|
| 397 | - $bytesWritten = $chunk_handler->store($info['index'], $data); |
|
| 398 | - |
|
| 399 | - //detect aborted upload |
|
| 400 | - if (isset ($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PUT') { |
|
| 401 | - if (isset($_SERVER['CONTENT_LENGTH'])) { |
|
| 402 | - $expected = $_SERVER['CONTENT_LENGTH']; |
|
| 403 | - if ($bytesWritten != $expected) { |
|
| 404 | - $chunk_handler->remove($info['index']); |
|
| 405 | - throw new BadRequest( |
|
| 406 | - 'expected filesize ' . $expected . ' got ' . $bytesWritten); |
|
| 407 | - } |
|
| 408 | - } |
|
| 409 | - } |
|
| 410 | - |
|
| 411 | - if ($chunk_handler->isComplete()) { |
|
| 412 | - list($storage,) = $this->fileView->resolvePath($path); |
|
| 413 | - $needsPartFile = $this->needsPartFile($storage); |
|
| 414 | - $partFile = null; |
|
| 415 | - |
|
| 416 | - $targetPath = $path . '/' . $info['name']; |
|
| 417 | - /** @var \OC\Files\Storage\Storage $targetStorage */ |
|
| 418 | - list($targetStorage, $targetInternalPath) = $this->fileView->resolvePath($targetPath); |
|
| 419 | - |
|
| 420 | - $exists = $this->fileView->file_exists($targetPath); |
|
| 421 | - |
|
| 422 | - try { |
|
| 423 | - $this->fileView->lockFile($targetPath, ILockingProvider::LOCK_SHARED); |
|
| 424 | - |
|
| 425 | - $this->emitPreHooks($exists, $targetPath); |
|
| 426 | - $this->fileView->changeLock($targetPath, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 427 | - /** @var \OC\Files\Storage\Storage $targetStorage */ |
|
| 428 | - list($targetStorage, $targetInternalPath) = $this->fileView->resolvePath($targetPath); |
|
| 429 | - |
|
| 430 | - if ($needsPartFile) { |
|
| 431 | - // we first assembly the target file as a part file |
|
| 432 | - $partFile = $this->getPartFileBasePath($path . '/' . $info['name']) . '.ocTransferId' . $info['transferid'] . '.part'; |
|
| 433 | - /** @var \OC\Files\Storage\Storage $targetStorage */ |
|
| 434 | - list($partStorage, $partInternalPath) = $this->fileView->resolvePath($partFile); |
|
| 435 | - |
|
| 436 | - |
|
| 437 | - $chunk_handler->file_assemble($partStorage, $partInternalPath); |
|
| 438 | - |
|
| 439 | - // here is the final atomic rename |
|
| 440 | - $renameOkay = $targetStorage->moveFromStorage($partStorage, $partInternalPath, $targetInternalPath); |
|
| 441 | - $fileExists = $targetStorage->file_exists($targetInternalPath); |
|
| 442 | - if ($renameOkay === false || $fileExists === false) { |
|
| 443 | - \OCP\Util::writeLog('webdav', '\OC\Files\Filesystem::rename() failed', \OCP\Util::ERROR); |
|
| 444 | - // only delete if an error occurred and the target file was already created |
|
| 445 | - if ($fileExists) { |
|
| 446 | - // set to null to avoid double-deletion when handling exception |
|
| 447 | - // stray part file |
|
| 448 | - $partFile = null; |
|
| 449 | - $targetStorage->unlink($targetInternalPath); |
|
| 450 | - } |
|
| 451 | - $this->fileView->changeLock($targetPath, ILockingProvider::LOCK_SHARED); |
|
| 452 | - throw new Exception('Could not rename part file assembled from chunks'); |
|
| 453 | - } |
|
| 454 | - } else { |
|
| 455 | - // assemble directly into the final file |
|
| 456 | - $chunk_handler->file_assemble($targetStorage, $targetInternalPath); |
|
| 457 | - } |
|
| 458 | - |
|
| 459 | - // allow sync clients to send the mtime along in a header |
|
| 460 | - $request = \OC::$server->getRequest(); |
|
| 461 | - if (isset($request->server['HTTP_X_OC_MTIME'])) { |
|
| 462 | - if ($targetStorage->touch($targetInternalPath, $request->server['HTTP_X_OC_MTIME'])) { |
|
| 463 | - header('X-OC-MTime: accepted'); |
|
| 464 | - } |
|
| 465 | - } |
|
| 466 | - |
|
| 467 | - // since we skipped the view we need to scan and emit the hooks ourselves |
|
| 468 | - $targetStorage->getUpdater()->update($targetInternalPath); |
|
| 469 | - |
|
| 470 | - $this->fileView->changeLock($targetPath, ILockingProvider::LOCK_SHARED); |
|
| 471 | - |
|
| 472 | - $this->emitPostHooks($exists, $targetPath); |
|
| 473 | - |
|
| 474 | - // FIXME: should call refreshInfo but can't because $this->path is not the of the final file |
|
| 475 | - $info = $this->fileView->getFileInfo($targetPath); |
|
| 476 | - |
|
| 477 | - if (isset($request->server['HTTP_OC_CHECKSUM'])) { |
|
| 478 | - $checksum = trim($request->server['HTTP_OC_CHECKSUM']); |
|
| 479 | - $this->fileView->putFileInfo($targetPath, ['checksum' => $checksum]); |
|
| 480 | - } else if ($info->getChecksum() !== null && $info->getChecksum() !== '') { |
|
| 481 | - $this->fileView->putFileInfo($this->path, ['checksum' => '']); |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - $this->fileView->unlockFile($targetPath, ILockingProvider::LOCK_SHARED); |
|
| 485 | - |
|
| 486 | - return $info->getEtag(); |
|
| 487 | - } catch (\Exception $e) { |
|
| 488 | - if ($partFile !== null) { |
|
| 489 | - $targetStorage->unlink($targetInternalPath); |
|
| 490 | - } |
|
| 491 | - $this->convertToSabreException($e); |
|
| 492 | - } |
|
| 493 | - } |
|
| 494 | - |
|
| 495 | - return null; |
|
| 496 | - } |
|
| 497 | - |
|
| 498 | - /** |
|
| 499 | - * Returns whether a part file is needed for the given storage |
|
| 500 | - * or whether the file can be assembled/uploaded directly on the |
|
| 501 | - * target storage. |
|
| 502 | - * |
|
| 503 | - * @param \OCP\Files\Storage $storage |
|
| 504 | - * @return bool true if the storage needs part file handling |
|
| 505 | - */ |
|
| 506 | - private function needsPartFile($storage) { |
|
| 507 | - // TODO: in the future use ChunkHandler provided by storage |
|
| 508 | - // and/or add method on Storage called "needsPartFile()" |
|
| 509 | - return !$storage->instanceOfStorage('OCA\Files_Sharing\External\Storage') && |
|
| 510 | - !$storage->instanceOfStorage('OC\Files\Storage\OwnCloud'); |
|
| 511 | - } |
|
| 512 | - |
|
| 513 | - /** |
|
| 514 | - * Convert the given exception to a SabreException instance |
|
| 515 | - * |
|
| 516 | - * @param \Exception $e |
|
| 517 | - * |
|
| 518 | - * @throws \Sabre\DAV\Exception |
|
| 519 | - */ |
|
| 520 | - private function convertToSabreException(\Exception $e) { |
|
| 521 | - if ($e instanceof \Sabre\DAV\Exception) { |
|
| 522 | - throw $e; |
|
| 523 | - } |
|
| 524 | - if ($e instanceof NotPermittedException) { |
|
| 525 | - // a more general case - due to whatever reason the content could not be written |
|
| 526 | - throw new Forbidden($e->getMessage(), 0, $e); |
|
| 527 | - } |
|
| 528 | - if ($e instanceof ForbiddenException) { |
|
| 529 | - // the path for the file was forbidden |
|
| 530 | - throw new DAVForbiddenException($e->getMessage(), $e->getRetry(), $e); |
|
| 531 | - } |
|
| 532 | - if ($e instanceof EntityTooLargeException) { |
|
| 533 | - // the file is too big to be stored |
|
| 534 | - throw new EntityTooLarge($e->getMessage(), 0, $e); |
|
| 535 | - } |
|
| 536 | - if ($e instanceof InvalidContentException) { |
|
| 537 | - // the file content is not permitted |
|
| 538 | - throw new UnsupportedMediaType($e->getMessage(), 0, $e); |
|
| 539 | - } |
|
| 540 | - if ($e instanceof InvalidPathException) { |
|
| 541 | - // the path for the file was not valid |
|
| 542 | - // TODO: find proper http status code for this case |
|
| 543 | - throw new Forbidden($e->getMessage(), 0, $e); |
|
| 544 | - } |
|
| 545 | - if ($e instanceof LockedException || $e instanceof LockNotAcquiredException) { |
|
| 546 | - // the file is currently being written to by another process |
|
| 547 | - throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 548 | - } |
|
| 549 | - if ($e instanceof GenericEncryptionException) { |
|
| 550 | - // returning 503 will allow retry of the operation at a later point in time |
|
| 551 | - throw new ServiceUnavailable('Encryption not ready: ' . $e->getMessage(), 0, $e); |
|
| 552 | - } |
|
| 553 | - if ($e instanceof StorageNotAvailableException) { |
|
| 554 | - throw new ServiceUnavailable('Failed to write file contents: ' . $e->getMessage(), 0, $e); |
|
| 555 | - } |
|
| 556 | - |
|
| 557 | - throw new \Sabre\DAV\Exception($e->getMessage(), 0, $e); |
|
| 558 | - } |
|
| 559 | - |
|
| 560 | - /** |
|
| 561 | - * Get the checksum for this file |
|
| 562 | - * |
|
| 563 | - * @return string |
|
| 564 | - */ |
|
| 565 | - public function getChecksum() { |
|
| 566 | - return $this->info->getChecksum(); |
|
| 567 | - } |
|
| 60 | + /** |
|
| 61 | + * Updates the data |
|
| 62 | + * |
|
| 63 | + * The data argument is a readable stream resource. |
|
| 64 | + * |
|
| 65 | + * After a successful put operation, you may choose to return an ETag. The |
|
| 66 | + * etag must always be surrounded by double-quotes. These quotes must |
|
| 67 | + * appear in the actual string you're returning. |
|
| 68 | + * |
|
| 69 | + * Clients may use the ETag from a PUT request to later on make sure that |
|
| 70 | + * when they update the file, the contents haven't changed in the mean |
|
| 71 | + * time. |
|
| 72 | + * |
|
| 73 | + * If you don't plan to store the file byte-by-byte, and you return a |
|
| 74 | + * different object on a subsequent GET you are strongly recommended to not |
|
| 75 | + * return an ETag, and just return null. |
|
| 76 | + * |
|
| 77 | + * @param resource $data |
|
| 78 | + * |
|
| 79 | + * @throws Forbidden |
|
| 80 | + * @throws UnsupportedMediaType |
|
| 81 | + * @throws BadRequest |
|
| 82 | + * @throws Exception |
|
| 83 | + * @throws EntityTooLarge |
|
| 84 | + * @throws ServiceUnavailable |
|
| 85 | + * @throws FileLocked |
|
| 86 | + * @return string|null |
|
| 87 | + */ |
|
| 88 | + public function put($data) { |
|
| 89 | + try { |
|
| 90 | + $exists = $this->fileView->file_exists($this->path); |
|
| 91 | + if ($this->info && $exists && !$this->info->isUpdateable()) { |
|
| 92 | + throw new Forbidden(); |
|
| 93 | + } |
|
| 94 | + } catch (StorageNotAvailableException $e) { |
|
| 95 | + throw new ServiceUnavailable("File is not updatable: " . $e->getMessage()); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + // verify path of the target |
|
| 99 | + $this->verifyPath(); |
|
| 100 | + |
|
| 101 | + // chunked handling |
|
| 102 | + if (isset($_SERVER['HTTP_OC_CHUNKED'])) { |
|
| 103 | + try { |
|
| 104 | + return $this->createFileChunked($data); |
|
| 105 | + } catch (\Exception $e) { |
|
| 106 | + $this->convertToSabreException($e); |
|
| 107 | + } |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + list($partStorage) = $this->fileView->resolvePath($this->path); |
|
| 111 | + $needsPartFile = $this->needsPartFile($partStorage) && (strlen($this->path) > 1); |
|
| 112 | + |
|
| 113 | + if ($needsPartFile) { |
|
| 114 | + // mark file as partial while uploading (ignored by the scanner) |
|
| 115 | + $partFilePath = $this->getPartFileBasePath($this->path) . '.ocTransferId' . rand() . '.part'; |
|
| 116 | + } else { |
|
| 117 | + // upload file directly as the final path |
|
| 118 | + $partFilePath = $this->path; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + // the part file and target file might be on a different storage in case of a single file storage (e.g. single file share) |
|
| 122 | + /** @var \OC\Files\Storage\Storage $partStorage */ |
|
| 123 | + list($partStorage, $internalPartPath) = $this->fileView->resolvePath($partFilePath); |
|
| 124 | + /** @var \OC\Files\Storage\Storage $storage */ |
|
| 125 | + list($storage, $internalPath) = $this->fileView->resolvePath($this->path); |
|
| 126 | + try { |
|
| 127 | + $target = $partStorage->fopen($internalPartPath, 'wb'); |
|
| 128 | + if ($target === false) { |
|
| 129 | + \OCP\Util::writeLog('webdav', '\OC\Files\Filesystem::fopen() failed', \OCP\Util::ERROR); |
|
| 130 | + // because we have no clue about the cause we can only throw back a 500/Internal Server Error |
|
| 131 | + throw new Exception('Could not write file contents'); |
|
| 132 | + } |
|
| 133 | + list($count, $result) = \OC_Helper::streamCopy($data, $target); |
|
| 134 | + fclose($target); |
|
| 135 | + |
|
| 136 | + if ($result === false) { |
|
| 137 | + $expected = -1; |
|
| 138 | + if (isset($_SERVER['CONTENT_LENGTH'])) { |
|
| 139 | + $expected = $_SERVER['CONTENT_LENGTH']; |
|
| 140 | + } |
|
| 141 | + throw new Exception('Error while copying file to target location (copied bytes: ' . $count . ', expected filesize: ' . $expected . ' )'); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + // if content length is sent by client: |
|
| 145 | + // double check if the file was fully received |
|
| 146 | + // compare expected and actual size |
|
| 147 | + if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['REQUEST_METHOD'] === 'PUT') { |
|
| 148 | + $expected = $_SERVER['CONTENT_LENGTH']; |
|
| 149 | + if ($count != $expected) { |
|
| 150 | + throw new BadRequest('expected filesize ' . $expected . ' got ' . $count); |
|
| 151 | + } |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + } catch (\Exception $e) { |
|
| 155 | + if ($needsPartFile) { |
|
| 156 | + $partStorage->unlink($internalPartPath); |
|
| 157 | + } |
|
| 158 | + $this->convertToSabreException($e); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + try { |
|
| 162 | + $view = \OC\Files\Filesystem::getView(); |
|
| 163 | + if ($view) { |
|
| 164 | + $run = $this->emitPreHooks($exists); |
|
| 165 | + } else { |
|
| 166 | + $run = true; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + try { |
|
| 170 | + $this->changeLock(ILockingProvider::LOCK_EXCLUSIVE); |
|
| 171 | + } catch (LockedException $e) { |
|
| 172 | + if ($needsPartFile) { |
|
| 173 | + $partStorage->unlink($internalPartPath); |
|
| 174 | + } |
|
| 175 | + throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + if ($needsPartFile) { |
|
| 179 | + // rename to correct path |
|
| 180 | + try { |
|
| 181 | + if ($run) { |
|
| 182 | + $renameOkay = $storage->moveFromStorage($partStorage, $internalPartPath, $internalPath); |
|
| 183 | + $fileExists = $storage->file_exists($internalPath); |
|
| 184 | + } |
|
| 185 | + if (!$run || $renameOkay === false || $fileExists === false) { |
|
| 186 | + \OCP\Util::writeLog('webdav', 'renaming part file to final file failed', \OCP\Util::ERROR); |
|
| 187 | + throw new Exception('Could not rename part file to final file'); |
|
| 188 | + } |
|
| 189 | + } catch (ForbiddenException $ex) { |
|
| 190 | + throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry()); |
|
| 191 | + } catch (\Exception $e) { |
|
| 192 | + $partStorage->unlink($internalPartPath); |
|
| 193 | + $this->convertToSabreException($e); |
|
| 194 | + } |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + // since we skipped the view we need to scan and emit the hooks ourselves |
|
| 198 | + $storage->getUpdater()->update($internalPath); |
|
| 199 | + |
|
| 200 | + try { |
|
| 201 | + $this->changeLock(ILockingProvider::LOCK_SHARED); |
|
| 202 | + } catch (LockedException $e) { |
|
| 203 | + throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + if ($view) { |
|
| 207 | + $this->emitPostHooks($exists); |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + // allow sync clients to send the mtime along in a header |
|
| 211 | + $request = \OC::$server->getRequest(); |
|
| 212 | + if (isset($request->server['HTTP_X_OC_MTIME'])) { |
|
| 213 | + if ($this->fileView->touch($this->path, $request->server['HTTP_X_OC_MTIME'])) { |
|
| 214 | + header('X-OC-MTime: accepted'); |
|
| 215 | + } |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + $this->refreshInfo(); |
|
| 219 | + |
|
| 220 | + if (isset($request->server['HTTP_OC_CHECKSUM'])) { |
|
| 221 | + $checksum = trim($request->server['HTTP_OC_CHECKSUM']); |
|
| 222 | + $this->fileView->putFileInfo($this->path, ['checksum' => $checksum]); |
|
| 223 | + $this->refreshInfo(); |
|
| 224 | + } else if ($this->getChecksum() !== null && $this->getChecksum() !== '') { |
|
| 225 | + $this->fileView->putFileInfo($this->path, ['checksum' => '']); |
|
| 226 | + $this->refreshInfo(); |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + } catch (StorageNotAvailableException $e) { |
|
| 230 | + throw new ServiceUnavailable("Failed to check file size: " . $e->getMessage()); |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + return '"' . $this->info->getEtag() . '"'; |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + private function getPartFileBasePath($path) { |
|
| 237 | + $partFileInStorage = \OC::$server->getConfig()->getSystemValue('part_file_in_storage', true); |
|
| 238 | + if ($partFileInStorage) { |
|
| 239 | + return $path; |
|
| 240 | + } else { |
|
| 241 | + return md5($path); // will place it in the root of the view with a unique name |
|
| 242 | + } |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + /** |
|
| 246 | + * @param string $path |
|
| 247 | + */ |
|
| 248 | + private function emitPreHooks($exists, $path = null) { |
|
| 249 | + if (is_null($path)) { |
|
| 250 | + $path = $this->path; |
|
| 251 | + } |
|
| 252 | + $hookPath = Filesystem::getView()->getRelativePath($this->fileView->getAbsolutePath($path)); |
|
| 253 | + $run = true; |
|
| 254 | + |
|
| 255 | + if (!$exists) { |
|
| 256 | + \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_create, array( |
|
| 257 | + \OC\Files\Filesystem::signal_param_path => $hookPath, |
|
| 258 | + \OC\Files\Filesystem::signal_param_run => &$run, |
|
| 259 | + )); |
|
| 260 | + } else { |
|
| 261 | + \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_update, array( |
|
| 262 | + \OC\Files\Filesystem::signal_param_path => $hookPath, |
|
| 263 | + \OC\Files\Filesystem::signal_param_run => &$run, |
|
| 264 | + )); |
|
| 265 | + } |
|
| 266 | + \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_write, array( |
|
| 267 | + \OC\Files\Filesystem::signal_param_path => $hookPath, |
|
| 268 | + \OC\Files\Filesystem::signal_param_run => &$run, |
|
| 269 | + )); |
|
| 270 | + return $run; |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + /** |
|
| 274 | + * @param string $path |
|
| 275 | + */ |
|
| 276 | + private function emitPostHooks($exists, $path = null) { |
|
| 277 | + if (is_null($path)) { |
|
| 278 | + $path = $this->path; |
|
| 279 | + } |
|
| 280 | + $hookPath = Filesystem::getView()->getRelativePath($this->fileView->getAbsolutePath($path)); |
|
| 281 | + if (!$exists) { |
|
| 282 | + \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_post_create, array( |
|
| 283 | + \OC\Files\Filesystem::signal_param_path => $hookPath |
|
| 284 | + )); |
|
| 285 | + } else { |
|
| 286 | + \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_post_update, array( |
|
| 287 | + \OC\Files\Filesystem::signal_param_path => $hookPath |
|
| 288 | + )); |
|
| 289 | + } |
|
| 290 | + \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_post_write, array( |
|
| 291 | + \OC\Files\Filesystem::signal_param_path => $hookPath |
|
| 292 | + )); |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + /** |
|
| 296 | + * Returns the data |
|
| 297 | + * |
|
| 298 | + * @return resource |
|
| 299 | + * @throws Forbidden |
|
| 300 | + * @throws ServiceUnavailable |
|
| 301 | + */ |
|
| 302 | + public function get() { |
|
| 303 | + //throw exception if encryption is disabled but files are still encrypted |
|
| 304 | + try { |
|
| 305 | + $res = $this->fileView->fopen(ltrim($this->path, '/'), 'rb'); |
|
| 306 | + if ($res === false) { |
|
| 307 | + throw new ServiceUnavailable("Could not open file"); |
|
| 308 | + } |
|
| 309 | + return $res; |
|
| 310 | + } catch (GenericEncryptionException $e) { |
|
| 311 | + // returning 503 will allow retry of the operation at a later point in time |
|
| 312 | + throw new ServiceUnavailable("Encryption not ready: " . $e->getMessage()); |
|
| 313 | + } catch (StorageNotAvailableException $e) { |
|
| 314 | + throw new ServiceUnavailable("Failed to open file: " . $e->getMessage()); |
|
| 315 | + } catch (ForbiddenException $ex) { |
|
| 316 | + throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry()); |
|
| 317 | + } catch (LockedException $e) { |
|
| 318 | + throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 319 | + } |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + /** |
|
| 323 | + * Delete the current file |
|
| 324 | + * |
|
| 325 | + * @throws Forbidden |
|
| 326 | + * @throws ServiceUnavailable |
|
| 327 | + */ |
|
| 328 | + public function delete() { |
|
| 329 | + if (!$this->info->isDeletable()) { |
|
| 330 | + throw new Forbidden(); |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + try { |
|
| 334 | + if (!$this->fileView->unlink($this->path)) { |
|
| 335 | + // assume it wasn't possible to delete due to permissions |
|
| 336 | + throw new Forbidden(); |
|
| 337 | + } |
|
| 338 | + } catch (StorageNotAvailableException $e) { |
|
| 339 | + throw new ServiceUnavailable("Failed to unlink: " . $e->getMessage()); |
|
| 340 | + } catch (ForbiddenException $ex) { |
|
| 341 | + throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry()); |
|
| 342 | + } catch (LockedException $e) { |
|
| 343 | + throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 344 | + } |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + /** |
|
| 348 | + * Returns the mime-type for a file |
|
| 349 | + * |
|
| 350 | + * If null is returned, we'll assume application/octet-stream |
|
| 351 | + * |
|
| 352 | + * @return string |
|
| 353 | + */ |
|
| 354 | + public function getContentType() { |
|
| 355 | + $mimeType = $this->info->getMimetype(); |
|
| 356 | + |
|
| 357 | + // PROPFIND needs to return the correct mime type, for consistency with the web UI |
|
| 358 | + if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PROPFIND') { |
|
| 359 | + return $mimeType; |
|
| 360 | + } |
|
| 361 | + return \OC::$server->getMimeTypeDetector()->getSecureMimeType($mimeType); |
|
| 362 | + } |
|
| 363 | + |
|
| 364 | + /** |
|
| 365 | + * @return array|false |
|
| 366 | + */ |
|
| 367 | + public function getDirectDownload() { |
|
| 368 | + if (\OCP\App::isEnabled('encryption')) { |
|
| 369 | + return []; |
|
| 370 | + } |
|
| 371 | + /** @var \OCP\Files\Storage $storage */ |
|
| 372 | + list($storage, $internalPath) = $this->fileView->resolvePath($this->path); |
|
| 373 | + if (is_null($storage)) { |
|
| 374 | + return []; |
|
| 375 | + } |
|
| 376 | + |
|
| 377 | + return $storage->getDirectDownload($internalPath); |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + /** |
|
| 381 | + * @param resource $data |
|
| 382 | + * @return null|string |
|
| 383 | + * @throws Exception |
|
| 384 | + * @throws BadRequest |
|
| 385 | + * @throws NotImplemented |
|
| 386 | + * @throws ServiceUnavailable |
|
| 387 | + */ |
|
| 388 | + private function createFileChunked($data) { |
|
| 389 | + list($path, $name) = \Sabre\HTTP\URLUtil::splitPath($this->path); |
|
| 390 | + |
|
| 391 | + $info = \OC_FileChunking::decodeName($name); |
|
| 392 | + if (empty($info)) { |
|
| 393 | + throw new NotImplemented('Invalid chunk name'); |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + $chunk_handler = new \OC_FileChunking($info); |
|
| 397 | + $bytesWritten = $chunk_handler->store($info['index'], $data); |
|
| 398 | + |
|
| 399 | + //detect aborted upload |
|
| 400 | + if (isset ($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PUT') { |
|
| 401 | + if (isset($_SERVER['CONTENT_LENGTH'])) { |
|
| 402 | + $expected = $_SERVER['CONTENT_LENGTH']; |
|
| 403 | + if ($bytesWritten != $expected) { |
|
| 404 | + $chunk_handler->remove($info['index']); |
|
| 405 | + throw new BadRequest( |
|
| 406 | + 'expected filesize ' . $expected . ' got ' . $bytesWritten); |
|
| 407 | + } |
|
| 408 | + } |
|
| 409 | + } |
|
| 410 | + |
|
| 411 | + if ($chunk_handler->isComplete()) { |
|
| 412 | + list($storage,) = $this->fileView->resolvePath($path); |
|
| 413 | + $needsPartFile = $this->needsPartFile($storage); |
|
| 414 | + $partFile = null; |
|
| 415 | + |
|
| 416 | + $targetPath = $path . '/' . $info['name']; |
|
| 417 | + /** @var \OC\Files\Storage\Storage $targetStorage */ |
|
| 418 | + list($targetStorage, $targetInternalPath) = $this->fileView->resolvePath($targetPath); |
|
| 419 | + |
|
| 420 | + $exists = $this->fileView->file_exists($targetPath); |
|
| 421 | + |
|
| 422 | + try { |
|
| 423 | + $this->fileView->lockFile($targetPath, ILockingProvider::LOCK_SHARED); |
|
| 424 | + |
|
| 425 | + $this->emitPreHooks($exists, $targetPath); |
|
| 426 | + $this->fileView->changeLock($targetPath, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 427 | + /** @var \OC\Files\Storage\Storage $targetStorage */ |
|
| 428 | + list($targetStorage, $targetInternalPath) = $this->fileView->resolvePath($targetPath); |
|
| 429 | + |
|
| 430 | + if ($needsPartFile) { |
|
| 431 | + // we first assembly the target file as a part file |
|
| 432 | + $partFile = $this->getPartFileBasePath($path . '/' . $info['name']) . '.ocTransferId' . $info['transferid'] . '.part'; |
|
| 433 | + /** @var \OC\Files\Storage\Storage $targetStorage */ |
|
| 434 | + list($partStorage, $partInternalPath) = $this->fileView->resolvePath($partFile); |
|
| 435 | + |
|
| 436 | + |
|
| 437 | + $chunk_handler->file_assemble($partStorage, $partInternalPath); |
|
| 438 | + |
|
| 439 | + // here is the final atomic rename |
|
| 440 | + $renameOkay = $targetStorage->moveFromStorage($partStorage, $partInternalPath, $targetInternalPath); |
|
| 441 | + $fileExists = $targetStorage->file_exists($targetInternalPath); |
|
| 442 | + if ($renameOkay === false || $fileExists === false) { |
|
| 443 | + \OCP\Util::writeLog('webdav', '\OC\Files\Filesystem::rename() failed', \OCP\Util::ERROR); |
|
| 444 | + // only delete if an error occurred and the target file was already created |
|
| 445 | + if ($fileExists) { |
|
| 446 | + // set to null to avoid double-deletion when handling exception |
|
| 447 | + // stray part file |
|
| 448 | + $partFile = null; |
|
| 449 | + $targetStorage->unlink($targetInternalPath); |
|
| 450 | + } |
|
| 451 | + $this->fileView->changeLock($targetPath, ILockingProvider::LOCK_SHARED); |
|
| 452 | + throw new Exception('Could not rename part file assembled from chunks'); |
|
| 453 | + } |
|
| 454 | + } else { |
|
| 455 | + // assemble directly into the final file |
|
| 456 | + $chunk_handler->file_assemble($targetStorage, $targetInternalPath); |
|
| 457 | + } |
|
| 458 | + |
|
| 459 | + // allow sync clients to send the mtime along in a header |
|
| 460 | + $request = \OC::$server->getRequest(); |
|
| 461 | + if (isset($request->server['HTTP_X_OC_MTIME'])) { |
|
| 462 | + if ($targetStorage->touch($targetInternalPath, $request->server['HTTP_X_OC_MTIME'])) { |
|
| 463 | + header('X-OC-MTime: accepted'); |
|
| 464 | + } |
|
| 465 | + } |
|
| 466 | + |
|
| 467 | + // since we skipped the view we need to scan and emit the hooks ourselves |
|
| 468 | + $targetStorage->getUpdater()->update($targetInternalPath); |
|
| 469 | + |
|
| 470 | + $this->fileView->changeLock($targetPath, ILockingProvider::LOCK_SHARED); |
|
| 471 | + |
|
| 472 | + $this->emitPostHooks($exists, $targetPath); |
|
| 473 | + |
|
| 474 | + // FIXME: should call refreshInfo but can't because $this->path is not the of the final file |
|
| 475 | + $info = $this->fileView->getFileInfo($targetPath); |
|
| 476 | + |
|
| 477 | + if (isset($request->server['HTTP_OC_CHECKSUM'])) { |
|
| 478 | + $checksum = trim($request->server['HTTP_OC_CHECKSUM']); |
|
| 479 | + $this->fileView->putFileInfo($targetPath, ['checksum' => $checksum]); |
|
| 480 | + } else if ($info->getChecksum() !== null && $info->getChecksum() !== '') { |
|
| 481 | + $this->fileView->putFileInfo($this->path, ['checksum' => '']); |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + $this->fileView->unlockFile($targetPath, ILockingProvider::LOCK_SHARED); |
|
| 485 | + |
|
| 486 | + return $info->getEtag(); |
|
| 487 | + } catch (\Exception $e) { |
|
| 488 | + if ($partFile !== null) { |
|
| 489 | + $targetStorage->unlink($targetInternalPath); |
|
| 490 | + } |
|
| 491 | + $this->convertToSabreException($e); |
|
| 492 | + } |
|
| 493 | + } |
|
| 494 | + |
|
| 495 | + return null; |
|
| 496 | + } |
|
| 497 | + |
|
| 498 | + /** |
|
| 499 | + * Returns whether a part file is needed for the given storage |
|
| 500 | + * or whether the file can be assembled/uploaded directly on the |
|
| 501 | + * target storage. |
|
| 502 | + * |
|
| 503 | + * @param \OCP\Files\Storage $storage |
|
| 504 | + * @return bool true if the storage needs part file handling |
|
| 505 | + */ |
|
| 506 | + private function needsPartFile($storage) { |
|
| 507 | + // TODO: in the future use ChunkHandler provided by storage |
|
| 508 | + // and/or add method on Storage called "needsPartFile()" |
|
| 509 | + return !$storage->instanceOfStorage('OCA\Files_Sharing\External\Storage') && |
|
| 510 | + !$storage->instanceOfStorage('OC\Files\Storage\OwnCloud'); |
|
| 511 | + } |
|
| 512 | + |
|
| 513 | + /** |
|
| 514 | + * Convert the given exception to a SabreException instance |
|
| 515 | + * |
|
| 516 | + * @param \Exception $e |
|
| 517 | + * |
|
| 518 | + * @throws \Sabre\DAV\Exception |
|
| 519 | + */ |
|
| 520 | + private function convertToSabreException(\Exception $e) { |
|
| 521 | + if ($e instanceof \Sabre\DAV\Exception) { |
|
| 522 | + throw $e; |
|
| 523 | + } |
|
| 524 | + if ($e instanceof NotPermittedException) { |
|
| 525 | + // a more general case - due to whatever reason the content could not be written |
|
| 526 | + throw new Forbidden($e->getMessage(), 0, $e); |
|
| 527 | + } |
|
| 528 | + if ($e instanceof ForbiddenException) { |
|
| 529 | + // the path for the file was forbidden |
|
| 530 | + throw new DAVForbiddenException($e->getMessage(), $e->getRetry(), $e); |
|
| 531 | + } |
|
| 532 | + if ($e instanceof EntityTooLargeException) { |
|
| 533 | + // the file is too big to be stored |
|
| 534 | + throw new EntityTooLarge($e->getMessage(), 0, $e); |
|
| 535 | + } |
|
| 536 | + if ($e instanceof InvalidContentException) { |
|
| 537 | + // the file content is not permitted |
|
| 538 | + throw new UnsupportedMediaType($e->getMessage(), 0, $e); |
|
| 539 | + } |
|
| 540 | + if ($e instanceof InvalidPathException) { |
|
| 541 | + // the path for the file was not valid |
|
| 542 | + // TODO: find proper http status code for this case |
|
| 543 | + throw new Forbidden($e->getMessage(), 0, $e); |
|
| 544 | + } |
|
| 545 | + if ($e instanceof LockedException || $e instanceof LockNotAcquiredException) { |
|
| 546 | + // the file is currently being written to by another process |
|
| 547 | + throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 548 | + } |
|
| 549 | + if ($e instanceof GenericEncryptionException) { |
|
| 550 | + // returning 503 will allow retry of the operation at a later point in time |
|
| 551 | + throw new ServiceUnavailable('Encryption not ready: ' . $e->getMessage(), 0, $e); |
|
| 552 | + } |
|
| 553 | + if ($e instanceof StorageNotAvailableException) { |
|
| 554 | + throw new ServiceUnavailable('Failed to write file contents: ' . $e->getMessage(), 0, $e); |
|
| 555 | + } |
|
| 556 | + |
|
| 557 | + throw new \Sabre\DAV\Exception($e->getMessage(), 0, $e); |
|
| 558 | + } |
|
| 559 | + |
|
| 560 | + /** |
|
| 561 | + * Get the checksum for this file |
|
| 562 | + * |
|
| 563 | + * @return string |
|
| 564 | + */ |
|
| 565 | + public function getChecksum() { |
|
| 566 | + return $this->info->getChecksum(); |
|
| 567 | + } |
|
| 568 | 568 | } |