2015-10-15 20:06:59 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Wallabag\CoreBundle\Controller;
|
|
|
|
|
2022-12-19 13:23:56 +01:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
2016-10-27 09:41:19 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2022-08-28 16:59:43 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2015-10-16 10:51:53 +02:00
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
2018-10-04 14:07:20 +02:00
|
|
|
use Symfony\Component\Routing\Annotation\Route;
|
2022-04-24 17:48:59 +02:00
|
|
|
use Wallabag\CoreBundle\Helper\EntriesExport;
|
2022-04-24 17:58:57 +02:00
|
|
|
use Wallabag\CoreBundle\Repository\EntryRepository;
|
|
|
|
use Wallabag\CoreBundle\Repository\TagRepository;
|
2015-10-15 20:06:59 +02:00
|
|
|
|
2015-10-30 20:57:10 +01:00
|
|
|
/**
|
|
|
|
* The try/catch can be removed once all formats will be implemented.
|
|
|
|
* Still need implementation: txt.
|
|
|
|
*/
|
2022-12-19 13:23:56 +01:00
|
|
|
class ExportController extends AbstractController
|
2015-10-15 20:06:59 +02:00
|
|
|
{
|
|
|
|
/**
|
2015-10-16 10:51:53 +02:00
|
|
|
* Gets one entry content.
|
2015-10-15 20:06:59 +02:00
|
|
|
*
|
2015-10-30 20:57:10 +01:00
|
|
|
* @Route("/export/{id}.{format}", name="export_entry", requirements={
|
|
|
|
* "format": "epub|mobi|pdf|json|xml|txt|csv",
|
|
|
|
* "id": "\d+"
|
|
|
|
* })
|
2016-04-12 11:36:01 +02:00
|
|
|
*
|
2022-08-28 16:59:43 +02:00
|
|
|
* @return Response
|
2015-10-15 20:06:59 +02:00
|
|
|
*/
|
2023-04-24 14:36:32 +02:00
|
|
|
public function downloadEntryAction(Request $request, EntryRepository $entryRepository, EntriesExport $entriesExport, string $format, int $id)
|
2015-10-15 20:06:59 +02:00
|
|
|
{
|
2015-10-16 10:51:53 +02:00
|
|
|
try {
|
2023-04-24 14:36:32 +02:00
|
|
|
$entry = $entryRepository->find($id);
|
2023-01-12 23:40:16 +01:00
|
|
|
|
2023-02-01 09:51:02 +01:00
|
|
|
/*
|
2023-01-12 23:40:16 +01:00
|
|
|
* We duplicate EntryController::checkUserAction here as a quick fix for an improper authorization vulnerability
|
|
|
|
*
|
|
|
|
* This should be eventually rewritten
|
2023-02-01 09:51:02 +01:00
|
|
|
*/
|
2023-01-12 23:40:16 +01:00
|
|
|
if (null === $entry || null === $this->getUser() || $this->getUser()->getId() !== $entry->getUser()->getId()) {
|
|
|
|
throw new NotFoundHttpException();
|
|
|
|
}
|
|
|
|
|
2022-12-19 10:37:22 +01:00
|
|
|
return $entriesExport
|
2015-10-16 10:51:53 +02:00
|
|
|
->setEntries($entry)
|
|
|
|
->updateTitle('entry')
|
2017-07-08 17:55:58 +02:00
|
|
|
->updateAuthor('entry')
|
2015-10-16 10:51:53 +02:00
|
|
|
->exportAs($format);
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
throw new NotFoundHttpException($e->getMessage());
|
2015-10-15 20:06:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-16 10:51:53 +02:00
|
|
|
* Export all entries for current user.
|
2015-10-15 20:06:59 +02:00
|
|
|
*
|
2015-10-16 10:51:53 +02:00
|
|
|
* @Route("/export/{category}.{format}", name="export_entries", requirements={
|
2015-10-30 20:57:10 +01:00
|
|
|
* "format": "epub|mobi|pdf|json|xml|txt|csv",
|
2020-04-26 14:09:16 +02:00
|
|
|
* "category": "all|unread|starred|archive|tag_entries|untagged|search|annotated|same_domain"
|
2015-10-16 10:51:53 +02:00
|
|
|
* })
|
2016-04-12 11:36:01 +02:00
|
|
|
*
|
2022-08-28 16:59:43 +02:00
|
|
|
* @return Response
|
2015-10-15 20:06:59 +02:00
|
|
|
*/
|
2022-12-19 10:37:22 +01:00
|
|
|
public function downloadEntriesAction(Request $request, EntryRepository $entryRepository, TagRepository $tagRepository, EntriesExport $entriesExport, string $format, string $category)
|
2015-10-15 20:06:59 +02:00
|
|
|
{
|
2015-10-16 10:51:53 +02:00
|
|
|
$method = ucfirst($category);
|
2017-07-01 09:52:38 +02:00
|
|
|
$methodBuilder = 'getBuilderFor' . $method . 'ByUser';
|
2019-01-06 20:17:35 +01:00
|
|
|
$title = $method;
|
2016-10-27 09:41:19 +02:00
|
|
|
|
2017-10-09 16:47:15 +02:00
|
|
|
if ('tag_entries' === $category) {
|
2022-12-19 10:37:22 +01:00
|
|
|
$tag = $tagRepository->findOneBySlug($request->query->get('tag'));
|
2016-10-27 09:41:19 +02:00
|
|
|
|
2022-12-19 10:37:22 +01:00
|
|
|
$entries = $entryRepository->findAllByTagId(
|
2017-06-10 12:33:58 +02:00
|
|
|
$this->getUser()->getId(),
|
|
|
|
$tag->getId()
|
|
|
|
);
|
2019-01-06 20:17:35 +01:00
|
|
|
|
|
|
|
$title = 'Tag ' . $tag->getLabel();
|
2020-02-04 22:19:45 +01:00
|
|
|
} elseif ('search' === $category) {
|
|
|
|
$searchTerm = (isset($request->get('search_entry')['term']) ? $request->get('search_entry')['term'] : '');
|
|
|
|
$currentRoute = (null !== $request->query->get('currentRoute') ? $request->query->get('currentRoute') : '');
|
|
|
|
|
2022-12-19 10:37:22 +01:00
|
|
|
$entries = $entryRepository->getBuilderForSearchByUser(
|
2020-02-04 22:19:45 +01:00
|
|
|
$this->getUser()->getId(),
|
|
|
|
$searchTerm,
|
|
|
|
$currentRoute
|
|
|
|
)->getQuery()
|
|
|
|
->getResult();
|
|
|
|
|
|
|
|
$title = 'Search ' . $searchTerm;
|
2020-04-26 14:09:16 +02:00
|
|
|
} elseif ('annotated' === $category) {
|
2022-12-19 10:37:22 +01:00
|
|
|
$entries = $entryRepository->getBuilderForAnnotationsByUser(
|
2020-04-20 19:00:58 +02:00
|
|
|
$this->getUser()->getId()
|
|
|
|
)->getQuery()
|
|
|
|
->getResult();
|
|
|
|
|
|
|
|
$title = 'With annotations';
|
2016-10-27 09:41:19 +02:00
|
|
|
} else {
|
2022-12-19 10:37:22 +01:00
|
|
|
$entries = $entryRepository
|
2016-10-27 09:41:19 +02:00
|
|
|
->$methodBuilder($this->getUser()->getId())
|
|
|
|
->getQuery()
|
|
|
|
->getResult();
|
|
|
|
}
|
2015-10-16 10:51:53 +02:00
|
|
|
|
|
|
|
try {
|
2022-12-19 10:37:22 +01:00
|
|
|
return $entriesExport
|
2015-10-16 10:51:53 +02:00
|
|
|
->setEntries($entries)
|
2019-01-06 20:17:35 +01:00
|
|
|
->updateTitle($title)
|
2017-07-08 17:55:58 +02:00
|
|
|
->updateAuthor($method)
|
2015-10-16 10:51:53 +02:00
|
|
|
->exportAs($format);
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
throw new NotFoundHttpException($e->getMessage());
|
|
|
|
}
|
2015-10-15 20:06:59 +02:00
|
|
|
}
|
|
|
|
}
|