atompubshowsubscription.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, StatusNet, Inc.
  5. *
  6. * Single subscription
  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('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
  31. /**
  32. * Show a single subscription
  33. *
  34. * @category AtomPub
  35. * @package StatusNet
  36. * @author Evan Prodromou <evan@status.net>
  37. * @copyright 2010 StatusNet, Inc.
  38. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  39. * @link http://status.net/
  40. */
  41. class AtompubshowsubscriptionAction extends AtompubAction
  42. {
  43. private $_subscriber = null;
  44. private $_subscribed = null;
  45. private $_subscription = null;
  46. protected function atompubPrepare()
  47. {
  48. $subscriberId = $this->trimmed('subscriber');
  49. $this->_subscriber = Profile::getKV('id', $subscriberId);
  50. if (!$this->_subscriber instanceof Profile) {
  51. // TRANS: Client exception thrown when trying to display a subscription for a non-existing profile ID.
  52. // TRANS: %d is the non-existing profile ID number.
  53. throw new ClientException(sprintf(_('No such profile id: %d.'),
  54. $subscriberId), 404);
  55. }
  56. $subscribedId = $this->trimmed('subscribed');
  57. $this->_subscribed = Profile::getKV('id', $subscribedId);
  58. if (!$this->_subscribed instanceof Profile) {
  59. // TRANS: Client exception thrown when trying to display a subscription for a non-existing profile ID.
  60. // TRANS: %d is the non-existing profile ID number.
  61. throw new ClientException(sprintf(_('No such profile id: %d.'),
  62. $subscribedId), 404);
  63. }
  64. $this->_subscription = Subscription::pkeyGet(array('subscriber' => $subscriberId,
  65. 'subscribed' => $subscribedId));
  66. if (!$this->_subscription instanceof Subscription) {
  67. // TRANS: Client exception thrown when trying to display a subscription for a non-subscribed profile ID.
  68. // TRANS: %1$d is the non-existing subscriber ID number, $2$d is the ID of the profile that was not subscribed to.
  69. $msg = sprintf(_('Profile %1$d not subscribed to profile %2$d.'),
  70. $subscriberId, $subscribedId);
  71. throw new ClientException($msg, 404);
  72. }
  73. return true;
  74. }
  75. protected function handleGet()
  76. {
  77. $this->showSubscription();
  78. }
  79. protected function handleDelete()
  80. {
  81. $this->deleteSubscription();
  82. }
  83. /**
  84. * Show the subscription in ActivityStreams Atom format.
  85. *
  86. * @return void
  87. */
  88. function showSubscription()
  89. {
  90. $activity = $this->_subscription->asActivity();
  91. header('Content-Type: application/atom+xml; charset=utf-8');
  92. $this->startXML();
  93. $this->raw($activity->asString(true, true, true));
  94. $this->endXML();
  95. }
  96. /**
  97. * Delete the subscription
  98. *
  99. * @return void
  100. */
  101. function deleteSubscription()
  102. {
  103. if (!$this->scoped instanceof Profile ||
  104. $this->scoped->id != $this->_subscriber->id) {
  105. // TRANS: Client exception thrown when trying to delete a subscription of another user.
  106. throw new ClientException(_("Cannot delete someone else's subscription."), 403);
  107. }
  108. Subscription::cancel($this->_subscriber, $this->_subscribed);
  109. }
  110. /**
  111. * Is this action read only?
  112. *
  113. * @param array $args other arguments
  114. *
  115. * @return boolean true
  116. */
  117. function isReadOnly($args)
  118. {
  119. if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
  120. return false;
  121. }
  122. return true;
  123. }
  124. /**
  125. * Return last modified, if applicable.
  126. *
  127. * @return string last modified http header
  128. */
  129. function lastModified()
  130. {
  131. return max(strtotime($this->_subscriber->modified),
  132. strtotime($this->_subscribed->modified),
  133. strtotime($this->_subscription->modified));
  134. }
  135. /**
  136. * Etag for this object
  137. *
  138. * @return string etag http header
  139. */
  140. function etag()
  141. {
  142. $mtime = strtotime($this->_subscription->modified);
  143. return 'W/"' . implode(':', array('AtomPubShowSubscription',
  144. $this->_subscriber->id,
  145. $this->_subscribed->id,
  146. $mtime)) . '"';
  147. }
  148. /**
  149. * Does this require authentication?
  150. *
  151. * @return boolean true if delete, else false
  152. */
  153. function requiresAuth()
  154. {
  155. if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
  156. return true;
  157. } else {
  158. return false;
  159. }
  160. }
  161. }