ADD #143 (80%)
[cl7.git] / php / controller.php
1 <?php # coding: utf-8
2
3 /**
4 * 'Controller' traite les données envoyées par le client.
5 */
6 class Controller
7 {
8 public $message_utilisateur = NULL;
9 public $nouvel_inscrit = FALSE;
10
11 private $participant;
12 private $config;
13
14 public function Controller($participant, $config)
15 {
16 $this->participant = $participant;
17 $this->config = $config;
18
19 # inscription d'un nouveau participant
20 if (isset($_POST['inscription']) && !Participant::nombre_participant_max_atteint())
21 {
22 if ($this->config->get("inscription_terminees"))
23 return;
24
25 $this->traiter_donnees_inscription();
26 if (!$this->login_deja_pris() && # vérification des données
27 $this->donnees_inscription_valides() &&
28 $_POST['accord'] == "on"
29 )
30 {
31 pg_query("BEGIN");
32
33 // Cherche un id libre.
34 // Pour des questions de sécurité, les ids ne sont pas générés sequentiellement
35 // car ils sont mémorisé dans un cookie et permette l'authentification.
36 $id = 0;
37 do
38 {
39 $id = rand(1, PHP_INT_MAX);
40 $row = pg_fetch_row(pg_query_params("SELECT count(*) FROM participants WHERE id = $1", array($id)));
41 } while ($row[0] != 0);
42
43 pg_query_params("
44 INSERT INTO participants
45 (id, pseudo, password, clan_nom, clan_tag, nom, prenom, age, e_mail, remarques)
46 VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
47 ",
48 array(
49 $id,
50 $_POST['pseudo'],
51 sha1($_POST['pass1']),
52 $_POST['clan_nom'],
53 $_POST['clan_tag'],
54 $_POST['nom'],
55 $_POST['prenom'],
56 $_POST['age'],
57 $_POST['e_mail'],
58 $_POST['remarques'],
59 )
60 );
61
62 $this->set_periodes($id);
63 pg_query("COMMIT");
64
65 $this->participant->chargerInfos($_POST['pseudo'], $_POST['pass1']);
66 setcookie("COOKIE_INFO_PARTICIPANT", $this->participant->info->id, time() + 31104000);
67 $this->nouvel_inscrit = TRUE;
68 }
69 }
70 # un participant modifie ses infos
71 else if(isset($_POST['modification_participant']) && $this->participant->existe())
72 {
73 if ($this->config->get("inscription_terminees"))
74 return;
75
76 $this->traiter_donnees_inscription();
77 if (!$this->login_deja_pris() && $this->donnees_inscription_valides(true))
78 {
79 pg_query("BEGIN");
80 pg_query_params("UPDATE participants SET pseudo = $1 WHERE id = $2", array($_POST['pseudo'], $this->participant->info->id));
81 if ($_POST['pass1'] != '') // Uniquement si un nouveau password est donné.
82 pg_query_params("UPDATE participants SET password = $1 WHERE id = $2", array(sha1($_POST['pass1']), $this->participant->info->id));
83 pg_query_params("UPDATE participants SET clan_nom = $1 WHERE id = $2", array($_POST['clan_nom'], $this->participant->info->id));
84 pg_query_params("UPDATE participants SET clan_tag = $1 WHERE id = $2", array($_POST['clan_tag'], $this->participant->info->id));
85 pg_query_params("UPDATE participants SET nom = $1 WHERE id = $2", array($_POST['nom'], $this->participant->info->id));
86 pg_query_params("UPDATE participants SET prenom = $1 WHERE id = $2", array($_POST['prenom'], $this->participant->info->id));
87 pg_query_params("UPDATE participants SET age = $1 WHERE id = $2", array($_POST['age'], $this->participant->info->id));
88 pg_query_params("UPDATE participants SET e_mail = $1 WHERE id = $2", array($_POST['e_mail'], $this->participant->info->id));
89 pg_query_params("UPDATE participants SET remarques = $1 WHERE id = $2", array($_POST['remarques'], $this->participant->info->id));
90 $this->set_periodes($this->participant->info->id);
91 pg_query("COMMIT");
92 //header("Location: /inscrits.html");
93 $this->message_utilisateur = "Les modifications ont été enregistrées";
94
95 // Puisque les données du participant ont changés il faut les re-charger.
96 $this->participant->chargerInfos();
97 }
98 }
99 # vote pour des jeux (autorisé même lorsque les inscriptions sont terminées)
100 else if (isset($_POST['set_jeux_joues']) && $this->participant->existe() && !$config->get("inscription_terminees"))
101 {
102 $votes = $_POST['votes'];
103 if (!$votes)
104 $votes = array();
105
106 pg_query("BEGIN");
107
108 # l'utilisateur peut proposer le nom d'un jeu qui ne se trouve pas dans la liste
109 $jeu = trim($_POST['jeu']);
110 if ($jeu !== '')
111 {
112 if(@pg_query_params("INSERT INTO jeux (participant_id, nom, type, url) VALUES ($1, $2, $3, $4)", array($this->participant->info->id, $jeu, $_POST['type'], $_POST['url'])))
113 {
114 $row = pg_fetch_row(pg_query("SELECT LASTVAL()"));
115 $id = $row[0];
116 if ($id != 0) # si le jeu se trouve déjà dans la liste alors $id == 0
117 array_unshift($votes, $id);
118 }
119 else # Puisque le jeu existe déjà, on le recherche
120 {
121 pg_query("ROLLBACK");
122 pg_query("BEGIN");
123 $res = pg_query_params("SELECT id FROM jeux WHERE nom = $1", array($jeu));
124 if ($id = pg_fetch_object($res))
125 if (!in_array($id->id, $votes))
126 array_unshift($votes, $id->id);
127 }
128 }
129
130 # suppression des anciens votes (remplacement par les nouveaux)
131 pg_query_params("DELETE FROM jeux_choisis WHERE participant_id = $1", array($this->participant->info->id));
132
133 # traite les trois premiers votes
134 for ($i = 0; $i < count($votes) && $i < $this->config->get('nb_votes_jeux'); $i++)
135 {
136 pg_query_params("INSERT INTO jeux_choisis (participant_id, jeu_id) VALUES ($1, $2)", array($this->participant->info->id, (int)$votes[$i]));
137 }
138
139 pg_query("COMMIT");
140 }
141 }
142
143 /**
144 * Traiter les données de l'inscription (trim par exemple).
145 */
146 private function traiter_donnees_inscription()
147 {
148 $_POST['pseudo'] = trim($_POST['pseudo']);
149 }
150
151 private function login_deja_pris()
152 {
153 if ($this->participant->existe() && strtolower($this->participant->info->pseudo) === strtolower($_POST['pseudo'])) // le pseudo n'a pas changé
154 return FALSE;
155
156 $loginDejaPris = pg_fetch_array(pg_query_params("SELECT count(*) FROM participants WHERE pseudo = $1", array($_POST['pseudo'])));
157 if ($loginDejaPris[0] > 0)
158 {
159 $this->message_utilisateur = "Le pseudo '".$_POST["pseudo"]."' est déjà pris";
160 return TRUE;
161 }
162 return FALSE;
163 }
164
165 /**
166 * Renvoie TRUE si les données d'une inscription sont valides (POST).
167 * $maj : égal 'TRUE' si c'est une mise à jour, dans ce cas le password est traité différement
168 */
169 private function donnees_inscription_valides($maj = FALSE)
170 {
171 return
172 $_POST['pseudo'] != "" &&
173 ($maj || $_POST['pass1'] != "") &&
174 $_POST['pass1'] == $_POST['pass2'] &&
175 (($maj && $_POST['pass1'] == "") || strlen($_POST['pass1']) >= 3) &&
176 $_POST['nom'] != "" &&
177 $_POST['prenom'] != "" &&
178 $_POST['e_mail'] != "";
179 }
180
181 /**
182 * Met à jour les periodes du participant dont l'id est donnée
183 * en fonction de $_POST["periodes"]
184 * Attention, cette fonction doit être appelée dans une transaction.
185 */
186 private function set_periodes($id)
187 {
188 $periodes = $_POST['periodes'];
189 if (!$periodes)
190 $periodes = array();
191
192 pg_query_params("DELETE FROM participations WHERE participant_id = $1", array($id));
193 for ($i = 0; $i < count($periodes); $i++)
194 {
195 pg_query_params("
196 INSERT INTO participations (participant_id, periode_id)
197 VALUES ($1, $2)", array($id, $periodes[$i])
198 );
199 }
200 }
201 }
202
203 ?>