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