diff --git a/admin/migration.php b/admin/migration.php index 1551e9d..f60d9ce 100644 --- a/admin/migration.php +++ b/admin/migration.php @@ -20,6 +20,7 @@ use Framadate\Migration\From_0_0_to_0_8_Migration; use Framadate\Migration\From_0_8_to_0_9_Migration; use Framadate\Migration\AddColumn_receiveNewComments_For_0_9; +use Framadate\Migration\AddColumn_uniqId_In_vote_For_0_9; use Framadate\Migration\Migration; use Framadate\Utils; @@ -31,7 +32,8 @@ set_time_limit(300); $migrations = [ new From_0_0_to_0_8_Migration(), new From_0_8_to_0_9_Migration(), - new AddColumn_receiveNewComments_For_0_9() + new AddColumn_receiveNewComments_For_0_9(), + new AddColumn_uniqId_In_vote_For_0_9() ]; // --------------------------------------- diff --git a/app/classes/Framadate/FramaDB.php b/app/classes/Framadate/FramaDB.php index d9743be..ba1b250 100644 --- a/app/classes/Framadate/FramaDB.php +++ b/app/classes/Framadate/FramaDB.php @@ -122,15 +122,16 @@ class FramaDB { return $prepared->execute([$insert_position, $insert_position + 1, $poll_id]); } - function insertVote($poll_id, $name, $choices) { - $prepared = $this->prepare('INSERT INTO `' . Utils::table('vote') . '` (poll_id, name, choices) VALUES (?,?,?)'); - $prepared->execute([$poll_id, $name, $choices]); + function insertVote($poll_id, $name, $choices, $token) { + $prepared = $this->prepare('INSERT INTO `' . Utils::table('vote') . '` (poll_id, name, choices, uniqId) VALUES (?,?,?,?)'); + $prepared->execute([$poll_id, $name, $choices, $token]); $newVote = new \stdClass(); $newVote->poll_id = $poll_id; $newVote->id = $this->pdo->lastInsertId(); $newVote->name = $name; $newVote->choices = $choices; + $newVote->token = $token; return $newVote; } diff --git a/app/classes/Framadate/Migration/AddColumn_uniqId_In_vote_For_0_9.php b/app/classes/Framadate/Migration/AddColumn_uniqId_In_vote_For_0_9.php new file mode 100644 index 0000000..9f50e4a --- /dev/null +++ b/app/classes/Framadate/Migration/AddColumn_uniqId_In_vote_For_0_9.php @@ -0,0 +1,79 @@ +query('SHOW TABLES'); + $tables = $stmt->fetchAll(\PDO::FETCH_COLUMN); + + // Check if tables of v0.9 are presents + $diff = array_diff([Utils::table('poll'), Utils::table('slot'), Utils::table('vote'), Utils::table('comment')], $tables); + return count($diff) === 0; + } + + /** + * This methode is called only one time in the migration page. + * + * @param \PDO $pdo The connection to database + * @return bool true is the execution succeeded + */ + function execute(\PDO $pdo) { + $this->alterPollTable($pdo); + + return true; + } + + private function alterPollTable(\PDO $pdo) { + $pdo->exec(' + ALTER TABLE `' . Utils::table('vote') . '` + ADD `uniqId` CHAR(16) NOT NULL + AFTER `id`, + ADD INDEX (`uniqId`) ;'); + } + +} diff --git a/app/classes/Framadate/Services/PollService.php b/app/classes/Framadate/Services/PollService.php index cf32713..0fd397f 100644 --- a/app/classes/Framadate/Services/PollService.php +++ b/app/classes/Framadate/Services/PollService.php @@ -21,6 +21,7 @@ namespace Framadate\Services; use Framadate\Form; use Framadate\FramaDB; use Framadate\Utils; +use Framadate\Security\Token; class PollService { @@ -66,8 +67,8 @@ class PollService { function addVote($poll_id, $name, $choices) { $choices = implode($choices); - - return $this->connect->insertVote($poll_id, $name, $choices); + $token = $this->random(16); + return $this->connect->insertVote($poll_id, $name, $choices, $token); } function addComment($poll_id, $name, $comment) { @@ -176,15 +177,8 @@ class PollService { return [$poll_id, $admin_poll_id]; } - private function random($car) { - // TODO Better random ? - $string = ''; - $chaine = 'abcdefghijklmnopqrstuvwxyz123456789'; - mt_srand(); - for ($i = 0; $i < $car; $i++) { - $string .= $chaine[mt_rand() % strlen($chaine)]; - } - - return $string; + private function random($length) { + return Token::getToken($length); } + } diff --git a/app/inc/constants.php b/app/inc/constants.php index 60bab3c..fb5b618 100644 --- a/app/inc/constants.php +++ b/app/inc/constants.php @@ -21,7 +21,7 @@ const VERSION = '0.9'; // Regex -const POLL_REGEX = '/^[a-z0-9]+$/'; +const POLL_REGEX = '/^[a-zA-Z0-9]+$/'; const CHOICE_REGEX = '/^[012]$/'; const NAME_REGEX = '/^[áàâäãåçéèêëíìîïñóòôöõúùûüýÿæœa-z0-9_ -]+$/i'; const BOOLEAN_REGEX = '/^(on|off|true|false|1|0)$/';