0337d704 |
1 | <?php |
2 | /*************************************************************************** |
50a40a33 |
3 | * Copyright (C) 2003-2006 Polytechnique.org * |
0337d704 |
4 | * http://opensource.polytechnique.org/ * |
5 | * * |
6 | * This program is free software; you can redistribute it and/or modify * |
7 | * it under the terms of the GNU General Public License as published by * |
8 | * the Free Software Foundation; either version 2 of the License, or * |
9 | * (at your option) any later version. * |
10 | * * |
11 | * This program is distributed in the hope that it will be useful, * |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
14 | * GNU General Public License for more details. * |
15 | * * |
16 | * You should have received a copy of the GNU General Public License * |
17 | * along with this program; if not, write to the Free Software * |
18 | * Foundation, Inc., * |
19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
20 | ***************************************************************************/ |
21 | |
0337d704 |
22 | define('SIZE_MAX', 32768); |
23 | |
2f151d5f |
24 | __autoload('XDB'); |
25 | |
0337d704 |
26 | /** |
27 | * Iterator class, that lists objects through the database |
28 | */ |
29 | class ValidateIterator extends XOrgDBIterator |
30 | { |
31 | // {{{ constuctor |
d71befc4 |
32 | |
0337d704 |
33 | function ValidateIterator () |
34 | { |
917c4d11 |
35 | parent::__construct('SELECT data,stamp FROM requests ORDER BY stamp', MYSQL_NUM); |
0337d704 |
36 | } |
37 | |
38 | // }}} |
39 | // {{{ function next() |
40 | |
41 | function next () |
42 | { |
43 | if (list($result, $stamp) = parent::next()) { |
44 | $result = unserialize($result); |
45 | $result->stamp = $stamp; |
46 | return($result); |
47 | } else { |
48 | return null; |
49 | } |
50 | } |
51 | |
52 | // }}} |
53 | } |
54 | |
0337d704 |
55 | /** classe "virtuelle" à dériver pour chaque nouvelle implémentation |
56 | */ |
57 | class Validate |
58 | { |
59 | // {{{ properties |
60 | |
61 | var $uid; |
62 | var $prenom; |
63 | var $nom; |
64 | var $promo; |
65 | var $bestalias; |
66 | var $forlife; |
67 | |
68 | var $stamp; |
69 | var $unique; |
70 | // enable the refuse button |
71 | var $refuse = true; |
6aa01fed |
72 | |
0337d704 |
73 | var $type; |
74 | var $comments = Array(); |
75 | // the validations rules : comments for admins |
76 | var $rules = "Mieux vaut laisser une demande de validation à un autre admin que de valider une requête illégale ou que de refuser une demande légitime"; |
77 | |
78 | // }}} |
79 | // {{{ constructor |
80 | |
81 | /** constructeur |
82 | * @param $_uid user id |
83 | * @param $_unique requête pouvant être multiple ou non |
84 | * @param $_type type de la donnée comme dans le champ type de x4dat.requests |
85 | */ |
86 | function Validate($_uid, $_unique, $_type) |
87 | { |
0337d704 |
88 | $this->uid = $_uid; |
89 | $this->stamp = date('YmdHis'); |
90 | $this->unique = $_unique; |
91 | $this->type = $_type; |
08cce2ff |
92 | $res = XDB::query( |
0337d704 |
93 | "SELECT u.prenom, u.nom, u.promo, a.alias, b.alias |
94 | FROM auth_user_md5 AS u |
95 | INNER JOIN aliases AS a ON ( u.user_id=a.id AND a.type='a_vie' ) |
96 | INNER JOIN aliases AS b ON ( u.user_id=b.id AND b.type!='homonyme' AND FIND_IN_SET('bestalias', b.flags) ) |
97 | WHERE u.user_id={?}", $_uid); |
98 | list($this->prenom, $this->nom, $this->promo, $this->forlife, $this->bestalias) = $res->fetchOneRow(); |
99 | } |
100 | |
101 | // }}} |
102 | // {{{ function submit() |
103 | |
104 | /** fonction à utiliser pour envoyer les données à la modération |
105 | * cette fonction supprimme les doublons sur un couple ($user,$type) si $this->unique est vrai |
106 | */ |
107 | function submit () |
108 | { |
0337d704 |
109 | if ($this->unique) { |
08cce2ff |
110 | XDB::execute('DELETE FROM requests WHERE user_id={?} AND type={?}', $this->uid, $this->type); |
0337d704 |
111 | } |
112 | |
113 | $this->stamp = date('YmdHis'); |
08cce2ff |
114 | XDB::execute('INSERT INTO requests (user_id, type, data, stamp) VALUES ({?}, {?}, {?}, {?})', |
0337d704 |
115 | $this->uid, $this->type, $this, $this->stamp); |
116 | |
117 | return true; |
118 | } |
119 | |
120 | // }}} |
121 | // {{{ function update() |
122 | |
123 | function update () |
124 | { |
08cce2ff |
125 | XDB::execute('UPDATE requests SET data={?}, stamp=stamp |
0337d704 |
126 | WHERE user_id={?} AND type={?} AND stamp={?}', |
127 | $this, $this->uid, $this->type, $this->stamp); |
128 | |
129 | return true; |
130 | } |
131 | |
132 | // }}} |
133 | // {{{ function clean() |
134 | |
135 | /** fonction à utiliser pour nettoyer l'entrée de la requête dans la table requests |
136 | * attention, tout est supprimé si c'est un unique |
137 | */ |
138 | function clean () |
139 | { |
0337d704 |
140 | if ($this->unique) { |
08cce2ff |
141 | return XDB::execute('DELETE FROM requests WHERE user_id={?} AND type={?}', |
0337d704 |
142 | $this->uid, $this->type); |
143 | } else { |
08cce2ff |
144 | return XDB::execute('DELETE FROM requests WHERE user_id={?} AND type={?} AND stamp={?}', |
0337d704 |
145 | $this->uid, $this->type, $this->stamp); |
146 | } |
147 | } |
148 | |
149 | // }}} |
150 | // {{{ function handle_formu() |
151 | |
152 | /** fonction à réaliser en cas de valistion du formulaire |
153 | */ |
154 | function handle_formu() |
155 | { |
156 | if (Env::has('delete')) { |
157 | $this->clean(); |
158 | $this->trig('requete supprimée'); |
159 | return true; |
160 | } |
161 | |
6aa01fed |
162 | // mise à jour des informations |
163 | if (Env::has('edit')) { |
164 | if ($this->handle_editor()) { |
165 | $this->update(); |
166 | $this->trig('requête mise à jour'); |
167 | return true; |
168 | } |
169 | return false; |
170 | } |
171 | |
0337d704 |
172 | // ajout d'un commentaire |
173 | if (Env::has('hold') && Env::has('comm')) { |
4791ff77 |
174 | $formid = Env::i('formid'); |
175 | foreach ($this->comments as $comment) { |
176 | if ($comment[2] === $formid) { |
177 | return true; |
178 | } |
179 | } |
180 | $this->comments[] = Array(S::v('bestalias'), Env::v('comm'), $formid); |
0337d704 |
181 | |
182 | // envoi d'un mail à hotliners |
183 | global $globals; |
184 | require_once('diogenes/diogenes.hermes.inc.php'); |
185 | $mailer = new HermesMailer; |
186 | $mailer->setSubject("Commentaires de validation {$this->type}"); |
187 | $mailer->setFrom("validation+{$this->type}@{$globals->mail->domain}"); |
c33efb7c |
188 | $mailer->addTo("hotliners@staff.polytechnique.org"); |
0337d704 |
189 | |
190 | $body = "Validation {$this->type} pour {$this->prenom} {$this->nom}\n\n" |
cab08090 |
191 | . S::v('bestalias')." a ajouté le commentaire :\n\n" |
5e2307dc |
192 | . Env::v('comm')."\n\n" |
8c7809b5 |
193 | . "cf la discussion sur : ".$globals->baseurl."/admin/validate"; |
0337d704 |
194 | |
195 | $mailer->setTxtBody(wordwrap($body)); |
196 | $mailer->send(); |
197 | |
198 | $this->update(); |
199 | $this->trig('commentaire ajouté'); |
200 | return true; |
201 | } |
202 | |
203 | if (Env::has('accept')) { |
204 | if ($this->commit()) { |
205 | $this->sendmail(true); |
206 | $this->clean(); |
207 | $this->trig('mail envoyé'); |
208 | return true; |
209 | } else { |
210 | $this->trig('erreur lors de la validation'); |
211 | return false; |
212 | } |
213 | } |
214 | |
215 | if (Env::has('refuse')) { |
5e2307dc |
216 | if (Env::v('comm')) { |
0337d704 |
217 | $this->sendmail(false); |
218 | $this->clean(); |
219 | $this->trig('mail envoyé'); |
220 | return true; |
221 | } else { |
222 | $this->trig('pas de motivation pour le refus !!!'); |
223 | } |
224 | } |
225 | |
226 | return false; |
227 | } |
228 | |
229 | // }}} |
230 | // {{{ function sendmail |
231 | |
232 | function sendmail($isok) |
233 | { |
234 | global $globals; |
235 | require_once('diogenes/diogenes.hermes.inc.php'); |
236 | $mailer = new HermesMailer; |
237 | $mailer->setSubject($this->_mail_subj()); |
238 | $mailer->setFrom("validation+{$this->type}@{$globals->mail->domain}"); |
239 | $mailer->addTo("\"{$this->prenom} {$this->nom}\" <{$this->bestalias}@{$globals->mail->domain}>"); |
240 | $mailer->addCc("validation+{$this->type}@{$globals->mail->domain}"); |
241 | |
242 | $body = "Cher(e) camarade,\n\n" |
243 | . $this->_mail_body($isok) |
5e2307dc |
244 | . (Env::has('comm') ? "\n\n".Env::v('comm') : '') |
0337d704 |
245 | . "\n\nCordialement,\nL'équipe Polytechnique.org\n"; |
246 | |
247 | $mailer->setTxtBody(wordwrap($body)); |
248 | $mailer->send(); |
249 | } |
250 | |
251 | // }}} |
252 | // {{{ function trig() |
253 | |
254 | function trig($msg) { |
255 | global $page; |
256 | $page->trig($msg); |
257 | } |
258 | |
259 | // }}} |
260 | // {{{ function get_request() |
261 | |
262 | /** fonction statique qui renvoie la requête de l'utilisateur d'id $uidau timestamp $t |
263 | * @param $uid l'id de l'utilisateur concerné |
264 | * @param $type le type de la requête |
265 | * @param $stamp le timestamp de la requête |
266 | * |
267 | * XXX fonction "statique" XXX |
268 | * à utiliser uniquement pour récupérer un objet dans la BD avec Validate::get_request(...) |
269 | */ |
270 | function get_request($uid, $type, $stamp = -1) |
271 | { |
0337d704 |
272 | if ($stamp == -1) { |
08cce2ff |
273 | $res = XDB::query('SELECT data FROM requests WHERE user_id={?} and type={?}', $uid, $type); |
0337d704 |
274 | } else { |
08cce2ff |
275 | $res = XDB::query("SELECT data, stamp FROM requests WHERE user_id={?} AND type={?} and stamp={?}", $uid, $type, $stamp); |
0337d704 |
276 | } |
277 | if ($result = $res->fetchOneCell()) { |
278 | $result = unserialize($result); |
279 | } else { |
280 | $result = false; |
281 | } |
282 | return($result); |
283 | } |
284 | |
285 | // }}} |
286 | // {{{ function _mail_body |
287 | |
288 | function _mail_body($isok) |
289 | { |
290 | } |
291 | |
292 | // }}} |
293 | // {{{ function _mail_subj |
294 | |
295 | function _mail_subj() |
296 | { |
297 | } |
298 | |
299 | // }}} |
300 | // {{{ function commit() |
301 | |
302 | /** fonction à utiliser pour insérer les données dans x4dat |
303 | * XXX la fonction est "virtuelle" XXX |
304 | */ |
305 | function commit () |
306 | { } |
307 | |
308 | // }}} |
309 | // {{{ function formu() |
310 | |
311 | /** nom du template qui contient le formulaire */ |
312 | function formu() |
313 | { return null; } |
314 | |
315 | // }}} |
6aa01fed |
316 | // {{{ function editor() |
317 | |
318 | /** nom du formulaire d'édition */ |
319 | function editor() |
320 | { return null; } |
321 | |
322 | // }}} |
e18888f4 |
323 | // {{{ function answers() |
324 | |
325 | /** automatic answers table for this type of validation */ |
326 | function answers() { |
327 | static $answers_table; |
328 | if (!isset($answers_table[$this->type])) { |
329 | $r = XDB::query("SELECT id, title, answer FROM requests_answers WHERE category = {?}", $this->type); |
330 | $answers_table[$this->type] = $r->fetchAllAssoc($r); |
331 | } |
332 | return $answers_table[$this->type]; |
333 | } |
334 | |
335 | // }}} |
ed5b9703 |
336 | // {{{ function id() |
337 | |
338 | function id() |
339 | { |
340 | return $this->uid . '_' . $this->type . '_' . $this->stamp; |
341 | } |
342 | |
343 | // }}} |
0337d704 |
344 | } |
345 | |
0337d704 |
346 | foreach (glob(dirname(__FILE__).'/validations/*.inc.php') as $file) { |
347 | require_once($file); |
348 | } |
349 | |
0337d704 |
350 | /* vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 foldmethod=marker: */ |
351 | ?> |