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