-
-
Notifications
You must be signed in to change notification settings - Fork 258
/
Copy pathfaq.php
379 lines (338 loc) · 12.8 KB
/
faq.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php
/**
* Shows the page with the FAQ record and - when available - the user comments.
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at https://mozilla.org/MPL/2.0/.
*
* @package phpMyFAQ
* @author Thorsten Rinne <[email protected]>
* @author Lars Tiedemann <[email protected]>
* @copyright 2002-2023 phpMyFAQ Team
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
* @link https://www.phpmyfaq.de
* @since 2002-08-27
*/
use phpMyFAQ\Component\Alert;
use phpMyFAQ\Attachment\AttachmentException;
use phpMyFAQ\Attachment\AttachmentFactory;
use phpMyFAQ\Bookmark;
use phpMyFAQ\Captcha\Captcha;
use phpMyFAQ\Captcha\Helper\CaptchaHelper;
use phpMyFAQ\Comments;
use phpMyFAQ\Date;
use phpMyFAQ\Entity\CommentType;
use phpMyFAQ\Faq\FaqPermission;
use phpMyFAQ\Filter;
use phpMyFAQ\Glossary;
use phpMyFAQ\Helper\AttachmentHelper;
use phpMyFAQ\Helper\FaqHelper as HelperFaq;
use phpMyFAQ\Helper\SearchHelper;
use phpMyFAQ\Link;
use phpMyFAQ\Rating;
use phpMyFAQ\Relation;
use phpMyFAQ\Search\SearchResultSet;
use phpMyFAQ\Services;
use phpMyFAQ\Session\Token;
use phpMyFAQ\Strings;
use phpMyFAQ\Tags;
use phpMyFAQ\Translation;
use phpMyFAQ\User\CurrentUser;
use phpMyFAQ\Utils;
use phpMyFAQ\Visits;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
if (!defined('IS_VALID_PHPMYFAQ')) {
http_response_code(400);
exit();
}
$glossary = new Glossary($faqConfig);
$tagging = new Tags($faqConfig);
$relation = new Relation($faqConfig);
$rating = new Rating($faqConfig);
$comment = new Comments($faqConfig);
$markDown = new ParsedownExtra();
$faqHelper = new HelperFaq($faqConfig);
$faqPermission = new FaqPermission($faqConfig);
$attachmentHelper = new AttachmentHelper();
if (is_null($user)) {
$user = new CurrentUser($faqConfig);
}
$faqSearchResult = new SearchResultSet($user, $faqPermission, $faqConfig);
$captcha = Captcha::getInstance($faqConfig);
$captcha->setSessionId($sids);
if ($showCaptcha !== '') {
$captcha->drawCaptchaImage();
exit;
}
$currentCategory = $cat;
$request = Request::createFromGlobals();
$faqId = Filter::filterVar($request->query->get('id'), FILTER_VALIDATE_INT, 0);
$solutionId = Filter::filterVar($request->query->get('solution_id'), FILTER_VALIDATE_INT);
$highlight = Filter::filterVar($request->query->get('highlight'), FILTER_SANITIZE_SPECIAL_CHARS);
$bookmarkAction = Filter::filterVar($request->query->get('bookmark_action'), FILTER_SANITIZE_SPECIAL_CHARS);
// Handle bookmarks
$bookmark = new Bookmark($faqConfig, $user);
if ($bookmarkAction === 'add' && isset($faqId)) {
$bookmark->saveFaqAsBookmarkById($faqId);
$alert = new Alert();
$bookmarkAlert = $alert->success('msgBookmarkAdded');
}
if ($bookmarkAction === 'remove' && isset($faqId)) {
$bookmark->remove($faqId);
$alert = new Alert();
$bookmarkAlert = $alert->success('msgBookmarkRemoved');
}
// Get all data from the FAQ record
if (0 === (int)$solutionId) {
$faq->getRecord($faqId);
} else {
$faq->getRecordBySolutionId($solutionId);
}
if (isset($faq->faqRecord['id'])) {
$faqId = $faq->faqRecord['id'];
}
try {
$faqSession->userTracking('article_view', $faqId);
} catch (Exception) {
// @todo handle the exception
}
$faqVisits = new Visits($faqConfig);
$faqVisits->logViews((int) $faqId);
$question = $faq->getRecordTitle($faqId);
$question = Strings::htmlentities($question);
if ($faqConfig->get('main.enableMarkdownEditor')) {
$answer = $markDown->text($faq->faqRecord['content']);
} else {
$answer = $faqHelper->renderMarkupContent($faq->faqRecord['content']);
}
// Cleanup answer content first
$answer = $faqHelper->cleanUpContent($answer);
// Rewrite URL fragments
$currentUrl = htmlspecialchars("//{$request->getHost()}{$request->getRequestUri()}", ENT_QUOTES, 'UTF-8');
$answer = $faqHelper->rewriteUrlFragments($answer, $currentUrl);
// Add Glossary entries for answers only
$answer = $glossary->insertItemsIntoContent($answer);
// Set the path of the current category
$categoryName = $category->getPath($currentCategory, ' » ', true);
if (
!is_null($highlight) && $highlight != '/' && $highlight != '<' && $highlight != '>' && Strings::strlen(
$highlight
) > 3
) {
$highlight = str_replace("'", '´', (string) $highlight);
$highlight = str_replace(['^', '.', '?', '*', '+', '{', '}', '(', ')', '[', ']'], '', $highlight);
$highlight = preg_quote($highlight, '/');
$searchItems = explode(' ', $highlight);
foreach ($searchItems as $item) {
if (Strings::strlen($item) > 2) {
$question = Utils::setHighlightedString($question, $item);
$answer = Utils::setHighlightedString($answer, $item);
}
}
}
// List all faq attachments
if ($faqConfig->get('records.disableAttachments') && 'yes' == $faq->faqRecord['active']) {
try {
$attList = AttachmentFactory::fetchByRecordId($faqConfig, $faqId);
$answer .= $attachmentHelper->renderAttachmentList($attList);
} catch (AttachmentException) {
// handle exception
}
}
// List all categories for this faq
$renderedCategoryPath = '';
$multiCategories = $category->getCategoriesFromFaq($faqId);
if ((is_countable($multiCategories) ? count($multiCategories) : 0) > 1) {
foreach ($multiCategories as $multiCat) {
$path = $category->getPath($multiCat['id'], ' » ', true, 'breadcrumb-related-categories');
if ('' === trim($path)) {
continue;
}
$renderedCategoryPath .= $path;
}
}
// Related FAQs
try {
$faqSearchResult->reviewResultSet(
$relation->getAllRelatedByQuestion(
Strings::htmlentities($faq->faqRecord['title']),
Strings::htmlentities($faq->faqRecord['keywords'])
)
);
} catch (Exception) {
// handle exception
}
$searchHelper = new SearchHelper($faqConfig);
$relatedFaqs = $searchHelper->renderRelatedFaqs($faqSearchResult, $faqId);
// Show link to edit the faq?
$editThisEntry = '';
if ($user->perm->hasPermission($user->getUserId(), 'edit_faq')) {
$editThisEntry = sprintf(
'<i aria-hidden="true" class="fa fa-pencil"></i> ' .
'<a class="text-decoration-none" href="./admin/index.php?action=editentry&id=%d&lang=%s">%s</a>',
$faqId,
$lang,
Translation::get('ad_entry_edit_1') . ' ' . Translation::get('ad_entry_edit_2')
);
}
// Is the faq expired?
$expired = (date('YmdHis') > $faq->faqRecord['dateEnd']);
// Number of comments
$numComments = $comment->getNumberOfComments();
// Does the user have the right to add a comment?
if (
(-1 === $user->getUserId() && !$faqConfig->get('records.allowCommentsForGuests')) ||
($faq->faqRecord['active'] === 'no') || ('n' === $faq->faqRecord['comment']) || $expired
) {
$commentMessage = Translation::get('msgWriteNoComment');
} else {
$commentMessage = sprintf(
'%s<a href="#" data-bs-toggle="modal" data-bs-target="#pmf-modal-add-comment">%s</a>',
Translation::get('msgYouCan'),
Translation::get('msgWriteComment')
);
$template->parseBlock(
'mainPageContent',
'enableComments',
[
'numberOfComments' => sprintf('%d %s', $numComments[$faqId] ?? 0, Translation::get('ad_start_comments')),
]
);
}
$availableLanguages = $faqConfig->getLanguage()->isLanguageAvailable($faq->faqRecord['id']);
if (!empty($availableLanguages) && (is_countable($availableLanguages) ? count($availableLanguages) : 0) > 1) {
$template->parseBlock(
'mainPageContent',
'switchLanguage',
[
'msgChangeLanguage' => Translation::get('msgLanguageSubmit'),
]
);
}
if ($user->perm->hasPermission($user->getUserId(), 'edit_faq') && !empty($faq->faqRecord['notes'])) {
$template->parseBlock(
'mainPageContent',
'privateNotes',
[
'notesHeader' => Translation::get('ad_admin_notes'),
'notes' => $faq->faqRecord['notes']
]
);
}
if ('-' !== $tagging->getAllLinkTagsById($faqId)) {
$template->parseBlock(
'mainPageContent',
'tagsAvailable',
[
'renderTagsHeader' => Translation::get('msg_tags'),
'renderTags' => $tagging->getAllLinkTagsById($faqId),
]
);
}
if ('' !== $renderedCategoryPath) {
$template->parseBlock(
'mainPageContent',
'relatedCategories',
[
'renderRelatedCategoriesHeader' => Translation::get('msgArticleCategories'),
'renderRelatedCategories' => $renderedCategoryPath,
]
);
}
if ('' !== $relatedFaqs) {
$template->parseBlock(
'mainPageContent',
'relatedFaqs',
[
'renderRelatedArticlesHeader' => Translation::get('msg_related_articles'),
'renderRelatedArticles' => $relatedFaqs,
]
);
}
$date = new Date($faqConfig);
$captchaHelper = CaptchaHelper::getInstance($faqConfig);
// We need some Links from social networks
$faqServices = new Services($faqConfig);
$faqServices->setCategoryId($cat);
$faqServices->setFaqId($id);
$faqServices->setLanguage($lang);
$faqServices->setQuestion($faq->getRecordTitle($id));
// Check if category ID and FAQ ID are linked together
if (!$category->categoryHasLinkToFaq($faqId, $currentCategory)) {
$response = new Response();
$response->setStatusCode(Response::HTTP_NOT_FOUND);
}
// Check if the author name should be visible, according to the GDPR option
if ($user->getUserVisibilityByEmail($faq->faqRecord['email'])) {
$author = $faq->faqRecord['author'];
} else {
$author = 'n/a';
}
$template->parse(
'mainPageContent',
[
'baseHref' => $faqSystem->getSystemUri($faqConfig),
'solutionId' => $faq->faqRecord['solution_id'],
'solutionIdLink' => Link::getSystemRelativeUri() . '?solution_id=' . $faq->faqRecord['solution_id'],
'question' => $question,
'answer' => $answer,
'faqDate' => $date->format($faq->faqRecord['date']),
'faqAuthor' => Strings::htmlentities($author),
'editThisEntry' => $editThisEntry,
'msgPdf' => Translation::get('msgPDF'),
'msgPrintFaq' => Translation::get('msgPrintArticle'),
'sendToFriend' => $faqHelper->renderSendToFriend($faqServices->getSuggestLink()),
'shareOnTwitter' => $faqHelper->renderTwitterShareLink($faqServices->getShareOnTwitterLink()),
'linkToPdf' => $faqServices->getPdfLink(),
'saveVotingID' => $faqId,
'msgAverageVote' => Translation::get('msgAverageVote'),
'renderVotingResult' => $rating->getVotingResult($faqId),
'switchLanguage' => $faqHelper->renderChangeLanguageSelector($faq, $currentCategory),
'msgVoteUsability' => Translation::get('msgVoteUsability'),
'msgVoteBad' => Translation::get('msgVoteBad'),
'msgVoteGood' => Translation::get('msgVoteGood'),
'msgVoteSubmit' => Translation::get('msgVoteSubmit'),
'writeCommentMsg' => $commentMessage,
'msgWriteComment' => Translation::get('msgWriteComment'),
'id' => $faqId,
'lang' => $lang,
'msgCommentHeader' => Translation::get('msgCommentHeader'),
'msgNewContentName' => Translation::get('msgNewContentName'),
'msgNewContentMail' => Translation::get('msgNewContentMail'),
'defaultContentMail' => ($user->getUserId() > 0) ? $user->getUserData('email') : '',
'defaultContentName' =>
($user->getUserId() > 0) ? Strings::htmlentities($user->getUserData('display_name')) : '',
'msgYourComment' => Translation::get('msgYourComment'),
'msgCancel' => Translation::get('ad_gen_cancel'),
'msgNewContentSubmit' => Translation::get('msgNewContentSubmit'),
'csrfInput' => Token::getInstance()->getTokenInput('add-comment'),
'captchaFieldset' =>
$captchaHelper->renderCaptcha(
$captcha,
'writecomment',
Translation::get('msgCaptcha'),
$user->isLoggedIn()
),
'renderComments' => $comment->getComments($faqId, CommentType::FAQ),
'msg_about_faq' => Translation::get('msg_about_faq'),
'bookmarkIcon' => $bookmark->isFaqBookmark($faqId) ? 'fa fa-bookmark' : 'fa fa-bookmark-o',
'bookmarkLink' =>
$bookmark->isFaqBookmark($faqId)
?
sprintf('index.php?action=faq&bookmark_action=remove&id=%d', $faqId)
:
sprintf('index.php?action=faq&bookmark_action=add&id=%d', $faqId),
'msgAddBookmark' =>
$bookmark->isFaqBookmark($faqId) ? Translation::get('removeBookmark') : Translation::get('msgAddBookmark'),
'alert' => (isset($bookmarkAlert)) ? $bookmarkAlert : '',
]
);
$template->parseBlock(
'index',
'breadcrumb',
[
'breadcrumbHeadline' => $categoryName
]
);