From 9ed0043569dbad4aa280d75d21cf95d9dd8b10b3 Mon Sep 17 00:00:00 2001 From: Olivier PEREZ Date: Thu, 25 Dec 2014 00:55:52 +0100 Subject: [PATCH] Some cleaning in poll creation --- adminstuds.php | 2 +- app/classes/Framadate/Form.php | 12 +- app/classes/Framadate/Services/LogService.php | 2 +- .../Framadate/Services/PollService.php | 79 +++++++++++- bandeaux.php | 2 +- choix_autre.php | 2 - choix_date.php | 58 ++++++--- creation_sondage.php | 114 ------------------ infos_sondage.php | 26 ++-- studs.php | 5 +- 10 files changed, 141 insertions(+), 161 deletions(-) delete mode 100644 creation_sondage.php diff --git a/adminstuds.php b/adminstuds.php index 20dc5bb..05aed7e 100644 --- a/adminstuds.php +++ b/adminstuds.php @@ -37,7 +37,7 @@ $editingVoteId = 0; /*----------*/ $logService = new LogService(LOG_FILE); -$pollService = new PollService($connect); +$pollService = new PollService($connect, $logService); $adminPollService = new AdminPollService($connect, $pollService, $logService); $inputService = new InputService(); diff --git a/app/classes/Framadate/Form.php b/app/classes/Framadate/Form.php index 2be9e6c..c002997 100644 --- a/app/classes/Framadate/Form.php +++ b/app/classes/Framadate/Form.php @@ -21,12 +21,12 @@ namespace Framadate; class Form { - public $titre; - public $commentaires; - public $nom; - public $adresse; - public $formatsondage; - public $champdatefin; + public $title; + public $description; + public $admin_name; + public $admin_mail; + public $format; + public $end_date; public $choix_sondage; /** diff --git a/app/classes/Framadate/Services/LogService.php b/app/classes/Framadate/Services/LogService.php index a8f27aa..620ffa9 100644 --- a/app/classes/Framadate/Services/LogService.php +++ b/app/classes/Framadate/Services/LogService.php @@ -21,7 +21,7 @@ class LogService { * @param $message string some message */ function log($tag, $message) { - error_log('[' . $tag . '] ' . $message . "\n", 3, $this->output); + error_log(date('H:i:s d/m/Y:') . '[' . $tag . '] ' . $message . "\n", 3, $this->output); } } diff --git a/app/classes/Framadate/Services/PollService.php b/app/classes/Framadate/Services/PollService.php index 2e62b44..25a4b2e 100644 --- a/app/classes/Framadate/Services/PollService.php +++ b/app/classes/Framadate/Services/PollService.php @@ -18,12 +18,18 @@ */ namespace Framadate\Services; +use Framadate\Form; +use Framadate\FramaDB; +use Framadate\Utils; + class PollService { private $connect; + private $logService; - function __construct($connect) { + function __construct(FramaDB $connect, LogService $logService) { $this->connect = $connect; + $this->logService = $logService; } /** @@ -54,11 +60,13 @@ class PollService { public function updateVote($poll_id, $vote_id, $choices) { $choices = implode($choices); + return $this->connect->updateVote($poll_id, $vote_id, $choices); } function addVote($poll_id, $name, $choices) { $choices = implode($choices); + return $this->connect->insertVote($poll_id, $name, $choices); } @@ -71,7 +79,7 @@ class PollService { $result = []; foreach ($votes as $vote) { $choices = str_split($vote->reponses); - foreach ($choices as $i=>$choice) { + foreach ($choices as $i => $choice) { if (empty($result[$i])) { $result[$i] = 0; } @@ -80,6 +88,7 @@ class PollService { } } } + return $result; } @@ -93,6 +102,7 @@ class PollService { $splitted[] = $obj; } + return $splitted; } @@ -106,6 +116,71 @@ class PollService { $splitted[] = $obj; } + return $splitted; } + + /** + * @param Form $form + * @return string + */ + function createPoll(Form $form) { + + // Generate poll IDs + $poll_id = $this->random(16); + $admin_poll_id = $poll_id . $this->random(8); + + // Insert poll + slots + $this->connect->beginTransaction(); + + $sql = 'INSERT INTO sondage + (poll_id, admin_poll_id, title, comment, admin_name, admin_mail, end_date, format, editable, receiveNewVotes) + VALUES (?,?,?,?,?,?,FROM_UNIXTIME(?),?,?,?)'; + $prepared = $this->connect->prepare($sql); + $prepared->execute(array($poll_id, $admin_poll_id, $form->title, $form->description, $form->admin_name, $form->admin_mail, $form->end_date, $form->format, $form->editable, $form->receiveNewVotes)); + + $prepared = $this->connect->prepare('INSERT INTO sujet_studs (id_sondage, sujet) VALUES (?, ?)'); + + foreach ($form->getChoices() as $choice) { + + // We prepared the slots (joined by comas) + $joinedSlots = ''; + $first = true; + foreach ($choice->getSlots() as $slot) { + if ($first) { + $joinedSlots = $slot; + $first = false; + } else { + $joinedSlots .= ',' . $slot; + } + } + + // We execute the insertion + if (empty($joinedSlots)) { + $prepared->execute(array($poll_id, $choice->getName())); + } else { + $prepared->execute(array($poll_id, $choice->getName() . '@' . $joinedSlots)); + } + + } + + $this->connect->commit(); + + $this->logService->log('CREATE_POLL', ' id:' . $poll_id . ', format:' . $form->format . ', admin:' . $form->admin_name . ', mail:' . $form->admin_mail); + + + return [$poll_id, $admin_poll_id]; + } + + private function random($car) { + // TODO Better random ? + $string = ''; + $chaine = 'abcdefghijklmnopqrstuvwxyz123456789'; + srand((double)microtime() * 1000000); + for ($i = 0; $i < $car; $i++) { + $string .= $chaine[rand() % strlen($chaine)]; + } + + return $string; + } } diff --git a/bandeaux.php b/bandeaux.php index 7f86cde..f931177 100644 --- a/bandeaux.php +++ b/bandeaux.php @@ -16,7 +16,7 @@ * Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ * Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft) */ -namespace Framadate; +use Framadate\Utils; include_once __DIR__ . '/app/inc/init.php'; diff --git a/choix_autre.php b/choix_autre.php index 7ebca4d..368e6b5 100644 --- a/choix_autre.php +++ b/choix_autre.php @@ -20,8 +20,6 @@ namespace Framadate; include_once __DIR__ . '/app/inc/init.php'; -include_once('creation_sondage.php'); - if (file_exists('bandeaux_local.php')) { include_once('bandeaux_local.php'); } else { diff --git a/choix_date.php b/choix_date.php index 5a4e06c..6bf17e6 100644 --- a/choix_date.php +++ b/choix_date.php @@ -16,11 +16,19 @@ * Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ * Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft) */ -namespace Framadate; +use Framadate\Services\LogService; +use Framadate\Services\PollService; +use Framadate\Services\MailService; +use Framadate\Utils; +use Framadate\Choice; include_once __DIR__ . '/app/inc/init.php'; -include_once('creation_sondage.php'); +/* Service */ +/*---------*/ +$logService = new LogService(LOG_FILE); +$pollService = new PollService($connect, $logService); +$mailService = new MailService($config['use_smtp']); if (is_readable('bandeaux_local.php')) { include_once('bandeaux_local.php'); @@ -29,7 +37,7 @@ if (is_readable('bandeaux_local.php')) { } // Step 1/4 : error if $_SESSION from info_sondage are not valid -if (!isset($_SESSION['form']->titre) || !isset($_SESSION['form']->nom) || (($config['use_smtp']) ? !isset($_SESSION['form']->adresse) : false)) { +if (!isset($_SESSION['form']->title) || !isset($_SESSION['form']->admin_name) || ($config['use_smtp'] && !isset($_SESSION['form']->admin_mail))) { Utils::print_header ( _("Error!") ); bandeau_titre(_("Error!")); @@ -56,34 +64,46 @@ if (!isset($_SESSION['form']->titre) || !isset($_SESSION['form']->nom) || (($con $time = mktime(0,0,0, $registredate[1], $registredate[0], $registredate[2]); if ($time > time() + (24*60*60)) { - $_SESSION['form']->champdatefin=$time; + $_SESSION['form']->end_date=$time; } } } - if(empty($_SESSION['form']->champdatefin)) - { + if(empty($_SESSION['form']->end_date)) { // By default, expiration date is 6 months after last day - $_SESSION['form']->champdatefin=end($temp_results)+(86400 * $config['default_poll_duration']); + $_SESSION['form']->end_date=end($temp_results)+(86400 * $config['default_poll_duration']); } // Insert poll in database - $admin_poll_id = ajouter_sondage( - $_SESSION['form']->titre, - $_SESSION['form']->commentaires, - $_SESSION['form']->nom, - $_SESSION['form']->adresse, - $_SESSION['form']->formatsondage, - $_SESSION['form']->editable, - $_SESSION['form']->champdatefin, - $_SESSION['form']->receiveNewVotes, - $_SESSION['form']->getChoices() - ); + $ids = $pollService->createPoll($_SESSION['form']); + $poll_id = $ids[0]; + $admin_poll_id = $ids[1]; + + + // Send confirmation by mail if enabled + if ($config['use_smtp'] === true) { + $message = _("This is the message you have to send to the people you want to poll. \nNow, you have to send this message to everyone you want to poll."); + $message .= "\n\n"; + $message .= stripslashes(html_entity_decode($_SESSION['form']->admin_name, ENT_QUOTES, "UTF-8")) . ' ' . _("hast just created a poll called") . ' : "' . stripslashes(htmlspecialchars_decode($_SESSION['form']->title, ENT_QUOTES)) . "\".\n"; + $message .= _('Thanks for filling the poll at the link above') . " :\n\n%s\n\n" . _('Thanks for your confidence.') . "\n" . NOMAPPLICATION; + + $message_admin = _("This message should NOT be sent to the polled people. It is private for the poll's creator.\n\nYou can now modify it at the link above"); + $message_admin .= " :\n\n" . "%s \n\n" . _('Thanks for your confidence.') . "\n" . NOMAPPLICATION; + + $message = sprintf($message, Utils::getUrlSondage($poll_id)); + $message_admin = sprintf($message_admin, Utils::getUrlSondage($admin_poll_id, true)); + + if ($mailService->isValidEmail($_SESSION['form']->admin_mail)) { + $mailService->send($_SESSION['form']->admin_mail, '[' . NOMAPPLICATION . '][' . _('Author\'s message') . '] ' . _('Poll') . ' : ' . stripslashes(htmlspecialchars_decode($_SESSION['form']->title, ENT_QUOTES)), $message_admin); + $mailService->send($_SESSION['form']->admin_mail, '[' . NOMAPPLICATION . '][' . _('For sending to the polled users') . '] ' . _('Poll') . ' : ' . stripslashes(htmlspecialchars_decode($_SESSION['form']->title, ENT_QUOTES)), $message); + } + } // Clean Form data in $_SESSION unset($_SESSION['form']); // Delete old polls + // TODO Create a PurgeService Utils::cleaningOldPolls($connect, 'admin/logs_studs.txt'); // Redirect to poll administration @@ -118,7 +138,7 @@ if (!isset($_SESSION['form']->titre) || !isset($_SESSION['form']->nom) || (($con } //le format du sondage est DATE - $_SESSION['form']->formatsondage = 'D'; + $_SESSION['form']->format = 'D'; // Step 3/4 : Confirm poll creation if (!empty($_POST['choixheures']) && !isset($_SESSION['form']->totalchoixjour)) { diff --git a/creation_sondage.php b/creation_sondage.php deleted file mode 100644 index 6705af1..0000000 --- a/creation_sondage.php +++ /dev/null @@ -1,114 +0,0 @@ -beginTransaction(); - - $sql = 'INSERT INTO sondage - (poll_id, admin_poll_id, title, comment, admin_name, admin_mail, end_date, format, editable, receiveNewVotes) - VALUES (?,?,?,?,?,?,FROM_UNIXTIME(?),?,?,?)'; - $prepared = $connect->prepare($sql); - $prepared->execute(array($poll_id, $admin_poll_id, $title, $comment, $adminName, $adminMail, $endDate, $format, $editable, $receiveNewVotes)); - - $prepared = $connect->prepare('INSERT INTO sujet_studs (id_sondage, sujet) VALUES (?, ?)'); - foreach ($choices as $choice) { - - // We prepared the slots (joined by comas) - $joinedSlots = ''; - $first = true; - foreach ($choice->getSlots() as $slot) { - - // We prepared the slots (joined by comas) - $joinedSlots = ''; - $first = true; - foreach ($choice->getSlots() as $slot) { - if ($first) { - $joinedSlots = $slot; - $first = false; - } else { - $joinedSlots .= ',' . $slot; - } - } - - // We execute the insertion - if (empty($joinedSlots)) { - $prepared->execute(array($poll_id, $choice->getName())); - } else { - $prepared->execute(array($poll_id, $choice->getName().'@'.$joinedSlots)); - } - - } - - } - - $connect->commit(); - - // Send confirmation by mail if enabled - if($config['use_smtp'] === true){ - $message = _("This is the message you have to send to the people you want to poll. \nNow, you have to send this message to everyone you want to poll."); - $message .= "\n\n"; - $message .= stripslashes(html_entity_decode($adminName, ENT_QUOTES, "UTF-8"))." " . _("hast just created a poll called") . " : \"".stripslashes(htmlspecialchars_decode($title,ENT_QUOTES))."\".\n"; - $message .= _("Thanks for filling the poll at the link above") . " :\n\n%s\n\n" . _("Thanks for your confidence.") . "\n".NOMAPPLICATION; - - $message_admin = _("This message should NOT be sent to the polled people. It is private for the poll's creator.\n\nYou can now modify it at the link above"); - $message_admin .= " :\n\n"."%s \n\n" . _("Thanks for your confidence.") . "\n".NOMAPPLICATION; - - $message = sprintf($message, Utils::getUrlSondage($poll_id)); - $message_admin = sprintf($message_admin, Utils::getUrlSondage($admin_poll_id, true)); - - if (Utils::isValidEmail($_SESSION['adresse'])) { - Utils::sendEmail( $adminMail, "[".NOMAPPLICATION."][" . _("Author's message") . "] " . _("Poll") . " : ".stripslashes(htmlspecialchars_decode($title,ENT_QUOTES)), $message_admin, $_SESSION['adresse'] ); - Utils::sendEmail( $adminMail, "[".NOMAPPLICATION."][" . _("For sending to the polled users") . "] " . _("Poll") . " : ".stripslashes(htmlspecialchars_decode($title,ENT_QUOTES)), $message, $_SESSION['adresse'] ); - } - } - - error_log(date('H:i:s d/m/Y:') . ' CREATION: '.$poll_id."\t".$format."\t".$adminName."\t".$adminMail."\n", 3, 'admin/logs_studs.txt'); - - return $admin_poll_id; -} diff --git a/infos_sondage.php b/infos_sondage.php index 58e00d8..6d9d24f 100644 --- a/infos_sondage.php +++ b/infos_sondage.php @@ -58,10 +58,10 @@ $erreur_injection_commentaires = false; #tests if (!empty($_POST['poursuivre'])){ - $_SESSION['form']->titre = $titre; - $_SESSION['form']->nom = $nom; - $_SESSION['form']->adresse = $adresse; - $_SESSION['form']->commentaires = $commentaires; + $_SESSION['form']->title = $titre; + $_SESSION['form']->admin_name = $nom; + $_SESSION['form']->admin_mail = $adresse; + $_SESSION['form']->description = $commentaires; $_SESSION['form']->editable = ($editable !== null) ? true : false; $_SESSION['form']->receiveNewVotes = ($receiveNewVotes !== null) ? true : false; @@ -140,7 +140,7 @@ $errors = array( ) ); -if (!$_SESSION['form']->titre && !empty($_POST['poursuivre'])) { +if (!$_SESSION['form']->title && !empty($_POST['poursuivre'])) { $errors['title']['aria'] = 'aria-describeby="poll_title_error" '; $errors['title']['class'] = ' has-error'; $errors['title']['msg'] = '

' . _("Enter a title") . '

'; } elseif ($erreur_injection_titre) { @@ -153,7 +153,7 @@ if ($erreur_injection_commentaires) { $errors['description']['msg'] = '

' . _("Characters < > and \" are not permitted") . '

'; } -if (!$_SESSION['form']->nom && !empty($_POST['poursuivre'])) { +if (!$_SESSION['form']->admin_name && !empty($_POST['poursuivre'])) { $errors['name']['aria'] = 'aria-describeby="poll_name_error" '; $errors['name']['class'] = ' has-error'; $errors['name']['msg'] = '

' . _("Enter a name") . '

'; } elseif ($erreur_injection_nom) { @@ -161,7 +161,7 @@ if (!$_SESSION['form']->nom && !empty($_POST['poursuivre'])) { $errors['name']['msg'] = '

' . _("Characters < > and \" are not permitted") . '

'; } -if (!$_SESSION['form']->adresse && !empty($_POST['poursuivre'])) { +if (!$_SESSION['form']->admin_mail && !empty($_POST['poursuivre'])) { $errors['email']['aria'] = 'aria-describeby="poll_name_error" '; $errors['email']['class'] = ' has-error'; $errors['email']['msg'] = '

' . _("Enter an email address") . '

'; } elseif ($erreur_adresse && !empty($_POST['poursuivre'])) { @@ -175,15 +175,15 @@ if (!$_SESSION['form']->adresse && !empty($_POST['poursuivre'])) { // REMOTE_USER ? if (USE_REMOTE_USER && isset($_SERVER['REMOTE_USER'])) { - $input_name = ''.stripslashes($_SESSION['form']->nom); + $input_name = ''.stripslashes($_SESSION['form']->admin_name); } else { - $input_name = ''; + $input_name = ''; } if (USE_REMOTE_USER && isset($_SERVER['REMOTE_USER'])) { - $input_email = ''.$_SESSION['form']->adresse; + $input_email = ''.$_SESSION['form']->admin_mail; } else { - $input_email = ''; + $input_email = ''; } // Checkbox checked ? @@ -208,14 +208,14 @@ echo '
- +
'.$errors['title']['msg'].'
- +
'.$errors['description']['msg'].' diff --git a/studs.php b/studs.php index 437e462..a893a27 100644 --- a/studs.php +++ b/studs.php @@ -16,6 +16,7 @@ * Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ * Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft) */ +use Framadate\Services\LogService; use Framadate\Services\PollService; use Framadate\Services\InputService; use Framadate\Services\MailService; @@ -34,8 +35,8 @@ $editingVoteId = 0; /* Services */ /*----------*/ - -$pollService = new PollService($connect); +$logService = new LogService(LOG_FILE); +$pollService = new PollService($connect, $logService); $inputService = new InputService(); $mailService = new MailService($config['use_smtp']);