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