1<?php
2
3/**
4 * Shows the page with the news record and - when available - the user
5 * comments.
6 *
7 * This Source Code Form is subject to the terms of the Mozilla Public License,
8 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
9 * obtain one at http://mozilla.org/MPL/2.0/.
10 *
11 * @package phpMyFAQ
12 * @author Thorsten Rinne <thorsten@phpmyfaq.de>
13 * @author Matteo Scaramuccia <matteo@scaramuccia.com>
14 * @copyright 2006-2020 phpMyFAQ Team
15 * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
16 * @link https://www.phpmyfaq.de
17 * @since 2006-07-23
18 */
19
20use phpMyFAQ\Captcha;
21use phpMyFAQ\Comments;
22use phpMyFAQ\Date;
23use phpMyFAQ\Entity\CommentType;
24use phpMyFAQ\Filter;
25use phpMyFAQ\Glossary;
26use phpMyFAQ\Helper\CaptchaHelper;
27use phpMyFAQ\News;
28use phpMyFAQ\User\CurrentUser;
29
30if (!defined('IS_VALID_PHPMYFAQ')) {
31    http_response_code(400);
32    exit();
33}
34
35$captcha = new Captcha($faqConfig);
36$comment = new Comments($faqConfig);
37
38$captcha->setSessionId($sids);
39if (!is_null($showCaptcha)) {
40    $captcha->drawCaptchaImage();
41    exit;
42}
43
44$oNews = new News($faqConfig);
45$newsId = Filter::filterInput(INPUT_GET, 'newsid', FILTER_VALIDATE_INT);
46
47if (is_null($newsId)) {
48    header('Location: http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['SCRIPT_NAME']));
49    exit();
50}
51
52try {
53    $faqSession->userTracking('news_view', $newsId);
54} catch (Exception $e) {
55    // @todo handle the exception
56}
57
58// Define the header of the page
59$newsMainHeader = $faqConfig->get('main.titleFAQ') . $PMF_LANG['msgNews'];
60if ($faqConfig->get('main.enableRssFeeds')) {
61    $newsFeed = '&nbsp;<a href="feed/news/rss.php" target="_blank"><i class="fa fa-rss-square"></i></a>';
62} else {
63    $newsFeed = '';
64}
65
66// Get all data from the news record
67$news = $oNews->getNewsEntry($newsId);
68
69$newsContent = $news['content'];
70$newsHeader = $news['header'];
71
72// Add Glossary entries
73$oGlossary = new Glossary($faqConfig);
74$newsContent = $oGlossary->insertItemsIntoContent($newsContent);
75$newsHeader = $oGlossary->insertItemsIntoContent($newsHeader);
76
77// Add information link if existing
78if (strlen($news['link']) > 0) {
79    $newsContent .= sprintf('</p><p>%s<a href="%s" target="%s">%s</a>',
80        $PMF_LANG['msgInfo'],
81        $news['link'],
82        $news['target'],
83        $news['linkTitle']);
84}
85
86// Show link to edit the news?
87$editThisEntry = '';
88if ($user->perm->checkRight($user->getUserId(), 'editnews')) {
89    $editThisEntry = sprintf(
90        '<a href="./admin/index.php?action=news&amp;do=edit&amp;id=%d">%s</a>',
91        $newsId,
92        $PMF_LANG['ad_menu_news_edit']);
93}
94
95// Is the news item expired?
96$expired = (date('YmdHis') > $news['dateEnd']);
97
98// Does the user have the right to add a comment?
99if ((-1 === $user->getUserId() && !$faqConfig->get('records.allowCommentsForGuests')) ||
100    (!$news['active']) || (!$news['allowComments']) || $expired) {
101    $commentMessage = $PMF_LANG['msgWriteNoComment'];
102} else {
103    $commentMessage = sprintf('<a href="#" class="show-comment-form">%s</a>', $PMF_LANG['newsWriteComment']);
104}
105
106// date of news entry
107if ($news['active'] && (!$expired)) {
108    $date = new Date($faqConfig);
109    $newsDate = sprintf('%s<span id="newsLastUpd">%s</span>',
110        $PMF_LANG['msgLastUpdateArticle'],
111        $date->format($news['date'])
112    );
113} else {
114    $newsDate = '';
115}
116
117$captchaHelper = new CaptchaHelper($faqConfig);
118
119$template->parse(
120    'mainPageContent',
121    [
122        'writeNewsHeader' => $newsMainHeader,
123        'writeNewsRSS' => $newsFeed,
124        'writeHeader' => $newsHeader,
125        'mainPageContent' => $newsContent,
126        'writeDateMsg' => $newsDate,
127        'msgAboutThisNews' => $PMF_LANG['msgAboutThisNews'],
128        'writeAuthor' => ($news['active'] && (!$expired)) ? $PMF_LANG['msgAuthor'] . ': ' . $news['authorName'] : '',
129        'editThisEntry' => $editThisEntry,
130        'writeCommentMsg' => $commentMessage,
131        'msgWriteComment' => $PMF_LANG['newsWriteComment'],
132        'newsId' => $newsId,
133        'newsLang' => $news['lang'],
134        'msgCommentHeader' => $PMF_LANG['msgCommentHeader'],
135        'msgNewContentName' => $PMF_LANG['msgNewContentName'],
136        'msgNewContentMail' => $PMF_LANG['msgNewContentMail'],
137        'defaultContentMail' => ($user instanceof CurrentUser) ? $user->getUserData('email') : '',
138        'defaultContentName' => ($user instanceof CurrentUser) ? $user->getUserData('display_name') : '',
139        'msgYourComment' => $PMF_LANG['msgYourComment'],
140        'msgNewContentSubmit' => $PMF_LANG['msgNewContentSubmit'],
141        'captchaFieldset' => $captchaHelper->renderCaptcha($captcha, 'writecomment', $PMF_LANG['msgCaptcha'], $auth),
142        'renderComments' => $comment->getComments($newsId, CommentType::NEWS),
143    ]
144);
145
146$template->parseBlock(
147    'index',
148    'breadcrumb',
149    [
150        'breadcrumbHeadline' => $newsMainHeader
151    ]
152);
153