]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/register.php
Fix header comment
[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      * @param array $args $_REQUEST data
120      *
121      * @return void
122      */
123
124     function handle($args)
125     {
126         parent::handle($args);
127
128         if (common_config('site', 'closed')) {
129             $this->clientError(_('Registration not allowed.'));
130         } else if (common_logged_in()) {
131             $this->clientError(_('Already logged in.'));
132         } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
133             $this->tryRegister();
134         } else {
135             $this->showForm();
136         }
137     }
138
139     function showScripts()
140     {
141         parent::showScripts();
142         $this->autofocus('nickname');
143     }
144
145     /**
146      * Try to register a user
147      *
148      * Validates the input and tries to save a new user and profile
149      * record. On success, shows an instructions page.
150      *
151      * @return void
152      */
153
154     function tryRegister()
155     {
156         if (Event::handle('StartRegistrationTry', array($this))) {
157             $token = $this->trimmed('token');
158             if (!$token || $token != common_session_token()) {
159                 $this->showForm(_('There was a problem with your session token. '.
160                                   'Try again, please.'));
161                 return;
162             }
163
164             $nickname = $this->trimmed('nickname');
165             $email    = $this->trimmed('email');
166             $fullname = $this->trimmed('fullname');
167             $homepage = $this->trimmed('homepage');
168             $bio      = $this->trimmed('bio');
169             $location = $this->trimmed('location');
170
171             // We don't trim these... whitespace is OK in a password!
172             $password = $this->arg('password');
173             $confirm  = $this->arg('confirm');
174
175             // invitation code, if any
176             $code = $this->trimmed('code');
177
178             if ($code) {
179                 $invite = Invitation::staticGet($code);
180             }
181
182             if (common_config('site', 'inviteonly') && !($code && $invite)) {
183                 $this->clientError(_('Sorry, only invited people can register.'));
184                 return;
185             }
186
187             // Input scrubbing
188             $nickname = common_canonical_nickname($nickname);
189             $email    = common_canonical_email($email);
190
191             if (!$this->boolean('license')) {
192                 $this->showForm(_('You can\'t register if you don\'t '.
193                                   'agree to the license.'));
194             } else if ($email && !Validate::email($email, true)) {
195                 $this->showForm(_('Not a valid email address.'));
196             } else if (!Validate::string($nickname, array('min_length' => 1,
197                                                           'max_length' => 64,
198                                                           'format' => NICKNAME_FMT))) {
199                 $this->showForm(_('Nickname must have only lowercase letters '.
200                                   'and numbers and no spaces.'));
201             } else if ($this->nicknameExists($nickname)) {
202                 $this->showForm(_('Nickname already in use. Try another one.'));
203             } else if (!User::allowed_nickname($nickname)) {
204                 $this->showForm(_('Not a valid nickname.'));
205             } else if ($this->emailExists($email)) {
206                 $this->showForm(_('Email address already exists.'));
207             } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
208                        !Validate::uri($homepage,
209                                       array('allowed_schemes' =>
210                                             array('http', 'https')))) {
211                 $this->showForm(_('Homepage is not a valid URL.'));
212                 return;
213             } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
214                 $this->showForm(_('Full name is too long (max 255 chars).'));
215                 return;
216             } else if (Profile::bioTooLong($bio)) {
217                 $this->showForm(sprintf(_('Bio is too long (max %d chars).'),
218                                         Profile::maxBio()));
219                 return;
220             } else if (!is_null($location) && mb_strlen($location) > 255) {
221                 $this->showForm(_('Location is too long (max 255 chars).'));
222                 return;
223             } else if (strlen($password) < 6) {
224                 $this->showForm(_('Password must be 6 or more characters.'));
225                 return;
226             } else if ($password != $confirm) {
227                 $this->showForm(_('Passwords don\'t match.'));
228             } else if ($user = User::register(array('nickname' => $nickname,
229                                                     'password' => $password,
230                                                     'email' => $email,
231                                                     'fullname' => $fullname,
232                                                     'homepage' => $homepage,
233                                                     'bio' => $bio,
234                                                     'location' => $location,
235                                                     'code' => $code))) {
236                 if (!$user) {
237                     $this->showForm(_('Invalid username or password.'));
238                     return;
239                 }
240                 // success!
241                 if (!common_set_user($user)) {
242                     $this->serverError(_('Error setting user.'));
243                     return;
244                 }
245                 // this is a real login
246                 common_real_login(true);
247                 if ($this->boolean('rememberme')) {
248                     common_debug('Adding rememberme cookie for ' . $nickname);
249                     common_rememberme($user);
250                 }
251
252                 Event::handle('EndRegistrationTry', array($this));
253
254                 // Re-init language env in case it changed (not yet, but soon)
255                 common_init_language();
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
273     function nicknameExists($nickname)
274     {
275         $user = User::staticGet('nickname', $nickname);
276         return ($user !== false);
277     }
278
279     /**
280      * Does the given email address already exist?
281      *
282      * Checks a canonical email address against the database.
283      *
284      * @param string $email email address to check
285      *
286      * @return boolean true if the address already exists
287      */
288
289     function emailExists($email)
290     {
291         $email = common_canonical_email($email);
292         if (!$email || strlen($email) == 0) {
293             return false;
294         }
295         $user = User::staticGet('email', $email);
296         return ($user !== false);
297     }
298
299     // overrrided to add entry-title class
300     function showPageTitle() {
301         if (Event::handle('StartShowPageTitle', array($this))) {
302             $this->element('h1', array('class' => 'entry-title'), $this->title());
303         }
304     }
305
306     // overrided to add hentry, and content-inner class
307     function showContentBlock()
308     {
309         $this->elementStart('div', array('id' => 'content', 'class' => 'hentry'));
310         $this->showPageTitle();
311         $this->showPageNoticeBlock();
312         $this->elementStart('div', array('id' => 'content_inner',
313                                          'class' => 'entry-content'));
314         // show the actual content (forms, lists, whatever)
315         $this->showContent();
316         $this->elementEnd('div');
317         $this->elementEnd('div');
318     }
319
320     /**
321      * Instructions or a notice for the page
322      *
323      * Shows the error, if any, or instructions for registration.
324      *
325      * @return void
326      */
327
328     function showPageNotice()
329     {
330         if ($this->registered) {
331             return;
332         } else if ($this->error) {
333             $this->element('p', 'error', $this->error);
334         } else {
335             $instr =
336               common_markup_to_html(_('With this form you can create '.
337                                       ' a new account. ' .
338                                       'You can then post notices and '.
339                                       'link up to friends and colleagues. '));
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             $maxBio = Profile::maxBio();
453             if ($maxBio > 0) {
454                 $bioInstr = sprintf(_('Describe yourself and your interests in %d chars'),
455                                     $maxBio);
456             } else {
457                 $bioInstr = _('Describe yourself and your interests');
458             }
459             $this->textarea('bio', _('Bio'),
460                             $this->trimmed('bio'),
461                             $bioInstr);
462             $this->elementEnd('li');
463             $this->elementStart('li');
464             $this->input('location', _('Location'),
465                          $this->trimmed('location'),
466                          _('Where you are, like "City, '.
467                            'State (or Region), Country"'));
468             $this->elementEnd('li');
469             Event::handle('EndRegistrationFormData', array($this));
470             $this->elementStart('li', array('id' => 'settings_rememberme'));
471             $this->checkbox('rememberme', _('Remember me'),
472                             $this->boolean('rememberme'),
473                             _('Automatically login in the future; '.
474                               'not for shared computers!'));
475             $this->elementEnd('li');
476             $attrs = array('type' => 'checkbox',
477                            'id' => 'license',
478                            'class' => 'checkbox',
479                            'name' => 'license',
480                            'value' => 'true');
481             if ($this->boolean('license')) {
482                 $attrs['checked'] = 'checked';
483             }
484             $this->elementStart('li');
485             $this->element('input', $attrs);
486             $this->elementStart('label', array('class' => 'checkbox', 'for' => 'license'));
487             $this->text(_('My text and files are available under '));
488             $this->element('a', array('href' => common_config('license', 'url')),
489                            common_config('license', 'title'), _("Creative Commons Attribution 3.0"));
490             $this->text(_(' except this private data: password, '.
491                           'email address, IM address, and phone number.'));
492             $this->elementEnd('label');
493             $this->elementEnd('li');
494         }
495         $this->elementEnd('ul');
496         $this->submit('submit', _('Register'));
497         $this->elementEnd('fieldset');
498         $this->elementEnd('form');
499     }
500
501     /**
502      * Show some information about registering for the site
503      *
504      * Save the registration flag, run showPage
505      *
506      * @return void
507      */
508
509     function showSuccess()
510     {
511         $this->registered = true;
512         $this->showPage();
513     }
514
515     /**
516      * Show some information about registering for the site
517      *
518      * Gives some information and options for new registrees.
519      *
520      * @return void
521      */
522
523     function showSuccessContent()
524     {
525         $nickname = $this->arg('nickname');
526
527         $profileurl = common_local_url('showstream',
528                                        array('nickname' => $nickname));
529
530         $this->elementStart('div', 'success');
531         $instr = sprintf(_('Congratulations, %s! And welcome to %%%%site.name%%%%. '.
532                            'From here, you may want to...'. "\n\n" .
533                            '* Go to [your profile](%s) '.
534                            'and post your first message.' .  "\n" .
535                            '* Add a [Jabber/GTalk address]'.
536                            '(%%%%action.imsettings%%%%) '.
537                            'so you can send notices '.
538                            'through instant messages.' . "\n" .
539                            '* [Search for people](%%%%action.peoplesearch%%%%) '.
540                            'that you may know or '.
541                            'that share your interests. ' . "\n" .
542                            '* Update your [profile settings]'.
543                            '(%%%%action.profilesettings%%%%)'.
544                            ' to tell others more about you. ' . "\n" .
545                            '* Read over the [online docs](%%%%doc.help%%%%)'.
546                            ' for features you may have missed. ' . "\n\n" .
547                            'Thanks for signing up and we hope '.
548                            'you enjoy using this service.'),
549                          $nickname, $profileurl);
550
551         $this->raw(common_markup_to_html($instr));
552
553         $have_email = $this->trimmed('email');
554         if ($have_email) {
555             $emailinstr = _('(You should receive a message by email '.
556                             'momentarily, with ' .
557                             'instructions on how to confirm '.
558                             'your email address.)');
559             $this->raw(common_markup_to_html($emailinstr));
560         }
561         $this->elementEnd('div');
562     }
563
564     /**
565      * Show the login group nav menu
566      *
567      * @return void
568      */
569
570     function showLocalNav()
571     {
572         $nav = new LoginGroupNav($this);
573         $nav->show();
574     }
575 }
576