atompubmembershipfeed.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, StatusNet, Inc.
  5. *
  6. * Feed of group memberships for a user, in ActivityStreams 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('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
  31. /**
  32. * Feed of group memberships for a user, in ActivityStreams format
  33. *
  34. * @category Action
  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 AtompubmembershipfeedAction extends AtompubAction
  42. {
  43. private $_profile = null;
  44. private $_memberships = null;
  45. protected function atompubPrepare()
  46. {
  47. $this->_profile = Profile::getKV('id', $this->trimmed('profile'));
  48. if (!$this->_profile instanceof Profile) {
  49. // TRANS: Client exception.
  50. throw new ClientException(_('No such profile.'), 404);
  51. }
  52. $this->_memberships = Group_member::byMember($this->_profile->id,
  53. $this->offset,
  54. $this->limit);
  55. return true;
  56. }
  57. protected function handleGet()
  58. {
  59. return $this->showFeed();
  60. }
  61. protected function handlePost()
  62. {
  63. return $this->addMembership();
  64. }
  65. /**
  66. * Show a feed of favorite activity streams objects
  67. *
  68. * @return void
  69. */
  70. function showFeed()
  71. {
  72. header('Content-Type: application/atom+xml; charset=utf-8');
  73. $url = common_local_url('AtomPubMembershipFeed',
  74. array('profile' => $this->_profile->id));
  75. $feed = new Atom10Feed(true);
  76. $feed->addNamespace('activity',
  77. 'http://activitystrea.ms/spec/1.0/');
  78. $feed->addNamespace('poco',
  79. 'http://portablecontacts.net/spec/1.0');
  80. $feed->addNamespace('media',
  81. 'http://purl.org/syndication/atommedia');
  82. $feed->id = $url;
  83. $feed->setUpdated('now');
  84. $feed->addAuthor($this->_profile->getBestName(),
  85. $this->_profile->getURI());
  86. // TRANS: Title for group membership feed.
  87. // TRANS: %s is a username.
  88. $feed->setTitle(sprintf(_('Group memberships of %s'),
  89. $this->_profile->getBestName()));
  90. // TRANS: Subtitle for group membership feed.
  91. // TRANS: %1$s is a username, %2$s is the StatusNet sitename.
  92. $feed->setSubtitle(sprintf(_('Groups %1$s is a member of on %2$s'),
  93. $this->_profile->getBestName(),
  94. common_config('site', 'name')));
  95. $feed->addLink(common_local_url('usergroups',
  96. array('nickname' =>
  97. $this->_profile->nickname)));
  98. $feed->addLink($url,
  99. array('rel' => 'self',
  100. 'type' => 'application/atom+xml'));
  101. // If there's more...
  102. if ($this->page > 1) {
  103. $feed->addLink($url,
  104. array('rel' => 'first',
  105. 'type' => 'application/atom+xml'));
  106. $feed->addLink(common_local_url('AtomPubMembershipFeed',
  107. array('profile' =>
  108. $this->_profile->id),
  109. array('page' =>
  110. $this->page - 1)),
  111. array('rel' => 'prev',
  112. 'type' => 'application/atom+xml'));
  113. }
  114. if ($this->_memberships->N > $this->count) {
  115. $feed->addLink(common_local_url('AtomPubMembershipFeed',
  116. array('profile' =>
  117. $this->_profile->id),
  118. array('page' =>
  119. $this->page + 1)),
  120. array('rel' => 'next',
  121. 'type' => 'application/atom+xml'));
  122. }
  123. $i = 0;
  124. while ($this->_memberships->fetch()) {
  125. // We get one more than needed; skip that one
  126. $i++;
  127. if ($i > $this->count) {
  128. break;
  129. }
  130. $act = $this->_memberships->asActivity();
  131. $feed->addEntryRaw($act->asString(false, false, false));
  132. }
  133. $this->raw($feed->getString());
  134. }
  135. /**
  136. * add a new favorite
  137. *
  138. * @return void
  139. */
  140. function addMembership()
  141. {
  142. // XXX: Refactor this; all the same for atompub
  143. if (empty($this->auth_user) ||
  144. $this->auth_user->id != $this->_profile->id) {
  145. // TRANS: Client exception thrown when trying subscribe someone else to a group.
  146. throw new ClientException(_("Cannot add someone else's".
  147. " membership."), 403);
  148. }
  149. $xml = file_get_contents('php://input');
  150. $dom = DOMDocument::loadXML($xml);
  151. if ($dom->documentElement->namespaceURI != Activity::ATOM ||
  152. $dom->documentElement->localName != 'entry') {
  153. // TRANS: Client error displayed when not using an Atom entry.
  154. throw new ClientException(_('Atom post must be an Atom entry.'));
  155. return;
  156. }
  157. $activity = new Activity($dom->documentElement);
  158. $membership = null;
  159. if (Event::handle('StartAtomPubNewActivity', array(&$activity))) {
  160. if ($activity->verb != ActivityVerb::JOIN) {
  161. // TRANS: Client error displayed when not using the join verb.
  162. throw new ClientException(_('Can only handle join activities.'));
  163. }
  164. $groupObj = $activity->objects[0];
  165. if ($groupObj->type != ActivityObject::GROUP) {
  166. // TRANS: Client exception thrown when trying to join something which is not a group
  167. throw new ClientException(_('Can only join groups.'));
  168. }
  169. $group = User_group::getKV('uri', $groupObj->id);
  170. if (empty($group)) {
  171. // XXX: import from listed URL or something
  172. // TRANS: Client exception thrown when trying to subscribe to a non-existing group.
  173. throw new ClientException(_('Unknown group.'));
  174. }
  175. $old = Group_member::pkeyGet(array('profile_id' => $this->auth_user->id,
  176. 'group_id' => $group->id));
  177. if (!empty($old)) {
  178. // TRANS: Client exception thrown when trying to subscribe to an already subscribed group.
  179. throw new ClientException(_('Already a member.'));
  180. }
  181. $profile = $this->auth_user->getProfile();
  182. if (Group_block::isBlocked($group, $profile)) {
  183. // XXX: import from listed URL or something
  184. // TRANS: Client exception thrown when trying to subscribe to group while blocked from that group.
  185. throw new ClientException(_('Blocked by admin.'));
  186. }
  187. $this->auth_user->joinGroup($group);
  188. Event::handle('EndAtomPubNewActivity', array($activity, $membership));
  189. }
  190. if (!empty($membership)) {
  191. $act = $membership->asActivity();
  192. header('Content-Type: application/atom+xml; charset=utf-8');
  193. header('Content-Location: ' . $act->selfLink);
  194. $this->startXML();
  195. $this->raw($act->asString(true, true, true));
  196. $this->endXML();
  197. }
  198. }
  199. /**
  200. * Return last modified, if applicable.
  201. *
  202. * MAY override
  203. *
  204. * @return string last modified http header
  205. */
  206. function lastModified()
  207. {
  208. // For comparison with If-Last-Modified
  209. // If not applicable, return null
  210. return null;
  211. }
  212. /**
  213. * Return etag, if applicable.
  214. *
  215. * MAY override
  216. *
  217. * @return string etag http header
  218. */
  219. function etag()
  220. {
  221. return null;
  222. }
  223. }