apilistusers.php 5.98 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
<?php
/**
 * StatusNet, the distributed open-source microblogging tool
 *
 * Base class for list members and list subscribers api.
 *
 * PHP version 5
 *
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @category  API
 * @package   StatusNet
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
 * @link      http://status.net/
 */

if (!defined('STATUSNET')) {
    exit(1);
}

class ApiListUsersAction extends ApiBareAuthAction
{
    var $list   = null;
    var $user   = false;
    var $create = false;
    var $delete = false;
    var $cursor = -1;
    var $next_cursor = 0;
    var $prev_cursor = 0;
    var $users = null;

43
    protected function prepare(array $args=array())
44 45 46 47 48 49 50 51 52 53 54
    {
        // delete list member if method is DELETE or if method is POST and an argument
        // _method is set to DELETE
        $this->delete = ($_SERVER['REQUEST_METHOD'] == 'DELETE' ||
                            ($this->trimmed('_method') == 'DELETE' &&
                             $_SERVER['REQUEST_METHOD'] == 'POST'));

        // add member if method is POST
        $this->create = (!$this->delete &&
                         $_SERVER['REQUEST_METHOD'] == 'POST');

55 56
        if ($this->arg('id')) {
            $this->target = $this->getTargetProfile($this->arg('id'));
57 58 59 60 61 62 63
        }

        parent::prepare($args);

        $this->list = $this->getTargetList($this->arg('user'), $this->arg('list_id'));

        if (empty($this->list)) {
64 65
            // TRANS: Client error displayed when referring to a non-existing list.
            $this->clientError(_('List not found.'), 404, $this->format);
66 67 68 69 70 71 72 73 74 75 76 77 78 79
        }

        if(!$this->create && !$this->delete) {
            $this->getUsers();
        }
        return true;
    }

    function requiresAuth()
    {
        return parent::requiresAuth() ||
            $this->create || $this->delete;
    }

80
    protected function handle()
81
    {
82
        parent::handle();
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

        if($this->delete) {
            return $this->handleDelete();
        }

        if($this->create) {
            return $this->handlePost();
        }

        switch($this->format) {
        case 'xml':
            $this->initDocument('xml');
            $this->elementStart('users_list', array('xmlns:statusnet' =>
                                         'http://status.net/schema/api/1/'));
            $this->elementStart('users', array('type' => 'array'));

            if (is_array($this->users)) {
                foreach ($this->users as $u) {
                    $twitter_user = $this->twitterUserArray($u, true);
                    $this->showTwitterXmlUser($twitter_user);
                }
            } else {
                while ($this->users->fetch()) {
                    $twitter_user = $this->twitterUserArray($this->users, true);
                    $this->showTwitterXmlUser($twitter_user);
                }
            }

            $this->elementEnd('users');
            $this->element('next_cursor', null, $this->next_cursor);
            $this->element('previous_cursor', null, $this->prev_cursor);
            $this->elementEnd('users_list');
            break;
        case 'json':
            $this->initDocument('json');

            $users = array();

            if (is_array($this->users)) {
                foreach ($this->users as $u) {
                    $twitter_user = $this->twitterUserArray($u, true);
                    array_push($users, $twitter_user);
                }
            } else {
                while ($this->users->fetch()) {
                    $twitter_user = $this->twitterUserArray($this->users, true);
                    array_push($users, $twitter_user);
                }
            }

            $users_list = array('users' => $users,
                                'next_cursor' => $this->next_cursor,
                                'next_cursor_str' => strval($this->next_cursor),
                                'previous_cursor' => $this->prev_cursor,
                                'previous_cursor_str' => strval($this->prev_cursor));

            $this->showJsonObjects($users_list);

            $this->endDocument('json');
            break;
        default:
            $this->clientError(
145
                // TRANS: Client error displayed when coming across a non-supported API method.
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
                _('API method not found.'),
                404,
                $this->format
            );
            break;
        }
    }

    function handlePost()
    {
    }

    function handleDelete()
    {
    }

    function getUsers()
    {
    }

    function isReadOnly($args)
    {
        return false;
    }

    function lastModified()
    {
        if(!empty($this->list)) {
            return strtotime($this->list->modified);
        }
        return null;
    }

    /**
     * An entity tag for this list
     *
     * Returns an Etag based on the action name, language, user ID and
     * timestamps of the first and last list the user has joined
     *
     * @return string etag
     */
    function etag()
    {
        if (!empty($this->list)) {

            return '"' . implode(
                ':',
                array($this->arg('action'),
                      common_language(),
                      $this->list->id,
                      strtotime($this->list->created),
                      strtotime($this->list->modified))
            )
            . '"';
        }

        return null;
    }
}