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