| @@ -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); | 
| @@ -808,7 +808,9 @@ | ||
| 808 | 808 | $stmt = $query->execute(); | 
| 809 | 809 | $row = $stmt->fetch(\PDO::FETCH_ASSOC); | 
| 810 | 810 | |
| 811 | - if(!$row) return null; | |
| 811 | +		if(!$row) { | |
| 812 | + return null; | |
| 813 | + } | |
| 812 | 814 | |
| 813 | 815 | return [ | 
| 814 | 816 | 'id' => $row['id'], | 
| @@ -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 | } | 
| @@ -164,24 +164,24 @@ discard block | ||
| 164 | 164 | $stmt = $query->execute(); | 
| 165 | 165 | |
| 166 | 166 | $calendars = []; | 
| 167 | -		while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { | |
| 167 | +		while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { | |
| 168 | 168 | |
| 169 | 169 | $components = []; | 
| 170 | 170 |  			if ($row['components']) { | 
| 171 | -				$components = explode(',',$row['components']); | |
| 171 | +				$components = explode(',', $row['components']); | |
| 172 | 172 | } | 
| 173 | 173 | |
| 174 | 174 | $calendar = [ | 
| 175 | 175 | 'id' => $row['id'], | 
| 176 | 176 | 'uri' => $row['uri'], | 
| 177 | 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'), | |
| 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 | 182 | ]; | 
| 183 | 183 | |
| 184 | -			foreach($this->propertyMap as $xmlName=>$dbName) { | |
| 184 | +			foreach ($this->propertyMap as $xmlName=>$dbName) { | |
| 185 | 185 | $calendar[$xmlName] = $row[$dbName]; | 
| 186 | 186 | } | 
| 187 | 187 | |
| @@ -194,7 +194,7 @@ discard block | ||
| 194 | 194 | |
| 195 | 195 | // query for shared calendars | 
| 196 | 196 | $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); | 
| 197 | - $principals[]= $principalUri; | |
| 197 | + $principals[] = $principalUri; | |
| 198 | 198 | |
| 199 | 199 | $fields = array_values($this->propertyMap); | 
| 200 | 200 | $fields[] = 'a.id'; | 
| @@ -214,27 +214,27 @@ discard block | ||
| 214 | 214 |  			->setParameter('principaluri', $principals, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY) | 
| 215 | 215 | ->execute(); | 
| 216 | 216 | |
| 217 | -		while($row = $result->fetch()) { | |
| 217 | +		while ($row = $result->fetch()) { | |
| 218 | 218 | list(, $name) = URLUtil::splitPath($row['principaluri']); | 
| 219 | - $uri = $row['uri'] . '_shared_by_' . $name; | |
| 220 | - $row['displayname'] = $row['displayname'] . "($name)"; | |
| 219 | + $uri = $row['uri'].'_shared_by_'.$name; | |
| 220 | + $row['displayname'] = $row['displayname']."($name)"; | |
| 221 | 221 | $components = []; | 
| 222 | 222 |  			if ($row['components']) { | 
| 223 | -				$components = explode(',',$row['components']); | |
| 223 | +				$components = explode(',', $row['components']); | |
| 224 | 224 | } | 
| 225 | 225 | $calendar = [ | 
| 226 | 226 | 'id' => $row['id'], | 
| 227 | 227 | 'uri' => $uri, | 
| 228 | 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, | |
| 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 | 235 | ]; | 
| 236 | 236 | |
| 237 | -			foreach($this->propertyMap as $xmlName=>$dbName) { | |
| 237 | +			foreach ($this->propertyMap as $xmlName=>$dbName) { | |
| 238 | 238 | $calendar[$xmlName] = $row[$dbName]; | 
| 239 | 239 | } | 
| 240 | 240 | |
| @@ -277,20 +277,20 @@ discard block | ||
| 277 | 277 | |
| 278 | 278 | $components = []; | 
| 279 | 279 |  		if ($row['components']) { | 
| 280 | -			$components = explode(',',$row['components']); | |
| 280 | +			$components = explode(',', $row['components']); | |
| 281 | 281 | } | 
| 282 | 282 | |
| 283 | 283 | $calendar = [ | 
| 284 | 284 | 'id' => $row['id'], | 
| 285 | 285 | 'uri' => $row['uri'], | 
| 286 | 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'), | |
| 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 | 291 | ]; | 
| 292 | 292 | |
| 293 | -		foreach($this->propertyMap as $xmlName=>$dbName) { | |
| 293 | +		foreach ($this->propertyMap as $xmlName=>$dbName) { | |
| 294 | 294 | $calendar[$xmlName] = $row[$dbName]; | 
| 295 | 295 | } | 
| 296 | 296 | |
| @@ -321,20 +321,20 @@ discard block | ||
| 321 | 321 | |
| 322 | 322 | $components = []; | 
| 323 | 323 |  		if ($row['components']) { | 
| 324 | -			$components = explode(',',$row['components']); | |
| 324 | +			$components = explode(',', $row['components']); | |
| 325 | 325 | } | 
| 326 | 326 | |
| 327 | 327 | $calendar = [ | 
| 328 | 328 | 'id' => $row['id'], | 
| 329 | 329 | 'uri' => $row['uri'], | 
| 330 | 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'), | |
| 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 | 335 | ]; | 
| 336 | 336 | |
| 337 | -		foreach($this->propertyMap as $xmlName=>$dbName) { | |
| 337 | +		foreach ($this->propertyMap as $xmlName=>$dbName) { | |
| 338 | 338 | $calendar[$xmlName] = $row[$dbName]; | 
| 339 | 339 | } | 
| 340 | 340 | |
| @@ -366,16 +366,16 @@ discard block | ||
| 366 | 366 |  		$sccs = '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set'; | 
| 367 | 367 |  		if (isset($properties[$sccs])) { | 
| 368 | 368 |  			if (!($properties[$sccs] instanceof SupportedCalendarComponentSet)) { | 
| 369 | -				throw new DAV\Exception('The ' . $sccs . ' property must be of type: \Sabre\CalDAV\Property\SupportedCalendarComponentSet'); | |
| 369 | +				throw new DAV\Exception('The '.$sccs.' property must be of type: \Sabre\CalDAV\Property\SupportedCalendarComponentSet'); | |
| 370 | 370 | } | 
| 371 | -			$values['components'] = implode(',',$properties[$sccs]->getValue()); | |
| 371 | +			$values['components'] = implode(',', $properties[$sccs]->getValue()); | |
| 372 | 372 | } | 
| 373 | -		$transp = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; | |
| 373 | +		$transp = '{'.Plugin::NS_CALDAV.'}schedule-calendar-transp'; | |
| 374 | 374 |  		if (isset($properties[$transp])) { | 
| 375 | 375 | $values['transparent'] = (int) ($properties[$transp]->getValue() === 'transparent'); | 
| 376 | 376 | } | 
| 377 | 377 | |
| 378 | -		foreach($this->propertyMap as $xmlName=>$dbName) { | |
| 378 | +		foreach ($this->propertyMap as $xmlName=>$dbName) { | |
| 379 | 379 |  			if (isset($properties[$xmlName])) { | 
| 380 | 380 | $values[$dbName] = $properties[$xmlName]; | 
| 381 | 381 | } | 
| @@ -383,7 +383,7 @@ discard block | ||
| 383 | 383 | |
| 384 | 384 | $query = $this->db->getQueryBuilder(); | 
| 385 | 385 |  		$query->insert('calendars'); | 
| 386 | -		foreach($values as $column => $value) { | |
| 386 | +		foreach ($values as $column => $value) { | |
| 387 | 387 | $query->setValue($column, $query->createNamedParameter($value)); | 
| 388 | 388 | } | 
| 389 | 389 | $query->execute(); | 
| @@ -407,14 +407,14 @@ discard block | ||
| 407 | 407 | */ | 
| 408 | 408 |  	function updateCalendar($calendarId, PropPatch $propPatch) { | 
| 409 | 409 | $supportedProperties = array_keys($this->propertyMap); | 
| 410 | -		$supportedProperties[] = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; | |
| 410 | +		$supportedProperties[] = '{'.Plugin::NS_CALDAV.'}schedule-calendar-transp'; | |
| 411 | 411 | |
| 412 | 412 |  		$propPatch->handle($supportedProperties, function($mutations) use ($calendarId) { | 
| 413 | 413 | $newValues = []; | 
| 414 | 414 |  			foreach ($mutations as $propertyName => $propertyValue) { | 
| 415 | 415 | |
| 416 | 416 |  				switch ($propertyName) { | 
| 417 | -					case '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' : | |
| 417 | +					case '{'.Plugin::NS_CALDAV.'}schedule-calendar-transp' : | |
| 418 | 418 | $fieldName = 'transparent'; | 
| 419 | 419 | $newValues[$fieldName] = (int) ($propertyValue->getValue() === 'transparent'); | 
| 420 | 420 | break; | 
| @@ -497,16 +497,16 @@ discard block | ||
| 497 | 497 | $stmt = $query->execute(); | 
| 498 | 498 | |
| 499 | 499 | $result = []; | 
| 500 | -		foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) { | |
| 500 | +		foreach ($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) { | |
| 501 | 501 | $result[] = [ | 
| 502 | 502 | 'id' => $row['id'], | 
| 503 | 503 | 'uri' => $row['uri'], | 
| 504 | 504 | 'lastmodified' => $row['lastmodified'], | 
| 505 | - 'etag' => '"' . $row['etag'] . '"', | |
| 505 | + 'etag' => '"'.$row['etag'].'"', | |
| 506 | 506 | 'calendarid' => $row['calendarid'], | 
| 507 | - 'size' => (int)$row['size'], | |
| 507 | + 'size' => (int) $row['size'], | |
| 508 | 508 | 'component' => strtolower($row['componenttype']), | 
| 509 | - 'classification'=> (int)$row['classification'] | |
| 509 | + 'classification'=> (int) $row['classification'] | |
| 510 | 510 | ]; | 
| 511 | 511 | } | 
| 512 | 512 | |
| @@ -539,18 +539,18 @@ discard block | ||
| 539 | 539 | $stmt = $query->execute(); | 
| 540 | 540 | $row = $stmt->fetch(\PDO::FETCH_ASSOC); | 
| 541 | 541 | |
| 542 | - if(!$row) return null; | |
| 542 | + if (!$row) return null; | |
| 543 | 543 | |
| 544 | 544 | return [ | 
| 545 | 545 | 'id' => $row['id'], | 
| 546 | 546 | 'uri' => $row['uri'], | 
| 547 | 547 | 'lastmodified' => $row['lastmodified'], | 
| 548 | - 'etag' => '"' . $row['etag'] . '"', | |
| 548 | + 'etag' => '"'.$row['etag'].'"', | |
| 549 | 549 | 'calendarid' => $row['calendarid'], | 
| 550 | - 'size' => (int)$row['size'], | |
| 550 | + 'size' => (int) $row['size'], | |
| 551 | 551 | 'calendardata' => $this->readBlob($row['calendardata']), | 
| 552 | 552 | 'component' => strtolower($row['componenttype']), | 
| 553 | - 'classification'=> (int)$row['classification'] | |
| 553 | + 'classification'=> (int) $row['classification'] | |
| 554 | 554 | ]; | 
| 555 | 555 | } | 
| 556 | 556 | |
| @@ -589,12 +589,12 @@ discard block | ||
| 589 | 589 | 'id' => $row['id'], | 
| 590 | 590 | 'uri' => $row['uri'], | 
| 591 | 591 | 'lastmodified' => $row['lastmodified'], | 
| 592 | - 'etag' => '"' . $row['etag'] . '"', | |
| 592 | + 'etag' => '"'.$row['etag'].'"', | |
| 593 | 593 | 'calendarid' => $row['calendarid'], | 
| 594 | - 'size' => (int)$row['size'], | |
| 594 | + 'size' => (int) $row['size'], | |
| 595 | 595 | 'calendardata' => $this->readBlob($row['calendardata']), | 
| 596 | 596 | 'component' => strtolower($row['componenttype']), | 
| 597 | - 'classification' => (int)$row['classification'] | |
| 597 | + 'classification' => (int) $row['classification'] | |
| 598 | 598 | ]; | 
| 599 | 599 | } | 
| 600 | 600 | $result->closeCursor(); | 
| @@ -642,7 +642,7 @@ discard block | ||
| 642 | 642 | |
| 643 | 643 | $this->addChange($calendarId, $objectUri, 1); | 
| 644 | 644 | |
| 645 | - return '"' . $extraData['etag'] . '"'; | |
| 645 | + return '"'.$extraData['etag'].'"'; | |
| 646 | 646 | } | 
| 647 | 647 | |
| 648 | 648 | /** | 
| @@ -683,7 +683,7 @@ discard block | ||
| 683 | 683 | |
| 684 | 684 | $this->addChange($calendarId, $objectUri, 2); | 
| 685 | 685 | |
| 686 | - return '"' . $extraData['etag'] . '"'; | |
| 686 | + return '"'.$extraData['etag'].'"'; | |
| 687 | 687 | } | 
| 688 | 688 | |
| 689 | 689 | /** | 
| @@ -821,7 +821,7 @@ discard block | ||
| 821 | 821 | $stmt = $query->execute(); | 
| 822 | 822 | |
| 823 | 823 | $result = []; | 
| 824 | -		while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { | |
| 824 | +		while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { | |
| 825 | 825 |  			if ($requirePostFilter) { | 
| 826 | 826 |  				if (!$this->validateFilterForObject($row, $filters)) { | 
| 827 | 827 | continue; | 
| @@ -864,7 +864,7 @@ discard block | ||
| 864 | 864 | $stmt = $query->execute(); | 
| 865 | 865 | |
| 866 | 866 |  		if ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { | 
| 867 | - return $row['calendaruri'] . '/' . $row['objecturi']; | |
| 867 | + return $row['calendaruri'].'/'.$row['objecturi']; | |
| 868 | 868 | } | 
| 869 | 869 | |
| 870 | 870 | return null; | 
| @@ -929,7 +929,7 @@ discard block | ||
| 929 | 929 |  	function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null) { | 
| 930 | 930 | // Current synctoken | 
| 931 | 931 |  		$stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*calendars` WHERE `id` = ?'); | 
| 932 | - $stmt->execute([ $calendarId ]); | |
| 932 | + $stmt->execute([$calendarId]); | |
| 933 | 933 | $currentToken = $stmt->fetchColumn(0); | 
| 934 | 934 | |
| 935 | 935 |  		if (is_null($currentToken)) { | 
| @@ -946,8 +946,8 @@ discard block | ||
| 946 | 946 |  		if ($syncToken) { | 
| 947 | 947 | |
| 948 | 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; | |
| 949 | +			if ($limit > 0) { | |
| 950 | + $query .= " `LIMIT` ".(int) $limit; | |
| 951 | 951 | } | 
| 952 | 952 | |
| 953 | 953 | // Fetching all changes | 
| @@ -958,15 +958,15 @@ discard block | ||
| 958 | 958 | |
| 959 | 959 | // This loop ensures that any duplicates are overwritten, only the | 
| 960 | 960 | // last change on a node is relevant. | 
| 961 | -			while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { | |
| 961 | +			while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { | |
| 962 | 962 | |
| 963 | 963 | $changes[$row['uri']] = $row['operation']; | 
| 964 | 964 | |
| 965 | 965 | } | 
| 966 | 966 | |
| 967 | -			foreach($changes as $uri => $operation) { | |
| 967 | +			foreach ($changes as $uri => $operation) { | |
| 968 | 968 | |
| 969 | -				switch($operation) { | |
| 969 | +				switch ($operation) { | |
| 970 | 970 | case 1 : | 
| 971 | 971 | $result['added'][] = $uri; | 
| 972 | 972 | break; | 
| @@ -1036,10 +1036,10 @@ discard block | ||
| 1036 | 1036 |  			->from('calendarsubscriptions') | 
| 1037 | 1037 |  			->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) | 
| 1038 | 1038 |  			->orderBy('calendarorder', 'asc'); | 
| 1039 | - $stmt =$query->execute(); | |
| 1039 | + $stmt = $query->execute(); | |
| 1040 | 1040 | |
| 1041 | 1041 | $subscriptions = []; | 
| 1042 | -		while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { | |
| 1042 | +		while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { | |
| 1043 | 1043 | |
| 1044 | 1044 | $subscription = [ | 
| 1045 | 1045 | 'id' => $row['id'], | 
| @@ -1048,10 +1048,10 @@ discard block | ||
| 1048 | 1048 | 'source' => $row['source'], | 
| 1049 | 1049 | 'lastmodified' => $row['lastmodified'], | 
| 1050 | 1050 | |
| 1051 | -				'{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), | |
| 1051 | +				'{'.Plugin::NS_CALDAV.'}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), | |
| 1052 | 1052 | ]; | 
| 1053 | 1053 | |
| 1054 | -			foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) { | |
| 1054 | +			foreach ($this->subscriptionPropertyMap as $xmlName=>$dbName) { | |
| 1055 | 1055 |  				if (!is_null($row[$dbName])) { | 
| 1056 | 1056 | $subscription[$xmlName] = $row[$dbName]; | 
| 1057 | 1057 | } | 
| @@ -1090,7 +1090,7 @@ discard block | ||
| 1090 | 1090 | |
| 1091 | 1091 | $propertiesBoolean = ['striptodos', 'stripalarms', 'stripattachments']; | 
| 1092 | 1092 | |
| 1093 | -		foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) { | |
| 1093 | +		foreach ($this->subscriptionPropertyMap as $xmlName=>$dbName) { | |
| 1094 | 1094 |  			if (array_key_exists($xmlName, $properties)) { | 
| 1095 | 1095 | $values[$dbName] = $properties[$xmlName]; | 
| 1096 | 1096 |  					if (in_array($dbName, $propertiesBoolean)) { | 
| @@ -1138,7 +1138,7 @@ discard block | ||
| 1138 | 1138 | |
| 1139 | 1139 | $newValues = []; | 
| 1140 | 1140 | |
| 1141 | -			foreach($mutations as $propertyName=>$propertyValue) { | |
| 1141 | +			foreach ($mutations as $propertyName=>$propertyValue) { | |
| 1142 | 1142 |  				if ($propertyName === '{http://calendarserver.org/ns/}source') { | 
| 1143 | 1143 | $newValues['source'] = $propertyValue->getHref(); | 
| 1144 | 1144 |  				} else { | 
| @@ -1150,7 +1150,7 @@ discard block | ||
| 1150 | 1150 | $query = $this->db->getQueryBuilder(); | 
| 1151 | 1151 |  			$query->update('calendarsubscriptions') | 
| 1152 | 1152 |  				->set('lastmodified', $query->createNamedParameter(time())); | 
| 1153 | -			foreach($newValues as $fieldName=>$value) { | |
| 1153 | +			foreach ($newValues as $fieldName=>$value) { | |
| 1154 | 1154 | $query->set($fieldName, $query->createNamedParameter($value)); | 
| 1155 | 1155 | } | 
| 1156 | 1156 |  			$query->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) | 
| @@ -1200,7 +1200,7 @@ discard block | ||
| 1200 | 1200 | |
| 1201 | 1201 | $row = $stmt->fetch(\PDO::FETCH_ASSOC); | 
| 1202 | 1202 | |
| 1203 | -		if(!$row) { | |
| 1203 | +		if (!$row) { | |
| 1204 | 1204 | return null; | 
| 1205 | 1205 | } | 
| 1206 | 1206 | |
| @@ -1208,8 +1208,8 @@ discard block | ||
| 1208 | 1208 | 'uri' => $row['uri'], | 
| 1209 | 1209 | 'calendardata' => $row['calendardata'], | 
| 1210 | 1210 | 'lastmodified' => $row['lastmodified'], | 
| 1211 | - 'etag' => '"' . $row['etag'] . '"', | |
| 1212 | - 'size' => (int)$row['size'], | |
| 1211 | + 'etag' => '"'.$row['etag'].'"', | |
| 1212 | + 'size' => (int) $row['size'], | |
| 1213 | 1213 | ]; | 
| 1214 | 1214 | } | 
| 1215 | 1215 | |
| @@ -1232,13 +1232,13 @@ discard block | ||
| 1232 | 1232 | ->execute(); | 
| 1233 | 1233 | |
| 1234 | 1234 | $result = []; | 
| 1235 | -		foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) { | |
| 1235 | +		foreach ($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) { | |
| 1236 | 1236 | $result[] = [ | 
| 1237 | 1237 | 'calendardata' => $row['calendardata'], | 
| 1238 | 1238 | 'uri' => $row['uri'], | 
| 1239 | 1239 | 'lastmodified' => $row['lastmodified'], | 
| 1240 | - 'etag' => '"' . $row['etag'] . '"', | |
| 1241 | - 'size' => (int)$row['size'], | |
| 1240 | + 'etag' => '"'.$row['etag'].'"', | |
| 1241 | + 'size' => (int) $row['size'], | |
| 1242 | 1242 | ]; | 
| 1243 | 1243 | } | 
| 1244 | 1244 | |
| @@ -1330,10 +1330,10 @@ discard block | ||
| 1330 | 1330 | $lastOccurrence = null; | 
| 1331 | 1331 | $uid = null; | 
| 1332 | 1332 | $classification = self::CLASSIFICATION_PUBLIC; | 
| 1333 | -		foreach($vObject->getComponents() as $component) { | |
| 1334 | -			if ($component->name!=='VTIMEZONE') { | |
| 1333 | +		foreach ($vObject->getComponents() as $component) { | |
| 1334 | +			if ($component->name !== 'VTIMEZONE') { | |
| 1335 | 1335 | $componentType = $component->name; | 
| 1336 | - $uid = (string)$component->UID; | |
| 1336 | + $uid = (string) $component->UID; | |
| 1337 | 1337 | break; | 
| 1338 | 1338 | } | 
| 1339 | 1339 | } | 
| @@ -1358,13 +1358,13 @@ discard block | ||
| 1358 | 1358 | $lastOccurrence = $firstOccurrence; | 
| 1359 | 1359 | } | 
| 1360 | 1360 |  			} else { | 
| 1361 | - $it = new EventIterator($vObject, (string)$component->UID); | |
| 1361 | + $it = new EventIterator($vObject, (string) $component->UID); | |
| 1362 | 1362 | $maxDate = new \DateTime(self::MAX_DATE); | 
| 1363 | 1363 |  				if ($it->isInfinite()) { | 
| 1364 | 1364 | $lastOccurrence = $maxDate->getTimeStamp(); | 
| 1365 | 1365 |  				} else { | 
| 1366 | 1366 | $end = $it->getDtEnd(); | 
| 1367 | -					while($it->valid() && $end < $maxDate) { | |
| 1367 | +					while ($it->valid() && $end < $maxDate) { | |
| 1368 | 1368 | $end = $it->getDtEnd(); | 
| 1369 | 1369 | $it->next(); | 
| 1370 | 1370 | |
| @@ -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); | 
| @@ -745,7 +745,9 @@ | ||
| 745 | 745 | $stmt->execute([ $addressBookId ]); | 
| 746 | 746 | $currentToken = $stmt->fetchColumn(0); | 
| 747 | 747 | |
| 748 | - if (is_null($currentToken)) return null; | |
| 748 | +		if (is_null($currentToken)) { | |
| 749 | + return null; | |
| 750 | + } | |
| 749 | 751 | |
| 750 | 752 | $result = [ | 
| 751 | 753 | 'syncToken' => $currentToken, | 
| @@ -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 | } | 
| @@ -113,22 +113,22 @@ discard block | ||
| 113 | 113 | $addressBooks = []; | 
| 114 | 114 | |
| 115 | 115 | $result = $query->execute(); | 
| 116 | -		while($row = $result->fetch()) { | |
| 116 | +		while ($row = $result->fetch()) { | |
| 117 | 117 | $addressBooks[$row['id']] = [ | 
| 118 | 118 | 'id' => $row['id'], | 
| 119 | 119 | 'uri' => $row['uri'], | 
| 120 | 120 | 'principaluri' => $this->convertPrincipal($row['principaluri'], false), | 
| 121 | 121 |  				'{DAV:}displayname' => $row['displayname'], | 
| 122 | -				'{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], | |
| 122 | +				'{'.Plugin::NS_CARDDAV.'}addressbook-description' => $row['description'], | |
| 123 | 123 |  				'{http://calendarserver.org/ns/}getctag' => $row['synctoken'], | 
| 124 | -				'{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', | |
| 124 | +				'{http://sabredav.org/ns}sync-token' => $row['synctoken'] ? $row['synctoken'] : '0', | |
| 125 | 125 | ]; | 
| 126 | 126 | } | 
| 127 | 127 | $result->closeCursor(); | 
| 128 | 128 | |
| 129 | 129 | // query for shared calendars | 
| 130 | 130 | $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); | 
| 131 | - $principals[]= $principalUri; | |
| 131 | + $principals[] = $principalUri; | |
| 132 | 132 | |
| 133 | 133 | $query = $this->db->getQueryBuilder(); | 
| 134 | 134 | $result = $query->select(['a.id', 'a.uri', 'a.displayname', 'a.principaluri', 'a.description', 'a.synctoken', 's.access']) | 
| @@ -140,21 +140,21 @@ discard block | ||
| 140 | 140 |  			->setParameter('principaluri', $principals, IQueryBuilder::PARAM_STR_ARRAY) | 
| 141 | 141 | ->execute(); | 
| 142 | 142 | |
| 143 | -		while($row = $result->fetch()) { | |
| 143 | +		while ($row = $result->fetch()) { | |
| 144 | 144 | list(, $name) = URLUtil::splitPath($row['principaluri']); | 
| 145 | - $uri = $row['uri'] . '_shared_by_' . $name; | |
| 146 | - $displayName = $row['displayname'] . "($name)"; | |
| 145 | + $uri = $row['uri'].'_shared_by_'.$name; | |
| 146 | + $displayName = $row['displayname']."($name)"; | |
| 147 | 147 |  			if (!isset($addressBooks[$row['id']])) { | 
| 148 | 148 | $addressBooks[$row['id']] = [ | 
| 149 | 149 | 'id' => $row['id'], | 
| 150 | 150 | 'uri' => $uri, | 
| 151 | 151 | 'principaluri' => $principalUri, | 
| 152 | 152 |  					'{DAV:}displayname' => $displayName, | 
| 153 | -					'{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], | |
| 153 | +					'{'.Plugin::NS_CARDDAV.'}addressbook-description' => $row['description'], | |
| 154 | 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, | |
| 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 | 158 | ]; | 
| 159 | 159 | } | 
| 160 | 160 | } | 
| @@ -184,9 +184,9 @@ discard block | ||
| 184 | 184 | 'uri' => $row['uri'], | 
| 185 | 185 | 'principaluri' => $row['principaluri'], | 
| 186 | 186 |  			'{DAV:}displayname' => $row['displayname'], | 
| 187 | -			'{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], | |
| 187 | +			'{'.Plugin::NS_CARDDAV.'}addressbook-description' => $row['description'], | |
| 188 | 188 |  			'{http://calendarserver.org/ns/}getctag' => $row['synctoken'], | 
| 189 | -			'{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', | |
| 189 | +			'{http://sabredav.org/ns}sync-token' => $row['synctoken'] ? $row['synctoken'] : '0', | |
| 190 | 190 | ]; | 
| 191 | 191 | } | 
| 192 | 192 | |
| @@ -214,9 +214,9 @@ discard block | ||
| 214 | 214 | 'uri' => $row['uri'], | 
| 215 | 215 | 'principaluri' => $row['principaluri'], | 
| 216 | 216 |  				'{DAV:}displayname' => $row['displayname'], | 
| 217 | -				'{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], | |
| 217 | +				'{'.Plugin::NS_CARDDAV.'}addressbook-description' => $row['description'], | |
| 218 | 218 |  				'{http://calendarserver.org/ns/}getctag' => $row['synctoken'], | 
| 219 | -				'{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', | |
| 219 | +				'{http://sabredav.org/ns}sync-token' => $row['synctoken'] ? $row['synctoken'] : '0', | |
| 220 | 220 | ]; | 
| 221 | 221 | } | 
| 222 | 222 | |
| @@ -239,19 +239,19 @@ discard block | ||
| 239 | 239 |  	function updateAddressBook($addressBookId, \Sabre\DAV\PropPatch $propPatch) { | 
| 240 | 240 | $supportedProperties = [ | 
| 241 | 241 |  			'{DAV:}displayname', | 
| 242 | -			'{' . Plugin::NS_CARDDAV . '}addressbook-description', | |
| 242 | +			'{'.Plugin::NS_CARDDAV.'}addressbook-description', | |
| 243 | 243 | ]; | 
| 244 | 244 | |
| 245 | 245 |  		$propPatch->handle($supportedProperties, function($mutations) use ($addressBookId) { | 
| 246 | 246 | |
| 247 | 247 | $updates = []; | 
| 248 | -			foreach($mutations as $property=>$newValue) { | |
| 248 | +			foreach ($mutations as $property=>$newValue) { | |
| 249 | 249 | |
| 250 | -				switch($property) { | |
| 250 | +				switch ($property) { | |
| 251 | 251 |  					case '{DAV:}displayname' : | 
| 252 | 252 | $updates['displayname'] = $newValue; | 
| 253 | 253 | break; | 
| 254 | -					case '{' . Plugin::NS_CARDDAV . '}addressbook-description' : | |
| 254 | +					case '{'.Plugin::NS_CARDDAV.'}addressbook-description' : | |
| 255 | 255 | $updates['description'] = $newValue; | 
| 256 | 256 | break; | 
| 257 | 257 | } | 
| @@ -259,7 +259,7 @@ discard block | ||
| 259 | 259 | $query = $this->db->getQueryBuilder(); | 
| 260 | 260 |  			$query->update('addressbooks'); | 
| 261 | 261 | |
| 262 | -			foreach($updates as $key=>$value) { | |
| 262 | +			foreach ($updates as $key=>$value) { | |
| 263 | 263 | $query->set($key, $query->createNamedParameter($value)); | 
| 264 | 264 | } | 
| 265 | 265 |  			$query->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) | 
| @@ -290,24 +290,24 @@ discard block | ||
| 290 | 290 | 'synctoken' => 1 | 
| 291 | 291 | ]; | 
| 292 | 292 | |
| 293 | -		foreach($properties as $property=>$newValue) { | |
| 293 | +		foreach ($properties as $property=>$newValue) { | |
| 294 | 294 | |
| 295 | -			switch($property) { | |
| 295 | +			switch ($property) { | |
| 296 | 296 |  				case '{DAV:}displayname' : | 
| 297 | 297 | $values['displayname'] = $newValue; | 
| 298 | 298 | break; | 
| 299 | -				case '{' . Plugin::NS_CARDDAV . '}addressbook-description' : | |
| 299 | +				case '{'.Plugin::NS_CARDDAV.'}addressbook-description' : | |
| 300 | 300 | $values['description'] = $newValue; | 
| 301 | 301 | break; | 
| 302 | 302 | default : | 
| 303 | -					throw new BadRequest('Unknown property: ' . $property); | |
| 303 | +					throw new BadRequest('Unknown property: '.$property); | |
| 304 | 304 | } | 
| 305 | 305 | |
| 306 | 306 | } | 
| 307 | 307 | |
| 308 | 308 | // Fallback to make sure the displayname is set. Some clients may refuse | 
| 309 | 309 | // to work with addressbooks not having a displayname. | 
| 310 | -		if(is_null($values['displayname'])) { | |
| 310 | +		if (is_null($values['displayname'])) { | |
| 311 | 311 | $values['displayname'] = $url; | 
| 312 | 312 | } | 
| 313 | 313 | |
| @@ -385,8 +385,8 @@ discard block | ||
| 385 | 385 | $cards = []; | 
| 386 | 386 | |
| 387 | 387 | $result = $query->execute(); | 
| 388 | -		while($row = $result->fetch()) { | |
| 389 | - $row['etag'] = '"' . $row['etag'] . '"'; | |
| 388 | +		while ($row = $result->fetch()) { | |
| 389 | + $row['etag'] = '"'.$row['etag'].'"'; | |
| 390 | 390 | $row['carddata'] = $this->readBlob($row['carddata']); | 
| 391 | 391 | $cards[] = $row; | 
| 392 | 392 | } | 
| @@ -420,7 +420,7 @@ discard block | ||
| 420 | 420 |  		if (!$row) { | 
| 421 | 421 | return false; | 
| 422 | 422 | } | 
| 423 | - $row['etag'] = '"' . $row['etag'] . '"'; | |
| 423 | + $row['etag'] = '"'.$row['etag'].'"'; | |
| 424 | 424 | $row['carddata'] = $this->readBlob($row['carddata']); | 
| 425 | 425 | |
| 426 | 426 | return $row; | 
| @@ -457,7 +457,7 @@ discard block | ||
| 457 | 457 | $result = $query->execute(); | 
| 458 | 458 | |
| 459 | 459 |  			while ($row = $result->fetch()) { | 
| 460 | - $row['etag'] = '"' . $row['etag'] . '"'; | |
| 460 | + $row['etag'] = '"'.$row['etag'].'"'; | |
| 461 | 461 | $row['carddata'] = $this->readBlob($row['carddata']); | 
| 462 | 462 | $cards[] = $row; | 
| 463 | 463 | } | 
| @@ -517,7 +517,7 @@ discard block | ||
| 517 | 517 | 'cardData' => $cardData])); | 
| 518 | 518 | } | 
| 519 | 519 | |
| 520 | - return '"' . $etag . '"'; | |
| 520 | + return '"'.$etag.'"'; | |
| 521 | 521 | } | 
| 522 | 522 | |
| 523 | 523 | /** | 
| @@ -569,7 +569,7 @@ discard block | ||
| 569 | 569 | 'cardData' => $cardData])); | 
| 570 | 570 | } | 
| 571 | 571 | |
| 572 | - return '"' . $etag . '"'; | |
| 572 | + return '"'.$etag.'"'; | |
| 573 | 573 | } | 
| 574 | 574 | |
| 575 | 575 | /** | 
| @@ -669,7 +669,7 @@ discard block | ||
| 669 | 669 |  	function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel, $limit = null) { | 
| 670 | 670 | // Current synctoken | 
| 671 | 671 |  		$stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*addressbooks` WHERE `id` = ?'); | 
| 672 | - $stmt->execute([ $addressBookId ]); | |
| 672 | + $stmt->execute([$addressBookId]); | |
| 673 | 673 | $currentToken = $stmt->fetchColumn(0); | 
| 674 | 674 | |
| 675 | 675 | if (is_null($currentToken)) return null; | 
| @@ -684,8 +684,8 @@ discard block | ||
| 684 | 684 |  		if ($syncToken) { | 
| 685 | 685 | |
| 686 | 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; | |
| 687 | +			if ($limit > 0) { | |
| 688 | + $query .= " `LIMIT` ".(int) $limit; | |
| 689 | 689 | } | 
| 690 | 690 | |
| 691 | 691 | // Fetching all changes | 
| @@ -696,15 +696,15 @@ discard block | ||
| 696 | 696 | |
| 697 | 697 | // This loop ensures that any duplicates are overwritten, only the | 
| 698 | 698 | // last change on a node is relevant. | 
| 699 | -			while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { | |
| 699 | +			while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { | |
| 700 | 700 | |
| 701 | 701 | $changes[$row['uri']] = $row['operation']; | 
| 702 | 702 | |
| 703 | 703 | } | 
| 704 | 704 | |
| 705 | -			foreach($changes as $uri => $operation) { | |
| 705 | +			foreach ($changes as $uri => $operation) { | |
| 706 | 706 | |
| 707 | -				switch($operation) { | |
| 707 | +				switch ($operation) { | |
| 708 | 708 | case 1: | 
| 709 | 709 | $result['added'][] = $uri; | 
| 710 | 710 | break; | 
| @@ -784,7 +784,7 @@ discard block | ||
| 784 | 784 | $query2->orWhere( | 
| 785 | 785 | $query2->expr()->andX( | 
| 786 | 786 |  					$query2->expr()->eq('cp.name', $query->createNamedParameter($property)), | 
| 787 | -					$query2->expr()->ilike('cp.value', $query->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%')) | |
| 787 | +					$query2->expr()->ilike('cp.value', $query->createNamedParameter('%'.$this->db->escapeLikeParameter($pattern).'%')) | |
| 788 | 788 | ) | 
| 789 | 789 | ); | 
| 790 | 790 | } | 
| @@ -840,7 +840,7 @@ discard block | ||
| 840 | 840 | $result->closeCursor(); | 
| 841 | 841 | |
| 842 | 842 |  		if (!isset($uri['uri'])) { | 
| 843 | -			throw new \InvalidArgumentException('Card does not exists: ' . $id); | |
| 843 | +			throw new \InvalidArgumentException('Card does not exists: '.$id); | |
| 844 | 844 | } | 
| 845 | 845 | |
| 846 | 846 | return $uri['uri']; | 
| @@ -912,11 +912,11 @@ discard block | ||
| 912 | 912 | ); | 
| 913 | 913 | |
| 914 | 914 |  		foreach ($vCard->children as $property) { | 
| 915 | -			if(!in_array($property->name, self::$indexProperties)) { | |
| 915 | +			if (!in_array($property->name, self::$indexProperties)) { | |
| 916 | 916 | continue; | 
| 917 | 917 | } | 
| 918 | 918 | $preferred = 0; | 
| 919 | -			foreach($property->parameters as $parameter) { | |
| 919 | +			foreach ($property->parameters as $parameter) { | |
| 920 | 920 |  				if ($parameter->name == 'TYPE' && strtoupper($parameter->getValue()) == 'PREF') { | 
| 921 | 921 | $preferred = 1; | 
| 922 | 922 | break; | 
| @@ -971,10 +971,10 @@ discard block | ||
| 971 | 971 | $result->closeCursor(); | 
| 972 | 972 | |
| 973 | 973 |  		if (!isset($cardIds['id'])) { | 
| 974 | -			throw new \InvalidArgumentException('Card does not exists: ' . $uri); | |
| 974 | +			throw new \InvalidArgumentException('Card does not exists: '.$uri); | |
| 975 | 975 | } | 
| 976 | 976 | |
| 977 | - return (int)$cardIds['id']; | |
| 977 | + return (int) $cardIds['id']; | |
| 978 | 978 | } | 
| 979 | 979 | |
| 980 | 980 | /** | 
| @@ -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)) { | 
| @@ -90,7 +90,7 @@ discard block | ||
| 90 | 90 |  			if ($ex->getCode() === Http::STATUS_UNAUTHORIZED) { | 
| 91 | 91 | // remote server revoked access to the address book, remove it | 
| 92 | 92 | $this->backend->deleteAddressBook($addressBookId); | 
| 93 | -				$this->logger->info('Authorization failed, remove address book: ' . $url, ['app' => 'dav']); | |
| 93 | +				$this->logger->info('Authorization failed, remove address book: '.$url, ['app' => 'dav']); | |
| 94 | 94 | throw $ex; | 
| 95 | 95 | } | 
| 96 | 96 | } | 
| @@ -141,7 +141,7 @@ discard block | ||
| 141 | 141 | */ | 
| 142 | 142 |  	protected function requestSyncReport($url, $userName, $sharedSecret, $syncToken) { | 
| 143 | 143 | $settings = [ | 
| 144 | - 'baseUri' => $url . '/', | |
| 144 | + 'baseUri' => $url.'/', | |
| 145 | 145 | 'userName' => $userName, | 
| 146 | 146 | 'password' => $sharedSecret, | 
| 147 | 147 | ]; | 
| @@ -254,7 +254,7 @@ discard block | ||
| 254 | 254 | */ | 
| 255 | 255 |  	public function deleteUser($userOrCardId) { | 
| 256 | 256 | $systemAddressBook = $this->getLocalSystemAddressBook(); | 
| 257 | -		if ($userOrCardId instanceof IUser){ | |
| 257 | +		if ($userOrCardId instanceof IUser) { | |
| 258 | 258 | $name = $userOrCardId->getBackendClassName(); | 
| 259 | 259 | $userId = $userOrCardId->getUID(); | 
| 260 | 260 | |
| @@ -270,7 +270,7 @@ discard block | ||
| 270 | 270 |  		if (is_null($this->localSystemAddressBook)) { | 
| 271 | 271 | $systemPrincipal = "principals/system/system"; | 
| 272 | 272 | $this->localSystemAddressBook = $this->ensureSystemAddressBookExists($systemPrincipal, 'system', [ | 
| 273 | -				'{' . Plugin::NS_CARDDAV . '}addressbook-description' => 'System addressbook which holds all users of this instance' | |
| 273 | +				'{'.Plugin::NS_CARDDAV.'}addressbook-description' => 'System addressbook which holds all users of this instance' | |
| 274 | 274 | ]); | 
| 275 | 275 | } | 
| 276 | 276 | |
| @@ -288,7 +288,7 @@ discard block | ||
| 288 | 288 | |
| 289 | 289 | // remove no longer existing | 
| 290 | 290 | $allCards = $this->backend->getCards($systemAddressBook['id']); | 
| 291 | -		foreach($allCards as $card) { | |
| 291 | +		foreach ($allCards as $card) { | |
| 292 | 292 | $vCard = Reader::read($card['carddata']); | 
| 293 | 293 | $uid = $vCard->UID->getValue(); | 
| 294 | 294 | // load backend and see if user exists | 
| @@ -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 | } | 
| @@ -21,7 +21,6 @@ | ||
| 21 | 21 | namespace OCA\DAV\CardDAV\Xml; | 
| 22 | 22 | |
| 23 | 23 | use Sabre\Xml\XmlSerializable; | 
| 24 | -use Sabre\Xml\Element; | |
| 25 | 24 | use Sabre\Xml\Writer; | 
| 26 | 25 | |
| 27 | 26 |  class Groups implements XmlSerializable { | 
| @@ -26,21 +26,21 @@ | ||
| 26 | 26 | use Sabre\Xml\Writer; | 
| 27 | 27 | |
| 28 | 28 |  class Groups implements XmlSerializable { | 
| 29 | - const NS_OWNCLOUD = 'http://owncloud.org/ns'; | |
| 29 | + const NS_OWNCLOUD = 'http://owncloud.org/ns'; | |
| 30 | 30 | |
| 31 | - /** @var string[] of TYPE:CHECKSUM */ | |
| 32 | - private $groups; | |
| 31 | + /** @var string[] of TYPE:CHECKSUM */ | |
| 32 | + private $groups; | |
| 33 | 33 | |
| 34 | - /** | |
| 35 | - * @param string $groups | |
| 36 | - */ | |
| 37 | -	public function __construct($groups) { | |
| 38 | - $this->groups = $groups; | |
| 39 | - } | |
| 34 | + /** | |
| 35 | + * @param string $groups | |
| 36 | + */ | |
| 37 | +    public function __construct($groups) { | |
| 38 | + $this->groups = $groups; | |
| 39 | + } | |
| 40 | 40 | |
| 41 | -	function xmlSerialize(Writer $writer) { | |
| 42 | -		foreach ($this->groups as $group) { | |
| 43 | -			$writer->writeElement('{' . self::NS_OWNCLOUD . '}group', $group); | |
| 44 | - } | |
| 45 | - } | |
| 41 | +    function xmlSerialize(Writer $writer) { | |
| 42 | +        foreach ($this->groups as $group) { | |
| 43 | +            $writer->writeElement('{' . self::NS_OWNCLOUD . '}group', $group); | |
| 44 | + } | |
| 45 | + } | |
| 46 | 46 | } | 
| @@ -40,7 +40,7 @@ | ||
| 40 | 40 | |
| 41 | 41 |  	function xmlSerialize(Writer $writer) { | 
| 42 | 42 |  		foreach ($this->groups as $group) { | 
| 43 | -			$writer->writeElement('{' . self::NS_OWNCLOUD . '}group', $group); | |
| 43 | +			$writer->writeElement('{'.self::NS_OWNCLOUD.'}group', $group); | |
| 44 | 44 | } | 
| 45 | 45 | } | 
| 46 | 46 | } | 
| @@ -22,11 +22,6 @@ | ||
| 22 | 22 | namespace OCA\DAV\Command; | 
| 23 | 23 | |
| 24 | 24 | use OCA\DAV\CardDAV\CardDavBackend; | 
| 25 | -use OCA\DAV\Connector\Sabre\Principal; | |
| 26 | -use OCP\IConfig; | |
| 27 | -use OCP\IDBConnection; | |
| 28 | -use OCP\IGroupManager; | |
| 29 | -use OCP\ILogger; | |
| 30 | 25 | use OCP\IUserManager; | 
| 31 | 26 | use Symfony\Component\Console\Command\Command; | 
| 32 | 27 | use Symfony\Component\Console\Input\InputArgument; | 
| @@ -36,43 +36,43 @@ | ||
| 36 | 36 | |
| 37 | 37 |  class CreateAddressBook extends Command { | 
| 38 | 38 | |
| 39 | - /** @var IUserManager */ | |
| 40 | - private $userManager; | |
| 39 | + /** @var IUserManager */ | |
| 40 | + private $userManager; | |
| 41 | 41 | |
| 42 | - /** @var CardDavBackend */ | |
| 43 | - private $cardDavBackend; | |
| 42 | + /** @var CardDavBackend */ | |
| 43 | + private $cardDavBackend; | |
| 44 | 44 | |
| 45 | - /** | |
| 46 | - * @param IUserManager $userManager | |
| 47 | - * @param CardDavBackend $cardDavBackend | |
| 48 | - */ | |
| 49 | - function __construct(IUserManager $userManager, | |
| 50 | - CardDavBackend $cardDavBackend | |
| 51 | -	) { | |
| 52 | - parent::__construct(); | |
| 53 | - $this->userManager = $userManager; | |
| 54 | - $this->cardDavBackend = $cardDavBackend; | |
| 55 | - } | |
| 45 | + /** | |
| 46 | + * @param IUserManager $userManager | |
| 47 | + * @param CardDavBackend $cardDavBackend | |
| 48 | + */ | |
| 49 | + function __construct(IUserManager $userManager, | |
| 50 | + CardDavBackend $cardDavBackend | |
| 51 | +    ) { | |
| 52 | + parent::__construct(); | |
| 53 | + $this->userManager = $userManager; | |
| 54 | + $this->cardDavBackend = $cardDavBackend; | |
| 55 | + } | |
| 56 | 56 | |
| 57 | -	protected function configure() { | |
| 58 | - $this | |
| 59 | -				->setName('dav:create-addressbook') | |
| 60 | -				->setDescription('Create a dav addressbook') | |
| 61 | -				->addArgument('user', | |
| 62 | - InputArgument::REQUIRED, | |
| 63 | - 'User for whom the addressbook will be created') | |
| 64 | -				->addArgument('name', | |
| 65 | - InputArgument::REQUIRED, | |
| 66 | - 'Name of the addressbook'); | |
| 67 | - } | |
| 57 | +    protected function configure() { | |
| 58 | + $this | |
| 59 | +                ->setName('dav:create-addressbook') | |
| 60 | +                ->setDescription('Create a dav addressbook') | |
| 61 | +                ->addArgument('user', | |
| 62 | + InputArgument::REQUIRED, | |
| 63 | + 'User for whom the addressbook will be created') | |
| 64 | +                ->addArgument('name', | |
| 65 | + InputArgument::REQUIRED, | |
| 66 | + 'Name of the addressbook'); | |
| 67 | + } | |
| 68 | 68 | |
| 69 | -	protected function execute(InputInterface $input, OutputInterface $output) { | |
| 70 | -		$user = $input->getArgument('user'); | |
| 71 | -		if (!$this->userManager->userExists($user)) { | |
| 72 | -			throw new \InvalidArgumentException("User <$user> in unknown."); | |
| 73 | - } | |
| 69 | +    protected function execute(InputInterface $input, OutputInterface $output) { | |
| 70 | +        $user = $input->getArgument('user'); | |
| 71 | +        if (!$this->userManager->userExists($user)) { | |
| 72 | +            throw new \InvalidArgumentException("User <$user> in unknown."); | |
| 73 | + } | |
| 74 | 74 | |
| 75 | -		$name = $input->getArgument('name'); | |
| 76 | -		$this->cardDavBackend->createAddressBook("principals/users/$user", $name, []); | |
| 77 | - } | |
| 75 | +        $name = $input->getArgument('name'); | |
| 76 | +        $this->cardDavBackend->createAddressBook("principals/users/$user", $name, []); | |
| 77 | + } | |
| 78 | 78 | } | 
| @@ -21,10 +21,8 @@ | ||
| 21 | 21 | namespace OCA\DAV\Command; | 
| 22 | 22 | |
| 23 | 23 | use OCA\DAV\CardDAV\SyncService; | 
| 24 | -use OCP\IUserManager; | |
| 25 | 24 | use Symfony\Component\Console\Command\Command; | 
| 26 | 25 | use Symfony\Component\Console\Helper\ProgressBar; | 
| 27 | -use Symfony\Component\Console\Input\InputArgument; | |
| 28 | 26 | use Symfony\Component\Console\Input\InputInterface; | 
| 29 | 27 | use Symfony\Component\Console\Output\OutputInterface; | 
| 30 | 28 | |
| @@ -31,36 +31,36 @@ | ||
| 31 | 31 | |
| 32 | 32 |  class SyncSystemAddressBook extends Command { | 
| 33 | 33 | |
| 34 | - /** @var SyncService */ | |
| 35 | - private $syncService; | |
| 34 | + /** @var SyncService */ | |
| 35 | + private $syncService; | |
| 36 | 36 | |
| 37 | - /** | |
| 38 | - * @param SyncService $syncService | |
| 39 | - */ | |
| 40 | -	function __construct(SyncService $syncService) { | |
| 41 | - parent::__construct(); | |
| 42 | - $this->syncService = $syncService; | |
| 43 | - } | |
| 37 | + /** | |
| 38 | + * @param SyncService $syncService | |
| 39 | + */ | |
| 40 | +    function __construct(SyncService $syncService) { | |
| 41 | + parent::__construct(); | |
| 42 | + $this->syncService = $syncService; | |
| 43 | + } | |
| 44 | 44 | |
| 45 | -	protected function configure() { | |
| 46 | - $this | |
| 47 | -			->setName('dav:sync-system-addressbook') | |
| 48 | -			->setDescription('Synchronizes users to the system addressbook'); | |
| 49 | - } | |
| 45 | +    protected function configure() { | |
| 46 | + $this | |
| 47 | +            ->setName('dav:sync-system-addressbook') | |
| 48 | +            ->setDescription('Synchronizes users to the system addressbook'); | |
| 49 | + } | |
| 50 | 50 | |
| 51 | - /** | |
| 52 | - * @param InputInterface $input | |
| 53 | - * @param OutputInterface $output | |
| 54 | - */ | |
| 55 | -	protected function execute(InputInterface $input, OutputInterface $output) { | |
| 56 | -		$output->writeln('Syncing users ...'); | |
| 57 | - $progress = new ProgressBar($output); | |
| 58 | - $progress->start(); | |
| 59 | -		$this->syncService->syncInstance(function() use ($progress) { | |
| 60 | - $progress->advance(); | |
| 61 | - }); | |
| 51 | + /** | |
| 52 | + * @param InputInterface $input | |
| 53 | + * @param OutputInterface $output | |
| 54 | + */ | |
| 55 | +    protected function execute(InputInterface $input, OutputInterface $output) { | |
| 56 | +        $output->writeln('Syncing users ...'); | |
| 57 | + $progress = new ProgressBar($output); | |
| 58 | + $progress->start(); | |
| 59 | +        $this->syncService->syncInstance(function() use ($progress) { | |
| 60 | + $progress->advance(); | |
| 61 | + }); | |
| 62 | 62 | |
| 63 | - $progress->finish(); | |
| 64 | -		$output->writeln(''); | |
| 65 | - } | |
| 63 | + $progress->finish(); | |
| 64 | +        $output->writeln(''); | |
| 65 | + } | |
| 66 | 66 | } | 
| @@ -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 | } | 
| @@ -81,11 +81,11 @@ discard block | ||
| 81 | 81 | $this->logger = $logger; | 
| 82 | 82 | |
| 83 | 83 | $methods = get_class_methods($this->comment); | 
| 84 | -		$methods = array_filter($methods, function($name){ | |
| 84 | +		$methods = array_filter($methods, function($name) { | |
| 85 | 85 | return strpos($name, 'get') === 0; | 
| 86 | 86 | }); | 
| 87 | -		foreach($methods as $getter) { | |
| 88 | -			$name = '{'.self::NS_OWNCLOUD.'}' . lcfirst(substr($getter, 3)); | |
| 87 | +		foreach ($methods as $getter) { | |
| 88 | +			$name = '{'.self::NS_OWNCLOUD.'}'.lcfirst(substr($getter, 3)); | |
| 89 | 89 | $this->properties[$name] = $getter; | 
| 90 | 90 | } | 
| 91 | 91 | $this->userManager = $userManager; | 
| @@ -119,7 +119,7 @@ discard block | ||
| 119 | 119 | |
| 120 | 120 |  	protected function checkWriteAccessOnComment() { | 
| 121 | 121 | $user = $this->userSession->getUser(); | 
| 122 | - if( $this->comment->getActorType() !== 'users' | |
| 122 | + if ($this->comment->getActorType() !== 'users' | |
| 123 | 123 | || is_null($user) | 
| 124 | 124 | || $this->comment->getActorId() !== $user->getUID() | 
| 125 | 125 |  		) { | 
| @@ -183,9 +183,9 @@ discard block | ||
| 183 | 183 | return true; | 
| 184 | 184 |  		} catch (\Exception $e) { | 
| 185 | 185 | $this->logger->logException($e, ['app' => 'dav/comments']); | 
| 186 | -			if($e instanceof MessageTooLongException) { | |
| 186 | +			if ($e instanceof MessageTooLongException) { | |
| 187 | 187 | $msg = 'Message exceeds allowed character limit of '; | 
| 188 | - throw new BadRequest($msg . IComment::MAX_MESSAGE_LENGTH, 0, $e); | |
| 188 | + throw new BadRequest($msg.IComment::MAX_MESSAGE_LENGTH, 0, $e); | |
| 189 | 189 | } | 
| 190 | 190 | throw $e; | 
| 191 | 191 | } | 
| @@ -227,28 +227,28 @@ discard block | ||
| 227 | 227 | $properties = array_keys($this->properties); | 
| 228 | 228 | |
| 229 | 229 | $result = []; | 
| 230 | -		foreach($properties as $property) { | |
| 230 | +		foreach ($properties as $property) { | |
| 231 | 231 | $getter = $this->properties[$property]; | 
| 232 | -			if(method_exists($this->comment, $getter)) { | |
| 232 | +			if (method_exists($this->comment, $getter)) { | |
| 233 | 233 | $result[$property] = $this->comment->$getter(); | 
| 234 | 234 | } | 
| 235 | 235 | } | 
| 236 | 236 | |
| 237 | -		if($this->comment->getActorType() === 'users') { | |
| 237 | +		if ($this->comment->getActorType() === 'users') { | |
| 238 | 238 | $user = $this->userManager->get($this->comment->getActorId()); | 
| 239 | 239 | $displayName = is_null($user) ? null : $user->getDisplayName(); | 
| 240 | 240 | $result[self::PROPERTY_NAME_ACTOR_DISPLAYNAME] = $displayName; | 
| 241 | 241 | } | 
| 242 | 242 | |
| 243 | 243 | $unread = null; | 
| 244 | - $user = $this->userSession->getUser(); | |
| 245 | -		if(!is_null($user)) { | |
| 244 | + $user = $this->userSession->getUser(); | |
| 245 | +		if (!is_null($user)) { | |
| 246 | 246 | $readUntil = $this->commentsManager->getReadMark( | 
| 247 | 247 | $this->comment->getObjectType(), | 
| 248 | 248 | $this->comment->getObjectId(), | 
| 249 | 249 | $user | 
| 250 | 250 | ); | 
| 251 | -			if(is_null($readUntil)) { | |
| 251 | +			if (is_null($readUntil)) { | |
| 252 | 252 | $unread = 'true'; | 
| 253 | 253 |  			} else { | 
| 254 | 254 | $unread = $this->comment->getCreationDateTime() > $readUntil; | 
| @@ -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]; | 
| @@ -84,7 +84,7 @@ discard block | ||
| 84 | 84 | */ | 
| 85 | 85 |  	function initialize(Server $server) { | 
| 86 | 86 | $this->server = $server; | 
| 87 | -		if(strpos($this->server->getRequestUri(), 'comments/') !== 0) { | |
| 87 | +		if (strpos($this->server->getRequestUri(), 'comments/') !== 0) { | |
| 88 | 88 | return; | 
| 89 | 89 | } | 
| 90 | 90 | |
| @@ -124,7 +124,7 @@ discard block | ||
| 124 | 124 | // having their own comments marked as unread | 
| 125 | 125 | $node->setReadMarker(null); | 
| 126 | 126 | |
| 127 | - $url = rtrim($request->getUrl(), '/') . '/' . urlencode($comment->getId()); | |
| 127 | + $url = rtrim($request->getUrl(), '/').'/'.urlencode($comment->getId()); | |
| 128 | 128 | |
| 129 | 129 |  		$response->setHeader('Content-Location', $url); | 
| 130 | 130 | |
| @@ -157,7 +157,7 @@ discard block | ||
| 157 | 157 | */ | 
| 158 | 158 |  	public function onReport($reportName, $report, $uri) { | 
| 159 | 159 | $node = $this->server->tree->getNodeForPath($uri); | 
| 160 | -		if(!$node instanceof EntityCollection || $reportName !== self::REPORT_NAME) { | |
| 160 | +		if (!$node instanceof EntityCollection || $reportName !== self::REPORT_NAME) { | |
| 161 | 161 | throw new ReportNotSupported(); | 
| 162 | 162 | } | 
| 163 | 163 | $args = ['limit' => 0, 'offset' => 0, 'datetime' => null]; | 
| @@ -166,27 +166,27 @@ discard block | ||
| 166 | 166 | $this::REPORT_PARAM_OFFSET, | 
| 167 | 167 | $this::REPORT_PARAM_TIMESTAMP | 
| 168 | 168 | ]; | 
| 169 | -		$ns = '{' . $this::NS_OWNCLOUD . '}'; | |
| 170 | -		foreach($report as $parameter) { | |
| 171 | -			if(!in_array($parameter['name'], $acceptableParameters) || empty($parameter['value'])) { | |
| 169 | +		$ns = '{'.$this::NS_OWNCLOUD.'}'; | |
| 170 | +		foreach ($report as $parameter) { | |
| 171 | +			if (!in_array($parameter['name'], $acceptableParameters) || empty($parameter['value'])) { | |
| 172 | 172 | continue; | 
| 173 | 173 | } | 
| 174 | 174 | $args[str_replace($ns, '', $parameter['name'])] = $parameter['value']; | 
| 175 | 175 | } | 
| 176 | 176 | |
| 177 | -		if(!is_null($args['datetime'])) { | |
| 177 | +		if (!is_null($args['datetime'])) { | |
| 178 | 178 | $args['datetime'] = new \DateTime($args['datetime']); | 
| 179 | 179 | } | 
| 180 | 180 | |
| 181 | 181 | $results = $node->findChildren($args['limit'], $args['offset'], $args['datetime']); | 
| 182 | 182 | |
| 183 | 183 | $responses = []; | 
| 184 | -		foreach($results as $node) { | |
| 185 | - $nodePath = $this->server->getRequestUri() . '/' . $node->comment->getId(); | |
| 184 | +		foreach ($results as $node) { | |
| 185 | + $nodePath = $this->server->getRequestUri().'/'.$node->comment->getId(); | |
| 186 | 186 | $resultSet = $this->server->getPropertiesForPath($nodePath, CommentNode::getPropertyNames()); | 
| 187 | -			if(isset($resultSet[0]) && isset($resultSet[0][200])) { | |
| 187 | +			if (isset($resultSet[0]) && isset($resultSet[0][200])) { | |
| 188 | 188 | $responses[] = new Response( | 
| 189 | - $this->server->getBaseUri() . $nodePath, | |
| 189 | + $this->server->getBaseUri().$nodePath, | |
| 190 | 190 | [200 => $resultSet[0][200]], | 
| 191 | 191 | 200 | 
| 192 | 192 | ); | 
| @@ -227,14 +227,14 @@ discard block | ||
| 227 | 227 | |
| 228 | 228 | $actorType = $data['actorType']; | 
| 229 | 229 | $actorId = null; | 
| 230 | -		if($actorType === 'users') { | |
| 230 | +		if ($actorType === 'users') { | |
| 231 | 231 | $user = $this->userSession->getUser(); | 
| 232 | -			if(!is_null($user)) { | |
| 232 | +			if (!is_null($user)) { | |
| 233 | 233 | $actorId = $user->getUID(); | 
| 234 | 234 | } | 
| 235 | 235 | } | 
| 236 | -		if(is_null($actorId)) { | |
| 237 | -			throw new BadRequest('Invalid actor "' .  $actorType .'"'); | |
| 236 | +		if (is_null($actorId)) { | |
| 237 | +			throw new BadRequest('Invalid actor "'.$actorType.'"'); | |
| 238 | 238 | } | 
| 239 | 239 | |
| 240 | 240 |  		try { | 
| @@ -247,7 +247,7 @@ discard block | ||
| 247 | 247 |  			throw new BadRequest('Invalid input values', 0, $e); | 
| 248 | 248 |  		} catch (\OCP\Comments\MessageTooLongException $e) { | 
| 249 | 249 | $msg = 'Message exceeds allowed character limit of '; | 
| 250 | - throw new BadRequest($msg . \OCP\Comments\IComment::MAX_MESSAGE_LENGTH, 0, $e); | |
| 250 | + throw new BadRequest($msg.\OCP\Comments\IComment::MAX_MESSAGE_LENGTH, 0, $e); | |
| 251 | 251 | } | 
| 252 | 252 | } | 
| 253 | 253 | |
| @@ -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 | |
| @@ -41,7 +41,7 @@ discard block | ||
| 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 | 46 | /** @var string */ | 
| 47 | 47 | protected $id; | 
| @@ -65,10 +65,10 @@ discard block | ||
| 65 | 65 | IUserSession $userSession, | 
| 66 | 66 | ILogger $logger | 
| 67 | 67 |  	) { | 
| 68 | -		foreach(['id', 'name'] as $property) { | |
| 68 | +		foreach (['id', 'name'] as $property) { | |
| 69 | 69 | $$property = trim($$property); | 
| 70 | -			if(empty($$property) || !is_string($$property)) { | |
| 71 | -				throw new \InvalidArgumentException('"' . $property . '" parameter must be non-empty string'); | |
| 70 | +			if (empty($$property) || !is_string($$property)) { | |
| 71 | +				throw new \InvalidArgumentException('"'.$property.'" parameter must be non-empty string'); | |
| 72 | 72 | } | 
| 73 | 73 | } | 
| 74 | 74 | $this->id = $id; | 
| @@ -134,7 +134,7 @@ discard block | ||
| 134 | 134 |  	function findChildren($limit = 0, $offset = 0, \DateTime $datetime = null) { | 
| 135 | 135 | $comments = $this->commentsManager->getForObject($this->name, $this->id, $limit, $offset, $datetime); | 
| 136 | 136 | $result = []; | 
| 137 | -		foreach($comments as $comment) { | |
| 137 | +		foreach ($comments as $comment) { | |
| 138 | 138 | $result[] = new CommentNode( | 
| 139 | 139 | $this->commentsManager, | 
| 140 | 140 | $comment, | 
| @@ -187,7 +187,7 @@ discard block | ||
| 187 | 187 |  	function getProperties($properties) { | 
| 188 | 188 | $marker = null; | 
| 189 | 189 | $user = $this->userSession->getUser(); | 
| 190 | -		if(!is_null($user)) { | |
| 190 | +		if (!is_null($user)) { | |
| 191 | 191 | $marker = $this->commentsManager->getReadMark($this->name, $this->id, $user); | 
| 192 | 192 | } | 
| 193 | 193 | return [self::PROPERTY_NAME_READ_MARKER => $marker]; |