| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2006 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 | class EventsModule extends PLModule |
| 23 | { |
| 24 | function handlers() |
| 25 | { |
| 26 | return array( |
| 27 | 'events' => $this->make_hook('ev', AUTH_COOKIE), |
| 28 | 'events/submit' => $this->make_hook('ev_submit', AUTH_MDP), |
| 29 | 'admin/events' => $this->make_hook('admin_events', AUTH_MDP, 'admin'), |
| 30 | |
| 31 | 'nl' => $this->make_hook('nl', AUTH_COOKIE), |
| 32 | 'nl/show' => $this->make_hook('nl_show', AUTH_COOKIE), |
| 33 | 'nl/submit' => $this->make_hook('nl_submit', AUTH_COOKIE), |
| 34 | 'admin/newsletter' => $this->make_hook('admin_nl', AUTH_MDP, 'admin'), |
| 35 | 'admin/newsletter/categories' => $this->make_hook('admin_nl_cat', AUTH_MDP, 'admin'), |
| 36 | 'admin/newsletter/edit' => $this->make_hook('admin_nl_edit', AUTH_MDP, 'admin'), |
| 37 | ); |
| 38 | } |
| 39 | |
| 40 | function on_subscribe($forlife, $uid, $promo, $password) |
| 41 | { |
| 42 | require_once 'newsletter.inc.php'; |
| 43 | subscribe_nl($uid); |
| 44 | } |
| 45 | |
| 46 | function handler_ev(&$page, $action = 'list', $eid = null) |
| 47 | { |
| 48 | $page->changeTpl('login.tpl'); |
| 49 | |
| 50 | $res = XDB::query('SELECT date, naissance FROM auth_user_md5 |
| 51 | WHERE user_id={?}', S::v('uid')); |
| 52 | list($date, $naissance) = $res->fetchOneRow(); |
| 53 | |
| 54 | // incitation à mettre à jour la fiche |
| 55 | |
| 56 | $d2 = mktime(0, 0, 0, substr($date, 5, 2), substr($date, 8, 2), |
| 57 | substr($date, 0, 4)); |
| 58 | if( (time() - $d2) > 60 * 60 * 24 * 400 ) { |
| 59 | // si fiche date de + de 400j; |
| 60 | $page->assign('fiche_incitation', $date); |
| 61 | } |
| 62 | |
| 63 | // Souhaite bon anniversaire |
| 64 | |
| 65 | if (substr($naissance, 5) == date('m-d')) { |
| 66 | $page->assign('birthday', date('Y') - substr($naissance, 0, 4)); |
| 67 | } |
| 68 | |
| 69 | // incitation à mettre une photo |
| 70 | |
| 71 | $res = XDB::query('SELECT COUNT(*) FROM photo |
| 72 | WHERE uid={?}', S::v('uid')); |
| 73 | $page->assign('photo_incitation', $res->fetchOneCell() == 0); |
| 74 | |
| 75 | // Incitation à se géolocaliser |
| 76 | require_once 'geoloc.inc.php'; |
| 77 | $res = localize_addresses(S::v('uid', -1)); |
| 78 | $page->assign('geoloc_incitation', count($res)); |
| 79 | |
| 80 | // affichage de la boîte avec quelques liens |
| 81 | /* Bandeau de publicité sur la page de login */ |
| 82 | $publicite = array( |
| 83 | 'password' => 'Changer mon mot de passe' , |
| 84 | 'Docs/Dons' => 'Faire un don à l\'association Polytechnique.org' |
| 85 | ) ; |
| 86 | |
| 87 | // Liens apparaissant de façon aléatoire |
| 88 | $pub_rnd = array( |
| 89 | 'nl/show' => 'Afficher la dernière newsletter' , |
| 90 | 'http://www.polytechnique.net' => 'Vers les autres sites polytechniciens' , |
| 91 | "trombi/{$_SESSION["promo"]}" => "Voir le trombi de ma promo" , |
| 92 | 'banana' => 'Un petit tour du côté des forums !!' |
| 93 | ) ; |
| 94 | |
| 95 | $choix = array_rand($pub_rnd, 2); |
| 96 | foreach ($choix as $url) { |
| 97 | $publicite[$url] = $pub_rnd[$url] ; |
| 98 | } |
| 99 | $page->assign('publicite', array_chunk($publicite, 2, true)); |
| 100 | |
| 101 | // ajout du lien RSS |
| 102 | if (S::has('core_rss_hash')) { |
| 103 | $page->assign('xorg_rss', |
| 104 | array('title' => 'Polytechnique.org :: News', |
| 105 | 'href' => '/rss/'.S::v('forlife') |
| 106 | .'/'.S::v('core_rss_hash').'/rss.xml') |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | // cache les evenements lus et raffiche les evenements a relire |
| 111 | if ($action == 'read' && $eid) { |
| 112 | XDB::execute('DELETE evenements_vus.* |
| 113 | FROM evenements_vus AS ev |
| 114 | INNER JOIN evenements AS e ON e.id = ev.evt_id |
| 115 | WHERE peremption < NOW()'); |
| 116 | XDB::execute('REPLACE INTO evenements_vus VALUES({?},{?})', |
| 117 | $eid, S::v('uid')); |
| 118 | } |
| 119 | |
| 120 | if ($action == 'unread' && $eid) { |
| 121 | XDB::execute('DELETE FROM evenements_vus |
| 122 | WHERE evt_id = {?} AND user_id = {?}', |
| 123 | $eid, S::v('uid')); |
| 124 | } |
| 125 | |
| 126 | // affichage des evenements |
| 127 | // annonces promos triées par présence d'une limite sur les promos |
| 128 | // puis par dates croissantes d'expiration |
| 129 | $promo = S::v('promo'); |
| 130 | $sql = "SELECT e.id,e.titre,e.texte,a.user_id,a.nom,a.prenom,a.promo,l.alias AS forlife |
| 131 | FROM evenements AS e |
| 132 | INNER JOIN auth_user_md5 AS a ON e.user_id=a.user_id |
| 133 | INNER JOIN aliases AS l ON ( a.user_id=l.id AND l.type='a_vie' ) |
| 134 | LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?}) |
| 135 | WHERE FIND_IN_SET(e.flags, 'valide') AND peremption >= NOW() |
| 136 | AND (e.promo_min = 0 || e.promo_min <= {?}) |
| 137 | AND (e.promo_max = 0 || e.promo_max >= {?}) |
| 138 | AND ev.user_id IS NULL |
| 139 | ORDER BY (e.promo_min != 0 AND e.promo_max != 0) DESC, e.peremption"; |
| 140 | $page->assign('evenement', |
| 141 | XDB::iterator($sql, S::v('uid'), |
| 142 | $promo, $promo) |
| 143 | ); |
| 144 | |
| 145 | $sql = "SELECT e.id,e.titre, ev.user_id IS NULL AS nonlu |
| 146 | FROM evenements AS e |
| 147 | LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?}) |
| 148 | WHERE FIND_IN_SET(e.flags, 'valide') AND peremption >= NOW() |
| 149 | AND (e.promo_min = 0 || e.promo_min <= {?}) |
| 150 | AND (e.promo_max = 0 || e.promo_max >= {?}) |
| 151 | ORDER BY (e.promo_min != 0 AND e.promo_max != 0) DESC, e.peremption"; |
| 152 | $page->assign('evenement_summary', |
| 153 | XDB::iterator($sql, S::v('uid'), |
| 154 | $promo, $promo) |
| 155 | ); |
| 156 | } |
| 157 | |
| 158 | function handler_ev_submit(&$page) |
| 159 | { |
| 160 | $page->changeTpl('evenements.tpl'); |
| 161 | |
| 162 | $titre = Post::v('titre'); |
| 163 | $texte = Post::v('texte'); |
| 164 | $promo_min = Post::i('promo_min'); |
| 165 | $promo_max = Post::i('promo_max'); |
| 166 | $peremption = Post::i('peremption'); |
| 167 | $valid_mesg = Post::v('valid_mesg'); |
| 168 | $action = Post::v('action'); |
| 169 | |
| 170 | $page->assign('titre', $titre); |
| 171 | $page->assign('texte', $texte); |
| 172 | $page->assign('promo_min', $promo_min); |
| 173 | $page->assign('promo_max', $promo_max); |
| 174 | $page->assign('peremption', $peremption); |
| 175 | $page->assign('valid_mesg', $valid_mesg); |
| 176 | $page->assign('action', strtolower($action)); |
| 177 | |
| 178 | if ($action == 'Confirmer') { |
| 179 | $texte = preg_replace('/((http|ftp)+(s)?:\/\/[^<>\s]+)/i', |
| 180 | '<a href=\"\\0\">\\0</a>', $texte); |
| 181 | $texte = preg_replace('/([^,\s]+@[^,\s]+)/i', |
| 182 | '<a href=\"mailto:\\0\">\\0</a>', $texte); |
| 183 | require_once 'validations.inc.php'; |
| 184 | $evtreq = new EvtReq($titre, $texte, $promo_min, $promo_max, |
| 185 | $peremption, $valid_mesg, S::v('uid')); |
| 186 | $evtreq->submit(); |
| 187 | $page->assign('ok', true); |
| 188 | } |
| 189 | |
| 190 | $select = ''; |
| 191 | for ($i = 1 ; $i < 30 ; $i++) { |
| 192 | $time = time() + 3600 * 24 * $i; |
| 193 | $p_stamp = date('Ymd', $time); |
| 194 | $year = date('Y', $time); |
| 195 | $month = date('m', $time); |
| 196 | $day = date('d', $time); |
| 197 | |
| 198 | $select .= "<option value=\"$p_stamp\""; |
| 199 | if ($p_stamp == strtr($peremption, array("-" => ""))) { |
| 200 | $select .= " selected='selected'"; |
| 201 | } |
| 202 | $select .= "> $day / $month / $year</option>\n"; |
| 203 | } |
| 204 | $page->assign('select',$select); |
| 205 | } |
| 206 | |
| 207 | function handler_nl(&$page, $action = null) |
| 208 | { |
| 209 | require_once 'newsletter.inc.php'; |
| 210 | |
| 211 | $page->changeTpl('newsletter/index.tpl'); |
| 212 | $page->assign('xorg_title','Polytechnique.org - Lettres mensuelles'); |
| 213 | |
| 214 | switch ($action) { |
| 215 | case 'out': unsubscribe_nl(); break; |
| 216 | case 'in': subscribe_nl(); break; |
| 217 | default: ; |
| 218 | } |
| 219 | |
| 220 | $page->assign('nls', get_nl_state()); |
| 221 | $page->assign_by_ref('nl_list', get_nl_list()); |
| 222 | } |
| 223 | |
| 224 | function handler_nl_show(&$page, $nid = 'last') |
| 225 | { |
| 226 | $page->changeTpl('newsletter/show.tpl'); |
| 227 | |
| 228 | require_once 'newsletter.inc.php'; |
| 229 | |
| 230 | $nl = new NewsLetter($nid); |
| 231 | $page->assign_by_ref('nl', $nl); |
| 232 | |
| 233 | if (Post::has('send')) { |
| 234 | $nl->sendTo(S::v('prenom'), S::v('nom'), |
| 235 | S::v('bestalias'), S::v('femme'), |
| 236 | S::v('mail_fmt') != 'text'); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | function handler_nl_submit(&$page) |
| 241 | { |
| 242 | $page->changeTpl('newsletter/submit.tpl'); |
| 243 | |
| 244 | require_once 'newsletter.inc.php'; |
| 245 | |
| 246 | if (Post::has('see')) { |
| 247 | $art = new NLArticle(Post::v('title'), Post::v('body'), Post::v('append')); |
| 248 | $page->assign('art', $art); |
| 249 | } elseif (Post::has('valid')) { |
| 250 | require_once('validations.inc.php'); |
| 251 | $art = new NLReq(S::v('uid'), Post::v('title'), |
| 252 | Post::v('body'), Post::v('append')); |
| 253 | $art->submit(); |
| 254 | $page->assign('submited', true); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | function handler_admin_events(&$page, $action = 'list', $eid = null) |
| 259 | { |
| 260 | $page->changeTpl('admin/evenements.tpl'); |
| 261 | $page->assign('xorg_title','Polytechnique.org - Administration - Evenements'); |
| 262 | $page->register_modifier('hde', 'html_entity_decode'); |
| 263 | |
| 264 | $arch = $action == 'archives'; |
| 265 | $page->assign('action', $action); |
| 266 | |
| 267 | if (Post::v('action') == "Proposer" && $eid) { |
| 268 | XDB::execute('UPDATE evenements |
| 269 | SET titre={?}, texte={?}, peremption={?}, promo_min={?}, promo_max={?} |
| 270 | WHERE id = {?}', |
| 271 | Post::v('titre'), Post::v('texte'), Post::v('peremption'), |
| 272 | Post::v('promo_min'), Post::v('promo_max'), $eid); |
| 273 | } |
| 274 | |
| 275 | if ($action == 'edit') { |
| 276 | $res = XDB::query('SELECT titre, texte, peremption, promo_min, promo_max |
| 277 | FROM evenements |
| 278 | WHERE id={?}', $eid); |
| 279 | list($titre, $texte, $peremption, $promo_min, $promo_max) = $res->fetchOneRow(); |
| 280 | $page->assign('titre',$titre); |
| 281 | $page->assign('texte',$texte); |
| 282 | $page->assign('promo_min',$promo_min); |
| 283 | $page->assign('promo_max',$promo_max); |
| 284 | $page->assign('peremption',$peremption); |
| 285 | |
| 286 | $select = ""; |
| 287 | for ($i = 1 ; $i < 30 ; $i++) { |
| 288 | $p_stamp=date("Ymd",time()+3600*24*$i); |
| 289 | $year=substr($p_stamp,0,4); |
| 290 | $month=substr($p_stamp,4,2); |
| 291 | $day=substr($p_stamp,6,2); |
| 292 | |
| 293 | $select .= "<option value=\"$p_stamp\"" |
| 294 | . (($p_stamp == strtr($peremption, array("-" => ""))) ? " selected" : "") |
| 295 | . "> $day / $month / $year</option>\n"; |
| 296 | } |
| 297 | $page->assign('select',$select); |
| 298 | } else { |
| 299 | switch ($action) { |
| 300 | case 'delete': |
| 301 | XDB::execute('DELETE from evenements |
| 302 | WHERE id = {?}', $eid); |
| 303 | break; |
| 304 | |
| 305 | case "archive": |
| 306 | XDB::execute('UPDATE evenements |
| 307 | SET creation_date = creation_date, flags = CONCAT(flags,",archive") |
| 308 | WHERE id = {?}', $eid); |
| 309 | break; |
| 310 | |
| 311 | case "unarchive": |
| 312 | XDB::execute('UPDATE evenements |
| 313 | SET creation_date = creation_date, flags = REPLACE(flags,"archive","") |
| 314 | WHERE id = {?}', $eid); |
| 315 | $action = 'archives'; |
| 316 | $arch = true; |
| 317 | break; |
| 318 | |
| 319 | case "valid": |
| 320 | XDB::execute('UPDATE evenements |
| 321 | SET creation_date = creation_date, flags = CONCAT(flags,",valide") |
| 322 | WHERE id = {?}', $eid); |
| 323 | break; |
| 324 | |
| 325 | case "unvalid": |
| 326 | XDB::execute('UPDATE evenements |
| 327 | SET creation_date = creation_date, flags = REPLACE(flags,"valide", "") |
| 328 | WHERE id = {?}', $eid); |
| 329 | break; |
| 330 | } |
| 331 | |
| 332 | $pid = ($eid && $action == 'preview') ? $eid : -1; |
| 333 | $sql = "SELECT e.id, e.titre, e.texte,e.id = $pid AS preview, |
| 334 | DATE_FORMAT(e.creation_date,'%d/%m/%Y %T') AS creation_date, |
| 335 | DATE_FORMAT(e.peremption,'%d/%m/%Y') AS peremption, |
| 336 | e.promo_min, e.promo_max, |
| 337 | FIND_IN_SET('valide', e.flags) AS fvalide, |
| 338 | FIND_IN_SET('archive', e.flags) AS farch, |
| 339 | u.promo, u.nom, u.prenom, a.alias AS forlife |
| 340 | FROM evenements AS e |
| 341 | INNER JOIN auth_user_md5 AS u ON(e.user_id = u.user_id) |
| 342 | INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie') |
| 343 | WHERE ".($arch ? "" : "!")."FIND_IN_SET('archive',e.flags) |
| 344 | ORDER BY FIND_IN_SET('valide',e.flags), e.peremption DESC"; |
| 345 | $page->assign('evs', XDB::iterator($sql)); |
| 346 | } |
| 347 | $page->assign('arch', $arch); |
| 348 | } |
| 349 | |
| 350 | function handler_admin_nl(&$page, $new = false) { |
| 351 | $page->changeTpl('newsletter/admin.tpl'); |
| 352 | $page->assign('xorg_title','Polytechnique.org - Administration - Newsletter : liste'); |
| 353 | require_once("newsletter.inc.php"); |
| 354 | |
| 355 | if($new) { |
| 356 | insert_new_nl(); |
| 357 | pl_redirect("admin/newsletter"); |
| 358 | } |
| 359 | |
| 360 | $page->assign_by_ref('nl_list', get_nl_slist()); |
| 361 | } |
| 362 | |
| 363 | function handler_admin_nl_edit(&$page, $nid = 'last', $aid = null, $action = 'edit') { |
| 364 | $page->changeTpl('newsletter/edit.tpl'); |
| 365 | $page->assign('xorg_title','Polytechnique.org - Administration - Newsletter : Edition'); |
| 366 | require_once("newsletter.inc.php"); |
| 367 | |
| 368 | $nl = new NewsLetter($nid); |
| 369 | |
| 370 | if($action == 'delete') { |
| 371 | $nl->delArticle($aid); |
| 372 | pl_redirect("admin/newsletter/edit/$nid"); |
| 373 | } |
| 374 | |
| 375 | if($aid == 'update') { |
| 376 | $nl->_title = Post::v('title'); |
| 377 | $nl->_date = Post::v('date'); |
| 378 | $nl->_head = Post::v('head'); |
| 379 | $nl->save(); |
| 380 | } |
| 381 | |
| 382 | if(Post::v('save')) { |
| 383 | $art = new NLArticle(Post::v('title'), Post::v('body'), Post::v('append'), |
| 384 | $aid, Post::v('cid'), Post::v('pos')); |
| 385 | $nl->saveArticle($art); |
| 386 | pl_redirect("admin/newsletter/edit/$nid"); |
| 387 | } |
| 388 | |
| 389 | if($action == 'edit') { |
| 390 | $eaid = $aid; |
| 391 | if(Post::has('title')) { |
| 392 | $art = new NLArticle(Post::v('title'), Post::v('body'), Post::v('append'), |
| 393 | $eaid, Post::v('cid'), Post::v('pos')); |
| 394 | } else { |
| 395 | $art = ($eaid == 'new') ? new NLArticle() : $nl->getArt($eaid); |
| 396 | } |
| 397 | $page->assign('art', $art); |
| 398 | } |
| 399 | |
| 400 | $page->assign_by_ref('nl',$nl); |
| 401 | } |
| 402 | function handler_admin_nl_cat(&$page, $action = 'list', $id = null) { |
| 403 | require_once('../classes/PLTableEditor.php'); |
| 404 | $page->assign('xorg_title','Polytechnique.org - Administration - Newsletter : Catégories'); |
| 405 | $page->assign('title', 'Gestion des catégories de la newsletter'); |
| 406 | $table_editor = new PLTableEditor('admin/newsletter/categories','newsletter_cat','cid'); |
| 407 | $table_editor->describe('titre','intitulé',true); |
| 408 | $table_editor->describe('pos','position',true); |
| 409 | $table_editor->apply($page, $action, $id); |
| 410 | } |
| 411 | |
| 412 | } |
| 413 | |
| 414 | ?> |