]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apiaccountregister.php
Merge branch 'master' into social-master
[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(array $args=array())
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::getKV('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, true);
118         } catch (NicknameException $e) {
119             // clientError handles Api exceptions with various formats and stuff
120                 $this->clientError($e->getMessage(), $e->getCode());
121         }
122
123         $email = common_canonical_email($email);
124
125             if ($email && !Validate::email($email, common_config('email', 'check_domain'))) {
126             // TRANS: Form validation error displayed when trying to register without a valid e-mail address.
127                 $this->clientError(_('Not a valid email address.'), 400);
128         } else if ($this->emailExists($email)) {
129             // TRANS: Form validation error displayed when trying to register with an already registered e-mail address.
130                 $this->clientError(_('Email address already exists.'), 400);
131         } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
132                    !common_valid_http_url($homepage)) {
133             // TRANS: Form validation error displayed when trying to register with an invalid homepage URL.
134                 $this->clientError(_('Homepage is not a valid URL.'), 400);
135         } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
136             // TRANS: Form validation error displayed when trying to register with a too long full name.
137                 $this->clientError(_('Full name is too long (maximum 255 characters).'), 400);
138         } else if (Profile::bioTooLong($bio)) {
139             // TRANS: Form validation error on registration page when providing too long a bio text.
140             // TRANS: %d is the maximum number of characters for bio; used for plural.
141                 $this->clientError(sprintf(_m('Bio is too long (maximum %d character).',
142                                        'Bio is too long (maximum %d characters).',
143                                        Profile::maxBio()),
144                                        Profile::maxBio()), 400);
145         } else if (!is_null($location) && mb_strlen($location) > 255) {
146             // TRANS: Form validation error displayed when trying to register with a too long location.
147                 $this->clientError(_('Location is too long (maximum 255 characters).'), 400);
148         } else if (strlen($password) < 6) {
149             // TRANS: Form validation error displayed when trying to register with too short a password.
150                 $this->clientError(_('Password must be 6 or more characters.'), 400);
151         } else if ($password != $confirm) {
152             // TRANS: Form validation error displayed when trying to register with non-matching passwords.
153                 $this->clientError(_('Passwords do not match.'), 400);
154         } else {
155             
156                 // annoy spammers
157                 sleep(7);
158             
159                 if ($user = User::register(array('nickname' => $nickname,
160                                                         'password' => $password,
161                                                         'email' => $email,
162                                                         'fullname' => $fullname,
163                                                         'homepage' => $homepage,
164                                                         'bio' => $bio,
165                                                         'location' => $location,
166                                                         'code' => $this->code))) {
167                     if (!$user instanceof User) {
168                         // TRANS: Form validation error displayed when trying to register with an invalid username or password.
169                         $this->clientError(_('Invalid username or password.'), 400);
170                     }
171
172                     Event::handle('EndRegistrationTry', array($this));
173
174                     $this->initDocument('json');
175                     $this->showJsonObjects($this->twitterUserArray($user->getProfile()));
176                     $this->endDocument('json');
177
178                 } else {
179                     // TRANS: Form validation error displayed when trying to register with an invalid username or password.
180                         $this->clientError(_('Invalid username or password.'), 400);
181                 }                   
182         } 
183     }
184
185     /**
186      * Does the given email address already exist?
187      *
188      * Checks a canonical email address against the database.
189      *
190      * @param string $email email address to check
191      *
192      * @return boolean true if the address already exists
193      */
194     function emailExists($email)
195     {
196         $email = common_canonical_email($email);
197         if (!$email || strlen($email) == 0) {
198             return false;
199         }
200         $user = User::getKV('email', $email);
201         return is_object($user);
202     }
203
204 }