apiexternalusershow.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Show an external user's profile information
  6. *
  7. * PHP version 5
  8. *
  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.
  13. *
  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.
  18. *
  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/>.
  21. *
  22. * @category API
  23. * @package GNUsocial
  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/
  27. */
  28. if (!defined('GNUSOCIAL')) { exit(1); }
  29. /**
  30. * Ouputs information for a user, specified by ID or screen name.
  31. * The user's most recent status will be returned inline.
  32. */
  33. class ApiExternalUserShowAction extends ApiPrivateAuthAction
  34. {
  35. /**
  36. * Take arguments for running
  37. *
  38. * @param array $args $_REQUEST args
  39. *
  40. * @return boolean success flag
  41. *
  42. */
  43. protected function prepare(array $args=array())
  44. {
  45. parent::prepare($args);
  46. $this->format = 'json';
  47. $profileurl = urldecode($this->arg('profileurl'));
  48. $nickname = urldecode($this->arg('nickname'));
  49. $this->profile = new stdClass();
  50. $this->profile->external = null;
  51. $this->profile->local = null;
  52. // we can get urls of two types of urls (1) ://instance/nickname
  53. // (2) ://instance/user/1234
  54. //
  55. // in case (1) we have the problem that the html can be outdated,
  56. // i.e. the user can have changed her nickname. we also have no idea
  57. // if it is a multi or single user instance, which forces us to
  58. // guess the api root url.
  59. //
  60. // in case (2) we have another problem: we can't use that url to find
  61. // the local profile for the external user, we need url:s of type (2)
  62. // for that. so we have to try getting the nickname from the external
  63. // instance first
  64. // case (2)
  65. if(strstr($profileurl, '/user/')) {
  66. $external_user_id = substr($profileurl,strpos($profileurl,'/user/')+6);
  67. $external_instance_url = substr($profileurl,0,strpos($profileurl,'/user/')+1);
  68. if(!is_numeric($external_user_id)) {
  69. return true;
  70. }
  71. $external_profile = $this->getProfileFromExternalInstance($external_instance_url,$external_user_id);
  72. if(!isset($external_profile->statusnet_profile_url)) {
  73. return true;
  74. }
  75. $this->profile->external = $external_profile;
  76. $local_profile = Profile::getKV('profileurl',$external_profile->statusnet_profile_url);
  77. if(!$local_profile instanceof Profile) {
  78. return true;
  79. }
  80. $this->profile->local = $this->twitterUserArray($local_profile);
  81. return true;
  82. }
  83. // case (1)
  84. $local_profile = Profile::getKV('profileurl',$profileurl);
  85. if($local_profile instanceof Profile) {
  86. // if profile url is not ending with nickname, this is probably a single user instance
  87. if(!substr($local_profile->profileurl, -strlen($local_profile->nickname))===$local_profile->nickname) {
  88. $external_instance_url = $local_profile->profileurl;
  89. }
  90. // multi user instance
  91. else {
  92. $external_instance_url = substr($local_profile->profileurl, 0, strrpos($local_profile->profileurl, '/'));
  93. }
  94. $external_profile = $this->getProfileFromExternalInstance($external_instance_url,$local_profile->nickname);
  95. if(!isset($external_profile->statusnet_profile_url)) {
  96. return true;
  97. }
  98. $this->profile->external = $external_profile;
  99. $this->profile->local = $this->twitterUserArray($local_profile);
  100. return true;
  101. }
  102. return true;
  103. }
  104. /**
  105. * Handle the request
  106. *
  107. * Check the format and show the user info
  108. *
  109. * @param array $args $_REQUEST data (unused)
  110. *
  111. * @return void
  112. */
  113. protected function handle()
  114. {
  115. parent::handle();
  116. if(is_null($this->profile->local) && is_null($this->profile->external)) {
  117. $this->clientError(_('List not found.'), 404);
  118. } else {
  119. $this->initDocument('json');
  120. $this->showJsonObjects($this->profile);
  121. $this->endDocument('json');
  122. }
  123. }
  124. /**
  125. * Return true if read only.
  126. *
  127. * MAY override
  128. *
  129. * @param array $args other arguments
  130. *
  131. * @return boolean is read only action?
  132. */
  133. function isReadOnly($args)
  134. {
  135. return true;
  136. }
  137. /**
  138. * Get profile from external instance
  139. *
  140. * @return null or profile object
  141. */
  142. function getProfileFromExternalInstance($instance_url,$user_id_or_nickname)
  143. {
  144. $apicall = $instance_url.'/api/users/show.json?id='.$user_id_or_nickname;
  145. $client = new HTTPClient();
  146. $response = $client->get($apicall);
  147. // json_decode returns null if it fails to decode
  148. return $response->isOk() ? json_decode($response->getBody()) : null;
  149. }
  150. }