]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/register.php
move openid instructions to OpenIDPlugin
[quix0rs-gnu-social.git] / actions / register.php
1 <?php
2 /**
3  * Laconica, 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   Laconica
24  * @author    Evan Prodromou <evan@controlyourself.ca>
25  * @copyright 2008-2009 Control Yourself, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://laconi.ca/
28  */
29
30 if (!defined('LACONICA')) {
31     exit(1);
32 }
33
34 /**
35  * An action for registering a new user account
36  *
37  * @category Login
38  * @package  Laconica
39  * @author   Evan Prodromou <evan@controlyourself.ca>
40  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41  * @link     http://laconi.ca/
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 (!is_null($bio) && mb_strlen($bio) > 140) {
211                 $this->showForm(_('Bio is too long (max 140 chars).'));
212                 return;
213             } else if (!is_null($location) && mb_strlen($location) > 255) {
214                 $this->showForm(_('Location is too long (max 255 chars).'));
215                 return;
216             } else if (strlen($password) < 6) {
217                 $this->showForm(_('Password must be 6 or more characters.'));
218                 return;
219             } else if ($password != $confirm) {
220                 $this->showForm(_('Passwords don\'t match.'));
221             } else if ($user = User::register(array('nickname' => $nickname,
222                                                     'password' => $password,
223                                                     'email' => $email,
224                                                     'fullname' => $fullname,
225                                                     'homepage' => $homepage,
226                                                     'bio' => $bio,
227                                                     'location' => $location,
228                                                     'code' => $code))) {
229                 if (!$user) {
230                     $this->showForm(_('Invalid username or password.'));
231                     return;
232                 }
233                 // success!
234                 if (!common_set_user($user)) {
235                     $this->serverError(_('Error setting user.'));
236                     return;
237                 }
238                 // this is a real login
239                 common_real_login(true);
240                 if ($this->boolean('rememberme')) {
241                     common_debug('Adding rememberme cookie for ' . $nickname);
242                     common_rememberme($user);
243                 }
244
245                 Event::handle('EndRegistrationTry', array($this));
246
247                 // Re-init language env in case it changed (not yet, but soon)
248                 common_init_language();
249                 $this->showSuccess();
250             } else {
251                 $this->showForm(_('Invalid username or password.'));
252             }
253         }
254     }
255
256     /**
257      * Does the given nickname already exist?
258      *
259      * Checks a canonical nickname against the database.
260      *
261      * @param string $nickname nickname to check
262      *
263      * @return boolean true if the nickname already exists
264      */
265
266     function nicknameExists($nickname)
267     {
268         $user = User::staticGet('nickname', $nickname);
269         return ($user !== false);
270     }
271
272     /**
273      * Does the given email address already exist?
274      *
275      * Checks a canonical email address against the database.
276      *
277      * @param string $email email address to check
278      *
279      * @return boolean true if the address already exists
280      */
281
282     function emailExists($email)
283     {
284         $email = common_canonical_email($email);
285         if (!$email || strlen($email) == 0) {
286             return false;
287         }
288         $user = User::staticGet('email', $email);
289         return ($user !== false);
290     }
291
292     // overrrided to add entry-title class
293     function showPageTitle() {
294         if (Event::handle('StartShowPageTitle', array($this))) {
295             $this->element('h1', array('class' => 'entry-title'), $this->title());
296         }
297     }
298
299     // overrided to add hentry, and content-inner class
300     function showContentBlock()
301     {
302         $this->elementStart('div', array('id' => 'content', 'class' => 'hentry'));
303         $this->showPageTitle();
304         $this->showPageNoticeBlock();
305         $this->elementStart('div', array('id' => 'content_inner',
306                                          'class' => 'entry-content'));
307         // show the actual content (forms, lists, whatever)
308         $this->showContent();
309         $this->elementEnd('div');
310         $this->elementEnd('div');
311     }
312
313     /**
314      * Instructions or a notice for the page
315      *
316      * Shows the error, if any, or instructions for registration.
317      *
318      * @return void
319      */
320
321     function showPageNotice()
322     {
323         if ($this->registered) {
324             return;
325         } else if ($this->error) {
326             $this->element('p', 'error', $this->error);
327         } else {
328             $instr =
329               common_markup_to_html(_('With this form you can create '.
330                                       ' a new account. ' .
331                                       'You can then post notices and '.
332                                       'link up to friends and colleagues. '));
333
334             $this->elementStart('div', 'instructions');
335             $this->raw($instr);
336             $this->elementEnd('div');
337         }
338     }
339
340     /**
341      * Wrapper for showing a page
342      *
343      * Stores an error and shows the page
344      *
345      * @param string $error Error, if any
346      *
347      * @return void
348      */
349
350     function showForm($error=null)
351     {
352         $this->error = $error;
353         $this->showPage();
354     }
355
356     /**
357      * Show the page content
358      *
359      * Either shows the registration form or, if registration was successful,
360      * instructions for using the site.
361      *
362      * @return void
363      */
364
365     function showContent()
366     {
367         if ($this->registered) {
368             $this->showSuccessContent();
369         } else {
370             $this->showFormContent();
371         }
372     }
373
374     /**
375      * Show the registration form
376      *
377      * @return void
378      */
379
380     function showFormContent()
381     {
382         $code = $this->trimmed('code');
383
384         $invite = null;
385
386         if ($code) {
387             $invite = Invitation::staticGet($code);
388         }
389
390         if (common_config('site', 'inviteonly') && !($code && $invite)) {
391             $this->clientError(_('Sorry, only invited people can register.'));
392             return;
393         }
394
395         $this->elementStart('form', array('method' => 'post',
396                                           'id' => 'form_register',
397                                           'class' => 'form_settings',
398                                           'action' => common_local_url('register')));
399         $this->elementStart('fieldset');
400         $this->element('legend', null, 'Account settings');
401         $this->hidden('token', common_session_token());
402
403         if ($this->code) {
404             $this->hidden('code', $this->code);
405         }
406
407         $this->elementStart('ul', 'form_data');
408         if (Event::handle('StartRegistrationFormData', array($this))) {
409             $this->elementStart('li');
410             $this->input('nickname', _('Nickname'), $this->trimmed('nickname'),
411                          _('1-64 lowercase letters or numbers, '.
412                            'no punctuation or spaces. Required.'));
413             $this->elementEnd('li');
414             $this->elementStart('li');
415             $this->password('password', _('Password'),
416                             _('6 or more characters. Required.'));
417             $this->elementEnd('li');
418             $this->elementStart('li');
419             $this->password('confirm', _('Confirm'),
420                             _('Same as password above. Required.'));
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             $this->textarea('bio', _('Bio'),
446                             $this->trimmed('bio'),
447                             _('Describe yourself and your '.
448                               'interests in 140 chars'));
449             $this->elementEnd('li');
450             $this->elementStart('li');
451             $this->input('location', _('Location'),
452                          $this->trimmed('location'),
453                          _('Where you are, like "City, '.
454                            'State (or Region), Country"'));
455             $this->elementEnd('li');
456             Event::handle('EndRegistrationFormData', array($this));
457             $this->elementStart('li', array('id' => 'settings_rememberme'));
458             $this->checkbox('rememberme', _('Remember me'),
459                             $this->boolean('rememberme'),
460                             _('Automatically login in the future; '.
461                               'not for shared computers!'));
462             $this->elementEnd('li');
463             $attrs = array('type' => 'checkbox',
464                            'id' => 'license',
465                            'class' => 'checkbox',
466                            'name' => 'license',
467                            'value' => 'true');
468             if ($this->boolean('license')) {
469                 $attrs['checked'] = 'checked';
470             }
471             $this->elementStart('li');
472             $this->element('input', $attrs);
473             $this->elementStart('label', array('class' => 'checkbox', 'for' => 'license'));
474             $this->text(_('My text and files are available under '));
475             $this->element('a', array('href' => common_config('license', 'url')),
476                            common_config('license', 'title'), _("Creative Commons Attribution 3.0"));
477             $this->text(_(' except this private data: password, '.
478                           'email address, IM address, and phone number.'));
479             $this->elementEnd('label');
480             $this->elementEnd('li');
481         }
482         $this->elementEnd('ul');
483         $this->submit('submit', _('Register'));
484         $this->elementEnd('fieldset');
485         $this->elementEnd('form');
486     }
487
488     /**
489      * Show some information about registering for the site
490      *
491      * Save the registration flag, run showPage
492      *
493      * @return void
494      */
495
496     function showSuccess()
497     {
498         $this->registered = true;
499         $this->showPage();
500     }
501
502     /**
503      * Show some information about registering for the site
504      *
505      * Gives some information and options for new registrees.
506      *
507      * @return void
508      */
509
510     function showSuccessContent()
511     {
512         $nickname = $this->arg('nickname');
513
514         $profileurl = common_local_url('showstream',
515                                        array('nickname' => $nickname));
516
517         $this->elementStart('div', 'success');
518         $instr = sprintf(_('Congratulations, %s! And welcome to %%%%site.name%%%%. '.
519                            'From here, you may want to...'. "\n\n" .
520                            '* Go to [your profile](%s) '.
521                            'and post your first message.' .  "\n" .
522                            '* Add a [Jabber/GTalk address]'.
523                            '(%%%%action.imsettings%%%%) '.
524                            'so you can send notices '.
525                            'through instant messages.' . "\n" .
526                            '* [Search for people](%%%%action.peoplesearch%%%%) '.
527                            'that you may know or '.
528                            'that share your interests. ' . "\n" .
529                            '* Update your [profile settings]'.
530                            '(%%%%action.profilesettings%%%%)'.
531                            ' to tell others more about you. ' . "\n" .
532                            '* Read over the [online docs](%%%%doc.help%%%%)'.
533                            ' for features you may have missed. ' . "\n\n" .
534                            'Thanks for signing up and we hope '.
535                            'you enjoy using this service.'),
536                          $nickname, $profileurl);
537
538         $this->raw(common_markup_to_html($instr));
539
540         $have_email = $this->trimmed('email');
541         if ($have_email) {
542             $emailinstr = _('(You should receive a message by email '.
543                             'momentarily, with ' .
544                             'instructions on how to confirm '.
545                             'your email address.)');
546             $this->raw(common_markup_to_html($emailinstr));
547         }
548         $this->elementEnd('div');
549     }
550
551     /**
552      * Show the login group nav menu
553      *
554      * @return void
555      */
556
557     function showLocalNav()
558     {
559         $nav = new LoginGroupNav($this);
560         $nav->show();
561     }
562 }
563