]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/register.php
Merge branch '1.0.x' 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 class RegisterAction extends Action
44 {
45     /**
46      * Has there been an error?
47      */
48     var $error = null;
49
50     /**
51      * Have we registered?
52      */
53     var $registered = false;
54
55     /**
56      * Are we processing an invite?
57      */
58     var $invite = null;
59
60     /**
61      * Prepare page to run
62      *
63      *
64      * @param $args
65      * @return string title
66      */
67     function prepare($args)
68     {
69         parent::prepare($args);
70         $this->code = $this->trimmed('code');
71
72         if (empty($this->code)) {
73             common_ensure_session();
74             if (array_key_exists('invitecode', $_SESSION)) {
75                 $this->code = $_SESSION['invitecode'];
76             }
77         }
78
79         if (common_config('site', 'inviteonly') && empty($this->code)) {
80             $this->clientError(_('Sorry, only invited people can register.'));
81             return false;
82         }
83
84         if (!empty($this->code)) {
85             $this->invite = Invitation::staticGet('code', $this->code);
86             if (empty($this->invite)) {
87                 $this->clientError(_('Sorry, invalid invitation code.'));
88                 return false;
89             }
90             // Store this in case we need it
91             common_ensure_session();
92             $_SESSION['invitecode'] = $this->code;
93         }
94
95         return true;
96     }
97
98     /**
99      * Title of the page
100      *
101      * @return string title
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      * @param array $args $_REQUEST data
120      *
121      * @return void
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     function showScripts()
139     {
140         parent::showScripts();
141         $this->autofocus('nickname');
142     }
143
144     /**
145      * Try to register a user
146      *
147      * Validates the input and tries to save a new user and profile
148      * record. On success, shows an instructions page.
149      *
150      * @return void
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             try {
187                 $nickname = Nickname::normalize($nickname);
188             } catch (NicknameException $e) {
189                 $this->showForm($e->getMessage());
190             }
191             $email    = common_canonical_email($email);
192
193             if (!$this->boolean('license')) {
194                 $this->showForm(_('You cannot register if you don\'t '.
195                                   'agree to the license.'));
196             } else if ($email && !Validate::email($email, common_config('email', 'check_domain'))) {
197                 $this->showForm(_('Not a valid email address.'));
198             } else if ($this->nicknameExists($nickname)) {
199                 $this->showForm(_('Nickname already in use. Try another one.'));
200             } else if (!User::allowed_nickname($nickname)) {
201                 $this->showForm(_('Not a valid nickname.'));
202             } else if ($this->emailExists($email)) {
203                 $this->showForm(_('Email address already exists.'));
204             } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
205                        !Validate::uri($homepage,
206                                       array('allowed_schemes' =>
207                                             array('http', 'https')))) {
208                 $this->showForm(_('Homepage is not a valid URL.'));
209                 return;
210             } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
211                 $this->showForm(_('Full name is too long (maximum 255 characters).'));
212                 return;
213             } else if (Profile::bioTooLong($bio)) {
214                 $this->showForm(sprintf(_m('Bio is too long (maximum %d character).',
215                                            'Bio is too long (maximum %d characters).',
216                                            Profile::maxBio()),
217                                         Profile::maxBio()));
218                 return;
219             } else if (!is_null($location) && mb_strlen($location) > 255) {
220                 $this->showForm(_('Location is too long (maximum 255 characters).'));
221                 return;
222             } else if (strlen($password) < 6) {
223                 $this->showForm(_('Password must be 6 or more characters.'));
224                 return;
225             } else if ($password != $confirm) {
226                 $this->showForm(_('Passwords don\'t match.'));
227             } else if ($user = User::register(array('nickname' => $nickname,
228                                                     'password' => $password,
229                                                     'email' => $email,
230                                                     'fullname' => $fullname,
231                                                     'homepage' => $homepage,
232                                                     'bio' => $bio,
233                                                     'location' => $location,
234                                                     'code' => $code))) {
235                 if (!$user) {
236                     $this->showForm(_('Invalid username or password.'));
237                     return;
238                 }
239                 // success!
240                 if (!common_set_user($user)) {
241                     $this->serverError(_('Error setting user.'));
242                     return;
243                 }
244                 // this is a real login
245                 common_real_login(true);
246                 if ($this->boolean('rememberme')) {
247                     common_debug('Adding rememberme cookie for ' . $nickname);
248                     common_rememberme($user);
249                 }
250
251                 Event::handle('EndRegistrationTry', array($this));
252
253                 // Re-init language env in case it changed (not yet, but soon)
254                 common_init_language();
255
256                 $this->showSuccess();
257             } else {
258                 $this->showForm(_('Invalid username or password.'));
259             }
260         }
261     }
262
263     /**
264      * Does the given nickname already exist?
265      *
266      * Checks a canonical nickname against the database.
267      *
268      * @param string $nickname nickname to check
269      *
270      * @return boolean true if the nickname already exists
271      */
272     function nicknameExists($nickname)
273     {
274         $user = User::staticGet('nickname', $nickname);
275         return is_object($user);
276     }
277
278     /**
279      * Does the given email address already exist?
280      *
281      * Checks a canonical email address against the database.
282      *
283      * @param string $email email address to check
284      *
285      * @return boolean true if the address already exists
286      */
287     function emailExists($email)
288     {
289         $email = common_canonical_email($email);
290         if (!$email || strlen($email) == 0) {
291             return false;
292         }
293         $user = User::staticGet('email', $email);
294         return is_object($user);
295     }
296
297     // overrrided to add entry-title class
298     function showPageTitle() {
299         if (Event::handle('StartShowPageTitle', array($this))) {
300             $this->element('h1', array('class' => 'entry-title'), $this->title());
301         }
302     }
303
304     // overrided to add hentry, and content-inner class
305     function showContentBlock()
306     {
307         $this->elementStart('div', array('id' => 'content', 'class' => 'hentry'));
308         $this->showPageTitle();
309         $this->showPageNoticeBlock();
310         $this->elementStart('div', array('id' => 'content_inner',
311                                          'class' => 'entry-content'));
312         // show the actual content (forms, lists, whatever)
313         $this->showContent();
314         $this->elementEnd('div');
315         $this->elementEnd('div');
316     }
317
318     /**
319      * Instructions or a notice for the page
320      *
321      * Shows the error, if any, or instructions for registration.
322      *
323      * @return void
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
338             $this->elementStart('div', 'instructions');
339             $this->raw($instr);
340             $this->elementEnd('div');
341         }
342     }
343
344     /**
345      * Wrapper for showing a page
346      *
347      * Stores an error and shows the page
348      *
349      * @param string $error Error, if any
350      *
351      * @return void
352      */
353     function showForm($error=null)
354     {
355         $this->error = $error;
356         $this->showPage();
357     }
358
359     /**
360      * Show the page content
361      *
362      * Either shows the registration form or, if registration was successful,
363      * instructions for using the site.
364      *
365      * @return void
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     function showFormContent()
382     {
383         $code = $this->trimmed('code');
384
385         $invite = null;
386
387         if ($code) {
388             $invite = Invitation::staticGet($code);
389         }
390
391         if (common_config('site', 'inviteonly') && !($code && $invite)) {
392             $this->clientError(_('Sorry, only invited people can register.'));
393             return;
394         }
395
396         $this->elementStart('form', array('method' => 'post',
397                                           'id' => 'form_register',
398                                           'class' => 'form_settings',
399                                           'action' => common_local_url('register')));
400         $this->elementStart('fieldset');
401         $this->element('legend', null, 'Account settings');
402         $this->hidden('token', common_session_token());
403
404         if ($this->code) {
405             $this->hidden('code', $this->code);
406         }
407
408         $this->elementStart('ul', 'form_data');
409         if (Event::handle('StartRegistrationFormData', array($this))) {
410             $this->elementStart('li');
411             $this->input('nickname', _('Nickname'), $this->trimmed('nickname'),
412                          _('1-64 lowercase letters or numbers, no punctuation or spaces.'));
413             $this->elementEnd('li');
414             $this->elementStart('li');
415             $this->password('password', _('Password'),
416                             _('6 or more characters.'));
417             $this->elementEnd('li');
418             $this->elementStart('li');
419             $this->password('confirm', _('Confirm'),
420                             _('Same as password above.'));
421             $this->elementEnd('li');
422             $this->elementStart('li');
423             if ($this->invite && $this->invite->address_type == 'email') {
424                 $this->input('email', _('Email'), $this->invite->address,
425                              _('Used only for updates, announcements, '.
426                                'and password recovery.'));
427             } else {
428                 $this->input('email', _('Email'), $this->trimmed('email'),
429                              _('Used only for updates, announcements, '.
430                                'and password recovery.'));
431             }
432             $this->elementEnd('li');
433             $this->elementStart('li');
434             $this->input('fullname', _('Full name'),
435                          $this->trimmed('fullname'),
436                          _('Longer name, preferably your "real" name.'));
437             $this->elementEnd('li');
438             $this->elementStart('li');
439             $this->input('homepage', _('Homepage'),
440                          $this->trimmed('homepage'),
441                          _('URL of your homepage, blog, '.
442                            'or profile on another site.'));
443             $this->elementEnd('li');
444             $this->elementStart('li');
445             $maxBio = Profile::maxBio();
446             if ($maxBio > 0) {
447                 // TRANS: Tooltip for field label in form for profile settings. Plural
448                 // TRANS: is decided by the number of characters available for the
449                 // TRANS: biography (%d).
450                 $bioInstr = sprintf(_m('Describe yourself and your interests in %d character',
451                                        'Describe yourself and your interests in %d characters',
452                                        $maxBio),
453                                     $maxBio);
454             } else {
455                 $bioInstr = _('Describe yourself and your interests');
456             }
457             $this->textarea('bio', _('Bio'),
458                             $this->trimmed('bio'),
459                             $bioInstr);
460             $this->elementEnd('li');
461             $this->elementStart('li');
462             $this->input('location', _('Location'),
463                          $this->trimmed('location'),
464                          _('Where you are, like "City, '.
465                            'State (or Region), Country".'));
466             $this->elementEnd('li');
467             Event::handle('EndRegistrationFormData', array($this));
468             $this->elementStart('li', array('id' => 'settings_rememberme'));
469             $this->checkbox('rememberme', _('Remember me'),
470                             $this->boolean('rememberme'),
471                             _('Automatically login in the future; '.
472                               'not for shared computers!'));
473             $this->elementEnd('li');
474             $attrs = array('type' => 'checkbox',
475                            'id' => 'license',
476                            'class' => 'checkbox',
477                            'name' => 'license',
478                            'value' => 'true');
479             if ($this->boolean('license')) {
480                 $attrs['checked'] = 'checked';
481             }
482             $this->elementStart('li');
483             $this->element('input', $attrs);
484             $this->elementStart('label', array('class' => 'checkbox', 'for' => 'license'));
485             $this->raw($this->licenseCheckbox());
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     function licenseCheckbox()
496     {
497         $out = '';
498         switch (common_config('license', 'type')) {
499         case 'private':
500             // TRANS: Copyright checkbox label in registration dialog, for private sites.
501             // TRANS: %1$s is the StatusNet sitename.
502             $out .= htmlspecialchars(sprintf(
503                 _('I understand that content and data of %1$s are private and confidential.'),
504                 common_config('site', 'name')));
505             // fall through
506         case 'allrightsreserved':
507             if ($out != '') {
508                 $out .= ' ';
509             }
510             if (common_config('license', 'owner')) {
511                 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with a specified copyright owner.
512                 $out .= htmlspecialchars(sprintf(
513                     _('My text and files are copyright by %1$s.'),
514                     common_config('license', 'owner')));
515             } else {
516                 // TRANS: Copyright checkbox label in registration dialog, for all rights reserved with ownership left to contributors.
517                 $out .= htmlspecialchars(_('My text and files remain under my own copyright.'));
518             }
519             // TRANS: Copyright checkbox label in registration dialog, for all rights reserved.
520             $out .= ' ' . _('All rights reserved.');
521             break;
522         case 'cc': // fall through
523         default:
524             // TRANS: Copyright checkbox label in registration dialog, for Creative Commons-style licenses.
525             $message = _('My text and files are available under %s ' .
526                          'except this private data: password, ' .
527                          'email address, IM address, and phone number.');
528             $link = '<a href="' .
529                     htmlspecialchars(common_config('license', 'url')) .
530                     '">' .
531                     htmlspecialchars(common_config('license', 'title')) .
532                     '</a>';
533             $out .= sprintf(htmlspecialchars($message), $link);
534         }
535         return $out;
536     }
537
538     /**
539      * Show some information about registering for the site
540      *
541      * Save the registration flag, run showPage
542      *
543      * @return void
544      */
545     function showSuccess()
546     {
547         $this->registered = true;
548         $this->showPage();
549     }
550
551     /**
552      * Show some information about registering for the site
553      *
554      * Gives some information and options for new registrees.
555      *
556      * @return void
557      */
558     function showSuccessContent()
559     {
560         $nickname = $this->arg('nickname');
561
562         $profileurl = common_local_url('showstream',
563                                        array('nickname' => $nickname));
564
565         $this->elementStart('div', 'success');
566         $instr = sprintf(_('Congratulations, %1$s! And welcome to %%%%site.name%%%%. '.
567                            'From here, you may want to...'. "\n\n" .
568                            '* Go to [your profile](%2$s) '.
569                            'and post your first message.' .  "\n" .
570                            '* Add a [Jabber/GTalk address]'.
571                            '(%%%%action.imsettings%%%%) '.
572                            'so you can send notices '.
573                            'through instant messages.' . "\n" .
574                            '* [Search for people](%%%%action.peoplesearch%%%%) '.
575                            'that you may know or '.
576                            'that share your interests. ' . "\n" .
577                            '* Update your [profile settings]'.
578                            '(%%%%action.profilesettings%%%%)'.
579                            ' to tell others more about you. ' . "\n" .
580                            '* Read over the [online docs](%%%%doc.help%%%%)'.
581                            ' for features you may have missed. ' . "\n\n" .
582                            'Thanks for signing up and we hope '.
583                            'you enjoy using this service.'),
584                          $nickname, $profileurl);
585
586         $this->raw(common_markup_to_html($instr));
587
588         $have_email = $this->trimmed('email');
589         if ($have_email) {
590             $emailinstr = _('(You should receive a message by email '.
591                             'momentarily, with ' .
592                             'instructions on how to confirm '.
593                             'your email address.)');
594             $this->raw(common_markup_to_html($emailinstr));
595         }
596         $this->elementEnd('div');
597     }
598
599     /**
600      * Show the login group nav menu
601      *
602      * @return void
603      */
604     function showLocalNav()
605     {
606         $nav = new LoginGroupNav($this);
607         $nav->show();
608     }
609
610     function showNoticeForm()
611     {
612     }
613
614     function showProfileBlock()
615     {
616     }
617 }