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