2008-05-27 20:42:19 +09:00
|
|
|
<?php
|
2009-08-10 21:48:50 +09:00
|
|
|
/**
|
|
|
|
* Handler for remote subscription finish callback
|
|
|
|
*
|
|
|
|
* PHP version 5
|
|
|
|
*
|
|
|
|
* @category Action
|
2009-08-28 03:16:45 +09:00
|
|
|
* @package StatusNet
|
|
|
|
* @author Evan Prodromou <evan@status.net>
|
|
|
|
* @author Robin Millette <millette@status.net>
|
2009-08-10 21:48:50 +09:00
|
|
|
* @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
|
2009-08-28 03:16:45 +09:00
|
|
|
* @link http://status.net/
|
2009-08-10 21:48:50 +09:00
|
|
|
*
|
2009-08-26 07:14:12 +09:00
|
|
|
* StatusNet - the distributed open-source microblogging tool
|
2009-08-26 07:12:20 +09:00
|
|
|
* Copyright (C) 2008, 2009, StatusNet, Inc.
|
2008-05-27 20:42:19 +09:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-08-10 21:48:50 +09:00
|
|
|
**/
|
2008-05-27 20:42:19 +09:00
|
|
|
|
2009-08-26 23:41:36 +09:00
|
|
|
if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
|
2008-05-27 20:42:19 +09:00
|
|
|
|
2009-08-10 21:48:50 +09:00
|
|
|
require_once INSTALLDIR.'/extlib/libomb/service_consumer.php';
|
|
|
|
require_once INSTALLDIR.'/lib/omb.php';
|
2008-05-27 20:42:19 +09:00
|
|
|
|
2009-08-10 21:48:50 +09:00
|
|
|
/**
|
|
|
|
* Handler for remote subscription finish callback
|
|
|
|
*
|
|
|
|
* When a remote user subscribes a local user, a redirect to this action is
|
|
|
|
* issued after the remote user authorized his service to subscribe.
|
|
|
|
*
|
|
|
|
* @category Action
|
|
|
|
* @package Laconica
|
|
|
|
* @author Evan Prodromou <evan@controlyourself.ca>
|
|
|
|
* @author Robin Millette <millette@controlyourself.ca>
|
|
|
|
* @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
|
|
|
|
* @link http://laconi.ca/
|
|
|
|
*/
|
2008-12-24 04:49:23 +09:00
|
|
|
class FinishremotesubscribeAction extends Action
|
|
|
|
{
|
2008-07-08 18:51:13 +09:00
|
|
|
|
2009-08-10 21:48:50 +09:00
|
|
|
/**
|
|
|
|
* Class handler.
|
|
|
|
*
|
|
|
|
* @param array $args query arguments
|
|
|
|
*
|
|
|
|
* @return nothing
|
|
|
|
*
|
|
|
|
**/
|
2008-12-24 04:33:23 +09:00
|
|
|
function handle($args)
|
|
|
|
{
|
2008-12-24 04:19:07 +09:00
|
|
|
parent::handle($args);
|
2008-05-27 20:42:19 +09:00
|
|
|
|
2009-08-10 21:48:50 +09:00
|
|
|
/* Restore session data. RemotesubscribeAction should have stored
|
|
|
|
this entry. */
|
|
|
|
$service = unserialize($_SESSION['oauth_authorization_request']);
|
2008-12-24 04:19:07 +09:00
|
|
|
|
2009-08-10 21:48:50 +09:00
|
|
|
if (!$service) {
|
2009-01-16 08:03:38 +09:00
|
|
|
$this->clientError(_('Not expecting this response!'));
|
2008-12-24 04:19:07 +09:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-08-10 21:48:50 +09:00
|
|
|
common_debug('stored request: '. print_r($service, true), __FILE__);
|
2008-07-08 18:51:13 +09:00
|
|
|
|
2009-08-10 21:48:50 +09:00
|
|
|
/* Create user objects for both users. Do it early for request
|
|
|
|
validation. */
|
2009-08-21 19:13:24 +09:00
|
|
|
$user = User::staticGet('uri', $service->getListeneeURI());
|
2008-12-24 04:19:07 +09:00
|
|
|
|
|
|
|
if (!$user) {
|
2009-08-21 19:13:24 +09:00
|
|
|
$this->clientError(_('User being listened to does not exist.'));
|
2008-12-24 04:19:07 +09:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-08-10 21:48:50 +09:00
|
|
|
$other = User::staticGet('uri', $service->getListenerURI());
|
2008-12-24 04:19:07 +09:00
|
|
|
|
|
|
|
if ($other) {
|
2009-01-16 08:03:38 +09:00
|
|
|
$this->clientError(_('You can use the local subscription!'));
|
2008-12-24 04:19:07 +09:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-08-21 19:13:24 +09:00
|
|
|
$remote = Remote_profile::staticGet('uri', $service->getListenerURI());
|
|
|
|
|
|
|
|
$profile = Profile::staticGet($remote->id);
|
|
|
|
|
|
|
|
if ($user->hasBlocked($profile)) {
|
|
|
|
$this->clientError(_('That user has blocked you from subscribing.'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-08-10 21:48:50 +09:00
|
|
|
/* Perform the handling itself via libomb. */
|
|
|
|
try {
|
2009-08-21 19:13:24 +09:00
|
|
|
$service->finishAuthorization();
|
2009-08-10 21:48:50 +09:00
|
|
|
} catch (OAuthException $e) {
|
|
|
|
if ($e->getMessage() == 'The authorized token does not equal the ' .
|
|
|
|
'submitted token.') {
|
2009-08-21 19:13:24 +09:00
|
|
|
$this->clientError(_('You are not authorized.'));
|
2008-12-24 04:19:07 +09:00
|
|
|
return;
|
2009-08-10 21:48:50 +09:00
|
|
|
} else {
|
2009-08-21 19:13:24 +09:00
|
|
|
$this->clientError(_('Could not convert request token to ' .
|
2009-08-10 21:48:50 +09:00
|
|
|
'access token.'));
|
2008-12-24 04:19:07 +09:00
|
|
|
return;
|
|
|
|
}
|
2009-08-10 21:48:50 +09:00
|
|
|
} catch (OMB_RemoteServiceException $e) {
|
2009-08-21 19:13:24 +09:00
|
|
|
$this->clientError(_('Remote service uses unknown version of ' .
|
|
|
|
'OMB protocol.'));
|
2009-08-10 21:48:50 +09:00
|
|
|
return;
|
|
|
|
} catch (Exception $e) {
|
|
|
|
common_debug('Got exception ' . print_r($e, true), __FILE__);
|
|
|
|
$this->clientError($e->getMessage());
|
2008-12-08 12:15:32 +09:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-08-10 21:48:50 +09:00
|
|
|
/* The service URLs are not accessible from datastore, so setting them
|
|
|
|
after insertion of the profile. */
|
|
|
|
$orig_remote = clone($remote);
|
2008-12-15 22:48:24 +09:00
|
|
|
|
2009-08-10 21:48:50 +09:00
|
|
|
$remote->postnoticeurl =
|
|
|
|
$service->getServiceURI(OMB_ENDPOINT_POSTNOTICE);
|
|
|
|
$remote->updateprofileurl =
|
|
|
|
$service->getServiceURI(OMB_ENDPOINT_UPDATEPROFILE);
|
2008-12-15 22:48:24 +09:00
|
|
|
|
2009-08-10 21:48:50 +09:00
|
|
|
if (!$remote->update($orig_remote)) {
|
|
|
|
$this->serverError(_('Error updating remote profile'));
|
|
|
|
return;
|
2008-12-24 04:19:07 +09:00
|
|
|
}
|
2008-05-27 20:42:19 +09:00
|
|
|
|
2009-08-10 21:48:50 +09:00
|
|
|
/* Clear the session data. */
|
2008-12-24 04:19:07 +09:00
|
|
|
unset($_SESSION['oauth_authorization_request']);
|
2008-07-08 18:51:13 +09:00
|
|
|
|
2009-08-10 21:48:50 +09:00
|
|
|
/* If we show subscriptions in reverse chronological order, the new one
|
|
|
|
should show up close to the top of the page. */
|
2008-12-24 04:19:07 +09:00
|
|
|
common_redirect(common_local_url('subscribers', array('nickname' =>
|
2009-04-02 04:30:59 +09:00
|
|
|
$user->nickname)),
|
|
|
|
303);
|
2008-12-24 04:19:07 +09:00
|
|
|
}
|
2008-07-08 18:51:13 +09:00
|
|
|
|
2008-12-24 04:33:23 +09:00
|
|
|
function add_avatar($profile, $url)
|
|
|
|
{
|
2008-12-24 04:19:07 +09:00
|
|
|
$temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
|
|
|
|
copy($url, $temp_filename);
|
2009-02-10 08:13:11 +09:00
|
|
|
$imagefile = new ImageFile($profile->id, $temp_filename);
|
|
|
|
$filename = Avatar::filename($profile->id,
|
|
|
|
image_type_to_extension($imagefile->type),
|
|
|
|
null,
|
|
|
|
common_timestamp());
|
|
|
|
rename($temp_filename, Avatar::path($filename));
|
|
|
|
return $profile->setOriginal($filename);
|
2008-12-24 04:19:07 +09:00
|
|
|
}
|
2008-07-08 18:51:13 +09:00
|
|
|
|
2008-12-24 04:33:23 +09:00
|
|
|
function access_token($omb)
|
|
|
|
{
|
2008-06-06 15:29:58 +09:00
|
|
|
|
2008-12-24 04:19:07 +09:00
|
|
|
common_debug('starting request for access token', __FILE__);
|
2008-07-08 18:51:13 +09:00
|
|
|
|
2008-12-24 04:19:07 +09:00
|
|
|
$con = omb_oauth_consumer();
|
|
|
|
$tok = new OAuthToken($omb['token'], $omb['secret']);
|
2008-05-27 20:42:19 +09:00
|
|
|
|
2008-12-24 04:19:07 +09:00
|
|
|
common_debug('using request token "'.$tok.'"', __FILE__);
|
2008-07-08 18:51:13 +09:00
|
|
|
|
2008-12-24 04:19:07 +09:00
|
|
|
$url = $omb['access_token_url'];
|
2008-06-06 15:29:58 +09:00
|
|
|
|
2008-12-24 04:19:07 +09:00
|
|
|
common_debug('using access token url "'.$url.'"', __FILE__);
|
2008-07-08 18:51:13 +09:00
|
|
|
|
2008-12-24 04:19:07 +09:00
|
|
|
# XXX: Is this the right thing to do? Strip off GET params and make them
|
|
|
|
# POST params? Seems wrong to me.
|
2008-07-08 18:51:13 +09:00
|
|
|
|
2008-12-24 04:19:07 +09:00
|
|
|
$parsed = parse_url($url);
|
|
|
|
$params = array();
|
|
|
|
parse_str($parsed['query'], $params);
|
2008-05-27 20:42:19 +09:00
|
|
|
|
2008-12-24 04:19:07 +09:00
|
|
|
$req = OAuthRequest::from_consumer_and_token($con, $tok, "POST", $url, $params);
|
2008-07-08 18:51:13 +09:00
|
|
|
|
2008-12-24 04:19:07 +09:00
|
|
|
$req->set_parameter('omb_version', OMB_VERSION_01);
|
2008-07-08 18:51:13 +09:00
|
|
|
|
2008-12-24 04:19:07 +09:00
|
|
|
# XXX: test to see if endpoint accepts this signature method
|
2008-05-27 20:42:19 +09:00
|
|
|
|
2008-12-24 04:19:07 +09:00
|
|
|
$req->sign_request(omb_hmac_sha1(), $con, $tok);
|
2008-07-08 18:51:13 +09:00
|
|
|
|
2008-12-24 04:19:07 +09:00
|
|
|
# We re-use this tool's fetcher, since it's pretty good
|
2008-06-06 15:29:58 +09:00
|
|
|
|
2008-12-24 04:19:07 +09:00
|
|
|
common_debug('posting to access token url "'.$req->get_normalized_http_url().'"', __FILE__);
|
|
|
|
common_debug('posting request data "'.$req->to_postdata().'"', __FILE__);
|
2008-07-08 18:51:13 +09:00
|
|
|
|
2008-12-24 04:19:07 +09:00
|
|
|
$fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
|
|
|
|
$result = $fetcher->post($req->get_normalized_http_url(),
|
|
|
|
$req->to_postdata(),
|
2009-08-26 07:42:34 +09:00
|
|
|
array('User-Agent: StatusNet/' . STATUSNET_VERSION));
|
2008-06-06 15:29:58 +09:00
|
|
|
|
2008-12-24 04:44:28 +09:00
|
|
|
common_debug('got result: "'.print_r($result,true).'"', __FILE__);
|
2008-07-08 18:51:13 +09:00
|
|
|
|
2008-12-24 04:19:07 +09:00
|
|
|
if ($result->status != 200) {
|
2008-12-24 04:21:29 +09:00
|
|
|
return null;
|
2008-12-24 04:19:07 +09:00
|
|
|
}
|
2008-05-27 20:42:19 +09:00
|
|
|
|
2008-12-24 04:19:07 +09:00
|
|
|
parse_str($result->body, $return);
|
2008-07-08 18:51:13 +09:00
|
|
|
|
2008-12-24 04:19:07 +09:00
|
|
|
return array($return['oauth_token'], $return['oauth_token_secret']);
|
|
|
|
}
|
2008-09-04 15:55:04 +09:00
|
|
|
}
|