]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/EmailRegistration/emailregister.php
Make EmailRegistration respect registration flags
[quix0rs-gnu-social.git] / plugins / EmailRegistration / 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             throw new ClientException(_('Registration not allowed.'), 403);
84         }
85
86         if ($this->isPost()) {
87
88             $this->checkSessionToken();
89
90             $this->email = $this->trimmed('email');
91
92             if (!empty($this->email)) {
93                 if (common_config('site', 'inviteonly')) {
94                     throw new ClientException(_('Sorry, only invited people can register.'), 403);
95                 }
96                 $this->email = common_canonical_email($this->email);
97                 $this->state = self::NEWEMAIL;
98             } else {
99                 $this->state = self::SETPASSWORD;
100
101                 $this->code = $this->trimmed('code');
102
103                 if (empty($this->code)) {
104                     // TRANS: Client exception thrown when no confirmation code was provided.
105                     throw new ClientException(_m('No confirmation code.'));
106                 }
107
108                 $this->invitation = Invitation::staticGet('code', $this->code);
109
110                 if (empty($this->invitation)) {
111
112                     $this->confirmation = Confirm_address::staticGet('code', $this->code);
113
114                     if (empty($this->confirmation)) {
115                         // TRANS: Client exception thrown when given confirmation code was not issued.
116                         throw new ClientException(_m('No such confirmation code.'), 403);
117                     }
118                 }
119
120                 $this->password1 = $this->trimmed('password1');
121                 $this->password2 = $this->trimmed('password2');
122
123                 $this->tos = $this->boolean('tos');
124             }
125         } else { // GET
126             $this->code = $this->trimmed('code');
127
128             if (empty($this->code)) {
129                 if (common_config('site', 'inviteonly')) {
130                     throw new ClientException(_('Sorry, only invited people can register.'), 403);
131                 }
132                 $this->state = self::NEWREGISTER;
133             } else {
134                 $this->invitation = Invitation::staticGet('code', $this->code);
135                 if (!empty($this->invitation)) {
136                     $this->state = self::CONFIRMINVITE;
137                 } else {
138                     $this->state = self::CONFIRMREGISTER;
139                     $this->confirmation = Confirm_address::staticGet('code', $this->code);
140
141                     if (empty($this->confirmation)) {
142                         // TRANS: Client exception thrown when given confirmation code was not issued.
143                         throw new ClientException(_m('No such confirmation code.'), 405);
144                     }
145                 }
146             }
147         }
148
149         return true;
150     }
151
152     function title()
153     {
154         switch ($this->state) {
155         case self::NEWREGISTER:
156         case self::NEWEMAIL:
157             // TRANS: Title for registration page.
158             return _m('TITLE','Register');
159             break;
160         case self::SETPASSWORD:
161         case self::CONFIRMINVITE:
162         case self::CONFIRMREGISTER:
163             // TRANS: Title for page where to register with a confirmation code.
164             return _m('TITLE','Complete registration');
165             break;
166         }
167     }
168
169     /**
170      * Handler method
171      *
172      * @param array $argarray is ignored since it's now passed in in prepare()
173      *
174      * @return void
175      */
176
177     function handle($argarray=null)
178     {
179         $cur = common_current_user();
180
181         if (!empty($cur)) {
182             common_redirect(common_local_url('all', array('nickname' => $cur->nickname)));
183             return;
184         }
185
186         switch ($this->state) {
187         case self::NEWREGISTER:
188             $this->showRegistrationForm();
189             break;
190         case self::NEWEMAIL:
191             $this->registerUser();
192             break;
193         case self::CONFIRMINVITE:
194             $this->confirmRegistration();
195             break;
196         case self::CONFIRMREGISTER:
197             $this->confirmRegistration();
198             break;
199         case self::SETPASSWORD:
200             $this->setPassword();
201             break;
202         }
203         return;
204     }
205
206     function showRegistrationForm()
207     {
208         $this->form = new EmailRegistrationForm($this, $this->email);
209         $this->showPage();
210     }
211
212     function registerUser()
213     {
214         try {
215             $confirm = EmailRegistrationPlugin::registerEmail($this->email);
216         } catch (ClientException $ce) {
217             $this->error = $ce->getMessage();
218             $this->showRegistrationForm();
219             return;
220         }
221
222         EmailRegistrationPlugin::sendConfirmEmail($confirm);
223
224         // TRANS: Confirmation text after initial registration.
225         // TRANS: %s an e-mail address.
226         $prompt = sprintf(_m('An email was sent to %s to confirm that address. Check your email inbox for instructions.'),
227                           $this->email);
228
229         $this->complete = $prompt;
230
231         $this->showPage();
232     }
233
234     function confirmRegistration()
235     {
236         if (!empty($this->invitation)) {
237             $email = $this->invitation->address;
238         } else if (!empty($this->confirmation)) {
239             $email = $this->confirmation->address;
240         }
241
242         $nickname = $this->nicknameFromEmail($email);
243
244         $this->form = new ConfirmRegistrationForm($this,
245                                                   $nickname,
246                                                   $email,
247                                                   $this->code);
248         $this->showPage();
249     }
250
251     function setPassword()
252     {
253         if (Event::handle('StartRegistrationTry', array($this))) {
254             if (!empty($this->invitation)) {
255                 $email = trim($this->invitation->address);
256             } else if (!empty($this->confirmation)) {
257                 $email = trim($this->confirmation->address);
258             } else {
259                 throw new Exception('No confirmation thing.');
260             }
261
262             if (!$this->tos) {
263                 // TRANS: Error text when trying to register without agreeing to the terms.
264                 $this->error = _m('You must accept the terms of service and privacy policy to register.');
265                 return;
266             } else if (empty($this->password1)) {
267                 // TRANS: Error text when trying to register without a password.
268                 $this->error = _m('You must set a password');
269             } else if (strlen($this->password1) < 6) {
270                 // TRANS: Error text when trying to register with too short a password.
271                 $this->error = _m('Password must be 6 or more characters.');
272             } else if ($this->password1 != $this->password2) {
273                 // TRANS: Error text when trying to register without providing the same password twice.
274                 $this->error = _m('Passwords do not match.');
275             }
276
277             if (!empty($this->error)) {
278                 $nickname = $this->nicknameFromEmail($email);
279                 $this->form = new ConfirmRegistrationForm($this, $nickname, $this->email, $this->code);
280                 $this->showPage();
281                 return;
282             }
283
284             $nickname = $this->nicknameFromEmail($email);
285
286             try {
287                 $this->user = User::register(array('nickname' => $nickname,
288                                                    'email' => $email,
289                                                    'password' => $this->password1,
290                                                    'email_confirmed' => true));
291             } catch (ClientException $e) {
292                 $this->error = $e->getMessage();
293                 $nickname = $this->nicknameFromEmail($email);
294                 $this->form = new ConfirmRegistrationForm($this, $nickname, $this->email, $this->code);
295                 $this->showPage();
296                 return;
297             }
298
299             if (empty($this->user)) {
300                 throw new Exception('Failed to register user.');
301             }
302
303             common_set_user($this->user);
304             // this is a real login
305             common_real_login(true);
306
307             // Re-init language env in case it changed (not yet, but soon)
308             common_init_language();
309
310             if (!empty($this->invitation)) {
311                 $inviter = User::staticGet('id', $this->invitation->user_id);
312                 if (!empty($inviter)) {
313                     Subscription::start($inviter->getProfile(),
314                                         $this->user->getProfile());
315                 }
316
317                 $this->invitation->delete();
318             } else if (!empty($this->confirmation)) {
319                 $this->confirmation->delete();
320             } else {
321                 throw new Exception('No confirmation thing.');
322             }
323
324             Event::handle('EndRegistrationTry', array($this));
325         }
326
327         if (Event::handle('StartRegisterSuccess', array($this))) {
328             common_redirect(common_local_url('doc', array('title' => 'welcome')),
329                             303);
330             Event::handle('EndRegisterSuccess', array($this));
331         }
332     }
333
334     function sendConfirmEmail($confirm)
335     {
336         $sitename = common_config('site', 'name');
337
338         $recipients = array($confirm->address);
339
340         $headers['From'] = mail_notify_from();
341         $headers['To'] = trim($confirm->address);
342          // TRANS: Subject for confirmation e-mail.
343          // TRANS: %s is the StatusNet sitename.
344         $headers['Subject'] = sprintf(_m('Confirm your registration on %s'), $sitename);
345
346         $confirmUrl = common_local_url('register', array('code' => $confirm->code));
347
348          // TRANS: Body for confirmation e-mail.
349          // TRANS: %1$s is the StatusNet sitename, %2$s is the confirmation URL.
350         $body = sprintf(_m('Someone (probably you) has requested an account on %1$s using this email address.'.
351                           "\n".
352                           'To confirm the address, click the following URL or copy it into the address bar of your browser.'.
353                           "\n".
354                           '%2$s'.
355                           "\n".
356                           'If it was not you, you can safely ignore this message.'),
357                         $sitename,
358                         $confirmUrl);
359
360         mail_send($recipients, $headers, $body);
361     }
362
363     function showContent()
364     {
365         if ($this->complete) {
366             $this->elementStart('p', 'success');
367             $this->raw($this->complete);
368             $this->elementEnd('p');
369         } else {
370             if ($this->error) {
371                 $this->elementStart('p', 'error');
372                 $this->raw($this->error);
373                 $this->elementEnd('p');
374             }
375
376             if (!empty($this->form)) {
377                 $this->form->show();
378             }
379         }
380     }
381
382     /**
383      * Return true if read only.
384      *
385      * MAY override
386      *
387      * @param array $args other arguments
388      *
389      * @return boolean is read only action?
390      */
391     function isReadOnly($args)
392     {
393         return false;
394     }
395
396     function nicknameFromEmail($email)
397     {
398         return EmailRegistrationPlugin::nicknameFromEmail($email);
399     }
400
401     /**
402      * A local menu
403      *
404      * Shows different login/register actions.
405      *
406      * @return void
407      */
408     function showLocalNav()
409     {
410         $nav = new LoginGroupNav($this);
411         $nav->show();
412     }
413 }