3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, Inc.
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.
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.
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/>.
20 if (!defined('LACONICA')) { exit(1); }
22 require_once(INSTALLDIR.'/lib/omb.php');
24 class UpdateprofileAction extends Action {
26 function is_readonly() {
30 function handle($args) {
31 parent::handle($args);
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;
40 } catch (OAuthException $e) {
41 $this->server_error($e->getMessage());
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);
52 # First, check to see if listenee exists
53 $listenee = $req->get_parameter('omb_listenee');
54 $remote = Remote_profile::staticGet('uri', $listenee);
56 $this->client_error(_('Profile unknown'), 404);
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
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);
71 $profile = Profile::staticGet('id', $remote->id);
73 # This one is our fault
74 $this->server_error(_('Remote profile with no matching profile'), 500);
77 $nickname = $req->get_parameter('omb_listenee_nickname');
78 if ($nickname && !Validate::string($nickname, array('min_length' => 1,
80 'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
81 $this->client_error(_('Nickname must have only lowercase letters and numbers and no spaces.'));
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));
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));
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)."));
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));
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)."));
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)."));
115 $avatar = $req->get_parameter('omb_listenee_avatar');
117 if (!common_valid_http_url($avatar) || strlen($avatar) > 255) {
118 $this->client_error(sprintf(_("Invalid avatar URL '%s'"), $avatar));
121 $size = @getimagesize($avatar);
123 $this->client_error(sprintf(_("Can't read avatar URL '%s'"), $avatar));
126 if ($size[0] != AVATAR_PROFILE_SIZE || $size[1] != AVATAR_PROFILE_SIZE) {
127 $this->client_error(sprintf(_("Wrong size image at '%s'"), $avatar));
130 if (!in_array($size[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG,
132 $this->client_error(sprintf(_("Wrong image type for '%s'"), $avatar));
137 $orig_profile = clone($profile);
140 $profile->nickname = $nickname;
143 $profile->profileurl = $profile_url;
146 $profile->fullname = $fullname;
149 $profile->homepage = $homepage;
152 $profile->bio = $bio;
155 $profile->location = $location;
158 if (!$profile->update($orig_profile)) {
159 $this->server_error(_('Could not save new profile info'), 500);
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);
170 header('HTTP/1.1 200 OK');
171 header('Content-type: text/plain');
172 print 'Updated profile';