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