|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * PDF Controller |
| 5 | + * |
| 6 | + * This Source Code Form is subject to the terms of the Mozilla Public License, |
| 7 | + * v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 8 | + * obtain one at https://mozilla.org/MPL/2.0/. |
| 9 | + * |
| 10 | + * @package phpMyFAQ |
| 11 | + * @author Thorsten Rinne <thorsten@phpmyfaq.de> |
| 12 | + * @author Peter Beauvain <pbeauvain@web.de> |
| 13 | + * @author Olivier Plathey <olivier@fpdf.org> |
| 14 | + * @author Krzysztof Kruszynski <thywolf@wolf.homelinux.net> |
| 15 | + * @author Matteo Scaramuccia <matteo@phpmyfaq.de> |
| 16 | + * @copyright 2003-2026 phpMyFAQ Team |
| 17 | + * @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
| 18 | + * @link https://www.phpmyfaq.de |
| 19 | + * @since 2003-02-12 |
| 20 | + */ |
| 21 | + |
| 22 | +namespace phpMyFAQ\Controller\Frontend; |
| 23 | + |
| 24 | +use DateTime; |
| 25 | +use League\CommonMark\Exception\CommonMarkException; |
| 26 | +use phpMyFAQ\Attachment\AttachmentException; |
| 27 | +use phpMyFAQ\Attachment\AttachmentFactory; |
| 28 | +use phpMyFAQ\Category; |
| 29 | +use phpMyFAQ\Core\Exception; |
| 30 | +use phpMyFAQ\Export\Pdf; |
| 31 | +use phpMyFAQ\Filter; |
| 32 | +use phpMyFAQ\Helper\AttachmentHelper; |
| 33 | +use phpMyFAQ\User\CurrentUser; |
| 34 | +use Symfony\Component\HttpFoundation\RedirectResponse; |
| 35 | +use Symfony\Component\HttpFoundation\Request; |
| 36 | +use Symfony\Component\HttpFoundation\Response; |
| 37 | +use Symfony\Component\Routing\Attribute\Route; |
| 38 | + |
| 39 | +final class PdfController extends AbstractFrontController |
| 40 | +{ |
| 41 | + /** |
| 42 | + * @throws Exception|\Exception|CommonMarkException |
| 43 | + */ |
| 44 | + #[Route(path: '/pdf/{categoryId}/{faqId}/{faqLanguage}', name: 'public.pdf.faq')] |
| 45 | + public function index(Request $request): Response |
| 46 | + { |
| 47 | + $categoryId = Filter::filterVar($request->attributes->get('categoryId'), FILTER_VALIDATE_INT); |
| 48 | + $faqId = Filter::filterVar($request->attributes->get('faqId'), FILTER_VALIDATE_INT); |
| 49 | + $faqLanguage = Filter::filterVar($request->attributes->get('faqLanguage'), FILTER_SANITIZE_SPECIAL_CHARS); |
| 50 | + |
| 51 | + if ($categoryId === false || $categoryId === null || $faqId === false || $faqId === null) { |
| 52 | + return new RedirectResponse($this->configuration->getDefaultUrl()); |
| 53 | + } |
| 54 | + |
| 55 | + [$currentUser, $currentGroups] = CurrentUser::getCurrentUserGroupId($this->currentUser); |
| 56 | + |
| 57 | + $faq = $this->container->get('phpmyfaq.faq'); |
| 58 | + $faq->setUser($currentUser); |
| 59 | + $faq->setGroups($currentGroups); |
| 60 | + |
| 61 | + $category = new Category($this->configuration, $currentGroups, true); |
| 62 | + $category->setUser($currentUser); |
| 63 | + |
| 64 | + $tags = $this->container->get('phpmyfaq.tags'); |
| 65 | + $tags->setUser($currentUser)->setGroups($currentGroups); |
| 66 | + |
| 67 | + $pdf = new Pdf($faq, $category, $this->configuration); |
| 68 | + |
| 69 | + $faq->getFaq($faqId); |
| 70 | + $faq->faqRecord['category_id'] = $categoryId; |
| 71 | + |
| 72 | + if (!$this->configuration->get('records.disableAttachments') && 'yes' === $faq->faqRecord['active']) { |
| 73 | + try { |
| 74 | + $attachmentHelper = new AttachmentHelper(); |
| 75 | + $attList = AttachmentFactory::fetchByRecordId($this->configuration, $faqId); |
| 76 | + $faq->faqRecord['attachmentList'] = $attachmentHelper->getAttachmentList($attList); |
| 77 | + } catch (AttachmentException) { |
| 78 | + $faq->faqRecord['attachmentList'] = ''; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + $filename = 'FAQ-' . $faqId . '-' . $faqLanguage . '.pdf'; |
| 83 | + $pdfFile = $pdf->generateFile($faq->faqRecord, $filename); |
| 84 | + |
| 85 | + $response = new Response(); |
| 86 | + $response->setExpires(new DateTime()); |
| 87 | + $response->headers->set('Content-Type', 'application/pdf'); |
| 88 | + $response->setContent($pdfFile); |
| 89 | + |
| 90 | + return $response; |
| 91 | + } |
| 92 | +} |
0 commit comments