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