Refactor calculating max/min expiration date and enforce on poll edition

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2021-12-17 14:08:09 +01:00
parent 9c969f8896
commit 5a5c233a5e
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773
6 changed files with 46 additions and 77 deletions

View File

@ -80,7 +80,7 @@ $messagePollCreated = $sessionService->get("Framadate", "messagePollCreated", FA
if ($messagePollCreated) {
$sessionService->remove("Framadate", "messagePollCreated");
$message = new Message('success', __('adminstuds', 'The poll is created.'));
}
@ -137,9 +137,9 @@ if (isset($_POST['update_poll_info'])) {
break;
}
} elseif ($field === 'expiration_date') {
$expiration_date = $inputService->filterDate($_POST['expiration_date']);
$expiration_date = $inputService->validateDate($_POST['expiration_date'], $pollService->minExpiryDate(), $pollService->maxExpiryDate());
if ($expiration_date) {
$poll->end_date = $expiration_date;
$poll->end_date = $expiration_date->getTimestamp();
$updated = true;
}
} elseif ($field === 'name') {

View File

@ -120,9 +120,22 @@ class InputService {
return $this->returnIfNotBlank($comment);
}
public function filterDate($date) {
$dDate = DateTime::createFromFormat(__('Date', 'datetime_parseformat'), $date)->setTime(0, 0, 0);
return $dDate->format('Y-m-d H:i:s');
public function validateDate(string $date, DateTime $maxDate, DateTime $minDate): DateTime {
$dDate = $this->parseDate($date);
if (!$dDate) return $maxDate;
if ($dDate < $minDate) {
return $minDate;
} elseif ($maxDate < $dDate) {
return $maxDate;
}
return $dDate;
}
/**
* @return DateTime|false
*/
private function parseDate(string $date) {
return DateTime::createFromFormat(__('Date', 'datetime_parseformat'), $date)->setTime(0, 0, 0);
}
/**

View File

@ -18,6 +18,8 @@
*/
namespace Framadate\Services;
use DateInterval;
use DateTime;
use Framadate\Exception\AlreadyExistsException;
use Framadate\Exception\ConcurrentEditionException;
use Framadate\Exception\ConcurrentVoteException;
@ -95,12 +97,12 @@ class PollService {
*/
public function updateVote($poll_id, $vote_id, $name, $choices, $slots_hash) {
$this->checkVoteConstraints($choices, $poll_id, $slots_hash, $name, $vote_id);
// Update vote
$choices = implode($choices);
return $this->voteRepository->update($poll_id, $vote_id, $name, $choices);
}
/**
* @param $poll_id
* @param $name
@ -113,7 +115,7 @@ class PollService {
*/
function addVote($poll_id, $name, $choices, $slots_hash) {
$this->checkVoteConstraints($choices, $poll_id, $slots_hash, $name);
// Insert new vote
$choices = implode($choices);
$token = $this->random(16);
@ -124,7 +126,7 @@ class PollService {
if ($this->commentRepository->exists($poll_id, $name, $comment)) {
return true;
}
return $this->commentRepository->insert($poll_id, $name, $comment);
}
@ -232,18 +234,18 @@ class PollService {
}
/**
* @return int The max timestamp allowed for expiry date
* @return DateTime The max date allowed for expiry date
*/
public function maxExpiryDate() {
public function maxExpiryDate(): DateTime {
global $config;
return time() + (86400 * $config['default_poll_duration']);
return (new DateTime())->add(new DateInterval('P' . $config['default_poll_duration'] . 'D'));
}
/**
* @return int The min timestamp allowed for expiry date
* @return DateTime The min date allowed for expiry date
*/
public function minExpiryDate() {
return time() + 86400;
return (new DateTime())->add(new DateInterval('P1D'));
}
/**
@ -292,7 +294,7 @@ class PollService {
private function random($length) {
return Token::getToken($length);
}
/**
* @param $choices
* @param $poll_id
@ -310,20 +312,20 @@ class PollService {
} else {
$exists = $this->voteRepository->existsByPollIdAndNameAndVoteId($poll_id, $name, $vote_id);
}
if ($exists) {
throw new AlreadyExistsException();
}
$poll = $this->findById($poll_id);
// Check that no-one voted in the meantime and it conflicts the maximum votes constraint
$this->checkMaxVotes($choices, $poll, $poll_id);
// Check if slots are still the same
$this->checkThatSlotsDidntChanged($poll, $slots_hash);
}
/**
* This method checks if the hash send by the user is the same as the computed hash.
*

View File

@ -43,7 +43,7 @@ function bandeau_titre($titre)
<hr class="trait" role="presentation" />
</header>
<main role="main">';
global $connect;
$tables = $connect->allTables();
$diff = array_diff([Utils::table('comment'), Utils::table('poll'), Utils::table('slot'), Utils::table('vote')], $tables);

View File

@ -17,6 +17,7 @@
* Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
*/
use Framadate\Choice;
use Framadate\Services\InputService;
use Framadate\Services\LogService;
use Framadate\Services\MailService;
use Framadate\Services\PollService;
@ -33,6 +34,7 @@ $pollService = new PollService($connect, $logService);
$mailService = new MailService($config['use_smtp'], $config['smtp_options']);
$purgeService = new PurgeService($connect, $logService);
$sessionService = new SessionService();
$inputService = new InputService();
if (is_file('bandeaux_local.php')) {
include_once('bandeaux_local.php');
@ -49,10 +51,6 @@ if (empty($form->title) || empty($form->admin_name) || (($config['use_smtp']) ?
$smarty->display('error.tpl');
exit;
}
// Min/Max archive date
$min_expiry_time = $pollService->minExpiryDate();
$max_expiry_time = $pollService->maxExpiryDate();
// The poll format is other (A) if we are in this file
if (!isset($form->format)) {
$form->format = 'A';
@ -66,28 +64,8 @@ if (empty($form->title) || empty($form->admin_name) || (($config['use_smtp']) ?
// Step 4 : Data prepare before insert in DB
if (isset($_POST['confirmation'])) {
// Define expiration date
$enddate = filter_input(INPUT_POST, 'enddate', FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => '#^[0-9]{2}/[0-9]{2}/[0-9]{4}$#']]);
if (!empty($enddate)) {
$registredate = explode('/', $enddate);
if (is_array($registredate) && count($registredate) === 3) {
$time = mktime(0, 0, 0, $registredate[1], $registredate[0], $registredate[2]);
if ($time < $min_expiry_time) {
$form->end_date = $min_expiry_time;
} elseif ($max_expiry_time < $time) {
$form->end_date = $max_expiry_time;
} else {
$form->end_date = $time;
}
}
}
if (empty($form->end_date)) {
// By default, expiration date is 6 months after last day
$form->end_date = $max_expiry_time;
}
$expiration_date = $inputService->validateDate($_POST['expiration_date'], $pollService->minExpiryDate(), $pollService->maxExpiryDate());
$form->end_date = $expiration_date->getTimestamp();
// Insert poll in database
$ids = $pollService->createPoll($form);
@ -137,7 +115,7 @@ if (empty($form->title) || empty($form->admin_name) || (($config['use_smtp']) ?
}
// Expiration date is initialised with config parameter. Value will be modified in step 4 if user has defined an other date
$form->end_date = $max_expiry_time;
$form->end_date = $pollService->maxExpiryDate()->format('Y-m-d H:i:s');
// Summary
$summary = '<ol>';
@ -163,7 +141,7 @@ if (empty($form->title) || empty($form->admin_name) || (($config['use_smtp']) ?
}
$summary .= '</ol>';
$end_date_str = utf8_encode(strftime($date_format['txt_date'], $max_expiry_time)); //textual date
$end_date_str = utf8_encode(strftime($date_format['txt_date'], $pollService->maxExpiryDate()->getTimestamp())); //textual date
$_SESSION['form'] = serialize($form);

View File

@ -40,10 +40,6 @@ if (is_readable('bandeaux_local.php')) {
include_once('bandeaux_local.php');
}
// Min/Max archive date
$min_expiry_time = $pollService->minExpiryDate();
$max_expiry_time = $pollService->maxExpiryDate();
$form = unserialize($_SESSION['form']);
// The poll format is DATE if we are in this file
@ -175,7 +171,7 @@ switch ($step) {
}
$summary .= '</ul>';
$end_date_str = utf8_encode(strftime($date_format['txt_date'], $max_expiry_time)); // textual date
$end_date_str = utf8_encode(strftime($date_format['txt_date'], $pollService->maxExpiryDate()->getTimestamp())); // textual date
$_SESSION['form'] = serialize($form);
@ -192,28 +188,8 @@ switch ($step) {
// Step 4 : Data prepare before insert in DB
// Define expiration date
$enddate = filter_input(INPUT_POST, 'enddate', FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => '#^[0-9]{2}/[0-9]{2}/[0-9]{4}$#']]);
if (!empty($enddate)) {
$registredate = explode('/', $enddate);
if (is_array($registredate) && count($registredate) === 3) {
$time = mktime(0, 0, 0, $registredate[1], $registredate[0], $registredate[2]);
if ($time < $min_expiry_time) {
$form->end_date = $min_expiry_time;
} elseif ($max_expiry_time < $time) {
$form->end_date = $max_expiry_time;
} else {
$form->end_date = $time;
}
}
}
if (empty($form->end_date)) {
// By default, expiration date is 6 months after last day
$form->end_date = $max_expiry_time;
}
$expiration_date = $inputService->validateDate($_POST['enddate'], $pollService->minExpiryDate(), $pollService->maxExpiryDate());
$form->end_date = $expiration_date->getTimestamp();
// Insert poll in database
$ids = $pollService->createPoll($form);