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