apistatusesshow.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Show a notice (as a Twitter-style status)
  18. *
  19. * @category API
  20. * @package GNUsocial
  21. * @author Craig Andrews <candrews@integralblue.com>
  22. * @author Evan Prodromou <evan@status.net>
  23. * @author Jeffery To <jeffery.to@gmail.com>
  24. * @author Tom Blankenship <mac65@mac65.com>
  25. * @author Mike Cochrane <mikec@mikenz.geek.nz>
  26. * @author Robin Millette <robin@millette.info>
  27. * @author Zach Copley <zach@status.net>
  28. * @copyright 2009 StatusNet, Inc.
  29. * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
  30. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  31. */
  32. defined('GNUSOCIAL') || die();
  33. /**
  34. * Returns the notice specified by id as a Twitter-style status and inline user
  35. *
  36. * @category API
  37. * @package GNUsocial
  38. * @author Craig Andrews <candrews@integralblue.com>
  39. * @author Evan Prodromou <evan@status.net>
  40. * @author Jeffery To <jeffery.to@gmail.com>
  41. * @author Tom Blankenship <mac65@mac65.com>
  42. * @author Mike Cochrane <mikec@mikenz.geek.nz>
  43. * @author Robin Millette <robin@millette.info>
  44. * @author Zach Copley <zach@status.net>
  45. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  46. */
  47. class ApiStatusesShowAction extends ApiPrivateAuthAction
  48. {
  49. public $notice_id = null;
  50. public $notice = null;
  51. /**
  52. * Take arguments for running
  53. *
  54. * @param array $args $_REQUEST args
  55. *
  56. * @return boolean success flag
  57. */
  58. protected function prepare(array $args=array())
  59. {
  60. parent::prepare($args);
  61. // 'id' is an undocumented parameter in Twitter's API. Several
  62. // clients make use of it, so we support it too.
  63. // show.json?id=12345 takes precedence over /show/12345.json
  64. $this->notice_id = (int)$this->trimmed('id');
  65. $this->notice = null;
  66. try {
  67. $this->notice = Notice::getByID($this->notice_id);
  68. } catch (NoResultException $e) {
  69. // No such notice was found, maybe it was deleted?
  70. $deleted = null;
  71. Event::handle('IsNoticeDeleted', array($this->notice_id, &$deleted));
  72. if ($deleted === true) {
  73. // TRANS: Client error displayed trying to show a deleted notice.
  74. throw new ClientException(_('Notice deleted.'), 410);
  75. }
  76. // TRANS: Client error displayed trying to show a non-existing notice.
  77. throw new ClientException(_('No such notice.'), 404);
  78. }
  79. if (!$this->notice->inScope($this->scoped)) {
  80. // TRANS: Client exception thrown when trying a view a notice the user has no access to.
  81. throw new ClientException(_('Access restricted.'), 403);
  82. }
  83. return true;
  84. }
  85. /**
  86. * Handle the request
  87. *
  88. * Check the format and show the notice
  89. *
  90. * @return void
  91. */
  92. protected function handle()
  93. {
  94. parent::handle();
  95. if (!in_array($this->format, array('xml', 'json', 'atom'))) {
  96. // TRANS: Client error displayed when coming across a non-supported API method.
  97. $this->clientError(_('API method not found.'), 404);
  98. }
  99. switch ($_SERVER['REQUEST_METHOD']) {
  100. case 'GET':
  101. $this->showNotice();
  102. break;
  103. case 'DELETE':
  104. $this->deleteNotice();
  105. break;
  106. default:
  107. // TRANS: Client error displayed calling an unsupported HTTP error in API status show.
  108. $this->clientError(_('HTTP method not supported.'), 405);
  109. }
  110. }
  111. /**
  112. * Show the notice
  113. *
  114. * @return void
  115. */
  116. public function showNotice()
  117. {
  118. switch ($this->format) {
  119. case 'xml':
  120. $this->showSingleXmlStatus($this->notice);
  121. break;
  122. case 'json':
  123. $this->show_single_json_status($this->notice);
  124. break;
  125. case 'atom':
  126. $this->showSingleAtomStatus($this->notice);
  127. break;
  128. default:
  129. // TRANS: Exception thrown requesting an unsupported notice output format.
  130. // TRANS: %s is the requested output format.
  131. throw new Exception(sprintf(_("Unsupported format: %s."), $this->format));
  132. }
  133. }
  134. /**
  135. * We expose AtomPub here, so non-GET/HEAD reqs must be read/write.
  136. *
  137. * @param array $args other arguments
  138. *
  139. * @return boolean true
  140. */
  141. public function isReadOnly($args)
  142. {
  143. return in_array($_SERVER['REQUEST_METHOD'], ['GET', 'HEAD']);
  144. }
  145. /**
  146. * When was this notice last modified?
  147. *
  148. * @return string datestamp of the latest notice in the stream
  149. */
  150. public function lastModified()
  151. {
  152. return strtotime($this->notice->created);
  153. }
  154. /**
  155. * An entity tag for this notice
  156. *
  157. * Returns an Etag based on the action name, language, and
  158. * timestamps of the notice
  159. *
  160. * @return string etag
  161. */
  162. public function etag()
  163. {
  164. return '"' . implode(
  165. ':',
  166. array($this->arg('action'),
  167. common_user_cache_hash($this->auth_user),
  168. common_language(),
  169. $this->notice->id,
  170. strtotime($this->notice->created))
  171. )
  172. . '"';
  173. }
  174. public function deleteNotice()
  175. {
  176. if ($this->format != 'atom') {
  177. // TRANS: Client error displayed when trying to delete a notice not using the Atom format.
  178. $this->clientError(_('Can only delete using the Atom format.'));
  179. }
  180. if (empty($this->auth_user) ||
  181. ($this->notice->profile_id != $this->auth_user->id &&
  182. !$this->auth_user->hasRight(Right::DELETEOTHERSNOTICE))) {
  183. // TRANS: Client error displayed when a user has no rights to delete notices of other users.
  184. $this->clientError(_('Cannot delete this notice.'), 403);
  185. }
  186. if (Event::handle('StartDeleteOwnNotice', array($this->auth_user, $this->notice))) {
  187. $this->notice->deleteAs($this->scoped);
  188. Event::handle('EndDeleteOwnNotice', array($this->auth_user, $this->notice));
  189. }
  190. // @fixme is there better output we could do here?
  191. http_response_code(200);
  192. header('Content-Type: text/plain');
  193. // TRANS: Confirmation of notice deletion in API. %d is the ID (number) of the deleted notice.
  194. print(sprintf(_('Deleted notice %d'), $this->notice->id));
  195. print("\n");
  196. }
  197. }