diff options
Diffstat (limited to 'application/api')
-rw-r--r-- | application/api/ApiUtils.php | 6 | ||||
-rw-r--r-- | application/api/controllers/Links.php | 19 |
2 files changed, 14 insertions, 11 deletions
diff --git a/application/api/ApiUtils.php b/application/api/ApiUtils.php index 4a6326f0..eb1ca9bc 100644 --- a/application/api/ApiUtils.php +++ b/application/api/ApiUtils.php | |||
@@ -89,12 +89,12 @@ class ApiUtils | |||
89 | * If no URL is provided, it will generate a local note URL. | 89 | * If no URL is provided, it will generate a local note URL. |
90 | * If no title is provided, it will use the URL as title. | 90 | * If no title is provided, it will use the URL as title. |
91 | * | 91 | * |
92 | * @param array $input Request Link. | 92 | * @param array|null $input Request Link. |
93 | * @param bool $defaultPrivate Request Link. | 93 | * @param bool $defaultPrivate Setting defined if a bookmark is private by default. |
94 | * | 94 | * |
95 | * @return Bookmark instance. | 95 | * @return Bookmark instance. |
96 | */ | 96 | */ |
97 | public static function buildBookmarkFromRequest($input, $defaultPrivate): Bookmark | 97 | public static function buildBookmarkFromRequest(?array $input, bool $defaultPrivate): Bookmark |
98 | { | 98 | { |
99 | $bookmark = new Bookmark(); | 99 | $bookmark = new Bookmark(); |
100 | $url = ! empty($input['url']) ? cleanup_url($input['url']) : ''; | 100 | $url = ! empty($input['url']) ? cleanup_url($input['url']) : ''; |
diff --git a/application/api/controllers/Links.php b/application/api/controllers/Links.php index 778097fd..73a1b84e 100644 --- a/application/api/controllers/Links.php +++ b/application/api/controllers/Links.php | |||
@@ -96,11 +96,12 @@ class Links extends ApiController | |||
96 | */ | 96 | */ |
97 | public function getLink($request, $response, $args) | 97 | public function getLink($request, $response, $args) |
98 | { | 98 | { |
99 | if (!$this->bookmarkService->exists($args['id'])) { | 99 | $id = is_integer_mixed($args['id']) ? (int) $args['id'] : null; |
100 | if ($id === null || ! $this->bookmarkService->exists($id)) { | ||
100 | throw new ApiLinkNotFoundException(); | 101 | throw new ApiLinkNotFoundException(); |
101 | } | 102 | } |
102 | $index = index_url($this->ci['environment']); | 103 | $index = index_url($this->ci['environment']); |
103 | $out = ApiUtils::formatLink($this->bookmarkService->get($args['id']), $index); | 104 | $out = ApiUtils::formatLink($this->bookmarkService->get($id), $index); |
104 | 105 | ||
105 | return $response->withJson($out, 200, $this->jsonStyle); | 106 | return $response->withJson($out, 200, $this->jsonStyle); |
106 | } | 107 | } |
@@ -115,7 +116,7 @@ class Links extends ApiController | |||
115 | */ | 116 | */ |
116 | public function postLink($request, $response) | 117 | public function postLink($request, $response) |
117 | { | 118 | { |
118 | $data = $request->getParsedBody(); | 119 | $data = (array) ($request->getParsedBody() ?? []); |
119 | $bookmark = ApiUtils::buildBookmarkFromRequest($data, $this->conf->get('privacy.default_private_links')); | 120 | $bookmark = ApiUtils::buildBookmarkFromRequest($data, $this->conf->get('privacy.default_private_links')); |
120 | // duplicate by URL, return 409 Conflict | 121 | // duplicate by URL, return 409 Conflict |
121 | if (! empty($bookmark->getUrl()) | 122 | if (! empty($bookmark->getUrl()) |
@@ -148,7 +149,8 @@ class Links extends ApiController | |||
148 | */ | 149 | */ |
149 | public function putLink($request, $response, $args) | 150 | public function putLink($request, $response, $args) |
150 | { | 151 | { |
151 | if (! $this->bookmarkService->exists($args['id'])) { | 152 | $id = is_integer_mixed($args['id']) ? (int) $args['id'] : null; |
153 | if ($id === null || !$this->bookmarkService->exists($id)) { | ||
152 | throw new ApiLinkNotFoundException(); | 154 | throw new ApiLinkNotFoundException(); |
153 | } | 155 | } |
154 | 156 | ||
@@ -159,7 +161,7 @@ class Links extends ApiController | |||
159 | // duplicate URL on a different link, return 409 Conflict | 161 | // duplicate URL on a different link, return 409 Conflict |
160 | if (! empty($requestBookmark->getUrl()) | 162 | if (! empty($requestBookmark->getUrl()) |
161 | && ! empty($dup = $this->bookmarkService->findByUrl($requestBookmark->getUrl())) | 163 | && ! empty($dup = $this->bookmarkService->findByUrl($requestBookmark->getUrl())) |
162 | && $dup->getId() != $args['id'] | 164 | && $dup->getId() != $id |
163 | ) { | 165 | ) { |
164 | return $response->withJson( | 166 | return $response->withJson( |
165 | ApiUtils::formatLink($dup, $index), | 167 | ApiUtils::formatLink($dup, $index), |
@@ -168,7 +170,7 @@ class Links extends ApiController | |||
168 | ); | 170 | ); |
169 | } | 171 | } |
170 | 172 | ||
171 | $responseBookmark = $this->bookmarkService->get($args['id']); | 173 | $responseBookmark = $this->bookmarkService->get($id); |
172 | $responseBookmark = ApiUtils::updateLink($responseBookmark, $requestBookmark); | 174 | $responseBookmark = ApiUtils::updateLink($responseBookmark, $requestBookmark); |
173 | $this->bookmarkService->set($responseBookmark); | 175 | $this->bookmarkService->set($responseBookmark); |
174 | 176 | ||
@@ -189,10 +191,11 @@ class Links extends ApiController | |||
189 | */ | 191 | */ |
190 | public function deleteLink($request, $response, $args) | 192 | public function deleteLink($request, $response, $args) |
191 | { | 193 | { |
192 | if (! $this->bookmarkService->exists($args['id'])) { | 194 | $id = is_integer_mixed($args['id']) ? (int) $args['id'] : null; |
195 | if ($id === null || !$this->bookmarkService->exists($id)) { | ||
193 | throw new ApiLinkNotFoundException(); | 196 | throw new ApiLinkNotFoundException(); |
194 | } | 197 | } |
195 | $bookmark = $this->bookmarkService->get($args['id']); | 198 | $bookmark = $this->bookmarkService->get($id); |
196 | $this->bookmarkService->remove($bookmark); | 199 | $this->bookmarkService->remove($bookmark); |
197 | 200 | ||
198 | return $response->withStatus(204); | 201 | return $response->withStatus(204); |