apifriendshipsshow.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Show information about the relationship between two users
  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 StatusNet
  24. * @author Dan Moore <dan@moore.cx>
  25. * @author Evan Prodromou <evan@status.net>
  26. * @author Zach Copley <zach@status.net>
  27. * @copyright 2009 StatusNet, Inc.
  28. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  29. * @link http://status.net/
  30. */
  31. if (!defined('STATUSNET')) {
  32. exit(1);
  33. }
  34. /**
  35. * Outputs detailed information about the relationship between two users
  36. *
  37. * @category API
  38. * @package StatusNet
  39. * @author Dan Moore <dan@moore.cx>
  40. * @author Evan Prodromou <evan@status.net>
  41. * @author Zach Copley <zach@status.net>
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  43. * @link http://status.net/
  44. */
  45. class ApiFriendshipsShowAction extends ApiBareAuthAction
  46. {
  47. var $source = null;
  48. var $target = null;
  49. /**
  50. * Take arguments for running
  51. *
  52. * @param array $args $_REQUEST args
  53. *
  54. * @return boolean success flag
  55. */
  56. function prepare($args)
  57. {
  58. parent::prepare($args);
  59. $source_id = (int)$this->trimmed('source_id');
  60. $source_screen_name = $this->trimmed('source_screen_name');
  61. $target_id = (int)$this->trimmed('target_id');
  62. $target_screen_name = $this->trimmed('target_screen_name');
  63. if (!empty($source_id)) {
  64. $this->source = User::getKV($source_id);
  65. } elseif (!empty($source_screen_name)) {
  66. $this->source = User::getKV('nickname', $source_screen_name);
  67. } else {
  68. $this->source = $this->auth_user;
  69. }
  70. if (!empty($target_id)) {
  71. $this->target = User::getKV($target_id);
  72. } elseif (!empty($target_screen_name)) {
  73. $this->target = User::getKV('nickname', $target_screen_name);
  74. }
  75. return true;
  76. }
  77. /**
  78. * Determines whether this API resource requires auth. Overloaded to look
  79. * return true in case source_id and source_screen_name are both empty
  80. *
  81. * @return boolean true or false
  82. */
  83. function requiresAuth()
  84. {
  85. if (common_config('site', 'private')) {
  86. return true;
  87. }
  88. $source_id = $this->trimmed('source_id');
  89. $source_screen_name = $this->trimmed('source_screen_name');
  90. if (empty($source_id) && empty($source_screen_name)) {
  91. return true;
  92. }
  93. return false;
  94. }
  95. /**
  96. * Handle the request
  97. *
  98. * Check the format and show the user info
  99. *
  100. * @param array $args $_REQUEST data (unused)
  101. *
  102. * @return void
  103. */
  104. function handle($args)
  105. {
  106. parent::handle($args);
  107. if (!in_array($this->format, array('xml', 'json'))) {
  108. // TRANS: Client error displayed when coming across a non-supported API method.
  109. $this->clientError(_('API method not found.'), 404);
  110. }
  111. if (empty($this->source)) {
  112. $this->clientError(
  113. // TRANS: Client error displayed when a source user could not be determined showing friendship.
  114. _('Could not determine source user.'),
  115. 404
  116. );
  117. }
  118. if (empty($this->target)) {
  119. $this->clientError(
  120. // TRANS: Client error displayed when a target user could not be determined showing friendship.
  121. _('Could not find target user.'),
  122. 404
  123. );
  124. }
  125. $result = $this->twitterRelationshipArray($this->source, $this->target);
  126. switch ($this->format) {
  127. case 'xml':
  128. $this->initDocument('xml');
  129. $this->showTwitterXmlRelationship($result[relationship]);
  130. $this->endDocument('xml');
  131. break;
  132. case 'json':
  133. $this->initDocument('json');
  134. print json_encode($result);
  135. $this->endDocument('json');
  136. break;
  137. default:
  138. break;
  139. }
  140. }
  141. /**
  142. * Return true if read only.
  143. *
  144. * MAY override
  145. *
  146. * @param array $args other arguments
  147. *
  148. * @return boolean is read only action?
  149. */
  150. function isReadOnly($args)
  151. {
  152. return true;
  153. }
  154. }