4 * StatusNet, the distributed open-source microblogging tool
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.
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.
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/>.
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/
30 if (!defined('GNUSOCIAL')) { exit(1); }
32 class ApiAccountRegisterAction extends ApiAction
36 * Has there been an error?
43 var $registered = false;
45 protected $needPost = true;
47 protected $code = null; // invite code
48 protected $invite = null; // invite to-be-stored
51 * Take arguments for running
53 * @param array $args $_REQUEST args
55 * @return boolean success flag
57 protected function prepare($args)
59 parent::prepare($args);
61 if ($this->format !== 'json') {
62 $this->clientError('This method currently only serves JSON.', 415);
65 $this->code = $this->trimmed('code');
73 * @param array $args $_REQUEST data (unused)
77 protected function handle()
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');
88 // We don't trim these... whitespace is OK in a password!
89 $password = $this->arg('password');
90 $confirm = $this->arg('confirm');
92 if (empty($this->code)) {
93 common_ensure_session();
94 if (array_key_exists('invitecode', $_SESSION)) {
95 $this->code = $_SESSION['invitecode'];
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);
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);
110 // Store this in case we need it
111 common_ensure_session();
112 $_SESSION['invitecode'] = $this->code;
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);
122 $email = common_canonical_email($email);
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).',
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);
164 if ($user = User::register(array('nickname' => $nickname,
165 'password' => $password,
167 'fullname' => $fullname,
168 'homepage' => $homepage,
170 'location' => $location,
173 // TRANS: Form validation error displayed when trying to register with an invalid username or password.
174 $this->clientError(_('Invalid username or password.'), 400);
177 Event::handle('EndRegistrationTry', array($this));
179 $this->initDocument('json');
180 $this->showJsonObjects($this->twitterUserArray($user->getProfile()));
181 $this->endDocument('json');
184 // TRANS: Form validation error displayed when trying to register with an invalid username or password.
185 $this->clientError(_('Invalid username or password.'), 400);
192 * Does the given nickname already exist?
194 * Checks a canonical nickname against the database.
196 * @param string $nickname nickname to check
198 * @return boolean true if the nickname already exists
200 function nicknameExists($nickname)
202 $user = User::staticGet('nickname', $nickname);
203 return is_object($user);
207 * Does the given email address already exist?
209 * Checks a canonical email address against the database.
211 * @param string $email email address to check
213 * @return boolean true if the address already exists
215 function emailExists($email)
217 $email = common_canonical_email($email);
218 if (!$email || strlen($email) == 0) {
221 $user = User::staticGet('email', $email);
222 return is_object($user);