View source
<?php
require_once 'LingotekOAuthRequestVerifier.php';
require_once 'OAuthSession.php';
class LingotekOAuthServer extends LingotekOAuthRequestVerifier {
protected $session;
protected $allowed_uri_schemes = array(
'http',
'https',
);
protected $disallowed_uri_schemes = array(
'file',
'callto',
'mailto',
);
function __construct($uri = null, $method = null, $params = null, $store = 'SESSION', $store_options = array(), $options = array()) {
parent::__construct($uri, $method, $params);
$this->session = OAuthSession::instance($store, $store_options);
if (array_key_exists('allowed_uri_schemes', $options) && is_array($options['allowed_uri_schemes'])) {
$this->allowed_uri_schemes = $options['allowed_uri_schemes'];
}
if (array_key_exists('disallowed_uri_schemes', $options) && is_array($options['disallowed_uri_schemes'])) {
$this->disallowed_uri_schemes = $options['disallowed_uri_schemes'];
}
}
public function requestToken() {
LingotekOAuthRequestLogger::start($this);
try {
$this
->verify(false);
$options = array();
$ttl = $this
->getParam('xoauth_token_ttl', false);
if ($ttl) {
$options['token_ttl'] = $ttl;
}
$cbUrl = $this
->getParam('oauth_callback', true);
if ($cbUrl) {
$options['oauth_callback'] = $cbUrl;
}
$store = OAuthStore::instance();
$token = $store
->addConsumerRequestToken($this
->getParam('oauth_consumer_key', true), $options);
$result = 'oauth_callback_confirmed=1&oauth_token=' . $this
->urlencode($token['token']) . '&oauth_token_secret=' . $this
->urlencode($token['token_secret']);
if (!empty($token['token_ttl'])) {
$result .= '&xoauth_token_ttl=' . $this
->urlencode($token['token_ttl']);
}
$request_token = $token['token'];
header('HTTP/1.1 200 OK');
header('Content-Length: ' . strlen($result));
header('Content-Type: application/x-www-form-urlencoded');
echo $result;
} catch (OAuthException2 $e) {
$request_token = false;
header('HTTP/1.1 401 Unauthorized');
header('Content-Type: text/plain');
echo "OAuth Verification Failed: " . $e
->getMessage();
}
LingotekOAuthRequestLogger::flush();
return $request_token;
}
public function authorizeVerify() {
LingotekOAuthRequestLogger::start($this);
$store = OAuthStore::instance();
$token = $this
->getParam('oauth_token', true);
$rs = $store
->getConsumerRequestToken($token);
if (empty($rs)) {
throw new OAuthException2('Unknown request token "' . $token . '"');
}
$verify_oauth_token = $this->session
->get('verify_oauth_token');
if (empty($verify_oauth_token) || strcmp($verify_oauth_token, $rs['token'])) {
$this->session
->set('verify_oauth_token', $rs['token']);
$this->session
->set('verify_oauth_consumer_key', $rs['consumer_key']);
$cb = $this
->getParam('oauth_callback', true);
if ($cb) {
$this->session
->set('verify_oauth_callback', $cb);
}
else {
$this->session
->set('verify_oauth_callback', $rs['callback_url']);
}
}
LingotekOAuthRequestLogger::flush();
return $rs;
}
public function authorizeFinish($authorized, $user_id) {
LingotekOAuthRequestLogger::start($this);
$token = $this
->getParam('oauth_token', true);
$verifier = null;
if ($this->session
->get('verify_oauth_token') == $token) {
$store = OAuthStore::instance();
$referrer_host = '';
$oauth_callback = false;
$verify_oauth_callback = $this->session
->get('verify_oauth_callback');
if (!empty($verify_oauth_callback) && $verify_oauth_callback != 'oob') {
$oauth_callback = $this->session
->get('verify_oauth_callback');
$ps = parse_url($oauth_callback);
if (isset($ps['host'])) {
$referrer_host = $ps['host'];
}
}
if ($authorized) {
LingotekOAuthRequestLogger::addNote('Authorized token "' . $token . '" for user ' . $user_id . ' with referrer "' . $referrer_host . '"');
$verifier = $store
->authorizeConsumerRequestToken($token, $user_id, $referrer_host);
}
else {
LingotekOAuthRequestLogger::addNote('Authorization rejected for token "' . $token . '" for user ' . $user_id . "\nToken has been deleted");
$store
->deleteConsumerRequestToken($token);
}
if (!empty($oauth_callback)) {
$params = array(
'oauth_token' => rawurlencode($token),
);
if ($verifier) {
$params['oauth_verifier'] = $verifier;
}
$uri = preg_replace('/\\s/', '%20', $oauth_callback);
if (!empty($this->allowed_uri_schemes)) {
if (!in_array(substr($uri, 0, strpos($uri, '://')), $this->allowed_uri_schemes)) {
throw new OAuthException2('Illegal protocol in redirect uri ' . $uri);
}
}
else {
if (!empty($this->disallowed_uri_schemes)) {
if (in_array(substr($uri, 0, strpos($uri, '://')), $this->disallowed_uri_schemes)) {
throw new OAuthException2('Illegal protocol in redirect uri ' . $uri);
}
}
}
$this
->redirect($oauth_callback, $params);
}
}
LingotekOAuthRequestLogger::flush();
return $verifier;
}
public function accessToken() {
LingotekOAuthRequestLogger::start($this);
try {
$this
->verify('request');
$options = array();
$ttl = $this
->getParam('xoauth_token_ttl', false);
if ($ttl) {
$options['token_ttl'] = $ttl;
}
$verifier = $this
->getParam('oauth_verifier', false);
if ($verifier) {
$options['verifier'] = $verifier;
}
$store = OAuthStore::instance();
$token = $store
->exchangeConsumerRequestForAccessToken($this
->getParam('oauth_token', true), $options);
$result = 'oauth_token=' . $this
->urlencode($token['token']) . '&oauth_token_secret=' . $this
->urlencode($token['token_secret']);
if (!empty($token['token_ttl'])) {
$result .= '&xoauth_token_ttl=' . $this
->urlencode($token['token_ttl']);
}
header('HTTP/1.1 200 OK');
header('Content-Length: ' . strlen($result));
header('Content-Type: application/x-www-form-urlencoded');
echo $result;
} catch (OAuthException2 $e) {
header('HTTP/1.1 401 Access Denied');
header('Content-Type: text/plain');
echo "OAuth Verification Failed: " . $e
->getMessage();
}
LingotekOAuthRequestLogger::flush();
exit;
}
}