apifriendshipsshow.php 5.0 KB

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