The official, canonical postActiv repository. http://www.postactiv.com

apifriendshipsshow.php 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /* ============================================================================
  3. * Title: APIFriendshipsShow
  4. * Show information about the relationship between two users
  5. *
  6. * postActiv:
  7. * the micro-blogging software
  8. *
  9. * Copyright:
  10. * Copyright (C) 2016-2018, Maiyannah Bishop
  11. *
  12. * Derived from code copyright various sources:
  13. * o GNU Social (C) 2013-2016, Free Software Foundation, Inc
  14. * o StatusNet (C) 2008-2012, StatusNet, Inc
  15. * ----------------------------------------------------------------------------
  16. * License:
  17. * This program is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License as published by
  19. * the Free Software Foundation, either version 3 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. *
  30. * <https://www.gnu.org/licenses/agpl.html>
  31. * ----------------------------------------------------------------------------
  32. * About:
  33. * Show information about the relationship between two users
  34. *
  35. * PHP version:
  36. * Tested with PHP 7.0
  37. * ----------------------------------------------------------------------------
  38. * File Authors:
  39. * o Dan Moore <dan@moore.cx>
  40. * o Evan Prodromou
  41. * o Zach Copley
  42. * o Maiyannah Bishop <maiyannah.bishop@postactiv.com>
  43. *
  44. * Web:
  45. * o postActiv <http://www.postactiv.com>
  46. * o GNU social <https://www.gnu.org/s/social/>
  47. * ============================================================================
  48. */
  49. // This file is formatted so that it provides useful documentation output in
  50. // NaturalDocs. Please be considerate of this before changing formatting.
  51. if (!defined('POSTACTIV')) { exit(1); }
  52. /**
  53. * Outputs detailed information about the relationship between two users
  54. */
  55. class ApiFriendshipsShowAction extends ApiBareAuthAction
  56. {
  57. var $source = null;
  58. var $target = null;
  59. /**
  60. * Take arguments for running
  61. *
  62. * @param array $args $_REQUEST args
  63. *
  64. * @return boolean success flag
  65. */
  66. protected function prepare(array $args=array())
  67. {
  68. parent::prepare($args);
  69. $source_id = (int)$this->trimmed('source_id');
  70. $source_screen_name = $this->trimmed('source_screen_name');
  71. $target_id = (int)$this->trimmed('target_id');
  72. $target_screen_name = $this->trimmed('target_screen_name');
  73. if (!empty($source_id)) {
  74. $this->source = User::getKV($source_id);
  75. } elseif (!empty($source_screen_name)) {
  76. $this->source = User::getKV('nickname', $source_screen_name);
  77. } else {
  78. $this->source = $this->auth_user;
  79. }
  80. if (!empty($target_id)) {
  81. $this->target = User::getKV($target_id);
  82. } elseif (!empty($target_screen_name)) {
  83. $this->target = User::getKV('nickname', $target_screen_name);
  84. }
  85. return true;
  86. }
  87. /**
  88. * Determines whether this API resource requires auth. Overloaded to look
  89. * return true in case source_id and source_screen_name are both empty
  90. *
  91. * @return boolean true or false
  92. */
  93. function requiresAuth()
  94. {
  95. if (common_config('site', 'private')) {
  96. return true;
  97. }
  98. $source_id = $this->trimmed('source_id');
  99. $source_screen_name = $this->trimmed('source_screen_name');
  100. if (empty($source_id) && empty($source_screen_name)) {
  101. return true;
  102. }
  103. return false;
  104. }
  105. /**
  106. * Handle the request
  107. *
  108. * Check the format and show the user info
  109. *
  110. * @return void
  111. */
  112. protected function handle()
  113. {
  114. parent::handle();
  115. if (!in_array($this->format, array('xml', 'json'))) {
  116. // TRANS: Client error displayed when coming across a non-supported API method.
  117. $this->clientError(_('API method not found.'), 404);
  118. }
  119. if (empty($this->source)) {
  120. $this->clientError(
  121. // TRANS: Client error displayed when a source user could not be determined showing friendship.
  122. _('Could not determine source user.'),
  123. 404
  124. );
  125. }
  126. if (empty($this->target)) {
  127. $this->clientError(
  128. // TRANS: Client error displayed when a target user could not be determined showing friendship.
  129. _('Could not find target user.'),
  130. 404
  131. );
  132. }
  133. $result = $this->twitterRelationshipArray($this->source, $this->target);
  134. switch ($this->format) {
  135. case 'xml':
  136. $this->initDocument('xml');
  137. $this->showTwitterXmlRelationship($result[relationship]);
  138. $this->endDocument('xml');
  139. break;
  140. case 'json':
  141. $this->initDocument('json');
  142. print json_encode($result);
  143. $this->endDocument('json');
  144. break;
  145. default:
  146. break;
  147. }
  148. }
  149. /**
  150. * Return true if read only.
  151. *
  152. * MAY override
  153. *
  154. * @param array $args other arguments
  155. *
  156. * @return boolean is read only action?
  157. */
  158. function isReadOnly($args)
  159. {
  160. return true;
  161. }
  162. }
  163. // END OF FILE
  164. // ============================================================================
  165. ?>