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

apistatusesshow.php 7.26 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
<?php
/**
 * StatusNet, the distributed open-source microblogging tool
 *
 * Show a notice (as a Twitter-style status)
 *
 * 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
Zach Copley's avatar
Zach Copley committed
24 25 26 27 28 29
 * @author    Craig Andrews <candrews@integralblue.com>
 * @author    Evan Prodromou <evan@status.net>
 * @author    Jeffery To <jeffery.to@gmail.com>
 * @author    Tom Blankenship <mac65@mac65.com>
 * @author    Mike Cochrane <mikec@mikenz.geek.nz>
 * @author    Robin Millette <robin@millette.info>
30 31
 * @author    Zach Copley <zach@status.net>
 * @copyright 2009 StatusNet, Inc.
32
 * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
33 34 35 36
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
 * @link      http://status.net/
 */

37
if (!defined('GNUSOCIAL')) { exit(1); }
38 39 40 41 42 43

/**
 * Returns the notice specified by id as a Twitter-style status and inline user
 *
 * @category API
 * @package  StatusNet
Zach Copley's avatar
Zach Copley committed
44 45 46 47 48 49
 * @author   Craig Andrews <candrews@integralblue.com>
 * @author   Evan Prodromou <evan@status.net>
 * @author   Jeffery To <jeffery.to@gmail.com>
 * @author   Tom Blankenship <mac65@mac65.com>
 * @author   Mike Cochrane <mikec@mikenz.geek.nz>
 * @author   Robin Millette <robin@millette.info>
50 51 52 53
 * @author   Zach Copley <zach@status.net>
 * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
 * @link     http://status.net/
 */
54
class ApiStatusesShowAction extends ApiPrivateAuthAction
55 56 57 58 59 60 61 62 63 64 65
{
    var $notice_id = null;
    var $notice    = null;

    /**
     * Take arguments for running
     *
     * @param array $args $_REQUEST args
     *
     * @return boolean success flag
     */
66
    protected function prepare(array $args=array())
67 68 69 70 71 72 73 74 75 76
    {
        parent::prepare($args);

        // 'id' is an undocumented parameter in Twitter's API. Several
        // clients make use of it, so we support it too.

        // show.json?id=12345 takes precedence over /show/12345.json

        $this->notice_id = (int)$this->trimmed('id');

77 78 79 80 81 82 83 84
        $this->notice = null;
        try {
            $this->notice = Notice::getByID($this->notice_id);
        } catch (NoResultException $e) {
            // No such notice was found, maybe it was deleted?
            $deleted = null;
            Event::handle('IsNoticeDeleted', array($this->notice_id, &$deleted));
            if ($deleted === true) {
85
                // TRANS: Client error displayed trying to show a deleted notice.
86
                throw new ClientException(_('Notice deleted.'), 410);
87 88
            }
            // TRANS: Client error displayed trying to show a non-existing notice.
89
            throw new ClientException(_('No such notice.'), 404);
90
        }
91

92 93 94
        if (!$this->notice->inScope($this->scoped)) {
            // TRANS: Client exception thrown when trying a view a notice the user has no access to.
            throw new ClientException(_('Access restricted.'), 403);
95 96 97 98 99 100 101 102
        }

        return true;
    }

    /**
     * Handle the request
     *
103
     * Check the format and show the notice
104 105 106
     *
     * @return void
     */
107
    protected function handle()
108
    {
109
        parent::handle();
110

111
        if (!in_array($this->format, array('xml', 'json', 'atom'))) {
112
            // TRANS: Client error displayed when coming across a non-supported API method.
113
            $this->clientError(_('API method not found.'), 404);
114 115
        }

116 117 118 119 120 121 122 123
        switch ($_SERVER['REQUEST_METHOD']) {
        case 'GET':
            $this->showNotice();
            break;
        case 'DELETE':
            $this->deleteNotice();
            break;
        default:
124
            // TRANS: Client error displayed calling an unsupported HTTP error in API status show.
125 126
            $this->clientError(_('HTTP method not supported.'), 405);
        }
127 128 129
    }

    /**
130
     * Show the notice
131 132 133 134 135
     *
     * @return void
     */
    function showNotice()
    {
136 137 138 139 140 141 142 143 144 145 146 147 148 149
        switch ($this->format) {
        case 'xml':
            $this->showSingleXmlStatus($this->notice);
            break;
        case 'json':
            $this->show_single_json_status($this->notice);
            break;
        case 'atom':
            $this->showSingleAtomStatus($this->notice);
            break;
        default:
            // TRANS: Exception thrown requesting an unsupported notice output format.
            // TRANS: %s is the requested output format.
            throw new Exception(sprintf(_("Unsupported format: %s."), $this->format));
150 151 152 153
        }
    }

    /**
154
     * We expose AtomPub here, so non-GET/HEAD reqs must be read/write.
155 156 157 158 159
     *
     * @param array $args other arguments
     *
     * @return boolean true
     */
160

161 162
    function isReadOnly($args)
    {
163
        return ($_SERVER['REQUEST_METHOD'] == 'GET' || $_SERVER['REQUEST_METHOD'] == 'HEAD');
164 165 166 167 168 169 170 171 172
    }

    /**
     * When was this notice last modified?
     *
     * @return string datestamp of the latest notice in the stream
     */
    function lastModified()
    {
173
        return strtotime($this->notice->created);
174 175 176 177 178 179 180 181 182 183 184 185
    }

    /**
     * An entity tag for this notice
     *
     * Returns an Etag based on the action name, language, and
     * timestamps of the notice
     *
     * @return string etag
     */
    function etag()
    {
186 187 188 189 190 191 192 193 194
        return '"' . implode(
            ':',
            array($this->arg('action'),
                  common_user_cache_hash($this->auth_user),
                  common_language(),
                  $this->notice->id,
                  strtotime($this->notice->created))
        )
        . '"';
195 196
    }

197 198 199
    function deleteNotice()
    {
        if ($this->format != 'atom') {
200
            // TRANS: Client error displayed when trying to delete a notice not using the Atom format.
201
            $this->clientError(_('Can only delete using the Atom format.'));
202 203 204 205 206
        }

        if (empty($this->auth_user) ||
            ($this->notice->profile_id != $this->auth_user->id &&
             !$this->auth_user->hasRight(Right::DELETEOTHERSNOTICE))) {
207 208
            // TRANS: Client error displayed when a user has no rights to delete notices of other users.
            $this->clientError(_('Cannot delete this notice.'), 403);
209 210 211
        }

        if (Event::handle('StartDeleteOwnNotice', array($this->auth_user, $this->notice))) {
212
            $this->notice->deleteAs($this->scoped);
213 214 215 216 217 218 219
            Event::handle('EndDeleteOwnNotice', array($this->auth_user, $this->notice));
        }

        // @fixme is there better output we could do here?

        header('HTTP/1.1 200 OK');
        header('Content-Type: text/plain');
220
        // TRANS: Confirmation of notice deletion in API. %d is the ID (number) of the deleted notice.
221 222 223
        print(sprintf(_('Deleted notice %d'), $this->notice->id));
        print("\n");
    }
224
}