make javascript smaller, and nicer
[platal.git] / htdocs / alias.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2004 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("xorg.inc.php");
23 require_once("validations.inc.php");
24
25 new_skinned_page('alias.tpl', AUTH_MDP);
26
27 $uid = Session::getInt('uid');
28 $forlife = Session::get('forlife');
29
30 $page->assign('demande', AliasReq::get_request($uid));
31
32 //Suppression d'un alias
33 if (Env::get('suppr', false)) {
34 $globals->xdb->execute(
35 'DELETE virtual, virtual_redirect
36 FROM virtual
37 INNER JOIN virtual_redirect USING (vid)
38 WHERE alias LIKE {?} AND (redirect = {?} OR redirect = {?})',
39 Env::get('suppr'),
40 $forlife.'@'.$globals->mail->domain, $forlife.'@'.$globals->mail->domain2);
41 }
42
43 //Récupération des alias éventuellement existants
44 $res = $globals->xdb->query(
45 "SELECT alias, emails_alias_pub
46 FROM auth_user_quick, virtual
47 INNER JOIN virtual_redirect USING(vid)
48 WHERE ( redirect={?} OR redirect= {?} )
49 AND alias LIKE '%@{$globals->mail->alias_dom}' AND user_id = {?}",
50 $forlife.'@'.$globals->mail->domain, $forlife.'@'.$globals->mail->domain2, Session::getInt('uid'));
51 list($alias, $visibility) = $res->fetchOneRow();
52 $page->assign('actuel', $alias);
53
54 //Si l'utilisateur vient de faire une damande
55 if (Env::has('alias') and Env::has('raison')) {
56 $alias = Env::get('alias');
57 $raison = Env::get('raison');
58 $public = (Env::get('public', 'off') == 'on')?"public":"private";
59
60 $page->assign('r_alias', $alias);
61 $page->assign('r_raison', $raison);
62 if ($public == 'public') {
63 $page->assign('r_public', true);
64 }
65
66 //Quelques vérifications sur l'alias (caractères spéciaux)
67 if (!preg_match( "/^[a-zA-Z0-9\-.]{3,20}$/", $alias)) {
68 $page->trig("L'adresse demandée n'est pas valide.
69 Vérifie qu'elle comporte entre 3 et 20 caractères
70 et qu'elle ne contient que des lettres non accentuées,
71 des chiffres ou les caractères - et .");
72 $page->run('error');
73 } else {
74 //vérifier que l'alias n'est pas déja pris
75 $res = $globals->xdb->query('SELECT COUNT(*) FROM virtual WHERE alias={?}', $alias.'@'.$globals->mail->alias_dom);
76 if ($res->fetchOneCell() > 0) {
77 $page->trig("L'alias $alias@{$globals->mail->alias_dom} a déja été attribué.
78 Tu ne peux donc pas l'obtenir.");
79 $page->run('error');
80 }
81
82 //vérifier que l'alias n'est pas déja en demande
83 $it = new ValidateIterator ();
84 while($req = $it->next()) {
85 if ($req->type == "alias" and $req->alias == $alias) {
86 $page->trig("L'alias $alias@{$globals->mail->alias_dom} a déja été demandé.
87 Tu ne peux donc pas l'obtenir pour l'instant.");
88 $page->run('error');
89 }
90 }
91
92 //Insertion de la demande dans la base, écrase les requêtes précédente
93 $myalias = new AliasReq($uid, $alias, $raison, $public);
94 $myalias->submit();
95 $page->assign('success',$alias);
96 $page->run('succes');
97 }
98 }
99
100 // montrer son alias
101 elseif ((Env::get('visible') == 'public') && ($visibility != 'public')) {
102 $globals->xdb->execute("UPDATE auth_user_quick SET emails_alias_pub = 'public' WHERE user_id = {?}", Session::getInt('uid'));
103 $visibility = 'public';
104 }
105
106 // cacher son alias
107 elseif ((Env::get('visible') == 'private') && ($visibility != 'private')) {
108 $globals->xdb->execute("UPDATE auth_user_quick SET emails_alias_pub = 'private' WHERE user_id = {?}", Session::getInt('uid'));
109 $visibility = 'private';
110 }
111
112 if ($visibility == 'public') {
113 $page->assign('mail_public', true);
114 }
115
116 $page->run();
117 ?>