17f9789a2de27a5376bd78083c5f29a5b1203038
[platal.git] / modules / register / register.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2011 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 // {{{ function checkId
23
24 function checkId($subState)
25 {
26 $subState->set('xorgid', Profile::getXorgId($subState->i('schoolid')));
27 if (!$subState->v('xorgid')) {
28 return 'Le matricule est incorrect.';
29 }
30
31 $uf = new UserFilter(new PFC_And(
32 new UFC_SchoolId('xorg', $subState->i('xorgid')),
33 new PFC_Not(new UFC_Dead()),
34 new PFC_Not(new UFC_Registered(true))
35 ));
36 $profile = $uf->getProfile();
37 if (is_null($profile)) {
38 return "Tu es déjà inscrit ou ton matricule est incorrect !";
39 }
40
41 if ($profile->promo() != $subState->s('promo')) {
42 return 'Le matricule est incorrect.';
43 }
44
45 if (!$profile->compareNames($subState->s('firstname'), $subState->s('lastname'))) {
46 return "Erreur dans l'identification. Réessaie, il y a une erreur quelque part !";
47 }
48
49 $subState->set('lastname', $profile->lastName());
50 $subState->set('firstname', $profile->firstName());
51 $subState->set('uid', $profile->owner()->id());
52 $subState->set('watch', $profile->owner()->watch);
53 $subState->set('birthdateRef', $profile->__get('birthdate_ref'));
54 return true;
55 }
56
57 // }}}
58 // {{{ function checkOldId
59
60 function checkOldId($subState)
61 {
62 $uf = new UserFilter(new PFC_And(
63 new PFC_Not(new UFC_Dead()),
64 new UFC_Promo('=', UserFilter::DISPLAY, $subState->s('promo')),
65 new PFC_Not(new UFC_Registered(true))
66 ));
67 if ($it = $uf->iterProfiles()) {
68 while ($profile = $it->next()) {
69 if ($profile->compareNames($subState->s('firstname'), $subState->s('lastname'))) {
70 $subState->set('lastname', $profile->lastName());
71 $subState->set('firstname', $profile->firstName());
72 $subState->set('uid', $profile->owner()->id());
73 $subState->set('watch', $profile->owner()->watch);
74 $subState->set('birthdateRef', $profile->__get('birthdate_ref'));
75 $subState->set('xorgid', $profile->__get('xorg_id'));
76 return true;
77 }
78 }
79 }
80
81 $uf = new UserFilter(new PFC_And(
82 new PFC_Not(new UFC_Dead()),
83 new UFC_Promo('=', UserFilter::DISPLAY, $subState->s('promo')),
84 new UFC_Registered(true)
85 ));
86 if ($it = $uf->iterProfiles()) {
87 while ($profile = $it->next()) {
88 if ($profile->compareNames($subState->s('firstname'), $subState->s('lastname'))) {
89 $subState->set('uid', $profile->owner()->id());
90 $subState->set('watch', $profile->owner()->watch);
91 $subState->set('birthdateRef', $profile->__get('birthdate_ref'));
92 $subState->set('xorgid', $profile->__get('xorg_id'));
93 return 'Tu es vraisemblablement déjà inscrit !';
94 }
95 }
96 }
97 return 'Erreur : vérifie que tu as bien orthographié ton nom !';
98 }
99
100 // }}}
101 // {{{ function checkNewUser
102
103 function checkNewUser($subState)
104 {
105 $firstname = preg_replace("/[ \t]+/", ' ', $subState->t('firstname'));
106 $firstname = preg_replace("/--+/", '-', $firstname);
107 $firstname = preg_replace("/''+/", '\'', $firstname);
108 $subState->set('firstname', PlUser::fixFirstnameCase($firstname));
109
110 $lastname = preg_replace("/[ \t]+/", ' ', $subState->t('lastname'));
111 $lastname = preg_replace("/--+/", '-', $lastname);
112 $lastname = preg_replace("/''+/", '\'', $lastname);
113 $subState->set('lastname', mb_strtoupper($lastname));
114
115 if ($subState->i('yearpromo') >= 1996 && $subState->v('edu_type') == 'X') {
116 $res = checkId($subState);
117 } else {
118 $res = checkOldId($subState);
119 }
120 if ($subState->v('edu_type') != 'X' &&
121 $subState->v('xorgid') != $subState->v('schoolid')) {
122 return 'Le matricule est incorrect.';
123 }
124 if ($res !== true) {
125 return $res;
126 }
127
128 return true;
129 }
130
131 // }}}
132 // {{{ function createAliases
133
134 function createAliases($subState)
135 {
136 global $globals;
137
138 $emailXorg = PlUser::makeUserName($subState->t('firstname'), $subState->t('lastname'));
139 $emailXorg2 = $emailXorg . sprintf(".%02u", ($subState->i('yearpromo') % 100));
140
141 $res = XDB::query("SELECT hruid, state, type
142 FROM accounts
143 WHERE uid = {?} AND hruid != ''",
144 $subState->i('uid'));
145 if ($res->numRows() == 0) {
146 return "Tu n'as pas d'adresse à vie pré-attribuée.<br />"
147 . "Envoie un mail à <a href=\"mailto:support@{$globals->mail->domain}\">"
148 . "support@{$globals->mail->domain}</a> en expliquant ta situation.";
149 } else {
150 list($forlife, $state, $type) = $res->fetchOneRow();
151 }
152 if ($state == 'active') {
153 return "Tu es déjà inscrit, si tu ne te souviens plus de ton mot de passe d'accès au site, "
154 . "tu peux suivre <a href=\"recovery\">la procédure de récupération de mot de passe</a>.";
155 } else if ($state == 'disabled') {
156 return "Ton compte a été désactivé par les administrateurs du site suite à des abus. "
157 . "Pour plus d'information ou pour demander la réactivation du compte, tu peux t'adresser à "
158 . "<a href=\"mailto:support@{$globals->mail->domain}\">support@{$globals->mail->domain}</a>.";
159 }
160
161 $res = XDB::query('SELECT uid, expire
162 FROM email_source_account
163 WHERE email = {?} AND type != \'alias_aux\'',
164 $emailXorg);
165 if ($res->numRows()) {
166 list($h_id, $expire) = $res->fetchOneRow();
167 if (empty($expire)) {
168 XDB::execute('UPDATE email_source_account
169 SET expire = ADDDATE(NOW(), INTERVAL 1 MONTH)
170 WHERE email = {?} AND type != \'alias_aux\'',
171 $emailXorg);
172 $hrmid = User::makeHomonymHrmid($emailXorg);
173 XDB::execute('INSERT IGNORE INTO homonyms_list (hrmid, uid)
174 VALUES ({?}, {?}), ({?}, {?})',
175 $hrmid, $h_id, $hrmid, $subState->i('uid'));
176 $als = XDB::fetchColumn('SELECT email
177 FROM email_source_account
178 WHERE uid = {?} AND type != \'alias_aux\' AND expire IS NULL',
179 $h_id);
180
181 $homonym = User::getSilentWithUID($h_id);
182 $mailer = new PlMailer('register/lostalias.mail.tpl');
183 $mailer->addTo($homonym);
184 $mailer->setSubject("Perte de ton alias $emailXorg dans un mois !");
185 $mailer->assign('emailXorg', $emailXorg);
186 $mailer->assign('als', join(', ', $als));
187 $mailer->SetTxtBody(wordwrap($msg,72));
188 $mailer->send();
189 }
190
191 $subState->set('forlife', $forlife);
192 $subState->set('bestalias', $emailXorg2);
193 $subState->set('emailXorg2', null);
194 } else {
195 $subState->set('forlife', $forlife);
196 $subState->set('bestalias', $emailXorg);
197 $subState->set('emailXorg2', $emailXorg2);
198 }
199 $subState->set('main_mail_domain', User::$sub_mail_domains[$type] . Platal::globals()->mail->domain);
200
201 return true;
202 }
203
204 // }}}
205 // {{{ function finishRegistration
206
207 function finishRegistration($subState)
208 {
209 global $globals;
210
211 $hash = rand_url_id(12);
212 XDB::execute('INSERT INTO register_pending (uid, forlife, bestalias, mailorg2, password,
213 email, date, relance, naissance, hash, services)
214 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, NOW(), 0, {?}, {?}, {?})
215 ON DUPLICATE KEY UPDATE password=VALUES(password), email=VALUES(email),
216 date=VALUES(date), naissance=VALUES(naissance),
217 hash=VALUES(hash), services=VALUES(services)',
218 $subState->i('uid'), $subState->s('forlife'), $subState->s('bestalias'),
219 $subState->s('emailXorg2'), $subState->s('password'), $subState->s('email'),
220 $subState->s('birthdate'), $hash, implode(',', $subState->v('services')));
221
222 $mymail = new PlMailer('register/end.mail.tpl');
223 $mymail->assign('emailXorg', $subState->s('bestalias'));
224 $mymail->assign('to', $subState->s('email'));
225 $mymail->assign('baseurl', $globals->baseurl);
226 $mymail->assign('hash', $hash);
227 $mymail->assign('subject', ucfirst($globals->mail->domain) . ' : ' . $subState->s('bestalias'));
228 $mymail->send();
229 }
230
231 // }}}
232 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
233 ?>