diff --git a/action/add_comment.php b/action/add_comment.php index 11c9152..7dfa47e 100644 --- a/action/add_comment.php +++ b/action/add_comment.php @@ -40,10 +40,10 @@ $is_admin = false; /*----------*/ $logService = new LogService(); -$pollService = new PollService($connect, $logService); $inputService = new InputService(); $mailService = new MailService($config['use_smtp'], $config['smtp_options'], $config['use_sendmail']); $notificationService = new NotificationService($mailService, $smarty); +$pollService = new PollService($connect, $logService, $notificationService); $securityService = new SecurityService(); /* PAGE */ diff --git a/action/send_edit_link_by_email_action.php b/action/send_edit_link_by_email_action.php index 8bf4232..9ec80e5 100644 --- a/action/send_edit_link_by_email_action.php +++ b/action/send_edit_link_by_email_action.php @@ -31,7 +31,7 @@ $logService = new LogService(); $sessionService = new SessionService(); $mailService = new MailService($config['use_smtp'], $config['smtp_options'], $config['use_sendmail']); $notificationService = new NotificationService($mailService, $smarty); -$pollService = new PollService($connect, $logService); +$pollService = new PollService($connect, $logService, $notificationService); $result = false; $message = null; diff --git a/admin/polls.php b/admin/polls.php index 06d3b45..42ab48b 100644 --- a/admin/polls.php +++ b/admin/polls.php @@ -19,6 +19,7 @@ use Framadate\Services\AdminPollService; use Framadate\Services\LogService; +use Framadate\Services\MailService; use Framadate\Services\PollService; use Framadate\Services\SecurityService; use Framadate\Services\SuperAdminService; @@ -50,7 +51,9 @@ $poll_to_delete = null; /*----------*/ $logService = new LogService(); -$pollService = new PollService($connect, $logService); +$mailService = new MailService($config['use_smtp'], $config['smtp_options'], $config['use_sendmail']); +$notificationService = new NotificationService($mailService, $smarty); +$pollService = new PollService($connect, $logService, $notificationService); $adminPollService = new AdminPollService($connect, $pollService, $logService); $superAdminService = new SuperAdminService(); $securityService = new SecurityService(); diff --git a/adminstuds.php b/adminstuds.php index 93e7b41..02bd208 100644 --- a/adminstuds.php +++ b/adminstuds.php @@ -47,11 +47,11 @@ $editingVoteId = 0; /*----------*/ $logService = new LogService(); -$pollService = new PollService($connect, $logService); -$adminPollService = new AdminPollService($connect, $pollService, $logService); -$inputService = new InputService(); $mailService = new MailService($config['use_smtp'], $config['smtp_options'], $config['use_sendmail']); $notificationService = new NotificationService($mailService, $smarty); +$pollService = new PollService($connect, $logService, $notificationService); +$adminPollService = new AdminPollService($connect, $pollService, $logService); +$inputService = new InputService(); $sessionService = new SessionService(); /* PAGE */ diff --git a/app/classes/Framadate/Services/MailService.php b/app/classes/Framadate/Services/MailService.php index cdf8e5a..29e945c 100644 --- a/app/classes/Framadate/Services/MailService.php +++ b/app/classes/Framadate/Services/MailService.php @@ -47,8 +47,12 @@ class MailService { return filter_var($email, FILTER_VALIDATE_EMAIL); } + public function isEnabled() { + return $this->smtp_allowed === true; + } + public function send($to, $subject, $body, $msgKey = null) { - if ($this->smtp_allowed === true && $this->canSendMsg($msgKey)) { + if ($this->isEnabled() && $this->canSendMsg($msgKey)) { $mail = new PHPMailer(true); $this->configureMailer($mail); diff --git a/app/classes/Framadate/Services/NotificationService.php b/app/classes/Framadate/Services/NotificationService.php index e0ae590..d0941a3 100644 --- a/app/classes/Framadate/Services/NotificationService.php +++ b/app/classes/Framadate/Services/NotificationService.php @@ -87,6 +87,10 @@ class NotificationService { } function sendPollCreationMails($creator_mail, $creator_name, $poll_name, $poll_id, $admin_poll_id) { + if (!$this->mailService->isEnabled() || !$this->mailService->isValidEmail($creator_mail)) { + return null; + } + $this->smarty->assign('poll_creator_name', Utils::htmlMailEscape($creator_name)); $this->smarty->assign('poll_name', Utils::htmlMailEscape($poll_name)); $this->smarty->assign('poll_url', Utils::getUrlSondage($poll_id)); diff --git a/app/classes/Framadate/Services/PollService.php b/app/classes/Framadate/Services/PollService.php index cba7f1c..eb41f5c 100644 --- a/app/classes/Framadate/Services/PollService.php +++ b/app/classes/Framadate/Services/PollService.php @@ -17,7 +17,6 @@ * Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft) */ namespace Framadate\Services; - use Doctrine\DBAL\Connection; use Doctrine\DBAL\ConnectionException; use Doctrine\DBAL\DBALException; @@ -27,6 +26,11 @@ use Framadate\Exception\ConcurrentVoteException; use Framadate\Form; use Framadate\Repositories\RepositoryFactory; use Framadate\Security\Token; +use Framadate\Services\LogService; +use Framadate\Services\NotificationService; +use Framadate\Services\PurgeService; +use Framadate\Services\SessionService; +use Framadate\Utils; class PollService { private $connect; @@ -37,9 +41,12 @@ class PollService { private $voteRepository; private $commentRepository; - function __construct(Connection $connect, LogService $logService) { + function __construct(Connection $connect, LogService $logService, NotificationService $notificationService) { $this->connect = $connect; $this->logService = $logService; + $this->notificationService = $notificationService; + $this->sessionService = new SessionService(); + $this->purgeService = new PurgeService($connect, $logService); $this->pollRepository = RepositoryFactory::pollRepository(); $this->slotRepository = RepositoryFactory::slotRepository(); $this->voteRepository = RepositoryFactory::voteRepository(); @@ -154,6 +161,61 @@ class PollService { return $this->commentRepository->insert($poll_id, $name, $comment); } + /** + * Main entry point for poll creation. This action does all the following: + * - Validate input data + * - Create the poll in database + * - Send emails + * - Cleanups post creation (session and purges) + * + * @param Form $form + * @param string $end_date + * @return string|null returns the admin_id if the poll was created + */ + function doPollCreation(Form $form, $end_date) { + // Min/Max archive date + $min_expiry_time = $this->minExpiryDate(); + $max_expiry_time = $this->maxExpiryDate(); + + if (!empty($end_date)) { + $registredate = explode('/', $end_date); + + 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 + list($poll_id, $admin_poll_id) = $this->createPoll($form); + + // Send confirmation by mail if enabled + // Everything went well + $this->notificationService->sendPollCreationMails($form->admin_mail, $form->admin_name, $form->title, $poll_id, $admin_poll_id); + + // Clean Form data in session + $this->sessionService->removeAll('form'); + // Creation message + $this->sessionService->set("Framadate", "messagePollCreated", TRUE); + + // Delete old polls + $this->purgeService->repeatedCleanings(); + + return $admin_poll_id; + } + /** * @param Form $form * @return array diff --git a/app/classes/Framadate/Services/SessionService.php b/app/classes/Framadate/Services/SessionService.php index 8620951..ca045e1 100644 --- a/app/classes/Framadate/Services/SessionService.php +++ b/app/classes/Framadate/Services/SessionService.php @@ -55,9 +55,20 @@ class SessionService { unset($_SESSION[$section][$key]); } + /** + * Remove a session section + * + * @param $section + */ + public function removeAll($section) { + assert(!empty($section)); + + unset($_SESSION[$section]); + } + private function initSectionIfNeeded($section) { if (!isset($_SESSION[$section])) { $_SESSION[$section] = []; } } -} \ No newline at end of file +} diff --git a/create_classic_poll.php b/create_classic_poll.php index 15966ad..00c8646 100644 --- a/create_classic_poll.php +++ b/create_classic_poll.php @@ -32,9 +32,9 @@ include_once __DIR__ . '/app/inc/init.php'; /* Service */ /*---------*/ $logService = new LogService(); -$pollService = new PollService($connect, $logService); $mailService = new MailService($config['use_smtp'], $config['smtp_options'], $config['use_sendmail']); $notificationService = new NotificationService($mailService, $smarty); +$pollService = new PollService($connect, $logService, $notificationService); $purgeService = new PurgeService($connect, $logService); $sessionService = new SessionService(); @@ -44,8 +44,6 @@ if (is_file('bandeaux_local.php')) { include_once('bandeaux.php'); } -// Min/Max archive date -$min_expiry_time = $pollService->minExpiryDate(); $max_expiry_time = $pollService->maxExpiryDate(); $form = isset($_SESSION['form']) ? unserialize($_SESSION['form']) : null; @@ -156,54 +154,24 @@ switch ($step) { $smarty->display('create_poll_step_3.tpl'); exit; - case 4: // Step 4 : Data prepare before insert in DB - $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); + case 4: + // Step 4 : Do the poll creation - if (is_array($registredate) && count($registredate) === 3) { - $time = mktime(0, 0, 0, $registredate[1], $registredate[0], $registredate[2]); + // Read expiration date passed in POST parameters + $end_date = filter_input(INPUT_POST, 'enddate', FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => '#^[0-9]{2}/[0-9]{2}/[0-9]{4}$#']]); - 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; - } - } + $admin_poll_id = $pollService->doPollCreation($form, $end_date); + + if (!is_null($admin_poll_id)) { + // Redirect to poll administration + header('Location:' . Utils::getUrlSondage($admin_poll_id, true)); } - - 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 - $ids = $pollService->createPoll($form); - $poll_id = $ids[0]; - $admin_poll_id = $ids[1]; - - // Send confirmation by mail if enabled - if ($config['use_smtp'] === true && $mailService->isValidEmail($form->admin_mail)) { - $notificationService->sendPollCreationMails($form->admin_mail, $form->admin_name, $form->title, $poll_id, $admin_poll_id); - } - - // Clean Form data in $_SESSION - unset($_SESSION['form']); - - // Delete old polls - $purgeService->purgeOldPolls(); - - // creation message - $sessionService->set("Framadate", "messagePollCreated", TRUE); - // Redirect to poll administration - header('Location:' . Utils::getUrlSondage($admin_poll_id, true)); exit; - case 1: // Step 1/4 : error if $_SESSION from info_sondage are not valid + case 1: default: + // Step 1/4 : error if $_SESSION from info_sondage are not valid $smarty->assign('title', __('Error', 'Error!')); $smarty->assign('error', __('Error', 'You haven\'t filled the first section of the poll creation, or your session has expired.')); $smarty->display('error.tpl'); diff --git a/create_date_poll.php b/create_date_poll.php index 998653f..8ec2731 100644 --- a/create_date_poll.php +++ b/create_date_poll.php @@ -32,19 +32,17 @@ include_once __DIR__ . '/app/inc/init.php'; /* Service */ /*---------*/ $logService = new LogService(); -$pollService = new PollService($connect, $logService); $mailService = new MailService($config['use_smtp'], $config['smtp_options'], $config['use_sendmail']); $notificationService = new NotificationService($mailService, $smarty); $purgeService = new PurgeService($connect, $logService); $inputService = new InputService(); $sessionService = new SessionService(); +$pollService = new PollService($connect, $logService, $notificationService); 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 = isset($_SESSION['form']) ? unserialize($_SESSION['form']) : null; @@ -193,52 +191,17 @@ switch ($step) { exit; case 4: - // Step 4 : Data prepare before insert in DB + // Step 4 : Do the poll creation - // Define expiration date - $enddate = filter_input(INPUT_POST, 'enddate', FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => '#^[0-9]{2}/[0-9]{2}/[0-9]{4}$#']]); + // Read expiration date passed in POST parameters + $end_date = 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); + $admin_poll_id = $pollService->doPollCreation($form, $end_date); - 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 (!is_null($admin_poll_id)) { + // Redirect to poll administration + header('Location:' . Utils::getUrlSondage($admin_poll_id, true)); } - - 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 - $ids = $pollService->createPoll($form); - $poll_id = $ids[0]; - $admin_poll_id = $ids[1]; - - // Send confirmation by mail if enabled - if ($config['use_smtp'] === true && $mailService->isValidEmail($form->admin_mail)) { - $notificationService->sendPollCreationMails($form->admin_mail, $form->admin_name, $form->title, $poll_id, $admin_poll_id); - } - - // Clean Form data in $_SESSION - unset($_SESSION['form']); - - $purgeService->repeatedCleanings(); - - // creation message - $sessionService->set("Framadate", "messagePollCreated", TRUE); - - // Redirect to poll administration - header('Location:' . Utils::getUrlSondage($admin_poll_id, true)); exit; case 1: diff --git a/exportcsv.php b/exportcsv.php index c60b7ff..f2d08e5 100644 --- a/exportcsv.php +++ b/exportcsv.php @@ -35,7 +35,9 @@ $poll = null; /*----------*/ $logService = new LogService(); -$pollService = new PollService($connect, $logService); +$mailService = new MailService($config['use_smtp'], $config['smtp_options'], $config['use_sendmail']); +$notificationService = new NotificationService($mailService, $smarty); +$pollService = new PollService($connect, $logService, $notificationService); $securityService = new SecurityService(); /* PAGE */ diff --git a/find_polls.php b/find_polls.php index 0a1b3f9..b95b2ce 100644 --- a/find_polls.php +++ b/find_polls.php @@ -28,9 +28,9 @@ include_once __DIR__ . '/app/inc/init.php'; /* SERVICES */ /* -------- */ $logService = new LogService(); -$pollService = new PollService($connect, $logService); $mailService = new MailService($config['use_smtp'], $config['smtp_options'], $config['use_sendmail']); $notificationService = new NotificationService($mailService, $smarty); +$pollService = new PollService($connect, $logService, $notificationService); /* PAGE */ /* ---- */ diff --git a/index.php b/index.php index 1ac2354..d8b557f 100644 --- a/index.php +++ b/index.php @@ -17,6 +17,9 @@ * Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft) */ +use Framadate\Services\LogService; +use Framadate\Services\MailService; +use Framadate\Services\NotificationService; use Framadate\Services\PollService; use Framadate\Utils; @@ -29,8 +32,10 @@ if (!is_file(CONF_FILENAME)) { /* SERVICES */ /* -------- */ -$logService = '\Framadate\Services\LogService'; -$pollService = new PollService($connect, new $logService()); +$logService = new LogService(); +$mailService = new MailService($config['use_smtp'], $config['smtp_options'], $config['use_sendmail']); +$notificationService = new NotificationService($mailService, $smarty); +$pollService = new PollService($connect, $logService, $notificationService); /* PAGE */ /* ---- */ diff --git a/studs.php b/studs.php index 2d30882..fb9f970 100644 --- a/studs.php +++ b/studs.php @@ -56,10 +56,10 @@ $selectedNewVotes = []; /*----------*/ $logService = new LogService(); -$pollService = new PollService($connect, $logService); $inputService = new InputService(); $mailService = new MailService($config['use_smtp'], $config['smtp_options'], $config['use_sendmail']); $notificationService = new NotificationService($mailService, $smarty); +$pollService = new PollService($connect, $logService, $notifierService); $securityService = new SecurityService(); $sessionService = new SessionService();