]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/register.php
store invite code in session so openidfinish can find it
[quix0rs-gnu-social.git] / actions / register.php
1 <?php
2 /**
3  * Laconica, the distributed open-source microblogging tool
4  *
5  * Register a new user account
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Login
23  * @package   Laconica
24  * @author    Evan Prodromou <evan@controlyourself.ca>
25  * @copyright 2008-2009 Control Yourself, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://laconi.ca/
28  */
29
30 if (!defined('LACONICA')) {
31     exit(1);
32 }
33
34 /**
35  * An action for registering a new user account
36  *
37  * @category Login
38  * @package  Laconica
39  * @author   Evan Prodromou <evan@controlyourself.ca>
40  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41  * @link     http://laconi.ca/
42  */
43
44 class RegisterAction extends Action
45 {
46     /**
47      * Has there been an error?
48      */
49
50     var $error = null;
51
52     /**
53      * Have we registered?
54      */
55
56     var $registered = false;
57
58     /**
59      * Prepare page to run
60      *
61      *
62      * @param $args
63      * @return string title
64      */
65
66     function prepare()
67     {
68         $this->code = $this->trimmed('code');
69
70         if (empty($this->code)) {
71             common_ensure_session();
72             if (!empty($_SESSION['invitecode'])) {
73                 $this->code = $_SESSION['invitecode'];
74             }
75         }
76
77         if (common_config('site', 'inviteonly') && empty($this->code)) {
78             $this->clientError(_('Sorry, only invited people can register.'));
79             return false;
80         }
81
82         if (!empty($this->code)) {
83             $this->invite = Invitation::staticGet($code);
84             if (empty($this->invite)) {
85                 $this->clientError(_('Sorry, invalid invitation code.'));
86                 return false;
87             }
88             // Store this in case we need it
89             common_ensure_session();
90             $_SESSION['invitecode'] = $this->code;
91         }
92
93         return true;
94     }
95
96     /**
97      * Title of the page
98      *
99      * @return string title
100      */
101
102     function title()
103     {
104         if ($this->registered) {
105             return _('Registration successful');
106         } else {
107             return _('Register');
108         }
109     }
110
111     /**
112      * Handle input, produce output
113      *
114      * Switches on request method; either shows the form or handles its input.
115      *
116      * Checks if registration is closed and shows an error if so.
117      *
118      * @param array $args $_REQUEST data
119      *
120      * @return void
121      */
122
123     function handle($args)
124     {
125         parent::handle($args);
126
127         if (common_config('site', 'closed')) {
128             $this->clientError(_('Registration not allowed.'));
129         } else if (common_logged_in()) {
130             $this->clientError(_('Already logged in.'));
131         } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
132             $this->tryRegister();
133         } else {
134             $this->showForm();
135         }
136     }
137
138     /**
139      * Try to register a user
140      *
141      * Validates the input and tries to save a new user and profile
142      * record. On success, shows an instructions page.
143      *
144      * @return void
145      */
146
147     function tryRegister()
148     {
149         if (Event::handle('StartRegistrationTry', array($this))) {
150             $token = $this->trimmed('token');
151             if (!$token || $token != common_session_token()) {
152                 $this->showForm(_('There was a problem with your session token. '.
153                                   'Try again, please.'));
154                 return;
155             }
156
157             $nickname = $this->trimmed('nickname');
158             $email    = $this->trimmed('email');
159             $fullname = $this->trimmed('fullname');
160             $homepage = $this->trimmed('homepage');
161             $bio      = $this->trimmed('bio');
162             $location = $this->trimmed('location');
163
164             // We don't trim these... whitespace is OK in a password!
165             $password = $this->arg('password');
166             $confirm  = $this->arg('confirm');
167
168             // invitation code, if any
169             $code = $this->trimmed('code');
170
171             if ($code) {
172                 $invite = Invitation::staticGet($code);
173             }
174
175             if (common_config('site', 'inviteonly') && !($code && $invite)) {
176                 $this->clientError(_('Sorry, only invited people can register.'));
177                 return;
178             }
179
180             // Input scrubbing
181             $nickname = common_canonical_nickname($nickname);
182             $email    = common_canonical_email($email);
183
184             if (!$this->boolean('license')) {
185                 $this->showForm(_('You can\'t register if you don\'t '.
186                                   'agree to the license.'));
187             } else if ($email && !Validate::email($email, true)) {
188                 $this->showForm(_('Not a valid email address.'));
189             } else if (!Validate::string($nickname, array('min_length' => 1,
190                                                           'max_length' => 64,
191                                                           'format' => NICKNAME_FMT))) {
192                 $this->showForm(_('Nickname must have only lowercase letters '.
193                                   'and numbers and no spaces.'));
194             } else if ($this->nicknameExists($nickname)) {
195                 $this->showForm(_('Nickname already in use. Try another one.'));
196             } else if (!User::allowed_nickname($nickname)) {
197                 $this->showForm(_('Not a valid nickname.'));
198             } else if ($this->emailExists($email)) {
199                 $this->showForm(_('Email address already exists.'));
200             } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
201                        !Validate::uri($homepage,
202                                       array('allowed_schemes' =>
203                                             array('http', 'https')))) {
204                 $this->showForm(_('Homepage is not a valid URL.'));
205                 return;
206             } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
207                 $this->showForm(_('Full name is too long (max 255 chars).'));
208                 return;
209             } else if (!is_null($bio) && mb_strlen($bio) > 140) {
210                 $this->showForm(_('Bio is too long (max 140 chars).'));
211                 return;
212             } else if (!is_null($location) && mb_strlen($location) > 255) {
213                 $this->showForm(_('Location is too long (max 255 chars).'));
214                 return;
215             } else if (strlen($password) < 6) {
216                 $this->showForm(_('Password must be 6 or more characters.'));
217                 return;
218             } else if ($password != $confirm) {
219                 $this->showForm(_('Passwords don\'t match.'));
220             } else if ($user = User::register(array('nickname' => $nickname,
221                                                     'password' => $password,
222                                                     'email' => $email,
223                                                     'fullname' => $fullname,
224                                                     'homepage' => $homepage,
225                                                     'bio' => $bio,
226                                                     'location' => $location,
227                                                     'code' => $code))) {
228                 if (!$user) {
229                     $this->showForm(_('Invalid username or password.'));
230                     return;
231                 }
232                 // success!
233                 if (!common_set_user($user)) {
234                     $this->serverError(_('Error setting user.'));
235                     return;
236                 }
237                 // this is a real login
238                 common_real_login(true);
239                 if ($this->boolean('rememberme')) {
240                     common_debug('Adding rememberme cookie for ' . $nickname);
241                     common_rememberme($user);
242                 }
243
244                 Event::handle('EndRegistrationTry', array($this));
245
246                 // Re-init language env in case it changed (not yet, but soon)
247                 common_init_language();
248                 $this->showSuccess();
249             } else {
250                 $this->showForm(_('Invalid username or password.'));
251             }
252         }
253     }
254
255     /**
256      * Does the given nickname already exist?
257      *
258      * Checks a canonical nickname against the database.
259      *
260      * @param string $nickname nickname to check
261      *
262      * @return boolean true if the nickname already exists
263      */
264
265     function nicknameExists($nickname)
266     {
267         $user = User::staticGet('nickname', $nickname);
268         return ($user !== false);
269     }
270
271     /**
272      * Does the given email address already exist?
273      *
274      * Checks a canonical email address against the database.
275      *
276      * @param string $email email address to check
277      *
278      * @return boolean true if the address already exists
279      */
280
281     function emailExists($email)
282     {
283         $email = common_canonical_email($email);
284         if (!$email || strlen($email) == 0) {
285             return false;
286         }
287         $user = User::staticGet('email', $email);
288         return ($user !== false);
289     }
290
291     // overrrided to add entry-title class
292     function showPageTitle() {
293         if (Event::handle('StartShowPageTitle', array($this))) {
294             $this->element('h1', array('class' => 'entry-title'), $this->title());
295         }
296     }
297
298     // overrided to add hentry, and content-inner class
299     function showContentBlock()
300     {
301         $this->elementStart('div', array('id' => 'content', 'class' => 'hentry'));
302         $this->showPageTitle();
303         $this->showPageNoticeBlock();
304         $this->elementStart('div', array('id' => 'content_inner',
305                                          'class' => 'entry-content'));
306         // show the actual content (forms, lists, whatever)
307         $this->showContent();
308         $this->elementEnd('div');
309         $this->elementEnd('div');
310     }
311
312     /**
313      * Instructions or a notice for the page
314      *
315      * Shows the error, if any, or instructions for registration.
316      *
317      * @return void
318      */
319
320     function showPageNotice()
321     {
322         if ($this->registered) {
323             return;
324         } else if ($this->error) {
325             $this->element('p', 'error', $this->error);
326         } else {
327             $instr =
328               common_markup_to_html(_('With this form you can create '.
329                                       ' a new account. ' .
330                                       'You can then post notices and '.
331                                       'link up to friends and colleagues. '.
332                                       '(Have an [OpenID](http://openid.net/)? ' .
333                                       'Try our [OpenID registration]'.
334                                       '(%%action.openidlogin%%)!)'));
335
336             $this->elementStart('div', 'instructions');
337             $this->raw($instr);
338             $this->elementEnd('div');
339         }
340     }
341
342     /**
343      * Wrapper for showing a page
344      *
345      * Stores an error and shows the page
346      *
347      * @param string $error Error, if any
348      *
349      * @return void
350      */
351
352     function showForm($error=null)
353     {
354         $this->error = $error;
355         $this->showPage();
356     }
357
358     /**
359      * Show the page content
360      *
361      * Either shows the registration form or, if registration was successful,
362      * instructions for using the site.
363      *
364      * @return void
365      */
366
367     function showContent()
368     {
369         if ($this->registered) {
370             $this->showSuccessContent();
371         } else {
372             $this->showFormContent();
373         }
374     }
375
376     /**
377      * Show the registration form
378      *
379      * @return void
380      */
381
382     function showFormContent()
383     {
384         $this->elementStart('form', array('method' => 'post',
385                                           'id' => 'form_register',
386                                           'class' => 'form_settings',
387                                           'action' => common_local_url('register')));
388         $this->elementStart('fieldset');
389         $this->element('legend', null, 'Account settings');
390         $this->hidden('token', common_session_token());
391
392         if ($this->code) {
393             $this->hidden('code', $this->code);
394         }
395
396         $this->elementStart('ul', 'form_data');
397         if (Event::handle('StartRegistrationFormData', array($this))) {
398             $this->elementStart('li');
399             $this->input('nickname', _('Nickname'), $this->trimmed('nickname'),
400                          _('1-64 lowercase letters or numbers, '.
401                            'no punctuation or spaces. Required.'));
402             $this->elementEnd('li');
403             $this->elementStart('li');
404             $this->password('password', _('Password'),
405                             _('6 or more characters. Required.'));
406             $this->elementEnd('li');
407             $this->elementStart('li');
408             $this->password('confirm', _('Confirm'),
409                             _('Same as password above. Required.'));
410             $this->elementEnd('li');
411             $this->elementStart('li');
412             if ($this->invite && $this->invite->address_type == 'email') {
413                 $this->input('email', _('Email'), $this->invite->address,
414                              _('Used only for updates, announcements, '.
415                                'and password recovery'));
416             } else {
417                 $this->input('email', _('Email'), $this->trimmed('email'),
418                              _('Used only for updates, announcements, '.
419                                'and password recovery'));
420             }
421             $this->elementEnd('li');
422             $this->elementStart('li');
423             $this->input('fullname', _('Full name'),
424                          $this->trimmed('fullname'),
425                          _('Longer name, preferably your "real" name'));
426             $this->elementEnd('li');
427             $this->elementStart('li');
428             $this->input('homepage', _('Homepage'),
429                          $this->trimmed('homepage'),
430                          _('URL of your homepage, blog, '.
431                            'or profile on another site'));
432             $this->elementEnd('li');
433             $this->elementStart('li');
434             $this->textarea('bio', _('Bio'),
435                             $this->trimmed('bio'),
436                             _('Describe yourself and your '.
437                               'interests in 140 chars'));
438             $this->elementEnd('li');
439             $this->elementStart('li');
440             $this->input('location', _('Location'),
441                          $this->trimmed('location'),
442                          _('Where you are, like "City, '.
443                            'State (or Region), Country"'));
444             $this->elementEnd('li');
445             Event::handle('EndRegistrationFormData', array($this));
446             $this->elementStart('li', array('id' => 'settings_rememberme'));
447             $this->checkbox('rememberme', _('Remember me'),
448                             $this->boolean('rememberme'),
449                             _('Automatically login in the future; '.
450                               'not for shared computers!'));
451             $this->elementEnd('li');
452             $attrs = array('type' => 'checkbox',
453                            'id' => 'license',
454                            'class' => 'checkbox',
455                            'name' => 'license',
456                            'value' => 'true');
457             if ($this->boolean('license')) {
458                 $attrs['checked'] = 'checked';
459             }
460             $this->elementStart('li');
461             $this->element('input', $attrs);
462             $this->elementStart('label', array('class' => 'checkbox', 'for' => 'license'));
463             $this->text(_('My text and files are available under '));
464             $this->element('a', array('href' => common_config('license', 'url')),
465                            common_config('license', 'title'), _("Creative Commons Attribution 3.0"));
466             $this->text(_(' except this private data: password, '.
467                           'email address, IM address, and phone number.'));
468             $this->elementEnd('label');
469             $this->elementEnd('li');
470         }
471         $this->elementEnd('ul');
472         $this->submit('submit', _('Register'));
473         $this->elementEnd('fieldset');
474         $this->elementEnd('form');
475     }
476
477     /**
478      * Show some information about registering for the site
479      *
480      * Save the registration flag, run showPage
481      *
482      * @return void
483      */
484
485     function showSuccess()
486     {
487         $this->registered = true;
488         $this->showPage();
489     }
490
491     /**
492      * Show some information about registering for the site
493      *
494      * Gives some information and options for new registrees.
495      *
496      * @return void
497      */
498
499     function showSuccessContent()
500     {
501         $nickname = $this->arg('nickname');
502
503         $profileurl = common_local_url('showstream',
504                                        array('nickname' => $nickname));
505
506         $this->elementStart('div', 'success');
507         $instr = sprintf(_('Congratulations, %s! And welcome to %%%%site.name%%%%. '.
508                            'From here, you may want to...'. "\n\n" .
509                            '* Go to [your profile](%s) '.
510                            'and post your first message.' .  "\n" .
511                            '* Add a [Jabber/GTalk address]'.
512                            '(%%%%action.imsettings%%%%) '.
513                            'so you can send notices '.
514                            'through instant messages.' . "\n" .
515                            '* [Search for people](%%%%action.peoplesearch%%%%) '.
516                            'that you may know or '.
517                            'that share your interests. ' . "\n" .
518                            '* Update your [profile settings]'.
519                            '(%%%%action.profilesettings%%%%)'.
520                            ' to tell others more about you. ' . "\n" .
521                            '* Read over the [online docs](%%%%doc.help%%%%)'.
522                            ' for features you may have missed. ' . "\n\n" .
523                            'Thanks for signing up and we hope '.
524                            'you enjoy using this service.'),
525                          $nickname, $profileurl);
526
527         $this->raw(common_markup_to_html($instr));
528
529         $have_email = $this->trimmed('email');
530         if ($have_email) {
531             $emailinstr = _('(You should receive a message by email '.
532                             'momentarily, with ' .
533                             'instructions on how to confirm '.
534                             'your email address.)');
535             $this->raw(common_markup_to_html($emailinstr));
536         }
537         $this->elementEnd('div');
538     }
539
540     /**
541      * Show the login group nav menu
542      *
543      * @return void
544      */
545
546     function showLocalNav()
547     {
548         $nav = new LoginGroupNav($this);
549         $nav->show();
550     }
551 }
552