]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apiaccountregister.php
Add AtomPub, Twitter-compat. API documentation to doc-src/
[quix0rs-gnu-social.git] / actions / apiaccountregister.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Register 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  API
23  * @package   GNUsocial
24  * @author    Hannes Mannerheim <h@nnesmannerhe.im>
25  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
26  * @link      http://www.gnu.org/software/social/
27  */
28
29 if (!defined('GNUSOCIAL')) { exit(1); }
30
31 class ApiAccountRegisterAction extends ApiAction
32 {
33
34     /**
35      * Has there been an error?
36      */
37     var $error = null;
38
39     /**
40      * Have we registered?
41      */
42     var $registered = false;
43
44     protected $needPost = true;
45
46     protected $code   = null; // invite code
47     protected $invite = null; // invite to-be-stored
48
49     /**
50      * Take arguments for running
51      *
52      * @param array $args $_REQUEST args
53      *
54      * @return boolean success flag
55      */
56     protected function prepare(array $args=array())
57     {
58         parent::prepare($args);
59
60         if ($this->format !== 'json') {
61             $this->clientError('This method currently only serves JSON.', 415);
62         }
63
64         $this->code = $this->trimmed('code');
65
66         return true;
67     }
68
69     /**
70      * Handle the request
71      *
72      * @param array $args $_REQUEST data (unused)
73      *
74      * @return void
75      */
76     protected function handle()
77     {
78         parent::handle();
79
80         $nickname = $this->trimmed('nickname');
81         $email    = $this->trimmed('email');
82         $fullname = $this->trimmed('fullname');
83         $homepage = $this->trimmed('homepage');
84         $bio      = $this->trimmed('bio');
85         $location = $this->trimmed('location');
86
87         // We don't trim these... whitespace is OK in a password!
88         $password = $this->arg('password');
89         $confirm  = $this->arg('confirm');
90
91         if (empty($this->code)) {
92             common_ensure_session();
93             if (array_key_exists('invitecode', $_SESSION)) {
94                 $this->code = $_SESSION['invitecode'];
95             }
96         }
97
98         if (common_config('site', 'inviteonly') && empty($this->code)) {
99             // TRANS: Client error displayed when trying to register to an invite-only site without an invitation.
100             $this->clientError(_('Sorry, only invited people can register.'), 401);
101         }
102
103         if (!empty($this->code)) {
104             $this->invite = Invitation::getKV('code', $this->code);
105             if (empty($this->invite)) {
106             // TRANS: Client error displayed when trying to register to an invite-only site without a valid invitation.
107                     $this->clientError(_('Sorry, invalid invitation code.'), 401);
108             }
109             // Store this in case we need it
110             common_ensure_session();
111             $_SESSION['invitecode'] = $this->code;
112         }
113
114         // Input scrubbing
115         try {
116             $nickname = Nickname::normalize($nickname, true);
117         } catch (NicknameException $e) {
118             // clientError handles Api exceptions with various formats and stuff
119                 $this->clientError($e->getMessage(), $e->getCode());
120         }
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->emailExists($email)) {
128             // TRANS: Form validation error displayed when trying to register with an already registered e-mail address.
129                 $this->clientError(_('Email address already exists.'), 400);
130         } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
131                    !common_valid_http_url($homepage)) {
132             // TRANS: Form validation error displayed when trying to register with an invalid homepage URL.
133                 $this->clientError(_('Homepage is not a valid URL.'), 400);
134         } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
135             // TRANS: Form validation error displayed when trying to register with a too long full name.
136                 $this->clientError(_('Full name is too long (maximum 255 characters).'), 400);
137         } else if (Profile::bioTooLong($bio)) {
138             // TRANS: Form validation error on registration page when providing too long a bio text.
139             // TRANS: %d is the maximum number of characters for bio; used for plural.
140                 $this->clientError(sprintf(_m('Bio is too long (maximum %d character).',
141                                        'Bio is too long (maximum %d characters).',
142                                        Profile::maxBio()),
143                                        Profile::maxBio()), 400);
144         } else if (!is_null($location) && mb_strlen($location) > 255) {
145             // TRANS: Form validation error displayed when trying to register with a too long location.
146                 $this->clientError(_('Location is too long (maximum 255 characters).'), 400);
147         } else if (strlen($password) < 6) {
148             // TRANS: Form validation error displayed when trying to register with too short a password.
149                 $this->clientError(_('Password must be 6 or more characters.'), 400);
150         } else if ($password != $confirm) {
151             // TRANS: Form validation error displayed when trying to register with non-matching passwords.
152                 $this->clientError(_('Passwords do not match.'), 400);
153         } else {
154
155             // annoy spammers
156             sleep(7);
157
158             try {
159                 $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                 Event::handle('EndRegistrationTry', array($this));
168
169                 $this->initDocument('json');
170                 $this->showJsonObjects($this->twitterUserArray($user->getProfile()));
171                 $this->endDocument('json');
172
173             } catch (Exception $e) {
174                 $this->clientError($e->getMessage(), 400);
175             }
176         }
177     }
178
179     /**
180      * Does the given email address already exist?
181      *
182      * Checks a canonical email address against the database.
183      *
184      * @param string $email email address to check
185      *
186      * @return boolean true if the address already exists
187      */
188     function emailExists($email)
189     {
190         $email = common_canonical_email($email);
191         if (!$email || strlen($email) == 0) {
192             return false;
193         }
194         $user = User::getKV('email', $email);
195         return is_object($user);
196     }
197
198 }