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