atompubshowmembership.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, StatusNet, Inc.
  5. *
  6. * Show a single membership as an Activity Streams entry
  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 (or delete) a single membership event as an ActivityStreams entry
  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 AtompubshowmembershipAction extends AtompubAction
  42. {
  43. private $_private = null;
  44. private $_group = null;
  45. private $_membership = null;
  46. protected function atompubPrepare()
  47. {
  48. $this->_profile = Profile::getKV('id', $this->trimmed('profile'));
  49. if (!$this->_profile instanceof Profile) {
  50. // TRANS: Client exception.
  51. throw new ClientException(_('No such profile.'), 404);
  52. }
  53. $this->_group = User_group::getKV('id', $this->trimmed('group'));
  54. if (!$this->_group instanceof User_group) {
  55. // TRANS: Client exception thrown when referencing a non-existing group.
  56. throw new ClientException(_('No such group.'), 404);
  57. }
  58. $kv = array('group_id' => $groupId,
  59. 'profile_id' => $this->_profile->id);
  60. $this->_membership = Group_member::pkeyGet($kv);
  61. if (!$this->_membership instanceof Group_member) {
  62. // TRANS: Client exception thrown when trying to show membership of a non-subscribed group
  63. throw new ClientException(_('Not a member.'), 404);
  64. }
  65. return true;
  66. }
  67. protected function handleGet() {
  68. return $this->showMembership();
  69. }
  70. protected function handleDelete() {
  71. return $this->deleteMembership();
  72. }
  73. /**
  74. * show a single membership
  75. *
  76. * @return void
  77. */
  78. function showMembership()
  79. {
  80. $activity = $this->_membership->asActivity();
  81. header('Content-Type: application/atom+xml; charset=utf-8');
  82. $this->startXML();
  83. $this->raw($activity->asString(true, true, true));
  84. $this->endXML();
  85. return;
  86. }
  87. /**
  88. * Delete the membership (leave the group)
  89. *
  90. * @return void
  91. */
  92. function deleteMembership()
  93. {
  94. if (empty($this->auth_user) ||
  95. $this->auth_user->id != $this->_profile->id) {
  96. // TRANS: Client exception thrown when deleting someone else's membership.
  97. throw new ClientException(_("Cannot delete someone else's".
  98. " membership."), 403);
  99. }
  100. $this->auth_user->leaveGroup($this->_group);
  101. return;
  102. }
  103. /**
  104. * Return last modified, if applicable.
  105. *
  106. * Because the representation depends on the profile and group,
  107. * our last modified value is the maximum of their mod time
  108. * with the actual membership's mod time.
  109. *
  110. * @return string last modified http header
  111. */
  112. function lastModified()
  113. {
  114. return max(strtotime($this->_profile->modified),
  115. strtotime($this->_group->modified),
  116. strtotime($this->_membership->modified));
  117. }
  118. /**
  119. * Return etag, if applicable.
  120. *
  121. * A "weak" Etag including the profile and group id as well as
  122. * the admin flag and ctime of the membership.
  123. *
  124. * @return string etag http header
  125. */
  126. function etag()
  127. {
  128. $ctime = strtotime($this->_membership->created);
  129. $adminflag = ($this->_membership->is_admin) ? 't' : 'f';
  130. return 'W/"' . implode(':', array('AtomPubShowMembership',
  131. $this->_profile->id,
  132. $this->_group->id,
  133. $adminflag,
  134. $ctime)) . '"';
  135. }
  136. }