@@ -27,72 +27,72 @@ |
||
| 27 | 27 | |
| 28 | 28 | class Todo implements ISetting { |
| 29 | 29 | |
| 30 | - /** @var IL10N */ |
|
| 31 | - protected $l; |
|
| 30 | + /** @var IL10N */ |
|
| 31 | + protected $l; |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * @param IL10N $l |
|
| 35 | - */ |
|
| 36 | - public function __construct(IL10N $l) { |
|
| 37 | - $this->l = $l; |
|
| 38 | - } |
|
| 33 | + /** |
|
| 34 | + * @param IL10N $l |
|
| 35 | + */ |
|
| 36 | + public function __construct(IL10N $l) { |
|
| 37 | + $this->l = $l; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * @return string Lowercase a-z and underscore only identifier |
|
| 42 | - * @since 11.0.0 |
|
| 43 | - */ |
|
| 44 | - public function getIdentifier() { |
|
| 45 | - return 'calendar_todo'; |
|
| 46 | - } |
|
| 40 | + /** |
|
| 41 | + * @return string Lowercase a-z and underscore only identifier |
|
| 42 | + * @since 11.0.0 |
|
| 43 | + */ |
|
| 44 | + public function getIdentifier() { |
|
| 45 | + return 'calendar_todo'; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * @return string A translated string |
|
| 50 | - * @since 11.0.0 |
|
| 51 | - */ |
|
| 52 | - public function getName() { |
|
| 53 | - return $this->l->t('A calendar <strong>todo</strong> was modified'); |
|
| 54 | - } |
|
| 48 | + /** |
|
| 49 | + * @return string A translated string |
|
| 50 | + * @since 11.0.0 |
|
| 51 | + */ |
|
| 52 | + public function getName() { |
|
| 53 | + return $this->l->t('A calendar <strong>todo</strong> was modified'); |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * @return int whether the filter should be rather on the top or bottom of |
|
| 58 | - * the admin section. The filters are arranged in ascending order of the |
|
| 59 | - * priority values. It is required to return a value between 0 and 100. |
|
| 60 | - * @since 11.0.0 |
|
| 61 | - */ |
|
| 62 | - public function getPriority() { |
|
| 63 | - return 50; |
|
| 64 | - } |
|
| 56 | + /** |
|
| 57 | + * @return int whether the filter should be rather on the top or bottom of |
|
| 58 | + * the admin section. The filters are arranged in ascending order of the |
|
| 59 | + * priority values. It is required to return a value between 0 and 100. |
|
| 60 | + * @since 11.0.0 |
|
| 61 | + */ |
|
| 62 | + public function getPriority() { |
|
| 63 | + return 50; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * @return bool True when the option can be changed for the stream |
|
| 68 | - * @since 11.0.0 |
|
| 69 | - */ |
|
| 70 | - public function canChangeStream() { |
|
| 71 | - return true; |
|
| 72 | - } |
|
| 66 | + /** |
|
| 67 | + * @return bool True when the option can be changed for the stream |
|
| 68 | + * @since 11.0.0 |
|
| 69 | + */ |
|
| 70 | + public function canChangeStream() { |
|
| 71 | + return true; |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * @return bool True when the option can be changed for the stream |
|
| 76 | - * @since 11.0.0 |
|
| 77 | - */ |
|
| 78 | - public function isDefaultEnabledStream() { |
|
| 79 | - return true; |
|
| 80 | - } |
|
| 74 | + /** |
|
| 75 | + * @return bool True when the option can be changed for the stream |
|
| 76 | + * @since 11.0.0 |
|
| 77 | + */ |
|
| 78 | + public function isDefaultEnabledStream() { |
|
| 79 | + return true; |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * @return bool True when the option can be changed for the mail |
|
| 84 | - * @since 11.0.0 |
|
| 85 | - */ |
|
| 86 | - public function canChangeMail() { |
|
| 87 | - return true; |
|
| 88 | - } |
|
| 82 | + /** |
|
| 83 | + * @return bool True when the option can be changed for the mail |
|
| 84 | + * @since 11.0.0 |
|
| 85 | + */ |
|
| 86 | + public function canChangeMail() { |
|
| 87 | + return true; |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - /** |
|
| 91 | - * @return bool True when the option can be changed for the stream |
|
| 92 | - * @since 11.0.0 |
|
| 93 | - */ |
|
| 94 | - public function isDefaultEnabledMail() { |
|
| 95 | - return false; |
|
| 96 | - } |
|
| 90 | + /** |
|
| 91 | + * @return bool True when the option can be changed for the stream |
|
| 92 | + * @since 11.0.0 |
|
| 93 | + */ |
|
| 94 | + public function isDefaultEnabledMail() { |
|
| 95 | + return false; |
|
| 96 | + } |
|
| 97 | 97 | } |
| 98 | 98 | |
@@ -34,194 +34,194 @@ |
||
| 34 | 34 | use OCP\IConfig; |
| 35 | 35 | |
| 36 | 36 | class PublishPlugin extends ServerPlugin { |
| 37 | - const NS_CALENDARSERVER = 'http://calendarserver.org/ns/'; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Reference to SabreDAV server object. |
|
| 41 | - * |
|
| 42 | - * @var \Sabre\DAV\Server |
|
| 43 | - */ |
|
| 44 | - protected $server; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Config instance to get instance secret. |
|
| 48 | - * |
|
| 49 | - * @var IConfig |
|
| 50 | - */ |
|
| 51 | - protected $config; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * URL Generator for absolute URLs. |
|
| 55 | - * |
|
| 56 | - * @var IURLGenerator |
|
| 57 | - */ |
|
| 58 | - protected $urlGenerator; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * PublishPlugin constructor. |
|
| 62 | - * |
|
| 63 | - * @param IConfig $config |
|
| 64 | - * @param IURLGenerator $urlGenerator |
|
| 65 | - */ |
|
| 66 | - public function __construct(IConfig $config, IURLGenerator $urlGenerator) { |
|
| 67 | - $this->config = $config; |
|
| 68 | - $this->urlGenerator = $urlGenerator; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * This method should return a list of server-features. |
|
| 73 | - * |
|
| 74 | - * This is for example 'versioning' and is added to the DAV: header |
|
| 75 | - * in an OPTIONS response. |
|
| 76 | - * |
|
| 77 | - * @return string[] |
|
| 78 | - */ |
|
| 79 | - public function getFeatures() { |
|
| 80 | - // May have to be changed to be detected |
|
| 81 | - return ['oc-calendar-publishing', 'calendarserver-sharing']; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Returns a plugin name. |
|
| 86 | - * |
|
| 87 | - * Using this name other plugins will be able to access other plugins |
|
| 88 | - * using Sabre\DAV\Server::getPlugin |
|
| 89 | - * |
|
| 90 | - * @return string |
|
| 91 | - */ |
|
| 92 | - public function getPluginName() { |
|
| 93 | - return 'oc-calendar-publishing'; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * This initializes the plugin. |
|
| 98 | - * |
|
| 99 | - * This function is called by Sabre\DAV\Server, after |
|
| 100 | - * addPlugin is called. |
|
| 101 | - * |
|
| 102 | - * This method should set up the required event subscriptions. |
|
| 103 | - * |
|
| 104 | - * @param Server $server |
|
| 105 | - */ |
|
| 106 | - public function initialize(Server $server) { |
|
| 107 | - $this->server = $server; |
|
| 108 | - |
|
| 109 | - $this->server->on('method:POST', [$this, 'httpPost']); |
|
| 110 | - $this->server->on('propFind', [$this, 'propFind']); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - public function propFind(PropFind $propFind, INode $node) { |
|
| 114 | - if ($node instanceof Calendar) { |
|
| 115 | - $propFind->handle('{'.self::NS_CALENDARSERVER.'}publish-url', function () use ($node) { |
|
| 116 | - if ($node->getPublishStatus()) { |
|
| 117 | - // We return the publish-url only if the calendar is published. |
|
| 118 | - $token = $node->getPublishStatus(); |
|
| 119 | - $publishUrl = $this->urlGenerator->getAbsoluteURL($this->server->getBaseUri().'public-calendars/').$token; |
|
| 120 | - |
|
| 121 | - return new Publisher($publishUrl, true); |
|
| 122 | - } |
|
| 123 | - }); |
|
| 124 | - |
|
| 125 | - $propFind->handle('{'.self::NS_CALENDARSERVER.'}allowed-sharing-modes', function() use ($node) { |
|
| 126 | - return new AllowedSharingModes(!$node->isSubscription(), !$node->isSubscription()); |
|
| 127 | - }); |
|
| 128 | - } |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * We intercept this to handle POST requests on calendars. |
|
| 133 | - * |
|
| 134 | - * @param RequestInterface $request |
|
| 135 | - * @param ResponseInterface $response |
|
| 136 | - * |
|
| 137 | - * @return void|bool |
|
| 138 | - */ |
|
| 139 | - public function httpPost(RequestInterface $request, ResponseInterface $response) { |
|
| 140 | - $path = $request->getPath(); |
|
| 141 | - |
|
| 142 | - // Only handling xml |
|
| 143 | - $contentType = $request->getHeader('Content-Type'); |
|
| 144 | - if (strpos($contentType, 'application/xml') === false && strpos($contentType, 'text/xml') === false) { |
|
| 145 | - return; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - // Making sure the node exists |
|
| 149 | - try { |
|
| 150 | - $node = $this->server->tree->getNodeForPath($path); |
|
| 151 | - } catch (NotFound $e) { |
|
| 152 | - return; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - $requestBody = $request->getBodyAsString(); |
|
| 156 | - |
|
| 157 | - // If this request handler could not deal with this POST request, it |
|
| 158 | - // will return 'null' and other plugins get a chance to handle the |
|
| 159 | - // request. |
|
| 160 | - // |
|
| 161 | - // However, we already requested the full body. This is a problem, |
|
| 162 | - // because a body can only be read once. This is why we preemptively |
|
| 163 | - // re-populated the request body with the existing data. |
|
| 164 | - $request->setBody($requestBody); |
|
| 165 | - |
|
| 166 | - $this->server->xml->parse($requestBody, $request->getUrl(), $documentType); |
|
| 167 | - |
|
| 168 | - switch ($documentType) { |
|
| 169 | - |
|
| 170 | - case '{'.self::NS_CALENDARSERVER.'}publish-calendar' : |
|
| 171 | - |
|
| 172 | - // We can only deal with IShareableCalendar objects |
|
| 173 | - if (!$node instanceof Calendar) { |
|
| 174 | - return; |
|
| 175 | - } |
|
| 176 | - $this->server->transactionType = 'post-publish-calendar'; |
|
| 177 | - |
|
| 178 | - // Getting ACL info |
|
| 179 | - $acl = $this->server->getPlugin('acl'); |
|
| 180 | - |
|
| 181 | - // If there's no ACL support, we allow everything |
|
| 182 | - if ($acl) { |
|
| 183 | - $acl->checkPrivileges($path, '{DAV:}write'); |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - $node->setPublishStatus(true); |
|
| 187 | - |
|
| 188 | - // iCloud sends back the 202, so we will too. |
|
| 189 | - $response->setStatus(202); |
|
| 190 | - |
|
| 191 | - // Adding this because sending a response body may cause issues, |
|
| 192 | - // and I wanted some type of indicator the response was handled. |
|
| 193 | - $response->setHeader('X-Sabre-Status', 'everything-went-well'); |
|
| 194 | - |
|
| 195 | - // Breaking the event chain |
|
| 196 | - return false; |
|
| 197 | - |
|
| 198 | - case '{'.self::NS_CALENDARSERVER.'}unpublish-calendar' : |
|
| 199 | - |
|
| 200 | - // We can only deal with IShareableCalendar objects |
|
| 201 | - if (!$node instanceof Calendar) { |
|
| 202 | - return; |
|
| 203 | - } |
|
| 204 | - $this->server->transactionType = 'post-unpublish-calendar'; |
|
| 205 | - |
|
| 206 | - // Getting ACL info |
|
| 207 | - $acl = $this->server->getPlugin('acl'); |
|
| 208 | - |
|
| 209 | - // If there's no ACL support, we allow everything |
|
| 210 | - if ($acl) { |
|
| 211 | - $acl->checkPrivileges($path, '{DAV:}write'); |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - $node->setPublishStatus(false); |
|
| 215 | - |
|
| 216 | - $response->setStatus(200); |
|
| 217 | - |
|
| 218 | - // Adding this because sending a response body may cause issues, |
|
| 219 | - // and I wanted some type of indicator the response was handled. |
|
| 220 | - $response->setHeader('X-Sabre-Status', 'everything-went-well'); |
|
| 221 | - |
|
| 222 | - // Breaking the event chain |
|
| 223 | - return false; |
|
| 37 | + const NS_CALENDARSERVER = 'http://calendarserver.org/ns/'; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Reference to SabreDAV server object. |
|
| 41 | + * |
|
| 42 | + * @var \Sabre\DAV\Server |
|
| 43 | + */ |
|
| 44 | + protected $server; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Config instance to get instance secret. |
|
| 48 | + * |
|
| 49 | + * @var IConfig |
|
| 50 | + */ |
|
| 51 | + protected $config; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * URL Generator for absolute URLs. |
|
| 55 | + * |
|
| 56 | + * @var IURLGenerator |
|
| 57 | + */ |
|
| 58 | + protected $urlGenerator; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * PublishPlugin constructor. |
|
| 62 | + * |
|
| 63 | + * @param IConfig $config |
|
| 64 | + * @param IURLGenerator $urlGenerator |
|
| 65 | + */ |
|
| 66 | + public function __construct(IConfig $config, IURLGenerator $urlGenerator) { |
|
| 67 | + $this->config = $config; |
|
| 68 | + $this->urlGenerator = $urlGenerator; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * This method should return a list of server-features. |
|
| 73 | + * |
|
| 74 | + * This is for example 'versioning' and is added to the DAV: header |
|
| 75 | + * in an OPTIONS response. |
|
| 76 | + * |
|
| 77 | + * @return string[] |
|
| 78 | + */ |
|
| 79 | + public function getFeatures() { |
|
| 80 | + // May have to be changed to be detected |
|
| 81 | + return ['oc-calendar-publishing', 'calendarserver-sharing']; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Returns a plugin name. |
|
| 86 | + * |
|
| 87 | + * Using this name other plugins will be able to access other plugins |
|
| 88 | + * using Sabre\DAV\Server::getPlugin |
|
| 89 | + * |
|
| 90 | + * @return string |
|
| 91 | + */ |
|
| 92 | + public function getPluginName() { |
|
| 93 | + return 'oc-calendar-publishing'; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * This initializes the plugin. |
|
| 98 | + * |
|
| 99 | + * This function is called by Sabre\DAV\Server, after |
|
| 100 | + * addPlugin is called. |
|
| 101 | + * |
|
| 102 | + * This method should set up the required event subscriptions. |
|
| 103 | + * |
|
| 104 | + * @param Server $server |
|
| 105 | + */ |
|
| 106 | + public function initialize(Server $server) { |
|
| 107 | + $this->server = $server; |
|
| 108 | + |
|
| 109 | + $this->server->on('method:POST', [$this, 'httpPost']); |
|
| 110 | + $this->server->on('propFind', [$this, 'propFind']); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + public function propFind(PropFind $propFind, INode $node) { |
|
| 114 | + if ($node instanceof Calendar) { |
|
| 115 | + $propFind->handle('{'.self::NS_CALENDARSERVER.'}publish-url', function () use ($node) { |
|
| 116 | + if ($node->getPublishStatus()) { |
|
| 117 | + // We return the publish-url only if the calendar is published. |
|
| 118 | + $token = $node->getPublishStatus(); |
|
| 119 | + $publishUrl = $this->urlGenerator->getAbsoluteURL($this->server->getBaseUri().'public-calendars/').$token; |
|
| 120 | + |
|
| 121 | + return new Publisher($publishUrl, true); |
|
| 122 | + } |
|
| 123 | + }); |
|
| 124 | + |
|
| 125 | + $propFind->handle('{'.self::NS_CALENDARSERVER.'}allowed-sharing-modes', function() use ($node) { |
|
| 126 | + return new AllowedSharingModes(!$node->isSubscription(), !$node->isSubscription()); |
|
| 127 | + }); |
|
| 128 | + } |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * We intercept this to handle POST requests on calendars. |
|
| 133 | + * |
|
| 134 | + * @param RequestInterface $request |
|
| 135 | + * @param ResponseInterface $response |
|
| 136 | + * |
|
| 137 | + * @return void|bool |
|
| 138 | + */ |
|
| 139 | + public function httpPost(RequestInterface $request, ResponseInterface $response) { |
|
| 140 | + $path = $request->getPath(); |
|
| 141 | + |
|
| 142 | + // Only handling xml |
|
| 143 | + $contentType = $request->getHeader('Content-Type'); |
|
| 144 | + if (strpos($contentType, 'application/xml') === false && strpos($contentType, 'text/xml') === false) { |
|
| 145 | + return; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + // Making sure the node exists |
|
| 149 | + try { |
|
| 150 | + $node = $this->server->tree->getNodeForPath($path); |
|
| 151 | + } catch (NotFound $e) { |
|
| 152 | + return; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + $requestBody = $request->getBodyAsString(); |
|
| 156 | + |
|
| 157 | + // If this request handler could not deal with this POST request, it |
|
| 158 | + // will return 'null' and other plugins get a chance to handle the |
|
| 159 | + // request. |
|
| 160 | + // |
|
| 161 | + // However, we already requested the full body. This is a problem, |
|
| 162 | + // because a body can only be read once. This is why we preemptively |
|
| 163 | + // re-populated the request body with the existing data. |
|
| 164 | + $request->setBody($requestBody); |
|
| 165 | + |
|
| 166 | + $this->server->xml->parse($requestBody, $request->getUrl(), $documentType); |
|
| 167 | + |
|
| 168 | + switch ($documentType) { |
|
| 169 | + |
|
| 170 | + case '{'.self::NS_CALENDARSERVER.'}publish-calendar' : |
|
| 171 | + |
|
| 172 | + // We can only deal with IShareableCalendar objects |
|
| 173 | + if (!$node instanceof Calendar) { |
|
| 174 | + return; |
|
| 175 | + } |
|
| 176 | + $this->server->transactionType = 'post-publish-calendar'; |
|
| 177 | + |
|
| 178 | + // Getting ACL info |
|
| 179 | + $acl = $this->server->getPlugin('acl'); |
|
| 180 | + |
|
| 181 | + // If there's no ACL support, we allow everything |
|
| 182 | + if ($acl) { |
|
| 183 | + $acl->checkPrivileges($path, '{DAV:}write'); |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + $node->setPublishStatus(true); |
|
| 187 | + |
|
| 188 | + // iCloud sends back the 202, so we will too. |
|
| 189 | + $response->setStatus(202); |
|
| 190 | + |
|
| 191 | + // Adding this because sending a response body may cause issues, |
|
| 192 | + // and I wanted some type of indicator the response was handled. |
|
| 193 | + $response->setHeader('X-Sabre-Status', 'everything-went-well'); |
|
| 194 | + |
|
| 195 | + // Breaking the event chain |
|
| 196 | + return false; |
|
| 197 | + |
|
| 198 | + case '{'.self::NS_CALENDARSERVER.'}unpublish-calendar' : |
|
| 199 | + |
|
| 200 | + // We can only deal with IShareableCalendar objects |
|
| 201 | + if (!$node instanceof Calendar) { |
|
| 202 | + return; |
|
| 203 | + } |
|
| 204 | + $this->server->transactionType = 'post-unpublish-calendar'; |
|
| 205 | + |
|
| 206 | + // Getting ACL info |
|
| 207 | + $acl = $this->server->getPlugin('acl'); |
|
| 208 | + |
|
| 209 | + // If there's no ACL support, we allow everything |
|
| 210 | + if ($acl) { |
|
| 211 | + $acl->checkPrivileges($path, '{DAV:}write'); |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + $node->setPublishStatus(false); |
|
| 215 | + |
|
| 216 | + $response->setStatus(200); |
|
| 217 | + |
|
| 218 | + // Adding this because sending a response body may cause issues, |
|
| 219 | + // and I wanted some type of indicator the response was handled. |
|
| 220 | + $response->setHeader('X-Sabre-Status', 'everything-went-well'); |
|
| 221 | + |
|
| 222 | + // Breaking the event chain |
|
| 223 | + return false; |
|
| 224 | 224 | |
| 225 | - } |
|
| 226 | - } |
|
| 225 | + } |
|
| 226 | + } |
|
| 227 | 227 | } |
@@ -89,7 +89,7 @@ discard block |
||
| 89 | 89 | * |
| 90 | 90 | * @return string |
| 91 | 91 | */ |
| 92 | - public function getPluginName() { |
|
| 92 | + public function getPluginName() { |
|
| 93 | 93 | return 'oc-calendar-publishing'; |
| 94 | 94 | } |
| 95 | 95 | |
@@ -107,12 +107,12 @@ discard block |
||
| 107 | 107 | $this->server = $server; |
| 108 | 108 | |
| 109 | 109 | $this->server->on('method:POST', [$this, 'httpPost']); |
| 110 | - $this->server->on('propFind', [$this, 'propFind']); |
|
| 110 | + $this->server->on('propFind', [$this, 'propFind']); |
|
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | public function propFind(PropFind $propFind, INode $node) { |
| 114 | 114 | if ($node instanceof Calendar) { |
| 115 | - $propFind->handle('{'.self::NS_CALENDARSERVER.'}publish-url', function () use ($node) { |
|
| 115 | + $propFind->handle('{'.self::NS_CALENDARSERVER.'}publish-url', function() use ($node) { |
|
| 116 | 116 | if ($node->getPublishStatus()) { |
| 117 | 117 | // We return the publish-url only if the calendar is published. |
| 118 | 118 | $token = $node->getPublishStatus(); |
@@ -26,58 +26,58 @@ |
||
| 26 | 26 | |
| 27 | 27 | class Publisher implements XmlSerializable { |
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * @var string $publishUrl |
|
| 31 | - */ |
|
| 32 | - protected $publishUrl; |
|
| 29 | + /** |
|
| 30 | + * @var string $publishUrl |
|
| 31 | + */ |
|
| 32 | + protected $publishUrl; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * @var boolean $isPublished |
|
| 36 | - */ |
|
| 37 | - protected $isPublished; |
|
| 34 | + /** |
|
| 35 | + * @var boolean $isPublished |
|
| 36 | + */ |
|
| 37 | + protected $isPublished; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * @param string $publishUrl |
|
| 41 | - * @param boolean $isPublished |
|
| 42 | - */ |
|
| 43 | - function __construct($publishUrl, $isPublished) { |
|
| 44 | - $this->publishUrl = $publishUrl; |
|
| 45 | - $this->isPublished = $isPublished; |
|
| 46 | - } |
|
| 39 | + /** |
|
| 40 | + * @param string $publishUrl |
|
| 41 | + * @param boolean $isPublished |
|
| 42 | + */ |
|
| 43 | + function __construct($publishUrl, $isPublished) { |
|
| 44 | + $this->publishUrl = $publishUrl; |
|
| 45 | + $this->isPublished = $isPublished; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * @return string |
|
| 50 | - */ |
|
| 51 | - function getValue() { |
|
| 52 | - return $this->publishUrl; |
|
| 53 | - } |
|
| 48 | + /** |
|
| 49 | + * @return string |
|
| 50 | + */ |
|
| 51 | + function getValue() { |
|
| 52 | + return $this->publishUrl; |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * The xmlSerialize metod is called during xml writing. |
|
| 57 | - * |
|
| 58 | - * Use the $writer argument to write its own xml serialization. |
|
| 59 | - * |
|
| 60 | - * An important note: do _not_ create a parent element. Any element |
|
| 61 | - * implementing XmlSerializble should only ever write what's considered |
|
| 62 | - * its 'inner xml'. |
|
| 63 | - * |
|
| 64 | - * The parent of the current element is responsible for writing a |
|
| 65 | - * containing element. |
|
| 66 | - * |
|
| 67 | - * This allows serializers to be re-used for different element names. |
|
| 68 | - * |
|
| 69 | - * If you are opening new elements, you must also close them again. |
|
| 70 | - * |
|
| 71 | - * @param Writer $writer |
|
| 72 | - * @return void |
|
| 73 | - */ |
|
| 74 | - function xmlSerialize(Writer $writer) { |
|
| 75 | - if (!$this->isPublished) { |
|
| 76 | - // for pre-publish-url |
|
| 77 | - $writer->write($this->publishUrl); |
|
| 78 | - } else { |
|
| 79 | - // for publish-url |
|
| 80 | - $writer->writeElement('{DAV:}href', $this->publishUrl); |
|
| 81 | - } |
|
| 82 | - } |
|
| 55 | + /** |
|
| 56 | + * The xmlSerialize metod is called during xml writing. |
|
| 57 | + * |
|
| 58 | + * Use the $writer argument to write its own xml serialization. |
|
| 59 | + * |
|
| 60 | + * An important note: do _not_ create a parent element. Any element |
|
| 61 | + * implementing XmlSerializble should only ever write what's considered |
|
| 62 | + * its 'inner xml'. |
|
| 63 | + * |
|
| 64 | + * The parent of the current element is responsible for writing a |
|
| 65 | + * containing element. |
|
| 66 | + * |
|
| 67 | + * This allows serializers to be re-used for different element names. |
|
| 68 | + * |
|
| 69 | + * If you are opening new elements, you must also close them again. |
|
| 70 | + * |
|
| 71 | + * @param Writer $writer |
|
| 72 | + * @return void |
|
| 73 | + */ |
|
| 74 | + function xmlSerialize(Writer $writer) { |
|
| 75 | + if (!$this->isPublished) { |
|
| 76 | + // for pre-publish-url |
|
| 77 | + $writer->write($this->publishUrl); |
|
| 78 | + } else { |
|
| 79 | + // for publish-url |
|
| 80 | + $writer->writeElement('{DAV:}href', $this->publishUrl); |
|
| 81 | + } |
|
| 82 | + } |
|
| 83 | 83 | } |
@@ -111,7 +111,7 @@ discard block |
||
| 111 | 111 | $update = false; |
| 112 | 112 | if (!isset($properties['URI'])) { // create a new contact |
| 113 | 113 | $uid = $this->createUid(); |
| 114 | - $uri = $uid . '.vcf'; |
|
| 114 | + $uri = $uid.'.vcf'; |
|
| 115 | 115 | $vCard = $this->createEmptyVCard($uid); |
| 116 | 116 | } else { // update existing contact |
| 117 | 117 | $uri = $properties['URI']; |
@@ -142,7 +142,7 @@ discard block |
||
| 142 | 142 | $permissions = $this->addressBook->getACL(); |
| 143 | 143 | $result = 0; |
| 144 | 144 | foreach ($permissions as $permission) { |
| 145 | - switch($permission['privilege']) { |
|
| 145 | + switch ($permission['privilege']) { |
|
| 146 | 146 | case '{DAV:}read': |
| 147 | 147 | $result |= Constants::PERMISSION_READ; |
| 148 | 148 | break; |
@@ -187,7 +187,7 @@ discard block |
||
| 187 | 187 | protected function createUid() { |
| 188 | 188 | do { |
| 189 | 189 | $uid = $this->getUid(); |
| 190 | - $contact = $this->backend->getContact($this->getKey(), $uid . '.vcf'); |
|
| 190 | + $contact = $this->backend->getContact($this->getKey(), $uid.'.vcf'); |
|
| 191 | 191 | } while (!empty($contact)); |
| 192 | 192 | |
| 193 | 193 | return $uid; |
@@ -227,15 +227,15 @@ discard block |
||
| 227 | 227 | foreach ($vCard->children() as $property) { |
| 228 | 228 | if ($property->name === 'PHOTO' && $property->getValueType() === 'BINARY') { |
| 229 | 229 | $url = $this->urlGenerator->getAbsoluteURL( |
| 230 | - $this->urlGenerator->linkTo('', 'remote.php') . '/dav/'); |
|
| 230 | + $this->urlGenerator->linkTo('', 'remote.php').'/dav/'); |
|
| 231 | 231 | $url .= implode('/', [ |
| 232 | 232 | 'addressbooks', |
| 233 | 233 | substr($this->addressBookInfo['principaluri'], 11), //cut off 'principals/' |
| 234 | 234 | $this->addressBookInfo['uri'], |
| 235 | 235 | $uri |
| 236 | - ]) . '?photo'; |
|
| 236 | + ]).'?photo'; |
|
| 237 | 237 | |
| 238 | - $result['PHOTO'] = 'VALUE=uri:' . $url; |
|
| 238 | + $result['PHOTO'] = 'VALUE=uri:'.$url; |
|
| 239 | 239 | |
| 240 | 240 | } else if ($property->name === 'X-SOCIALPROFILE') { |
| 241 | 241 | $type = $this->getTypeFromProperty($property); |
@@ -35,287 +35,287 @@ |
||
| 35 | 35 | |
| 36 | 36 | class AddressBookImpl implements IAddressBook { |
| 37 | 37 | |
| 38 | - /** @var CardDavBackend */ |
|
| 39 | - private $backend; |
|
| 40 | - |
|
| 41 | - /** @var array */ |
|
| 42 | - private $addressBookInfo; |
|
| 43 | - |
|
| 44 | - /** @var AddressBook */ |
|
| 45 | - private $addressBook; |
|
| 46 | - |
|
| 47 | - /** @var IURLGenerator */ |
|
| 48 | - private $urlGenerator; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * AddressBookImpl constructor. |
|
| 52 | - * |
|
| 53 | - * @param AddressBook $addressBook |
|
| 54 | - * @param array $addressBookInfo |
|
| 55 | - * @param CardDavBackend $backend |
|
| 56 | - * @param IUrlGenerator $urlGenerator |
|
| 57 | - */ |
|
| 58 | - public function __construct( |
|
| 59 | - AddressBook $addressBook, |
|
| 60 | - array $addressBookInfo, |
|
| 61 | - CardDavBackend $backend, |
|
| 62 | - IURLGenerator $urlGenerator) { |
|
| 63 | - |
|
| 64 | - $this->addressBook = $addressBook; |
|
| 65 | - $this->addressBookInfo = $addressBookInfo; |
|
| 66 | - $this->backend = $backend; |
|
| 67 | - $this->urlGenerator = $urlGenerator; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * @return string defining the technical unique key |
|
| 72 | - * @since 5.0.0 |
|
| 73 | - */ |
|
| 74 | - public function getKey() { |
|
| 75 | - return $this->addressBookInfo['id']; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * @return string defining the unique uri |
|
| 80 | - * @since 16.0.0 |
|
| 81 | - * @return string |
|
| 82 | - */ |
|
| 83 | - public function getUri(): string { |
|
| 84 | - return $this->addressBookInfo['uri']; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * In comparison to getKey() this function returns a human readable (maybe translated) name |
|
| 89 | - * |
|
| 90 | - * @return mixed |
|
| 91 | - * @since 5.0.0 |
|
| 92 | - */ |
|
| 93 | - public function getDisplayName() { |
|
| 94 | - return $this->addressBookInfo['{DAV:}displayname']; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * @param string $pattern which should match within the $searchProperties |
|
| 99 | - * @param array $searchProperties defines the properties within the query pattern should match |
|
| 100 | - * @param array $options Options to define the output format |
|
| 101 | - * - types boolean (since 15.0.0) If set to true, fields that come with a TYPE property will be an array |
|
| 102 | - * example: ['id' => 5, 'FN' => 'Thomas Tanghus', 'EMAIL' => ['type => 'HOME', 'value' => '[email protected]']] |
|
| 103 | - * @return array an array of contacts which are arrays of key-value-pairs |
|
| 104 | - * example result: |
|
| 105 | - * [ |
|
| 106 | - * ['id' => 0, 'FN' => 'Thomas Müller', 'EMAIL' => '[email protected]', 'GEO' => '37.386013;-122.082932'], |
|
| 107 | - * ['id' => 5, 'FN' => 'Thomas Tanghus', 'EMAIL' => ['[email protected]', '[email protected]']] |
|
| 108 | - * ] |
|
| 109 | - * @return array an array of contacts which are arrays of key-value-pairs |
|
| 110 | - * @since 5.0.0 |
|
| 111 | - */ |
|
| 112 | - public function search($pattern, $searchProperties, $options) { |
|
| 113 | - $results = $this->backend->search($this->getKey(), $pattern, $searchProperties); |
|
| 114 | - |
|
| 115 | - $withTypes = \array_key_exists('types', $options) && $options['types'] === true; |
|
| 116 | - |
|
| 117 | - $vCards = []; |
|
| 118 | - foreach ($results as $result) { |
|
| 119 | - $vCards[] = $this->vCard2Array($result['uri'], $this->readCard($result['carddata']), $withTypes); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - return $vCards; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * @param array $properties this array if key-value-pairs defines a contact |
|
| 127 | - * @return array an array representing the contact just created or updated |
|
| 128 | - * @since 5.0.0 |
|
| 129 | - */ |
|
| 130 | - public function createOrUpdate($properties) { |
|
| 131 | - $update = false; |
|
| 132 | - if (!isset($properties['URI'])) { // create a new contact |
|
| 133 | - $uid = $this->createUid(); |
|
| 134 | - $uri = $uid . '.vcf'; |
|
| 135 | - $vCard = $this->createEmptyVCard($uid); |
|
| 136 | - } else { // update existing contact |
|
| 137 | - $uri = $properties['URI']; |
|
| 138 | - $vCardData = $this->backend->getCard($this->getKey(), $uri); |
|
| 139 | - $vCard = $this->readCard($vCardData['carddata']); |
|
| 140 | - $update = true; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - foreach ($properties as $key => $value) { |
|
| 144 | - $vCard->$key = $vCard->createProperty($key, $value); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - if ($update) { |
|
| 148 | - $this->backend->updateCard($this->getKey(), $uri, $vCard->serialize()); |
|
| 149 | - } else { |
|
| 150 | - $this->backend->createCard($this->getKey(), $uri, $vCard->serialize()); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - return $this->vCard2Array($uri, $vCard); |
|
| 154 | - |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * @return mixed |
|
| 159 | - * @since 5.0.0 |
|
| 160 | - */ |
|
| 161 | - public function getPermissions() { |
|
| 162 | - $permissions = $this->addressBook->getACL(); |
|
| 163 | - $result = 0; |
|
| 164 | - foreach ($permissions as $permission) { |
|
| 165 | - switch($permission['privilege']) { |
|
| 166 | - case '{DAV:}read': |
|
| 167 | - $result |= Constants::PERMISSION_READ; |
|
| 168 | - break; |
|
| 169 | - case '{DAV:}write': |
|
| 170 | - $result |= Constants::PERMISSION_CREATE; |
|
| 171 | - $result |= Constants::PERMISSION_UPDATE; |
|
| 172 | - break; |
|
| 173 | - case '{DAV:}all': |
|
| 174 | - $result |= Constants::PERMISSION_ALL; |
|
| 175 | - break; |
|
| 176 | - } |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - return $result; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * @param object $id the unique identifier to a contact |
|
| 184 | - * @return bool successful or not |
|
| 185 | - * @since 5.0.0 |
|
| 186 | - */ |
|
| 187 | - public function delete($id) { |
|
| 188 | - $uri = $this->backend->getCardUri($id); |
|
| 189 | - return $this->backend->deleteCard($this->addressBookInfo['id'], $uri); |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * read vCard data into a vCard object |
|
| 194 | - * |
|
| 195 | - * @param string $cardData |
|
| 196 | - * @return VCard |
|
| 197 | - */ |
|
| 198 | - protected function readCard($cardData) { |
|
| 199 | - return Reader::read($cardData); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * create UID for contact |
|
| 204 | - * |
|
| 205 | - * @return string |
|
| 206 | - */ |
|
| 207 | - protected function createUid() { |
|
| 208 | - do { |
|
| 209 | - $uid = $this->getUid(); |
|
| 210 | - $contact = $this->backend->getContact($this->getKey(), $uid . '.vcf'); |
|
| 211 | - } while (!empty($contact)); |
|
| 212 | - |
|
| 213 | - return $uid; |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * getUid is only there for testing, use createUid instead |
|
| 218 | - */ |
|
| 219 | - protected function getUid() { |
|
| 220 | - return UUIDUtil::getUUID(); |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * create empty vcard |
|
| 225 | - * |
|
| 226 | - * @param string $uid |
|
| 227 | - * @return VCard |
|
| 228 | - */ |
|
| 229 | - protected function createEmptyVCard($uid) { |
|
| 230 | - $vCard = new VCard(); |
|
| 231 | - $vCard->UID = $uid; |
|
| 232 | - return $vCard; |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * create array with all vCard properties |
|
| 237 | - * |
|
| 238 | - * @param string $uri |
|
| 239 | - * @param VCard $vCard |
|
| 240 | - * @return array |
|
| 241 | - */ |
|
| 242 | - protected function vCard2Array($uri, VCard $vCard, $withTypes = false) { |
|
| 243 | - $result = [ |
|
| 244 | - 'URI' => $uri, |
|
| 245 | - ]; |
|
| 246 | - |
|
| 247 | - foreach ($vCard->children() as $property) { |
|
| 248 | - if ($property->name === 'PHOTO' && $property->getValueType() === 'BINARY') { |
|
| 249 | - $url = $this->urlGenerator->getAbsoluteURL( |
|
| 250 | - $this->urlGenerator->linkTo('', 'remote.php') . '/dav/'); |
|
| 251 | - $url .= implode('/', [ |
|
| 252 | - 'addressbooks', |
|
| 253 | - substr($this->addressBookInfo['principaluri'], 11), //cut off 'principals/' |
|
| 254 | - $this->addressBookInfo['uri'], |
|
| 255 | - $uri |
|
| 256 | - ]) . '?photo'; |
|
| 257 | - |
|
| 258 | - $result['PHOTO'] = 'VALUE=uri:' . $url; |
|
| 259 | - |
|
| 260 | - } else if ($property->name === 'X-SOCIALPROFILE') { |
|
| 261 | - $type = $this->getTypeFromProperty($property); |
|
| 262 | - |
|
| 263 | - // Type is the social network, when it's empty we don't need this. |
|
| 264 | - if ($type !== null) { |
|
| 265 | - if (!isset($result[$property->name])) { |
|
| 266 | - $result[$property->name] = []; |
|
| 267 | - } |
|
| 268 | - $result[$property->name][$type] = $property->getValue(); |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - // The following properties can be set multiple times |
|
| 272 | - } else if (in_array($property->name, ['CLOUD', 'EMAIL', 'IMPP', 'TEL', 'URL'])) { |
|
| 273 | - if (!isset($result[$property->name])) { |
|
| 274 | - $result[$property->name] = []; |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - $type = $this->getTypeFromProperty($property); |
|
| 278 | - if ($withTypes) { |
|
| 279 | - $result[$property->name][] = [ |
|
| 280 | - 'type' => $type, |
|
| 281 | - 'value' => $property->getValue() |
|
| 282 | - ]; |
|
| 283 | - } else { |
|
| 284 | - $result[$property->name][] = $property->getValue(); |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - |
|
| 288 | - } else { |
|
| 289 | - $result[$property->name] = $property->getValue(); |
|
| 290 | - } |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - if ( |
|
| 294 | - $this->addressBookInfo['principaluri'] === 'principals/system/system' && ( |
|
| 295 | - $this->addressBookInfo['uri'] === 'system' || |
|
| 296 | - $this->addressBookInfo['{DAV:}displayname'] === $this->urlGenerator->getBaseUrl() |
|
| 297 | - ) |
|
| 298 | - ) { |
|
| 299 | - $result['isLocalSystemBook'] = true; |
|
| 300 | - } |
|
| 301 | - return $result; |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - /** |
|
| 305 | - * Get the type of the current property |
|
| 306 | - * |
|
| 307 | - * @param Property $property |
|
| 308 | - * @return null|string |
|
| 309 | - */ |
|
| 310 | - protected function getTypeFromProperty(Property $property) { |
|
| 311 | - $parameters = $property->parameters(); |
|
| 312 | - // Type is the social network, when it's empty we don't need this. |
|
| 313 | - if (isset($parameters['TYPE'])) { |
|
| 314 | - /** @var \Sabre\VObject\Parameter $type */ |
|
| 315 | - $type = $parameters['TYPE']; |
|
| 316 | - return $type->getValue(); |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - return null; |
|
| 320 | - } |
|
| 38 | + /** @var CardDavBackend */ |
|
| 39 | + private $backend; |
|
| 40 | + |
|
| 41 | + /** @var array */ |
|
| 42 | + private $addressBookInfo; |
|
| 43 | + |
|
| 44 | + /** @var AddressBook */ |
|
| 45 | + private $addressBook; |
|
| 46 | + |
|
| 47 | + /** @var IURLGenerator */ |
|
| 48 | + private $urlGenerator; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * AddressBookImpl constructor. |
|
| 52 | + * |
|
| 53 | + * @param AddressBook $addressBook |
|
| 54 | + * @param array $addressBookInfo |
|
| 55 | + * @param CardDavBackend $backend |
|
| 56 | + * @param IUrlGenerator $urlGenerator |
|
| 57 | + */ |
|
| 58 | + public function __construct( |
|
| 59 | + AddressBook $addressBook, |
|
| 60 | + array $addressBookInfo, |
|
| 61 | + CardDavBackend $backend, |
|
| 62 | + IURLGenerator $urlGenerator) { |
|
| 63 | + |
|
| 64 | + $this->addressBook = $addressBook; |
|
| 65 | + $this->addressBookInfo = $addressBookInfo; |
|
| 66 | + $this->backend = $backend; |
|
| 67 | + $this->urlGenerator = $urlGenerator; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * @return string defining the technical unique key |
|
| 72 | + * @since 5.0.0 |
|
| 73 | + */ |
|
| 74 | + public function getKey() { |
|
| 75 | + return $this->addressBookInfo['id']; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * @return string defining the unique uri |
|
| 80 | + * @since 16.0.0 |
|
| 81 | + * @return string |
|
| 82 | + */ |
|
| 83 | + public function getUri(): string { |
|
| 84 | + return $this->addressBookInfo['uri']; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * In comparison to getKey() this function returns a human readable (maybe translated) name |
|
| 89 | + * |
|
| 90 | + * @return mixed |
|
| 91 | + * @since 5.0.0 |
|
| 92 | + */ |
|
| 93 | + public function getDisplayName() { |
|
| 94 | + return $this->addressBookInfo['{DAV:}displayname']; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * @param string $pattern which should match within the $searchProperties |
|
| 99 | + * @param array $searchProperties defines the properties within the query pattern should match |
|
| 100 | + * @param array $options Options to define the output format |
|
| 101 | + * - types boolean (since 15.0.0) If set to true, fields that come with a TYPE property will be an array |
|
| 102 | + * example: ['id' => 5, 'FN' => 'Thomas Tanghus', 'EMAIL' => ['type => 'HOME', 'value' => '[email protected]']] |
|
| 103 | + * @return array an array of contacts which are arrays of key-value-pairs |
|
| 104 | + * example result: |
|
| 105 | + * [ |
|
| 106 | + * ['id' => 0, 'FN' => 'Thomas Müller', 'EMAIL' => '[email protected]', 'GEO' => '37.386013;-122.082932'], |
|
| 107 | + * ['id' => 5, 'FN' => 'Thomas Tanghus', 'EMAIL' => ['[email protected]', '[email protected]']] |
|
| 108 | + * ] |
|
| 109 | + * @return array an array of contacts which are arrays of key-value-pairs |
|
| 110 | + * @since 5.0.0 |
|
| 111 | + */ |
|
| 112 | + public function search($pattern, $searchProperties, $options) { |
|
| 113 | + $results = $this->backend->search($this->getKey(), $pattern, $searchProperties); |
|
| 114 | + |
|
| 115 | + $withTypes = \array_key_exists('types', $options) && $options['types'] === true; |
|
| 116 | + |
|
| 117 | + $vCards = []; |
|
| 118 | + foreach ($results as $result) { |
|
| 119 | + $vCards[] = $this->vCard2Array($result['uri'], $this->readCard($result['carddata']), $withTypes); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + return $vCards; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * @param array $properties this array if key-value-pairs defines a contact |
|
| 127 | + * @return array an array representing the contact just created or updated |
|
| 128 | + * @since 5.0.0 |
|
| 129 | + */ |
|
| 130 | + public function createOrUpdate($properties) { |
|
| 131 | + $update = false; |
|
| 132 | + if (!isset($properties['URI'])) { // create a new contact |
|
| 133 | + $uid = $this->createUid(); |
|
| 134 | + $uri = $uid . '.vcf'; |
|
| 135 | + $vCard = $this->createEmptyVCard($uid); |
|
| 136 | + } else { // update existing contact |
|
| 137 | + $uri = $properties['URI']; |
|
| 138 | + $vCardData = $this->backend->getCard($this->getKey(), $uri); |
|
| 139 | + $vCard = $this->readCard($vCardData['carddata']); |
|
| 140 | + $update = true; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + foreach ($properties as $key => $value) { |
|
| 144 | + $vCard->$key = $vCard->createProperty($key, $value); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + if ($update) { |
|
| 148 | + $this->backend->updateCard($this->getKey(), $uri, $vCard->serialize()); |
|
| 149 | + } else { |
|
| 150 | + $this->backend->createCard($this->getKey(), $uri, $vCard->serialize()); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + return $this->vCard2Array($uri, $vCard); |
|
| 154 | + |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * @return mixed |
|
| 159 | + * @since 5.0.0 |
|
| 160 | + */ |
|
| 161 | + public function getPermissions() { |
|
| 162 | + $permissions = $this->addressBook->getACL(); |
|
| 163 | + $result = 0; |
|
| 164 | + foreach ($permissions as $permission) { |
|
| 165 | + switch($permission['privilege']) { |
|
| 166 | + case '{DAV:}read': |
|
| 167 | + $result |= Constants::PERMISSION_READ; |
|
| 168 | + break; |
|
| 169 | + case '{DAV:}write': |
|
| 170 | + $result |= Constants::PERMISSION_CREATE; |
|
| 171 | + $result |= Constants::PERMISSION_UPDATE; |
|
| 172 | + break; |
|
| 173 | + case '{DAV:}all': |
|
| 174 | + $result |= Constants::PERMISSION_ALL; |
|
| 175 | + break; |
|
| 176 | + } |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + return $result; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * @param object $id the unique identifier to a contact |
|
| 184 | + * @return bool successful or not |
|
| 185 | + * @since 5.0.0 |
|
| 186 | + */ |
|
| 187 | + public function delete($id) { |
|
| 188 | + $uri = $this->backend->getCardUri($id); |
|
| 189 | + return $this->backend->deleteCard($this->addressBookInfo['id'], $uri); |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * read vCard data into a vCard object |
|
| 194 | + * |
|
| 195 | + * @param string $cardData |
|
| 196 | + * @return VCard |
|
| 197 | + */ |
|
| 198 | + protected function readCard($cardData) { |
|
| 199 | + return Reader::read($cardData); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * create UID for contact |
|
| 204 | + * |
|
| 205 | + * @return string |
|
| 206 | + */ |
|
| 207 | + protected function createUid() { |
|
| 208 | + do { |
|
| 209 | + $uid = $this->getUid(); |
|
| 210 | + $contact = $this->backend->getContact($this->getKey(), $uid . '.vcf'); |
|
| 211 | + } while (!empty($contact)); |
|
| 212 | + |
|
| 213 | + return $uid; |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * getUid is only there for testing, use createUid instead |
|
| 218 | + */ |
|
| 219 | + protected function getUid() { |
|
| 220 | + return UUIDUtil::getUUID(); |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * create empty vcard |
|
| 225 | + * |
|
| 226 | + * @param string $uid |
|
| 227 | + * @return VCard |
|
| 228 | + */ |
|
| 229 | + protected function createEmptyVCard($uid) { |
|
| 230 | + $vCard = new VCard(); |
|
| 231 | + $vCard->UID = $uid; |
|
| 232 | + return $vCard; |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * create array with all vCard properties |
|
| 237 | + * |
|
| 238 | + * @param string $uri |
|
| 239 | + * @param VCard $vCard |
|
| 240 | + * @return array |
|
| 241 | + */ |
|
| 242 | + protected function vCard2Array($uri, VCard $vCard, $withTypes = false) { |
|
| 243 | + $result = [ |
|
| 244 | + 'URI' => $uri, |
|
| 245 | + ]; |
|
| 246 | + |
|
| 247 | + foreach ($vCard->children() as $property) { |
|
| 248 | + if ($property->name === 'PHOTO' && $property->getValueType() === 'BINARY') { |
|
| 249 | + $url = $this->urlGenerator->getAbsoluteURL( |
|
| 250 | + $this->urlGenerator->linkTo('', 'remote.php') . '/dav/'); |
|
| 251 | + $url .= implode('/', [ |
|
| 252 | + 'addressbooks', |
|
| 253 | + substr($this->addressBookInfo['principaluri'], 11), //cut off 'principals/' |
|
| 254 | + $this->addressBookInfo['uri'], |
|
| 255 | + $uri |
|
| 256 | + ]) . '?photo'; |
|
| 257 | + |
|
| 258 | + $result['PHOTO'] = 'VALUE=uri:' . $url; |
|
| 259 | + |
|
| 260 | + } else if ($property->name === 'X-SOCIALPROFILE') { |
|
| 261 | + $type = $this->getTypeFromProperty($property); |
|
| 262 | + |
|
| 263 | + // Type is the social network, when it's empty we don't need this. |
|
| 264 | + if ($type !== null) { |
|
| 265 | + if (!isset($result[$property->name])) { |
|
| 266 | + $result[$property->name] = []; |
|
| 267 | + } |
|
| 268 | + $result[$property->name][$type] = $property->getValue(); |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + // The following properties can be set multiple times |
|
| 272 | + } else if (in_array($property->name, ['CLOUD', 'EMAIL', 'IMPP', 'TEL', 'URL'])) { |
|
| 273 | + if (!isset($result[$property->name])) { |
|
| 274 | + $result[$property->name] = []; |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + $type = $this->getTypeFromProperty($property); |
|
| 278 | + if ($withTypes) { |
|
| 279 | + $result[$property->name][] = [ |
|
| 280 | + 'type' => $type, |
|
| 281 | + 'value' => $property->getValue() |
|
| 282 | + ]; |
|
| 283 | + } else { |
|
| 284 | + $result[$property->name][] = $property->getValue(); |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + |
|
| 288 | + } else { |
|
| 289 | + $result[$property->name] = $property->getValue(); |
|
| 290 | + } |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + if ( |
|
| 294 | + $this->addressBookInfo['principaluri'] === 'principals/system/system' && ( |
|
| 295 | + $this->addressBookInfo['uri'] === 'system' || |
|
| 296 | + $this->addressBookInfo['{DAV:}displayname'] === $this->urlGenerator->getBaseUrl() |
|
| 297 | + ) |
|
| 298 | + ) { |
|
| 299 | + $result['isLocalSystemBook'] = true; |
|
| 300 | + } |
|
| 301 | + return $result; |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + /** |
|
| 305 | + * Get the type of the current property |
|
| 306 | + * |
|
| 307 | + * @param Property $property |
|
| 308 | + * @return null|string |
|
| 309 | + */ |
|
| 310 | + protected function getTypeFromProperty(Property $property) { |
|
| 311 | + $parameters = $property->parameters(); |
|
| 312 | + // Type is the social network, when it's empty we don't need this. |
|
| 313 | + if (isset($parameters['TYPE'])) { |
|
| 314 | + /** @var \Sabre\VObject\Parameter $type */ |
|
| 315 | + $type = $parameters['TYPE']; |
|
| 316 | + return $type->getValue(); |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + return null; |
|
| 320 | + } |
|
| 321 | 321 | } |
@@ -26,21 +26,21 @@ |
||
| 26 | 26 | use Sabre\Xml\Writer; |
| 27 | 27 | |
| 28 | 28 | class Groups implements XmlSerializable { |
| 29 | - const NS_OWNCLOUD = 'http://owncloud.org/ns'; |
|
| 29 | + const NS_OWNCLOUD = 'http://owncloud.org/ns'; |
|
| 30 | 30 | |
| 31 | - /** @var string[] of TYPE:CHECKSUM */ |
|
| 32 | - private $groups; |
|
| 31 | + /** @var string[] of TYPE:CHECKSUM */ |
|
| 32 | + private $groups; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * @param string $groups |
|
| 36 | - */ |
|
| 37 | - public function __construct($groups) { |
|
| 38 | - $this->groups = $groups; |
|
| 39 | - } |
|
| 34 | + /** |
|
| 35 | + * @param string $groups |
|
| 36 | + */ |
|
| 37 | + public function __construct($groups) { |
|
| 38 | + $this->groups = $groups; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - function xmlSerialize(Writer $writer) { |
|
| 42 | - foreach ($this->groups as $group) { |
|
| 43 | - $writer->writeElement('{' . self::NS_OWNCLOUD . '}group', $group); |
|
| 44 | - } |
|
| 45 | - } |
|
| 41 | + function xmlSerialize(Writer $writer) { |
|
| 42 | + foreach ($this->groups as $group) { |
|
| 43 | + $writer->writeElement('{' . self::NS_OWNCLOUD . '}group', $group); |
|
| 44 | + } |
|
| 45 | + } |
|
| 46 | 46 | } |
@@ -40,7 +40,7 @@ |
||
| 40 | 40 | |
| 41 | 41 | function xmlSerialize(Writer $writer) { |
| 42 | 42 | foreach ($this->groups as $group) { |
| 43 | - $writer->writeElement('{' . self::NS_OWNCLOUD . '}group', $group); |
|
| 43 | + $writer->writeElement('{'.self::NS_OWNCLOUD.'}group', $group); |
|
| 44 | 44 | } |
| 45 | 45 | } |
| 46 | 46 | } |
@@ -25,46 +25,46 @@ |
||
| 25 | 25 | |
| 26 | 26 | class AddressBookRoot extends \Sabre\CardDAV\AddressBookRoot { |
| 27 | 27 | |
| 28 | - /** @var IL10N */ |
|
| 29 | - protected $l10n; |
|
| 28 | + /** @var IL10N */ |
|
| 29 | + protected $l10n; |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * @param \Sabre\DAVACL\PrincipalBackend\BackendInterface $principalBackend |
|
| 33 | - * @param \Sabre\CardDAV\Backend\BackendInterface $carddavBackend |
|
| 34 | - * @param string $principalPrefix |
|
| 35 | - */ |
|
| 36 | - public function __construct(\Sabre\DAVACL\PrincipalBackend\BackendInterface $principalBackend, \Sabre\CardDAV\Backend\BackendInterface $carddavBackend, $principalPrefix = 'principals') { |
|
| 37 | - parent::__construct($principalBackend, $carddavBackend, $principalPrefix); |
|
| 38 | - $this->l10n = \OC::$server->getL10N('dav'); |
|
| 39 | - } |
|
| 31 | + /** |
|
| 32 | + * @param \Sabre\DAVACL\PrincipalBackend\BackendInterface $principalBackend |
|
| 33 | + * @param \Sabre\CardDAV\Backend\BackendInterface $carddavBackend |
|
| 34 | + * @param string $principalPrefix |
|
| 35 | + */ |
|
| 36 | + public function __construct(\Sabre\DAVACL\PrincipalBackend\BackendInterface $principalBackend, \Sabre\CardDAV\Backend\BackendInterface $carddavBackend, $principalPrefix = 'principals') { |
|
| 37 | + parent::__construct($principalBackend, $carddavBackend, $principalPrefix); |
|
| 38 | + $this->l10n = \OC::$server->getL10N('dav'); |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * This method returns a node for a principal. |
|
| 43 | - * |
|
| 44 | - * The passed array contains principal information, and is guaranteed to |
|
| 45 | - * at least contain a uri item. Other properties may or may not be |
|
| 46 | - * supplied by the authentication backend. |
|
| 47 | - * |
|
| 48 | - * @param array $principal |
|
| 49 | - * @return \Sabre\DAV\INode |
|
| 50 | - */ |
|
| 51 | - function getChildForPrincipal(array $principal) { |
|
| 41 | + /** |
|
| 42 | + * This method returns a node for a principal. |
|
| 43 | + * |
|
| 44 | + * The passed array contains principal information, and is guaranteed to |
|
| 45 | + * at least contain a uri item. Other properties may or may not be |
|
| 46 | + * supplied by the authentication backend. |
|
| 47 | + * |
|
| 48 | + * @param array $principal |
|
| 49 | + * @return \Sabre\DAV\INode |
|
| 50 | + */ |
|
| 51 | + function getChildForPrincipal(array $principal) { |
|
| 52 | 52 | |
| 53 | - return new UserAddressBooks($this->carddavBackend, $principal['uri'], $this->l10n); |
|
| 53 | + return new UserAddressBooks($this->carddavBackend, $principal['uri'], $this->l10n); |
|
| 54 | 54 | |
| 55 | - } |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - function getName() { |
|
| 57 | + function getName() { |
|
| 58 | 58 | |
| 59 | - if ($this->principalPrefix === 'principals') { |
|
| 60 | - return parent::getName(); |
|
| 61 | - } |
|
| 62 | - // Grabbing all the components of the principal path. |
|
| 63 | - $parts = explode('/', $this->principalPrefix); |
|
| 59 | + if ($this->principalPrefix === 'principals') { |
|
| 60 | + return parent::getName(); |
|
| 61 | + } |
|
| 62 | + // Grabbing all the components of the principal path. |
|
| 63 | + $parts = explode('/', $this->principalPrefix); |
|
| 64 | 64 | |
| 65 | - // We are only interested in the second part. |
|
| 66 | - return $parts[1]; |
|
| 65 | + // We are only interested in the second part. |
|
| 66 | + return $parts[1]; |
|
| 67 | 67 | |
| 68 | - } |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | 70 | } |
@@ -745,7 +745,9 @@ |
||
| 745 | 745 | $stmt->execute([ $addressBookId ]); |
| 746 | 746 | $currentToken = $stmt->fetchColumn(0); |
| 747 | 747 | |
| 748 | - if (is_null($currentToken)) return null; |
|
| 748 | + if (is_null($currentToken)) { |
|
| 749 | + return null; |
|
| 750 | + } |
|
| 749 | 751 | |
| 750 | 752 | $result = [ |
| 751 | 753 | 'syncToken' => $currentToken, |
@@ -114,7 +114,7 @@ discard block |
||
| 114 | 114 | ->from('addressbooks') |
| 115 | 115 | ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
| 116 | 116 | |
| 117 | - return (int)$query->execute()->fetchColumn(); |
|
| 117 | + return (int) $query->execute()->fetchColumn(); |
|
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | /** |
@@ -145,15 +145,15 @@ discard block |
||
| 145 | 145 | $addressBooks = []; |
| 146 | 146 | |
| 147 | 147 | $result = $query->execute(); |
| 148 | - while($row = $result->fetch()) { |
|
| 148 | + while ($row = $result->fetch()) { |
|
| 149 | 149 | $addressBooks[$row['id']] = [ |
| 150 | 150 | 'id' => $row['id'], |
| 151 | 151 | 'uri' => $row['uri'], |
| 152 | 152 | 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
| 153 | 153 | '{DAV:}displayname' => $row['displayname'], |
| 154 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 154 | + '{'.Plugin::NS_CARDDAV.'}addressbook-description' => $row['description'], |
|
| 155 | 155 | '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
| 156 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 156 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ? $row['synctoken'] : '0', |
|
| 157 | 157 | ]; |
| 158 | 158 | |
| 159 | 159 | $this->addOwnerPrincipal($addressBooks[$row['id']]); |
@@ -165,7 +165,7 @@ discard block |
||
| 165 | 165 | $principals = array_map(function($principal) { |
| 166 | 166 | return urldecode($principal); |
| 167 | 167 | }, $principals); |
| 168 | - $principals[]= $principalUri; |
|
| 168 | + $principals[] = $principalUri; |
|
| 169 | 169 | |
| 170 | 170 | $query = $this->db->getQueryBuilder(); |
| 171 | 171 | $result = $query->select(['a.id', 'a.uri', 'a.displayname', 'a.principaluri', 'a.description', 'a.synctoken', 's.access']) |
@@ -177,8 +177,8 @@ discard block |
||
| 177 | 177 | ->setParameter('principaluri', $principals, IQueryBuilder::PARAM_STR_ARRAY) |
| 178 | 178 | ->execute(); |
| 179 | 179 | |
| 180 | - $readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'; |
|
| 181 | - while($row = $result->fetch()) { |
|
| 180 | + $readOnlyPropertyName = '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}read-only'; |
|
| 181 | + while ($row = $result->fetch()) { |
|
| 182 | 182 | if ($row['principaluri'] === $principalUri) { |
| 183 | 183 | continue; |
| 184 | 184 | } |
@@ -197,18 +197,18 @@ discard block |
||
| 197 | 197 | } |
| 198 | 198 | |
| 199 | 199 | list(, $name) = \Sabre\Uri\split($row['principaluri']); |
| 200 | - $uri = $row['uri'] . '_shared_by_' . $name; |
|
| 201 | - $displayName = $row['displayname'] . ' (' . $this->getUserDisplayName($name) . ')'; |
|
| 200 | + $uri = $row['uri'].'_shared_by_'.$name; |
|
| 201 | + $displayName = $row['displayname'].' ('.$this->getUserDisplayName($name).')'; |
|
| 202 | 202 | |
| 203 | 203 | $addressBooks[$row['id']] = [ |
| 204 | 204 | 'id' => $row['id'], |
| 205 | 205 | 'uri' => $uri, |
| 206 | 206 | 'principaluri' => $principalUriOriginal, |
| 207 | 207 | '{DAV:}displayname' => $displayName, |
| 208 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 208 | + '{'.Plugin::NS_CARDDAV.'}addressbook-description' => $row['description'], |
|
| 209 | 209 | '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
| 210 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 211 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $row['principaluri'], |
|
| 210 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ? $row['synctoken'] : '0', |
|
| 211 | + '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}owner-principal' => $row['principaluri'], |
|
| 212 | 212 | $readOnlyPropertyName => $readOnly, |
| 213 | 213 | ]; |
| 214 | 214 | |
@@ -229,15 +229,15 @@ discard block |
||
| 229 | 229 | $addressBooks = []; |
| 230 | 230 | |
| 231 | 231 | $result = $query->execute(); |
| 232 | - while($row = $result->fetch()) { |
|
| 232 | + while ($row = $result->fetch()) { |
|
| 233 | 233 | $addressBooks[$row['id']] = [ |
| 234 | 234 | 'id' => $row['id'], |
| 235 | 235 | 'uri' => $row['uri'], |
| 236 | 236 | 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
| 237 | 237 | '{DAV:}displayname' => $row['displayname'], |
| 238 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 238 | + '{'.Plugin::NS_CARDDAV.'}addressbook-description' => $row['description'], |
|
| 239 | 239 | '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
| 240 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 240 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ? $row['synctoken'] : '0', |
|
| 241 | 241 | ]; |
| 242 | 242 | |
| 243 | 243 | $this->addOwnerPrincipal($addressBooks[$row['id']]); |
@@ -282,9 +282,9 @@ discard block |
||
| 282 | 282 | 'uri' => $row['uri'], |
| 283 | 283 | 'principaluri' => $row['principaluri'], |
| 284 | 284 | '{DAV:}displayname' => $row['displayname'], |
| 285 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 285 | + '{'.Plugin::NS_CARDDAV.'}addressbook-description' => $row['description'], |
|
| 286 | 286 | '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
| 287 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 287 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ? $row['synctoken'] : '0', |
|
| 288 | 288 | ]; |
| 289 | 289 | |
| 290 | 290 | $this->addOwnerPrincipal($addressBook); |
@@ -316,9 +316,9 @@ discard block |
||
| 316 | 316 | 'uri' => $row['uri'], |
| 317 | 317 | 'principaluri' => $row['principaluri'], |
| 318 | 318 | '{DAV:}displayname' => $row['displayname'], |
| 319 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 319 | + '{'.Plugin::NS_CARDDAV.'}addressbook-description' => $row['description'], |
|
| 320 | 320 | '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
| 321 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 321 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ? $row['synctoken'] : '0', |
|
| 322 | 322 | ]; |
| 323 | 323 | |
| 324 | 324 | $this->addOwnerPrincipal($addressBook); |
@@ -345,7 +345,7 @@ discard block |
||
| 345 | 345 | function updateAddressBook($addressBookId, \Sabre\DAV\PropPatch $propPatch) { |
| 346 | 346 | $supportedProperties = [ |
| 347 | 347 | '{DAV:}displayname', |
| 348 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description', |
|
| 348 | + '{'.Plugin::NS_CARDDAV.'}addressbook-description', |
|
| 349 | 349 | ]; |
| 350 | 350 | |
| 351 | 351 | /** |
@@ -354,13 +354,13 @@ discard block |
||
| 354 | 354 | $propPatch->handle($supportedProperties, function($mutations) use ($addressBookId) { |
| 355 | 355 | |
| 356 | 356 | $updates = []; |
| 357 | - foreach($mutations as $property=>$newValue) { |
|
| 357 | + foreach ($mutations as $property=>$newValue) { |
|
| 358 | 358 | |
| 359 | - switch($property) { |
|
| 359 | + switch ($property) { |
|
| 360 | 360 | case '{DAV:}displayname' : |
| 361 | 361 | $updates['displayname'] = $newValue; |
| 362 | 362 | break; |
| 363 | - case '{' . Plugin::NS_CARDDAV . '}addressbook-description' : |
|
| 363 | + case '{'.Plugin::NS_CARDDAV.'}addressbook-description' : |
|
| 364 | 364 | $updates['description'] = $newValue; |
| 365 | 365 | break; |
| 366 | 366 | } |
@@ -368,7 +368,7 @@ discard block |
||
| 368 | 368 | $query = $this->db->getQueryBuilder(); |
| 369 | 369 | $query->update('addressbooks'); |
| 370 | 370 | |
| 371 | - foreach($updates as $key=>$value) { |
|
| 371 | + foreach ($updates as $key=>$value) { |
|
| 372 | 372 | $query->set($key, $query->createNamedParameter($value)); |
| 373 | 373 | } |
| 374 | 374 | $query->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) |
@@ -399,24 +399,24 @@ discard block |
||
| 399 | 399 | 'synctoken' => 1 |
| 400 | 400 | ]; |
| 401 | 401 | |
| 402 | - foreach($properties as $property=>$newValue) { |
|
| 402 | + foreach ($properties as $property=>$newValue) { |
|
| 403 | 403 | |
| 404 | - switch($property) { |
|
| 404 | + switch ($property) { |
|
| 405 | 405 | case '{DAV:}displayname' : |
| 406 | 406 | $values['displayname'] = $newValue; |
| 407 | 407 | break; |
| 408 | - case '{' . Plugin::NS_CARDDAV . '}addressbook-description' : |
|
| 408 | + case '{'.Plugin::NS_CARDDAV.'}addressbook-description' : |
|
| 409 | 409 | $values['description'] = $newValue; |
| 410 | 410 | break; |
| 411 | 411 | default : |
| 412 | - throw new BadRequest('Unknown property: ' . $property); |
|
| 412 | + throw new BadRequest('Unknown property: '.$property); |
|
| 413 | 413 | } |
| 414 | 414 | |
| 415 | 415 | } |
| 416 | 416 | |
| 417 | 417 | // Fallback to make sure the displayname is set. Some clients may refuse |
| 418 | 418 | // to work with addressbooks not having a displayname. |
| 419 | - if(is_null($values['displayname'])) { |
|
| 419 | + if (is_null($values['displayname'])) { |
|
| 420 | 420 | $values['displayname'] = $url; |
| 421 | 421 | } |
| 422 | 422 | |
@@ -494,8 +494,8 @@ discard block |
||
| 494 | 494 | $cards = []; |
| 495 | 495 | |
| 496 | 496 | $result = $query->execute(); |
| 497 | - while($row = $result->fetch()) { |
|
| 498 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 497 | + while ($row = $result->fetch()) { |
|
| 498 | + $row['etag'] = '"'.$row['etag'].'"'; |
|
| 499 | 499 | $row['carddata'] = $this->readBlob($row['carddata']); |
| 500 | 500 | $cards[] = $row; |
| 501 | 501 | } |
@@ -529,7 +529,7 @@ discard block |
||
| 529 | 529 | if (!$row) { |
| 530 | 530 | return false; |
| 531 | 531 | } |
| 532 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 532 | + $row['etag'] = '"'.$row['etag'].'"'; |
|
| 533 | 533 | $row['carddata'] = $this->readBlob($row['carddata']); |
| 534 | 534 | |
| 535 | 535 | return $row; |
@@ -566,7 +566,7 @@ discard block |
||
| 566 | 566 | $result = $query->execute(); |
| 567 | 567 | |
| 568 | 568 | while ($row = $result->fetch()) { |
| 569 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 569 | + $row['etag'] = '"'.$row['etag'].'"'; |
|
| 570 | 570 | $row['carddata'] = $this->readBlob($row['carddata']); |
| 571 | 571 | $cards[] = $row; |
| 572 | 572 | } |
@@ -624,7 +624,7 @@ discard block |
||
| 624 | 624 | 'cardUri' => $cardUri, |
| 625 | 625 | 'cardData' => $cardData])); |
| 626 | 626 | |
| 627 | - return '"' . $etag . '"'; |
|
| 627 | + return '"'.$etag.'"'; |
|
| 628 | 628 | } |
| 629 | 629 | |
| 630 | 630 | /** |
@@ -674,7 +674,7 @@ discard block |
||
| 674 | 674 | 'cardUri' => $cardUri, |
| 675 | 675 | 'cardData' => $cardData])); |
| 676 | 676 | |
| 677 | - return '"' . $etag . '"'; |
|
| 677 | + return '"'.$etag.'"'; |
|
| 678 | 678 | } |
| 679 | 679 | |
| 680 | 680 | /** |
@@ -772,7 +772,7 @@ discard block |
||
| 772 | 772 | function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel, $limit = null) { |
| 773 | 773 | // Current synctoken |
| 774 | 774 | $stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*addressbooks` WHERE `id` = ?'); |
| 775 | - $stmt->execute([ $addressBookId ]); |
|
| 775 | + $stmt->execute([$addressBookId]); |
|
| 776 | 776 | $currentToken = $stmt->fetchColumn(0); |
| 777 | 777 | |
| 778 | 778 | if (is_null($currentToken)) return null; |
@@ -787,8 +787,8 @@ discard block |
||
| 787 | 787 | if ($syncToken) { |
| 788 | 788 | |
| 789 | 789 | $query = "SELECT `uri`, `operation` FROM `*PREFIX*addressbookchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `addressbookid` = ? ORDER BY `synctoken`"; |
| 790 | - if ($limit>0) { |
|
| 791 | - $query .= " `LIMIT` " . (int)$limit; |
|
| 790 | + if ($limit > 0) { |
|
| 791 | + $query .= " `LIMIT` ".(int) $limit; |
|
| 792 | 792 | } |
| 793 | 793 | |
| 794 | 794 | // Fetching all changes |
@@ -799,15 +799,15 @@ discard block |
||
| 799 | 799 | |
| 800 | 800 | // This loop ensures that any duplicates are overwritten, only the |
| 801 | 801 | // last change on a node is relevant. |
| 802 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 802 | + while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 803 | 803 | |
| 804 | 804 | $changes[$row['uri']] = $row['operation']; |
| 805 | 805 | |
| 806 | 806 | } |
| 807 | 807 | |
| 808 | - foreach($changes as $uri => $operation) { |
|
| 808 | + foreach ($changes as $uri => $operation) { |
|
| 809 | 809 | |
| 810 | - switch($operation) { |
|
| 810 | + switch ($operation) { |
|
| 811 | 811 | case 1: |
| 812 | 812 | $result['added'][] = $uri; |
| 813 | 813 | break; |
@@ -893,7 +893,7 @@ discard block |
||
| 893 | 893 | |
| 894 | 894 | // No need for like when the pattern is empty |
| 895 | 895 | if ('' !== $pattern) { |
| 896 | - $query2->andWhere($query2->expr()->ilike('cp.value', $query->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%'))); |
|
| 896 | + $query2->andWhere($query2->expr()->ilike('cp.value', $query->createNamedParameter('%'.$this->db->escapeLikeParameter($pattern).'%'))); |
|
| 897 | 897 | } |
| 898 | 898 | |
| 899 | 899 | $query->select('c.carddata', 'c.uri')->from($this->dbCardsTable, 'c') |
@@ -946,7 +946,7 @@ discard block |
||
| 946 | 946 | $result->closeCursor(); |
| 947 | 947 | |
| 948 | 948 | if (!isset($uri['uri'])) { |
| 949 | - throw new \InvalidArgumentException('Card does not exists: ' . $id); |
|
| 949 | + throw new \InvalidArgumentException('Card does not exists: '.$id); |
|
| 950 | 950 | } |
| 951 | 951 | |
| 952 | 952 | return $uri['uri']; |
@@ -1018,11 +1018,11 @@ discard block |
||
| 1018 | 1018 | ); |
| 1019 | 1019 | |
| 1020 | 1020 | foreach ($vCard->children() as $property) { |
| 1021 | - if(!in_array($property->name, self::$indexProperties)) { |
|
| 1021 | + if (!in_array($property->name, self::$indexProperties)) { |
|
| 1022 | 1022 | continue; |
| 1023 | 1023 | } |
| 1024 | 1024 | $preferred = 0; |
| 1025 | - foreach($property->parameters as $parameter) { |
|
| 1025 | + foreach ($property->parameters as $parameter) { |
|
| 1026 | 1026 | if ($parameter->name === 'TYPE' && strtoupper($parameter->getValue()) === 'PREF') { |
| 1027 | 1027 | $preferred = 1; |
| 1028 | 1028 | break; |
@@ -1077,10 +1077,10 @@ discard block |
||
| 1077 | 1077 | $result->closeCursor(); |
| 1078 | 1078 | |
| 1079 | 1079 | if (!isset($cardIds['id'])) { |
| 1080 | - throw new \InvalidArgumentException('Card does not exists: ' . $uri); |
|
| 1080 | + throw new \InvalidArgumentException('Card does not exists: '.$uri); |
|
| 1081 | 1081 | } |
| 1082 | 1082 | |
| 1083 | - return (int)$cardIds['id']; |
|
| 1083 | + return (int) $cardIds['id']; |
|
| 1084 | 1084 | } |
| 1085 | 1085 | |
| 1086 | 1086 | /** |
@@ -1105,8 +1105,8 @@ discard block |
||
| 1105 | 1105 | } |
| 1106 | 1106 | |
| 1107 | 1107 | private function addOwnerPrincipal(&$addressbookInfo) { |
| 1108 | - $ownerPrincipalKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'; |
|
| 1109 | - $displaynameKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'; |
|
| 1108 | + $ownerPrincipalKey = '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}owner-principal'; |
|
| 1109 | + $displaynameKey = '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD.'}owner-displayname'; |
|
| 1110 | 1110 | if (isset($addressbookInfo[$ownerPrincipalKey])) { |
| 1111 | 1111 | $uri = $addressbookInfo[$ownerPrincipalKey]; |
| 1112 | 1112 | } else { |
@@ -53,1114 +53,1114 @@ |
||
| 53 | 53 | |
| 54 | 54 | class CardDavBackend implements BackendInterface, SyncSupport { |
| 55 | 55 | |
| 56 | - const PERSONAL_ADDRESSBOOK_URI = 'contacts'; |
|
| 57 | - const PERSONAL_ADDRESSBOOK_NAME = 'Contacts'; |
|
| 58 | - |
|
| 59 | - /** @var Principal */ |
|
| 60 | - private $principalBackend; |
|
| 61 | - |
|
| 62 | - /** @var string */ |
|
| 63 | - private $dbCardsTable = 'cards'; |
|
| 64 | - |
|
| 65 | - /** @var string */ |
|
| 66 | - private $dbCardsPropertiesTable = 'cards_properties'; |
|
| 67 | - |
|
| 68 | - /** @var IDBConnection */ |
|
| 69 | - private $db; |
|
| 70 | - |
|
| 71 | - /** @var Backend */ |
|
| 72 | - private $sharingBackend; |
|
| 73 | - |
|
| 74 | - /** @var array properties to index */ |
|
| 75 | - public static $indexProperties = array( |
|
| 76 | - 'BDAY', 'UID', 'N', 'FN', 'TITLE', 'ROLE', 'NOTE', 'NICKNAME', |
|
| 77 | - 'ORG', 'CATEGORIES', 'EMAIL', 'TEL', 'IMPP', 'ADR', 'URL', 'GEO', 'CLOUD'); |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * @var string[] Map of uid => display name |
|
| 81 | - */ |
|
| 82 | - protected $userDisplayNames; |
|
| 83 | - |
|
| 84 | - /** @var IUserManager */ |
|
| 85 | - private $userManager; |
|
| 86 | - |
|
| 87 | - /** @var EventDispatcherInterface */ |
|
| 88 | - private $dispatcher; |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * CardDavBackend constructor. |
|
| 92 | - * |
|
| 93 | - * @param IDBConnection $db |
|
| 94 | - * @param Principal $principalBackend |
|
| 95 | - * @param IUserManager $userManager |
|
| 96 | - * @param IGroupManager $groupManager |
|
| 97 | - * @param EventDispatcherInterface $dispatcher |
|
| 98 | - */ |
|
| 99 | - public function __construct(IDBConnection $db, |
|
| 100 | - Principal $principalBackend, |
|
| 101 | - IUserManager $userManager, |
|
| 102 | - IGroupManager $groupManager, |
|
| 103 | - EventDispatcherInterface $dispatcher) { |
|
| 104 | - $this->db = $db; |
|
| 105 | - $this->principalBackend = $principalBackend; |
|
| 106 | - $this->userManager = $userManager; |
|
| 107 | - $this->dispatcher = $dispatcher; |
|
| 108 | - $this->sharingBackend = new Backend($this->db, $this->userManager, $groupManager, $principalBackend, 'addressbook'); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Return the number of address books for a principal |
|
| 113 | - * |
|
| 114 | - * @param $principalUri |
|
| 115 | - * @return int |
|
| 116 | - */ |
|
| 117 | - public function getAddressBooksForUserCount($principalUri) { |
|
| 118 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 119 | - $query = $this->db->getQueryBuilder(); |
|
| 120 | - $query->select($query->func()->count('*')) |
|
| 121 | - ->from('addressbooks') |
|
| 122 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 123 | - |
|
| 124 | - return (int)$query->execute()->fetchColumn(); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * Returns the list of address books for a specific user. |
|
| 129 | - * |
|
| 130 | - * Every addressbook should have the following properties: |
|
| 131 | - * id - an arbitrary unique id |
|
| 132 | - * uri - the 'basename' part of the url |
|
| 133 | - * principaluri - Same as the passed parameter |
|
| 134 | - * |
|
| 135 | - * Any additional clark-notation property may be passed besides this. Some |
|
| 136 | - * common ones are : |
|
| 137 | - * {DAV:}displayname |
|
| 138 | - * {urn:ietf:params:xml:ns:carddav}addressbook-description |
|
| 139 | - * {http://calendarserver.org/ns/}getctag |
|
| 140 | - * |
|
| 141 | - * @param string $principalUri |
|
| 142 | - * @return array |
|
| 143 | - */ |
|
| 144 | - function getAddressBooksForUser($principalUri) { |
|
| 145 | - $principalUriOriginal = $principalUri; |
|
| 146 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 147 | - $query = $this->db->getQueryBuilder(); |
|
| 148 | - $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 149 | - ->from('addressbooks') |
|
| 150 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 151 | - |
|
| 152 | - $addressBooks = []; |
|
| 153 | - |
|
| 154 | - $result = $query->execute(); |
|
| 155 | - while($row = $result->fetch()) { |
|
| 156 | - $addressBooks[$row['id']] = [ |
|
| 157 | - 'id' => $row['id'], |
|
| 158 | - 'uri' => $row['uri'], |
|
| 159 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
|
| 160 | - '{DAV:}displayname' => $row['displayname'], |
|
| 161 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 162 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 163 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 164 | - ]; |
|
| 165 | - |
|
| 166 | - $this->addOwnerPrincipal($addressBooks[$row['id']]); |
|
| 167 | - } |
|
| 168 | - $result->closeCursor(); |
|
| 169 | - |
|
| 170 | - // query for shared calendars |
|
| 171 | - $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); |
|
| 172 | - $principals = array_map(function($principal) { |
|
| 173 | - return urldecode($principal); |
|
| 174 | - }, $principals); |
|
| 175 | - $principals[]= $principalUri; |
|
| 176 | - |
|
| 177 | - $query = $this->db->getQueryBuilder(); |
|
| 178 | - $result = $query->select(['a.id', 'a.uri', 'a.displayname', 'a.principaluri', 'a.description', 'a.synctoken', 's.access']) |
|
| 179 | - ->from('dav_shares', 's') |
|
| 180 | - ->join('s', 'addressbooks', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
| 181 | - ->where($query->expr()->in('s.principaluri', $query->createParameter('principaluri'))) |
|
| 182 | - ->andWhere($query->expr()->eq('s.type', $query->createParameter('type'))) |
|
| 183 | - ->setParameter('type', 'addressbook') |
|
| 184 | - ->setParameter('principaluri', $principals, IQueryBuilder::PARAM_STR_ARRAY) |
|
| 185 | - ->execute(); |
|
| 186 | - |
|
| 187 | - $readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'; |
|
| 188 | - while($row = $result->fetch()) { |
|
| 189 | - if ($row['principaluri'] === $principalUri) { |
|
| 190 | - continue; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - $readOnly = (int) $row['access'] === Backend::ACCESS_READ; |
|
| 194 | - if (isset($addressBooks[$row['id']])) { |
|
| 195 | - if ($readOnly) { |
|
| 196 | - // New share can not have more permissions then the old one. |
|
| 197 | - continue; |
|
| 198 | - } |
|
| 199 | - if (isset($addressBooks[$row['id']][$readOnlyPropertyName]) && |
|
| 200 | - $addressBooks[$row['id']][$readOnlyPropertyName] === 0) { |
|
| 201 | - // Old share is already read-write, no more permissions can be gained |
|
| 202 | - continue; |
|
| 203 | - } |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - list(, $name) = \Sabre\Uri\split($row['principaluri']); |
|
| 207 | - $uri = $row['uri'] . '_shared_by_' . $name; |
|
| 208 | - $displayName = $row['displayname'] . ' (' . $this->getUserDisplayName($name) . ')'; |
|
| 209 | - |
|
| 210 | - $addressBooks[$row['id']] = [ |
|
| 211 | - 'id' => $row['id'], |
|
| 212 | - 'uri' => $uri, |
|
| 213 | - 'principaluri' => $principalUriOriginal, |
|
| 214 | - '{DAV:}displayname' => $displayName, |
|
| 215 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 216 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 217 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 218 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $row['principaluri'], |
|
| 219 | - $readOnlyPropertyName => $readOnly, |
|
| 220 | - ]; |
|
| 221 | - |
|
| 222 | - $this->addOwnerPrincipal($addressBooks[$row['id']]); |
|
| 223 | - } |
|
| 224 | - $result->closeCursor(); |
|
| 225 | - |
|
| 226 | - return array_values($addressBooks); |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - public function getUsersOwnAddressBooks($principalUri) { |
|
| 230 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 231 | - $query = $this->db->getQueryBuilder(); |
|
| 232 | - $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 233 | - ->from('addressbooks') |
|
| 234 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 235 | - |
|
| 236 | - $addressBooks = []; |
|
| 237 | - |
|
| 238 | - $result = $query->execute(); |
|
| 239 | - while($row = $result->fetch()) { |
|
| 240 | - $addressBooks[$row['id']] = [ |
|
| 241 | - 'id' => $row['id'], |
|
| 242 | - 'uri' => $row['uri'], |
|
| 243 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
|
| 244 | - '{DAV:}displayname' => $row['displayname'], |
|
| 245 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 246 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 247 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 248 | - ]; |
|
| 249 | - |
|
| 250 | - $this->addOwnerPrincipal($addressBooks[$row['id']]); |
|
| 251 | - } |
|
| 252 | - $result->closeCursor(); |
|
| 253 | - |
|
| 254 | - return array_values($addressBooks); |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - private function getUserDisplayName($uid) { |
|
| 258 | - if (!isset($this->userDisplayNames[$uid])) { |
|
| 259 | - $user = $this->userManager->get($uid); |
|
| 260 | - |
|
| 261 | - if ($user instanceof IUser) { |
|
| 262 | - $this->userDisplayNames[$uid] = $user->getDisplayName(); |
|
| 263 | - } else { |
|
| 264 | - $this->userDisplayNames[$uid] = $uid; |
|
| 265 | - } |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - return $this->userDisplayNames[$uid]; |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - /** |
|
| 272 | - * @param int $addressBookId |
|
| 273 | - */ |
|
| 274 | - public function getAddressBookById($addressBookId) { |
|
| 275 | - $query = $this->db->getQueryBuilder(); |
|
| 276 | - $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 277 | - ->from('addressbooks') |
|
| 278 | - ->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) |
|
| 279 | - ->execute(); |
|
| 280 | - |
|
| 281 | - $row = $result->fetch(); |
|
| 282 | - $result->closeCursor(); |
|
| 283 | - if ($row === false) { |
|
| 284 | - return null; |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - $addressBook = [ |
|
| 288 | - 'id' => $row['id'], |
|
| 289 | - 'uri' => $row['uri'], |
|
| 290 | - 'principaluri' => $row['principaluri'], |
|
| 291 | - '{DAV:}displayname' => $row['displayname'], |
|
| 292 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 293 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 294 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 295 | - ]; |
|
| 296 | - |
|
| 297 | - $this->addOwnerPrincipal($addressBook); |
|
| 298 | - |
|
| 299 | - return $addressBook; |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - /** |
|
| 303 | - * @param $addressBookUri |
|
| 304 | - * @return array|null |
|
| 305 | - */ |
|
| 306 | - public function getAddressBooksByUri($principal, $addressBookUri) { |
|
| 307 | - $query = $this->db->getQueryBuilder(); |
|
| 308 | - $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 309 | - ->from('addressbooks') |
|
| 310 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($addressBookUri))) |
|
| 311 | - ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal))) |
|
| 312 | - ->setMaxResults(1) |
|
| 313 | - ->execute(); |
|
| 314 | - |
|
| 315 | - $row = $result->fetch(); |
|
| 316 | - $result->closeCursor(); |
|
| 317 | - if ($row === false) { |
|
| 318 | - return null; |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - $addressBook = [ |
|
| 322 | - 'id' => $row['id'], |
|
| 323 | - 'uri' => $row['uri'], |
|
| 324 | - 'principaluri' => $row['principaluri'], |
|
| 325 | - '{DAV:}displayname' => $row['displayname'], |
|
| 326 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 327 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 328 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 329 | - ]; |
|
| 330 | - |
|
| 331 | - $this->addOwnerPrincipal($addressBook); |
|
| 332 | - |
|
| 333 | - return $addressBook; |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - /** |
|
| 337 | - * Updates properties for an address book. |
|
| 338 | - * |
|
| 339 | - * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
| 340 | - * To do the actual updates, you must tell this object which properties |
|
| 341 | - * you're going to process with the handle() method. |
|
| 342 | - * |
|
| 343 | - * Calling the handle method is like telling the PropPatch object "I |
|
| 344 | - * promise I can handle updating this property". |
|
| 345 | - * |
|
| 346 | - * Read the PropPatch documentation for more info and examples. |
|
| 347 | - * |
|
| 348 | - * @param string $addressBookId |
|
| 349 | - * @param \Sabre\DAV\PropPatch $propPatch |
|
| 350 | - * @return void |
|
| 351 | - */ |
|
| 352 | - function updateAddressBook($addressBookId, \Sabre\DAV\PropPatch $propPatch) { |
|
| 353 | - $supportedProperties = [ |
|
| 354 | - '{DAV:}displayname', |
|
| 355 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description', |
|
| 356 | - ]; |
|
| 357 | - |
|
| 358 | - /** |
|
| 359 | - * @suppress SqlInjectionChecker |
|
| 360 | - */ |
|
| 361 | - $propPatch->handle($supportedProperties, function($mutations) use ($addressBookId) { |
|
| 362 | - |
|
| 363 | - $updates = []; |
|
| 364 | - foreach($mutations as $property=>$newValue) { |
|
| 365 | - |
|
| 366 | - switch($property) { |
|
| 367 | - case '{DAV:}displayname' : |
|
| 368 | - $updates['displayname'] = $newValue; |
|
| 369 | - break; |
|
| 370 | - case '{' . Plugin::NS_CARDDAV . '}addressbook-description' : |
|
| 371 | - $updates['description'] = $newValue; |
|
| 372 | - break; |
|
| 373 | - } |
|
| 374 | - } |
|
| 375 | - $query = $this->db->getQueryBuilder(); |
|
| 376 | - $query->update('addressbooks'); |
|
| 377 | - |
|
| 378 | - foreach($updates as $key=>$value) { |
|
| 379 | - $query->set($key, $query->createNamedParameter($value)); |
|
| 380 | - } |
|
| 381 | - $query->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) |
|
| 382 | - ->execute(); |
|
| 383 | - |
|
| 384 | - $this->addChange($addressBookId, "", 2); |
|
| 385 | - |
|
| 386 | - return true; |
|
| 387 | - |
|
| 388 | - }); |
|
| 389 | - } |
|
| 390 | - |
|
| 391 | - /** |
|
| 392 | - * Creates a new address book |
|
| 393 | - * |
|
| 394 | - * @param string $principalUri |
|
| 395 | - * @param string $url Just the 'basename' of the url. |
|
| 396 | - * @param array $properties |
|
| 397 | - * @return int |
|
| 398 | - * @throws BadRequest |
|
| 399 | - */ |
|
| 400 | - function createAddressBook($principalUri, $url, array $properties) { |
|
| 401 | - $values = [ |
|
| 402 | - 'displayname' => null, |
|
| 403 | - 'description' => null, |
|
| 404 | - 'principaluri' => $principalUri, |
|
| 405 | - 'uri' => $url, |
|
| 406 | - 'synctoken' => 1 |
|
| 407 | - ]; |
|
| 408 | - |
|
| 409 | - foreach($properties as $property=>$newValue) { |
|
| 410 | - |
|
| 411 | - switch($property) { |
|
| 412 | - case '{DAV:}displayname' : |
|
| 413 | - $values['displayname'] = $newValue; |
|
| 414 | - break; |
|
| 415 | - case '{' . Plugin::NS_CARDDAV . '}addressbook-description' : |
|
| 416 | - $values['description'] = $newValue; |
|
| 417 | - break; |
|
| 418 | - default : |
|
| 419 | - throw new BadRequest('Unknown property: ' . $property); |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - // Fallback to make sure the displayname is set. Some clients may refuse |
|
| 425 | - // to work with addressbooks not having a displayname. |
|
| 426 | - if(is_null($values['displayname'])) { |
|
| 427 | - $values['displayname'] = $url; |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - $query = $this->db->getQueryBuilder(); |
|
| 431 | - $query->insert('addressbooks') |
|
| 432 | - ->values([ |
|
| 433 | - 'uri' => $query->createParameter('uri'), |
|
| 434 | - 'displayname' => $query->createParameter('displayname'), |
|
| 435 | - 'description' => $query->createParameter('description'), |
|
| 436 | - 'principaluri' => $query->createParameter('principaluri'), |
|
| 437 | - 'synctoken' => $query->createParameter('synctoken'), |
|
| 438 | - ]) |
|
| 439 | - ->setParameters($values) |
|
| 440 | - ->execute(); |
|
| 441 | - |
|
| 442 | - return $query->getLastInsertId(); |
|
| 443 | - } |
|
| 444 | - |
|
| 445 | - /** |
|
| 446 | - * Deletes an entire addressbook and all its contents |
|
| 447 | - * |
|
| 448 | - * @param mixed $addressBookId |
|
| 449 | - * @return void |
|
| 450 | - */ |
|
| 451 | - function deleteAddressBook($addressBookId) { |
|
| 452 | - $query = $this->db->getQueryBuilder(); |
|
| 453 | - $query->delete('cards') |
|
| 454 | - ->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid'))) |
|
| 455 | - ->setParameter('addressbookid', $addressBookId) |
|
| 456 | - ->execute(); |
|
| 457 | - |
|
| 458 | - $query->delete('addressbookchanges') |
|
| 459 | - ->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid'))) |
|
| 460 | - ->setParameter('addressbookid', $addressBookId) |
|
| 461 | - ->execute(); |
|
| 462 | - |
|
| 463 | - $query->delete('addressbooks') |
|
| 464 | - ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
| 465 | - ->setParameter('id', $addressBookId) |
|
| 466 | - ->execute(); |
|
| 467 | - |
|
| 468 | - $this->sharingBackend->deleteAllShares($addressBookId); |
|
| 469 | - |
|
| 470 | - $query->delete($this->dbCardsPropertiesTable) |
|
| 471 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 472 | - ->execute(); |
|
| 473 | - |
|
| 474 | - } |
|
| 475 | - |
|
| 476 | - /** |
|
| 477 | - * Returns all cards for a specific addressbook id. |
|
| 478 | - * |
|
| 479 | - * This method should return the following properties for each card: |
|
| 480 | - * * carddata - raw vcard data |
|
| 481 | - * * uri - Some unique url |
|
| 482 | - * * lastmodified - A unix timestamp |
|
| 483 | - * |
|
| 484 | - * It's recommended to also return the following properties: |
|
| 485 | - * * etag - A unique etag. This must change every time the card changes. |
|
| 486 | - * * size - The size of the card in bytes. |
|
| 487 | - * |
|
| 488 | - * If these last two properties are provided, less time will be spent |
|
| 489 | - * calculating them. If they are specified, you can also ommit carddata. |
|
| 490 | - * This may speed up certain requests, especially with large cards. |
|
| 491 | - * |
|
| 492 | - * @param mixed $addressBookId |
|
| 493 | - * @return array |
|
| 494 | - */ |
|
| 495 | - function getCards($addressBookId) { |
|
| 496 | - $query = $this->db->getQueryBuilder(); |
|
| 497 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata', 'uid']) |
|
| 498 | - ->from('cards') |
|
| 499 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 500 | - |
|
| 501 | - $cards = []; |
|
| 502 | - |
|
| 503 | - $result = $query->execute(); |
|
| 504 | - while($row = $result->fetch()) { |
|
| 505 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 506 | - $row['carddata'] = $this->readBlob($row['carddata']); |
|
| 507 | - $cards[] = $row; |
|
| 508 | - } |
|
| 509 | - $result->closeCursor(); |
|
| 510 | - |
|
| 511 | - return $cards; |
|
| 512 | - } |
|
| 513 | - |
|
| 514 | - /** |
|
| 515 | - * Returns a specific card. |
|
| 516 | - * |
|
| 517 | - * The same set of properties must be returned as with getCards. The only |
|
| 518 | - * exception is that 'carddata' is absolutely required. |
|
| 519 | - * |
|
| 520 | - * If the card does not exist, you must return false. |
|
| 521 | - * |
|
| 522 | - * @param mixed $addressBookId |
|
| 523 | - * @param string $cardUri |
|
| 524 | - * @return array |
|
| 525 | - */ |
|
| 526 | - function getCard($addressBookId, $cardUri) { |
|
| 527 | - $query = $this->db->getQueryBuilder(); |
|
| 528 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata', 'uid']) |
|
| 529 | - ->from('cards') |
|
| 530 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 531 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 532 | - ->setMaxResults(1); |
|
| 533 | - |
|
| 534 | - $result = $query->execute(); |
|
| 535 | - $row = $result->fetch(); |
|
| 536 | - if (!$row) { |
|
| 537 | - return false; |
|
| 538 | - } |
|
| 539 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 540 | - $row['carddata'] = $this->readBlob($row['carddata']); |
|
| 541 | - |
|
| 542 | - return $row; |
|
| 543 | - } |
|
| 544 | - |
|
| 545 | - /** |
|
| 546 | - * Returns a list of cards. |
|
| 547 | - * |
|
| 548 | - * This method should work identical to getCard, but instead return all the |
|
| 549 | - * cards in the list as an array. |
|
| 550 | - * |
|
| 551 | - * If the backend supports this, it may allow for some speed-ups. |
|
| 552 | - * |
|
| 553 | - * @param mixed $addressBookId |
|
| 554 | - * @param string[] $uris |
|
| 555 | - * @return array |
|
| 556 | - */ |
|
| 557 | - function getMultipleCards($addressBookId, array $uris) { |
|
| 558 | - if (empty($uris)) { |
|
| 559 | - return []; |
|
| 560 | - } |
|
| 561 | - |
|
| 562 | - $chunks = array_chunk($uris, 100); |
|
| 563 | - $cards = []; |
|
| 564 | - |
|
| 565 | - $query = $this->db->getQueryBuilder(); |
|
| 566 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata', 'uid']) |
|
| 567 | - ->from('cards') |
|
| 568 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 569 | - ->andWhere($query->expr()->in('uri', $query->createParameter('uri'))); |
|
| 570 | - |
|
| 571 | - foreach ($chunks as $uris) { |
|
| 572 | - $query->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY); |
|
| 573 | - $result = $query->execute(); |
|
| 574 | - |
|
| 575 | - while ($row = $result->fetch()) { |
|
| 576 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 577 | - $row['carddata'] = $this->readBlob($row['carddata']); |
|
| 578 | - $cards[] = $row; |
|
| 579 | - } |
|
| 580 | - $result->closeCursor(); |
|
| 581 | - } |
|
| 582 | - return $cards; |
|
| 583 | - } |
|
| 584 | - |
|
| 585 | - /** |
|
| 586 | - * Creates a new card. |
|
| 587 | - * |
|
| 588 | - * The addressbook id will be passed as the first argument. This is the |
|
| 589 | - * same id as it is returned from the getAddressBooksForUser method. |
|
| 590 | - * |
|
| 591 | - * The cardUri is a base uri, and doesn't include the full path. The |
|
| 592 | - * cardData argument is the vcard body, and is passed as a string. |
|
| 593 | - * |
|
| 594 | - * It is possible to return an ETag from this method. This ETag is for the |
|
| 595 | - * newly created resource, and must be enclosed with double quotes (that |
|
| 596 | - * is, the string itself must contain the double quotes). |
|
| 597 | - * |
|
| 598 | - * You should only return the ETag if you store the carddata as-is. If a |
|
| 599 | - * subsequent GET request on the same card does not have the same body, |
|
| 600 | - * byte-by-byte and you did return an ETag here, clients tend to get |
|
| 601 | - * confused. |
|
| 602 | - * |
|
| 603 | - * If you don't return an ETag, you can just return null. |
|
| 604 | - * |
|
| 605 | - * @param mixed $addressBookId |
|
| 606 | - * @param string $cardUri |
|
| 607 | - * @param string $cardData |
|
| 608 | - * @return string |
|
| 609 | - */ |
|
| 610 | - function createCard($addressBookId, $cardUri, $cardData) { |
|
| 611 | - $etag = md5($cardData); |
|
| 612 | - $uid = $this->getUID($cardData); |
|
| 613 | - |
|
| 614 | - $q = $this->db->getQueryBuilder(); |
|
| 615 | - $q->select('uid') |
|
| 616 | - ->from('cards') |
|
| 617 | - ->where($q->expr()->eq('addressbookid', $q->createNamedParameter($addressBookId))) |
|
| 618 | - ->andWhere($q->expr()->eq('uid', $q->createNamedParameter($uid))) |
|
| 619 | - ->setMaxResults(1); |
|
| 620 | - $result = $q->execute(); |
|
| 621 | - $count = (bool) $result->fetchColumn(); |
|
| 622 | - $result->closeCursor(); |
|
| 623 | - if ($count) { |
|
| 624 | - throw new \Sabre\DAV\Exception\BadRequest('VCard object with uid already exists in this addressbook collection.'); |
|
| 625 | - } |
|
| 626 | - |
|
| 627 | - $query = $this->db->getQueryBuilder(); |
|
| 628 | - $query->insert('cards') |
|
| 629 | - ->values([ |
|
| 630 | - 'carddata' => $query->createNamedParameter($cardData, IQueryBuilder::PARAM_LOB), |
|
| 631 | - 'uri' => $query->createNamedParameter($cardUri), |
|
| 632 | - 'lastmodified' => $query->createNamedParameter(time()), |
|
| 633 | - 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
| 634 | - 'size' => $query->createNamedParameter(strlen($cardData)), |
|
| 635 | - 'etag' => $query->createNamedParameter($etag), |
|
| 636 | - 'uid' => $query->createNamedParameter($uid), |
|
| 637 | - ]) |
|
| 638 | - ->execute(); |
|
| 639 | - |
|
| 640 | - $this->addChange($addressBookId, $cardUri, 1); |
|
| 641 | - $this->updateProperties($addressBookId, $cardUri, $cardData); |
|
| 642 | - |
|
| 643 | - $this->dispatcher->dispatch('\OCA\DAV\CardDAV\CardDavBackend::createCard', |
|
| 644 | - new GenericEvent(null, [ |
|
| 645 | - 'addressBookId' => $addressBookId, |
|
| 646 | - 'cardUri' => $cardUri, |
|
| 647 | - 'cardData' => $cardData])); |
|
| 648 | - |
|
| 649 | - return '"' . $etag . '"'; |
|
| 650 | - } |
|
| 651 | - |
|
| 652 | - /** |
|
| 653 | - * Updates a card. |
|
| 654 | - * |
|
| 655 | - * The addressbook id will be passed as the first argument. This is the |
|
| 656 | - * same id as it is returned from the getAddressBooksForUser method. |
|
| 657 | - * |
|
| 658 | - * The cardUri is a base uri, and doesn't include the full path. The |
|
| 659 | - * cardData argument is the vcard body, and is passed as a string. |
|
| 660 | - * |
|
| 661 | - * It is possible to return an ETag from this method. This ETag should |
|
| 662 | - * match that of the updated resource, and must be enclosed with double |
|
| 663 | - * quotes (that is: the string itself must contain the actual quotes). |
|
| 664 | - * |
|
| 665 | - * You should only return the ETag if you store the carddata as-is. If a |
|
| 666 | - * subsequent GET request on the same card does not have the same body, |
|
| 667 | - * byte-by-byte and you did return an ETag here, clients tend to get |
|
| 668 | - * confused. |
|
| 669 | - * |
|
| 670 | - * If you don't return an ETag, you can just return null. |
|
| 671 | - * |
|
| 672 | - * @param mixed $addressBookId |
|
| 673 | - * @param string $cardUri |
|
| 674 | - * @param string $cardData |
|
| 675 | - * @return string |
|
| 676 | - */ |
|
| 677 | - function updateCard($addressBookId, $cardUri, $cardData) { |
|
| 678 | - |
|
| 679 | - $uid = $this->getUID($cardData); |
|
| 680 | - $etag = md5($cardData); |
|
| 681 | - $query = $this->db->getQueryBuilder(); |
|
| 682 | - $query->update('cards') |
|
| 683 | - ->set('carddata', $query->createNamedParameter($cardData, IQueryBuilder::PARAM_LOB)) |
|
| 684 | - ->set('lastmodified', $query->createNamedParameter(time())) |
|
| 685 | - ->set('size', $query->createNamedParameter(strlen($cardData))) |
|
| 686 | - ->set('etag', $query->createNamedParameter($etag)) |
|
| 687 | - ->set('uid', $query->createNamedParameter($uid)) |
|
| 688 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 689 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 690 | - ->execute(); |
|
| 691 | - |
|
| 692 | - $this->addChange($addressBookId, $cardUri, 2); |
|
| 693 | - $this->updateProperties($addressBookId, $cardUri, $cardData); |
|
| 694 | - |
|
| 695 | - $this->dispatcher->dispatch('\OCA\DAV\CardDAV\CardDavBackend::updateCard', |
|
| 696 | - new GenericEvent(null, [ |
|
| 697 | - 'addressBookId' => $addressBookId, |
|
| 698 | - 'cardUri' => $cardUri, |
|
| 699 | - 'cardData' => $cardData])); |
|
| 700 | - |
|
| 701 | - return '"' . $etag . '"'; |
|
| 702 | - } |
|
| 703 | - |
|
| 704 | - /** |
|
| 705 | - * Deletes a card |
|
| 706 | - * |
|
| 707 | - * @param mixed $addressBookId |
|
| 708 | - * @param string $cardUri |
|
| 709 | - * @return bool |
|
| 710 | - */ |
|
| 711 | - function deleteCard($addressBookId, $cardUri) { |
|
| 712 | - try { |
|
| 713 | - $cardId = $this->getCardId($addressBookId, $cardUri); |
|
| 714 | - } catch (\InvalidArgumentException $e) { |
|
| 715 | - $cardId = null; |
|
| 716 | - } |
|
| 717 | - $query = $this->db->getQueryBuilder(); |
|
| 718 | - $ret = $query->delete('cards') |
|
| 719 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 720 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 721 | - ->execute(); |
|
| 722 | - |
|
| 723 | - $this->addChange($addressBookId, $cardUri, 3); |
|
| 724 | - |
|
| 725 | - $this->dispatcher->dispatch('\OCA\DAV\CardDAV\CardDavBackend::deleteCard', |
|
| 726 | - new GenericEvent(null, [ |
|
| 727 | - 'addressBookId' => $addressBookId, |
|
| 728 | - 'cardUri' => $cardUri])); |
|
| 729 | - |
|
| 730 | - if ($ret === 1) { |
|
| 731 | - if ($cardId !== null) { |
|
| 732 | - $this->purgeProperties($addressBookId, $cardId); |
|
| 733 | - } |
|
| 734 | - return true; |
|
| 735 | - } |
|
| 736 | - |
|
| 737 | - return false; |
|
| 738 | - } |
|
| 739 | - |
|
| 740 | - /** |
|
| 741 | - * The getChanges method returns all the changes that have happened, since |
|
| 742 | - * the specified syncToken in the specified address book. |
|
| 743 | - * |
|
| 744 | - * This function should return an array, such as the following: |
|
| 745 | - * |
|
| 746 | - * [ |
|
| 747 | - * 'syncToken' => 'The current synctoken', |
|
| 748 | - * 'added' => [ |
|
| 749 | - * 'new.txt', |
|
| 750 | - * ], |
|
| 751 | - * 'modified' => [ |
|
| 752 | - * 'modified.txt', |
|
| 753 | - * ], |
|
| 754 | - * 'deleted' => [ |
|
| 755 | - * 'foo.php.bak', |
|
| 756 | - * 'old.txt' |
|
| 757 | - * ] |
|
| 758 | - * ]; |
|
| 759 | - * |
|
| 760 | - * The returned syncToken property should reflect the *current* syncToken |
|
| 761 | - * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
|
| 762 | - * property. This is needed here too, to ensure the operation is atomic. |
|
| 763 | - * |
|
| 764 | - * If the $syncToken argument is specified as null, this is an initial |
|
| 765 | - * sync, and all members should be reported. |
|
| 766 | - * |
|
| 767 | - * The modified property is an array of nodenames that have changed since |
|
| 768 | - * the last token. |
|
| 769 | - * |
|
| 770 | - * The deleted property is an array with nodenames, that have been deleted |
|
| 771 | - * from collection. |
|
| 772 | - * |
|
| 773 | - * The $syncLevel argument is basically the 'depth' of the report. If it's |
|
| 774 | - * 1, you only have to report changes that happened only directly in |
|
| 775 | - * immediate descendants. If it's 2, it should also include changes from |
|
| 776 | - * the nodes below the child collections. (grandchildren) |
|
| 777 | - * |
|
| 778 | - * The $limit argument allows a client to specify how many results should |
|
| 779 | - * be returned at most. If the limit is not specified, it should be treated |
|
| 780 | - * as infinite. |
|
| 781 | - * |
|
| 782 | - * If the limit (infinite or not) is higher than you're willing to return, |
|
| 783 | - * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
|
| 784 | - * |
|
| 785 | - * If the syncToken is expired (due to data cleanup) or unknown, you must |
|
| 786 | - * return null. |
|
| 787 | - * |
|
| 788 | - * The limit is 'suggestive'. You are free to ignore it. |
|
| 789 | - * |
|
| 790 | - * @param string $addressBookId |
|
| 791 | - * @param string $syncToken |
|
| 792 | - * @param int $syncLevel |
|
| 793 | - * @param int $limit |
|
| 794 | - * @return array |
|
| 795 | - */ |
|
| 796 | - function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel, $limit = null) { |
|
| 797 | - // Current synctoken |
|
| 798 | - $stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*addressbooks` WHERE `id` = ?'); |
|
| 799 | - $stmt->execute([ $addressBookId ]); |
|
| 800 | - $currentToken = $stmt->fetchColumn(0); |
|
| 801 | - |
|
| 802 | - if (is_null($currentToken)) return null; |
|
| 803 | - |
|
| 804 | - $result = [ |
|
| 805 | - 'syncToken' => $currentToken, |
|
| 806 | - 'added' => [], |
|
| 807 | - 'modified' => [], |
|
| 808 | - 'deleted' => [], |
|
| 809 | - ]; |
|
| 810 | - |
|
| 811 | - if ($syncToken) { |
|
| 812 | - |
|
| 813 | - $query = "SELECT `uri`, `operation` FROM `*PREFIX*addressbookchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `addressbookid` = ? ORDER BY `synctoken`"; |
|
| 814 | - if ($limit>0) { |
|
| 815 | - $query .= " `LIMIT` " . (int)$limit; |
|
| 816 | - } |
|
| 817 | - |
|
| 818 | - // Fetching all changes |
|
| 819 | - $stmt = $this->db->prepare($query); |
|
| 820 | - $stmt->execute([$syncToken, $currentToken, $addressBookId]); |
|
| 821 | - |
|
| 822 | - $changes = []; |
|
| 823 | - |
|
| 824 | - // This loop ensures that any duplicates are overwritten, only the |
|
| 825 | - // last change on a node is relevant. |
|
| 826 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 827 | - |
|
| 828 | - $changes[$row['uri']] = $row['operation']; |
|
| 829 | - |
|
| 830 | - } |
|
| 831 | - |
|
| 832 | - foreach($changes as $uri => $operation) { |
|
| 833 | - |
|
| 834 | - switch($operation) { |
|
| 835 | - case 1: |
|
| 836 | - $result['added'][] = $uri; |
|
| 837 | - break; |
|
| 838 | - case 2: |
|
| 839 | - $result['modified'][] = $uri; |
|
| 840 | - break; |
|
| 841 | - case 3: |
|
| 842 | - $result['deleted'][] = $uri; |
|
| 843 | - break; |
|
| 844 | - } |
|
| 845 | - |
|
| 846 | - } |
|
| 847 | - } else { |
|
| 848 | - // No synctoken supplied, this is the initial sync. |
|
| 849 | - $query = "SELECT `uri` FROM `*PREFIX*cards` WHERE `addressbookid` = ?"; |
|
| 850 | - $stmt = $this->db->prepare($query); |
|
| 851 | - $stmt->execute([$addressBookId]); |
|
| 852 | - |
|
| 853 | - $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
| 854 | - } |
|
| 855 | - return $result; |
|
| 856 | - } |
|
| 857 | - |
|
| 858 | - /** |
|
| 859 | - * Adds a change record to the addressbookchanges table. |
|
| 860 | - * |
|
| 861 | - * @param mixed $addressBookId |
|
| 862 | - * @param string $objectUri |
|
| 863 | - * @param int $operation 1 = add, 2 = modify, 3 = delete |
|
| 864 | - * @return void |
|
| 865 | - */ |
|
| 866 | - protected function addChange($addressBookId, $objectUri, $operation) { |
|
| 867 | - $sql = 'INSERT INTO `*PREFIX*addressbookchanges`(`uri`, `synctoken`, `addressbookid`, `operation`) SELECT ?, `synctoken`, ?, ? FROM `*PREFIX*addressbooks` WHERE `id` = ?'; |
|
| 868 | - $stmt = $this->db->prepare($sql); |
|
| 869 | - $stmt->execute([ |
|
| 870 | - $objectUri, |
|
| 871 | - $addressBookId, |
|
| 872 | - $operation, |
|
| 873 | - $addressBookId |
|
| 874 | - ]); |
|
| 875 | - $stmt = $this->db->prepare('UPDATE `*PREFIX*addressbooks` SET `synctoken` = `synctoken` + 1 WHERE `id` = ?'); |
|
| 876 | - $stmt->execute([ |
|
| 877 | - $addressBookId |
|
| 878 | - ]); |
|
| 879 | - } |
|
| 880 | - |
|
| 881 | - private function readBlob($cardData) { |
|
| 882 | - if (is_resource($cardData)) { |
|
| 883 | - return stream_get_contents($cardData); |
|
| 884 | - } |
|
| 885 | - |
|
| 886 | - return $cardData; |
|
| 887 | - } |
|
| 888 | - |
|
| 889 | - /** |
|
| 890 | - * @param IShareable $shareable |
|
| 891 | - * @param string[] $add |
|
| 892 | - * @param string[] $remove |
|
| 893 | - */ |
|
| 894 | - public function updateShares(IShareable $shareable, $add, $remove) { |
|
| 895 | - $this->sharingBackend->updateShares($shareable, $add, $remove); |
|
| 896 | - } |
|
| 897 | - |
|
| 898 | - /** |
|
| 899 | - * search contact |
|
| 900 | - * |
|
| 901 | - * @param int $addressBookId |
|
| 902 | - * @param string $pattern which should match within the $searchProperties |
|
| 903 | - * @param array $searchProperties defines the properties within the query pattern should match |
|
| 904 | - * @return array an array of contacts which are arrays of key-value-pairs |
|
| 905 | - */ |
|
| 906 | - public function search($addressBookId, $pattern, $searchProperties) { |
|
| 907 | - $query = $this->db->getQueryBuilder(); |
|
| 908 | - $query2 = $this->db->getQueryBuilder(); |
|
| 909 | - |
|
| 910 | - $query2->selectDistinct('cp.cardid')->from($this->dbCardsPropertiesTable, 'cp'); |
|
| 911 | - $query2->andWhere($query2->expr()->eq('cp.addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 912 | - $or = $query2->expr()->orX(); |
|
| 913 | - foreach ($searchProperties as $property) { |
|
| 914 | - $or->add($query2->expr()->eq('cp.name', $query->createNamedParameter($property))); |
|
| 915 | - } |
|
| 916 | - $query2->andWhere($or); |
|
| 917 | - |
|
| 918 | - // No need for like when the pattern is empty |
|
| 919 | - if ('' !== $pattern) { |
|
| 920 | - $query2->andWhere($query2->expr()->ilike('cp.value', $query->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%'))); |
|
| 921 | - } |
|
| 922 | - |
|
| 923 | - $query->select('c.carddata', 'c.uri')->from($this->dbCardsTable, 'c') |
|
| 924 | - ->where($query->expr()->in('c.id', $query->createFunction($query2->getSQL()))); |
|
| 925 | - |
|
| 926 | - $result = $query->execute(); |
|
| 927 | - $cards = $result->fetchAll(); |
|
| 928 | - |
|
| 929 | - $result->closeCursor(); |
|
| 930 | - |
|
| 931 | - return array_map(function($array) { |
|
| 932 | - $array['carddata'] = $this->readBlob($array['carddata']); |
|
| 933 | - return $array; |
|
| 934 | - }, $cards); |
|
| 935 | - } |
|
| 936 | - |
|
| 937 | - /** |
|
| 938 | - * @param int $bookId |
|
| 939 | - * @param string $name |
|
| 940 | - * @return array |
|
| 941 | - */ |
|
| 942 | - public function collectCardProperties($bookId, $name) { |
|
| 943 | - $query = $this->db->getQueryBuilder(); |
|
| 944 | - $result = $query->selectDistinct('value') |
|
| 945 | - ->from($this->dbCardsPropertiesTable) |
|
| 946 | - ->where($query->expr()->eq('name', $query->createNamedParameter($name))) |
|
| 947 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($bookId))) |
|
| 948 | - ->execute(); |
|
| 949 | - |
|
| 950 | - $all = $result->fetchAll(PDO::FETCH_COLUMN); |
|
| 951 | - $result->closeCursor(); |
|
| 952 | - |
|
| 953 | - return $all; |
|
| 954 | - } |
|
| 955 | - |
|
| 956 | - /** |
|
| 957 | - * get URI from a given contact |
|
| 958 | - * |
|
| 959 | - * @param int $id |
|
| 960 | - * @return string |
|
| 961 | - */ |
|
| 962 | - public function getCardUri($id) { |
|
| 963 | - $query = $this->db->getQueryBuilder(); |
|
| 964 | - $query->select('uri')->from($this->dbCardsTable) |
|
| 965 | - ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
| 966 | - ->setParameter('id', $id); |
|
| 967 | - |
|
| 968 | - $result = $query->execute(); |
|
| 969 | - $uri = $result->fetch(); |
|
| 970 | - $result->closeCursor(); |
|
| 971 | - |
|
| 972 | - if (!isset($uri['uri'])) { |
|
| 973 | - throw new \InvalidArgumentException('Card does not exists: ' . $id); |
|
| 974 | - } |
|
| 975 | - |
|
| 976 | - return $uri['uri']; |
|
| 977 | - } |
|
| 978 | - |
|
| 979 | - /** |
|
| 980 | - * return contact with the given URI |
|
| 981 | - * |
|
| 982 | - * @param int $addressBookId |
|
| 983 | - * @param string $uri |
|
| 984 | - * @returns array |
|
| 985 | - */ |
|
| 986 | - public function getContact($addressBookId, $uri) { |
|
| 987 | - $result = []; |
|
| 988 | - $query = $this->db->getQueryBuilder(); |
|
| 989 | - $query->select('*')->from($this->dbCardsTable) |
|
| 990 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 991 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 992 | - $queryResult = $query->execute(); |
|
| 993 | - $contact = $queryResult->fetch(); |
|
| 994 | - $queryResult->closeCursor(); |
|
| 995 | - |
|
| 996 | - if (is_array($contact)) { |
|
| 997 | - $result = $contact; |
|
| 998 | - } |
|
| 999 | - |
|
| 1000 | - return $result; |
|
| 1001 | - } |
|
| 1002 | - |
|
| 1003 | - /** |
|
| 1004 | - * Returns the list of people whom this address book is shared with. |
|
| 1005 | - * |
|
| 1006 | - * Every element in this array should have the following properties: |
|
| 1007 | - * * href - Often a mailto: address |
|
| 1008 | - * * commonName - Optional, for example a first + last name |
|
| 1009 | - * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
|
| 1010 | - * * readOnly - boolean |
|
| 1011 | - * * summary - Optional, a description for the share |
|
| 1012 | - * |
|
| 1013 | - * @return array |
|
| 1014 | - */ |
|
| 1015 | - public function getShares($addressBookId) { |
|
| 1016 | - return $this->sharingBackend->getShares($addressBookId); |
|
| 1017 | - } |
|
| 1018 | - |
|
| 1019 | - /** |
|
| 1020 | - * update properties table |
|
| 1021 | - * |
|
| 1022 | - * @param int $addressBookId |
|
| 1023 | - * @param string $cardUri |
|
| 1024 | - * @param string $vCardSerialized |
|
| 1025 | - */ |
|
| 1026 | - protected function updateProperties($addressBookId, $cardUri, $vCardSerialized) { |
|
| 1027 | - $cardId = $this->getCardId($addressBookId, $cardUri); |
|
| 1028 | - $vCard = $this->readCard($vCardSerialized); |
|
| 1029 | - |
|
| 1030 | - $this->purgeProperties($addressBookId, $cardId); |
|
| 1031 | - |
|
| 1032 | - $query = $this->db->getQueryBuilder(); |
|
| 1033 | - $query->insert($this->dbCardsPropertiesTable) |
|
| 1034 | - ->values( |
|
| 1035 | - [ |
|
| 1036 | - 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
| 1037 | - 'cardid' => $query->createNamedParameter($cardId), |
|
| 1038 | - 'name' => $query->createParameter('name'), |
|
| 1039 | - 'value' => $query->createParameter('value'), |
|
| 1040 | - 'preferred' => $query->createParameter('preferred') |
|
| 1041 | - ] |
|
| 1042 | - ); |
|
| 1043 | - |
|
| 1044 | - foreach ($vCard->children() as $property) { |
|
| 1045 | - if(!in_array($property->name, self::$indexProperties)) { |
|
| 1046 | - continue; |
|
| 1047 | - } |
|
| 1048 | - $preferred = 0; |
|
| 1049 | - foreach($property->parameters as $parameter) { |
|
| 1050 | - if ($parameter->name === 'TYPE' && strtoupper($parameter->getValue()) === 'PREF') { |
|
| 1051 | - $preferred = 1; |
|
| 1052 | - break; |
|
| 1053 | - } |
|
| 1054 | - } |
|
| 1055 | - $query->setParameter('name', $property->name); |
|
| 1056 | - $query->setParameter('value', substr($property->getValue(), 0, 254)); |
|
| 1057 | - $query->setParameter('preferred', $preferred); |
|
| 1058 | - $query->execute(); |
|
| 1059 | - } |
|
| 1060 | - } |
|
| 1061 | - |
|
| 1062 | - /** |
|
| 1063 | - * read vCard data into a vCard object |
|
| 1064 | - * |
|
| 1065 | - * @param string $cardData |
|
| 1066 | - * @return VCard |
|
| 1067 | - */ |
|
| 1068 | - protected function readCard($cardData) { |
|
| 1069 | - return Reader::read($cardData); |
|
| 1070 | - } |
|
| 1071 | - |
|
| 1072 | - /** |
|
| 1073 | - * delete all properties from a given card |
|
| 1074 | - * |
|
| 1075 | - * @param int $addressBookId |
|
| 1076 | - * @param int $cardId |
|
| 1077 | - */ |
|
| 1078 | - protected function purgeProperties($addressBookId, $cardId) { |
|
| 1079 | - $query = $this->db->getQueryBuilder(); |
|
| 1080 | - $query->delete($this->dbCardsPropertiesTable) |
|
| 1081 | - ->where($query->expr()->eq('cardid', $query->createNamedParameter($cardId))) |
|
| 1082 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 1083 | - $query->execute(); |
|
| 1084 | - } |
|
| 1085 | - |
|
| 1086 | - /** |
|
| 1087 | - * get ID from a given contact |
|
| 1088 | - * |
|
| 1089 | - * @param int $addressBookId |
|
| 1090 | - * @param string $uri |
|
| 1091 | - * @return int |
|
| 1092 | - */ |
|
| 1093 | - protected function getCardId($addressBookId, $uri) { |
|
| 1094 | - $query = $this->db->getQueryBuilder(); |
|
| 1095 | - $query->select('id')->from($this->dbCardsTable) |
|
| 1096 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 1097 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 1098 | - |
|
| 1099 | - $result = $query->execute(); |
|
| 1100 | - $cardIds = $result->fetch(); |
|
| 1101 | - $result->closeCursor(); |
|
| 1102 | - |
|
| 1103 | - if (!isset($cardIds['id'])) { |
|
| 1104 | - throw new \InvalidArgumentException('Card does not exists: ' . $uri); |
|
| 1105 | - } |
|
| 1106 | - |
|
| 1107 | - return (int)$cardIds['id']; |
|
| 1108 | - } |
|
| 1109 | - |
|
| 1110 | - /** |
|
| 1111 | - * For shared address books the sharee is set in the ACL of the address book |
|
| 1112 | - * @param $addressBookId |
|
| 1113 | - * @param $acl |
|
| 1114 | - * @return array |
|
| 1115 | - */ |
|
| 1116 | - public function applyShareAcl($addressBookId, $acl) { |
|
| 1117 | - return $this->sharingBackend->applyShareAcl($addressBookId, $acl); |
|
| 1118 | - } |
|
| 1119 | - |
|
| 1120 | - private function convertPrincipal($principalUri, $toV2) { |
|
| 1121 | - if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
|
| 1122 | - list(, $name) = \Sabre\Uri\split($principalUri); |
|
| 1123 | - if ($toV2 === true) { |
|
| 1124 | - return "principals/users/$name"; |
|
| 1125 | - } |
|
| 1126 | - return "principals/$name"; |
|
| 1127 | - } |
|
| 1128 | - return $principalUri; |
|
| 1129 | - } |
|
| 1130 | - |
|
| 1131 | - private function addOwnerPrincipal(&$addressbookInfo) { |
|
| 1132 | - $ownerPrincipalKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'; |
|
| 1133 | - $displaynameKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'; |
|
| 1134 | - if (isset($addressbookInfo[$ownerPrincipalKey])) { |
|
| 1135 | - $uri = $addressbookInfo[$ownerPrincipalKey]; |
|
| 1136 | - } else { |
|
| 1137 | - $uri = $addressbookInfo['principaluri']; |
|
| 1138 | - } |
|
| 1139 | - |
|
| 1140 | - $principalInformation = $this->principalBackend->getPrincipalByPath($uri); |
|
| 1141 | - if (isset($principalInformation['{DAV:}displayname'])) { |
|
| 1142 | - $addressbookInfo[$displaynameKey] = $principalInformation['{DAV:}displayname']; |
|
| 1143 | - } |
|
| 1144 | - } |
|
| 1145 | - |
|
| 1146 | - /** |
|
| 1147 | - * Extract UID from vcard |
|
| 1148 | - * |
|
| 1149 | - * @param string $cardData the vcard raw data |
|
| 1150 | - * @return string the uid |
|
| 1151 | - * @throws BadRequest if no UID is available |
|
| 1152 | - */ |
|
| 1153 | - private function getUID($cardData) { |
|
| 1154 | - if ($cardData != '') { |
|
| 1155 | - $vCard = Reader::read($cardData); |
|
| 1156 | - if ($vCard->UID) { |
|
| 1157 | - $uid = $vCard->UID->getValue(); |
|
| 1158 | - return $uid; |
|
| 1159 | - } |
|
| 1160 | - // should already be handled, but just in case |
|
| 1161 | - throw new BadRequest('vCards on CardDAV servers MUST have a UID property'); |
|
| 1162 | - } |
|
| 1163 | - // should already be handled, but just in case |
|
| 1164 | - throw new BadRequest('vCard can not be empty'); |
|
| 1165 | - } |
|
| 56 | + const PERSONAL_ADDRESSBOOK_URI = 'contacts'; |
|
| 57 | + const PERSONAL_ADDRESSBOOK_NAME = 'Contacts'; |
|
| 58 | + |
|
| 59 | + /** @var Principal */ |
|
| 60 | + private $principalBackend; |
|
| 61 | + |
|
| 62 | + /** @var string */ |
|
| 63 | + private $dbCardsTable = 'cards'; |
|
| 64 | + |
|
| 65 | + /** @var string */ |
|
| 66 | + private $dbCardsPropertiesTable = 'cards_properties'; |
|
| 67 | + |
|
| 68 | + /** @var IDBConnection */ |
|
| 69 | + private $db; |
|
| 70 | + |
|
| 71 | + /** @var Backend */ |
|
| 72 | + private $sharingBackend; |
|
| 73 | + |
|
| 74 | + /** @var array properties to index */ |
|
| 75 | + public static $indexProperties = array( |
|
| 76 | + 'BDAY', 'UID', 'N', 'FN', 'TITLE', 'ROLE', 'NOTE', 'NICKNAME', |
|
| 77 | + 'ORG', 'CATEGORIES', 'EMAIL', 'TEL', 'IMPP', 'ADR', 'URL', 'GEO', 'CLOUD'); |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * @var string[] Map of uid => display name |
|
| 81 | + */ |
|
| 82 | + protected $userDisplayNames; |
|
| 83 | + |
|
| 84 | + /** @var IUserManager */ |
|
| 85 | + private $userManager; |
|
| 86 | + |
|
| 87 | + /** @var EventDispatcherInterface */ |
|
| 88 | + private $dispatcher; |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * CardDavBackend constructor. |
|
| 92 | + * |
|
| 93 | + * @param IDBConnection $db |
|
| 94 | + * @param Principal $principalBackend |
|
| 95 | + * @param IUserManager $userManager |
|
| 96 | + * @param IGroupManager $groupManager |
|
| 97 | + * @param EventDispatcherInterface $dispatcher |
|
| 98 | + */ |
|
| 99 | + public function __construct(IDBConnection $db, |
|
| 100 | + Principal $principalBackend, |
|
| 101 | + IUserManager $userManager, |
|
| 102 | + IGroupManager $groupManager, |
|
| 103 | + EventDispatcherInterface $dispatcher) { |
|
| 104 | + $this->db = $db; |
|
| 105 | + $this->principalBackend = $principalBackend; |
|
| 106 | + $this->userManager = $userManager; |
|
| 107 | + $this->dispatcher = $dispatcher; |
|
| 108 | + $this->sharingBackend = new Backend($this->db, $this->userManager, $groupManager, $principalBackend, 'addressbook'); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Return the number of address books for a principal |
|
| 113 | + * |
|
| 114 | + * @param $principalUri |
|
| 115 | + * @return int |
|
| 116 | + */ |
|
| 117 | + public function getAddressBooksForUserCount($principalUri) { |
|
| 118 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 119 | + $query = $this->db->getQueryBuilder(); |
|
| 120 | + $query->select($query->func()->count('*')) |
|
| 121 | + ->from('addressbooks') |
|
| 122 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 123 | + |
|
| 124 | + return (int)$query->execute()->fetchColumn(); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * Returns the list of address books for a specific user. |
|
| 129 | + * |
|
| 130 | + * Every addressbook should have the following properties: |
|
| 131 | + * id - an arbitrary unique id |
|
| 132 | + * uri - the 'basename' part of the url |
|
| 133 | + * principaluri - Same as the passed parameter |
|
| 134 | + * |
|
| 135 | + * Any additional clark-notation property may be passed besides this. Some |
|
| 136 | + * common ones are : |
|
| 137 | + * {DAV:}displayname |
|
| 138 | + * {urn:ietf:params:xml:ns:carddav}addressbook-description |
|
| 139 | + * {http://calendarserver.org/ns/}getctag |
|
| 140 | + * |
|
| 141 | + * @param string $principalUri |
|
| 142 | + * @return array |
|
| 143 | + */ |
|
| 144 | + function getAddressBooksForUser($principalUri) { |
|
| 145 | + $principalUriOriginal = $principalUri; |
|
| 146 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 147 | + $query = $this->db->getQueryBuilder(); |
|
| 148 | + $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 149 | + ->from('addressbooks') |
|
| 150 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 151 | + |
|
| 152 | + $addressBooks = []; |
|
| 153 | + |
|
| 154 | + $result = $query->execute(); |
|
| 155 | + while($row = $result->fetch()) { |
|
| 156 | + $addressBooks[$row['id']] = [ |
|
| 157 | + 'id' => $row['id'], |
|
| 158 | + 'uri' => $row['uri'], |
|
| 159 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
|
| 160 | + '{DAV:}displayname' => $row['displayname'], |
|
| 161 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 162 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 163 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 164 | + ]; |
|
| 165 | + |
|
| 166 | + $this->addOwnerPrincipal($addressBooks[$row['id']]); |
|
| 167 | + } |
|
| 168 | + $result->closeCursor(); |
|
| 169 | + |
|
| 170 | + // query for shared calendars |
|
| 171 | + $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); |
|
| 172 | + $principals = array_map(function($principal) { |
|
| 173 | + return urldecode($principal); |
|
| 174 | + }, $principals); |
|
| 175 | + $principals[]= $principalUri; |
|
| 176 | + |
|
| 177 | + $query = $this->db->getQueryBuilder(); |
|
| 178 | + $result = $query->select(['a.id', 'a.uri', 'a.displayname', 'a.principaluri', 'a.description', 'a.synctoken', 's.access']) |
|
| 179 | + ->from('dav_shares', 's') |
|
| 180 | + ->join('s', 'addressbooks', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
| 181 | + ->where($query->expr()->in('s.principaluri', $query->createParameter('principaluri'))) |
|
| 182 | + ->andWhere($query->expr()->eq('s.type', $query->createParameter('type'))) |
|
| 183 | + ->setParameter('type', 'addressbook') |
|
| 184 | + ->setParameter('principaluri', $principals, IQueryBuilder::PARAM_STR_ARRAY) |
|
| 185 | + ->execute(); |
|
| 186 | + |
|
| 187 | + $readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'; |
|
| 188 | + while($row = $result->fetch()) { |
|
| 189 | + if ($row['principaluri'] === $principalUri) { |
|
| 190 | + continue; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + $readOnly = (int) $row['access'] === Backend::ACCESS_READ; |
|
| 194 | + if (isset($addressBooks[$row['id']])) { |
|
| 195 | + if ($readOnly) { |
|
| 196 | + // New share can not have more permissions then the old one. |
|
| 197 | + continue; |
|
| 198 | + } |
|
| 199 | + if (isset($addressBooks[$row['id']][$readOnlyPropertyName]) && |
|
| 200 | + $addressBooks[$row['id']][$readOnlyPropertyName] === 0) { |
|
| 201 | + // Old share is already read-write, no more permissions can be gained |
|
| 202 | + continue; |
|
| 203 | + } |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + list(, $name) = \Sabre\Uri\split($row['principaluri']); |
|
| 207 | + $uri = $row['uri'] . '_shared_by_' . $name; |
|
| 208 | + $displayName = $row['displayname'] . ' (' . $this->getUserDisplayName($name) . ')'; |
|
| 209 | + |
|
| 210 | + $addressBooks[$row['id']] = [ |
|
| 211 | + 'id' => $row['id'], |
|
| 212 | + 'uri' => $uri, |
|
| 213 | + 'principaluri' => $principalUriOriginal, |
|
| 214 | + '{DAV:}displayname' => $displayName, |
|
| 215 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 216 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 217 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 218 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $row['principaluri'], |
|
| 219 | + $readOnlyPropertyName => $readOnly, |
|
| 220 | + ]; |
|
| 221 | + |
|
| 222 | + $this->addOwnerPrincipal($addressBooks[$row['id']]); |
|
| 223 | + } |
|
| 224 | + $result->closeCursor(); |
|
| 225 | + |
|
| 226 | + return array_values($addressBooks); |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + public function getUsersOwnAddressBooks($principalUri) { |
|
| 230 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 231 | + $query = $this->db->getQueryBuilder(); |
|
| 232 | + $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 233 | + ->from('addressbooks') |
|
| 234 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 235 | + |
|
| 236 | + $addressBooks = []; |
|
| 237 | + |
|
| 238 | + $result = $query->execute(); |
|
| 239 | + while($row = $result->fetch()) { |
|
| 240 | + $addressBooks[$row['id']] = [ |
|
| 241 | + 'id' => $row['id'], |
|
| 242 | + 'uri' => $row['uri'], |
|
| 243 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
|
| 244 | + '{DAV:}displayname' => $row['displayname'], |
|
| 245 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 246 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 247 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 248 | + ]; |
|
| 249 | + |
|
| 250 | + $this->addOwnerPrincipal($addressBooks[$row['id']]); |
|
| 251 | + } |
|
| 252 | + $result->closeCursor(); |
|
| 253 | + |
|
| 254 | + return array_values($addressBooks); |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + private function getUserDisplayName($uid) { |
|
| 258 | + if (!isset($this->userDisplayNames[$uid])) { |
|
| 259 | + $user = $this->userManager->get($uid); |
|
| 260 | + |
|
| 261 | + if ($user instanceof IUser) { |
|
| 262 | + $this->userDisplayNames[$uid] = $user->getDisplayName(); |
|
| 263 | + } else { |
|
| 264 | + $this->userDisplayNames[$uid] = $uid; |
|
| 265 | + } |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + return $this->userDisplayNames[$uid]; |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + /** |
|
| 272 | + * @param int $addressBookId |
|
| 273 | + */ |
|
| 274 | + public function getAddressBookById($addressBookId) { |
|
| 275 | + $query = $this->db->getQueryBuilder(); |
|
| 276 | + $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 277 | + ->from('addressbooks') |
|
| 278 | + ->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) |
|
| 279 | + ->execute(); |
|
| 280 | + |
|
| 281 | + $row = $result->fetch(); |
|
| 282 | + $result->closeCursor(); |
|
| 283 | + if ($row === false) { |
|
| 284 | + return null; |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + $addressBook = [ |
|
| 288 | + 'id' => $row['id'], |
|
| 289 | + 'uri' => $row['uri'], |
|
| 290 | + 'principaluri' => $row['principaluri'], |
|
| 291 | + '{DAV:}displayname' => $row['displayname'], |
|
| 292 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 293 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 294 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 295 | + ]; |
|
| 296 | + |
|
| 297 | + $this->addOwnerPrincipal($addressBook); |
|
| 298 | + |
|
| 299 | + return $addressBook; |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + /** |
|
| 303 | + * @param $addressBookUri |
|
| 304 | + * @return array|null |
|
| 305 | + */ |
|
| 306 | + public function getAddressBooksByUri($principal, $addressBookUri) { |
|
| 307 | + $query = $this->db->getQueryBuilder(); |
|
| 308 | + $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 309 | + ->from('addressbooks') |
|
| 310 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($addressBookUri))) |
|
| 311 | + ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal))) |
|
| 312 | + ->setMaxResults(1) |
|
| 313 | + ->execute(); |
|
| 314 | + |
|
| 315 | + $row = $result->fetch(); |
|
| 316 | + $result->closeCursor(); |
|
| 317 | + if ($row === false) { |
|
| 318 | + return null; |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + $addressBook = [ |
|
| 322 | + 'id' => $row['id'], |
|
| 323 | + 'uri' => $row['uri'], |
|
| 324 | + 'principaluri' => $row['principaluri'], |
|
| 325 | + '{DAV:}displayname' => $row['displayname'], |
|
| 326 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 327 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 328 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 329 | + ]; |
|
| 330 | + |
|
| 331 | + $this->addOwnerPrincipal($addressBook); |
|
| 332 | + |
|
| 333 | + return $addressBook; |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + /** |
|
| 337 | + * Updates properties for an address book. |
|
| 338 | + * |
|
| 339 | + * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
| 340 | + * To do the actual updates, you must tell this object which properties |
|
| 341 | + * you're going to process with the handle() method. |
|
| 342 | + * |
|
| 343 | + * Calling the handle method is like telling the PropPatch object "I |
|
| 344 | + * promise I can handle updating this property". |
|
| 345 | + * |
|
| 346 | + * Read the PropPatch documentation for more info and examples. |
|
| 347 | + * |
|
| 348 | + * @param string $addressBookId |
|
| 349 | + * @param \Sabre\DAV\PropPatch $propPatch |
|
| 350 | + * @return void |
|
| 351 | + */ |
|
| 352 | + function updateAddressBook($addressBookId, \Sabre\DAV\PropPatch $propPatch) { |
|
| 353 | + $supportedProperties = [ |
|
| 354 | + '{DAV:}displayname', |
|
| 355 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description', |
|
| 356 | + ]; |
|
| 357 | + |
|
| 358 | + /** |
|
| 359 | + * @suppress SqlInjectionChecker |
|
| 360 | + */ |
|
| 361 | + $propPatch->handle($supportedProperties, function($mutations) use ($addressBookId) { |
|
| 362 | + |
|
| 363 | + $updates = []; |
|
| 364 | + foreach($mutations as $property=>$newValue) { |
|
| 365 | + |
|
| 366 | + switch($property) { |
|
| 367 | + case '{DAV:}displayname' : |
|
| 368 | + $updates['displayname'] = $newValue; |
|
| 369 | + break; |
|
| 370 | + case '{' . Plugin::NS_CARDDAV . '}addressbook-description' : |
|
| 371 | + $updates['description'] = $newValue; |
|
| 372 | + break; |
|
| 373 | + } |
|
| 374 | + } |
|
| 375 | + $query = $this->db->getQueryBuilder(); |
|
| 376 | + $query->update('addressbooks'); |
|
| 377 | + |
|
| 378 | + foreach($updates as $key=>$value) { |
|
| 379 | + $query->set($key, $query->createNamedParameter($value)); |
|
| 380 | + } |
|
| 381 | + $query->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) |
|
| 382 | + ->execute(); |
|
| 383 | + |
|
| 384 | + $this->addChange($addressBookId, "", 2); |
|
| 385 | + |
|
| 386 | + return true; |
|
| 387 | + |
|
| 388 | + }); |
|
| 389 | + } |
|
| 390 | + |
|
| 391 | + /** |
|
| 392 | + * Creates a new address book |
|
| 393 | + * |
|
| 394 | + * @param string $principalUri |
|
| 395 | + * @param string $url Just the 'basename' of the url. |
|
| 396 | + * @param array $properties |
|
| 397 | + * @return int |
|
| 398 | + * @throws BadRequest |
|
| 399 | + */ |
|
| 400 | + function createAddressBook($principalUri, $url, array $properties) { |
|
| 401 | + $values = [ |
|
| 402 | + 'displayname' => null, |
|
| 403 | + 'description' => null, |
|
| 404 | + 'principaluri' => $principalUri, |
|
| 405 | + 'uri' => $url, |
|
| 406 | + 'synctoken' => 1 |
|
| 407 | + ]; |
|
| 408 | + |
|
| 409 | + foreach($properties as $property=>$newValue) { |
|
| 410 | + |
|
| 411 | + switch($property) { |
|
| 412 | + case '{DAV:}displayname' : |
|
| 413 | + $values['displayname'] = $newValue; |
|
| 414 | + break; |
|
| 415 | + case '{' . Plugin::NS_CARDDAV . '}addressbook-description' : |
|
| 416 | + $values['description'] = $newValue; |
|
| 417 | + break; |
|
| 418 | + default : |
|
| 419 | + throw new BadRequest('Unknown property: ' . $property); |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + // Fallback to make sure the displayname is set. Some clients may refuse |
|
| 425 | + // to work with addressbooks not having a displayname. |
|
| 426 | + if(is_null($values['displayname'])) { |
|
| 427 | + $values['displayname'] = $url; |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + $query = $this->db->getQueryBuilder(); |
|
| 431 | + $query->insert('addressbooks') |
|
| 432 | + ->values([ |
|
| 433 | + 'uri' => $query->createParameter('uri'), |
|
| 434 | + 'displayname' => $query->createParameter('displayname'), |
|
| 435 | + 'description' => $query->createParameter('description'), |
|
| 436 | + 'principaluri' => $query->createParameter('principaluri'), |
|
| 437 | + 'synctoken' => $query->createParameter('synctoken'), |
|
| 438 | + ]) |
|
| 439 | + ->setParameters($values) |
|
| 440 | + ->execute(); |
|
| 441 | + |
|
| 442 | + return $query->getLastInsertId(); |
|
| 443 | + } |
|
| 444 | + |
|
| 445 | + /** |
|
| 446 | + * Deletes an entire addressbook and all its contents |
|
| 447 | + * |
|
| 448 | + * @param mixed $addressBookId |
|
| 449 | + * @return void |
|
| 450 | + */ |
|
| 451 | + function deleteAddressBook($addressBookId) { |
|
| 452 | + $query = $this->db->getQueryBuilder(); |
|
| 453 | + $query->delete('cards') |
|
| 454 | + ->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid'))) |
|
| 455 | + ->setParameter('addressbookid', $addressBookId) |
|
| 456 | + ->execute(); |
|
| 457 | + |
|
| 458 | + $query->delete('addressbookchanges') |
|
| 459 | + ->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid'))) |
|
| 460 | + ->setParameter('addressbookid', $addressBookId) |
|
| 461 | + ->execute(); |
|
| 462 | + |
|
| 463 | + $query->delete('addressbooks') |
|
| 464 | + ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
| 465 | + ->setParameter('id', $addressBookId) |
|
| 466 | + ->execute(); |
|
| 467 | + |
|
| 468 | + $this->sharingBackend->deleteAllShares($addressBookId); |
|
| 469 | + |
|
| 470 | + $query->delete($this->dbCardsPropertiesTable) |
|
| 471 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 472 | + ->execute(); |
|
| 473 | + |
|
| 474 | + } |
|
| 475 | + |
|
| 476 | + /** |
|
| 477 | + * Returns all cards for a specific addressbook id. |
|
| 478 | + * |
|
| 479 | + * This method should return the following properties for each card: |
|
| 480 | + * * carddata - raw vcard data |
|
| 481 | + * * uri - Some unique url |
|
| 482 | + * * lastmodified - A unix timestamp |
|
| 483 | + * |
|
| 484 | + * It's recommended to also return the following properties: |
|
| 485 | + * * etag - A unique etag. This must change every time the card changes. |
|
| 486 | + * * size - The size of the card in bytes. |
|
| 487 | + * |
|
| 488 | + * If these last two properties are provided, less time will be spent |
|
| 489 | + * calculating them. If they are specified, you can also ommit carddata. |
|
| 490 | + * This may speed up certain requests, especially with large cards. |
|
| 491 | + * |
|
| 492 | + * @param mixed $addressBookId |
|
| 493 | + * @return array |
|
| 494 | + */ |
|
| 495 | + function getCards($addressBookId) { |
|
| 496 | + $query = $this->db->getQueryBuilder(); |
|
| 497 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata', 'uid']) |
|
| 498 | + ->from('cards') |
|
| 499 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 500 | + |
|
| 501 | + $cards = []; |
|
| 502 | + |
|
| 503 | + $result = $query->execute(); |
|
| 504 | + while($row = $result->fetch()) { |
|
| 505 | + $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 506 | + $row['carddata'] = $this->readBlob($row['carddata']); |
|
| 507 | + $cards[] = $row; |
|
| 508 | + } |
|
| 509 | + $result->closeCursor(); |
|
| 510 | + |
|
| 511 | + return $cards; |
|
| 512 | + } |
|
| 513 | + |
|
| 514 | + /** |
|
| 515 | + * Returns a specific card. |
|
| 516 | + * |
|
| 517 | + * The same set of properties must be returned as with getCards. The only |
|
| 518 | + * exception is that 'carddata' is absolutely required. |
|
| 519 | + * |
|
| 520 | + * If the card does not exist, you must return false. |
|
| 521 | + * |
|
| 522 | + * @param mixed $addressBookId |
|
| 523 | + * @param string $cardUri |
|
| 524 | + * @return array |
|
| 525 | + */ |
|
| 526 | + function getCard($addressBookId, $cardUri) { |
|
| 527 | + $query = $this->db->getQueryBuilder(); |
|
| 528 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata', 'uid']) |
|
| 529 | + ->from('cards') |
|
| 530 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 531 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 532 | + ->setMaxResults(1); |
|
| 533 | + |
|
| 534 | + $result = $query->execute(); |
|
| 535 | + $row = $result->fetch(); |
|
| 536 | + if (!$row) { |
|
| 537 | + return false; |
|
| 538 | + } |
|
| 539 | + $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 540 | + $row['carddata'] = $this->readBlob($row['carddata']); |
|
| 541 | + |
|
| 542 | + return $row; |
|
| 543 | + } |
|
| 544 | + |
|
| 545 | + /** |
|
| 546 | + * Returns a list of cards. |
|
| 547 | + * |
|
| 548 | + * This method should work identical to getCard, but instead return all the |
|
| 549 | + * cards in the list as an array. |
|
| 550 | + * |
|
| 551 | + * If the backend supports this, it may allow for some speed-ups. |
|
| 552 | + * |
|
| 553 | + * @param mixed $addressBookId |
|
| 554 | + * @param string[] $uris |
|
| 555 | + * @return array |
|
| 556 | + */ |
|
| 557 | + function getMultipleCards($addressBookId, array $uris) { |
|
| 558 | + if (empty($uris)) { |
|
| 559 | + return []; |
|
| 560 | + } |
|
| 561 | + |
|
| 562 | + $chunks = array_chunk($uris, 100); |
|
| 563 | + $cards = []; |
|
| 564 | + |
|
| 565 | + $query = $this->db->getQueryBuilder(); |
|
| 566 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata', 'uid']) |
|
| 567 | + ->from('cards') |
|
| 568 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 569 | + ->andWhere($query->expr()->in('uri', $query->createParameter('uri'))); |
|
| 570 | + |
|
| 571 | + foreach ($chunks as $uris) { |
|
| 572 | + $query->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY); |
|
| 573 | + $result = $query->execute(); |
|
| 574 | + |
|
| 575 | + while ($row = $result->fetch()) { |
|
| 576 | + $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 577 | + $row['carddata'] = $this->readBlob($row['carddata']); |
|
| 578 | + $cards[] = $row; |
|
| 579 | + } |
|
| 580 | + $result->closeCursor(); |
|
| 581 | + } |
|
| 582 | + return $cards; |
|
| 583 | + } |
|
| 584 | + |
|
| 585 | + /** |
|
| 586 | + * Creates a new card. |
|
| 587 | + * |
|
| 588 | + * The addressbook id will be passed as the first argument. This is the |
|
| 589 | + * same id as it is returned from the getAddressBooksForUser method. |
|
| 590 | + * |
|
| 591 | + * The cardUri is a base uri, and doesn't include the full path. The |
|
| 592 | + * cardData argument is the vcard body, and is passed as a string. |
|
| 593 | + * |
|
| 594 | + * It is possible to return an ETag from this method. This ETag is for the |
|
| 595 | + * newly created resource, and must be enclosed with double quotes (that |
|
| 596 | + * is, the string itself must contain the double quotes). |
|
| 597 | + * |
|
| 598 | + * You should only return the ETag if you store the carddata as-is. If a |
|
| 599 | + * subsequent GET request on the same card does not have the same body, |
|
| 600 | + * byte-by-byte and you did return an ETag here, clients tend to get |
|
| 601 | + * confused. |
|
| 602 | + * |
|
| 603 | + * If you don't return an ETag, you can just return null. |
|
| 604 | + * |
|
| 605 | + * @param mixed $addressBookId |
|
| 606 | + * @param string $cardUri |
|
| 607 | + * @param string $cardData |
|
| 608 | + * @return string |
|
| 609 | + */ |
|
| 610 | + function createCard($addressBookId, $cardUri, $cardData) { |
|
| 611 | + $etag = md5($cardData); |
|
| 612 | + $uid = $this->getUID($cardData); |
|
| 613 | + |
|
| 614 | + $q = $this->db->getQueryBuilder(); |
|
| 615 | + $q->select('uid') |
|
| 616 | + ->from('cards') |
|
| 617 | + ->where($q->expr()->eq('addressbookid', $q->createNamedParameter($addressBookId))) |
|
| 618 | + ->andWhere($q->expr()->eq('uid', $q->createNamedParameter($uid))) |
|
| 619 | + ->setMaxResults(1); |
|
| 620 | + $result = $q->execute(); |
|
| 621 | + $count = (bool) $result->fetchColumn(); |
|
| 622 | + $result->closeCursor(); |
|
| 623 | + if ($count) { |
|
| 624 | + throw new \Sabre\DAV\Exception\BadRequest('VCard object with uid already exists in this addressbook collection.'); |
|
| 625 | + } |
|
| 626 | + |
|
| 627 | + $query = $this->db->getQueryBuilder(); |
|
| 628 | + $query->insert('cards') |
|
| 629 | + ->values([ |
|
| 630 | + 'carddata' => $query->createNamedParameter($cardData, IQueryBuilder::PARAM_LOB), |
|
| 631 | + 'uri' => $query->createNamedParameter($cardUri), |
|
| 632 | + 'lastmodified' => $query->createNamedParameter(time()), |
|
| 633 | + 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
| 634 | + 'size' => $query->createNamedParameter(strlen($cardData)), |
|
| 635 | + 'etag' => $query->createNamedParameter($etag), |
|
| 636 | + 'uid' => $query->createNamedParameter($uid), |
|
| 637 | + ]) |
|
| 638 | + ->execute(); |
|
| 639 | + |
|
| 640 | + $this->addChange($addressBookId, $cardUri, 1); |
|
| 641 | + $this->updateProperties($addressBookId, $cardUri, $cardData); |
|
| 642 | + |
|
| 643 | + $this->dispatcher->dispatch('\OCA\DAV\CardDAV\CardDavBackend::createCard', |
|
| 644 | + new GenericEvent(null, [ |
|
| 645 | + 'addressBookId' => $addressBookId, |
|
| 646 | + 'cardUri' => $cardUri, |
|
| 647 | + 'cardData' => $cardData])); |
|
| 648 | + |
|
| 649 | + return '"' . $etag . '"'; |
|
| 650 | + } |
|
| 651 | + |
|
| 652 | + /** |
|
| 653 | + * Updates a card. |
|
| 654 | + * |
|
| 655 | + * The addressbook id will be passed as the first argument. This is the |
|
| 656 | + * same id as it is returned from the getAddressBooksForUser method. |
|
| 657 | + * |
|
| 658 | + * The cardUri is a base uri, and doesn't include the full path. The |
|
| 659 | + * cardData argument is the vcard body, and is passed as a string. |
|
| 660 | + * |
|
| 661 | + * It is possible to return an ETag from this method. This ETag should |
|
| 662 | + * match that of the updated resource, and must be enclosed with double |
|
| 663 | + * quotes (that is: the string itself must contain the actual quotes). |
|
| 664 | + * |
|
| 665 | + * You should only return the ETag if you store the carddata as-is. If a |
|
| 666 | + * subsequent GET request on the same card does not have the same body, |
|
| 667 | + * byte-by-byte and you did return an ETag here, clients tend to get |
|
| 668 | + * confused. |
|
| 669 | + * |
|
| 670 | + * If you don't return an ETag, you can just return null. |
|
| 671 | + * |
|
| 672 | + * @param mixed $addressBookId |
|
| 673 | + * @param string $cardUri |
|
| 674 | + * @param string $cardData |
|
| 675 | + * @return string |
|
| 676 | + */ |
|
| 677 | + function updateCard($addressBookId, $cardUri, $cardData) { |
|
| 678 | + |
|
| 679 | + $uid = $this->getUID($cardData); |
|
| 680 | + $etag = md5($cardData); |
|
| 681 | + $query = $this->db->getQueryBuilder(); |
|
| 682 | + $query->update('cards') |
|
| 683 | + ->set('carddata', $query->createNamedParameter($cardData, IQueryBuilder::PARAM_LOB)) |
|
| 684 | + ->set('lastmodified', $query->createNamedParameter(time())) |
|
| 685 | + ->set('size', $query->createNamedParameter(strlen($cardData))) |
|
| 686 | + ->set('etag', $query->createNamedParameter($etag)) |
|
| 687 | + ->set('uid', $query->createNamedParameter($uid)) |
|
| 688 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 689 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 690 | + ->execute(); |
|
| 691 | + |
|
| 692 | + $this->addChange($addressBookId, $cardUri, 2); |
|
| 693 | + $this->updateProperties($addressBookId, $cardUri, $cardData); |
|
| 694 | + |
|
| 695 | + $this->dispatcher->dispatch('\OCA\DAV\CardDAV\CardDavBackend::updateCard', |
|
| 696 | + new GenericEvent(null, [ |
|
| 697 | + 'addressBookId' => $addressBookId, |
|
| 698 | + 'cardUri' => $cardUri, |
|
| 699 | + 'cardData' => $cardData])); |
|
| 700 | + |
|
| 701 | + return '"' . $etag . '"'; |
|
| 702 | + } |
|
| 703 | + |
|
| 704 | + /** |
|
| 705 | + * Deletes a card |
|
| 706 | + * |
|
| 707 | + * @param mixed $addressBookId |
|
| 708 | + * @param string $cardUri |
|
| 709 | + * @return bool |
|
| 710 | + */ |
|
| 711 | + function deleteCard($addressBookId, $cardUri) { |
|
| 712 | + try { |
|
| 713 | + $cardId = $this->getCardId($addressBookId, $cardUri); |
|
| 714 | + } catch (\InvalidArgumentException $e) { |
|
| 715 | + $cardId = null; |
|
| 716 | + } |
|
| 717 | + $query = $this->db->getQueryBuilder(); |
|
| 718 | + $ret = $query->delete('cards') |
|
| 719 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 720 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 721 | + ->execute(); |
|
| 722 | + |
|
| 723 | + $this->addChange($addressBookId, $cardUri, 3); |
|
| 724 | + |
|
| 725 | + $this->dispatcher->dispatch('\OCA\DAV\CardDAV\CardDavBackend::deleteCard', |
|
| 726 | + new GenericEvent(null, [ |
|
| 727 | + 'addressBookId' => $addressBookId, |
|
| 728 | + 'cardUri' => $cardUri])); |
|
| 729 | + |
|
| 730 | + if ($ret === 1) { |
|
| 731 | + if ($cardId !== null) { |
|
| 732 | + $this->purgeProperties($addressBookId, $cardId); |
|
| 733 | + } |
|
| 734 | + return true; |
|
| 735 | + } |
|
| 736 | + |
|
| 737 | + return false; |
|
| 738 | + } |
|
| 739 | + |
|
| 740 | + /** |
|
| 741 | + * The getChanges method returns all the changes that have happened, since |
|
| 742 | + * the specified syncToken in the specified address book. |
|
| 743 | + * |
|
| 744 | + * This function should return an array, such as the following: |
|
| 745 | + * |
|
| 746 | + * [ |
|
| 747 | + * 'syncToken' => 'The current synctoken', |
|
| 748 | + * 'added' => [ |
|
| 749 | + * 'new.txt', |
|
| 750 | + * ], |
|
| 751 | + * 'modified' => [ |
|
| 752 | + * 'modified.txt', |
|
| 753 | + * ], |
|
| 754 | + * 'deleted' => [ |
|
| 755 | + * 'foo.php.bak', |
|
| 756 | + * 'old.txt' |
|
| 757 | + * ] |
|
| 758 | + * ]; |
|
| 759 | + * |
|
| 760 | + * The returned syncToken property should reflect the *current* syncToken |
|
| 761 | + * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
|
| 762 | + * property. This is needed here too, to ensure the operation is atomic. |
|
| 763 | + * |
|
| 764 | + * If the $syncToken argument is specified as null, this is an initial |
|
| 765 | + * sync, and all members should be reported. |
|
| 766 | + * |
|
| 767 | + * The modified property is an array of nodenames that have changed since |
|
| 768 | + * the last token. |
|
| 769 | + * |
|
| 770 | + * The deleted property is an array with nodenames, that have been deleted |
|
| 771 | + * from collection. |
|
| 772 | + * |
|
| 773 | + * The $syncLevel argument is basically the 'depth' of the report. If it's |
|
| 774 | + * 1, you only have to report changes that happened only directly in |
|
| 775 | + * immediate descendants. If it's 2, it should also include changes from |
|
| 776 | + * the nodes below the child collections. (grandchildren) |
|
| 777 | + * |
|
| 778 | + * The $limit argument allows a client to specify how many results should |
|
| 779 | + * be returned at most. If the limit is not specified, it should be treated |
|
| 780 | + * as infinite. |
|
| 781 | + * |
|
| 782 | + * If the limit (infinite or not) is higher than you're willing to return, |
|
| 783 | + * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
|
| 784 | + * |
|
| 785 | + * If the syncToken is expired (due to data cleanup) or unknown, you must |
|
| 786 | + * return null. |
|
| 787 | + * |
|
| 788 | + * The limit is 'suggestive'. You are free to ignore it. |
|
| 789 | + * |
|
| 790 | + * @param string $addressBookId |
|
| 791 | + * @param string $syncToken |
|
| 792 | + * @param int $syncLevel |
|
| 793 | + * @param int $limit |
|
| 794 | + * @return array |
|
| 795 | + */ |
|
| 796 | + function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel, $limit = null) { |
|
| 797 | + // Current synctoken |
|
| 798 | + $stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*addressbooks` WHERE `id` = ?'); |
|
| 799 | + $stmt->execute([ $addressBookId ]); |
|
| 800 | + $currentToken = $stmt->fetchColumn(0); |
|
| 801 | + |
|
| 802 | + if (is_null($currentToken)) return null; |
|
| 803 | + |
|
| 804 | + $result = [ |
|
| 805 | + 'syncToken' => $currentToken, |
|
| 806 | + 'added' => [], |
|
| 807 | + 'modified' => [], |
|
| 808 | + 'deleted' => [], |
|
| 809 | + ]; |
|
| 810 | + |
|
| 811 | + if ($syncToken) { |
|
| 812 | + |
|
| 813 | + $query = "SELECT `uri`, `operation` FROM `*PREFIX*addressbookchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `addressbookid` = ? ORDER BY `synctoken`"; |
|
| 814 | + if ($limit>0) { |
|
| 815 | + $query .= " `LIMIT` " . (int)$limit; |
|
| 816 | + } |
|
| 817 | + |
|
| 818 | + // Fetching all changes |
|
| 819 | + $stmt = $this->db->prepare($query); |
|
| 820 | + $stmt->execute([$syncToken, $currentToken, $addressBookId]); |
|
| 821 | + |
|
| 822 | + $changes = []; |
|
| 823 | + |
|
| 824 | + // This loop ensures that any duplicates are overwritten, only the |
|
| 825 | + // last change on a node is relevant. |
|
| 826 | + while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 827 | + |
|
| 828 | + $changes[$row['uri']] = $row['operation']; |
|
| 829 | + |
|
| 830 | + } |
|
| 831 | + |
|
| 832 | + foreach($changes as $uri => $operation) { |
|
| 833 | + |
|
| 834 | + switch($operation) { |
|
| 835 | + case 1: |
|
| 836 | + $result['added'][] = $uri; |
|
| 837 | + break; |
|
| 838 | + case 2: |
|
| 839 | + $result['modified'][] = $uri; |
|
| 840 | + break; |
|
| 841 | + case 3: |
|
| 842 | + $result['deleted'][] = $uri; |
|
| 843 | + break; |
|
| 844 | + } |
|
| 845 | + |
|
| 846 | + } |
|
| 847 | + } else { |
|
| 848 | + // No synctoken supplied, this is the initial sync. |
|
| 849 | + $query = "SELECT `uri` FROM `*PREFIX*cards` WHERE `addressbookid` = ?"; |
|
| 850 | + $stmt = $this->db->prepare($query); |
|
| 851 | + $stmt->execute([$addressBookId]); |
|
| 852 | + |
|
| 853 | + $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
| 854 | + } |
|
| 855 | + return $result; |
|
| 856 | + } |
|
| 857 | + |
|
| 858 | + /** |
|
| 859 | + * Adds a change record to the addressbookchanges table. |
|
| 860 | + * |
|
| 861 | + * @param mixed $addressBookId |
|
| 862 | + * @param string $objectUri |
|
| 863 | + * @param int $operation 1 = add, 2 = modify, 3 = delete |
|
| 864 | + * @return void |
|
| 865 | + */ |
|
| 866 | + protected function addChange($addressBookId, $objectUri, $operation) { |
|
| 867 | + $sql = 'INSERT INTO `*PREFIX*addressbookchanges`(`uri`, `synctoken`, `addressbookid`, `operation`) SELECT ?, `synctoken`, ?, ? FROM `*PREFIX*addressbooks` WHERE `id` = ?'; |
|
| 868 | + $stmt = $this->db->prepare($sql); |
|
| 869 | + $stmt->execute([ |
|
| 870 | + $objectUri, |
|
| 871 | + $addressBookId, |
|
| 872 | + $operation, |
|
| 873 | + $addressBookId |
|
| 874 | + ]); |
|
| 875 | + $stmt = $this->db->prepare('UPDATE `*PREFIX*addressbooks` SET `synctoken` = `synctoken` + 1 WHERE `id` = ?'); |
|
| 876 | + $stmt->execute([ |
|
| 877 | + $addressBookId |
|
| 878 | + ]); |
|
| 879 | + } |
|
| 880 | + |
|
| 881 | + private function readBlob($cardData) { |
|
| 882 | + if (is_resource($cardData)) { |
|
| 883 | + return stream_get_contents($cardData); |
|
| 884 | + } |
|
| 885 | + |
|
| 886 | + return $cardData; |
|
| 887 | + } |
|
| 888 | + |
|
| 889 | + /** |
|
| 890 | + * @param IShareable $shareable |
|
| 891 | + * @param string[] $add |
|
| 892 | + * @param string[] $remove |
|
| 893 | + */ |
|
| 894 | + public function updateShares(IShareable $shareable, $add, $remove) { |
|
| 895 | + $this->sharingBackend->updateShares($shareable, $add, $remove); |
|
| 896 | + } |
|
| 897 | + |
|
| 898 | + /** |
|
| 899 | + * search contact |
|
| 900 | + * |
|
| 901 | + * @param int $addressBookId |
|
| 902 | + * @param string $pattern which should match within the $searchProperties |
|
| 903 | + * @param array $searchProperties defines the properties within the query pattern should match |
|
| 904 | + * @return array an array of contacts which are arrays of key-value-pairs |
|
| 905 | + */ |
|
| 906 | + public function search($addressBookId, $pattern, $searchProperties) { |
|
| 907 | + $query = $this->db->getQueryBuilder(); |
|
| 908 | + $query2 = $this->db->getQueryBuilder(); |
|
| 909 | + |
|
| 910 | + $query2->selectDistinct('cp.cardid')->from($this->dbCardsPropertiesTable, 'cp'); |
|
| 911 | + $query2->andWhere($query2->expr()->eq('cp.addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 912 | + $or = $query2->expr()->orX(); |
|
| 913 | + foreach ($searchProperties as $property) { |
|
| 914 | + $or->add($query2->expr()->eq('cp.name', $query->createNamedParameter($property))); |
|
| 915 | + } |
|
| 916 | + $query2->andWhere($or); |
|
| 917 | + |
|
| 918 | + // No need for like when the pattern is empty |
|
| 919 | + if ('' !== $pattern) { |
|
| 920 | + $query2->andWhere($query2->expr()->ilike('cp.value', $query->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%'))); |
|
| 921 | + } |
|
| 922 | + |
|
| 923 | + $query->select('c.carddata', 'c.uri')->from($this->dbCardsTable, 'c') |
|
| 924 | + ->where($query->expr()->in('c.id', $query->createFunction($query2->getSQL()))); |
|
| 925 | + |
|
| 926 | + $result = $query->execute(); |
|
| 927 | + $cards = $result->fetchAll(); |
|
| 928 | + |
|
| 929 | + $result->closeCursor(); |
|
| 930 | + |
|
| 931 | + return array_map(function($array) { |
|
| 932 | + $array['carddata'] = $this->readBlob($array['carddata']); |
|
| 933 | + return $array; |
|
| 934 | + }, $cards); |
|
| 935 | + } |
|
| 936 | + |
|
| 937 | + /** |
|
| 938 | + * @param int $bookId |
|
| 939 | + * @param string $name |
|
| 940 | + * @return array |
|
| 941 | + */ |
|
| 942 | + public function collectCardProperties($bookId, $name) { |
|
| 943 | + $query = $this->db->getQueryBuilder(); |
|
| 944 | + $result = $query->selectDistinct('value') |
|
| 945 | + ->from($this->dbCardsPropertiesTable) |
|
| 946 | + ->where($query->expr()->eq('name', $query->createNamedParameter($name))) |
|
| 947 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($bookId))) |
|
| 948 | + ->execute(); |
|
| 949 | + |
|
| 950 | + $all = $result->fetchAll(PDO::FETCH_COLUMN); |
|
| 951 | + $result->closeCursor(); |
|
| 952 | + |
|
| 953 | + return $all; |
|
| 954 | + } |
|
| 955 | + |
|
| 956 | + /** |
|
| 957 | + * get URI from a given contact |
|
| 958 | + * |
|
| 959 | + * @param int $id |
|
| 960 | + * @return string |
|
| 961 | + */ |
|
| 962 | + public function getCardUri($id) { |
|
| 963 | + $query = $this->db->getQueryBuilder(); |
|
| 964 | + $query->select('uri')->from($this->dbCardsTable) |
|
| 965 | + ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
| 966 | + ->setParameter('id', $id); |
|
| 967 | + |
|
| 968 | + $result = $query->execute(); |
|
| 969 | + $uri = $result->fetch(); |
|
| 970 | + $result->closeCursor(); |
|
| 971 | + |
|
| 972 | + if (!isset($uri['uri'])) { |
|
| 973 | + throw new \InvalidArgumentException('Card does not exists: ' . $id); |
|
| 974 | + } |
|
| 975 | + |
|
| 976 | + return $uri['uri']; |
|
| 977 | + } |
|
| 978 | + |
|
| 979 | + /** |
|
| 980 | + * return contact with the given URI |
|
| 981 | + * |
|
| 982 | + * @param int $addressBookId |
|
| 983 | + * @param string $uri |
|
| 984 | + * @returns array |
|
| 985 | + */ |
|
| 986 | + public function getContact($addressBookId, $uri) { |
|
| 987 | + $result = []; |
|
| 988 | + $query = $this->db->getQueryBuilder(); |
|
| 989 | + $query->select('*')->from($this->dbCardsTable) |
|
| 990 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 991 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 992 | + $queryResult = $query->execute(); |
|
| 993 | + $contact = $queryResult->fetch(); |
|
| 994 | + $queryResult->closeCursor(); |
|
| 995 | + |
|
| 996 | + if (is_array($contact)) { |
|
| 997 | + $result = $contact; |
|
| 998 | + } |
|
| 999 | + |
|
| 1000 | + return $result; |
|
| 1001 | + } |
|
| 1002 | + |
|
| 1003 | + /** |
|
| 1004 | + * Returns the list of people whom this address book is shared with. |
|
| 1005 | + * |
|
| 1006 | + * Every element in this array should have the following properties: |
|
| 1007 | + * * href - Often a mailto: address |
|
| 1008 | + * * commonName - Optional, for example a first + last name |
|
| 1009 | + * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
|
| 1010 | + * * readOnly - boolean |
|
| 1011 | + * * summary - Optional, a description for the share |
|
| 1012 | + * |
|
| 1013 | + * @return array |
|
| 1014 | + */ |
|
| 1015 | + public function getShares($addressBookId) { |
|
| 1016 | + return $this->sharingBackend->getShares($addressBookId); |
|
| 1017 | + } |
|
| 1018 | + |
|
| 1019 | + /** |
|
| 1020 | + * update properties table |
|
| 1021 | + * |
|
| 1022 | + * @param int $addressBookId |
|
| 1023 | + * @param string $cardUri |
|
| 1024 | + * @param string $vCardSerialized |
|
| 1025 | + */ |
|
| 1026 | + protected function updateProperties($addressBookId, $cardUri, $vCardSerialized) { |
|
| 1027 | + $cardId = $this->getCardId($addressBookId, $cardUri); |
|
| 1028 | + $vCard = $this->readCard($vCardSerialized); |
|
| 1029 | + |
|
| 1030 | + $this->purgeProperties($addressBookId, $cardId); |
|
| 1031 | + |
|
| 1032 | + $query = $this->db->getQueryBuilder(); |
|
| 1033 | + $query->insert($this->dbCardsPropertiesTable) |
|
| 1034 | + ->values( |
|
| 1035 | + [ |
|
| 1036 | + 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
| 1037 | + 'cardid' => $query->createNamedParameter($cardId), |
|
| 1038 | + 'name' => $query->createParameter('name'), |
|
| 1039 | + 'value' => $query->createParameter('value'), |
|
| 1040 | + 'preferred' => $query->createParameter('preferred') |
|
| 1041 | + ] |
|
| 1042 | + ); |
|
| 1043 | + |
|
| 1044 | + foreach ($vCard->children() as $property) { |
|
| 1045 | + if(!in_array($property->name, self::$indexProperties)) { |
|
| 1046 | + continue; |
|
| 1047 | + } |
|
| 1048 | + $preferred = 0; |
|
| 1049 | + foreach($property->parameters as $parameter) { |
|
| 1050 | + if ($parameter->name === 'TYPE' && strtoupper($parameter->getValue()) === 'PREF') { |
|
| 1051 | + $preferred = 1; |
|
| 1052 | + break; |
|
| 1053 | + } |
|
| 1054 | + } |
|
| 1055 | + $query->setParameter('name', $property->name); |
|
| 1056 | + $query->setParameter('value', substr($property->getValue(), 0, 254)); |
|
| 1057 | + $query->setParameter('preferred', $preferred); |
|
| 1058 | + $query->execute(); |
|
| 1059 | + } |
|
| 1060 | + } |
|
| 1061 | + |
|
| 1062 | + /** |
|
| 1063 | + * read vCard data into a vCard object |
|
| 1064 | + * |
|
| 1065 | + * @param string $cardData |
|
| 1066 | + * @return VCard |
|
| 1067 | + */ |
|
| 1068 | + protected function readCard($cardData) { |
|
| 1069 | + return Reader::read($cardData); |
|
| 1070 | + } |
|
| 1071 | + |
|
| 1072 | + /** |
|
| 1073 | + * delete all properties from a given card |
|
| 1074 | + * |
|
| 1075 | + * @param int $addressBookId |
|
| 1076 | + * @param int $cardId |
|
| 1077 | + */ |
|
| 1078 | + protected function purgeProperties($addressBookId, $cardId) { |
|
| 1079 | + $query = $this->db->getQueryBuilder(); |
|
| 1080 | + $query->delete($this->dbCardsPropertiesTable) |
|
| 1081 | + ->where($query->expr()->eq('cardid', $query->createNamedParameter($cardId))) |
|
| 1082 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 1083 | + $query->execute(); |
|
| 1084 | + } |
|
| 1085 | + |
|
| 1086 | + /** |
|
| 1087 | + * get ID from a given contact |
|
| 1088 | + * |
|
| 1089 | + * @param int $addressBookId |
|
| 1090 | + * @param string $uri |
|
| 1091 | + * @return int |
|
| 1092 | + */ |
|
| 1093 | + protected function getCardId($addressBookId, $uri) { |
|
| 1094 | + $query = $this->db->getQueryBuilder(); |
|
| 1095 | + $query->select('id')->from($this->dbCardsTable) |
|
| 1096 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 1097 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 1098 | + |
|
| 1099 | + $result = $query->execute(); |
|
| 1100 | + $cardIds = $result->fetch(); |
|
| 1101 | + $result->closeCursor(); |
|
| 1102 | + |
|
| 1103 | + if (!isset($cardIds['id'])) { |
|
| 1104 | + throw new \InvalidArgumentException('Card does not exists: ' . $uri); |
|
| 1105 | + } |
|
| 1106 | + |
|
| 1107 | + return (int)$cardIds['id']; |
|
| 1108 | + } |
|
| 1109 | + |
|
| 1110 | + /** |
|
| 1111 | + * For shared address books the sharee is set in the ACL of the address book |
|
| 1112 | + * @param $addressBookId |
|
| 1113 | + * @param $acl |
|
| 1114 | + * @return array |
|
| 1115 | + */ |
|
| 1116 | + public function applyShareAcl($addressBookId, $acl) { |
|
| 1117 | + return $this->sharingBackend->applyShareAcl($addressBookId, $acl); |
|
| 1118 | + } |
|
| 1119 | + |
|
| 1120 | + private function convertPrincipal($principalUri, $toV2) { |
|
| 1121 | + if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
|
| 1122 | + list(, $name) = \Sabre\Uri\split($principalUri); |
|
| 1123 | + if ($toV2 === true) { |
|
| 1124 | + return "principals/users/$name"; |
|
| 1125 | + } |
|
| 1126 | + return "principals/$name"; |
|
| 1127 | + } |
|
| 1128 | + return $principalUri; |
|
| 1129 | + } |
|
| 1130 | + |
|
| 1131 | + private function addOwnerPrincipal(&$addressbookInfo) { |
|
| 1132 | + $ownerPrincipalKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'; |
|
| 1133 | + $displaynameKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'; |
|
| 1134 | + if (isset($addressbookInfo[$ownerPrincipalKey])) { |
|
| 1135 | + $uri = $addressbookInfo[$ownerPrincipalKey]; |
|
| 1136 | + } else { |
|
| 1137 | + $uri = $addressbookInfo['principaluri']; |
|
| 1138 | + } |
|
| 1139 | + |
|
| 1140 | + $principalInformation = $this->principalBackend->getPrincipalByPath($uri); |
|
| 1141 | + if (isset($principalInformation['{DAV:}displayname'])) { |
|
| 1142 | + $addressbookInfo[$displaynameKey] = $principalInformation['{DAV:}displayname']; |
|
| 1143 | + } |
|
| 1144 | + } |
|
| 1145 | + |
|
| 1146 | + /** |
|
| 1147 | + * Extract UID from vcard |
|
| 1148 | + * |
|
| 1149 | + * @param string $cardData the vcard raw data |
|
| 1150 | + * @return string the uid |
|
| 1151 | + * @throws BadRequest if no UID is available |
|
| 1152 | + */ |
|
| 1153 | + private function getUID($cardData) { |
|
| 1154 | + if ($cardData != '') { |
|
| 1155 | + $vCard = Reader::read($cardData); |
|
| 1156 | + if ($vCard->UID) { |
|
| 1157 | + $uid = $vCard->UID->getValue(); |
|
| 1158 | + return $uid; |
|
| 1159 | + } |
|
| 1160 | + // should already be handled, but just in case |
|
| 1161 | + throw new BadRequest('vCards on CardDAV servers MUST have a UID property'); |
|
| 1162 | + } |
|
| 1163 | + // should already be handled, but just in case |
|
| 1164 | + throw new BadRequest('vCard can not be empty'); |
|
| 1165 | + } |
|
| 1166 | 1166 | } |
@@ -39,136 +39,136 @@ |
||
| 39 | 39 | */ |
| 40 | 40 | class SystemTagNode implements \Sabre\DAV\INode { |
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * @var ISystemTag |
|
| 44 | - */ |
|
| 45 | - protected $tag; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * @var ISystemTagManager |
|
| 49 | - */ |
|
| 50 | - protected $tagManager; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * User |
|
| 54 | - * |
|
| 55 | - * @var IUser |
|
| 56 | - */ |
|
| 57 | - protected $user; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Whether to allow permissions for admins |
|
| 61 | - * |
|
| 62 | - * @var bool |
|
| 63 | - */ |
|
| 64 | - protected $isAdmin; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Sets up the node, expects a full path name |
|
| 68 | - * |
|
| 69 | - * @param ISystemTag $tag system tag |
|
| 70 | - * @param IUser $user user |
|
| 71 | - * @param bool $isAdmin whether to allow operations for admins |
|
| 72 | - * @param ISystemTagManager $tagManager tag manager |
|
| 73 | - */ |
|
| 74 | - public function __construct(ISystemTag $tag, IUser $user, $isAdmin, ISystemTagManager $tagManager) { |
|
| 75 | - $this->tag = $tag; |
|
| 76 | - $this->user = $user; |
|
| 77 | - $this->isAdmin = $isAdmin; |
|
| 78 | - $this->tagManager = $tagManager; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Returns the id of the tag |
|
| 83 | - * |
|
| 84 | - * @return string |
|
| 85 | - */ |
|
| 86 | - public function getName() { |
|
| 87 | - return $this->tag->getId(); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Returns the system tag represented by this node |
|
| 92 | - * |
|
| 93 | - * @return ISystemTag system tag |
|
| 94 | - */ |
|
| 95 | - public function getSystemTag() { |
|
| 96 | - return $this->tag; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Renames the node |
|
| 101 | - * |
|
| 102 | - * @param string $name The new name |
|
| 103 | - * |
|
| 104 | - * @throws MethodNotAllowed not allowed to rename node |
|
| 105 | - */ |
|
| 106 | - public function setName($name) { |
|
| 107 | - throw new MethodNotAllowed(); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Update tag |
|
| 112 | - * |
|
| 113 | - * @param string $name new tag name |
|
| 114 | - * @param bool $userVisible user visible |
|
| 115 | - * @param bool $userAssignable user assignable |
|
| 116 | - * @throws NotFound whenever the given tag id does not exist |
|
| 117 | - * @throws Forbidden whenever there is no permission to update said tag |
|
| 118 | - * @throws Conflict whenever a tag already exists with the given attributes |
|
| 119 | - */ |
|
| 120 | - public function update($name, $userVisible, $userAssignable) { |
|
| 121 | - try { |
|
| 122 | - if (!$this->tagManager->canUserSeeTag($this->tag, $this->user)) { |
|
| 123 | - throw new NotFound('Tag with id ' . $this->tag->getId() . ' does not exist'); |
|
| 124 | - } |
|
| 125 | - if (!$this->tagManager->canUserAssignTag($this->tag, $this->user)) { |
|
| 126 | - throw new Forbidden('No permission to update tag ' . $this->tag->getId()); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - // only admin is able to change permissions, regular users can only rename |
|
| 130 | - if (!$this->isAdmin) { |
|
| 131 | - // only renaming is allowed for regular users |
|
| 132 | - if ($userVisible !== $this->tag->isUserVisible() |
|
| 133 | - || $userAssignable !== $this->tag->isUserAssignable() |
|
| 134 | - ) { |
|
| 135 | - throw new Forbidden('No permission to update permissions for tag ' . $this->tag->getId()); |
|
| 136 | - } |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - $this->tagManager->updateTag($this->tag->getId(), $name, $userVisible, $userAssignable); |
|
| 140 | - } catch (TagNotFoundException $e) { |
|
| 141 | - throw new NotFound('Tag with id ' . $this->tag->getId() . ' does not exist'); |
|
| 142 | - } catch (TagAlreadyExistsException $e) { |
|
| 143 | - throw new Conflict( |
|
| 144 | - 'Tag with the properties "' . $name . '", ' . |
|
| 145 | - $userVisible . ', ' . $userAssignable . ' already exists' |
|
| 146 | - ); |
|
| 147 | - } |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * Returns null, not supported |
|
| 152 | - * |
|
| 153 | - */ |
|
| 154 | - public function getLastModified() { |
|
| 155 | - return null; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - public function delete() { |
|
| 159 | - try { |
|
| 160 | - if (!$this->isAdmin) { |
|
| 161 | - throw new Forbidden('No permission to delete tag ' . $this->tag->getId()); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - if (!$this->tagManager->canUserSeeTag($this->tag, $this->user)) { |
|
| 165 | - throw new NotFound('Tag with id ' . $this->tag->getId() . ' not found'); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - $this->tagManager->deleteTags($this->tag->getId()); |
|
| 169 | - } catch (TagNotFoundException $e) { |
|
| 170 | - // can happen if concurrent deletion occurred |
|
| 171 | - throw new NotFound('Tag with id ' . $this->tag->getId() . ' not found', 0, $e); |
|
| 172 | - } |
|
| 173 | - } |
|
| 42 | + /** |
|
| 43 | + * @var ISystemTag |
|
| 44 | + */ |
|
| 45 | + protected $tag; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * @var ISystemTagManager |
|
| 49 | + */ |
|
| 50 | + protected $tagManager; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * User |
|
| 54 | + * |
|
| 55 | + * @var IUser |
|
| 56 | + */ |
|
| 57 | + protected $user; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Whether to allow permissions for admins |
|
| 61 | + * |
|
| 62 | + * @var bool |
|
| 63 | + */ |
|
| 64 | + protected $isAdmin; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Sets up the node, expects a full path name |
|
| 68 | + * |
|
| 69 | + * @param ISystemTag $tag system tag |
|
| 70 | + * @param IUser $user user |
|
| 71 | + * @param bool $isAdmin whether to allow operations for admins |
|
| 72 | + * @param ISystemTagManager $tagManager tag manager |
|
| 73 | + */ |
|
| 74 | + public function __construct(ISystemTag $tag, IUser $user, $isAdmin, ISystemTagManager $tagManager) { |
|
| 75 | + $this->tag = $tag; |
|
| 76 | + $this->user = $user; |
|
| 77 | + $this->isAdmin = $isAdmin; |
|
| 78 | + $this->tagManager = $tagManager; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Returns the id of the tag |
|
| 83 | + * |
|
| 84 | + * @return string |
|
| 85 | + */ |
|
| 86 | + public function getName() { |
|
| 87 | + return $this->tag->getId(); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Returns the system tag represented by this node |
|
| 92 | + * |
|
| 93 | + * @return ISystemTag system tag |
|
| 94 | + */ |
|
| 95 | + public function getSystemTag() { |
|
| 96 | + return $this->tag; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Renames the node |
|
| 101 | + * |
|
| 102 | + * @param string $name The new name |
|
| 103 | + * |
|
| 104 | + * @throws MethodNotAllowed not allowed to rename node |
|
| 105 | + */ |
|
| 106 | + public function setName($name) { |
|
| 107 | + throw new MethodNotAllowed(); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Update tag |
|
| 112 | + * |
|
| 113 | + * @param string $name new tag name |
|
| 114 | + * @param bool $userVisible user visible |
|
| 115 | + * @param bool $userAssignable user assignable |
|
| 116 | + * @throws NotFound whenever the given tag id does not exist |
|
| 117 | + * @throws Forbidden whenever there is no permission to update said tag |
|
| 118 | + * @throws Conflict whenever a tag already exists with the given attributes |
|
| 119 | + */ |
|
| 120 | + public function update($name, $userVisible, $userAssignable) { |
|
| 121 | + try { |
|
| 122 | + if (!$this->tagManager->canUserSeeTag($this->tag, $this->user)) { |
|
| 123 | + throw new NotFound('Tag with id ' . $this->tag->getId() . ' does not exist'); |
|
| 124 | + } |
|
| 125 | + if (!$this->tagManager->canUserAssignTag($this->tag, $this->user)) { |
|
| 126 | + throw new Forbidden('No permission to update tag ' . $this->tag->getId()); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + // only admin is able to change permissions, regular users can only rename |
|
| 130 | + if (!$this->isAdmin) { |
|
| 131 | + // only renaming is allowed for regular users |
|
| 132 | + if ($userVisible !== $this->tag->isUserVisible() |
|
| 133 | + || $userAssignable !== $this->tag->isUserAssignable() |
|
| 134 | + ) { |
|
| 135 | + throw new Forbidden('No permission to update permissions for tag ' . $this->tag->getId()); |
|
| 136 | + } |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + $this->tagManager->updateTag($this->tag->getId(), $name, $userVisible, $userAssignable); |
|
| 140 | + } catch (TagNotFoundException $e) { |
|
| 141 | + throw new NotFound('Tag with id ' . $this->tag->getId() . ' does not exist'); |
|
| 142 | + } catch (TagAlreadyExistsException $e) { |
|
| 143 | + throw new Conflict( |
|
| 144 | + 'Tag with the properties "' . $name . '", ' . |
|
| 145 | + $userVisible . ', ' . $userAssignable . ' already exists' |
|
| 146 | + ); |
|
| 147 | + } |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * Returns null, not supported |
|
| 152 | + * |
|
| 153 | + */ |
|
| 154 | + public function getLastModified() { |
|
| 155 | + return null; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + public function delete() { |
|
| 159 | + try { |
|
| 160 | + if (!$this->isAdmin) { |
|
| 161 | + throw new Forbidden('No permission to delete tag ' . $this->tag->getId()); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + if (!$this->tagManager->canUserSeeTag($this->tag, $this->user)) { |
|
| 165 | + throw new NotFound('Tag with id ' . $this->tag->getId() . ' not found'); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + $this->tagManager->deleteTags($this->tag->getId()); |
|
| 169 | + } catch (TagNotFoundException $e) { |
|
| 170 | + // can happen if concurrent deletion occurred |
|
| 171 | + throw new NotFound('Tag with id ' . $this->tag->getId() . ' not found', 0, $e); |
|
| 172 | + } |
|
| 173 | + } |
|
| 174 | 174 | } |
@@ -120,10 +120,10 @@ discard block |
||
| 120 | 120 | public function update($name, $userVisible, $userAssignable) { |
| 121 | 121 | try { |
| 122 | 122 | if (!$this->tagManager->canUserSeeTag($this->tag, $this->user)) { |
| 123 | - throw new NotFound('Tag with id ' . $this->tag->getId() . ' does not exist'); |
|
| 123 | + throw new NotFound('Tag with id '.$this->tag->getId().' does not exist'); |
|
| 124 | 124 | } |
| 125 | 125 | if (!$this->tagManager->canUserAssignTag($this->tag, $this->user)) { |
| 126 | - throw new Forbidden('No permission to update tag ' . $this->tag->getId()); |
|
| 126 | + throw new Forbidden('No permission to update tag '.$this->tag->getId()); |
|
| 127 | 127 | } |
| 128 | 128 | |
| 129 | 129 | // only admin is able to change permissions, regular users can only rename |
@@ -132,17 +132,17 @@ discard block |
||
| 132 | 132 | if ($userVisible !== $this->tag->isUserVisible() |
| 133 | 133 | || $userAssignable !== $this->tag->isUserAssignable() |
| 134 | 134 | ) { |
| 135 | - throw new Forbidden('No permission to update permissions for tag ' . $this->tag->getId()); |
|
| 135 | + throw new Forbidden('No permission to update permissions for tag '.$this->tag->getId()); |
|
| 136 | 136 | } |
| 137 | 137 | } |
| 138 | 138 | |
| 139 | 139 | $this->tagManager->updateTag($this->tag->getId(), $name, $userVisible, $userAssignable); |
| 140 | 140 | } catch (TagNotFoundException $e) { |
| 141 | - throw new NotFound('Tag with id ' . $this->tag->getId() . ' does not exist'); |
|
| 141 | + throw new NotFound('Tag with id '.$this->tag->getId().' does not exist'); |
|
| 142 | 142 | } catch (TagAlreadyExistsException $e) { |
| 143 | 143 | throw new Conflict( |
| 144 | - 'Tag with the properties "' . $name . '", ' . |
|
| 145 | - $userVisible . ', ' . $userAssignable . ' already exists' |
|
| 144 | + 'Tag with the properties "'.$name.'", '. |
|
| 145 | + $userVisible.', '.$userAssignable.' already exists' |
|
| 146 | 146 | ); |
| 147 | 147 | } |
| 148 | 148 | } |
@@ -158,17 +158,17 @@ discard block |
||
| 158 | 158 | public function delete() { |
| 159 | 159 | try { |
| 160 | 160 | if (!$this->isAdmin) { |
| 161 | - throw new Forbidden('No permission to delete tag ' . $this->tag->getId()); |
|
| 161 | + throw new Forbidden('No permission to delete tag '.$this->tag->getId()); |
|
| 162 | 162 | } |
| 163 | 163 | |
| 164 | 164 | if (!$this->tagManager->canUserSeeTag($this->tag, $this->user)) { |
| 165 | - throw new NotFound('Tag with id ' . $this->tag->getId() . ' not found'); |
|
| 165 | + throw new NotFound('Tag with id '.$this->tag->getId().' not found'); |
|
| 166 | 166 | } |
| 167 | 167 | |
| 168 | 168 | $this->tagManager->deleteTags($this->tag->getId()); |
| 169 | 169 | } catch (TagNotFoundException $e) { |
| 170 | 170 | // can happen if concurrent deletion occurred |
| 171 | - throw new NotFound('Tag with id ' . $this->tag->getId() . ' not found', 0, $e); |
|
| 171 | + throw new NotFound('Tag with id '.$this->tag->getId().' not found', 0, $e); |
|
| 172 | 172 | } |
| 173 | 173 | } |
| 174 | 174 | } |
@@ -37,144 +37,144 @@ |
||
| 37 | 37 | |
| 38 | 38 | class SystemTagsByIdCollection implements ICollection { |
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * @var ISystemTagManager |
|
| 42 | - */ |
|
| 43 | - private $tagManager; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @var IGroupManager |
|
| 47 | - */ |
|
| 48 | - private $groupManager; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @var IUserSession |
|
| 52 | - */ |
|
| 53 | - private $userSession; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * SystemTagsByIdCollection constructor. |
|
| 57 | - * |
|
| 58 | - * @param ISystemTagManager $tagManager |
|
| 59 | - * @param IUserSession $userSession |
|
| 60 | - * @param IGroupManager $groupManager |
|
| 61 | - */ |
|
| 62 | - public function __construct( |
|
| 63 | - ISystemTagManager $tagManager, |
|
| 64 | - IUserSession $userSession, |
|
| 65 | - IGroupManager $groupManager |
|
| 66 | - ) { |
|
| 67 | - $this->tagManager = $tagManager; |
|
| 68 | - $this->userSession = $userSession; |
|
| 69 | - $this->groupManager = $groupManager; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Returns whether the currently logged in user is an administrator |
|
| 74 | - * |
|
| 75 | - * @return bool true if the user is an admin |
|
| 76 | - */ |
|
| 77 | - private function isAdmin() { |
|
| 78 | - $user = $this->userSession->getUser(); |
|
| 79 | - if ($user !== null) { |
|
| 80 | - return $this->groupManager->isAdmin($user->getUID()); |
|
| 81 | - } |
|
| 82 | - return false; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * @param string $name |
|
| 87 | - * @param resource|string $data Initial payload |
|
| 88 | - * @throws Forbidden |
|
| 89 | - */ |
|
| 90 | - function createFile($name, $data = null) { |
|
| 91 | - throw new Forbidden('Cannot create tags by id'); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * @param string $name |
|
| 96 | - */ |
|
| 97 | - function createDirectory($name) { |
|
| 98 | - throw new Forbidden('Permission denied to create collections'); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * @param string $name |
|
| 103 | - */ |
|
| 104 | - function getChild($name) { |
|
| 105 | - try { |
|
| 106 | - $tag = $this->tagManager->getTagsByIds([$name]); |
|
| 107 | - $tag = current($tag); |
|
| 108 | - if (!$this->tagManager->canUserSeeTag($tag, $this->userSession->getUser())) { |
|
| 109 | - throw new NotFound('Tag with id ' . $name . ' not found'); |
|
| 110 | - } |
|
| 111 | - return $this->makeNode($tag); |
|
| 112 | - } catch (\InvalidArgumentException $e) { |
|
| 113 | - throw new BadRequest('Invalid tag id', 0, $e); |
|
| 114 | - } catch (TagNotFoundException $e) { |
|
| 115 | - throw new NotFound('Tag with id ' . $name . ' not found', 0, $e); |
|
| 116 | - } |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - function getChildren() { |
|
| 120 | - $visibilityFilter = true; |
|
| 121 | - if ($this->isAdmin()) { |
|
| 122 | - $visibilityFilter = null; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - $tags = $this->tagManager->getAllTags($visibilityFilter); |
|
| 126 | - return array_map(function($tag) { |
|
| 127 | - return $this->makeNode($tag); |
|
| 128 | - }, $tags); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * @param string $name |
|
| 133 | - */ |
|
| 134 | - function childExists($name) { |
|
| 135 | - try { |
|
| 136 | - $tag = $this->tagManager->getTagsByIds([$name]); |
|
| 137 | - $tag = current($tag); |
|
| 138 | - if (!$this->tagManager->canUserSeeTag($tag, $this->userSession->getUser())) { |
|
| 139 | - return false; |
|
| 140 | - } |
|
| 141 | - return true; |
|
| 142 | - } catch (\InvalidArgumentException $e) { |
|
| 143 | - throw new BadRequest('Invalid tag id', 0, $e); |
|
| 144 | - } catch (TagNotFoundException $e) { |
|
| 145 | - return false; |
|
| 146 | - } |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - function delete() { |
|
| 150 | - throw new Forbidden('Permission denied to delete this collection'); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - function getName() { |
|
| 154 | - return 'systemtags'; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - function setName($name) { |
|
| 158 | - throw new Forbidden('Permission denied to rename this collection'); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * Returns the last modification time, as a unix timestamp |
|
| 163 | - * |
|
| 164 | - * @return int |
|
| 165 | - */ |
|
| 166 | - function getLastModified() { |
|
| 167 | - return null; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * Create a sabre node for the given system tag |
|
| 172 | - * |
|
| 173 | - * @param ISystemTag $tag |
|
| 174 | - * |
|
| 175 | - * @return SystemTagNode |
|
| 176 | - */ |
|
| 177 | - private function makeNode(ISystemTag $tag) { |
|
| 178 | - return new SystemTagNode($tag, $this->userSession->getUser(), $this->isAdmin(), $this->tagManager); |
|
| 179 | - } |
|
| 40 | + /** |
|
| 41 | + * @var ISystemTagManager |
|
| 42 | + */ |
|
| 43 | + private $tagManager; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @var IGroupManager |
|
| 47 | + */ |
|
| 48 | + private $groupManager; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @var IUserSession |
|
| 52 | + */ |
|
| 53 | + private $userSession; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * SystemTagsByIdCollection constructor. |
|
| 57 | + * |
|
| 58 | + * @param ISystemTagManager $tagManager |
|
| 59 | + * @param IUserSession $userSession |
|
| 60 | + * @param IGroupManager $groupManager |
|
| 61 | + */ |
|
| 62 | + public function __construct( |
|
| 63 | + ISystemTagManager $tagManager, |
|
| 64 | + IUserSession $userSession, |
|
| 65 | + IGroupManager $groupManager |
|
| 66 | + ) { |
|
| 67 | + $this->tagManager = $tagManager; |
|
| 68 | + $this->userSession = $userSession; |
|
| 69 | + $this->groupManager = $groupManager; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Returns whether the currently logged in user is an administrator |
|
| 74 | + * |
|
| 75 | + * @return bool true if the user is an admin |
|
| 76 | + */ |
|
| 77 | + private function isAdmin() { |
|
| 78 | + $user = $this->userSession->getUser(); |
|
| 79 | + if ($user !== null) { |
|
| 80 | + return $this->groupManager->isAdmin($user->getUID()); |
|
| 81 | + } |
|
| 82 | + return false; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * @param string $name |
|
| 87 | + * @param resource|string $data Initial payload |
|
| 88 | + * @throws Forbidden |
|
| 89 | + */ |
|
| 90 | + function createFile($name, $data = null) { |
|
| 91 | + throw new Forbidden('Cannot create tags by id'); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * @param string $name |
|
| 96 | + */ |
|
| 97 | + function createDirectory($name) { |
|
| 98 | + throw new Forbidden('Permission denied to create collections'); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * @param string $name |
|
| 103 | + */ |
|
| 104 | + function getChild($name) { |
|
| 105 | + try { |
|
| 106 | + $tag = $this->tagManager->getTagsByIds([$name]); |
|
| 107 | + $tag = current($tag); |
|
| 108 | + if (!$this->tagManager->canUserSeeTag($tag, $this->userSession->getUser())) { |
|
| 109 | + throw new NotFound('Tag with id ' . $name . ' not found'); |
|
| 110 | + } |
|
| 111 | + return $this->makeNode($tag); |
|
| 112 | + } catch (\InvalidArgumentException $e) { |
|
| 113 | + throw new BadRequest('Invalid tag id', 0, $e); |
|
| 114 | + } catch (TagNotFoundException $e) { |
|
| 115 | + throw new NotFound('Tag with id ' . $name . ' not found', 0, $e); |
|
| 116 | + } |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + function getChildren() { |
|
| 120 | + $visibilityFilter = true; |
|
| 121 | + if ($this->isAdmin()) { |
|
| 122 | + $visibilityFilter = null; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + $tags = $this->tagManager->getAllTags($visibilityFilter); |
|
| 126 | + return array_map(function($tag) { |
|
| 127 | + return $this->makeNode($tag); |
|
| 128 | + }, $tags); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * @param string $name |
|
| 133 | + */ |
|
| 134 | + function childExists($name) { |
|
| 135 | + try { |
|
| 136 | + $tag = $this->tagManager->getTagsByIds([$name]); |
|
| 137 | + $tag = current($tag); |
|
| 138 | + if (!$this->tagManager->canUserSeeTag($tag, $this->userSession->getUser())) { |
|
| 139 | + return false; |
|
| 140 | + } |
|
| 141 | + return true; |
|
| 142 | + } catch (\InvalidArgumentException $e) { |
|
| 143 | + throw new BadRequest('Invalid tag id', 0, $e); |
|
| 144 | + } catch (TagNotFoundException $e) { |
|
| 145 | + return false; |
|
| 146 | + } |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + function delete() { |
|
| 150 | + throw new Forbidden('Permission denied to delete this collection'); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + function getName() { |
|
| 154 | + return 'systemtags'; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + function setName($name) { |
|
| 158 | + throw new Forbidden('Permission denied to rename this collection'); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * Returns the last modification time, as a unix timestamp |
|
| 163 | + * |
|
| 164 | + * @return int |
|
| 165 | + */ |
|
| 166 | + function getLastModified() { |
|
| 167 | + return null; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * Create a sabre node for the given system tag |
|
| 172 | + * |
|
| 173 | + * @param ISystemTag $tag |
|
| 174 | + * |
|
| 175 | + * @return SystemTagNode |
|
| 176 | + */ |
|
| 177 | + private function makeNode(ISystemTag $tag) { |
|
| 178 | + return new SystemTagNode($tag, $this->userSession->getUser(), $this->isAdmin(), $this->tagManager); |
|
| 179 | + } |
|
| 180 | 180 | } |
@@ -106,13 +106,13 @@ |
||
| 106 | 106 | $tag = $this->tagManager->getTagsByIds([$name]); |
| 107 | 107 | $tag = current($tag); |
| 108 | 108 | if (!$this->tagManager->canUserSeeTag($tag, $this->userSession->getUser())) { |
| 109 | - throw new NotFound('Tag with id ' . $name . ' not found'); |
|
| 109 | + throw new NotFound('Tag with id '.$name.' not found'); |
|
| 110 | 110 | } |
| 111 | 111 | return $this->makeNode($tag); |
| 112 | 112 | } catch (\InvalidArgumentException $e) { |
| 113 | 113 | throw new BadRequest('Invalid tag id', 0, $e); |
| 114 | 114 | } catch (TagNotFoundException $e) { |
| 115 | - throw new NotFound('Tag with id ' . $name . ' not found', 0, $e); |
|
| 115 | + throw new NotFound('Tag with id '.$name.' not found', 0, $e); |
|
| 116 | 116 | } |
| 117 | 117 | } |
| 118 | 118 | |