atompubsubscriptionfeed.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, StatusNet, Inc.
  5. *
  6. * AtomPub subscription feed
  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 Cache
  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. * Subscription feed class for AtomPub
  33. *
  34. * Generates a list of the user's subscriptions
  35. *
  36. * @category AtomPub
  37. * @package StatusNet
  38. * @author Evan Prodromou <evan@status.net>
  39. * @copyright 2010 StatusNet, Inc.
  40. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  41. * @link http://status.net/
  42. */
  43. class AtompubsubscriptionfeedAction extends AtompubAction
  44. {
  45. private $_profile = null;
  46. private $_subscriptions = null;
  47. protected function atompubPrepare()
  48. {
  49. $subscriber = $this->trimmed('subscriber');
  50. $this->_profile = Profile::getKV('id', $subscriber);
  51. if (!$this->_profile instanceof Profile) {
  52. // TRANS: Client exception thrown when trying to display a subscription for a non-existing profile ID.
  53. // TRANS: %d is the non-existing profile ID number.
  54. throw new ClientException(sprintf(_('No such profile id: %d.'),
  55. $subscriber), 404);
  56. }
  57. $this->_subscriptions = Subscription::bySubscriber($this->_profile->id,
  58. $this->offset,
  59. $this->limit);
  60. return true;
  61. }
  62. protected function handleGet()
  63. {
  64. $this->showFeed();
  65. }
  66. protected function handlePost()
  67. {
  68. $this->addSubscription();
  69. }
  70. /**
  71. * Show the feed of subscriptions
  72. *
  73. * @return void
  74. */
  75. function showFeed()
  76. {
  77. header('Content-Type: application/atom+xml; charset=utf-8');
  78. $url = common_local_url('AtomPubSubscriptionFeed',
  79. array('subscriber' => $this->_profile->id));
  80. $feed = new Atom10Feed(true);
  81. $feed->addNamespace('activity',
  82. 'http://activitystrea.ms/spec/1.0/');
  83. $feed->addNamespace('poco',
  84. 'http://portablecontacts.net/spec/1.0');
  85. $feed->addNamespace('media',
  86. 'http://purl.org/syndication/atommedia');
  87. $feed->addNamespace('georss',
  88. 'http://www.georss.org/georss');
  89. $feed->id = $url;
  90. $feed->setUpdated('now');
  91. $feed->addAuthor($this->_profile->getBestName(),
  92. $this->_profile->getURI());
  93. // TRANS: Title for Atom subscription feed.
  94. // TRANS: %s is a user nickname.
  95. $feed->setTitle(sprintf(_("%s subscriptions"),
  96. $this->_profile->getBestName()));
  97. // TRANS: Subtitle for Atom subscription feed.
  98. // TRANS: %1$s is a user nickname, %s$s is the StatusNet sitename.
  99. $feed->setSubtitle(sprintf(_("People %1\$s has subscribed to on %2\$s"),
  100. $this->_profile->getBestName(),
  101. common_config('site', 'name')));
  102. $feed->addLink(common_local_url('subscriptions',
  103. array('nickname' =>
  104. $this->_profile->nickname)));
  105. $feed->addLink($url,
  106. array('rel' => 'self',
  107. 'type' => 'application/atom+xml'));
  108. // If there's more...
  109. if ($this->page > 1) {
  110. $feed->addLink($url,
  111. array('rel' => 'first',
  112. 'type' => 'application/atom+xml'));
  113. $feed->addLink(common_local_url('AtomPubSubscriptionFeed',
  114. array('subscriber' =>
  115. $this->_profile->id),
  116. array('page' =>
  117. $this->page - 1)),
  118. array('rel' => 'prev',
  119. 'type' => 'application/atom+xml'));
  120. }
  121. if ($this->_subscriptions->N > $this->count) {
  122. $feed->addLink(common_local_url('AtomPubSubscriptionFeed',
  123. array('subscriber' =>
  124. $this->_profile->id),
  125. array('page' =>
  126. $this->page + 1)),
  127. array('rel' => 'next',
  128. 'type' => 'application/atom+xml'));
  129. }
  130. $i = 0;
  131. // XXX: This is kind of inefficient
  132. while ($this->_subscriptions->fetch()) {
  133. // We get one more than needed; skip that one
  134. $i++;
  135. if ($i > $this->count) {
  136. break;
  137. }
  138. $act = $this->_subscriptions->asActivity();
  139. $feed->addEntryRaw($act->asString(false, false, false));
  140. }
  141. $this->raw($feed->getString());
  142. }
  143. /**
  144. * Add a new subscription
  145. *
  146. * Handling the POST method for AtomPub
  147. *
  148. * @return void
  149. */
  150. function addSubscription()
  151. {
  152. if (empty($this->auth_user) ||
  153. $this->auth_user->id != $this->_profile->id) {
  154. // TRANS: Client exception thrown when trying to subscribe another user.
  155. throw new ClientException(_("Cannot add someone else's".
  156. " subscription."), 403);
  157. }
  158. $xml = file_get_contents('php://input');
  159. $dom = DOMDocument::loadXML($xml);
  160. if ($dom->documentElement->namespaceURI != Activity::ATOM ||
  161. $dom->documentElement->localName != 'entry') {
  162. // TRANS: Client error displayed when not using an Atom entry.
  163. $this->clientError(_('Atom post must be an Atom entry.'));
  164. }
  165. $activity = new Activity($dom->documentElement);
  166. $sub = null;
  167. if (Event::handle('StartAtomPubNewActivity', array(&$activity))) {
  168. if ($activity->verb != ActivityVerb::FOLLOW) {
  169. // TRANS: Client error displayed when not using the follow verb.
  170. $this->clientError(_('Can only handle Follow activities.'));
  171. }
  172. $person = $activity->objects[0];
  173. if ($person->type != ActivityObject::PERSON) {
  174. // TRANS: Client exception thrown when subscribing to an object that is not a person.
  175. $this->clientError(_('Can only follow people.'));
  176. }
  177. // XXX: OStatus discovery (maybe)
  178. try {
  179. $profile = Profile::fromUri($person->id);
  180. } catch (UnknownUriException $e) {
  181. // TRANS: Client exception thrown when subscribing to a non-existing profile.
  182. // TRANS: %s is the unknown profile ID.
  183. $this->clientError(sprintf(_('Unknown profile %s.'), $person->id));
  184. }
  185. try {
  186. $sub = Subscription::start($this->_profile, $profile);
  187. } catch (AlreadyFulfilledException $e) {
  188. // 409 Conflict
  189. $this->clientError($e->getMessage(), 409);
  190. }
  191. Event::handle('EndAtomPubNewActivity', array($activity, $sub));
  192. }
  193. if (!empty($sub)) {
  194. $act = $sub->asActivity();
  195. header('Content-Type: application/atom+xml; charset=utf-8');
  196. header('Content-Location: ' . $act->selfLink);
  197. $this->startXML();
  198. $this->raw($act->asString(true, true, true));
  199. $this->endXML();
  200. }
  201. }
  202. }