]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apiaccountupdateprofileimage.php
Merge remote branch 'origin/master' into twitstream
[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 class ApiAccountUpdateProfileImageAction extends ApiAuthAction
47 {
48     /**
49      * Take arguments for running
50      *
51      * @param array $args $_REQUEST args
52      *
53      * @return boolean success flag
54      */
55     function prepare($args)
56     {
57         parent::prepare($args);
58
59         $this->user   = $this->auth_user;
60
61         return true;
62     }
63
64     /**
65      * Handle the request
66      *
67      * Check whether the credentials are valid and output the result
68      *
69      * @param array $args $_REQUEST data (unused)
70      *
71      * @return void
72      */
73     function handle($args)
74     {
75         parent::handle($args);
76
77         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
78             $this->clientError(
79                 // TRANS: Client error. POST is a HTTP command. It should not be translated.
80                 _('This method requires a POST.'),
81                 400, $this->format
82             );
83             return;
84         }
85
86         // Workaround for PHP returning empty $_POST and $_FILES when POST
87         // length > post_max_size in php.ini
88
89         if (empty($_FILES)
90             && empty($_POST)
91             && ($_SERVER['CONTENT_LENGTH'] > 0)
92         ) {
93             // TRANS: Client error displayed when the number of bytes in a POST request exceeds a limit.
94             // TRANS: %s is the number of bytes of the CONTENT_LENGTH.
95             $msg = _m('The server was unable to handle that much POST data (%s byte) due to its current configuration.',
96                       'The server was unable to handle that much POST data (%s bytes) due to its current configuration.',
97                       intval($_SERVER['CONTENT_LENGTH']));
98             $this->clientError(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
99             return;
100         }
101
102         if (empty($this->user)) {
103             // TRANS: Client error displayed updating profile image without having a user object.
104             $this->clientError(_('No such user.'), 404, $this->format);
105             return;
106         }
107
108         try {
109             $imagefile = ImageFile::fromUpload('image');
110         } catch (Exception $e) {
111             $this->clientError($e->getMessage(), 400, $this->format);
112             return;
113         }
114
115         $filename = Avatar::filename(
116             $user->id,
117             image_type_to_extension($imagefile->type),
118             null,
119             'tmp'.common_timestamp()
120         );
121
122         $filepath = Avatar::path($filename);
123
124         move_uploaded_file($imagefile->filepath, $filepath);
125
126         $profile = $this->user->getProfile();
127
128         if (empty($profile)) {
129             // TRANS: Client error displayed if a user profile could not be found updating a profile image.
130             $this->clientError(_('User has no profile.'));
131             return;
132         }
133
134         $profile->setOriginal($filename);
135
136         common_broadcast_profile($profile);
137
138         $twitter_user = $this->twitterUserArray($profile, true);
139
140         if ($this->format == 'xml') {
141             $this->initDocument('xml');
142             $this->showTwitterXmlUser($twitter_user);
143             $this->endDocument('xml');
144         } elseif ($this->format == 'json') {
145             $this->initDocument('json');
146             $this->showJsonObjects($twitter_user);
147             $this->endDocument('json');
148         }
149     }
150 }