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

@ -137,9 +137,9 @@ if (isset($_POST['update_poll_info'])) {
break; break;
} }
} elseif ($field === 'expiration_date') { } 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) { if ($expiration_date) {
$poll->end_date = $expiration_date; $poll->end_date = $expiration_date->getTimestamp();
$updated = true; $updated = true;
} }
} elseif ($field === 'name') { } elseif ($field === 'name') {

View File

@ -120,9 +120,22 @@ class InputService {
return $this->returnIfNotBlank($comment); return $this->returnIfNotBlank($comment);
} }
public function filterDate($date) { public function validateDate(string $date, DateTime $maxDate, DateTime $minDate): DateTime {
$dDate = DateTime::createFromFormat(__('Date', 'datetime_parseformat'), $date)->setTime(0, 0, 0); $dDate = $this->parseDate($date);
return $dDate->format('Y-m-d H:i:s'); 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; namespace Framadate\Services;
use DateInterval;
use DateTime;
use Framadate\Exception\AlreadyExistsException; use Framadate\Exception\AlreadyExistsException;
use Framadate\Exception\ConcurrentEditionException; use Framadate\Exception\ConcurrentEditionException;
use Framadate\Exception\ConcurrentVoteException; use Framadate\Exception\ConcurrentVoteException;
@ -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; 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() { public function minExpiryDate() {
return time() + 86400; return (new DateTime())->add(new DateInterval('P1D'));
} }
/** /**

View File

@ -17,6 +17,7 @@
* Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft) * Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
*/ */
use Framadate\Choice; use Framadate\Choice;
use Framadate\Services\InputService;
use Framadate\Services\LogService; use Framadate\Services\LogService;
use Framadate\Services\MailService; use Framadate\Services\MailService;
use Framadate\Services\PollService; use Framadate\Services\PollService;
@ -33,6 +34,7 @@ $pollService = new PollService($connect, $logService);
$mailService = new MailService($config['use_smtp'], $config['smtp_options']); $mailService = new MailService($config['use_smtp'], $config['smtp_options']);
$purgeService = new PurgeService($connect, $logService); $purgeService = new PurgeService($connect, $logService);
$sessionService = new SessionService(); $sessionService = new SessionService();
$inputService = new InputService();
if (is_file('bandeaux_local.php')) { if (is_file('bandeaux_local.php')) {
include_once('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'); $smarty->display('error.tpl');
exit; 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 // The poll format is other (A) if we are in this file
if (!isset($form->format)) { if (!isset($form->format)) {
$form->format = 'A'; $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 // Step 4 : Data prepare before insert in DB
if (isset($_POST['confirmation'])) { if (isset($_POST['confirmation'])) {
// Define expiration date // Define expiration date
$enddate = filter_input(INPUT_POST, 'enddate', FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => '#^[0-9]{2}/[0-9]{2}/[0-9]{4}$#']]); $expiration_date = $inputService->validateDate($_POST['expiration_date'], $pollService->minExpiryDate(), $pollService->maxExpiryDate());
$form->end_date = $expiration_date->getTimestamp();
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;
}
// Insert poll in database // Insert poll in database
$ids = $pollService->createPoll($form); $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 // 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
$summary = '<ol>'; $summary = '<ol>';
@ -163,7 +141,7 @@ if (empty($form->title) || empty($form->admin_name) || (($config['use_smtp']) ?
} }
$summary .= '</ol>'; $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); $_SESSION['form'] = serialize($form);

View File

@ -40,10 +40,6 @@ if (is_readable('bandeaux_local.php')) {
include_once('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']); $form = unserialize($_SESSION['form']);
// The poll format is DATE if we are in this file // The poll format is DATE if we are in this file
@ -175,7 +171,7 @@ switch ($step) {
} }
$summary .= '</ul>'; $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); $_SESSION['form'] = serialize($form);
@ -192,28 +188,8 @@ switch ($step) {
// Step 4 : Data prepare before insert in DB // Step 4 : Data prepare before insert in DB
// Define expiration date // Define expiration date
$enddate = filter_input(INPUT_POST, 'enddate', FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => '#^[0-9]{2}/[0-9]{2}/[0-9]{4}$#']]); $expiration_date = $inputService->validateDate($_POST['enddate'], $pollService->minExpiryDate(), $pollService->maxExpiryDate());
$form->end_date = $expiration_date->getTimestamp();
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;
}
// Insert poll in database // Insert poll in database
$ids = $pollService->createPoll($form); $ids = $pollService->createPoll($form);