]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/updateprofile.php
replace all tabs with four spaces
[quix0rs-gnu-social.git] / actions / updateprofile.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, 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     function handle($args) {
27         parent::handle($args);
28         try {
29             common_remove_magic_from_request();
30             $req = OAuthRequest::from_request();
31             # Note: server-to-server function!
32             $server = omb_oauth_server();
33             list($consumer, $token) = $server->verify_request($req);
34             if ($this->update_profile($req, $consumer, $token)) {
35                 print "omb_version=".OMB_VERSION_01;
36             }
37         } catch (OAuthException $e) {
38             $this->server_error($e->getMessage());
39             return;
40         }
41     }
42
43     function update_profile($req, $consumer, $token) {
44         $version = $req->get_parameter('omb_version');
45         if ($version != OMB_VERSION_01) {
46             $this->client_error(_('Unsupported OMB version'), 400);
47             return false;
48         }
49         # First, check to see if listenee exists
50         $listenee =  $req->get_parameter('omb_listenee');
51         $remote = Remote_profile::staticGet('uri', $listenee);
52         if (!$remote) {
53             $this->client_error(_('Profile unknown'), 404);
54             return false;
55         }
56         # Second, check to see if they should be able to post updates!
57         # We see if there are any subscriptions to that remote user with
58         # the given token.
59
60         $sub = new Subscription();
61         $sub->subscribed = $remote->id;
62         $sub->token = $token->key;
63         if (!$sub->find(true)) {
64             $this->client_error(_('You did not send us that profile'), 403);
65             return false;
66         }
67
68         $profile = Profile::staticGet('id', $remote->id);
69         if (!$profile) {
70             # This one is our fault
71             $this->server_error(_('Remote profile with no matching profile'), 500);
72             return false;
73         }
74         $nickname = $req->get_parameter('omb_listenee_nickname');
75         if ($nickname && !Validate::string($nickname, array('min_length' => 1,
76                                                             'max_length' => 64,
77                                                             'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
78             $this->client_error(_('Nickname must have only lowercase letters and numbers and no spaces.'));
79             return false;
80         }
81         $license = $req->get_parameter('omb_listenee_license');
82         if ($license && !common_valid_http_url($license)) {
83             $this->client_error(sprintf(_("Invalid license URL '%s'"), $license));
84             return false;
85         }
86         $profile_url = $req->get_parameter('omb_listenee_profile');
87         if ($profile_url && !common_valid_http_url($profile_url)) {
88             $this->client_error(sprintf(_("Invalid profile URL '%s'."), $profile_url));
89             return false;
90         }
91         # optional stuff
92         $fullname = $req->get_parameter('omb_listenee_fullname');
93         if ($fullname && strlen($fullname) > 255) {
94             $this->client_error(_("Full name is too long (max 255 chars)."));
95             return false;
96         }
97         $homepage = $req->get_parameter('omb_listenee_homepage');
98         if ($homepage && (!common_valid_http_url($homepage) || strlen($homepage) > 255)) {
99             $this->client_error(sprintf(_("Invalid homepage '%s'"), $homepage));
100             return false;
101         }
102         $bio = $req->get_parameter('omb_listenee_bio');
103         if ($bio && strlen($bio) > 140) {
104             $this->client_error(_("Bio is too long (max 140 chars)."));
105             return false;
106         }
107         $location = $req->get_parameter('omb_listenee_location');
108         if ($location && strlen($location) > 255) {
109             $this->client_error(_("Location is too long (max 255 chars)."));
110             return false;
111         }
112         $avatar = $req->get_parameter('omb_listenee_avatar');
113         if ($avatar) {
114             if (!common_valid_http_url($avatar) || strlen($avatar) > 255) {
115                 $this->client_error(sprintf(_("Invalid avatar URL '%s'"), $avatar));
116                 return false;
117             }
118             $size = @getimagesize($avatar);
119             if (!$size) {
120                 $this->client_error(sprintf(_("Can't read avatar URL '%s'"), $avatar));
121                 return false;
122             }
123             if ($size[0] != AVATAR_PROFILE_SIZE || $size[1] != AVATAR_PROFILE_SIZE) {
124                 $this->client_error(sprintf(_("Wrong size image at '%s'"), $avatar));
125                 return false;
126             }
127             if (!in_array($size[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG,
128                                           IMAGETYPE_PNG))) {
129                 $this->client_error(sprintf(_("Wrong image type for '%s'"), $avatar));
130                 return false;
131             }
132         }
133
134         $orig_profile = clone($profile);
135
136         if ($nickname) {
137             $profile->nickname = $nickname;
138         }
139         if ($profile_url) {
140             $profile->profileurl = $profile_url;
141         }
142         if ($fullname) {
143             $profile->fullname = $fullname;
144         }
145         if ($homepage) {
146             $profile->homepage = $homepage;
147         }
148         if ($bio) {
149             $profile->bio = $bio;
150         }
151         if ($location) {
152             $profile->location = $location;
153         }
154
155         if (!$profile->update($orig_profile)) {
156             $this->server_error(_('Could not save new profile info'), 500);
157             return false;
158         } else {
159             if ($avatar) {
160                 $temp_filename = tempnam(sys_get_temp_dir(), 'listenee_avatar');
161                 copy($avatar, $temp_filename);
162                 if (!$profile->setOriginal($temp_filename)) {
163                     $this->server_error(_('Could not save avatar info'), 500);
164                     return false;
165                 }
166             }
167             header('HTTP/1.1 200 OK');
168             header('Content-type: text/plain');
169             print 'Updated profile';
170             print "\n";
171             return true;
172         }
173     }
174 }