]>
Commit | Line | Data |
---|---|---|
c3b00963 A |
1 | <?php |
2 | ||
3 | namespace Shaarli\Api\Controllers; | |
4 | ||
5 | use Shaarli\Api\ApiUtils; | |
6 | use Shaarli\Api\Exceptions\ApiBadParametersException; | |
16e3d006 | 7 | use Shaarli\Api\Exceptions\ApiLinkNotFoundException; |
c3b00963 A |
8 | use Slim\Http\Request; |
9 | use Slim\Http\Response; | |
10 | ||
11 | /** | |
12 | * Class Links | |
13 | * | |
cf92b4dd | 14 | * REST API Controller: all services related to bookmarks collection. |
c3b00963 A |
15 | * |
16 | * @package Api\Controllers | |
17 | * @see http://shaarli.github.io/api-documentation/#links-links-collection | |
18 | */ | |
19 | class Links extends ApiController | |
20 | { | |
21 | /** | |
cf92b4dd | 22 | * @var int Number of bookmarks returned if no limit is provided. |
c3b00963 A |
23 | */ |
24 | public static $DEFAULT_LIMIT = 20; | |
25 | ||
26 | /** | |
cf92b4dd | 27 | * Retrieve a list of bookmarks, allowing different filters. |
c3b00963 A |
28 | * |
29 | * @param Request $request Slim request. | |
30 | * @param Response $response Slim response. | |
31 | * | |
32 | * @return Response response. | |
33 | * | |
34 | * @throws ApiBadParametersException Invalid parameters. | |
35 | */ | |
36 | public function getLinks($request, $response) | |
37 | { | |
c37a6f82 | 38 | $private = $request->getParam('visibility'); |
cf92b4dd | 39 | $bookmarks = $this->bookmarkService->search( |
c3b00963 A |
40 | [ |
41 | 'searchtags' => $request->getParam('searchtags', ''), | |
42 | 'searchterm' => $request->getParam('searchterm', ''), | |
43 | ], | |
c37a6f82 | 44 | $private |
c3b00963 A |
45 | ); |
46 | ||
cf92b4dd | 47 | // Return bookmarks from the {offset}th link, starting from 0. |
c3b00963 A |
48 | $offset = $request->getParam('offset'); |
49 | if (! empty($offset) && ! ctype_digit($offset)) { | |
50 | throw new ApiBadParametersException('Invalid offset'); | |
51 | } | |
52 | $offset = ! empty($offset) ? intval($offset) : 0; | |
cf92b4dd | 53 | if ($offset > count($bookmarks)) { |
c3b00963 A |
54 | return $response->withJson([], 200, $this->jsonStyle); |
55 | } | |
56 | ||
cf92b4dd | 57 | // limit parameter is either a number of bookmarks or 'all' for everything. |
c3b00963 A |
58 | $limit = $request->getParam('limit'); |
59 | if (empty($limit)) { | |
60 | $limit = self::$DEFAULT_LIMIT; | |
d2d4f993 | 61 | } elseif (ctype_digit($limit)) { |
c3b00963 | 62 | $limit = intval($limit); |
d2d4f993 | 63 | } elseif ($limit === 'all') { |
cf92b4dd | 64 | $limit = count($bookmarks); |
c3b00963 A |
65 | } else { |
66 | throw new ApiBadParametersException('Invalid limit'); | |
67 | } | |
68 | ||
69 | // 'environment' is set by Slim and encapsulate $_SERVER. | |
d3f42ca4 | 70 | $indexUrl = index_url($this->ci['environment']); |
c3b00963 A |
71 | |
72 | $out = []; | |
d3f42ca4 | 73 | $index = 0; |
cf92b4dd | 74 | foreach ($bookmarks as $bookmark) { |
c3b00963 A |
75 | if (count($out) >= $limit) { |
76 | break; | |
77 | } | |
d3f42ca4 | 78 | if ($index++ >= $offset) { |
cf92b4dd | 79 | $out[] = ApiUtils::formatLink($bookmark, $indexUrl); |
c3b00963 A |
80 | } |
81 | } | |
82 | ||
83 | return $response->withJson($out, 200, $this->jsonStyle); | |
84 | } | |
16e3d006 A |
85 | |
86 | /** | |
87 | * Return a single formatted link by its ID. | |
88 | * | |
89 | * @param Request $request Slim request. | |
90 | * @param Response $response Slim response. | |
91 | * @param array $args Path parameters. including the ID. | |
92 | * | |
93 | * @return Response containing the link array. | |
94 | * | |
95 | * @throws ApiLinkNotFoundException generating a 404 error. | |
96 | */ | |
97 | public function getLink($request, $response, $args) | |
98 | { | |
efb7d21b A |
99 | $id = is_integer_mixed($args['id']) ? (int) $args['id'] : null; |
100 | if ($id === null || ! $this->bookmarkService->exists($id)) { | |
16e3d006 A |
101 | throw new ApiLinkNotFoundException(); |
102 | } | |
103 | $index = index_url($this->ci['environment']); | |
efb7d21b | 104 | $out = ApiUtils::formatLink($this->bookmarkService->get($id), $index); |
68016e37 | 105 | |
16e3d006 A |
106 | return $response->withJson($out, 200, $this->jsonStyle); |
107 | } | |
68016e37 A |
108 | |
109 | /** | |
110 | * Creates a new link from posted request body. | |
111 | * | |
112 | * @param Request $request Slim request. | |
113 | * @param Response $response Slim response. | |
114 | * | |
115 | * @return Response response. | |
116 | */ | |
117 | public function postLink($request, $response) | |
118 | { | |
efb7d21b | 119 | $data = (array) ($request->getParsedBody() ?? []); |
b06fc28a | 120 | $bookmark = ApiUtils::buildBookmarkFromRequest($data, $this->conf->get('privacy.default_private_links')); |
68016e37 | 121 | // duplicate by URL, return 409 Conflict |
cf92b4dd A |
122 | if (! empty($bookmark->getUrl()) |
123 | && ! empty($dup = $this->bookmarkService->findByUrl($bookmark->getUrl())) | |
124 | ) { | |
68016e37 A |
125 | return $response->withJson( |
126 | ApiUtils::formatLink($dup, index_url($this->ci['environment'])), | |
127 | 409, | |
128 | $this->jsonStyle | |
129 | ); | |
130 | } | |
131 | ||
cf92b4dd A |
132 | $this->bookmarkService->add($bookmark); |
133 | $out = ApiUtils::formatLink($bookmark, index_url($this->ci['environment'])); | |
134 | $redirect = $this->ci->router->relativePathFor('getLink', ['id' => $bookmark->getId()]); | |
68016e37 A |
135 | return $response->withAddedHeader('Location', $redirect) |
136 | ->withJson($out, 201, $this->jsonStyle); | |
137 | } | |
cf9181dd A |
138 | |
139 | /** | |
140 | * Updates an existing link from posted request body. | |
141 | * | |
142 | * @param Request $request Slim request. | |
143 | * @param Response $response Slim response. | |
144 | * @param array $args Path parameters. including the ID. | |
145 | * | |
146 | * @return Response response. | |
147 | * | |
148 | * @throws ApiLinkNotFoundException generating a 404 error. | |
149 | */ | |
150 | public function putLink($request, $response, $args) | |
151 | { | |
efb7d21b A |
152 | $id = is_integer_mixed($args['id']) ? (int) $args['id'] : null; |
153 | if ($id === null || !$this->bookmarkService->exists($id)) { | |
cf9181dd A |
154 | throw new ApiLinkNotFoundException(); |
155 | } | |
156 | ||
157 | $index = index_url($this->ci['environment']); | |
158 | $data = $request->getParsedBody(); | |
159 | ||
b06fc28a | 160 | $requestBookmark = ApiUtils::buildBookmarkFromRequest($data, $this->conf->get('privacy.default_private_links')); |
cf9181dd | 161 | // duplicate URL on a different link, return 409 Conflict |
cf92b4dd A |
162 | if (! empty($requestBookmark->getUrl()) |
163 | && ! empty($dup = $this->bookmarkService->findByUrl($requestBookmark->getUrl())) | |
efb7d21b | 164 | && $dup->getId() != $id |
cf9181dd A |
165 | ) { |
166 | return $response->withJson( | |
167 | ApiUtils::formatLink($dup, $index), | |
168 | 409, | |
169 | $this->jsonStyle | |
170 | ); | |
171 | } | |
172 | ||
efb7d21b | 173 | $responseBookmark = $this->bookmarkService->get($id); |
cf92b4dd A |
174 | $responseBookmark = ApiUtils::updateLink($responseBookmark, $requestBookmark); |
175 | $this->bookmarkService->set($responseBookmark); | |
cf9181dd | 176 | |
cf92b4dd | 177 | $out = ApiUtils::formatLink($responseBookmark, $index); |
cf9181dd A |
178 | return $response->withJson($out, 200, $this->jsonStyle); |
179 | } | |
0843848c A |
180 | |
181 | /** | |
182 | * Delete an existing link by its ID. | |
183 | * | |
184 | * @param Request $request Slim request. | |
185 | * @param Response $response Slim response. | |
186 | * @param array $args Path parameters. including the ID. | |
187 | * | |
188 | * @return Response response. | |
189 | * | |
190 | * @throws ApiLinkNotFoundException generating a 404 error. | |
191 | */ | |
192 | public function deleteLink($request, $response, $args) | |
193 | { | |
efb7d21b A |
194 | $id = is_integer_mixed($args['id']) ? (int) $args['id'] : null; |
195 | if ($id === null || !$this->bookmarkService->exists($id)) { | |
0843848c A |
196 | throw new ApiLinkNotFoundException(); |
197 | } | |
efb7d21b | 198 | $bookmark = $this->bookmarkService->get($id); |
cf92b4dd | 199 | $this->bookmarkService->remove($bookmark); |
0843848c A |
200 | |
201 | return $response->withStatus(204); | |
202 | } | |
c3b00963 | 203 | } |