diff options
Diffstat (limited to 'application/api/controllers')
-rw-r--r-- | application/api/controllers/ApiController.php | 3 | ||||
-rw-r--r-- | application/api/controllers/Links.php | 23 |
2 files changed, 15 insertions, 11 deletions
diff --git a/application/api/controllers/ApiController.php b/application/api/controllers/ApiController.php index c4b3d0c3..88a845eb 100644 --- a/application/api/controllers/ApiController.php +++ b/application/api/controllers/ApiController.php | |||
@@ -4,6 +4,7 @@ namespace Shaarli\Api\Controllers; | |||
4 | 4 | ||
5 | use Shaarli\Bookmark\BookmarkServiceInterface; | 5 | use Shaarli\Bookmark\BookmarkServiceInterface; |
6 | use Shaarli\Config\ConfigManager; | 6 | use Shaarli\Config\ConfigManager; |
7 | use Shaarli\History; | ||
7 | use Slim\Container; | 8 | use Slim\Container; |
8 | 9 | ||
9 | /** | 10 | /** |
@@ -31,7 +32,7 @@ abstract class ApiController | |||
31 | protected $bookmarkService; | 32 | protected $bookmarkService; |
32 | 33 | ||
33 | /** | 34 | /** |
34 | * @var HistoryController | 35 | * @var History |
35 | */ | 36 | */ |
36 | protected $history; | 37 | protected $history; |
37 | 38 | ||
diff --git a/application/api/controllers/Links.php b/application/api/controllers/Links.php index 16fc8688..6bf529e4 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,8 +116,8 @@ 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::buildLinkFromRequest($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()) |
122 | && ! empty($dup = $this->bookmarkService->findByUrl($bookmark->getUrl())) | 123 | && ! empty($dup = $this->bookmarkService->findByUrl($bookmark->getUrl())) |
@@ -148,18 +149,19 @@ 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 | ||
155 | $index = index_url($this->ci['environment']); | 157 | $index = index_url($this->ci['environment']); |
156 | $data = $request->getParsedBody(); | 158 | $data = $request->getParsedBody(); |
157 | 159 | ||
158 | $requestBookmark = ApiUtils::buildLinkFromRequest($data, $this->conf->get('privacy.default_private_links')); | 160 | $requestBookmark = ApiUtils::buildBookmarkFromRequest($data, $this->conf->get('privacy.default_private_links')); |
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); |