3 namespace Shaarli\Api\Controllers
;
5 use Shaarli\Api\ApiUtils
;
6 use Shaarli\Api\Exceptions\ApiBadParametersException
;
7 use Shaarli\Api\Exceptions\ApiLinkNotFoundException
;
9 use Slim\Http\Response
;
14 * REST API Controller: all services related to bookmarks collection.
16 * @package Api\Controllers
17 * @see http://shaarli.github.io/api-documentation/#links-links-collection
19 class Links
extends ApiController
22 * @var int Number of bookmarks returned if no limit is provided.
24 public static $DEFAULT_LIMIT = 20;
27 * Retrieve a list of bookmarks, allowing different filters.
29 * @param Request $request Slim request.
30 * @param Response $response Slim response.
32 * @return Response response.
34 * @throws ApiBadParametersException Invalid parameters.
36 public function getLinks($request, $response)
38 $private = $request->getParam('visibility');
39 $bookmarks = $this->bookmarkService
->search(
41 'searchtags' => $request->getParam('searchtags', ''),
42 'searchterm' => $request->getParam('searchterm', ''),
47 // Return bookmarks from the {offset}th link, starting from 0.
48 $offset = $request->getParam('offset');
49 if (! empty($offset) && ! ctype_digit($offset)) {
50 throw new ApiBadParametersException('Invalid offset');
52 $offset = ! empty($offset) ? intval($offset) : 0;
53 if ($offset > count($bookmarks)) {
54 return $response->withJson([], 200, $this->jsonStyle
);
57 // limit parameter is either a number of bookmarks or 'all' for everything.
58 $limit = $request->getParam('limit');
60 $limit = self
::$DEFAULT_LIMIT;
61 } elseif (ctype_digit($limit)) {
62 $limit = intval($limit);
63 } elseif ($limit === 'all') {
64 $limit = count($bookmarks);
66 throw new ApiBadParametersException('Invalid limit');
69 // 'environment' is set by Slim and encapsulate $_SERVER.
70 $indexUrl = index_url($this->ci
['environment']);
74 foreach ($bookmarks as $bookmark) {
75 if (count($out) >= $limit) {
78 if ($index++
>= $offset) {
79 $out[] = ApiUtils
::formatLink($bookmark, $indexUrl);
83 return $response->withJson($out, 200, $this->jsonStyle
);
87 * Return a single formatted link by its ID.
89 * @param Request $request Slim request.
90 * @param Response $response Slim response.
91 * @param array $args Path parameters. including the ID.
93 * @return Response containing the link array.
95 * @throws ApiLinkNotFoundException generating a 404 error.
97 public function getLink($request, $response, $args)
99 $id = is_integer_mixed($args['id']) ? (int) $args['id'] : null;
100 if ($id === null || ! $this->bookmarkService
->exists($id)) {
101 throw new ApiLinkNotFoundException();
103 $index = index_url($this->ci
['environment']);
104 $out = ApiUtils
::formatLink($this->bookmarkService
->get($id), $index);
106 return $response->withJson($out, 200, $this->jsonStyle
);
110 * Creates a new link from posted request body.
112 * @param Request $request Slim request.
113 * @param Response $response Slim response.
115 * @return Response response.
117 public function postLink($request, $response)
119 $data = (array) ($request->getParsedBody() ?? []);
120 $bookmark = ApiUtils
::buildBookmarkFromRequest($data, $this->conf
->get('privacy.default_private_links'));
121 // duplicate by URL, return 409 Conflict
123 ! empty($bookmark->getUrl())
124 && ! empty($dup = $this->bookmarkService
->findByUrl($bookmark->getUrl()))
126 return $response->withJson(
127 ApiUtils
::formatLink($dup, index_url($this->ci
['environment'])),
133 $this->bookmarkService
->add($bookmark);
134 $out = ApiUtils
::formatLink($bookmark, index_url($this->ci
['environment']));
135 $redirect = $this->ci
->router
->pathFor('getLink', ['id' => $bookmark->getId()]);
136 return $response->withAddedHeader('Location', $redirect)
137 ->withJson($out, 201, $this->jsonStyle
);
141 * Updates an existing link from posted request body.
143 * @param Request $request Slim request.
144 * @param Response $response Slim response.
145 * @param array $args Path parameters. including the ID.
147 * @return Response response.
149 * @throws ApiLinkNotFoundException generating a 404 error.
151 public function putLink($request, $response, $args)
153 $id = is_integer_mixed($args['id']) ? (int) $args['id'] : null;
154 if ($id === null || !$this->bookmarkService
->exists($id)) {
155 throw new ApiLinkNotFoundException();
158 $index = index_url($this->ci
['environment']);
159 $data = $request->getParsedBody();
161 $requestBookmark = ApiUtils
::buildBookmarkFromRequest($data, $this->conf
->get('privacy.default_private_links'));
162 // duplicate URL on a different link, return 409 Conflict
164 ! empty($requestBookmark->getUrl())
165 && ! empty($dup = $this->bookmarkService
->findByUrl($requestBookmark->getUrl()))
166 && $dup->getId() != $id
168 return $response->withJson(
169 ApiUtils
::formatLink($dup, $index),
175 $responseBookmark = $this->bookmarkService
->get($id);
176 $responseBookmark = ApiUtils
::updateLink($responseBookmark, $requestBookmark);
177 $this->bookmarkService
->set($responseBookmark);
179 $out = ApiUtils
::formatLink($responseBookmark, $index);
180 return $response->withJson($out, 200, $this->jsonStyle
);
184 * Delete an existing link by its ID.
186 * @param Request $request Slim request.
187 * @param Response $response Slim response.
188 * @param array $args Path parameters. including the ID.
190 * @return Response response.
192 * @throws ApiLinkNotFoundException generating a 404 error.
194 public function deleteLink($request, $response, $args)
196 $id = is_integer_mixed($args['id']) ? (int) $args['id'] : null;
197 if ($id === null || !$this->bookmarkService
->exists($id)) {
198 throw new ApiLinkNotFoundException();
200 $bookmark = $this->bookmarkService
->get($id);
201 $this->bookmarkService
->remove($bookmark);
203 return $response->withStatus(204);