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