From 972a05bd563baf1df7717ae6c599d1f4ff9bbaa4 Mon Sep 17 00:00:00 2001 From: ralf Date: Fri, 10 May 2024 16:28:37 +0200 Subject: [PATCH] WIP vidoteach/smallpart REST API --- api/src/CalDAV.php | 2 +- api/src/CalDAV/Handler.php | 18 +++++++++++++++--- api/src/CalDAV/JsBase.php | 14 ++++++++++++++ api/src/CalDAV/JsCalendar.php | 14 -------------- 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/api/src/CalDAV.php b/api/src/CalDAV.php index e8d66524af..42127246c5 100644 --- a/api/src/CalDAV.php +++ b/api/src/CalDAV.php @@ -2067,7 +2067,7 @@ class CalDAV extends HTTP_WebDAV_Server if (is_bool($status)) $status = $status ? '204 No Content' : '400 Something went wrong'; // check/handle Prefer: return-representation - if ($status[0] === '2' || $status === true) + if (((string)$status)[0] === '2' || $status === true) { // we can NOT use 204 No content (forbids a body) with return=representation, therefore we need to use 200 Ok instead! if ($handler->check_return_representation($options, $id, $user) && (int)$status == 204) diff --git a/api/src/CalDAV/Handler.php b/api/src/CalDAV/Handler.php index b2cfd19c60..95286b34ea 100644 --- a/api/src/CalDAV/Handler.php +++ b/api/src/CalDAV/Handler.php @@ -156,7 +156,7 @@ abstract class Handler abstract function propfind($path,&$options,&$files,$user); /** - * Propfind callback, if interator is used + * Propfind callback, if iterator is used * * @param string $path * @param array &$filter @@ -381,8 +381,13 @@ abstract class Handler //error_log(__METHOD__."(, $id, $user) start ".function_backtrace()); if (isset($_SERVER['HTTP_PREFER']) && in_array('return=representation', preg_split('/, ?/', $_SERVER['HTTP_PREFER']))) { - if ($_SERVER['REQUEST_METHOD'] == 'POST') + // fix path for POST request to collection + if ($_SERVER['REQUEST_METHOD'] === 'POST') { + if (!empty($this->new_id)) + { + $options['path'] = rtrim($options['path'], '/').'/'.$this->new_id; + } header('Content-Location: '.Api\Framework::getUrl($this->caldav->base_uri.$options['path'])); } @@ -390,7 +395,14 @@ abstract class Handler unset($_SERVER['HTTP_IF_MATCH']); unset($_SERVER['HTTP_IF_NONE_MATCH']); - if (($ret = $this->get($options, $id ? $id : $this->new_id, $user)) && !empty($options['data'])) + // add "Accept: application/json" for JSON and set request-method to GET + if (empty($_SERVER['HTTP_ACCEPT']) && Api\CalDAV::isJSON()) + { + $_SERVER['HTTP_ACCEPT'] = 'application/json'; + } + $_SERVER['REQUEST_METHOD'] = 'GET'; + + if (($ret = $this->get($options, $id ?: $this->new_id, $user)) && !empty($options['data'])) { if (!$this->caldav->use_compression()) header('Content-Length: '.$this->caldav->bytes($options['data'])); header('Content-Type: '.$options['mimetype']); diff --git a/api/src/CalDAV/JsBase.php b/api/src/CalDAV/JsBase.php index 5db49c2445..98a49e36b0 100644 --- a/api/src/CalDAV/JsBase.php +++ b/api/src/CalDAV/JsBase.php @@ -436,4 +436,18 @@ class JsBase throw new JsParseException("Error parsing $type attribute '$name': ". $e->getMessage(), 422, $e); } } + + /** + * Parse a DateTime value + * + * @param string $value + * @param string|null $timezone + * @param bool $showWithoutTime true: return H:i set to 00:00 + * @return Api\DateTime + * @throws Api\Exception + */ + protected static function parseDateTime(string $value, ?string $timezone=null, bool $showWithoutTime=false) + { + return new Api\DateTime($value, !empty($timezone) ? new \DateTimeZone($timezone) : null); + } } \ No newline at end of file diff --git a/api/src/CalDAV/JsCalendar.php b/api/src/CalDAV/JsCalendar.php index 506cfc10a8..c5595d741b 100644 --- a/api/src/CalDAV/JsCalendar.php +++ b/api/src/CalDAV/JsCalendar.php @@ -571,20 +571,6 @@ class JsCalendar extends JsBase return $duration; } - /** - * Parse a DateTime value - * - * @param string $value - * @param string|null $timezone - * @param bool $showWithoutTime true: return H:i set to 00:00 - * @return Api\DateTime - * @throws Api\Exception - */ - protected static function parseDateTime(string $value, ?string $timezone=null, bool $showWithoutTime=false) - { - return new Api\DateTime($value, !empty($timezone) ? new \DateTimeZone($timezone) : null); - } - protected static function parseStartDuration(array $data) { $parsed = [];