apistatusesshow.php 7.3 KB

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