3 * StatusNet, the distributed open-source microblogging tool
5 * Update the authenticating user's profile image
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.
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.
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/>.
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/
30 if (!defined('STATUSNET')) {
35 * Updates the authenticating user's profile image. Note that this API method
36 * expects raw multipart data, not a URL to an image.
40 * @author Zach Copley <zach@status.net>
41 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
42 * @link http://status.net/
44 class ApiAccountUpdateProfileImageAction extends ApiAuthAction
47 * Take arguments for running
49 * @param array $args $_REQUEST args
51 * @return boolean success flag
53 function prepare($args)
55 parent::prepare($args);
57 $this->user = $this->auth_user;
65 * Check whether the credentials are valid and output the result
67 * @param array $args $_REQUEST data (unused)
71 function handle($args)
73 parent::handle($args);
75 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
77 // TRANS: Client error. POST is a HTTP command. It should not be translated.
78 _('This method requires a POST.'),
84 // Workaround for PHP returning empty $_POST and $_FILES when POST
85 // length > post_max_size in php.ini
89 && ($_SERVER['CONTENT_LENGTH'] > 0)
91 // TRANS: Client error displayed when the number of bytes in a POST request exceeds a limit.
92 // TRANS: %s is the number of bytes of the CONTENT_LENGTH.
93 $msg = _m('The server was unable to handle that much POST data (%s byte) due to its current configuration.',
94 'The server was unable to handle that much POST data (%s bytes) due to its current configuration.',
95 intval($_SERVER['CONTENT_LENGTH']));
96 $this->clientError(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
100 if (empty($this->user)) {
101 // TRANS: Client error displayed updating profile image without having a user object.
102 $this->clientError(_('No such user.'), 404, $this->format);
107 $imagefile = ImageFile::fromUpload('image');
108 } catch (Exception $e) {
109 $this->clientError($e->getMessage(), 400, $this->format);
113 $type = $imagefile->preferredType();
114 $filename = Avatar::filename(
116 image_type_to_extension($type),
118 'tmp'.common_timestamp()
121 $filepath = Avatar::path($filename);
123 $imagefile->copyTo($filepath);
125 $profile = $this->user->getProfile();
127 if (empty($profile)) {
128 // TRANS: Error message displayed when referring to a user without a profile.
129 $this->clientError(_('User has no profile.'));
133 $profile->setOriginal($filename);
135 common_broadcast_profile($profile);
137 $twitter_user = $this->twitterUserArray($profile, true);
139 if ($this->format == 'xml') {
140 $this->initDocument('xml');
141 $this->showTwitterXmlUser($twitter_user, 'user', true);
142 $this->endDocument('xml');
143 } elseif ($this->format == 'json') {
144 $this->initDocument('json');
145 $this->showJsonObjects($twitter_user);
146 $this->endDocument('json');