git.gnu.io has moved to IP address 209.51.188.249 -- please double check where you are logging in.

apilist.php 6.76 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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
<?php
/**
 * StatusNet, the distributed open-source microblogging tool
 *
 * Show, update or delete a list.
 *
 * 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
 * @author    Shashi Gowda <connect2shashi@gmail.com>
 * @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 ApiListAction extends ApiBareAuthAction
{
    /**
     * The list in question in the current request
     */
    var $list   = null;

    /**
     * Is this an update request?
     */
    var $update = false;

    /**
     * Is this a delete request?
     */
    var $delete = false;

    /**
     * Set the flags for handling the request. Show list if this is a GET
     * request, update it if it is POST, delete list if method is DELETE
     * or if method is POST and an argument _method is set to DELETE. Act
     * like we don't know if the current user has no access to the list.
     *
     * Takes parameters:
     *     - user: the user id or nickname
     *     - id:   the id of the tag or the tag itself
     *
     * @return boolean success flag
     */
62
    protected function prepare(array $args=array())
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    {
        parent::prepare($args);

        $this->delete = ($_SERVER['REQUEST_METHOD'] == 'DELETE' ||
                            ($this->trimmed('_method') == 'DELETE' &&
                             $_SERVER['REQUEST_METHOD'] == 'POST'));

        // update list if method is POST or PUT and $this->delete is not true
        $this->update = (!$this->delete &&
                         in_array($_SERVER['REQUEST_METHOD'], array('POST', 'PUT')));

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

        if (empty($this->list)) {
78
            // TRANS: Client error displayed when referring to a non-existing list.
79
            $this->clientError(_('List not found.'), 404);
80 81 82 83 84 85 86 87 88 89
        }

        return true;
    }

    /**
     * Handle the request
     *
     * @return boolean success flag
     */
90
    protected function handle()
91
    {
92
        parent::handle();
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

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

        if($this->update) {
            $this->handlePut();
            return true;
        }

        switch($this->format) {
        case 'xml':
            $this->showSingleXmlList($this->list);
            break;
        case 'json':
            $this->showSingleJsonList($this->list);
            break;
        default:
112 113
            // TRANS: Client error displayed when coming across a non-supported API method.
            $this->clientError(_('API method not found.'), 404);
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
        }
    }

    /**
     * require authentication if it is a write action or user is ambiguous
     *
     */
    function requiresAuth()
    {
        return parent::requiresAuth() ||
            $this->create || $this->delete;
    }

    /**
     * Update a list
     *
     * @return boolean success
     */
    function handlePut()
    {
        if($this->auth_user->id != $this->list->tagger) {
135 136
            // TRANS: Client error displayed when trying to update another user's list.
            $this->clientError(_('You cannot update lists that do not belong to you.'), 401);
137 138 139 140 141 142 143 144 145 146
        }

        $new_list = clone($this->list);
        $new_list->tag = common_canonical_tag($this->arg('name'));
        $new_list->description = common_canonical_tag($this->arg('description'));
        $new_list->private = ($this->arg('mode') === 'private') ? true : false;

        $result = $new_list->update($this->list);

        if(!$result) {
147 148
            // TRANS: Client error displayed when an unknown error occurs updating a list.
            $this->clientError(_('An error occured.'), 503);
149 150 151 152 153 154 155 156 157 158
        }

        switch($this->format) {
        case 'xml':
            $this->showSingleXmlList($new_list);
            break;
        case 'json':
            $this->showSingleJsonList($new_list);
            break;
        default:
159 160
            // TRANS: Client error displayed when coming across a non-supported API method.
            $this->clientError(_('API method not found.'), 404);
161 162 163 164 165 166 167 168 169 170 171
        }
    }

    /**
     * Delete a list
     *
     * @return boolean success
     */
    function handleDelete()
    {
        if($this->auth_user->id != $this->list->tagger) {
172 173
            // TRANS: Client error displayed when trying to delete another user's list.
            $this->clientError(_('You cannot delete lists that do not belong to you.'), 401);
174 175 176 177 178 179 180 181 182 183 184 185 186
        }

        $record = clone($this->list);
        $this->list->delete();

        switch($this->format) {
        case 'xml':
            $this->showSingleXmlList($record);
            break;
        case 'json':
            $this->showSingleJsonList($record);
            break;
        default:
187 188
            // TRANS: Client error displayed when coming across a non-supported API method.
            $this->clientError(_('API method not found.'), 404);
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
        }
    }

    /**
     * Indicate that this resource is not read-only.
     *
     * @return boolean is_read-only=false
     */
    function isReadOnly($args)
    {
        return false;
    }

    /**
     * When was the list (people tag) last updated?
     *
     * @return String time_last_modified
     */
    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->user->id,
                      strtotime($this->list->created),
                      strtotime($this->list->modified))
            )
            . '"';
        }

        return null;
    }
}