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