Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions apps/dav/appinfo/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,11 @@ function(GenericEvent $event) use ($app) {
$app->setupContactsProvider($cm, $user->getUID());
}
});

$calendarManager = \OC::$server->getCalendarManager();
$calendarManager->register(function() use ($calendarManager, $app) {
$user = \OC::$server->getUserSession()->getUser();
if ($user !== null) {
$app->setupCalendarProvider($calendarManager, $user->getUID());
}
});
2 changes: 2 additions & 0 deletions apps/dav/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
'OCA\\DAV\\CalDAV\\CalDavBackend' => $baseDir . '/../lib/CalDAV/CalDavBackend.php',
'OCA\\DAV\\CalDAV\\Calendar' => $baseDir . '/../lib/CalDAV/Calendar.php',
'OCA\\DAV\\CalDAV\\CalendarHome' => $baseDir . '/../lib/CalDAV/CalendarHome.php',
'OCA\\DAV\\CalDAV\\CalendarImpl' => $baseDir . '/../lib/CalDAV/CalendarImpl.php',
'OCA\\DAV\\CalDAV\\CalendarManager' => $baseDir . '/../lib/CalDAV/CalendarManager.php',
'OCA\\DAV\\CalDAV\\CalendarObject' => $baseDir . '/../lib/CalDAV/CalendarObject.php',
'OCA\\DAV\\CalDAV\\CalendarRoot' => $baseDir . '/../lib/CalDAV/CalendarRoot.php',
'OCA\\DAV\\CalDAV\\Plugin' => $baseDir . '/../lib/CalDAV/Plugin.php',
Expand Down
2 changes: 2 additions & 0 deletions apps/dav/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class ComposerStaticInitDAV
'OCA\\DAV\\CalDAV\\CalDavBackend' => __DIR__ . '/..' . '/../lib/CalDAV/CalDavBackend.php',
'OCA\\DAV\\CalDAV\\Calendar' => __DIR__ . '/..' . '/../lib/CalDAV/Calendar.php',
'OCA\\DAV\\CalDAV\\CalendarHome' => __DIR__ . '/..' . '/../lib/CalDAV/CalendarHome.php',
'OCA\\DAV\\CalDAV\\CalendarImpl' => __DIR__ . '/..' . '/../lib/CalDAV/CalendarImpl.php',
'OCA\\DAV\\CalDAV\\CalendarManager' => __DIR__ . '/..' . '/../lib/CalDAV/CalendarManager.php',
'OCA\\DAV\\CalDAV\\CalendarObject' => __DIR__ . '/..' . '/../lib/CalDAV/CalendarObject.php',
'OCA\\DAV\\CalDAV\\CalendarRoot' => __DIR__ . '/..' . '/../lib/CalDAV/CalendarRoot.php',
'OCA\\DAV\\CalDAV\\Plugin' => __DIR__ . '/..' . '/../lib/CalDAV/Plugin.php',
Expand Down
17 changes: 14 additions & 3 deletions apps/dav/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
use OCA\DAV\CalDAV\Activity\Backend;
use OCA\DAV\CalDAV\Activity\Provider\Event;
use OCA\DAV\CalDAV\BirthdayService;
use OCA\DAV\CalDAV\CalendarManager;
use OCA\DAV\Capabilities;
use OCA\DAV\CardDAV\ContactsManager;
use OCA\DAV\CardDAV\PhotoCache;
use OCA\DAV\CardDAV\SyncService;
use OCA\DAV\HookManager;
use \OCP\AppFramework\App;
use OCP\Contacts\IManager;
use OCP\Contacts\IManager as IContactsManager;
use OCP\Calendar\IManager as ICalendarManager;
use OCP\IUser;
use Symfony\Component\EventDispatcher\GenericEvent;

Expand Down Expand Up @@ -63,16 +65,25 @@ public function __construct() {
}

/**
* @param IManager $contactsManager
* @param IContactsManager $contactsManager
* @param string $userID
*/
public function setupContactsProvider(IManager $contactsManager, $userID) {
public function setupContactsProvider(IContactsManager $contactsManager, $userID) {
/** @var ContactsManager $cm */
$cm = $this->getContainer()->query(ContactsManager::class);
$urlGenerator = $this->getContainer()->getServer()->getURLGenerator();
$cm->setupContactsProvider($contactsManager, $userID, $urlGenerator);
}

/**
* @param ICalendarManager $calendarManager
* @param string $userId
*/
public function setupCalendarProvider(ICalendarManager $calendarManager, $userId) {
$cm = $this->getContainer()->query(CalendarManager::class);
$cm->setupCalendarProvider($calendarManager, $userId);
}

public function registerHooks() {
/** @var HookManager $hm */
$hm = $this->getContainer()->query(HookManager::class);
Expand Down
171 changes: 171 additions & 0 deletions apps/dav/lib/CalDAV/CalDavBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@
use Sabre\DAV\Exception\NotFound;
use Sabre\DAV\PropPatch;
use Sabre\HTTP\URLUtil;
use Sabre\VObject\Component;
use Sabre\VObject\Component\VCalendar;
use Sabre\VObject\Component\VEvent;
use Sabre\VObject\Component\VTimeZone;
use Sabre\VObject\DateTimeParser;
use Sabre\VObject\Property;
use Sabre\VObject\Reader;
use Sabre\VObject\Recur\EventIterator;
use Sabre\Uri;
Expand Down Expand Up @@ -1344,6 +1348,173 @@ public function calendarSearch($principalUri, array $filters, $limit=null, $offs
return $result;
}

/**
* used for Nextcloud's calendar API
*
* @param array $calendarInfo
* @param string $pattern
* @param array $searchProperties
* @param array $options
* @param integer|null $limit
* @param integer|null $offset
*
* @return array
*/
public function search(array $calendarInfo, $pattern, array $searchProperties,
array $options, $limit, $offset) {
$outerQuery = $this->db->getQueryBuilder();
$innerQuery = $this->db->getQueryBuilder();

$innerQuery->selectDistinct('op.objectid')
->from($this->dbObjectPropertiesTable, 'op')
->andWhere($innerQuery->expr()->eq('op.calendarid',
$outerQuery->createNamedParameter($calendarInfo['id'])));

// only return public items for shared calendars for now
if ($calendarInfo['principaluri'] !== $calendarInfo['{http://owncloud.org/ns}owner-principal']) {
$innerQuery->andWhere($innerQuery->expr()->eq('c.classification',
$outerQuery->createNamedParameter(self::CLASSIFICATION_PUBLIC)));
}

$or = $innerQuery->expr()->orX();
foreach($searchProperties as $searchProperty) {
$or->add($innerQuery->expr()->eq('op.name',
$outerQuery->createNamedParameter($searchProperty)));
}
$innerQuery->andWhere($or);

if ($pattern !== '') {
$innerQuery->andWhere($innerQuery->expr()->iLike('op.value',
$outerQuery->createNamedParameter('%' .
$this->db->escapeLikeParameter($pattern) . '%')));
}

$outerQuery->select('c.id', 'c.calendardata', 'c.componenttype', 'c.uid', 'c.uri')
->from('calendarobjects', 'c');

if (isset($options['timerange'])) {
if (isset($options['timerange']['start'])) {
$outerQuery->andWhere($outerQuery->expr()->gt('lastoccurence',
$outerQuery->createNamedParameter($options['timerange']['start']->getTimeStamp)));

}
if (isset($options['timerange']['end'])) {
$outerQuery->andWhere($outerQuery->expr()->lt('firstoccurence',
$outerQuery->createNamedParameter($options['timerange']['end']->getTimeStamp)));
}
}

if (isset($options['types'])) {
$or = $outerQuery->expr()->orX();
foreach($options['types'] as $type) {
$or->add($outerQuery->expr()->eq('componenttype',
$outerQuery->createNamedParameter($type)));
}
$outerQuery->andWhere($or);
}

$outerQuery->andWhere($outerQuery->expr()->in('c.id',
$outerQuery->createFunction($innerQuery->getSQL())));

if ($offset) {
$outerQuery->setFirstResult($offset);
}
if ($limit) {
$outerQuery->setMaxResults($limit);
}

$result = $outerQuery->execute();
$calendarObjects = $result->fetchAll();

return array_map(function($o) {
$calendarData = Reader::read($o['calendardata']);
$comps = $calendarData->getComponents();
$objects = [];
$timezones = [];
foreach($comps as $comp) {
if ($comp instanceof VTimeZone) {
$timezones[] = $comp;
} else {
$objects[] = $comp;
}
}

return [
'id' => $o['id'],
'type' => $o['componenttype'],
'uid' => $o['uid'],
'uri' => $o['uri'],
'objects' => array_map(function($c) {
return $this->transformSearchData($c);
}, $objects),
'timezones' => array_map(function($c) {
return $this->transformSearchData($c);
}, $timezones),
];
}, $calendarObjects);
}

/**
* @param Component $comp
* @return array
*/
private function transformSearchData(Component $comp) {
$data = [];
/** @var Component[] $subComponents */
$subComponents = $comp->getComponents();
/** @var Property[] $properties */
$properties = array_filter($comp->children(), function($c) {
return $c instanceof Property;
});
$validationRules = $comp->getValidationRules();

foreach($subComponents as $subComponent) {
$name = $subComponent->name;
if (!isset($data[$name])) {
$data[$name] = [];
}
$data[$name][] = $this->transformSearchData($subComponent);
}

foreach($properties as $property) {
$name = $property->name;
if (!isset($validationRules[$name])) {
$validationRules[$name] = '*';
}

$rule = $validationRules[$property->name];
if ($rule === '+' || $rule === '*') { // multiple
if (!isset($data[$name])) {
$data[$name] = [];
}

$data[$name][] = $this->transformSearchProperty($property);
} else { // once
$data[$name] = $this->transformSearchProperty($property);
}
}

return $data;
}

/**
* @param Property $prop
* @return array
*/
private function transformSearchProperty(Property $prop) {
// No need to check Date, as it extends DateTime
if ($prop instanceof Property\ICalendar\DateTime) {
$value = $prop->getDateTime();
} else {
$value = $prop->getValue();
}

return [
$value,
$prop->parameters()
];
}

/**
* Searches through all of a users calendars and calendar objects to find
* an object with a specific UID.
Expand Down
119 changes: 119 additions & 0 deletions apps/dav/lib/CalDAV/CalendarImpl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php
/**
* @copyright 2017, Georg Ehrke <[email protected]>
*
* @author Georg Ehrke <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\DAV\CalDAV;

use OCP\Constants;
use OCP\Calendar\ICalendar;

class CalendarImpl implements ICalendar {

/** @var CalDavBackend */
private $backend;

/** @var Calendar */
private $calendar;

/** @var array */
private $calendarInfo;

/**
* CalendarImpl constructor.
*
* @param Calendar $calendar
* @param array $calendarInfo
* @param CalDavBackend $backend
*/
public function __construct(Calendar $calendar, array $calendarInfo,
CalDavBackend $backend) {
$this->calendar = $calendar;
$this->calendarInfo = $calendarInfo;
$this->backend = $backend;
}

/**
* @return string defining the technical unique key
* @since 13.0.0
*/
public function getKey() {
return $this->calendarInfo['id'];
}

/**
* In comparison to getKey() this function returns a human readable (maybe translated) name
* @return null|string
* @since 13.0.0
*/
public function getDisplayName() {
return $this->calendarInfo['{DAV:}displayname'];
}

/**
* Calendar color
* @return null|string
* @since 13.0.0
*/
public function getDisplayColor() {
return $this->calendarInfo['{http://apple.com/ns/ical/}calendar-color'];
}

/**
* @param string $pattern which should match within the $searchProperties
* @param array $searchProperties defines the properties within the query pattern should match
* @param array $options - optional parameters:
* ['timerange' => ['start' => new DateTime(...), 'end' => new DateTime(...)]]
* @param integer|null $limit - limit number of search results
* @param integer|null $offset - offset for paging of search results
* @return array an array of events/journals/todos which are arrays of key-value-pairs
* @since 13.0.0
*/
public function search($pattern, array $searchProperties=[], array $options=[], $limit=null, $offset=null) {
return $this->backend->search($this->calendarInfo, $pattern,
$searchProperties, $options, $limit, $offset);
}

/**
* @return integer build up using \OCP\Constants
* @since 13.0.0
*/
public function getPermissions() {
$permissions = $this->calendar->getACL();
$result = 0;
foreach ($permissions as $permission) {
switch($permission['privilege']) {
case '{DAV:}read':
$result |= Constants::PERMISSION_READ;
break;
case '{DAV:}write':
$result |= Constants::PERMISSION_CREATE;
$result |= Constants::PERMISSION_UPDATE;
break;
case '{DAV:}all':
$result |= Constants::PERMISSION_ALL;
break;
}
}

return $result;
}
}
Loading