@@ -323,7 +323,7 @@ |
||
323 | 323 | /** |
324 | 324 | * find all available languages for an app |
325 | 325 | * @param string $app App that needs to be translated |
326 | - * @return array an array of available languages |
|
326 | + * @return string[] an array of available languages |
|
327 | 327 | * @deprecated 9.0.0 Use \OC::$server->getL10NFactory()->findAvailableLanguages() instead |
328 | 328 | */ |
329 | 329 | public static function findAvailableLanguages($app=null) { |
@@ -35,309 +35,309 @@ |
||
35 | 35 | * @deprecated 9.0.0 Use \OC::$server->getL10NFactory()->get() instead |
36 | 36 | */ |
37 | 37 | class OC_L10N implements \OCP\IL10N { |
38 | - /** |
|
39 | - * cache |
|
40 | - */ |
|
41 | - protected static $cache = array(); |
|
42 | - protected static $availableLanguages = array(); |
|
43 | - |
|
44 | - /** |
|
45 | - * The best language |
|
46 | - */ |
|
47 | - protected static $language = ''; |
|
48 | - |
|
49 | - /** |
|
50 | - * App of this object |
|
51 | - */ |
|
52 | - protected $app; |
|
53 | - |
|
54 | - /** |
|
55 | - * Language of this object |
|
56 | - */ |
|
57 | - protected $lang; |
|
58 | - |
|
59 | - /** |
|
60 | - * Translations |
|
61 | - */ |
|
62 | - private $translations = array(); |
|
63 | - |
|
64 | - /** |
|
65 | - * Plural forms (string) |
|
66 | - */ |
|
67 | - private $pluralFormString = 'nplurals=2; plural=(n != 1);'; |
|
68 | - |
|
69 | - /** |
|
70 | - * Plural forms (function) |
|
71 | - */ |
|
72 | - private $pluralFormFunction = null; |
|
73 | - |
|
74 | - /** |
|
75 | - * The constructor |
|
76 | - * @param string $app app requesting l10n |
|
77 | - * @param string $lang default: null Language |
|
78 | - * |
|
79 | - * If language is not set, the constructor tries to find the right |
|
80 | - * language. |
|
81 | - * @deprecated 9.0.0 Use \OC::$server->getL10NFactory()->get() instead |
|
82 | - */ |
|
83 | - public function __construct($app, $lang = null) { |
|
84 | - $app = \OC_App::cleanAppId($app); |
|
85 | - $this->app = $app; |
|
86 | - |
|
87 | - if ($lang !== null) { |
|
88 | - $lang = str_replace(array('\0', '/', '\\', '..'), '', $lang); |
|
89 | - } |
|
90 | - |
|
91 | - // Find the right language |
|
92 | - if ($app !== 'test' && !\OC::$server->getL10NFactory()->languageExists($app, $lang)) { |
|
93 | - $lang = \OC::$server->getL10NFactory()->findLanguage($app); |
|
94 | - } |
|
95 | - |
|
96 | - $this->lang = $lang; |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * @param $transFile |
|
101 | - * @return bool |
|
102 | - */ |
|
103 | - public function load($transFile) { |
|
104 | - $this->app = true; |
|
105 | - |
|
106 | - $json = json_decode(file_get_contents($transFile), true); |
|
107 | - if (!is_array($json)) { |
|
108 | - $jsonError = json_last_error(); |
|
109 | - \OC::$server->getLogger()->warning("Failed to load $transFile - json error code: $jsonError", ['app' => 'l10n']); |
|
110 | - return false; |
|
111 | - } |
|
112 | - |
|
113 | - $this->pluralFormString = $json['pluralForm']; |
|
114 | - $translations = $json['translations']; |
|
115 | - |
|
116 | - $this->translations = array_merge($this->translations, $translations); |
|
117 | - |
|
118 | - return true; |
|
119 | - } |
|
120 | - |
|
121 | - protected function init() { |
|
122 | - if ($this->app === true) { |
|
123 | - return; |
|
124 | - } |
|
125 | - $app = $this->app; |
|
126 | - $lang = $this->lang; |
|
127 | - $this->app = true; |
|
128 | - |
|
129 | - /** @var \OC\L10N\Factory $factory */ |
|
130 | - $factory = \OC::$server->getL10NFactory(); |
|
131 | - $languageFiles = $factory->getL10nFilesForApp($app, $lang); |
|
132 | - |
|
133 | - $this->translations = []; |
|
134 | - foreach ($languageFiles as $languageFile) { |
|
135 | - $this->load($languageFile); |
|
136 | - } |
|
137 | - } |
|
138 | - |
|
139 | - /** |
|
140 | - * Translating |
|
141 | - * @param string $text The text we need a translation for |
|
142 | - * @param array $parameters default:array() Parameters for sprintf |
|
143 | - * @return \OC_L10N_String Translation or the same text |
|
144 | - * |
|
145 | - * Returns the translation. If no translation is found, $text will be |
|
146 | - * returned. |
|
147 | - */ |
|
148 | - public function t($text, $parameters = array()) { |
|
149 | - return new OC_L10N_String($this, $text, $parameters); |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * Translating |
|
154 | - * @param string $text_singular the string to translate for exactly one object |
|
155 | - * @param string $text_plural the string to translate for n objects |
|
156 | - * @param integer $count Number of objects |
|
157 | - * @param array $parameters default:array() Parameters for sprintf |
|
158 | - * @return \OC_L10N_String Translation or the same text |
|
159 | - * |
|
160 | - * Returns the translation. If no translation is found, $text will be |
|
161 | - * returned. %n will be replaced with the number of objects. |
|
162 | - * |
|
163 | - * The correct plural is determined by the plural_forms-function |
|
164 | - * provided by the po file. |
|
165 | - * |
|
166 | - */ |
|
167 | - public function n($text_singular, $text_plural, $count, $parameters = array()) { |
|
168 | - $this->init(); |
|
169 | - $identifier = "_${text_singular}_::_${text_plural}_"; |
|
170 | - if( array_key_exists($identifier, $this->translations)) { |
|
171 | - return new OC_L10N_String( $this, $identifier, $parameters, $count ); |
|
172 | - }else{ |
|
173 | - if($count === 1) { |
|
174 | - return new OC_L10N_String($this, $text_singular, $parameters, $count); |
|
175 | - }else{ |
|
176 | - return new OC_L10N_String($this, $text_plural, $parameters, $count); |
|
177 | - } |
|
178 | - } |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * getTranslations |
|
183 | - * @return array Fetch all translations |
|
184 | - * |
|
185 | - * Returns an associative array with all translations |
|
186 | - */ |
|
187 | - public function getTranslations() { |
|
188 | - $this->init(); |
|
189 | - return $this->translations; |
|
190 | - } |
|
191 | - |
|
192 | - /** |
|
193 | - * getPluralFormFunction |
|
194 | - * @return string the plural form function |
|
195 | - * |
|
196 | - * returned function accepts the argument $n |
|
197 | - */ |
|
198 | - public function getPluralFormFunction() { |
|
199 | - $this->init(); |
|
200 | - if (is_null($this->pluralFormFunction)) { |
|
201 | - $this->pluralFormFunction = \OC::$server->getL10NFactory()->createPluralFunction($this->pluralFormString); |
|
202 | - } |
|
203 | - return $this->pluralFormFunction; |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * Localization |
|
208 | - * @param string $type Type of localization |
|
209 | - * @param array|int|string $data parameters for this localization |
|
210 | - * @param array $options |
|
211 | - * @return string|false |
|
212 | - * |
|
213 | - * Returns the localized data. |
|
214 | - * |
|
215 | - * Implemented types: |
|
216 | - * - date |
|
217 | - * - Creates a date |
|
218 | - * - params: timestamp (int/string) |
|
219 | - * - datetime |
|
220 | - * - Creates date and time |
|
221 | - * - params: timestamp (int/string) |
|
222 | - * - time |
|
223 | - * - Creates a time |
|
224 | - * - params: timestamp (int/string) |
|
225 | - * - firstday: Returns the first day of the week (0 sunday - 6 saturday) |
|
226 | - * - jsdate: Returns the short JS date format |
|
227 | - */ |
|
228 | - public function l($type, $data, $options = array()) { |
|
229 | - if ($type === 'firstday') { |
|
230 | - return $this->getFirstWeekDay(); |
|
231 | - } |
|
232 | - if ($type === 'jsdate') { |
|
233 | - return $this->getDateFormat(); |
|
234 | - } |
|
235 | - |
|
236 | - $this->init(); |
|
237 | - $value = new DateTime(); |
|
238 | - if($data instanceof DateTime) { |
|
239 | - $value = $data; |
|
240 | - } elseif(is_string($data) && !is_numeric($data)) { |
|
241 | - $data = strtotime($data); |
|
242 | - $value->setTimestamp($data); |
|
243 | - } else { |
|
244 | - $value->setTimestamp($data); |
|
245 | - } |
|
246 | - |
|
247 | - // Use the language of the instance |
|
248 | - $locale = $this->transformToCLDRLocale($this->getLanguageCode()); |
|
249 | - |
|
250 | - $options = array_merge(array('width' => 'long'), $options); |
|
251 | - $width = $options['width']; |
|
252 | - switch($type) { |
|
253 | - case 'date': |
|
254 | - return Punic\Calendar::formatDate($value, $width, $locale); |
|
255 | - case 'datetime': |
|
256 | - return Punic\Calendar::formatDatetime($value, $width, $locale); |
|
257 | - case 'time': |
|
258 | - return Punic\Calendar::formatTime($value, $width, $locale); |
|
259 | - default: |
|
260 | - return false; |
|
261 | - } |
|
262 | - } |
|
263 | - |
|
264 | - /** |
|
265 | - * The code (en, de, ...) of the language that is used for this OC_L10N object |
|
266 | - * |
|
267 | - * @return string language |
|
268 | - */ |
|
269 | - public function getLanguageCode() { |
|
270 | - return $this->lang; |
|
271 | - } |
|
272 | - |
|
273 | - /** |
|
274 | - * @return string |
|
275 | - * @throws \Punic\Exception\ValueNotInList |
|
276 | - * @deprecated 9.0.0 Use $this->l('jsdate', null) instead |
|
277 | - */ |
|
278 | - public function getDateFormat() { |
|
279 | - $locale = $this->transformToCLDRLocale($this->getLanguageCode()); |
|
280 | - return Punic\Calendar::getDateFormat('short', $locale); |
|
281 | - } |
|
282 | - |
|
283 | - /** |
|
284 | - * @return int |
|
285 | - * @deprecated 9.0.0 Use $this->l('firstday', null) instead |
|
286 | - */ |
|
287 | - public function getFirstWeekDay() { |
|
288 | - $locale = $this->transformToCLDRLocale($this->getLanguageCode()); |
|
289 | - return Punic\Calendar::getFirstWeekday($locale); |
|
290 | - } |
|
291 | - |
|
292 | - /** |
|
293 | - * @param string $locale |
|
294 | - * @return string |
|
295 | - */ |
|
296 | - private function transformToCLDRLocale($locale) { |
|
297 | - if ($locale === 'sr@latin') { |
|
298 | - return 'sr_latn'; |
|
299 | - } |
|
300 | - |
|
301 | - return $locale; |
|
302 | - } |
|
303 | - |
|
304 | - /** |
|
305 | - * find the best language |
|
306 | - * @param string $app |
|
307 | - * @return string language |
|
308 | - * |
|
309 | - * If nothing works it returns 'en' |
|
310 | - * @deprecated 9.0.0 Use \OC::$server->getL10NFactory()->findLanguage() instead |
|
311 | - */ |
|
312 | - public static function findLanguage($app = null) { |
|
313 | - return \OC::$server->getL10NFactory()->findLanguage($app); |
|
314 | - } |
|
315 | - |
|
316 | - /** |
|
317 | - * @return string |
|
318 | - * @deprecated 9.0.0 Use \OC::$server->getL10NFactory()->setLanguageFromRequest() instead |
|
319 | - */ |
|
320 | - public static function setLanguageFromRequest() { |
|
321 | - return \OC::$server->getL10NFactory()->setLanguageFromRequest(); |
|
322 | - } |
|
323 | - |
|
324 | - /** |
|
325 | - * find all available languages for an app |
|
326 | - * @param string $app App that needs to be translated |
|
327 | - * @return array an array of available languages |
|
328 | - * @deprecated 9.0.0 Use \OC::$server->getL10NFactory()->findAvailableLanguages() instead |
|
329 | - */ |
|
330 | - public static function findAvailableLanguages($app=null) { |
|
331 | - return \OC::$server->getL10NFactory()->findAvailableLanguages($app); |
|
332 | - } |
|
333 | - |
|
334 | - /** |
|
335 | - * @param string $app |
|
336 | - * @param string $lang |
|
337 | - * @return bool |
|
338 | - * @deprecated 9.0.0 Use \OC::$server->getL10NFactory()->languageExists() instead |
|
339 | - */ |
|
340 | - public static function languageExists($app, $lang) { |
|
341 | - return \OC::$server->getL10NFactory()->languageExists($app, $lang); |
|
342 | - } |
|
38 | + /** |
|
39 | + * cache |
|
40 | + */ |
|
41 | + protected static $cache = array(); |
|
42 | + protected static $availableLanguages = array(); |
|
43 | + |
|
44 | + /** |
|
45 | + * The best language |
|
46 | + */ |
|
47 | + protected static $language = ''; |
|
48 | + |
|
49 | + /** |
|
50 | + * App of this object |
|
51 | + */ |
|
52 | + protected $app; |
|
53 | + |
|
54 | + /** |
|
55 | + * Language of this object |
|
56 | + */ |
|
57 | + protected $lang; |
|
58 | + |
|
59 | + /** |
|
60 | + * Translations |
|
61 | + */ |
|
62 | + private $translations = array(); |
|
63 | + |
|
64 | + /** |
|
65 | + * Plural forms (string) |
|
66 | + */ |
|
67 | + private $pluralFormString = 'nplurals=2; plural=(n != 1);'; |
|
68 | + |
|
69 | + /** |
|
70 | + * Plural forms (function) |
|
71 | + */ |
|
72 | + private $pluralFormFunction = null; |
|
73 | + |
|
74 | + /** |
|
75 | + * The constructor |
|
76 | + * @param string $app app requesting l10n |
|
77 | + * @param string $lang default: null Language |
|
78 | + * |
|
79 | + * If language is not set, the constructor tries to find the right |
|
80 | + * language. |
|
81 | + * @deprecated 9.0.0 Use \OC::$server->getL10NFactory()->get() instead |
|
82 | + */ |
|
83 | + public function __construct($app, $lang = null) { |
|
84 | + $app = \OC_App::cleanAppId($app); |
|
85 | + $this->app = $app; |
|
86 | + |
|
87 | + if ($lang !== null) { |
|
88 | + $lang = str_replace(array('\0', '/', '\\', '..'), '', $lang); |
|
89 | + } |
|
90 | + |
|
91 | + // Find the right language |
|
92 | + if ($app !== 'test' && !\OC::$server->getL10NFactory()->languageExists($app, $lang)) { |
|
93 | + $lang = \OC::$server->getL10NFactory()->findLanguage($app); |
|
94 | + } |
|
95 | + |
|
96 | + $this->lang = $lang; |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * @param $transFile |
|
101 | + * @return bool |
|
102 | + */ |
|
103 | + public function load($transFile) { |
|
104 | + $this->app = true; |
|
105 | + |
|
106 | + $json = json_decode(file_get_contents($transFile), true); |
|
107 | + if (!is_array($json)) { |
|
108 | + $jsonError = json_last_error(); |
|
109 | + \OC::$server->getLogger()->warning("Failed to load $transFile - json error code: $jsonError", ['app' => 'l10n']); |
|
110 | + return false; |
|
111 | + } |
|
112 | + |
|
113 | + $this->pluralFormString = $json['pluralForm']; |
|
114 | + $translations = $json['translations']; |
|
115 | + |
|
116 | + $this->translations = array_merge($this->translations, $translations); |
|
117 | + |
|
118 | + return true; |
|
119 | + } |
|
120 | + |
|
121 | + protected function init() { |
|
122 | + if ($this->app === true) { |
|
123 | + return; |
|
124 | + } |
|
125 | + $app = $this->app; |
|
126 | + $lang = $this->lang; |
|
127 | + $this->app = true; |
|
128 | + |
|
129 | + /** @var \OC\L10N\Factory $factory */ |
|
130 | + $factory = \OC::$server->getL10NFactory(); |
|
131 | + $languageFiles = $factory->getL10nFilesForApp($app, $lang); |
|
132 | + |
|
133 | + $this->translations = []; |
|
134 | + foreach ($languageFiles as $languageFile) { |
|
135 | + $this->load($languageFile); |
|
136 | + } |
|
137 | + } |
|
138 | + |
|
139 | + /** |
|
140 | + * Translating |
|
141 | + * @param string $text The text we need a translation for |
|
142 | + * @param array $parameters default:array() Parameters for sprintf |
|
143 | + * @return \OC_L10N_String Translation or the same text |
|
144 | + * |
|
145 | + * Returns the translation. If no translation is found, $text will be |
|
146 | + * returned. |
|
147 | + */ |
|
148 | + public function t($text, $parameters = array()) { |
|
149 | + return new OC_L10N_String($this, $text, $parameters); |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * Translating |
|
154 | + * @param string $text_singular the string to translate for exactly one object |
|
155 | + * @param string $text_plural the string to translate for n objects |
|
156 | + * @param integer $count Number of objects |
|
157 | + * @param array $parameters default:array() Parameters for sprintf |
|
158 | + * @return \OC_L10N_String Translation or the same text |
|
159 | + * |
|
160 | + * Returns the translation. If no translation is found, $text will be |
|
161 | + * returned. %n will be replaced with the number of objects. |
|
162 | + * |
|
163 | + * The correct plural is determined by the plural_forms-function |
|
164 | + * provided by the po file. |
|
165 | + * |
|
166 | + */ |
|
167 | + public function n($text_singular, $text_plural, $count, $parameters = array()) { |
|
168 | + $this->init(); |
|
169 | + $identifier = "_${text_singular}_::_${text_plural}_"; |
|
170 | + if( array_key_exists($identifier, $this->translations)) { |
|
171 | + return new OC_L10N_String( $this, $identifier, $parameters, $count ); |
|
172 | + }else{ |
|
173 | + if($count === 1) { |
|
174 | + return new OC_L10N_String($this, $text_singular, $parameters, $count); |
|
175 | + }else{ |
|
176 | + return new OC_L10N_String($this, $text_plural, $parameters, $count); |
|
177 | + } |
|
178 | + } |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * getTranslations |
|
183 | + * @return array Fetch all translations |
|
184 | + * |
|
185 | + * Returns an associative array with all translations |
|
186 | + */ |
|
187 | + public function getTranslations() { |
|
188 | + $this->init(); |
|
189 | + return $this->translations; |
|
190 | + } |
|
191 | + |
|
192 | + /** |
|
193 | + * getPluralFormFunction |
|
194 | + * @return string the plural form function |
|
195 | + * |
|
196 | + * returned function accepts the argument $n |
|
197 | + */ |
|
198 | + public function getPluralFormFunction() { |
|
199 | + $this->init(); |
|
200 | + if (is_null($this->pluralFormFunction)) { |
|
201 | + $this->pluralFormFunction = \OC::$server->getL10NFactory()->createPluralFunction($this->pluralFormString); |
|
202 | + } |
|
203 | + return $this->pluralFormFunction; |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * Localization |
|
208 | + * @param string $type Type of localization |
|
209 | + * @param array|int|string $data parameters for this localization |
|
210 | + * @param array $options |
|
211 | + * @return string|false |
|
212 | + * |
|
213 | + * Returns the localized data. |
|
214 | + * |
|
215 | + * Implemented types: |
|
216 | + * - date |
|
217 | + * - Creates a date |
|
218 | + * - params: timestamp (int/string) |
|
219 | + * - datetime |
|
220 | + * - Creates date and time |
|
221 | + * - params: timestamp (int/string) |
|
222 | + * - time |
|
223 | + * - Creates a time |
|
224 | + * - params: timestamp (int/string) |
|
225 | + * - firstday: Returns the first day of the week (0 sunday - 6 saturday) |
|
226 | + * - jsdate: Returns the short JS date format |
|
227 | + */ |
|
228 | + public function l($type, $data, $options = array()) { |
|
229 | + if ($type === 'firstday') { |
|
230 | + return $this->getFirstWeekDay(); |
|
231 | + } |
|
232 | + if ($type === 'jsdate') { |
|
233 | + return $this->getDateFormat(); |
|
234 | + } |
|
235 | + |
|
236 | + $this->init(); |
|
237 | + $value = new DateTime(); |
|
238 | + if($data instanceof DateTime) { |
|
239 | + $value = $data; |
|
240 | + } elseif(is_string($data) && !is_numeric($data)) { |
|
241 | + $data = strtotime($data); |
|
242 | + $value->setTimestamp($data); |
|
243 | + } else { |
|
244 | + $value->setTimestamp($data); |
|
245 | + } |
|
246 | + |
|
247 | + // Use the language of the instance |
|
248 | + $locale = $this->transformToCLDRLocale($this->getLanguageCode()); |
|
249 | + |
|
250 | + $options = array_merge(array('width' => 'long'), $options); |
|
251 | + $width = $options['width']; |
|
252 | + switch($type) { |
|
253 | + case 'date': |
|
254 | + return Punic\Calendar::formatDate($value, $width, $locale); |
|
255 | + case 'datetime': |
|
256 | + return Punic\Calendar::formatDatetime($value, $width, $locale); |
|
257 | + case 'time': |
|
258 | + return Punic\Calendar::formatTime($value, $width, $locale); |
|
259 | + default: |
|
260 | + return false; |
|
261 | + } |
|
262 | + } |
|
263 | + |
|
264 | + /** |
|
265 | + * The code (en, de, ...) of the language that is used for this OC_L10N object |
|
266 | + * |
|
267 | + * @return string language |
|
268 | + */ |
|
269 | + public function getLanguageCode() { |
|
270 | + return $this->lang; |
|
271 | + } |
|
272 | + |
|
273 | + /** |
|
274 | + * @return string |
|
275 | + * @throws \Punic\Exception\ValueNotInList |
|
276 | + * @deprecated 9.0.0 Use $this->l('jsdate', null) instead |
|
277 | + */ |
|
278 | + public function getDateFormat() { |
|
279 | + $locale = $this->transformToCLDRLocale($this->getLanguageCode()); |
|
280 | + return Punic\Calendar::getDateFormat('short', $locale); |
|
281 | + } |
|
282 | + |
|
283 | + /** |
|
284 | + * @return int |
|
285 | + * @deprecated 9.0.0 Use $this->l('firstday', null) instead |
|
286 | + */ |
|
287 | + public function getFirstWeekDay() { |
|
288 | + $locale = $this->transformToCLDRLocale($this->getLanguageCode()); |
|
289 | + return Punic\Calendar::getFirstWeekday($locale); |
|
290 | + } |
|
291 | + |
|
292 | + /** |
|
293 | + * @param string $locale |
|
294 | + * @return string |
|
295 | + */ |
|
296 | + private function transformToCLDRLocale($locale) { |
|
297 | + if ($locale === 'sr@latin') { |
|
298 | + return 'sr_latn'; |
|
299 | + } |
|
300 | + |
|
301 | + return $locale; |
|
302 | + } |
|
303 | + |
|
304 | + /** |
|
305 | + * find the best language |
|
306 | + * @param string $app |
|
307 | + * @return string language |
|
308 | + * |
|
309 | + * If nothing works it returns 'en' |
|
310 | + * @deprecated 9.0.0 Use \OC::$server->getL10NFactory()->findLanguage() instead |
|
311 | + */ |
|
312 | + public static function findLanguage($app = null) { |
|
313 | + return \OC::$server->getL10NFactory()->findLanguage($app); |
|
314 | + } |
|
315 | + |
|
316 | + /** |
|
317 | + * @return string |
|
318 | + * @deprecated 9.0.0 Use \OC::$server->getL10NFactory()->setLanguageFromRequest() instead |
|
319 | + */ |
|
320 | + public static function setLanguageFromRequest() { |
|
321 | + return \OC::$server->getL10NFactory()->setLanguageFromRequest(); |
|
322 | + } |
|
323 | + |
|
324 | + /** |
|
325 | + * find all available languages for an app |
|
326 | + * @param string $app App that needs to be translated |
|
327 | + * @return array an array of available languages |
|
328 | + * @deprecated 9.0.0 Use \OC::$server->getL10NFactory()->findAvailableLanguages() instead |
|
329 | + */ |
|
330 | + public static function findAvailableLanguages($app=null) { |
|
331 | + return \OC::$server->getL10NFactory()->findAvailableLanguages($app); |
|
332 | + } |
|
333 | + |
|
334 | + /** |
|
335 | + * @param string $app |
|
336 | + * @param string $lang |
|
337 | + * @return bool |
|
338 | + * @deprecated 9.0.0 Use \OC::$server->getL10NFactory()->languageExists() instead |
|
339 | + */ |
|
340 | + public static function languageExists($app, $lang) { |
|
341 | + return \OC::$server->getL10NFactory()->languageExists($app, $lang); |
|
342 | + } |
|
343 | 343 | } |
@@ -289,6 +289,9 @@ discard block |
||
289 | 289 | return $calendar; |
290 | 290 | } |
291 | 291 | |
292 | + /** |
|
293 | + * @param integer $calendarId |
|
294 | + */ |
|
292 | 295 | public function getCalendarById($calendarId) { |
293 | 296 | $fields = array_values($this->propertyMap); |
294 | 297 | $fields[] = 'id'; |
@@ -597,7 +600,7 @@ discard block |
||
597 | 600 | * calendar-data. If the result of a subsequent GET to this object is not |
598 | 601 | * the exact same as this request body, you should omit the ETag. |
599 | 602 | * |
600 | - * @param mixed $calendarId |
|
603 | + * @param integer $calendarId |
|
601 | 604 | * @param string $objectUri |
602 | 605 | * @param string $calendarData |
603 | 606 | * @return string |
@@ -1036,7 +1039,7 @@ discard block |
||
1036 | 1039 | * @param string $principalUri |
1037 | 1040 | * @param string $uri |
1038 | 1041 | * @param array $properties |
1039 | - * @return mixed |
|
1042 | + * @return integer |
|
1040 | 1043 | */ |
1041 | 1044 | function createSubscription($principalUri, $uri, array $properties) { |
1042 | 1045 | |
@@ -1377,6 +1380,9 @@ discard block |
||
1377 | 1380 | return $this->sharingBackend->applyShareAcl($resourceId, $acl); |
1378 | 1381 | } |
1379 | 1382 | |
1383 | + /** |
|
1384 | + * @param boolean $toV2 |
|
1385 | + */ |
|
1380 | 1386 | private function convertPrincipal($principalUri, $toV2) { |
1381 | 1387 | if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
1382 | 1388 | list(, $name) = URLUtil::splitPath($principalUri); |
@@ -51,1341 +51,1341 @@ |
||
51 | 51 | */ |
52 | 52 | class CalDavBackend extends AbstractBackend implements SyncSupport, SubscriptionSupport, SchedulingSupport { |
53 | 53 | |
54 | - /** |
|
55 | - * We need to specify a max date, because we need to stop *somewhere* |
|
56 | - * |
|
57 | - * On 32 bit system the maximum for a signed integer is 2147483647, so |
|
58 | - * MAX_DATE cannot be higher than date('Y-m-d', 2147483647) which results |
|
59 | - * in 2038-01-19 to avoid problems when the date is converted |
|
60 | - * to a unix timestamp. |
|
61 | - */ |
|
62 | - const MAX_DATE = '2038-01-01'; |
|
63 | - |
|
64 | - /** |
|
65 | - * List of CalDAV properties, and how they map to database field names |
|
66 | - * Add your own properties by simply adding on to this array. |
|
67 | - * |
|
68 | - * Note that only string-based properties are supported here. |
|
69 | - * |
|
70 | - * @var array |
|
71 | - */ |
|
72 | - public $propertyMap = [ |
|
73 | - '{DAV:}displayname' => 'displayname', |
|
74 | - '{urn:ietf:params:xml:ns:caldav}calendar-description' => 'description', |
|
75 | - '{urn:ietf:params:xml:ns:caldav}calendar-timezone' => 'timezone', |
|
76 | - '{http://apple.com/ns/ical/}calendar-order' => 'calendarorder', |
|
77 | - '{http://apple.com/ns/ical/}calendar-color' => 'calendarcolor', |
|
78 | - ]; |
|
79 | - |
|
80 | - /** |
|
81 | - * List of subscription properties, and how they map to database field names. |
|
82 | - * |
|
83 | - * @var array |
|
84 | - */ |
|
85 | - public $subscriptionPropertyMap = [ |
|
86 | - '{DAV:}displayname' => 'displayname', |
|
87 | - '{http://apple.com/ns/ical/}refreshrate' => 'refreshrate', |
|
88 | - '{http://apple.com/ns/ical/}calendar-order' => 'calendarorder', |
|
89 | - '{http://apple.com/ns/ical/}calendar-color' => 'calendarcolor', |
|
90 | - '{http://calendarserver.org/ns/}subscribed-strip-todos' => 'striptodos', |
|
91 | - '{http://calendarserver.org/ns/}subscribed-strip-alarms' => 'stripalarms', |
|
92 | - '{http://calendarserver.org/ns/}subscribed-strip-attachments' => 'stripattachments', |
|
93 | - ]; |
|
94 | - |
|
95 | - /** @var IDBConnection */ |
|
96 | - private $db; |
|
97 | - |
|
98 | - /** @var Backend */ |
|
99 | - private $sharingBackend; |
|
100 | - |
|
101 | - /** @var Principal */ |
|
102 | - private $principalBackend; |
|
103 | - |
|
104 | - /** |
|
105 | - * CalDavBackend constructor. |
|
106 | - * |
|
107 | - * @param IDBConnection $db |
|
108 | - * @param Principal $principalBackend |
|
109 | - */ |
|
110 | - public function __construct(IDBConnection $db, Principal $principalBackend) { |
|
111 | - $this->db = $db; |
|
112 | - $this->principalBackend = $principalBackend; |
|
113 | - $this->sharingBackend = new Backend($this->db, $principalBackend, 'calendar'); |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * Returns a list of calendars for a principal. |
|
118 | - * |
|
119 | - * Every project is an array with the following keys: |
|
120 | - * * id, a unique id that will be used by other functions to modify the |
|
121 | - * calendar. This can be the same as the uri or a database key. |
|
122 | - * * uri, which the basename of the uri with which the calendar is |
|
123 | - * accessed. |
|
124 | - * * principaluri. The owner of the calendar. Almost always the same as |
|
125 | - * principalUri passed to this method. |
|
126 | - * |
|
127 | - * Furthermore it can contain webdav properties in clark notation. A very |
|
128 | - * common one is '{DAV:}displayname'. |
|
129 | - * |
|
130 | - * Many clients also require: |
|
131 | - * {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
|
132 | - * For this property, you can just return an instance of |
|
133 | - * Sabre\CalDAV\Property\SupportedCalendarComponentSet. |
|
134 | - * |
|
135 | - * If you return {http://sabredav.org/ns}read-only and set the value to 1, |
|
136 | - * ACL will automatically be put in read-only mode. |
|
137 | - * |
|
138 | - * @param string $principalUri |
|
139 | - * @return array |
|
140 | - */ |
|
141 | - function getCalendarsForUser($principalUri) { |
|
142 | - $principalUriOriginal = $principalUri; |
|
143 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
144 | - $fields = array_values($this->propertyMap); |
|
145 | - $fields[] = 'id'; |
|
146 | - $fields[] = 'uri'; |
|
147 | - $fields[] = 'synctoken'; |
|
148 | - $fields[] = 'components'; |
|
149 | - $fields[] = 'principaluri'; |
|
150 | - $fields[] = 'transparent'; |
|
151 | - |
|
152 | - // Making fields a comma-delimited list |
|
153 | - $query = $this->db->getQueryBuilder(); |
|
154 | - $query->select($fields)->from('calendars') |
|
155 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
156 | - ->orderBy('calendarorder', 'ASC'); |
|
157 | - $stmt = $query->execute(); |
|
158 | - |
|
159 | - $calendars = []; |
|
160 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
161 | - |
|
162 | - $components = []; |
|
163 | - if ($row['components']) { |
|
164 | - $components = explode(',',$row['components']); |
|
165 | - } |
|
166 | - |
|
167 | - $calendar = [ |
|
168 | - 'id' => $row['id'], |
|
169 | - 'uri' => $row['uri'], |
|
170 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
|
171 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
172 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
173 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
174 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
175 | - ]; |
|
176 | - |
|
177 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
178 | - $calendar[$xmlName] = $row[$dbName]; |
|
179 | - } |
|
180 | - |
|
181 | - if (!isset($calendars[$calendar['id']])) { |
|
182 | - $calendars[$calendar['id']] = $calendar; |
|
183 | - } |
|
184 | - } |
|
185 | - |
|
186 | - $stmt->closeCursor(); |
|
187 | - |
|
188 | - // query for shared calendars |
|
189 | - $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); |
|
190 | - $principals[]= $principalUri; |
|
191 | - |
|
192 | - $fields = array_values($this->propertyMap); |
|
193 | - $fields[] = 'a.id'; |
|
194 | - $fields[] = 'a.uri'; |
|
195 | - $fields[] = 'a.synctoken'; |
|
196 | - $fields[] = 'a.components'; |
|
197 | - $fields[] = 'a.principaluri'; |
|
198 | - $fields[] = 'a.transparent'; |
|
199 | - $fields[] = 's.access'; |
|
200 | - $query = $this->db->getQueryBuilder(); |
|
201 | - $result = $query->select($fields) |
|
202 | - ->from('dav_shares', 's') |
|
203 | - ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
204 | - ->where($query->expr()->in('s.principaluri', $query->createParameter('principaluri'))) |
|
205 | - ->andWhere($query->expr()->eq('s.type', $query->createParameter('type'))) |
|
206 | - ->setParameter('type', 'calendar') |
|
207 | - ->setParameter('principaluri', $principals, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY) |
|
208 | - ->execute(); |
|
209 | - |
|
210 | - while($row = $result->fetch()) { |
|
211 | - list(, $name) = URLUtil::splitPath($row['principaluri']); |
|
212 | - $uri = $row['uri'] . '_shared_by_' . $name; |
|
213 | - $row['displayname'] = $row['displayname'] . "($name)"; |
|
214 | - $components = []; |
|
215 | - if ($row['components']) { |
|
216 | - $components = explode(',',$row['components']); |
|
217 | - } |
|
218 | - $calendar = [ |
|
219 | - 'id' => $row['id'], |
|
220 | - 'uri' => $uri, |
|
221 | - 'principaluri' => $principalUri, |
|
222 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
223 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
224 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
225 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
226 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $row['principaluri'], |
|
227 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ, |
|
228 | - ]; |
|
229 | - |
|
230 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
231 | - $calendar[$xmlName] = $row[$dbName]; |
|
232 | - } |
|
233 | - |
|
234 | - if (!isset($calendars[$calendar['id']])) { |
|
235 | - $calendars[$calendar['id']] = $calendar; |
|
236 | - } |
|
237 | - } |
|
238 | - $result->closeCursor(); |
|
239 | - |
|
240 | - return array_values($calendars); |
|
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * @param string $principal |
|
245 | - * @param string $uri |
|
246 | - * @return array|null |
|
247 | - */ |
|
248 | - public function getCalendarByUri($principal, $uri) { |
|
249 | - $fields = array_values($this->propertyMap); |
|
250 | - $fields[] = 'id'; |
|
251 | - $fields[] = 'uri'; |
|
252 | - $fields[] = 'synctoken'; |
|
253 | - $fields[] = 'components'; |
|
254 | - $fields[] = 'principaluri'; |
|
255 | - $fields[] = 'transparent'; |
|
256 | - |
|
257 | - // Making fields a comma-delimited list |
|
258 | - $query = $this->db->getQueryBuilder(); |
|
259 | - $query->select($fields)->from('calendars') |
|
260 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
261 | - ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal))) |
|
262 | - ->setMaxResults(1); |
|
263 | - $stmt = $query->execute(); |
|
264 | - |
|
265 | - $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
266 | - $stmt->closeCursor(); |
|
267 | - if ($row === false) { |
|
268 | - return null; |
|
269 | - } |
|
270 | - |
|
271 | - $components = []; |
|
272 | - if ($row['components']) { |
|
273 | - $components = explode(',',$row['components']); |
|
274 | - } |
|
275 | - |
|
276 | - $calendar = [ |
|
277 | - 'id' => $row['id'], |
|
278 | - 'uri' => $row['uri'], |
|
279 | - 'principaluri' => $row['principaluri'], |
|
280 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
281 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
282 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
283 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
284 | - ]; |
|
285 | - |
|
286 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
287 | - $calendar[$xmlName] = $row[$dbName]; |
|
288 | - } |
|
289 | - |
|
290 | - return $calendar; |
|
291 | - } |
|
292 | - |
|
293 | - public function getCalendarById($calendarId) { |
|
294 | - $fields = array_values($this->propertyMap); |
|
295 | - $fields[] = 'id'; |
|
296 | - $fields[] = 'uri'; |
|
297 | - $fields[] = 'synctoken'; |
|
298 | - $fields[] = 'components'; |
|
299 | - $fields[] = 'principaluri'; |
|
300 | - $fields[] = 'transparent'; |
|
301 | - |
|
302 | - // Making fields a comma-delimited list |
|
303 | - $query = $this->db->getQueryBuilder(); |
|
304 | - $query->select($fields)->from('calendars') |
|
305 | - ->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))) |
|
306 | - ->setMaxResults(1); |
|
307 | - $stmt = $query->execute(); |
|
308 | - |
|
309 | - $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
310 | - $stmt->closeCursor(); |
|
311 | - if ($row === false) { |
|
312 | - return null; |
|
313 | - } |
|
314 | - |
|
315 | - $components = []; |
|
316 | - if ($row['components']) { |
|
317 | - $components = explode(',',$row['components']); |
|
318 | - } |
|
319 | - |
|
320 | - $calendar = [ |
|
321 | - 'id' => $row['id'], |
|
322 | - 'uri' => $row['uri'], |
|
323 | - 'principaluri' => $row['principaluri'], |
|
324 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
325 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
326 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
327 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
328 | - ]; |
|
329 | - |
|
330 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
331 | - $calendar[$xmlName] = $row[$dbName]; |
|
332 | - } |
|
333 | - |
|
334 | - return $calendar; |
|
335 | - } |
|
336 | - |
|
337 | - /** |
|
338 | - * Creates a new calendar for a principal. |
|
339 | - * |
|
340 | - * If the creation was a success, an id must be returned that can be used to reference |
|
341 | - * this calendar in other methods, such as updateCalendar. |
|
342 | - * |
|
343 | - * @param string $principalUri |
|
344 | - * @param string $calendarUri |
|
345 | - * @param array $properties |
|
346 | - * @return int |
|
347 | - */ |
|
348 | - function createCalendar($principalUri, $calendarUri, array $properties) { |
|
349 | - $values = [ |
|
350 | - 'principaluri' => $principalUri, |
|
351 | - 'uri' => $calendarUri, |
|
352 | - 'synctoken' => 1, |
|
353 | - 'transparent' => 0, |
|
354 | - 'components' => 'VEVENT,VTODO', |
|
355 | - 'displayname' => $calendarUri |
|
356 | - ]; |
|
357 | - |
|
358 | - // Default value |
|
359 | - $sccs = '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set'; |
|
360 | - if (isset($properties[$sccs])) { |
|
361 | - if (!($properties[$sccs] instanceof SupportedCalendarComponentSet)) { |
|
362 | - throw new DAV\Exception('The ' . $sccs . ' property must be of type: \Sabre\CalDAV\Property\SupportedCalendarComponentSet'); |
|
363 | - } |
|
364 | - $values['components'] = implode(',',$properties[$sccs]->getValue()); |
|
365 | - } |
|
366 | - $transp = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
367 | - if (isset($properties[$transp])) { |
|
368 | - $values['transparent'] = $properties[$transp]->getValue()==='transparent'; |
|
369 | - } |
|
370 | - |
|
371 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
372 | - if (isset($properties[$xmlName])) { |
|
373 | - $values[$dbName] = $properties[$xmlName]; |
|
374 | - } |
|
375 | - } |
|
376 | - |
|
377 | - $query = $this->db->getQueryBuilder(); |
|
378 | - $query->insert('calendars'); |
|
379 | - foreach($values as $column => $value) { |
|
380 | - $query->setValue($column, $query->createNamedParameter($value)); |
|
381 | - } |
|
382 | - $query->execute(); |
|
383 | - return $query->getLastInsertId(); |
|
384 | - } |
|
385 | - |
|
386 | - /** |
|
387 | - * Updates properties for a calendar. |
|
388 | - * |
|
389 | - * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
390 | - * To do the actual updates, you must tell this object which properties |
|
391 | - * you're going to process with the handle() method. |
|
392 | - * |
|
393 | - * Calling the handle method is like telling the PropPatch object "I |
|
394 | - * promise I can handle updating this property". |
|
395 | - * |
|
396 | - * Read the PropPatch documentation for more info and examples. |
|
397 | - * |
|
398 | - * @param \Sabre\DAV\PropPatch $propPatch |
|
399 | - * @return void |
|
400 | - */ |
|
401 | - function updateCalendar($calendarId, \Sabre\DAV\PropPatch $propPatch) { |
|
402 | - $supportedProperties = array_keys($this->propertyMap); |
|
403 | - $supportedProperties[] = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
404 | - |
|
405 | - $propPatch->handle($supportedProperties, function($mutations) use ($calendarId) { |
|
406 | - $newValues = []; |
|
407 | - foreach ($mutations as $propertyName => $propertyValue) { |
|
408 | - |
|
409 | - switch ($propertyName) { |
|
410 | - case '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' : |
|
411 | - $fieldName = 'transparent'; |
|
412 | - $newValues[$fieldName] = $propertyValue->getValue() === 'transparent'; |
|
413 | - break; |
|
414 | - default : |
|
415 | - $fieldName = $this->propertyMap[$propertyName]; |
|
416 | - $newValues[$fieldName] = $propertyValue; |
|
417 | - break; |
|
418 | - } |
|
419 | - |
|
420 | - } |
|
421 | - $query = $this->db->getQueryBuilder(); |
|
422 | - $query->update('calendars'); |
|
423 | - foreach ($newValues as $fieldName => $value) { |
|
424 | - $query->set($fieldName, $query->createNamedParameter($value)); |
|
425 | - } |
|
426 | - $query->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))); |
|
427 | - $query->execute(); |
|
428 | - |
|
429 | - $this->addChange($calendarId, "", 2); |
|
430 | - |
|
431 | - return true; |
|
432 | - }); |
|
433 | - } |
|
434 | - |
|
435 | - /** |
|
436 | - * Delete a calendar and all it's objects |
|
437 | - * |
|
438 | - * @param mixed $calendarId |
|
439 | - * @return void |
|
440 | - */ |
|
441 | - function deleteCalendar($calendarId) { |
|
442 | - $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ?'); |
|
443 | - $stmt->execute([$calendarId]); |
|
444 | - |
|
445 | - $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendars` WHERE `id` = ?'); |
|
446 | - $stmt->execute([$calendarId]); |
|
447 | - |
|
448 | - $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarchanges` WHERE `calendarid` = ?'); |
|
449 | - $stmt->execute([$calendarId]); |
|
450 | - |
|
451 | - $this->sharingBackend->deleteAllShares($calendarId); |
|
452 | - } |
|
453 | - |
|
454 | - /** |
|
455 | - * Returns all calendar objects within a calendar. |
|
456 | - * |
|
457 | - * Every item contains an array with the following keys: |
|
458 | - * * calendardata - The iCalendar-compatible calendar data |
|
459 | - * * uri - a unique key which will be used to construct the uri. This can |
|
460 | - * be any arbitrary string, but making sure it ends with '.ics' is a |
|
461 | - * good idea. This is only the basename, or filename, not the full |
|
462 | - * path. |
|
463 | - * * lastmodified - a timestamp of the last modification time |
|
464 | - * * etag - An arbitrary string, surrounded by double-quotes. (e.g.: |
|
465 | - * '"abcdef"') |
|
466 | - * * size - The size of the calendar objects, in bytes. |
|
467 | - * * component - optional, a string containing the type of object, such |
|
468 | - * as 'vevent' or 'vtodo'. If specified, this will be used to populate |
|
469 | - * the Content-Type header. |
|
470 | - * |
|
471 | - * Note that the etag is optional, but it's highly encouraged to return for |
|
472 | - * speed reasons. |
|
473 | - * |
|
474 | - * The calendardata is also optional. If it's not returned |
|
475 | - * 'getCalendarObject' will be called later, which *is* expected to return |
|
476 | - * calendardata. |
|
477 | - * |
|
478 | - * If neither etag or size are specified, the calendardata will be |
|
479 | - * used/fetched to determine these numbers. If both are specified the |
|
480 | - * amount of times this is needed is reduced by a great degree. |
|
481 | - * |
|
482 | - * @param mixed $calendarId |
|
483 | - * @return array |
|
484 | - */ |
|
485 | - function getCalendarObjects($calendarId) { |
|
486 | - $query = $this->db->getQueryBuilder(); |
|
487 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'componenttype']) |
|
488 | - ->from('calendarobjects') |
|
489 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))); |
|
490 | - $stmt = $query->execute(); |
|
491 | - |
|
492 | - $result = []; |
|
493 | - foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) { |
|
494 | - $result[] = [ |
|
495 | - 'id' => $row['id'], |
|
496 | - 'uri' => $row['uri'], |
|
497 | - 'lastmodified' => $row['lastmodified'], |
|
498 | - 'etag' => '"' . $row['etag'] . '"', |
|
499 | - 'calendarid' => $row['calendarid'], |
|
500 | - 'size' => (int)$row['size'], |
|
501 | - 'component' => strtolower($row['componenttype']), |
|
502 | - ]; |
|
503 | - } |
|
504 | - |
|
505 | - return $result; |
|
506 | - } |
|
507 | - |
|
508 | - /** |
|
509 | - * Returns information from a single calendar object, based on it's object |
|
510 | - * uri. |
|
511 | - * |
|
512 | - * The object uri is only the basename, or filename and not a full path. |
|
513 | - * |
|
514 | - * The returned array must have the same keys as getCalendarObjects. The |
|
515 | - * 'calendardata' object is required here though, while it's not required |
|
516 | - * for getCalendarObjects. |
|
517 | - * |
|
518 | - * This method must return null if the object did not exist. |
|
519 | - * |
|
520 | - * @param mixed $calendarId |
|
521 | - * @param string $objectUri |
|
522 | - * @return array|null |
|
523 | - */ |
|
524 | - function getCalendarObject($calendarId, $objectUri) { |
|
525 | - |
|
526 | - $query = $this->db->getQueryBuilder(); |
|
527 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype']) |
|
528 | - ->from('calendarobjects') |
|
529 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
530 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))); |
|
531 | - $stmt = $query->execute(); |
|
532 | - $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
533 | - |
|
534 | - if(!$row) return null; |
|
535 | - |
|
536 | - return [ |
|
537 | - 'id' => $row['id'], |
|
538 | - 'uri' => $row['uri'], |
|
539 | - 'lastmodified' => $row['lastmodified'], |
|
540 | - 'etag' => '"' . $row['etag'] . '"', |
|
541 | - 'calendarid' => $row['calendarid'], |
|
542 | - 'size' => (int)$row['size'], |
|
543 | - 'calendardata' => $this->readBlob($row['calendardata']), |
|
544 | - 'component' => strtolower($row['componenttype']), |
|
545 | - ]; |
|
546 | - } |
|
547 | - |
|
548 | - /** |
|
549 | - * Returns a list of calendar objects. |
|
550 | - * |
|
551 | - * This method should work identical to getCalendarObject, but instead |
|
552 | - * return all the calendar objects in the list as an array. |
|
553 | - * |
|
554 | - * If the backend supports this, it may allow for some speed-ups. |
|
555 | - * |
|
556 | - * @param mixed $calendarId |
|
557 | - * @param string[] $uris |
|
558 | - * @return array |
|
559 | - */ |
|
560 | - function getMultipleCalendarObjects($calendarId, array $uris) { |
|
561 | - $query = $this->db->getQueryBuilder(); |
|
562 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype']) |
|
563 | - ->from('calendarobjects') |
|
564 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
565 | - ->andWhere($query->expr()->in('uri', $query->createParameter('uri'))) |
|
566 | - ->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY); |
|
567 | - |
|
568 | - $stmt = $query->execute(); |
|
569 | - |
|
570 | - $result = []; |
|
571 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
572 | - |
|
573 | - $result[] = [ |
|
574 | - 'id' => $row['id'], |
|
575 | - 'uri' => $row['uri'], |
|
576 | - 'lastmodified' => $row['lastmodified'], |
|
577 | - 'etag' => '"' . $row['etag'] . '"', |
|
578 | - 'calendarid' => $row['calendarid'], |
|
579 | - 'size' => (int)$row['size'], |
|
580 | - 'calendardata' => $this->readBlob($row['calendardata']), |
|
581 | - 'component' => strtolower($row['componenttype']), |
|
582 | - ]; |
|
583 | - |
|
584 | - } |
|
585 | - return $result; |
|
586 | - } |
|
587 | - |
|
588 | - /** |
|
589 | - * Creates a new calendar object. |
|
590 | - * |
|
591 | - * The object uri is only the basename, or filename and not a full path. |
|
592 | - * |
|
593 | - * It is possible return an etag from this function, which will be used in |
|
594 | - * the response to this PUT request. Note that the ETag must be surrounded |
|
595 | - * by double-quotes. |
|
596 | - * |
|
597 | - * However, you should only really return this ETag if you don't mangle the |
|
598 | - * calendar-data. If the result of a subsequent GET to this object is not |
|
599 | - * the exact same as this request body, you should omit the ETag. |
|
600 | - * |
|
601 | - * @param mixed $calendarId |
|
602 | - * @param string $objectUri |
|
603 | - * @param string $calendarData |
|
604 | - * @return string |
|
605 | - */ |
|
606 | - function createCalendarObject($calendarId, $objectUri, $calendarData) { |
|
607 | - $extraData = $this->getDenormalizedData($calendarData); |
|
608 | - |
|
609 | - $query = $this->db->getQueryBuilder(); |
|
610 | - $query->insert('calendarobjects') |
|
611 | - ->values([ |
|
612 | - 'calendarid' => $query->createNamedParameter($calendarId), |
|
613 | - 'uri' => $query->createNamedParameter($objectUri), |
|
614 | - 'calendardata' => $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB), |
|
615 | - 'lastmodified' => $query->createNamedParameter(time()), |
|
616 | - 'etag' => $query->createNamedParameter($extraData['etag']), |
|
617 | - 'size' => $query->createNamedParameter($extraData['size']), |
|
618 | - 'componenttype' => $query->createNamedParameter($extraData['componentType']), |
|
619 | - 'firstoccurence' => $query->createNamedParameter($extraData['firstOccurence']), |
|
620 | - 'lastoccurence' => $query->createNamedParameter($extraData['lastOccurence']), |
|
621 | - 'uid' => $query->createNamedParameter($extraData['uid']), |
|
622 | - ]) |
|
623 | - ->execute(); |
|
624 | - |
|
625 | - $this->addChange($calendarId, $objectUri, 1); |
|
626 | - |
|
627 | - return '"' . $extraData['etag'] . '"'; |
|
628 | - } |
|
629 | - |
|
630 | - /** |
|
631 | - * Updates an existing calendarobject, based on it's uri. |
|
632 | - * |
|
633 | - * The object uri is only the basename, or filename and not a full path. |
|
634 | - * |
|
635 | - * It is possible return an etag from this function, which will be used in |
|
636 | - * the response to this PUT request. Note that the ETag must be surrounded |
|
637 | - * by double-quotes. |
|
638 | - * |
|
639 | - * However, you should only really return this ETag if you don't mangle the |
|
640 | - * calendar-data. If the result of a subsequent GET to this object is not |
|
641 | - * the exact same as this request body, you should omit the ETag. |
|
642 | - * |
|
643 | - * @param mixed $calendarId |
|
644 | - * @param string $objectUri |
|
645 | - * @param string $calendarData |
|
646 | - * @return string |
|
647 | - */ |
|
648 | - function updateCalendarObject($calendarId, $objectUri, $calendarData) { |
|
649 | - $extraData = $this->getDenormalizedData($calendarData); |
|
650 | - |
|
651 | - $query = $this->db->getQueryBuilder(); |
|
652 | - $query->update('calendarobjects') |
|
653 | - ->set('calendardata', $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB)) |
|
654 | - ->set('lastmodified', $query->createNamedParameter(time())) |
|
655 | - ->set('etag', $query->createNamedParameter($extraData['etag'])) |
|
656 | - ->set('size', $query->createNamedParameter($extraData['size'])) |
|
657 | - ->set('componenttype', $query->createNamedParameter($extraData['componentType'])) |
|
658 | - ->set('firstoccurence', $query->createNamedParameter($extraData['firstOccurence'])) |
|
659 | - ->set('lastoccurence', $query->createNamedParameter($extraData['lastOccurence'])) |
|
660 | - ->set('uid', $query->createNamedParameter($extraData['uid'])) |
|
661 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
662 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
663 | - ->execute(); |
|
664 | - |
|
665 | - $this->addChange($calendarId, $objectUri, 2); |
|
666 | - |
|
667 | - return '"' . $extraData['etag'] . '"'; |
|
668 | - } |
|
669 | - |
|
670 | - /** |
|
671 | - * Deletes an existing calendar object. |
|
672 | - * |
|
673 | - * The object uri is only the basename, or filename and not a full path. |
|
674 | - * |
|
675 | - * @param mixed $calendarId |
|
676 | - * @param string $objectUri |
|
677 | - * @return void |
|
678 | - */ |
|
679 | - function deleteCalendarObject($calendarId, $objectUri) { |
|
680 | - $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ? AND `uri` = ?'); |
|
681 | - $stmt->execute([$calendarId, $objectUri]); |
|
682 | - |
|
683 | - $this->addChange($calendarId, $objectUri, 3); |
|
684 | - } |
|
685 | - |
|
686 | - /** |
|
687 | - * Performs a calendar-query on the contents of this calendar. |
|
688 | - * |
|
689 | - * The calendar-query is defined in RFC4791 : CalDAV. Using the |
|
690 | - * calendar-query it is possible for a client to request a specific set of |
|
691 | - * object, based on contents of iCalendar properties, date-ranges and |
|
692 | - * iCalendar component types (VTODO, VEVENT). |
|
693 | - * |
|
694 | - * This method should just return a list of (relative) urls that match this |
|
695 | - * query. |
|
696 | - * |
|
697 | - * The list of filters are specified as an array. The exact array is |
|
698 | - * documented by Sabre\CalDAV\CalendarQueryParser. |
|
699 | - * |
|
700 | - * Note that it is extremely likely that getCalendarObject for every path |
|
701 | - * returned from this method will be called almost immediately after. You |
|
702 | - * may want to anticipate this to speed up these requests. |
|
703 | - * |
|
704 | - * This method provides a default implementation, which parses *all* the |
|
705 | - * iCalendar objects in the specified calendar. |
|
706 | - * |
|
707 | - * This default may well be good enough for personal use, and calendars |
|
708 | - * that aren't very large. But if you anticipate high usage, big calendars |
|
709 | - * or high loads, you are strongly adviced to optimize certain paths. |
|
710 | - * |
|
711 | - * The best way to do so is override this method and to optimize |
|
712 | - * specifically for 'common filters'. |
|
713 | - * |
|
714 | - * Requests that are extremely common are: |
|
715 | - * * requests for just VEVENTS |
|
716 | - * * requests for just VTODO |
|
717 | - * * requests with a time-range-filter on either VEVENT or VTODO. |
|
718 | - * |
|
719 | - * ..and combinations of these requests. It may not be worth it to try to |
|
720 | - * handle every possible situation and just rely on the (relatively |
|
721 | - * easy to use) CalendarQueryValidator to handle the rest. |
|
722 | - * |
|
723 | - * Note that especially time-range-filters may be difficult to parse. A |
|
724 | - * time-range filter specified on a VEVENT must for instance also handle |
|
725 | - * recurrence rules correctly. |
|
726 | - * A good example of how to interprete all these filters can also simply |
|
727 | - * be found in Sabre\CalDAV\CalendarQueryFilter. This class is as correct |
|
728 | - * as possible, so it gives you a good idea on what type of stuff you need |
|
729 | - * to think of. |
|
730 | - * |
|
731 | - * @param mixed $calendarId |
|
732 | - * @param array $filters |
|
733 | - * @return array |
|
734 | - */ |
|
735 | - function calendarQuery($calendarId, array $filters) { |
|
736 | - $componentType = null; |
|
737 | - $requirePostFilter = true; |
|
738 | - $timeRange = null; |
|
739 | - |
|
740 | - // if no filters were specified, we don't need to filter after a query |
|
741 | - if (!$filters['prop-filters'] && !$filters['comp-filters']) { |
|
742 | - $requirePostFilter = false; |
|
743 | - } |
|
744 | - |
|
745 | - // Figuring out if there's a component filter |
|
746 | - if (count($filters['comp-filters']) > 0 && !$filters['comp-filters'][0]['is-not-defined']) { |
|
747 | - $componentType = $filters['comp-filters'][0]['name']; |
|
748 | - |
|
749 | - // Checking if we need post-filters |
|
750 | - if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['time-range'] && !$filters['comp-filters'][0]['prop-filters']) { |
|
751 | - $requirePostFilter = false; |
|
752 | - } |
|
753 | - // There was a time-range filter |
|
754 | - if ($componentType == 'VEVENT' && isset($filters['comp-filters'][0]['time-range'])) { |
|
755 | - $timeRange = $filters['comp-filters'][0]['time-range']; |
|
756 | - |
|
757 | - // If start time OR the end time is not specified, we can do a |
|
758 | - // 100% accurate mysql query. |
|
759 | - if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['prop-filters'] && (!$timeRange['start'] || !$timeRange['end'])) { |
|
760 | - $requirePostFilter = false; |
|
761 | - } |
|
762 | - } |
|
763 | - |
|
764 | - } |
|
765 | - $columns = ['uri']; |
|
766 | - if ($requirePostFilter) { |
|
767 | - $columns = ['uri', 'calendardata']; |
|
768 | - } |
|
769 | - $query = $this->db->getQueryBuilder(); |
|
770 | - $query->select($columns) |
|
771 | - ->from('calendarobjects') |
|
772 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))); |
|
773 | - |
|
774 | - if ($componentType) { |
|
775 | - $query->andWhere($query->expr()->eq('componenttype', $query->createNamedParameter($componentType))); |
|
776 | - } |
|
777 | - |
|
778 | - if ($timeRange && $timeRange['start']) { |
|
779 | - $query->andWhere($query->expr()->gt('lastoccurence', $query->createNamedParameter($timeRange['start']->getTimeStamp()))); |
|
780 | - } |
|
781 | - if ($timeRange && $timeRange['end']) { |
|
782 | - $query->andWhere($query->expr()->lt('firstoccurence', $query->createNamedParameter($timeRange['end']->getTimeStamp()))); |
|
783 | - } |
|
784 | - |
|
785 | - $stmt = $query->execute(); |
|
786 | - |
|
787 | - $result = []; |
|
788 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
789 | - if ($requirePostFilter) { |
|
790 | - if (!$this->validateFilterForObject($row, $filters)) { |
|
791 | - continue; |
|
792 | - } |
|
793 | - } |
|
794 | - $result[] = $row['uri']; |
|
795 | - } |
|
796 | - |
|
797 | - return $result; |
|
798 | - } |
|
799 | - |
|
800 | - /** |
|
801 | - * Searches through all of a users calendars and calendar objects to find |
|
802 | - * an object with a specific UID. |
|
803 | - * |
|
804 | - * This method should return the path to this object, relative to the |
|
805 | - * calendar home, so this path usually only contains two parts: |
|
806 | - * |
|
807 | - * calendarpath/objectpath.ics |
|
808 | - * |
|
809 | - * If the uid is not found, return null. |
|
810 | - * |
|
811 | - * This method should only consider * objects that the principal owns, so |
|
812 | - * any calendars owned by other principals that also appear in this |
|
813 | - * collection should be ignored. |
|
814 | - * |
|
815 | - * @param string $principalUri |
|
816 | - * @param string $uid |
|
817 | - * @return string|null |
|
818 | - */ |
|
819 | - function getCalendarObjectByUID($principalUri, $uid) { |
|
820 | - |
|
821 | - $query = $this->db->getQueryBuilder(); |
|
822 | - $query->selectAlias('c.uri', 'calendaruri')->selectAlias('co.uri', 'objecturi') |
|
823 | - ->from('calendarobjects', 'co') |
|
824 | - ->leftJoin('co', 'calendars', 'c', $query->expr()->eq('co.calendarid', 'c.id')) |
|
825 | - ->where($query->expr()->eq('c.principaluri', $query->createNamedParameter($principalUri))) |
|
826 | - ->andWhere($query->expr()->eq('co.uid', $query->createNamedParameter($uid))); |
|
827 | - |
|
828 | - $stmt = $query->execute(); |
|
829 | - |
|
830 | - if ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
831 | - return $row['calendaruri'] . '/' . $row['objecturi']; |
|
832 | - } |
|
833 | - |
|
834 | - return null; |
|
835 | - } |
|
836 | - |
|
837 | - /** |
|
838 | - * The getChanges method returns all the changes that have happened, since |
|
839 | - * the specified syncToken in the specified calendar. |
|
840 | - * |
|
841 | - * This function should return an array, such as the following: |
|
842 | - * |
|
843 | - * [ |
|
844 | - * 'syncToken' => 'The current synctoken', |
|
845 | - * 'added' => [ |
|
846 | - * 'new.txt', |
|
847 | - * ], |
|
848 | - * 'modified' => [ |
|
849 | - * 'modified.txt', |
|
850 | - * ], |
|
851 | - * 'deleted' => [ |
|
852 | - * 'foo.php.bak', |
|
853 | - * 'old.txt' |
|
854 | - * ] |
|
855 | - * ); |
|
856 | - * |
|
857 | - * The returned syncToken property should reflect the *current* syncToken |
|
858 | - * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
|
859 | - * property This is * needed here too, to ensure the operation is atomic. |
|
860 | - * |
|
861 | - * If the $syncToken argument is specified as null, this is an initial |
|
862 | - * sync, and all members should be reported. |
|
863 | - * |
|
864 | - * The modified property is an array of nodenames that have changed since |
|
865 | - * the last token. |
|
866 | - * |
|
867 | - * The deleted property is an array with nodenames, that have been deleted |
|
868 | - * from collection. |
|
869 | - * |
|
870 | - * The $syncLevel argument is basically the 'depth' of the report. If it's |
|
871 | - * 1, you only have to report changes that happened only directly in |
|
872 | - * immediate descendants. If it's 2, it should also include changes from |
|
873 | - * the nodes below the child collections. (grandchildren) |
|
874 | - * |
|
875 | - * The $limit argument allows a client to specify how many results should |
|
876 | - * be returned at most. If the limit is not specified, it should be treated |
|
877 | - * as infinite. |
|
878 | - * |
|
879 | - * If the limit (infinite or not) is higher than you're willing to return, |
|
880 | - * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
|
881 | - * |
|
882 | - * If the syncToken is expired (due to data cleanup) or unknown, you must |
|
883 | - * return null. |
|
884 | - * |
|
885 | - * The limit is 'suggestive'. You are free to ignore it. |
|
886 | - * |
|
887 | - * @param string $calendarId |
|
888 | - * @param string $syncToken |
|
889 | - * @param int $syncLevel |
|
890 | - * @param int $limit |
|
891 | - * @return array |
|
892 | - */ |
|
893 | - function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null) { |
|
894 | - // Current synctoken |
|
895 | - $stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*calendars` WHERE `id` = ?'); |
|
896 | - $stmt->execute([ $calendarId ]); |
|
897 | - $currentToken = $stmt->fetchColumn(0); |
|
898 | - |
|
899 | - if (is_null($currentToken)) { |
|
900 | - return null; |
|
901 | - } |
|
902 | - |
|
903 | - $result = [ |
|
904 | - 'syncToken' => $currentToken, |
|
905 | - 'added' => [], |
|
906 | - 'modified' => [], |
|
907 | - 'deleted' => [], |
|
908 | - ]; |
|
909 | - |
|
910 | - if ($syncToken) { |
|
911 | - |
|
912 | - $query = "SELECT `uri`, `operation` FROM `*PREFIX*calendarchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `calendarid` = ? ORDER BY `synctoken`"; |
|
913 | - if ($limit>0) { |
|
914 | - $query.= " `LIMIT` " . (int)$limit; |
|
915 | - } |
|
916 | - |
|
917 | - // Fetching all changes |
|
918 | - $stmt = $this->db->prepare($query); |
|
919 | - $stmt->execute([$syncToken, $currentToken, $calendarId]); |
|
920 | - |
|
921 | - $changes = []; |
|
922 | - |
|
923 | - // This loop ensures that any duplicates are overwritten, only the |
|
924 | - // last change on a node is relevant. |
|
925 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
926 | - |
|
927 | - $changes[$row['uri']] = $row['operation']; |
|
928 | - |
|
929 | - } |
|
930 | - |
|
931 | - foreach($changes as $uri => $operation) { |
|
932 | - |
|
933 | - switch($operation) { |
|
934 | - case 1 : |
|
935 | - $result['added'][] = $uri; |
|
936 | - break; |
|
937 | - case 2 : |
|
938 | - $result['modified'][] = $uri; |
|
939 | - break; |
|
940 | - case 3 : |
|
941 | - $result['deleted'][] = $uri; |
|
942 | - break; |
|
943 | - } |
|
944 | - |
|
945 | - } |
|
946 | - } else { |
|
947 | - // No synctoken supplied, this is the initial sync. |
|
948 | - $query = "SELECT `uri` FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ?"; |
|
949 | - $stmt = $this->db->prepare($query); |
|
950 | - $stmt->execute([$calendarId]); |
|
951 | - |
|
952 | - $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
953 | - } |
|
954 | - return $result; |
|
955 | - |
|
956 | - } |
|
957 | - |
|
958 | - /** |
|
959 | - * Returns a list of subscriptions for a principal. |
|
960 | - * |
|
961 | - * Every subscription is an array with the following keys: |
|
962 | - * * id, a unique id that will be used by other functions to modify the |
|
963 | - * subscription. This can be the same as the uri or a database key. |
|
964 | - * * uri. This is just the 'base uri' or 'filename' of the subscription. |
|
965 | - * * principaluri. The owner of the subscription. Almost always the same as |
|
966 | - * principalUri passed to this method. |
|
967 | - * |
|
968 | - * Furthermore, all the subscription info must be returned too: |
|
969 | - * |
|
970 | - * 1. {DAV:}displayname |
|
971 | - * 2. {http://apple.com/ns/ical/}refreshrate |
|
972 | - * 3. {http://calendarserver.org/ns/}subscribed-strip-todos (omit if todos |
|
973 | - * should not be stripped). |
|
974 | - * 4. {http://calendarserver.org/ns/}subscribed-strip-alarms (omit if alarms |
|
975 | - * should not be stripped). |
|
976 | - * 5. {http://calendarserver.org/ns/}subscribed-strip-attachments (omit if |
|
977 | - * attachments should not be stripped). |
|
978 | - * 6. {http://calendarserver.org/ns/}source (Must be a |
|
979 | - * Sabre\DAV\Property\Href). |
|
980 | - * 7. {http://apple.com/ns/ical/}calendar-color |
|
981 | - * 8. {http://apple.com/ns/ical/}calendar-order |
|
982 | - * 9. {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
|
983 | - * (should just be an instance of |
|
984 | - * Sabre\CalDAV\Property\SupportedCalendarComponentSet, with a bunch of |
|
985 | - * default components). |
|
986 | - * |
|
987 | - * @param string $principalUri |
|
988 | - * @return array |
|
989 | - */ |
|
990 | - function getSubscriptionsForUser($principalUri) { |
|
991 | - $fields = array_values($this->subscriptionPropertyMap); |
|
992 | - $fields[] = 'id'; |
|
993 | - $fields[] = 'uri'; |
|
994 | - $fields[] = 'source'; |
|
995 | - $fields[] = 'principaluri'; |
|
996 | - $fields[] = 'lastmodified'; |
|
997 | - |
|
998 | - $query = $this->db->getQueryBuilder(); |
|
999 | - $query->select($fields) |
|
1000 | - ->from('calendarsubscriptions') |
|
1001 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
1002 | - ->orderBy('calendarorder', 'asc'); |
|
1003 | - $stmt =$query->execute(); |
|
1004 | - |
|
1005 | - $subscriptions = []; |
|
1006 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
1007 | - |
|
1008 | - $subscription = [ |
|
1009 | - 'id' => $row['id'], |
|
1010 | - 'uri' => $row['uri'], |
|
1011 | - 'principaluri' => $row['principaluri'], |
|
1012 | - 'source' => $row['source'], |
|
1013 | - 'lastmodified' => $row['lastmodified'], |
|
1014 | - |
|
1015 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), |
|
1016 | - ]; |
|
1017 | - |
|
1018 | - foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) { |
|
1019 | - if (!is_null($row[$dbName])) { |
|
1020 | - $subscription[$xmlName] = $row[$dbName]; |
|
1021 | - } |
|
1022 | - } |
|
1023 | - |
|
1024 | - $subscriptions[] = $subscription; |
|
1025 | - |
|
1026 | - } |
|
1027 | - |
|
1028 | - return $subscriptions; |
|
1029 | - } |
|
1030 | - |
|
1031 | - /** |
|
1032 | - * Creates a new subscription for a principal. |
|
1033 | - * |
|
1034 | - * If the creation was a success, an id must be returned that can be used to reference |
|
1035 | - * this subscription in other methods, such as updateSubscription. |
|
1036 | - * |
|
1037 | - * @param string $principalUri |
|
1038 | - * @param string $uri |
|
1039 | - * @param array $properties |
|
1040 | - * @return mixed |
|
1041 | - */ |
|
1042 | - function createSubscription($principalUri, $uri, array $properties) { |
|
1043 | - |
|
1044 | - if (!isset($properties['{http://calendarserver.org/ns/}source'])) { |
|
1045 | - throw new Forbidden('The {http://calendarserver.org/ns/}source property is required when creating subscriptions'); |
|
1046 | - } |
|
1047 | - |
|
1048 | - $values = [ |
|
1049 | - 'principaluri' => $principalUri, |
|
1050 | - 'uri' => $uri, |
|
1051 | - 'source' => $properties['{http://calendarserver.org/ns/}source']->getHref(), |
|
1052 | - 'lastmodified' => time(), |
|
1053 | - ]; |
|
1054 | - |
|
1055 | - foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) { |
|
1056 | - if (isset($properties[$xmlName])) { |
|
1057 | - |
|
1058 | - $values[$dbName] = $properties[$xmlName]; |
|
1059 | - $fieldNames[] = $dbName; |
|
1060 | - } |
|
1061 | - } |
|
1062 | - |
|
1063 | - $query = $this->db->getQueryBuilder(); |
|
1064 | - $query->insert('calendarsubscriptions') |
|
1065 | - ->values([ |
|
1066 | - 'principaluri' => $query->createNamedParameter($values['principaluri']), |
|
1067 | - 'uri' => $query->createNamedParameter($values['uri']), |
|
1068 | - 'source' => $query->createNamedParameter($values['source']), |
|
1069 | - 'lastmodified' => $query->createNamedParameter($values['lastmodified']), |
|
1070 | - ]) |
|
1071 | - ->execute(); |
|
1072 | - |
|
1073 | - return $this->db->lastInsertId('*PREFIX*calendarsubscriptions'); |
|
1074 | - } |
|
1075 | - |
|
1076 | - /** |
|
1077 | - * Updates a subscription |
|
1078 | - * |
|
1079 | - * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
1080 | - * To do the actual updates, you must tell this object which properties |
|
1081 | - * you're going to process with the handle() method. |
|
1082 | - * |
|
1083 | - * Calling the handle method is like telling the PropPatch object "I |
|
1084 | - * promise I can handle updating this property". |
|
1085 | - * |
|
1086 | - * Read the PropPatch documentation for more info and examples. |
|
1087 | - * |
|
1088 | - * @param mixed $subscriptionId |
|
1089 | - * @param \Sabre\DAV\PropPatch $propPatch |
|
1090 | - * @return void |
|
1091 | - */ |
|
1092 | - function updateSubscription($subscriptionId, DAV\PropPatch $propPatch) { |
|
1093 | - $supportedProperties = array_keys($this->subscriptionPropertyMap); |
|
1094 | - $supportedProperties[] = '{http://calendarserver.org/ns/}source'; |
|
1095 | - |
|
1096 | - $propPatch->handle($supportedProperties, function($mutations) use ($subscriptionId) { |
|
1097 | - |
|
1098 | - $newValues = []; |
|
1099 | - |
|
1100 | - foreach($mutations as $propertyName=>$propertyValue) { |
|
1101 | - if ($propertyName === '{http://calendarserver.org/ns/}source') { |
|
1102 | - $newValues['source'] = $propertyValue->getHref(); |
|
1103 | - } else { |
|
1104 | - $fieldName = $this->subscriptionPropertyMap[$propertyName]; |
|
1105 | - $newValues[$fieldName] = $propertyValue; |
|
1106 | - } |
|
1107 | - } |
|
1108 | - |
|
1109 | - $query = $this->db->getQueryBuilder(); |
|
1110 | - $query->update('calendarsubscriptions') |
|
1111 | - ->set('lastmodified', $query->createNamedParameter(time())); |
|
1112 | - foreach($newValues as $fieldName=>$value) { |
|
1113 | - $query->set($fieldName, $query->createNamedParameter($value)); |
|
1114 | - } |
|
1115 | - $query->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
1116 | - ->execute(); |
|
1117 | - |
|
1118 | - return true; |
|
1119 | - |
|
1120 | - }); |
|
1121 | - } |
|
1122 | - |
|
1123 | - /** |
|
1124 | - * Deletes a subscription. |
|
1125 | - * |
|
1126 | - * @param mixed $subscriptionId |
|
1127 | - * @return void |
|
1128 | - */ |
|
1129 | - function deleteSubscription($subscriptionId) { |
|
1130 | - $query = $this->db->getQueryBuilder(); |
|
1131 | - $query->delete('calendarsubscriptions') |
|
1132 | - ->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
1133 | - ->execute(); |
|
1134 | - } |
|
1135 | - |
|
1136 | - /** |
|
1137 | - * Returns a single scheduling object for the inbox collection. |
|
1138 | - * |
|
1139 | - * The returned array should contain the following elements: |
|
1140 | - * * uri - A unique basename for the object. This will be used to |
|
1141 | - * construct a full uri. |
|
1142 | - * * calendardata - The iCalendar object |
|
1143 | - * * lastmodified - The last modification date. Can be an int for a unix |
|
1144 | - * timestamp, or a PHP DateTime object. |
|
1145 | - * * etag - A unique token that must change if the object changed. |
|
1146 | - * * size - The size of the object, in bytes. |
|
1147 | - * |
|
1148 | - * @param string $principalUri |
|
1149 | - * @param string $objectUri |
|
1150 | - * @return array |
|
1151 | - */ |
|
1152 | - function getSchedulingObject($principalUri, $objectUri) { |
|
1153 | - $query = $this->db->getQueryBuilder(); |
|
1154 | - $stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size']) |
|
1155 | - ->from('schedulingobjects') |
|
1156 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
1157 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
1158 | - ->execute(); |
|
1159 | - |
|
1160 | - $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
1161 | - |
|
1162 | - if(!$row) { |
|
1163 | - return null; |
|
1164 | - } |
|
1165 | - |
|
1166 | - return [ |
|
1167 | - 'uri' => $row['uri'], |
|
1168 | - 'calendardata' => $row['calendardata'], |
|
1169 | - 'lastmodified' => $row['lastmodified'], |
|
1170 | - 'etag' => '"' . $row['etag'] . '"', |
|
1171 | - 'size' => (int)$row['size'], |
|
1172 | - ]; |
|
1173 | - } |
|
1174 | - |
|
1175 | - /** |
|
1176 | - * Returns all scheduling objects for the inbox collection. |
|
1177 | - * |
|
1178 | - * These objects should be returned as an array. Every item in the array |
|
1179 | - * should follow the same structure as returned from getSchedulingObject. |
|
1180 | - * |
|
1181 | - * The main difference is that 'calendardata' is optional. |
|
1182 | - * |
|
1183 | - * @param string $principalUri |
|
1184 | - * @return array |
|
1185 | - */ |
|
1186 | - function getSchedulingObjects($principalUri) { |
|
1187 | - $query = $this->db->getQueryBuilder(); |
|
1188 | - $stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size']) |
|
1189 | - ->from('schedulingobjects') |
|
1190 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
1191 | - ->execute(); |
|
1192 | - |
|
1193 | - $result = []; |
|
1194 | - foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) { |
|
1195 | - $result[] = [ |
|
1196 | - 'calendardata' => $row['calendardata'], |
|
1197 | - 'uri' => $row['uri'], |
|
1198 | - 'lastmodified' => $row['lastmodified'], |
|
1199 | - 'etag' => '"' . $row['etag'] . '"', |
|
1200 | - 'size' => (int)$row['size'], |
|
1201 | - ]; |
|
1202 | - } |
|
1203 | - |
|
1204 | - return $result; |
|
1205 | - } |
|
1206 | - |
|
1207 | - /** |
|
1208 | - * Deletes a scheduling object from the inbox collection. |
|
1209 | - * |
|
1210 | - * @param string $principalUri |
|
1211 | - * @param string $objectUri |
|
1212 | - * @return void |
|
1213 | - */ |
|
1214 | - function deleteSchedulingObject($principalUri, $objectUri) { |
|
1215 | - $query = $this->db->getQueryBuilder(); |
|
1216 | - $query->delete('schedulingobjects') |
|
1217 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
1218 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
1219 | - ->execute(); |
|
1220 | - } |
|
1221 | - |
|
1222 | - /** |
|
1223 | - * Creates a new scheduling object. This should land in a users' inbox. |
|
1224 | - * |
|
1225 | - * @param string $principalUri |
|
1226 | - * @param string $objectUri |
|
1227 | - * @param string $objectData |
|
1228 | - * @return void |
|
1229 | - */ |
|
1230 | - function createSchedulingObject($principalUri, $objectUri, $objectData) { |
|
1231 | - $query = $this->db->getQueryBuilder(); |
|
1232 | - $query->insert('schedulingobjects') |
|
1233 | - ->values([ |
|
1234 | - 'principaluri' => $query->createNamedParameter($principalUri), |
|
1235 | - 'calendardata' => $query->createNamedParameter($objectData), |
|
1236 | - 'uri' => $query->createNamedParameter($objectUri), |
|
1237 | - 'lastmodified' => $query->createNamedParameter(time()), |
|
1238 | - 'etag' => $query->createNamedParameter(md5($objectData)), |
|
1239 | - 'size' => $query->createNamedParameter(strlen($objectData)) |
|
1240 | - ]) |
|
1241 | - ->execute(); |
|
1242 | - } |
|
1243 | - |
|
1244 | - /** |
|
1245 | - * Adds a change record to the calendarchanges table. |
|
1246 | - * |
|
1247 | - * @param mixed $calendarId |
|
1248 | - * @param string $objectUri |
|
1249 | - * @param int $operation 1 = add, 2 = modify, 3 = delete. |
|
1250 | - * @return void |
|
1251 | - */ |
|
1252 | - protected function addChange($calendarId, $objectUri, $operation) { |
|
1253 | - |
|
1254 | - $stmt = $this->db->prepare('INSERT INTO `*PREFIX*calendarchanges` (`uri`, `synctoken`, `calendarid`, `operation`) SELECT ?, `synctoken`, ?, ? FROM `*PREFIX*calendars` WHERE `id` = ?'); |
|
1255 | - $stmt->execute([ |
|
1256 | - $objectUri, |
|
1257 | - $calendarId, |
|
1258 | - $operation, |
|
1259 | - $calendarId |
|
1260 | - ]); |
|
1261 | - $stmt = $this->db->prepare('UPDATE `*PREFIX*calendars` SET `synctoken` = `synctoken` + 1 WHERE `id` = ?'); |
|
1262 | - $stmt->execute([ |
|
1263 | - $calendarId |
|
1264 | - ]); |
|
1265 | - |
|
1266 | - } |
|
1267 | - |
|
1268 | - /** |
|
1269 | - * Parses some information from calendar objects, used for optimized |
|
1270 | - * calendar-queries. |
|
1271 | - * |
|
1272 | - * Returns an array with the following keys: |
|
1273 | - * * etag - An md5 checksum of the object without the quotes. |
|
1274 | - * * size - Size of the object in bytes |
|
1275 | - * * componentType - VEVENT, VTODO or VJOURNAL |
|
1276 | - * * firstOccurence |
|
1277 | - * * lastOccurence |
|
1278 | - * * uid - value of the UID property |
|
1279 | - * |
|
1280 | - * @param string $calendarData |
|
1281 | - * @return array |
|
1282 | - */ |
|
1283 | - protected function getDenormalizedData($calendarData) { |
|
1284 | - |
|
1285 | - $vObject = Reader::read($calendarData); |
|
1286 | - $componentType = null; |
|
1287 | - $component = null; |
|
1288 | - $firstOccurence = null; |
|
1289 | - $lastOccurence = null; |
|
1290 | - $uid = null; |
|
1291 | - foreach($vObject->getComponents() as $component) { |
|
1292 | - if ($component->name!=='VTIMEZONE') { |
|
1293 | - $componentType = $component->name; |
|
1294 | - $uid = (string)$component->UID; |
|
1295 | - break; |
|
1296 | - } |
|
1297 | - } |
|
1298 | - if (!$componentType) { |
|
1299 | - throw new \Sabre\DAV\Exception\BadRequest('Calendar objects must have a VJOURNAL, VEVENT or VTODO component'); |
|
1300 | - } |
|
1301 | - if ($componentType === 'VEVENT' && $component->DTSTART) { |
|
1302 | - $firstOccurence = $component->DTSTART->getDateTime()->getTimeStamp(); |
|
1303 | - // Finding the last occurence is a bit harder |
|
1304 | - if (!isset($component->RRULE)) { |
|
1305 | - if (isset($component->DTEND)) { |
|
1306 | - $lastOccurence = $component->DTEND->getDateTime()->getTimeStamp(); |
|
1307 | - } elseif (isset($component->DURATION)) { |
|
1308 | - $endDate = clone $component->DTSTART->getDateTime(); |
|
1309 | - $endDate->add(DateTimeParser::parse($component->DURATION->getValue())); |
|
1310 | - $lastOccurence = $endDate->getTimeStamp(); |
|
1311 | - } elseif (!$component->DTSTART->hasTime()) { |
|
1312 | - $endDate = clone $component->DTSTART->getDateTime(); |
|
1313 | - $endDate->modify('+1 day'); |
|
1314 | - $lastOccurence = $endDate->getTimeStamp(); |
|
1315 | - } else { |
|
1316 | - $lastOccurence = $firstOccurence; |
|
1317 | - } |
|
1318 | - } else { |
|
1319 | - $it = new RecurrenceIterator($vObject, (string)$component->UID); |
|
1320 | - $maxDate = new \DateTime(self::MAX_DATE); |
|
1321 | - if ($it->isInfinite()) { |
|
1322 | - $lastOccurence = $maxDate->getTimeStamp(); |
|
1323 | - } else { |
|
1324 | - $end = $it->getDtEnd(); |
|
1325 | - while($it->valid() && $end < $maxDate) { |
|
1326 | - $end = $it->getDtEnd(); |
|
1327 | - $it->next(); |
|
1328 | - |
|
1329 | - } |
|
1330 | - $lastOccurence = $end->getTimeStamp(); |
|
1331 | - } |
|
1332 | - |
|
1333 | - } |
|
1334 | - } |
|
1335 | - |
|
1336 | - return [ |
|
1337 | - 'etag' => md5($calendarData), |
|
1338 | - 'size' => strlen($calendarData), |
|
1339 | - 'componentType' => $componentType, |
|
1340 | - 'firstOccurence' => is_null($firstOccurence) ? null : max(0, $firstOccurence), |
|
1341 | - 'lastOccurence' => $lastOccurence, |
|
1342 | - 'uid' => $uid, |
|
1343 | - ]; |
|
1344 | - |
|
1345 | - } |
|
1346 | - |
|
1347 | - private function readBlob($cardData) { |
|
1348 | - if (is_resource($cardData)) { |
|
1349 | - return stream_get_contents($cardData); |
|
1350 | - } |
|
1351 | - |
|
1352 | - return $cardData; |
|
1353 | - } |
|
1354 | - |
|
1355 | - /** |
|
1356 | - * @param IShareable $shareable |
|
1357 | - * @param array $add |
|
1358 | - * @param array $remove |
|
1359 | - */ |
|
1360 | - public function updateShares($shareable, $add, $remove) { |
|
1361 | - $this->sharingBackend->updateShares($shareable, $add, $remove); |
|
1362 | - } |
|
1363 | - |
|
1364 | - /** |
|
1365 | - * @param int $resourceId |
|
1366 | - * @return array |
|
1367 | - */ |
|
1368 | - public function getShares($resourceId) { |
|
1369 | - return $this->sharingBackend->getShares($resourceId); |
|
1370 | - } |
|
1371 | - |
|
1372 | - /** |
|
1373 | - * @param int $resourceId |
|
1374 | - * @param array $acl |
|
1375 | - * @return array |
|
1376 | - */ |
|
1377 | - public function applyShareAcl($resourceId, $acl) { |
|
1378 | - return $this->sharingBackend->applyShareAcl($resourceId, $acl); |
|
1379 | - } |
|
1380 | - |
|
1381 | - private function convertPrincipal($principalUri, $toV2) { |
|
1382 | - if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
|
1383 | - list(, $name) = URLUtil::splitPath($principalUri); |
|
1384 | - if ($toV2 === true) { |
|
1385 | - return "principals/users/$name"; |
|
1386 | - } |
|
1387 | - return "principals/$name"; |
|
1388 | - } |
|
1389 | - return $principalUri; |
|
1390 | - } |
|
54 | + /** |
|
55 | + * We need to specify a max date, because we need to stop *somewhere* |
|
56 | + * |
|
57 | + * On 32 bit system the maximum for a signed integer is 2147483647, so |
|
58 | + * MAX_DATE cannot be higher than date('Y-m-d', 2147483647) which results |
|
59 | + * in 2038-01-19 to avoid problems when the date is converted |
|
60 | + * to a unix timestamp. |
|
61 | + */ |
|
62 | + const MAX_DATE = '2038-01-01'; |
|
63 | + |
|
64 | + /** |
|
65 | + * List of CalDAV properties, and how they map to database field names |
|
66 | + * Add your own properties by simply adding on to this array. |
|
67 | + * |
|
68 | + * Note that only string-based properties are supported here. |
|
69 | + * |
|
70 | + * @var array |
|
71 | + */ |
|
72 | + public $propertyMap = [ |
|
73 | + '{DAV:}displayname' => 'displayname', |
|
74 | + '{urn:ietf:params:xml:ns:caldav}calendar-description' => 'description', |
|
75 | + '{urn:ietf:params:xml:ns:caldav}calendar-timezone' => 'timezone', |
|
76 | + '{http://apple.com/ns/ical/}calendar-order' => 'calendarorder', |
|
77 | + '{http://apple.com/ns/ical/}calendar-color' => 'calendarcolor', |
|
78 | + ]; |
|
79 | + |
|
80 | + /** |
|
81 | + * List of subscription properties, and how they map to database field names. |
|
82 | + * |
|
83 | + * @var array |
|
84 | + */ |
|
85 | + public $subscriptionPropertyMap = [ |
|
86 | + '{DAV:}displayname' => 'displayname', |
|
87 | + '{http://apple.com/ns/ical/}refreshrate' => 'refreshrate', |
|
88 | + '{http://apple.com/ns/ical/}calendar-order' => 'calendarorder', |
|
89 | + '{http://apple.com/ns/ical/}calendar-color' => 'calendarcolor', |
|
90 | + '{http://calendarserver.org/ns/}subscribed-strip-todos' => 'striptodos', |
|
91 | + '{http://calendarserver.org/ns/}subscribed-strip-alarms' => 'stripalarms', |
|
92 | + '{http://calendarserver.org/ns/}subscribed-strip-attachments' => 'stripattachments', |
|
93 | + ]; |
|
94 | + |
|
95 | + /** @var IDBConnection */ |
|
96 | + private $db; |
|
97 | + |
|
98 | + /** @var Backend */ |
|
99 | + private $sharingBackend; |
|
100 | + |
|
101 | + /** @var Principal */ |
|
102 | + private $principalBackend; |
|
103 | + |
|
104 | + /** |
|
105 | + * CalDavBackend constructor. |
|
106 | + * |
|
107 | + * @param IDBConnection $db |
|
108 | + * @param Principal $principalBackend |
|
109 | + */ |
|
110 | + public function __construct(IDBConnection $db, Principal $principalBackend) { |
|
111 | + $this->db = $db; |
|
112 | + $this->principalBackend = $principalBackend; |
|
113 | + $this->sharingBackend = new Backend($this->db, $principalBackend, 'calendar'); |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * Returns a list of calendars for a principal. |
|
118 | + * |
|
119 | + * Every project is an array with the following keys: |
|
120 | + * * id, a unique id that will be used by other functions to modify the |
|
121 | + * calendar. This can be the same as the uri or a database key. |
|
122 | + * * uri, which the basename of the uri with which the calendar is |
|
123 | + * accessed. |
|
124 | + * * principaluri. The owner of the calendar. Almost always the same as |
|
125 | + * principalUri passed to this method. |
|
126 | + * |
|
127 | + * Furthermore it can contain webdav properties in clark notation. A very |
|
128 | + * common one is '{DAV:}displayname'. |
|
129 | + * |
|
130 | + * Many clients also require: |
|
131 | + * {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
|
132 | + * For this property, you can just return an instance of |
|
133 | + * Sabre\CalDAV\Property\SupportedCalendarComponentSet. |
|
134 | + * |
|
135 | + * If you return {http://sabredav.org/ns}read-only and set the value to 1, |
|
136 | + * ACL will automatically be put in read-only mode. |
|
137 | + * |
|
138 | + * @param string $principalUri |
|
139 | + * @return array |
|
140 | + */ |
|
141 | + function getCalendarsForUser($principalUri) { |
|
142 | + $principalUriOriginal = $principalUri; |
|
143 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
144 | + $fields = array_values($this->propertyMap); |
|
145 | + $fields[] = 'id'; |
|
146 | + $fields[] = 'uri'; |
|
147 | + $fields[] = 'synctoken'; |
|
148 | + $fields[] = 'components'; |
|
149 | + $fields[] = 'principaluri'; |
|
150 | + $fields[] = 'transparent'; |
|
151 | + |
|
152 | + // Making fields a comma-delimited list |
|
153 | + $query = $this->db->getQueryBuilder(); |
|
154 | + $query->select($fields)->from('calendars') |
|
155 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
156 | + ->orderBy('calendarorder', 'ASC'); |
|
157 | + $stmt = $query->execute(); |
|
158 | + |
|
159 | + $calendars = []; |
|
160 | + while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
161 | + |
|
162 | + $components = []; |
|
163 | + if ($row['components']) { |
|
164 | + $components = explode(',',$row['components']); |
|
165 | + } |
|
166 | + |
|
167 | + $calendar = [ |
|
168 | + 'id' => $row['id'], |
|
169 | + 'uri' => $row['uri'], |
|
170 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
|
171 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
172 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
173 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
174 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
175 | + ]; |
|
176 | + |
|
177 | + foreach($this->propertyMap as $xmlName=>$dbName) { |
|
178 | + $calendar[$xmlName] = $row[$dbName]; |
|
179 | + } |
|
180 | + |
|
181 | + if (!isset($calendars[$calendar['id']])) { |
|
182 | + $calendars[$calendar['id']] = $calendar; |
|
183 | + } |
|
184 | + } |
|
185 | + |
|
186 | + $stmt->closeCursor(); |
|
187 | + |
|
188 | + // query for shared calendars |
|
189 | + $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); |
|
190 | + $principals[]= $principalUri; |
|
191 | + |
|
192 | + $fields = array_values($this->propertyMap); |
|
193 | + $fields[] = 'a.id'; |
|
194 | + $fields[] = 'a.uri'; |
|
195 | + $fields[] = 'a.synctoken'; |
|
196 | + $fields[] = 'a.components'; |
|
197 | + $fields[] = 'a.principaluri'; |
|
198 | + $fields[] = 'a.transparent'; |
|
199 | + $fields[] = 's.access'; |
|
200 | + $query = $this->db->getQueryBuilder(); |
|
201 | + $result = $query->select($fields) |
|
202 | + ->from('dav_shares', 's') |
|
203 | + ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
204 | + ->where($query->expr()->in('s.principaluri', $query->createParameter('principaluri'))) |
|
205 | + ->andWhere($query->expr()->eq('s.type', $query->createParameter('type'))) |
|
206 | + ->setParameter('type', 'calendar') |
|
207 | + ->setParameter('principaluri', $principals, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY) |
|
208 | + ->execute(); |
|
209 | + |
|
210 | + while($row = $result->fetch()) { |
|
211 | + list(, $name) = URLUtil::splitPath($row['principaluri']); |
|
212 | + $uri = $row['uri'] . '_shared_by_' . $name; |
|
213 | + $row['displayname'] = $row['displayname'] . "($name)"; |
|
214 | + $components = []; |
|
215 | + if ($row['components']) { |
|
216 | + $components = explode(',',$row['components']); |
|
217 | + } |
|
218 | + $calendar = [ |
|
219 | + 'id' => $row['id'], |
|
220 | + 'uri' => $uri, |
|
221 | + 'principaluri' => $principalUri, |
|
222 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
223 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
224 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
225 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
226 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $row['principaluri'], |
|
227 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ, |
|
228 | + ]; |
|
229 | + |
|
230 | + foreach($this->propertyMap as $xmlName=>$dbName) { |
|
231 | + $calendar[$xmlName] = $row[$dbName]; |
|
232 | + } |
|
233 | + |
|
234 | + if (!isset($calendars[$calendar['id']])) { |
|
235 | + $calendars[$calendar['id']] = $calendar; |
|
236 | + } |
|
237 | + } |
|
238 | + $result->closeCursor(); |
|
239 | + |
|
240 | + return array_values($calendars); |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * @param string $principal |
|
245 | + * @param string $uri |
|
246 | + * @return array|null |
|
247 | + */ |
|
248 | + public function getCalendarByUri($principal, $uri) { |
|
249 | + $fields = array_values($this->propertyMap); |
|
250 | + $fields[] = 'id'; |
|
251 | + $fields[] = 'uri'; |
|
252 | + $fields[] = 'synctoken'; |
|
253 | + $fields[] = 'components'; |
|
254 | + $fields[] = 'principaluri'; |
|
255 | + $fields[] = 'transparent'; |
|
256 | + |
|
257 | + // Making fields a comma-delimited list |
|
258 | + $query = $this->db->getQueryBuilder(); |
|
259 | + $query->select($fields)->from('calendars') |
|
260 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
261 | + ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal))) |
|
262 | + ->setMaxResults(1); |
|
263 | + $stmt = $query->execute(); |
|
264 | + |
|
265 | + $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
266 | + $stmt->closeCursor(); |
|
267 | + if ($row === false) { |
|
268 | + return null; |
|
269 | + } |
|
270 | + |
|
271 | + $components = []; |
|
272 | + if ($row['components']) { |
|
273 | + $components = explode(',',$row['components']); |
|
274 | + } |
|
275 | + |
|
276 | + $calendar = [ |
|
277 | + 'id' => $row['id'], |
|
278 | + 'uri' => $row['uri'], |
|
279 | + 'principaluri' => $row['principaluri'], |
|
280 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
281 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
282 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
283 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
284 | + ]; |
|
285 | + |
|
286 | + foreach($this->propertyMap as $xmlName=>$dbName) { |
|
287 | + $calendar[$xmlName] = $row[$dbName]; |
|
288 | + } |
|
289 | + |
|
290 | + return $calendar; |
|
291 | + } |
|
292 | + |
|
293 | + public function getCalendarById($calendarId) { |
|
294 | + $fields = array_values($this->propertyMap); |
|
295 | + $fields[] = 'id'; |
|
296 | + $fields[] = 'uri'; |
|
297 | + $fields[] = 'synctoken'; |
|
298 | + $fields[] = 'components'; |
|
299 | + $fields[] = 'principaluri'; |
|
300 | + $fields[] = 'transparent'; |
|
301 | + |
|
302 | + // Making fields a comma-delimited list |
|
303 | + $query = $this->db->getQueryBuilder(); |
|
304 | + $query->select($fields)->from('calendars') |
|
305 | + ->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))) |
|
306 | + ->setMaxResults(1); |
|
307 | + $stmt = $query->execute(); |
|
308 | + |
|
309 | + $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
310 | + $stmt->closeCursor(); |
|
311 | + if ($row === false) { |
|
312 | + return null; |
|
313 | + } |
|
314 | + |
|
315 | + $components = []; |
|
316 | + if ($row['components']) { |
|
317 | + $components = explode(',',$row['components']); |
|
318 | + } |
|
319 | + |
|
320 | + $calendar = [ |
|
321 | + 'id' => $row['id'], |
|
322 | + 'uri' => $row['uri'], |
|
323 | + 'principaluri' => $row['principaluri'], |
|
324 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
325 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
326 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
327 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
328 | + ]; |
|
329 | + |
|
330 | + foreach($this->propertyMap as $xmlName=>$dbName) { |
|
331 | + $calendar[$xmlName] = $row[$dbName]; |
|
332 | + } |
|
333 | + |
|
334 | + return $calendar; |
|
335 | + } |
|
336 | + |
|
337 | + /** |
|
338 | + * Creates a new calendar for a principal. |
|
339 | + * |
|
340 | + * If the creation was a success, an id must be returned that can be used to reference |
|
341 | + * this calendar in other methods, such as updateCalendar. |
|
342 | + * |
|
343 | + * @param string $principalUri |
|
344 | + * @param string $calendarUri |
|
345 | + * @param array $properties |
|
346 | + * @return int |
|
347 | + */ |
|
348 | + function createCalendar($principalUri, $calendarUri, array $properties) { |
|
349 | + $values = [ |
|
350 | + 'principaluri' => $principalUri, |
|
351 | + 'uri' => $calendarUri, |
|
352 | + 'synctoken' => 1, |
|
353 | + 'transparent' => 0, |
|
354 | + 'components' => 'VEVENT,VTODO', |
|
355 | + 'displayname' => $calendarUri |
|
356 | + ]; |
|
357 | + |
|
358 | + // Default value |
|
359 | + $sccs = '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set'; |
|
360 | + if (isset($properties[$sccs])) { |
|
361 | + if (!($properties[$sccs] instanceof SupportedCalendarComponentSet)) { |
|
362 | + throw new DAV\Exception('The ' . $sccs . ' property must be of type: \Sabre\CalDAV\Property\SupportedCalendarComponentSet'); |
|
363 | + } |
|
364 | + $values['components'] = implode(',',$properties[$sccs]->getValue()); |
|
365 | + } |
|
366 | + $transp = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
367 | + if (isset($properties[$transp])) { |
|
368 | + $values['transparent'] = $properties[$transp]->getValue()==='transparent'; |
|
369 | + } |
|
370 | + |
|
371 | + foreach($this->propertyMap as $xmlName=>$dbName) { |
|
372 | + if (isset($properties[$xmlName])) { |
|
373 | + $values[$dbName] = $properties[$xmlName]; |
|
374 | + } |
|
375 | + } |
|
376 | + |
|
377 | + $query = $this->db->getQueryBuilder(); |
|
378 | + $query->insert('calendars'); |
|
379 | + foreach($values as $column => $value) { |
|
380 | + $query->setValue($column, $query->createNamedParameter($value)); |
|
381 | + } |
|
382 | + $query->execute(); |
|
383 | + return $query->getLastInsertId(); |
|
384 | + } |
|
385 | + |
|
386 | + /** |
|
387 | + * Updates properties for a calendar. |
|
388 | + * |
|
389 | + * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
390 | + * To do the actual updates, you must tell this object which properties |
|
391 | + * you're going to process with the handle() method. |
|
392 | + * |
|
393 | + * Calling the handle method is like telling the PropPatch object "I |
|
394 | + * promise I can handle updating this property". |
|
395 | + * |
|
396 | + * Read the PropPatch documentation for more info and examples. |
|
397 | + * |
|
398 | + * @param \Sabre\DAV\PropPatch $propPatch |
|
399 | + * @return void |
|
400 | + */ |
|
401 | + function updateCalendar($calendarId, \Sabre\DAV\PropPatch $propPatch) { |
|
402 | + $supportedProperties = array_keys($this->propertyMap); |
|
403 | + $supportedProperties[] = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
404 | + |
|
405 | + $propPatch->handle($supportedProperties, function($mutations) use ($calendarId) { |
|
406 | + $newValues = []; |
|
407 | + foreach ($mutations as $propertyName => $propertyValue) { |
|
408 | + |
|
409 | + switch ($propertyName) { |
|
410 | + case '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' : |
|
411 | + $fieldName = 'transparent'; |
|
412 | + $newValues[$fieldName] = $propertyValue->getValue() === 'transparent'; |
|
413 | + break; |
|
414 | + default : |
|
415 | + $fieldName = $this->propertyMap[$propertyName]; |
|
416 | + $newValues[$fieldName] = $propertyValue; |
|
417 | + break; |
|
418 | + } |
|
419 | + |
|
420 | + } |
|
421 | + $query = $this->db->getQueryBuilder(); |
|
422 | + $query->update('calendars'); |
|
423 | + foreach ($newValues as $fieldName => $value) { |
|
424 | + $query->set($fieldName, $query->createNamedParameter($value)); |
|
425 | + } |
|
426 | + $query->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))); |
|
427 | + $query->execute(); |
|
428 | + |
|
429 | + $this->addChange($calendarId, "", 2); |
|
430 | + |
|
431 | + return true; |
|
432 | + }); |
|
433 | + } |
|
434 | + |
|
435 | + /** |
|
436 | + * Delete a calendar and all it's objects |
|
437 | + * |
|
438 | + * @param mixed $calendarId |
|
439 | + * @return void |
|
440 | + */ |
|
441 | + function deleteCalendar($calendarId) { |
|
442 | + $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ?'); |
|
443 | + $stmt->execute([$calendarId]); |
|
444 | + |
|
445 | + $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendars` WHERE `id` = ?'); |
|
446 | + $stmt->execute([$calendarId]); |
|
447 | + |
|
448 | + $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarchanges` WHERE `calendarid` = ?'); |
|
449 | + $stmt->execute([$calendarId]); |
|
450 | + |
|
451 | + $this->sharingBackend->deleteAllShares($calendarId); |
|
452 | + } |
|
453 | + |
|
454 | + /** |
|
455 | + * Returns all calendar objects within a calendar. |
|
456 | + * |
|
457 | + * Every item contains an array with the following keys: |
|
458 | + * * calendardata - The iCalendar-compatible calendar data |
|
459 | + * * uri - a unique key which will be used to construct the uri. This can |
|
460 | + * be any arbitrary string, but making sure it ends with '.ics' is a |
|
461 | + * good idea. This is only the basename, or filename, not the full |
|
462 | + * path. |
|
463 | + * * lastmodified - a timestamp of the last modification time |
|
464 | + * * etag - An arbitrary string, surrounded by double-quotes. (e.g.: |
|
465 | + * '"abcdef"') |
|
466 | + * * size - The size of the calendar objects, in bytes. |
|
467 | + * * component - optional, a string containing the type of object, such |
|
468 | + * as 'vevent' or 'vtodo'. If specified, this will be used to populate |
|
469 | + * the Content-Type header. |
|
470 | + * |
|
471 | + * Note that the etag is optional, but it's highly encouraged to return for |
|
472 | + * speed reasons. |
|
473 | + * |
|
474 | + * The calendardata is also optional. If it's not returned |
|
475 | + * 'getCalendarObject' will be called later, which *is* expected to return |
|
476 | + * calendardata. |
|
477 | + * |
|
478 | + * If neither etag or size are specified, the calendardata will be |
|
479 | + * used/fetched to determine these numbers. If both are specified the |
|
480 | + * amount of times this is needed is reduced by a great degree. |
|
481 | + * |
|
482 | + * @param mixed $calendarId |
|
483 | + * @return array |
|
484 | + */ |
|
485 | + function getCalendarObjects($calendarId) { |
|
486 | + $query = $this->db->getQueryBuilder(); |
|
487 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'componenttype']) |
|
488 | + ->from('calendarobjects') |
|
489 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))); |
|
490 | + $stmt = $query->execute(); |
|
491 | + |
|
492 | + $result = []; |
|
493 | + foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) { |
|
494 | + $result[] = [ |
|
495 | + 'id' => $row['id'], |
|
496 | + 'uri' => $row['uri'], |
|
497 | + 'lastmodified' => $row['lastmodified'], |
|
498 | + 'etag' => '"' . $row['etag'] . '"', |
|
499 | + 'calendarid' => $row['calendarid'], |
|
500 | + 'size' => (int)$row['size'], |
|
501 | + 'component' => strtolower($row['componenttype']), |
|
502 | + ]; |
|
503 | + } |
|
504 | + |
|
505 | + return $result; |
|
506 | + } |
|
507 | + |
|
508 | + /** |
|
509 | + * Returns information from a single calendar object, based on it's object |
|
510 | + * uri. |
|
511 | + * |
|
512 | + * The object uri is only the basename, or filename and not a full path. |
|
513 | + * |
|
514 | + * The returned array must have the same keys as getCalendarObjects. The |
|
515 | + * 'calendardata' object is required here though, while it's not required |
|
516 | + * for getCalendarObjects. |
|
517 | + * |
|
518 | + * This method must return null if the object did not exist. |
|
519 | + * |
|
520 | + * @param mixed $calendarId |
|
521 | + * @param string $objectUri |
|
522 | + * @return array|null |
|
523 | + */ |
|
524 | + function getCalendarObject($calendarId, $objectUri) { |
|
525 | + |
|
526 | + $query = $this->db->getQueryBuilder(); |
|
527 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype']) |
|
528 | + ->from('calendarobjects') |
|
529 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
530 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))); |
|
531 | + $stmt = $query->execute(); |
|
532 | + $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
533 | + |
|
534 | + if(!$row) return null; |
|
535 | + |
|
536 | + return [ |
|
537 | + 'id' => $row['id'], |
|
538 | + 'uri' => $row['uri'], |
|
539 | + 'lastmodified' => $row['lastmodified'], |
|
540 | + 'etag' => '"' . $row['etag'] . '"', |
|
541 | + 'calendarid' => $row['calendarid'], |
|
542 | + 'size' => (int)$row['size'], |
|
543 | + 'calendardata' => $this->readBlob($row['calendardata']), |
|
544 | + 'component' => strtolower($row['componenttype']), |
|
545 | + ]; |
|
546 | + } |
|
547 | + |
|
548 | + /** |
|
549 | + * Returns a list of calendar objects. |
|
550 | + * |
|
551 | + * This method should work identical to getCalendarObject, but instead |
|
552 | + * return all the calendar objects in the list as an array. |
|
553 | + * |
|
554 | + * If the backend supports this, it may allow for some speed-ups. |
|
555 | + * |
|
556 | + * @param mixed $calendarId |
|
557 | + * @param string[] $uris |
|
558 | + * @return array |
|
559 | + */ |
|
560 | + function getMultipleCalendarObjects($calendarId, array $uris) { |
|
561 | + $query = $this->db->getQueryBuilder(); |
|
562 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype']) |
|
563 | + ->from('calendarobjects') |
|
564 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
565 | + ->andWhere($query->expr()->in('uri', $query->createParameter('uri'))) |
|
566 | + ->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY); |
|
567 | + |
|
568 | + $stmt = $query->execute(); |
|
569 | + |
|
570 | + $result = []; |
|
571 | + while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
572 | + |
|
573 | + $result[] = [ |
|
574 | + 'id' => $row['id'], |
|
575 | + 'uri' => $row['uri'], |
|
576 | + 'lastmodified' => $row['lastmodified'], |
|
577 | + 'etag' => '"' . $row['etag'] . '"', |
|
578 | + 'calendarid' => $row['calendarid'], |
|
579 | + 'size' => (int)$row['size'], |
|
580 | + 'calendardata' => $this->readBlob($row['calendardata']), |
|
581 | + 'component' => strtolower($row['componenttype']), |
|
582 | + ]; |
|
583 | + |
|
584 | + } |
|
585 | + return $result; |
|
586 | + } |
|
587 | + |
|
588 | + /** |
|
589 | + * Creates a new calendar object. |
|
590 | + * |
|
591 | + * The object uri is only the basename, or filename and not a full path. |
|
592 | + * |
|
593 | + * It is possible return an etag from this function, which will be used in |
|
594 | + * the response to this PUT request. Note that the ETag must be surrounded |
|
595 | + * by double-quotes. |
|
596 | + * |
|
597 | + * However, you should only really return this ETag if you don't mangle the |
|
598 | + * calendar-data. If the result of a subsequent GET to this object is not |
|
599 | + * the exact same as this request body, you should omit the ETag. |
|
600 | + * |
|
601 | + * @param mixed $calendarId |
|
602 | + * @param string $objectUri |
|
603 | + * @param string $calendarData |
|
604 | + * @return string |
|
605 | + */ |
|
606 | + function createCalendarObject($calendarId, $objectUri, $calendarData) { |
|
607 | + $extraData = $this->getDenormalizedData($calendarData); |
|
608 | + |
|
609 | + $query = $this->db->getQueryBuilder(); |
|
610 | + $query->insert('calendarobjects') |
|
611 | + ->values([ |
|
612 | + 'calendarid' => $query->createNamedParameter($calendarId), |
|
613 | + 'uri' => $query->createNamedParameter($objectUri), |
|
614 | + 'calendardata' => $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB), |
|
615 | + 'lastmodified' => $query->createNamedParameter(time()), |
|
616 | + 'etag' => $query->createNamedParameter($extraData['etag']), |
|
617 | + 'size' => $query->createNamedParameter($extraData['size']), |
|
618 | + 'componenttype' => $query->createNamedParameter($extraData['componentType']), |
|
619 | + 'firstoccurence' => $query->createNamedParameter($extraData['firstOccurence']), |
|
620 | + 'lastoccurence' => $query->createNamedParameter($extraData['lastOccurence']), |
|
621 | + 'uid' => $query->createNamedParameter($extraData['uid']), |
|
622 | + ]) |
|
623 | + ->execute(); |
|
624 | + |
|
625 | + $this->addChange($calendarId, $objectUri, 1); |
|
626 | + |
|
627 | + return '"' . $extraData['etag'] . '"'; |
|
628 | + } |
|
629 | + |
|
630 | + /** |
|
631 | + * Updates an existing calendarobject, based on it's uri. |
|
632 | + * |
|
633 | + * The object uri is only the basename, or filename and not a full path. |
|
634 | + * |
|
635 | + * It is possible return an etag from this function, which will be used in |
|
636 | + * the response to this PUT request. Note that the ETag must be surrounded |
|
637 | + * by double-quotes. |
|
638 | + * |
|
639 | + * However, you should only really return this ETag if you don't mangle the |
|
640 | + * calendar-data. If the result of a subsequent GET to this object is not |
|
641 | + * the exact same as this request body, you should omit the ETag. |
|
642 | + * |
|
643 | + * @param mixed $calendarId |
|
644 | + * @param string $objectUri |
|
645 | + * @param string $calendarData |
|
646 | + * @return string |
|
647 | + */ |
|
648 | + function updateCalendarObject($calendarId, $objectUri, $calendarData) { |
|
649 | + $extraData = $this->getDenormalizedData($calendarData); |
|
650 | + |
|
651 | + $query = $this->db->getQueryBuilder(); |
|
652 | + $query->update('calendarobjects') |
|
653 | + ->set('calendardata', $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB)) |
|
654 | + ->set('lastmodified', $query->createNamedParameter(time())) |
|
655 | + ->set('etag', $query->createNamedParameter($extraData['etag'])) |
|
656 | + ->set('size', $query->createNamedParameter($extraData['size'])) |
|
657 | + ->set('componenttype', $query->createNamedParameter($extraData['componentType'])) |
|
658 | + ->set('firstoccurence', $query->createNamedParameter($extraData['firstOccurence'])) |
|
659 | + ->set('lastoccurence', $query->createNamedParameter($extraData['lastOccurence'])) |
|
660 | + ->set('uid', $query->createNamedParameter($extraData['uid'])) |
|
661 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
662 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
663 | + ->execute(); |
|
664 | + |
|
665 | + $this->addChange($calendarId, $objectUri, 2); |
|
666 | + |
|
667 | + return '"' . $extraData['etag'] . '"'; |
|
668 | + } |
|
669 | + |
|
670 | + /** |
|
671 | + * Deletes an existing calendar object. |
|
672 | + * |
|
673 | + * The object uri is only the basename, or filename and not a full path. |
|
674 | + * |
|
675 | + * @param mixed $calendarId |
|
676 | + * @param string $objectUri |
|
677 | + * @return void |
|
678 | + */ |
|
679 | + function deleteCalendarObject($calendarId, $objectUri) { |
|
680 | + $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ? AND `uri` = ?'); |
|
681 | + $stmt->execute([$calendarId, $objectUri]); |
|
682 | + |
|
683 | + $this->addChange($calendarId, $objectUri, 3); |
|
684 | + } |
|
685 | + |
|
686 | + /** |
|
687 | + * Performs a calendar-query on the contents of this calendar. |
|
688 | + * |
|
689 | + * The calendar-query is defined in RFC4791 : CalDAV. Using the |
|
690 | + * calendar-query it is possible for a client to request a specific set of |
|
691 | + * object, based on contents of iCalendar properties, date-ranges and |
|
692 | + * iCalendar component types (VTODO, VEVENT). |
|
693 | + * |
|
694 | + * This method should just return a list of (relative) urls that match this |
|
695 | + * query. |
|
696 | + * |
|
697 | + * The list of filters are specified as an array. The exact array is |
|
698 | + * documented by Sabre\CalDAV\CalendarQueryParser. |
|
699 | + * |
|
700 | + * Note that it is extremely likely that getCalendarObject for every path |
|
701 | + * returned from this method will be called almost immediately after. You |
|
702 | + * may want to anticipate this to speed up these requests. |
|
703 | + * |
|
704 | + * This method provides a default implementation, which parses *all* the |
|
705 | + * iCalendar objects in the specified calendar. |
|
706 | + * |
|
707 | + * This default may well be good enough for personal use, and calendars |
|
708 | + * that aren't very large. But if you anticipate high usage, big calendars |
|
709 | + * or high loads, you are strongly adviced to optimize certain paths. |
|
710 | + * |
|
711 | + * The best way to do so is override this method and to optimize |
|
712 | + * specifically for 'common filters'. |
|
713 | + * |
|
714 | + * Requests that are extremely common are: |
|
715 | + * * requests for just VEVENTS |
|
716 | + * * requests for just VTODO |
|
717 | + * * requests with a time-range-filter on either VEVENT or VTODO. |
|
718 | + * |
|
719 | + * ..and combinations of these requests. It may not be worth it to try to |
|
720 | + * handle every possible situation and just rely on the (relatively |
|
721 | + * easy to use) CalendarQueryValidator to handle the rest. |
|
722 | + * |
|
723 | + * Note that especially time-range-filters may be difficult to parse. A |
|
724 | + * time-range filter specified on a VEVENT must for instance also handle |
|
725 | + * recurrence rules correctly. |
|
726 | + * A good example of how to interprete all these filters can also simply |
|
727 | + * be found in Sabre\CalDAV\CalendarQueryFilter. This class is as correct |
|
728 | + * as possible, so it gives you a good idea on what type of stuff you need |
|
729 | + * to think of. |
|
730 | + * |
|
731 | + * @param mixed $calendarId |
|
732 | + * @param array $filters |
|
733 | + * @return array |
|
734 | + */ |
|
735 | + function calendarQuery($calendarId, array $filters) { |
|
736 | + $componentType = null; |
|
737 | + $requirePostFilter = true; |
|
738 | + $timeRange = null; |
|
739 | + |
|
740 | + // if no filters were specified, we don't need to filter after a query |
|
741 | + if (!$filters['prop-filters'] && !$filters['comp-filters']) { |
|
742 | + $requirePostFilter = false; |
|
743 | + } |
|
744 | + |
|
745 | + // Figuring out if there's a component filter |
|
746 | + if (count($filters['comp-filters']) > 0 && !$filters['comp-filters'][0]['is-not-defined']) { |
|
747 | + $componentType = $filters['comp-filters'][0]['name']; |
|
748 | + |
|
749 | + // Checking if we need post-filters |
|
750 | + if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['time-range'] && !$filters['comp-filters'][0]['prop-filters']) { |
|
751 | + $requirePostFilter = false; |
|
752 | + } |
|
753 | + // There was a time-range filter |
|
754 | + if ($componentType == 'VEVENT' && isset($filters['comp-filters'][0]['time-range'])) { |
|
755 | + $timeRange = $filters['comp-filters'][0]['time-range']; |
|
756 | + |
|
757 | + // If start time OR the end time is not specified, we can do a |
|
758 | + // 100% accurate mysql query. |
|
759 | + if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['prop-filters'] && (!$timeRange['start'] || !$timeRange['end'])) { |
|
760 | + $requirePostFilter = false; |
|
761 | + } |
|
762 | + } |
|
763 | + |
|
764 | + } |
|
765 | + $columns = ['uri']; |
|
766 | + if ($requirePostFilter) { |
|
767 | + $columns = ['uri', 'calendardata']; |
|
768 | + } |
|
769 | + $query = $this->db->getQueryBuilder(); |
|
770 | + $query->select($columns) |
|
771 | + ->from('calendarobjects') |
|
772 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))); |
|
773 | + |
|
774 | + if ($componentType) { |
|
775 | + $query->andWhere($query->expr()->eq('componenttype', $query->createNamedParameter($componentType))); |
|
776 | + } |
|
777 | + |
|
778 | + if ($timeRange && $timeRange['start']) { |
|
779 | + $query->andWhere($query->expr()->gt('lastoccurence', $query->createNamedParameter($timeRange['start']->getTimeStamp()))); |
|
780 | + } |
|
781 | + if ($timeRange && $timeRange['end']) { |
|
782 | + $query->andWhere($query->expr()->lt('firstoccurence', $query->createNamedParameter($timeRange['end']->getTimeStamp()))); |
|
783 | + } |
|
784 | + |
|
785 | + $stmt = $query->execute(); |
|
786 | + |
|
787 | + $result = []; |
|
788 | + while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
789 | + if ($requirePostFilter) { |
|
790 | + if (!$this->validateFilterForObject($row, $filters)) { |
|
791 | + continue; |
|
792 | + } |
|
793 | + } |
|
794 | + $result[] = $row['uri']; |
|
795 | + } |
|
796 | + |
|
797 | + return $result; |
|
798 | + } |
|
799 | + |
|
800 | + /** |
|
801 | + * Searches through all of a users calendars and calendar objects to find |
|
802 | + * an object with a specific UID. |
|
803 | + * |
|
804 | + * This method should return the path to this object, relative to the |
|
805 | + * calendar home, so this path usually only contains two parts: |
|
806 | + * |
|
807 | + * calendarpath/objectpath.ics |
|
808 | + * |
|
809 | + * If the uid is not found, return null. |
|
810 | + * |
|
811 | + * This method should only consider * objects that the principal owns, so |
|
812 | + * any calendars owned by other principals that also appear in this |
|
813 | + * collection should be ignored. |
|
814 | + * |
|
815 | + * @param string $principalUri |
|
816 | + * @param string $uid |
|
817 | + * @return string|null |
|
818 | + */ |
|
819 | + function getCalendarObjectByUID($principalUri, $uid) { |
|
820 | + |
|
821 | + $query = $this->db->getQueryBuilder(); |
|
822 | + $query->selectAlias('c.uri', 'calendaruri')->selectAlias('co.uri', 'objecturi') |
|
823 | + ->from('calendarobjects', 'co') |
|
824 | + ->leftJoin('co', 'calendars', 'c', $query->expr()->eq('co.calendarid', 'c.id')) |
|
825 | + ->where($query->expr()->eq('c.principaluri', $query->createNamedParameter($principalUri))) |
|
826 | + ->andWhere($query->expr()->eq('co.uid', $query->createNamedParameter($uid))); |
|
827 | + |
|
828 | + $stmt = $query->execute(); |
|
829 | + |
|
830 | + if ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
831 | + return $row['calendaruri'] . '/' . $row['objecturi']; |
|
832 | + } |
|
833 | + |
|
834 | + return null; |
|
835 | + } |
|
836 | + |
|
837 | + /** |
|
838 | + * The getChanges method returns all the changes that have happened, since |
|
839 | + * the specified syncToken in the specified calendar. |
|
840 | + * |
|
841 | + * This function should return an array, such as the following: |
|
842 | + * |
|
843 | + * [ |
|
844 | + * 'syncToken' => 'The current synctoken', |
|
845 | + * 'added' => [ |
|
846 | + * 'new.txt', |
|
847 | + * ], |
|
848 | + * 'modified' => [ |
|
849 | + * 'modified.txt', |
|
850 | + * ], |
|
851 | + * 'deleted' => [ |
|
852 | + * 'foo.php.bak', |
|
853 | + * 'old.txt' |
|
854 | + * ] |
|
855 | + * ); |
|
856 | + * |
|
857 | + * The returned syncToken property should reflect the *current* syncToken |
|
858 | + * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
|
859 | + * property This is * needed here too, to ensure the operation is atomic. |
|
860 | + * |
|
861 | + * If the $syncToken argument is specified as null, this is an initial |
|
862 | + * sync, and all members should be reported. |
|
863 | + * |
|
864 | + * The modified property is an array of nodenames that have changed since |
|
865 | + * the last token. |
|
866 | + * |
|
867 | + * The deleted property is an array with nodenames, that have been deleted |
|
868 | + * from collection. |
|
869 | + * |
|
870 | + * The $syncLevel argument is basically the 'depth' of the report. If it's |
|
871 | + * 1, you only have to report changes that happened only directly in |
|
872 | + * immediate descendants. If it's 2, it should also include changes from |
|
873 | + * the nodes below the child collections. (grandchildren) |
|
874 | + * |
|
875 | + * The $limit argument allows a client to specify how many results should |
|
876 | + * be returned at most. If the limit is not specified, it should be treated |
|
877 | + * as infinite. |
|
878 | + * |
|
879 | + * If the limit (infinite or not) is higher than you're willing to return, |
|
880 | + * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
|
881 | + * |
|
882 | + * If the syncToken is expired (due to data cleanup) or unknown, you must |
|
883 | + * return null. |
|
884 | + * |
|
885 | + * The limit is 'suggestive'. You are free to ignore it. |
|
886 | + * |
|
887 | + * @param string $calendarId |
|
888 | + * @param string $syncToken |
|
889 | + * @param int $syncLevel |
|
890 | + * @param int $limit |
|
891 | + * @return array |
|
892 | + */ |
|
893 | + function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null) { |
|
894 | + // Current synctoken |
|
895 | + $stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*calendars` WHERE `id` = ?'); |
|
896 | + $stmt->execute([ $calendarId ]); |
|
897 | + $currentToken = $stmt->fetchColumn(0); |
|
898 | + |
|
899 | + if (is_null($currentToken)) { |
|
900 | + return null; |
|
901 | + } |
|
902 | + |
|
903 | + $result = [ |
|
904 | + 'syncToken' => $currentToken, |
|
905 | + 'added' => [], |
|
906 | + 'modified' => [], |
|
907 | + 'deleted' => [], |
|
908 | + ]; |
|
909 | + |
|
910 | + if ($syncToken) { |
|
911 | + |
|
912 | + $query = "SELECT `uri`, `operation` FROM `*PREFIX*calendarchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `calendarid` = ? ORDER BY `synctoken`"; |
|
913 | + if ($limit>0) { |
|
914 | + $query.= " `LIMIT` " . (int)$limit; |
|
915 | + } |
|
916 | + |
|
917 | + // Fetching all changes |
|
918 | + $stmt = $this->db->prepare($query); |
|
919 | + $stmt->execute([$syncToken, $currentToken, $calendarId]); |
|
920 | + |
|
921 | + $changes = []; |
|
922 | + |
|
923 | + // This loop ensures that any duplicates are overwritten, only the |
|
924 | + // last change on a node is relevant. |
|
925 | + while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
926 | + |
|
927 | + $changes[$row['uri']] = $row['operation']; |
|
928 | + |
|
929 | + } |
|
930 | + |
|
931 | + foreach($changes as $uri => $operation) { |
|
932 | + |
|
933 | + switch($operation) { |
|
934 | + case 1 : |
|
935 | + $result['added'][] = $uri; |
|
936 | + break; |
|
937 | + case 2 : |
|
938 | + $result['modified'][] = $uri; |
|
939 | + break; |
|
940 | + case 3 : |
|
941 | + $result['deleted'][] = $uri; |
|
942 | + break; |
|
943 | + } |
|
944 | + |
|
945 | + } |
|
946 | + } else { |
|
947 | + // No synctoken supplied, this is the initial sync. |
|
948 | + $query = "SELECT `uri` FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ?"; |
|
949 | + $stmt = $this->db->prepare($query); |
|
950 | + $stmt->execute([$calendarId]); |
|
951 | + |
|
952 | + $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
953 | + } |
|
954 | + return $result; |
|
955 | + |
|
956 | + } |
|
957 | + |
|
958 | + /** |
|
959 | + * Returns a list of subscriptions for a principal. |
|
960 | + * |
|
961 | + * Every subscription is an array with the following keys: |
|
962 | + * * id, a unique id that will be used by other functions to modify the |
|
963 | + * subscription. This can be the same as the uri or a database key. |
|
964 | + * * uri. This is just the 'base uri' or 'filename' of the subscription. |
|
965 | + * * principaluri. The owner of the subscription. Almost always the same as |
|
966 | + * principalUri passed to this method. |
|
967 | + * |
|
968 | + * Furthermore, all the subscription info must be returned too: |
|
969 | + * |
|
970 | + * 1. {DAV:}displayname |
|
971 | + * 2. {http://apple.com/ns/ical/}refreshrate |
|
972 | + * 3. {http://calendarserver.org/ns/}subscribed-strip-todos (omit if todos |
|
973 | + * should not be stripped). |
|
974 | + * 4. {http://calendarserver.org/ns/}subscribed-strip-alarms (omit if alarms |
|
975 | + * should not be stripped). |
|
976 | + * 5. {http://calendarserver.org/ns/}subscribed-strip-attachments (omit if |
|
977 | + * attachments should not be stripped). |
|
978 | + * 6. {http://calendarserver.org/ns/}source (Must be a |
|
979 | + * Sabre\DAV\Property\Href). |
|
980 | + * 7. {http://apple.com/ns/ical/}calendar-color |
|
981 | + * 8. {http://apple.com/ns/ical/}calendar-order |
|
982 | + * 9. {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
|
983 | + * (should just be an instance of |
|
984 | + * Sabre\CalDAV\Property\SupportedCalendarComponentSet, with a bunch of |
|
985 | + * default components). |
|
986 | + * |
|
987 | + * @param string $principalUri |
|
988 | + * @return array |
|
989 | + */ |
|
990 | + function getSubscriptionsForUser($principalUri) { |
|
991 | + $fields = array_values($this->subscriptionPropertyMap); |
|
992 | + $fields[] = 'id'; |
|
993 | + $fields[] = 'uri'; |
|
994 | + $fields[] = 'source'; |
|
995 | + $fields[] = 'principaluri'; |
|
996 | + $fields[] = 'lastmodified'; |
|
997 | + |
|
998 | + $query = $this->db->getQueryBuilder(); |
|
999 | + $query->select($fields) |
|
1000 | + ->from('calendarsubscriptions') |
|
1001 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
1002 | + ->orderBy('calendarorder', 'asc'); |
|
1003 | + $stmt =$query->execute(); |
|
1004 | + |
|
1005 | + $subscriptions = []; |
|
1006 | + while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
1007 | + |
|
1008 | + $subscription = [ |
|
1009 | + 'id' => $row['id'], |
|
1010 | + 'uri' => $row['uri'], |
|
1011 | + 'principaluri' => $row['principaluri'], |
|
1012 | + 'source' => $row['source'], |
|
1013 | + 'lastmodified' => $row['lastmodified'], |
|
1014 | + |
|
1015 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), |
|
1016 | + ]; |
|
1017 | + |
|
1018 | + foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) { |
|
1019 | + if (!is_null($row[$dbName])) { |
|
1020 | + $subscription[$xmlName] = $row[$dbName]; |
|
1021 | + } |
|
1022 | + } |
|
1023 | + |
|
1024 | + $subscriptions[] = $subscription; |
|
1025 | + |
|
1026 | + } |
|
1027 | + |
|
1028 | + return $subscriptions; |
|
1029 | + } |
|
1030 | + |
|
1031 | + /** |
|
1032 | + * Creates a new subscription for a principal. |
|
1033 | + * |
|
1034 | + * If the creation was a success, an id must be returned that can be used to reference |
|
1035 | + * this subscription in other methods, such as updateSubscription. |
|
1036 | + * |
|
1037 | + * @param string $principalUri |
|
1038 | + * @param string $uri |
|
1039 | + * @param array $properties |
|
1040 | + * @return mixed |
|
1041 | + */ |
|
1042 | + function createSubscription($principalUri, $uri, array $properties) { |
|
1043 | + |
|
1044 | + if (!isset($properties['{http://calendarserver.org/ns/}source'])) { |
|
1045 | + throw new Forbidden('The {http://calendarserver.org/ns/}source property is required when creating subscriptions'); |
|
1046 | + } |
|
1047 | + |
|
1048 | + $values = [ |
|
1049 | + 'principaluri' => $principalUri, |
|
1050 | + 'uri' => $uri, |
|
1051 | + 'source' => $properties['{http://calendarserver.org/ns/}source']->getHref(), |
|
1052 | + 'lastmodified' => time(), |
|
1053 | + ]; |
|
1054 | + |
|
1055 | + foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) { |
|
1056 | + if (isset($properties[$xmlName])) { |
|
1057 | + |
|
1058 | + $values[$dbName] = $properties[$xmlName]; |
|
1059 | + $fieldNames[] = $dbName; |
|
1060 | + } |
|
1061 | + } |
|
1062 | + |
|
1063 | + $query = $this->db->getQueryBuilder(); |
|
1064 | + $query->insert('calendarsubscriptions') |
|
1065 | + ->values([ |
|
1066 | + 'principaluri' => $query->createNamedParameter($values['principaluri']), |
|
1067 | + 'uri' => $query->createNamedParameter($values['uri']), |
|
1068 | + 'source' => $query->createNamedParameter($values['source']), |
|
1069 | + 'lastmodified' => $query->createNamedParameter($values['lastmodified']), |
|
1070 | + ]) |
|
1071 | + ->execute(); |
|
1072 | + |
|
1073 | + return $this->db->lastInsertId('*PREFIX*calendarsubscriptions'); |
|
1074 | + } |
|
1075 | + |
|
1076 | + /** |
|
1077 | + * Updates a subscription |
|
1078 | + * |
|
1079 | + * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
1080 | + * To do the actual updates, you must tell this object which properties |
|
1081 | + * you're going to process with the handle() method. |
|
1082 | + * |
|
1083 | + * Calling the handle method is like telling the PropPatch object "I |
|
1084 | + * promise I can handle updating this property". |
|
1085 | + * |
|
1086 | + * Read the PropPatch documentation for more info and examples. |
|
1087 | + * |
|
1088 | + * @param mixed $subscriptionId |
|
1089 | + * @param \Sabre\DAV\PropPatch $propPatch |
|
1090 | + * @return void |
|
1091 | + */ |
|
1092 | + function updateSubscription($subscriptionId, DAV\PropPatch $propPatch) { |
|
1093 | + $supportedProperties = array_keys($this->subscriptionPropertyMap); |
|
1094 | + $supportedProperties[] = '{http://calendarserver.org/ns/}source'; |
|
1095 | + |
|
1096 | + $propPatch->handle($supportedProperties, function($mutations) use ($subscriptionId) { |
|
1097 | + |
|
1098 | + $newValues = []; |
|
1099 | + |
|
1100 | + foreach($mutations as $propertyName=>$propertyValue) { |
|
1101 | + if ($propertyName === '{http://calendarserver.org/ns/}source') { |
|
1102 | + $newValues['source'] = $propertyValue->getHref(); |
|
1103 | + } else { |
|
1104 | + $fieldName = $this->subscriptionPropertyMap[$propertyName]; |
|
1105 | + $newValues[$fieldName] = $propertyValue; |
|
1106 | + } |
|
1107 | + } |
|
1108 | + |
|
1109 | + $query = $this->db->getQueryBuilder(); |
|
1110 | + $query->update('calendarsubscriptions') |
|
1111 | + ->set('lastmodified', $query->createNamedParameter(time())); |
|
1112 | + foreach($newValues as $fieldName=>$value) { |
|
1113 | + $query->set($fieldName, $query->createNamedParameter($value)); |
|
1114 | + } |
|
1115 | + $query->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
1116 | + ->execute(); |
|
1117 | + |
|
1118 | + return true; |
|
1119 | + |
|
1120 | + }); |
|
1121 | + } |
|
1122 | + |
|
1123 | + /** |
|
1124 | + * Deletes a subscription. |
|
1125 | + * |
|
1126 | + * @param mixed $subscriptionId |
|
1127 | + * @return void |
|
1128 | + */ |
|
1129 | + function deleteSubscription($subscriptionId) { |
|
1130 | + $query = $this->db->getQueryBuilder(); |
|
1131 | + $query->delete('calendarsubscriptions') |
|
1132 | + ->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
1133 | + ->execute(); |
|
1134 | + } |
|
1135 | + |
|
1136 | + /** |
|
1137 | + * Returns a single scheduling object for the inbox collection. |
|
1138 | + * |
|
1139 | + * The returned array should contain the following elements: |
|
1140 | + * * uri - A unique basename for the object. This will be used to |
|
1141 | + * construct a full uri. |
|
1142 | + * * calendardata - The iCalendar object |
|
1143 | + * * lastmodified - The last modification date. Can be an int for a unix |
|
1144 | + * timestamp, or a PHP DateTime object. |
|
1145 | + * * etag - A unique token that must change if the object changed. |
|
1146 | + * * size - The size of the object, in bytes. |
|
1147 | + * |
|
1148 | + * @param string $principalUri |
|
1149 | + * @param string $objectUri |
|
1150 | + * @return array |
|
1151 | + */ |
|
1152 | + function getSchedulingObject($principalUri, $objectUri) { |
|
1153 | + $query = $this->db->getQueryBuilder(); |
|
1154 | + $stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size']) |
|
1155 | + ->from('schedulingobjects') |
|
1156 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
1157 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
1158 | + ->execute(); |
|
1159 | + |
|
1160 | + $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
1161 | + |
|
1162 | + if(!$row) { |
|
1163 | + return null; |
|
1164 | + } |
|
1165 | + |
|
1166 | + return [ |
|
1167 | + 'uri' => $row['uri'], |
|
1168 | + 'calendardata' => $row['calendardata'], |
|
1169 | + 'lastmodified' => $row['lastmodified'], |
|
1170 | + 'etag' => '"' . $row['etag'] . '"', |
|
1171 | + 'size' => (int)$row['size'], |
|
1172 | + ]; |
|
1173 | + } |
|
1174 | + |
|
1175 | + /** |
|
1176 | + * Returns all scheduling objects for the inbox collection. |
|
1177 | + * |
|
1178 | + * These objects should be returned as an array. Every item in the array |
|
1179 | + * should follow the same structure as returned from getSchedulingObject. |
|
1180 | + * |
|
1181 | + * The main difference is that 'calendardata' is optional. |
|
1182 | + * |
|
1183 | + * @param string $principalUri |
|
1184 | + * @return array |
|
1185 | + */ |
|
1186 | + function getSchedulingObjects($principalUri) { |
|
1187 | + $query = $this->db->getQueryBuilder(); |
|
1188 | + $stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size']) |
|
1189 | + ->from('schedulingobjects') |
|
1190 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
1191 | + ->execute(); |
|
1192 | + |
|
1193 | + $result = []; |
|
1194 | + foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) { |
|
1195 | + $result[] = [ |
|
1196 | + 'calendardata' => $row['calendardata'], |
|
1197 | + 'uri' => $row['uri'], |
|
1198 | + 'lastmodified' => $row['lastmodified'], |
|
1199 | + 'etag' => '"' . $row['etag'] . '"', |
|
1200 | + 'size' => (int)$row['size'], |
|
1201 | + ]; |
|
1202 | + } |
|
1203 | + |
|
1204 | + return $result; |
|
1205 | + } |
|
1206 | + |
|
1207 | + /** |
|
1208 | + * Deletes a scheduling object from the inbox collection. |
|
1209 | + * |
|
1210 | + * @param string $principalUri |
|
1211 | + * @param string $objectUri |
|
1212 | + * @return void |
|
1213 | + */ |
|
1214 | + function deleteSchedulingObject($principalUri, $objectUri) { |
|
1215 | + $query = $this->db->getQueryBuilder(); |
|
1216 | + $query->delete('schedulingobjects') |
|
1217 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
1218 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
1219 | + ->execute(); |
|
1220 | + } |
|
1221 | + |
|
1222 | + /** |
|
1223 | + * Creates a new scheduling object. This should land in a users' inbox. |
|
1224 | + * |
|
1225 | + * @param string $principalUri |
|
1226 | + * @param string $objectUri |
|
1227 | + * @param string $objectData |
|
1228 | + * @return void |
|
1229 | + */ |
|
1230 | + function createSchedulingObject($principalUri, $objectUri, $objectData) { |
|
1231 | + $query = $this->db->getQueryBuilder(); |
|
1232 | + $query->insert('schedulingobjects') |
|
1233 | + ->values([ |
|
1234 | + 'principaluri' => $query->createNamedParameter($principalUri), |
|
1235 | + 'calendardata' => $query->createNamedParameter($objectData), |
|
1236 | + 'uri' => $query->createNamedParameter($objectUri), |
|
1237 | + 'lastmodified' => $query->createNamedParameter(time()), |
|
1238 | + 'etag' => $query->createNamedParameter(md5($objectData)), |
|
1239 | + 'size' => $query->createNamedParameter(strlen($objectData)) |
|
1240 | + ]) |
|
1241 | + ->execute(); |
|
1242 | + } |
|
1243 | + |
|
1244 | + /** |
|
1245 | + * Adds a change record to the calendarchanges table. |
|
1246 | + * |
|
1247 | + * @param mixed $calendarId |
|
1248 | + * @param string $objectUri |
|
1249 | + * @param int $operation 1 = add, 2 = modify, 3 = delete. |
|
1250 | + * @return void |
|
1251 | + */ |
|
1252 | + protected function addChange($calendarId, $objectUri, $operation) { |
|
1253 | + |
|
1254 | + $stmt = $this->db->prepare('INSERT INTO `*PREFIX*calendarchanges` (`uri`, `synctoken`, `calendarid`, `operation`) SELECT ?, `synctoken`, ?, ? FROM `*PREFIX*calendars` WHERE `id` = ?'); |
|
1255 | + $stmt->execute([ |
|
1256 | + $objectUri, |
|
1257 | + $calendarId, |
|
1258 | + $operation, |
|
1259 | + $calendarId |
|
1260 | + ]); |
|
1261 | + $stmt = $this->db->prepare('UPDATE `*PREFIX*calendars` SET `synctoken` = `synctoken` + 1 WHERE `id` = ?'); |
|
1262 | + $stmt->execute([ |
|
1263 | + $calendarId |
|
1264 | + ]); |
|
1265 | + |
|
1266 | + } |
|
1267 | + |
|
1268 | + /** |
|
1269 | + * Parses some information from calendar objects, used for optimized |
|
1270 | + * calendar-queries. |
|
1271 | + * |
|
1272 | + * Returns an array with the following keys: |
|
1273 | + * * etag - An md5 checksum of the object without the quotes. |
|
1274 | + * * size - Size of the object in bytes |
|
1275 | + * * componentType - VEVENT, VTODO or VJOURNAL |
|
1276 | + * * firstOccurence |
|
1277 | + * * lastOccurence |
|
1278 | + * * uid - value of the UID property |
|
1279 | + * |
|
1280 | + * @param string $calendarData |
|
1281 | + * @return array |
|
1282 | + */ |
|
1283 | + protected function getDenormalizedData($calendarData) { |
|
1284 | + |
|
1285 | + $vObject = Reader::read($calendarData); |
|
1286 | + $componentType = null; |
|
1287 | + $component = null; |
|
1288 | + $firstOccurence = null; |
|
1289 | + $lastOccurence = null; |
|
1290 | + $uid = null; |
|
1291 | + foreach($vObject->getComponents() as $component) { |
|
1292 | + if ($component->name!=='VTIMEZONE') { |
|
1293 | + $componentType = $component->name; |
|
1294 | + $uid = (string)$component->UID; |
|
1295 | + break; |
|
1296 | + } |
|
1297 | + } |
|
1298 | + if (!$componentType) { |
|
1299 | + throw new \Sabre\DAV\Exception\BadRequest('Calendar objects must have a VJOURNAL, VEVENT or VTODO component'); |
|
1300 | + } |
|
1301 | + if ($componentType === 'VEVENT' && $component->DTSTART) { |
|
1302 | + $firstOccurence = $component->DTSTART->getDateTime()->getTimeStamp(); |
|
1303 | + // Finding the last occurence is a bit harder |
|
1304 | + if (!isset($component->RRULE)) { |
|
1305 | + if (isset($component->DTEND)) { |
|
1306 | + $lastOccurence = $component->DTEND->getDateTime()->getTimeStamp(); |
|
1307 | + } elseif (isset($component->DURATION)) { |
|
1308 | + $endDate = clone $component->DTSTART->getDateTime(); |
|
1309 | + $endDate->add(DateTimeParser::parse($component->DURATION->getValue())); |
|
1310 | + $lastOccurence = $endDate->getTimeStamp(); |
|
1311 | + } elseif (!$component->DTSTART->hasTime()) { |
|
1312 | + $endDate = clone $component->DTSTART->getDateTime(); |
|
1313 | + $endDate->modify('+1 day'); |
|
1314 | + $lastOccurence = $endDate->getTimeStamp(); |
|
1315 | + } else { |
|
1316 | + $lastOccurence = $firstOccurence; |
|
1317 | + } |
|
1318 | + } else { |
|
1319 | + $it = new RecurrenceIterator($vObject, (string)$component->UID); |
|
1320 | + $maxDate = new \DateTime(self::MAX_DATE); |
|
1321 | + if ($it->isInfinite()) { |
|
1322 | + $lastOccurence = $maxDate->getTimeStamp(); |
|
1323 | + } else { |
|
1324 | + $end = $it->getDtEnd(); |
|
1325 | + while($it->valid() && $end < $maxDate) { |
|
1326 | + $end = $it->getDtEnd(); |
|
1327 | + $it->next(); |
|
1328 | + |
|
1329 | + } |
|
1330 | + $lastOccurence = $end->getTimeStamp(); |
|
1331 | + } |
|
1332 | + |
|
1333 | + } |
|
1334 | + } |
|
1335 | + |
|
1336 | + return [ |
|
1337 | + 'etag' => md5($calendarData), |
|
1338 | + 'size' => strlen($calendarData), |
|
1339 | + 'componentType' => $componentType, |
|
1340 | + 'firstOccurence' => is_null($firstOccurence) ? null : max(0, $firstOccurence), |
|
1341 | + 'lastOccurence' => $lastOccurence, |
|
1342 | + 'uid' => $uid, |
|
1343 | + ]; |
|
1344 | + |
|
1345 | + } |
|
1346 | + |
|
1347 | + private function readBlob($cardData) { |
|
1348 | + if (is_resource($cardData)) { |
|
1349 | + return stream_get_contents($cardData); |
|
1350 | + } |
|
1351 | + |
|
1352 | + return $cardData; |
|
1353 | + } |
|
1354 | + |
|
1355 | + /** |
|
1356 | + * @param IShareable $shareable |
|
1357 | + * @param array $add |
|
1358 | + * @param array $remove |
|
1359 | + */ |
|
1360 | + public function updateShares($shareable, $add, $remove) { |
|
1361 | + $this->sharingBackend->updateShares($shareable, $add, $remove); |
|
1362 | + } |
|
1363 | + |
|
1364 | + /** |
|
1365 | + * @param int $resourceId |
|
1366 | + * @return array |
|
1367 | + */ |
|
1368 | + public function getShares($resourceId) { |
|
1369 | + return $this->sharingBackend->getShares($resourceId); |
|
1370 | + } |
|
1371 | + |
|
1372 | + /** |
|
1373 | + * @param int $resourceId |
|
1374 | + * @param array $acl |
|
1375 | + * @return array |
|
1376 | + */ |
|
1377 | + public function applyShareAcl($resourceId, $acl) { |
|
1378 | + return $this->sharingBackend->applyShareAcl($resourceId, $acl); |
|
1379 | + } |
|
1380 | + |
|
1381 | + private function convertPrincipal($principalUri, $toV2) { |
|
1382 | + if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
|
1383 | + list(, $name) = URLUtil::splitPath($principalUri); |
|
1384 | + if ($toV2 === true) { |
|
1385 | + return "principals/users/$name"; |
|
1386 | + } |
|
1387 | + return "principals/$name"; |
|
1388 | + } |
|
1389 | + return $principalUri; |
|
1390 | + } |
|
1391 | 1391 | } |
@@ -867,6 +867,7 @@ discard block |
||
867 | 867 | * * readOnly - boolean |
868 | 868 | * * summary - Optional, a description for the share |
869 | 869 | * |
870 | + * @param integer $addressBookId |
|
870 | 871 | * @return array |
871 | 872 | */ |
872 | 873 | public function getShares($addressBookId) { |
@@ -966,7 +967,7 @@ discard block |
||
966 | 967 | |
967 | 968 | /** |
968 | 969 | * For shared address books the sharee is set in the ACL of the address book |
969 | - * @param $addressBookId |
|
970 | + * @param integer $addressBookId |
|
970 | 971 | * @param $acl |
971 | 972 | * @return array |
972 | 973 | */ |
@@ -974,6 +975,9 @@ discard block |
||
974 | 975 | return $this->sharingBackend->applyShareAcl($addressBookId, $acl); |
975 | 976 | } |
976 | 977 | |
978 | + /** |
|
979 | + * @param boolean $toV2 |
|
980 | + */ |
|
977 | 981 | private function convertPrincipal($principalUri, $toV2) { |
978 | 982 | if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
979 | 983 | list(, $name) = URLUtil::splitPath($principalUri); |
@@ -44,946 +44,946 @@ |
||
44 | 44 | |
45 | 45 | class CardDavBackend implements BackendInterface, SyncSupport { |
46 | 46 | |
47 | - /** @var Principal */ |
|
48 | - private $principalBackend; |
|
49 | - |
|
50 | - /** @var string */ |
|
51 | - private $dbCardsTable = 'cards'; |
|
52 | - |
|
53 | - /** @var string */ |
|
54 | - private $dbCardsPropertiesTable = 'cards_properties'; |
|
55 | - |
|
56 | - /** @var IDBConnection */ |
|
57 | - private $db; |
|
58 | - |
|
59 | - /** @var Backend */ |
|
60 | - private $sharingBackend; |
|
61 | - |
|
62 | - /** @var array properties to index */ |
|
63 | - public static $indexProperties = array( |
|
64 | - 'BDAY', 'UID', 'N', 'FN', 'TITLE', 'ROLE', 'NOTE', 'NICKNAME', |
|
65 | - 'ORG', 'CATEGORIES', 'EMAIL', 'TEL', 'IMPP', 'ADR', 'URL', 'GEO', 'CLOUD'); |
|
66 | - |
|
67 | - /** @var EventDispatcherInterface */ |
|
68 | - private $dispatcher; |
|
69 | - |
|
70 | - /** |
|
71 | - * CardDavBackend constructor. |
|
72 | - * |
|
73 | - * @param IDBConnection $db |
|
74 | - * @param Principal $principalBackend |
|
75 | - * @param EventDispatcherInterface $dispatcher |
|
76 | - */ |
|
77 | - public function __construct(IDBConnection $db, |
|
78 | - Principal $principalBackend, |
|
79 | - EventDispatcherInterface $dispatcher = null) { |
|
80 | - $this->db = $db; |
|
81 | - $this->principalBackend = $principalBackend; |
|
82 | - $this->dispatcher = $dispatcher; |
|
83 | - $this->sharingBackend = new Backend($this->db, $principalBackend, 'addressbook'); |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Returns the list of address books for a specific user. |
|
88 | - * |
|
89 | - * Every addressbook should have the following properties: |
|
90 | - * id - an arbitrary unique id |
|
91 | - * uri - the 'basename' part of the url |
|
92 | - * principaluri - Same as the passed parameter |
|
93 | - * |
|
94 | - * Any additional clark-notation property may be passed besides this. Some |
|
95 | - * common ones are : |
|
96 | - * {DAV:}displayname |
|
97 | - * {urn:ietf:params:xml:ns:carddav}addressbook-description |
|
98 | - * {http://calendarserver.org/ns/}getctag |
|
99 | - * |
|
100 | - * @param string $principalUri |
|
101 | - * @return array |
|
102 | - */ |
|
103 | - function getAddressBooksForUser($principalUri) { |
|
104 | - $principalUriOriginal = $principalUri; |
|
105 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
106 | - $query = $this->db->getQueryBuilder(); |
|
107 | - $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
108 | - ->from('addressbooks') |
|
109 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
110 | - |
|
111 | - $addressBooks = []; |
|
112 | - |
|
113 | - $result = $query->execute(); |
|
114 | - while($row = $result->fetch()) { |
|
115 | - $addressBooks[$row['id']] = [ |
|
116 | - 'id' => $row['id'], |
|
117 | - 'uri' => $row['uri'], |
|
118 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
|
119 | - '{DAV:}displayname' => $row['displayname'], |
|
120 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
121 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
122 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
123 | - ]; |
|
124 | - } |
|
125 | - $result->closeCursor(); |
|
126 | - |
|
127 | - // query for shared calendars |
|
128 | - $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); |
|
129 | - $principals[]= $principalUri; |
|
130 | - |
|
131 | - $query = $this->db->getQueryBuilder(); |
|
132 | - $result = $query->select(['a.id', 'a.uri', 'a.displayname', 'a.principaluri', 'a.description', 'a.synctoken', 's.access']) |
|
133 | - ->from('dav_shares', 's') |
|
134 | - ->join('s', 'addressbooks', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
135 | - ->where($query->expr()->in('s.principaluri', $query->createParameter('principaluri'))) |
|
136 | - ->andWhere($query->expr()->eq('s.type', $query->createParameter('type'))) |
|
137 | - ->setParameter('type', 'addressbook') |
|
138 | - ->setParameter('principaluri', $principals, IQueryBuilder::PARAM_STR_ARRAY) |
|
139 | - ->execute(); |
|
140 | - |
|
141 | - while($row = $result->fetch()) { |
|
142 | - list(, $name) = URLUtil::splitPath($row['principaluri']); |
|
143 | - $uri = $row['uri'] . '_shared_by_' . $name; |
|
144 | - $displayName = $row['displayname'] . "($name)"; |
|
145 | - if (!isset($addressBooks[$row['id']])) { |
|
146 | - $addressBooks[$row['id']] = [ |
|
147 | - 'id' => $row['id'], |
|
148 | - 'uri' => $uri, |
|
149 | - 'principaluri' => $principalUri, |
|
150 | - '{DAV:}displayname' => $displayName, |
|
151 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
152 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
153 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
154 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $row['principaluri'], |
|
155 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ, |
|
156 | - ]; |
|
157 | - } |
|
158 | - } |
|
159 | - $result->closeCursor(); |
|
160 | - |
|
161 | - return array_values($addressBooks); |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * @param int $addressBookId |
|
166 | - */ |
|
167 | - public function getAddressBookById($addressBookId) { |
|
168 | - $query = $this->db->getQueryBuilder(); |
|
169 | - $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
170 | - ->from('addressbooks') |
|
171 | - ->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) |
|
172 | - ->execute(); |
|
173 | - |
|
174 | - $row = $result->fetch(); |
|
175 | - $result->closeCursor(); |
|
176 | - if ($row === false) { |
|
177 | - return null; |
|
178 | - } |
|
179 | - |
|
180 | - return [ |
|
181 | - 'id' => $row['id'], |
|
182 | - 'uri' => $row['uri'], |
|
183 | - 'principaluri' => $row['principaluri'], |
|
184 | - '{DAV:}displayname' => $row['displayname'], |
|
185 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
186 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
187 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
188 | - ]; |
|
189 | - } |
|
190 | - |
|
191 | - /** |
|
192 | - * @param $addressBookUri |
|
193 | - * @return array|null |
|
194 | - */ |
|
195 | - public function getAddressBooksByUri($principal, $addressBookUri) { |
|
196 | - $query = $this->db->getQueryBuilder(); |
|
197 | - $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
198 | - ->from('addressbooks') |
|
199 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($addressBookUri))) |
|
200 | - ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal))) |
|
201 | - ->setMaxResults(1) |
|
202 | - ->execute(); |
|
203 | - |
|
204 | - $row = $result->fetch(); |
|
205 | - $result->closeCursor(); |
|
206 | - if ($row === false) { |
|
207 | - return null; |
|
208 | - } |
|
209 | - |
|
210 | - return [ |
|
211 | - 'id' => $row['id'], |
|
212 | - 'uri' => $row['uri'], |
|
213 | - 'principaluri' => $row['principaluri'], |
|
214 | - '{DAV:}displayname' => $row['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 | - ]; |
|
219 | - } |
|
220 | - |
|
221 | - /** |
|
222 | - * Updates properties for an address book. |
|
223 | - * |
|
224 | - * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
225 | - * To do the actual updates, you must tell this object which properties |
|
226 | - * you're going to process with the handle() method. |
|
227 | - * |
|
228 | - * Calling the handle method is like telling the PropPatch object "I |
|
229 | - * promise I can handle updating this property". |
|
230 | - * |
|
231 | - * Read the PropPatch documentation for more info and examples. |
|
232 | - * |
|
233 | - * @param string $addressBookId |
|
234 | - * @param \Sabre\DAV\PropPatch $propPatch |
|
235 | - * @return void |
|
236 | - */ |
|
237 | - function updateAddressBook($addressBookId, \Sabre\DAV\PropPatch $propPatch) { |
|
238 | - $supportedProperties = [ |
|
239 | - '{DAV:}displayname', |
|
240 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description', |
|
241 | - ]; |
|
242 | - |
|
243 | - $propPatch->handle($supportedProperties, function($mutations) use ($addressBookId) { |
|
244 | - |
|
245 | - $updates = []; |
|
246 | - foreach($mutations as $property=>$newValue) { |
|
247 | - |
|
248 | - switch($property) { |
|
249 | - case '{DAV:}displayname' : |
|
250 | - $updates['displayname'] = $newValue; |
|
251 | - break; |
|
252 | - case '{' . Plugin::NS_CARDDAV . '}addressbook-description' : |
|
253 | - $updates['description'] = $newValue; |
|
254 | - break; |
|
255 | - } |
|
256 | - } |
|
257 | - $query = $this->db->getQueryBuilder(); |
|
258 | - $query->update('addressbooks'); |
|
259 | - |
|
260 | - foreach($updates as $key=>$value) { |
|
261 | - $query->set($key, $query->createNamedParameter($value)); |
|
262 | - } |
|
263 | - $query->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) |
|
264 | - ->execute(); |
|
265 | - |
|
266 | - $this->addChange($addressBookId, "", 2); |
|
267 | - |
|
268 | - return true; |
|
269 | - |
|
270 | - }); |
|
271 | - } |
|
272 | - |
|
273 | - /** |
|
274 | - * Creates a new address book |
|
275 | - * |
|
276 | - * @param string $principalUri |
|
277 | - * @param string $url Just the 'basename' of the url. |
|
278 | - * @param array $properties |
|
279 | - * @return int |
|
280 | - * @throws BadRequest |
|
281 | - */ |
|
282 | - function createAddressBook($principalUri, $url, array $properties) { |
|
283 | - $values = [ |
|
284 | - 'displayname' => null, |
|
285 | - 'description' => null, |
|
286 | - 'principaluri' => $principalUri, |
|
287 | - 'uri' => $url, |
|
288 | - 'synctoken' => 1 |
|
289 | - ]; |
|
290 | - |
|
291 | - foreach($properties as $property=>$newValue) { |
|
292 | - |
|
293 | - switch($property) { |
|
294 | - case '{DAV:}displayname' : |
|
295 | - $values['displayname'] = $newValue; |
|
296 | - break; |
|
297 | - case '{' . Plugin::NS_CARDDAV . '}addressbook-description' : |
|
298 | - $values['description'] = $newValue; |
|
299 | - break; |
|
300 | - default : |
|
301 | - throw new BadRequest('Unknown property: ' . $property); |
|
302 | - } |
|
303 | - |
|
304 | - } |
|
305 | - |
|
306 | - // Fallback to make sure the displayname is set. Some clients may refuse |
|
307 | - // to work with addressbooks not having a displayname. |
|
308 | - if(is_null($values['displayname'])) { |
|
309 | - $values['displayname'] = $url; |
|
310 | - } |
|
311 | - |
|
312 | - $query = $this->db->getQueryBuilder(); |
|
313 | - $query->insert('addressbooks') |
|
314 | - ->values([ |
|
315 | - 'uri' => $query->createParameter('uri'), |
|
316 | - 'displayname' => $query->createParameter('displayname'), |
|
317 | - 'description' => $query->createParameter('description'), |
|
318 | - 'principaluri' => $query->createParameter('principaluri'), |
|
319 | - 'synctoken' => $query->createParameter('synctoken'), |
|
320 | - ]) |
|
321 | - ->setParameters($values) |
|
322 | - ->execute(); |
|
323 | - |
|
324 | - return $query->getLastInsertId(); |
|
325 | - } |
|
326 | - |
|
327 | - /** |
|
328 | - * Deletes an entire addressbook and all its contents |
|
329 | - * |
|
330 | - * @param mixed $addressBookId |
|
331 | - * @return void |
|
332 | - */ |
|
333 | - function deleteAddressBook($addressBookId) { |
|
334 | - $query = $this->db->getQueryBuilder(); |
|
335 | - $query->delete('cards') |
|
336 | - ->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid'))) |
|
337 | - ->setParameter('addressbookid', $addressBookId) |
|
338 | - ->execute(); |
|
339 | - |
|
340 | - $query->delete('addressbookchanges') |
|
341 | - ->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid'))) |
|
342 | - ->setParameter('addressbookid', $addressBookId) |
|
343 | - ->execute(); |
|
344 | - |
|
345 | - $query->delete('addressbooks') |
|
346 | - ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
347 | - ->setParameter('id', $addressBookId) |
|
348 | - ->execute(); |
|
349 | - |
|
350 | - $this->sharingBackend->deleteAllShares($addressBookId); |
|
351 | - |
|
352 | - $query->delete($this->dbCardsPropertiesTable) |
|
353 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
354 | - ->execute(); |
|
355 | - |
|
356 | - } |
|
357 | - |
|
358 | - /** |
|
359 | - * Returns all cards for a specific addressbook id. |
|
360 | - * |
|
361 | - * This method should return the following properties for each card: |
|
362 | - * * carddata - raw vcard data |
|
363 | - * * uri - Some unique url |
|
364 | - * * lastmodified - A unix timestamp |
|
365 | - * |
|
366 | - * It's recommended to also return the following properties: |
|
367 | - * * etag - A unique etag. This must change every time the card changes. |
|
368 | - * * size - The size of the card in bytes. |
|
369 | - * |
|
370 | - * If these last two properties are provided, less time will be spent |
|
371 | - * calculating them. If they are specified, you can also ommit carddata. |
|
372 | - * This may speed up certain requests, especially with large cards. |
|
373 | - * |
|
374 | - * @param mixed $addressBookId |
|
375 | - * @return array |
|
376 | - */ |
|
377 | - function getCards($addressBookId) { |
|
378 | - $query = $this->db->getQueryBuilder(); |
|
379 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata']) |
|
380 | - ->from('cards') |
|
381 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
382 | - |
|
383 | - $cards = []; |
|
384 | - |
|
385 | - $result = $query->execute(); |
|
386 | - while($row = $result->fetch()) { |
|
387 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
388 | - $row['carddata'] = $this->readBlob($row['carddata']); |
|
389 | - $cards[] = $row; |
|
390 | - } |
|
391 | - $result->closeCursor(); |
|
392 | - |
|
393 | - return $cards; |
|
394 | - } |
|
395 | - |
|
396 | - /** |
|
397 | - * Returns a specfic card. |
|
398 | - * |
|
399 | - * The same set of properties must be returned as with getCards. The only |
|
400 | - * exception is that 'carddata' is absolutely required. |
|
401 | - * |
|
402 | - * If the card does not exist, you must return false. |
|
403 | - * |
|
404 | - * @param mixed $addressBookId |
|
405 | - * @param string $cardUri |
|
406 | - * @return array |
|
407 | - */ |
|
408 | - function getCard($addressBookId, $cardUri) { |
|
409 | - $query = $this->db->getQueryBuilder(); |
|
410 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata']) |
|
411 | - ->from('cards') |
|
412 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
413 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
414 | - ->setMaxResults(1); |
|
415 | - |
|
416 | - $result = $query->execute(); |
|
417 | - $row = $result->fetch(); |
|
418 | - if (!$row) { |
|
419 | - return false; |
|
420 | - } |
|
421 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
422 | - $row['carddata'] = $this->readBlob($row['carddata']); |
|
423 | - |
|
424 | - return $row; |
|
425 | - } |
|
426 | - |
|
427 | - /** |
|
428 | - * Returns a list of cards. |
|
429 | - * |
|
430 | - * This method should work identical to getCard, but instead return all the |
|
431 | - * cards in the list as an array. |
|
432 | - * |
|
433 | - * If the backend supports this, it may allow for some speed-ups. |
|
434 | - * |
|
435 | - * @param mixed $addressBookId |
|
436 | - * @param string[] $uris |
|
437 | - * @return array |
|
438 | - */ |
|
439 | - function getMultipleCards($addressBookId, array $uris) { |
|
440 | - $query = $this->db->getQueryBuilder(); |
|
441 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata']) |
|
442 | - ->from('cards') |
|
443 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
444 | - ->andWhere($query->expr()->in('uri', $query->createParameter('uri'))) |
|
445 | - ->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY); |
|
446 | - |
|
447 | - $cards = []; |
|
448 | - |
|
449 | - $result = $query->execute(); |
|
450 | - while($row = $result->fetch()) { |
|
451 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
452 | - $row['carddata'] = $this->readBlob($row['carddata']); |
|
453 | - $cards[] = $row; |
|
454 | - } |
|
455 | - $result->closeCursor(); |
|
456 | - |
|
457 | - return $cards; |
|
458 | - } |
|
459 | - |
|
460 | - /** |
|
461 | - * Creates a new card. |
|
462 | - * |
|
463 | - * The addressbook id will be passed as the first argument. This is the |
|
464 | - * same id as it is returned from the getAddressBooksForUser method. |
|
465 | - * |
|
466 | - * The cardUri is a base uri, and doesn't include the full path. The |
|
467 | - * cardData argument is the vcard body, and is passed as a string. |
|
468 | - * |
|
469 | - * It is possible to return an ETag from this method. This ETag is for the |
|
470 | - * newly created resource, and must be enclosed with double quotes (that |
|
471 | - * is, the string itself must contain the double quotes). |
|
472 | - * |
|
473 | - * You should only return the ETag if you store the carddata as-is. If a |
|
474 | - * subsequent GET request on the same card does not have the same body, |
|
475 | - * byte-by-byte and you did return an ETag here, clients tend to get |
|
476 | - * confused. |
|
477 | - * |
|
478 | - * If you don't return an ETag, you can just return null. |
|
479 | - * |
|
480 | - * @param mixed $addressBookId |
|
481 | - * @param string $cardUri |
|
482 | - * @param string $cardData |
|
483 | - * @return string |
|
484 | - */ |
|
485 | - function createCard($addressBookId, $cardUri, $cardData) { |
|
486 | - $etag = md5($cardData); |
|
487 | - |
|
488 | - $query = $this->db->getQueryBuilder(); |
|
489 | - $query->insert('cards') |
|
490 | - ->values([ |
|
491 | - 'carddata' => $query->createNamedParameter($cardData, IQueryBuilder::PARAM_LOB), |
|
492 | - 'uri' => $query->createNamedParameter($cardUri), |
|
493 | - 'lastmodified' => $query->createNamedParameter(time()), |
|
494 | - 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
495 | - 'size' => $query->createNamedParameter(strlen($cardData)), |
|
496 | - 'etag' => $query->createNamedParameter($etag), |
|
497 | - ]) |
|
498 | - ->execute(); |
|
499 | - |
|
500 | - $this->addChange($addressBookId, $cardUri, 1); |
|
501 | - $this->updateProperties($addressBookId, $cardUri, $cardData); |
|
502 | - |
|
503 | - if (!is_null($this->dispatcher)) { |
|
504 | - $this->dispatcher->dispatch('\OCA\DAV\CardDAV\CardDavBackend::createCard', |
|
505 | - new GenericEvent(null, [ |
|
506 | - 'addressBookId' => $addressBookId, |
|
507 | - 'cardUri' => $cardUri, |
|
508 | - 'cardData' => $cardData])); |
|
509 | - } |
|
510 | - |
|
511 | - return '"' . $etag . '"'; |
|
512 | - } |
|
513 | - |
|
514 | - /** |
|
515 | - * Updates a card. |
|
516 | - * |
|
517 | - * The addressbook id will be passed as the first argument. This is the |
|
518 | - * same id as it is returned from the getAddressBooksForUser method. |
|
519 | - * |
|
520 | - * The cardUri is a base uri, and doesn't include the full path. The |
|
521 | - * cardData argument is the vcard body, and is passed as a string. |
|
522 | - * |
|
523 | - * It is possible to return an ETag from this method. This ETag should |
|
524 | - * match that of the updated resource, and must be enclosed with double |
|
525 | - * quotes (that is: the string itself must contain the actual quotes). |
|
526 | - * |
|
527 | - * You should only return the ETag if you store the carddata as-is. If a |
|
528 | - * subsequent GET request on the same card does not have the same body, |
|
529 | - * byte-by-byte and you did return an ETag here, clients tend to get |
|
530 | - * confused. |
|
531 | - * |
|
532 | - * If you don't return an ETag, you can just return null. |
|
533 | - * |
|
534 | - * @param mixed $addressBookId |
|
535 | - * @param string $cardUri |
|
536 | - * @param string $cardData |
|
537 | - * @return string |
|
538 | - */ |
|
539 | - function updateCard($addressBookId, $cardUri, $cardData) { |
|
540 | - |
|
541 | - $etag = md5($cardData); |
|
542 | - $query = $this->db->getQueryBuilder(); |
|
543 | - $query->update('cards') |
|
544 | - ->set('carddata', $query->createNamedParameter($cardData, IQueryBuilder::PARAM_LOB)) |
|
545 | - ->set('lastmodified', $query->createNamedParameter(time())) |
|
546 | - ->set('size', $query->createNamedParameter(strlen($cardData))) |
|
547 | - ->set('etag', $query->createNamedParameter($etag)) |
|
548 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
549 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
550 | - ->execute(); |
|
551 | - |
|
552 | - $this->addChange($addressBookId, $cardUri, 2); |
|
553 | - $this->updateProperties($addressBookId, $cardUri, $cardData); |
|
554 | - |
|
555 | - if (!is_null($this->dispatcher)) { |
|
556 | - $this->dispatcher->dispatch('\OCA\DAV\CardDAV\CardDavBackend::updateCard', |
|
557 | - new GenericEvent(null, [ |
|
558 | - 'addressBookId' => $addressBookId, |
|
559 | - 'cardUri' => $cardUri, |
|
560 | - 'cardData' => $cardData])); |
|
561 | - } |
|
562 | - |
|
563 | - return '"' . $etag . '"'; |
|
564 | - } |
|
565 | - |
|
566 | - /** |
|
567 | - * Deletes a card |
|
568 | - * |
|
569 | - * @param mixed $addressBookId |
|
570 | - * @param string $cardUri |
|
571 | - * @return bool |
|
572 | - */ |
|
573 | - function deleteCard($addressBookId, $cardUri) { |
|
574 | - try { |
|
575 | - $cardId = $this->getCardId($addressBookId, $cardUri); |
|
576 | - } catch (\InvalidArgumentException $e) { |
|
577 | - $cardId = null; |
|
578 | - } |
|
579 | - $query = $this->db->getQueryBuilder(); |
|
580 | - $ret = $query->delete('cards') |
|
581 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
582 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
583 | - ->execute(); |
|
584 | - |
|
585 | - $this->addChange($addressBookId, $cardUri, 3); |
|
586 | - |
|
587 | - if (!is_null($this->dispatcher)) { |
|
588 | - $this->dispatcher->dispatch('\OCA\DAV\CardDAV\CardDavBackend::deleteCard', |
|
589 | - new GenericEvent(null, [ |
|
590 | - 'addressBookId' => $addressBookId, |
|
591 | - 'cardUri' => $cardUri])); |
|
592 | - } |
|
593 | - |
|
594 | - if ($ret === 1) { |
|
595 | - if ($cardId !== null) { |
|
596 | - $this->purgeProperties($addressBookId, $cardId); |
|
597 | - } |
|
598 | - return true; |
|
599 | - } |
|
600 | - |
|
601 | - return false; |
|
602 | - } |
|
603 | - |
|
604 | - /** |
|
605 | - * The getChanges method returns all the changes that have happened, since |
|
606 | - * the specified syncToken in the specified address book. |
|
607 | - * |
|
608 | - * This function should return an array, such as the following: |
|
609 | - * |
|
610 | - * [ |
|
611 | - * 'syncToken' => 'The current synctoken', |
|
612 | - * 'added' => [ |
|
613 | - * 'new.txt', |
|
614 | - * ], |
|
615 | - * 'modified' => [ |
|
616 | - * 'modified.txt', |
|
617 | - * ], |
|
618 | - * 'deleted' => [ |
|
619 | - * 'foo.php.bak', |
|
620 | - * 'old.txt' |
|
621 | - * ] |
|
622 | - * ]; |
|
623 | - * |
|
624 | - * The returned syncToken property should reflect the *current* syncToken |
|
625 | - * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
|
626 | - * property. This is needed here too, to ensure the operation is atomic. |
|
627 | - * |
|
628 | - * If the $syncToken argument is specified as null, this is an initial |
|
629 | - * sync, and all members should be reported. |
|
630 | - * |
|
631 | - * The modified property is an array of nodenames that have changed since |
|
632 | - * the last token. |
|
633 | - * |
|
634 | - * The deleted property is an array with nodenames, that have been deleted |
|
635 | - * from collection. |
|
636 | - * |
|
637 | - * The $syncLevel argument is basically the 'depth' of the report. If it's |
|
638 | - * 1, you only have to report changes that happened only directly in |
|
639 | - * immediate descendants. If it's 2, it should also include changes from |
|
640 | - * the nodes below the child collections. (grandchildren) |
|
641 | - * |
|
642 | - * The $limit argument allows a client to specify how many results should |
|
643 | - * be returned at most. If the limit is not specified, it should be treated |
|
644 | - * as infinite. |
|
645 | - * |
|
646 | - * If the limit (infinite or not) is higher than you're willing to return, |
|
647 | - * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
|
648 | - * |
|
649 | - * If the syncToken is expired (due to data cleanup) or unknown, you must |
|
650 | - * return null. |
|
651 | - * |
|
652 | - * The limit is 'suggestive'. You are free to ignore it. |
|
653 | - * |
|
654 | - * @param string $addressBookId |
|
655 | - * @param string $syncToken |
|
656 | - * @param int $syncLevel |
|
657 | - * @param int $limit |
|
658 | - * @return array |
|
659 | - */ |
|
660 | - function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel, $limit = null) { |
|
661 | - // Current synctoken |
|
662 | - $stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*addressbooks` WHERE `id` = ?'); |
|
663 | - $stmt->execute([ $addressBookId ]); |
|
664 | - $currentToken = $stmt->fetchColumn(0); |
|
665 | - |
|
666 | - if (is_null($currentToken)) return null; |
|
667 | - |
|
668 | - $result = [ |
|
669 | - 'syncToken' => $currentToken, |
|
670 | - 'added' => [], |
|
671 | - 'modified' => [], |
|
672 | - 'deleted' => [], |
|
673 | - ]; |
|
674 | - |
|
675 | - if ($syncToken) { |
|
676 | - |
|
677 | - $query = "SELECT `uri`, `operation` FROM `*PREFIX*addressbookchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `addressbookid` = ? ORDER BY `synctoken`"; |
|
678 | - if ($limit>0) { |
|
679 | - $query .= " `LIMIT` " . (int)$limit; |
|
680 | - } |
|
681 | - |
|
682 | - // Fetching all changes |
|
683 | - $stmt = $this->db->prepare($query); |
|
684 | - $stmt->execute([$syncToken, $currentToken, $addressBookId]); |
|
685 | - |
|
686 | - $changes = []; |
|
687 | - |
|
688 | - // This loop ensures that any duplicates are overwritten, only the |
|
689 | - // last change on a node is relevant. |
|
690 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
691 | - |
|
692 | - $changes[$row['uri']] = $row['operation']; |
|
693 | - |
|
694 | - } |
|
695 | - |
|
696 | - foreach($changes as $uri => $operation) { |
|
697 | - |
|
698 | - switch($operation) { |
|
699 | - case 1: |
|
700 | - $result['added'][] = $uri; |
|
701 | - break; |
|
702 | - case 2: |
|
703 | - $result['modified'][] = $uri; |
|
704 | - break; |
|
705 | - case 3: |
|
706 | - $result['deleted'][] = $uri; |
|
707 | - break; |
|
708 | - } |
|
709 | - |
|
710 | - } |
|
711 | - } else { |
|
712 | - // No synctoken supplied, this is the initial sync. |
|
713 | - $query = "SELECT `uri` FROM `*PREFIX*cards` WHERE `addressbookid` = ?"; |
|
714 | - $stmt = $this->db->prepare($query); |
|
715 | - $stmt->execute([$addressBookId]); |
|
716 | - |
|
717 | - $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
718 | - } |
|
719 | - return $result; |
|
720 | - } |
|
721 | - |
|
722 | - /** |
|
723 | - * Adds a change record to the addressbookchanges table. |
|
724 | - * |
|
725 | - * @param mixed $addressBookId |
|
726 | - * @param string $objectUri |
|
727 | - * @param int $operation 1 = add, 2 = modify, 3 = delete |
|
728 | - * @return void |
|
729 | - */ |
|
730 | - protected function addChange($addressBookId, $objectUri, $operation) { |
|
731 | - $sql = 'INSERT INTO `*PREFIX*addressbookchanges`(`uri`, `synctoken`, `addressbookid`, `operation`) SELECT ?, `synctoken`, ?, ? FROM `*PREFIX*addressbooks` WHERE `id` = ?'; |
|
732 | - $stmt = $this->db->prepare($sql); |
|
733 | - $stmt->execute([ |
|
734 | - $objectUri, |
|
735 | - $addressBookId, |
|
736 | - $operation, |
|
737 | - $addressBookId |
|
738 | - ]); |
|
739 | - $stmt = $this->db->prepare('UPDATE `*PREFIX*addressbooks` SET `synctoken` = `synctoken` + 1 WHERE `id` = ?'); |
|
740 | - $stmt->execute([ |
|
741 | - $addressBookId |
|
742 | - ]); |
|
743 | - } |
|
744 | - |
|
745 | - private function readBlob($cardData) { |
|
746 | - if (is_resource($cardData)) { |
|
747 | - return stream_get_contents($cardData); |
|
748 | - } |
|
749 | - |
|
750 | - return $cardData; |
|
751 | - } |
|
752 | - |
|
753 | - /** |
|
754 | - * @param IShareable $shareable |
|
755 | - * @param string[] $add |
|
756 | - * @param string[] $remove |
|
757 | - */ |
|
758 | - public function updateShares(IShareable $shareable, $add, $remove) { |
|
759 | - $this->sharingBackend->updateShares($shareable, $add, $remove); |
|
760 | - } |
|
761 | - |
|
762 | - /** |
|
763 | - * search contact |
|
764 | - * |
|
765 | - * @param int $addressBookId |
|
766 | - * @param string $pattern which should match within the $searchProperties |
|
767 | - * @param array $searchProperties defines the properties within the query pattern should match |
|
768 | - * @return array an array of contacts which are arrays of key-value-pairs |
|
769 | - */ |
|
770 | - public function search($addressBookId, $pattern, $searchProperties) { |
|
771 | - $query = $this->db->getQueryBuilder(); |
|
772 | - $query2 = $this->db->getQueryBuilder(); |
|
773 | - $query2->selectDistinct('cp.cardid')->from($this->dbCardsPropertiesTable, 'cp'); |
|
774 | - foreach ($searchProperties as $property) { |
|
775 | - $query2->orWhere( |
|
776 | - $query2->expr()->andX( |
|
777 | - $query2->expr()->eq('cp.name', $query->createNamedParameter($property)), |
|
778 | - $query2->expr()->ilike('cp.value', $query->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%')) |
|
779 | - ) |
|
780 | - ); |
|
781 | - } |
|
782 | - $query2->andWhere($query2->expr()->eq('cp.addressbookid', $query->createNamedParameter($addressBookId))); |
|
783 | - |
|
784 | - $query->select('c.carddata')->from($this->dbCardsTable, 'c') |
|
785 | - ->where($query->expr()->in('c.id', $query->createFunction($query2->getSQL()))); |
|
786 | - |
|
787 | - $result = $query->execute(); |
|
788 | - $cards = $result->fetchAll(); |
|
789 | - |
|
790 | - $result->closeCursor(); |
|
791 | - |
|
792 | - return array_map(function($array) {return $this->readBlob($array['carddata']);}, $cards); |
|
793 | - |
|
794 | - } |
|
795 | - |
|
796 | - /** |
|
797 | - * @param int $bookId |
|
798 | - * @param string $name |
|
799 | - * @return array |
|
800 | - */ |
|
801 | - public function collectCardProperties($bookId, $name) { |
|
802 | - $query = $this->db->getQueryBuilder(); |
|
803 | - $result = $query->selectDistinct('value') |
|
804 | - ->from($this->dbCardsPropertiesTable) |
|
805 | - ->where($query->expr()->eq('name', $query->createNamedParameter($name))) |
|
806 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($bookId))) |
|
807 | - ->execute(); |
|
808 | - |
|
809 | - $all = $result->fetchAll(PDO::FETCH_COLUMN); |
|
810 | - $result->closeCursor(); |
|
811 | - |
|
812 | - return $all; |
|
813 | - } |
|
814 | - |
|
815 | - /** |
|
816 | - * get URI from a given contact |
|
817 | - * |
|
818 | - * @param int $id |
|
819 | - * @return string |
|
820 | - */ |
|
821 | - public function getCardUri($id) { |
|
822 | - $query = $this->db->getQueryBuilder(); |
|
823 | - $query->select('uri')->from($this->dbCardsTable) |
|
824 | - ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
825 | - ->setParameter('id', $id); |
|
826 | - |
|
827 | - $result = $query->execute(); |
|
828 | - $uri = $result->fetch(); |
|
829 | - $result->closeCursor(); |
|
830 | - |
|
831 | - if (!isset($uri['uri'])) { |
|
832 | - throw new \InvalidArgumentException('Card does not exists: ' . $id); |
|
833 | - } |
|
834 | - |
|
835 | - return $uri['uri']; |
|
836 | - } |
|
837 | - |
|
838 | - /** |
|
839 | - * return contact with the given URI |
|
840 | - * |
|
841 | - * @param int $addressBookId |
|
842 | - * @param string $uri |
|
843 | - * @returns array |
|
844 | - */ |
|
845 | - public function getContact($addressBookId, $uri) { |
|
846 | - $result = []; |
|
847 | - $query = $this->db->getQueryBuilder(); |
|
848 | - $query->select('*')->from($this->dbCardsTable) |
|
849 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
850 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
851 | - $queryResult = $query->execute(); |
|
852 | - $contact = $queryResult->fetch(); |
|
853 | - $queryResult->closeCursor(); |
|
854 | - |
|
855 | - if (is_array($contact)) { |
|
856 | - $result = $contact; |
|
857 | - } |
|
858 | - |
|
859 | - return $result; |
|
860 | - } |
|
861 | - |
|
862 | - /** |
|
863 | - * Returns the list of people whom this address book is shared with. |
|
864 | - * |
|
865 | - * Every element in this array should have the following properties: |
|
866 | - * * href - Often a mailto: address |
|
867 | - * * commonName - Optional, for example a first + last name |
|
868 | - * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
|
869 | - * * readOnly - boolean |
|
870 | - * * summary - Optional, a description for the share |
|
871 | - * |
|
872 | - * @return array |
|
873 | - */ |
|
874 | - public function getShares($addressBookId) { |
|
875 | - return $this->sharingBackend->getShares($addressBookId); |
|
876 | - } |
|
877 | - |
|
878 | - /** |
|
879 | - * update properties table |
|
880 | - * |
|
881 | - * @param int $addressBookId |
|
882 | - * @param string $cardUri |
|
883 | - * @param string $vCardSerialized |
|
884 | - */ |
|
885 | - protected function updateProperties($addressBookId, $cardUri, $vCardSerialized) { |
|
886 | - $cardId = $this->getCardId($addressBookId, $cardUri); |
|
887 | - $vCard = $this->readCard($vCardSerialized); |
|
888 | - |
|
889 | - $this->purgeProperties($addressBookId, $cardId); |
|
890 | - |
|
891 | - $query = $this->db->getQueryBuilder(); |
|
892 | - $query->insert($this->dbCardsPropertiesTable) |
|
893 | - ->values( |
|
894 | - [ |
|
895 | - 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
896 | - 'cardid' => $query->createNamedParameter($cardId), |
|
897 | - 'name' => $query->createParameter('name'), |
|
898 | - 'value' => $query->createParameter('value'), |
|
899 | - 'preferred' => $query->createParameter('preferred') |
|
900 | - ] |
|
901 | - ); |
|
902 | - |
|
903 | - foreach ($vCard->children as $property) { |
|
904 | - if(!in_array($property->name, self::$indexProperties)) { |
|
905 | - continue; |
|
906 | - } |
|
907 | - $preferred = 0; |
|
908 | - foreach($property->parameters as $parameter) { |
|
909 | - if ($parameter->name == 'TYPE' && strtoupper($parameter->getValue()) == 'PREF') { |
|
910 | - $preferred = 1; |
|
911 | - break; |
|
912 | - } |
|
913 | - } |
|
914 | - $query->setParameter('name', $property->name); |
|
915 | - $query->setParameter('value', substr($property->getValue(), 0, 254)); |
|
916 | - $query->setParameter('preferred', $preferred); |
|
917 | - $query->execute(); |
|
918 | - } |
|
919 | - } |
|
920 | - |
|
921 | - /** |
|
922 | - * read vCard data into a vCard object |
|
923 | - * |
|
924 | - * @param string $cardData |
|
925 | - * @return VCard |
|
926 | - */ |
|
927 | - protected function readCard($cardData) { |
|
928 | - return Reader::read($cardData); |
|
929 | - } |
|
930 | - |
|
931 | - /** |
|
932 | - * delete all properties from a given card |
|
933 | - * |
|
934 | - * @param int $addressBookId |
|
935 | - * @param int $cardId |
|
936 | - */ |
|
937 | - protected function purgeProperties($addressBookId, $cardId) { |
|
938 | - $query = $this->db->getQueryBuilder(); |
|
939 | - $query->delete($this->dbCardsPropertiesTable) |
|
940 | - ->where($query->expr()->eq('cardid', $query->createNamedParameter($cardId))) |
|
941 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
942 | - $query->execute(); |
|
943 | - } |
|
944 | - |
|
945 | - /** |
|
946 | - * get ID from a given contact |
|
947 | - * |
|
948 | - * @param int $addressBookId |
|
949 | - * @param string $uri |
|
950 | - * @return int |
|
951 | - */ |
|
952 | - protected function getCardId($addressBookId, $uri) { |
|
953 | - $query = $this->db->getQueryBuilder(); |
|
954 | - $query->select('id')->from($this->dbCardsTable) |
|
955 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
956 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
957 | - |
|
958 | - $result = $query->execute(); |
|
959 | - $cardIds = $result->fetch(); |
|
960 | - $result->closeCursor(); |
|
961 | - |
|
962 | - if (!isset($cardIds['id'])) { |
|
963 | - throw new \InvalidArgumentException('Card does not exists: ' . $uri); |
|
964 | - } |
|
965 | - |
|
966 | - return (int)$cardIds['id']; |
|
967 | - } |
|
968 | - |
|
969 | - /** |
|
970 | - * For shared address books the sharee is set in the ACL of the address book |
|
971 | - * @param $addressBookId |
|
972 | - * @param $acl |
|
973 | - * @return array |
|
974 | - */ |
|
975 | - public function applyShareAcl($addressBookId, $acl) { |
|
976 | - return $this->sharingBackend->applyShareAcl($addressBookId, $acl); |
|
977 | - } |
|
978 | - |
|
979 | - private function convertPrincipal($principalUri, $toV2) { |
|
980 | - if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
|
981 | - list(, $name) = URLUtil::splitPath($principalUri); |
|
982 | - if ($toV2 === true) { |
|
983 | - return "principals/users/$name"; |
|
984 | - } |
|
985 | - return "principals/$name"; |
|
986 | - } |
|
987 | - return $principalUri; |
|
988 | - } |
|
47 | + /** @var Principal */ |
|
48 | + private $principalBackend; |
|
49 | + |
|
50 | + /** @var string */ |
|
51 | + private $dbCardsTable = 'cards'; |
|
52 | + |
|
53 | + /** @var string */ |
|
54 | + private $dbCardsPropertiesTable = 'cards_properties'; |
|
55 | + |
|
56 | + /** @var IDBConnection */ |
|
57 | + private $db; |
|
58 | + |
|
59 | + /** @var Backend */ |
|
60 | + private $sharingBackend; |
|
61 | + |
|
62 | + /** @var array properties to index */ |
|
63 | + public static $indexProperties = array( |
|
64 | + 'BDAY', 'UID', 'N', 'FN', 'TITLE', 'ROLE', 'NOTE', 'NICKNAME', |
|
65 | + 'ORG', 'CATEGORIES', 'EMAIL', 'TEL', 'IMPP', 'ADR', 'URL', 'GEO', 'CLOUD'); |
|
66 | + |
|
67 | + /** @var EventDispatcherInterface */ |
|
68 | + private $dispatcher; |
|
69 | + |
|
70 | + /** |
|
71 | + * CardDavBackend constructor. |
|
72 | + * |
|
73 | + * @param IDBConnection $db |
|
74 | + * @param Principal $principalBackend |
|
75 | + * @param EventDispatcherInterface $dispatcher |
|
76 | + */ |
|
77 | + public function __construct(IDBConnection $db, |
|
78 | + Principal $principalBackend, |
|
79 | + EventDispatcherInterface $dispatcher = null) { |
|
80 | + $this->db = $db; |
|
81 | + $this->principalBackend = $principalBackend; |
|
82 | + $this->dispatcher = $dispatcher; |
|
83 | + $this->sharingBackend = new Backend($this->db, $principalBackend, 'addressbook'); |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Returns the list of address books for a specific user. |
|
88 | + * |
|
89 | + * Every addressbook should have the following properties: |
|
90 | + * id - an arbitrary unique id |
|
91 | + * uri - the 'basename' part of the url |
|
92 | + * principaluri - Same as the passed parameter |
|
93 | + * |
|
94 | + * Any additional clark-notation property may be passed besides this. Some |
|
95 | + * common ones are : |
|
96 | + * {DAV:}displayname |
|
97 | + * {urn:ietf:params:xml:ns:carddav}addressbook-description |
|
98 | + * {http://calendarserver.org/ns/}getctag |
|
99 | + * |
|
100 | + * @param string $principalUri |
|
101 | + * @return array |
|
102 | + */ |
|
103 | + function getAddressBooksForUser($principalUri) { |
|
104 | + $principalUriOriginal = $principalUri; |
|
105 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
106 | + $query = $this->db->getQueryBuilder(); |
|
107 | + $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
108 | + ->from('addressbooks') |
|
109 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
110 | + |
|
111 | + $addressBooks = []; |
|
112 | + |
|
113 | + $result = $query->execute(); |
|
114 | + while($row = $result->fetch()) { |
|
115 | + $addressBooks[$row['id']] = [ |
|
116 | + 'id' => $row['id'], |
|
117 | + 'uri' => $row['uri'], |
|
118 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
|
119 | + '{DAV:}displayname' => $row['displayname'], |
|
120 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
121 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
122 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
123 | + ]; |
|
124 | + } |
|
125 | + $result->closeCursor(); |
|
126 | + |
|
127 | + // query for shared calendars |
|
128 | + $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); |
|
129 | + $principals[]= $principalUri; |
|
130 | + |
|
131 | + $query = $this->db->getQueryBuilder(); |
|
132 | + $result = $query->select(['a.id', 'a.uri', 'a.displayname', 'a.principaluri', 'a.description', 'a.synctoken', 's.access']) |
|
133 | + ->from('dav_shares', 's') |
|
134 | + ->join('s', 'addressbooks', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
135 | + ->where($query->expr()->in('s.principaluri', $query->createParameter('principaluri'))) |
|
136 | + ->andWhere($query->expr()->eq('s.type', $query->createParameter('type'))) |
|
137 | + ->setParameter('type', 'addressbook') |
|
138 | + ->setParameter('principaluri', $principals, IQueryBuilder::PARAM_STR_ARRAY) |
|
139 | + ->execute(); |
|
140 | + |
|
141 | + while($row = $result->fetch()) { |
|
142 | + list(, $name) = URLUtil::splitPath($row['principaluri']); |
|
143 | + $uri = $row['uri'] . '_shared_by_' . $name; |
|
144 | + $displayName = $row['displayname'] . "($name)"; |
|
145 | + if (!isset($addressBooks[$row['id']])) { |
|
146 | + $addressBooks[$row['id']] = [ |
|
147 | + 'id' => $row['id'], |
|
148 | + 'uri' => $uri, |
|
149 | + 'principaluri' => $principalUri, |
|
150 | + '{DAV:}displayname' => $displayName, |
|
151 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
152 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
153 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
154 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $row['principaluri'], |
|
155 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ, |
|
156 | + ]; |
|
157 | + } |
|
158 | + } |
|
159 | + $result->closeCursor(); |
|
160 | + |
|
161 | + return array_values($addressBooks); |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * @param int $addressBookId |
|
166 | + */ |
|
167 | + public function getAddressBookById($addressBookId) { |
|
168 | + $query = $this->db->getQueryBuilder(); |
|
169 | + $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
170 | + ->from('addressbooks') |
|
171 | + ->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) |
|
172 | + ->execute(); |
|
173 | + |
|
174 | + $row = $result->fetch(); |
|
175 | + $result->closeCursor(); |
|
176 | + if ($row === false) { |
|
177 | + return null; |
|
178 | + } |
|
179 | + |
|
180 | + return [ |
|
181 | + 'id' => $row['id'], |
|
182 | + 'uri' => $row['uri'], |
|
183 | + 'principaluri' => $row['principaluri'], |
|
184 | + '{DAV:}displayname' => $row['displayname'], |
|
185 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
186 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
187 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
188 | + ]; |
|
189 | + } |
|
190 | + |
|
191 | + /** |
|
192 | + * @param $addressBookUri |
|
193 | + * @return array|null |
|
194 | + */ |
|
195 | + public function getAddressBooksByUri($principal, $addressBookUri) { |
|
196 | + $query = $this->db->getQueryBuilder(); |
|
197 | + $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
198 | + ->from('addressbooks') |
|
199 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($addressBookUri))) |
|
200 | + ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal))) |
|
201 | + ->setMaxResults(1) |
|
202 | + ->execute(); |
|
203 | + |
|
204 | + $row = $result->fetch(); |
|
205 | + $result->closeCursor(); |
|
206 | + if ($row === false) { |
|
207 | + return null; |
|
208 | + } |
|
209 | + |
|
210 | + return [ |
|
211 | + 'id' => $row['id'], |
|
212 | + 'uri' => $row['uri'], |
|
213 | + 'principaluri' => $row['principaluri'], |
|
214 | + '{DAV:}displayname' => $row['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 | + ]; |
|
219 | + } |
|
220 | + |
|
221 | + /** |
|
222 | + * Updates properties for an address book. |
|
223 | + * |
|
224 | + * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
225 | + * To do the actual updates, you must tell this object which properties |
|
226 | + * you're going to process with the handle() method. |
|
227 | + * |
|
228 | + * Calling the handle method is like telling the PropPatch object "I |
|
229 | + * promise I can handle updating this property". |
|
230 | + * |
|
231 | + * Read the PropPatch documentation for more info and examples. |
|
232 | + * |
|
233 | + * @param string $addressBookId |
|
234 | + * @param \Sabre\DAV\PropPatch $propPatch |
|
235 | + * @return void |
|
236 | + */ |
|
237 | + function updateAddressBook($addressBookId, \Sabre\DAV\PropPatch $propPatch) { |
|
238 | + $supportedProperties = [ |
|
239 | + '{DAV:}displayname', |
|
240 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description', |
|
241 | + ]; |
|
242 | + |
|
243 | + $propPatch->handle($supportedProperties, function($mutations) use ($addressBookId) { |
|
244 | + |
|
245 | + $updates = []; |
|
246 | + foreach($mutations as $property=>$newValue) { |
|
247 | + |
|
248 | + switch($property) { |
|
249 | + case '{DAV:}displayname' : |
|
250 | + $updates['displayname'] = $newValue; |
|
251 | + break; |
|
252 | + case '{' . Plugin::NS_CARDDAV . '}addressbook-description' : |
|
253 | + $updates['description'] = $newValue; |
|
254 | + break; |
|
255 | + } |
|
256 | + } |
|
257 | + $query = $this->db->getQueryBuilder(); |
|
258 | + $query->update('addressbooks'); |
|
259 | + |
|
260 | + foreach($updates as $key=>$value) { |
|
261 | + $query->set($key, $query->createNamedParameter($value)); |
|
262 | + } |
|
263 | + $query->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) |
|
264 | + ->execute(); |
|
265 | + |
|
266 | + $this->addChange($addressBookId, "", 2); |
|
267 | + |
|
268 | + return true; |
|
269 | + |
|
270 | + }); |
|
271 | + } |
|
272 | + |
|
273 | + /** |
|
274 | + * Creates a new address book |
|
275 | + * |
|
276 | + * @param string $principalUri |
|
277 | + * @param string $url Just the 'basename' of the url. |
|
278 | + * @param array $properties |
|
279 | + * @return int |
|
280 | + * @throws BadRequest |
|
281 | + */ |
|
282 | + function createAddressBook($principalUri, $url, array $properties) { |
|
283 | + $values = [ |
|
284 | + 'displayname' => null, |
|
285 | + 'description' => null, |
|
286 | + 'principaluri' => $principalUri, |
|
287 | + 'uri' => $url, |
|
288 | + 'synctoken' => 1 |
|
289 | + ]; |
|
290 | + |
|
291 | + foreach($properties as $property=>$newValue) { |
|
292 | + |
|
293 | + switch($property) { |
|
294 | + case '{DAV:}displayname' : |
|
295 | + $values['displayname'] = $newValue; |
|
296 | + break; |
|
297 | + case '{' . Plugin::NS_CARDDAV . '}addressbook-description' : |
|
298 | + $values['description'] = $newValue; |
|
299 | + break; |
|
300 | + default : |
|
301 | + throw new BadRequest('Unknown property: ' . $property); |
|
302 | + } |
|
303 | + |
|
304 | + } |
|
305 | + |
|
306 | + // Fallback to make sure the displayname is set. Some clients may refuse |
|
307 | + // to work with addressbooks not having a displayname. |
|
308 | + if(is_null($values['displayname'])) { |
|
309 | + $values['displayname'] = $url; |
|
310 | + } |
|
311 | + |
|
312 | + $query = $this->db->getQueryBuilder(); |
|
313 | + $query->insert('addressbooks') |
|
314 | + ->values([ |
|
315 | + 'uri' => $query->createParameter('uri'), |
|
316 | + 'displayname' => $query->createParameter('displayname'), |
|
317 | + 'description' => $query->createParameter('description'), |
|
318 | + 'principaluri' => $query->createParameter('principaluri'), |
|
319 | + 'synctoken' => $query->createParameter('synctoken'), |
|
320 | + ]) |
|
321 | + ->setParameters($values) |
|
322 | + ->execute(); |
|
323 | + |
|
324 | + return $query->getLastInsertId(); |
|
325 | + } |
|
326 | + |
|
327 | + /** |
|
328 | + * Deletes an entire addressbook and all its contents |
|
329 | + * |
|
330 | + * @param mixed $addressBookId |
|
331 | + * @return void |
|
332 | + */ |
|
333 | + function deleteAddressBook($addressBookId) { |
|
334 | + $query = $this->db->getQueryBuilder(); |
|
335 | + $query->delete('cards') |
|
336 | + ->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid'))) |
|
337 | + ->setParameter('addressbookid', $addressBookId) |
|
338 | + ->execute(); |
|
339 | + |
|
340 | + $query->delete('addressbookchanges') |
|
341 | + ->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid'))) |
|
342 | + ->setParameter('addressbookid', $addressBookId) |
|
343 | + ->execute(); |
|
344 | + |
|
345 | + $query->delete('addressbooks') |
|
346 | + ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
347 | + ->setParameter('id', $addressBookId) |
|
348 | + ->execute(); |
|
349 | + |
|
350 | + $this->sharingBackend->deleteAllShares($addressBookId); |
|
351 | + |
|
352 | + $query->delete($this->dbCardsPropertiesTable) |
|
353 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
354 | + ->execute(); |
|
355 | + |
|
356 | + } |
|
357 | + |
|
358 | + /** |
|
359 | + * Returns all cards for a specific addressbook id. |
|
360 | + * |
|
361 | + * This method should return the following properties for each card: |
|
362 | + * * carddata - raw vcard data |
|
363 | + * * uri - Some unique url |
|
364 | + * * lastmodified - A unix timestamp |
|
365 | + * |
|
366 | + * It's recommended to also return the following properties: |
|
367 | + * * etag - A unique etag. This must change every time the card changes. |
|
368 | + * * size - The size of the card in bytes. |
|
369 | + * |
|
370 | + * If these last two properties are provided, less time will be spent |
|
371 | + * calculating them. If they are specified, you can also ommit carddata. |
|
372 | + * This may speed up certain requests, especially with large cards. |
|
373 | + * |
|
374 | + * @param mixed $addressBookId |
|
375 | + * @return array |
|
376 | + */ |
|
377 | + function getCards($addressBookId) { |
|
378 | + $query = $this->db->getQueryBuilder(); |
|
379 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata']) |
|
380 | + ->from('cards') |
|
381 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
382 | + |
|
383 | + $cards = []; |
|
384 | + |
|
385 | + $result = $query->execute(); |
|
386 | + while($row = $result->fetch()) { |
|
387 | + $row['etag'] = '"' . $row['etag'] . '"'; |
|
388 | + $row['carddata'] = $this->readBlob($row['carddata']); |
|
389 | + $cards[] = $row; |
|
390 | + } |
|
391 | + $result->closeCursor(); |
|
392 | + |
|
393 | + return $cards; |
|
394 | + } |
|
395 | + |
|
396 | + /** |
|
397 | + * Returns a specfic card. |
|
398 | + * |
|
399 | + * The same set of properties must be returned as with getCards. The only |
|
400 | + * exception is that 'carddata' is absolutely required. |
|
401 | + * |
|
402 | + * If the card does not exist, you must return false. |
|
403 | + * |
|
404 | + * @param mixed $addressBookId |
|
405 | + * @param string $cardUri |
|
406 | + * @return array |
|
407 | + */ |
|
408 | + function getCard($addressBookId, $cardUri) { |
|
409 | + $query = $this->db->getQueryBuilder(); |
|
410 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata']) |
|
411 | + ->from('cards') |
|
412 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
413 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
414 | + ->setMaxResults(1); |
|
415 | + |
|
416 | + $result = $query->execute(); |
|
417 | + $row = $result->fetch(); |
|
418 | + if (!$row) { |
|
419 | + return false; |
|
420 | + } |
|
421 | + $row['etag'] = '"' . $row['etag'] . '"'; |
|
422 | + $row['carddata'] = $this->readBlob($row['carddata']); |
|
423 | + |
|
424 | + return $row; |
|
425 | + } |
|
426 | + |
|
427 | + /** |
|
428 | + * Returns a list of cards. |
|
429 | + * |
|
430 | + * This method should work identical to getCard, but instead return all the |
|
431 | + * cards in the list as an array. |
|
432 | + * |
|
433 | + * If the backend supports this, it may allow for some speed-ups. |
|
434 | + * |
|
435 | + * @param mixed $addressBookId |
|
436 | + * @param string[] $uris |
|
437 | + * @return array |
|
438 | + */ |
|
439 | + function getMultipleCards($addressBookId, array $uris) { |
|
440 | + $query = $this->db->getQueryBuilder(); |
|
441 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata']) |
|
442 | + ->from('cards') |
|
443 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
444 | + ->andWhere($query->expr()->in('uri', $query->createParameter('uri'))) |
|
445 | + ->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY); |
|
446 | + |
|
447 | + $cards = []; |
|
448 | + |
|
449 | + $result = $query->execute(); |
|
450 | + while($row = $result->fetch()) { |
|
451 | + $row['etag'] = '"' . $row['etag'] . '"'; |
|
452 | + $row['carddata'] = $this->readBlob($row['carddata']); |
|
453 | + $cards[] = $row; |
|
454 | + } |
|
455 | + $result->closeCursor(); |
|
456 | + |
|
457 | + return $cards; |
|
458 | + } |
|
459 | + |
|
460 | + /** |
|
461 | + * Creates a new card. |
|
462 | + * |
|
463 | + * The addressbook id will be passed as the first argument. This is the |
|
464 | + * same id as it is returned from the getAddressBooksForUser method. |
|
465 | + * |
|
466 | + * The cardUri is a base uri, and doesn't include the full path. The |
|
467 | + * cardData argument is the vcard body, and is passed as a string. |
|
468 | + * |
|
469 | + * It is possible to return an ETag from this method. This ETag is for the |
|
470 | + * newly created resource, and must be enclosed with double quotes (that |
|
471 | + * is, the string itself must contain the double quotes). |
|
472 | + * |
|
473 | + * You should only return the ETag if you store the carddata as-is. If a |
|
474 | + * subsequent GET request on the same card does not have the same body, |
|
475 | + * byte-by-byte and you did return an ETag here, clients tend to get |
|
476 | + * confused. |
|
477 | + * |
|
478 | + * If you don't return an ETag, you can just return null. |
|
479 | + * |
|
480 | + * @param mixed $addressBookId |
|
481 | + * @param string $cardUri |
|
482 | + * @param string $cardData |
|
483 | + * @return string |
|
484 | + */ |
|
485 | + function createCard($addressBookId, $cardUri, $cardData) { |
|
486 | + $etag = md5($cardData); |
|
487 | + |
|
488 | + $query = $this->db->getQueryBuilder(); |
|
489 | + $query->insert('cards') |
|
490 | + ->values([ |
|
491 | + 'carddata' => $query->createNamedParameter($cardData, IQueryBuilder::PARAM_LOB), |
|
492 | + 'uri' => $query->createNamedParameter($cardUri), |
|
493 | + 'lastmodified' => $query->createNamedParameter(time()), |
|
494 | + 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
495 | + 'size' => $query->createNamedParameter(strlen($cardData)), |
|
496 | + 'etag' => $query->createNamedParameter($etag), |
|
497 | + ]) |
|
498 | + ->execute(); |
|
499 | + |
|
500 | + $this->addChange($addressBookId, $cardUri, 1); |
|
501 | + $this->updateProperties($addressBookId, $cardUri, $cardData); |
|
502 | + |
|
503 | + if (!is_null($this->dispatcher)) { |
|
504 | + $this->dispatcher->dispatch('\OCA\DAV\CardDAV\CardDavBackend::createCard', |
|
505 | + new GenericEvent(null, [ |
|
506 | + 'addressBookId' => $addressBookId, |
|
507 | + 'cardUri' => $cardUri, |
|
508 | + 'cardData' => $cardData])); |
|
509 | + } |
|
510 | + |
|
511 | + return '"' . $etag . '"'; |
|
512 | + } |
|
513 | + |
|
514 | + /** |
|
515 | + * Updates a card. |
|
516 | + * |
|
517 | + * The addressbook id will be passed as the first argument. This is the |
|
518 | + * same id as it is returned from the getAddressBooksForUser method. |
|
519 | + * |
|
520 | + * The cardUri is a base uri, and doesn't include the full path. The |
|
521 | + * cardData argument is the vcard body, and is passed as a string. |
|
522 | + * |
|
523 | + * It is possible to return an ETag from this method. This ETag should |
|
524 | + * match that of the updated resource, and must be enclosed with double |
|
525 | + * quotes (that is: the string itself must contain the actual quotes). |
|
526 | + * |
|
527 | + * You should only return the ETag if you store the carddata as-is. If a |
|
528 | + * subsequent GET request on the same card does not have the same body, |
|
529 | + * byte-by-byte and you did return an ETag here, clients tend to get |
|
530 | + * confused. |
|
531 | + * |
|
532 | + * If you don't return an ETag, you can just return null. |
|
533 | + * |
|
534 | + * @param mixed $addressBookId |
|
535 | + * @param string $cardUri |
|
536 | + * @param string $cardData |
|
537 | + * @return string |
|
538 | + */ |
|
539 | + function updateCard($addressBookId, $cardUri, $cardData) { |
|
540 | + |
|
541 | + $etag = md5($cardData); |
|
542 | + $query = $this->db->getQueryBuilder(); |
|
543 | + $query->update('cards') |
|
544 | + ->set('carddata', $query->createNamedParameter($cardData, IQueryBuilder::PARAM_LOB)) |
|
545 | + ->set('lastmodified', $query->createNamedParameter(time())) |
|
546 | + ->set('size', $query->createNamedParameter(strlen($cardData))) |
|
547 | + ->set('etag', $query->createNamedParameter($etag)) |
|
548 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
549 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
550 | + ->execute(); |
|
551 | + |
|
552 | + $this->addChange($addressBookId, $cardUri, 2); |
|
553 | + $this->updateProperties($addressBookId, $cardUri, $cardData); |
|
554 | + |
|
555 | + if (!is_null($this->dispatcher)) { |
|
556 | + $this->dispatcher->dispatch('\OCA\DAV\CardDAV\CardDavBackend::updateCard', |
|
557 | + new GenericEvent(null, [ |
|
558 | + 'addressBookId' => $addressBookId, |
|
559 | + 'cardUri' => $cardUri, |
|
560 | + 'cardData' => $cardData])); |
|
561 | + } |
|
562 | + |
|
563 | + return '"' . $etag . '"'; |
|
564 | + } |
|
565 | + |
|
566 | + /** |
|
567 | + * Deletes a card |
|
568 | + * |
|
569 | + * @param mixed $addressBookId |
|
570 | + * @param string $cardUri |
|
571 | + * @return bool |
|
572 | + */ |
|
573 | + function deleteCard($addressBookId, $cardUri) { |
|
574 | + try { |
|
575 | + $cardId = $this->getCardId($addressBookId, $cardUri); |
|
576 | + } catch (\InvalidArgumentException $e) { |
|
577 | + $cardId = null; |
|
578 | + } |
|
579 | + $query = $this->db->getQueryBuilder(); |
|
580 | + $ret = $query->delete('cards') |
|
581 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
582 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
583 | + ->execute(); |
|
584 | + |
|
585 | + $this->addChange($addressBookId, $cardUri, 3); |
|
586 | + |
|
587 | + if (!is_null($this->dispatcher)) { |
|
588 | + $this->dispatcher->dispatch('\OCA\DAV\CardDAV\CardDavBackend::deleteCard', |
|
589 | + new GenericEvent(null, [ |
|
590 | + 'addressBookId' => $addressBookId, |
|
591 | + 'cardUri' => $cardUri])); |
|
592 | + } |
|
593 | + |
|
594 | + if ($ret === 1) { |
|
595 | + if ($cardId !== null) { |
|
596 | + $this->purgeProperties($addressBookId, $cardId); |
|
597 | + } |
|
598 | + return true; |
|
599 | + } |
|
600 | + |
|
601 | + return false; |
|
602 | + } |
|
603 | + |
|
604 | + /** |
|
605 | + * The getChanges method returns all the changes that have happened, since |
|
606 | + * the specified syncToken in the specified address book. |
|
607 | + * |
|
608 | + * This function should return an array, such as the following: |
|
609 | + * |
|
610 | + * [ |
|
611 | + * 'syncToken' => 'The current synctoken', |
|
612 | + * 'added' => [ |
|
613 | + * 'new.txt', |
|
614 | + * ], |
|
615 | + * 'modified' => [ |
|
616 | + * 'modified.txt', |
|
617 | + * ], |
|
618 | + * 'deleted' => [ |
|
619 | + * 'foo.php.bak', |
|
620 | + * 'old.txt' |
|
621 | + * ] |
|
622 | + * ]; |
|
623 | + * |
|
624 | + * The returned syncToken property should reflect the *current* syncToken |
|
625 | + * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
|
626 | + * property. This is needed here too, to ensure the operation is atomic. |
|
627 | + * |
|
628 | + * If the $syncToken argument is specified as null, this is an initial |
|
629 | + * sync, and all members should be reported. |
|
630 | + * |
|
631 | + * The modified property is an array of nodenames that have changed since |
|
632 | + * the last token. |
|
633 | + * |
|
634 | + * The deleted property is an array with nodenames, that have been deleted |
|
635 | + * from collection. |
|
636 | + * |
|
637 | + * The $syncLevel argument is basically the 'depth' of the report. If it's |
|
638 | + * 1, you only have to report changes that happened only directly in |
|
639 | + * immediate descendants. If it's 2, it should also include changes from |
|
640 | + * the nodes below the child collections. (grandchildren) |
|
641 | + * |
|
642 | + * The $limit argument allows a client to specify how many results should |
|
643 | + * be returned at most. If the limit is not specified, it should be treated |
|
644 | + * as infinite. |
|
645 | + * |
|
646 | + * If the limit (infinite or not) is higher than you're willing to return, |
|
647 | + * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
|
648 | + * |
|
649 | + * If the syncToken is expired (due to data cleanup) or unknown, you must |
|
650 | + * return null. |
|
651 | + * |
|
652 | + * The limit is 'suggestive'. You are free to ignore it. |
|
653 | + * |
|
654 | + * @param string $addressBookId |
|
655 | + * @param string $syncToken |
|
656 | + * @param int $syncLevel |
|
657 | + * @param int $limit |
|
658 | + * @return array |
|
659 | + */ |
|
660 | + function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel, $limit = null) { |
|
661 | + // Current synctoken |
|
662 | + $stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*addressbooks` WHERE `id` = ?'); |
|
663 | + $stmt->execute([ $addressBookId ]); |
|
664 | + $currentToken = $stmt->fetchColumn(0); |
|
665 | + |
|
666 | + if (is_null($currentToken)) return null; |
|
667 | + |
|
668 | + $result = [ |
|
669 | + 'syncToken' => $currentToken, |
|
670 | + 'added' => [], |
|
671 | + 'modified' => [], |
|
672 | + 'deleted' => [], |
|
673 | + ]; |
|
674 | + |
|
675 | + if ($syncToken) { |
|
676 | + |
|
677 | + $query = "SELECT `uri`, `operation` FROM `*PREFIX*addressbookchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `addressbookid` = ? ORDER BY `synctoken`"; |
|
678 | + if ($limit>0) { |
|
679 | + $query .= " `LIMIT` " . (int)$limit; |
|
680 | + } |
|
681 | + |
|
682 | + // Fetching all changes |
|
683 | + $stmt = $this->db->prepare($query); |
|
684 | + $stmt->execute([$syncToken, $currentToken, $addressBookId]); |
|
685 | + |
|
686 | + $changes = []; |
|
687 | + |
|
688 | + // This loop ensures that any duplicates are overwritten, only the |
|
689 | + // last change on a node is relevant. |
|
690 | + while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
691 | + |
|
692 | + $changes[$row['uri']] = $row['operation']; |
|
693 | + |
|
694 | + } |
|
695 | + |
|
696 | + foreach($changes as $uri => $operation) { |
|
697 | + |
|
698 | + switch($operation) { |
|
699 | + case 1: |
|
700 | + $result['added'][] = $uri; |
|
701 | + break; |
|
702 | + case 2: |
|
703 | + $result['modified'][] = $uri; |
|
704 | + break; |
|
705 | + case 3: |
|
706 | + $result['deleted'][] = $uri; |
|
707 | + break; |
|
708 | + } |
|
709 | + |
|
710 | + } |
|
711 | + } else { |
|
712 | + // No synctoken supplied, this is the initial sync. |
|
713 | + $query = "SELECT `uri` FROM `*PREFIX*cards` WHERE `addressbookid` = ?"; |
|
714 | + $stmt = $this->db->prepare($query); |
|
715 | + $stmt->execute([$addressBookId]); |
|
716 | + |
|
717 | + $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
718 | + } |
|
719 | + return $result; |
|
720 | + } |
|
721 | + |
|
722 | + /** |
|
723 | + * Adds a change record to the addressbookchanges table. |
|
724 | + * |
|
725 | + * @param mixed $addressBookId |
|
726 | + * @param string $objectUri |
|
727 | + * @param int $operation 1 = add, 2 = modify, 3 = delete |
|
728 | + * @return void |
|
729 | + */ |
|
730 | + protected function addChange($addressBookId, $objectUri, $operation) { |
|
731 | + $sql = 'INSERT INTO `*PREFIX*addressbookchanges`(`uri`, `synctoken`, `addressbookid`, `operation`) SELECT ?, `synctoken`, ?, ? FROM `*PREFIX*addressbooks` WHERE `id` = ?'; |
|
732 | + $stmt = $this->db->prepare($sql); |
|
733 | + $stmt->execute([ |
|
734 | + $objectUri, |
|
735 | + $addressBookId, |
|
736 | + $operation, |
|
737 | + $addressBookId |
|
738 | + ]); |
|
739 | + $stmt = $this->db->prepare('UPDATE `*PREFIX*addressbooks` SET `synctoken` = `synctoken` + 1 WHERE `id` = ?'); |
|
740 | + $stmt->execute([ |
|
741 | + $addressBookId |
|
742 | + ]); |
|
743 | + } |
|
744 | + |
|
745 | + private function readBlob($cardData) { |
|
746 | + if (is_resource($cardData)) { |
|
747 | + return stream_get_contents($cardData); |
|
748 | + } |
|
749 | + |
|
750 | + return $cardData; |
|
751 | + } |
|
752 | + |
|
753 | + /** |
|
754 | + * @param IShareable $shareable |
|
755 | + * @param string[] $add |
|
756 | + * @param string[] $remove |
|
757 | + */ |
|
758 | + public function updateShares(IShareable $shareable, $add, $remove) { |
|
759 | + $this->sharingBackend->updateShares($shareable, $add, $remove); |
|
760 | + } |
|
761 | + |
|
762 | + /** |
|
763 | + * search contact |
|
764 | + * |
|
765 | + * @param int $addressBookId |
|
766 | + * @param string $pattern which should match within the $searchProperties |
|
767 | + * @param array $searchProperties defines the properties within the query pattern should match |
|
768 | + * @return array an array of contacts which are arrays of key-value-pairs |
|
769 | + */ |
|
770 | + public function search($addressBookId, $pattern, $searchProperties) { |
|
771 | + $query = $this->db->getQueryBuilder(); |
|
772 | + $query2 = $this->db->getQueryBuilder(); |
|
773 | + $query2->selectDistinct('cp.cardid')->from($this->dbCardsPropertiesTable, 'cp'); |
|
774 | + foreach ($searchProperties as $property) { |
|
775 | + $query2->orWhere( |
|
776 | + $query2->expr()->andX( |
|
777 | + $query2->expr()->eq('cp.name', $query->createNamedParameter($property)), |
|
778 | + $query2->expr()->ilike('cp.value', $query->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%')) |
|
779 | + ) |
|
780 | + ); |
|
781 | + } |
|
782 | + $query2->andWhere($query2->expr()->eq('cp.addressbookid', $query->createNamedParameter($addressBookId))); |
|
783 | + |
|
784 | + $query->select('c.carddata')->from($this->dbCardsTable, 'c') |
|
785 | + ->where($query->expr()->in('c.id', $query->createFunction($query2->getSQL()))); |
|
786 | + |
|
787 | + $result = $query->execute(); |
|
788 | + $cards = $result->fetchAll(); |
|
789 | + |
|
790 | + $result->closeCursor(); |
|
791 | + |
|
792 | + return array_map(function($array) {return $this->readBlob($array['carddata']);}, $cards); |
|
793 | + |
|
794 | + } |
|
795 | + |
|
796 | + /** |
|
797 | + * @param int $bookId |
|
798 | + * @param string $name |
|
799 | + * @return array |
|
800 | + */ |
|
801 | + public function collectCardProperties($bookId, $name) { |
|
802 | + $query = $this->db->getQueryBuilder(); |
|
803 | + $result = $query->selectDistinct('value') |
|
804 | + ->from($this->dbCardsPropertiesTable) |
|
805 | + ->where($query->expr()->eq('name', $query->createNamedParameter($name))) |
|
806 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($bookId))) |
|
807 | + ->execute(); |
|
808 | + |
|
809 | + $all = $result->fetchAll(PDO::FETCH_COLUMN); |
|
810 | + $result->closeCursor(); |
|
811 | + |
|
812 | + return $all; |
|
813 | + } |
|
814 | + |
|
815 | + /** |
|
816 | + * get URI from a given contact |
|
817 | + * |
|
818 | + * @param int $id |
|
819 | + * @return string |
|
820 | + */ |
|
821 | + public function getCardUri($id) { |
|
822 | + $query = $this->db->getQueryBuilder(); |
|
823 | + $query->select('uri')->from($this->dbCardsTable) |
|
824 | + ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
825 | + ->setParameter('id', $id); |
|
826 | + |
|
827 | + $result = $query->execute(); |
|
828 | + $uri = $result->fetch(); |
|
829 | + $result->closeCursor(); |
|
830 | + |
|
831 | + if (!isset($uri['uri'])) { |
|
832 | + throw new \InvalidArgumentException('Card does not exists: ' . $id); |
|
833 | + } |
|
834 | + |
|
835 | + return $uri['uri']; |
|
836 | + } |
|
837 | + |
|
838 | + /** |
|
839 | + * return contact with the given URI |
|
840 | + * |
|
841 | + * @param int $addressBookId |
|
842 | + * @param string $uri |
|
843 | + * @returns array |
|
844 | + */ |
|
845 | + public function getContact($addressBookId, $uri) { |
|
846 | + $result = []; |
|
847 | + $query = $this->db->getQueryBuilder(); |
|
848 | + $query->select('*')->from($this->dbCardsTable) |
|
849 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
850 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
851 | + $queryResult = $query->execute(); |
|
852 | + $contact = $queryResult->fetch(); |
|
853 | + $queryResult->closeCursor(); |
|
854 | + |
|
855 | + if (is_array($contact)) { |
|
856 | + $result = $contact; |
|
857 | + } |
|
858 | + |
|
859 | + return $result; |
|
860 | + } |
|
861 | + |
|
862 | + /** |
|
863 | + * Returns the list of people whom this address book is shared with. |
|
864 | + * |
|
865 | + * Every element in this array should have the following properties: |
|
866 | + * * href - Often a mailto: address |
|
867 | + * * commonName - Optional, for example a first + last name |
|
868 | + * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
|
869 | + * * readOnly - boolean |
|
870 | + * * summary - Optional, a description for the share |
|
871 | + * |
|
872 | + * @return array |
|
873 | + */ |
|
874 | + public function getShares($addressBookId) { |
|
875 | + return $this->sharingBackend->getShares($addressBookId); |
|
876 | + } |
|
877 | + |
|
878 | + /** |
|
879 | + * update properties table |
|
880 | + * |
|
881 | + * @param int $addressBookId |
|
882 | + * @param string $cardUri |
|
883 | + * @param string $vCardSerialized |
|
884 | + */ |
|
885 | + protected function updateProperties($addressBookId, $cardUri, $vCardSerialized) { |
|
886 | + $cardId = $this->getCardId($addressBookId, $cardUri); |
|
887 | + $vCard = $this->readCard($vCardSerialized); |
|
888 | + |
|
889 | + $this->purgeProperties($addressBookId, $cardId); |
|
890 | + |
|
891 | + $query = $this->db->getQueryBuilder(); |
|
892 | + $query->insert($this->dbCardsPropertiesTable) |
|
893 | + ->values( |
|
894 | + [ |
|
895 | + 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
896 | + 'cardid' => $query->createNamedParameter($cardId), |
|
897 | + 'name' => $query->createParameter('name'), |
|
898 | + 'value' => $query->createParameter('value'), |
|
899 | + 'preferred' => $query->createParameter('preferred') |
|
900 | + ] |
|
901 | + ); |
|
902 | + |
|
903 | + foreach ($vCard->children as $property) { |
|
904 | + if(!in_array($property->name, self::$indexProperties)) { |
|
905 | + continue; |
|
906 | + } |
|
907 | + $preferred = 0; |
|
908 | + foreach($property->parameters as $parameter) { |
|
909 | + if ($parameter->name == 'TYPE' && strtoupper($parameter->getValue()) == 'PREF') { |
|
910 | + $preferred = 1; |
|
911 | + break; |
|
912 | + } |
|
913 | + } |
|
914 | + $query->setParameter('name', $property->name); |
|
915 | + $query->setParameter('value', substr($property->getValue(), 0, 254)); |
|
916 | + $query->setParameter('preferred', $preferred); |
|
917 | + $query->execute(); |
|
918 | + } |
|
919 | + } |
|
920 | + |
|
921 | + /** |
|
922 | + * read vCard data into a vCard object |
|
923 | + * |
|
924 | + * @param string $cardData |
|
925 | + * @return VCard |
|
926 | + */ |
|
927 | + protected function readCard($cardData) { |
|
928 | + return Reader::read($cardData); |
|
929 | + } |
|
930 | + |
|
931 | + /** |
|
932 | + * delete all properties from a given card |
|
933 | + * |
|
934 | + * @param int $addressBookId |
|
935 | + * @param int $cardId |
|
936 | + */ |
|
937 | + protected function purgeProperties($addressBookId, $cardId) { |
|
938 | + $query = $this->db->getQueryBuilder(); |
|
939 | + $query->delete($this->dbCardsPropertiesTable) |
|
940 | + ->where($query->expr()->eq('cardid', $query->createNamedParameter($cardId))) |
|
941 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
942 | + $query->execute(); |
|
943 | + } |
|
944 | + |
|
945 | + /** |
|
946 | + * get ID from a given contact |
|
947 | + * |
|
948 | + * @param int $addressBookId |
|
949 | + * @param string $uri |
|
950 | + * @return int |
|
951 | + */ |
|
952 | + protected function getCardId($addressBookId, $uri) { |
|
953 | + $query = $this->db->getQueryBuilder(); |
|
954 | + $query->select('id')->from($this->dbCardsTable) |
|
955 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
956 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
957 | + |
|
958 | + $result = $query->execute(); |
|
959 | + $cardIds = $result->fetch(); |
|
960 | + $result->closeCursor(); |
|
961 | + |
|
962 | + if (!isset($cardIds['id'])) { |
|
963 | + throw new \InvalidArgumentException('Card does not exists: ' . $uri); |
|
964 | + } |
|
965 | + |
|
966 | + return (int)$cardIds['id']; |
|
967 | + } |
|
968 | + |
|
969 | + /** |
|
970 | + * For shared address books the sharee is set in the ACL of the address book |
|
971 | + * @param $addressBookId |
|
972 | + * @param $acl |
|
973 | + * @return array |
|
974 | + */ |
|
975 | + public function applyShareAcl($addressBookId, $acl) { |
|
976 | + return $this->sharingBackend->applyShareAcl($addressBookId, $acl); |
|
977 | + } |
|
978 | + |
|
979 | + private function convertPrincipal($principalUri, $toV2) { |
|
980 | + if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
|
981 | + list(, $name) = URLUtil::splitPath($principalUri); |
|
982 | + if ($toV2 === true) { |
|
983 | + return "principals/users/$name"; |
|
984 | + } |
|
985 | + return "principals/$name"; |
|
986 | + } |
|
987 | + return $principalUri; |
|
988 | + } |
|
989 | 989 | } |
@@ -246,7 +246,7 @@ |
||
246 | 246 | } |
247 | 247 | |
248 | 248 | /** |
249 | - * @return array|null |
|
249 | + * @return string |
|
250 | 250 | */ |
251 | 251 | public function getLocalSystemAddressBook() { |
252 | 252 | if (is_null($this->localSystemAddressBook)) { |
@@ -36,250 +36,250 @@ |
||
36 | 36 | |
37 | 37 | class SyncService { |
38 | 38 | |
39 | - /** @var CardDavBackend */ |
|
40 | - private $backend; |
|
41 | - |
|
42 | - /** @var IUserManager */ |
|
43 | - private $userManager; |
|
44 | - |
|
45 | - /** @var ILogger */ |
|
46 | - private $logger; |
|
47 | - |
|
48 | - /** @var array */ |
|
49 | - private $localSystemAddressBook; |
|
50 | - |
|
51 | - public function __construct(CardDavBackend $backend, IUserManager $userManager, ILogger $logger) { |
|
52 | - $this->backend = $backend; |
|
53 | - $this->userManager = $userManager; |
|
54 | - $this->logger = $logger; |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * @param string $url |
|
59 | - * @param string $userName |
|
60 | - * @param string $sharedSecret |
|
61 | - * @param string $syncToken |
|
62 | - * @param int $targetBookId |
|
63 | - * @param string $targetPrincipal |
|
64 | - * @param array $targetProperties |
|
65 | - * @return string |
|
66 | - * @throws \Exception |
|
67 | - */ |
|
68 | - public function syncRemoteAddressBook($url, $userName, $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetProperties) { |
|
69 | - // 1. create addressbook |
|
70 | - $book = $this->ensureSystemAddressBookExists($targetPrincipal, $targetBookId, $targetProperties); |
|
71 | - $addressBookId = $book['id']; |
|
72 | - |
|
73 | - // 2. query changes |
|
74 | - try { |
|
75 | - $response = $this->requestSyncReport($url, $userName, $sharedSecret, $syncToken); |
|
76 | - } catch (ClientHttpException $ex) { |
|
77 | - if ($ex->getCode() === Http::STATUS_UNAUTHORIZED) { |
|
78 | - // remote server revoked access to the address book, remove it |
|
79 | - $this->backend->deleteAddressBook($addressBookId); |
|
80 | - $this->logger->info('Authorization failed, remove address book: ' . $url, ['app' => 'dav']); |
|
81 | - throw $ex; |
|
82 | - } |
|
83 | - } |
|
84 | - |
|
85 | - // 3. apply changes |
|
86 | - // TODO: use multi-get for download |
|
87 | - foreach ($response['response'] as $resource => $status) { |
|
88 | - $cardUri = basename($resource); |
|
89 | - if (isset($status[200])) { |
|
90 | - $vCard = $this->download($url, $sharedSecret, $resource); |
|
91 | - $existingCard = $this->backend->getCard($addressBookId, $cardUri); |
|
92 | - if ($existingCard === false) { |
|
93 | - $this->backend->createCard($addressBookId, $cardUri, $vCard['body']); |
|
94 | - } else { |
|
95 | - $this->backend->updateCard($addressBookId, $cardUri, $vCard['body']); |
|
96 | - } |
|
97 | - } else { |
|
98 | - $this->backend->deleteCard($addressBookId, $cardUri); |
|
99 | - } |
|
100 | - } |
|
101 | - |
|
102 | - return $response['token']; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * @param string $principal |
|
107 | - * @param string $id |
|
108 | - * @param array $properties |
|
109 | - * @return array|null |
|
110 | - * @throws \Sabre\DAV\Exception\BadRequest |
|
111 | - */ |
|
112 | - public function ensureSystemAddressBookExists($principal, $id, $properties) { |
|
113 | - $book = $this->backend->getAddressBooksByUri($principal, $id); |
|
114 | - if (!is_null($book)) { |
|
115 | - return $book; |
|
116 | - } |
|
117 | - $this->backend->createAddressBook($principal, $id, $properties); |
|
118 | - |
|
119 | - return $this->backend->getAddressBooksByUri($principal, $id); |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * @param string $url |
|
124 | - * @param string $userName |
|
125 | - * @param string $sharedSecret |
|
126 | - * @param string $syncToken |
|
127 | - * @return array |
|
128 | - */ |
|
129 | - protected function requestSyncReport($url, $userName, $sharedSecret, $syncToken) { |
|
130 | - $settings = [ |
|
131 | - 'baseUri' => $url . '/', |
|
132 | - 'userName' => $userName, |
|
133 | - 'password' => $sharedSecret, |
|
134 | - ]; |
|
135 | - $client = new Client($settings); |
|
136 | - $client->setThrowExceptions(true); |
|
137 | - |
|
138 | - $addressBookUrl = "remote.php/dav/addressbooks/system/system/system"; |
|
139 | - $body = $this->buildSyncCollectionRequestBody($syncToken); |
|
140 | - |
|
141 | - $response = $client->request('REPORT', $addressBookUrl, $body, [ |
|
142 | - 'Content-Type' => 'application/xml' |
|
143 | - ]); |
|
144 | - |
|
145 | - $result = $this->parseMultiStatus($response['body']); |
|
146 | - |
|
147 | - return $result; |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * @param string $url |
|
152 | - * @param string $sharedSecret |
|
153 | - * @param string $resourcePath |
|
154 | - * @return array |
|
155 | - */ |
|
156 | - protected function download($url, $sharedSecret, $resourcePath) { |
|
157 | - $settings = [ |
|
158 | - 'baseUri' => $url, |
|
159 | - 'userName' => 'system', |
|
160 | - 'password' => $sharedSecret, |
|
161 | - ]; |
|
162 | - $client = new Client($settings); |
|
163 | - $client->setThrowExceptions(true); |
|
164 | - |
|
165 | - $response = $client->request('GET', $resourcePath); |
|
166 | - return $response; |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * @param string|null $syncToken |
|
171 | - * @return string |
|
172 | - */ |
|
173 | - private function buildSyncCollectionRequestBody($syncToken) { |
|
174 | - |
|
175 | - $dom = new \DOMDocument('1.0', 'UTF-8'); |
|
176 | - $dom->formatOutput = true; |
|
177 | - $root = $dom->createElementNS('DAV:', 'd:sync-collection'); |
|
178 | - $sync = $dom->createElement('d:sync-token', $syncToken); |
|
179 | - $prop = $dom->createElement('d:prop'); |
|
180 | - $cont = $dom->createElement('d:getcontenttype'); |
|
181 | - $etag = $dom->createElement('d:getetag'); |
|
182 | - |
|
183 | - $prop->appendChild($cont); |
|
184 | - $prop->appendChild($etag); |
|
185 | - $root->appendChild($sync); |
|
186 | - $root->appendChild($prop); |
|
187 | - $dom->appendChild($root); |
|
188 | - $body = $dom->saveXML(); |
|
189 | - |
|
190 | - return $body; |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * @param string $body |
|
195 | - * @return array |
|
196 | - * @throws \Sabre\Xml\ParseException |
|
197 | - */ |
|
198 | - private function parseMultiStatus($body) { |
|
199 | - $xml = new Service(); |
|
200 | - |
|
201 | - /** @var MultiStatus $multiStatus */ |
|
202 | - $multiStatus = $xml->expect('{DAV:}multistatus', $body); |
|
203 | - |
|
204 | - $result = []; |
|
205 | - foreach ($multiStatus->getResponses() as $response) { |
|
206 | - $result[$response->getHref()] = $response->getResponseProperties(); |
|
207 | - } |
|
208 | - |
|
209 | - return ['response' => $result, 'token' => $multiStatus->getSyncToken()]; |
|
210 | - } |
|
211 | - |
|
212 | - /** |
|
213 | - * @param IUser $user |
|
214 | - */ |
|
215 | - public function updateUser($user) { |
|
216 | - $systemAddressBook = $this->getLocalSystemAddressBook(); |
|
217 | - $addressBookId = $systemAddressBook['id']; |
|
218 | - $converter = new Converter(); |
|
219 | - $name = $user->getBackendClassName(); |
|
220 | - $userId = $user->getUID(); |
|
221 | - |
|
222 | - $cardId = "$name:$userId.vcf"; |
|
223 | - $card = $this->backend->getCard($addressBookId, $cardId); |
|
224 | - if ($card === false) { |
|
225 | - $vCard = $converter->createCardFromUser($user); |
|
226 | - $this->backend->createCard($addressBookId, $cardId, $vCard->serialize()); |
|
227 | - } else { |
|
228 | - $vCard = Reader::read($card['carddata']); |
|
229 | - if ($converter->updateCard($vCard, $user)) { |
|
230 | - $this->backend->updateCard($addressBookId, $cardId, $vCard->serialize()); |
|
231 | - } |
|
232 | - } |
|
233 | - } |
|
234 | - |
|
235 | - /** |
|
236 | - * @param IUser|string $userOrCardId |
|
237 | - */ |
|
238 | - public function deleteUser($userOrCardId) { |
|
239 | - $systemAddressBook = $this->getLocalSystemAddressBook(); |
|
240 | - if ($userOrCardId instanceof IUser){ |
|
241 | - $name = $userOrCardId->getBackendClassName(); |
|
242 | - $userId = $userOrCardId->getUID(); |
|
243 | - |
|
244 | - $userOrCardId = "$name:$userId.vcf"; |
|
245 | - } |
|
246 | - $this->backend->deleteCard($systemAddressBook['id'], $userOrCardId); |
|
247 | - } |
|
248 | - |
|
249 | - /** |
|
250 | - * @return array|null |
|
251 | - */ |
|
252 | - public function getLocalSystemAddressBook() { |
|
253 | - if (is_null($this->localSystemAddressBook)) { |
|
254 | - $systemPrincipal = "principals/system/system"; |
|
255 | - $this->localSystemAddressBook = $this->ensureSystemAddressBookExists($systemPrincipal, 'system', [ |
|
256 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => 'System addressbook which holds all users of this instance' |
|
257 | - ]); |
|
258 | - } |
|
259 | - |
|
260 | - return $this->localSystemAddressBook; |
|
261 | - } |
|
262 | - |
|
263 | - public function syncInstance(\Closure $progressCallback = null) { |
|
264 | - $systemAddressBook = $this->getLocalSystemAddressBook(); |
|
265 | - $this->userManager->callForAllUsers(function($user) use ($systemAddressBook, $progressCallback) { |
|
266 | - $this->updateUser($user); |
|
267 | - if (!is_null($progressCallback)) { |
|
268 | - $progressCallback(); |
|
269 | - } |
|
270 | - }); |
|
271 | - |
|
272 | - // remove no longer existing |
|
273 | - $allCards = $this->backend->getCards($systemAddressBook['id']); |
|
274 | - foreach($allCards as $card) { |
|
275 | - $vCard = Reader::read($card['carddata']); |
|
276 | - $uid = $vCard->UID->getValue(); |
|
277 | - // load backend and see if user exists |
|
278 | - if (!$this->userManager->userExists($uid)) { |
|
279 | - $this->deleteUser($card['uri']); |
|
280 | - } |
|
281 | - } |
|
282 | - } |
|
39 | + /** @var CardDavBackend */ |
|
40 | + private $backend; |
|
41 | + |
|
42 | + /** @var IUserManager */ |
|
43 | + private $userManager; |
|
44 | + |
|
45 | + /** @var ILogger */ |
|
46 | + private $logger; |
|
47 | + |
|
48 | + /** @var array */ |
|
49 | + private $localSystemAddressBook; |
|
50 | + |
|
51 | + public function __construct(CardDavBackend $backend, IUserManager $userManager, ILogger $logger) { |
|
52 | + $this->backend = $backend; |
|
53 | + $this->userManager = $userManager; |
|
54 | + $this->logger = $logger; |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * @param string $url |
|
59 | + * @param string $userName |
|
60 | + * @param string $sharedSecret |
|
61 | + * @param string $syncToken |
|
62 | + * @param int $targetBookId |
|
63 | + * @param string $targetPrincipal |
|
64 | + * @param array $targetProperties |
|
65 | + * @return string |
|
66 | + * @throws \Exception |
|
67 | + */ |
|
68 | + public function syncRemoteAddressBook($url, $userName, $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetProperties) { |
|
69 | + // 1. create addressbook |
|
70 | + $book = $this->ensureSystemAddressBookExists($targetPrincipal, $targetBookId, $targetProperties); |
|
71 | + $addressBookId = $book['id']; |
|
72 | + |
|
73 | + // 2. query changes |
|
74 | + try { |
|
75 | + $response = $this->requestSyncReport($url, $userName, $sharedSecret, $syncToken); |
|
76 | + } catch (ClientHttpException $ex) { |
|
77 | + if ($ex->getCode() === Http::STATUS_UNAUTHORIZED) { |
|
78 | + // remote server revoked access to the address book, remove it |
|
79 | + $this->backend->deleteAddressBook($addressBookId); |
|
80 | + $this->logger->info('Authorization failed, remove address book: ' . $url, ['app' => 'dav']); |
|
81 | + throw $ex; |
|
82 | + } |
|
83 | + } |
|
84 | + |
|
85 | + // 3. apply changes |
|
86 | + // TODO: use multi-get for download |
|
87 | + foreach ($response['response'] as $resource => $status) { |
|
88 | + $cardUri = basename($resource); |
|
89 | + if (isset($status[200])) { |
|
90 | + $vCard = $this->download($url, $sharedSecret, $resource); |
|
91 | + $existingCard = $this->backend->getCard($addressBookId, $cardUri); |
|
92 | + if ($existingCard === false) { |
|
93 | + $this->backend->createCard($addressBookId, $cardUri, $vCard['body']); |
|
94 | + } else { |
|
95 | + $this->backend->updateCard($addressBookId, $cardUri, $vCard['body']); |
|
96 | + } |
|
97 | + } else { |
|
98 | + $this->backend->deleteCard($addressBookId, $cardUri); |
|
99 | + } |
|
100 | + } |
|
101 | + |
|
102 | + return $response['token']; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * @param string $principal |
|
107 | + * @param string $id |
|
108 | + * @param array $properties |
|
109 | + * @return array|null |
|
110 | + * @throws \Sabre\DAV\Exception\BadRequest |
|
111 | + */ |
|
112 | + public function ensureSystemAddressBookExists($principal, $id, $properties) { |
|
113 | + $book = $this->backend->getAddressBooksByUri($principal, $id); |
|
114 | + if (!is_null($book)) { |
|
115 | + return $book; |
|
116 | + } |
|
117 | + $this->backend->createAddressBook($principal, $id, $properties); |
|
118 | + |
|
119 | + return $this->backend->getAddressBooksByUri($principal, $id); |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * @param string $url |
|
124 | + * @param string $userName |
|
125 | + * @param string $sharedSecret |
|
126 | + * @param string $syncToken |
|
127 | + * @return array |
|
128 | + */ |
|
129 | + protected function requestSyncReport($url, $userName, $sharedSecret, $syncToken) { |
|
130 | + $settings = [ |
|
131 | + 'baseUri' => $url . '/', |
|
132 | + 'userName' => $userName, |
|
133 | + 'password' => $sharedSecret, |
|
134 | + ]; |
|
135 | + $client = new Client($settings); |
|
136 | + $client->setThrowExceptions(true); |
|
137 | + |
|
138 | + $addressBookUrl = "remote.php/dav/addressbooks/system/system/system"; |
|
139 | + $body = $this->buildSyncCollectionRequestBody($syncToken); |
|
140 | + |
|
141 | + $response = $client->request('REPORT', $addressBookUrl, $body, [ |
|
142 | + 'Content-Type' => 'application/xml' |
|
143 | + ]); |
|
144 | + |
|
145 | + $result = $this->parseMultiStatus($response['body']); |
|
146 | + |
|
147 | + return $result; |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * @param string $url |
|
152 | + * @param string $sharedSecret |
|
153 | + * @param string $resourcePath |
|
154 | + * @return array |
|
155 | + */ |
|
156 | + protected function download($url, $sharedSecret, $resourcePath) { |
|
157 | + $settings = [ |
|
158 | + 'baseUri' => $url, |
|
159 | + 'userName' => 'system', |
|
160 | + 'password' => $sharedSecret, |
|
161 | + ]; |
|
162 | + $client = new Client($settings); |
|
163 | + $client->setThrowExceptions(true); |
|
164 | + |
|
165 | + $response = $client->request('GET', $resourcePath); |
|
166 | + return $response; |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * @param string|null $syncToken |
|
171 | + * @return string |
|
172 | + */ |
|
173 | + private function buildSyncCollectionRequestBody($syncToken) { |
|
174 | + |
|
175 | + $dom = new \DOMDocument('1.0', 'UTF-8'); |
|
176 | + $dom->formatOutput = true; |
|
177 | + $root = $dom->createElementNS('DAV:', 'd:sync-collection'); |
|
178 | + $sync = $dom->createElement('d:sync-token', $syncToken); |
|
179 | + $prop = $dom->createElement('d:prop'); |
|
180 | + $cont = $dom->createElement('d:getcontenttype'); |
|
181 | + $etag = $dom->createElement('d:getetag'); |
|
182 | + |
|
183 | + $prop->appendChild($cont); |
|
184 | + $prop->appendChild($etag); |
|
185 | + $root->appendChild($sync); |
|
186 | + $root->appendChild($prop); |
|
187 | + $dom->appendChild($root); |
|
188 | + $body = $dom->saveXML(); |
|
189 | + |
|
190 | + return $body; |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * @param string $body |
|
195 | + * @return array |
|
196 | + * @throws \Sabre\Xml\ParseException |
|
197 | + */ |
|
198 | + private function parseMultiStatus($body) { |
|
199 | + $xml = new Service(); |
|
200 | + |
|
201 | + /** @var MultiStatus $multiStatus */ |
|
202 | + $multiStatus = $xml->expect('{DAV:}multistatus', $body); |
|
203 | + |
|
204 | + $result = []; |
|
205 | + foreach ($multiStatus->getResponses() as $response) { |
|
206 | + $result[$response->getHref()] = $response->getResponseProperties(); |
|
207 | + } |
|
208 | + |
|
209 | + return ['response' => $result, 'token' => $multiStatus->getSyncToken()]; |
|
210 | + } |
|
211 | + |
|
212 | + /** |
|
213 | + * @param IUser $user |
|
214 | + */ |
|
215 | + public function updateUser($user) { |
|
216 | + $systemAddressBook = $this->getLocalSystemAddressBook(); |
|
217 | + $addressBookId = $systemAddressBook['id']; |
|
218 | + $converter = new Converter(); |
|
219 | + $name = $user->getBackendClassName(); |
|
220 | + $userId = $user->getUID(); |
|
221 | + |
|
222 | + $cardId = "$name:$userId.vcf"; |
|
223 | + $card = $this->backend->getCard($addressBookId, $cardId); |
|
224 | + if ($card === false) { |
|
225 | + $vCard = $converter->createCardFromUser($user); |
|
226 | + $this->backend->createCard($addressBookId, $cardId, $vCard->serialize()); |
|
227 | + } else { |
|
228 | + $vCard = Reader::read($card['carddata']); |
|
229 | + if ($converter->updateCard($vCard, $user)) { |
|
230 | + $this->backend->updateCard($addressBookId, $cardId, $vCard->serialize()); |
|
231 | + } |
|
232 | + } |
|
233 | + } |
|
234 | + |
|
235 | + /** |
|
236 | + * @param IUser|string $userOrCardId |
|
237 | + */ |
|
238 | + public function deleteUser($userOrCardId) { |
|
239 | + $systemAddressBook = $this->getLocalSystemAddressBook(); |
|
240 | + if ($userOrCardId instanceof IUser){ |
|
241 | + $name = $userOrCardId->getBackendClassName(); |
|
242 | + $userId = $userOrCardId->getUID(); |
|
243 | + |
|
244 | + $userOrCardId = "$name:$userId.vcf"; |
|
245 | + } |
|
246 | + $this->backend->deleteCard($systemAddressBook['id'], $userOrCardId); |
|
247 | + } |
|
248 | + |
|
249 | + /** |
|
250 | + * @return array|null |
|
251 | + */ |
|
252 | + public function getLocalSystemAddressBook() { |
|
253 | + if (is_null($this->localSystemAddressBook)) { |
|
254 | + $systemPrincipal = "principals/system/system"; |
|
255 | + $this->localSystemAddressBook = $this->ensureSystemAddressBookExists($systemPrincipal, 'system', [ |
|
256 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => 'System addressbook which holds all users of this instance' |
|
257 | + ]); |
|
258 | + } |
|
259 | + |
|
260 | + return $this->localSystemAddressBook; |
|
261 | + } |
|
262 | + |
|
263 | + public function syncInstance(\Closure $progressCallback = null) { |
|
264 | + $systemAddressBook = $this->getLocalSystemAddressBook(); |
|
265 | + $this->userManager->callForAllUsers(function($user) use ($systemAddressBook, $progressCallback) { |
|
266 | + $this->updateUser($user); |
|
267 | + if (!is_null($progressCallback)) { |
|
268 | + $progressCallback(); |
|
269 | + } |
|
270 | + }); |
|
271 | + |
|
272 | + // remove no longer existing |
|
273 | + $allCards = $this->backend->getCards($systemAddressBook['id']); |
|
274 | + foreach($allCards as $card) { |
|
275 | + $vCard = Reader::read($card['carddata']); |
|
276 | + $uid = $vCard->UID->getValue(); |
|
277 | + // load backend and see if user exists |
|
278 | + if (!$this->userManager->userExists($uid)) { |
|
279 | + $this->deleteUser($card['uri']); |
|
280 | + } |
|
281 | + } |
|
282 | + } |
|
283 | 283 | |
284 | 284 | |
285 | 285 | } |
@@ -115,7 +115,7 @@ |
||
115 | 115 | /** |
116 | 116 | * Returns an array with all the child nodes |
117 | 117 | * |
118 | - * @return \Sabre\DAV\INode[] |
|
118 | + * @return CommentNode[] |
|
119 | 119 | */ |
120 | 120 | function getChildren() { |
121 | 121 | return $this->findChildren(); |
@@ -39,162 +39,162 @@ |
||
39 | 39 | * @package OCA\DAV\Comments |
40 | 40 | */ |
41 | 41 | class EntityCollection extends RootCollection implements \Sabre\DAV\IProperties { |
42 | - const PROPERTY_NAME_READ_MARKER = '{http://owncloud.org/ns}readMarker'; |
|
43 | - |
|
44 | - /** @var Folder */ |
|
45 | - protected $fileRoot; |
|
46 | - |
|
47 | - /** @var string */ |
|
48 | - protected $id; |
|
49 | - |
|
50 | - /** @var ILogger */ |
|
51 | - protected $logger; |
|
52 | - |
|
53 | - /** |
|
54 | - * @param string $id |
|
55 | - * @param string $name |
|
56 | - * @param ICommentsManager $commentsManager |
|
57 | - * @param Folder $fileRoot |
|
58 | - * @param IUserManager $userManager |
|
59 | - * @param IUserSession $userSession |
|
60 | - * @param ILogger $logger |
|
61 | - */ |
|
62 | - public function __construct( |
|
63 | - $id, |
|
64 | - $name, |
|
65 | - ICommentsManager $commentsManager, |
|
66 | - Folder $fileRoot, |
|
67 | - IUserManager $userManager, |
|
68 | - IUserSession $userSession, |
|
69 | - ILogger $logger |
|
70 | - ) { |
|
71 | - foreach(['id', 'name'] as $property) { |
|
72 | - $$property = trim($$property); |
|
73 | - if(empty($$property) || !is_string($$property)) { |
|
74 | - throw new \InvalidArgumentException('"' . $property . '" parameter must be non-empty string'); |
|
75 | - } |
|
76 | - } |
|
77 | - $this->id = $id; |
|
78 | - $this->name = $name; |
|
79 | - $this->commentsManager = $commentsManager; |
|
80 | - $this->fileRoot = $fileRoot; |
|
81 | - $this->logger = $logger; |
|
82 | - $this->userManager = $userManager; |
|
83 | - $this->userSession = $userSession; |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * returns the ID of this entity |
|
88 | - * |
|
89 | - * @return string |
|
90 | - */ |
|
91 | - public function getId() { |
|
92 | - return $this->id; |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * Returns a specific child node, referenced by its name |
|
97 | - * |
|
98 | - * This method must throw Sabre\DAV\Exception\NotFound if the node does not |
|
99 | - * exist. |
|
100 | - * |
|
101 | - * @param string $name |
|
102 | - * @return \Sabre\DAV\INode |
|
103 | - * @throws NotFound |
|
104 | - */ |
|
105 | - function getChild($name) { |
|
106 | - try { |
|
107 | - $comment = $this->commentsManager->get($name); |
|
108 | - return new CommentNode( |
|
109 | - $this->commentsManager, |
|
110 | - $comment, |
|
111 | - $this->userManager, |
|
112 | - $this->userSession, |
|
113 | - $this->logger |
|
114 | - ); |
|
115 | - } catch (\OCP\Comments\NotFoundException $e) { |
|
116 | - throw new NotFound(); |
|
117 | - } |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * Returns an array with all the child nodes |
|
122 | - * |
|
123 | - * @return \Sabre\DAV\INode[] |
|
124 | - */ |
|
125 | - function getChildren() { |
|
126 | - return $this->findChildren(); |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Returns an array of comment nodes. Result can be influenced by offset, |
|
131 | - * limit and date time parameters. |
|
132 | - * |
|
133 | - * @param int $limit |
|
134 | - * @param int $offset |
|
135 | - * @param \DateTime|null $datetime |
|
136 | - * @return CommentNode[] |
|
137 | - */ |
|
138 | - function findChildren($limit = 0, $offset = 0, \DateTime $datetime = null) { |
|
139 | - $comments = $this->commentsManager->getForObject($this->name, $this->id, $limit, $offset, $datetime); |
|
140 | - $result = []; |
|
141 | - foreach($comments as $comment) { |
|
142 | - $result[] = new CommentNode( |
|
143 | - $this->commentsManager, |
|
144 | - $comment, |
|
145 | - $this->userManager, |
|
146 | - $this->userSession, |
|
147 | - $this->logger |
|
148 | - ); |
|
149 | - } |
|
150 | - return $result; |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * Checks if a child-node with the specified name exists |
|
155 | - * |
|
156 | - * @param string $name |
|
157 | - * @return bool |
|
158 | - */ |
|
159 | - function childExists($name) { |
|
160 | - try { |
|
161 | - $this->commentsManager->get($name); |
|
162 | - return true; |
|
163 | - } catch (\OCP\Comments\NotFoundException $e) { |
|
164 | - return false; |
|
165 | - } |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * Sets the read marker to the specified date for the logged in user |
|
170 | - * |
|
171 | - * @param \DateTime $value |
|
172 | - * @return bool |
|
173 | - */ |
|
174 | - public function setReadMarker($value) { |
|
175 | - $dateTime = new \DateTime($value); |
|
176 | - $user = $this->userSession->getUser(); |
|
177 | - $this->commentsManager->setReadMark($this->name, $this->id, $dateTime, $user); |
|
178 | - return true; |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * @inheritdoc |
|
183 | - */ |
|
184 | - function propPatch(PropPatch $propPatch) { |
|
185 | - $propPatch->handle(self::PROPERTY_NAME_READ_MARKER, [$this, 'setReadMarker']); |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * @inheritdoc |
|
190 | - */ |
|
191 | - function getProperties($properties) { |
|
192 | - $marker = null; |
|
193 | - $user = $this->userSession->getUser(); |
|
194 | - if(!is_null($user)) { |
|
195 | - $marker = $this->commentsManager->getReadMark($this->name, $this->id, $user); |
|
196 | - } |
|
197 | - return [self::PROPERTY_NAME_READ_MARKER => $marker]; |
|
198 | - } |
|
42 | + const PROPERTY_NAME_READ_MARKER = '{http://owncloud.org/ns}readMarker'; |
|
43 | + |
|
44 | + /** @var Folder */ |
|
45 | + protected $fileRoot; |
|
46 | + |
|
47 | + /** @var string */ |
|
48 | + protected $id; |
|
49 | + |
|
50 | + /** @var ILogger */ |
|
51 | + protected $logger; |
|
52 | + |
|
53 | + /** |
|
54 | + * @param string $id |
|
55 | + * @param string $name |
|
56 | + * @param ICommentsManager $commentsManager |
|
57 | + * @param Folder $fileRoot |
|
58 | + * @param IUserManager $userManager |
|
59 | + * @param IUserSession $userSession |
|
60 | + * @param ILogger $logger |
|
61 | + */ |
|
62 | + public function __construct( |
|
63 | + $id, |
|
64 | + $name, |
|
65 | + ICommentsManager $commentsManager, |
|
66 | + Folder $fileRoot, |
|
67 | + IUserManager $userManager, |
|
68 | + IUserSession $userSession, |
|
69 | + ILogger $logger |
|
70 | + ) { |
|
71 | + foreach(['id', 'name'] as $property) { |
|
72 | + $$property = trim($$property); |
|
73 | + if(empty($$property) || !is_string($$property)) { |
|
74 | + throw new \InvalidArgumentException('"' . $property . '" parameter must be non-empty string'); |
|
75 | + } |
|
76 | + } |
|
77 | + $this->id = $id; |
|
78 | + $this->name = $name; |
|
79 | + $this->commentsManager = $commentsManager; |
|
80 | + $this->fileRoot = $fileRoot; |
|
81 | + $this->logger = $logger; |
|
82 | + $this->userManager = $userManager; |
|
83 | + $this->userSession = $userSession; |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * returns the ID of this entity |
|
88 | + * |
|
89 | + * @return string |
|
90 | + */ |
|
91 | + public function getId() { |
|
92 | + return $this->id; |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * Returns a specific child node, referenced by its name |
|
97 | + * |
|
98 | + * This method must throw Sabre\DAV\Exception\NotFound if the node does not |
|
99 | + * exist. |
|
100 | + * |
|
101 | + * @param string $name |
|
102 | + * @return \Sabre\DAV\INode |
|
103 | + * @throws NotFound |
|
104 | + */ |
|
105 | + function getChild($name) { |
|
106 | + try { |
|
107 | + $comment = $this->commentsManager->get($name); |
|
108 | + return new CommentNode( |
|
109 | + $this->commentsManager, |
|
110 | + $comment, |
|
111 | + $this->userManager, |
|
112 | + $this->userSession, |
|
113 | + $this->logger |
|
114 | + ); |
|
115 | + } catch (\OCP\Comments\NotFoundException $e) { |
|
116 | + throw new NotFound(); |
|
117 | + } |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * Returns an array with all the child nodes |
|
122 | + * |
|
123 | + * @return \Sabre\DAV\INode[] |
|
124 | + */ |
|
125 | + function getChildren() { |
|
126 | + return $this->findChildren(); |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Returns an array of comment nodes. Result can be influenced by offset, |
|
131 | + * limit and date time parameters. |
|
132 | + * |
|
133 | + * @param int $limit |
|
134 | + * @param int $offset |
|
135 | + * @param \DateTime|null $datetime |
|
136 | + * @return CommentNode[] |
|
137 | + */ |
|
138 | + function findChildren($limit = 0, $offset = 0, \DateTime $datetime = null) { |
|
139 | + $comments = $this->commentsManager->getForObject($this->name, $this->id, $limit, $offset, $datetime); |
|
140 | + $result = []; |
|
141 | + foreach($comments as $comment) { |
|
142 | + $result[] = new CommentNode( |
|
143 | + $this->commentsManager, |
|
144 | + $comment, |
|
145 | + $this->userManager, |
|
146 | + $this->userSession, |
|
147 | + $this->logger |
|
148 | + ); |
|
149 | + } |
|
150 | + return $result; |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * Checks if a child-node with the specified name exists |
|
155 | + * |
|
156 | + * @param string $name |
|
157 | + * @return bool |
|
158 | + */ |
|
159 | + function childExists($name) { |
|
160 | + try { |
|
161 | + $this->commentsManager->get($name); |
|
162 | + return true; |
|
163 | + } catch (\OCP\Comments\NotFoundException $e) { |
|
164 | + return false; |
|
165 | + } |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * Sets the read marker to the specified date for the logged in user |
|
170 | + * |
|
171 | + * @param \DateTime $value |
|
172 | + * @return bool |
|
173 | + */ |
|
174 | + public function setReadMarker($value) { |
|
175 | + $dateTime = new \DateTime($value); |
|
176 | + $user = $this->userSession->getUser(); |
|
177 | + $this->commentsManager->setReadMark($this->name, $this->id, $dateTime, $user); |
|
178 | + return true; |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * @inheritdoc |
|
183 | + */ |
|
184 | + function propPatch(PropPatch $propPatch) { |
|
185 | + $propPatch->handle(self::PROPERTY_NAME_READ_MARKER, [$this, 'setReadMarker']); |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * @inheritdoc |
|
190 | + */ |
|
191 | + function getProperties($properties) { |
|
192 | + $marker = null; |
|
193 | + $user = $this->userSession->getUser(); |
|
194 | + if(!is_null($user)) { |
|
195 | + $marker = $this->commentsManager->getReadMark($this->name, $this->id, $user); |
|
196 | + } |
|
197 | + return [self::PROPERTY_NAME_READ_MARKER => $marker]; |
|
198 | + } |
|
199 | 199 | } |
200 | 200 |
@@ -146,7 +146,7 @@ |
||
146 | 146 | /** |
147 | 147 | * Returns an array with all the child nodes |
148 | 148 | * |
149 | - * @return \Sabre\DAV\INode[] |
|
149 | + * @return EntityTypeCollection[] |
|
150 | 150 | */ |
151 | 151 | function getChildren() { |
152 | 152 | $this->initCollections(); |
@@ -34,173 +34,173 @@ |
||
34 | 34 | |
35 | 35 | class RootCollection implements ICollection { |
36 | 36 | |
37 | - /** @var EntityTypeCollection[] */ |
|
38 | - private $entityTypeCollections = []; |
|
39 | - |
|
40 | - /** @var ICommentsManager */ |
|
41 | - protected $commentsManager; |
|
42 | - |
|
43 | - /** @var string */ |
|
44 | - protected $name = 'comments'; |
|
45 | - |
|
46 | - /** @var ILogger */ |
|
47 | - protected $logger; |
|
48 | - |
|
49 | - /** @var IUserManager */ |
|
50 | - protected $userManager; |
|
51 | - /** |
|
52 | - * @var IUserSession |
|
53 | - */ |
|
54 | - protected $userSession; |
|
55 | - /** |
|
56 | - * @var IRootFolder |
|
57 | - */ |
|
58 | - protected $rootFolder; |
|
59 | - |
|
60 | - /** |
|
61 | - * @param ICommentsManager $commentsManager |
|
62 | - * @param IUserManager $userManager |
|
63 | - * @param IUserSession $userSession |
|
64 | - * @param IRootFolder $rootFolder |
|
65 | - * @param ILogger $logger |
|
66 | - */ |
|
67 | - public function __construct( |
|
68 | - ICommentsManager $commentsManager, |
|
69 | - IUserManager $userManager, |
|
70 | - IUserSession $userSession, |
|
71 | - IRootFolder $rootFolder, |
|
72 | - ILogger $logger) |
|
73 | - { |
|
74 | - $this->commentsManager = $commentsManager; |
|
75 | - $this->logger = $logger; |
|
76 | - $this->userManager = $userManager; |
|
77 | - $this->userSession = $userSession; |
|
78 | - $this->rootFolder = $rootFolder; |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * initializes the collection. At this point of time, we need the logged in |
|
83 | - * user. Since it is not the case when the instance is created, we cannot |
|
84 | - * have this in the constructor. |
|
85 | - * |
|
86 | - * @throws NotAuthenticated |
|
87 | - */ |
|
88 | - protected function initCollections() { |
|
89 | - if(!empty($this->entityTypeCollections)) { |
|
90 | - return; |
|
91 | - } |
|
92 | - $user = $this->userSession->getUser(); |
|
93 | - if(is_null($user)) { |
|
94 | - throw new NotAuthenticated(); |
|
95 | - } |
|
96 | - $userFolder = $this->rootFolder->getUserFolder($user->getUID()); |
|
97 | - $this->entityTypeCollections['files'] = new EntityTypeCollection( |
|
98 | - 'files', |
|
99 | - $this->commentsManager, |
|
100 | - $userFolder, |
|
101 | - $this->userManager, |
|
102 | - $this->userSession, |
|
103 | - $this->logger |
|
104 | - ); |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * Creates a new file in the directory |
|
109 | - * |
|
110 | - * @param string $name Name of the file |
|
111 | - * @param resource|string $data Initial payload |
|
112 | - * @return null|string |
|
113 | - * @throws Forbidden |
|
114 | - */ |
|
115 | - function createFile($name, $data = null) { |
|
116 | - throw new Forbidden('Cannot create comments by id'); |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * Creates a new subdirectory |
|
121 | - * |
|
122 | - * @param string $name |
|
123 | - * @throws Forbidden |
|
124 | - */ |
|
125 | - function createDirectory($name) { |
|
126 | - throw new Forbidden('Permission denied to create collections'); |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Returns a specific child node, referenced by its name |
|
131 | - * |
|
132 | - * This method must throw Sabre\DAV\Exception\NotFound if the node does not |
|
133 | - * exist. |
|
134 | - * |
|
135 | - * @param string $name |
|
136 | - * @return \Sabre\DAV\INode |
|
137 | - * @throws NotFound |
|
138 | - */ |
|
139 | - function getChild($name) { |
|
140 | - $this->initCollections(); |
|
141 | - if(isset($this->entityTypeCollections[$name])) { |
|
142 | - return $this->entityTypeCollections[$name]; |
|
143 | - } |
|
144 | - throw new NotFound('Entity type "' . $name . '" not found."'); |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * Returns an array with all the child nodes |
|
149 | - * |
|
150 | - * @return \Sabre\DAV\INode[] |
|
151 | - */ |
|
152 | - function getChildren() { |
|
153 | - $this->initCollections(); |
|
154 | - return $this->entityTypeCollections; |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * Checks if a child-node with the specified name exists |
|
159 | - * |
|
160 | - * @param string $name |
|
161 | - * @return bool |
|
162 | - */ |
|
163 | - function childExists($name) { |
|
164 | - $this->initCollections(); |
|
165 | - return isset($this->entityTypeCollections[$name]); |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * Deleted the current node |
|
170 | - * |
|
171 | - * @throws Forbidden |
|
172 | - */ |
|
173 | - function delete() { |
|
174 | - throw new Forbidden('Permission denied to delete this collection'); |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * Returns the name of the node. |
|
179 | - * |
|
180 | - * This is used to generate the url. |
|
181 | - * |
|
182 | - * @return string |
|
183 | - */ |
|
184 | - function getName() { |
|
185 | - return $this->name; |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * Renames the node |
|
190 | - * |
|
191 | - * @param string $name The new name |
|
192 | - * @throws Forbidden |
|
193 | - */ |
|
194 | - function setName($name) { |
|
195 | - throw new Forbidden('Permission denied to rename this collection'); |
|
196 | - } |
|
197 | - |
|
198 | - /** |
|
199 | - * Returns the last modification time, as a unix timestamp |
|
200 | - * |
|
201 | - * @return int |
|
202 | - */ |
|
203 | - function getLastModified() { |
|
204 | - return null; |
|
205 | - } |
|
37 | + /** @var EntityTypeCollection[] */ |
|
38 | + private $entityTypeCollections = []; |
|
39 | + |
|
40 | + /** @var ICommentsManager */ |
|
41 | + protected $commentsManager; |
|
42 | + |
|
43 | + /** @var string */ |
|
44 | + protected $name = 'comments'; |
|
45 | + |
|
46 | + /** @var ILogger */ |
|
47 | + protected $logger; |
|
48 | + |
|
49 | + /** @var IUserManager */ |
|
50 | + protected $userManager; |
|
51 | + /** |
|
52 | + * @var IUserSession |
|
53 | + */ |
|
54 | + protected $userSession; |
|
55 | + /** |
|
56 | + * @var IRootFolder |
|
57 | + */ |
|
58 | + protected $rootFolder; |
|
59 | + |
|
60 | + /** |
|
61 | + * @param ICommentsManager $commentsManager |
|
62 | + * @param IUserManager $userManager |
|
63 | + * @param IUserSession $userSession |
|
64 | + * @param IRootFolder $rootFolder |
|
65 | + * @param ILogger $logger |
|
66 | + */ |
|
67 | + public function __construct( |
|
68 | + ICommentsManager $commentsManager, |
|
69 | + IUserManager $userManager, |
|
70 | + IUserSession $userSession, |
|
71 | + IRootFolder $rootFolder, |
|
72 | + ILogger $logger) |
|
73 | + { |
|
74 | + $this->commentsManager = $commentsManager; |
|
75 | + $this->logger = $logger; |
|
76 | + $this->userManager = $userManager; |
|
77 | + $this->userSession = $userSession; |
|
78 | + $this->rootFolder = $rootFolder; |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * initializes the collection. At this point of time, we need the logged in |
|
83 | + * user. Since it is not the case when the instance is created, we cannot |
|
84 | + * have this in the constructor. |
|
85 | + * |
|
86 | + * @throws NotAuthenticated |
|
87 | + */ |
|
88 | + protected function initCollections() { |
|
89 | + if(!empty($this->entityTypeCollections)) { |
|
90 | + return; |
|
91 | + } |
|
92 | + $user = $this->userSession->getUser(); |
|
93 | + if(is_null($user)) { |
|
94 | + throw new NotAuthenticated(); |
|
95 | + } |
|
96 | + $userFolder = $this->rootFolder->getUserFolder($user->getUID()); |
|
97 | + $this->entityTypeCollections['files'] = new EntityTypeCollection( |
|
98 | + 'files', |
|
99 | + $this->commentsManager, |
|
100 | + $userFolder, |
|
101 | + $this->userManager, |
|
102 | + $this->userSession, |
|
103 | + $this->logger |
|
104 | + ); |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * Creates a new file in the directory |
|
109 | + * |
|
110 | + * @param string $name Name of the file |
|
111 | + * @param resource|string $data Initial payload |
|
112 | + * @return null|string |
|
113 | + * @throws Forbidden |
|
114 | + */ |
|
115 | + function createFile($name, $data = null) { |
|
116 | + throw new Forbidden('Cannot create comments by id'); |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * Creates a new subdirectory |
|
121 | + * |
|
122 | + * @param string $name |
|
123 | + * @throws Forbidden |
|
124 | + */ |
|
125 | + function createDirectory($name) { |
|
126 | + throw new Forbidden('Permission denied to create collections'); |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Returns a specific child node, referenced by its name |
|
131 | + * |
|
132 | + * This method must throw Sabre\DAV\Exception\NotFound if the node does not |
|
133 | + * exist. |
|
134 | + * |
|
135 | + * @param string $name |
|
136 | + * @return \Sabre\DAV\INode |
|
137 | + * @throws NotFound |
|
138 | + */ |
|
139 | + function getChild($name) { |
|
140 | + $this->initCollections(); |
|
141 | + if(isset($this->entityTypeCollections[$name])) { |
|
142 | + return $this->entityTypeCollections[$name]; |
|
143 | + } |
|
144 | + throw new NotFound('Entity type "' . $name . '" not found."'); |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * Returns an array with all the child nodes |
|
149 | + * |
|
150 | + * @return \Sabre\DAV\INode[] |
|
151 | + */ |
|
152 | + function getChildren() { |
|
153 | + $this->initCollections(); |
|
154 | + return $this->entityTypeCollections; |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * Checks if a child-node with the specified name exists |
|
159 | + * |
|
160 | + * @param string $name |
|
161 | + * @return bool |
|
162 | + */ |
|
163 | + function childExists($name) { |
|
164 | + $this->initCollections(); |
|
165 | + return isset($this->entityTypeCollections[$name]); |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * Deleted the current node |
|
170 | + * |
|
171 | + * @throws Forbidden |
|
172 | + */ |
|
173 | + function delete() { |
|
174 | + throw new Forbidden('Permission denied to delete this collection'); |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * Returns the name of the node. |
|
179 | + * |
|
180 | + * This is used to generate the url. |
|
181 | + * |
|
182 | + * @return string |
|
183 | + */ |
|
184 | + function getName() { |
|
185 | + return $this->name; |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * Renames the node |
|
190 | + * |
|
191 | + * @param string $name The new name |
|
192 | + * @throws Forbidden |
|
193 | + */ |
|
194 | + function setName($name) { |
|
195 | + throw new Forbidden('Permission denied to rename this collection'); |
|
196 | + } |
|
197 | + |
|
198 | + /** |
|
199 | + * Returns the last modification time, as a unix timestamp |
|
200 | + * |
|
201 | + * @return int |
|
202 | + */ |
|
203 | + function getLastModified() { |
|
204 | + return null; |
|
205 | + } |
|
206 | 206 | } |
@@ -64,6 +64,9 @@ |
||
64 | 64 | ); |
65 | 65 | } |
66 | 66 | |
67 | + /** |
|
68 | + * @param boolean $toV2 |
|
69 | + */ |
|
67 | 70 | private function convertPrincipal($principal, $toV2) { |
68 | 71 | list(, $name) = URLUtil::splitPath($principal); |
69 | 72 | if ($toV2) { |
@@ -32,56 +32,56 @@ |
||
32 | 32 | |
33 | 33 | class LegacyDAVACL extends DavAclPlugin { |
34 | 34 | |
35 | - /** |
|
36 | - * Converts the v1 principal `principal/<username>` to the new v2 |
|
37 | - * `principal/users/<username>` which is required for permission checks |
|
38 | - * |
|
39 | - * @inheritdoc |
|
40 | - */ |
|
41 | - function getCurrentUserPrincipal() { |
|
42 | - $principalV1 = parent::getCurrentUserPrincipal(); |
|
43 | - if (is_null($principalV1)) { |
|
44 | - return $principalV1; |
|
45 | - } |
|
46 | - return $this->convertPrincipal($principalV1, true); |
|
47 | - } |
|
35 | + /** |
|
36 | + * Converts the v1 principal `principal/<username>` to the new v2 |
|
37 | + * `principal/users/<username>` which is required for permission checks |
|
38 | + * |
|
39 | + * @inheritdoc |
|
40 | + */ |
|
41 | + function getCurrentUserPrincipal() { |
|
42 | + $principalV1 = parent::getCurrentUserPrincipal(); |
|
43 | + if (is_null($principalV1)) { |
|
44 | + return $principalV1; |
|
45 | + } |
|
46 | + return $this->convertPrincipal($principalV1, true); |
|
47 | + } |
|
48 | 48 | |
49 | 49 | |
50 | - /** |
|
51 | - * @inheritdoc |
|
52 | - */ |
|
53 | - function getCurrentUserPrincipals() { |
|
54 | - $principalV2 = $this->getCurrentUserPrincipal(); |
|
50 | + /** |
|
51 | + * @inheritdoc |
|
52 | + */ |
|
53 | + function getCurrentUserPrincipals() { |
|
54 | + $principalV2 = $this->getCurrentUserPrincipal(); |
|
55 | 55 | |
56 | - if (is_null($principalV2)) return []; |
|
56 | + if (is_null($principalV2)) return []; |
|
57 | 57 | |
58 | - $principalV1 = $this->convertPrincipal($principalV2, false); |
|
59 | - return array_merge( |
|
60 | - [ |
|
61 | - $principalV2, |
|
62 | - $principalV1 |
|
63 | - ], |
|
64 | - $this->getPrincipalMembership($principalV1) |
|
65 | - ); |
|
66 | - } |
|
58 | + $principalV1 = $this->convertPrincipal($principalV2, false); |
|
59 | + return array_merge( |
|
60 | + [ |
|
61 | + $principalV2, |
|
62 | + $principalV1 |
|
63 | + ], |
|
64 | + $this->getPrincipalMembership($principalV1) |
|
65 | + ); |
|
66 | + } |
|
67 | 67 | |
68 | - private function convertPrincipal($principal, $toV2) { |
|
69 | - list(, $name) = URLUtil::splitPath($principal); |
|
70 | - if ($toV2) { |
|
71 | - return "principals/users/$name"; |
|
72 | - } |
|
73 | - return "principals/$name"; |
|
74 | - } |
|
68 | + private function convertPrincipal($principal, $toV2) { |
|
69 | + list(, $name) = URLUtil::splitPath($principal); |
|
70 | + if ($toV2) { |
|
71 | + return "principals/users/$name"; |
|
72 | + } |
|
73 | + return "principals/$name"; |
|
74 | + } |
|
75 | 75 | |
76 | - function propFind(PropFind $propFind, INode $node) { |
|
77 | - /* Overload current-user-principal */ |
|
78 | - $propFind->handle('{DAV:}current-user-principal', function () { |
|
79 | - if ($url = parent::getCurrentUserPrincipal()) { |
|
80 | - return new Principal(Principal::HREF, $url . '/'); |
|
81 | - } else { |
|
82 | - return new Principal(Principal::UNAUTHENTICATED); |
|
83 | - } |
|
84 | - }); |
|
85 | - parent::propFind($propFind, $node); |
|
86 | - } |
|
76 | + function propFind(PropFind $propFind, INode $node) { |
|
77 | + /* Overload current-user-principal */ |
|
78 | + $propFind->handle('{DAV:}current-user-principal', function () { |
|
79 | + if ($url = parent::getCurrentUserPrincipal()) { |
|
80 | + return new Principal(Principal::HREF, $url . '/'); |
|
81 | + } else { |
|
82 | + return new Principal(Principal::UNAUTHENTICATED); |
|
83 | + } |
|
84 | + }); |
|
85 | + parent::propFind($propFind, $node); |
|
86 | + } |
|
87 | 87 | } |
@@ -95,7 +95,7 @@ |
||
95 | 95 | * returns a reference to the comments node |
96 | 96 | * |
97 | 97 | * @param Node $node |
98 | - * @return mixed|string |
|
98 | + * @return null|string |
|
99 | 99 | */ |
100 | 100 | public function getCommentsLink(Node $node) { |
101 | 101 | $href = $this->server->getBaseUri(); |
@@ -29,103 +29,103 @@ |
||
29 | 29 | |
30 | 30 | class CommentPropertiesPlugin extends ServerPlugin { |
31 | 31 | |
32 | - const PROPERTY_NAME_HREF = '{http://owncloud.org/ns}comments-href'; |
|
33 | - const PROPERTY_NAME_COUNT = '{http://owncloud.org/ns}comments-count'; |
|
34 | - const PROPERTY_NAME_UNREAD = '{http://owncloud.org/ns}comments-unread'; |
|
35 | - |
|
36 | - /** @var \Sabre\DAV\Server */ |
|
37 | - protected $server; |
|
38 | - |
|
39 | - /** @var ICommentsManager */ |
|
40 | - private $commentsManager; |
|
41 | - |
|
42 | - /** @var IUserSession */ |
|
43 | - private $userSession; |
|
44 | - |
|
45 | - public function __construct(ICommentsManager $commentsManager, IUserSession $userSession) { |
|
46 | - $this->commentsManager = $commentsManager; |
|
47 | - $this->userSession = $userSession; |
|
48 | - } |
|
49 | - |
|
50 | - /** |
|
51 | - * This initializes the plugin. |
|
52 | - * |
|
53 | - * This function is called by Sabre\DAV\Server, after |
|
54 | - * addPlugin is called. |
|
55 | - * |
|
56 | - * This method should set up the required event subscriptions. |
|
57 | - * |
|
58 | - * @param \Sabre\DAV\Server $server |
|
59 | - * @return void |
|
60 | - */ |
|
61 | - function initialize(\Sabre\DAV\Server $server) { |
|
62 | - $this->server = $server; |
|
63 | - $this->server->on('propFind', array($this, 'handleGetProperties')); |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * Adds tags and favorites properties to the response, |
|
68 | - * if requested. |
|
69 | - * |
|
70 | - * @param PropFind $propFind |
|
71 | - * @param \Sabre\DAV\INode $node |
|
72 | - * @return void |
|
73 | - */ |
|
74 | - public function handleGetProperties( |
|
75 | - PropFind $propFind, |
|
76 | - \Sabre\DAV\INode $node |
|
77 | - ) { |
|
78 | - if (!($node instanceof File) && !($node instanceof Directory)) { |
|
79 | - return; |
|
80 | - } |
|
81 | - |
|
82 | - $propFind->handle(self::PROPERTY_NAME_COUNT, function() use ($node) { |
|
83 | - return $this->commentsManager->getNumberOfCommentsForObject('files', strval($node->getId())); |
|
84 | - }); |
|
85 | - |
|
86 | - $propFind->handle(self::PROPERTY_NAME_HREF, function() use ($node) { |
|
87 | - return $this->getCommentsLink($node); |
|
88 | - }); |
|
89 | - |
|
90 | - $propFind->handle(self::PROPERTY_NAME_UNREAD, function() use ($node) { |
|
91 | - return $this->getUnreadCount($node); |
|
92 | - }); |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * returns a reference to the comments node |
|
97 | - * |
|
98 | - * @param Node $node |
|
99 | - * @return mixed|string |
|
100 | - */ |
|
101 | - public function getCommentsLink(Node $node) { |
|
102 | - $href = $this->server->getBaseUri(); |
|
103 | - $entryPoint = strrpos($href, '/webdav/'); |
|
104 | - if($entryPoint === false) { |
|
105 | - // in case we end up somewhere else, unexpectedly. |
|
106 | - return null; |
|
107 | - } |
|
108 | - $href = substr_replace($href, '/dav/', $entryPoint); |
|
109 | - $href .= 'comments/files/' . rawurldecode($node->getId()); |
|
110 | - return $href; |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * returns the number of unread comments for the currently logged in user |
|
115 | - * on the given file or directory node |
|
116 | - * |
|
117 | - * @param Node $node |
|
118 | - * @return Int|null |
|
119 | - */ |
|
120 | - public function getUnreadCount(Node $node) { |
|
121 | - $user = $this->userSession->getUser(); |
|
122 | - if(is_null($user)) { |
|
123 | - return null; |
|
124 | - } |
|
125 | - |
|
126 | - $lastRead = $this->commentsManager->getReadMark('files', strval($node->getId()), $user); |
|
127 | - |
|
128 | - return $this->commentsManager->getNumberOfCommentsForObject('files', strval($node->getId()), $lastRead); |
|
129 | - } |
|
32 | + const PROPERTY_NAME_HREF = '{http://owncloud.org/ns}comments-href'; |
|
33 | + const PROPERTY_NAME_COUNT = '{http://owncloud.org/ns}comments-count'; |
|
34 | + const PROPERTY_NAME_UNREAD = '{http://owncloud.org/ns}comments-unread'; |
|
35 | + |
|
36 | + /** @var \Sabre\DAV\Server */ |
|
37 | + protected $server; |
|
38 | + |
|
39 | + /** @var ICommentsManager */ |
|
40 | + private $commentsManager; |
|
41 | + |
|
42 | + /** @var IUserSession */ |
|
43 | + private $userSession; |
|
44 | + |
|
45 | + public function __construct(ICommentsManager $commentsManager, IUserSession $userSession) { |
|
46 | + $this->commentsManager = $commentsManager; |
|
47 | + $this->userSession = $userSession; |
|
48 | + } |
|
49 | + |
|
50 | + /** |
|
51 | + * This initializes the plugin. |
|
52 | + * |
|
53 | + * This function is called by Sabre\DAV\Server, after |
|
54 | + * addPlugin is called. |
|
55 | + * |
|
56 | + * This method should set up the required event subscriptions. |
|
57 | + * |
|
58 | + * @param \Sabre\DAV\Server $server |
|
59 | + * @return void |
|
60 | + */ |
|
61 | + function initialize(\Sabre\DAV\Server $server) { |
|
62 | + $this->server = $server; |
|
63 | + $this->server->on('propFind', array($this, 'handleGetProperties')); |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * Adds tags and favorites properties to the response, |
|
68 | + * if requested. |
|
69 | + * |
|
70 | + * @param PropFind $propFind |
|
71 | + * @param \Sabre\DAV\INode $node |
|
72 | + * @return void |
|
73 | + */ |
|
74 | + public function handleGetProperties( |
|
75 | + PropFind $propFind, |
|
76 | + \Sabre\DAV\INode $node |
|
77 | + ) { |
|
78 | + if (!($node instanceof File) && !($node instanceof Directory)) { |
|
79 | + return; |
|
80 | + } |
|
81 | + |
|
82 | + $propFind->handle(self::PROPERTY_NAME_COUNT, function() use ($node) { |
|
83 | + return $this->commentsManager->getNumberOfCommentsForObject('files', strval($node->getId())); |
|
84 | + }); |
|
85 | + |
|
86 | + $propFind->handle(self::PROPERTY_NAME_HREF, function() use ($node) { |
|
87 | + return $this->getCommentsLink($node); |
|
88 | + }); |
|
89 | + |
|
90 | + $propFind->handle(self::PROPERTY_NAME_UNREAD, function() use ($node) { |
|
91 | + return $this->getUnreadCount($node); |
|
92 | + }); |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * returns a reference to the comments node |
|
97 | + * |
|
98 | + * @param Node $node |
|
99 | + * @return mixed|string |
|
100 | + */ |
|
101 | + public function getCommentsLink(Node $node) { |
|
102 | + $href = $this->server->getBaseUri(); |
|
103 | + $entryPoint = strrpos($href, '/webdav/'); |
|
104 | + if($entryPoint === false) { |
|
105 | + // in case we end up somewhere else, unexpectedly. |
|
106 | + return null; |
|
107 | + } |
|
108 | + $href = substr_replace($href, '/dav/', $entryPoint); |
|
109 | + $href .= 'comments/files/' . rawurldecode($node->getId()); |
|
110 | + return $href; |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * returns the number of unread comments for the currently logged in user |
|
115 | + * on the given file or directory node |
|
116 | + * |
|
117 | + * @param Node $node |
|
118 | + * @return Int|null |
|
119 | + */ |
|
120 | + public function getUnreadCount(Node $node) { |
|
121 | + $user = $this->userSession->getUser(); |
|
122 | + if(is_null($user)) { |
|
123 | + return null; |
|
124 | + } |
|
125 | + |
|
126 | + $lastRead = $this->commentsManager->getReadMark('files', strval($node->getId()), $user); |
|
127 | + |
|
128 | + return $this->commentsManager->getNumberOfCommentsForObject('files', strval($node->getId()), $lastRead); |
|
129 | + } |
|
130 | 130 | |
131 | 131 | } |
@@ -75,7 +75,7 @@ |
||
75 | 75 | private $cache = []; |
76 | 76 | |
77 | 77 | /** |
78 | - * @param Tree $tree node tree |
|
78 | + * @param ObjectTree $tree node tree |
|
79 | 79 | * @param IDBConnection $connection database connection |
80 | 80 | * @param IUser $user owner of the tree and properties |
81 | 81 | */ |
@@ -35,322 +35,322 @@ |
||
35 | 35 | |
36 | 36 | class CustomPropertiesBackend implements BackendInterface { |
37 | 37 | |
38 | - /** |
|
39 | - * Ignored properties |
|
40 | - * |
|
41 | - * @var array |
|
42 | - */ |
|
43 | - private $ignoredProperties = array( |
|
44 | - '{DAV:}getcontentlength', |
|
45 | - '{DAV:}getcontenttype', |
|
46 | - '{DAV:}getetag', |
|
47 | - '{DAV:}quota-used-bytes', |
|
48 | - '{DAV:}quota-available-bytes', |
|
49 | - '{DAV:}quota-available-bytes', |
|
50 | - '{http://owncloud.org/ns}permissions', |
|
51 | - '{http://owncloud.org/ns}downloadURL', |
|
52 | - '{http://owncloud.org/ns}dDC', |
|
53 | - '{http://owncloud.org/ns}size', |
|
54 | - ); |
|
55 | - |
|
56 | - /** |
|
57 | - * @var Tree |
|
58 | - */ |
|
59 | - private $tree; |
|
60 | - |
|
61 | - /** |
|
62 | - * @var IDBConnection |
|
63 | - */ |
|
64 | - private $connection; |
|
65 | - |
|
66 | - /** |
|
67 | - * @var IUser |
|
68 | - */ |
|
69 | - private $user; |
|
70 | - |
|
71 | - /** |
|
72 | - * Properties cache |
|
73 | - * |
|
74 | - * @var array |
|
75 | - */ |
|
76 | - private $cache = []; |
|
77 | - |
|
78 | - /** |
|
79 | - * @param Tree $tree node tree |
|
80 | - * @param IDBConnection $connection database connection |
|
81 | - * @param IUser $user owner of the tree and properties |
|
82 | - */ |
|
83 | - public function __construct( |
|
84 | - Tree $tree, |
|
85 | - IDBConnection $connection, |
|
86 | - IUser $user) { |
|
87 | - $this->tree = $tree; |
|
88 | - $this->connection = $connection; |
|
89 | - $this->user = $user->getUID(); |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * Fetches properties for a path. |
|
94 | - * |
|
95 | - * @param string $path |
|
96 | - * @param PropFind $propFind |
|
97 | - * @return void |
|
98 | - */ |
|
99 | - public function propFind($path, PropFind $propFind) { |
|
100 | - try { |
|
101 | - $node = $this->tree->getNodeForPath($path); |
|
102 | - if (!($node instanceof Node)) { |
|
103 | - return; |
|
104 | - } |
|
105 | - } catch (ServiceUnavailable $e) { |
|
106 | - // might happen for unavailable mount points, skip |
|
107 | - return; |
|
108 | - } catch (NotFound $e) { |
|
109 | - // in some rare (buggy) cases the node might not be found, |
|
110 | - // we catch the exception to prevent breaking the whole list with a 404 |
|
111 | - // (soft fail) |
|
112 | - \OC::$server->getLogger()->warning( |
|
113 | - 'Could not get node for path: \"' . $path . '\" : ' . $e->getMessage(), |
|
114 | - array('app' => 'files') |
|
115 | - ); |
|
116 | - return; |
|
117 | - } |
|
118 | - |
|
119 | - $requestedProps = $propFind->get404Properties(); |
|
120 | - |
|
121 | - // these might appear |
|
122 | - $requestedProps = array_diff( |
|
123 | - $requestedProps, |
|
124 | - $this->ignoredProperties |
|
125 | - ); |
|
126 | - |
|
127 | - if (empty($requestedProps)) { |
|
128 | - return; |
|
129 | - } |
|
130 | - |
|
131 | - if ($node instanceof Directory |
|
132 | - && $propFind->getDepth() !== 0 |
|
133 | - ) { |
|
134 | - // note: pre-fetching only supported for depth <= 1 |
|
135 | - $this->loadChildrenProperties($node, $requestedProps); |
|
136 | - } |
|
137 | - |
|
138 | - $props = $this->getProperties($node, $requestedProps); |
|
139 | - foreach ($props as $propName => $propValue) { |
|
140 | - $propFind->set($propName, $propValue); |
|
141 | - } |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * Updates properties for a path |
|
146 | - * |
|
147 | - * @param string $path |
|
148 | - * @param PropPatch $propPatch |
|
149 | - * |
|
150 | - * @return void |
|
151 | - */ |
|
152 | - public function propPatch($path, PropPatch $propPatch) { |
|
153 | - $node = $this->tree->getNodeForPath($path); |
|
154 | - if (!($node instanceof Node)) { |
|
155 | - return; |
|
156 | - } |
|
157 | - |
|
158 | - $propPatch->handleRemaining(function($changedProps) use ($node) { |
|
159 | - return $this->updateProperties($node, $changedProps); |
|
160 | - }); |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * This method is called after a node is deleted. |
|
165 | - * |
|
166 | - * @param string $path path of node for which to delete properties |
|
167 | - */ |
|
168 | - public function delete($path) { |
|
169 | - $statement = $this->connection->prepare( |
|
170 | - 'DELETE FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` = ?' |
|
171 | - ); |
|
172 | - $statement->execute(array($this->user, '/' . $path)); |
|
173 | - $statement->closeCursor(); |
|
174 | - |
|
175 | - unset($this->cache[$path]); |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * This method is called after a successful MOVE |
|
180 | - * |
|
181 | - * @param string $source |
|
182 | - * @param string $destination |
|
183 | - * |
|
184 | - * @return void |
|
185 | - */ |
|
186 | - public function move($source, $destination) { |
|
187 | - $statement = $this->connection->prepare( |
|
188 | - 'UPDATE `*PREFIX*properties` SET `propertypath` = ?' . |
|
189 | - ' WHERE `userid` = ? AND `propertypath` = ?' |
|
190 | - ); |
|
191 | - $statement->execute(array('/' . $destination, $this->user, '/' . $source)); |
|
192 | - $statement->closeCursor(); |
|
193 | - } |
|
194 | - |
|
195 | - /** |
|
196 | - * Returns a list of properties for this nodes.; |
|
197 | - * @param Node $node |
|
198 | - * @param array $requestedProperties requested properties or empty array for "all" |
|
199 | - * @return array |
|
200 | - * @note The properties list is a list of propertynames the client |
|
201 | - * requested, encoded as xmlnamespace#tagName, for example: |
|
202 | - * http://www.example.org/namespace#author If the array is empty, all |
|
203 | - * properties should be returned |
|
204 | - */ |
|
205 | - private function getProperties(Node $node, array $requestedProperties) { |
|
206 | - $path = $node->getPath(); |
|
207 | - if (isset($this->cache[$path])) { |
|
208 | - return $this->cache[$path]; |
|
209 | - } |
|
210 | - |
|
211 | - // TODO: chunking if more than 1000 properties |
|
212 | - $sql = 'SELECT * FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` = ?'; |
|
213 | - |
|
214 | - $whereValues = array($this->user, $path); |
|
215 | - $whereTypes = array(null, null); |
|
216 | - |
|
217 | - if (!empty($requestedProperties)) { |
|
218 | - // request only a subset |
|
219 | - $sql .= ' AND `propertyname` in (?)'; |
|
220 | - $whereValues[] = $requestedProperties; |
|
221 | - $whereTypes[] = \Doctrine\DBAL\Connection::PARAM_STR_ARRAY; |
|
222 | - } |
|
223 | - |
|
224 | - $result = $this->connection->executeQuery( |
|
225 | - $sql, |
|
226 | - $whereValues, |
|
227 | - $whereTypes |
|
228 | - ); |
|
229 | - |
|
230 | - $props = []; |
|
231 | - while ($row = $result->fetch()) { |
|
232 | - $props[$row['propertyname']] = $row['propertyvalue']; |
|
233 | - } |
|
234 | - |
|
235 | - $result->closeCursor(); |
|
236 | - |
|
237 | - $this->cache[$path] = $props; |
|
238 | - return $props; |
|
239 | - } |
|
240 | - |
|
241 | - /** |
|
242 | - * Update properties |
|
243 | - * |
|
244 | - * @param Node $node node for which to update properties |
|
245 | - * @param array $properties array of properties to update |
|
246 | - * |
|
247 | - * @return bool |
|
248 | - */ |
|
249 | - private function updateProperties($node, $properties) { |
|
250 | - $path = $node->getPath(); |
|
251 | - |
|
252 | - $deleteStatement = 'DELETE FROM `*PREFIX*properties`' . |
|
253 | - ' WHERE `userid` = ? AND `propertypath` = ? AND `propertyname` = ?'; |
|
254 | - |
|
255 | - $insertStatement = 'INSERT INTO `*PREFIX*properties`' . |
|
256 | - ' (`userid`,`propertypath`,`propertyname`,`propertyvalue`) VALUES(?,?,?,?)'; |
|
257 | - |
|
258 | - $updateStatement = 'UPDATE `*PREFIX*properties` SET `propertyvalue` = ?' . |
|
259 | - ' WHERE `userid` = ? AND `propertypath` = ? AND `propertyname` = ?'; |
|
260 | - |
|
261 | - // TODO: use "insert or update" strategy ? |
|
262 | - $existing = $this->getProperties($node, array()); |
|
263 | - $this->connection->beginTransaction(); |
|
264 | - foreach ($properties as $propertyName => $propertyValue) { |
|
265 | - // If it was null, we need to delete the property |
|
266 | - if (is_null($propertyValue)) { |
|
267 | - if (array_key_exists($propertyName, $existing)) { |
|
268 | - $this->connection->executeUpdate($deleteStatement, |
|
269 | - array( |
|
270 | - $this->user, |
|
271 | - $path, |
|
272 | - $propertyName |
|
273 | - ) |
|
274 | - ); |
|
275 | - } |
|
276 | - } else { |
|
277 | - if (!array_key_exists($propertyName, $existing)) { |
|
278 | - $this->connection->executeUpdate($insertStatement, |
|
279 | - array( |
|
280 | - $this->user, |
|
281 | - $path, |
|
282 | - $propertyName, |
|
283 | - $propertyValue |
|
284 | - ) |
|
285 | - ); |
|
286 | - } else { |
|
287 | - $this->connection->executeUpdate($updateStatement, |
|
288 | - array( |
|
289 | - $propertyValue, |
|
290 | - $this->user, |
|
291 | - $path, |
|
292 | - $propertyName |
|
293 | - ) |
|
294 | - ); |
|
295 | - } |
|
296 | - } |
|
297 | - } |
|
298 | - |
|
299 | - $this->connection->commit(); |
|
300 | - unset($this->cache[$path]); |
|
301 | - |
|
302 | - return true; |
|
303 | - } |
|
304 | - |
|
305 | - /** |
|
306 | - * Bulk load properties for directory children |
|
307 | - * |
|
308 | - * @param Directory $node |
|
309 | - * @param array $requestedProperties requested properties |
|
310 | - * |
|
311 | - * @return void |
|
312 | - */ |
|
313 | - private function loadChildrenProperties(Directory $node, $requestedProperties) { |
|
314 | - $path = $node->getPath(); |
|
315 | - if (isset($this->cache[$path])) { |
|
316 | - // we already loaded them at some point |
|
317 | - return; |
|
318 | - } |
|
319 | - |
|
320 | - $childNodes = $node->getChildren(); |
|
321 | - // pre-fill cache |
|
322 | - foreach ($childNodes as $childNode) { |
|
323 | - $this->cache[$childNode->getPath()] = []; |
|
324 | - } |
|
325 | - |
|
326 | - $sql = 'SELECT * FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` LIKE ?'; |
|
327 | - $sql .= ' AND `propertyname` in (?) ORDER BY `propertypath`, `propertyname`'; |
|
328 | - |
|
329 | - $result = $this->connection->executeQuery( |
|
330 | - $sql, |
|
331 | - array($this->user, rtrim($path, '/') . '/%', $requestedProperties), |
|
332 | - array(null, null, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY) |
|
333 | - ); |
|
334 | - |
|
335 | - $oldPath = null; |
|
336 | - $props = []; |
|
337 | - while ($row = $result->fetch()) { |
|
338 | - $path = $row['propertypath']; |
|
339 | - if ($oldPath !== $path) { |
|
340 | - // save previously gathered props |
|
341 | - $this->cache[$oldPath] = $props; |
|
342 | - $oldPath = $path; |
|
343 | - // prepare props for next path |
|
344 | - $props = []; |
|
345 | - } |
|
346 | - $props[$row['propertyname']] = $row['propertyvalue']; |
|
347 | - } |
|
348 | - if (!is_null($oldPath)) { |
|
349 | - // save props from last run |
|
350 | - $this->cache[$oldPath] = $props; |
|
351 | - } |
|
352 | - |
|
353 | - $result->closeCursor(); |
|
354 | - } |
|
38 | + /** |
|
39 | + * Ignored properties |
|
40 | + * |
|
41 | + * @var array |
|
42 | + */ |
|
43 | + private $ignoredProperties = array( |
|
44 | + '{DAV:}getcontentlength', |
|
45 | + '{DAV:}getcontenttype', |
|
46 | + '{DAV:}getetag', |
|
47 | + '{DAV:}quota-used-bytes', |
|
48 | + '{DAV:}quota-available-bytes', |
|
49 | + '{DAV:}quota-available-bytes', |
|
50 | + '{http://owncloud.org/ns}permissions', |
|
51 | + '{http://owncloud.org/ns}downloadURL', |
|
52 | + '{http://owncloud.org/ns}dDC', |
|
53 | + '{http://owncloud.org/ns}size', |
|
54 | + ); |
|
55 | + |
|
56 | + /** |
|
57 | + * @var Tree |
|
58 | + */ |
|
59 | + private $tree; |
|
60 | + |
|
61 | + /** |
|
62 | + * @var IDBConnection |
|
63 | + */ |
|
64 | + private $connection; |
|
65 | + |
|
66 | + /** |
|
67 | + * @var IUser |
|
68 | + */ |
|
69 | + private $user; |
|
70 | + |
|
71 | + /** |
|
72 | + * Properties cache |
|
73 | + * |
|
74 | + * @var array |
|
75 | + */ |
|
76 | + private $cache = []; |
|
77 | + |
|
78 | + /** |
|
79 | + * @param Tree $tree node tree |
|
80 | + * @param IDBConnection $connection database connection |
|
81 | + * @param IUser $user owner of the tree and properties |
|
82 | + */ |
|
83 | + public function __construct( |
|
84 | + Tree $tree, |
|
85 | + IDBConnection $connection, |
|
86 | + IUser $user) { |
|
87 | + $this->tree = $tree; |
|
88 | + $this->connection = $connection; |
|
89 | + $this->user = $user->getUID(); |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * Fetches properties for a path. |
|
94 | + * |
|
95 | + * @param string $path |
|
96 | + * @param PropFind $propFind |
|
97 | + * @return void |
|
98 | + */ |
|
99 | + public function propFind($path, PropFind $propFind) { |
|
100 | + try { |
|
101 | + $node = $this->tree->getNodeForPath($path); |
|
102 | + if (!($node instanceof Node)) { |
|
103 | + return; |
|
104 | + } |
|
105 | + } catch (ServiceUnavailable $e) { |
|
106 | + // might happen for unavailable mount points, skip |
|
107 | + return; |
|
108 | + } catch (NotFound $e) { |
|
109 | + // in some rare (buggy) cases the node might not be found, |
|
110 | + // we catch the exception to prevent breaking the whole list with a 404 |
|
111 | + // (soft fail) |
|
112 | + \OC::$server->getLogger()->warning( |
|
113 | + 'Could not get node for path: \"' . $path . '\" : ' . $e->getMessage(), |
|
114 | + array('app' => 'files') |
|
115 | + ); |
|
116 | + return; |
|
117 | + } |
|
118 | + |
|
119 | + $requestedProps = $propFind->get404Properties(); |
|
120 | + |
|
121 | + // these might appear |
|
122 | + $requestedProps = array_diff( |
|
123 | + $requestedProps, |
|
124 | + $this->ignoredProperties |
|
125 | + ); |
|
126 | + |
|
127 | + if (empty($requestedProps)) { |
|
128 | + return; |
|
129 | + } |
|
130 | + |
|
131 | + if ($node instanceof Directory |
|
132 | + && $propFind->getDepth() !== 0 |
|
133 | + ) { |
|
134 | + // note: pre-fetching only supported for depth <= 1 |
|
135 | + $this->loadChildrenProperties($node, $requestedProps); |
|
136 | + } |
|
137 | + |
|
138 | + $props = $this->getProperties($node, $requestedProps); |
|
139 | + foreach ($props as $propName => $propValue) { |
|
140 | + $propFind->set($propName, $propValue); |
|
141 | + } |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * Updates properties for a path |
|
146 | + * |
|
147 | + * @param string $path |
|
148 | + * @param PropPatch $propPatch |
|
149 | + * |
|
150 | + * @return void |
|
151 | + */ |
|
152 | + public function propPatch($path, PropPatch $propPatch) { |
|
153 | + $node = $this->tree->getNodeForPath($path); |
|
154 | + if (!($node instanceof Node)) { |
|
155 | + return; |
|
156 | + } |
|
157 | + |
|
158 | + $propPatch->handleRemaining(function($changedProps) use ($node) { |
|
159 | + return $this->updateProperties($node, $changedProps); |
|
160 | + }); |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * This method is called after a node is deleted. |
|
165 | + * |
|
166 | + * @param string $path path of node for which to delete properties |
|
167 | + */ |
|
168 | + public function delete($path) { |
|
169 | + $statement = $this->connection->prepare( |
|
170 | + 'DELETE FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` = ?' |
|
171 | + ); |
|
172 | + $statement->execute(array($this->user, '/' . $path)); |
|
173 | + $statement->closeCursor(); |
|
174 | + |
|
175 | + unset($this->cache[$path]); |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * This method is called after a successful MOVE |
|
180 | + * |
|
181 | + * @param string $source |
|
182 | + * @param string $destination |
|
183 | + * |
|
184 | + * @return void |
|
185 | + */ |
|
186 | + public function move($source, $destination) { |
|
187 | + $statement = $this->connection->prepare( |
|
188 | + 'UPDATE `*PREFIX*properties` SET `propertypath` = ?' . |
|
189 | + ' WHERE `userid` = ? AND `propertypath` = ?' |
|
190 | + ); |
|
191 | + $statement->execute(array('/' . $destination, $this->user, '/' . $source)); |
|
192 | + $statement->closeCursor(); |
|
193 | + } |
|
194 | + |
|
195 | + /** |
|
196 | + * Returns a list of properties for this nodes.; |
|
197 | + * @param Node $node |
|
198 | + * @param array $requestedProperties requested properties or empty array for "all" |
|
199 | + * @return array |
|
200 | + * @note The properties list is a list of propertynames the client |
|
201 | + * requested, encoded as xmlnamespace#tagName, for example: |
|
202 | + * http://www.example.org/namespace#author If the array is empty, all |
|
203 | + * properties should be returned |
|
204 | + */ |
|
205 | + private function getProperties(Node $node, array $requestedProperties) { |
|
206 | + $path = $node->getPath(); |
|
207 | + if (isset($this->cache[$path])) { |
|
208 | + return $this->cache[$path]; |
|
209 | + } |
|
210 | + |
|
211 | + // TODO: chunking if more than 1000 properties |
|
212 | + $sql = 'SELECT * FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` = ?'; |
|
213 | + |
|
214 | + $whereValues = array($this->user, $path); |
|
215 | + $whereTypes = array(null, null); |
|
216 | + |
|
217 | + if (!empty($requestedProperties)) { |
|
218 | + // request only a subset |
|
219 | + $sql .= ' AND `propertyname` in (?)'; |
|
220 | + $whereValues[] = $requestedProperties; |
|
221 | + $whereTypes[] = \Doctrine\DBAL\Connection::PARAM_STR_ARRAY; |
|
222 | + } |
|
223 | + |
|
224 | + $result = $this->connection->executeQuery( |
|
225 | + $sql, |
|
226 | + $whereValues, |
|
227 | + $whereTypes |
|
228 | + ); |
|
229 | + |
|
230 | + $props = []; |
|
231 | + while ($row = $result->fetch()) { |
|
232 | + $props[$row['propertyname']] = $row['propertyvalue']; |
|
233 | + } |
|
234 | + |
|
235 | + $result->closeCursor(); |
|
236 | + |
|
237 | + $this->cache[$path] = $props; |
|
238 | + return $props; |
|
239 | + } |
|
240 | + |
|
241 | + /** |
|
242 | + * Update properties |
|
243 | + * |
|
244 | + * @param Node $node node for which to update properties |
|
245 | + * @param array $properties array of properties to update |
|
246 | + * |
|
247 | + * @return bool |
|
248 | + */ |
|
249 | + private function updateProperties($node, $properties) { |
|
250 | + $path = $node->getPath(); |
|
251 | + |
|
252 | + $deleteStatement = 'DELETE FROM `*PREFIX*properties`' . |
|
253 | + ' WHERE `userid` = ? AND `propertypath` = ? AND `propertyname` = ?'; |
|
254 | + |
|
255 | + $insertStatement = 'INSERT INTO `*PREFIX*properties`' . |
|
256 | + ' (`userid`,`propertypath`,`propertyname`,`propertyvalue`) VALUES(?,?,?,?)'; |
|
257 | + |
|
258 | + $updateStatement = 'UPDATE `*PREFIX*properties` SET `propertyvalue` = ?' . |
|
259 | + ' WHERE `userid` = ? AND `propertypath` = ? AND `propertyname` = ?'; |
|
260 | + |
|
261 | + // TODO: use "insert or update" strategy ? |
|
262 | + $existing = $this->getProperties($node, array()); |
|
263 | + $this->connection->beginTransaction(); |
|
264 | + foreach ($properties as $propertyName => $propertyValue) { |
|
265 | + // If it was null, we need to delete the property |
|
266 | + if (is_null($propertyValue)) { |
|
267 | + if (array_key_exists($propertyName, $existing)) { |
|
268 | + $this->connection->executeUpdate($deleteStatement, |
|
269 | + array( |
|
270 | + $this->user, |
|
271 | + $path, |
|
272 | + $propertyName |
|
273 | + ) |
|
274 | + ); |
|
275 | + } |
|
276 | + } else { |
|
277 | + if (!array_key_exists($propertyName, $existing)) { |
|
278 | + $this->connection->executeUpdate($insertStatement, |
|
279 | + array( |
|
280 | + $this->user, |
|
281 | + $path, |
|
282 | + $propertyName, |
|
283 | + $propertyValue |
|
284 | + ) |
|
285 | + ); |
|
286 | + } else { |
|
287 | + $this->connection->executeUpdate($updateStatement, |
|
288 | + array( |
|
289 | + $propertyValue, |
|
290 | + $this->user, |
|
291 | + $path, |
|
292 | + $propertyName |
|
293 | + ) |
|
294 | + ); |
|
295 | + } |
|
296 | + } |
|
297 | + } |
|
298 | + |
|
299 | + $this->connection->commit(); |
|
300 | + unset($this->cache[$path]); |
|
301 | + |
|
302 | + return true; |
|
303 | + } |
|
304 | + |
|
305 | + /** |
|
306 | + * Bulk load properties for directory children |
|
307 | + * |
|
308 | + * @param Directory $node |
|
309 | + * @param array $requestedProperties requested properties |
|
310 | + * |
|
311 | + * @return void |
|
312 | + */ |
|
313 | + private function loadChildrenProperties(Directory $node, $requestedProperties) { |
|
314 | + $path = $node->getPath(); |
|
315 | + if (isset($this->cache[$path])) { |
|
316 | + // we already loaded them at some point |
|
317 | + return; |
|
318 | + } |
|
319 | + |
|
320 | + $childNodes = $node->getChildren(); |
|
321 | + // pre-fill cache |
|
322 | + foreach ($childNodes as $childNode) { |
|
323 | + $this->cache[$childNode->getPath()] = []; |
|
324 | + } |
|
325 | + |
|
326 | + $sql = 'SELECT * FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` LIKE ?'; |
|
327 | + $sql .= ' AND `propertyname` in (?) ORDER BY `propertypath`, `propertyname`'; |
|
328 | + |
|
329 | + $result = $this->connection->executeQuery( |
|
330 | + $sql, |
|
331 | + array($this->user, rtrim($path, '/') . '/%', $requestedProperties), |
|
332 | + array(null, null, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY) |
|
333 | + ); |
|
334 | + |
|
335 | + $oldPath = null; |
|
336 | + $props = []; |
|
337 | + while ($row = $result->fetch()) { |
|
338 | + $path = $row['propertypath']; |
|
339 | + if ($oldPath !== $path) { |
|
340 | + // save previously gathered props |
|
341 | + $this->cache[$oldPath] = $props; |
|
342 | + $oldPath = $path; |
|
343 | + // prepare props for next path |
|
344 | + $props = []; |
|
345 | + } |
|
346 | + $props[$row['propertyname']] = $row['propertyvalue']; |
|
347 | + } |
|
348 | + if (!is_null($oldPath)) { |
|
349 | + // save props from last run |
|
350 | + $this->cache[$oldPath] = $props; |
|
351 | + } |
|
352 | + |
|
353 | + $result->closeCursor(); |
|
354 | + } |
|
355 | 355 | |
356 | 356 | } |