apilistusers.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Base class for list members and list subscribers api.
  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. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  25. * @link http://status.net/
  26. */
  27. if (!defined('STATUSNET')) {
  28. exit(1);
  29. }
  30. class ApiListUsersAction extends ApiBareAuthAction
  31. {
  32. var $list = null;
  33. var $user = false;
  34. var $create = false;
  35. var $delete = false;
  36. var $cursor = -1;
  37. var $next_cursor = 0;
  38. var $prev_cursor = 0;
  39. var $users = null;
  40. protected function prepare(array $args=array())
  41. {
  42. // delete list member if method is DELETE or if method is POST and an argument
  43. // _method is set to DELETE
  44. $this->delete = ($_SERVER['REQUEST_METHOD'] == 'DELETE' ||
  45. ($this->trimmed('_method') == 'DELETE' &&
  46. $_SERVER['REQUEST_METHOD'] == 'POST'));
  47. // add member if method is POST
  48. $this->create = (!$this->delete &&
  49. $_SERVER['REQUEST_METHOD'] == 'POST');
  50. if ($this->arg('id')) {
  51. $this->target = $this->getTargetProfile($this->arg('id'));
  52. }
  53. parent::prepare($args);
  54. $this->list = $this->getTargetList($this->arg('user'), $this->arg('list_id'));
  55. if (empty($this->list)) {
  56. // TRANS: Client error displayed when referring to a non-existing list.
  57. $this->clientError(_('List not found.'), 404, $this->format);
  58. }
  59. if(!$this->create && !$this->delete) {
  60. $this->getUsers();
  61. }
  62. return true;
  63. }
  64. function requiresAuth()
  65. {
  66. return parent::requiresAuth() ||
  67. $this->create || $this->delete;
  68. }
  69. protected function handle()
  70. {
  71. parent::handle();
  72. if($this->delete) {
  73. return $this->handleDelete();
  74. }
  75. if($this->create) {
  76. return $this->handlePost();
  77. }
  78. switch($this->format) {
  79. case 'xml':
  80. $this->initDocument('xml');
  81. $this->elementStart('users_list', array('xmlns:statusnet' =>
  82. 'http://status.net/schema/api/1/'));
  83. $this->elementStart('users', array('type' => 'array'));
  84. if (is_array($this->users)) {
  85. foreach ($this->users as $u) {
  86. $twitter_user = $this->twitterUserArray($u, true);
  87. $this->showTwitterXmlUser($twitter_user);
  88. }
  89. } else {
  90. while ($this->users->fetch()) {
  91. $twitter_user = $this->twitterUserArray($this->users, true);
  92. $this->showTwitterXmlUser($twitter_user);
  93. }
  94. }
  95. $this->elementEnd('users');
  96. $this->element('next_cursor', null, $this->next_cursor);
  97. $this->element('previous_cursor', null, $this->prev_cursor);
  98. $this->elementEnd('users_list');
  99. break;
  100. case 'json':
  101. $this->initDocument('json');
  102. $users = array();
  103. if (is_array($this->users)) {
  104. foreach ($this->users as $u) {
  105. $twitter_user = $this->twitterUserArray($u, true);
  106. array_push($users, $twitter_user);
  107. }
  108. } else {
  109. while ($this->users->fetch()) {
  110. $twitter_user = $this->twitterUserArray($this->users, true);
  111. array_push($users, $twitter_user);
  112. }
  113. }
  114. $users_list = array('users' => $users,
  115. 'next_cursor' => $this->next_cursor,
  116. 'next_cursor_str' => strval($this->next_cursor),
  117. 'previous_cursor' => $this->prev_cursor,
  118. 'previous_cursor_str' => strval($this->prev_cursor));
  119. $this->showJsonObjects($users_list);
  120. $this->endDocument('json');
  121. break;
  122. default:
  123. $this->clientError(
  124. // TRANS: Client error displayed when coming across a non-supported API method.
  125. _('API method not found.'),
  126. 404,
  127. $this->format
  128. );
  129. break;
  130. }
  131. }
  132. function handlePost()
  133. {
  134. }
  135. function handleDelete()
  136. {
  137. }
  138. function getUsers()
  139. {
  140. }
  141. function isReadOnly($args)
  142. {
  143. return false;
  144. }
  145. function lastModified()
  146. {
  147. if(!empty($this->list)) {
  148. return strtotime($this->list->modified);
  149. }
  150. return null;
  151. }
  152. /**
  153. * An entity tag for this list
  154. *
  155. * Returns an Etag based on the action name, language, user ID and
  156. * timestamps of the first and last list the user has joined
  157. *
  158. * @return string etag
  159. */
  160. function etag()
  161. {
  162. if (!empty($this->list)) {
  163. return '"' . implode(
  164. ':',
  165. array($this->arg('action'),
  166. common_language(),
  167. $this->list->id,
  168. strtotime($this->list->created),
  169. strtotime($this->list->modified))
  170. )
  171. . '"';
  172. }
  173. return null;
  174. }
  175. }