apiexternalusershow.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. // the user might not exist in our db yet, try to use the Ostatus plugin
  53. // to get it in there
  54. $validate = new Validate();
  55. if ($validate->uri($profileurl)) {
  56. $ostatus_profile = Ostatus_profile::ensureProfileURL($profileurl);
  57. $local_profile = Profile::getKV('id',$ostatus_profile->profile_id);
  58. }
  59. // we can get urls of two types of urls (1) ://instance/nickname
  60. // (2) ://instance/user/1234
  61. //
  62. // in case (1) we have the problem that the html can be outdated,
  63. // i.e. the user can have changed her nickname. we also have no idea
  64. // if it is a multi or single user instance, which forces us to
  65. // guess the api root url.
  66. //
  67. // in case (2) we have another problem: we can't use that url to find
  68. // the local profile for the external user, we need url:s of type (2)
  69. // for that. so we have to try getting the nickname from the external
  70. // instance first
  71. // case (2)
  72. if(strstr($profileurl, '/user/')) {
  73. $external_user_id = substr($profileurl,strpos($profileurl,'/user/')+6);
  74. $external_instance_url = substr($profileurl,0,strpos($profileurl,'/user/')+1);
  75. if(!is_numeric($external_user_id)) {
  76. return true;
  77. }
  78. $external_profile = $this->getProfileFromExternalInstance($external_instance_url,$external_user_id);
  79. if(!isset($external_profile->statusnet_profile_url)) {
  80. return true;
  81. }
  82. $this->profile->external = $external_profile;
  83. $local_profile = Profile::getKV('profileurl',$external_profile->statusnet_profile_url);
  84. if(!$local_profile instanceof Profile) {
  85. return true;
  86. }
  87. $this->profile->local = $this->twitterUserArray($local_profile);
  88. return true;
  89. }
  90. // case (1)
  91. if(!isset($local_profile)) {
  92. $local_profile = Profile::getKV('profileurl',$profileurl);
  93. }
  94. if($local_profile instanceof Profile) {
  95. $this->profile->local = $this->twitterUserArray($local_profile);
  96. // if profile url is not ending with nickname, this is probably a single user instance
  97. if(!substr($local_profile->profileurl, -strlen($local_profile->nickname))===$local_profile->nickname) {
  98. $external_instance_url = $local_profile->profileurl;
  99. }
  100. // multi user instance
  101. else {
  102. $external_instance_url = substr($local_profile->profileurl, 0, strrpos($local_profile->profileurl, '/'));
  103. }
  104. $external_profile = $this->getProfileFromExternalInstance($external_instance_url,$local_profile->nickname);
  105. if(!isset($external_profile->statusnet_profile_url)) {
  106. return true;
  107. }
  108. $this->profile->external = $external_profile;
  109. return true;
  110. }
  111. return true;
  112. }
  113. /**
  114. * Handle the request
  115. *
  116. * Check the format and show the user info
  117. *
  118. * @param array $args $_REQUEST data (unused)
  119. *
  120. * @return void
  121. */
  122. protected function handle()
  123. {
  124. parent::handle();
  125. $this->initDocument('json');
  126. $this->showJsonObjects($this->profile);
  127. $this->endDocument('json');
  128. }
  129. /**
  130. * Return true if read only.
  131. *
  132. * MAY override
  133. *
  134. * @param array $args other arguments
  135. *
  136. * @return boolean is read only action?
  137. */
  138. function isReadOnly($args)
  139. {
  140. return true;
  141. }
  142. /**
  143. * Get profile from external instance
  144. *
  145. * @return null or profile object
  146. */
  147. function getProfileFromExternalInstance($instance_url,$user_id_or_nickname)
  148. {
  149. $apicall = $instance_url.'/api/users/show.json?id='.$user_id_or_nickname;
  150. $client = new HTTPClient();
  151. $response = $client->get($apicall);
  152. // json_decode returns null if it fails to decode
  153. return $response->isOk() ? json_decode($response->getBody()) : null;
  154. }
  155. }