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