]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apiaccountupdateprofileimage.php
Add AtomPub, Twitter-compat. API documentation to doc-src/
[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 /**
35  * Updates the authenticating user's profile image. Note that this API method
36  * expects raw multipart data, not a URL to an image.
37  *
38  * @category API
39  * @package  StatusNet
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/
43  */
44 class ApiAccountUpdateProfileImageAction extends ApiAuthAction
45 {
46     protected $needPost = true;
47
48     /**
49      * Handle the request
50      *
51      * Check whether the credentials are valid and output the result
52      *
53      * @return void
54      */
55     protected function handle()
56     {
57         parent::handle();
58
59         // Workaround for PHP returning empty $_POST and $_FILES when POST
60         // length > post_max_size in php.ini
61
62         if (empty($_FILES)
63             && empty($_POST)
64             && ($_SERVER['CONTENT_LENGTH'] > 0)
65         ) {
66             // TRANS: Client error displayed when the number of bytes in a POST request exceeds a limit.
67             // TRANS: %s is the number of bytes of the CONTENT_LENGTH.
68             $msg = _m('The server was unable to handle that much POST data (%s byte) due to its current configuration.',
69                       'The server was unable to handle that much POST data (%s bytes) due to its current configuration.',
70                       intval($_SERVER['CONTENT_LENGTH']));
71             $this->clientError(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
72         }
73
74         if (empty($this->user)) {
75             // TRANS: Client error displayed updating profile image without having a user object.
76             $this->clientError(_('No such user.'), 404);
77         }
78
79         try {
80             $imagefile = ImageFile::fromUpload('image');
81         } catch (Exception $e) {
82             $this->clientError($e->getMessage());
83         }
84
85         $type = $imagefile->preferredType();
86         $filename = Avatar::filename(
87             $user->id,
88             image_type_to_extension($type),
89             null,
90             'tmp'.common_timestamp()
91         );
92
93         $filepath = Avatar::path($filename);
94
95         $imagefile->copyTo($filepath);
96
97         $profile = $this->user->getProfile();
98         $profile->setOriginal($filename);
99
100         $twitter_user = $this->twitterUserArray($profile, true);
101
102         if ($this->format == 'xml') {
103             $this->initDocument('xml');
104             $this->showTwitterXmlUser($twitter_user, 'user', true);
105             $this->endDocument('xml');
106         } elseif ($this->format == 'json') {
107             $this->initDocument('json');
108             $this->showJsonObjects($twitter_user);
109             $this->endDocument('json');
110         }
111     }
112 }