]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apiaccountregister.php
Let's not limit qvitter stuff to 'json' requests
[quix0rs-gnu-social.git] / actions / apiaccountregister.php
1 <?php
2         
3 /**
4  * StatusNet, the distributed open-source microblogging tool
5  *
6  * Register account
7  *
8  * PHP version 5
9  *
10  * LICENCE: This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  API
24  * @package   GNUSocial
25  * @author    Hannes Mannerheim <h@nnesmannerhe.im>
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://www.gnu.org/software/social/
28  */
29
30 if (!defined('GNUSOCIAL')) { exit(1); }
31
32 class ApiAccountRegisterAction extends ApiAction
33 {
34
35     /**
36      * Has there been an error?
37      */
38     var $error = null;
39
40     /**
41      * Have we registered?
42      */
43     var $registered = false;
44
45     protected $needPost = true;
46
47     protected $code   = null; // invite code
48     protected $invite = null; // invite to-be-stored
49
50     /**
51      * Take arguments for running
52      *
53      * @param array $args $_REQUEST args
54      *
55      * @return boolean success flag
56      */
57     protected function prepare($args)
58     {
59         parent::prepare($args);
60
61         if ($this->format !== 'json') {
62             $this->clientError('This method currently only serves JSON.', 415);
63         }
64
65         $this->code = $this->trimmed('code');
66
67         return true;
68     }
69
70     /**
71      * Handle the request
72      *
73      * @param array $args $_REQUEST data (unused)
74      *
75      * @return void
76      */
77     protected function handle()
78     {
79         parent::handle();
80
81         $nickname = $this->trimmed('nickname');
82         $email    = $this->trimmed('email');
83         $fullname = $this->trimmed('fullname');
84         $homepage = $this->trimmed('homepage');
85         $bio      = $this->trimmed('bio');
86         $location = $this->trimmed('location');
87
88         // We don't trim these... whitespace is OK in a password!
89         $password = $this->arg('password');
90         $confirm  = $this->arg('confirm');
91
92         if (empty($this->code)) {
93             common_ensure_session();
94             if (array_key_exists('invitecode', $_SESSION)) {
95                 $this->code = $_SESSION['invitecode'];
96             }
97         }
98
99         if (common_config('site', 'inviteonly') && empty($this->code)) {
100             // TRANS: Client error displayed when trying to register to an invite-only site without an invitation.
101             $this->clientError(_('Sorry, only invited people can register.'), 401);
102         }
103
104         if (!empty($this->code)) {
105             $this->invite = Invitation::staticGet('code', $this->code);
106             if (empty($this->invite)) {
107             // TRANS: Client error displayed when trying to register to an invite-only site without a valid invitation.
108                     $this->clientError(_('Sorry, invalid invitation code.'), 401);
109             }
110             // Store this in case we need it
111             common_ensure_session();
112             $_SESSION['invitecode'] = $this->code;
113         }
114
115         // Input scrubbing
116         try {
117             $nickname = Nickname::normalize($nickname);
118         } catch (NicknameException $e) {
119             // clientError handles Api exceptions with various formats and stuff
120                 $this->clientError(_('Not a valid nickname.'), 400);
121         }
122         $email = common_canonical_email($email);
123
124             if ($email && !Validate::email($email, common_config('email', 'check_domain'))) {
125             // TRANS: Form validation error displayed when trying to register without a valid e-mail address.
126                 $this->clientError(_('Not a valid email address.'), 400);
127         } else if ($this->nicknameExists($nickname)) {
128             // TRANS: Form validation error displayed when trying to register with an existing nickname.
129                 $this->clientError(_('Nickname already in use. Try another one.'), 400);
130         } else if (!User::allowed_nickname($nickname)) {
131             // TRANS: Form validation error displayed when trying to register with an invalid nickname.
132                 $this->clientError(_('Not a valid nickname.'), 400);
133         } else if ($this->emailExists($email)) {
134             // TRANS: Form validation error displayed when trying to register with an already registered e-mail address.
135                 $this->clientError(_('Email address already exists.'), 400);
136         } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
137                    !common_valid_http_url($homepage)) {
138             // TRANS: Form validation error displayed when trying to register with an invalid homepage URL.
139                 $this->clientError(_('Homepage is not a valid URL.'), 400);
140         } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
141             // TRANS: Form validation error displayed when trying to register with a too long full name.
142                 $this->clientError(_('Full name is too long (maximum 255 characters).'), 400);
143         } else if (Profile::bioTooLong($bio)) {
144             // TRANS: Form validation error on registration page when providing too long a bio text.
145             // TRANS: %d is the maximum number of characters for bio; used for plural.
146                 $this->clientError(sprintf(_m('Bio is too long (maximum %d character).',
147                                        'Bio is too long (maximum %d characters).',
148                                        Profile::maxBio()),
149                                        Profile::maxBio()), 400);
150         } else if (!is_null($location) && mb_strlen($location) > 255) {
151             // TRANS: Form validation error displayed when trying to register with a too long location.
152                 $this->clientError(_('Location is too long (maximum 255 characters).'), 400);
153         } else if (strlen($password) < 6) {
154             // TRANS: Form validation error displayed when trying to register with too short a password.
155                 $this->clientError(_('Password must be 6 or more characters.'), 400);
156         } else if ($password != $confirm) {
157             // TRANS: Form validation error displayed when trying to register with non-matching passwords.
158                 $this->clientError(_('Passwords do not match.'), 400);
159         } else {
160             
161                 // annoy spammers
162                 sleep(7);
163             
164                 if ($user = User::register(array('nickname' => $nickname,
165                                                         'password' => $password,
166                                                         'email' => $email,
167                                                         'fullname' => $fullname,
168                                                         'homepage' => $homepage,
169                                                         'bio' => $bio,
170                                                         'location' => $location,
171                                                         'code' => $code))) {
172                     if (!$user) {
173                         // TRANS: Form validation error displayed when trying to register with an invalid username or password.
174                         $this->clientError(_('Invalid username or password.'), 400);
175                     }
176
177                     Event::handle('EndRegistrationTry', array($this));
178
179                     $this->initDocument('json');
180                     $this->showJsonObjects($this->twitterUserArray($user->getProfile()));
181                     $this->endDocument('json');
182
183                 } else {
184                     // TRANS: Form validation error displayed when trying to register with an invalid username or password.
185                         $this->clientError(_('Invalid username or password.'), 400);
186                 }                   
187         } 
188     }
189       
190
191     /**
192      * Does the given nickname already exist?
193      *
194      * Checks a canonical nickname against the database.
195      *
196      * @param string $nickname nickname to check
197      *
198      * @return boolean true if the nickname already exists
199      */
200     function nicknameExists($nickname)
201     {
202         $user = User::staticGet('nickname', $nickname);
203         return is_object($user);
204     }
205
206     /**
207      * Does the given email address already exist?
208      *
209      * Checks a canonical email address against the database.
210      *
211      * @param string $email email address to check
212      *
213      * @return boolean true if the address already exists
214      */
215     function emailExists($email)
216     {
217         $email = common_canonical_email($email);
218         if (!$email || strlen($email) == 0) {
219             return false;
220         }
221         $user = User::staticGet('email', $email);
222         return is_object($user);
223     }
224
225 }