]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/EmailRegistration/actions/emailregister.php
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / plugins / EmailRegistration / actions / emailregister.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * Register a user by their email address
7  *
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  Email registration
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2011 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 /**
38  * Email registration
39  *
40  * There are four cases where we're called:
41  *
42  * 1. GET, no arguments. Initial registration; ask for an email address.
43  * 2. POST, email address argument. Initial registration; send an email to confirm.
44  * 3. GET, code argument. Confirming an invitation or a registration; look them up,
45  *    create the relevant user if possible, login as that user, and
46  *    show a password-entry form.
47  * 4. POST, password argument. After confirmation, set the password for the new
48  *    user, and redirect to a registration complete action with some instructions.
49  *
50  * @category  Action
51  * @package   StatusNet
52  * @author    Evan Prodromou <evan@status.net>
53  * @copyright 2011 StatusNet, Inc.
54  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
55  * @link      http://status.net/
56  */
57 class EmailregisterAction extends Action
58 {
59     const NEWEMAIL = 1;
60     const SETPASSWORD = 2;
61     const NEWREGISTER = 3;
62     const CONFIRMINVITE = 4;
63     const CONFIRMREGISTER = 5;
64
65     const CONFIRMTYPE = 'register';
66
67     protected $user;
68     protected $email;
69     protected $code;
70     protected $invitation;
71     protected $confirmation;
72     protected $password1;
73     protected $password2;
74     protected $state;
75     protected $error;
76     protected $complete;
77
78     function prepare($argarray)
79     {
80         parent::prepare($argarray);
81
82         if (common_config('site', 'closed')) {
83             // TRANS: Client exception trown when registration by e-mail is not allowed.
84             throw new ClientException(_m('Registration not allowed.'), 403);
85         }
86
87         if ($this->isPost()) {
88
89             $this->checkSessionToken();
90
91             $this->email = $this->trimmed('email');
92
93             if (!empty($this->email)) {
94                 if (common_config('site', 'inviteonly')) {
95                     // TRANS: Client exception trown when trying to register without an invitation.
96                     throw new ClientException(_m('Sorry, only invited people can register.'), 403);
97                 }
98                 $this->email = common_canonical_email($this->email);
99                 $this->state = self::NEWEMAIL;
100             } else {
101                 $this->state = self::SETPASSWORD;
102
103                 $this->code = $this->trimmed('code');
104
105                 if (empty($this->code)) {
106                     // TRANS: Client exception thrown when no confirmation code was provided.
107                     throw new ClientException(_m('No confirmation code.'));
108                 }
109
110                 $this->invitation = Invitation::getKV('code', $this->code);
111
112                 if (!empty($this->invitation)) {
113                     if (!empty($this->invitation->registered_user_id)) {
114                         // TRANS: Client exception trown when using an invitation multiple times.
115                         throw new ClientException(_m('Invitation already used.'), 403);
116                     }
117                 } else {
118
119                     $this->confirmation = Confirm_address::getKV('code', $this->code);
120
121                     if (empty($this->confirmation)) {
122                         // TRANS: Client exception thrown when given confirmation code was not issued.
123                         throw new ClientException(_m('No such confirmation code.'), 403);
124                     }
125                 }
126
127                 $this->nickname = Nickname::normalize($this->trimmed('nickname'));
128                 $this->password1 = $this->trimmed('password1');
129                 $this->password2 = $this->trimmed('password2');
130
131                 $this->tos = $this->boolean('tos');
132             }
133         } else { // GET
134             $this->code = $this->trimmed('code');
135
136             if (empty($this->code)) {
137                 if (common_config('site', 'inviteonly')) {
138                     // TRANS: Client exception trown when trying to register without an invitation.
139                     throw new ClientException(_m('Sorry, only invited people can register.'), 403);
140                 }
141                 $this->state = self::NEWREGISTER;
142             } else {
143                 $this->invitation = Invitation::getKV('code', $this->code);
144                 if (!empty($this->invitation)) {
145                     if (!empty($this->invitation->registered_user_id)) {
146                         // TRANS: Client exception trown when using an invitation multiple times.
147                         throw new ClientException(_m('Invitation already used.'), 403);
148                     }
149                     $this->state = self::CONFIRMINVITE;
150                 } else {
151                     $this->state = self::CONFIRMREGISTER;
152                     $this->confirmation = Confirm_address::getKV('code', $this->code);
153
154                     if (empty($this->confirmation)) {
155                         // TRANS: Client exception thrown when given confirmation code was not issued.
156                         throw new ClientException(_m('No such confirmation code.'), 405);
157                     }
158                 }
159             }
160         }
161
162         return true;
163     }
164
165     function title()
166     {
167         switch ($this->state) {
168         case self::NEWREGISTER:
169         case self::NEWEMAIL:
170             // TRANS: Title for registration page.
171             return _m('TITLE','Register');
172             break;
173         case self::SETPASSWORD:
174         case self::CONFIRMINVITE:
175         case self::CONFIRMREGISTER:
176             // TRANS: Title for page where to register with a confirmation code.
177             return _m('TITLE','Complete registration');
178             break;
179         }
180     }
181
182     /**
183      * Handler method
184      *
185      * @param array $argarray is ignored since it's now passed in in prepare()
186      *
187      * @return void
188      */
189     function handle($argarray=null)
190     {
191         $cur = common_current_user();
192
193         if (!empty($cur)) {
194             common_redirect(common_local_url('all', array('nickname' => $cur->nickname)));
195         }
196
197         switch ($this->state) {
198         case self::NEWREGISTER:
199             $this->showRegistrationForm();
200             break;
201         case self::NEWEMAIL:
202             $this->registerUser();
203             break;
204         case self::CONFIRMINVITE:
205             $this->confirmRegistration();
206             break;
207         case self::CONFIRMREGISTER:
208             $this->confirmRegistration();
209             break;
210         case self::SETPASSWORD:
211             $this->setPassword();
212             break;
213         }
214         return;
215     }
216
217     function showRegistrationForm()
218     {
219         $this->form = new EmailRegistrationForm($this, $this->email);
220         $this->showPage();
221     }
222
223     function registerUser()
224     {
225         try {
226             $confirm = EmailRegistrationPlugin::registerEmail($this->email);
227         } catch (ClientException $ce) {
228             $this->error = $ce->getMessage();
229             $this->showRegistrationForm();
230             return;
231         }
232
233         EmailRegistrationPlugin::sendConfirmEmail($confirm);
234
235         // TRANS: Confirmation text after initial registration.
236         // TRANS: %s an e-mail address.
237         $prompt = sprintf(_m('An email was sent to %s to confirm that address. Check your email inbox for instructions.'),
238                           $this->email);
239
240         $this->complete = $prompt;
241
242         $this->showPage();
243     }
244
245     function confirmRegistration()
246     {
247         if (!empty($this->invitation)) {
248             $email = $this->invitation->address;
249         } else if (!empty($this->confirmation)) {
250             $email = $this->confirmation->address;
251         }
252
253         $nickname = $this->nicknameFromEmail($email);
254
255         $this->form = new ConfirmRegistrationForm($this,
256                                                   $nickname,
257                                                   $email,
258                                                   $this->code);
259         $this->showPage();
260     }
261
262     function setPassword()
263     {
264         if (Event::handle('StartRegistrationTry', array($this))) {
265             if (!empty($this->invitation)) {
266                 $email = trim($this->invitation->address);
267             } else if (!empty($this->confirmation)) {
268                 $email = trim($this->confirmation->address);
269             } else {
270                 // TRANS: Client exception trown when trying to set password with an invalid confirmation code.
271                 throw new Exception(_m('No confirmation thing.'));
272             }
273
274             if (!$this->tos) {
275                 // TRANS: Error text when trying to register without agreeing to the terms.
276                 $this->error = _m('You must accept the terms of service and privacy policy to register.');
277             } else if (empty($this->password1)) {
278                 // TRANS: Error text when trying to register without a password.
279                 $this->error = _m('You must set a password');
280             } else if (strlen($this->password1) < 6) {
281                 // TRANS: Error text when trying to register with too short a password.
282                 $this->error = _m('Password must be 6 or more characters.');
283             } else if ($this->password1 != $this->password2) {
284                 // TRANS: Error text when trying to register without providing the same password twice.
285                 $this->error = _m('Passwords do not match.');
286             }
287
288             if (!empty($this->error)) {
289                 $this->form = new ConfirmRegistrationForm($this, $this->nickname, $email, $this->code);
290                 $this->showPage();
291                 return;
292             }
293
294             try {
295                 $fields = array('nickname' => $this->nickname,
296                                 'email' => $email,
297                                 'password' => $this->password1,
298                                 'email_confirmed' => true);
299
300                 if (!empty($this->invitation)) {
301                     $fields['code'] = $this->invitation->code;
302                 }
303                 $this->user = User::register($fields);
304             } catch (ClientException $e) {
305                 $this->error = $e->getMessage();
306                 $this->form = new ConfirmRegistrationForm($this, $this->nickname, $email, $this->code);
307                 $this->showPage();
308                 return;
309             }
310
311             if (empty($this->user)) {
312                 // TRANS: Exception trown when using an invitation multiple times.
313                 throw new Exception(_m('Failed to register user.'));
314             }
315
316             common_set_user($this->user);
317             // this is a real login
318             common_real_login(true);
319
320             // Re-init language env in case it changed (not yet, but soon)
321             common_init_language();
322
323             if (!empty($this->confirmation)) {
324                 $this->confirmation->delete();
325             }
326
327             Event::handle('EndRegistrationTry', array($this));
328         }
329
330         if (Event::handle('StartRegisterSuccess', array($this))) {
331             Event::handle('EndRegisterSuccess', array($this));
332             common_redirect(common_local_url('doc', array('title' => 'welcome')), 303);
333             // common_redirect exits, so we can't run the event _after_ it of course.
334         }
335     }
336
337     function sendConfirmEmail($confirm)
338     {
339         $sitename = common_config('site', 'name');
340
341         $recipients = array($confirm->address);
342
343         $headers['From'] = mail_notify_from();
344         $headers['To'] = trim($confirm->address);
345          // TRANS: Subject for confirmation e-mail.
346          // TRANS: %s is the StatusNet sitename.
347         $headers['Subject'] = sprintf(_m('Confirm your registration on %s'), $sitename);
348
349         $confirmUrl = common_local_url('register', array('code' => $confirm->code));
350
351         // TRANS: Body for confirmation e-mail.
352         // TRANS: %1$s is the StatusNet sitename, %2$s is the confirmation URL.
353         $body = sprintf(_m('Someone (probably you) has requested an account on %1$s using this email address.'.
354                           "\n".
355                           'To confirm the address, click the following URL or copy it into the address bar of your browser.'.
356                           "\n".
357                           '%2$s'.
358                           "\n".
359                           'If it was not you, you can safely ignore this message.'),
360                         $sitename,
361                         $confirmUrl);
362
363         mail_send($recipients, $headers, $body);
364     }
365
366     function showContent()
367     {
368         if ($this->complete) {
369             $this->elementStart('p', 'success');
370             $this->raw($this->complete);
371             $this->elementEnd('p');
372         } else {
373             if ($this->error) {
374                 $this->elementStart('p', 'error');
375                 $this->raw($this->error);
376                 $this->elementEnd('p');
377             }
378
379             if (!empty($this->form)) {
380                 $this->form->show();
381             }
382         }
383     }
384
385     /**
386      * Return true if read only.
387      *
388      * MAY override
389      *
390      * @param array $args other arguments
391      *
392      * @return boolean is read only action?
393      */
394     function isReadOnly($args)
395     {
396         return false;
397     }
398
399     function nicknameFromEmail($email)
400     {
401         return EmailRegistrationPlugin::nicknameFromEmail($email);
402     }
403
404     /**
405      * A local menu
406      *
407      * Shows different login/register actions.
408      *
409      * @return void
410      */
411     function showLocalNav()
412     {
413         $nav = new LoginGroupNav($this);
414         $nav->show();
415     }
416 }