apiexternalusershow.php 5.46 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 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
<?php
/**
 * StatusNet, the distributed open-source microblogging tool
 *
 * Show an external user's profile information
 *
 * 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
 * @package   GNUsocial
 * @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); }

/**
 * Ouputs information for a user, specified by ID or screen name.
 * The user's most recent status will be returned inline.
 */
class ApiExternalUserShowAction extends ApiPrivateAuthAction
{
    /**
     * Take arguments for running
     *
     * @param array $args $_REQUEST args
     *
     * @return boolean success flag
     *
     */
    protected function prepare(array $args=array())
    {
        parent::prepare($args);

49
        $this->format = 'json';
50

51 52 53
        $profileurl = urldecode($this->arg('profileurl'));
        $nickname = urldecode($this->arg('nickname'));

hannes's avatar
hannes committed
54
		$this->profile = new stdClass();
55
        $this->profile->external = null;
hannes's avatar
hannes committed
56
		$this->profile->local = null;
57 58 59 60 61 62 63 64

        // the user might not exist in our db yet, try to use the Ostatus plugin
        // to get it in there
        $validate = new Validate();
        if ($validate->uri($profileurl)) {
            $ostatus_profile = Ostatus_profile::ensureProfileURL($profileurl);
            $local_profile = Profile::getKV('id',$ostatus_profile->profile_id);
            }
hannes's avatar
hannes committed
65

66
		// we can get urls of two types of urls 	(1) ://instance/nickname
hannes's avatar
hannes committed
67
		//						    				(2) ://instance/user/1234
68 69 70
		//
		// in case (1) we have the problem that the html can be outdated,
		// i.e. the user can have changed her nickname. we also have no idea
hannes's avatar
hannes committed
71 72
		// if it is a multi or single user instance, which forces us to
		// guess the api root url.
73
		//
hannes's avatar
hannes committed
74
		// in case (2) we have another problem: we can't use that url to find
75 76 77 78 79
		// the local profile for the external user, we need url:s of type (2)
		// for that. so we have to try getting the nickname from the external
		// instance first


hannes's avatar
hannes committed
80
		// case (2)
81 82
		if(strstr($profileurl, '/user/')) {

83 84 85 86
			$external_user_id = substr($profileurl,strpos($profileurl,'/user/')+6);
			$external_instance_url = substr($profileurl,0,strpos($profileurl,'/user/')+1);

			if(!is_numeric($external_user_id)) {
hannes's avatar
hannes committed
87
				return true;
88 89 90 91 92
				}

			$external_profile = $this->getProfileFromExternalInstance($external_instance_url,$external_user_id);

			if(!isset($external_profile->statusnet_profile_url)) {
hannes's avatar
hannes committed
93
				return true;
94 95
				}

hannes's avatar
hannes committed
96
			$this->profile->external = $external_profile;
97 98 99
			$local_profile = Profile::getKV('profileurl',$external_profile->statusnet_profile_url);

			if(!$local_profile instanceof Profile) {
hannes's avatar
hannes committed
100 101
				return true;
				}
102 103 104

			$this->profile->local = $this->twitterUserArray($local_profile);
			return true;
105
			}
hannes's avatar
hannes committed
106

107
		// case (1)
108 109 110
        if(!isset($local_profile)) {
            $local_profile = Profile::getKV('profileurl',$profileurl);
            }
hannes's avatar
hannes committed
111

112
		if($local_profile instanceof Profile) {
hannes's avatar
hannes committed
113

hannes's avatar
hannes committed
114 115
            $this->profile->local = $this->twitterUserArray($local_profile);

hannes's avatar
hannes committed
116
			// if profile url is not ending with nickname, this is probably a single user instance
117 118
			if(!substr($local_profile->profileurl, -strlen($local_profile->nickname))===$local_profile->nickname) {
				$external_instance_url = $local_profile->profileurl;
hannes's avatar
hannes committed
119 120
				}
			// multi user instance
hannes's avatar
hannes committed
121 122 123
			else {
				$external_instance_url = substr($local_profile->profileurl, 0, strrpos($local_profile->profileurl, '/'));
				}
hannes's avatar
hannes committed
124

125 126 127
			$external_profile = $this->getProfileFromExternalInstance($external_instance_url,$local_profile->nickname);

			if(!isset($external_profile->statusnet_profile_url)) {
hannes's avatar
hannes committed
128 129
				return true;
				}
130 131 132

			$this->profile->external = $external_profile;
			return true;
133
			}
hannes's avatar
hannes committed
134 135 136

		return true;

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
    }

    /**
     * Handle the request
     *
     * Check the format and show the user info
     *
     * @param array $args $_REQUEST data (unused)
     *
     * @return void
     */
    protected function handle()
    {
        parent::handle();

152 153 154
        $this->initDocument('json');
    	$this->showJsonObjects($this->profile);
        $this->endDocument('json');
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    }

    /**
     * Return true if read only.
     *
     * MAY override
     *
     * @param array $args other arguments
     *
     * @return boolean is read only action?
     */
    function isReadOnly($args)
    {
        return true;
    }
hannes's avatar
hannes committed
170

171 172

    /**
173
     * Get profile from external instance
174
     *
175
     * @return null or profile object
176
     */
hannes's avatar
hannes committed
177
	function getProfileFromExternalInstance($instance_url,$user_id_or_nickname)
178
	{
hannes's avatar
hannes committed
179
		$apicall = $instance_url.'/api/users/show.json?id='.$user_id_or_nickname;
180 181 182
        $client = new HTTPClient();
        $response = $client->get($apicall);
        // json_decode returns null if it fails to decode
hannes's avatar
hannes committed
183
        return $response->isOk() ? json_decode($response->getBody()) : null;
184 185
	}

hannes's avatar
hannes committed
186

187
}