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 | |
0337d704 |
24 | /** |
25 | * Iterator class, that lists objects through the database |
26 | */ |
27 | class ValidateIterator extends XOrgDBIterator |
28 | { |
29 | // {{{ constuctor |
d71befc4 |
30 | |
0337d704 |
31 | function ValidateIterator () |
32 | { |
33 | parent::XOrgDBIterator('SELECT data,stamp FROM requests ORDER BY stamp', MYSQL_NUM); |
34 | } |
35 | |
36 | // }}} |
37 | // {{{ function next() |
38 | |
39 | function next () |
40 | { |
41 | if (list($result, $stamp) = parent::next()) { |
42 | $result = unserialize($result); |
43 | $result->stamp = $stamp; |
44 | return($result); |
45 | } else { |
46 | return null; |
47 | } |
48 | } |
49 | |
50 | // }}} |
51 | } |
52 | |
0337d704 |
53 | /** classe "virtuelle" à dériver pour chaque nouvelle implémentation |
54 | */ |
55 | class Validate |
56 | { |
57 | // {{{ properties |
58 | |
59 | var $uid; |
60 | var $prenom; |
61 | var $nom; |
62 | var $promo; |
63 | var $bestalias; |
64 | var $forlife; |
65 | |
66 | var $stamp; |
67 | var $unique; |
68 | // enable the refuse button |
69 | var $refuse = true; |
70 | var $type; |
71 | var $comments = Array(); |
72 | // the validations rules : comments for admins |
73 | 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"; |
74 | |
75 | // }}} |
76 | // {{{ constructor |
77 | |
78 | /** constructeur |
79 | * @param $_uid user id |
80 | * @param $_unique requête pouvant être multiple ou non |
81 | * @param $_type type de la donnée comme dans le champ type de x4dat.requests |
82 | */ |
83 | function Validate($_uid, $_unique, $_type) |
84 | { |
0337d704 |
85 | $this->uid = $_uid; |
86 | $this->stamp = date('YmdHis'); |
87 | $this->unique = $_unique; |
88 | $this->type = $_type; |
08cce2ff |
89 | $res = XDB::query( |
0337d704 |
90 | "SELECT u.prenom, u.nom, u.promo, a.alias, b.alias |
91 | FROM auth_user_md5 AS u |
92 | INNER JOIN aliases AS a ON ( u.user_id=a.id AND a.type='a_vie' ) |
93 | INNER JOIN aliases AS b ON ( u.user_id=b.id AND b.type!='homonyme' AND FIND_IN_SET('bestalias', b.flags) ) |
94 | WHERE u.user_id={?}", $_uid); |
95 | list($this->prenom, $this->nom, $this->promo, $this->forlife, $this->bestalias) = $res->fetchOneRow(); |
96 | } |
97 | |
98 | // }}} |
99 | // {{{ function submit() |
100 | |
101 | /** fonction à utiliser pour envoyer les données à la modération |
102 | * cette fonction supprimme les doublons sur un couple ($user,$type) si $this->unique est vrai |
103 | */ |
104 | function submit () |
105 | { |
0337d704 |
106 | if ($this->unique) { |
08cce2ff |
107 | XDB::execute('DELETE FROM requests WHERE user_id={?} AND type={?}', $this->uid, $this->type); |
0337d704 |
108 | } |
109 | |
110 | $this->stamp = date('YmdHis'); |
08cce2ff |
111 | XDB::execute('INSERT INTO requests (user_id, type, data, stamp) VALUES ({?}, {?}, {?}, {?})', |
0337d704 |
112 | $this->uid, $this->type, $this, $this->stamp); |
113 | |
114 | return true; |
115 | } |
116 | |
117 | // }}} |
118 | // {{{ function update() |
119 | |
120 | function update () |
121 | { |
08cce2ff |
122 | XDB::execute('UPDATE requests SET data={?}, stamp=stamp |
0337d704 |
123 | WHERE user_id={?} AND type={?} AND stamp={?}', |
124 | $this, $this->uid, $this->type, $this->stamp); |
125 | |
126 | return true; |
127 | } |
128 | |
129 | // }}} |
130 | // {{{ function clean() |
131 | |
132 | /** fonction à utiliser pour nettoyer l'entrée de la requête dans la table requests |
133 | * attention, tout est supprimé si c'est un unique |
134 | */ |
135 | function clean () |
136 | { |
0337d704 |
137 | if ($this->unique) { |
08cce2ff |
138 | return XDB::execute('DELETE FROM requests WHERE user_id={?} AND type={?}', |
0337d704 |
139 | $this->uid, $this->type); |
140 | } else { |
08cce2ff |
141 | return XDB::execute('DELETE FROM requests WHERE user_id={?} AND type={?} AND stamp={?}', |
0337d704 |
142 | $this->uid, $this->type, $this->stamp); |
143 | } |
144 | } |
145 | |
146 | // }}} |
147 | // {{{ function handle_formu() |
148 | |
149 | /** fonction à réaliser en cas de valistion du formulaire |
150 | */ |
151 | function handle_formu() |
152 | { |
153 | if (Env::has('delete')) { |
154 | $this->clean(); |
155 | $this->trig('requete supprimée'); |
156 | return true; |
157 | } |
158 | |
159 | // ajout d'un commentaire |
160 | if (Env::has('hold') && Env::has('comm')) { |
4791ff77 |
161 | $formid = Env::i('formid'); |
162 | foreach ($this->comments as $comment) { |
163 | if ($comment[2] === $formid) { |
164 | return true; |
165 | } |
166 | } |
167 | $this->comments[] = Array(S::v('bestalias'), Env::v('comm'), $formid); |
0337d704 |
168 | |
169 | // envoi d'un mail à hotliners |
170 | global $globals; |
171 | require_once('diogenes/diogenes.hermes.inc.php'); |
172 | $mailer = new HermesMailer; |
173 | $mailer->setSubject("Commentaires de validation {$this->type}"); |
174 | $mailer->setFrom("validation+{$this->type}@{$globals->mail->domain}"); |
175 | $mailer->addTo("hotliners@{$globals->mail->domain}"); |
176 | |
177 | $body = "Validation {$this->type} pour {$this->prenom} {$this->nom}\n\n" |
cab08090 |
178 | . S::v('bestalias')." a ajouté le commentaire :\n\n" |
5e2307dc |
179 | . Env::v('comm')."\n\n" |
0337d704 |
180 | . "cf la discussion sur : ".$globals->baseurl."/admin/valider.php"; |
181 | |
182 | $mailer->setTxtBody(wordwrap($body)); |
183 | $mailer->send(); |
184 | |
185 | $this->update(); |
186 | $this->trig('commentaire ajouté'); |
187 | return true; |
188 | } |
189 | |
190 | if (Env::has('accept')) { |
191 | if ($this->commit()) { |
192 | $this->sendmail(true); |
193 | $this->clean(); |
194 | $this->trig('mail envoyé'); |
195 | return true; |
196 | } else { |
197 | $this->trig('erreur lors de la validation'); |
198 | return false; |
199 | } |
200 | } |
201 | |
202 | if (Env::has('refuse')) { |
5e2307dc |
203 | if (Env::v('comm')) { |
0337d704 |
204 | $this->sendmail(false); |
205 | $this->clean(); |
206 | $this->trig('mail envoyé'); |
207 | return true; |
208 | } else { |
209 | $this->trig('pas de motivation pour le refus !!!'); |
210 | } |
211 | } |
212 | |
213 | return false; |
214 | } |
215 | |
216 | // }}} |
217 | // {{{ function sendmail |
218 | |
219 | function sendmail($isok) |
220 | { |
221 | global $globals; |
222 | require_once('diogenes/diogenes.hermes.inc.php'); |
223 | $mailer = new HermesMailer; |
224 | $mailer->setSubject($this->_mail_subj()); |
225 | $mailer->setFrom("validation+{$this->type}@{$globals->mail->domain}"); |
226 | $mailer->addTo("\"{$this->prenom} {$this->nom}\" <{$this->bestalias}@{$globals->mail->domain}>"); |
227 | $mailer->addCc("validation+{$this->type}@{$globals->mail->domain}"); |
228 | |
229 | $body = "Cher(e) camarade,\n\n" |
230 | . $this->_mail_body($isok) |
5e2307dc |
231 | . (Env::has('comm') ? "\n\n".Env::v('comm') : '') |
0337d704 |
232 | . "\n\nCordialement,\nL'équipe Polytechnique.org\n"; |
233 | |
234 | $mailer->setTxtBody(wordwrap($body)); |
235 | $mailer->send(); |
236 | } |
237 | |
238 | // }}} |
239 | // {{{ function trig() |
240 | |
241 | function trig($msg) { |
242 | global $page; |
243 | $page->trig($msg); |
244 | } |
245 | |
246 | // }}} |
247 | // {{{ function get_request() |
248 | |
249 | /** fonction statique qui renvoie la requête de l'utilisateur d'id $uidau timestamp $t |
250 | * @param $uid l'id de l'utilisateur concerné |
251 | * @param $type le type de la requête |
252 | * @param $stamp le timestamp de la requête |
253 | * |
254 | * XXX fonction "statique" XXX |
255 | * à utiliser uniquement pour récupérer un objet dans la BD avec Validate::get_request(...) |
256 | */ |
257 | function get_request($uid, $type, $stamp = -1) |
258 | { |
0337d704 |
259 | if ($stamp == -1) { |
08cce2ff |
260 | $res = XDB::query('SELECT data FROM requests WHERE user_id={?} and type={?}', $uid, $type); |
0337d704 |
261 | } else { |
08cce2ff |
262 | $res = XDB::query("SELECT data, stamp FROM requests WHERE user_id={?} AND type={?} and stamp={?}", $uid, $type, $stamp); |
0337d704 |
263 | } |
264 | if ($result = $res->fetchOneCell()) { |
265 | $result = unserialize($result); |
266 | } else { |
267 | $result = false; |
268 | } |
269 | return($result); |
270 | } |
271 | |
272 | // }}} |
273 | // {{{ function _mail_body |
274 | |
275 | function _mail_body($isok) |
276 | { |
277 | } |
278 | |
279 | // }}} |
280 | // {{{ function _mail_subj |
281 | |
282 | function _mail_subj() |
283 | { |
284 | } |
285 | |
286 | // }}} |
287 | // {{{ function commit() |
288 | |
289 | /** fonction à utiliser pour insérer les données dans x4dat |
290 | * XXX la fonction est "virtuelle" XXX |
291 | */ |
292 | function commit () |
293 | { } |
294 | |
295 | // }}} |
296 | // {{{ function formu() |
297 | |
298 | /** nom du template qui contient le formulaire */ |
299 | function formu() |
300 | { return null; } |
301 | |
302 | // }}} |
e18888f4 |
303 | // {{{ function answers() |
304 | |
305 | /** automatic answers table for this type of validation */ |
306 | function answers() { |
307 | static $answers_table; |
308 | if (!isset($answers_table[$this->type])) { |
309 | $r = XDB::query("SELECT id, title, answer FROM requests_answers WHERE category = {?}", $this->type); |
310 | $answers_table[$this->type] = $r->fetchAllAssoc($r); |
311 | } |
312 | return $answers_table[$this->type]; |
313 | } |
314 | |
315 | // }}} |
0337d704 |
316 | } |
317 | |
0337d704 |
318 | foreach (glob(dirname(__FILE__).'/validations/*.inc.php') as $file) { |
319 | require_once($file); |
320 | } |
321 | |
0337d704 |
322 | /* vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 foldmethod=marker: */ |
323 | ?> |