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(array $args=array())
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::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);
110 // Store this in case we need it
111 common_ensure_session();
112 $_SESSION['invitecode'] = $this->code;
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());
123 $email = common_canonical_email($email);
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).',
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);
159 if ($user = User::register(array('nickname' => $nickname,
160 'password' => $password,
162 'fullname' => $fullname,
163 'homepage' => $homepage,
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);
172 Event::handle('EndRegistrationTry', array($this));
174 $this->initDocument('json');
175 $this->showJsonObjects($this->twitterUserArray($user->getProfile()));
176 $this->endDocument('json');
179 // TRANS: Form validation error displayed when trying to register with an invalid username or password.
180 $this->clientError(_('Invalid username or password.'), 400);
186 * Does the given email address already exist?
188 * Checks a canonical email address against the database.
190 * @param string $email email address to check
192 * @return boolean true if the address already exists
194 function emailExists($email)
196 $email = common_canonical_email($email);
197 if (!$email || strlen($email) == 0) {
200 $user = User::getKV('email', $email);
201 return is_object($user);