]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apimediaupload.php
Merge branch 'master' into 0.9.x
[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('STATUSNET')) {
30     exit(1);
31 }
32
33 require_once INSTALLDIR . '/lib/apiauth.php';
34 require_once INSTALLDIR . '/lib/mediafile.php';
35
36 /**
37  * Upload an image via the API.  Returns a shortened URL for the image
38  * to the user.
39  *
40  * @category API
41  * @package  StatusNet
42  * @author   Zach Copley <zach@status.net>
43  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44  * @link     http://status.net/
45  */
46 class ApiMediaUploadAction extends ApiAuthAction
47 {
48     /**
49      * Handle the request
50      *
51      * Grab the file from the 'media' param, then store, and shorten
52      *
53      * @todo Upload throttle!
54      *
55      * @param array $args $_REQUEST data (unused)
56      *
57      * @return void
58      */
59     function handle($args)
60     {
61         parent::handle($args);
62
63         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
64             $this->clientError(
65                 // TRANS: Client error. POST is a HTTP command. It should not be translated.
66                 _('This method requires a POST.'),
67                 400, $this->format
68             );
69             return;
70         }
71
72         // Workaround for PHP returning empty $_POST and $_FILES when POST
73         // length > post_max_size in php.ini
74
75         if (empty($_FILES)
76             && empty($_POST)
77             && ($_SERVER['CONTENT_LENGTH'] > 0)
78         ) {
79             // TRANS: Client error displayed when the number of bytes in a POST request exceeds a limit.
80             // TRANS: %s is the number of bytes of the CONTENT_LENGTH.
81             $msg = _m('The server was unable to handle that much POST data (%s byte) due to its current configuration.',
82                       'The server was unable to handle that much POST data (%s bytes) due to its current configuration.',
83                       intval($_SERVER['CONTENT_LENGTH']));
84             $this->clientError(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
85             return;
86         }
87
88         $upload = null;
89
90         try {
91             $upload = MediaFile::fromUpload('media', $this->auth_user);
92         } catch (Exception $e) {
93             $this->clientError($e->getMessage(), $e->getCode());
94             return;
95         }
96
97         if (isset($upload)) {
98             $this->showResponse($upload);
99         } else {
100             // TRANS: Client error displayed when uploading a media file has failed.
101             $this->clientError(_('Upload failed.'));
102             return;
103         }
104     }
105
106     /**
107      * Show a Twitpic-like response with the ID of the media file
108      * and a (hopefully) shortened URL for it.
109      *
110      * @param File $upload  the uploaded file
111      *
112      * @return void
113      */
114     function showResponse($upload)
115     {
116         $this->initDocument();
117         $this->elementStart('rsp', array('stat' => 'ok'));
118         $this->element('mediaid', null, $upload->fileRecord->id);
119         $this->element('mediaurl', null, $upload->shortUrl());
120         $this->elementEnd('rsp');
121         $this->endDocument();
122     }
123
124     /**
125      * Overrided clientError to show a more Twitpic-like error
126      *
127      * @param String $msg an error message
128      */
129     function clientError($msg)
130     {
131         $this->initDocument();
132         $this->elementStart('rsp', array('stat' => 'fail'));
133
134         // @todo add in error code
135         $errAttr = array('msg' => $msg);
136
137         $this->element('err', $errAttr, null);
138         $this->elementEnd('rsp');
139         $this->endDocument();
140     }
141 }