]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/register.php
Update register action to match phpcs and new framework
[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) && strlen($fullname) > 255) {
171             $this->showForm(_('Full name is too long (max 255 chars).'));
172             return;
173         } else if (!is_null($bio) && strlen($bio) > 140) {
174             $this->showForm(_('Bio is too long (max 140 chars).'));
175             return;
176         } else if (!is_null($location) && 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      * Does the given email address already exist?
233      *
234      * Checks a canonical email address against the database.
235      *
236      * @param string $email email address to check
237      *
238      * @return boolean true if the address already exists
239      */
240
241     function emailExists($email)
242     {
243         $email = common_canonical_email($email);
244         if (!$email || strlen($email) == 0) {
245             return false;
246         }
247         $user = User::staticGet('email', $email);
248         return ($user !== false);
249     }
250
251     /**
252      * Instructions or a notice for the page
253      *
254      * Shows the error, if any, or instructions for registration.
255      *
256      * @return void
257      */
258
259     function showPageNotice()
260     {
261         if ($this->registered) {
262             return;
263         } else if ($this->error) {
264             $this->element('p', 'error', $this->error);
265         } else {
266             $instr =
267               common_markup_to_html(_('With this form you can create '.
268                                       ' a new account. ' .
269                                       'You can then post notices and '.
270                                       'link up to friends and colleagues. '.
271                                       '(Have an [OpenID](http://openid.net/)? ' .
272                                       'Try our [OpenID registration]'.
273                                       '(%%action.openidlogin%%)!)'));
274
275             $this->elementStart('div', 'instructions');
276             $this->raw($instr);
277             $this->elementEnd('div');
278         }
279     }
280
281     /**
282      * Wrapper for showing a page
283      *
284      * Stores an error and shows the page
285      *
286      * @param string $error Error, if any
287      *
288      * @return void
289      */
290
291     function showForm($error=null)
292     {
293         $this->error = $error;
294         $this->showPage();
295     }
296
297     /**
298      * Show the page content
299      *
300      * Either shows the registration form or, if registration was successful,
301      * instructions for using the site.
302      *
303      * @return void
304      */
305
306     function showContent()
307     {
308         if ($this->registered) {
309             $this->showSuccessContent();
310         } else {
311             $this->showFormContent();
312         }
313     }
314
315     /**
316      * Show the registration form
317      *
318      * @return void
319      */
320
321     function showFormContent()
322     {
323         $code = $this->trimmed('code');
324
325         if ($code) {
326             $invite = Invitation::staticGet($code);
327         }
328
329         if (common_config('site', 'inviteonly') && !($code && $invite)) {
330             $this->clientError(_('Sorry, only invited people can register.'));
331             return;
332         }
333
334         $this->elementStart('form', array('method' => 'post',
335                                           'id' => 'login',
336                                           'action' => common_local_url('register')));
337
338         $this->hidden('token', common_session_token());
339
340         if ($code) {
341             $this->hidden('code', $code);
342         }
343
344         $this->input('nickname', _('Nickname'), $this->trimmed('nickname'),
345                      _('1-64 lowercase letters or numbers, '.
346                        'no punctuation or spaces. Required.'));
347         $this->password('password', _('Password'),
348                         _('6 or more characters. Required.'));
349         $this->password('confirm', _('Confirm'),
350                         _('Same as password above. Required.'));
351         if ($invite && $invite->address_type == 'email') {
352             $this->input('email', _('Email'), $invite->address,
353                          _('Used only for updates, announcements, '.
354                            'and password recovery'));
355         } else {
356             $this->input('email', _('Email'), $this->trimmed('email'),
357                          _('Used only for updates, announcements, '.
358                            'and password recovery'));
359         }
360         $this->input('fullname', _('Full name'),
361                      $this->trimmed('fullname'),
362                      _('Longer name, preferably your "real" name'));
363         $this->input('homepage', _('Homepage'),
364                      $this->trimmed('homepage'),
365                      _('URL of your homepage, blog, '.
366                        'or profile on another site'));
367         $this->textarea('bio', _('Bio'),
368                         $this->trimmed('bio'),
369                         _('Describe yourself and your '.
370                           'interests in 140 chars'));
371         $this->input('location', _('Location'),
372                      $this->trimmed('location'),
373                      _('Where you are, like "City, '.
374                        'State (or Region), Country"'));
375         $this->checkbox('rememberme', _('Remember me'),
376                         $this->boolean('rememberme'),
377                         _('Automatically login in the future; '.
378                           'not for shared computers!'));
379         $this->elementStart('p');
380         $attrs = array('type' => 'checkbox',
381                        'id' => 'license',
382                        'name' => 'license',
383                        'value' => 'true');
384         if ($this->boolean('license')) {
385             $attrs['checked'] = 'checked';
386         }
387         $this->element('input', $attrs);
388         $this->text(_('My text and files are available under '));
389         $this->element('a', array('href' => common_config('license', 'url')),
390                        $config['license']['title']);
391         $this->text(_(' except this private data: password, '.
392                       'email address, IM address, phone number.'));
393         $this->elementEnd('p');
394         $this->submit('submit', _('Register'));
395         $this->elementEnd('form');
396     }
397
398     /**
399      * Show some information about registering for the site
400      *
401      * Save the registration flag, run showPage
402      *
403      * @return void
404      */
405
406     function showSuccess()
407     {
408         $this->registered = true;
409         $this->showPage();
410     }
411
412     /**
413      * Show some information about registering for the site
414      *
415      * Gives some information and options for new registrees.
416      *
417      * @return void
418      */
419
420     function showSuccessContent()
421     {
422         $nickname = $this->arg('nickname');
423
424         $profileurl = common_local_url('showstream',
425                                        array('nickname' => $nickname));
426
427         $this->elementStart('div', 'success');
428         $instr = sprintf(_('Congratulations, %s! And welcome to %%%%site.name%%%%. '.
429                            'From here, you may want to...'. "\n\n" .
430                            '* Go to [your profile](%s) '.
431                            'and post your first message.' .  "\n" .
432                            '* Add a [Jabber/GTalk address]'.
433                            '(%%%%action.imsettings%%%%) '.
434                            'so you can send notices '.
435                            'through instant messages.' . "\n" .
436                            '* [Search for people](%%%%action.peoplesearch%%%%) '.
437                            'that you may know or '.
438                            'that share your interests. ' . "\n" .
439                            '* Update your [profile settings]'.
440                            '(%%%%action.profilesettings%%%%)'.
441                            ' to tell others more about you. ' . "\n" .
442                            '* Read over the [online docs](%%%%doc.help%%%%)'.
443                            ' for features you may have missed. ' . "\n\n" .
444                            'Thanks for signing up and we hope '.
445                            'you enjoy using this service.'),
446                          $nickname, $profileurl);
447
448         $this->raw(common_markup_to_html($instr));
449
450         $have_email = $this->trimmed('email');
451         if ($have_email) {
452             $emailinstr = _('(You should receive a message by email '.
453                             'momentarily, with ' .
454                             'instructions on how to confirm '.
455                             'your email address.)');
456             $this->raw(common_markup_to_html($emailinstr));
457         }
458         $this->elementEnd('div');
459     }
460
461     /**
462      * Show the login group nav menu
463      *
464      * @return void
465      */
466
467     function showLocalNav()
468     {
469         $nav = new LoginGroupNav($this);
470         $nav->show();
471     }
472 }