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