]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apiaccountupdateprofileimage.php
Implement update avatar via API (/api/account/update_profile_image.format)
[quix0rs-gnu-social.git] / actions / apiaccountupdateprofileimage.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Update the authenticating user's profile image
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  * @package   StatusNet
24  * @author    Zach Copley <zach@status.net>
25  * @copyright 2009 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR . '/lib/apiauth.php';
35
36 /**
37  * Updates the authenticating user's profile image. Note that this API method
38  * expects raw multipart data, not a URL to an image.
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 ApiAccountUpdateProfileImageAction extends ApiAuthAction
48 {
49
50     /**
51      * Take arguments for running
52      *
53      * @param array $args $_REQUEST args
54      *
55      * @return boolean success flag
56      *
57      */
58
59     function prepare($args)
60     {
61         parent::prepare($args);
62
63         $this->user   = $this->auth_user;
64
65         return true;
66     }
67
68     /**
69      * Handle the request
70      *
71      * Check whether the credentials are valid and output the result
72      *
73      * @param array $args $_REQUEST data (unused)
74      *
75      * @return void
76      */
77
78     function handle($args)
79     {
80         parent::handle($args);
81
82         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
83             $this->clientError(
84                 _('This method requires a POST.'),
85                 400, $this->format
86             );
87             return;
88         }
89
90         if (empty($this->user)) {
91             $this->clientError(_('No such user!'), 404, $this->format);
92             return;
93         }
94
95         // Workaround for PHP returning empty $_FILES when POST length > PHP settings
96
97         if (empty($_FILES) && ($_SERVER['CONTENT_LENGTH'] > 0)) {
98             common_debug('content-length = ' . $_SERVER['CONTENT_LENGTH']);
99             $this->clientError(_('Unable to handle that much POST data!'));
100             return;
101         }
102
103         try {
104             $imagefile = ImageFile::fromUpload('image');
105         } catch (Exception $e) {
106             $this->clientError($e->getMessage(), 400, $this->format);
107             return;
108         }
109
110         $filename = Avatar::filename(
111             $user->id,
112             image_type_to_extension($imagefile->type),
113             null,
114             'tmp'.common_timestamp()
115         );
116
117         $filepath = Avatar::path($filename);
118
119         move_uploaded_file($imagefile->filepath, $filepath);
120
121         $profile = $this->user->getProfile();
122
123         if (empty($profile)) {
124             $this->clientError(_('User has no profile.'));
125             return;
126         }
127
128         $profile->setOriginal($filename);
129
130         common_broadcast_profile($profile);
131
132         $twitter_user = $this->twitterUserArray($this->user->getProfile(), true);
133
134         if ($this->format == 'xml') {
135             $this->initDocument('xml');
136             $this->showTwitterXmlUser($twitter_user);
137             $this->endDocument('xml');
138         } elseif ($this->format == 'json') {
139             $this->initDocument('json');
140             $this->showJsonObjects($twitter_user);
141             $this->endDocument('json');
142         }
143     }
144
145 }