]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apimediaupload.php
Merge commit 'origin/0.9.x' 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
47 class ApiMediaUploadAction extends ApiAuthAction
48 {
49     /**
50      * Handle the request
51      *
52      * Grab the file from the 'media' param, then store, and shorten
53      *
54      * @todo Upload throttle!
55      *
56      * @param array $args $_REQUEST data (unused)
57      *
58      * @return void
59      */
60
61     function handle($args)
62     {
63         parent::handle($args);
64
65         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
66             $this->clientError(
67                 _('This method requires a POST.'),
68                 400, $this->format
69             );
70             return;
71         }
72
73         // Workaround for PHP returning empty $_POST and $_FILES when POST
74         // length > post_max_size in php.ini
75
76         if (empty($_FILES)
77             && empty($_POST)
78             && ($_SERVER['CONTENT_LENGTH'] > 0)
79         ) {
80              $msg = _('The server was unable to handle that much POST ' .
81                     'data (%s bytes) due to its current configuration.');
82
83             $this->clientError(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
84             return;
85         }
86
87         $upload = null;
88
89         try {
90             $upload = MediaFile::fromUpload('media', $this->auth_user);
91         } catch (Exception $e) {
92             $this->clientError($e->getMessage(), $e->getCode());
93             return;
94         }
95
96         if (isset($upload)) {
97             $this->showResponse($upload);
98         } else {
99             $this->clientError(_('Upload failed.'));
100             return;
101         }
102     }
103
104     /**
105      * Show a Twitpic-like response with the ID of the media file
106      * and a (hopefully) shortened URL for it.
107      *
108      * @param File $upload  the uploaded file
109      *
110      * @return void
111      */
112     function showResponse($upload)
113     {
114         $this->initDocument();
115         $this->elementStart('rsp', array('stat' => 'ok'));
116         $this->element('mediaid', null, $upload->fileRecord->id);
117         $this->element('mediaurl', null, $upload->shortUrl());
118         $this->elementEnd('rsp');
119         $this->endDocument();
120     }
121
122     /**
123      * Overrided clientError to show a more Twitpic-like error
124      *
125      * @param String $msg an error message
126      *
127      */
128     function clientError($msg)
129     {
130         $this->initDocument();
131         $this->elementStart('rsp', array('stat' => 'fail'));
132
133         // @todo add in error code
134         $errAttr = array('msg' => $msg);
135
136         $this->element('err', $errAttr, null);
137         $this->elementEnd('rsp');
138         $this->endDocument();
139     }
140
141 }