3 * StatusNet, the distributed open-source microblogging tool
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.
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.
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/>.
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/
29 if (!defined('GNUSOCIAL')) { exit(1); }
31 class ApiAccountRegisterAction extends ApiAction
35 * Has there been an error?
42 var $registered = false;
44 protected $needPost = true;
46 protected $code = null; // invite code
47 protected $invite = null; // invite to-be-stored
50 * Take arguments for running
52 * @param array $args $_REQUEST args
54 * @return boolean success flag
56 protected function prepare(array $args=array())
58 parent::prepare($args);
60 if ($this->format !== 'json') {
61 $this->clientError('This method currently only serves JSON.', 415);
64 $this->code = $this->trimmed('code');
72 * @param array $args $_REQUEST data (unused)
76 protected function handle()
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');
87 // We don't trim these... whitespace is OK in a password!
88 $password = $this->arg('password');
89 $confirm = $this->arg('confirm');
91 if (empty($this->code)) {
92 common_ensure_session();
93 if (array_key_exists('invitecode', $_SESSION)) {
94 $this->code = $_SESSION['invitecode'];
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);
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);
109 // Store this in case we need it
110 common_ensure_session();
111 $_SESSION['invitecode'] = $this->code;
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());
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->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).',
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);
159 $user = User::register(array('nickname' => $nickname,
160 'password' => $password,
162 'fullname' => $fullname,
163 'homepage' => $homepage,
165 'location' => $location,
166 'code' => $this->code));
167 Event::handle('EndRegistrationTry', array($this));
169 $this->initDocument('json');
170 $this->showJsonObjects($this->twitterUserArray($user->getProfile()));
171 $this->endDocument('json');
173 } catch (Exception $e) {
174 $this->clientError($e->getMessage(), 400);
180 * Does the given email address already exist?
182 * Checks a canonical email address against the database.
184 * @param string $email email address to check
186 * @return boolean true if the address already exists
188 function emailExists($email)
190 $email = common_canonical_email($email);
191 if (!$email || strlen($email) == 0) {
194 $user = User::getKV('email', $email);
195 return is_object($user);