apigrouplist.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Check to see whether a user a member of a group
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category API
  23. * @package StatusNet
  24. * @author Craig Andrews <candrews@integralblue.com>
  25. * @author Evan Prodromou <evan@status.net>
  26. * @author Jeffery To <jeffery.to@gmail.com>
  27. * @author Zach Copley <zach@status.net>
  28. * @copyright 2009 StatusNet, Inc.
  29. * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
  30. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  31. * @link http://status.net/
  32. */
  33. if (!defined('STATUSNET')) {
  34. exit(1);
  35. }
  36. /**
  37. * Returns whether a user is a member of a specified group.
  38. *
  39. * @category API
  40. * @package StatusNet
  41. * @author Craig Andrews <candrews@integralblue.com>
  42. * @author Evan Prodromou <evan@status.net>
  43. * @author Jeffery To <jeffery.to@gmail.com>
  44. * @author Zach Copley <zach@status.net>
  45. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  46. * @link http://status.net/
  47. */
  48. class ApiGroupListAction extends ApiBareAuthAction
  49. {
  50. var $groups = null;
  51. /**
  52. * Take arguments for running
  53. *
  54. * @param array $args $_REQUEST args
  55. *
  56. * @return boolean success flag
  57. */
  58. protected function prepare(array $args=array())
  59. {
  60. parent::prepare($args);
  61. //TODO: Make sure this doesn't leak unwantedly for federated users
  62. $this->target = $this->getTargetProfile(null);
  63. if (!($this->target instanceof Profile)) {
  64. // TRANS: Client error displayed when user not found for an action.
  65. $this->clientError(_('No such user.'), 404);
  66. }
  67. $this->groups = $this->getGroups();
  68. return true;
  69. }
  70. /**
  71. * Handle the request
  72. *
  73. * Show the user's groups
  74. *
  75. * @return void
  76. */
  77. protected function handle()
  78. {
  79. parent::handle();
  80. $sitename = common_config('site', 'name');
  81. // TRANS: Used as title in check for group membership. %s is a user name.
  82. $title = sprintf(_("%s's groups"), $this->target->nickname);
  83. $taguribase = TagURI::base();
  84. $id = "tag:$taguribase:Groups";
  85. $link = common_local_url(
  86. 'usergroups',
  87. array('nickname' => $this->target->nickname)
  88. );
  89. $subtitle = sprintf(
  90. // TRANS: Used as subtitle in check for group membership. %1$s is the site name, %2$s is a user name.
  91. _('%1$s groups %2$s is a member of.'),
  92. $sitename,
  93. $this->target->nickname
  94. );
  95. switch($this->format) {
  96. case 'xml':
  97. $this->showXmlGroups($this->groups);
  98. break;
  99. case 'rss':
  100. $this->showRssGroups($this->groups, $title, $link, $subtitle);
  101. break;
  102. case 'atom':
  103. $selfuri = common_local_url('ApiGroupList', array('id'=>$this->target->id, 'format'=>'atom'));
  104. $this->showAtomGroups(
  105. $this->groups,
  106. $title,
  107. $id,
  108. $link,
  109. $subtitle,
  110. $selfuri
  111. );
  112. break;
  113. case 'json':
  114. $this->showJsonGroups($this->groups);
  115. break;
  116. default:
  117. // TRANS: Client error displayed when coming across a non-supported API method.
  118. $this->clientError(_('API method not found.'), 404);
  119. }
  120. }
  121. /**
  122. * Get groups
  123. *
  124. * @return array groups
  125. */
  126. function getGroups()
  127. {
  128. $groups = array();
  129. $group = $this->target->getGroups(
  130. ($this->page - 1) * $this->count,
  131. $this->count,
  132. $this->since_id,
  133. $this->max_id
  134. );
  135. while ($group->fetch()) {
  136. $groups[] = clone($group);
  137. }
  138. return $groups;
  139. }
  140. /**
  141. * Is this action read only?
  142. *
  143. * @param array $args other arguments
  144. *
  145. * @return boolean true
  146. */
  147. function isReadOnly($args)
  148. {
  149. return true;
  150. }
  151. /**
  152. * When was this feed last modified?
  153. *
  154. * @return string datestamp of the latest group the user has joined
  155. */
  156. function lastModified()
  157. {
  158. if (!empty($this->groups) && (count($this->groups) > 0)) {
  159. return strtotime($this->groups[0]->created);
  160. }
  161. return null;
  162. }
  163. /**
  164. * An entity tag for this list of groups
  165. *
  166. * Returns an Etag based on the action name, language, user ID and
  167. * timestamps of the first and last group the user has joined
  168. *
  169. * @return string etag
  170. */
  171. function etag()
  172. {
  173. if (!empty($this->groups) && (count($this->groups) > 0)) {
  174. $last = count($this->groups) - 1;
  175. return '"' . implode(
  176. ':',
  177. array($this->arg('action'),
  178. common_user_cache_hash($this->auth_user),
  179. common_language(),
  180. $this->target->id,
  181. strtotime($this->groups[0]->created),
  182. strtotime($this->groups[$last]->created))
  183. )
  184. . '"';
  185. }
  186. return null;
  187. }
  188. }