123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- <?php
- if (!defined('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
- class AtompubmembershipfeedAction extends AtompubAction
- {
- private $_profile = null;
- private $_memberships = null;
- protected function atompubPrepare()
- {
- $this->_profile = Profile::getKV('id', $this->trimmed('profile'));
- if (!$this->_profile instanceof Profile) {
-
- throw new ClientException(_('No such profile.'), 404);
- }
- $this->_memberships = Group_member::byMember($this->_profile->id,
- $this->offset,
- $this->limit);
- return true;
- }
- protected function handleGet()
- {
- return $this->showFeed();
- }
- protected function handlePost()
- {
- return $this->addMembership();
- }
-
- function showFeed()
- {
- header('Content-Type: application/atom+xml; charset=utf-8');
- $url = common_local_url('AtomPubMembershipFeed',
- array('profile' => $this->_profile->id));
- $feed = new Atom10Feed(true);
- $feed->addNamespace('activity',
- 'http://activitystrea.ms/spec/1.0/');
- $feed->addNamespace('poco',
- 'http://portablecontacts.net/spec/1.0');
- $feed->addNamespace('media',
- 'http://purl.org/syndication/atommedia');
- $feed->id = $url;
- $feed->setUpdated('now');
- $feed->addAuthor($this->_profile->getBestName(),
- $this->_profile->getURI());
-
-
- $feed->setTitle(sprintf(_('Group memberships of %s'),
- $this->_profile->getBestName()));
-
-
- $feed->setSubtitle(sprintf(_('Groups %1$s is a member of on %2$s'),
- $this->_profile->getBestName(),
- common_config('site', 'name')));
- $feed->addLink(common_local_url('usergroups',
- array('nickname' =>
- $this->_profile->nickname)));
- $feed->addLink($url,
- array('rel' => 'self',
- 'type' => 'application/atom+xml'));
-
- if ($this->page > 1) {
- $feed->addLink($url,
- array('rel' => 'first',
- 'type' => 'application/atom+xml'));
- $feed->addLink(common_local_url('AtomPubMembershipFeed',
- array('profile' =>
- $this->_profile->id),
- array('page' =>
- $this->page - 1)),
- array('rel' => 'prev',
- 'type' => 'application/atom+xml'));
- }
- if ($this->_memberships->N > $this->count) {
- $feed->addLink(common_local_url('AtomPubMembershipFeed',
- array('profile' =>
- $this->_profile->id),
- array('page' =>
- $this->page + 1)),
- array('rel' => 'next',
- 'type' => 'application/atom+xml'));
- }
- $i = 0;
- while ($this->_memberships->fetch()) {
-
- $i++;
- if ($i > $this->count) {
- break;
- }
- $act = $this->_memberships->asActivity();
- $feed->addEntryRaw($act->asString(false, false, false));
- }
- $this->raw($feed->getString());
- }
-
- function addMembership()
- {
-
- if (empty($this->auth_user) ||
- $this->auth_user->id != $this->_profile->id) {
-
- throw new ClientException(_("Cannot add someone else's".
- " membership."), 403);
- }
- $xml = file_get_contents('php://input');
- $dom = DOMDocument::loadXML($xml);
- if ($dom->documentElement->namespaceURI != Activity::ATOM ||
- $dom->documentElement->localName != 'entry') {
-
- throw new ClientException(_('Atom post must be an Atom entry.'));
- return;
- }
- $activity = new Activity($dom->documentElement);
- $membership = null;
- if (Event::handle('StartAtomPubNewActivity', array(&$activity))) {
- if ($activity->verb != ActivityVerb::JOIN) {
-
- throw new ClientException(_('Can only handle join activities.'));
- }
- $groupObj = $activity->objects[0];
- if ($groupObj->type != ActivityObject::GROUP) {
-
- throw new ClientException(_('Can only join groups.'));
- }
- $group = User_group::getKV('uri', $groupObj->id);
- if (empty($group)) {
-
-
- throw new ClientException(_('Unknown group.'));
- }
- $old = Group_member::pkeyGet(array('profile_id' => $this->auth_user->id,
- 'group_id' => $group->id));
- if (!empty($old)) {
-
- throw new ClientException(_('Already a member.'));
- }
- $profile = $this->auth_user->getProfile();
- if (Group_block::isBlocked($group, $profile)) {
-
-
- throw new ClientException(_('Blocked by admin.'));
- }
- $this->auth_user->joinGroup($group);
- Event::handle('EndAtomPubNewActivity', array($activity, $membership));
- }
- if (!empty($membership)) {
- $act = $membership->asActivity();
- header('Content-Type: application/atom+xml; charset=utf-8');
- header('Content-Location: ' . $act->selfLink);
- $this->startXML();
- $this->raw($act->asString(true, true, true));
- $this->endXML();
- }
- }
-
- function lastModified()
- {
-
-
- return null;
- }
-
- function etag()
- {
- return null;
- }
- }
|