<?php
namespace App\BackendBundle\Helper;
use DateTime;
use DateInterval;
Use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use App\Entity\SchoolClass;
class ValidationHelper {
private RequestStack $requestStack;
private SessionInterface $session;
/* @var $logger LoggerInterface */
private $logger;
private $minutesValid;
private $formSaltText;
private $scoreSaltText;
public function __construct(RequestStack $requestStack, LoggerInterface $logger) {
$this->requestStack = $requestStack;
$this->session = $this->requestStack->getSession();
$this->logger = $logger;
$this->formSaltText = "form-random-salt123";
$this->scoreSaltText = "score-csrf-text-2022";
$this->minutesValid = 5;
}
public function isEmailValid($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL);
}
public function getCommonValidationData() {
$dt = new DateTime();
$strDateTime = $dt->format("d.m.Y H:i:s");
$scoreSalt = $this->scoreSaltText;
$csrftoken = hash('sha256', "$strDateTime-$scoreSalt");
$data = array();
$data['datetime'] = $strDateTime;
$data["csrf"] = $csrftoken;
$this->session->set("common_csrf_token", $csrftoken);
return $data;
}
public function isCommonDataValid(Request $request) {
$submittedData = $this->getCommonSubmittedData($request);
if (empty($submittedData)) {
return false;
}
$submittedToken = $submittedData['csrf'];
$sessionToken = $this->session->get("common_csrf_token");
if (empty($sessionToken)) {
return false;
}
if ($sessionToken != $submittedToken) {
return false;
}
return true;
}
public function getCommonSubmittedData(Request $request) {
$data = array();
$strDateTime = $request->get('datetime');
if (empty($strDateTime)) {
$this->logger->error("Empty strDateTime");
return null;
}
$dt = DateTime::createFromFormat("d.m.Y H:i:s", $strDateTime);
if ($dt == false) {
$this->logger->error("Wrong date time format");
return null;
}
$now = new DateTime();
/* @var $interval DateInterval */
$interval = $dt->diff($now);
$minutes = $interval->i;
/* if submitted data is older as 'minutesValid' return null -> error */
if ($minutes > $this->minutesValid) {
$this->logger->error('Submitted data to old');
return null;
}
$data['dt'] = $dt->format('d.m.Y H:i');
$strCsrf = $request->get('csrf');
if (empty($strCsrf)) {
$this->logger->error('No csrf token submitted');
return null;
}
$data['csrf'] = $strCsrf;
return $data;
}
public function getScoreValidationData(SchoolClass $schoolClass, $round) {
$dt = new DateTime();
$strDateTime = $dt->format("d.m.Y H:i:s");
$schoolClassID = $schoolClass->getId();
$scoreSalt = $this->scoreSaltText;
$csrftoken = hash('sha256', "$strDateTime-$schoolClassID-$round-$scoreSalt");
$data = array();
$data['datetime'] = $strDateTime;
$data['schoolclassid'] = $schoolClassID;
$data['round'] = $round;
$data["csrf"] = $csrftoken;
$this->session->set("score_csrf_token", $csrftoken);
return $data;
}
public function isScoreValid(Request $request) {
$submittedData = $this->getScoreSubmittedData($request);
if (empty($submittedData)) {
return false;
}
$submittedToken = $submittedData['csrf'];
$sessionToken = $this->session->get("score_csrf_token");
if (empty($sessionToken)) {
return false;
}
if ($sessionToken != $submittedToken) {
return false;
}
return true;
}
public function getScoreSubmittedData(Request $request) {
$data = array();
$strDateTime = $request->get('datetime');
if (empty($strDateTime)) {
$this->logger->error("Empty date time string");
return null;
}
$dt = DateTime::createFromFormat("d.m.Y H:i:s", $strDateTime);
if ($dt == false) {
$this->logger->error("Wrong date time format");
return null;
}
$now = new DateTime();
/* @var $interval DateInterval */
$interval = $dt->diff($now);
$minutes = $interval->i;
/* if submitted data is older as 'minutesValid' return null -> error */
if ($minutes > $this->minutesValid) {
$this->logger->error("Submitted data is to old");
return null;
}
$data['dt'] = $dt->format('d.m.Y H:i');
$round = $request->get('round');
if (empty($round)) {
$this->logger->error("Empty round value");
return null;
}
$data['round'] = intval($round);
$schoolClassID = $request->get('schoolclassid');
if (empty($schoolClassID)) {
$this->logger->error("Empty school class id");
return null;
}
$data['schoolclassid'] = intval($schoolClassID);
$strCsrf = $request->get('csrf');
if (empty($strCsrf)) {
$this->logger->error("Empty csrf token");
return null;
}
$data['csrf'] = $strCsrf;
return $data;
}
public function getFormValidationData($formName) {
$dt = new DateTime();
$strDateTime = $dt->format("d.m.Y H:i:s");
$formSalt = $this->formSaltText;
$csrftoken = hash('sha256', "$strDateTime-$formName-$formSalt");
$data = array();
$data['datetime'] = $strDateTime;
$data['formname'] = $formName;
$data["csrf"] = $csrftoken;
$this->session->set("csrf_token_$formName", $csrftoken);
return $data;
}
public function isFormValid($formName, Request $request) {
$submittedData = $this->getSubmittedData($formName, $request);
if (empty($submittedData)) {
return false;
}
$submittedToken = $submittedData['csrf'];
$sessionToken = $this->session->get("csrf_token_$formName");
if (empty($sessionToken)) {
return false;
}
if ($sessionToken != $submittedToken) {
return false;
}
return true;
}
public function getSubmittedData($formName, Request $request) {
$data = array();
$strDateTime = $request->get('valid_dt');
if (empty($strDateTime)) {
$this->logger->error("Empty date time string");
return null;
}
$dt = DateTime::createFromFormat("d.m.Y H:i:s", $strDateTime);
if ($dt == false) {
$this->logger->error("Wrong date time format");
return null;
}
$now = new DateTime();
/* @var $interval DateInterval */
$interval = $dt->diff($now);
$minutes = $interval->i;
/* if submitted data is older as 'minutesValid' return null -> error */
if ($minutes > $this->minutesValid) {
$this->logger->error("Submitted data too old");
return null;
}
$data['dt'] = $dt->format('d.m.Y H:i');
$submittedFormName = $request->get('valid_formname');
if (empty($submittedFormName)) {
$this->logger->error("Empty form name");
return null;
}
if ($submittedFormName != $formName) {
$this->logger->error("Form name doesnt match");
return null;
}
$data['formName'] = $formName;
$strCsrf = $request->get('valid_csrf');
if (empty($strCsrf)) {
$this->logger->error("empty csrf token");
return null;
}
$data['csrf'] = $strCsrf;
return $data;
}
}