]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/ApiBundle/Controller/WallabagRestController.php
Added a missing namespace
[github/wallabag/wallabag.git] / src / Wallabag / ApiBundle / Controller / WallabagRestController.php
1 <?php
2
3 namespace Wallabag\ApiBundle\Controller;
4
5 use FOS\RestBundle\Controller\FOSRestController;
6 use Hateoas\Configuration\Route as HateoasRoute;
7 use Hateoas\Representation\Factory\PagerfantaFactory;
8 use Nelmio\ApiDocBundle\Annotation\ApiDoc;
9 use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
10 use Symfony\Component\HttpFoundation\Request;
11 use Symfony\Component\HttpFoundation\JsonResponse;
12 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
13 use Symfony\Component\Security\Core\Exception\AccessDeniedException;
14 use Wallabag\CoreBundle\Entity\Entry;
15 use Wallabag\CoreBundle\Entity\Tag;
16 use Wallabag\AnnotationBundle\Entity\Annotation;
17 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
18
19 class WallabagRestController extends FOSRestController
20 {
21 private function validateAuthentication()
22 {
23 if (false === $this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
24 throw new AccessDeniedException();
25 }
26 }
27
28 /**
29 * Check if an entry exist by url.
30 *
31 * @ApiDoc(
32 * parameters={
33 * {"name"="url", "dataType"="string", "required"=true, "format"="An url", "description"="Url to check if it exists"},
34 * {"name"="urls", "dataType"="string", "required"=false, "format"="An array of urls (?urls[]=http...&urls[]=http...)", "description"="Urls (as an array) to check if it exists"}
35 * }
36 * )
37 *
38 * @return JsonResponse
39 */
40 public function getEntriesExistsAction(Request $request)
41 {
42 $this->validateAuthentication();
43
44 $urls = $request->query->get('urls', []);
45
46 // handle multiple urls first
47 if (!empty($urls)) {
48 $results = [];
49 foreach ($urls as $url) {
50 $res = $this->getDoctrine()
51 ->getRepository('WallabagCoreBundle:Entry')
52 ->findByUrlAndUserId($url, $this->getUser()->getId());
53
54 $results[$url] = false === $res ? false : true;
55 }
56
57 $json = $this->get('serializer')->serialize($results, 'json');
58
59 return (new JsonResponse())->setJson($json);
60 }
61
62 // let's see if it is a simple url?
63 $url = $request->query->get('url', '');
64
65 if (empty($url)) {
66 throw $this->createAccessDeniedException('URL is empty?, logged user id: '.$this->getUser()->getId());
67 }
68
69 $res = $this->getDoctrine()
70 ->getRepository('WallabagCoreBundle:Entry')
71 ->findByUrlAndUserId($url, $this->getUser()->getId());
72
73 $exists = false === $res ? false : true;
74
75 $json = $this->get('serializer')->serialize(['exists' => $exists], 'json');
76
77 return (new JsonResponse())->setJson($json);
78 }
79
80 /**
81 * Retrieve all entries. It could be filtered by many options.
82 *
83 * @ApiDoc(
84 * parameters={
85 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by archived status."},
86 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by starred status."},
87 * {"name"="sort", "dataType"="string", "required"=false, "format"="'created' or 'updated', default 'created'", "description"="sort entries by date."},
88 * {"name"="order", "dataType"="string", "required"=false, "format"="'asc' or 'desc', default 'desc'", "description"="order of sort."},
89 * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."},
90 * {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."},
91 * {"name"="tags", "dataType"="string", "required"=false, "format"="api,rest", "description"="a list of tags url encoded. Will returns entries that matches ALL tags."},
92 * {"name"="since", "dataType"="integer", "required"=false, "format"="default '0'", "description"="The timestamp since when you want entries updated."},
93 * }
94 * )
95 *
96 * @return JsonResponse
97 */
98 public function getEntriesAction(Request $request)
99 {
100 $this->validateAuthentication();
101
102 $isArchived = (null === $request->query->get('archive')) ? null : (bool) $request->query->get('archive');
103 $isStarred = (null === $request->query->get('starred')) ? null : (bool) $request->query->get('starred');
104 $sort = $request->query->get('sort', 'created');
105 $order = $request->query->get('order', 'desc');
106 $page = (int) $request->query->get('page', 1);
107 $perPage = (int) $request->query->get('perPage', 30);
108 $tags = $request->query->get('tags', '');
109 $since = $request->query->get('since', 0);
110
111 $pager = $this->getDoctrine()
112 ->getRepository('WallabagCoreBundle:Entry')
113 ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order, $since, $tags);
114
115 $pager->setCurrentPage($page);
116 $pager->setMaxPerPage($perPage);
117
118 $pagerfantaFactory = new PagerfantaFactory('page', 'perPage');
119 $paginatedCollection = $pagerfantaFactory->createRepresentation(
120 $pager,
121 new HateoasRoute(
122 'api_get_entries',
123 [
124 'archive' => $isArchived,
125 'starred' => $isStarred,
126 'sort' => $sort,
127 'order' => $order,
128 'page' => $page,
129 'perPage' => $perPage,
130 'tags' => $tags,
131 'since' => $since,
132 ],
133 UrlGeneratorInterface::ABSOLUTE_URL
134 )
135 );
136
137 $json = $this->get('serializer')->serialize($paginatedCollection, 'json');
138
139 return (new JsonResponse())->setJson($json);
140 }
141
142 /**
143 * Retrieve a single entry.
144 *
145 * @ApiDoc(
146 * requirements={
147 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
148 * }
149 * )
150 *
151 * @return JsonResponse
152 */
153 public function getEntryAction(Entry $entry)
154 {
155 $this->validateAuthentication();
156 $this->validateUserAccess($entry->getUser()->getId());
157
158 $json = $this->get('serializer')->serialize($entry, 'json');
159
160 return (new JsonResponse())->setJson($json);
161 }
162
163 /**
164 * Retrieve a single entry as a predefined format.
165 *
166 * @ApiDoc(
167 * requirements={
168 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
169 * }
170 * )
171 *
172 * @return Response
173 */
174 public function getEntryExportAction(Entry $entry, Request $request)
175 {
176 $this->validateAuthentication();
177 $this->validateUserAccess($entry->getUser()->getId());
178
179 return $this->get('wallabag_core.helper.entries_export')
180 ->setEntries($entry)
181 ->updateTitle('entry')
182 ->exportAs($request->attributes->get('_format'));
183 }
184
185 /**
186 * Create an entry.
187 *
188 * @ApiDoc(
189 * parameters={
190 * {"name"="url", "dataType"="string", "required"=true, "format"="http://www.test.com/article.html", "description"="Url for the entry."},
191 * {"name"="title", "dataType"="string", "required"=false, "description"="Optional, we'll get the title from the page."},
192 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
193 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already starred"},
194 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already archived"},
195 * }
196 * )
197 *
198 * @return JsonResponse
199 */
200 public function postEntriesAction(Request $request)
201 {
202 $this->validateAuthentication();
203
204 $url = $request->request->get('url');
205 $title = $request->request->get('title');
206 $isArchived = $request->request->get('archive');
207 $isStarred = $request->request->get('starred');
208
209 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($url, $this->getUser()->getId());
210
211 if (false === $entry) {
212 $entry = $this->get('wallabag_core.content_proxy')->updateEntry(
213 new Entry($this->getUser()),
214 $url
215 );
216 }
217
218 if (!is_null($title)) {
219 $entry->setTitle($title);
220 }
221
222 $tags = $request->request->get('tags', '');
223 if (!empty($tags)) {
224 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
225 }
226
227 if (!is_null($isStarred)) {
228 $entry->setStarred((bool) $isStarred);
229 }
230
231 if (!is_null($isArchived)) {
232 $entry->setArchived((bool) $isArchived);
233 }
234
235 $em = $this->getDoctrine()->getManager();
236 $em->persist($entry);
237
238 $em->flush();
239
240 $json = $this->get('serializer')->serialize($entry, 'json');
241
242 return (new JsonResponse())->setJson($json);
243 }
244
245 /**
246 * Change several properties of an entry.
247 *
248 * @ApiDoc(
249 * requirements={
250 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
251 * },
252 * parameters={
253 * {"name"="title", "dataType"="string", "required"=false},
254 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
255 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="archived the entry."},
256 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="starred the entry."},
257 * }
258 * )
259 *
260 * @return JsonResponse
261 */
262 public function patchEntriesAction(Entry $entry, Request $request)
263 {
264 $this->validateAuthentication();
265 $this->validateUserAccess($entry->getUser()->getId());
266
267 $title = $request->request->get('title');
268 $isArchived = $request->request->get('archive');
269 $isStarred = $request->request->get('starred');
270
271 if (!is_null($title)) {
272 $entry->setTitle($title);
273 }
274
275 if (!is_null($isArchived)) {
276 $entry->setArchived((bool) $isArchived);
277 }
278
279 if (!is_null($isStarred)) {
280 $entry->setStarred((bool) $isStarred);
281 }
282
283 $tags = $request->request->get('tags', '');
284 if (!empty($tags)) {
285 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
286 }
287
288 $em = $this->getDoctrine()->getManager();
289 $em->flush();
290
291 $json = $this->get('serializer')->serialize($entry, 'json');
292
293 return (new JsonResponse())->setJson($json);
294 }
295
296 /**
297 * Delete **permanently** an entry.
298 *
299 * @ApiDoc(
300 * requirements={
301 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
302 * }
303 * )
304 *
305 * @return JsonResponse
306 */
307 public function deleteEntriesAction(Entry $entry)
308 {
309 $this->validateAuthentication();
310 $this->validateUserAccess($entry->getUser()->getId());
311
312 $em = $this->getDoctrine()->getManager();
313 $em->remove($entry);
314 $em->flush();
315
316 $json = $this->get('serializer')->serialize($entry, 'json');
317
318 return (new JsonResponse())->setJson($json);
319 }
320
321 /**
322 * Retrieve all tags for an entry.
323 *
324 * @ApiDoc(
325 * requirements={
326 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
327 * }
328 * )
329 *
330 * @return JsonResponse
331 */
332 public function getEntriesTagsAction(Entry $entry)
333 {
334 $this->validateAuthentication();
335 $this->validateUserAccess($entry->getUser()->getId());
336
337 $json = $this->get('serializer')->serialize($entry->getTags(), 'json');
338
339 return (new JsonResponse())->setJson($json);
340 }
341
342 /**
343 * Add one or more tags to an entry.
344 *
345 * @ApiDoc(
346 * requirements={
347 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
348 * },
349 * parameters={
350 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
351 * }
352 * )
353 *
354 * @return JsonResponse
355 */
356 public function postEntriesTagsAction(Request $request, Entry $entry)
357 {
358 $this->validateAuthentication();
359 $this->validateUserAccess($entry->getUser()->getId());
360
361 $tags = $request->request->get('tags', '');
362 if (!empty($tags)) {
363 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
364 }
365
366 $em = $this->getDoctrine()->getManager();
367 $em->persist($entry);
368 $em->flush();
369
370 $json = $this->get('serializer')->serialize($entry, 'json');
371
372 return (new JsonResponse())->setJson($json);
373 }
374
375 /**
376 * Permanently remove one tag for an entry.
377 *
378 * @ApiDoc(
379 * requirements={
380 * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag ID"},
381 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
382 * }
383 * )
384 *
385 * @return JsonResponse
386 */
387 public function deleteEntriesTagsAction(Entry $entry, Tag $tag)
388 {
389 $this->validateAuthentication();
390 $this->validateUserAccess($entry->getUser()->getId());
391
392 $entry->removeTag($tag);
393 $em = $this->getDoctrine()->getManager();
394 $em->persist($entry);
395 $em->flush();
396
397 $json = $this->get('serializer')->serialize($entry, 'json');
398
399 return (new JsonResponse())->setJson($json);
400 }
401
402 /**
403 * Retrieve all tags.
404 *
405 * @ApiDoc()
406 *
407 * @return JsonResponse
408 */
409 public function getTagsAction()
410 {
411 $this->validateAuthentication();
412
413 $tags = $this->getDoctrine()
414 ->getRepository('WallabagCoreBundle:Tag')
415 ->findAllTags($this->getUser()->getId());
416
417 $json = $this->get('serializer')->serialize($tags, 'json');
418
419 return (new JsonResponse())->setJson($json);
420 }
421
422 /**
423 * Permanently remove one tag from **every** entry.
424 *
425 * @ApiDoc(
426 * requirements={
427 * {"name"="tag", "dataType"="string", "required"=true, "requirement"="\w+", "description"="Tag as a string"}
428 * }
429 * )
430 *
431 * @return JsonResponse
432 */
433 public function deleteTagLabelAction(Request $request)
434 {
435 $this->validateAuthentication();
436 $label = $request->request->get('tag', '');
437
438 $tag = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($label);
439
440 if (empty($tag)) {
441 throw $this->createNotFoundException('Tag not found');
442 }
443
444 $this->getDoctrine()
445 ->getRepository('WallabagCoreBundle:Entry')
446 ->removeTag($this->getUser()->getId(), $tag);
447
448 $this->cleanOrphanTag($tag);
449
450 $json = $this->get('serializer')->serialize($tag, 'json');
451
452 return (new JsonResponse())->setJson($json);
453 }
454
455 /**
456 * Permanently remove some tags from **every** entry.
457 *
458 * @ApiDoc(
459 * requirements={
460 * {"name"="tags", "dataType"="string", "required"=true, "format"="tag1,tag2", "description"="Tags as strings (comma splitted)"}
461 * }
462 * )
463 *
464 * @return JsonResponse
465 */
466 public function deleteTagsLabelAction(Request $request)
467 {
468 $this->validateAuthentication();
469
470 $tagsLabels = $request->request->get('tags', '');
471
472 $tags = [];
473
474 foreach (explode(',', $tagsLabels) as $tagLabel) {
475 $tagEntity = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($tagLabel);
476
477 if (!empty($tagEntity)) {
478 $tags[] = $tagEntity;
479 }
480 }
481
482 if (empty($tags)) {
483 throw $this->createNotFoundException('Tags not found');
484 }
485
486 $this->getDoctrine()
487 ->getRepository('WallabagCoreBundle:Entry')
488 ->removeTags($this->getUser()->getId(), $tags);
489
490 $this->cleanOrphanTag($tags);
491
492 $json = $this->get('serializer')->serialize($tags, 'json');
493
494 return (new JsonResponse())->setJson($json);
495 }
496
497 /**
498 * Permanently remove one tag from **every** entry.
499 *
500 * @ApiDoc(
501 * requirements={
502 * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag"}
503 * }
504 * )
505 *
506 * @return JsonResponse
507 */
508 public function deleteTagAction(Tag $tag)
509 {
510 $this->validateAuthentication();
511
512 $this->getDoctrine()
513 ->getRepository('WallabagCoreBundle:Entry')
514 ->removeTag($this->getUser()->getId(), $tag);
515
516 $this->cleanOrphanTag($tag);
517
518 $json = $this->get('serializer')->serialize($tag, 'json');
519
520 return (new JsonResponse())->setJson($json);
521 }
522
523 /**
524 * Retrieve annotations for an entry.
525 *
526 * @ApiDoc(
527 * requirements={
528 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
529 * }
530 * )
531 *
532 * @param Entry $entry
533 *
534 * @return JsonResponse
535 */
536 public function getAnnotationsAction(Entry $entry)
537 {
538 $this->validateAuthentication();
539
540 return $this->forward('WallabagApiBundle:WallabagRest:getAnnotations', [
541 'entry' => $entry,
542 ]);
543 }
544
545 /**
546 * Creates a new annotation.
547 *
548 * @param Request $request
549 * @param Entry $entry
550 *
551 * @return JsonResponse
552 * @ApiDoc(
553 * requirements={
554 * {"name"="ranges", "dataType"="array", "requirement"="\w+", "description"="The range array for the annotation"},
555 * {"name"="quote", "dataType"="string", "required"=false, "description"="Optional, quote for the annotation"},
556 * {"name"="text", "dataType"="string", "required"=true, "description"=""},
557 * }
558 * )
559 */
560 public function postAnnotationAction(Request $request, Entry $entry)
561 {
562 $this->validateAuthentication();
563
564 return $this->forward('WallabagApiBundle:WallabagRest:postAnnotation', [
565 'request' => $request,
566 'entry' => $entry,
567 ]);
568 }
569
570 /**
571 * Updates an annotation.
572 *
573 * @ApiDoc(
574 * requirements={
575 * {"name"="annotation", "dataType"="string", "requirement"="\w+", "description"="The annotation ID"}
576 * }
577 * )
578 *
579 * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
580 *
581 * @param Annotation $annotation
582 * @param Request $request
583 *
584 * @return JsonResponse
585 */
586 public function putAnnotationAction(Annotation $annotation, Request $request)
587 {
588 $this->validateAuthentication();
589
590 return $this->forward('WallabagApiBundle:WallabagRest:putAnnotation', [
591 'annotation' => $annotation,
592 'request' => $request,
593 ]);
594 }
595
596 /**
597 * Removes an annotation.
598 *
599 * @ApiDoc(
600 * requirements={
601 * {"name"="annotation", "dataType"="string", "requirement"="\w+", "description"="The annotation ID"}
602 * }
603 * )
604 *
605 * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
606 *
607 * @param Annotation $annotation
608 *
609 * @return JsonResponse
610 */
611 public function deleteAnnotationAction(Annotation $annotation)
612 {
613 $this->validateAuthentication();
614
615 return $this->forward('WallabagApiBundle:WallabagRest:deleteAnnotation', [
616 'annotation' => $annotation,
617 ]);
618 }
619
620 /**
621 * Retrieve version number.
622 *
623 * @ApiDoc()
624 *
625 * @return JsonResponse
626 */
627 public function getVersionAction()
628 {
629 $version = $this->container->getParameter('wallabag_core.version');
630
631 $json = $this->get('serializer')->serialize($version, 'json');
632
633 return (new JsonResponse())->setJson($json);
634 }
635
636 /**
637 * Remove orphan tag in case no entries are associated to it.
638 *
639 * @param Tag|array $tags
640 */
641 private function cleanOrphanTag($tags)
642 {
643 if (!is_array($tags)) {
644 $tags = [$tags];
645 }
646
647 $em = $this->getDoctrine()->getManager();
648
649 foreach ($tags as $tag) {
650 if (count($tag->getEntries()) === 0) {
651 $em->remove($tag);
652 }
653 }
654
655 $em->flush();
656 }
657
658 /**
659 * Validate that the first id is equal to the second one.
660 * If not, throw exception. It means a user try to access information from an other user.
661 *
662 * @param int $requestUserId User id from the requested source
663 */
664 private function validateUserAccess($requestUserId)
665 {
666 $user = $this->get('security.token_storage')->getToken()->getUser();
667 if ($requestUserId != $user->getId()) {
668 throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$requestUserId.', logged user id: '.$user->getId());
669 }
670 }
671 }