1<?php
2
3/**
4 * Export of a generated report.
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 http://mozilla.org/MPL/2.0/.
9 *
10 * @package phpMyFAQ
11 * @author Gustavo Solt <gustavo.solt@mayflower.de>
12 * @author Thorsten Rinne <thorsten@phpmyfaq.de>
13 * @copyright 2011-2020 phpMyFAQ Team
14 * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
15 * @link https://www.phpmyfaq.de
16 * @since 2011-01-12
17 */
18
19use phpMyFAQ\Filter;
20use phpMyFAQ\Report;
21use phpMyFAQ\HttpStreamer;
22
23if (!defined('IS_VALID_PHPMYFAQ')) {
24    http_response_code(400);
25    exit();
26}
27
28if ($user->perm->checkRight($user->getUserId(), 'reports')) {
29    $useCategory = Filter::filterInput(INPUT_POST, 'report_category', FILTER_VALIDATE_INT);
30    $useSubcategory = Filter::filterInput(INPUT_POST, 'report_sub_category', FILTER_VALIDATE_INT);
31    $useTranslation = Filter::filterInput(INPUT_POST, 'report_translations', FILTER_VALIDATE_INT);
32    $useLanguage = Filter::filterInput(INPUT_POST, 'report_language', FILTER_VALIDATE_INT);
33    $useId = Filter::filterInput(INPUT_POST, 'report_id', FILTER_VALIDATE_INT);
34    $useSticky = Filter::filterInput(INPUT_POST, 'report_sticky', FILTER_VALIDATE_INT);
35    $useTitle = Filter::filterInput(INPUT_POST, 'report_title', FILTER_VALIDATE_INT);
36    $useCreationDate = Filter::filterInput(INPUT_POST, 'report_creation_date', FILTER_VALIDATE_INT);
37    $useOwner = Filter::filterInput(INPUT_POST, 'report_owner', FILTER_VALIDATE_INT);
38    $useLastModified = Filter::filterInput(INPUT_POST, 'report_last_modified_person', FILTER_VALIDATE_INT);
39    $useUrl = Filter::filterInput(INPUT_POST, 'report_url', FILTER_VALIDATE_INT);
40    $useVisits = Filter::filterInput(INPUT_POST, 'report_visits', FILTER_VALIDATE_INT);
41
42    $text = [];
43    $text[0] = [];
44    ($useCategory) ? $text[0][] = $PMF_LANG['ad_stat_report_category'] : '';
45    ($useSubcategory) ? $text[0][] = $PMF_LANG['ad_stat_report_sub_category'] : '';
46    ($useTranslation) ? $text[0][] = $PMF_LANG['ad_stat_report_translations'] : '';
47    ($useLanguage) ? $text[0][] = $PMF_LANG['ad_stat_report_language'] : '';
48    ($useId) ? $text[0][] = $PMF_LANG['ad_stat_report_id'] : '';
49    ($useSticky) ? $text[0][] = $PMF_LANG['ad_stat_report_sticky'] : '';
50    ($useTitle) ? $text[0][] = $PMF_LANG['ad_stat_report_title'] : '';
51    ($useCreationDate) ? $text[0][] = $PMF_LANG['ad_stat_report_creation_date'] : '';
52    ($useOwner) ? $text[0][] = $PMF_LANG['ad_stat_report_owner'] : '';
53    ($useLastModified) ? $text[0][] = $PMF_LANG['ad_stat_report_last_modified_person'] : '';
54    ($useUrl) ? $text[0][] = $PMF_LANG['ad_stat_report_url'] : '';
55    ($useVisits) ? $text[0][] = $PMF_LANG['ad_stat_report_visits'] : '';
56
57    $report = new Report($faqConfig);
58
59    foreach ($report->getReportingData() as $data) {
60        $i = $data['faq_id'];
61        if ($useCategory && isset($data['category_name'])) {
62            if (0 !== $data['category_parent']) {
63                $text[$i][] = $data['category_parent'];
64            } else {
65                $text[$i][] = $report->convertEncoding($data['category_name']);
66            }
67        }
68        if ($useSubcategory) {
69            if (0 != $data['category_parent']) {
70                $text[$i][] = $report->convertEncoding($data['category_name']);
71            } else {
72                $text[$i][] = 'n/a';
73            }
74        }
75        if ($useTranslation) {
76            $text[$i][] = $data['faq_translations'];
77        }
78        if ($useLanguage && isset($languageCodes[strtoupper($data['faq_language'])])) {
79            $text[$i][] = $report->convertEncoding($languageCodes[strtoupper($data['faq_language'])]);
80        }
81        if ($useId) {
82            $text[$i][] = $data['faq_id'];
83        }
84        if ($useSticky) {
85            $text[$i][] = $data['faq_sticky'];
86        }
87        if ($useTitle) {
88            $text[$i][] = $report->convertEncoding($data['faq_question']);
89        }
90        if ($useCreationDate) {
91            $text[$i][] = $data['faq_updated'];
92        }
93        if ($useOwner) {
94            $text[$i][] = $report->convertEncoding($data['faq_org_author']);
95        }
96        if ($useLastModified && isset($data['faq_last_author'])) {
97            $text[$i][] = $report->convertEncoding($data['faq_last_author']);
98        } else {
99            $text[$i][] = '';
100        }
101        if ($useUrl) {
102            $text[$i][] = $report->convertEncoding(
103                sprintf('%sindex.php?action=faq&amp;cat=%d&amp;id=%d&amp;artlang=%s',
104                    $faqConfig->getDefaultUrl(),
105                    $data['category_id'],
106                    $data['faq_id'],
107                    $data['faq_language']
108                )
109            );
110        }
111        if ($useVisits) {
112            $text[$i][] = $data['faq_visits'];
113        }
114    }
115
116    $content = '';
117    foreach ($text as $row) {
118        $content .= implode(';', $row);
119        $content .= "\r\n";
120    }
121
122    $oHttpStreamer = new HttpStreamer('csv', $content);
123    $oHttpStreamer->send(HttpStreamer::HTTP_CONTENT_DISPOSITION_ATTACHMENT);
124} else {
125    echo $PMF_LANG['err_noArticles'];
126}
127