| 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 | require_once dirname(__FILE__).'/lists.php'; |
| 23 | |
| 24 | class XnetListsModule extends ListsModule |
| 25 | { |
| 26 | var $client; |
| 27 | |
| 28 | function handlers() |
| 29 | { |
| 30 | return array( |
| 31 | '%grp/lists' => $this->make_hook('lists', AUTH_MDP), |
| 32 | '%grp/lists/create' => $this->make_hook('create', AUTH_MDP), |
| 33 | |
| 34 | '%grp/lists/members' => $this->make_hook('members', AUTH_COOKIE), |
| 35 | '%grp/lists/trombi' => $this->make_hook('trombi', AUTH_COOKIE), |
| 36 | '%grp/lists/archives' => $this->make_hook('archives', AUTH_COOKIE), |
| 37 | |
| 38 | '%grp/lists/moderate' => $this->make_hook('moderate', AUTH_MDP), |
| 39 | '%grp/lists/admin' => $this->make_hook('admin', AUTH_MDP), |
| 40 | '%grp/lists/options' => $this->make_hook('options', AUTH_MDP), |
| 41 | '%grp/lists/delete' => $this->make_hook('delete', AUTH_MDP), |
| 42 | |
| 43 | '%grp/lists/soptions' => $this->make_hook('soptions', AUTH_MDP), |
| 44 | '%grp/lists/check' => $this->make_hook('check', AUTH_MDP), |
| 45 | '%grp/lists/sync' => $this->make_hook('sync', AUTH_MDP), |
| 46 | |
| 47 | '%grp/alias/admin' => $this->make_hook('aadmin', AUTH_MDP), |
| 48 | '%grp/alias/create' => $this->make_hook('acreate', AUTH_MDP), |
| 49 | |
| 50 | /* hack: lists uses that */ |
| 51 | 'profile' => $this->make_hook('profile', AUTH_PUBLIC), |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | function prepare_client(&$page) |
| 56 | { |
| 57 | global $globals; |
| 58 | |
| 59 | require_once dirname(__FILE__).'/lists/lists.inc.php'; |
| 60 | |
| 61 | $this->client = new MMList(S::v('uid'), S::v('password'), |
| 62 | $globals->asso('mail_domain')); |
| 63 | |
| 64 | $page->assign('asso', $globals->asso()); |
| 65 | $page->setType($globals->asso('cat')); |
| 66 | |
| 67 | return $globals->asso('mail_domain'); |
| 68 | } |
| 69 | |
| 70 | function handler_lists(&$page) |
| 71 | { |
| 72 | global $globals; |
| 73 | |
| 74 | if (!$globals->asso('mail_domain')) { |
| 75 | return PL_NOT_FOUND; |
| 76 | } |
| 77 | $this->prepare_client($page); |
| 78 | |
| 79 | new_group_page('xnetlists/index.tpl'); |
| 80 | |
| 81 | if (Get::has('del')) { |
| 82 | $this->client->unsubscribe(Get::v('del')); |
| 83 | pl_redirect('lists'); |
| 84 | } |
| 85 | if (Get::has('add')) { |
| 86 | $this->client->subscribe(Get::v('add')); |
| 87 | pl_redirect('lists'); |
| 88 | } |
| 89 | |
| 90 | if (Post::has('del_alias') && may_update()) { |
| 91 | $alias = Post::v('del_alias'); |
| 92 | // prevent group admin from erasing aliases from other groups |
| 93 | $alias = substr($alias, 0, strpos($alias, '@')).'@'.$globals->asso('mail_domain'); |
| 94 | XDB::query( |
| 95 | 'DELETE FROM x4dat.virtual_redirect, x4dat.virtual |
| 96 | USING x4dat.virtual AS v |
| 97 | LEFT JOIN x4dat.virtual_redirect USING(vid) |
| 98 | WHERE v.alias={?}', $alias); |
| 99 | $page->trig(Post::v('del_alias')." supprimé !"); |
| 100 | } |
| 101 | |
| 102 | $listes = $this->client->get_lists(); |
| 103 | $page->assign('listes',$listes); |
| 104 | |
| 105 | $alias = XDB::iterator( |
| 106 | 'SELECT alias,type |
| 107 | FROM x4dat.virtual |
| 108 | WHERE alias |
| 109 | LIKE {?} AND type="user" |
| 110 | ORDER BY alias', '%@'.$globals->asso('mail_domain')); |
| 111 | $page->assign('alias', $alias); |
| 112 | |
| 113 | $page->assign('may_update', may_update()); |
| 114 | } |
| 115 | |
| 116 | function handler_create(&$page) |
| 117 | { |
| 118 | global $globals; |
| 119 | |
| 120 | if (!$globals->asso('mail_domain')) { |
| 121 | return PL_NOT_FOUND; |
| 122 | } |
| 123 | $this->prepare_client($page); |
| 124 | |
| 125 | $page->changeTpl('xnetlists/create.tpl'); |
| 126 | |
| 127 | if (!Post::has('submit')) { |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | if (!Post::has('liste')) { |
| 132 | $page->trig('champs «addresse souhaitée» vide'); |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | $liste = strtolower(Post::v('liste')); |
| 137 | |
| 138 | if (!preg_match("/^[a-zA-Z0-9\-]*$/", $liste)) { |
| 139 | $page->trig('le nom de la liste ne doit contenir que des lettres non accentuées, chiffres et tirets'); |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | $new = $liste.'@'.$globals->asso('mail_domain'); |
| 144 | $res = XDB::query('SELECT COUNT(*) FROM x4dat.virtual WHERE alias={?}', $new); |
| 145 | $n = $res->fetchOneCell(); |
| 146 | |
| 147 | if ($n) { |
| 148 | $page->trig('cet alias est déjà pris'); |
| 149 | return; |
| 150 | } |
| 151 | if (!Post::v('desc')) { |
| 152 | $page->trig('le sujet est vide'); |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | $ret = $this->client->create_list( |
| 157 | $liste, Post::v('desc'), Post::v('advertise'), |
| 158 | Post::v('modlevel'), Post::v('inslevel'), |
| 159 | array(S::v('forlife')), array()); |
| 160 | |
| 161 | $dom = strtolower($globals->asso("mail_domain")); |
| 162 | $red = $dom.'_'.$liste; |
| 163 | |
| 164 | if (!$ret) { |
| 165 | $page->kill("Un problème est survenu, contacter " |
| 166 | ."<a href='mailto:support@m4x.org'>support@m4x.org</a>"); |
| 167 | return; |
| 168 | } |
| 169 | XDB::execute('INSERT INTO x4dat.virtual (alias,type) |
| 170 | VALUES({?},{?})', $liste.'@'.$dom, 'list'); |
| 171 | XDB::execute('INSERT INTO x4dat.virtual_redirect (vid,redirect) |
| 172 | VALUES ({?}, {?})', XDB::insertId(), |
| 173 | "$red+post@listes.polytechnique.org"); |
| 174 | XDB::execute('INSERT INTO x4dat.virtual (alias,type) |
| 175 | VALUES({?},{?})', $liste.'-owner@'.$dom, 'list'); |
| 176 | XDB::execute('INSERT INTO x4dat.virtual_redirect (vid,redirect) |
| 177 | VALUES ({?}, {?})', XDB::insertId(), |
| 178 | "$red+owner@listes.polytechnique.org"); |
| 179 | XDB::execute('INSERT INTO x4dat.virtual (alias,type) |
| 180 | VALUES({?},{?})', $liste.'-admin@'.$dom, 'list'); |
| 181 | XDB::execute('INSERT INTO x4dat.virtual_redirect (vid,redirect) |
| 182 | VALUES ({?}, {?})', XDB::insertId(), |
| 183 | "$red+admin@listes.polytechnique.org"); |
| 184 | XDB::execute('INSERT INTO x4dat.virtual (alias,type) |
| 185 | VALUES({?},{?})', $liste.'-bounces@'.$dom, 'list'); |
| 186 | XDB::execute('INSERT INTO x4dat.virtual_redirect (vid,redirect) |
| 187 | VALUES ({?}, {?})', XDB::insertId(), |
| 188 | "$red+bounces@listes.polytechnique.org"); |
| 189 | |
| 190 | pl_redirect('lists/admin/'.$liste); |
| 191 | } |
| 192 | |
| 193 | function handler_sync(&$page, $liste = null) |
| 194 | { |
| 195 | global $globals; |
| 196 | |
| 197 | if (!$globals->asso('mail_domain')) { |
| 198 | return PL_NOT_FOUND; |
| 199 | } |
| 200 | $this->prepare_client($page); |
| 201 | |
| 202 | $page->changeTpl('xnetlists/sync.tpl'); |
| 203 | |
| 204 | if (Env::has('add')) { |
| 205 | $this->client->mass_subscribe($liste, array_keys(Env::v('add'))); |
| 206 | } |
| 207 | |
| 208 | list(,$members) = $this->client->get_members($liste); |
| 209 | $mails = array_map(create_function('$arr', 'return $arr[1];'), $members); |
| 210 | $subscribers = array_unique($mails); |
| 211 | |
| 212 | $not_in_group_x = array(); |
| 213 | $not_in_group_ext = array(); |
| 214 | |
| 215 | $ann = XDB::iterator( |
| 216 | "SELECT if (m.origine='X',if (u.nom_usage<>'', u.nom_usage, u.nom) ,m.nom) AS nom, |
| 217 | if (m.origine='X',u.prenom,m.prenom) AS prenom, |
| 218 | if (m.origine='X',u.promo,'extérieur') AS promo, |
| 219 | if (m.origine='X',CONCAT(a.alias, '@polytechnique.org'),m.email) AS email, |
| 220 | if (m.origine='X',FIND_IN_SET('femme', u.flags),0) AS femme, |
| 221 | m.perms='admin' AS admin, |
| 222 | m.origine='X' AS x |
| 223 | FROM groupex.membres AS m |
| 224 | LEFT JOIN auth_user_md5 AS u ON ( u.user_id = m.uid ) |
| 225 | LEFT JOIN aliases AS a ON ( a.id = m.uid AND a.type='a_vie' ) |
| 226 | WHERE m.asso_id = {?} |
| 227 | ORDER BY promo, nom, prenom", $globals->asso('id')); |
| 228 | |
| 229 | $not_in_list = array(); |
| 230 | |
| 231 | while ($tmp = $ann->next()) { |
| 232 | if (!in_array(strtolower($tmp['email']), $subscribers)) { |
| 233 | $not_in_list[] = $tmp; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | $page->assign('not_in_list', $not_in_list); |
| 238 | } |
| 239 | |
| 240 | function handler_aadmin(&$page, $lfull = null) |
| 241 | { |
| 242 | global $globals; |
| 243 | |
| 244 | if (!$globals->asso('mail_domain') || is_null($lfull)) { |
| 245 | return PL_NOT_FOUND; |
| 246 | } |
| 247 | |
| 248 | new_groupadmin_page('xnetlists/alias-admin.tpl'); |
| 249 | |
| 250 | if (Env::has('add_member')) { |
| 251 | $add = Env::v('add_member'); |
| 252 | if (strstr($add, '@')) { |
| 253 | list($mbox,$dom) = explode('@', strtolower($add)); |
| 254 | } else { |
| 255 | $mbox = $add; |
| 256 | $dom = 'm4x.org'; |
| 257 | } |
| 258 | if ($dom == 'polytechnique.org' || $dom == 'm4x.org') { |
| 259 | $res = XDB::query( |
| 260 | "SELECT a.alias, b.alias |
| 261 | FROM x4dat.aliases AS a |
| 262 | LEFT JOIN x4dat.aliases AS b ON (a.id=b.id AND b.type = 'a_vie') |
| 263 | WHERE a.alias={?} AND a.type!='homonyme'", $mbox); |
| 264 | if (list($alias, $blias) = $res->fetchOneRow()) { |
| 265 | $alias = empty($blias) ? $alias : $blias; |
| 266 | XDB::query( |
| 267 | "INSERT INTO x4dat.virtual_redirect (vid,redirect) |
| 268 | SELECT vid, {?} |
| 269 | FROM x4dat.virtual |
| 270 | WHERE alias={?}", "$alias@m4x.org", $lfull); |
| 271 | $page->trig("$alias@m4x.org ajouté"); |
| 272 | } else { |
| 273 | $page->trig("$mbox@polytechnique.org n'existe pas."); |
| 274 | } |
| 275 | } else { |
| 276 | XDB::query( |
| 277 | "INSERT INTO x4dat.virtual_redirect (vid,redirect) |
| 278 | SELECT vid,{?} |
| 279 | FROM x4dat.virtual |
| 280 | WHERE alias={?}", "$mbox@$dom", $lfull); |
| 281 | $page->trig("$mbox@$dom ajouté"); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | if (Env::has('del_member')) { |
| 286 | XDB::query( |
| 287 | "DELETE FROM x4dat.virtual_redirect |
| 288 | USING x4dat.virtual_redirect |
| 289 | INNER JOIN x4dat.virtual USING(vid) |
| 290 | WHERE redirect={?} AND alias={?}", Env::v('del_member'), $lfull); |
| 291 | pl_redirect('alias/admin/'.$lfull); |
| 292 | } |
| 293 | |
| 294 | global $globals; |
| 295 | $res = XDB::iterator( |
| 296 | "SELECT redirect, |
| 297 | IF(u.nom IS NOT NULL, IF(u.nom_usage<>'', u.nom_usage, u.nom), m.nom) AS nom, |
| 298 | IF(u.prenom IS NOT NULL, u.prenom, m.prenom) AS prenom, |
| 299 | IF(u.promo IS NOT NULL, u.promo, 'extérieur') AS promo, |
| 300 | IF(m2.perms, m2.perms = 'admin', m.perms = 'admin') AS admin, |
| 301 | a.alias |
| 302 | FROM x4dat.virtual_redirect AS vr |
| 303 | INNER JOIN x4dat.virtual AS v USING(vid) |
| 304 | LEFT JOIN x4dat.aliases AS a ON(vr.redirect = CONCAT(a.alias, CONCAT('@', {?})) |
| 305 | OR vr.redirect = CONCAT(a.alias, CONCAT('@', {?}))) |
| 306 | LEFT JOIN x4dat.auth_user_md5 AS u ON(a.id = u.user_id) |
| 307 | LEFT JOIN groupex.membres AS m2 ON(u.user_id = m2.uid AND m2.asso_id = {?}) |
| 308 | LEFT JOIN groupex.membres AS m ON(m.email = vr.redirect AND m.asso_id = {?}) |
| 309 | WHERE v.alias={?} |
| 310 | ORDER BY redirect", |
| 311 | $globals->mail->domain, $globals->mail->domain2, |
| 312 | $globals->asso('id'), $globals->asso('id'), $lfull); |
| 313 | $page->assign('mem', $res); |
| 314 | } |
| 315 | |
| 316 | function handler_acreate(&$page) |
| 317 | { |
| 318 | global $globals; |
| 319 | |
| 320 | if (!$globals->asso('mail_domain')) { |
| 321 | return PL_NOT_FOUND; |
| 322 | } |
| 323 | new_groupadmin_page('xnetlists/alias-create.tpl'); |
| 324 | |
| 325 | if (!Post::has('submit')) { |
| 326 | return; |
| 327 | } |
| 328 | |
| 329 | if (!Post::has('liste')) { |
| 330 | $page->trig('champs «addresse souhaitée» vide'); |
| 331 | return; |
| 332 | } |
| 333 | $liste = Post::v('liste'); |
| 334 | if (!preg_match("/^[a-zA-Z0-9\-\.]*$/", $liste)) { |
| 335 | $page->trig('le nom de l\'alias ne doit contenir que des lettres,' |
| 336 | .' chiffres, tirets et points'); |
| 337 | return; |
| 338 | } |
| 339 | |
| 340 | $new = $liste.'@'.$globals->asso('mail_domain'); |
| 341 | $res = XDB::query('SELECT COUNT(*) FROM x4dat.virtual WHERE alias={?}', $new); |
| 342 | $n = $res->fetchOneCell(); |
| 343 | if ($n) { |
| 344 | $page->trig('cet alias est déjà pris'); |
| 345 | return; |
| 346 | } |
| 347 | |
| 348 | XDB::query('INSERT INTO x4dat.virtual (alias,type) VALUES({?}, "user")', $new); |
| 349 | |
| 350 | pl_redirect("alias/admin/$new"); |
| 351 | } |
| 352 | |
| 353 | function handler_profile(&$page, $user = null) |
| 354 | { |
| 355 | http_redirect('https://www.polytechnique.org/profile/'.$user); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 360 | ?> |