Passed
Push — master ( 62403d...0c3e2f )
by Joas
14:50 queued 14s
created
apps/comments/lib/AppInfo/Application.php 1 patch
Indentation   +39 added lines, -39 removed lines patch added patch discarded remove patch
@@ -42,55 +42,55 @@
 block discarded – undo
42 42
 
43 43
 class Application extends App {
44 44
 
45
-	const APP_ID = 'comments';
45
+    const APP_ID = 'comments';
46 46
 
47
-	public function __construct (array $urlParams = []) {
48
-		parent::__construct(self::APP_ID, $urlParams);
49
-		$container = $this->getContainer();
47
+    public function __construct (array $urlParams = []) {
48
+        parent::__construct(self::APP_ID, $urlParams);
49
+        $container = $this->getContainer();
50 50
 
51
-		$container->registerAlias('NotificationsController', Notifications::class);
51
+        $container->registerAlias('NotificationsController', Notifications::class);
52 52
 
53
-		$jsSettingsHelper = new JSSettingsHelper($container->getServer());
54
-		Util::connectHook('\OCP\Config', 'js', $jsSettingsHelper, 'extend');
53
+        $jsSettingsHelper = new JSSettingsHelper($container->getServer());
54
+        Util::connectHook('\OCP\Config', 'js', $jsSettingsHelper, 'extend');
55 55
 
56
-		$this->register();
57
-	}
56
+        $this->register();
57
+    }
58 58
 
59
-	private function register() {
60
-		$server = $this->getContainer()->getServer();
59
+    private function register() {
60
+        $server = $this->getContainer()->getServer();
61 61
 
62
-		/** @var IEventDispatcher $newDispatcher */
63
-		$dispatcher = $server->query(IEventDispatcher::class);
62
+        /** @var IEventDispatcher $newDispatcher */
63
+        $dispatcher = $server->query(IEventDispatcher::class);
64 64
 
65
-		$this->registerEventsScripts($dispatcher);
66
-		$this->registerDavEntity($dispatcher);
67
-		$this->registerNotifier();
68
-		$this->registerCommentsEventHandler();
65
+        $this->registerEventsScripts($dispatcher);
66
+        $this->registerDavEntity($dispatcher);
67
+        $this->registerNotifier();
68
+        $this->registerCommentsEventHandler();
69 69
 
70
-		$server->getSearch()->registerProvider(Provider::class, ['apps' => ['files']]);
71
-	}
70
+        $server->getSearch()->registerProvider(Provider::class, ['apps' => ['files']]);
71
+    }
72 72
 
73
-	protected function registerEventsScripts(IEventDispatcher $dispatcher) {
74
-		$dispatcher->addServiceListener(LoadAdditionalScriptsEvent::class, LoadAdditionalScripts::class);
75
-		$dispatcher->addServiceListener(LoadSidebar::class, LoadSidebarScripts::class);
76
-	}
73
+    protected function registerEventsScripts(IEventDispatcher $dispatcher) {
74
+        $dispatcher->addServiceListener(LoadAdditionalScriptsEvent::class, LoadAdditionalScripts::class);
75
+        $dispatcher->addServiceListener(LoadSidebar::class, LoadSidebarScripts::class);
76
+    }
77 77
 
78
-	protected function registerDavEntity(IEventDispatcher $dispatcher) {
79
-		$dispatcher->addListener(CommentsEntityEvent::EVENT_ENTITY, function(CommentsEntityEvent $event) {
80
-			$event->addEntityCollection('files', function($name) {
81
-				$nodes = \OC::$server->getUserFolder()->getById((int)$name);
82
-				return !empty($nodes);
83
-			});
84
-		});
85
-	}
78
+    protected function registerDavEntity(IEventDispatcher $dispatcher) {
79
+        $dispatcher->addListener(CommentsEntityEvent::EVENT_ENTITY, function(CommentsEntityEvent $event) {
80
+            $event->addEntityCollection('files', function($name) {
81
+                $nodes = \OC::$server->getUserFolder()->getById((int)$name);
82
+                return !empty($nodes);
83
+            });
84
+        });
85
+    }
86 86
 
87
-	protected function registerNotifier() {
88
-		$this->getContainer()->getServer()->getNotificationManager()->registerNotifierService(Notifier::class);
89
-	}
87
+    protected function registerNotifier() {
88
+        $this->getContainer()->getServer()->getNotificationManager()->registerNotifierService(Notifier::class);
89
+    }
90 90
 
91
-	protected function registerCommentsEventHandler() {
92
-		$this->getContainer()->getServer()->getCommentsManager()->registerEventHandler(function () {
93
-			return $this->getContainer()->query(EventHandler::class);
94
-		});
95
-	}
91
+    protected function registerCommentsEventHandler() {
92
+        $this->getContainer()->getServer()->getCommentsManager()->registerEventHandler(function () {
93
+            return $this->getContainer()->query(EventHandler::class);
94
+        });
95
+    }
96 96
 }
Please login to merge, or discard this patch.
apps/dav/appinfo/v1/caldav.php 1 patch
Indentation   +18 added lines, -18 removed lines patch added patch discarded remove patch
@@ -35,22 +35,22 @@  discard block
 block discarded – undo
35 35
 use OCA\DAV\Connector\Sabre\Principal;
36 36
 
37 37
 $authBackend = new Auth(
38
-	\OC::$server->getSession(),
39
-	\OC::$server->getUserSession(),
40
-	\OC::$server->getRequest(),
41
-	\OC::$server->getTwoFactorAuthManager(),
42
-	\OC::$server->getBruteForceThrottler(),
43
-	'principals/'
38
+    \OC::$server->getSession(),
39
+    \OC::$server->getUserSession(),
40
+    \OC::$server->getRequest(),
41
+    \OC::$server->getTwoFactorAuthManager(),
42
+    \OC::$server->getBruteForceThrottler(),
43
+    'principals/'
44 44
 );
45 45
 $principalBackend = new Principal(
46
-	\OC::$server->getUserManager(),
47
-	\OC::$server->getGroupManager(),
48
-	\OC::$server->getShareManager(),
49
-	\OC::$server->getUserSession(),
50
-	\OC::$server->getAppManager(),
51
-	\OC::$server->query(\OCA\DAV\CalDAV\Proxy\ProxyMapper::class),
52
-	\OC::$server->getConfig(),
53
-	'principals/'
46
+    \OC::$server->getUserManager(),
47
+    \OC::$server->getGroupManager(),
48
+    \OC::$server->getShareManager(),
49
+    \OC::$server->getUserSession(),
50
+    \OC::$server->getAppManager(),
51
+    \OC::$server->query(\OCA\DAV\CalDAV\Proxy\ProxyMapper::class),
52
+    \OC::$server->getConfig(),
53
+    'principals/'
54 54
 );
55 55
 $db = \OC::$server->getDatabaseConnection();
56 56
 $userManager = \OC::$server->getUserManager();
@@ -70,8 +70,8 @@  discard block
 block discarded – undo
70 70
 $addressBookRoot->disableListing = !$debugging; // Disable listing
71 71
 
72 72
 $nodes = [
73
-	$principalCollection,
74
-	$addressBookRoot,
73
+    $principalCollection,
74
+    $addressBookRoot,
75 75
 ];
76 76
 
77 77
 // Fire up server
@@ -87,7 +87,7 @@  discard block
 block discarded – undo
87 87
 
88 88
 $server->addPlugin(new LegacyDAVACL());
89 89
 if ($debugging) {
90
-	$server->addPlugin(new Sabre\DAV\Browser\Plugin());
90
+    $server->addPlugin(new Sabre\DAV\Browser\Plugin());
91 91
 }
92 92
 
93 93
 $server->addPlugin(new \Sabre\DAV\Sync\Plugin());
@@ -95,7 +95,7 @@  discard block
 block discarded – undo
95 95
 $server->addPlugin(new \OCA\DAV\CalDAV\Schedule\Plugin());
96 96
 
97 97
 if ($sendInvitations) {
98
-	$server->addPlugin(\OC::$server->query(\OCA\DAV\CalDAV\Schedule\IMipPlugin::class));
98
+    $server->addPlugin(\OC::$server->query(\OCA\DAV\CalDAV\Schedule\IMipPlugin::class));
99 99
 }
100 100
 $server->addPlugin(new ExceptionLoggerPlugin('caldav', \OC::$server->getLogger()));
101 101
 
Please login to merge, or discard this patch.
apps/dav/appinfo/v1/carddav.php 1 patch
Indentation   +19 added lines, -19 removed lines patch added patch discarded remove patch
@@ -39,22 +39,22 @@  discard block
 block discarded – undo
39 39
 use Sabre\CardDAV\Plugin;
40 40
 
41 41
 $authBackend = new Auth(
42
-	\OC::$server->getSession(),
43
-	\OC::$server->getUserSession(),
44
-	\OC::$server->getRequest(),
45
-	\OC::$server->getTwoFactorAuthManager(),
46
-	\OC::$server->getBruteForceThrottler(),
47
-	'principals/'
42
+    \OC::$server->getSession(),
43
+    \OC::$server->getUserSession(),
44
+    \OC::$server->getRequest(),
45
+    \OC::$server->getTwoFactorAuthManager(),
46
+    \OC::$server->getBruteForceThrottler(),
47
+    'principals/'
48 48
 );
49 49
 $principalBackend = new Principal(
50
-	\OC::$server->getUserManager(),
51
-	\OC::$server->getGroupManager(),
52
-	\OC::$server->getShareManager(),
53
-	\OC::$server->getUserSession(),
54
-	\OC::$server->getAppManager(),
55
-	\OC::$server->query(\OCA\DAV\CalDAV\Proxy\ProxyMapper::class),
56
-	\OC::$server->getConfig(),
57
-	'principals/'
50
+    \OC::$server->getUserManager(),
51
+    \OC::$server->getGroupManager(),
52
+    \OC::$server->getShareManager(),
53
+    \OC::$server->getUserSession(),
54
+    \OC::$server->getAppManager(),
55
+    \OC::$server->query(\OCA\DAV\CalDAV\Proxy\ProxyMapper::class),
56
+    \OC::$server->getConfig(),
57
+    'principals/'
58 58
 );
59 59
 $db = \OC::$server->getDatabaseConnection();
60 60
 $cardDavBackend = new CardDavBackend($db, $principalBackend, \OC::$server->getUserManager(), \OC::$server->getGroupManager(), \OC::$server->getEventDispatcher());
@@ -70,8 +70,8 @@  discard block
 block discarded – undo
70 70
 $addressBookRoot->disableListing = !$debugging; // Disable listing
71 71
 
72 72
 $nodes = [
73
-	$principalCollection,
74
-	$addressBookRoot,
73
+    $principalCollection,
74
+    $addressBookRoot,
75 75
 ];
76 76
 
77 77
 // Fire up server
@@ -86,14 +86,14 @@  discard block
 block discarded – undo
86 86
 
87 87
 $server->addPlugin(new LegacyDAVACL());
88 88
 if ($debugging) {
89
-	$server->addPlugin(new Sabre\DAV\Browser\Plugin());
89
+    $server->addPlugin(new Sabre\DAV\Browser\Plugin());
90 90
 }
91 91
 
92 92
 $server->addPlugin(new \Sabre\DAV\Sync\Plugin());
93 93
 $server->addPlugin(new \Sabre\CardDAV\VCFExportPlugin());
94 94
 $server->addPlugin(new \OCA\DAV\CardDAV\ImageExportPlugin(new \OCA\DAV\CardDAV\PhotoCache(
95
-	\OC::$server->getAppDataDir('dav-photocache'),
96
-	\OC::$server->getLogger()
95
+    \OC::$server->getAppDataDir('dav-photocache'),
96
+    \OC::$server->getLogger()
97 97
 )));
98 98
 $server->addPlugin(new ExceptionLoggerPlugin('carddav', \OC::$server->getLogger()));
99 99
 
Please login to merge, or discard this patch.
apps/dav/appinfo/v1/publicwebdav.php 1 patch
Indentation   +49 added lines, -49 removed lines patch added patch discarded remove patch
@@ -39,22 +39,22 @@  discard block
 block discarded – undo
39 39
 
40 40
 // Backends
41 41
 $authBackend = new OCA\DAV\Connector\PublicAuth(
42
-	\OC::$server->getRequest(),
43
-	\OC::$server->getShareManager(),
44
-	\OC::$server->getSession()
42
+    \OC::$server->getRequest(),
43
+    \OC::$server->getShareManager(),
44
+    \OC::$server->getSession()
45 45
 );
46 46
 $authPlugin = new \Sabre\DAV\Auth\Plugin($authBackend);
47 47
 
48 48
 $serverFactory = new OCA\DAV\Connector\Sabre\ServerFactory(
49
-	\OC::$server->getConfig(),
50
-	\OC::$server->getLogger(),
51
-	\OC::$server->getDatabaseConnection(),
52
-	\OC::$server->getUserSession(),
53
-	\OC::$server->getMountManager(),
54
-	\OC::$server->getTagManager(),
55
-	\OC::$server->getRequest(),
56
-	\OC::$server->getPreviewManager(),
57
-	\OC::$server->getEventDispatcher()
49
+    \OC::$server->getConfig(),
50
+    \OC::$server->getLogger(),
51
+    \OC::$server->getDatabaseConnection(),
52
+    \OC::$server->getUserSession(),
53
+    \OC::$server->getMountManager(),
54
+    \OC::$server->getTagManager(),
55
+    \OC::$server->getRequest(),
56
+    \OC::$server->getPreviewManager(),
57
+    \OC::$server->getEventDispatcher()
58 58
 );
59 59
 
60 60
 $requestUri = \OC::$server->getRequest()->getRequestUri();
@@ -63,43 +63,43 @@  discard block
 block discarded – undo
63 63
 $filesDropPlugin = new \OCA\DAV\Files\Sharing\FilesDropPlugin();
64 64
 
65 65
 $server = $serverFactory->createServer($baseuri, $requestUri, $authPlugin, function (\Sabre\DAV\Server $server) use ($authBackend, $linkCheckPlugin, $filesDropPlugin) {
66
-	$isAjax = (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest');
67
-	$federatedSharingApp = new \OCA\FederatedFileSharing\AppInfo\Application();
68
-	$federatedShareProvider = $federatedSharingApp->getFederatedShareProvider();
69
-	if ($federatedShareProvider->isOutgoingServer2serverShareEnabled() === false && !$isAjax) {
70
-		// this is what is thrown when trying to access a non-existing share
71
-		throw new \Sabre\DAV\Exception\NotAuthenticated();
72
-	}
73
-
74
-	$share = $authBackend->getShare();
75
-	$owner = $share->getShareOwner();
76
-	$isReadable = $share->getPermissions() & \OCP\Constants::PERMISSION_READ;
77
-	$fileId = $share->getNodeId();
78
-
79
-	// FIXME: should not add storage wrappers outside of preSetup, need to find a better way
80
-	$previousLog = \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper(false);
81
-	\OC\Files\Filesystem::addStorageWrapper('sharePermissions', function ($mountPoint, $storage) use ($share) {
82
-		return new \OC\Files\Storage\Wrapper\PermissionsMask(['storage' => $storage, 'mask' => $share->getPermissions() | \OCP\Constants::PERMISSION_SHARE]);
83
-	});
84
-
85
-	\OC\Files\Filesystem::logWarningWhenAddingStorageWrapper($previousLog);
86
-
87
-	OC_Util::tearDownFS();
88
-	OC_Util::setupFS($owner);
89
-	$ownerView = new \OC\Files\View('/'. $owner . '/files');
90
-	$path = $ownerView->getPath($fileId);
91
-	$fileInfo = $ownerView->getFileInfo($path);
92
-	$linkCheckPlugin->setFileInfo($fileInfo);
93
-
94
-	// If not readble (files_drop) enable the filesdrop plugin
95
-	if (!$isReadable) {
96
-		$filesDropPlugin->enable();
97
-	}
98
-
99
-	$view = new \OC\Files\View($ownerView->getAbsolutePath($path));
100
-	$filesDropPlugin->setView($view);
101
-
102
-	return $view;
66
+    $isAjax = (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest');
67
+    $federatedSharingApp = new \OCA\FederatedFileSharing\AppInfo\Application();
68
+    $federatedShareProvider = $federatedSharingApp->getFederatedShareProvider();
69
+    if ($federatedShareProvider->isOutgoingServer2serverShareEnabled() === false && !$isAjax) {
70
+        // this is what is thrown when trying to access a non-existing share
71
+        throw new \Sabre\DAV\Exception\NotAuthenticated();
72
+    }
73
+
74
+    $share = $authBackend->getShare();
75
+    $owner = $share->getShareOwner();
76
+    $isReadable = $share->getPermissions() & \OCP\Constants::PERMISSION_READ;
77
+    $fileId = $share->getNodeId();
78
+
79
+    // FIXME: should not add storage wrappers outside of preSetup, need to find a better way
80
+    $previousLog = \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper(false);
81
+    \OC\Files\Filesystem::addStorageWrapper('sharePermissions', function ($mountPoint, $storage) use ($share) {
82
+        return new \OC\Files\Storage\Wrapper\PermissionsMask(['storage' => $storage, 'mask' => $share->getPermissions() | \OCP\Constants::PERMISSION_SHARE]);
83
+    });
84
+
85
+    \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper($previousLog);
86
+
87
+    OC_Util::tearDownFS();
88
+    OC_Util::setupFS($owner);
89
+    $ownerView = new \OC\Files\View('/'. $owner . '/files');
90
+    $path = $ownerView->getPath($fileId);
91
+    $fileInfo = $ownerView->getFileInfo($path);
92
+    $linkCheckPlugin->setFileInfo($fileInfo);
93
+
94
+    // If not readble (files_drop) enable the filesdrop plugin
95
+    if (!$isReadable) {
96
+        $filesDropPlugin->enable();
97
+    }
98
+
99
+    $view = new \OC\Files\View($ownerView->getAbsolutePath($path));
100
+    $filesDropPlugin->setView($view);
101
+
102
+    return $view;
103 103
 });
104 104
 
105 105
 $server->addPlugin($linkCheckPlugin);
Please login to merge, or discard this patch.
apps/dav/lib/Files/BrowserErrorPagePlugin.php 1 patch
Indentation   +75 added lines, -75 removed lines patch added patch discarded remove patch
@@ -32,86 +32,86 @@
 block discarded – undo
32 32
 use Sabre\DAV\ServerPlugin;
33 33
 
34 34
 class BrowserErrorPagePlugin extends ServerPlugin {
35
-	/** @var Server */
36
-	private $server;
35
+    /** @var Server */
36
+    private $server;
37 37
 
38
-	/**
39
-	 * This initializes the plugin.
40
-	 *
41
-	 * This function is called by Sabre\DAV\Server, after
42
-	 * addPlugin is called.
43
-	 *
44
-	 * This method should set up the required event subscriptions.
45
-	 *
46
-	 * @param Server $server
47
-	 * @return void
48
-	 */
49
-	function initialize(Server $server) {
50
-		$this->server = $server;
51
-		$server->on('exception', [$this, 'logException'], 1000);
52
-	}
38
+    /**
39
+     * This initializes the plugin.
40
+     *
41
+     * This function is called by Sabre\DAV\Server, after
42
+     * addPlugin is called.
43
+     *
44
+     * This method should set up the required event subscriptions.
45
+     *
46
+     * @param Server $server
47
+     * @return void
48
+     */
49
+    function initialize(Server $server) {
50
+        $this->server = $server;
51
+        $server->on('exception', [$this, 'logException'], 1000);
52
+    }
53 53
 
54
-	/**
55
-	 * @param IRequest $request
56
-	 * @return bool
57
-	 */
58
-	public static function isBrowserRequest(IRequest $request) {
59
-		if ($request->getMethod() !== 'GET') {
60
-			return false;
61
-		}
62
-		return $request->isUserAgent([
63
-			Request::USER_AGENT_IE,
64
-			Request::USER_AGENT_MS_EDGE,
65
-			Request::USER_AGENT_CHROME,
66
-			Request::USER_AGENT_FIREFOX,
67
-			Request::USER_AGENT_SAFARI,
68
-		]);
69
-	}
54
+    /**
55
+     * @param IRequest $request
56
+     * @return bool
57
+     */
58
+    public static function isBrowserRequest(IRequest $request) {
59
+        if ($request->getMethod() !== 'GET') {
60
+            return false;
61
+        }
62
+        return $request->isUserAgent([
63
+            Request::USER_AGENT_IE,
64
+            Request::USER_AGENT_MS_EDGE,
65
+            Request::USER_AGENT_CHROME,
66
+            Request::USER_AGENT_FIREFOX,
67
+            Request::USER_AGENT_SAFARI,
68
+        ]);
69
+    }
70 70
 
71
-	/**
72
-	 * @param \Exception $ex
73
-	 */
74
-	public function logException(\Exception $ex) {
75
-		if ($ex instanceof Exception) {
76
-			$httpCode = $ex->getHTTPCode();
77
-			$headers = $ex->getHTTPHeaders($this->server);
78
-		} else {
79
-			$httpCode = 500;
80
-			$headers = [];
81
-		}
82
-		$this->server->httpResponse->addHeaders($headers);
83
-		$this->server->httpResponse->setStatus($httpCode);
84
-		$body = $this->generateBody($httpCode);
85
-		$this->server->httpResponse->setBody($body);
86
-		$csp = new ContentSecurityPolicy();
87
-		$this->server->httpResponse->addHeader('Content-Security-Policy', $csp->buildPolicy());
88
-		$this->sendResponse();
89
-	}
71
+    /**
72
+     * @param \Exception $ex
73
+     */
74
+    public function logException(\Exception $ex) {
75
+        if ($ex instanceof Exception) {
76
+            $httpCode = $ex->getHTTPCode();
77
+            $headers = $ex->getHTTPHeaders($this->server);
78
+        } else {
79
+            $httpCode = 500;
80
+            $headers = [];
81
+        }
82
+        $this->server->httpResponse->addHeaders($headers);
83
+        $this->server->httpResponse->setStatus($httpCode);
84
+        $body = $this->generateBody($httpCode);
85
+        $this->server->httpResponse->setBody($body);
86
+        $csp = new ContentSecurityPolicy();
87
+        $this->server->httpResponse->addHeader('Content-Security-Policy', $csp->buildPolicy());
88
+        $this->sendResponse();
89
+    }
90 90
 
91
-	/**
92
-	 * @codeCoverageIgnore
93
-	 * @return bool|string
94
-	 */
95
-	public function generateBody(int $httpCode) {
96
-		$request = \OC::$server->getRequest();
91
+    /**
92
+     * @codeCoverageIgnore
93
+     * @return bool|string
94
+     */
95
+    public function generateBody(int $httpCode) {
96
+        $request = \OC::$server->getRequest();
97 97
 
98
-		$templateName = 'exception';
99
-		if($httpCode === 403 || $httpCode === 404) {
100
-			$templateName = (string)$httpCode;
101
-		}
98
+        $templateName = 'exception';
99
+        if($httpCode === 403 || $httpCode === 404) {
100
+            $templateName = (string)$httpCode;
101
+        }
102 102
 
103
-		$content = new OC_Template('core', $templateName, 'guest');
104
-		$content->assign('title', $this->server->httpResponse->getStatusText());
105
-		$content->assign('remoteAddr', $request->getRemoteAddress());
106
-		$content->assign('requestID', $request->getId());
107
-		return $content->fetchPage();
108
-	}
103
+        $content = new OC_Template('core', $templateName, 'guest');
104
+        $content->assign('title', $this->server->httpResponse->getStatusText());
105
+        $content->assign('remoteAddr', $request->getRemoteAddress());
106
+        $content->assign('requestID', $request->getId());
107
+        return $content->fetchPage();
108
+    }
109 109
 
110
-	/**
111
-	 * @codeCoverageIgnore
112
-	 */
113
-	public function sendResponse() {
114
-		$this->server->sapi->sendResponse($this->server->httpResponse);
115
-		exit();
116
-	}
110
+    /**
111
+     * @codeCoverageIgnore
112
+     */
113
+    public function sendResponse() {
114
+        $this->server->sapi->sendResponse($this->server->httpResponse);
115
+        exit();
116
+    }
117 117
 }
Please login to merge, or discard this patch.
apps/dav/lib/SystemTag/SystemTagPlugin.php 1 patch
Indentation   +276 added lines, -276 removed lines patch added patch discarded remove patch
@@ -48,280 +48,280 @@
 block discarded – undo
48 48
  */
49 49
 class SystemTagPlugin extends \Sabre\DAV\ServerPlugin {
50 50
 
51
-	// namespace
52
-	const NS_OWNCLOUD = 'http://owncloud.org/ns';
53
-	const ID_PROPERTYNAME = '{http://owncloud.org/ns}id';
54
-	const DISPLAYNAME_PROPERTYNAME = '{http://owncloud.org/ns}display-name';
55
-	const USERVISIBLE_PROPERTYNAME = '{http://owncloud.org/ns}user-visible';
56
-	const USERASSIGNABLE_PROPERTYNAME = '{http://owncloud.org/ns}user-assignable';
57
-	const GROUPS_PROPERTYNAME = '{http://owncloud.org/ns}groups';
58
-	const CANASSIGN_PROPERTYNAME = '{http://owncloud.org/ns}can-assign';
59
-
60
-	/**
61
-	 * @var \Sabre\DAV\Server $server
62
-	 */
63
-	private $server;
64
-
65
-	/**
66
-	 * @var ISystemTagManager
67
-	 */
68
-	protected $tagManager;
69
-
70
-	/**
71
-	 * @var IUserSession
72
-	 */
73
-	protected $userSession;
74
-
75
-	/**
76
-	 * @var IGroupManager
77
-	 */
78
-	protected $groupManager;
79
-
80
-	/**
81
-	 * @param ISystemTagManager $tagManager tag manager
82
-	 * @param IGroupManager $groupManager
83
-	 * @param IUserSession $userSession
84
-	 */
85
-	public function __construct(ISystemTagManager $tagManager,
86
-								IGroupManager $groupManager,
87
-								IUserSession $userSession) {
88
-		$this->tagManager = $tagManager;
89
-		$this->userSession = $userSession;
90
-		$this->groupManager = $groupManager;
91
-	}
92
-
93
-	/**
94
-	 * This initializes the plugin.
95
-	 *
96
-	 * This function is called by \Sabre\DAV\Server, after
97
-	 * addPlugin is called.
98
-	 *
99
-	 * This method should set up the required event subscriptions.
100
-	 *
101
-	 * @param \Sabre\DAV\Server $server
102
-	 * @return void
103
-	 */
104
-	public function initialize(\Sabre\DAV\Server $server) {
105
-
106
-		$server->xml->namespaceMap[self::NS_OWNCLOUD] = 'oc';
107
-
108
-		$server->protectedProperties[] = self::ID_PROPERTYNAME;
109
-
110
-		$server->on('propFind', [$this, 'handleGetProperties']);
111
-		$server->on('propPatch', [$this, 'handleUpdateProperties']);
112
-		$server->on('method:POST', [$this, 'httpPost']);
113
-
114
-		$this->server = $server;
115
-	}
116
-
117
-	/**
118
-	 * POST operation on system tag collections
119
-	 *
120
-	 * @param RequestInterface $request request object
121
-	 * @param ResponseInterface $response response object
122
-	 * @return null|false
123
-	 */
124
-	public function httpPost(RequestInterface $request, ResponseInterface $response) {
125
-		$path = $request->getPath();
126
-
127
-		// Making sure the node exists
128
-		$node = $this->server->tree->getNodeForPath($path);
129
-		if ($node instanceof SystemTagsByIdCollection || $node instanceof SystemTagsObjectMappingCollection) {
130
-			$data = $request->getBodyAsString();
131
-
132
-			$tag = $this->createTag($data, $request->getHeader('Content-Type'));
133
-
134
-			if ($node instanceof SystemTagsObjectMappingCollection) {
135
-				// also add to collection
136
-				$node->createFile($tag->getId());
137
-				$url = $request->getBaseUrl() . 'systemtags/';
138
-			} else {
139
-				$url = $request->getUrl();
140
-			}
141
-
142
-			if ($url[strlen($url) - 1] !== '/') {
143
-				$url .= '/';
144
-			}
145
-
146
-			$response->setHeader('Content-Location', $url . $tag->getId());
147
-
148
-			// created
149
-			$response->setStatus(201);
150
-			return false;
151
-		}
152
-	}
153
-
154
-	/**
155
-	 * Creates a new tag
156
-	 *
157
-	 * @param string $data JSON encoded string containing the properties of the tag to create
158
-	 * @param string $contentType content type of the data
159
-	 * @return ISystemTag newly created system tag
160
-	 *
161
-	 * @throws BadRequest if a field was missing
162
-	 * @throws Conflict if a tag with the same properties already exists
163
-	 * @throws UnsupportedMediaType if the content type is not supported
164
-	 */
165
-	private function createTag($data, $contentType = 'application/json') {
166
-		if (explode(';', $contentType)[0] === 'application/json') {
167
-			$data = json_decode($data, true);
168
-		} else {
169
-			throw new UnsupportedMediaType();
170
-		}
171
-
172
-		if (!isset($data['name'])) {
173
-			throw new BadRequest('Missing "name" attribute');
174
-		}
175
-
176
-		$tagName = $data['name'];
177
-		$userVisible = true;
178
-		$userAssignable = true;
179
-
180
-		if (isset($data['userVisible'])) {
181
-			$userVisible = (bool)$data['userVisible'];
182
-		}
183
-
184
-		if (isset($data['userAssignable'])) {
185
-			$userAssignable = (bool)$data['userAssignable'];
186
-		}
187
-
188
-		$groups = [];
189
-		if (isset($data['groups'])) {
190
-			$groups = $data['groups'];
191
-			if (is_string($groups)) {
192
-				$groups = explode('|', $groups);
193
-			}
194
-		}
195
-
196
-		if($userVisible === false || $userAssignable === false || !empty($groups)) {
197
-			if(!$this->userSession->isLoggedIn() || !$this->groupManager->isAdmin($this->userSession->getUser()->getUID())) {
198
-				throw new BadRequest('Not sufficient permissions');
199
-			}
200
-		}
201
-
202
-		try {
203
-			$tag = $this->tagManager->createTag($tagName, $userVisible, $userAssignable);
204
-			if (!empty($groups)) {
205
-				$this->tagManager->setTagGroups($tag, $groups);
206
-			}
207
-			return $tag;
208
-		} catch (TagAlreadyExistsException $e) {
209
-			throw new Conflict('Tag already exists', 0, $e);
210
-		}
211
-	}
212
-
213
-
214
-	/**
215
-	 * Retrieves system tag properties
216
-	 *
217
-	 * @param PropFind $propFind
218
-	 * @param \Sabre\DAV\INode $node
219
-	 */
220
-	public function handleGetProperties(
221
-		PropFind $propFind,
222
-		\Sabre\DAV\INode $node
223
-	) {
224
-		if (!($node instanceof SystemTagNode) && !($node instanceof SystemTagMappingNode)) {
225
-			return;
226
-		}
227
-
228
-		$propFind->handle(self::ID_PROPERTYNAME, function() use ($node) {
229
-			return $node->getSystemTag()->getId();
230
-		});
231
-
232
-		$propFind->handle(self::DISPLAYNAME_PROPERTYNAME, function() use ($node) {
233
-			return $node->getSystemTag()->getName();
234
-		});
235
-
236
-		$propFind->handle(self::USERVISIBLE_PROPERTYNAME, function() use ($node) {
237
-			return $node->getSystemTag()->isUserVisible() ? 'true' : 'false';
238
-		});
239
-
240
-		$propFind->handle(self::USERASSIGNABLE_PROPERTYNAME, function() use ($node) {
241
-			// this is the tag's inherent property "is user assignable"
242
-			return $node->getSystemTag()->isUserAssignable() ? 'true' : 'false';
243
-		});
244
-
245
-		$propFind->handle(self::CANASSIGN_PROPERTYNAME, function() use ($node) {
246
-			// this is the effective permission for the current user
247
-			return $this->tagManager->canUserAssignTag($node->getSystemTag(), $this->userSession->getUser()) ? 'true' : 'false';
248
-		});
249
-
250
-		$propFind->handle(self::GROUPS_PROPERTYNAME, function() use ($node) {
251
-			if (!$this->groupManager->isAdmin($this->userSession->getUser()->getUID())) {
252
-				// property only available for admins
253
-				throw new Forbidden();
254
-			}
255
-			$groups = [];
256
-			// no need to retrieve groups for namespaces that don't qualify
257
-			if ($node->getSystemTag()->isUserVisible() && !$node->getSystemTag()->isUserAssignable()) {
258
-				$groups = $this->tagManager->getTagGroups($node->getSystemTag());
259
-			}
260
-			return implode('|', $groups);
261
-		});
262
-	}
263
-
264
-	/**
265
-	 * Updates tag attributes
266
-	 *
267
-	 * @param string $path
268
-	 * @param PropPatch $propPatch
269
-	 *
270
-	 * @return void
271
-	 */
272
-	public function handleUpdateProperties($path, PropPatch $propPatch) {
273
-		$node = $this->server->tree->getNodeForPath($path);
274
-		if (!($node instanceof SystemTagNode)) {
275
-			return;
276
-		}
277
-
278
-		$propPatch->handle([
279
-			self::DISPLAYNAME_PROPERTYNAME,
280
-			self::USERVISIBLE_PROPERTYNAME,
281
-			self::USERASSIGNABLE_PROPERTYNAME,
282
-			self::GROUPS_PROPERTYNAME,
283
-		], function($props) use ($node) {
284
-			$tag = $node->getSystemTag();
285
-			$name = $tag->getName();
286
-			$userVisible = $tag->isUserVisible();
287
-			$userAssignable = $tag->isUserAssignable();
288
-
289
-			$updateTag = false;
290
-
291
-			if (isset($props[self::DISPLAYNAME_PROPERTYNAME])) {
292
-				$name = $props[self::DISPLAYNAME_PROPERTYNAME];
293
-				$updateTag = true;
294
-			}
295
-
296
-			if (isset($props[self::USERVISIBLE_PROPERTYNAME])) {
297
-				$propValue = $props[self::USERVISIBLE_PROPERTYNAME];
298
-				$userVisible = ($propValue !== 'false' && $propValue !== '0');
299
-				$updateTag = true;
300
-			}
301
-
302
-			if (isset($props[self::USERASSIGNABLE_PROPERTYNAME])) {
303
-				$propValue = $props[self::USERASSIGNABLE_PROPERTYNAME];
304
-				$userAssignable = ($propValue !== 'false' && $propValue !== '0');
305
-				$updateTag = true;
306
-			}
307
-
308
-			if (isset($props[self::GROUPS_PROPERTYNAME])) {
309
-				if (!$this->groupManager->isAdmin($this->userSession->getUser()->getUID())) {
310
-					// property only available for admins
311
-					throw new Forbidden();
312
-				}
313
-
314
-				$propValue = $props[self::GROUPS_PROPERTYNAME];
315
-				$groupIds = explode('|', $propValue);
316
-				$this->tagManager->setTagGroups($tag, $groupIds);
317
-			}
318
-
319
-			if ($updateTag) {
320
-				$node->update($name, $userVisible, $userAssignable);
321
-			}
322
-
323
-			return true;
324
-		});
325
-
326
-	}
51
+    // namespace
52
+    const NS_OWNCLOUD = 'http://owncloud.org/ns';
53
+    const ID_PROPERTYNAME = '{http://owncloud.org/ns}id';
54
+    const DISPLAYNAME_PROPERTYNAME = '{http://owncloud.org/ns}display-name';
55
+    const USERVISIBLE_PROPERTYNAME = '{http://owncloud.org/ns}user-visible';
56
+    const USERASSIGNABLE_PROPERTYNAME = '{http://owncloud.org/ns}user-assignable';
57
+    const GROUPS_PROPERTYNAME = '{http://owncloud.org/ns}groups';
58
+    const CANASSIGN_PROPERTYNAME = '{http://owncloud.org/ns}can-assign';
59
+
60
+    /**
61
+     * @var \Sabre\DAV\Server $server
62
+     */
63
+    private $server;
64
+
65
+    /**
66
+     * @var ISystemTagManager
67
+     */
68
+    protected $tagManager;
69
+
70
+    /**
71
+     * @var IUserSession
72
+     */
73
+    protected $userSession;
74
+
75
+    /**
76
+     * @var IGroupManager
77
+     */
78
+    protected $groupManager;
79
+
80
+    /**
81
+     * @param ISystemTagManager $tagManager tag manager
82
+     * @param IGroupManager $groupManager
83
+     * @param IUserSession $userSession
84
+     */
85
+    public function __construct(ISystemTagManager $tagManager,
86
+                                IGroupManager $groupManager,
87
+                                IUserSession $userSession) {
88
+        $this->tagManager = $tagManager;
89
+        $this->userSession = $userSession;
90
+        $this->groupManager = $groupManager;
91
+    }
92
+
93
+    /**
94
+     * This initializes the plugin.
95
+     *
96
+     * This function is called by \Sabre\DAV\Server, after
97
+     * addPlugin is called.
98
+     *
99
+     * This method should set up the required event subscriptions.
100
+     *
101
+     * @param \Sabre\DAV\Server $server
102
+     * @return void
103
+     */
104
+    public function initialize(\Sabre\DAV\Server $server) {
105
+
106
+        $server->xml->namespaceMap[self::NS_OWNCLOUD] = 'oc';
107
+
108
+        $server->protectedProperties[] = self::ID_PROPERTYNAME;
109
+
110
+        $server->on('propFind', [$this, 'handleGetProperties']);
111
+        $server->on('propPatch', [$this, 'handleUpdateProperties']);
112
+        $server->on('method:POST', [$this, 'httpPost']);
113
+
114
+        $this->server = $server;
115
+    }
116
+
117
+    /**
118
+     * POST operation on system tag collections
119
+     *
120
+     * @param RequestInterface $request request object
121
+     * @param ResponseInterface $response response object
122
+     * @return null|false
123
+     */
124
+    public function httpPost(RequestInterface $request, ResponseInterface $response) {
125
+        $path = $request->getPath();
126
+
127
+        // Making sure the node exists
128
+        $node = $this->server->tree->getNodeForPath($path);
129
+        if ($node instanceof SystemTagsByIdCollection || $node instanceof SystemTagsObjectMappingCollection) {
130
+            $data = $request->getBodyAsString();
131
+
132
+            $tag = $this->createTag($data, $request->getHeader('Content-Type'));
133
+
134
+            if ($node instanceof SystemTagsObjectMappingCollection) {
135
+                // also add to collection
136
+                $node->createFile($tag->getId());
137
+                $url = $request->getBaseUrl() . 'systemtags/';
138
+            } else {
139
+                $url = $request->getUrl();
140
+            }
141
+
142
+            if ($url[strlen($url) - 1] !== '/') {
143
+                $url .= '/';
144
+            }
145
+
146
+            $response->setHeader('Content-Location', $url . $tag->getId());
147
+
148
+            // created
149
+            $response->setStatus(201);
150
+            return false;
151
+        }
152
+    }
153
+
154
+    /**
155
+     * Creates a new tag
156
+     *
157
+     * @param string $data JSON encoded string containing the properties of the tag to create
158
+     * @param string $contentType content type of the data
159
+     * @return ISystemTag newly created system tag
160
+     *
161
+     * @throws BadRequest if a field was missing
162
+     * @throws Conflict if a tag with the same properties already exists
163
+     * @throws UnsupportedMediaType if the content type is not supported
164
+     */
165
+    private function createTag($data, $contentType = 'application/json') {
166
+        if (explode(';', $contentType)[0] === 'application/json') {
167
+            $data = json_decode($data, true);
168
+        } else {
169
+            throw new UnsupportedMediaType();
170
+        }
171
+
172
+        if (!isset($data['name'])) {
173
+            throw new BadRequest('Missing "name" attribute');
174
+        }
175
+
176
+        $tagName = $data['name'];
177
+        $userVisible = true;
178
+        $userAssignable = true;
179
+
180
+        if (isset($data['userVisible'])) {
181
+            $userVisible = (bool)$data['userVisible'];
182
+        }
183
+
184
+        if (isset($data['userAssignable'])) {
185
+            $userAssignable = (bool)$data['userAssignable'];
186
+        }
187
+
188
+        $groups = [];
189
+        if (isset($data['groups'])) {
190
+            $groups = $data['groups'];
191
+            if (is_string($groups)) {
192
+                $groups = explode('|', $groups);
193
+            }
194
+        }
195
+
196
+        if($userVisible === false || $userAssignable === false || !empty($groups)) {
197
+            if(!$this->userSession->isLoggedIn() || !$this->groupManager->isAdmin($this->userSession->getUser()->getUID())) {
198
+                throw new BadRequest('Not sufficient permissions');
199
+            }
200
+        }
201
+
202
+        try {
203
+            $tag = $this->tagManager->createTag($tagName, $userVisible, $userAssignable);
204
+            if (!empty($groups)) {
205
+                $this->tagManager->setTagGroups($tag, $groups);
206
+            }
207
+            return $tag;
208
+        } catch (TagAlreadyExistsException $e) {
209
+            throw new Conflict('Tag already exists', 0, $e);
210
+        }
211
+    }
212
+
213
+
214
+    /**
215
+     * Retrieves system tag properties
216
+     *
217
+     * @param PropFind $propFind
218
+     * @param \Sabre\DAV\INode $node
219
+     */
220
+    public function handleGetProperties(
221
+        PropFind $propFind,
222
+        \Sabre\DAV\INode $node
223
+    ) {
224
+        if (!($node instanceof SystemTagNode) && !($node instanceof SystemTagMappingNode)) {
225
+            return;
226
+        }
227
+
228
+        $propFind->handle(self::ID_PROPERTYNAME, function() use ($node) {
229
+            return $node->getSystemTag()->getId();
230
+        });
231
+
232
+        $propFind->handle(self::DISPLAYNAME_PROPERTYNAME, function() use ($node) {
233
+            return $node->getSystemTag()->getName();
234
+        });
235
+
236
+        $propFind->handle(self::USERVISIBLE_PROPERTYNAME, function() use ($node) {
237
+            return $node->getSystemTag()->isUserVisible() ? 'true' : 'false';
238
+        });
239
+
240
+        $propFind->handle(self::USERASSIGNABLE_PROPERTYNAME, function() use ($node) {
241
+            // this is the tag's inherent property "is user assignable"
242
+            return $node->getSystemTag()->isUserAssignable() ? 'true' : 'false';
243
+        });
244
+
245
+        $propFind->handle(self::CANASSIGN_PROPERTYNAME, function() use ($node) {
246
+            // this is the effective permission for the current user
247
+            return $this->tagManager->canUserAssignTag($node->getSystemTag(), $this->userSession->getUser()) ? 'true' : 'false';
248
+        });
249
+
250
+        $propFind->handle(self::GROUPS_PROPERTYNAME, function() use ($node) {
251
+            if (!$this->groupManager->isAdmin($this->userSession->getUser()->getUID())) {
252
+                // property only available for admins
253
+                throw new Forbidden();
254
+            }
255
+            $groups = [];
256
+            // no need to retrieve groups for namespaces that don't qualify
257
+            if ($node->getSystemTag()->isUserVisible() && !$node->getSystemTag()->isUserAssignable()) {
258
+                $groups = $this->tagManager->getTagGroups($node->getSystemTag());
259
+            }
260
+            return implode('|', $groups);
261
+        });
262
+    }
263
+
264
+    /**
265
+     * Updates tag attributes
266
+     *
267
+     * @param string $path
268
+     * @param PropPatch $propPatch
269
+     *
270
+     * @return void
271
+     */
272
+    public function handleUpdateProperties($path, PropPatch $propPatch) {
273
+        $node = $this->server->tree->getNodeForPath($path);
274
+        if (!($node instanceof SystemTagNode)) {
275
+            return;
276
+        }
277
+
278
+        $propPatch->handle([
279
+            self::DISPLAYNAME_PROPERTYNAME,
280
+            self::USERVISIBLE_PROPERTYNAME,
281
+            self::USERASSIGNABLE_PROPERTYNAME,
282
+            self::GROUPS_PROPERTYNAME,
283
+        ], function($props) use ($node) {
284
+            $tag = $node->getSystemTag();
285
+            $name = $tag->getName();
286
+            $userVisible = $tag->isUserVisible();
287
+            $userAssignable = $tag->isUserAssignable();
288
+
289
+            $updateTag = false;
290
+
291
+            if (isset($props[self::DISPLAYNAME_PROPERTYNAME])) {
292
+                $name = $props[self::DISPLAYNAME_PROPERTYNAME];
293
+                $updateTag = true;
294
+            }
295
+
296
+            if (isset($props[self::USERVISIBLE_PROPERTYNAME])) {
297
+                $propValue = $props[self::USERVISIBLE_PROPERTYNAME];
298
+                $userVisible = ($propValue !== 'false' && $propValue !== '0');
299
+                $updateTag = true;
300
+            }
301
+
302
+            if (isset($props[self::USERASSIGNABLE_PROPERTYNAME])) {
303
+                $propValue = $props[self::USERASSIGNABLE_PROPERTYNAME];
304
+                $userAssignable = ($propValue !== 'false' && $propValue !== '0');
305
+                $updateTag = true;
306
+            }
307
+
308
+            if (isset($props[self::GROUPS_PROPERTYNAME])) {
309
+                if (!$this->groupManager->isAdmin($this->userSession->getUser()->getUID())) {
310
+                    // property only available for admins
311
+                    throw new Forbidden();
312
+                }
313
+
314
+                $propValue = $props[self::GROUPS_PROPERTYNAME];
315
+                $groupIds = explode('|', $propValue);
316
+                $this->tagManager->setTagGroups($tag, $groupIds);
317
+            }
318
+
319
+            if ($updateTag) {
320
+                $node->update($name, $userVisible, $userAssignable);
321
+            }
322
+
323
+            return true;
324
+        });
325
+
326
+    }
327 327
 }
Please login to merge, or discard this patch.
apps/dav/lib/CalDAV/CalDavBackend.php 1 patch
Indentation   +2560 added lines, -2560 removed lines patch added patch discarded remove patch
@@ -76,2564 +76,2564 @@
 block discarded – undo
76 76
  */
77 77
 class CalDavBackend extends AbstractBackend implements SyncSupport, SubscriptionSupport, SchedulingSupport {
78 78
 
79
-	const CALENDAR_TYPE_CALENDAR = 0;
80
-	const CALENDAR_TYPE_SUBSCRIPTION = 1;
81
-
82
-	const PERSONAL_CALENDAR_URI = 'personal';
83
-	const PERSONAL_CALENDAR_NAME = 'Personal';
84
-
85
-	const RESOURCE_BOOKING_CALENDAR_URI = 'calendar';
86
-	const RESOURCE_BOOKING_CALENDAR_NAME = 'Calendar';
87
-
88
-	/**
89
-	 * We need to specify a max date, because we need to stop *somewhere*
90
-	 *
91
-	 * On 32 bit system the maximum for a signed integer is 2147483647, so
92
-	 * MAX_DATE cannot be higher than date('Y-m-d', 2147483647) which results
93
-	 * in 2038-01-19 to avoid problems when the date is converted
94
-	 * to a unix timestamp.
95
-	 */
96
-	const MAX_DATE = '2038-01-01';
97
-
98
-	const ACCESS_PUBLIC = 4;
99
-	const CLASSIFICATION_PUBLIC = 0;
100
-	const CLASSIFICATION_PRIVATE = 1;
101
-	const CLASSIFICATION_CONFIDENTIAL = 2;
102
-
103
-	/**
104
-	 * List of CalDAV properties, and how they map to database field names
105
-	 * Add your own properties by simply adding on to this array.
106
-	 *
107
-	 * Note that only string-based properties are supported here.
108
-	 *
109
-	 * @var array
110
-	 */
111
-	public $propertyMap = [
112
-		'{DAV:}displayname'                          => 'displayname',
113
-		'{urn:ietf:params:xml:ns:caldav}calendar-description' => 'description',
114
-		'{urn:ietf:params:xml:ns:caldav}calendar-timezone'    => 'timezone',
115
-		'{http://apple.com/ns/ical/}calendar-order'  => 'calendarorder',
116
-		'{http://apple.com/ns/ical/}calendar-color'  => 'calendarcolor',
117
-	];
118
-
119
-	/**
120
-	 * List of subscription properties, and how they map to database field names.
121
-	 *
122
-	 * @var array
123
-	 */
124
-	public $subscriptionPropertyMap = [
125
-		'{DAV:}displayname'                                           => 'displayname',
126
-		'{http://apple.com/ns/ical/}refreshrate'                      => 'refreshrate',
127
-		'{http://apple.com/ns/ical/}calendar-order'                   => 'calendarorder',
128
-		'{http://apple.com/ns/ical/}calendar-color'                   => 'calendarcolor',
129
-		'{http://calendarserver.org/ns/}subscribed-strip-todos'       => 'striptodos',
130
-		'{http://calendarserver.org/ns/}subscribed-strip-alarms'      => 'stripalarms',
131
-		'{http://calendarserver.org/ns/}subscribed-strip-attachments' => 'stripattachments',
132
-	];
133
-
134
-	/** @var array properties to index */
135
-	public static $indexProperties = ['CATEGORIES', 'COMMENT', 'DESCRIPTION',
136
-		'LOCATION', 'RESOURCES', 'STATUS', 'SUMMARY', 'ATTENDEE', 'CONTACT',
137
-		'ORGANIZER'];
138
-
139
-	/** @var array parameters to index */
140
-	public static $indexParameters = [
141
-		'ATTENDEE' => ['CN'],
142
-		'ORGANIZER' => ['CN'],
143
-	];
144
-
145
-	/**
146
-	 * @var string[] Map of uid => display name
147
-	 */
148
-	protected $userDisplayNames;
149
-
150
-	/** @var IDBConnection */
151
-	private $db;
152
-
153
-	/** @var Backend */
154
-	private $calendarSharingBackend;
155
-
156
-	/** @var Principal */
157
-	private $principalBackend;
158
-
159
-	/** @var IUserManager */
160
-	private $userManager;
161
-
162
-	/** @var ISecureRandom */
163
-	private $random;
164
-
165
-	/** @var ILogger */
166
-	private $logger;
167
-
168
-	/** @var EventDispatcherInterface */
169
-	private $dispatcher;
170
-
171
-	/** @var bool */
172
-	private $legacyEndpoint;
173
-
174
-	/** @var string */
175
-	private $dbObjectPropertiesTable = 'calendarobjects_props';
176
-
177
-	/**
178
-	 * CalDavBackend constructor.
179
-	 *
180
-	 * @param IDBConnection $db
181
-	 * @param Principal $principalBackend
182
-	 * @param IUserManager $userManager
183
-	 * @param IGroupManager $groupManager
184
-	 * @param ISecureRandom $random
185
-	 * @param ILogger $logger
186
-	 * @param EventDispatcherInterface $dispatcher
187
-	 * @param bool $legacyEndpoint
188
-	 */
189
-	public function __construct(IDBConnection $db,
190
-								Principal $principalBackend,
191
-								IUserManager $userManager,
192
-								IGroupManager $groupManager,
193
-								ISecureRandom $random,
194
-								ILogger $logger,
195
-								EventDispatcherInterface $dispatcher,
196
-								bool $legacyEndpoint = false) {
197
-		$this->db = $db;
198
-		$this->principalBackend = $principalBackend;
199
-		$this->userManager = $userManager;
200
-		$this->calendarSharingBackend = new Backend($this->db, $this->userManager, $groupManager, $principalBackend, 'calendar');
201
-		$this->random = $random;
202
-		$this->logger = $logger;
203
-		$this->dispatcher = $dispatcher;
204
-		$this->legacyEndpoint = $legacyEndpoint;
205
-	}
206
-
207
-	/**
208
-	 * Return the number of calendars for a principal
209
-	 *
210
-	 * By default this excludes the automatically generated birthday calendar
211
-	 *
212
-	 * @param $principalUri
213
-	 * @param bool $excludeBirthday
214
-	 * @return int
215
-	 */
216
-	public function getCalendarsForUserCount($principalUri, $excludeBirthday = true) {
217
-		$principalUri = $this->convertPrincipal($principalUri, true);
218
-		$query = $this->db->getQueryBuilder();
219
-		$query->select($query->func()->count('*'))
220
-			->from('calendars')
221
-			->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri)));
222
-
223
-		if ($excludeBirthday) {
224
-			$query->andWhere($query->expr()->neq('uri', $query->createNamedParameter(BirthdayService::BIRTHDAY_CALENDAR_URI)));
225
-		}
226
-
227
-		return (int)$query->execute()->fetchColumn();
228
-	}
229
-
230
-	/**
231
-	 * Returns a list of calendars for a principal.
232
-	 *
233
-	 * Every project is an array with the following keys:
234
-	 *  * id, a unique id that will be used by other functions to modify the
235
-	 *    calendar. This can be the same as the uri or a database key.
236
-	 *  * uri, which the basename of the uri with which the calendar is
237
-	 *    accessed.
238
-	 *  * principaluri. The owner of the calendar. Almost always the same as
239
-	 *    principalUri passed to this method.
240
-	 *
241
-	 * Furthermore it can contain webdav properties in clark notation. A very
242
-	 * common one is '{DAV:}displayname'.
243
-	 *
244
-	 * Many clients also require:
245
-	 * {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set
246
-	 * For this property, you can just return an instance of
247
-	 * Sabre\CalDAV\Property\SupportedCalendarComponentSet.
248
-	 *
249
-	 * If you return {http://sabredav.org/ns}read-only and set the value to 1,
250
-	 * ACL will automatically be put in read-only mode.
251
-	 *
252
-	 * @param string $principalUri
253
-	 * @return array
254
-	 */
255
-	function getCalendarsForUser($principalUri) {
256
-		$principalUriOriginal = $principalUri;
257
-		$principalUri = $this->convertPrincipal($principalUri, true);
258
-		$fields = array_values($this->propertyMap);
259
-		$fields[] = 'id';
260
-		$fields[] = 'uri';
261
-		$fields[] = 'synctoken';
262
-		$fields[] = 'components';
263
-		$fields[] = 'principaluri';
264
-		$fields[] = 'transparent';
265
-
266
-		// Making fields a comma-delimited list
267
-		$query = $this->db->getQueryBuilder();
268
-		$query->select($fields)->from('calendars')
269
-				->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri)))
270
-				->orderBy('calendarorder', 'ASC');
271
-		$stmt = $query->execute();
272
-
273
-		$calendars = [];
274
-		while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
275
-
276
-			$components = [];
277
-			if ($row['components']) {
278
-				$components = explode(',',$row['components']);
279
-			}
280
-
281
-			$calendar = [
282
-				'id' => $row['id'],
283
-				'uri' => $row['uri'],
284
-				'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint),
285
-				'{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'),
286
-				'{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
287
-				'{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components),
288
-				'{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'),
289
-				'{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint),
290
-			];
291
-
292
-			foreach($this->propertyMap as $xmlName=>$dbName) {
293
-				$calendar[$xmlName] = $row[$dbName];
294
-			}
295
-
296
-			$this->addOwnerPrincipal($calendar);
297
-
298
-			if (!isset($calendars[$calendar['id']])) {
299
-				$calendars[$calendar['id']] = $calendar;
300
-			}
301
-		}
302
-
303
-		$stmt->closeCursor();
304
-
305
-		// query for shared calendars
306
-		$principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true);
307
-		$principals = array_merge($principals, $this->principalBackend->getCircleMembership($principalUriOriginal));
308
-
309
-		$principals = array_map(function($principal) {
310
-			return urldecode($principal);
311
-		}, $principals);
312
-		$principals[]= $principalUri;
313
-
314
-		$fields = array_values($this->propertyMap);
315
-		$fields[] = 'a.id';
316
-		$fields[] = 'a.uri';
317
-		$fields[] = 'a.synctoken';
318
-		$fields[] = 'a.components';
319
-		$fields[] = 'a.principaluri';
320
-		$fields[] = 'a.transparent';
321
-		$fields[] = 's.access';
322
-		$query = $this->db->getQueryBuilder();
323
-		$result = $query->select($fields)
324
-			->from('dav_shares', 's')
325
-			->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id'))
326
-			->where($query->expr()->in('s.principaluri', $query->createParameter('principaluri')))
327
-			->andWhere($query->expr()->eq('s.type', $query->createParameter('type')))
328
-			->setParameter('type', 'calendar')
329
-			->setParameter('principaluri', $principals, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY)
330
-			->execute();
331
-
332
-		$readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only';
333
-		while($row = $result->fetch()) {
334
-			if ($row['principaluri'] === $principalUri) {
335
-				continue;
336
-			}
337
-
338
-			$readOnly = (int) $row['access'] === Backend::ACCESS_READ;
339
-			if (isset($calendars[$row['id']])) {
340
-				if ($readOnly) {
341
-					// New share can not have more permissions then the old one.
342
-					continue;
343
-				}
344
-				if (isset($calendars[$row['id']][$readOnlyPropertyName]) &&
345
-					$calendars[$row['id']][$readOnlyPropertyName] === 0) {
346
-					// Old share is already read-write, no more permissions can be gained
347
-					continue;
348
-				}
349
-			}
350
-
351
-			list(, $name) = Uri\split($row['principaluri']);
352
-			$uri = $row['uri'] . '_shared_by_' . $name;
353
-			$row['displayname'] = $row['displayname'] . ' (' . $this->getUserDisplayName($name) . ')';
354
-			$components = [];
355
-			if ($row['components']) {
356
-				$components = explode(',',$row['components']);
357
-			}
358
-			$calendar = [
359
-				'id' => $row['id'],
360
-				'uri' => $uri,
361
-				'principaluri' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint),
362
-				'{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'),
363
-				'{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
364
-				'{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components),
365
-				'{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp('transparent'),
366
-				'{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint),
367
-				$readOnlyPropertyName => $readOnly,
368
-			];
369
-
370
-			foreach($this->propertyMap as $xmlName=>$dbName) {
371
-				$calendar[$xmlName] = $row[$dbName];
372
-			}
373
-
374
-			$this->addOwnerPrincipal($calendar);
375
-
376
-			$calendars[$calendar['id']] = $calendar;
377
-		}
378
-		$result->closeCursor();
379
-
380
-		return array_values($calendars);
381
-	}
382
-
383
-	/**
384
-	 * @param $principalUri
385
-	 * @return array
386
-	 */
387
-	public function getUsersOwnCalendars($principalUri) {
388
-		$principalUri = $this->convertPrincipal($principalUri, true);
389
-		$fields = array_values($this->propertyMap);
390
-		$fields[] = 'id';
391
-		$fields[] = 'uri';
392
-		$fields[] = 'synctoken';
393
-		$fields[] = 'components';
394
-		$fields[] = 'principaluri';
395
-		$fields[] = 'transparent';
396
-		// Making fields a comma-delimited list
397
-		$query = $this->db->getQueryBuilder();
398
-		$query->select($fields)->from('calendars')
399
-			->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri)))
400
-			->orderBy('calendarorder', 'ASC');
401
-		$stmt = $query->execute();
402
-		$calendars = [];
403
-		while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
404
-			$components = [];
405
-			if ($row['components']) {
406
-				$components = explode(',',$row['components']);
407
-			}
408
-			$calendar = [
409
-				'id' => $row['id'],
410
-				'uri' => $row['uri'],
411
-				'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint),
412
-				'{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'),
413
-				'{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
414
-				'{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components),
415
-				'{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'),
416
-			];
417
-			foreach($this->propertyMap as $xmlName=>$dbName) {
418
-				$calendar[$xmlName] = $row[$dbName];
419
-			}
420
-
421
-			$this->addOwnerPrincipal($calendar);
422
-
423
-			if (!isset($calendars[$calendar['id']])) {
424
-				$calendars[$calendar['id']] = $calendar;
425
-			}
426
-		}
427
-		$stmt->closeCursor();
428
-		return array_values($calendars);
429
-	}
430
-
431
-
432
-	/**
433
-	 * @param $uid
434
-	 * @return string
435
-	 */
436
-	private function getUserDisplayName($uid) {
437
-		if (!isset($this->userDisplayNames[$uid])) {
438
-			$user = $this->userManager->get($uid);
439
-
440
-			if ($user instanceof IUser) {
441
-				$this->userDisplayNames[$uid] = $user->getDisplayName();
442
-			} else {
443
-				$this->userDisplayNames[$uid] = $uid;
444
-			}
445
-		}
446
-
447
-		return $this->userDisplayNames[$uid];
448
-	}
449
-
450
-	/**
451
-	 * @return array
452
-	 */
453
-	public function getPublicCalendars() {
454
-		$fields = array_values($this->propertyMap);
455
-		$fields[] = 'a.id';
456
-		$fields[] = 'a.uri';
457
-		$fields[] = 'a.synctoken';
458
-		$fields[] = 'a.components';
459
-		$fields[] = 'a.principaluri';
460
-		$fields[] = 'a.transparent';
461
-		$fields[] = 's.access';
462
-		$fields[] = 's.publicuri';
463
-		$calendars = [];
464
-		$query = $this->db->getQueryBuilder();
465
-		$result = $query->select($fields)
466
-			->from('dav_shares', 's')
467
-			->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id'))
468
-			->where($query->expr()->in('s.access', $query->createNamedParameter(self::ACCESS_PUBLIC)))
469
-			->andWhere($query->expr()->eq('s.type', $query->createNamedParameter('calendar')))
470
-			->execute();
471
-
472
-		while($row = $result->fetch()) {
473
-			list(, $name) = Uri\split($row['principaluri']);
474
-			$row['displayname'] = $row['displayname'] . "($name)";
475
-			$components = [];
476
-			if ($row['components']) {
477
-				$components = explode(',',$row['components']);
478
-			}
479
-			$calendar = [
480
-				'id' => $row['id'],
481
-				'uri' => $row['publicuri'],
482
-				'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint),
483
-				'{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'),
484
-				'{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
485
-				'{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components),
486
-				'{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'),
487
-				'{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], $this->legacyEndpoint),
488
-				'{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ,
489
-				'{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC,
490
-			];
491
-
492
-			foreach($this->propertyMap as $xmlName=>$dbName) {
493
-				$calendar[$xmlName] = $row[$dbName];
494
-			}
495
-
496
-			$this->addOwnerPrincipal($calendar);
497
-
498
-			if (!isset($calendars[$calendar['id']])) {
499
-				$calendars[$calendar['id']] = $calendar;
500
-			}
501
-		}
502
-		$result->closeCursor();
503
-
504
-		return array_values($calendars);
505
-	}
506
-
507
-	/**
508
-	 * @param string $uri
509
-	 * @return array
510
-	 * @throws NotFound
511
-	 */
512
-	public function getPublicCalendar($uri) {
513
-		$fields = array_values($this->propertyMap);
514
-		$fields[] = 'a.id';
515
-		$fields[] = 'a.uri';
516
-		$fields[] = 'a.synctoken';
517
-		$fields[] = 'a.components';
518
-		$fields[] = 'a.principaluri';
519
-		$fields[] = 'a.transparent';
520
-		$fields[] = 's.access';
521
-		$fields[] = 's.publicuri';
522
-		$query = $this->db->getQueryBuilder();
523
-		$result = $query->select($fields)
524
-			->from('dav_shares', 's')
525
-			->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id'))
526
-			->where($query->expr()->in('s.access', $query->createNamedParameter(self::ACCESS_PUBLIC)))
527
-			->andWhere($query->expr()->eq('s.type', $query->createNamedParameter('calendar')))
528
-			->andWhere($query->expr()->eq('s.publicuri', $query->createNamedParameter($uri)))
529
-			->execute();
530
-
531
-		$row = $result->fetch(\PDO::FETCH_ASSOC);
532
-
533
-		$result->closeCursor();
534
-
535
-		if ($row === false) {
536
-			throw new NotFound('Node with name \'' . $uri . '\' could not be found');
537
-		}
538
-
539
-		list(, $name) = Uri\split($row['principaluri']);
540
-		$row['displayname'] = $row['displayname'] . ' ' . "($name)";
541
-		$components = [];
542
-		if ($row['components']) {
543
-			$components = explode(',',$row['components']);
544
-		}
545
-		$calendar = [
546
-			'id' => $row['id'],
547
-			'uri' => $row['publicuri'],
548
-			'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint),
549
-			'{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'),
550
-			'{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
551
-			'{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components),
552
-			'{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'),
553
-			'{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint),
554
-			'{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ,
555
-			'{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC,
556
-		];
557
-
558
-		foreach($this->propertyMap as $xmlName=>$dbName) {
559
-			$calendar[$xmlName] = $row[$dbName];
560
-		}
561
-
562
-		$this->addOwnerPrincipal($calendar);
563
-
564
-		return $calendar;
565
-
566
-	}
567
-
568
-	/**
569
-	 * @param string $principal
570
-	 * @param string $uri
571
-	 * @return array|null
572
-	 */
573
-	public function getCalendarByUri($principal, $uri) {
574
-		$fields = array_values($this->propertyMap);
575
-		$fields[] = 'id';
576
-		$fields[] = 'uri';
577
-		$fields[] = 'synctoken';
578
-		$fields[] = 'components';
579
-		$fields[] = 'principaluri';
580
-		$fields[] = 'transparent';
581
-
582
-		// Making fields a comma-delimited list
583
-		$query = $this->db->getQueryBuilder();
584
-		$query->select($fields)->from('calendars')
585
-			->where($query->expr()->eq('uri', $query->createNamedParameter($uri)))
586
-			->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal)))
587
-			->setMaxResults(1);
588
-		$stmt = $query->execute();
589
-
590
-		$row = $stmt->fetch(\PDO::FETCH_ASSOC);
591
-		$stmt->closeCursor();
592
-		if ($row === false) {
593
-			return null;
594
-		}
595
-
596
-		$components = [];
597
-		if ($row['components']) {
598
-			$components = explode(',',$row['components']);
599
-		}
600
-
601
-		$calendar = [
602
-			'id' => $row['id'],
603
-			'uri' => $row['uri'],
604
-			'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint),
605
-			'{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'),
606
-			'{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
607
-			'{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components),
608
-			'{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'),
609
-		];
610
-
611
-		foreach($this->propertyMap as $xmlName=>$dbName) {
612
-			$calendar[$xmlName] = $row[$dbName];
613
-		}
614
-
615
-		$this->addOwnerPrincipal($calendar);
616
-
617
-		return $calendar;
618
-	}
619
-
620
-	/**
621
-	 * @param $calendarId
622
-	 * @return array|null
623
-	 */
624
-	public function getCalendarById($calendarId) {
625
-		$fields = array_values($this->propertyMap);
626
-		$fields[] = 'id';
627
-		$fields[] = 'uri';
628
-		$fields[] = 'synctoken';
629
-		$fields[] = 'components';
630
-		$fields[] = 'principaluri';
631
-		$fields[] = 'transparent';
632
-
633
-		// Making fields a comma-delimited list
634
-		$query = $this->db->getQueryBuilder();
635
-		$query->select($fields)->from('calendars')
636
-			->where($query->expr()->eq('id', $query->createNamedParameter($calendarId)))
637
-			->setMaxResults(1);
638
-		$stmt = $query->execute();
639
-
640
-		$row = $stmt->fetch(\PDO::FETCH_ASSOC);
641
-		$stmt->closeCursor();
642
-		if ($row === false) {
643
-			return null;
644
-		}
645
-
646
-		$components = [];
647
-		if ($row['components']) {
648
-			$components = explode(',',$row['components']);
649
-		}
650
-
651
-		$calendar = [
652
-			'id' => $row['id'],
653
-			'uri' => $row['uri'],
654
-			'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint),
655
-			'{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'),
656
-			'{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
657
-			'{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components),
658
-			'{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'),
659
-		];
660
-
661
-		foreach($this->propertyMap as $xmlName=>$dbName) {
662
-			$calendar[$xmlName] = $row[$dbName];
663
-		}
664
-
665
-		$this->addOwnerPrincipal($calendar);
666
-
667
-		return $calendar;
668
-	}
669
-
670
-	/**
671
-	 * @param $subscriptionId
672
-	 */
673
-	public function getSubscriptionById($subscriptionId) {
674
-		$fields = array_values($this->subscriptionPropertyMap);
675
-		$fields[] = 'id';
676
-		$fields[] = 'uri';
677
-		$fields[] = 'source';
678
-		$fields[] = 'synctoken';
679
-		$fields[] = 'principaluri';
680
-		$fields[] = 'lastmodified';
681
-
682
-		$query = $this->db->getQueryBuilder();
683
-		$query->select($fields)
684
-			->from('calendarsubscriptions')
685
-			->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId)))
686
-			->orderBy('calendarorder', 'asc');
687
-		$stmt =$query->execute();
688
-
689
-		$row = $stmt->fetch(\PDO::FETCH_ASSOC);
690
-		$stmt->closeCursor();
691
-		if ($row === false) {
692
-			return null;
693
-		}
694
-
695
-		$subscription = [
696
-			'id'           => $row['id'],
697
-			'uri'          => $row['uri'],
698
-			'principaluri' => $row['principaluri'],
699
-			'source'       => $row['source'],
700
-			'lastmodified' => $row['lastmodified'],
701
-			'{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']),
702
-			'{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
703
-		];
704
-
705
-		foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) {
706
-			if (!is_null($row[$dbName])) {
707
-				$subscription[$xmlName] = $row[$dbName];
708
-			}
709
-		}
710
-
711
-		return $subscription;
712
-	}
713
-
714
-	/**
715
-	 * Creates a new calendar for a principal.
716
-	 *
717
-	 * If the creation was a success, an id must be returned that can be used to reference
718
-	 * this calendar in other methods, such as updateCalendar.
719
-	 *
720
-	 * @param string $principalUri
721
-	 * @param string $calendarUri
722
-	 * @param array $properties
723
-	 * @return int
724
-	 * @suppress SqlInjectionChecker
725
-	 */
726
-	function createCalendar($principalUri, $calendarUri, array $properties) {
727
-		$values = [
728
-			'principaluri' => $this->convertPrincipal($principalUri, true),
729
-			'uri'          => $calendarUri,
730
-			'synctoken'    => 1,
731
-			'transparent'  => 0,
732
-			'components'   => 'VEVENT,VTODO',
733
-			'displayname'  => $calendarUri
734
-		];
735
-
736
-		// Default value
737
-		$sccs = '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set';
738
-		if (isset($properties[$sccs])) {
739
-			if (!($properties[$sccs] instanceof SupportedCalendarComponentSet)) {
740
-				throw new DAV\Exception('The ' . $sccs . ' property must be of type: \Sabre\CalDAV\Property\SupportedCalendarComponentSet');
741
-			}
742
-			$values['components'] = implode(',',$properties[$sccs]->getValue());
743
-		} else if (isset($properties['components'])) {
744
-			// Allow to provide components internally without having
745
-			// to create a SupportedCalendarComponentSet object
746
-			$values['components'] = $properties['components'];
747
-		}
748
-
749
-		$transp = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp';
750
-		if (isset($properties[$transp])) {
751
-			$values['transparent'] = (int) ($properties[$transp]->getValue() === 'transparent');
752
-		}
753
-
754
-		foreach($this->propertyMap as $xmlName=>$dbName) {
755
-			if (isset($properties[$xmlName])) {
756
-				$values[$dbName] = $properties[$xmlName];
757
-			}
758
-		}
759
-
760
-		$query = $this->db->getQueryBuilder();
761
-		$query->insert('calendars');
762
-		foreach($values as $column => $value) {
763
-			$query->setValue($column, $query->createNamedParameter($value));
764
-		}
765
-		$query->execute();
766
-		$calendarId = $query->getLastInsertId();
767
-
768
-		$this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::createCalendar', new GenericEvent(
769
-			'\OCA\DAV\CalDAV\CalDavBackend::createCalendar',
770
-			[
771
-				'calendarId' => $calendarId,
772
-				'calendarData' => $this->getCalendarById($calendarId),
773
-		]));
774
-
775
-		return $calendarId;
776
-	}
777
-
778
-	/**
779
-	 * Updates properties for a calendar.
780
-	 *
781
-	 * The list of mutations is stored in a Sabre\DAV\PropPatch object.
782
-	 * To do the actual updates, you must tell this object which properties
783
-	 * you're going to process with the handle() method.
784
-	 *
785
-	 * Calling the handle method is like telling the PropPatch object "I
786
-	 * promise I can handle updating this property".
787
-	 *
788
-	 * Read the PropPatch documentation for more info and examples.
789
-	 *
790
-	 * @param mixed $calendarId
791
-	 * @param PropPatch $propPatch
792
-	 * @return void
793
-	 */
794
-	function updateCalendar($calendarId, PropPatch $propPatch) {
795
-		$supportedProperties = array_keys($this->propertyMap);
796
-		$supportedProperties[] = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp';
797
-
798
-		/**
799
-		 * @suppress SqlInjectionChecker
800
-		 */
801
-		$propPatch->handle($supportedProperties, function($mutations) use ($calendarId) {
802
-			$newValues = [];
803
-			foreach ($mutations as $propertyName => $propertyValue) {
804
-
805
-				switch ($propertyName) {
806
-					case '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' :
807
-						$fieldName = 'transparent';
808
-						$newValues[$fieldName] = (int) ($propertyValue->getValue() === 'transparent');
809
-						break;
810
-					default :
811
-						$fieldName = $this->propertyMap[$propertyName];
812
-						$newValues[$fieldName] = $propertyValue;
813
-						break;
814
-				}
815
-
816
-			}
817
-			$query = $this->db->getQueryBuilder();
818
-			$query->update('calendars');
819
-			foreach ($newValues as $fieldName => $value) {
820
-				$query->set($fieldName, $query->createNamedParameter($value));
821
-			}
822
-			$query->where($query->expr()->eq('id', $query->createNamedParameter($calendarId)));
823
-			$query->execute();
824
-
825
-			$this->addChange($calendarId, "", 2);
826
-
827
-			$this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateCalendar', new GenericEvent(
828
-				'\OCA\DAV\CalDAV\CalDavBackend::updateCalendar',
829
-				[
830
-					'calendarId' => $calendarId,
831
-					'calendarData' => $this->getCalendarById($calendarId),
832
-					'shares' => $this->getShares($calendarId),
833
-					'propertyMutations' => $mutations,
834
-			]));
835
-
836
-			return true;
837
-		});
838
-	}
839
-
840
-	/**
841
-	 * Delete a calendar and all it's objects
842
-	 *
843
-	 * @param mixed $calendarId
844
-	 * @return void
845
-	 */
846
-	function deleteCalendar($calendarId) {
847
-		$this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendar', new GenericEvent(
848
-			'\OCA\DAV\CalDAV\CalDavBackend::deleteCalendar',
849
-			[
850
-				'calendarId' => $calendarId,
851
-				'calendarData' => $this->getCalendarById($calendarId),
852
-				'shares' => $this->getShares($calendarId),
853
-		]));
854
-
855
-		$stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ? AND `calendartype` = ?');
856
-		$stmt->execute([$calendarId, self::CALENDAR_TYPE_CALENDAR]);
857
-
858
-		$stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendars` WHERE `id` = ?');
859
-		$stmt->execute([$calendarId]);
860
-
861
-		$stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarchanges` WHERE `calendarid` = ? AND `calendartype` = ?');
862
-		$stmt->execute([$calendarId, self::CALENDAR_TYPE_CALENDAR]);
863
-
864
-		$this->calendarSharingBackend->deleteAllShares($calendarId);
865
-
866
-		$query = $this->db->getQueryBuilder();
867
-		$query->delete($this->dbObjectPropertiesTable)
868
-			->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId)))
869
-			->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_CALENDAR)))
870
-			->execute();
871
-	}
872
-
873
-	/**
874
-	 * Delete all of an user's shares
875
-	 *
876
-	 * @param string $principaluri
877
-	 * @return void
878
-	 */
879
-	function deleteAllSharesByUser($principaluri) {
880
-		$this->calendarSharingBackend->deleteAllSharesByUser($principaluri);
881
-	}
882
-
883
-	/**
884
-	 * Returns all calendar objects within a calendar.
885
-	 *
886
-	 * Every item contains an array with the following keys:
887
-	 *   * calendardata - The iCalendar-compatible calendar data
888
-	 *   * uri - a unique key which will be used to construct the uri. This can
889
-	 *     be any arbitrary string, but making sure it ends with '.ics' is a
890
-	 *     good idea. This is only the basename, or filename, not the full
891
-	 *     path.
892
-	 *   * lastmodified - a timestamp of the last modification time
893
-	 *   * etag - An arbitrary string, surrounded by double-quotes. (e.g.:
894
-	 *   '"abcdef"')
895
-	 *   * size - The size of the calendar objects, in bytes.
896
-	 *   * component - optional, a string containing the type of object, such
897
-	 *     as 'vevent' or 'vtodo'. If specified, this will be used to populate
898
-	 *     the Content-Type header.
899
-	 *
900
-	 * Note that the etag is optional, but it's highly encouraged to return for
901
-	 * speed reasons.
902
-	 *
903
-	 * The calendardata is also optional. If it's not returned
904
-	 * 'getCalendarObject' will be called later, which *is* expected to return
905
-	 * calendardata.
906
-	 *
907
-	 * If neither etag or size are specified, the calendardata will be
908
-	 * used/fetched to determine these numbers. If both are specified the
909
-	 * amount of times this is needed is reduced by a great degree.
910
-	 *
911
-	 * @param mixed $id
912
-	 * @param int $calendarType
913
-	 * @return array
914
-	 */
915
-	public function getCalendarObjects($id, $calendarType=self::CALENDAR_TYPE_CALENDAR):array {
916
-		$query = $this->db->getQueryBuilder();
917
-		$query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'componenttype', 'classification'])
918
-			->from('calendarobjects')
919
-			->where($query->expr()->eq('calendarid', $query->createNamedParameter($id)))
920
-			->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType)));
921
-		$stmt = $query->execute();
922
-
923
-		$result = [];
924
-		foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) {
925
-			$result[] = [
926
-				'id'           => $row['id'],
927
-				'uri'          => $row['uri'],
928
-				'lastmodified' => $row['lastmodified'],
929
-				'etag'         => '"' . $row['etag'] . '"',
930
-				'calendarid'   => $row['calendarid'],
931
-				'size'         => (int)$row['size'],
932
-				'component'    => strtolower($row['componenttype']),
933
-				'classification'=> (int)$row['classification']
934
-			];
935
-		}
936
-
937
-		return $result;
938
-	}
939
-
940
-	/**
941
-	 * Returns information from a single calendar object, based on it's object
942
-	 * uri.
943
-	 *
944
-	 * The object uri is only the basename, or filename and not a full path.
945
-	 *
946
-	 * The returned array must have the same keys as getCalendarObjects. The
947
-	 * 'calendardata' object is required here though, while it's not required
948
-	 * for getCalendarObjects.
949
-	 *
950
-	 * This method must return null if the object did not exist.
951
-	 *
952
-	 * @param mixed $id
953
-	 * @param string $objectUri
954
-	 * @param int $calendarType
955
-	 * @return array|null
956
-	 */
957
-	public function getCalendarObject($id, $objectUri, $calendarType=self::CALENDAR_TYPE_CALENDAR) {
958
-		$query = $this->db->getQueryBuilder();
959
-		$query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification'])
960
-			->from('calendarobjects')
961
-			->where($query->expr()->eq('calendarid', $query->createNamedParameter($id)))
962
-			->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri)))
963
-			->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType)));
964
-		$stmt = $query->execute();
965
-		$row = $stmt->fetch(\PDO::FETCH_ASSOC);
966
-
967
-		if(!$row) {
968
-			return null;
969
-		}
970
-
971
-		return [
972
-			'id'            => $row['id'],
973
-			'uri'           => $row['uri'],
974
-			'lastmodified'  => $row['lastmodified'],
975
-			'etag'          => '"' . $row['etag'] . '"',
976
-			'calendarid'    => $row['calendarid'],
977
-			'size'          => (int)$row['size'],
978
-			'calendardata'  => $this->readBlob($row['calendardata']),
979
-			'component'     => strtolower($row['componenttype']),
980
-			'classification'=> (int)$row['classification']
981
-		];
982
-	}
983
-
984
-	/**
985
-	 * Returns a list of calendar objects.
986
-	 *
987
-	 * This method should work identical to getCalendarObject, but instead
988
-	 * return all the calendar objects in the list as an array.
989
-	 *
990
-	 * If the backend supports this, it may allow for some speed-ups.
991
-	 *
992
-	 * @param mixed $calendarId
993
-	 * @param string[] $uris
994
-	 * @param int $calendarType
995
-	 * @return array
996
-	 */
997
-	public function getMultipleCalendarObjects($id, array $uris, $calendarType=self::CALENDAR_TYPE_CALENDAR):array {
998
-		if (empty($uris)) {
999
-			return [];
1000
-		}
1001
-
1002
-		$chunks = array_chunk($uris, 100);
1003
-		$objects = [];
1004
-
1005
-		$query = $this->db->getQueryBuilder();
1006
-		$query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification'])
1007
-			->from('calendarobjects')
1008
-			->where($query->expr()->eq('calendarid', $query->createNamedParameter($id)))
1009
-			->andWhere($query->expr()->in('uri', $query->createParameter('uri')))
1010
-			->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType)));
1011
-
1012
-		foreach ($chunks as $uris) {
1013
-			$query->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY);
1014
-			$result = $query->execute();
1015
-
1016
-			while ($row = $result->fetch()) {
1017
-				$objects[] = [
1018
-					'id'           => $row['id'],
1019
-					'uri'          => $row['uri'],
1020
-					'lastmodified' => $row['lastmodified'],
1021
-					'etag'         => '"' . $row['etag'] . '"',
1022
-					'calendarid'   => $row['calendarid'],
1023
-					'size'         => (int)$row['size'],
1024
-					'calendardata' => $this->readBlob($row['calendardata']),
1025
-					'component'    => strtolower($row['componenttype']),
1026
-					'classification' => (int)$row['classification']
1027
-				];
1028
-			}
1029
-			$result->closeCursor();
1030
-		}
1031
-
1032
-		return $objects;
1033
-	}
1034
-
1035
-	/**
1036
-	 * Creates a new calendar object.
1037
-	 *
1038
-	 * The object uri is only the basename, or filename and not a full path.
1039
-	 *
1040
-	 * It is possible return an etag from this function, which will be used in
1041
-	 * the response to this PUT request. Note that the ETag must be surrounded
1042
-	 * by double-quotes.
1043
-	 *
1044
-	 * However, you should only really return this ETag if you don't mangle the
1045
-	 * calendar-data. If the result of a subsequent GET to this object is not
1046
-	 * the exact same as this request body, you should omit the ETag.
1047
-	 *
1048
-	 * @param mixed $calendarId
1049
-	 * @param string $objectUri
1050
-	 * @param string $calendarData
1051
-	 * @param int $calendarType
1052
-	 * @return string
1053
-	 */
1054
-	function createCalendarObject($calendarId, $objectUri, $calendarData, $calendarType=self::CALENDAR_TYPE_CALENDAR) {
1055
-		$extraData = $this->getDenormalizedData($calendarData);
1056
-
1057
-		$q = $this->db->getQueryBuilder();
1058
-		$q->select($q->func()->count('*'))
1059
-			->from('calendarobjects')
1060
-			->where($q->expr()->eq('calendarid', $q->createNamedParameter($calendarId)))
1061
-			->andWhere($q->expr()->eq('uid', $q->createNamedParameter($extraData['uid'])))
1062
-			->andWhere($q->expr()->eq('calendartype', $q->createNamedParameter($calendarType)));
1063
-
1064
-		$result = $q->execute();
1065
-		$count = (int) $result->fetchColumn();
1066
-		$result->closeCursor();
1067
-
1068
-		if ($count !== 0) {
1069
-			throw new \Sabre\DAV\Exception\BadRequest('Calendar object with uid already exists in this calendar collection.');
1070
-		}
1071
-
1072
-		$query = $this->db->getQueryBuilder();
1073
-		$query->insert('calendarobjects')
1074
-			->values([
1075
-				'calendarid' => $query->createNamedParameter($calendarId),
1076
-				'uri' => $query->createNamedParameter($objectUri),
1077
-				'calendardata' => $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB),
1078
-				'lastmodified' => $query->createNamedParameter(time()),
1079
-				'etag' => $query->createNamedParameter($extraData['etag']),
1080
-				'size' => $query->createNamedParameter($extraData['size']),
1081
-				'componenttype' => $query->createNamedParameter($extraData['componentType']),
1082
-				'firstoccurence' => $query->createNamedParameter($extraData['firstOccurence']),
1083
-				'lastoccurence' => $query->createNamedParameter($extraData['lastOccurence']),
1084
-				'classification' => $query->createNamedParameter($extraData['classification']),
1085
-				'uid' => $query->createNamedParameter($extraData['uid']),
1086
-				'calendartype' => $query->createNamedParameter($calendarType),
1087
-			])
1088
-			->execute();
1089
-
1090
-		$this->updateProperties($calendarId, $objectUri, $calendarData, $calendarType);
1091
-
1092
-		if ($calendarType === self::CALENDAR_TYPE_CALENDAR) {
1093
-			$this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject', new GenericEvent(
1094
-				'\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject',
1095
-				[
1096
-					'calendarId' => $calendarId,
1097
-					'calendarData' => $this->getCalendarById($calendarId),
1098
-					'shares' => $this->getShares($calendarId),
1099
-					'objectData' => $this->getCalendarObject($calendarId, $objectUri),
1100
-				]
1101
-			));
1102
-		} else {
1103
-			$this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::createCachedCalendarObject', new GenericEvent(
1104
-				'\OCA\DAV\CalDAV\CalDavBackend::createCachedCalendarObject',
1105
-				[
1106
-					'subscriptionId' => $calendarId,
1107
-					'calendarData' => $this->getCalendarById($calendarId),
1108
-					'shares' => $this->getShares($calendarId),
1109
-					'objectData' => $this->getCalendarObject($calendarId, $objectUri),
1110
-				]
1111
-			));
1112
-		}
1113
-		$this->addChange($calendarId, $objectUri, 1, $calendarType);
1114
-
1115
-		return '"' . $extraData['etag'] . '"';
1116
-	}
1117
-
1118
-	/**
1119
-	 * Updates an existing calendarobject, based on it's uri.
1120
-	 *
1121
-	 * The object uri is only the basename, or filename and not a full path.
1122
-	 *
1123
-	 * It is possible return an etag from this function, which will be used in
1124
-	 * the response to this PUT request. Note that the ETag must be surrounded
1125
-	 * by double-quotes.
1126
-	 *
1127
-	 * However, you should only really return this ETag if you don't mangle the
1128
-	 * calendar-data. If the result of a subsequent GET to this object is not
1129
-	 * the exact same as this request body, you should omit the ETag.
1130
-	 *
1131
-	 * @param mixed $calendarId
1132
-	 * @param string $objectUri
1133
-	 * @param string $calendarData
1134
-	 * @param int $calendarType
1135
-	 * @return string
1136
-	 */
1137
-	function updateCalendarObject($calendarId, $objectUri, $calendarData, $calendarType=self::CALENDAR_TYPE_CALENDAR) {
1138
-		$extraData = $this->getDenormalizedData($calendarData);
1139
-		$query = $this->db->getQueryBuilder();
1140
-		$query->update('calendarobjects')
1141
-				->set('calendardata', $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB))
1142
-				->set('lastmodified', $query->createNamedParameter(time()))
1143
-				->set('etag', $query->createNamedParameter($extraData['etag']))
1144
-				->set('size', $query->createNamedParameter($extraData['size']))
1145
-				->set('componenttype', $query->createNamedParameter($extraData['componentType']))
1146
-				->set('firstoccurence', $query->createNamedParameter($extraData['firstOccurence']))
1147
-				->set('lastoccurence', $query->createNamedParameter($extraData['lastOccurence']))
1148
-				->set('classification', $query->createNamedParameter($extraData['classification']))
1149
-				->set('uid', $query->createNamedParameter($extraData['uid']))
1150
-			->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId)))
1151
-			->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri)))
1152
-			->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType)))
1153
-			->execute();
1154
-
1155
-		$this->updateProperties($calendarId, $objectUri, $calendarData, $calendarType);
1156
-
1157
-		$data = $this->getCalendarObject($calendarId, $objectUri);
1158
-		if (is_array($data)) {
1159
-			if ($calendarType === self::CALENDAR_TYPE_CALENDAR) {
1160
-				$this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject', new GenericEvent(
1161
-					'\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject',
1162
-					[
1163
-						'calendarId' => $calendarId,
1164
-						'calendarData' => $this->getCalendarById($calendarId),
1165
-						'shares' => $this->getShares($calendarId),
1166
-						'objectData' => $data,
1167
-					]
1168
-				));
1169
-			} else {
1170
-				$this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateCachedCalendarObject', new GenericEvent(
1171
-					'\OCA\DAV\CalDAV\CalDavBackend::updateCachedCalendarObject',
1172
-					[
1173
-						'subscriptionId' => $calendarId,
1174
-						'calendarData' => $this->getCalendarById($calendarId),
1175
-						'shares' => $this->getShares($calendarId),
1176
-						'objectData' => $data,
1177
-					]
1178
-				));
1179
-			}
1180
-		}
1181
-		$this->addChange($calendarId, $objectUri, 2, $calendarType);
1182
-
1183
-		return '"' . $extraData['etag'] . '"';
1184
-	}
1185
-
1186
-	/**
1187
-	 * @param int $calendarObjectId
1188
-	 * @param int $classification
1189
-	 */
1190
-	public function setClassification($calendarObjectId, $classification) {
1191
-		if (!in_array($classification, [
1192
-			self::CLASSIFICATION_PUBLIC, self::CLASSIFICATION_PRIVATE, self::CLASSIFICATION_CONFIDENTIAL
1193
-		])) {
1194
-			throw new \InvalidArgumentException();
1195
-		}
1196
-		$query = $this->db->getQueryBuilder();
1197
-		$query->update('calendarobjects')
1198
-			->set('classification', $query->createNamedParameter($classification))
1199
-			->where($query->expr()->eq('id', $query->createNamedParameter($calendarObjectId)))
1200
-			->execute();
1201
-	}
1202
-
1203
-	/**
1204
-	 * Deletes an existing calendar object.
1205
-	 *
1206
-	 * The object uri is only the basename, or filename and not a full path.
1207
-	 *
1208
-	 * @param mixed $calendarId
1209
-	 * @param string $objectUri
1210
-	 * @param int $calendarType
1211
-	 * @return void
1212
-	 */
1213
-	function deleteCalendarObject($calendarId, $objectUri, $calendarType=self::CALENDAR_TYPE_CALENDAR) {
1214
-		$data = $this->getCalendarObject($calendarId, $objectUri, $calendarType);
1215
-		if (is_array($data)) {
1216
-			if ($calendarType === self::CALENDAR_TYPE_CALENDAR) {
1217
-				$this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject', new GenericEvent(
1218
-					'\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject',
1219
-					[
1220
-						'calendarId' => $calendarId,
1221
-						'calendarData' => $this->getCalendarById($calendarId),
1222
-						'shares' => $this->getShares($calendarId),
1223
-						'objectData' => $data,
1224
-					]
1225
-				));
1226
-			} else {
1227
-				$this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::deleteCachedCalendarObject', new GenericEvent(
1228
-					'\OCA\DAV\CalDAV\CalDavBackend::deleteCachedCalendarObject',
1229
-					[
1230
-						'subscriptionId' => $calendarId,
1231
-						'calendarData' => $this->getCalendarById($calendarId),
1232
-						'shares' => $this->getShares($calendarId),
1233
-						'objectData' => $data,
1234
-					]
1235
-				));
1236
-			}
1237
-		}
1238
-
1239
-		$stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ? AND `uri` = ? AND `calendartype` = ?');
1240
-		$stmt->execute([$calendarId, $objectUri, $calendarType]);
1241
-
1242
-		if (is_array($data)) {
1243
-			$this->purgeProperties($calendarId, $data['id'], $calendarType);
1244
-		}
1245
-
1246
-		$this->addChange($calendarId, $objectUri, 3, $calendarType);
1247
-	}
1248
-
1249
-	/**
1250
-	 * Performs a calendar-query on the contents of this calendar.
1251
-	 *
1252
-	 * The calendar-query is defined in RFC4791 : CalDAV. Using the
1253
-	 * calendar-query it is possible for a client to request a specific set of
1254
-	 * object, based on contents of iCalendar properties, date-ranges and
1255
-	 * iCalendar component types (VTODO, VEVENT).
1256
-	 *
1257
-	 * This method should just return a list of (relative) urls that match this
1258
-	 * query.
1259
-	 *
1260
-	 * The list of filters are specified as an array. The exact array is
1261
-	 * documented by Sabre\CalDAV\CalendarQueryParser.
1262
-	 *
1263
-	 * Note that it is extremely likely that getCalendarObject for every path
1264
-	 * returned from this method will be called almost immediately after. You
1265
-	 * may want to anticipate this to speed up these requests.
1266
-	 *
1267
-	 * This method provides a default implementation, which parses *all* the
1268
-	 * iCalendar objects in the specified calendar.
1269
-	 *
1270
-	 * This default may well be good enough for personal use, and calendars
1271
-	 * that aren't very large. But if you anticipate high usage, big calendars
1272
-	 * or high loads, you are strongly advised to optimize certain paths.
1273
-	 *
1274
-	 * The best way to do so is override this method and to optimize
1275
-	 * specifically for 'common filters'.
1276
-	 *
1277
-	 * Requests that are extremely common are:
1278
-	 *   * requests for just VEVENTS
1279
-	 *   * requests for just VTODO
1280
-	 *   * requests with a time-range-filter on either VEVENT or VTODO.
1281
-	 *
1282
-	 * ..and combinations of these requests. It may not be worth it to try to
1283
-	 * handle every possible situation and just rely on the (relatively
1284
-	 * easy to use) CalendarQueryValidator to handle the rest.
1285
-	 *
1286
-	 * Note that especially time-range-filters may be difficult to parse. A
1287
-	 * time-range filter specified on a VEVENT must for instance also handle
1288
-	 * recurrence rules correctly.
1289
-	 * A good example of how to interprete all these filters can also simply
1290
-	 * be found in Sabre\CalDAV\CalendarQueryFilter. This class is as correct
1291
-	 * as possible, so it gives you a good idea on what type of stuff you need
1292
-	 * to think of.
1293
-	 *
1294
-	 * @param mixed $id
1295
-	 * @param array $filters
1296
-	 * @param int $calendarType
1297
-	 * @return array
1298
-	 */
1299
-	public function calendarQuery($id, array $filters, $calendarType=self::CALENDAR_TYPE_CALENDAR):array {
1300
-		$componentType = null;
1301
-		$requirePostFilter = true;
1302
-		$timeRange = null;
1303
-
1304
-		// if no filters were specified, we don't need to filter after a query
1305
-		if (!$filters['prop-filters'] && !$filters['comp-filters']) {
1306
-			$requirePostFilter = false;
1307
-		}
1308
-
1309
-		// Figuring out if there's a component filter
1310
-		if (count($filters['comp-filters']) > 0 && !$filters['comp-filters'][0]['is-not-defined']) {
1311
-			$componentType = $filters['comp-filters'][0]['name'];
1312
-
1313
-			// Checking if we need post-filters
1314
-			if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['time-range'] && !$filters['comp-filters'][0]['prop-filters']) {
1315
-				$requirePostFilter = false;
1316
-			}
1317
-			// There was a time-range filter
1318
-			if ($componentType === 'VEVENT' && isset($filters['comp-filters'][0]['time-range'])) {
1319
-				$timeRange = $filters['comp-filters'][0]['time-range'];
1320
-
1321
-				// If start time OR the end time is not specified, we can do a
1322
-				// 100% accurate mysql query.
1323
-				if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['prop-filters'] && (!$timeRange['start'] || !$timeRange['end'])) {
1324
-					$requirePostFilter = false;
1325
-				}
1326
-			}
1327
-
1328
-		}
1329
-		$columns = ['uri'];
1330
-		if ($requirePostFilter) {
1331
-			$columns = ['uri', 'calendardata'];
1332
-		}
1333
-		$query = $this->db->getQueryBuilder();
1334
-		$query->select($columns)
1335
-			->from('calendarobjects')
1336
-			->where($query->expr()->eq('calendarid', $query->createNamedParameter($id)))
1337
-			->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType)));
1338
-
1339
-		if ($componentType) {
1340
-			$query->andWhere($query->expr()->eq('componenttype', $query->createNamedParameter($componentType)));
1341
-		}
1342
-
1343
-		if ($timeRange && $timeRange['start']) {
1344
-			$query->andWhere($query->expr()->gt('lastoccurence', $query->createNamedParameter($timeRange['start']->getTimeStamp())));
1345
-		}
1346
-		if ($timeRange && $timeRange['end']) {
1347
-			$query->andWhere($query->expr()->lt('firstoccurence', $query->createNamedParameter($timeRange['end']->getTimeStamp())));
1348
-		}
1349
-
1350
-		$stmt = $query->execute();
1351
-
1352
-		$result = [];
1353
-		while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
1354
-			if ($requirePostFilter) {
1355
-				// validateFilterForObject will parse the calendar data
1356
-				// catch parsing errors
1357
-				try {
1358
-					$matches = $this->validateFilterForObject($row, $filters);
1359
-				} catch(ParseException $ex) {
1360
-					$this->logger->logException($ex, [
1361
-						'app' => 'dav',
1362
-						'message' => 'Caught parsing exception for calendar data. This usually indicates invalid calendar data. calendar-id:'.$id.' uri:'.$row['uri']
1363
-					]);
1364
-					continue;
1365
-				} catch (InvalidDataException $ex) {
1366
-					$this->logger->logException($ex, [
1367
-						'app' => 'dav',
1368
-						'message' => 'Caught invalid data exception for calendar data. This usually indicates invalid calendar data. calendar-id:'.$id.' uri:'.$row['uri']
1369
-					]);
1370
-					continue;
1371
-				}
1372
-
1373
-				if (!$matches) {
1374
-					continue;
1375
-				}
1376
-			}
1377
-			$result[] = $row['uri'];
1378
-		}
1379
-
1380
-		return $result;
1381
-	}
1382
-
1383
-	/**
1384
-	 * custom Nextcloud search extension for CalDAV
1385
-	 *
1386
-	 * TODO - this should optionally cover cached calendar objects as well
1387
-	 *
1388
-	 * @param string $principalUri
1389
-	 * @param array $filters
1390
-	 * @param integer|null $limit
1391
-	 * @param integer|null $offset
1392
-	 * @return array
1393
-	 */
1394
-	public function calendarSearch($principalUri, array $filters, $limit=null, $offset=null) {
1395
-		$calendars = $this->getCalendarsForUser($principalUri);
1396
-		$ownCalendars = [];
1397
-		$sharedCalendars = [];
1398
-
1399
-		$uriMapper = [];
1400
-
1401
-		foreach($calendars as $calendar) {
1402
-			if ($calendar['{http://owncloud.org/ns}owner-principal'] === $principalUri) {
1403
-				$ownCalendars[] = $calendar['id'];
1404
-			} else {
1405
-				$sharedCalendars[] = $calendar['id'];
1406
-			}
1407
-			$uriMapper[$calendar['id']] = $calendar['uri'];
1408
-		}
1409
-		if (count($ownCalendars) === 0 && count($sharedCalendars) === 0) {
1410
-			return [];
1411
-		}
1412
-
1413
-		$query = $this->db->getQueryBuilder();
1414
-		// Calendar id expressions
1415
-		$calendarExpressions = [];
1416
-		foreach($ownCalendars as $id) {
1417
-			$calendarExpressions[] = $query->expr()->andX(
1418
-				$query->expr()->eq('c.calendarid',
1419
-					$query->createNamedParameter($id)),
1420
-				$query->expr()->eq('c.calendartype',
1421
-						$query->createNamedParameter(self::CALENDAR_TYPE_CALENDAR)));
1422
-		}
1423
-		foreach($sharedCalendars as $id) {
1424
-			$calendarExpressions[] = $query->expr()->andX(
1425
-				$query->expr()->eq('c.calendarid',
1426
-					$query->createNamedParameter($id)),
1427
-				$query->expr()->eq('c.classification',
1428
-					$query->createNamedParameter(self::CLASSIFICATION_PUBLIC)),
1429
-				$query->expr()->eq('c.calendartype',
1430
-					$query->createNamedParameter(self::CALENDAR_TYPE_CALENDAR)));
1431
-		}
1432
-
1433
-		if (count($calendarExpressions) === 1) {
1434
-			$calExpr = $calendarExpressions[0];
1435
-		} else {
1436
-			$calExpr = call_user_func_array([$query->expr(), 'orX'], $calendarExpressions);
1437
-		}
1438
-
1439
-		// Component expressions
1440
-		$compExpressions = [];
1441
-		foreach($filters['comps'] as $comp) {
1442
-			$compExpressions[] = $query->expr()
1443
-				->eq('c.componenttype', $query->createNamedParameter($comp));
1444
-		}
1445
-
1446
-		if (count($compExpressions) === 1) {
1447
-			$compExpr = $compExpressions[0];
1448
-		} else {
1449
-			$compExpr = call_user_func_array([$query->expr(), 'orX'], $compExpressions);
1450
-		}
1451
-
1452
-		if (!isset($filters['props'])) {
1453
-			$filters['props'] = [];
1454
-		}
1455
-		if (!isset($filters['params'])) {
1456
-			$filters['params'] = [];
1457
-		}
1458
-
1459
-		$propParamExpressions = [];
1460
-		foreach($filters['props'] as $prop) {
1461
-			$propParamExpressions[] = $query->expr()->andX(
1462
-				$query->expr()->eq('i.name', $query->createNamedParameter($prop)),
1463
-				$query->expr()->isNull('i.parameter')
1464
-			);
1465
-		}
1466
-		foreach($filters['params'] as $param) {
1467
-			$propParamExpressions[] = $query->expr()->andX(
1468
-				$query->expr()->eq('i.name', $query->createNamedParameter($param['property'])),
1469
-				$query->expr()->eq('i.parameter', $query->createNamedParameter($param['parameter']))
1470
-			);
1471
-		}
1472
-
1473
-		if (count($propParamExpressions) === 1) {
1474
-			$propParamExpr = $propParamExpressions[0];
1475
-		} else {
1476
-			$propParamExpr = call_user_func_array([$query->expr(), 'orX'], $propParamExpressions);
1477
-		}
1478
-
1479
-		$query->select(['c.calendarid', 'c.uri'])
1480
-			->from($this->dbObjectPropertiesTable, 'i')
1481
-			->join('i', 'calendarobjects', 'c', $query->expr()->eq('i.objectid', 'c.id'))
1482
-			->where($calExpr)
1483
-			->andWhere($compExpr)
1484
-			->andWhere($propParamExpr)
1485
-			->andWhere($query->expr()->iLike('i.value',
1486
-				$query->createNamedParameter('%'.$this->db->escapeLikeParameter($filters['search-term']).'%')));
1487
-
1488
-		if ($offset) {
1489
-			$query->setFirstResult($offset);
1490
-		}
1491
-		if ($limit) {
1492
-			$query->setMaxResults($limit);
1493
-		}
1494
-
1495
-		$stmt = $query->execute();
1496
-
1497
-		$result = [];
1498
-		while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
1499
-			$path = $uriMapper[$row['calendarid']] . '/' . $row['uri'];
1500
-			if (!in_array($path, $result)) {
1501
-				$result[] = $path;
1502
-			}
1503
-		}
1504
-
1505
-		return $result;
1506
-	}
1507
-
1508
-	/**
1509
-	 * used for Nextcloud's calendar API
1510
-	 *
1511
-	 * @param array $calendarInfo
1512
-	 * @param string $pattern
1513
-	 * @param array $searchProperties
1514
-	 * @param array $options
1515
-	 * @param integer|null $limit
1516
-	 * @param integer|null $offset
1517
-	 *
1518
-	 * @return array
1519
-	 */
1520
-	public function search(array $calendarInfo, $pattern, array $searchProperties,
1521
-						   array $options, $limit, $offset) {
1522
-		$outerQuery = $this->db->getQueryBuilder();
1523
-		$innerQuery = $this->db->getQueryBuilder();
1524
-
1525
-		$innerQuery->selectDistinct('op.objectid')
1526
-			->from($this->dbObjectPropertiesTable, 'op')
1527
-			->andWhere($innerQuery->expr()->eq('op.calendarid',
1528
-				$outerQuery->createNamedParameter($calendarInfo['id'])))
1529
-			->andWhere($innerQuery->expr()->eq('op.calendartype',
1530
-				$outerQuery->createNamedParameter(self::CALENDAR_TYPE_CALENDAR)));
1531
-
1532
-		// only return public items for shared calendars for now
1533
-		if ($calendarInfo['principaluri'] !== $calendarInfo['{http://owncloud.org/ns}owner-principal']) {
1534
-			$innerQuery->andWhere($innerQuery->expr()->eq('c.classification',
1535
-				$outerQuery->createNamedParameter(self::CLASSIFICATION_PUBLIC)));
1536
-		}
1537
-
1538
-		$or = $innerQuery->expr()->orX();
1539
-		foreach($searchProperties as $searchProperty) {
1540
-			$or->add($innerQuery->expr()->eq('op.name',
1541
-				$outerQuery->createNamedParameter($searchProperty)));
1542
-		}
1543
-		$innerQuery->andWhere($or);
1544
-
1545
-		if ($pattern !== '') {
1546
-			$innerQuery->andWhere($innerQuery->expr()->iLike('op.value',
1547
-				$outerQuery->createNamedParameter('%' .
1548
-					$this->db->escapeLikeParameter($pattern) . '%')));
1549
-		}
1550
-
1551
-		$outerQuery->select('c.id', 'c.calendardata', 'c.componenttype', 'c.uid', 'c.uri')
1552
-			->from('calendarobjects', 'c');
1553
-
1554
-		if (isset($options['timerange'])) {
1555
-			if (isset($options['timerange']['start']) && $options['timerange']['start'] instanceof DateTime) {
1556
-				$outerQuery->andWhere($outerQuery->expr()->gt('lastoccurence',
1557
-					$outerQuery->createNamedParameter($options['timerange']['start']->getTimeStamp())));
1558
-
1559
-			}
1560
-			if (isset($options['timerange']['end']) && $options['timerange']['end'] instanceof DateTime) {
1561
-				$outerQuery->andWhere($outerQuery->expr()->lt('firstoccurence',
1562
-					$outerQuery->createNamedParameter($options['timerange']['end']->getTimeStamp())));
1563
-			}
1564
-		}
1565
-
1566
-		if (isset($options['types'])) {
1567
-			$or = $outerQuery->expr()->orX();
1568
-			foreach($options['types'] as $type) {
1569
-				$or->add($outerQuery->expr()->eq('componenttype',
1570
-					$outerQuery->createNamedParameter($type)));
1571
-			}
1572
-			$outerQuery->andWhere($or);
1573
-		}
1574
-
1575
-		$outerQuery->andWhere($outerQuery->expr()->in('c.id',
1576
-			$outerQuery->createFunction($innerQuery->getSQL())));
1577
-
1578
-		if ($offset) {
1579
-			$outerQuery->setFirstResult($offset);
1580
-		}
1581
-		if ($limit) {
1582
-			$outerQuery->setMaxResults($limit);
1583
-		}
1584
-
1585
-		$result = $outerQuery->execute();
1586
-		$calendarObjects = $result->fetchAll();
1587
-
1588
-		return array_map(function($o) {
1589
-			$calendarData = Reader::read($o['calendardata']);
1590
-			$comps = $calendarData->getComponents();
1591
-			$objects = [];
1592
-			$timezones = [];
1593
-			foreach($comps as $comp) {
1594
-				if ($comp instanceof VTimeZone) {
1595
-					$timezones[] = $comp;
1596
-				} else {
1597
-					$objects[] = $comp;
1598
-				}
1599
-			}
1600
-
1601
-			return [
1602
-				'id' => $o['id'],
1603
-				'type' => $o['componenttype'],
1604
-				'uid' => $o['uid'],
1605
-				'uri' => $o['uri'],
1606
-				'objects' => array_map(function($c) {
1607
-					return $this->transformSearchData($c);
1608
-				}, $objects),
1609
-				'timezones' => array_map(function($c) {
1610
-					return $this->transformSearchData($c);
1611
-				}, $timezones),
1612
-			];
1613
-		}, $calendarObjects);
1614
-	}
1615
-
1616
-	/**
1617
-	 * @param Component $comp
1618
-	 * @return array
1619
-	 */
1620
-	private function transformSearchData(Component $comp) {
1621
-		$data = [];
1622
-		/** @var Component[] $subComponents */
1623
-		$subComponents = $comp->getComponents();
1624
-		/** @var Property[] $properties */
1625
-		$properties = array_filter($comp->children(), function($c) {
1626
-			return $c instanceof Property;
1627
-		});
1628
-		$validationRules = $comp->getValidationRules();
1629
-
1630
-		foreach($subComponents as $subComponent) {
1631
-			$name = $subComponent->name;
1632
-			if (!isset($data[$name])) {
1633
-				$data[$name] = [];
1634
-			}
1635
-			$data[$name][] = $this->transformSearchData($subComponent);
1636
-		}
1637
-
1638
-		foreach($properties as $property) {
1639
-			$name = $property->name;
1640
-			if (!isset($validationRules[$name])) {
1641
-				$validationRules[$name] = '*';
1642
-			}
1643
-
1644
-			$rule = $validationRules[$property->name];
1645
-			if ($rule === '+' || $rule === '*') { // multiple
1646
-				if (!isset($data[$name])) {
1647
-					$data[$name] = [];
1648
-				}
1649
-
1650
-				$data[$name][] = $this->transformSearchProperty($property);
1651
-			} else { // once
1652
-				$data[$name] = $this->transformSearchProperty($property);
1653
-			}
1654
-		}
1655
-
1656
-		return $data;
1657
-	}
1658
-
1659
-	/**
1660
-	 * @param Property $prop
1661
-	 * @return array
1662
-	 */
1663
-	private function transformSearchProperty(Property $prop) {
1664
-		// No need to check Date, as it extends DateTime
1665
-		if ($prop instanceof Property\ICalendar\DateTime) {
1666
-			$value = $prop->getDateTime();
1667
-		} else {
1668
-			$value = $prop->getValue();
1669
-		}
1670
-
1671
-		return [
1672
-			$value,
1673
-			$prop->parameters()
1674
-		];
1675
-	}
1676
-
1677
-	/**
1678
-	 * Searches through all of a users calendars and calendar objects to find
1679
-	 * an object with a specific UID.
1680
-	 *
1681
-	 * This method should return the path to this object, relative to the
1682
-	 * calendar home, so this path usually only contains two parts:
1683
-	 *
1684
-	 * calendarpath/objectpath.ics
1685
-	 *
1686
-	 * If the uid is not found, return null.
1687
-	 *
1688
-	 * This method should only consider * objects that the principal owns, so
1689
-	 * any calendars owned by other principals that also appear in this
1690
-	 * collection should be ignored.
1691
-	 *
1692
-	 * @param string $principalUri
1693
-	 * @param string $uid
1694
-	 * @return string|null
1695
-	 */
1696
-	function getCalendarObjectByUID($principalUri, $uid) {
1697
-
1698
-		$query = $this->db->getQueryBuilder();
1699
-		$query->selectAlias('c.uri', 'calendaruri')->selectAlias('co.uri', 'objecturi')
1700
-			->from('calendarobjects', 'co')
1701
-			->leftJoin('co', 'calendars', 'c', $query->expr()->eq('co.calendarid', 'c.id'))
1702
-			->where($query->expr()->eq('c.principaluri', $query->createNamedParameter($principalUri)))
1703
-			->andWhere($query->expr()->eq('co.uid', $query->createNamedParameter($uid)))
1704
-			->andWhere($query->expr()->eq('co.uid', $query->createNamedParameter($uid)));
1705
-
1706
-		$stmt = $query->execute();
1707
-
1708
-		if ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
1709
-			return $row['calendaruri'] . '/' . $row['objecturi'];
1710
-		}
1711
-
1712
-		return null;
1713
-	}
1714
-
1715
-	/**
1716
-	 * The getChanges method returns all the changes that have happened, since
1717
-	 * the specified syncToken in the specified calendar.
1718
-	 *
1719
-	 * This function should return an array, such as the following:
1720
-	 *
1721
-	 * [
1722
-	 *   'syncToken' => 'The current synctoken',
1723
-	 *   'added'   => [
1724
-	 *      'new.txt',
1725
-	 *   ],
1726
-	 *   'modified'   => [
1727
-	 *      'modified.txt',
1728
-	 *   ],
1729
-	 *   'deleted' => [
1730
-	 *      'foo.php.bak',
1731
-	 *      'old.txt'
1732
-	 *   ]
1733
-	 * );
1734
-	 *
1735
-	 * The returned syncToken property should reflect the *current* syncToken
1736
-	 * of the calendar, as reported in the {http://sabredav.org/ns}sync-token
1737
-	 * property This is * needed here too, to ensure the operation is atomic.
1738
-	 *
1739
-	 * If the $syncToken argument is specified as null, this is an initial
1740
-	 * sync, and all members should be reported.
1741
-	 *
1742
-	 * The modified property is an array of nodenames that have changed since
1743
-	 * the last token.
1744
-	 *
1745
-	 * The deleted property is an array with nodenames, that have been deleted
1746
-	 * from collection.
1747
-	 *
1748
-	 * The $syncLevel argument is basically the 'depth' of the report. If it's
1749
-	 * 1, you only have to report changes that happened only directly in
1750
-	 * immediate descendants. If it's 2, it should also include changes from
1751
-	 * the nodes below the child collections. (grandchildren)
1752
-	 *
1753
-	 * The $limit argument allows a client to specify how many results should
1754
-	 * be returned at most. If the limit is not specified, it should be treated
1755
-	 * as infinite.
1756
-	 *
1757
-	 * If the limit (infinite or not) is higher than you're willing to return,
1758
-	 * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception.
1759
-	 *
1760
-	 * If the syncToken is expired (due to data cleanup) or unknown, you must
1761
-	 * return null.
1762
-	 *
1763
-	 * The limit is 'suggestive'. You are free to ignore it.
1764
-	 *
1765
-	 * @param string $calendarId
1766
-	 * @param string $syncToken
1767
-	 * @param int $syncLevel
1768
-	 * @param int $limit
1769
-	 * @param int $calendarType
1770
-	 * @return array
1771
-	 */
1772
-	function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null, $calendarType=self::CALENDAR_TYPE_CALENDAR) {
1773
-		// Current synctoken
1774
-		$stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*calendars` WHERE `id` = ?');
1775
-		$stmt->execute([ $calendarId ]);
1776
-		$currentToken = $stmt->fetchColumn(0);
1777
-
1778
-		if (is_null($currentToken)) {
1779
-			return null;
1780
-		}
1781
-
1782
-		$result = [
1783
-			'syncToken' => $currentToken,
1784
-			'added'     => [],
1785
-			'modified'  => [],
1786
-			'deleted'   => [],
1787
-		];
1788
-
1789
-		if ($syncToken) {
1790
-
1791
-			$query = "SELECT `uri`, `operation` FROM `*PREFIX*calendarchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `calendarid` = ? AND `calendartype` = ? ORDER BY `synctoken`";
1792
-			if ($limit>0) {
1793
-				$query.= " LIMIT " . (int)$limit;
1794
-			}
1795
-
1796
-			// Fetching all changes
1797
-			$stmt = $this->db->prepare($query);
1798
-			$stmt->execute([$syncToken, $currentToken, $calendarId, $calendarType]);
1799
-
1800
-			$changes = [];
1801
-
1802
-			// This loop ensures that any duplicates are overwritten, only the
1803
-			// last change on a node is relevant.
1804
-			while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
1805
-
1806
-				$changes[$row['uri']] = $row['operation'];
1807
-
1808
-			}
1809
-
1810
-			foreach($changes as $uri => $operation) {
1811
-
1812
-				switch($operation) {
1813
-					case 1 :
1814
-						$result['added'][] = $uri;
1815
-						break;
1816
-					case 2 :
1817
-						$result['modified'][] = $uri;
1818
-						break;
1819
-					case 3 :
1820
-						$result['deleted'][] = $uri;
1821
-						break;
1822
-				}
1823
-
1824
-			}
1825
-		} else {
1826
-			// No synctoken supplied, this is the initial sync.
1827
-			$query = "SELECT `uri` FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ? AND `calendartype` = ?";
1828
-			$stmt = $this->db->prepare($query);
1829
-			$stmt->execute([$calendarId, $calendarType]);
1830
-
1831
-			$result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN);
1832
-		}
1833
-		return $result;
1834
-
1835
-	}
1836
-
1837
-	/**
1838
-	 * Returns a list of subscriptions for a principal.
1839
-	 *
1840
-	 * Every subscription is an array with the following keys:
1841
-	 *  * id, a unique id that will be used by other functions to modify the
1842
-	 *    subscription. This can be the same as the uri or a database key.
1843
-	 *  * uri. This is just the 'base uri' or 'filename' of the subscription.
1844
-	 *  * principaluri. The owner of the subscription. Almost always the same as
1845
-	 *    principalUri passed to this method.
1846
-	 *
1847
-	 * Furthermore, all the subscription info must be returned too:
1848
-	 *
1849
-	 * 1. {DAV:}displayname
1850
-	 * 2. {http://apple.com/ns/ical/}refreshrate
1851
-	 * 3. {http://calendarserver.org/ns/}subscribed-strip-todos (omit if todos
1852
-	 *    should not be stripped).
1853
-	 * 4. {http://calendarserver.org/ns/}subscribed-strip-alarms (omit if alarms
1854
-	 *    should not be stripped).
1855
-	 * 5. {http://calendarserver.org/ns/}subscribed-strip-attachments (omit if
1856
-	 *    attachments should not be stripped).
1857
-	 * 6. {http://calendarserver.org/ns/}source (Must be a
1858
-	 *     Sabre\DAV\Property\Href).
1859
-	 * 7. {http://apple.com/ns/ical/}calendar-color
1860
-	 * 8. {http://apple.com/ns/ical/}calendar-order
1861
-	 * 9. {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set
1862
-	 *    (should just be an instance of
1863
-	 *    Sabre\CalDAV\Property\SupportedCalendarComponentSet, with a bunch of
1864
-	 *    default components).
1865
-	 *
1866
-	 * @param string $principalUri
1867
-	 * @return array
1868
-	 */
1869
-	function getSubscriptionsForUser($principalUri) {
1870
-		$fields = array_values($this->subscriptionPropertyMap);
1871
-		$fields[] = 'id';
1872
-		$fields[] = 'uri';
1873
-		$fields[] = 'source';
1874
-		$fields[] = 'principaluri';
1875
-		$fields[] = 'lastmodified';
1876
-		$fields[] = 'synctoken';
1877
-
1878
-		$query = $this->db->getQueryBuilder();
1879
-		$query->select($fields)
1880
-			->from('calendarsubscriptions')
1881
-			->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri)))
1882
-			->orderBy('calendarorder', 'asc');
1883
-		$stmt =$query->execute();
1884
-
1885
-		$subscriptions = [];
1886
-		while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
1887
-
1888
-			$subscription = [
1889
-				'id'           => $row['id'],
1890
-				'uri'          => $row['uri'],
1891
-				'principaluri' => $row['principaluri'],
1892
-				'source'       => $row['source'],
1893
-				'lastmodified' => $row['lastmodified'],
1894
-
1895
-				'{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']),
1896
-				'{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
1897
-			];
1898
-
1899
-			foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) {
1900
-				if (!is_null($row[$dbName])) {
1901
-					$subscription[$xmlName] = $row[$dbName];
1902
-				}
1903
-			}
1904
-
1905
-			$subscriptions[] = $subscription;
1906
-
1907
-		}
1908
-
1909
-		return $subscriptions;
1910
-	}
1911
-
1912
-	/**
1913
-	 * Creates a new subscription for a principal.
1914
-	 *
1915
-	 * If the creation was a success, an id must be returned that can be used to reference
1916
-	 * this subscription in other methods, such as updateSubscription.
1917
-	 *
1918
-	 * @param string $principalUri
1919
-	 * @param string $uri
1920
-	 * @param array $properties
1921
-	 * @return mixed
1922
-	 */
1923
-	function createSubscription($principalUri, $uri, array $properties) {
1924
-
1925
-		if (!isset($properties['{http://calendarserver.org/ns/}source'])) {
1926
-			throw new Forbidden('The {http://calendarserver.org/ns/}source property is required when creating subscriptions');
1927
-		}
1928
-
1929
-		$values = [
1930
-			'principaluri' => $principalUri,
1931
-			'uri'          => $uri,
1932
-			'source'       => $properties['{http://calendarserver.org/ns/}source']->getHref(),
1933
-			'lastmodified' => time(),
1934
-		];
1935
-
1936
-		$propertiesBoolean = ['striptodos', 'stripalarms', 'stripattachments'];
1937
-
1938
-		foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) {
1939
-			if (array_key_exists($xmlName, $properties)) {
1940
-					$values[$dbName] = $properties[$xmlName];
1941
-					if (in_array($dbName, $propertiesBoolean)) {
1942
-						$values[$dbName] = true;
1943
-				}
1944
-			}
1945
-		}
1946
-
1947
-		$valuesToInsert = [];
1948
-
1949
-		$query = $this->db->getQueryBuilder();
1950
-
1951
-		foreach (array_keys($values) as $name) {
1952
-			$valuesToInsert[$name] = $query->createNamedParameter($values[$name]);
1953
-		}
1954
-
1955
-		$query->insert('calendarsubscriptions')
1956
-			->values($valuesToInsert)
1957
-			->execute();
1958
-
1959
-		$subscriptionId = $this->db->lastInsertId('*PREFIX*calendarsubscriptions');
1960
-
1961
-		$this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::createSubscription', new GenericEvent(
1962
-			'\OCA\DAV\CalDAV\CalDavBackend::createSubscription',
1963
-			[
1964
-				'subscriptionId' => $subscriptionId,
1965
-				'subscriptionData' => $this->getSubscriptionById($subscriptionId),
1966
-			]));
1967
-
1968
-		return $subscriptionId;
1969
-	}
1970
-
1971
-	/**
1972
-	 * Updates a subscription
1973
-	 *
1974
-	 * The list of mutations is stored in a Sabre\DAV\PropPatch object.
1975
-	 * To do the actual updates, you must tell this object which properties
1976
-	 * you're going to process with the handle() method.
1977
-	 *
1978
-	 * Calling the handle method is like telling the PropPatch object "I
1979
-	 * promise I can handle updating this property".
1980
-	 *
1981
-	 * Read the PropPatch documentation for more info and examples.
1982
-	 *
1983
-	 * @param mixed $subscriptionId
1984
-	 * @param PropPatch $propPatch
1985
-	 * @return void
1986
-	 */
1987
-	function updateSubscription($subscriptionId, PropPatch $propPatch) {
1988
-		$supportedProperties = array_keys($this->subscriptionPropertyMap);
1989
-		$supportedProperties[] = '{http://calendarserver.org/ns/}source';
1990
-
1991
-		/**
1992
-		 * @suppress SqlInjectionChecker
1993
-		 */
1994
-		$propPatch->handle($supportedProperties, function($mutations) use ($subscriptionId) {
1995
-
1996
-			$newValues = [];
1997
-
1998
-			foreach($mutations as $propertyName=>$propertyValue) {
1999
-				if ($propertyName === '{http://calendarserver.org/ns/}source') {
2000
-					$newValues['source'] = $propertyValue->getHref();
2001
-				} else {
2002
-					$fieldName = $this->subscriptionPropertyMap[$propertyName];
2003
-					$newValues[$fieldName] = $propertyValue;
2004
-				}
2005
-			}
2006
-
2007
-			$query = $this->db->getQueryBuilder();
2008
-			$query->update('calendarsubscriptions')
2009
-				->set('lastmodified', $query->createNamedParameter(time()));
2010
-			foreach($newValues as $fieldName=>$value) {
2011
-				$query->set($fieldName, $query->createNamedParameter($value));
2012
-			}
2013
-			$query->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId)))
2014
-				->execute();
2015
-
2016
-			$this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateSubscription', new GenericEvent(
2017
-				'\OCA\DAV\CalDAV\CalDavBackend::updateSubscription',
2018
-				[
2019
-					'subscriptionId' => $subscriptionId,
2020
-					'subscriptionData' => $this->getSubscriptionById($subscriptionId),
2021
-					'propertyMutations' => $mutations,
2022
-				]));
2023
-
2024
-			return true;
2025
-
2026
-		});
2027
-	}
2028
-
2029
-	/**
2030
-	 * Deletes a subscription.
2031
-	 *
2032
-	 * @param mixed $subscriptionId
2033
-	 * @return void
2034
-	 */
2035
-	function deleteSubscription($subscriptionId) {
2036
-		$this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::deleteSubscription', new GenericEvent(
2037
-			'\OCA\DAV\CalDAV\CalDavBackend::deleteSubscription',
2038
-			[
2039
-				'subscriptionId' => $subscriptionId,
2040
-				'subscriptionData' => $this->getSubscriptionById($subscriptionId),
2041
-			]));
2042
-
2043
-		$query = $this->db->getQueryBuilder();
2044
-		$query->delete('calendarsubscriptions')
2045
-			->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId)))
2046
-			->execute();
2047
-
2048
-		$query = $this->db->getQueryBuilder();
2049
-		$query->delete('calendarobjects')
2050
-			->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId)))
2051
-			->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION)))
2052
-			->execute();
2053
-
2054
-		$query->delete('calendarchanges')
2055
-			->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId)))
2056
-			->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION)))
2057
-			->execute();
2058
-
2059
-		$query->delete($this->dbObjectPropertiesTable)
2060
-			->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId)))
2061
-			->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION)))
2062
-			->execute();
2063
-	}
2064
-
2065
-	/**
2066
-	 * Returns a single scheduling object for the inbox collection.
2067
-	 *
2068
-	 * The returned array should contain the following elements:
2069
-	 *   * uri - A unique basename for the object. This will be used to
2070
-	 *           construct a full uri.
2071
-	 *   * calendardata - The iCalendar object
2072
-	 *   * lastmodified - The last modification date. Can be an int for a unix
2073
-	 *                    timestamp, or a PHP DateTime object.
2074
-	 *   * etag - A unique token that must change if the object changed.
2075
-	 *   * size - The size of the object, in bytes.
2076
-	 *
2077
-	 * @param string $principalUri
2078
-	 * @param string $objectUri
2079
-	 * @return array
2080
-	 */
2081
-	function getSchedulingObject($principalUri, $objectUri) {
2082
-		$query = $this->db->getQueryBuilder();
2083
-		$stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size'])
2084
-			->from('schedulingobjects')
2085
-			->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri)))
2086
-			->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri)))
2087
-			->execute();
2088
-
2089
-		$row = $stmt->fetch(\PDO::FETCH_ASSOC);
2090
-
2091
-		if(!$row) {
2092
-			return null;
2093
-		}
2094
-
2095
-		return [
2096
-				'uri'          => $row['uri'],
2097
-				'calendardata' => $row['calendardata'],
2098
-				'lastmodified' => $row['lastmodified'],
2099
-				'etag'         => '"' . $row['etag'] . '"',
2100
-				'size'         => (int)$row['size'],
2101
-		];
2102
-	}
2103
-
2104
-	/**
2105
-	 * Returns all scheduling objects for the inbox collection.
2106
-	 *
2107
-	 * These objects should be returned as an array. Every item in the array
2108
-	 * should follow the same structure as returned from getSchedulingObject.
2109
-	 *
2110
-	 * The main difference is that 'calendardata' is optional.
2111
-	 *
2112
-	 * @param string $principalUri
2113
-	 * @return array
2114
-	 */
2115
-	function getSchedulingObjects($principalUri) {
2116
-		$query = $this->db->getQueryBuilder();
2117
-		$stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size'])
2118
-				->from('schedulingobjects')
2119
-				->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri)))
2120
-				->execute();
2121
-
2122
-		$result = [];
2123
-		foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) {
2124
-			$result[] = [
2125
-					'calendardata' => $row['calendardata'],
2126
-					'uri'          => $row['uri'],
2127
-					'lastmodified' => $row['lastmodified'],
2128
-					'etag'         => '"' . $row['etag'] . '"',
2129
-					'size'         => (int)$row['size'],
2130
-			];
2131
-		}
2132
-
2133
-		return $result;
2134
-	}
2135
-
2136
-	/**
2137
-	 * Deletes a scheduling object from the inbox collection.
2138
-	 *
2139
-	 * @param string $principalUri
2140
-	 * @param string $objectUri
2141
-	 * @return void
2142
-	 */
2143
-	function deleteSchedulingObject($principalUri, $objectUri) {
2144
-		$query = $this->db->getQueryBuilder();
2145
-		$query->delete('schedulingobjects')
2146
-				->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri)))
2147
-				->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri)))
2148
-				->execute();
2149
-	}
2150
-
2151
-	/**
2152
-	 * Creates a new scheduling object. This should land in a users' inbox.
2153
-	 *
2154
-	 * @param string $principalUri
2155
-	 * @param string $objectUri
2156
-	 * @param string $objectData
2157
-	 * @return void
2158
-	 */
2159
-	function createSchedulingObject($principalUri, $objectUri, $objectData) {
2160
-		$query = $this->db->getQueryBuilder();
2161
-		$query->insert('schedulingobjects')
2162
-			->values([
2163
-				'principaluri' => $query->createNamedParameter($principalUri),
2164
-				'calendardata' => $query->createNamedParameter($objectData, IQueryBuilder::PARAM_LOB),
2165
-				'uri' => $query->createNamedParameter($objectUri),
2166
-				'lastmodified' => $query->createNamedParameter(time()),
2167
-				'etag' => $query->createNamedParameter(md5($objectData)),
2168
-				'size' => $query->createNamedParameter(strlen($objectData))
2169
-			])
2170
-			->execute();
2171
-	}
2172
-
2173
-	/**
2174
-	 * Adds a change record to the calendarchanges table.
2175
-	 *
2176
-	 * @param mixed $calendarId
2177
-	 * @param string $objectUri
2178
-	 * @param int $operation 1 = add, 2 = modify, 3 = delete.
2179
-	 * @param int $calendarType
2180
-	 * @return void
2181
-	 */
2182
-	protected function addChange($calendarId, $objectUri, $operation, $calendarType=self::CALENDAR_TYPE_CALENDAR) {
2183
-		$table = $calendarType === self::CALENDAR_TYPE_CALENDAR ? 'calendars': 'calendarsubscriptions';
2184
-
2185
-		$query = $this->db->getQueryBuilder();
2186
-		$query->select('synctoken')
2187
-			->from($table)
2188
-			->where($query->expr()->eq('id', $query->createNamedParameter($calendarId)));
2189
-		$syncToken = (int)$query->execute()->fetchColumn();
2190
-
2191
-		$query = $this->db->getQueryBuilder();
2192
-		$query->insert('calendarchanges')
2193
-			->values([
2194
-				'uri' => $query->createNamedParameter($objectUri),
2195
-				'synctoken' => $query->createNamedParameter($syncToken),
2196
-				'calendarid' => $query->createNamedParameter($calendarId),
2197
-				'operation' => $query->createNamedParameter($operation),
2198
-				'calendartype' => $query->createNamedParameter($calendarType),
2199
-			])
2200
-			->execute();
2201
-
2202
-		$stmt = $this->db->prepare("UPDATE `*PREFIX*$table` SET `synctoken` = `synctoken` + 1 WHERE `id` = ?");
2203
-		$stmt->execute([
2204
-			$calendarId
2205
-		]);
2206
-
2207
-	}
2208
-
2209
-	/**
2210
-	 * Parses some information from calendar objects, used for optimized
2211
-	 * calendar-queries.
2212
-	 *
2213
-	 * Returns an array with the following keys:
2214
-	 *   * etag - An md5 checksum of the object without the quotes.
2215
-	 *   * size - Size of the object in bytes
2216
-	 *   * componentType - VEVENT, VTODO or VJOURNAL
2217
-	 *   * firstOccurence
2218
-	 *   * lastOccurence
2219
-	 *   * uid - value of the UID property
2220
-	 *
2221
-	 * @param string $calendarData
2222
-	 * @return array
2223
-	 */
2224
-	public function getDenormalizedData($calendarData) {
2225
-
2226
-		$vObject = Reader::read($calendarData);
2227
-		$componentType = null;
2228
-		$component = null;
2229
-		$firstOccurrence = null;
2230
-		$lastOccurrence = null;
2231
-		$uid = null;
2232
-		$classification = self::CLASSIFICATION_PUBLIC;
2233
-		foreach($vObject->getComponents() as $component) {
2234
-			if ($component->name!=='VTIMEZONE') {
2235
-				$componentType = $component->name;
2236
-				$uid = (string)$component->UID;
2237
-				break;
2238
-			}
2239
-		}
2240
-		if (!$componentType) {
2241
-			throw new \Sabre\DAV\Exception\BadRequest('Calendar objects must have a VJOURNAL, VEVENT or VTODO component');
2242
-		}
2243
-		if ($componentType === 'VEVENT' && $component->DTSTART) {
2244
-			$firstOccurrence = $component->DTSTART->getDateTime()->getTimeStamp();
2245
-			// Finding the last occurrence is a bit harder
2246
-			if (!isset($component->RRULE)) {
2247
-				if (isset($component->DTEND)) {
2248
-					$lastOccurrence = $component->DTEND->getDateTime()->getTimeStamp();
2249
-				} elseif (isset($component->DURATION)) {
2250
-					$endDate = clone $component->DTSTART->getDateTime();
2251
-					$endDate->add(DateTimeParser::parse($component->DURATION->getValue()));
2252
-					$lastOccurrence = $endDate->getTimeStamp();
2253
-				} elseif (!$component->DTSTART->hasTime()) {
2254
-					$endDate = clone $component->DTSTART->getDateTime();
2255
-					$endDate->modify('+1 day');
2256
-					$lastOccurrence = $endDate->getTimeStamp();
2257
-				} else {
2258
-					$lastOccurrence = $firstOccurrence;
2259
-				}
2260
-			} else {
2261
-				$it = new EventIterator($vObject, (string)$component->UID);
2262
-				$maxDate = new DateTime(self::MAX_DATE);
2263
-				if ($it->isInfinite()) {
2264
-					$lastOccurrence = $maxDate->getTimestamp();
2265
-				} else {
2266
-					$end = $it->getDtEnd();
2267
-					while($it->valid() && $end < $maxDate) {
2268
-						$end = $it->getDtEnd();
2269
-						$it->next();
2270
-
2271
-					}
2272
-					$lastOccurrence = $end->getTimestamp();
2273
-				}
2274
-
2275
-			}
2276
-		}
2277
-
2278
-		if ($component->CLASS) {
2279
-			$classification = CalDavBackend::CLASSIFICATION_PRIVATE;
2280
-			switch ($component->CLASS->getValue()) {
2281
-				case 'PUBLIC':
2282
-					$classification = CalDavBackend::CLASSIFICATION_PUBLIC;
2283
-					break;
2284
-				case 'CONFIDENTIAL':
2285
-					$classification = CalDavBackend::CLASSIFICATION_CONFIDENTIAL;
2286
-					break;
2287
-			}
2288
-		}
2289
-		return [
2290
-			'etag' => md5($calendarData),
2291
-			'size' => strlen($calendarData),
2292
-			'componentType' => $componentType,
2293
-			'firstOccurence' => is_null($firstOccurrence) ? null : max(0, $firstOccurrence),
2294
-			'lastOccurence'  => $lastOccurrence,
2295
-			'uid' => $uid,
2296
-			'classification' => $classification
2297
-		];
2298
-
2299
-	}
2300
-
2301
-	/**
2302
-	 * @param $cardData
2303
-	 * @return bool|string
2304
-	 */
2305
-	private function readBlob($cardData) {
2306
-		if (is_resource($cardData)) {
2307
-			return stream_get_contents($cardData);
2308
-		}
2309
-
2310
-		return $cardData;
2311
-	}
2312
-
2313
-	/**
2314
-	 * @param IShareable $shareable
2315
-	 * @param array $add
2316
-	 * @param array $remove
2317
-	 */
2318
-	public function updateShares($shareable, $add, $remove) {
2319
-		$calendarId = $shareable->getResourceId();
2320
-		$this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateShares', new GenericEvent(
2321
-			'\OCA\DAV\CalDAV\CalDavBackend::updateShares',
2322
-			[
2323
-				'calendarId' => $calendarId,
2324
-				'calendarData' => $this->getCalendarById($calendarId),
2325
-				'shares' => $this->getShares($calendarId),
2326
-				'add' => $add,
2327
-				'remove' => $remove,
2328
-			]));
2329
-		$this->calendarSharingBackend->updateShares($shareable, $add, $remove);
2330
-	}
2331
-
2332
-	/**
2333
-	 * @param int $resourceId
2334
-	 * @param int $calendarType
2335
-	 * @return array
2336
-	 */
2337
-	public function getShares($resourceId, $calendarType=self::CALENDAR_TYPE_CALENDAR) {
2338
-		return $this->calendarSharingBackend->getShares($resourceId);
2339
-	}
2340
-
2341
-	/**
2342
-	 * @param boolean $value
2343
-	 * @param \OCA\DAV\CalDAV\Calendar $calendar
2344
-	 * @return string|null
2345
-	 */
2346
-	public function setPublishStatus($value, $calendar) {
2347
-
2348
-		$calendarId = $calendar->getResourceId();
2349
-		$this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::publishCalendar', new GenericEvent(
2350
-			'\OCA\DAV\CalDAV\CalDavBackend::updateShares',
2351
-			[
2352
-				'calendarId' => $calendarId,
2353
-				'calendarData' => $this->getCalendarById($calendarId),
2354
-				'public' => $value,
2355
-			]));
2356
-
2357
-		$query = $this->db->getQueryBuilder();
2358
-		if ($value) {
2359
-			$publicUri = $this->random->generate(16, ISecureRandom::CHAR_HUMAN_READABLE);
2360
-			$query->insert('dav_shares')
2361
-				->values([
2362
-					'principaluri' => $query->createNamedParameter($calendar->getPrincipalURI()),
2363
-					'type' => $query->createNamedParameter('calendar'),
2364
-					'access' => $query->createNamedParameter(self::ACCESS_PUBLIC),
2365
-					'resourceid' => $query->createNamedParameter($calendar->getResourceId()),
2366
-					'publicuri' => $query->createNamedParameter($publicUri)
2367
-				]);
2368
-			$query->execute();
2369
-			return $publicUri;
2370
-		}
2371
-		$query->delete('dav_shares')
2372
-			->where($query->expr()->eq('resourceid', $query->createNamedParameter($calendar->getResourceId())))
2373
-			->andWhere($query->expr()->eq('access', $query->createNamedParameter(self::ACCESS_PUBLIC)));
2374
-		$query->execute();
2375
-		return null;
2376
-	}
2377
-
2378
-	/**
2379
-	 * @param \OCA\DAV\CalDAV\Calendar $calendar
2380
-	 * @return mixed
2381
-	 */
2382
-	public function getPublishStatus($calendar) {
2383
-		$query = $this->db->getQueryBuilder();
2384
-		$result = $query->select('publicuri')
2385
-			->from('dav_shares')
2386
-			->where($query->expr()->eq('resourceid', $query->createNamedParameter($calendar->getResourceId())))
2387
-			->andWhere($query->expr()->eq('access', $query->createNamedParameter(self::ACCESS_PUBLIC)))
2388
-			->execute();
2389
-
2390
-		$row = $result->fetch();
2391
-		$result->closeCursor();
2392
-		return $row ? reset($row) : false;
2393
-	}
2394
-
2395
-	/**
2396
-	 * @param int $resourceId
2397
-	 * @param array $acl
2398
-	 * @return array
2399
-	 */
2400
-	public function applyShareAcl($resourceId, $acl) {
2401
-		return $this->calendarSharingBackend->applyShareAcl($resourceId, $acl);
2402
-	}
2403
-
2404
-
2405
-
2406
-	/**
2407
-	 * update properties table
2408
-	 *
2409
-	 * @param int $calendarId
2410
-	 * @param string $objectUri
2411
-	 * @param string $calendarData
2412
-	 * @param int $calendarType
2413
-	 */
2414
-	public function updateProperties($calendarId, $objectUri, $calendarData, $calendarType=self::CALENDAR_TYPE_CALENDAR) {
2415
-		$objectId = $this->getCalendarObjectId($calendarId, $objectUri, $calendarType);
2416
-
2417
-		try {
2418
-			$vCalendar = $this->readCalendarData($calendarData);
2419
-		} catch (\Exception $ex) {
2420
-			return;
2421
-		}
2422
-
2423
-		$this->purgeProperties($calendarId, $objectId);
2424
-
2425
-		$query = $this->db->getQueryBuilder();
2426
-		$query->insert($this->dbObjectPropertiesTable)
2427
-			->values(
2428
-				[
2429
-					'calendarid' => $query->createNamedParameter($calendarId),
2430
-					'calendartype' => $query->createNamedParameter($calendarType),
2431
-					'objectid' => $query->createNamedParameter($objectId),
2432
-					'name' => $query->createParameter('name'),
2433
-					'parameter' => $query->createParameter('parameter'),
2434
-					'value' => $query->createParameter('value'),
2435
-				]
2436
-			);
2437
-
2438
-		$indexComponents = ['VEVENT', 'VJOURNAL', 'VTODO'];
2439
-		foreach ($vCalendar->getComponents() as $component) {
2440
-			if (!in_array($component->name, $indexComponents)) {
2441
-				continue;
2442
-			}
2443
-
2444
-			foreach ($component->children() as $property) {
2445
-				if (in_array($property->name, self::$indexProperties)) {
2446
-					$value = $property->getValue();
2447
-					// is this a shitty db?
2448
-					if (!$this->db->supports4ByteText()) {
2449
-						$value = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $value);
2450
-					}
2451
-					$value = mb_substr($value, 0, 254);
2452
-
2453
-					$query->setParameter('name', $property->name);
2454
-					$query->setParameter('parameter', null);
2455
-					$query->setParameter('value', $value);
2456
-					$query->execute();
2457
-				}
2458
-
2459
-				if (array_key_exists($property->name, self::$indexParameters)) {
2460
-					$parameters = $property->parameters();
2461
-					$indexedParametersForProperty = self::$indexParameters[$property->name];
2462
-
2463
-					foreach ($parameters as $key => $value) {
2464
-						if (in_array($key, $indexedParametersForProperty)) {
2465
-							// is this a shitty db?
2466
-							if ($this->db->supports4ByteText()) {
2467
-								$value = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $value);
2468
-							}
2469
-
2470
-							$query->setParameter('name', $property->name);
2471
-							$query->setParameter('parameter', mb_substr($key, 0, 254));
2472
-							$query->setParameter('value', mb_substr($value, 0, 254));
2473
-							$query->execute();
2474
-						}
2475
-					}
2476
-				}
2477
-			}
2478
-		}
2479
-	}
2480
-
2481
-	/**
2482
-	 * deletes all birthday calendars
2483
-	 */
2484
-	public function deleteAllBirthdayCalendars() {
2485
-		$query = $this->db->getQueryBuilder();
2486
-		$result = $query->select(['id'])->from('calendars')
2487
-			->where($query->expr()->eq('uri', $query->createNamedParameter(BirthdayService::BIRTHDAY_CALENDAR_URI)))
2488
-			->execute();
2489
-
2490
-		$ids = $result->fetchAll();
2491
-		foreach($ids as $id) {
2492
-			$this->deleteCalendar($id['id']);
2493
-		}
2494
-	}
2495
-
2496
-	/**
2497
-	 * @param $subscriptionId
2498
-	 */
2499
-	public function purgeAllCachedEventsForSubscription($subscriptionId) {
2500
-		$query = $this->db->getQueryBuilder();
2501
-		$query->select('uri')
2502
-			->from('calendarobjects')
2503
-			->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId)))
2504
-			->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION)));
2505
-		$stmt = $query->execute();
2506
-
2507
-		$uris = [];
2508
-		foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) {
2509
-			$uris[] = $row['uri'];
2510
-		}
2511
-		$stmt->closeCursor();
2512
-
2513
-		$query = $this->db->getQueryBuilder();
2514
-		$query->delete('calendarobjects')
2515
-			->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId)))
2516
-			->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION)))
2517
-			->execute();
2518
-
2519
-		$query->delete('calendarchanges')
2520
-			->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId)))
2521
-			->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION)))
2522
-			->execute();
2523
-
2524
-		$query->delete($this->dbObjectPropertiesTable)
2525
-			->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId)))
2526
-			->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION)))
2527
-			->execute();
2528
-
2529
-		foreach($uris as $uri) {
2530
-			$this->addChange($subscriptionId, $uri, 3, self::CALENDAR_TYPE_SUBSCRIPTION);
2531
-		}
2532
-	}
2533
-
2534
-	/**
2535
-	 * Move a calendar from one user to another
2536
-	 *
2537
-	 * @param string $uriName
2538
-	 * @param string $uriOrigin
2539
-	 * @param string $uriDestination
2540
-	 */
2541
-	public function moveCalendar($uriName, $uriOrigin, $uriDestination)
2542
-	{
2543
-		$query = $this->db->getQueryBuilder();
2544
-		$query->update('calendars')
2545
-			->set('principaluri', $query->createNamedParameter($uriDestination))
2546
-			->where($query->expr()->eq('principaluri', $query->createNamedParameter($uriOrigin)))
2547
-			->andWhere($query->expr()->eq('uri', $query->createNamedParameter($uriName)))
2548
-			->execute();
2549
-	}
2550
-
2551
-	/**
2552
-	 * read VCalendar data into a VCalendar object
2553
-	 *
2554
-	 * @param string $objectData
2555
-	 * @return VCalendar
2556
-	 */
2557
-	protected function readCalendarData($objectData) {
2558
-		return Reader::read($objectData);
2559
-	}
2560
-
2561
-	/**
2562
-	 * delete all properties from a given calendar object
2563
-	 *
2564
-	 * @param int $calendarId
2565
-	 * @param int $objectId
2566
-	 */
2567
-	protected function purgeProperties($calendarId, $objectId) {
2568
-		$query = $this->db->getQueryBuilder();
2569
-		$query->delete($this->dbObjectPropertiesTable)
2570
-			->where($query->expr()->eq('objectid', $query->createNamedParameter($objectId)))
2571
-			->andWhere($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId)));
2572
-		$query->execute();
2573
-	}
2574
-
2575
-	/**
2576
-	 * get ID from a given calendar object
2577
-	 *
2578
-	 * @param int $calendarId
2579
-	 * @param string $uri
2580
-	 * @param int $calendarType
2581
-	 * @return int
2582
-	 */
2583
-	protected function getCalendarObjectId($calendarId, $uri, $calendarType):int {
2584
-		$query = $this->db->getQueryBuilder();
2585
-		$query->select('id')
2586
-			->from('calendarobjects')
2587
-			->where($query->expr()->eq('uri', $query->createNamedParameter($uri)))
2588
-			->andWhere($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId)))
2589
-			->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType)));
2590
-
2591
-		$result = $query->execute();
2592
-		$objectIds = $result->fetch();
2593
-		$result->closeCursor();
2594
-
2595
-		if (!isset($objectIds['id'])) {
2596
-			throw new \InvalidArgumentException('Calendarobject does not exists: ' . $uri);
2597
-		}
2598
-
2599
-		return (int)$objectIds['id'];
2600
-	}
2601
-
2602
-	/**
2603
-	 * return legacy endpoint principal name to new principal name
2604
-	 *
2605
-	 * @param $principalUri
2606
-	 * @param $toV2
2607
-	 * @return string
2608
-	 */
2609
-	private function convertPrincipal($principalUri, $toV2) {
2610
-		if ($this->principalBackend->getPrincipalPrefix() === 'principals') {
2611
-			list(, $name) = Uri\split($principalUri);
2612
-			if ($toV2 === true) {
2613
-				return "principals/users/$name";
2614
-			}
2615
-			return "principals/$name";
2616
-		}
2617
-		return $principalUri;
2618
-	}
2619
-
2620
-	/**
2621
-	 * adds information about an owner to the calendar data
2622
-	 *
2623
-	 * @param $calendarInfo
2624
-	 */
2625
-	private function addOwnerPrincipal(&$calendarInfo) {
2626
-		$ownerPrincipalKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal';
2627
-		$displaynameKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname';
2628
-		if (isset($calendarInfo[$ownerPrincipalKey])) {
2629
-			$uri = $calendarInfo[$ownerPrincipalKey];
2630
-		} else {
2631
-			$uri = $calendarInfo['principaluri'];
2632
-		}
2633
-
2634
-		$principalInformation = $this->principalBackend->getPrincipalByPath($uri);
2635
-		if (isset($principalInformation['{DAV:}displayname'])) {
2636
-			$calendarInfo[$displaynameKey] = $principalInformation['{DAV:}displayname'];
2637
-		}
2638
-	}
79
+    const CALENDAR_TYPE_CALENDAR = 0;
80
+    const CALENDAR_TYPE_SUBSCRIPTION = 1;
81
+
82
+    const PERSONAL_CALENDAR_URI = 'personal';
83
+    const PERSONAL_CALENDAR_NAME = 'Personal';
84
+
85
+    const RESOURCE_BOOKING_CALENDAR_URI = 'calendar';
86
+    const RESOURCE_BOOKING_CALENDAR_NAME = 'Calendar';
87
+
88
+    /**
89
+     * We need to specify a max date, because we need to stop *somewhere*
90
+     *
91
+     * On 32 bit system the maximum for a signed integer is 2147483647, so
92
+     * MAX_DATE cannot be higher than date('Y-m-d', 2147483647) which results
93
+     * in 2038-01-19 to avoid problems when the date is converted
94
+     * to a unix timestamp.
95
+     */
96
+    const MAX_DATE = '2038-01-01';
97
+
98
+    const ACCESS_PUBLIC = 4;
99
+    const CLASSIFICATION_PUBLIC = 0;
100
+    const CLASSIFICATION_PRIVATE = 1;
101
+    const CLASSIFICATION_CONFIDENTIAL = 2;
102
+
103
+    /**
104
+     * List of CalDAV properties, and how they map to database field names
105
+     * Add your own properties by simply adding on to this array.
106
+     *
107
+     * Note that only string-based properties are supported here.
108
+     *
109
+     * @var array
110
+     */
111
+    public $propertyMap = [
112
+        '{DAV:}displayname'                          => 'displayname',
113
+        '{urn:ietf:params:xml:ns:caldav}calendar-description' => 'description',
114
+        '{urn:ietf:params:xml:ns:caldav}calendar-timezone'    => 'timezone',
115
+        '{http://apple.com/ns/ical/}calendar-order'  => 'calendarorder',
116
+        '{http://apple.com/ns/ical/}calendar-color'  => 'calendarcolor',
117
+    ];
118
+
119
+    /**
120
+     * List of subscription properties, and how they map to database field names.
121
+     *
122
+     * @var array
123
+     */
124
+    public $subscriptionPropertyMap = [
125
+        '{DAV:}displayname'                                           => 'displayname',
126
+        '{http://apple.com/ns/ical/}refreshrate'                      => 'refreshrate',
127
+        '{http://apple.com/ns/ical/}calendar-order'                   => 'calendarorder',
128
+        '{http://apple.com/ns/ical/}calendar-color'                   => 'calendarcolor',
129
+        '{http://calendarserver.org/ns/}subscribed-strip-todos'       => 'striptodos',
130
+        '{http://calendarserver.org/ns/}subscribed-strip-alarms'      => 'stripalarms',
131
+        '{http://calendarserver.org/ns/}subscribed-strip-attachments' => 'stripattachments',
132
+    ];
133
+
134
+    /** @var array properties to index */
135
+    public static $indexProperties = ['CATEGORIES', 'COMMENT', 'DESCRIPTION',
136
+        'LOCATION', 'RESOURCES', 'STATUS', 'SUMMARY', 'ATTENDEE', 'CONTACT',
137
+        'ORGANIZER'];
138
+
139
+    /** @var array parameters to index */
140
+    public static $indexParameters = [
141
+        'ATTENDEE' => ['CN'],
142
+        'ORGANIZER' => ['CN'],
143
+    ];
144
+
145
+    /**
146
+     * @var string[] Map of uid => display name
147
+     */
148
+    protected $userDisplayNames;
149
+
150
+    /** @var IDBConnection */
151
+    private $db;
152
+
153
+    /** @var Backend */
154
+    private $calendarSharingBackend;
155
+
156
+    /** @var Principal */
157
+    private $principalBackend;
158
+
159
+    /** @var IUserManager */
160
+    private $userManager;
161
+
162
+    /** @var ISecureRandom */
163
+    private $random;
164
+
165
+    /** @var ILogger */
166
+    private $logger;
167
+
168
+    /** @var EventDispatcherInterface */
169
+    private $dispatcher;
170
+
171
+    /** @var bool */
172
+    private $legacyEndpoint;
173
+
174
+    /** @var string */
175
+    private $dbObjectPropertiesTable = 'calendarobjects_props';
176
+
177
+    /**
178
+     * CalDavBackend constructor.
179
+     *
180
+     * @param IDBConnection $db
181
+     * @param Principal $principalBackend
182
+     * @param IUserManager $userManager
183
+     * @param IGroupManager $groupManager
184
+     * @param ISecureRandom $random
185
+     * @param ILogger $logger
186
+     * @param EventDispatcherInterface $dispatcher
187
+     * @param bool $legacyEndpoint
188
+     */
189
+    public function __construct(IDBConnection $db,
190
+                                Principal $principalBackend,
191
+                                IUserManager $userManager,
192
+                                IGroupManager $groupManager,
193
+                                ISecureRandom $random,
194
+                                ILogger $logger,
195
+                                EventDispatcherInterface $dispatcher,
196
+                                bool $legacyEndpoint = false) {
197
+        $this->db = $db;
198
+        $this->principalBackend = $principalBackend;
199
+        $this->userManager = $userManager;
200
+        $this->calendarSharingBackend = new Backend($this->db, $this->userManager, $groupManager, $principalBackend, 'calendar');
201
+        $this->random = $random;
202
+        $this->logger = $logger;
203
+        $this->dispatcher = $dispatcher;
204
+        $this->legacyEndpoint = $legacyEndpoint;
205
+    }
206
+
207
+    /**
208
+     * Return the number of calendars for a principal
209
+     *
210
+     * By default this excludes the automatically generated birthday calendar
211
+     *
212
+     * @param $principalUri
213
+     * @param bool $excludeBirthday
214
+     * @return int
215
+     */
216
+    public function getCalendarsForUserCount($principalUri, $excludeBirthday = true) {
217
+        $principalUri = $this->convertPrincipal($principalUri, true);
218
+        $query = $this->db->getQueryBuilder();
219
+        $query->select($query->func()->count('*'))
220
+            ->from('calendars')
221
+            ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri)));
222
+
223
+        if ($excludeBirthday) {
224
+            $query->andWhere($query->expr()->neq('uri', $query->createNamedParameter(BirthdayService::BIRTHDAY_CALENDAR_URI)));
225
+        }
226
+
227
+        return (int)$query->execute()->fetchColumn();
228
+    }
229
+
230
+    /**
231
+     * Returns a list of calendars for a principal.
232
+     *
233
+     * Every project is an array with the following keys:
234
+     *  * id, a unique id that will be used by other functions to modify the
235
+     *    calendar. This can be the same as the uri or a database key.
236
+     *  * uri, which the basename of the uri with which the calendar is
237
+     *    accessed.
238
+     *  * principaluri. The owner of the calendar. Almost always the same as
239
+     *    principalUri passed to this method.
240
+     *
241
+     * Furthermore it can contain webdav properties in clark notation. A very
242
+     * common one is '{DAV:}displayname'.
243
+     *
244
+     * Many clients also require:
245
+     * {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set
246
+     * For this property, you can just return an instance of
247
+     * Sabre\CalDAV\Property\SupportedCalendarComponentSet.
248
+     *
249
+     * If you return {http://sabredav.org/ns}read-only and set the value to 1,
250
+     * ACL will automatically be put in read-only mode.
251
+     *
252
+     * @param string $principalUri
253
+     * @return array
254
+     */
255
+    function getCalendarsForUser($principalUri) {
256
+        $principalUriOriginal = $principalUri;
257
+        $principalUri = $this->convertPrincipal($principalUri, true);
258
+        $fields = array_values($this->propertyMap);
259
+        $fields[] = 'id';
260
+        $fields[] = 'uri';
261
+        $fields[] = 'synctoken';
262
+        $fields[] = 'components';
263
+        $fields[] = 'principaluri';
264
+        $fields[] = 'transparent';
265
+
266
+        // Making fields a comma-delimited list
267
+        $query = $this->db->getQueryBuilder();
268
+        $query->select($fields)->from('calendars')
269
+                ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri)))
270
+                ->orderBy('calendarorder', 'ASC');
271
+        $stmt = $query->execute();
272
+
273
+        $calendars = [];
274
+        while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
275
+
276
+            $components = [];
277
+            if ($row['components']) {
278
+                $components = explode(',',$row['components']);
279
+            }
280
+
281
+            $calendar = [
282
+                'id' => $row['id'],
283
+                'uri' => $row['uri'],
284
+                'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint),
285
+                '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'),
286
+                '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
287
+                '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components),
288
+                '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'),
289
+                '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint),
290
+            ];
291
+
292
+            foreach($this->propertyMap as $xmlName=>$dbName) {
293
+                $calendar[$xmlName] = $row[$dbName];
294
+            }
295
+
296
+            $this->addOwnerPrincipal($calendar);
297
+
298
+            if (!isset($calendars[$calendar['id']])) {
299
+                $calendars[$calendar['id']] = $calendar;
300
+            }
301
+        }
302
+
303
+        $stmt->closeCursor();
304
+
305
+        // query for shared calendars
306
+        $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true);
307
+        $principals = array_merge($principals, $this->principalBackend->getCircleMembership($principalUriOriginal));
308
+
309
+        $principals = array_map(function($principal) {
310
+            return urldecode($principal);
311
+        }, $principals);
312
+        $principals[]= $principalUri;
313
+
314
+        $fields = array_values($this->propertyMap);
315
+        $fields[] = 'a.id';
316
+        $fields[] = 'a.uri';
317
+        $fields[] = 'a.synctoken';
318
+        $fields[] = 'a.components';
319
+        $fields[] = 'a.principaluri';
320
+        $fields[] = 'a.transparent';
321
+        $fields[] = 's.access';
322
+        $query = $this->db->getQueryBuilder();
323
+        $result = $query->select($fields)
324
+            ->from('dav_shares', 's')
325
+            ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id'))
326
+            ->where($query->expr()->in('s.principaluri', $query->createParameter('principaluri')))
327
+            ->andWhere($query->expr()->eq('s.type', $query->createParameter('type')))
328
+            ->setParameter('type', 'calendar')
329
+            ->setParameter('principaluri', $principals, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY)
330
+            ->execute();
331
+
332
+        $readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only';
333
+        while($row = $result->fetch()) {
334
+            if ($row['principaluri'] === $principalUri) {
335
+                continue;
336
+            }
337
+
338
+            $readOnly = (int) $row['access'] === Backend::ACCESS_READ;
339
+            if (isset($calendars[$row['id']])) {
340
+                if ($readOnly) {
341
+                    // New share can not have more permissions then the old one.
342
+                    continue;
343
+                }
344
+                if (isset($calendars[$row['id']][$readOnlyPropertyName]) &&
345
+                    $calendars[$row['id']][$readOnlyPropertyName] === 0) {
346
+                    // Old share is already read-write, no more permissions can be gained
347
+                    continue;
348
+                }
349
+            }
350
+
351
+            list(, $name) = Uri\split($row['principaluri']);
352
+            $uri = $row['uri'] . '_shared_by_' . $name;
353
+            $row['displayname'] = $row['displayname'] . ' (' . $this->getUserDisplayName($name) . ')';
354
+            $components = [];
355
+            if ($row['components']) {
356
+                $components = explode(',',$row['components']);
357
+            }
358
+            $calendar = [
359
+                'id' => $row['id'],
360
+                'uri' => $uri,
361
+                'principaluri' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint),
362
+                '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'),
363
+                '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
364
+                '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components),
365
+                '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp('transparent'),
366
+                '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint),
367
+                $readOnlyPropertyName => $readOnly,
368
+            ];
369
+
370
+            foreach($this->propertyMap as $xmlName=>$dbName) {
371
+                $calendar[$xmlName] = $row[$dbName];
372
+            }
373
+
374
+            $this->addOwnerPrincipal($calendar);
375
+
376
+            $calendars[$calendar['id']] = $calendar;
377
+        }
378
+        $result->closeCursor();
379
+
380
+        return array_values($calendars);
381
+    }
382
+
383
+    /**
384
+     * @param $principalUri
385
+     * @return array
386
+     */
387
+    public function getUsersOwnCalendars($principalUri) {
388
+        $principalUri = $this->convertPrincipal($principalUri, true);
389
+        $fields = array_values($this->propertyMap);
390
+        $fields[] = 'id';
391
+        $fields[] = 'uri';
392
+        $fields[] = 'synctoken';
393
+        $fields[] = 'components';
394
+        $fields[] = 'principaluri';
395
+        $fields[] = 'transparent';
396
+        // Making fields a comma-delimited list
397
+        $query = $this->db->getQueryBuilder();
398
+        $query->select($fields)->from('calendars')
399
+            ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri)))
400
+            ->orderBy('calendarorder', 'ASC');
401
+        $stmt = $query->execute();
402
+        $calendars = [];
403
+        while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
404
+            $components = [];
405
+            if ($row['components']) {
406
+                $components = explode(',',$row['components']);
407
+            }
408
+            $calendar = [
409
+                'id' => $row['id'],
410
+                'uri' => $row['uri'],
411
+                'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint),
412
+                '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'),
413
+                '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
414
+                '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components),
415
+                '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'),
416
+            ];
417
+            foreach($this->propertyMap as $xmlName=>$dbName) {
418
+                $calendar[$xmlName] = $row[$dbName];
419
+            }
420
+
421
+            $this->addOwnerPrincipal($calendar);
422
+
423
+            if (!isset($calendars[$calendar['id']])) {
424
+                $calendars[$calendar['id']] = $calendar;
425
+            }
426
+        }
427
+        $stmt->closeCursor();
428
+        return array_values($calendars);
429
+    }
430
+
431
+
432
+    /**
433
+     * @param $uid
434
+     * @return string
435
+     */
436
+    private function getUserDisplayName($uid) {
437
+        if (!isset($this->userDisplayNames[$uid])) {
438
+            $user = $this->userManager->get($uid);
439
+
440
+            if ($user instanceof IUser) {
441
+                $this->userDisplayNames[$uid] = $user->getDisplayName();
442
+            } else {
443
+                $this->userDisplayNames[$uid] = $uid;
444
+            }
445
+        }
446
+
447
+        return $this->userDisplayNames[$uid];
448
+    }
449
+
450
+    /**
451
+     * @return array
452
+     */
453
+    public function getPublicCalendars() {
454
+        $fields = array_values($this->propertyMap);
455
+        $fields[] = 'a.id';
456
+        $fields[] = 'a.uri';
457
+        $fields[] = 'a.synctoken';
458
+        $fields[] = 'a.components';
459
+        $fields[] = 'a.principaluri';
460
+        $fields[] = 'a.transparent';
461
+        $fields[] = 's.access';
462
+        $fields[] = 's.publicuri';
463
+        $calendars = [];
464
+        $query = $this->db->getQueryBuilder();
465
+        $result = $query->select($fields)
466
+            ->from('dav_shares', 's')
467
+            ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id'))
468
+            ->where($query->expr()->in('s.access', $query->createNamedParameter(self::ACCESS_PUBLIC)))
469
+            ->andWhere($query->expr()->eq('s.type', $query->createNamedParameter('calendar')))
470
+            ->execute();
471
+
472
+        while($row = $result->fetch()) {
473
+            list(, $name) = Uri\split($row['principaluri']);
474
+            $row['displayname'] = $row['displayname'] . "($name)";
475
+            $components = [];
476
+            if ($row['components']) {
477
+                $components = explode(',',$row['components']);
478
+            }
479
+            $calendar = [
480
+                'id' => $row['id'],
481
+                'uri' => $row['publicuri'],
482
+                'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint),
483
+                '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'),
484
+                '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
485
+                '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components),
486
+                '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'),
487
+                '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], $this->legacyEndpoint),
488
+                '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ,
489
+                '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC,
490
+            ];
491
+
492
+            foreach($this->propertyMap as $xmlName=>$dbName) {
493
+                $calendar[$xmlName] = $row[$dbName];
494
+            }
495
+
496
+            $this->addOwnerPrincipal($calendar);
497
+
498
+            if (!isset($calendars[$calendar['id']])) {
499
+                $calendars[$calendar['id']] = $calendar;
500
+            }
501
+        }
502
+        $result->closeCursor();
503
+
504
+        return array_values($calendars);
505
+    }
506
+
507
+    /**
508
+     * @param string $uri
509
+     * @return array
510
+     * @throws NotFound
511
+     */
512
+    public function getPublicCalendar($uri) {
513
+        $fields = array_values($this->propertyMap);
514
+        $fields[] = 'a.id';
515
+        $fields[] = 'a.uri';
516
+        $fields[] = 'a.synctoken';
517
+        $fields[] = 'a.components';
518
+        $fields[] = 'a.principaluri';
519
+        $fields[] = 'a.transparent';
520
+        $fields[] = 's.access';
521
+        $fields[] = 's.publicuri';
522
+        $query = $this->db->getQueryBuilder();
523
+        $result = $query->select($fields)
524
+            ->from('dav_shares', 's')
525
+            ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id'))
526
+            ->where($query->expr()->in('s.access', $query->createNamedParameter(self::ACCESS_PUBLIC)))
527
+            ->andWhere($query->expr()->eq('s.type', $query->createNamedParameter('calendar')))
528
+            ->andWhere($query->expr()->eq('s.publicuri', $query->createNamedParameter($uri)))
529
+            ->execute();
530
+
531
+        $row = $result->fetch(\PDO::FETCH_ASSOC);
532
+
533
+        $result->closeCursor();
534
+
535
+        if ($row === false) {
536
+            throw new NotFound('Node with name \'' . $uri . '\' could not be found');
537
+        }
538
+
539
+        list(, $name) = Uri\split($row['principaluri']);
540
+        $row['displayname'] = $row['displayname'] . ' ' . "($name)";
541
+        $components = [];
542
+        if ($row['components']) {
543
+            $components = explode(',',$row['components']);
544
+        }
545
+        $calendar = [
546
+            'id' => $row['id'],
547
+            'uri' => $row['publicuri'],
548
+            'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint),
549
+            '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'),
550
+            '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
551
+            '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components),
552
+            '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'),
553
+            '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint),
554
+            '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ,
555
+            '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC,
556
+        ];
557
+
558
+        foreach($this->propertyMap as $xmlName=>$dbName) {
559
+            $calendar[$xmlName] = $row[$dbName];
560
+        }
561
+
562
+        $this->addOwnerPrincipal($calendar);
563
+
564
+        return $calendar;
565
+
566
+    }
567
+
568
+    /**
569
+     * @param string $principal
570
+     * @param string $uri
571
+     * @return array|null
572
+     */
573
+    public function getCalendarByUri($principal, $uri) {
574
+        $fields = array_values($this->propertyMap);
575
+        $fields[] = 'id';
576
+        $fields[] = 'uri';
577
+        $fields[] = 'synctoken';
578
+        $fields[] = 'components';
579
+        $fields[] = 'principaluri';
580
+        $fields[] = 'transparent';
581
+
582
+        // Making fields a comma-delimited list
583
+        $query = $this->db->getQueryBuilder();
584
+        $query->select($fields)->from('calendars')
585
+            ->where($query->expr()->eq('uri', $query->createNamedParameter($uri)))
586
+            ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal)))
587
+            ->setMaxResults(1);
588
+        $stmt = $query->execute();
589
+
590
+        $row = $stmt->fetch(\PDO::FETCH_ASSOC);
591
+        $stmt->closeCursor();
592
+        if ($row === false) {
593
+            return null;
594
+        }
595
+
596
+        $components = [];
597
+        if ($row['components']) {
598
+            $components = explode(',',$row['components']);
599
+        }
600
+
601
+        $calendar = [
602
+            'id' => $row['id'],
603
+            'uri' => $row['uri'],
604
+            'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint),
605
+            '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'),
606
+            '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
607
+            '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components),
608
+            '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'),
609
+        ];
610
+
611
+        foreach($this->propertyMap as $xmlName=>$dbName) {
612
+            $calendar[$xmlName] = $row[$dbName];
613
+        }
614
+
615
+        $this->addOwnerPrincipal($calendar);
616
+
617
+        return $calendar;
618
+    }
619
+
620
+    /**
621
+     * @param $calendarId
622
+     * @return array|null
623
+     */
624
+    public function getCalendarById($calendarId) {
625
+        $fields = array_values($this->propertyMap);
626
+        $fields[] = 'id';
627
+        $fields[] = 'uri';
628
+        $fields[] = 'synctoken';
629
+        $fields[] = 'components';
630
+        $fields[] = 'principaluri';
631
+        $fields[] = 'transparent';
632
+
633
+        // Making fields a comma-delimited list
634
+        $query = $this->db->getQueryBuilder();
635
+        $query->select($fields)->from('calendars')
636
+            ->where($query->expr()->eq('id', $query->createNamedParameter($calendarId)))
637
+            ->setMaxResults(1);
638
+        $stmt = $query->execute();
639
+
640
+        $row = $stmt->fetch(\PDO::FETCH_ASSOC);
641
+        $stmt->closeCursor();
642
+        if ($row === false) {
643
+            return null;
644
+        }
645
+
646
+        $components = [];
647
+        if ($row['components']) {
648
+            $components = explode(',',$row['components']);
649
+        }
650
+
651
+        $calendar = [
652
+            'id' => $row['id'],
653
+            'uri' => $row['uri'],
654
+            'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint),
655
+            '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'),
656
+            '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
657
+            '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components),
658
+            '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'),
659
+        ];
660
+
661
+        foreach($this->propertyMap as $xmlName=>$dbName) {
662
+            $calendar[$xmlName] = $row[$dbName];
663
+        }
664
+
665
+        $this->addOwnerPrincipal($calendar);
666
+
667
+        return $calendar;
668
+    }
669
+
670
+    /**
671
+     * @param $subscriptionId
672
+     */
673
+    public function getSubscriptionById($subscriptionId) {
674
+        $fields = array_values($this->subscriptionPropertyMap);
675
+        $fields[] = 'id';
676
+        $fields[] = 'uri';
677
+        $fields[] = 'source';
678
+        $fields[] = 'synctoken';
679
+        $fields[] = 'principaluri';
680
+        $fields[] = 'lastmodified';
681
+
682
+        $query = $this->db->getQueryBuilder();
683
+        $query->select($fields)
684
+            ->from('calendarsubscriptions')
685
+            ->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId)))
686
+            ->orderBy('calendarorder', 'asc');
687
+        $stmt =$query->execute();
688
+
689
+        $row = $stmt->fetch(\PDO::FETCH_ASSOC);
690
+        $stmt->closeCursor();
691
+        if ($row === false) {
692
+            return null;
693
+        }
694
+
695
+        $subscription = [
696
+            'id'           => $row['id'],
697
+            'uri'          => $row['uri'],
698
+            'principaluri' => $row['principaluri'],
699
+            'source'       => $row['source'],
700
+            'lastmodified' => $row['lastmodified'],
701
+            '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']),
702
+            '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
703
+        ];
704
+
705
+        foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) {
706
+            if (!is_null($row[$dbName])) {
707
+                $subscription[$xmlName] = $row[$dbName];
708
+            }
709
+        }
710
+
711
+        return $subscription;
712
+    }
713
+
714
+    /**
715
+     * Creates a new calendar for a principal.
716
+     *
717
+     * If the creation was a success, an id must be returned that can be used to reference
718
+     * this calendar in other methods, such as updateCalendar.
719
+     *
720
+     * @param string $principalUri
721
+     * @param string $calendarUri
722
+     * @param array $properties
723
+     * @return int
724
+     * @suppress SqlInjectionChecker
725
+     */
726
+    function createCalendar($principalUri, $calendarUri, array $properties) {
727
+        $values = [
728
+            'principaluri' => $this->convertPrincipal($principalUri, true),
729
+            'uri'          => $calendarUri,
730
+            'synctoken'    => 1,
731
+            'transparent'  => 0,
732
+            'components'   => 'VEVENT,VTODO',
733
+            'displayname'  => $calendarUri
734
+        ];
735
+
736
+        // Default value
737
+        $sccs = '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set';
738
+        if (isset($properties[$sccs])) {
739
+            if (!($properties[$sccs] instanceof SupportedCalendarComponentSet)) {
740
+                throw new DAV\Exception('The ' . $sccs . ' property must be of type: \Sabre\CalDAV\Property\SupportedCalendarComponentSet');
741
+            }
742
+            $values['components'] = implode(',',$properties[$sccs]->getValue());
743
+        } else if (isset($properties['components'])) {
744
+            // Allow to provide components internally without having
745
+            // to create a SupportedCalendarComponentSet object
746
+            $values['components'] = $properties['components'];
747
+        }
748
+
749
+        $transp = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp';
750
+        if (isset($properties[$transp])) {
751
+            $values['transparent'] = (int) ($properties[$transp]->getValue() === 'transparent');
752
+        }
753
+
754
+        foreach($this->propertyMap as $xmlName=>$dbName) {
755
+            if (isset($properties[$xmlName])) {
756
+                $values[$dbName] = $properties[$xmlName];
757
+            }
758
+        }
759
+
760
+        $query = $this->db->getQueryBuilder();
761
+        $query->insert('calendars');
762
+        foreach($values as $column => $value) {
763
+            $query->setValue($column, $query->createNamedParameter($value));
764
+        }
765
+        $query->execute();
766
+        $calendarId = $query->getLastInsertId();
767
+
768
+        $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::createCalendar', new GenericEvent(
769
+            '\OCA\DAV\CalDAV\CalDavBackend::createCalendar',
770
+            [
771
+                'calendarId' => $calendarId,
772
+                'calendarData' => $this->getCalendarById($calendarId),
773
+        ]));
774
+
775
+        return $calendarId;
776
+    }
777
+
778
+    /**
779
+     * Updates properties for a calendar.
780
+     *
781
+     * The list of mutations is stored in a Sabre\DAV\PropPatch object.
782
+     * To do the actual updates, you must tell this object which properties
783
+     * you're going to process with the handle() method.
784
+     *
785
+     * Calling the handle method is like telling the PropPatch object "I
786
+     * promise I can handle updating this property".
787
+     *
788
+     * Read the PropPatch documentation for more info and examples.
789
+     *
790
+     * @param mixed $calendarId
791
+     * @param PropPatch $propPatch
792
+     * @return void
793
+     */
794
+    function updateCalendar($calendarId, PropPatch $propPatch) {
795
+        $supportedProperties = array_keys($this->propertyMap);
796
+        $supportedProperties[] = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp';
797
+
798
+        /**
799
+         * @suppress SqlInjectionChecker
800
+         */
801
+        $propPatch->handle($supportedProperties, function($mutations) use ($calendarId) {
802
+            $newValues = [];
803
+            foreach ($mutations as $propertyName => $propertyValue) {
804
+
805
+                switch ($propertyName) {
806
+                    case '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' :
807
+                        $fieldName = 'transparent';
808
+                        $newValues[$fieldName] = (int) ($propertyValue->getValue() === 'transparent');
809
+                        break;
810
+                    default :
811
+                        $fieldName = $this->propertyMap[$propertyName];
812
+                        $newValues[$fieldName] = $propertyValue;
813
+                        break;
814
+                }
815
+
816
+            }
817
+            $query = $this->db->getQueryBuilder();
818
+            $query->update('calendars');
819
+            foreach ($newValues as $fieldName => $value) {
820
+                $query->set($fieldName, $query->createNamedParameter($value));
821
+            }
822
+            $query->where($query->expr()->eq('id', $query->createNamedParameter($calendarId)));
823
+            $query->execute();
824
+
825
+            $this->addChange($calendarId, "", 2);
826
+
827
+            $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateCalendar', new GenericEvent(
828
+                '\OCA\DAV\CalDAV\CalDavBackend::updateCalendar',
829
+                [
830
+                    'calendarId' => $calendarId,
831
+                    'calendarData' => $this->getCalendarById($calendarId),
832
+                    'shares' => $this->getShares($calendarId),
833
+                    'propertyMutations' => $mutations,
834
+            ]));
835
+
836
+            return true;
837
+        });
838
+    }
839
+
840
+    /**
841
+     * Delete a calendar and all it's objects
842
+     *
843
+     * @param mixed $calendarId
844
+     * @return void
845
+     */
846
+    function deleteCalendar($calendarId) {
847
+        $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendar', new GenericEvent(
848
+            '\OCA\DAV\CalDAV\CalDavBackend::deleteCalendar',
849
+            [
850
+                'calendarId' => $calendarId,
851
+                'calendarData' => $this->getCalendarById($calendarId),
852
+                'shares' => $this->getShares($calendarId),
853
+        ]));
854
+
855
+        $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ? AND `calendartype` = ?');
856
+        $stmt->execute([$calendarId, self::CALENDAR_TYPE_CALENDAR]);
857
+
858
+        $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendars` WHERE `id` = ?');
859
+        $stmt->execute([$calendarId]);
860
+
861
+        $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarchanges` WHERE `calendarid` = ? AND `calendartype` = ?');
862
+        $stmt->execute([$calendarId, self::CALENDAR_TYPE_CALENDAR]);
863
+
864
+        $this->calendarSharingBackend->deleteAllShares($calendarId);
865
+
866
+        $query = $this->db->getQueryBuilder();
867
+        $query->delete($this->dbObjectPropertiesTable)
868
+            ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId)))
869
+            ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_CALENDAR)))
870
+            ->execute();
871
+    }
872
+
873
+    /**
874
+     * Delete all of an user's shares
875
+     *
876
+     * @param string $principaluri
877
+     * @return void
878
+     */
879
+    function deleteAllSharesByUser($principaluri) {
880
+        $this->calendarSharingBackend->deleteAllSharesByUser($principaluri);
881
+    }
882
+
883
+    /**
884
+     * Returns all calendar objects within a calendar.
885
+     *
886
+     * Every item contains an array with the following keys:
887
+     *   * calendardata - The iCalendar-compatible calendar data
888
+     *   * uri - a unique key which will be used to construct the uri. This can
889
+     *     be any arbitrary string, but making sure it ends with '.ics' is a
890
+     *     good idea. This is only the basename, or filename, not the full
891
+     *     path.
892
+     *   * lastmodified - a timestamp of the last modification time
893
+     *   * etag - An arbitrary string, surrounded by double-quotes. (e.g.:
894
+     *   '"abcdef"')
895
+     *   * size - The size of the calendar objects, in bytes.
896
+     *   * component - optional, a string containing the type of object, such
897
+     *     as 'vevent' or 'vtodo'. If specified, this will be used to populate
898
+     *     the Content-Type header.
899
+     *
900
+     * Note that the etag is optional, but it's highly encouraged to return for
901
+     * speed reasons.
902
+     *
903
+     * The calendardata is also optional. If it's not returned
904
+     * 'getCalendarObject' will be called later, which *is* expected to return
905
+     * calendardata.
906
+     *
907
+     * If neither etag or size are specified, the calendardata will be
908
+     * used/fetched to determine these numbers. If both are specified the
909
+     * amount of times this is needed is reduced by a great degree.
910
+     *
911
+     * @param mixed $id
912
+     * @param int $calendarType
913
+     * @return array
914
+     */
915
+    public function getCalendarObjects($id, $calendarType=self::CALENDAR_TYPE_CALENDAR):array {
916
+        $query = $this->db->getQueryBuilder();
917
+        $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'componenttype', 'classification'])
918
+            ->from('calendarobjects')
919
+            ->where($query->expr()->eq('calendarid', $query->createNamedParameter($id)))
920
+            ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType)));
921
+        $stmt = $query->execute();
922
+
923
+        $result = [];
924
+        foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) {
925
+            $result[] = [
926
+                'id'           => $row['id'],
927
+                'uri'          => $row['uri'],
928
+                'lastmodified' => $row['lastmodified'],
929
+                'etag'         => '"' . $row['etag'] . '"',
930
+                'calendarid'   => $row['calendarid'],
931
+                'size'         => (int)$row['size'],
932
+                'component'    => strtolower($row['componenttype']),
933
+                'classification'=> (int)$row['classification']
934
+            ];
935
+        }
936
+
937
+        return $result;
938
+    }
939
+
940
+    /**
941
+     * Returns information from a single calendar object, based on it's object
942
+     * uri.
943
+     *
944
+     * The object uri is only the basename, or filename and not a full path.
945
+     *
946
+     * The returned array must have the same keys as getCalendarObjects. The
947
+     * 'calendardata' object is required here though, while it's not required
948
+     * for getCalendarObjects.
949
+     *
950
+     * This method must return null if the object did not exist.
951
+     *
952
+     * @param mixed $id
953
+     * @param string $objectUri
954
+     * @param int $calendarType
955
+     * @return array|null
956
+     */
957
+    public function getCalendarObject($id, $objectUri, $calendarType=self::CALENDAR_TYPE_CALENDAR) {
958
+        $query = $this->db->getQueryBuilder();
959
+        $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification'])
960
+            ->from('calendarobjects')
961
+            ->where($query->expr()->eq('calendarid', $query->createNamedParameter($id)))
962
+            ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri)))
963
+            ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType)));
964
+        $stmt = $query->execute();
965
+        $row = $stmt->fetch(\PDO::FETCH_ASSOC);
966
+
967
+        if(!$row) {
968
+            return null;
969
+        }
970
+
971
+        return [
972
+            'id'            => $row['id'],
973
+            'uri'           => $row['uri'],
974
+            'lastmodified'  => $row['lastmodified'],
975
+            'etag'          => '"' . $row['etag'] . '"',
976
+            'calendarid'    => $row['calendarid'],
977
+            'size'          => (int)$row['size'],
978
+            'calendardata'  => $this->readBlob($row['calendardata']),
979
+            'component'     => strtolower($row['componenttype']),
980
+            'classification'=> (int)$row['classification']
981
+        ];
982
+    }
983
+
984
+    /**
985
+     * Returns a list of calendar objects.
986
+     *
987
+     * This method should work identical to getCalendarObject, but instead
988
+     * return all the calendar objects in the list as an array.
989
+     *
990
+     * If the backend supports this, it may allow for some speed-ups.
991
+     *
992
+     * @param mixed $calendarId
993
+     * @param string[] $uris
994
+     * @param int $calendarType
995
+     * @return array
996
+     */
997
+    public function getMultipleCalendarObjects($id, array $uris, $calendarType=self::CALENDAR_TYPE_CALENDAR):array {
998
+        if (empty($uris)) {
999
+            return [];
1000
+        }
1001
+
1002
+        $chunks = array_chunk($uris, 100);
1003
+        $objects = [];
1004
+
1005
+        $query = $this->db->getQueryBuilder();
1006
+        $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification'])
1007
+            ->from('calendarobjects')
1008
+            ->where($query->expr()->eq('calendarid', $query->createNamedParameter($id)))
1009
+            ->andWhere($query->expr()->in('uri', $query->createParameter('uri')))
1010
+            ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType)));
1011
+
1012
+        foreach ($chunks as $uris) {
1013
+            $query->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY);
1014
+            $result = $query->execute();
1015
+
1016
+            while ($row = $result->fetch()) {
1017
+                $objects[] = [
1018
+                    'id'           => $row['id'],
1019
+                    'uri'          => $row['uri'],
1020
+                    'lastmodified' => $row['lastmodified'],
1021
+                    'etag'         => '"' . $row['etag'] . '"',
1022
+                    'calendarid'   => $row['calendarid'],
1023
+                    'size'         => (int)$row['size'],
1024
+                    'calendardata' => $this->readBlob($row['calendardata']),
1025
+                    'component'    => strtolower($row['componenttype']),
1026
+                    'classification' => (int)$row['classification']
1027
+                ];
1028
+            }
1029
+            $result->closeCursor();
1030
+        }
1031
+
1032
+        return $objects;
1033
+    }
1034
+
1035
+    /**
1036
+     * Creates a new calendar object.
1037
+     *
1038
+     * The object uri is only the basename, or filename and not a full path.
1039
+     *
1040
+     * It is possible return an etag from this function, which will be used in
1041
+     * the response to this PUT request. Note that the ETag must be surrounded
1042
+     * by double-quotes.
1043
+     *
1044
+     * However, you should only really return this ETag if you don't mangle the
1045
+     * calendar-data. If the result of a subsequent GET to this object is not
1046
+     * the exact same as this request body, you should omit the ETag.
1047
+     *
1048
+     * @param mixed $calendarId
1049
+     * @param string $objectUri
1050
+     * @param string $calendarData
1051
+     * @param int $calendarType
1052
+     * @return string
1053
+     */
1054
+    function createCalendarObject($calendarId, $objectUri, $calendarData, $calendarType=self::CALENDAR_TYPE_CALENDAR) {
1055
+        $extraData = $this->getDenormalizedData($calendarData);
1056
+
1057
+        $q = $this->db->getQueryBuilder();
1058
+        $q->select($q->func()->count('*'))
1059
+            ->from('calendarobjects')
1060
+            ->where($q->expr()->eq('calendarid', $q->createNamedParameter($calendarId)))
1061
+            ->andWhere($q->expr()->eq('uid', $q->createNamedParameter($extraData['uid'])))
1062
+            ->andWhere($q->expr()->eq('calendartype', $q->createNamedParameter($calendarType)));
1063
+
1064
+        $result = $q->execute();
1065
+        $count = (int) $result->fetchColumn();
1066
+        $result->closeCursor();
1067
+
1068
+        if ($count !== 0) {
1069
+            throw new \Sabre\DAV\Exception\BadRequest('Calendar object with uid already exists in this calendar collection.');
1070
+        }
1071
+
1072
+        $query = $this->db->getQueryBuilder();
1073
+        $query->insert('calendarobjects')
1074
+            ->values([
1075
+                'calendarid' => $query->createNamedParameter($calendarId),
1076
+                'uri' => $query->createNamedParameter($objectUri),
1077
+                'calendardata' => $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB),
1078
+                'lastmodified' => $query->createNamedParameter(time()),
1079
+                'etag' => $query->createNamedParameter($extraData['etag']),
1080
+                'size' => $query->createNamedParameter($extraData['size']),
1081
+                'componenttype' => $query->createNamedParameter($extraData['componentType']),
1082
+                'firstoccurence' => $query->createNamedParameter($extraData['firstOccurence']),
1083
+                'lastoccurence' => $query->createNamedParameter($extraData['lastOccurence']),
1084
+                'classification' => $query->createNamedParameter($extraData['classification']),
1085
+                'uid' => $query->createNamedParameter($extraData['uid']),
1086
+                'calendartype' => $query->createNamedParameter($calendarType),
1087
+            ])
1088
+            ->execute();
1089
+
1090
+        $this->updateProperties($calendarId, $objectUri, $calendarData, $calendarType);
1091
+
1092
+        if ($calendarType === self::CALENDAR_TYPE_CALENDAR) {
1093
+            $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject', new GenericEvent(
1094
+                '\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject',
1095
+                [
1096
+                    'calendarId' => $calendarId,
1097
+                    'calendarData' => $this->getCalendarById($calendarId),
1098
+                    'shares' => $this->getShares($calendarId),
1099
+                    'objectData' => $this->getCalendarObject($calendarId, $objectUri),
1100
+                ]
1101
+            ));
1102
+        } else {
1103
+            $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::createCachedCalendarObject', new GenericEvent(
1104
+                '\OCA\DAV\CalDAV\CalDavBackend::createCachedCalendarObject',
1105
+                [
1106
+                    'subscriptionId' => $calendarId,
1107
+                    'calendarData' => $this->getCalendarById($calendarId),
1108
+                    'shares' => $this->getShares($calendarId),
1109
+                    'objectData' => $this->getCalendarObject($calendarId, $objectUri),
1110
+                ]
1111
+            ));
1112
+        }
1113
+        $this->addChange($calendarId, $objectUri, 1, $calendarType);
1114
+
1115
+        return '"' . $extraData['etag'] . '"';
1116
+    }
1117
+
1118
+    /**
1119
+     * Updates an existing calendarobject, based on it's uri.
1120
+     *
1121
+     * The object uri is only the basename, or filename and not a full path.
1122
+     *
1123
+     * It is possible return an etag from this function, which will be used in
1124
+     * the response to this PUT request. Note that the ETag must be surrounded
1125
+     * by double-quotes.
1126
+     *
1127
+     * However, you should only really return this ETag if you don't mangle the
1128
+     * calendar-data. If the result of a subsequent GET to this object is not
1129
+     * the exact same as this request body, you should omit the ETag.
1130
+     *
1131
+     * @param mixed $calendarId
1132
+     * @param string $objectUri
1133
+     * @param string $calendarData
1134
+     * @param int $calendarType
1135
+     * @return string
1136
+     */
1137
+    function updateCalendarObject($calendarId, $objectUri, $calendarData, $calendarType=self::CALENDAR_TYPE_CALENDAR) {
1138
+        $extraData = $this->getDenormalizedData($calendarData);
1139
+        $query = $this->db->getQueryBuilder();
1140
+        $query->update('calendarobjects')
1141
+                ->set('calendardata', $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB))
1142
+                ->set('lastmodified', $query->createNamedParameter(time()))
1143
+                ->set('etag', $query->createNamedParameter($extraData['etag']))
1144
+                ->set('size', $query->createNamedParameter($extraData['size']))
1145
+                ->set('componenttype', $query->createNamedParameter($extraData['componentType']))
1146
+                ->set('firstoccurence', $query->createNamedParameter($extraData['firstOccurence']))
1147
+                ->set('lastoccurence', $query->createNamedParameter($extraData['lastOccurence']))
1148
+                ->set('classification', $query->createNamedParameter($extraData['classification']))
1149
+                ->set('uid', $query->createNamedParameter($extraData['uid']))
1150
+            ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId)))
1151
+            ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri)))
1152
+            ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType)))
1153
+            ->execute();
1154
+
1155
+        $this->updateProperties($calendarId, $objectUri, $calendarData, $calendarType);
1156
+
1157
+        $data = $this->getCalendarObject($calendarId, $objectUri);
1158
+        if (is_array($data)) {
1159
+            if ($calendarType === self::CALENDAR_TYPE_CALENDAR) {
1160
+                $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject', new GenericEvent(
1161
+                    '\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject',
1162
+                    [
1163
+                        'calendarId' => $calendarId,
1164
+                        'calendarData' => $this->getCalendarById($calendarId),
1165
+                        'shares' => $this->getShares($calendarId),
1166
+                        'objectData' => $data,
1167
+                    ]
1168
+                ));
1169
+            } else {
1170
+                $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateCachedCalendarObject', new GenericEvent(
1171
+                    '\OCA\DAV\CalDAV\CalDavBackend::updateCachedCalendarObject',
1172
+                    [
1173
+                        'subscriptionId' => $calendarId,
1174
+                        'calendarData' => $this->getCalendarById($calendarId),
1175
+                        'shares' => $this->getShares($calendarId),
1176
+                        'objectData' => $data,
1177
+                    ]
1178
+                ));
1179
+            }
1180
+        }
1181
+        $this->addChange($calendarId, $objectUri, 2, $calendarType);
1182
+
1183
+        return '"' . $extraData['etag'] . '"';
1184
+    }
1185
+
1186
+    /**
1187
+     * @param int $calendarObjectId
1188
+     * @param int $classification
1189
+     */
1190
+    public function setClassification($calendarObjectId, $classification) {
1191
+        if (!in_array($classification, [
1192
+            self::CLASSIFICATION_PUBLIC, self::CLASSIFICATION_PRIVATE, self::CLASSIFICATION_CONFIDENTIAL
1193
+        ])) {
1194
+            throw new \InvalidArgumentException();
1195
+        }
1196
+        $query = $this->db->getQueryBuilder();
1197
+        $query->update('calendarobjects')
1198
+            ->set('classification', $query->createNamedParameter($classification))
1199
+            ->where($query->expr()->eq('id', $query->createNamedParameter($calendarObjectId)))
1200
+            ->execute();
1201
+    }
1202
+
1203
+    /**
1204
+     * Deletes an existing calendar object.
1205
+     *
1206
+     * The object uri is only the basename, or filename and not a full path.
1207
+     *
1208
+     * @param mixed $calendarId
1209
+     * @param string $objectUri
1210
+     * @param int $calendarType
1211
+     * @return void
1212
+     */
1213
+    function deleteCalendarObject($calendarId, $objectUri, $calendarType=self::CALENDAR_TYPE_CALENDAR) {
1214
+        $data = $this->getCalendarObject($calendarId, $objectUri, $calendarType);
1215
+        if (is_array($data)) {
1216
+            if ($calendarType === self::CALENDAR_TYPE_CALENDAR) {
1217
+                $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject', new GenericEvent(
1218
+                    '\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject',
1219
+                    [
1220
+                        'calendarId' => $calendarId,
1221
+                        'calendarData' => $this->getCalendarById($calendarId),
1222
+                        'shares' => $this->getShares($calendarId),
1223
+                        'objectData' => $data,
1224
+                    ]
1225
+                ));
1226
+            } else {
1227
+                $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::deleteCachedCalendarObject', new GenericEvent(
1228
+                    '\OCA\DAV\CalDAV\CalDavBackend::deleteCachedCalendarObject',
1229
+                    [
1230
+                        'subscriptionId' => $calendarId,
1231
+                        'calendarData' => $this->getCalendarById($calendarId),
1232
+                        'shares' => $this->getShares($calendarId),
1233
+                        'objectData' => $data,
1234
+                    ]
1235
+                ));
1236
+            }
1237
+        }
1238
+
1239
+        $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ? AND `uri` = ? AND `calendartype` = ?');
1240
+        $stmt->execute([$calendarId, $objectUri, $calendarType]);
1241
+
1242
+        if (is_array($data)) {
1243
+            $this->purgeProperties($calendarId, $data['id'], $calendarType);
1244
+        }
1245
+
1246
+        $this->addChange($calendarId, $objectUri, 3, $calendarType);
1247
+    }
1248
+
1249
+    /**
1250
+     * Performs a calendar-query on the contents of this calendar.
1251
+     *
1252
+     * The calendar-query is defined in RFC4791 : CalDAV. Using the
1253
+     * calendar-query it is possible for a client to request a specific set of
1254
+     * object, based on contents of iCalendar properties, date-ranges and
1255
+     * iCalendar component types (VTODO, VEVENT).
1256
+     *
1257
+     * This method should just return a list of (relative) urls that match this
1258
+     * query.
1259
+     *
1260
+     * The list of filters are specified as an array. The exact array is
1261
+     * documented by Sabre\CalDAV\CalendarQueryParser.
1262
+     *
1263
+     * Note that it is extremely likely that getCalendarObject for every path
1264
+     * returned from this method will be called almost immediately after. You
1265
+     * may want to anticipate this to speed up these requests.
1266
+     *
1267
+     * This method provides a default implementation, which parses *all* the
1268
+     * iCalendar objects in the specified calendar.
1269
+     *
1270
+     * This default may well be good enough for personal use, and calendars
1271
+     * that aren't very large. But if you anticipate high usage, big calendars
1272
+     * or high loads, you are strongly advised to optimize certain paths.
1273
+     *
1274
+     * The best way to do so is override this method and to optimize
1275
+     * specifically for 'common filters'.
1276
+     *
1277
+     * Requests that are extremely common are:
1278
+     *   * requests for just VEVENTS
1279
+     *   * requests for just VTODO
1280
+     *   * requests with a time-range-filter on either VEVENT or VTODO.
1281
+     *
1282
+     * ..and combinations of these requests. It may not be worth it to try to
1283
+     * handle every possible situation and just rely on the (relatively
1284
+     * easy to use) CalendarQueryValidator to handle the rest.
1285
+     *
1286
+     * Note that especially time-range-filters may be difficult to parse. A
1287
+     * time-range filter specified on a VEVENT must for instance also handle
1288
+     * recurrence rules correctly.
1289
+     * A good example of how to interprete all these filters can also simply
1290
+     * be found in Sabre\CalDAV\CalendarQueryFilter. This class is as correct
1291
+     * as possible, so it gives you a good idea on what type of stuff you need
1292
+     * to think of.
1293
+     *
1294
+     * @param mixed $id
1295
+     * @param array $filters
1296
+     * @param int $calendarType
1297
+     * @return array
1298
+     */
1299
+    public function calendarQuery($id, array $filters, $calendarType=self::CALENDAR_TYPE_CALENDAR):array {
1300
+        $componentType = null;
1301
+        $requirePostFilter = true;
1302
+        $timeRange = null;
1303
+
1304
+        // if no filters were specified, we don't need to filter after a query
1305
+        if (!$filters['prop-filters'] && !$filters['comp-filters']) {
1306
+            $requirePostFilter = false;
1307
+        }
1308
+
1309
+        // Figuring out if there's a component filter
1310
+        if (count($filters['comp-filters']) > 0 && !$filters['comp-filters'][0]['is-not-defined']) {
1311
+            $componentType = $filters['comp-filters'][0]['name'];
1312
+
1313
+            // Checking if we need post-filters
1314
+            if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['time-range'] && !$filters['comp-filters'][0]['prop-filters']) {
1315
+                $requirePostFilter = false;
1316
+            }
1317
+            // There was a time-range filter
1318
+            if ($componentType === 'VEVENT' && isset($filters['comp-filters'][0]['time-range'])) {
1319
+                $timeRange = $filters['comp-filters'][0]['time-range'];
1320
+
1321
+                // If start time OR the end time is not specified, we can do a
1322
+                // 100% accurate mysql query.
1323
+                if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['prop-filters'] && (!$timeRange['start'] || !$timeRange['end'])) {
1324
+                    $requirePostFilter = false;
1325
+                }
1326
+            }
1327
+
1328
+        }
1329
+        $columns = ['uri'];
1330
+        if ($requirePostFilter) {
1331
+            $columns = ['uri', 'calendardata'];
1332
+        }
1333
+        $query = $this->db->getQueryBuilder();
1334
+        $query->select($columns)
1335
+            ->from('calendarobjects')
1336
+            ->where($query->expr()->eq('calendarid', $query->createNamedParameter($id)))
1337
+            ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType)));
1338
+
1339
+        if ($componentType) {
1340
+            $query->andWhere($query->expr()->eq('componenttype', $query->createNamedParameter($componentType)));
1341
+        }
1342
+
1343
+        if ($timeRange && $timeRange['start']) {
1344
+            $query->andWhere($query->expr()->gt('lastoccurence', $query->createNamedParameter($timeRange['start']->getTimeStamp())));
1345
+        }
1346
+        if ($timeRange && $timeRange['end']) {
1347
+            $query->andWhere($query->expr()->lt('firstoccurence', $query->createNamedParameter($timeRange['end']->getTimeStamp())));
1348
+        }
1349
+
1350
+        $stmt = $query->execute();
1351
+
1352
+        $result = [];
1353
+        while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
1354
+            if ($requirePostFilter) {
1355
+                // validateFilterForObject will parse the calendar data
1356
+                // catch parsing errors
1357
+                try {
1358
+                    $matches = $this->validateFilterForObject($row, $filters);
1359
+                } catch(ParseException $ex) {
1360
+                    $this->logger->logException($ex, [
1361
+                        'app' => 'dav',
1362
+                        'message' => 'Caught parsing exception for calendar data. This usually indicates invalid calendar data. calendar-id:'.$id.' uri:'.$row['uri']
1363
+                    ]);
1364
+                    continue;
1365
+                } catch (InvalidDataException $ex) {
1366
+                    $this->logger->logException($ex, [
1367
+                        'app' => 'dav',
1368
+                        'message' => 'Caught invalid data exception for calendar data. This usually indicates invalid calendar data. calendar-id:'.$id.' uri:'.$row['uri']
1369
+                    ]);
1370
+                    continue;
1371
+                }
1372
+
1373
+                if (!$matches) {
1374
+                    continue;
1375
+                }
1376
+            }
1377
+            $result[] = $row['uri'];
1378
+        }
1379
+
1380
+        return $result;
1381
+    }
1382
+
1383
+    /**
1384
+     * custom Nextcloud search extension for CalDAV
1385
+     *
1386
+     * TODO - this should optionally cover cached calendar objects as well
1387
+     *
1388
+     * @param string $principalUri
1389
+     * @param array $filters
1390
+     * @param integer|null $limit
1391
+     * @param integer|null $offset
1392
+     * @return array
1393
+     */
1394
+    public function calendarSearch($principalUri, array $filters, $limit=null, $offset=null) {
1395
+        $calendars = $this->getCalendarsForUser($principalUri);
1396
+        $ownCalendars = [];
1397
+        $sharedCalendars = [];
1398
+
1399
+        $uriMapper = [];
1400
+
1401
+        foreach($calendars as $calendar) {
1402
+            if ($calendar['{http://owncloud.org/ns}owner-principal'] === $principalUri) {
1403
+                $ownCalendars[] = $calendar['id'];
1404
+            } else {
1405
+                $sharedCalendars[] = $calendar['id'];
1406
+            }
1407
+            $uriMapper[$calendar['id']] = $calendar['uri'];
1408
+        }
1409
+        if (count($ownCalendars) === 0 && count($sharedCalendars) === 0) {
1410
+            return [];
1411
+        }
1412
+
1413
+        $query = $this->db->getQueryBuilder();
1414
+        // Calendar id expressions
1415
+        $calendarExpressions = [];
1416
+        foreach($ownCalendars as $id) {
1417
+            $calendarExpressions[] = $query->expr()->andX(
1418
+                $query->expr()->eq('c.calendarid',
1419
+                    $query->createNamedParameter($id)),
1420
+                $query->expr()->eq('c.calendartype',
1421
+                        $query->createNamedParameter(self::CALENDAR_TYPE_CALENDAR)));
1422
+        }
1423
+        foreach($sharedCalendars as $id) {
1424
+            $calendarExpressions[] = $query->expr()->andX(
1425
+                $query->expr()->eq('c.calendarid',
1426
+                    $query->createNamedParameter($id)),
1427
+                $query->expr()->eq('c.classification',
1428
+                    $query->createNamedParameter(self::CLASSIFICATION_PUBLIC)),
1429
+                $query->expr()->eq('c.calendartype',
1430
+                    $query->createNamedParameter(self::CALENDAR_TYPE_CALENDAR)));
1431
+        }
1432
+
1433
+        if (count($calendarExpressions) === 1) {
1434
+            $calExpr = $calendarExpressions[0];
1435
+        } else {
1436
+            $calExpr = call_user_func_array([$query->expr(), 'orX'], $calendarExpressions);
1437
+        }
1438
+
1439
+        // Component expressions
1440
+        $compExpressions = [];
1441
+        foreach($filters['comps'] as $comp) {
1442
+            $compExpressions[] = $query->expr()
1443
+                ->eq('c.componenttype', $query->createNamedParameter($comp));
1444
+        }
1445
+
1446
+        if (count($compExpressions) === 1) {
1447
+            $compExpr = $compExpressions[0];
1448
+        } else {
1449
+            $compExpr = call_user_func_array([$query->expr(), 'orX'], $compExpressions);
1450
+        }
1451
+
1452
+        if (!isset($filters['props'])) {
1453
+            $filters['props'] = [];
1454
+        }
1455
+        if (!isset($filters['params'])) {
1456
+            $filters['params'] = [];
1457
+        }
1458
+
1459
+        $propParamExpressions = [];
1460
+        foreach($filters['props'] as $prop) {
1461
+            $propParamExpressions[] = $query->expr()->andX(
1462
+                $query->expr()->eq('i.name', $query->createNamedParameter($prop)),
1463
+                $query->expr()->isNull('i.parameter')
1464
+            );
1465
+        }
1466
+        foreach($filters['params'] as $param) {
1467
+            $propParamExpressions[] = $query->expr()->andX(
1468
+                $query->expr()->eq('i.name', $query->createNamedParameter($param['property'])),
1469
+                $query->expr()->eq('i.parameter', $query->createNamedParameter($param['parameter']))
1470
+            );
1471
+        }
1472
+
1473
+        if (count($propParamExpressions) === 1) {
1474
+            $propParamExpr = $propParamExpressions[0];
1475
+        } else {
1476
+            $propParamExpr = call_user_func_array([$query->expr(), 'orX'], $propParamExpressions);
1477
+        }
1478
+
1479
+        $query->select(['c.calendarid', 'c.uri'])
1480
+            ->from($this->dbObjectPropertiesTable, 'i')
1481
+            ->join('i', 'calendarobjects', 'c', $query->expr()->eq('i.objectid', 'c.id'))
1482
+            ->where($calExpr)
1483
+            ->andWhere($compExpr)
1484
+            ->andWhere($propParamExpr)
1485
+            ->andWhere($query->expr()->iLike('i.value',
1486
+                $query->createNamedParameter('%'.$this->db->escapeLikeParameter($filters['search-term']).'%')));
1487
+
1488
+        if ($offset) {
1489
+            $query->setFirstResult($offset);
1490
+        }
1491
+        if ($limit) {
1492
+            $query->setMaxResults($limit);
1493
+        }
1494
+
1495
+        $stmt = $query->execute();
1496
+
1497
+        $result = [];
1498
+        while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
1499
+            $path = $uriMapper[$row['calendarid']] . '/' . $row['uri'];
1500
+            if (!in_array($path, $result)) {
1501
+                $result[] = $path;
1502
+            }
1503
+        }
1504
+
1505
+        return $result;
1506
+    }
1507
+
1508
+    /**
1509
+     * used for Nextcloud's calendar API
1510
+     *
1511
+     * @param array $calendarInfo
1512
+     * @param string $pattern
1513
+     * @param array $searchProperties
1514
+     * @param array $options
1515
+     * @param integer|null $limit
1516
+     * @param integer|null $offset
1517
+     *
1518
+     * @return array
1519
+     */
1520
+    public function search(array $calendarInfo, $pattern, array $searchProperties,
1521
+                            array $options, $limit, $offset) {
1522
+        $outerQuery = $this->db->getQueryBuilder();
1523
+        $innerQuery = $this->db->getQueryBuilder();
1524
+
1525
+        $innerQuery->selectDistinct('op.objectid')
1526
+            ->from($this->dbObjectPropertiesTable, 'op')
1527
+            ->andWhere($innerQuery->expr()->eq('op.calendarid',
1528
+                $outerQuery->createNamedParameter($calendarInfo['id'])))
1529
+            ->andWhere($innerQuery->expr()->eq('op.calendartype',
1530
+                $outerQuery->createNamedParameter(self::CALENDAR_TYPE_CALENDAR)));
1531
+
1532
+        // only return public items for shared calendars for now
1533
+        if ($calendarInfo['principaluri'] !== $calendarInfo['{http://owncloud.org/ns}owner-principal']) {
1534
+            $innerQuery->andWhere($innerQuery->expr()->eq('c.classification',
1535
+                $outerQuery->createNamedParameter(self::CLASSIFICATION_PUBLIC)));
1536
+        }
1537
+
1538
+        $or = $innerQuery->expr()->orX();
1539
+        foreach($searchProperties as $searchProperty) {
1540
+            $or->add($innerQuery->expr()->eq('op.name',
1541
+                $outerQuery->createNamedParameter($searchProperty)));
1542
+        }
1543
+        $innerQuery->andWhere($or);
1544
+
1545
+        if ($pattern !== '') {
1546
+            $innerQuery->andWhere($innerQuery->expr()->iLike('op.value',
1547
+                $outerQuery->createNamedParameter('%' .
1548
+                    $this->db->escapeLikeParameter($pattern) . '%')));
1549
+        }
1550
+
1551
+        $outerQuery->select('c.id', 'c.calendardata', 'c.componenttype', 'c.uid', 'c.uri')
1552
+            ->from('calendarobjects', 'c');
1553
+
1554
+        if (isset($options['timerange'])) {
1555
+            if (isset($options['timerange']['start']) && $options['timerange']['start'] instanceof DateTime) {
1556
+                $outerQuery->andWhere($outerQuery->expr()->gt('lastoccurence',
1557
+                    $outerQuery->createNamedParameter($options['timerange']['start']->getTimeStamp())));
1558
+
1559
+            }
1560
+            if (isset($options['timerange']['end']) && $options['timerange']['end'] instanceof DateTime) {
1561
+                $outerQuery->andWhere($outerQuery->expr()->lt('firstoccurence',
1562
+                    $outerQuery->createNamedParameter($options['timerange']['end']->getTimeStamp())));
1563
+            }
1564
+        }
1565
+
1566
+        if (isset($options['types'])) {
1567
+            $or = $outerQuery->expr()->orX();
1568
+            foreach($options['types'] as $type) {
1569
+                $or->add($outerQuery->expr()->eq('componenttype',
1570
+                    $outerQuery->createNamedParameter($type)));
1571
+            }
1572
+            $outerQuery->andWhere($or);
1573
+        }
1574
+
1575
+        $outerQuery->andWhere($outerQuery->expr()->in('c.id',
1576
+            $outerQuery->createFunction($innerQuery->getSQL())));
1577
+
1578
+        if ($offset) {
1579
+            $outerQuery->setFirstResult($offset);
1580
+        }
1581
+        if ($limit) {
1582
+            $outerQuery->setMaxResults($limit);
1583
+        }
1584
+
1585
+        $result = $outerQuery->execute();
1586
+        $calendarObjects = $result->fetchAll();
1587
+
1588
+        return array_map(function($o) {
1589
+            $calendarData = Reader::read($o['calendardata']);
1590
+            $comps = $calendarData->getComponents();
1591
+            $objects = [];
1592
+            $timezones = [];
1593
+            foreach($comps as $comp) {
1594
+                if ($comp instanceof VTimeZone) {
1595
+                    $timezones[] = $comp;
1596
+                } else {
1597
+                    $objects[] = $comp;
1598
+                }
1599
+            }
1600
+
1601
+            return [
1602
+                'id' => $o['id'],
1603
+                'type' => $o['componenttype'],
1604
+                'uid' => $o['uid'],
1605
+                'uri' => $o['uri'],
1606
+                'objects' => array_map(function($c) {
1607
+                    return $this->transformSearchData($c);
1608
+                }, $objects),
1609
+                'timezones' => array_map(function($c) {
1610
+                    return $this->transformSearchData($c);
1611
+                }, $timezones),
1612
+            ];
1613
+        }, $calendarObjects);
1614
+    }
1615
+
1616
+    /**
1617
+     * @param Component $comp
1618
+     * @return array
1619
+     */
1620
+    private function transformSearchData(Component $comp) {
1621
+        $data = [];
1622
+        /** @var Component[] $subComponents */
1623
+        $subComponents = $comp->getComponents();
1624
+        /** @var Property[] $properties */
1625
+        $properties = array_filter($comp->children(), function($c) {
1626
+            return $c instanceof Property;
1627
+        });
1628
+        $validationRules = $comp->getValidationRules();
1629
+
1630
+        foreach($subComponents as $subComponent) {
1631
+            $name = $subComponent->name;
1632
+            if (!isset($data[$name])) {
1633
+                $data[$name] = [];
1634
+            }
1635
+            $data[$name][] = $this->transformSearchData($subComponent);
1636
+        }
1637
+
1638
+        foreach($properties as $property) {
1639
+            $name = $property->name;
1640
+            if (!isset($validationRules[$name])) {
1641
+                $validationRules[$name] = '*';
1642
+            }
1643
+
1644
+            $rule = $validationRules[$property->name];
1645
+            if ($rule === '+' || $rule === '*') { // multiple
1646
+                if (!isset($data[$name])) {
1647
+                    $data[$name] = [];
1648
+                }
1649
+
1650
+                $data[$name][] = $this->transformSearchProperty($property);
1651
+            } else { // once
1652
+                $data[$name] = $this->transformSearchProperty($property);
1653
+            }
1654
+        }
1655
+
1656
+        return $data;
1657
+    }
1658
+
1659
+    /**
1660
+     * @param Property $prop
1661
+     * @return array
1662
+     */
1663
+    private function transformSearchProperty(Property $prop) {
1664
+        // No need to check Date, as it extends DateTime
1665
+        if ($prop instanceof Property\ICalendar\DateTime) {
1666
+            $value = $prop->getDateTime();
1667
+        } else {
1668
+            $value = $prop->getValue();
1669
+        }
1670
+
1671
+        return [
1672
+            $value,
1673
+            $prop->parameters()
1674
+        ];
1675
+    }
1676
+
1677
+    /**
1678
+     * Searches through all of a users calendars and calendar objects to find
1679
+     * an object with a specific UID.
1680
+     *
1681
+     * This method should return the path to this object, relative to the
1682
+     * calendar home, so this path usually only contains two parts:
1683
+     *
1684
+     * calendarpath/objectpath.ics
1685
+     *
1686
+     * If the uid is not found, return null.
1687
+     *
1688
+     * This method should only consider * objects that the principal owns, so
1689
+     * any calendars owned by other principals that also appear in this
1690
+     * collection should be ignored.
1691
+     *
1692
+     * @param string $principalUri
1693
+     * @param string $uid
1694
+     * @return string|null
1695
+     */
1696
+    function getCalendarObjectByUID($principalUri, $uid) {
1697
+
1698
+        $query = $this->db->getQueryBuilder();
1699
+        $query->selectAlias('c.uri', 'calendaruri')->selectAlias('co.uri', 'objecturi')
1700
+            ->from('calendarobjects', 'co')
1701
+            ->leftJoin('co', 'calendars', 'c', $query->expr()->eq('co.calendarid', 'c.id'))
1702
+            ->where($query->expr()->eq('c.principaluri', $query->createNamedParameter($principalUri)))
1703
+            ->andWhere($query->expr()->eq('co.uid', $query->createNamedParameter($uid)))
1704
+            ->andWhere($query->expr()->eq('co.uid', $query->createNamedParameter($uid)));
1705
+
1706
+        $stmt = $query->execute();
1707
+
1708
+        if ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
1709
+            return $row['calendaruri'] . '/' . $row['objecturi'];
1710
+        }
1711
+
1712
+        return null;
1713
+    }
1714
+
1715
+    /**
1716
+     * The getChanges method returns all the changes that have happened, since
1717
+     * the specified syncToken in the specified calendar.
1718
+     *
1719
+     * This function should return an array, such as the following:
1720
+     *
1721
+     * [
1722
+     *   'syncToken' => 'The current synctoken',
1723
+     *   'added'   => [
1724
+     *      'new.txt',
1725
+     *   ],
1726
+     *   'modified'   => [
1727
+     *      'modified.txt',
1728
+     *   ],
1729
+     *   'deleted' => [
1730
+     *      'foo.php.bak',
1731
+     *      'old.txt'
1732
+     *   ]
1733
+     * );
1734
+     *
1735
+     * The returned syncToken property should reflect the *current* syncToken
1736
+     * of the calendar, as reported in the {http://sabredav.org/ns}sync-token
1737
+     * property This is * needed here too, to ensure the operation is atomic.
1738
+     *
1739
+     * If the $syncToken argument is specified as null, this is an initial
1740
+     * sync, and all members should be reported.
1741
+     *
1742
+     * The modified property is an array of nodenames that have changed since
1743
+     * the last token.
1744
+     *
1745
+     * The deleted property is an array with nodenames, that have been deleted
1746
+     * from collection.
1747
+     *
1748
+     * The $syncLevel argument is basically the 'depth' of the report. If it's
1749
+     * 1, you only have to report changes that happened only directly in
1750
+     * immediate descendants. If it's 2, it should also include changes from
1751
+     * the nodes below the child collections. (grandchildren)
1752
+     *
1753
+     * The $limit argument allows a client to specify how many results should
1754
+     * be returned at most. If the limit is not specified, it should be treated
1755
+     * as infinite.
1756
+     *
1757
+     * If the limit (infinite or not) is higher than you're willing to return,
1758
+     * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception.
1759
+     *
1760
+     * If the syncToken is expired (due to data cleanup) or unknown, you must
1761
+     * return null.
1762
+     *
1763
+     * The limit is 'suggestive'. You are free to ignore it.
1764
+     *
1765
+     * @param string $calendarId
1766
+     * @param string $syncToken
1767
+     * @param int $syncLevel
1768
+     * @param int $limit
1769
+     * @param int $calendarType
1770
+     * @return array
1771
+     */
1772
+    function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null, $calendarType=self::CALENDAR_TYPE_CALENDAR) {
1773
+        // Current synctoken
1774
+        $stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*calendars` WHERE `id` = ?');
1775
+        $stmt->execute([ $calendarId ]);
1776
+        $currentToken = $stmt->fetchColumn(0);
1777
+
1778
+        if (is_null($currentToken)) {
1779
+            return null;
1780
+        }
1781
+
1782
+        $result = [
1783
+            'syncToken' => $currentToken,
1784
+            'added'     => [],
1785
+            'modified'  => [],
1786
+            'deleted'   => [],
1787
+        ];
1788
+
1789
+        if ($syncToken) {
1790
+
1791
+            $query = "SELECT `uri`, `operation` FROM `*PREFIX*calendarchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `calendarid` = ? AND `calendartype` = ? ORDER BY `synctoken`";
1792
+            if ($limit>0) {
1793
+                $query.= " LIMIT " . (int)$limit;
1794
+            }
1795
+
1796
+            // Fetching all changes
1797
+            $stmt = $this->db->prepare($query);
1798
+            $stmt->execute([$syncToken, $currentToken, $calendarId, $calendarType]);
1799
+
1800
+            $changes = [];
1801
+
1802
+            // This loop ensures that any duplicates are overwritten, only the
1803
+            // last change on a node is relevant.
1804
+            while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
1805
+
1806
+                $changes[$row['uri']] = $row['operation'];
1807
+
1808
+            }
1809
+
1810
+            foreach($changes as $uri => $operation) {
1811
+
1812
+                switch($operation) {
1813
+                    case 1 :
1814
+                        $result['added'][] = $uri;
1815
+                        break;
1816
+                    case 2 :
1817
+                        $result['modified'][] = $uri;
1818
+                        break;
1819
+                    case 3 :
1820
+                        $result['deleted'][] = $uri;
1821
+                        break;
1822
+                }
1823
+
1824
+            }
1825
+        } else {
1826
+            // No synctoken supplied, this is the initial sync.
1827
+            $query = "SELECT `uri` FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ? AND `calendartype` = ?";
1828
+            $stmt = $this->db->prepare($query);
1829
+            $stmt->execute([$calendarId, $calendarType]);
1830
+
1831
+            $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN);
1832
+        }
1833
+        return $result;
1834
+
1835
+    }
1836
+
1837
+    /**
1838
+     * Returns a list of subscriptions for a principal.
1839
+     *
1840
+     * Every subscription is an array with the following keys:
1841
+     *  * id, a unique id that will be used by other functions to modify the
1842
+     *    subscription. This can be the same as the uri or a database key.
1843
+     *  * uri. This is just the 'base uri' or 'filename' of the subscription.
1844
+     *  * principaluri. The owner of the subscription. Almost always the same as
1845
+     *    principalUri passed to this method.
1846
+     *
1847
+     * Furthermore, all the subscription info must be returned too:
1848
+     *
1849
+     * 1. {DAV:}displayname
1850
+     * 2. {http://apple.com/ns/ical/}refreshrate
1851
+     * 3. {http://calendarserver.org/ns/}subscribed-strip-todos (omit if todos
1852
+     *    should not be stripped).
1853
+     * 4. {http://calendarserver.org/ns/}subscribed-strip-alarms (omit if alarms
1854
+     *    should not be stripped).
1855
+     * 5. {http://calendarserver.org/ns/}subscribed-strip-attachments (omit if
1856
+     *    attachments should not be stripped).
1857
+     * 6. {http://calendarserver.org/ns/}source (Must be a
1858
+     *     Sabre\DAV\Property\Href).
1859
+     * 7. {http://apple.com/ns/ical/}calendar-color
1860
+     * 8. {http://apple.com/ns/ical/}calendar-order
1861
+     * 9. {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set
1862
+     *    (should just be an instance of
1863
+     *    Sabre\CalDAV\Property\SupportedCalendarComponentSet, with a bunch of
1864
+     *    default components).
1865
+     *
1866
+     * @param string $principalUri
1867
+     * @return array
1868
+     */
1869
+    function getSubscriptionsForUser($principalUri) {
1870
+        $fields = array_values($this->subscriptionPropertyMap);
1871
+        $fields[] = 'id';
1872
+        $fields[] = 'uri';
1873
+        $fields[] = 'source';
1874
+        $fields[] = 'principaluri';
1875
+        $fields[] = 'lastmodified';
1876
+        $fields[] = 'synctoken';
1877
+
1878
+        $query = $this->db->getQueryBuilder();
1879
+        $query->select($fields)
1880
+            ->from('calendarsubscriptions')
1881
+            ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri)))
1882
+            ->orderBy('calendarorder', 'asc');
1883
+        $stmt =$query->execute();
1884
+
1885
+        $subscriptions = [];
1886
+        while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
1887
+
1888
+            $subscription = [
1889
+                'id'           => $row['id'],
1890
+                'uri'          => $row['uri'],
1891
+                'principaluri' => $row['principaluri'],
1892
+                'source'       => $row['source'],
1893
+                'lastmodified' => $row['lastmodified'],
1894
+
1895
+                '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']),
1896
+                '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
1897
+            ];
1898
+
1899
+            foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) {
1900
+                if (!is_null($row[$dbName])) {
1901
+                    $subscription[$xmlName] = $row[$dbName];
1902
+                }
1903
+            }
1904
+
1905
+            $subscriptions[] = $subscription;
1906
+
1907
+        }
1908
+
1909
+        return $subscriptions;
1910
+    }
1911
+
1912
+    /**
1913
+     * Creates a new subscription for a principal.
1914
+     *
1915
+     * If the creation was a success, an id must be returned that can be used to reference
1916
+     * this subscription in other methods, such as updateSubscription.
1917
+     *
1918
+     * @param string $principalUri
1919
+     * @param string $uri
1920
+     * @param array $properties
1921
+     * @return mixed
1922
+     */
1923
+    function createSubscription($principalUri, $uri, array $properties) {
1924
+
1925
+        if (!isset($properties['{http://calendarserver.org/ns/}source'])) {
1926
+            throw new Forbidden('The {http://calendarserver.org/ns/}source property is required when creating subscriptions');
1927
+        }
1928
+
1929
+        $values = [
1930
+            'principaluri' => $principalUri,
1931
+            'uri'          => $uri,
1932
+            'source'       => $properties['{http://calendarserver.org/ns/}source']->getHref(),
1933
+            'lastmodified' => time(),
1934
+        ];
1935
+
1936
+        $propertiesBoolean = ['striptodos', 'stripalarms', 'stripattachments'];
1937
+
1938
+        foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) {
1939
+            if (array_key_exists($xmlName, $properties)) {
1940
+                    $values[$dbName] = $properties[$xmlName];
1941
+                    if (in_array($dbName, $propertiesBoolean)) {
1942
+                        $values[$dbName] = true;
1943
+                }
1944
+            }
1945
+        }
1946
+
1947
+        $valuesToInsert = [];
1948
+
1949
+        $query = $this->db->getQueryBuilder();
1950
+
1951
+        foreach (array_keys($values) as $name) {
1952
+            $valuesToInsert[$name] = $query->createNamedParameter($values[$name]);
1953
+        }
1954
+
1955
+        $query->insert('calendarsubscriptions')
1956
+            ->values($valuesToInsert)
1957
+            ->execute();
1958
+
1959
+        $subscriptionId = $this->db->lastInsertId('*PREFIX*calendarsubscriptions');
1960
+
1961
+        $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::createSubscription', new GenericEvent(
1962
+            '\OCA\DAV\CalDAV\CalDavBackend::createSubscription',
1963
+            [
1964
+                'subscriptionId' => $subscriptionId,
1965
+                'subscriptionData' => $this->getSubscriptionById($subscriptionId),
1966
+            ]));
1967
+
1968
+        return $subscriptionId;
1969
+    }
1970
+
1971
+    /**
1972
+     * Updates a subscription
1973
+     *
1974
+     * The list of mutations is stored in a Sabre\DAV\PropPatch object.
1975
+     * To do the actual updates, you must tell this object which properties
1976
+     * you're going to process with the handle() method.
1977
+     *
1978
+     * Calling the handle method is like telling the PropPatch object "I
1979
+     * promise I can handle updating this property".
1980
+     *
1981
+     * Read the PropPatch documentation for more info and examples.
1982
+     *
1983
+     * @param mixed $subscriptionId
1984
+     * @param PropPatch $propPatch
1985
+     * @return void
1986
+     */
1987
+    function updateSubscription($subscriptionId, PropPatch $propPatch) {
1988
+        $supportedProperties = array_keys($this->subscriptionPropertyMap);
1989
+        $supportedProperties[] = '{http://calendarserver.org/ns/}source';
1990
+
1991
+        /**
1992
+         * @suppress SqlInjectionChecker
1993
+         */
1994
+        $propPatch->handle($supportedProperties, function($mutations) use ($subscriptionId) {
1995
+
1996
+            $newValues = [];
1997
+
1998
+            foreach($mutations as $propertyName=>$propertyValue) {
1999
+                if ($propertyName === '{http://calendarserver.org/ns/}source') {
2000
+                    $newValues['source'] = $propertyValue->getHref();
2001
+                } else {
2002
+                    $fieldName = $this->subscriptionPropertyMap[$propertyName];
2003
+                    $newValues[$fieldName] = $propertyValue;
2004
+                }
2005
+            }
2006
+
2007
+            $query = $this->db->getQueryBuilder();
2008
+            $query->update('calendarsubscriptions')
2009
+                ->set('lastmodified', $query->createNamedParameter(time()));
2010
+            foreach($newValues as $fieldName=>$value) {
2011
+                $query->set($fieldName, $query->createNamedParameter($value));
2012
+            }
2013
+            $query->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId)))
2014
+                ->execute();
2015
+
2016
+            $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateSubscription', new GenericEvent(
2017
+                '\OCA\DAV\CalDAV\CalDavBackend::updateSubscription',
2018
+                [
2019
+                    'subscriptionId' => $subscriptionId,
2020
+                    'subscriptionData' => $this->getSubscriptionById($subscriptionId),
2021
+                    'propertyMutations' => $mutations,
2022
+                ]));
2023
+
2024
+            return true;
2025
+
2026
+        });
2027
+    }
2028
+
2029
+    /**
2030
+     * Deletes a subscription.
2031
+     *
2032
+     * @param mixed $subscriptionId
2033
+     * @return void
2034
+     */
2035
+    function deleteSubscription($subscriptionId) {
2036
+        $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::deleteSubscription', new GenericEvent(
2037
+            '\OCA\DAV\CalDAV\CalDavBackend::deleteSubscription',
2038
+            [
2039
+                'subscriptionId' => $subscriptionId,
2040
+                'subscriptionData' => $this->getSubscriptionById($subscriptionId),
2041
+            ]));
2042
+
2043
+        $query = $this->db->getQueryBuilder();
2044
+        $query->delete('calendarsubscriptions')
2045
+            ->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId)))
2046
+            ->execute();
2047
+
2048
+        $query = $this->db->getQueryBuilder();
2049
+        $query->delete('calendarobjects')
2050
+            ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId)))
2051
+            ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION)))
2052
+            ->execute();
2053
+
2054
+        $query->delete('calendarchanges')
2055
+            ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId)))
2056
+            ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION)))
2057
+            ->execute();
2058
+
2059
+        $query->delete($this->dbObjectPropertiesTable)
2060
+            ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId)))
2061
+            ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION)))
2062
+            ->execute();
2063
+    }
2064
+
2065
+    /**
2066
+     * Returns a single scheduling object for the inbox collection.
2067
+     *
2068
+     * The returned array should contain the following elements:
2069
+     *   * uri - A unique basename for the object. This will be used to
2070
+     *           construct a full uri.
2071
+     *   * calendardata - The iCalendar object
2072
+     *   * lastmodified - The last modification date. Can be an int for a unix
2073
+     *                    timestamp, or a PHP DateTime object.
2074
+     *   * etag - A unique token that must change if the object changed.
2075
+     *   * size - The size of the object, in bytes.
2076
+     *
2077
+     * @param string $principalUri
2078
+     * @param string $objectUri
2079
+     * @return array
2080
+     */
2081
+    function getSchedulingObject($principalUri, $objectUri) {
2082
+        $query = $this->db->getQueryBuilder();
2083
+        $stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size'])
2084
+            ->from('schedulingobjects')
2085
+            ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri)))
2086
+            ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri)))
2087
+            ->execute();
2088
+
2089
+        $row = $stmt->fetch(\PDO::FETCH_ASSOC);
2090
+
2091
+        if(!$row) {
2092
+            return null;
2093
+        }
2094
+
2095
+        return [
2096
+                'uri'          => $row['uri'],
2097
+                'calendardata' => $row['calendardata'],
2098
+                'lastmodified' => $row['lastmodified'],
2099
+                'etag'         => '"' . $row['etag'] . '"',
2100
+                'size'         => (int)$row['size'],
2101
+        ];
2102
+    }
2103
+
2104
+    /**
2105
+     * Returns all scheduling objects for the inbox collection.
2106
+     *
2107
+     * These objects should be returned as an array. Every item in the array
2108
+     * should follow the same structure as returned from getSchedulingObject.
2109
+     *
2110
+     * The main difference is that 'calendardata' is optional.
2111
+     *
2112
+     * @param string $principalUri
2113
+     * @return array
2114
+     */
2115
+    function getSchedulingObjects($principalUri) {
2116
+        $query = $this->db->getQueryBuilder();
2117
+        $stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size'])
2118
+                ->from('schedulingobjects')
2119
+                ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri)))
2120
+                ->execute();
2121
+
2122
+        $result = [];
2123
+        foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) {
2124
+            $result[] = [
2125
+                    'calendardata' => $row['calendardata'],
2126
+                    'uri'          => $row['uri'],
2127
+                    'lastmodified' => $row['lastmodified'],
2128
+                    'etag'         => '"' . $row['etag'] . '"',
2129
+                    'size'         => (int)$row['size'],
2130
+            ];
2131
+        }
2132
+
2133
+        return $result;
2134
+    }
2135
+
2136
+    /**
2137
+     * Deletes a scheduling object from the inbox collection.
2138
+     *
2139
+     * @param string $principalUri
2140
+     * @param string $objectUri
2141
+     * @return void
2142
+     */
2143
+    function deleteSchedulingObject($principalUri, $objectUri) {
2144
+        $query = $this->db->getQueryBuilder();
2145
+        $query->delete('schedulingobjects')
2146
+                ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri)))
2147
+                ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri)))
2148
+                ->execute();
2149
+    }
2150
+
2151
+    /**
2152
+     * Creates a new scheduling object. This should land in a users' inbox.
2153
+     *
2154
+     * @param string $principalUri
2155
+     * @param string $objectUri
2156
+     * @param string $objectData
2157
+     * @return void
2158
+     */
2159
+    function createSchedulingObject($principalUri, $objectUri, $objectData) {
2160
+        $query = $this->db->getQueryBuilder();
2161
+        $query->insert('schedulingobjects')
2162
+            ->values([
2163
+                'principaluri' => $query->createNamedParameter($principalUri),
2164
+                'calendardata' => $query->createNamedParameter($objectData, IQueryBuilder::PARAM_LOB),
2165
+                'uri' => $query->createNamedParameter($objectUri),
2166
+                'lastmodified' => $query->createNamedParameter(time()),
2167
+                'etag' => $query->createNamedParameter(md5($objectData)),
2168
+                'size' => $query->createNamedParameter(strlen($objectData))
2169
+            ])
2170
+            ->execute();
2171
+    }
2172
+
2173
+    /**
2174
+     * Adds a change record to the calendarchanges table.
2175
+     *
2176
+     * @param mixed $calendarId
2177
+     * @param string $objectUri
2178
+     * @param int $operation 1 = add, 2 = modify, 3 = delete.
2179
+     * @param int $calendarType
2180
+     * @return void
2181
+     */
2182
+    protected function addChange($calendarId, $objectUri, $operation, $calendarType=self::CALENDAR_TYPE_CALENDAR) {
2183
+        $table = $calendarType === self::CALENDAR_TYPE_CALENDAR ? 'calendars': 'calendarsubscriptions';
2184
+
2185
+        $query = $this->db->getQueryBuilder();
2186
+        $query->select('synctoken')
2187
+            ->from($table)
2188
+            ->where($query->expr()->eq('id', $query->createNamedParameter($calendarId)));
2189
+        $syncToken = (int)$query->execute()->fetchColumn();
2190
+
2191
+        $query = $this->db->getQueryBuilder();
2192
+        $query->insert('calendarchanges')
2193
+            ->values([
2194
+                'uri' => $query->createNamedParameter($objectUri),
2195
+                'synctoken' => $query->createNamedParameter($syncToken),
2196
+                'calendarid' => $query->createNamedParameter($calendarId),
2197
+                'operation' => $query->createNamedParameter($operation),
2198
+                'calendartype' => $query->createNamedParameter($calendarType),
2199
+            ])
2200
+            ->execute();
2201
+
2202
+        $stmt = $this->db->prepare("UPDATE `*PREFIX*$table` SET `synctoken` = `synctoken` + 1 WHERE `id` = ?");
2203
+        $stmt->execute([
2204
+            $calendarId
2205
+        ]);
2206
+
2207
+    }
2208
+
2209
+    /**
2210
+     * Parses some information from calendar objects, used for optimized
2211
+     * calendar-queries.
2212
+     *
2213
+     * Returns an array with the following keys:
2214
+     *   * etag - An md5 checksum of the object without the quotes.
2215
+     *   * size - Size of the object in bytes
2216
+     *   * componentType - VEVENT, VTODO or VJOURNAL
2217
+     *   * firstOccurence
2218
+     *   * lastOccurence
2219
+     *   * uid - value of the UID property
2220
+     *
2221
+     * @param string $calendarData
2222
+     * @return array
2223
+     */
2224
+    public function getDenormalizedData($calendarData) {
2225
+
2226
+        $vObject = Reader::read($calendarData);
2227
+        $componentType = null;
2228
+        $component = null;
2229
+        $firstOccurrence = null;
2230
+        $lastOccurrence = null;
2231
+        $uid = null;
2232
+        $classification = self::CLASSIFICATION_PUBLIC;
2233
+        foreach($vObject->getComponents() as $component) {
2234
+            if ($component->name!=='VTIMEZONE') {
2235
+                $componentType = $component->name;
2236
+                $uid = (string)$component->UID;
2237
+                break;
2238
+            }
2239
+        }
2240
+        if (!$componentType) {
2241
+            throw new \Sabre\DAV\Exception\BadRequest('Calendar objects must have a VJOURNAL, VEVENT or VTODO component');
2242
+        }
2243
+        if ($componentType === 'VEVENT' && $component->DTSTART) {
2244
+            $firstOccurrence = $component->DTSTART->getDateTime()->getTimeStamp();
2245
+            // Finding the last occurrence is a bit harder
2246
+            if (!isset($component->RRULE)) {
2247
+                if (isset($component->DTEND)) {
2248
+                    $lastOccurrence = $component->DTEND->getDateTime()->getTimeStamp();
2249
+                } elseif (isset($component->DURATION)) {
2250
+                    $endDate = clone $component->DTSTART->getDateTime();
2251
+                    $endDate->add(DateTimeParser::parse($component->DURATION->getValue()));
2252
+                    $lastOccurrence = $endDate->getTimeStamp();
2253
+                } elseif (!$component->DTSTART->hasTime()) {
2254
+                    $endDate = clone $component->DTSTART->getDateTime();
2255
+                    $endDate->modify('+1 day');
2256
+                    $lastOccurrence = $endDate->getTimeStamp();
2257
+                } else {
2258
+                    $lastOccurrence = $firstOccurrence;
2259
+                }
2260
+            } else {
2261
+                $it = new EventIterator($vObject, (string)$component->UID);
2262
+                $maxDate = new DateTime(self::MAX_DATE);
2263
+                if ($it->isInfinite()) {
2264
+                    $lastOccurrence = $maxDate->getTimestamp();
2265
+                } else {
2266
+                    $end = $it->getDtEnd();
2267
+                    while($it->valid() && $end < $maxDate) {
2268
+                        $end = $it->getDtEnd();
2269
+                        $it->next();
2270
+
2271
+                    }
2272
+                    $lastOccurrence = $end->getTimestamp();
2273
+                }
2274
+
2275
+            }
2276
+        }
2277
+
2278
+        if ($component->CLASS) {
2279
+            $classification = CalDavBackend::CLASSIFICATION_PRIVATE;
2280
+            switch ($component->CLASS->getValue()) {
2281
+                case 'PUBLIC':
2282
+                    $classification = CalDavBackend::CLASSIFICATION_PUBLIC;
2283
+                    break;
2284
+                case 'CONFIDENTIAL':
2285
+                    $classification = CalDavBackend::CLASSIFICATION_CONFIDENTIAL;
2286
+                    break;
2287
+            }
2288
+        }
2289
+        return [
2290
+            'etag' => md5($calendarData),
2291
+            'size' => strlen($calendarData),
2292
+            'componentType' => $componentType,
2293
+            'firstOccurence' => is_null($firstOccurrence) ? null : max(0, $firstOccurrence),
2294
+            'lastOccurence'  => $lastOccurrence,
2295
+            'uid' => $uid,
2296
+            'classification' => $classification
2297
+        ];
2298
+
2299
+    }
2300
+
2301
+    /**
2302
+     * @param $cardData
2303
+     * @return bool|string
2304
+     */
2305
+    private function readBlob($cardData) {
2306
+        if (is_resource($cardData)) {
2307
+            return stream_get_contents($cardData);
2308
+        }
2309
+
2310
+        return $cardData;
2311
+    }
2312
+
2313
+    /**
2314
+     * @param IShareable $shareable
2315
+     * @param array $add
2316
+     * @param array $remove
2317
+     */
2318
+    public function updateShares($shareable, $add, $remove) {
2319
+        $calendarId = $shareable->getResourceId();
2320
+        $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateShares', new GenericEvent(
2321
+            '\OCA\DAV\CalDAV\CalDavBackend::updateShares',
2322
+            [
2323
+                'calendarId' => $calendarId,
2324
+                'calendarData' => $this->getCalendarById($calendarId),
2325
+                'shares' => $this->getShares($calendarId),
2326
+                'add' => $add,
2327
+                'remove' => $remove,
2328
+            ]));
2329
+        $this->calendarSharingBackend->updateShares($shareable, $add, $remove);
2330
+    }
2331
+
2332
+    /**
2333
+     * @param int $resourceId
2334
+     * @param int $calendarType
2335
+     * @return array
2336
+     */
2337
+    public function getShares($resourceId, $calendarType=self::CALENDAR_TYPE_CALENDAR) {
2338
+        return $this->calendarSharingBackend->getShares($resourceId);
2339
+    }
2340
+
2341
+    /**
2342
+     * @param boolean $value
2343
+     * @param \OCA\DAV\CalDAV\Calendar $calendar
2344
+     * @return string|null
2345
+     */
2346
+    public function setPublishStatus($value, $calendar) {
2347
+
2348
+        $calendarId = $calendar->getResourceId();
2349
+        $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::publishCalendar', new GenericEvent(
2350
+            '\OCA\DAV\CalDAV\CalDavBackend::updateShares',
2351
+            [
2352
+                'calendarId' => $calendarId,
2353
+                'calendarData' => $this->getCalendarById($calendarId),
2354
+                'public' => $value,
2355
+            ]));
2356
+
2357
+        $query = $this->db->getQueryBuilder();
2358
+        if ($value) {
2359
+            $publicUri = $this->random->generate(16, ISecureRandom::CHAR_HUMAN_READABLE);
2360
+            $query->insert('dav_shares')
2361
+                ->values([
2362
+                    'principaluri' => $query->createNamedParameter($calendar->getPrincipalURI()),
2363
+                    'type' => $query->createNamedParameter('calendar'),
2364
+                    'access' => $query->createNamedParameter(self::ACCESS_PUBLIC),
2365
+                    'resourceid' => $query->createNamedParameter($calendar->getResourceId()),
2366
+                    'publicuri' => $query->createNamedParameter($publicUri)
2367
+                ]);
2368
+            $query->execute();
2369
+            return $publicUri;
2370
+        }
2371
+        $query->delete('dav_shares')
2372
+            ->where($query->expr()->eq('resourceid', $query->createNamedParameter($calendar->getResourceId())))
2373
+            ->andWhere($query->expr()->eq('access', $query->createNamedParameter(self::ACCESS_PUBLIC)));
2374
+        $query->execute();
2375
+        return null;
2376
+    }
2377
+
2378
+    /**
2379
+     * @param \OCA\DAV\CalDAV\Calendar $calendar
2380
+     * @return mixed
2381
+     */
2382
+    public function getPublishStatus($calendar) {
2383
+        $query = $this->db->getQueryBuilder();
2384
+        $result = $query->select('publicuri')
2385
+            ->from('dav_shares')
2386
+            ->where($query->expr()->eq('resourceid', $query->createNamedParameter($calendar->getResourceId())))
2387
+            ->andWhere($query->expr()->eq('access', $query->createNamedParameter(self::ACCESS_PUBLIC)))
2388
+            ->execute();
2389
+
2390
+        $row = $result->fetch();
2391
+        $result->closeCursor();
2392
+        return $row ? reset($row) : false;
2393
+    }
2394
+
2395
+    /**
2396
+     * @param int $resourceId
2397
+     * @param array $acl
2398
+     * @return array
2399
+     */
2400
+    public function applyShareAcl($resourceId, $acl) {
2401
+        return $this->calendarSharingBackend->applyShareAcl($resourceId, $acl);
2402
+    }
2403
+
2404
+
2405
+
2406
+    /**
2407
+     * update properties table
2408
+     *
2409
+     * @param int $calendarId
2410
+     * @param string $objectUri
2411
+     * @param string $calendarData
2412
+     * @param int $calendarType
2413
+     */
2414
+    public function updateProperties($calendarId, $objectUri, $calendarData, $calendarType=self::CALENDAR_TYPE_CALENDAR) {
2415
+        $objectId = $this->getCalendarObjectId($calendarId, $objectUri, $calendarType);
2416
+
2417
+        try {
2418
+            $vCalendar = $this->readCalendarData($calendarData);
2419
+        } catch (\Exception $ex) {
2420
+            return;
2421
+        }
2422
+
2423
+        $this->purgeProperties($calendarId, $objectId);
2424
+
2425
+        $query = $this->db->getQueryBuilder();
2426
+        $query->insert($this->dbObjectPropertiesTable)
2427
+            ->values(
2428
+                [
2429
+                    'calendarid' => $query->createNamedParameter($calendarId),
2430
+                    'calendartype' => $query->createNamedParameter($calendarType),
2431
+                    'objectid' => $query->createNamedParameter($objectId),
2432
+                    'name' => $query->createParameter('name'),
2433
+                    'parameter' => $query->createParameter('parameter'),
2434
+                    'value' => $query->createParameter('value'),
2435
+                ]
2436
+            );
2437
+
2438
+        $indexComponents = ['VEVENT', 'VJOURNAL', 'VTODO'];
2439
+        foreach ($vCalendar->getComponents() as $component) {
2440
+            if (!in_array($component->name, $indexComponents)) {
2441
+                continue;
2442
+            }
2443
+
2444
+            foreach ($component->children() as $property) {
2445
+                if (in_array($property->name, self::$indexProperties)) {
2446
+                    $value = $property->getValue();
2447
+                    // is this a shitty db?
2448
+                    if (!$this->db->supports4ByteText()) {
2449
+                        $value = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $value);
2450
+                    }
2451
+                    $value = mb_substr($value, 0, 254);
2452
+
2453
+                    $query->setParameter('name', $property->name);
2454
+                    $query->setParameter('parameter', null);
2455
+                    $query->setParameter('value', $value);
2456
+                    $query->execute();
2457
+                }
2458
+
2459
+                if (array_key_exists($property->name, self::$indexParameters)) {
2460
+                    $parameters = $property->parameters();
2461
+                    $indexedParametersForProperty = self::$indexParameters[$property->name];
2462
+
2463
+                    foreach ($parameters as $key => $value) {
2464
+                        if (in_array($key, $indexedParametersForProperty)) {
2465
+                            // is this a shitty db?
2466
+                            if ($this->db->supports4ByteText()) {
2467
+                                $value = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $value);
2468
+                            }
2469
+
2470
+                            $query->setParameter('name', $property->name);
2471
+                            $query->setParameter('parameter', mb_substr($key, 0, 254));
2472
+                            $query->setParameter('value', mb_substr($value, 0, 254));
2473
+                            $query->execute();
2474
+                        }
2475
+                    }
2476
+                }
2477
+            }
2478
+        }
2479
+    }
2480
+
2481
+    /**
2482
+     * deletes all birthday calendars
2483
+     */
2484
+    public function deleteAllBirthdayCalendars() {
2485
+        $query = $this->db->getQueryBuilder();
2486
+        $result = $query->select(['id'])->from('calendars')
2487
+            ->where($query->expr()->eq('uri', $query->createNamedParameter(BirthdayService::BIRTHDAY_CALENDAR_URI)))
2488
+            ->execute();
2489
+
2490
+        $ids = $result->fetchAll();
2491
+        foreach($ids as $id) {
2492
+            $this->deleteCalendar($id['id']);
2493
+        }
2494
+    }
2495
+
2496
+    /**
2497
+     * @param $subscriptionId
2498
+     */
2499
+    public function purgeAllCachedEventsForSubscription($subscriptionId) {
2500
+        $query = $this->db->getQueryBuilder();
2501
+        $query->select('uri')
2502
+            ->from('calendarobjects')
2503
+            ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId)))
2504
+            ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION)));
2505
+        $stmt = $query->execute();
2506
+
2507
+        $uris = [];
2508
+        foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) {
2509
+            $uris[] = $row['uri'];
2510
+        }
2511
+        $stmt->closeCursor();
2512
+
2513
+        $query = $this->db->getQueryBuilder();
2514
+        $query->delete('calendarobjects')
2515
+            ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId)))
2516
+            ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION)))
2517
+            ->execute();
2518
+
2519
+        $query->delete('calendarchanges')
2520
+            ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId)))
2521
+            ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION)))
2522
+            ->execute();
2523
+
2524
+        $query->delete($this->dbObjectPropertiesTable)
2525
+            ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId)))
2526
+            ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION)))
2527
+            ->execute();
2528
+
2529
+        foreach($uris as $uri) {
2530
+            $this->addChange($subscriptionId, $uri, 3, self::CALENDAR_TYPE_SUBSCRIPTION);
2531
+        }
2532
+    }
2533
+
2534
+    /**
2535
+     * Move a calendar from one user to another
2536
+     *
2537
+     * @param string $uriName
2538
+     * @param string $uriOrigin
2539
+     * @param string $uriDestination
2540
+     */
2541
+    public function moveCalendar($uriName, $uriOrigin, $uriDestination)
2542
+    {
2543
+        $query = $this->db->getQueryBuilder();
2544
+        $query->update('calendars')
2545
+            ->set('principaluri', $query->createNamedParameter($uriDestination))
2546
+            ->where($query->expr()->eq('principaluri', $query->createNamedParameter($uriOrigin)))
2547
+            ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($uriName)))
2548
+            ->execute();
2549
+    }
2550
+
2551
+    /**
2552
+     * read VCalendar data into a VCalendar object
2553
+     *
2554
+     * @param string $objectData
2555
+     * @return VCalendar
2556
+     */
2557
+    protected function readCalendarData($objectData) {
2558
+        return Reader::read($objectData);
2559
+    }
2560
+
2561
+    /**
2562
+     * delete all properties from a given calendar object
2563
+     *
2564
+     * @param int $calendarId
2565
+     * @param int $objectId
2566
+     */
2567
+    protected function purgeProperties($calendarId, $objectId) {
2568
+        $query = $this->db->getQueryBuilder();
2569
+        $query->delete($this->dbObjectPropertiesTable)
2570
+            ->where($query->expr()->eq('objectid', $query->createNamedParameter($objectId)))
2571
+            ->andWhere($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId)));
2572
+        $query->execute();
2573
+    }
2574
+
2575
+    /**
2576
+     * get ID from a given calendar object
2577
+     *
2578
+     * @param int $calendarId
2579
+     * @param string $uri
2580
+     * @param int $calendarType
2581
+     * @return int
2582
+     */
2583
+    protected function getCalendarObjectId($calendarId, $uri, $calendarType):int {
2584
+        $query = $this->db->getQueryBuilder();
2585
+        $query->select('id')
2586
+            ->from('calendarobjects')
2587
+            ->where($query->expr()->eq('uri', $query->createNamedParameter($uri)))
2588
+            ->andWhere($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId)))
2589
+            ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType)));
2590
+
2591
+        $result = $query->execute();
2592
+        $objectIds = $result->fetch();
2593
+        $result->closeCursor();
2594
+
2595
+        if (!isset($objectIds['id'])) {
2596
+            throw new \InvalidArgumentException('Calendarobject does not exists: ' . $uri);
2597
+        }
2598
+
2599
+        return (int)$objectIds['id'];
2600
+    }
2601
+
2602
+    /**
2603
+     * return legacy endpoint principal name to new principal name
2604
+     *
2605
+     * @param $principalUri
2606
+     * @param $toV2
2607
+     * @return string
2608
+     */
2609
+    private function convertPrincipal($principalUri, $toV2) {
2610
+        if ($this->principalBackend->getPrincipalPrefix() === 'principals') {
2611
+            list(, $name) = Uri\split($principalUri);
2612
+            if ($toV2 === true) {
2613
+                return "principals/users/$name";
2614
+            }
2615
+            return "principals/$name";
2616
+        }
2617
+        return $principalUri;
2618
+    }
2619
+
2620
+    /**
2621
+     * adds information about an owner to the calendar data
2622
+     *
2623
+     * @param $calendarInfo
2624
+     */
2625
+    private function addOwnerPrincipal(&$calendarInfo) {
2626
+        $ownerPrincipalKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal';
2627
+        $displaynameKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname';
2628
+        if (isset($calendarInfo[$ownerPrincipalKey])) {
2629
+            $uri = $calendarInfo[$ownerPrincipalKey];
2630
+        } else {
2631
+            $uri = $calendarInfo['principaluri'];
2632
+        }
2633
+
2634
+        $principalInformation = $this->principalBackend->getPrincipalByPath($uri);
2635
+        if (isset($principalInformation['{DAV:}displayname'])) {
2636
+            $calendarInfo[$displaynameKey] = $principalInformation['{DAV:}displayname'];
2637
+        }
2638
+    }
2639 2639
 }
Please login to merge, or discard this patch.
apps/dav/lib/CalDAV/Schedule/IMipPlugin.php 1 patch
Indentation   +537 added lines, -537 removed lines patch added patch discarded remove patch
@@ -67,205 +67,205 @@  discard block
 block discarded – undo
67 67
  */
68 68
 class IMipPlugin extends SabreIMipPlugin {
69 69
 
70
-	/** @var string */
71
-	private $userId;
72
-
73
-	/** @var IConfig */
74
-	private $config;
75
-
76
-	/** @var IMailer */
77
-	private $mailer;
78
-
79
-	/** @var ILogger */
80
-	private $logger;
81
-
82
-	/** @var ITimeFactory */
83
-	private $timeFactory;
84
-
85
-	/** @var L10NFactory */
86
-	private $l10nFactory;
87
-
88
-	/** @var IURLGenerator */
89
-	private $urlGenerator;
90
-
91
-	/** @var ISecureRandom */
92
-	private $random;
93
-
94
-	/** @var IDBConnection */
95
-	private $db;
96
-
97
-	/** @var Defaults */
98
-	private $defaults;
99
-
100
-	/** @var IUserManager */
101
-	private $userManager;
102
-
103
-	const MAX_DATE = '2038-01-01';
104
-
105
-	const METHOD_REQUEST = 'request';
106
-	const METHOD_REPLY = 'reply';
107
-	const METHOD_CANCEL = 'cancel';
108
-
109
-	/**
110
-	 * @param IConfig $config
111
-	 * @param IMailer $mailer
112
-	 * @param ILogger $logger
113
-	 * @param ITimeFactory $timeFactory
114
-	 * @param L10NFactory $l10nFactory
115
-	 * @param IUrlGenerator $urlGenerator
116
-	 * @param Defaults $defaults
117
-	 * @param ISecureRandom $random
118
-	 * @param IDBConnection $db
119
-	 * @param string $userId
120
-	 */
121
-	public function __construct(IConfig $config, IMailer $mailer, ILogger $logger,
122
-								ITimeFactory $timeFactory, L10NFactory $l10nFactory,
123
-								IURLGenerator $urlGenerator, Defaults $defaults,
124
-								ISecureRandom $random, IDBConnection $db, IUserManager $userManager,
125
-								$userId) {
126
-		parent::__construct('');
127
-		$this->userId = $userId;
128
-		$this->config = $config;
129
-		$this->mailer = $mailer;
130
-		$this->logger = $logger;
131
-		$this->timeFactory = $timeFactory;
132
-		$this->l10nFactory = $l10nFactory;
133
-		$this->urlGenerator = $urlGenerator;
134
-		$this->random = $random;
135
-		$this->db = $db;
136
-		$this->defaults = $defaults;
137
-		$this->userManager = $userManager;
138
-	}
139
-
140
-	/**
141
-	 * Event handler for the 'schedule' event.
142
-	 *
143
-	 * @param Message $iTipMessage
144
-	 * @return void
145
-	 */
146
-	public function schedule(Message $iTipMessage) {
147
-
148
-		// Not sending any emails if the system considers the update
149
-		// insignificant.
150
-		if (!$iTipMessage->significantChange) {
151
-			if (!$iTipMessage->scheduleStatus) {
152
-				$iTipMessage->scheduleStatus = '1.0;We got the message, but it\'s not significant enough to warrant an email';
153
-			}
154
-			return;
155
-		}
156
-
157
-		$summary = $iTipMessage->message->VEVENT->SUMMARY;
158
-
159
-		if (parse_url($iTipMessage->sender, PHP_URL_SCHEME) !== 'mailto') {
160
-			return;
161
-		}
162
-
163
-		if (parse_url($iTipMessage->recipient, PHP_URL_SCHEME) !== 'mailto') {
164
-			return;
165
-		}
166
-
167
-		// don't send out mails for events that already took place
168
-		$lastOccurrence = $this->getLastOccurrence($iTipMessage->message);
169
-		$currentTime = $this->timeFactory->getTime();
170
-		if ($lastOccurrence < $currentTime) {
171
-			return;
172
-		}
173
-
174
-		// Strip off mailto:
175
-		$sender = substr($iTipMessage->sender, 7);
176
-		$recipient = substr($iTipMessage->recipient, 7);
177
-
178
-		$senderName = $iTipMessage->senderName ?: null;
179
-		$recipientName = $iTipMessage->recipientName ?: null;
180
-
181
-		if ($senderName === null || empty(trim($senderName))) {
182
-			$user = $this->userManager->get($this->userId);
183
-			if ($user) {
184
-				// getDisplayName automatically uses the uid
185
-				// if no display-name is set
186
-				$senderName = $user->getDisplayName();
187
-			}
188
-		}
189
-
190
-		/** @var VEvent $vevent */
191
-		$vevent = $iTipMessage->message->VEVENT;
192
-
193
-		$attendee = $this->getCurrentAttendee($iTipMessage);
194
-		$defaultLang = $this->l10nFactory->findLanguage();
195
-		$lang = $this->getAttendeeLangOrDefault($defaultLang, $attendee);
196
-		$l10n = $this->l10nFactory->get('dav', $lang);
197
-
198
-		$meetingAttendeeName = $recipientName ?: $recipient;
199
-		$meetingInviteeName = $senderName ?: $sender;
200
-
201
-		$meetingTitle = $vevent->SUMMARY;
202
-		$meetingDescription = $vevent->DESCRIPTION;
203
-
204
-		$start = $vevent->DTSTART;
205
-		if (isset($vevent->DTEND)) {
206
-			$end = $vevent->DTEND;
207
-		} elseif (isset($vevent->DURATION)) {
208
-			$isFloating = $vevent->DTSTART->isFloating();
209
-			$end = clone $vevent->DTSTART;
210
-			$endDateTime = $end->getDateTime();
211
-			$endDateTime = $endDateTime->add(DateTimeParser::parse($vevent->DURATION->getValue()));
212
-			$end->setDateTime($endDateTime, $isFloating);
213
-		} elseif (!$vevent->DTSTART->hasTime()) {
214
-			$isFloating = $vevent->DTSTART->isFloating();
215
-			$end = clone $vevent->DTSTART;
216
-			$endDateTime = $end->getDateTime();
217
-			$endDateTime = $endDateTime->modify('+1 day');
218
-			$end->setDateTime($endDateTime, $isFloating);
219
-		} else {
220
-			$end = clone $vevent->DTSTART;
221
-		}
222
-
223
-		$meetingWhen = $this->generateWhenString($l10n, $start, $end);
224
-
225
-		$meetingUrl = $vevent->URL;
226
-		$meetingLocation = $vevent->LOCATION;
227
-
228
-		$defaultVal = '--';
229
-
230
-		$method = self::METHOD_REQUEST;
231
-		switch (strtolower($iTipMessage->method)) {
232
-			case self::METHOD_REPLY:
233
-				$method = self::METHOD_REPLY;
234
-				break;
235
-			case self::METHOD_CANCEL:
236
-				$method = self::METHOD_CANCEL;
237
-				break;
238
-		}
239
-
240
-		$data = [
241
-			'attendee_name' => (string)$meetingAttendeeName ?: $defaultVal,
242
-			'invitee_name' => (string)$meetingInviteeName ?: $defaultVal,
243
-			'meeting_title' => (string)$meetingTitle ?: $defaultVal,
244
-			'meeting_description' => (string)$meetingDescription ?: $defaultVal,
245
-			'meeting_url' => (string)$meetingUrl ?: $defaultVal,
246
-		];
247
-
248
-		$fromEMail = \OCP\Util::getDefaultEmailAddress('invitations-noreply');
249
-		$fromName = $l10n->t('%1$s via %2$s', [$senderName, $this->defaults->getName()]);
250
-
251
-		$message = $this->mailer->createMessage()
252
-			->setFrom([$fromEMail => $fromName])
253
-			->setReplyTo([$sender => $senderName])
254
-			->setTo([$recipient => $recipientName]);
255
-
256
-		$template = $this->mailer->createEMailTemplate('dav.calendarInvite.' . $method, $data);
257
-		$template->addHeader();
258
-
259
-		$this->addSubjectAndHeading($template, $l10n, $method, $summary,
260
-			$meetingAttendeeName, $meetingInviteeName);
261
-		$this->addBulletList($template, $l10n, $meetingWhen, $meetingLocation,
262
-			$meetingDescription, $meetingUrl);
263
-
264
-
265
-		// Only add response buttons to invitation requests: Fix Issue #11230
266
-		if (($method == self::METHOD_REQUEST) && $this->getAttendeeRSVP($attendee)) {
267
-
268
-			/*
70
+    /** @var string */
71
+    private $userId;
72
+
73
+    /** @var IConfig */
74
+    private $config;
75
+
76
+    /** @var IMailer */
77
+    private $mailer;
78
+
79
+    /** @var ILogger */
80
+    private $logger;
81
+
82
+    /** @var ITimeFactory */
83
+    private $timeFactory;
84
+
85
+    /** @var L10NFactory */
86
+    private $l10nFactory;
87
+
88
+    /** @var IURLGenerator */
89
+    private $urlGenerator;
90
+
91
+    /** @var ISecureRandom */
92
+    private $random;
93
+
94
+    /** @var IDBConnection */
95
+    private $db;
96
+
97
+    /** @var Defaults */
98
+    private $defaults;
99
+
100
+    /** @var IUserManager */
101
+    private $userManager;
102
+
103
+    const MAX_DATE = '2038-01-01';
104
+
105
+    const METHOD_REQUEST = 'request';
106
+    const METHOD_REPLY = 'reply';
107
+    const METHOD_CANCEL = 'cancel';
108
+
109
+    /**
110
+     * @param IConfig $config
111
+     * @param IMailer $mailer
112
+     * @param ILogger $logger
113
+     * @param ITimeFactory $timeFactory
114
+     * @param L10NFactory $l10nFactory
115
+     * @param IUrlGenerator $urlGenerator
116
+     * @param Defaults $defaults
117
+     * @param ISecureRandom $random
118
+     * @param IDBConnection $db
119
+     * @param string $userId
120
+     */
121
+    public function __construct(IConfig $config, IMailer $mailer, ILogger $logger,
122
+                                ITimeFactory $timeFactory, L10NFactory $l10nFactory,
123
+                                IURLGenerator $urlGenerator, Defaults $defaults,
124
+                                ISecureRandom $random, IDBConnection $db, IUserManager $userManager,
125
+                                $userId) {
126
+        parent::__construct('');
127
+        $this->userId = $userId;
128
+        $this->config = $config;
129
+        $this->mailer = $mailer;
130
+        $this->logger = $logger;
131
+        $this->timeFactory = $timeFactory;
132
+        $this->l10nFactory = $l10nFactory;
133
+        $this->urlGenerator = $urlGenerator;
134
+        $this->random = $random;
135
+        $this->db = $db;
136
+        $this->defaults = $defaults;
137
+        $this->userManager = $userManager;
138
+    }
139
+
140
+    /**
141
+     * Event handler for the 'schedule' event.
142
+     *
143
+     * @param Message $iTipMessage
144
+     * @return void
145
+     */
146
+    public function schedule(Message $iTipMessage) {
147
+
148
+        // Not sending any emails if the system considers the update
149
+        // insignificant.
150
+        if (!$iTipMessage->significantChange) {
151
+            if (!$iTipMessage->scheduleStatus) {
152
+                $iTipMessage->scheduleStatus = '1.0;We got the message, but it\'s not significant enough to warrant an email';
153
+            }
154
+            return;
155
+        }
156
+
157
+        $summary = $iTipMessage->message->VEVENT->SUMMARY;
158
+
159
+        if (parse_url($iTipMessage->sender, PHP_URL_SCHEME) !== 'mailto') {
160
+            return;
161
+        }
162
+
163
+        if (parse_url($iTipMessage->recipient, PHP_URL_SCHEME) !== 'mailto') {
164
+            return;
165
+        }
166
+
167
+        // don't send out mails for events that already took place
168
+        $lastOccurrence = $this->getLastOccurrence($iTipMessage->message);
169
+        $currentTime = $this->timeFactory->getTime();
170
+        if ($lastOccurrence < $currentTime) {
171
+            return;
172
+        }
173
+
174
+        // Strip off mailto:
175
+        $sender = substr($iTipMessage->sender, 7);
176
+        $recipient = substr($iTipMessage->recipient, 7);
177
+
178
+        $senderName = $iTipMessage->senderName ?: null;
179
+        $recipientName = $iTipMessage->recipientName ?: null;
180
+
181
+        if ($senderName === null || empty(trim($senderName))) {
182
+            $user = $this->userManager->get($this->userId);
183
+            if ($user) {
184
+                // getDisplayName automatically uses the uid
185
+                // if no display-name is set
186
+                $senderName = $user->getDisplayName();
187
+            }
188
+        }
189
+
190
+        /** @var VEvent $vevent */
191
+        $vevent = $iTipMessage->message->VEVENT;
192
+
193
+        $attendee = $this->getCurrentAttendee($iTipMessage);
194
+        $defaultLang = $this->l10nFactory->findLanguage();
195
+        $lang = $this->getAttendeeLangOrDefault($defaultLang, $attendee);
196
+        $l10n = $this->l10nFactory->get('dav', $lang);
197
+
198
+        $meetingAttendeeName = $recipientName ?: $recipient;
199
+        $meetingInviteeName = $senderName ?: $sender;
200
+
201
+        $meetingTitle = $vevent->SUMMARY;
202
+        $meetingDescription = $vevent->DESCRIPTION;
203
+
204
+        $start = $vevent->DTSTART;
205
+        if (isset($vevent->DTEND)) {
206
+            $end = $vevent->DTEND;
207
+        } elseif (isset($vevent->DURATION)) {
208
+            $isFloating = $vevent->DTSTART->isFloating();
209
+            $end = clone $vevent->DTSTART;
210
+            $endDateTime = $end->getDateTime();
211
+            $endDateTime = $endDateTime->add(DateTimeParser::parse($vevent->DURATION->getValue()));
212
+            $end->setDateTime($endDateTime, $isFloating);
213
+        } elseif (!$vevent->DTSTART->hasTime()) {
214
+            $isFloating = $vevent->DTSTART->isFloating();
215
+            $end = clone $vevent->DTSTART;
216
+            $endDateTime = $end->getDateTime();
217
+            $endDateTime = $endDateTime->modify('+1 day');
218
+            $end->setDateTime($endDateTime, $isFloating);
219
+        } else {
220
+            $end = clone $vevent->DTSTART;
221
+        }
222
+
223
+        $meetingWhen = $this->generateWhenString($l10n, $start, $end);
224
+
225
+        $meetingUrl = $vevent->URL;
226
+        $meetingLocation = $vevent->LOCATION;
227
+
228
+        $defaultVal = '--';
229
+
230
+        $method = self::METHOD_REQUEST;
231
+        switch (strtolower($iTipMessage->method)) {
232
+            case self::METHOD_REPLY:
233
+                $method = self::METHOD_REPLY;
234
+                break;
235
+            case self::METHOD_CANCEL:
236
+                $method = self::METHOD_CANCEL;
237
+                break;
238
+        }
239
+
240
+        $data = [
241
+            'attendee_name' => (string)$meetingAttendeeName ?: $defaultVal,
242
+            'invitee_name' => (string)$meetingInviteeName ?: $defaultVal,
243
+            'meeting_title' => (string)$meetingTitle ?: $defaultVal,
244
+            'meeting_description' => (string)$meetingDescription ?: $defaultVal,
245
+            'meeting_url' => (string)$meetingUrl ?: $defaultVal,
246
+        ];
247
+
248
+        $fromEMail = \OCP\Util::getDefaultEmailAddress('invitations-noreply');
249
+        $fromName = $l10n->t('%1$s via %2$s', [$senderName, $this->defaults->getName()]);
250
+
251
+        $message = $this->mailer->createMessage()
252
+            ->setFrom([$fromEMail => $fromName])
253
+            ->setReplyTo([$sender => $senderName])
254
+            ->setTo([$recipient => $recipientName]);
255
+
256
+        $template = $this->mailer->createEMailTemplate('dav.calendarInvite.' . $method, $data);
257
+        $template->addHeader();
258
+
259
+        $this->addSubjectAndHeading($template, $l10n, $method, $summary,
260
+            $meetingAttendeeName, $meetingInviteeName);
261
+        $this->addBulletList($template, $l10n, $meetingWhen, $meetingLocation,
262
+            $meetingDescription, $meetingUrl);
263
+
264
+
265
+        // Only add response buttons to invitation requests: Fix Issue #11230
266
+        if (($method == self::METHOD_REQUEST) && $this->getAttendeeRSVP($attendee)) {
267
+
268
+            /*
269 269
 			** Only offer invitation accept/reject buttons, which link back to the
270 270
 			** nextcloud server, to recipients who can access the nextcloud server via
271 271
 			** their internet/intranet.  Issue #12156
@@ -284,342 +284,342 @@  discard block
 block discarded – undo
284 284
 			** To suppress URLs entirely, set invitation_link_recipients to boolean "no".
285 285
 			*/
286 286
 
287
-			$recipientDomain = substr(strrchr($recipient, "@"), 1);
288
-			$invitationLinkRecipients = explode(',', preg_replace('/\s+/', '', strtolower($this->config->getAppValue('dav', 'invitation_link_recipients', 'yes'))));
289
-
290
-			if (strcmp('yes', $invitationLinkRecipients[0]) === 0
291
-				 || in_array(strtolower($recipient), $invitationLinkRecipients)
292
-				 || in_array(strtolower($recipientDomain), $invitationLinkRecipients)) {
293
-				$this->addResponseButtons($template, $l10n, $iTipMessage, $lastOccurrence);
294
-			}
295
-		}
296
-
297
-		$template->addFooter();
298
-
299
-		$message->useTemplate($template);
300
-
301
-		$attachment = $this->mailer->createAttachment(
302
-			$iTipMessage->message->serialize(),
303
-			'event.ics',// TODO(leon): Make file name unique, e.g. add event id
304
-			'text/calendar; method=' . $iTipMessage->method
305
-		);
306
-		$message->attach($attachment);
307
-
308
-		try {
309
-			$failed = $this->mailer->send($message);
310
-			$iTipMessage->scheduleStatus = '1.1; Scheduling message is sent via iMip';
311
-			if ($failed) {
312
-				$this->logger->error('Unable to deliver message to {failed}', ['app' => 'dav', 'failed' =>  implode(', ', $failed)]);
313
-				$iTipMessage->scheduleStatus = '5.0; EMail delivery failed';
314
-			}
315
-		} catch(\Exception $ex) {
316
-			$this->logger->logException($ex, ['app' => 'dav']);
317
-			$iTipMessage->scheduleStatus = '5.0; EMail delivery failed';
318
-		}
319
-	}
320
-
321
-	/**
322
-	 * check if event took place in the past already
323
-	 * @param VCalendar $vObject
324
-	 * @return int
325
-	 */
326
-	private function getLastOccurrence(VCalendar $vObject) {
327
-		/** @var VEvent $component */
328
-		$component = $vObject->VEVENT;
329
-
330
-		$firstOccurrence = $component->DTSTART->getDateTime()->getTimeStamp();
331
-		// Finding the last occurrence is a bit harder
332
-		if (!isset($component->RRULE)) {
333
-			if (isset($component->DTEND)) {
334
-				$lastOccurrence = $component->DTEND->getDateTime()->getTimeStamp();
335
-			} elseif (isset($component->DURATION)) {
336
-				/** @var \DateTime $endDate */
337
-				$endDate = clone $component->DTSTART->getDateTime();
338
-				// $component->DTEND->getDateTime() returns DateTimeImmutable
339
-				$endDate = $endDate->add(DateTimeParser::parse($component->DURATION->getValue()));
340
-				$lastOccurrence = $endDate->getTimestamp();
341
-			} elseif (!$component->DTSTART->hasTime()) {
342
-				/** @var \DateTime $endDate */
343
-				$endDate = clone $component->DTSTART->getDateTime();
344
-				// $component->DTSTART->getDateTime() returns DateTimeImmutable
345
-				$endDate = $endDate->modify('+1 day');
346
-				$lastOccurrence = $endDate->getTimestamp();
347
-			} else {
348
-				$lastOccurrence = $firstOccurrence;
349
-			}
350
-		} else {
351
-			$it = new EventIterator($vObject, (string)$component->UID);
352
-			$maxDate = new \DateTime(self::MAX_DATE);
353
-			if ($it->isInfinite()) {
354
-				$lastOccurrence = $maxDate->getTimestamp();
355
-			} else {
356
-				$end = $it->getDtEnd();
357
-				while($it->valid() && $end < $maxDate) {
358
-					$end = $it->getDtEnd();
359
-					$it->next();
360
-
361
-				}
362
-				$lastOccurrence = $end->getTimestamp();
363
-			}
364
-		}
365
-
366
-		return $lastOccurrence;
367
-	}
368
-
369
-
370
-	/**
371
-	 * @param Message $iTipMessage
372
-	 * @return null|Property
373
-	 */
374
-	private function getCurrentAttendee(Message $iTipMessage) {
375
-		/** @var VEvent $vevent */
376
-		$vevent = $iTipMessage->message->VEVENT;
377
-		$attendees = $vevent->select('ATTENDEE');
378
-		foreach ($attendees as $attendee) {
379
-			/** @var Property $attendee */
380
-			if (strcasecmp($attendee->getValue(), $iTipMessage->recipient) === 0) {
381
-				return $attendee;
382
-			}
383
-		}
384
-		return null;
385
-	}
386
-
387
-	/**
388
-	 * @param string $default
389
-	 * @param Property|null $attendee
390
-	 * @return string
391
-	 */
392
-	private function getAttendeeLangOrDefault($default, Property $attendee = null) {
393
-		if ($attendee !== null) {
394
-			$lang = $attendee->offsetGet('LANGUAGE');
395
-			if ($lang instanceof Parameter) {
396
-				return $lang->getValue();
397
-			}
398
-		}
399
-		return $default;
400
-	}
401
-
402
-	/**
403
-	 * @param Property|null $attendee
404
-	 * @return bool
405
-	 */
406
-	private function getAttendeeRSVP(Property $attendee = null) {
407
-		if ($attendee !== null) {
408
-			$rsvp = $attendee->offsetGet('RSVP');
409
-			if (($rsvp instanceof Parameter) && (strcasecmp($rsvp->getValue(), 'TRUE') === 0)) {
410
-				return true;
411
-			}
412
-		}
413
-		// RFC 5545 3.2.17: default RSVP is false
414
-		return false;
415
-	}
416
-
417
-	/**
418
-	 * @param IL10N $l10n
419
-	 * @param Property $dtstart
420
-	 * @param Property $dtend
421
-	 */
422
-	private function generateWhenString(IL10N $l10n, Property $dtstart, Property $dtend) {
423
-		$isAllDay = $dtstart instanceof Property\ICalendar\Date;
424
-
425
-		/** @var Property\ICalendar\Date | Property\ICalendar\DateTime $dtstart */
426
-		/** @var Property\ICalendar\Date | Property\ICalendar\DateTime $dtend */
427
-		/** @var \DateTimeImmutable $dtstartDt */
428
-		$dtstartDt = $dtstart->getDateTime();
429
-		/** @var \DateTimeImmutable $dtendDt */
430
-		$dtendDt = $dtend->getDateTime();
431
-
432
-		$diff = $dtstartDt->diff($dtendDt);
433
-
434
-		$dtstartDt = new \DateTime($dtstartDt->format(\DateTime::ATOM));
435
-		$dtendDt = new \DateTime($dtendDt->format(\DateTime::ATOM));
436
-
437
-		if ($isAllDay) {
438
-			// One day event
439
-			if ($diff->days === 1) {
440
-				return $l10n->l('date', $dtstartDt, ['width' => 'medium']);
441
-			}
442
-
443
-			// DTEND is exclusive, so if the ics data says 2020-01-01 to 2020-01-05,
444
-			// the email should show 2020-01-01 to 2020-01-04.
445
-			$dtendDt->modify('-1 day');
446
-
447
-			//event that spans over multiple days
448
-			$localeStart = $l10n->l('date', $dtstartDt, ['width' => 'medium']);
449
-			$localeEnd = $l10n->l('date', $dtendDt, ['width' => 'medium']);
450
-
451
-			return $localeStart . ' - ' . $localeEnd;
452
-		}
453
-
454
-		/** @var Property\ICalendar\DateTime $dtstart */
455
-		/** @var Property\ICalendar\DateTime $dtend */
456
-		$isFloating = $dtstart->isFloating();
457
-		$startTimezone = $endTimezone = null;
458
-		if (!$isFloating) {
459
-			$prop = $dtstart->offsetGet('TZID');
460
-			if ($prop instanceof Parameter) {
461
-				$startTimezone = $prop->getValue();
462
-			}
463
-
464
-			$prop = $dtend->offsetGet('TZID');
465
-			if ($prop instanceof Parameter) {
466
-				$endTimezone = $prop->getValue();
467
-			}
468
-		}
469
-
470
-		$localeStart = $l10n->l('weekdayName', $dtstartDt, ['width' => 'abbreviated']) . ', ' .
471
-			$l10n->l('datetime', $dtstartDt, ['width' => 'medium|short']);
472
-
473
-		// always show full date with timezone if timezones are different
474
-		if ($startTimezone !== $endTimezone) {
475
-			$localeEnd = $l10n->l('datetime', $dtendDt, ['width' => 'medium|short']);
476
-
477
-			return $localeStart . ' (' . $startTimezone . ') - ' .
478
-				$localeEnd . ' (' . $endTimezone . ')';
479
-		}
480
-
481
-		// show only end time if date is the same
482
-		if ($this->isDayEqual($dtstartDt, $dtendDt)) {
483
-			$localeEnd = $l10n->l('time', $dtendDt, ['width' => 'short']);
484
-		} else {
485
-			$localeEnd = $l10n->l('weekdayName', $dtendDt, ['width' => 'abbreviated']) . ', ' .
486
-				$l10n->l('datetime', $dtendDt, ['width' => 'medium|short']);
487
-		}
488
-
489
-		return  $localeStart . ' - ' . $localeEnd . ' (' . $startTimezone . ')';
490
-	}
491
-
492
-	/**
493
-	 * @param \DateTime $dtStart
494
-	 * @param \DateTime $dtEnd
495
-	 * @return bool
496
-	 */
497
-	private function isDayEqual(\DateTime $dtStart, \DateTime $dtEnd) {
498
-		return $dtStart->format('Y-m-d') === $dtEnd->format('Y-m-d');
499
-	}
500
-
501
-	/**
502
-	 * @param IEMailTemplate $template
503
-	 * @param IL10N $l10n
504
-	 * @param string $method
505
-	 * @param string $summary
506
-	 * @param string $attendeeName
507
-	 * @param string $inviteeName
508
-	 */
509
-	private function addSubjectAndHeading(IEMailTemplate $template, IL10N $l10n,
510
-										  $method, $summary, $attendeeName, $inviteeName) {
511
-		if ($method === self::METHOD_CANCEL) {
512
-			$template->setSubject('Cancelled: ' . $summary);
513
-			$template->addHeading($l10n->t('Invitation canceled'), $l10n->t('Hello %s,', [$attendeeName]));
514
-			$template->addBodyText($l10n->t('The meeting »%1$s« with %2$s was canceled.', [$summary, $inviteeName]));
515
-		} else if ($method === self::METHOD_REPLY) {
516
-			$template->setSubject('Re: ' . $summary);
517
-			$template->addHeading($l10n->t('Invitation updated'), $l10n->t('Hello %s,', [$attendeeName]));
518
-			$template->addBodyText($l10n->t('The meeting »%1$s« with %2$s was updated.', [$summary, $inviteeName]));
519
-		} else {
520
-			$template->setSubject('Invitation: ' . $summary);
521
-			$template->addHeading($l10n->t('%1$s invited you to »%2$s«', [$inviteeName, $summary]), $l10n->t('Hello %s,', [$attendeeName]));
522
-		}
523
-	}
524
-
525
-	/**
526
-	 * @param IEMailTemplate $template
527
-	 * @param IL10N $l10n
528
-	 * @param string $time
529
-	 * @param string $location
530
-	 * @param string $description
531
-	 * @param string $url
532
-	 */
533
-	private function addBulletList(IEMailTemplate $template, IL10N $l10n, $time, $location, $description, $url) {
534
-		$template->addBodyListItem($time, $l10n->t('When:'),
535
-			$this->getAbsoluteImagePath('filetypes/text-calendar.svg'));
536
-
537
-		if ($location) {
538
-			$template->addBodyListItem($location, $l10n->t('Where:'),
539
-				$this->getAbsoluteImagePath('filetypes/location.svg'));
540
-		}
541
-		if ($description) {
542
-			$template->addBodyListItem((string)$description, $l10n->t('Description:'),
543
-				$this->getAbsoluteImagePath('filetypes/text.svg'));
544
-		}
545
-		if ($url) {
546
-			$template->addBodyListItem((string)$url, $l10n->t('Link:'),
547
-				$this->getAbsoluteImagePath('filetypes/link.svg'));
548
-		}
549
-	}
550
-
551
-	/**
552
-	 * @param IEMailTemplate $template
553
-	 * @param IL10N $l10n
554
-	 * @param Message $iTipMessage
555
-	 * @param int $lastOccurrence
556
-	 */
557
-	private function addResponseButtons(IEMailTemplate $template, IL10N $l10n,
558
-										Message $iTipMessage, $lastOccurrence) {
559
-		$token = $this->createInvitationToken($iTipMessage, $lastOccurrence);
560
-
561
-		$template->addBodyButtonGroup(
562
-			$l10n->t('Accept'),
563
-			$this->urlGenerator->linkToRouteAbsolute('dav.invitation_response.accept', [
564
-				'token' => $token,
565
-			]),
566
-			$l10n->t('Decline'),
567
-			$this->urlGenerator->linkToRouteAbsolute('dav.invitation_response.decline', [
568
-				'token' => $token,
569
-			])
570
-		);
571
-
572
-		$moreOptionsURL = $this->urlGenerator->linkToRouteAbsolute('dav.invitation_response.options', [
573
-			'token' => $token,
574
-		]);
575
-		$html = vsprintf('<small><a href="%s">%s</a></small>', [
576
-			$moreOptionsURL, $l10n->t('More options …')
577
-		]);
578
-		$text = $l10n->t('More options at %s', [$moreOptionsURL]);
579
-
580
-		$template->addBodyText($html, $text);
581
-	}
582
-
583
-	/**
584
-	 * @param string $path
585
-	 * @return string
586
-	 */
587
-	private function getAbsoluteImagePath($path) {
588
-		return $this->urlGenerator->getAbsoluteURL(
589
-			$this->urlGenerator->imagePath('core', $path)
590
-		);
591
-	}
592
-
593
-	/**
594
-	 * @param Message $iTipMessage
595
-	 * @param int $lastOccurrence
596
-	 * @return string
597
-	 */
598
-	private function createInvitationToken(Message $iTipMessage, $lastOccurrence):string {
599
-		$token = $this->random->generate(60, ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS);
600
-
601
-		/** @var VEvent $vevent */
602
-		$vevent = $iTipMessage->message->VEVENT;
603
-		$attendee = $iTipMessage->recipient;
604
-		$organizer = $iTipMessage->sender;
605
-		$sequence = $iTipMessage->sequence;
606
-		$recurrenceId = isset($vevent->{'RECURRENCE-ID'}) ?
607
-			$vevent->{'RECURRENCE-ID'}->serialize() : null;
608
-		$uid = $vevent->{'UID'};
609
-
610
-		$query = $this->db->getQueryBuilder();
611
-		$query->insert('calendar_invitations')
612
-			->values([
613
-				'token' => $query->createNamedParameter($token),
614
-				'attendee' => $query->createNamedParameter($attendee),
615
-				'organizer' => $query->createNamedParameter($organizer),
616
-				'sequence' => $query->createNamedParameter($sequence),
617
-				'recurrenceid' => $query->createNamedParameter($recurrenceId),
618
-				'expiration' => $query->createNamedParameter($lastOccurrence),
619
-				'uid' => $query->createNamedParameter($uid)
620
-			])
621
-			->execute();
622
-
623
-		return $token;
624
-	}
287
+            $recipientDomain = substr(strrchr($recipient, "@"), 1);
288
+            $invitationLinkRecipients = explode(',', preg_replace('/\s+/', '', strtolower($this->config->getAppValue('dav', 'invitation_link_recipients', 'yes'))));
289
+
290
+            if (strcmp('yes', $invitationLinkRecipients[0]) === 0
291
+                 || in_array(strtolower($recipient), $invitationLinkRecipients)
292
+                 || in_array(strtolower($recipientDomain), $invitationLinkRecipients)) {
293
+                $this->addResponseButtons($template, $l10n, $iTipMessage, $lastOccurrence);
294
+            }
295
+        }
296
+
297
+        $template->addFooter();
298
+
299
+        $message->useTemplate($template);
300
+
301
+        $attachment = $this->mailer->createAttachment(
302
+            $iTipMessage->message->serialize(),
303
+            'event.ics',// TODO(leon): Make file name unique, e.g. add event id
304
+            'text/calendar; method=' . $iTipMessage->method
305
+        );
306
+        $message->attach($attachment);
307
+
308
+        try {
309
+            $failed = $this->mailer->send($message);
310
+            $iTipMessage->scheduleStatus = '1.1; Scheduling message is sent via iMip';
311
+            if ($failed) {
312
+                $this->logger->error('Unable to deliver message to {failed}', ['app' => 'dav', 'failed' =>  implode(', ', $failed)]);
313
+                $iTipMessage->scheduleStatus = '5.0; EMail delivery failed';
314
+            }
315
+        } catch(\Exception $ex) {
316
+            $this->logger->logException($ex, ['app' => 'dav']);
317
+            $iTipMessage->scheduleStatus = '5.0; EMail delivery failed';
318
+        }
319
+    }
320
+
321
+    /**
322
+     * check if event took place in the past already
323
+     * @param VCalendar $vObject
324
+     * @return int
325
+     */
326
+    private function getLastOccurrence(VCalendar $vObject) {
327
+        /** @var VEvent $component */
328
+        $component = $vObject->VEVENT;
329
+
330
+        $firstOccurrence = $component->DTSTART->getDateTime()->getTimeStamp();
331
+        // Finding the last occurrence is a bit harder
332
+        if (!isset($component->RRULE)) {
333
+            if (isset($component->DTEND)) {
334
+                $lastOccurrence = $component->DTEND->getDateTime()->getTimeStamp();
335
+            } elseif (isset($component->DURATION)) {
336
+                /** @var \DateTime $endDate */
337
+                $endDate = clone $component->DTSTART->getDateTime();
338
+                // $component->DTEND->getDateTime() returns DateTimeImmutable
339
+                $endDate = $endDate->add(DateTimeParser::parse($component->DURATION->getValue()));
340
+                $lastOccurrence = $endDate->getTimestamp();
341
+            } elseif (!$component->DTSTART->hasTime()) {
342
+                /** @var \DateTime $endDate */
343
+                $endDate = clone $component->DTSTART->getDateTime();
344
+                // $component->DTSTART->getDateTime() returns DateTimeImmutable
345
+                $endDate = $endDate->modify('+1 day');
346
+                $lastOccurrence = $endDate->getTimestamp();
347
+            } else {
348
+                $lastOccurrence = $firstOccurrence;
349
+            }
350
+        } else {
351
+            $it = new EventIterator($vObject, (string)$component->UID);
352
+            $maxDate = new \DateTime(self::MAX_DATE);
353
+            if ($it->isInfinite()) {
354
+                $lastOccurrence = $maxDate->getTimestamp();
355
+            } else {
356
+                $end = $it->getDtEnd();
357
+                while($it->valid() && $end < $maxDate) {
358
+                    $end = $it->getDtEnd();
359
+                    $it->next();
360
+
361
+                }
362
+                $lastOccurrence = $end->getTimestamp();
363
+            }
364
+        }
365
+
366
+        return $lastOccurrence;
367
+    }
368
+
369
+
370
+    /**
371
+     * @param Message $iTipMessage
372
+     * @return null|Property
373
+     */
374
+    private function getCurrentAttendee(Message $iTipMessage) {
375
+        /** @var VEvent $vevent */
376
+        $vevent = $iTipMessage->message->VEVENT;
377
+        $attendees = $vevent->select('ATTENDEE');
378
+        foreach ($attendees as $attendee) {
379
+            /** @var Property $attendee */
380
+            if (strcasecmp($attendee->getValue(), $iTipMessage->recipient) === 0) {
381
+                return $attendee;
382
+            }
383
+        }
384
+        return null;
385
+    }
386
+
387
+    /**
388
+     * @param string $default
389
+     * @param Property|null $attendee
390
+     * @return string
391
+     */
392
+    private function getAttendeeLangOrDefault($default, Property $attendee = null) {
393
+        if ($attendee !== null) {
394
+            $lang = $attendee->offsetGet('LANGUAGE');
395
+            if ($lang instanceof Parameter) {
396
+                return $lang->getValue();
397
+            }
398
+        }
399
+        return $default;
400
+    }
401
+
402
+    /**
403
+     * @param Property|null $attendee
404
+     * @return bool
405
+     */
406
+    private function getAttendeeRSVP(Property $attendee = null) {
407
+        if ($attendee !== null) {
408
+            $rsvp = $attendee->offsetGet('RSVP');
409
+            if (($rsvp instanceof Parameter) && (strcasecmp($rsvp->getValue(), 'TRUE') === 0)) {
410
+                return true;
411
+            }
412
+        }
413
+        // RFC 5545 3.2.17: default RSVP is false
414
+        return false;
415
+    }
416
+
417
+    /**
418
+     * @param IL10N $l10n
419
+     * @param Property $dtstart
420
+     * @param Property $dtend
421
+     */
422
+    private function generateWhenString(IL10N $l10n, Property $dtstart, Property $dtend) {
423
+        $isAllDay = $dtstart instanceof Property\ICalendar\Date;
424
+
425
+        /** @var Property\ICalendar\Date | Property\ICalendar\DateTime $dtstart */
426
+        /** @var Property\ICalendar\Date | Property\ICalendar\DateTime $dtend */
427
+        /** @var \DateTimeImmutable $dtstartDt */
428
+        $dtstartDt = $dtstart->getDateTime();
429
+        /** @var \DateTimeImmutable $dtendDt */
430
+        $dtendDt = $dtend->getDateTime();
431
+
432
+        $diff = $dtstartDt->diff($dtendDt);
433
+
434
+        $dtstartDt = new \DateTime($dtstartDt->format(\DateTime::ATOM));
435
+        $dtendDt = new \DateTime($dtendDt->format(\DateTime::ATOM));
436
+
437
+        if ($isAllDay) {
438
+            // One day event
439
+            if ($diff->days === 1) {
440
+                return $l10n->l('date', $dtstartDt, ['width' => 'medium']);
441
+            }
442
+
443
+            // DTEND is exclusive, so if the ics data says 2020-01-01 to 2020-01-05,
444
+            // the email should show 2020-01-01 to 2020-01-04.
445
+            $dtendDt->modify('-1 day');
446
+
447
+            //event that spans over multiple days
448
+            $localeStart = $l10n->l('date', $dtstartDt, ['width' => 'medium']);
449
+            $localeEnd = $l10n->l('date', $dtendDt, ['width' => 'medium']);
450
+
451
+            return $localeStart . ' - ' . $localeEnd;
452
+        }
453
+
454
+        /** @var Property\ICalendar\DateTime $dtstart */
455
+        /** @var Property\ICalendar\DateTime $dtend */
456
+        $isFloating = $dtstart->isFloating();
457
+        $startTimezone = $endTimezone = null;
458
+        if (!$isFloating) {
459
+            $prop = $dtstart->offsetGet('TZID');
460
+            if ($prop instanceof Parameter) {
461
+                $startTimezone = $prop->getValue();
462
+            }
463
+
464
+            $prop = $dtend->offsetGet('TZID');
465
+            if ($prop instanceof Parameter) {
466
+                $endTimezone = $prop->getValue();
467
+            }
468
+        }
469
+
470
+        $localeStart = $l10n->l('weekdayName', $dtstartDt, ['width' => 'abbreviated']) . ', ' .
471
+            $l10n->l('datetime', $dtstartDt, ['width' => 'medium|short']);
472
+
473
+        // always show full date with timezone if timezones are different
474
+        if ($startTimezone !== $endTimezone) {
475
+            $localeEnd = $l10n->l('datetime', $dtendDt, ['width' => 'medium|short']);
476
+
477
+            return $localeStart . ' (' . $startTimezone . ') - ' .
478
+                $localeEnd . ' (' . $endTimezone . ')';
479
+        }
480
+
481
+        // show only end time if date is the same
482
+        if ($this->isDayEqual($dtstartDt, $dtendDt)) {
483
+            $localeEnd = $l10n->l('time', $dtendDt, ['width' => 'short']);
484
+        } else {
485
+            $localeEnd = $l10n->l('weekdayName', $dtendDt, ['width' => 'abbreviated']) . ', ' .
486
+                $l10n->l('datetime', $dtendDt, ['width' => 'medium|short']);
487
+        }
488
+
489
+        return  $localeStart . ' - ' . $localeEnd . ' (' . $startTimezone . ')';
490
+    }
491
+
492
+    /**
493
+     * @param \DateTime $dtStart
494
+     * @param \DateTime $dtEnd
495
+     * @return bool
496
+     */
497
+    private function isDayEqual(\DateTime $dtStart, \DateTime $dtEnd) {
498
+        return $dtStart->format('Y-m-d') === $dtEnd->format('Y-m-d');
499
+    }
500
+
501
+    /**
502
+     * @param IEMailTemplate $template
503
+     * @param IL10N $l10n
504
+     * @param string $method
505
+     * @param string $summary
506
+     * @param string $attendeeName
507
+     * @param string $inviteeName
508
+     */
509
+    private function addSubjectAndHeading(IEMailTemplate $template, IL10N $l10n,
510
+                                            $method, $summary, $attendeeName, $inviteeName) {
511
+        if ($method === self::METHOD_CANCEL) {
512
+            $template->setSubject('Cancelled: ' . $summary);
513
+            $template->addHeading($l10n->t('Invitation canceled'), $l10n->t('Hello %s,', [$attendeeName]));
514
+            $template->addBodyText($l10n->t('The meeting »%1$s« with %2$s was canceled.', [$summary, $inviteeName]));
515
+        } else if ($method === self::METHOD_REPLY) {
516
+            $template->setSubject('Re: ' . $summary);
517
+            $template->addHeading($l10n->t('Invitation updated'), $l10n->t('Hello %s,', [$attendeeName]));
518
+            $template->addBodyText($l10n->t('The meeting »%1$s« with %2$s was updated.', [$summary, $inviteeName]));
519
+        } else {
520
+            $template->setSubject('Invitation: ' . $summary);
521
+            $template->addHeading($l10n->t('%1$s invited you to »%2$s«', [$inviteeName, $summary]), $l10n->t('Hello %s,', [$attendeeName]));
522
+        }
523
+    }
524
+
525
+    /**
526
+     * @param IEMailTemplate $template
527
+     * @param IL10N $l10n
528
+     * @param string $time
529
+     * @param string $location
530
+     * @param string $description
531
+     * @param string $url
532
+     */
533
+    private function addBulletList(IEMailTemplate $template, IL10N $l10n, $time, $location, $description, $url) {
534
+        $template->addBodyListItem($time, $l10n->t('When:'),
535
+            $this->getAbsoluteImagePath('filetypes/text-calendar.svg'));
536
+
537
+        if ($location) {
538
+            $template->addBodyListItem($location, $l10n->t('Where:'),
539
+                $this->getAbsoluteImagePath('filetypes/location.svg'));
540
+        }
541
+        if ($description) {
542
+            $template->addBodyListItem((string)$description, $l10n->t('Description:'),
543
+                $this->getAbsoluteImagePath('filetypes/text.svg'));
544
+        }
545
+        if ($url) {
546
+            $template->addBodyListItem((string)$url, $l10n->t('Link:'),
547
+                $this->getAbsoluteImagePath('filetypes/link.svg'));
548
+        }
549
+    }
550
+
551
+    /**
552
+     * @param IEMailTemplate $template
553
+     * @param IL10N $l10n
554
+     * @param Message $iTipMessage
555
+     * @param int $lastOccurrence
556
+     */
557
+    private function addResponseButtons(IEMailTemplate $template, IL10N $l10n,
558
+                                        Message $iTipMessage, $lastOccurrence) {
559
+        $token = $this->createInvitationToken($iTipMessage, $lastOccurrence);
560
+
561
+        $template->addBodyButtonGroup(
562
+            $l10n->t('Accept'),
563
+            $this->urlGenerator->linkToRouteAbsolute('dav.invitation_response.accept', [
564
+                'token' => $token,
565
+            ]),
566
+            $l10n->t('Decline'),
567
+            $this->urlGenerator->linkToRouteAbsolute('dav.invitation_response.decline', [
568
+                'token' => $token,
569
+            ])
570
+        );
571
+
572
+        $moreOptionsURL = $this->urlGenerator->linkToRouteAbsolute('dav.invitation_response.options', [
573
+            'token' => $token,
574
+        ]);
575
+        $html = vsprintf('<small><a href="%s">%s</a></small>', [
576
+            $moreOptionsURL, $l10n->t('More options …')
577
+        ]);
578
+        $text = $l10n->t('More options at %s', [$moreOptionsURL]);
579
+
580
+        $template->addBodyText($html, $text);
581
+    }
582
+
583
+    /**
584
+     * @param string $path
585
+     * @return string
586
+     */
587
+    private function getAbsoluteImagePath($path) {
588
+        return $this->urlGenerator->getAbsoluteURL(
589
+            $this->urlGenerator->imagePath('core', $path)
590
+        );
591
+    }
592
+
593
+    /**
594
+     * @param Message $iTipMessage
595
+     * @param int $lastOccurrence
596
+     * @return string
597
+     */
598
+    private function createInvitationToken(Message $iTipMessage, $lastOccurrence):string {
599
+        $token = $this->random->generate(60, ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS);
600
+
601
+        /** @var VEvent $vevent */
602
+        $vevent = $iTipMessage->message->VEVENT;
603
+        $attendee = $iTipMessage->recipient;
604
+        $organizer = $iTipMessage->sender;
605
+        $sequence = $iTipMessage->sequence;
606
+        $recurrenceId = isset($vevent->{'RECURRENCE-ID'}) ?
607
+            $vevent->{'RECURRENCE-ID'}->serialize() : null;
608
+        $uid = $vevent->{'UID'};
609
+
610
+        $query = $this->db->getQueryBuilder();
611
+        $query->insert('calendar_invitations')
612
+            ->values([
613
+                'token' => $query->createNamedParameter($token),
614
+                'attendee' => $query->createNamedParameter($attendee),
615
+                'organizer' => $query->createNamedParameter($organizer),
616
+                'sequence' => $query->createNamedParameter($sequence),
617
+                'recurrenceid' => $query->createNamedParameter($recurrenceId),
618
+                'expiration' => $query->createNamedParameter($lastOccurrence),
619
+                'uid' => $query->createNamedParameter($uid)
620
+            ])
621
+            ->execute();
622
+
623
+        return $token;
624
+    }
625 625
 }
Please login to merge, or discard this patch.
apps/dav/lib/Connector/Sabre/MaintenancePlugin.php 1 patch
Indentation   +48 added lines, -48 removed lines patch added patch discarded remove patch
@@ -33,58 +33,58 @@
 block discarded – undo
33 33
 
34 34
 class MaintenancePlugin extends ServerPlugin {
35 35
 
36
-	/** @var IConfig */
37
-	private $config;
36
+    /** @var IConfig */
37
+    private $config;
38 38
 
39
-	/**
40
-	 * Reference to main server object
41
-	 *
42
-	 * @var Server
43
-	 */
44
-	private $server;
39
+    /**
40
+     * Reference to main server object
41
+     *
42
+     * @var Server
43
+     */
44
+    private $server;
45 45
 
46
-	/**
47
-	 * @param IConfig $config
48
-	 */
49
-	public function __construct(IConfig $config = null) {
50
-		$this->config = $config;
51
-		if (is_null($config)) {
52
-			$this->config = \OC::$server->getConfig();
53
-		}
54
-	}
46
+    /**
47
+     * @param IConfig $config
48
+     */
49
+    public function __construct(IConfig $config = null) {
50
+        $this->config = $config;
51
+        if (is_null($config)) {
52
+            $this->config = \OC::$server->getConfig();
53
+        }
54
+    }
55 55
 
56 56
 
57
-	/**
58
-	 * This initializes the plugin.
59
-	 *
60
-	 * This function is called by \Sabre\DAV\Server, after
61
-	 * addPlugin is called.
62
-	 *
63
-	 * This method should set up the required event subscriptions.
64
-	 *
65
-	 * @param \Sabre\DAV\Server $server
66
-	 * @return void
67
-	 */
68
-	public function initialize(\Sabre\DAV\Server $server) {
69
-		$this->server = $server;
70
-		$this->server->on('beforeMethod', [$this, 'checkMaintenanceMode'], 1);
71
-	}
57
+    /**
58
+     * This initializes the plugin.
59
+     *
60
+     * This function is called by \Sabre\DAV\Server, after
61
+     * addPlugin is called.
62
+     *
63
+     * This method should set up the required event subscriptions.
64
+     *
65
+     * @param \Sabre\DAV\Server $server
66
+     * @return void
67
+     */
68
+    public function initialize(\Sabre\DAV\Server $server) {
69
+        $this->server = $server;
70
+        $this->server->on('beforeMethod', [$this, 'checkMaintenanceMode'], 1);
71
+    }
72 72
 
73
-	/**
74
-	 * This method is called before any HTTP method and returns http status code 503
75
-	 * in case the system is in maintenance mode.
76
-	 *
77
-	 * @throws ServiceUnavailable
78
-	 * @return bool
79
-	 */
80
-	public function checkMaintenanceMode() {
81
-		if ($this->config->getSystemValueBool('maintenance')) {
82
-			throw new ServiceUnavailable('System in maintenance mode.');
83
-		}
84
-		if (Util::needUpgrade()) {
85
-			throw new ServiceUnavailable('Upgrade needed');
86
-		}
73
+    /**
74
+     * This method is called before any HTTP method and returns http status code 503
75
+     * in case the system is in maintenance mode.
76
+     *
77
+     * @throws ServiceUnavailable
78
+     * @return bool
79
+     */
80
+    public function checkMaintenanceMode() {
81
+        if ($this->config->getSystemValueBool('maintenance')) {
82
+            throw new ServiceUnavailable('System in maintenance mode.');
83
+        }
84
+        if (Util::needUpgrade()) {
85
+            throw new ServiceUnavailable('Upgrade needed');
86
+        }
87 87
 
88
-		return true;
89
-	}
88
+        return true;
89
+    }
90 90
 }
Please login to merge, or discard this patch.