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 links 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 links returned if no limit is provided.
24 public static $DEFAULT_LIMIT = 20;
27 * Retrieve a list of links, 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 $links = $this->linkDb
->filterSearch(
41 'searchtags' => $request->getParam('searchtags', ''),
42 'searchterm' => $request->getParam('searchterm', ''),
48 // Return links from the {offset}th link, starting from 0.
49 $offset = $request->getParam('offset');
50 if (! empty($offset) && ! ctype_digit($offset)) {
51 throw new ApiBadParametersException('Invalid offset');
53 $offset = ! empty($offset) ? intval($offset) : 0;
54 if ($offset > count($links)) {
55 return $response->withJson([], 200, $this->jsonStyle
);
58 // limit parameter is either a number of links or 'all' for everything.
59 $limit = $request->getParam('limit');
61 $limit = self
::$DEFAULT_LIMIT;
62 } else if (ctype_digit($limit)) {
63 $limit = intval($limit);
64 } else if ($limit === 'all') {
65 $limit = count($links);
67 throw new ApiBadParametersException('Invalid limit');
70 // 'environment' is set by Slim and encapsulate $_SERVER.
71 $index = index_url($this->ci
['environment']);
75 foreach ($links as $link) {
76 if (count($out) >= $limit) {
79 if ($cpt++
>= $offset) {
80 $out[] = ApiUtils
::formatLink($link, $index);
84 return $response->withJson($out, 200, $this->jsonStyle
);
88 * Return a single formatted link by its ID.
90 * @param Request $request Slim request.
91 * @param Response $response Slim response.
92 * @param array $args Path parameters. including the ID.
94 * @return Response containing the link array.
96 * @throws ApiLinkNotFoundException generating a 404 error.
98 public function getLink($request, $response, $args)
100 if (!isset($this->linkDb
[$args['id']])) {
101 throw new ApiLinkNotFoundException();
103 $index = index_url($this->ci
['environment']);
104 $out = ApiUtils
::formatLink($this->linkDb
[$args['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 = $request->getParsedBody();
120 $link = ApiUtils
::buildLinkFromRequest($data, $this->conf
->get('privacy.default_private_links'));
121 // duplicate by URL, return 409 Conflict
122 if (! empty($link['url']) && ! empty($dup = $this->linkDb
->getLinkFromUrl($link['url']))) {
123 return $response->withJson(
124 ApiUtils
::formatLink($dup, index_url($this->ci
['environment'])),
130 $link['id'] = $this->linkDb
->getNextId();
131 $link['shorturl'] = link_small_hash($link['created'], $link['id']);
133 // note: general relative URL
134 if (empty($link['url'])) {
135 $link['url'] = '?' . $link['shorturl'];
138 if (empty($link['title'])) {
139 $link['title'] = $link['url'];
142 $this->linkDb
[$link['id']] = $link;
143 $this->linkDb
->save($this->conf
->get('resource.page_cache'));
144 $out = ApiUtils
::formatLink($link, index_url($this->ci
['environment']));
145 $redirect = $this->ci
->router
->relativePathFor('getLink', ['id' => $link['id']]);
146 return $response->withAddedHeader('Location', $redirect)
147 ->withJson($out, 201, $this->jsonStyle
);
151 * Updates an existing link from posted request body.
153 * @param Request $request Slim request.
154 * @param Response $response Slim response.
155 * @param array $args Path parameters. including the ID.
157 * @return Response response.
159 * @throws ApiLinkNotFoundException generating a 404 error.
161 public function putLink($request, $response, $args)
163 if (! isset($this->linkDb
[$args['id']])) {
164 throw new ApiLinkNotFoundException();
167 $index = index_url($this->ci
['environment']);
168 $data = $request->getParsedBody();
170 $requestLink = ApiUtils
::buildLinkFromRequest($data, $this->conf
->get('privacy.default_private_links'));
171 // duplicate URL on a different link, return 409 Conflict
172 if (! empty($requestLink['url'])
173 && ! empty($dup = $this->linkDb
->getLinkFromUrl($requestLink['url']))
174 && $dup['id'] != $args['id']
176 return $response->withJson(
177 ApiUtils
::formatLink($dup, $index),
183 $responseLink = $this->linkDb
[$args['id']];
184 $responseLink = ApiUtils
::updateLink($responseLink, $requestLink);
185 $this->linkDb
[$responseLink['id']] = $responseLink;
186 $this->linkDb
->save($this->conf
->get('resource.page_cache'));
188 $out = ApiUtils
::formatLink($responseLink, $index);
189 return $response->withJson($out, 200, $this->jsonStyle
);
193 * Delete an existing link by its ID.
195 * @param Request $request Slim request.
196 * @param Response $response Slim response.
197 * @param array $args Path parameters. including the ID.
199 * @return Response response.
201 * @throws ApiLinkNotFoundException generating a 404 error.
203 public function deleteLink($request, $response, $args)
205 if (! isset($this->linkDb
[$args['id']])) {
206 throw new ApiLinkNotFoundException();
209 unset($this->linkDb
[(int) $args['id']]);
210 $this->linkDb
->save($this->conf
->get('resource.page_cache'));
212 return $response->withStatus(204);