]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apiaccountregister.php
Validate::uri replaced with filter_var for HTTP[S] URL checks
[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     /**
46      * Are we processing an invite?
47      */
48     var $invite = null;           
49
50     /**
51      * Take arguments for running
52      *
53      * @param array $args $_REQUEST args
54      *
55      * @return boolean success flag
56      */
57     function prepare($args)
58     {
59         parent::prepare($args);
60         $this->code = $this->trimmed('code');
61
62         if (empty($this->code)) {
63             common_ensure_session();
64             if (array_key_exists('invitecode', $_SESSION)) {
65                 $this->code = $_SESSION['invitecode'];
66             }
67         }
68
69         if (common_config('site', 'inviteonly') && empty($this->code)) {
70             // TRANS: Client error displayed when trying to register to an invite-only site without an invitation.
71             $this->clientError(_('Sorry, only invited people can register.'),404,'json');
72             return false;
73         }
74
75         if (!empty($this->code)) {
76             $this->invite = Invitation::staticGet('code', $this->code);
77             if (empty($this->invite)) {
78             // TRANS: Client error displayed when trying to register to an invite-only site without a valid invitation.
79                     $this->clientError(_('Sorry, invalid invitation code.'),404,'json');        
80                 return false;
81             }
82             // Store this in case we need it
83             common_ensure_session();
84             $_SESSION['invitecode'] = $this->code;
85         }
86
87         return true;
88     }
89
90     /**
91      * Handle the request
92      *
93      * @param array $args $_REQUEST data (unused)
94      *
95      * @return void
96      */
97     function handle($args)
98     {
99         parent::handle($args);
100
101         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
102             $this->clientError(
103                 _('This method requires a POST.'),
104                 400, $this->format
105             );
106             return;
107
108         } else {
109         
110             $nickname = $this->trimmed('nickname');
111             $email    = $this->trimmed('email');
112             $fullname = $this->trimmed('fullname');
113             $homepage = $this->trimmed('homepage');
114             $bio      = $this->trimmed('bio');
115             $location = $this->trimmed('location');
116
117             // We don't trim these... whitespace is OK in a password!
118             $password = $this->arg('password');
119             $confirm  = $this->arg('confirm');
120
121             // invitation code, if any
122             $code = $this->trimmed('code');
123
124             if ($code) {
125                 $invite = Invitation::staticGet($code);
126             }
127
128             if (common_config('site', 'inviteonly') && !($code && $invite)) {
129                 // TRANS: Client error displayed when trying to register to an invite-only site without an invitation.
130                     $this->clientError(_('Sorry, only invited people can register.'),404,'json');       
131                 return;
132             }
133
134             // Input scrubbing
135             try {
136                 $nickname = Nickname::normalize($nickname);
137             } catch (NicknameException $e) {
138                 $this->showForm($e->getMessage());
139             }
140             $email    = common_canonical_email($email);
141
142                         if ($email && !Validate::email($email, common_config('email', 'check_domain'))) {
143                 // TRANS: Form validation error displayed when trying to register without a valid e-mail address.
144                     $this->clientError(_('Not a valid email address.'),404,'json');     
145             } else if ($this->nicknameExists($nickname)) {
146                 // TRANS: Form validation error displayed when trying to register with an existing nickname.
147                     $this->clientError(_('Nickname already in use. Try another one.'),404,'json');      
148             } else if (!User::allowed_nickname($nickname)) {
149                 // TRANS: Form validation error displayed when trying to register with an invalid nickname.
150                     $this->clientError(_('Not a valid nickname.'),404,'json');  
151             } else if ($this->emailExists($email)) {
152                 // TRANS: Form validation error displayed when trying to register with an already registered e-mail address.
153                     $this->clientError(_('Email address already exists.'),404,'json');  
154             } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
155                        !common_valid_http_url($homepage)) {
156                 // TRANS: Form validation error displayed when trying to register with an invalid homepage URL.
157                     $this->clientError(_('Homepage is not a valid URL.'),404,'json');   
158                 return;
159             } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
160                 // TRANS: Form validation error displayed when trying to register with a too long full name.
161                     $this->clientError(_('Full name is too long (maximum 255 characters).'),404,'json');        
162                 return;
163             } else if (Profile::bioTooLong($bio)) {
164                 // TRANS: Form validation error on registration page when providing too long a bio text.
165                 // TRANS: %d is the maximum number of characters for bio; used for plural.
166                     $this->clientError(sprintf(_m('Bio is too long (maximum %d character).',
167                                            'Bio is too long (maximum %d characters).',
168                                            Profile::maxBio()),
169                                            Profile::maxBio()),404,'json');      
170                 return;
171             } else if (!is_null($location) && mb_strlen($location) > 255) {
172                 // TRANS: Form validation error displayed when trying to register with a too long location.
173                     $this->clientError(_('Location is too long (maximum 255 characters).'),404,'json'); 
174                 return;
175             } else if (strlen($password) < 6) {
176                 // TRANS: Form validation error displayed when trying to register with too short a password.
177                     $this->clientError(_('Password must be 6 or more characters.'),404,'json'); 
178                 return;
179             } else if ($password != $confirm) {
180                 // TRANS: Form validation error displayed when trying to register with non-matching passwords.
181                     $this->clientError(_('Passwords do not match.'),404,'json');        
182             } else {
183                                 
184                                 // annoy spammers
185                                 sleep(7);
186                                 
187                                 if ($user = User::register(array('nickname' => $nickname,
188                                                             'password' => $password,
189                                                             'email' => $email,
190                                                             'fullname' => $fullname,
191                                                             'homepage' => $homepage,
192                                                             'bio' => $bio,
193                                                             'location' => $location,
194                                                             'code' => $code))) {
195                         if (!$user) {
196                             // TRANS: Form validation error displayed when trying to register with an invalid username or password.
197                                     $this->clientError(_('Invalid username or password.'),404,'json');  
198                             return;
199                         }
200         
201                         Event::handle('EndRegistrationTry', array($this));
202         
203                                 $this->initDocument('json');
204                                 $this->showJsonObjects($this->twitterUserArray($user->getProfile()));
205                                 $this->endDocument('json');
206         
207                     } else {
208                         // TRANS: Form validation error displayed when trying to register with an invalid username or password.
209                         $this->clientError(_('Invalid username or password.'),404,'json');      
210                 }                       
211             } 
212         }
213     }
214       
215
216     /**
217      * Does the given nickname already exist?
218      *
219      * Checks a canonical nickname against the database.
220      *
221      * @param string $nickname nickname to check
222      *
223      * @return boolean true if the nickname already exists
224      */
225     function nicknameExists($nickname)
226     {
227         $user = User::staticGet('nickname', $nickname);
228         return is_object($user);
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     function emailExists($email)
241     {
242         $email = common_canonical_email($email);
243         if (!$email || strlen($email) == 0) {
244             return false;
245         }
246         $user = User::staticGet('email', $email);
247         return is_object($user);
248     }
249
250 }