diff --git a/app/classes/Framadate/Choice.php b/app/classes/Framadate/Choice.php
new file mode 100644
index 0000000..d1eec51
--- /dev/null
+++ b/app/classes/Framadate/Choice.php
@@ -0,0 +1,42 @@
+name = $name;
+ $this->slots = array();
+ }
+
+ public function addSlot($slot)
+ {
+ $this->slots[] = $slot;
+ }
+
+ public function getName()
+ {
+ return $this->name;
+ }
+
+ public function getSlots()
+ {
+ return $this->slots;
+ }
+
+ static function compare(Choice $a, Choice $b)
+ {
+ return strcmp($a->name, $b->name);
+ }
+
+}
diff --git a/app/classes/Framadate/Form.php b/app/classes/Framadate/Form.php
new file mode 100644
index 0000000..641f0f1
--- /dev/null
+++ b/app/classes/Framadate/Form.php
@@ -0,0 +1,58 @@
+clearChoices();
+ }
+
+ public function clearChoices() {
+ $this->choices = array();
+ }
+
+ public function addChoice(Choice $choice)
+ {
+ $this->choices[] = $choice;
+ }
+
+ public function getChoices()
+ {
+ return $this->choices;
+ }
+
+ public function sortChoices()
+ {
+ usort($this->choices, array('Framadate\Choice', 'compare'));
+ }
+
+ public function lastChoice()
+ {
+ return end($this->choices);
+ }
+
+}
\ No newline at end of file
diff --git a/app/classes/Framadate/FramaDB.php b/app/classes/Framadate/FramaDB.php
new file mode 100644
index 0000000..a10884e
--- /dev/null
+++ b/app/classes/Framadate/FramaDB.php
@@ -0,0 +1,39 @@
+pdo = new \PDO($connection_string, $user, $password);
+ }
+
+ function areTablesCreated()
+ {
+ $result= $this->pdo->query('SHOW TABLES');
+ $schemas = $result->fetchAll(\PDO::FETCH_COLUMN);
+ return !empty(array_diff($schemas, ['comments', 'sondage', 'sujet_studs', 'user_studs']));
+ }
+
+ function prepare($sql) {
+ return $this->pdo->prepare($sql);
+ }
+
+ function beginTransaction() {
+ $this->pdo->beginTransaction();
+ }
+
+ function commit() {
+ $this->pdo->commit();
+ }
+
+ function query($sql) {
+ return $this->pdo->query($sql);
+ }
+
+}
diff --git a/app/classes/Framadate/Utils.php b/app/classes/Framadate/Utils.php
index 7dfd953..c48aff2 100644
--- a/app/classes/Framadate/Utils.php
+++ b/app/classes/Framadate/Utils.php
@@ -23,9 +23,10 @@ class Utils
public static function get_server_name()
{
$scheme = (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == 'on') ? 'https' : 'http';
- $port = in_array($_SERVER['SERVER_PORT'], [80, 443]) ? '/' : ':' . $_SERVER['SERVER_PORT'] . '/';
- $server_name = $_SERVER['SERVER_NAME'] . $port . dirname($_SERVER['SCRIPT_NAME']) . '/';
-
+ $port = in_array($_SERVER['SERVER_PORT'], [80, 443]) ? '' : ':' . $_SERVER['SERVER_PORT'];
+ $dirname = dirname($_SERVER['SCRIPT_NAME']);
+ $dirname = $dirname === '\\' ? '/' : $dirname . '/';
+ $server_name = $_SERVER['SERVER_NAME'] . $port . $dirname;
return $scheme . '://' . str_replace('/admin','',str_replace('//','/',str_replace('///','/',$server_name)));
}
@@ -222,43 +223,41 @@ class Utils
return $url;
}
- public static function remove_sondage($connect, $numsondage)
+ /**
+ * Completly delete data about the given poll
+ */
+ public static function removeSondage($poll_id)
{
- $connect->StartTrans();
+ global $connect;
- $req = 'DELETE FROM sondage WHERE id_sondage = ' . $connect->Param('numsondage') ;
- $sql = $connect->Prepare($req);
- $connect->Execute($sql, [$numsondage]);
+ $prepared = $connect->prepare('DELETE FROM sujet_studs WHERE id_sondage = ?');
+ $prepared->execute(array($poll_id));
- $req = 'DELETE FROM sujet_studs WHERE id_sondage = ' . $connect->Param('numsondage') ;
- $sql = $connect->Prepare($req);
- $connect->Execute($sql, [$numsondage]);
+ $prepared = $connect->prepare('DELETE FROM user_studs WHERE id_sondage = ?');
+ $prepared->execute(array($poll_id));
- $req = 'DELETE FROM user_studs WHERE id_sondage = ' . $connect->Param('numsondage') ;
- $sql = $connect->Prepare($req);
- $connect->Execute($sql, [$numsondage]);
+ $prepared = $connect->prepare('DELETE FROM comments WHERE id_sondage = ?');
+ $prepared->execute(array($poll_id));
- $req = 'DELETE FROM comments WHERE id_sondage = ' . $connect->Param('numsondage') ;
- $sql = $connect->Prepare($req);
- $connect->Execute($sql, [$numsondage]);
-
- $suppression_OK = ! $connect->HasFailedTrans();
- $connect->CompleteTrans();
-
- return $suppression_OK ;
+ $prepared = $connect->prepare('DELETE FROM sondage WHERE id_sondage = ?');
+ $prepared->execute(array($poll_id));
+
}
- public static function cleaning_polls($connect, $log_txt) {
- $connect->StartTrans();
- $req = 'SELECT * FROM sondage WHERE date_fin < NOW() LIMIT 20';
- $sql = $connect->Prepare($req);
- $cleaning = $connect->Execute($sql);
+ public static function cleaningOldPolls($log_txt) {
+ global $connect;
+
+ $resultSet = $connect->query('SELECT id_sondage, format, nom_admin, mail_admin FROM sondage WHERE date_fin < NOW() LIMIT 20');
+ $toClean = $resultSet->fetchAll(\PDO::FETCH_CLASS);
+
+ echo '
toClean:'.print_r($toClean, true).'
';
- while ($dcleaning = $cleaning->FetchNextObject(false)) {
- if (self::remove_sondage($connect, $dcleaning->id_sondage)) {
- error_log(date('H:i:s d/m/Y:') . ' EXPIRATION: '. $dcleaning->id_sondage."\t".$dcleaning->format."\t".$dcleaning->nom_admin."\t".$dcleaning->mail_admin."\n", 3, $log_txt);
+ $connect->beginTransaction();
+ foreach ($toClean as $row) {
+ if (self::removeSondage($row->id_sondage)) {
+ error_log(date('H:i:s d/m/Y:') . ' EXPIRATION: '. $row->id_sondage."\t".$row->format."\t".$row->nom_admin."\t".$row->mail_admin."\n", 3, $log_txt);
}
}
- $connect->CompleteTrans();
+ $connect->commit();
}
}
diff --git a/app/inc/init.php b/app/inc/init.php
index 2a13d2b..4068df0 100644
--- a/app/inc/init.php
+++ b/app/inc/init.php
@@ -26,6 +26,14 @@ require_once __DIR__ . '/../../vendor/autoload.php';
include_once __DIR__ . '/constants.php';
include_once __DIR__ . '/i18n.php';
-$connect = NewADOConnection(BASE_TYPE);
-$connect->Connect(SERVEURBASE, USERBASE, USERPASSWD, BASE);
+use Framadate\FramaDB;
+use Framadate\Form;
+use Framadate\Choice;
+use Framadate\Utils;
+
+if (session_id() == "") {
+ session_start();
+}
+
+$connect = new Framadate\FramaDB(DB_CONNECTION_STRING, DB_USER, DB_PASSWORD);
$err = 0;
diff --git a/bandeaux.php b/bandeaux.php
index efbe726..7f86cde 100644
--- a/bandeaux.php
+++ b/bandeaux.php
@@ -43,6 +43,14 @@ function bandeau_titre($titre)
';
+
+ global $connect;
+ if ($connect->areTablesCreated()) {
+ echo ''. _('Framadate is not properly installed, please check the "INSTALL" to setup the database before continuing.') .'
';
+ bandeau_pied();
+ die();
+ }
+
}
function liste_lang()
diff --git a/choix_date.php b/choix_date.php
index b9f6314..4d33ca3 100644
--- a/choix_date.php
+++ b/choix_date.php
@@ -18,7 +18,7 @@
*/
namespace Framadate;
-session_start();
+include_once __DIR__ . '/app/inc/init.php';
include_once('creation_sondage.php');
@@ -29,7 +29,7 @@ if (is_readable('bandeaux_local.php')) {
}
// Step 1/3 : error if $_SESSION from info_sondage are not valid
-if (Utils::issetAndNoEmpty('titre', $_SESSION) === false || Utils::issetAndNoEmpty('nom', $_SESSION) === false || (($config['use_smtp']) ? Utils::issetAndNoEmpty('adresse', $_SESSION) === false : false)) {
+if (!isset($_SESSION['form']->titre) || !isset($_SESSION['form']->nom) || (($config['use_smtp']) ? !isset($_SESSION['form']->adresse) : false)) {
Utils::print_header ( _("Error!") );
bandeau_titre(_("Error!"));
@@ -73,70 +73,103 @@ if (Utils::issetAndNoEmpty('titre', $_SESSION) === false || Utils::issetAndNoEmp
}
}
- $_SESSION["toutchoix"]=substr($choixdate,1);
+ $_SESSION['form']->toutchoix=substr($choixdate,1);
// Expiration date → 6 months after last day if not filled or in bad format
- $_SESSION["champdatefin"]=end($temp_results)+(86400 * $config['default_poll_duration']);
+ $_SESSION['form']->champdatefin=end($temp_results)+(86400 * $config['default_poll_duration']);
if (Utils::issetAndNoEmpty('champdatefin')) {
$registredate = explode("/",$_POST["champdatefin"]);
if (is_array($registredate) == true && count($registredate) == 3) {
$time = mktime(0,0,0,$registredate[1],$registredate[0],$registredate[2]);
if ($time > time() + (24*60*60)) {
- $_SESSION["champdatefin"]=$time;
+ $_SESSION['form']->champdatefin=$time;
}
}
}
- ajouter_sondage();
+exit(''.print_r($_SESSION, true).'
');
+ $admin_poll_id = ajouter_sondage(
+ $_SESSION['form']->titre,
+ $_SESSION['form']->commentaires,
+ $_SESSION['form']->nom,
+ $_SESSION['form']->adresse,
+ $_SESSION['form']->formatsondage,
+ $_SESSION['form']->champdatefin,
+ $_SESSION['form']->mailsonde,
+ $_SESSION['form']->toutchoix
+ );
+
+ unset($_SESSION['form']);
+exit(''.print_r($_SESSION, true).'
');
+
+
+ Utils::cleaningOldPolls($connect, 'admin/logs_studs.txt');
+
+ // TODO cleanup $_SESSION + Redirect
+
+ // Don't keep days, hours and choices in memory (in order to make new polls)
+ //for ($i = 0; $i < count($_SESSION['totalchoixjour']); $i++) {
+ // unset($_SESSION['horaires'.$i]);
+ //}
+ //unset($_SESSION['totalchoixjour']);
+ //unset($_SESSION['choices']);
+
+ //header('Location:' . Utils::getUrlSondage($sondage_admin, true));
+
+ exit;
} else {
+
if (Utils::issetAndNoEmpty('days')) {
- if (!isset($_SESSION["totalchoixjour"])) {
- $_SESSION["totalchoixjour"]=array();
- }
- $k = 0;
- for ($i = 0; $i < count($_POST["days"]); $i++) {
- if (isset($_POST["days"][$i]) && $_POST["days"][$i] !='') {
- $_SESSION['totalchoixjour'][$k] = mktime(0, 0, 0, substr($_POST["days"][$i],3,2),substr($_POST["days"][$i],0,2),substr($_POST["days"][$i],6,4));
+
+ // Clear previous choices
+ $_SESSION['form']->clearChoices();
- $l = 0;
- for($j = 0; $j < count($_POST['horaires'.$i]); $j++) {
- if (isset($_POST['horaires'.$i][$j]) && $_POST['horaires'.$i][$j] != '') {
- $_SESSION['horaires'.$k][$l] = $_POST['horaires'.$i][$j];
- $l++;
+ for ($i = 0; $i < count($_POST['days']); $i++) {
+ $day = $_POST['days'][$i];
+
+ if (!empty($day)) {
+ // Add choice to Form data
+ $time = mktime(0, 0, 0, substr($_POST["days"][$i],3,2),substr($_POST["days"][$i],0,2),substr($_POST["days"][$i],6,4));
+ $choice = new Choice($time);
+ $_SESSION['form']->addChoice($choice);
+
+ $schedules = $_POST['horaires'.$i];
+ for($j = 0; $j < count($schedules); $j++) {
+ if (!empty($schedules[$j])) {
+ $choice->addSlot($schedules[$j]);
}
}
- $k++;
}
}
}
}
//le format du sondage est DATE
- $_SESSION["formatsondage"] = "D".$_SESSION["studsplus"];
+ $_SESSION['form']->formatsondage = "D".$_SESSION['form']->studsplus;
// Step 3/3 : Confirm poll creation
- if (Utils::issetAndNoEmpty('choixheures') && Utils::issetAndNoEmpty('totalchoixjour', $_SESSION)) {
+ if (Utils::issetAndNoEmpty('choixheures') && !isset($_SESSION['form']->totalchoixjour)) {
Utils::print_header ( _("Removal date and confirmation (3 on 3)") );
bandeau_titre(_("Removal date and confirmation (3 on 3)"));
- $temp_array = array_unique($_SESSION["totalchoixjour"]);
- sort($temp_array);
- $removal_date=utf8_encode(strftime($date_format['txt_full'], end($temp_array)+ (86400 * $config['default_poll_duration'])));
+ $_SESSION['form']->sortChoices();
+ $last_date = $_SESSION['form']->lastChoice()->getName();
+ $removal_date = utf8_encode(strftime($date_format['txt_full'], $last_date + (86400 * $config['default_poll_duration'])));
- // Sumary
+ // Summary
$summary = '';
- for ($i=0;$igetChoices() as $choice) {
+ $summary .= '- '.strftime($date_format['txt_full'], $choice->getName());
+ $first = true;
+ foreach ($choice->getSlots() as $slots) {
+ $summary .= $first ? ' : ' : ', ';
+ $summary .= $slots;
+ $first = false;
}
- $summary .= '
'."\n";
+ $summary .= '';
}
$summary .= '
';
@@ -177,6 +210,7 @@ if (Utils::issetAndNoEmpty('titre', $_SESSION) === false || Utils::issetAndNoEmp
'."\n";
+//exit('POST
'.print_r($_POST, true).'
SESSION
'.print_r($_SESSION, true).'
');
bandeau_pied();
diff --git a/creation_sondage.php b/creation_sondage.php
index 1e7f982..1226637 100644
--- a/creation_sondage.php
+++ b/creation_sondage.php
@@ -16,20 +16,20 @@
* Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ
* Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
*/
+// TODO Move this file into a class into app/classes/Framadate
namespace Framadate;
-if (session_id() == "") {
- session_start();
-}
-
include_once __DIR__ . '/app/inc/init.php';
-//Generer une chaine de caractere unique et aleatoire
+/**
+ * Generer une chaine de caractere unique et aleatoire
+ */
function random($car)
{
- $string = "";
- $chaine = "abcdefghijklmnopqrstuvwxyz123456789";
+// TODO Better random ?
+ $string = '';
+ $chaine = 'abcdefghijklmnopqrstuvwxyz123456789';
srand((double)microtime()*1000000);
for($i=0; $i<$car; $i++) {
$string .= $chaine[rand()%strlen($chaine)];
@@ -38,63 +38,43 @@ function random($car)
return $string;
}
-function ajouter_sondage()
+function ajouter_sondage($title, $comment, $adminName, $adminMail, $format, $endDate, $mailsonde, $slots)
{
global $connect;
+ global $config;
+ $poll_id = random(16);
+ $admin_poll_id = $poll_id.random(8);
- $sondage=random(16);
- $sondage_admin=$sondage.random(8);
-
- $date_fin = $_SESSION["champdatefin"]; // provided by choix_autre.php or choix_date.php
- $_SESSION["champdatefin"]=""; //clean param cause 2 polls created by the same user in the same session can be affected by this param during the 2nd creation.
+ $date_fin = $_SESSION['champdatefin']; // provided by choix_autre.php or choix_date.php
+ $_SESSION['champdatefin'] = ''; //clean param cause 2 polls created by the same user in the same session can be affected by this param during the 2nd creation.
$sql = 'INSERT INTO sondage
(id_sondage, commentaires, mail_admin, nom_admin, titre, id_sondage_admin, date_fin, format, mailsonde)
- VALUES (
- '.$connect->Param('id_sondage').',
- '.$connect->Param('commentaires').',
- '.$connect->Param('mail_admin').',
- '.$connect->Param('nom_admin').',
- '.$connect->Param('titre').',
- '.$connect->Param('id_sondage_admin').',
- FROM_UNIXTIME('.$date_fin.'),
- '.$connect->Param('format').',
- '.$connect->Param('mailsonde').'
- )';
- $sql = $connect->Prepare($sql);
- $res = $connect->Execute($sql, array($sondage, $_SESSION['commentaires'], $_SESSION['adresse'], $_SESSION['nom'], $_SESSION['titre'], $sondage_admin, $_SESSION['formatsondage'], $_SESSION['mailsonde']));
+ VALUES (?,?,?,?,?,?,?,?)';
+ $prepared = $connect->prepare($sql);
+ $res = $prepared->execute(array($poll_id, $comment, $adminMail, $adminName, $title, $admin_poll_id, $format, $mailsonde));
- $sql = 'INSERT INTO sujet_studs values ('.$connect->Param('sondage').', '.$connect->Param('choix').')';
- $sql = $connect->Prepare($sql);
- $connect->Execute($sql, array($sondage, $_SESSION['toutchoix']));
+ $prepared = $connect->prepare('INSERT INTO sujet_studs values (?, ?)');
+ $prepared->execute(array($poll_id, $slots));
- if($config['use_smtp']==true){
+ 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["nom"],ENT_QUOTES,"UTF-8"))." " . _("hast just created a poll called") . " : \"".stripslashes(htmlspecialchars_decode($_SESSION["titre"],ENT_QUOTES))."\".\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($sondage));
- $message_admin = sprintf($message_admin, Utils::getUrlSondage($sondage_admin, true));
+ $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( "$_SESSION[adresse]", "[".NOMAPPLICATION."][" . _("Author's message") . "] " . _("Poll") . " : ".stripslashes(htmlspecialchars_decode($_SESSION["titre"],ENT_QUOTES)), $message_admin, $_SESSION['adresse'] );
- Utils::sendEmail( "$_SESSION[adresse]", "[".NOMAPPLICATION."][" . _("For sending to the polled users") . "] " . _("Poll") . " : ".stripslashes(htmlspecialchars_decode($_SESSION["titre"],ENT_QUOTES)), $message, $_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: '.$sondage."\t".$_SESSION[formatsondage]."\t".$_SESSION[nom]."\t".$_SESSION[adresse]."\t \t".$_SESSION[toutchoix]."\n", 3, 'admin/logs_studs.txt');
- Utils::cleaning_polls($connect, 'admin/logs_studs.txt');
+
+ error_log(date('H:i:s d/m/Y:') . ' CREATION: '.$poll_id."\t".$format."\t".$adminName."\t".$adminMail."\t \t".$slots."\n", 3, 'admin/logs_studs.txt');
- // Don't keep days, hours and choices in memory (in order to make new polls)
- for ($i = 0; $i < count($_SESSION["totalchoixjour"]); $i++) {
- unset($_SESSION['horaires'.$i]);
- }
- unset($_SESSION["totalchoixjour"]);
- unset($_SESSION['choices']);
-
- header("Location:".Utils::getUrlSondage($sondage_admin, true));
-
- exit();
+ return $admin_poll_id;
}
diff --git a/index.php b/index.php
index 6fed009..4e46fbe 100644
--- a/index.php
+++ b/index.php
@@ -18,8 +18,6 @@
*/
namespace Framadate;
-use Framadate\Utils;
-
include_once __DIR__ . '/app/inc/init.php';
if (is_readable('bandeaux_local.php')) {
@@ -28,12 +26,9 @@ if (is_readable('bandeaux_local.php')) {
include_once('bandeaux.php');
}
-session_start();
-
// affichage de la page
Utils::print_header( _("Home") );
bandeau_titre(_("Make your polls"));
-
echo '
diff --git a/infos_sondage.php b/infos_sondage.php
index 59d5848..99f27c3 100644
--- a/infos_sondage.php
+++ b/infos_sondage.php
@@ -18,8 +18,10 @@
*/
namespace Framadate;
-session_start();
include_once __DIR__ . '/app/inc/init.php';
+if (!isset($_SESSION['form'])) {
+ $_SESSION['form'] = new Form();
+}
if (file_exists('bandeaux_local.php')) {
include_once('bandeaux_local.php');
@@ -27,14 +29,14 @@ if (file_exists('bandeaux_local.php')) {
include_once('bandeaux.php');
}
-// Type de sondage :