atompubshowfavorite.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, StatusNet, Inc.
  5. *
  6. * Show a single favorite in Atom Activity Streams format
  7. *
  8. * PHP version 5
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @category AtomPub
  24. * @package StatusNet
  25. * @author Evan Prodromou <evan@status.net>
  26. * @copyright 2010 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. // This check helps protect against security problems;
  32. // your code file can't be executed directly from the web.
  33. exit(1);
  34. }
  35. /**
  36. * Show a single favorite in Atom Activity Streams format.
  37. *
  38. * Can also be used to delete a favorite.
  39. *
  40. * @category Action
  41. * @package StatusNet
  42. * @author Evan Prodromou <evan@status.net>
  43. * @copyright 2010 StatusNet, Inc.
  44. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  45. * @link http://status.net/
  46. */
  47. class AtompubshowfavoriteAction extends ApiAuthAction
  48. {
  49. private $_profile = null;
  50. private $_notice = null;
  51. private $_fave = null;
  52. /**
  53. * For initializing members of the class.
  54. *
  55. * @param array $args misc. arguments
  56. *
  57. * @return boolean true
  58. */
  59. protected function prepare(array $args=array())
  60. {
  61. parent::prepare($args);
  62. $profileId = $this->trimmed('profile');
  63. $noticeId = $this->trimmed('notice');
  64. $this->_profile = Profile::getKV('id', $profileId);
  65. if (empty($this->_profile)) {
  66. // TRANS: Client exception.
  67. throw new ClientException(_('No such profile.'), 404);
  68. }
  69. $this->_notice = Notice::getKV('id', $noticeId);
  70. if (empty($this->_notice)) {
  71. // TRANS: Client exception thrown when referencing a non-existing notice.
  72. throw new ClientException(_('No such notice.'), 404);
  73. }
  74. $this->_fave = Fave::pkeyGet(array('user_id' => $profileId,
  75. 'notice_id' => $noticeId));
  76. if (empty($this->_fave)) {
  77. // TRANS: Client exception thrown when referencing a non-existing favorite.
  78. throw new ClientException(_('No such favorite.'), 404);
  79. }
  80. return true;
  81. }
  82. /**
  83. * Handler method
  84. *
  85. * @return void
  86. */
  87. protected function handle()
  88. {
  89. parent::handle();
  90. switch ($_SERVER['REQUEST_METHOD']) {
  91. case 'GET':
  92. case 'HEAD':
  93. $this->showFave();
  94. break;
  95. case 'DELETE':
  96. $this->deleteFave();
  97. break;
  98. default:
  99. // TRANS: Client exception thrown using an unsupported HTTP method.
  100. throw new ClientException(_('HTTP method not supported.'), 405);
  101. }
  102. return true;
  103. }
  104. /**
  105. * Show a single favorite, in ActivityStreams format
  106. *
  107. * @return void
  108. */
  109. function showFave()
  110. {
  111. $activity = $this->_fave->asActivity();
  112. header('Content-Type: application/atom+xml; charset=utf-8');
  113. $this->startXML();
  114. $this->raw($activity->asString(true, true, true));
  115. $this->endXML();
  116. return;
  117. }
  118. /**
  119. * Delete the favorite
  120. *
  121. * @return void
  122. */
  123. function deleteFave()
  124. {
  125. if (empty($this->auth_user) ||
  126. $this->auth_user->id != $this->_profile->id) {
  127. // TRANS: Client exception thrown when trying to remove a favorite notice of another user.
  128. throw new ClientException(_("Cannot delete someone else's".
  129. " favorite."), 403);
  130. }
  131. $this->_fave->delete();
  132. return;
  133. }
  134. /**
  135. * Return true if read only.
  136. *
  137. * MAY override
  138. *
  139. * @param array $args other arguments
  140. *
  141. * @return boolean is read only action?
  142. */
  143. function isReadOnly($args)
  144. {
  145. if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
  146. $_SERVER['REQUEST_METHOD'] == 'HEAD') {
  147. return true;
  148. } else {
  149. return false;
  150. }
  151. }
  152. /**
  153. * Return last modified, if applicable.
  154. *
  155. * MAY override
  156. *
  157. * @return string last modified http header
  158. */
  159. function lastModified()
  160. {
  161. return max(strtotime($this->_profile->modified),
  162. strtotime($this->_notice->modified),
  163. strtotime($this->_fave->modified));
  164. }
  165. /**
  166. * Return etag, if applicable.
  167. *
  168. * MAY override
  169. *
  170. * @return string etag http header
  171. */
  172. function etag()
  173. {
  174. $mtime = strtotime($this->_fave->modified);
  175. return 'W/"' . implode(':', array('AtomPubShowFavorite',
  176. $this->_profile->id,
  177. $this->_notice->id,
  178. $mtime)) . '"';
  179. }
  180. /**
  181. * Does this require authentication?
  182. *
  183. * @return boolean true if delete, else false
  184. */
  185. function requiresAuth()
  186. {
  187. if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
  188. $_SERVER['REQUEST_METHOD'] == 'HEAD') {
  189. return false;
  190. } else {
  191. return true;
  192. }
  193. }
  194. }