]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apiuserprofileimage.php
Merge branch '1.0.x' of git://gitorious.org/statusnet/mainline
[quix0rs-gnu-social.git] / actions / apiuserprofileimage.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Return a user's avatar 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    Brion Vibber <brion@status.net>
25  * @copyright 2010 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/apiprivateauth.php';
35
36 /**
37  * Ouputs avatar URL for a user, specified by screen name.
38  * Unlike most API endpoints, this returns an HTTP redirect rather than direct data.
39  *
40  * @category API
41  * @package  StatusNet
42  * @author   Brion Vibber <brion@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 ApiUserProfileImageAction extends ApiPrivateAuthAction
47 {
48     /**
49      * Take arguments for running
50      *
51      * @param array $args $_REQUEST args
52      *
53      * @return boolean success flag
54      *
55      */
56     function prepare($args)
57     {
58         parent::prepare($args);
59         $this->user = User::staticGet('nickname', $this->arg('screen_name'));
60         $this->size = $this->arg('size');
61
62         return true;
63     }
64
65     /**
66      * Handle the request
67      *
68      * Check the format and show the user info
69      *
70      * @param array $args $_REQUEST data (unused)
71      *
72      * @return void
73      */
74     function handle($args)
75     {
76         parent::handle($args);
77
78         if (empty($this->user)) {
79             // TRANS: Client error displayed when requesting user information for a non-existing user.
80             $this->clientError(_('User not found.'), 404, $this->format);
81             return;
82         }
83
84         $profile = $this->user->getProfile();
85
86         if (empty($profile)) {
87             // TRANS: Client error displayed when requesting user information for a user without a profile.
88             $this->clientError(_('User has no profile.'));
89             return;
90         }
91
92         $size = $this->avatarSize();
93         $avatar = $profile->getAvatar($size);
94         if ($avatar) {
95             $url = $avatar->displayUrl();
96         } else {
97             $url = Avatar::defaultImage($size);
98         }
99
100         // We don't actually output JSON or XML data -- redirect!
101         common_redirect($url, 302);
102     }
103
104     /**
105      * Get the appropriate pixel size for an avatar based on the request...
106      *
107      * @return int
108      */
109     private function avatarSize()
110     {
111         switch ($this->size) {
112             case 'mini':
113                 return AVATAR_MINI_SIZE; // 24x24
114             case 'bigger':
115                 return AVATAR_PROFILE_SIZE; // Twitter does 73x73, but we do 96x96
116             case 'normal': // fall through
117             default:
118                 return AVATAR_STREAM_SIZE; // 48x48
119         }
120     }
121
122     /**
123      * Return true if read only.
124      *
125      * MAY override
126      *
127      * @param array $args other arguments
128      *
129      * @return boolean is read only action?
130      */
131     function isReadOnly($args)
132     {
133         return true;
134     }
135 }