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;
46 * Are we processing an invite?
51 * Take arguments for running
53 * @param array $args $_REQUEST args
55 * @return boolean success flag
57 function prepare($args)
59 parent::prepare($args);
60 $this->code = $this->trimmed('code');
62 if (empty($this->code)) {
63 common_ensure_session();
64 if (array_key_exists('invitecode', $_SESSION)) {
65 $this->code = $_SESSION['invitecode'];
69 if (common_config('site', 'inviteonly') && empty($this->code)) {
70 // TRANS: Client error displayed when trying to register to an invite-only site without an invitation.
71 $this->clientError(_('Sorry, only invited people can register.'),404,'json');
75 if (!empty($this->code)) {
76 $this->invite = Invitation::staticGet('code', $this->code);
77 if (empty($this->invite)) {
78 // TRANS: Client error displayed when trying to register to an invite-only site without a valid invitation.
79 $this->clientError(_('Sorry, invalid invitation code.'),404,'json');
82 // Store this in case we need it
83 common_ensure_session();
84 $_SESSION['invitecode'] = $this->code;
93 * @param array $args $_REQUEST data (unused)
97 function handle($args)
99 parent::handle($args);
101 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
103 _('This method requires a POST.'),
110 $nickname = $this->trimmed('nickname');
111 $email = $this->trimmed('email');
112 $fullname = $this->trimmed('fullname');
113 $homepage = $this->trimmed('homepage');
114 $bio = $this->trimmed('bio');
115 $location = $this->trimmed('location');
117 // We don't trim these... whitespace is OK in a password!
118 $password = $this->arg('password');
119 $confirm = $this->arg('confirm');
121 // invitation code, if any
122 $code = $this->trimmed('code');
125 $invite = Invitation::staticGet($code);
128 if (common_config('site', 'inviteonly') && !($code && $invite)) {
129 // TRANS: Client error displayed when trying to register to an invite-only site without an invitation.
130 $this->clientError(_('Sorry, only invited people can register.'),404,'json');
136 $nickname = Nickname::normalize($nickname);
137 } catch (NicknameException $e) {
138 $this->showForm($e->getMessage());
140 $email = common_canonical_email($email);
142 if ($email && !Validate::email($email, common_config('email', 'check_domain'))) {
143 // TRANS: Form validation error displayed when trying to register without a valid e-mail address.
144 $this->clientError(_('Not a valid email address.'),404,'json');
145 } else if ($this->nicknameExists($nickname)) {
146 // TRANS: Form validation error displayed when trying to register with an existing nickname.
147 $this->clientError(_('Nickname already in use. Try another one.'),404,'json');
148 } else if (!User::allowed_nickname($nickname)) {
149 // TRANS: Form validation error displayed when trying to register with an invalid nickname.
150 $this->clientError(_('Not a valid nickname.'),404,'json');
151 } else if ($this->emailExists($email)) {
152 // TRANS: Form validation error displayed when trying to register with an already registered e-mail address.
153 $this->clientError(_('Email address already exists.'),404,'json');
154 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
155 !Validate::uri($homepage,
156 array('allowed_schemes' =>
157 array('http', 'https')))) {
158 // TRANS: Form validation error displayed when trying to register with an invalid homepage URL.
159 $this->clientError(_('Homepage is not a valid URL.'),404,'json');
161 } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
162 // TRANS: Form validation error displayed when trying to register with a too long full name.
163 $this->clientError(_('Full name is too long (maximum 255 characters).'),404,'json');
165 } else if (Profile::bioTooLong($bio)) {
166 // TRANS: Form validation error on registration page when providing too long a bio text.
167 // TRANS: %d is the maximum number of characters for bio; used for plural.
168 $this->clientError(sprintf(_m('Bio is too long (maximum %d character).',
169 'Bio is too long (maximum %d characters).',
171 Profile::maxBio()),404,'json');
173 } else if (!is_null($location) && mb_strlen($location) > 255) {
174 // TRANS: Form validation error displayed when trying to register with a too long location.
175 $this->clientError(_('Location is too long (maximum 255 characters).'),404,'json');
177 } else if (strlen($password) < 6) {
178 // TRANS: Form validation error displayed when trying to register with too short a password.
179 $this->clientError(_('Password must be 6 or more characters.'),404,'json');
181 } else if ($password != $confirm) {
182 // TRANS: Form validation error displayed when trying to register with non-matching passwords.
183 $this->clientError(_('Passwords do not match.'),404,'json');
189 if ($user = User::register(array('nickname' => $nickname,
190 'password' => $password,
192 'fullname' => $fullname,
193 'homepage' => $homepage,
195 'location' => $location,
198 // TRANS: Form validation error displayed when trying to register with an invalid username or password.
199 $this->clientError(_('Invalid username or password.'),404,'json');
203 Event::handle('EndRegistrationTry', array($this));
205 $this->initDocument('json');
206 $this->showJsonObjects($this->twitterUserArray($user->getProfile()));
207 $this->endDocument('json');
210 // TRANS: Form validation error displayed when trying to register with an invalid username or password.
211 $this->clientError(_('Invalid username or password.'),404,'json');
219 * Does the given nickname already exist?
221 * Checks a canonical nickname against the database.
223 * @param string $nickname nickname to check
225 * @return boolean true if the nickname already exists
227 function nicknameExists($nickname)
229 $user = User::staticGet('nickname', $nickname);
230 return is_object($user);
234 * Does the given email address already exist?
236 * Checks a canonical email address against the database.
238 * @param string $email email address to check
240 * @return boolean true if the address already exists
242 function emailExists($email)
244 $email = common_canonical_email($email);
245 if (!$email || strlen($email) == 0) {
248 $user = User::staticGet('email', $email);
249 return is_object($user);