]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/updateprofile.php
change Laconica and Control Yourself to StatusNet in PHP files
[quix0rs-gnu-social.git] / actions / updateprofile.php
1 <?php
2 /*
3  * StatusNet - a distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/omb.php');
23
24 class UpdateprofileAction extends Action
25 {
26     
27     function handle($args)
28     {
29         parent::handle($args);
30         try {
31             common_remove_magic_from_request();
32             $req = OAuthRequest::from_request('POST', common_local_url('updateprofile'));
33             # Note: server-to-server function!
34             $server = omb_oauth_server();
35             list($consumer, $token) = $server->verify_request($req);
36             if ($this->update_profile($req, $consumer, $token)) {
37                 header('HTTP/1.1 200 OK');
38                 header('Content-type: text/plain');
39                 print "omb_version=".OMB_VERSION_01;
40             }
41         } catch (OAuthException $e) {
42             $this->serverError($e->getMessage());
43             return;
44         }
45     }
46
47     function update_profile($req, $consumer, $token)
48     {
49         $version = $req->get_parameter('omb_version');
50         if ($version != OMB_VERSION_01) {
51             $this->clientError(_('Unsupported OMB version'), 400);
52             return false;
53         }
54         # First, check to see if listenee exists
55         $listenee =  $req->get_parameter('omb_listenee');
56         $remote = Remote_profile::staticGet('uri', $listenee);
57         if (!$remote) {
58             $this->clientError(_('Profile unknown'), 404);
59             return false;
60         }
61         # Second, check to see if they should be able to post updates!
62         # We see if there are any subscriptions to that remote user with
63         # the given token.
64
65         $sub = new Subscription();
66         $sub->subscribed = $remote->id;
67         $sub->token = $token->key;
68         if (!$sub->find(true)) {
69             $this->clientError(_('You did not send us that profile'), 403);
70             return false;
71         }
72
73         $profile = Profile::staticGet('id', $remote->id);
74         if (!$profile) {
75             # This one is our fault
76             $this->serverError(_('Remote profile with no matching profile'), 500);
77             return false;
78         }
79         $nickname = $req->get_parameter('omb_listenee_nickname');
80         if ($nickname && !Validate::string($nickname, array('min_length' => 1,
81                                                             'max_length' => 64,
82                                                             'format' => NICKNAME_FMT))) {
83             $this->clientError(_('Nickname must have only lowercase letters and numbers and no spaces.'));
84             return false;
85         }
86         $license = $req->get_parameter('omb_listenee_license');
87         if ($license && !common_valid_http_url($license)) {
88             $this->clientError(sprintf(_("Invalid license URL '%s'"), $license));
89             return false;
90         }
91         $profile_url = $req->get_parameter('omb_listenee_profile');
92         if ($profile_url && !common_valid_http_url($profile_url)) {
93             $this->clientError(sprintf(_("Invalid profile URL '%s'."), $profile_url));
94             return false;
95         }
96         # optional stuff
97         $fullname = $req->get_parameter('omb_listenee_fullname');
98         if ($fullname && mb_strlen($fullname) > 255) {
99             $this->clientError(_("Full name is too long (max 255 chars)."));
100             return false;
101         }
102         $homepage = $req->get_parameter('omb_listenee_homepage');
103         if ($homepage && (!common_valid_http_url($homepage) || mb_strlen($homepage) > 255)) {
104             $this->clientError(sprintf(_("Invalid homepage '%s'"), $homepage));
105             return false;
106         }
107         $bio = $req->get_parameter('omb_listenee_bio');
108         if ($bio && mb_strlen($bio) > 140) {
109             $this->clientError(_("Bio is too long (max 140 chars)."));
110             return false;
111         }
112         $location = $req->get_parameter('omb_listenee_location');
113         if ($location && mb_strlen($location) > 255) {
114             $this->clientError(_("Location is too long (max 255 chars)."));
115             return false;
116         }
117         $avatar = $req->get_parameter('omb_listenee_avatar');
118         if ($avatar) {
119             if (!common_valid_http_url($avatar) || strlen($avatar) > 255) {
120                 $this->clientError(sprintf(_("Invalid avatar URL '%s'"), $avatar));
121                 return false;
122             }
123             $size = @getimagesize($avatar);
124             if (!$size) {
125                 $this->clientError(sprintf(_("Can't read avatar URL '%s'"), $avatar));
126                 return false;
127             }
128             if ($size[0] != AVATAR_PROFILE_SIZE || $size[1] != AVATAR_PROFILE_SIZE) {
129                 $this->clientError(sprintf(_("Wrong size image at '%s'"), $avatar));
130                 return false;
131             }
132             if (!in_array($size[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG,
133                                           IMAGETYPE_PNG))) {
134                 $this->clientError(sprintf(_("Wrong image type for '%s'"), $avatar));
135                 return false;
136             }
137         }
138
139         $orig_profile = clone($profile);
140
141         /* Use values even if they are an empty string. Parsing an empty string in
142            updateProfile is the specified way of clearing a parameter in OMB. */
143         if (!is_null($nickname)) {
144             $profile->nickname = $nickname;
145         }
146         if (!is_null($profile_url)) {
147             $profile->profileurl = $profile_url;
148         }
149         if (!is_null($fullname)) {
150             $profile->fullname = $fullname;
151         }
152         if (!is_null($homepage)) {
153             $profile->homepage = $homepage;
154         }
155         if (!is_null($bio)) {
156             $profile->bio = $bio;
157         }
158         if (!is_null($location)) {
159             $profile->location = $location;
160         }
161
162         if (!$profile->update($orig_profile)) {
163             $this->serverError(_('Could not save new profile info'), 500);
164             return false;
165         } else {
166             if ($avatar) {
167                 $temp_filename = tempnam(sys_get_temp_dir(), 'listenee_avatar');
168                 copy($avatar, $temp_filename);
169                 $imagefile = new ImageFile($profile->id, $temp_filename);
170                 $filename = Avatar::filename($profile->id,
171                                      image_type_to_extension($imagefile->type),
172                                      null,
173                                      common_timestamp());
174                 rename($temp_filename, Avatar::path($filename));
175                 if (!$profile->setOriginal($filename)) {
176                     $this->serverError(_('Could not save avatar info'), 500);
177                     return false;
178                 }
179             }
180             return true;
181         }
182     }
183 }