123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- <?php
- if (!defined('GNUSOCIAL')) { exit(1); }
- class ApiAccountRegisterAction extends ApiAction
- {
-
- var $error = null;
-
- var $registered = false;
- protected $needPost = true;
- protected $code = null;
- protected $invite = null;
-
- protected function prepare(array $args=array())
- {
- parent::prepare($args);
- if ($this->format !== 'json') {
- $this->clientError('This method currently only serves JSON.', 415);
- }
- $this->code = $this->trimmed('code');
- return true;
- }
-
- protected function handle()
- {
- parent::handle();
- $nickname = $this->trimmed('nickname');
- $email = $this->trimmed('email');
- $fullname = $this->trimmed('fullname');
- $homepage = $this->trimmed('homepage');
- $bio = $this->trimmed('bio');
- $location = $this->trimmed('location');
-
- $password = $this->arg('password');
- $confirm = $this->arg('confirm');
- if (empty($this->code)) {
- common_ensure_session();
- if (array_key_exists('invitecode', $_SESSION)) {
- $this->code = $_SESSION['invitecode'];
- }
- }
- if (common_config('site', 'inviteonly') && empty($this->code)) {
-
- $this->clientError(_('Sorry, only invited people can register.'), 401);
- }
- if (!empty($this->code)) {
- $this->invite = Invitation::getKV('code', $this->code);
- if (empty($this->invite)) {
-
- $this->clientError(_('Sorry, invalid invitation code.'), 401);
- }
-
- common_ensure_session();
- $_SESSION['invitecode'] = $this->code;
- }
-
- try {
- $nickname = Nickname::normalize($nickname, true);
- } catch (NicknameException $e) {
-
- $this->clientError($e->getMessage(), $e->getCode());
- }
- $email = common_canonical_email($email);
- if ($email && !Validate::email($email, common_config('email', 'check_domain'))) {
-
- $this->clientError(_('Not a valid email address.'), 400);
- } else if ($this->emailExists($email)) {
-
- $this->clientError(_('Email address already exists.'), 400);
- } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
- !common_valid_http_url($homepage)) {
-
- $this->clientError(_('Homepage is not a valid URL.'), 400);
- } else if (Profile::bioTooLong($bio)) {
-
-
- $this->clientError(sprintf(_m('Bio is too long (maximum %d character).',
- 'Bio is too long (maximum %d characters).',
- Profile::maxBio()),
- Profile::maxBio()), 400);
- } else if (strlen($password) < 6) {
-
- $this->clientError(_('Password must be 6 or more characters.'), 400);
- } else if ($password != $confirm) {
-
- $this->clientError(_('Passwords do not match.'), 400);
- } else {
-
- sleep(7);
-
- if (Event::handle('APIStartRegistrationTry', array($this))) {
- try {
- $user = User::register(array('nickname' => $nickname,
- 'password' => $password,
- 'email' => $email,
- 'fullname' => $fullname,
- 'homepage' => $homepage,
- 'bio' => $bio,
- 'location' => $location,
- 'code' => $this->code));
- Event::handle('EndRegistrationTry', array($this));
- $this->initDocument('json');
- $this->showJsonObjects($this->twitterUserArray($user->getProfile()));
- $this->endDocument('json');
- } catch (Exception $e) {
- $this->clientError($e->getMessage(), 400);
- }
- }
- }
- }
-
- function emailExists($email)
- {
- $email = common_canonical_email($email);
- if (!$email || strlen($email) == 0) {
- return false;
- }
- $user = User::getKV('email', $email);
- return is_object($user);
- }
- }
|