apiaccountregister.php 6.74 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?php
/**
 * StatusNet, the distributed open-source microblogging tool
 *
 * Register account
 *
 * PHP version 5
 *
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @category  API
mmn's avatar
mmn committed
23
 * @package   GNUsocial
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
 * @author    Hannes Mannerheim <h@nnesmannerhe.im>
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
 * @link      http://www.gnu.org/software/social/
 */

if (!defined('GNUSOCIAL')) { exit(1); }

class ApiAccountRegisterAction extends ApiAction
{

    /**
     * Has there been an error?
     */
    var $error = null;

    /**
     * Have we registered?
     */
    var $registered = false;

44 45 46 47
    protected $needPost = true;

    protected $code   = null; // invite code
    protected $invite = null; // invite to-be-stored
48 49 50 51 52 53 54 55

    /**
     * Take arguments for running
     *
     * @param array $args $_REQUEST args
     *
     * @return boolean success flag
     */
56
    protected function prepare(array $args=array())
57 58
    {
        parent::prepare($args);
59 60 61 62 63

        if ($this->format !== 'json') {
            $this->clientError('This method currently only serves JSON.', 415);
        }

64 65
        $this->code = $this->trimmed('code');

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
        return true;
    }

    /**
     * Handle the request
     *
     * @param array $args $_REQUEST data (unused)
     *
     * @return void
     */
    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');

        // We don't trim these... whitespace is OK in a password!
        $password = $this->arg('password');
        $confirm  = $this->arg('confirm');

91 92 93 94 95 96 97 98 99
        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)) {
            // 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);
101 102 103
        }

        if (!empty($this->code)) {
104
            $this->invite = Invitation::getKV('code', $this->code);
105 106
            if (empty($this->invite)) {
            // 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);
108 109 110 111 112 113
            }
            // Store this in case we need it
            common_ensure_session();
            $_SESSION['invitecode'] = $this->code;
        }

114 115
        // Input scrubbing
        try {
116
            $nickname = Nickname::normalize($nickname, true);
117 118
        } catch (NicknameException $e) {
            // clientError handles Api exceptions with various formats and stuff
119
	        $this->clientError($e->getMessage(), $e->getCode());
120
        }
121

122
        $email = common_canonical_email($email);
123

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
	    if ($email && !Validate::email($email, common_config('email', 'check_domain'))) {
            // TRANS: Form validation error displayed when trying to register without a valid e-mail address.
	        $this->clientError(_('Not a valid email address.'), 400);
        } else if ($this->emailExists($email)) {
            // TRANS: Form validation error displayed when trying to register with an already registered e-mail address.
	        $this->clientError(_('Email address already exists.'), 400);
        } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
                   !common_valid_http_url($homepage)) {
            // TRANS: Form validation error displayed when trying to register with an invalid homepage URL.
	        $this->clientError(_('Homepage is not a valid URL.'), 400);
        } else if (Profile::bioTooLong($bio)) {
            // TRANS: Form validation error on registration page when providing too long a bio text.
            // TRANS: %d is the maximum number of characters for bio; used for plural.
	        $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) {
            // TRANS: Form validation error displayed when trying to register with too short a password.
	        $this->clientError(_('Password must be 6 or more characters.'), 400);
        } else if ($password != $confirm) {
            // TRANS: Form validation error displayed when trying to register with non-matching passwords.
	        $this->clientError(_('Passwords do not match.'), 400);
147
        } else {
148 149 150

            // annoy spammers
            sleep(7);
hannes's avatar
hannes committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
            
			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);
				}			
			}
172
        }
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    }

    /**
     * Does the given email address already exist?
     *
     * Checks a canonical email address against the database.
     *
     * @param string $email email address to check
     *
     * @return boolean true if the address already exists
     */
    function emailExists($email)
    {
        $email = common_canonical_email($email);
        if (!$email || strlen($email) == 0) {
            return false;
        }
190
        $user = User::getKV('email', $email);
191 192 193
        return is_object($user);
    }

194
}