apistatusesshow.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 = Notice::getKV('id', $this->notice_id);
  71. if (!$this->notice instanceof Notice) {
  72. $deleted = Deleted_notice::getKV('id', $this->notice_id);
  73. if ($deleted instanceof Deleted_notice) {
  74. // TRANS: Client error displayed trying to show a deleted notice.
  75. $this->clientError(_('Notice deleted.'), 410);
  76. }
  77. // TRANS: Client error displayed trying to show a non-existing notice.
  78. $this->clientError(_('No such notice.'), 404);
  79. }
  80. if (!$this->notice->inScope($this->scoped)) {
  81. // TRANS: Client exception thrown when trying a view a notice the user has no access to.
  82. throw new ClientException(_('Access restricted.'), 403);
  83. }
  84. return true;
  85. }
  86. /**
  87. * Handle the request
  88. *
  89. * Check the format and show the notice
  90. *
  91. * @return void
  92. */
  93. protected function handle()
  94. {
  95. parent::handle();
  96. if (!in_array($this->format, array('xml', 'json', 'atom'))) {
  97. // TRANS: Client error displayed when coming across a non-supported API method.
  98. $this->clientError(_('API method not found.'), 404);
  99. }
  100. switch ($_SERVER['REQUEST_METHOD']) {
  101. case 'GET':
  102. $this->showNotice();
  103. break;
  104. case 'DELETE':
  105. $this->deleteNotice();
  106. break;
  107. default:
  108. // TRANS: Client error displayed calling an unsupported HTTP error in API status show.
  109. $this->clientError(_('HTTP method not supported.'), 405);
  110. }
  111. }
  112. /**
  113. * Show the notice
  114. *
  115. * @return void
  116. */
  117. function showNotice()
  118. {
  119. if (!empty($this->notice)) {
  120. switch ($this->format) {
  121. case 'xml':
  122. $this->showSingleXmlStatus($this->notice);
  123. break;
  124. case 'json':
  125. $this->show_single_json_status($this->notice);
  126. break;
  127. case 'atom':
  128. $this->showSingleAtomStatus($this->notice);
  129. break;
  130. default:
  131. // TRANS: Exception thrown requesting an unsupported notice output format.
  132. // TRANS: %s is the requested output format.
  133. throw new Exception(sprintf(_("Unsupported format: %s."), $this->format));
  134. }
  135. } else {
  136. // XXX: Twitter just sets a 404 header and doens't bother
  137. // to return an err msg
  138. $deleted = Deleted_notice::getKV($this->notice_id);
  139. if (!empty($deleted)) {
  140. $this->clientError(
  141. // TRANS: Client error displayed requesting a deleted status.
  142. _('Status deleted.'),
  143. 410,
  144. $this->format
  145. );
  146. } else {
  147. $this->clientError(
  148. // TRANS: Client error displayed requesting a status with an invalid ID.
  149. _('No status with that ID found.'),
  150. 404,
  151. $this->format
  152. );
  153. }
  154. }
  155. }
  156. /**
  157. * We expose AtomPub here, so non-GET/HEAD reqs must be read/write.
  158. *
  159. * @param array $args other arguments
  160. *
  161. * @return boolean true
  162. */
  163. function isReadOnly($args)
  164. {
  165. return ($_SERVER['REQUEST_METHOD'] == 'GET' || $_SERVER['REQUEST_METHOD'] == 'HEAD');
  166. }
  167. /**
  168. * When was this notice last modified?
  169. *
  170. * @return string datestamp of the latest notice in the stream
  171. */
  172. function lastModified()
  173. {
  174. if (!empty($this->notice)) {
  175. return strtotime($this->notice->created);
  176. }
  177. return null;
  178. }
  179. /**
  180. * An entity tag for this notice
  181. *
  182. * Returns an Etag based on the action name, language, and
  183. * timestamps of the notice
  184. *
  185. * @return string etag
  186. */
  187. function etag()
  188. {
  189. if (!empty($this->notice)) {
  190. return '"' . implode(
  191. ':',
  192. array($this->arg('action'),
  193. common_user_cache_hash($this->auth_user),
  194. common_language(),
  195. $this->notice->id,
  196. strtotime($this->notice->created))
  197. )
  198. . '"';
  199. }
  200. return null;
  201. }
  202. function deleteNotice()
  203. {
  204. if ($this->format != 'atom') {
  205. // TRANS: Client error displayed when trying to delete a notice not using the Atom format.
  206. $this->clientError(_('Can only delete using the Atom format.'));
  207. }
  208. if (empty($this->auth_user) ||
  209. ($this->notice->profile_id != $this->auth_user->id &&
  210. !$this->auth_user->hasRight(Right::DELETEOTHERSNOTICE))) {
  211. // TRANS: Client error displayed when a user has no rights to delete notices of other users.
  212. $this->clientError(_('Cannot delete this notice.'), 403);
  213. }
  214. if (Event::handle('StartDeleteOwnNotice', array($this->auth_user, $this->notice))) {
  215. $this->notice->delete();
  216. Event::handle('EndDeleteOwnNotice', array($this->auth_user, $this->notice));
  217. }
  218. // @fixme is there better output we could do here?
  219. header('HTTP/1.1 200 OK');
  220. header('Content-Type: text/plain');
  221. // TRANS: Confirmation of notice deletion in API. %d is the ID (number) of the deleted notice.
  222. print(sprintf(_('Deleted notice %d'), $this->notice->id));
  223. print("\n");
  224. }
  225. }