]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apimediaupload.php
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / actions / apimediaupload.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Upload an image via the 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  * @author    Zach Copley <zach@status.net>
24  * @copyright 2010 StatusNet, Inc.
25  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
26  * @link      http://status.net/
27  */
28
29 if (!defined('GNUSOCIAL')) { exit(1); }
30
31 /**
32  * Upload an image via the API.  Returns a shortened URL for the image
33  * to the user. Apparently modelled after a former Twitpic API.
34  *
35  * @category API
36  * @package  StatusNet
37  * @author   Zach Copley <zach@status.net>
38  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
39  * @link     http://status.net/
40  */
41 class ApiMediaUploadAction extends ApiAuthAction
42 {
43     protected $needPost = true;
44
45     /**
46      * Handle the request
47      *
48      * Grab the file from the 'media' param, then store, and shorten
49      *
50      * @todo Upload throttle!
51      *
52      * @param array $args $_REQUEST data (unused)
53      *
54      * @return void
55      */
56     protected function handle()
57     {
58         parent::handle();
59
60         // Workaround for PHP returning empty $_POST and $_FILES when POST
61         // length > post_max_size in php.ini
62
63         if (empty($_FILES)
64             && empty($_POST)
65             && ($_SERVER['CONTENT_LENGTH'] > 0)
66         ) {
67             // TRANS: Client error displayed when the number of bytes in a POST request exceeds a limit.
68             // TRANS: %s is the number of bytes of the CONTENT_LENGTH.
69             $msg = _m('The server was unable to handle that much POST data (%s byte) due to its current configuration.',
70                       'The server was unable to handle that much POST data (%s bytes) due to its current configuration.',
71                       intval($_SERVER['CONTENT_LENGTH']));
72             $this->clientError(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
73         }
74
75         // we could catch "NoUploadedMediaException" as "no media uploaded", but here we _always_ want an upload
76         $upload = MediaFile::fromUpload('media', $this->scoped);
77         
78         // Thumbnails will be generated/cached on demand when accessed (such as with /attachment/:id/thumbnail)
79
80         $this->showResponse($upload);
81     }
82
83     /**
84      * Show a Twitpic-like response with the ID of the media file
85      * and a (hopefully) shortened URL for it.
86      *
87      * @param MediaFile $upload  the uploaded file
88      *
89      * @return void
90      */
91     function showResponse(MediaFile $upload)
92     {
93         $this->initDocument();
94         $this->elementStart('rsp', array('stat' => 'ok', 'xmlns:atom'=>Activity::ATOM));
95         $this->element('mediaid', null, $upload->fileRecord->id);
96         $this->element('mediaurl', null, $upload->shortUrl());
97
98         $enclosure = $upload->fileRecord->getEnclosure();
99         $this->element('atom:link', array('rel'  => 'enclosure',
100                                           'href' => $enclosure->url,
101                                           'type' => $enclosure->mimetype));
102         $this->elementEnd('rsp');
103         $this->endDocument();
104     }
105
106     /**
107      * Overrided clientError to show a more Twitpic-like error
108      *
109      * @param String $msg an error message
110      */
111     function clientError($msg, $code=400, $format=null)
112     {
113         $this->initDocument();
114         $this->elementStart('rsp', array('stat' => 'fail'));
115
116         // @todo add in error code
117         $errAttr = array('msg' => $msg);
118
119         $this->element('err', $errAttr, null);
120         $this->elementEnd('rsp');
121         $this->endDocument();
122         exit;
123     }
124 }