Skip to content

Commit

Permalink
refactor: simplified session class
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Mar 23, 2024
1 parent 3e70237 commit 1839c49
Show file tree
Hide file tree
Showing 23 changed files with 147 additions and 284 deletions.
8 changes: 3 additions & 5 deletions phpmyfaq/404.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@
* @since 2019-01-25
*/

use phpMyFAQ\Enums\SessionActionType;

if (!defined('IS_VALID_PHPMYFAQ')) {
http_response_code(400);
exit();
}

try {
$faqSession->userTracking('404', 0);
} catch (Exception $exception) {
$faqConfig->getLogger()->error('Tracking of 404 page', ['exception' => $exception->getMessage()]);
}
$faqSession->userTracking(SessionActionType::NOT_FOUND->value, 0);

$template->parse('mainPageContent', []);
6 changes: 1 addition & 5 deletions phpmyfaq/add.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,7 @@

$questionObject = new Question($faqConfig);

try {
$faqSession->userTracking('new_entry', 0);
} catch (Exception $exception) {
$faqConfig->getLogger()->error('Tracking of new entry', ['exception' => $exception->getMessage()]);
}
$faqSession->userTracking('new_entry', 0);

// Get possible user input
$selectedQuestion = Filter::filterVar($request->query->get('question'), FILTER_VALIDATE_INT);
Expand Down
6 changes: 1 addition & 5 deletions phpmyfaq/ask.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@
$captcha = Captcha::getInstance($faqConfig);
$captcha->setSessionId($sids);

try {
$faqSession->userTracking('ask_question', 0);
} catch (Exception) {
// @todo handle the exception
}
$faqSession->userTracking('ask_question', 0);

$category->buildCategoryTree();

Expand Down
6 changes: 1 addition & 5 deletions phpmyfaq/contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@
exit();
}

try {
$faqSession->userTracking('contact', 0);
} catch (Exception) {
// @todo handle the exception
}
$faqSession->userTracking('contact', 0);

$faqConfig = Configuration::getConfigurationInstance();
$user = CurrentUser::getCurrentUser($faqConfig);
Expand Down
6 changes: 1 addition & 5 deletions phpmyfaq/faq.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,7 @@
$faqId = $faq->faqRecord['id'];
}

try {
$faqSession->userTracking('article_view', $faqId);
} catch (Exception) {
// @todo handle the exception
}
$faqSession->userTracking('article_view', $faqId);

$faqVisits = new Visits($faqConfig);
$faqVisits->logViews((int) $faqId);
Expand Down
6 changes: 1 addition & 5 deletions phpmyfaq/glossary.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@
exit();
}

try {
$faqSession->userTracking('glossary', 0);
} catch (Exception) {
// @todo handle the exception
}
$faqSession->userTracking('glossary', 0);

$request = Request::createFromGlobals();
$page = Filter::filterVar($request->query->get('page'), FILTER_VALIDATE_INT, 1);
Expand Down
6 changes: 1 addition & 5 deletions phpmyfaq/login.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@
$loginMessage = '<div class="alert alert-danger" role="alert">' . $error . '</div>';
}

try {
$faqSession->userTracking('login', 0);
} catch (Exception) {
// @todo handle the exception
}
$faqSession->userTracking('login', 0);

if ($faqConfig->get('security.enableRegistration')) {
$template->parseBlock(
Expand Down
6 changes: 1 addition & 5 deletions phpmyfaq/news.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@

$oNews = new News($faqConfig);

try {
$faqSession->userTracking('news_view', $newsId);
} catch (Exception) {
// @todo handle the exception
}
$faqSession->userTracking('news_view', $newsId);

// Define the header of the page
$newsMainHeader = $faqConfig->getTitle() . Translation::get('msgNews');
Expand Down
6 changes: 1 addition & 5 deletions phpmyfaq/open-questions.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@
exit();
}

try {
$faqSession->userTracking('open_questions', 0);
} catch (Exception) {
// @todo handle the exception
}
$faqSession->userTracking('open_questions', 0);

try {
$template->parse(
Expand Down
6 changes: 1 addition & 5 deletions phpmyfaq/overview.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@
exit();
}

try {
$faqSession->userTracking('overview', 0);
} catch (Exception) {
// @todo handle the exception
}
$faqSession->userTracking('overview', 0);

$faqHelper = new FaqHelper($faqConfig);

Expand Down
6 changes: 1 addition & 5 deletions phpmyfaq/password.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@
exit();
}

try {
$faqSession->userTracking('forgot_password', 0);
} catch (Exception $exception) {
$faqConfig->getLogger()->error('Tracking of forgot password', ['exception' => $exception->getMessage()]);
}
$faqSession->userTracking('forgot_password', 0);

if ($faqConfig->get('security.enableRegistration')) {
$template->parseBlock(
Expand Down
6 changes: 1 addition & 5 deletions phpmyfaq/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@
$redirect->send();
}

try {
$faqSession->userTracking('registration', 0);
} catch (Exception) {
// @todo handle the exception
}
$faqSession->userTracking('registration', 0);

$captcha = Captcha::getInstance($faqConfig);
$captcha->setSessionId($sids);
Expand Down
6 changes: 1 addition & 5 deletions phpmyfaq/request-removal.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@
exit();
}

try {
$faqSession->userTracking('request_removal', 0);
} catch (Exception $exception) {
$faqConfig->getLogger()->error('Tracking of request removal', ['exception' => $exception->getMessage()]);
}
$faqSession->userTracking('request_removal', 0);

$template->parse(
'mainPageContent',
Expand Down
12 changes: 2 additions & 10 deletions phpmyfaq/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@
exit();
}

try {
$faqSession->userTracking('fulltext_search', 0);
} catch (Exception) {
// @todo handle the exception
}
$faqSession->userTracking('fulltext_search', 0);

$faqConfig = Configuration::getConfigurationInstance();

Expand Down Expand Up @@ -201,11 +197,7 @@
// Change a little the $searchCategory value;
$inputCategory = ('%' == $inputCategory) ? 0 : $inputCategory;

try {
$faqSession->userTracking('fulltext_search', $inputSearchTerm);
} catch (Exception) {
// @todo handle the exception
}
$faqSession->userTracking('fulltext_search', $inputSearchTerm);

if (
is_numeric(
Expand Down
6 changes: 1 addition & 5 deletions phpmyfaq/send2friend.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@
exit();
}

try {
$faqSession->userTracking('send2friend', 0);
} catch (Exception) {
// @todo handle the exception
}
$faqSession->userTracking('send2friend', 0);

$request = Request::createFromGlobals();
$faqCategory = Filter::filterVar($request->query->get('cat'), FILTER_VALIDATE_INT);
Expand Down
12 changes: 2 additions & 10 deletions phpmyfaq/show.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@
$categoryHelper->setPlurals(new Plurals());

if (!is_null($selectedCategoryId) && isset($category->categoryName[$selectedCategoryId])) {
try {
$faqSession->userTracking('show_category', $selectedCategoryId);
} catch (Exception) {
// @todo handle the exception
}
$faqSession->userTracking('show_category', $selectedCategoryId);

$categoryData = $category->getCategoryData($selectedCategoryId);
$records = $faq->renderRecordsByCategoryId(
Expand Down Expand Up @@ -126,11 +122,7 @@
);
} else {
$selectedCategoryId = 0;
try {
$faqSession->userTracking('show_all_categories', 0);
} catch (Exception) {
// @todo handle the exception
}
$faqSession->userTracking('show_all_categories', 0);

$categoryHelper
->setConfiguration($faqConfig)
Expand Down
6 changes: 1 addition & 5 deletions phpmyfaq/sitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@
exit();
}

try {
$faqSession->userTracking('sitemap', 0);
} catch (Exception) {
// @todo handle the exception
}
$faqSession->userTracking('sitemap', 0);

$request = Request::createFromGlobals();
$letter = Filter::filterVar($request->query->get('letter'), FILTER_SANITIZE_SPECIAL_CHARS);
Expand Down
16 changes: 2 additions & 14 deletions phpmyfaq/src/phpMyFAQ/Controller/Frontend/CommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,7 @@ public function create(Request $request): JsonResponse
!empty($username) && !empty($mailer) && !empty($commentText) && $stopWords->checkBannedWord($commentText) &&
$comment->isCommentAllowed($id, $languageCode, $type) && !$faq->isActive($id, $languageCode, $type)
) {
try {
$session->userTracking('save_comment', $id);
} catch (Exception $exception) {
$this->configuration->getLogger()->error(
'Tracking of save new comment',
['exception' => $exception->getMessage()]
);
}

$session->userTracking('save_comment', $id);
$commentEntity = new Comment();
$commentEntity
->setRecordId($id)
Expand All @@ -148,11 +140,7 @@ public function create(Request $request): JsonResponse

return $this->json(['success' => Translation::get('msgCommentThanks')], Response::HTTP_OK);
} else {
try {
$session->userTracking('error_save_comment', $id);
} catch (Exception) {
// @todo handle the exception
}
$session->userTracking('error_save_comment', $id);
return $this->json(['error' => Translation::get('errSaveComment')], Response::HTTP_BAD_REQUEST);
}
} else {
Expand Down
6 changes: 1 addition & 5 deletions phpmyfaq/src/phpMyFAQ/Controller/Frontend/FaqController.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,7 @@ public function create(Request $request): JsonResponse
$stopWords->checkBannedWord(strip_tags($questionText)) &&
($answer !== '' && $answer !== '0') && $stopWords->checkBannedWord(strip_tags($answer))
) {
try {
$session->userTracking('save_new_entry', 0);
} catch (Exception) {
// @todo handle the exception
}
$session->userTracking('save_new_entry', 0);

if (!empty($contentLink) && Strings::substr($contentLink, 7) !== '') {
$answer = sprintf(
Expand Down
20 changes: 3 additions & 17 deletions phpmyfaq/src/phpMyFAQ/Controller/Frontend/VotingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@ public function create(Request $request): JsonResponse
$userIp = Filter::filterVar($request->server->get('REMOTE_ADDR'), FILTER_VALIDATE_IP);

if (isset($vote) && $rating->check($faqId, $userIp) && $vote > 0 && $vote < 6) {
try {
$session->userTracking('save_voting', $faqId);
} catch (Exception $exception) {
$this->configuration->getLogger()->error('Error saving voting', ['exception' => $exception]);
}
$session->userTracking('save_voting', $faqId);

$votingData = [
'record_id' => $faqId,
Expand All @@ -73,20 +69,10 @@ public function create(Request $request): JsonResponse
Response::HTTP_OK
);
} elseif (!$rating->check($faqId, $userIp)) {
try {
$session->userTracking('error_save_voting', $faqId);
} catch (Exception $exception) {
return $this->json(['error' => $exception->getMessage()], Response::HTTP_BAD_REQUEST);
}

$session->userTracking('error_save_voting', $faqId);
return $this->json(['error' => Translation::get('err_VoteTooMuch')], Response::HTTP_BAD_REQUEST);
} else {
try {
$session->userTracking('error_save_voting', $faqId);
} catch (Exception $exception) {
return $this->json(['error' => $exception->getMessage()], Response::HTTP_BAD_REQUEST);
}

$session->userTracking('error_save_voting', $faqId);
return $this->json(['error' => Translation::get('err_noVote')], Response::HTTP_BAD_REQUEST);
}
}
Expand Down
24 changes: 24 additions & 0 deletions phpmyfaq/src/phpMyFAQ/Enums/SessionActionType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* Session actions enum
*
* 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 <thorsten@phpmyfaq.de>
* @copyright 2024 phpMyFAQ Team
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
* @link https://www.phpmyfaq.de
* @since 2024-03-23
*/

namespace phpMyFAQ\Enums;

enum SessionActionType: string
{
case NOT_FOUND = '404';
case OLD_SESSION = 'old_session';
}

0 comments on commit 1839c49

Please sign in to comment.