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