-
Notifications
You must be signed in to change notification settings - Fork 0
/
faq.php
383 lines (343 loc) · 12.4 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
380
381
382
383
<?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 http://mozilla.org/MPL/2.0/.
*
* @package phpMyFAQ
* @author Thorsten Rinne <thorsten@phpmyfaq.de>
* @author Lars Tiedemann <larstiedemann@yahoo.de>
* @copyright 2002-2020 phpMyFAQ Team
* @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
* @link https://www.phpmyfaq.de
* @since 2002-08-27
*/
use phpMyFAQ\Attachment\AttachmentException;
use phpMyFAQ\Attachment\AttachmentFactory;
use phpMyFAQ\Captcha;
use phpMyFAQ\Comments;
use phpMyFAQ\Date;
use phpMyFAQ\Entity\CommentType;
use phpMyFAQ\Filter;
use phpMyFAQ\Glossary;
use phpMyFAQ\Helper\AttachmentHelper;
use phpMyFAQ\Helper\CaptchaHelper;
use phpMyFAQ\Helper\FaqHelper as HelperFaq;
use phpMyFAQ\Helper\LanguageHelper;
use phpMyFAQ\Helper\SearchHelper;
use phpMyFAQ\Link;
use phpMyFAQ\LinkVerifier;
use phpMyFAQ\Rating;
use phpMyFAQ\Relation;
use phpMyFAQ\Search\SearchResultSet as SearchResultset;
use phpMyFAQ\Services;
use phpMyFAQ\Strings;
use phpMyFAQ\Tags;
use phpMyFAQ\User\CurrentUser;
use phpMyFAQ\Utils;
use phpMyFAQ\Visits;
if (!defined('IS_VALID_PHPMYFAQ')) {
http_response_code(400);
exit();
}
$captcha = new Captcha($faqConfig);
$oGlossary = new Glossary($faqConfig);
$faqTagging = new Tags($faqConfig);
$faqRelation = new Relation($faqConfig);
$faqRating = new Rating($faqConfig);
$faqComment = new Comments($faqConfig);
$markDown = new \ParsedownExtra();
$faqHelper = new HelperFaq($faqConfig);
$attachmentHelper = new AttachmentHelper();
if (is_null($user)) {
$user = new CurrentUser($faqConfig);
}
$faqSearchResult = new SearchResultset($user, $faq, $faqConfig);
$captcha->setSessionId($sids);
if (!is_null($showCaptcha)) {
$captcha->drawCaptchaImage();
exit;
}
$currentCategory = $cat;
$recordId = Filter::filterInput(INPUT_GET, 'id', FILTER_VALIDATE_INT);
$solutionId = Filter::filterInput(INPUT_GET, 'solution_id', FILTER_VALIDATE_INT);
// Get all data from the FAQ record
if (0 === (int)$solutionId) {
$faq->getRecord($recordId);
} else {
$faq->getRecordBySolutionId($solutionId);
}
$recordId = $faq->faqRecord['id'];
try {
$faqSession->userTracking('article_view', $recordId);
} catch (Exception $e) {
// @todo handle the exception
}
$faqVisits = new Visits($faqConfig);
$faqVisits->logViews($recordId);
$question = $faq->getRecordTitle($recordId);
if ($faqConfig->get('main.enableMarkdownEditor')) {
$answer = $markDown->text($faq->faqRecord['content']);
} else {
$answer = $faqHelper->renderMarkupContent($faq->faqRecord['content']);
}
// Rewrite URL fragments
$currentUrl = htmlspecialchars("//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}", ENT_QUOTES, 'UTF-8');
$answer = $faqHelper->rewriteUrlFragments($answer, $currentUrl);
// Add Glossary entries for answers only
$answer = $oGlossary->insertItemsIntoContent($answer);
// Set the path of the current category
$categoryName = $category->getPath($currentCategory, ' » ', true, '');
$highlight = Filter::filterInput(INPUT_GET, 'highlight', FILTER_SANITIZE_STRIPPED);
if (!is_null($highlight) && $highlight != '/' && $highlight != '<' && $highlight != '>' && Strings::strlen(
$highlight
) > 3) {
$highlight = str_replace("'", '´', $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);
}
}
}
$linkVerifier = new LinkVerifier($faqConfig);
$linkArray = $linkVerifier->getUrlPool();
if (isset($linkArray['href'])) {
foreach (array_unique($linkArray['href']) as $_url) {
$xpos = strpos($_url, 'index.php?action=faq');
if (!($xpos === false)) {
// Get the FaqHelper link title
$matches = [];
preg_match('/id=([\d]+)/ism', $_url, $matches);
$_id = $matches[1];
$_title = $faq->getRecordTitle($_id);
$_link = substr($_url, $xpos + 9);
if (strpos($_url, '&') === false) {
$_link = str_replace('&', '&', $_link);
}
$oLink = new Link($faqConfig->getDefaultUrl() . $_link, $faqConfig);
$oLink->itemTitle = $oLink->tooltip = $_title;
$newFaqPath = $oLink->toString();
$answer = str_replace($_url, $newFaqPath, $answer);
}
}
}
// List all faq attachments
if ($faqConfig->get('records.disableAttachments') && 'yes' == $faq->faqRecord['active']) {
try {
$attList = AttachmentFactory::fetchByRecordId($faqConfig, $recordId);
$answer .= $attachmentHelper->renderAttachmentList($attList);
} catch (AttachmentException $e) {
// handle exception
}
}
// List all categories for this faq
$htmlAllCategories = '';
$multiCategories = $category->getCategoriesFromFaq($recordId);
if (count($multiCategories) > 1) {
foreach ($multiCategories as $multiCat) {
$path = $category->getPath($multiCat['id'], ' » ', true, 'breadcrumb-related-categories');
if ('' === trim($path)) {
continue;
}
$htmlAllCategories .= $path;
}
}
// Related FAQs
$faqSearchResult->reviewResultSet(
$faqRelation->getAllRelatedById(
$recordId,
$faq->faqRecord['title'],
$faq->faqRecord['keywords']
)
);
$searchHelper = new SearchHelper($faqConfig);
$relatedFaqs = $searchHelper->renderRelatedFaqs($faqSearchResult, $recordId);
// Show link to edit the faq?
$editThisEntry = '';
if ($user->perm->checkRight($user->getUserId(), 'edit_faq')) {
$editThisEntry = sprintf(
'<i aria-hidden="true" class="fa fa-pencil"></i> <a class="data" href="./admin/index.php?action=editentry&id=%d&lang=%s">%s</a>',
$recordId,
$lang,
$PMF_LANG['ad_entry_edit_1'] . ' ' . $PMF_LANG['ad_entry_edit_2']
);
}
// Is the faq expired?
$expired = (date('YmdHis') > $faq->faqRecord['dateEnd']);
// Number of comments
$numComments = $faqComment->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 = $PMF_LANG['msgWriteNoComment'];
} else {
$commentMessage = sprintf(
'%s<a href="#" class="show-comment-form">%s</a>',
$PMF_LANG['msgYouCan'],
$PMF_LANG['msgWriteComment']
);
$template->parseBlock(
'mainPageContent',
'enableComments',
[
'numberOfComments' => sprintf(
'%d %s',
isset($numComments[$recordId]) ? $numComments[$recordId] : 0,
$PMF_LANG['ad_start_comments']
),
]
);
}
$translationUrl = sprintf(
str_replace(
'%',
'%%',
Link::getSystemRelativeUri('index.php')
) . 'index.php?%saction=translate&cat=%s&id=%d&srclang=%s',
$sids,
$currentCategory,
$recordId,
$lang
);
$availableLanguages = $faqConfig->getLanguage()->languageAvailable($faq->faqRecord['id']);
if (!empty($availableLanguages) && count($availableLanguages) > 1) {
$template->parseBlock(
'mainPageContent',
'switchLanguage',
[
'msgChangeLanguage' => $PMF_LANG['msgLanguageSubmit'],
]
);
}
if ($user->perm->checkRight($user->getUserId(), 'addtranslation') &&
!empty($availableLanguages) && count($availableLanguages) > 1) {
$template->parseBlock(
'mainPageContent',
'addTranslation',
[
'msgTranslate' => $PMF_LANG['msgTranslate'],
]
);
}
if ($user->perm->checkRight($user->getUserId(), 'edit_faq') && !empty($faq->faqRecord['notes'])) {
$template->parseBlock(
'mainPageContent',
'privateNotes',
[
'notesHeader' => $PMF_LANG['ad_admin_notes'],
'notes' => $faq->faqRecord['notes']
]
);
}
if ('-' !== $faqTagging->getAllLinkTagsById($recordId)) {
$template->parseBlock(
'mainPageContent',
'tagsAvailable',
[
'renderTags' => $PMF_LANG['msg_tags'] . ': ' . $faqTagging->getAllLinkTagsById($recordId),
]
);
}
if ('' !== $htmlAllCategories) {
$template->parseBlock(
'mainPageContent',
'relatedCategories',
[
'renderRelatedCategoriesHeader' => $PMF_LANG['msgArticleCategories'],
'renderRelatedCategories' => $htmlAllCategories,
]
);
}
if ('' !== $relatedFaqs) {
$template->parseBlock(
'mainPageContent',
'relatedFaqs',
[
'renderRelatedArticlesHeader' => $PMF_LANG['msg_related_articles'],
'renderRelatedArticles' => $relatedFaqs,
]
);
}
$date = new Date($faqConfig);
$captchaHelper = new CaptchaHelper($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($recordId, $currentCategory)) {
$http->setStatus(404);
}
$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' => $faq->faqRecord['author'],
'editThisEntry' => $editThisEntry,
'msgPdf' => $PMF_LANG['msgPDF'],
'msgPrintFaq' => $PMF_LANG['msgPrintArticle'],
'sendToFriend' => $faqHelper->renderSendToFriend($faqServices->getSuggestLink()),
'shareOnTwitter' => $faqHelper->renderTwitterShareLink($faqServices->getShareOnTwitterLink()),
'linkToPdf' => $faqServices->getPdfLink(),
'translationUrl' => $translationUrl,
'languageSelection' => LanguageHelper::renderSelectLanguage(
$faqLangCode,
false,
$availableLanguages,
'translation'
),
'msgTranslateSubmit' => $PMF_LANG['msgTranslateSubmit'],
'saveVotingPATH' => sprintf(
str_replace(
'%',
'%%',
$faqConfig->getDefaultUrl()
) . 'index.php?%saction=savevoting',
$sids
),
'saveVotingID' => $recordId,
'saveVotingIP' => $_SERVER['REMOTE_ADDR'],
'msgAverageVote' => $PMF_LANG['msgAverageVote'],
'renderVotingStars' => '',
'printVotings' => $faqRating->getVotingResult($recordId),
'switchLanguage' => $faqHelper->renderChangeLanguageSelector($faq, $currentCategory),
'msgVoteUsability' => $PMF_LANG['msgVoteUsability'],
'msgVoteBad' => $PMF_LANG['msgVoteBad'],
'msgVoteGood' => $PMF_LANG['msgVoteGood'],
'msgVoteSubmit' => $PMF_LANG['msgVoteSubmit'],
'writeCommentMsg' => $commentMessage,
'msgWriteComment' => $PMF_LANG['msgWriteComment'],
'id' => $recordId,
'lang' => $lang,
'msgCommentHeader' => $PMF_LANG['msgCommentHeader'],
'msgNewContentName' => $PMF_LANG['msgNewContentName'],
'msgNewContentMail' => $PMF_LANG['msgNewContentMail'],
'defaultContentMail' => ($user instanceof CurrentUser) ? $user->getUserData('email') : '',
'defaultContentName' => ($user instanceof CurrentUser) ? $user->getUserData('display_name') : '',
'msgYourComment' => $PMF_LANG['msgYourComment'],
'msgNewContentSubmit' => $PMF_LANG['msgNewContentSubmit'],
'captchaFieldset' => $captchaHelper->renderCaptcha($captcha, 'writecomment', $PMF_LANG['msgCaptcha'], $auth),
'renderComments' => $faqComment->getComments($recordId, CommentType::FAQ),
'msg_about_faq' => $PMF_LANG['msg_about_faq'],
]
);
$template->parseBlock(
'index',
'breadcrumb',
[
'breadcrumbHeadline' => $categoryName
]
);