Update to last upstream version of libomb: coding style updates, static call fix, improved handling of invalid XRD URIs.

This commit is contained in:
Brion Vibber 2010-06-29 14:39:57 -04:00
parent c038164c0f
commit 15b1d130d2
16 changed files with 2072 additions and 1792 deletions

View File

@ -1,14 +1,6 @@
<?php <?php
require_once 'xrds_mapper.php';
require_once 'constants.php';
/** /**
* Map XRDS actions to URLs using base URLs. * This file is part of libomb
*
* This interface specifies classes which write the XRDS file announcing
* the OMB server. An instance of an implementing class should be passed to
* OMB_Service_Provider->writeXRDS.
* *
* PHP version 5 * PHP version 5
* *
@ -27,15 +19,34 @@ require_once 'constants.php';
* *
* @package OMB * @package OMB
* @author Adrian Lang <mail@adrianlang.de> * @author Adrian Lang <mail@adrianlang.de>
* @copyright 2009 Adrian Lang
* @license http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0
**/ * @version 0.1a-20090828
* @link http://adrianlang.de/libomb
*/
class OMB_Base_URL_XRDS_Mapper implements OMB_XRDS_Mapper { require_once 'xrds_mapper.php';
require_once 'constants.php';
/**
* Map XRDS actions to URLs using base URLs
*
* This class realizes a simple mapping of action URIs to handler URLs. The
* target URLs are constructed using a base URL.
*/
class OMB_Base_URL_XRDS_Mapper implements OMB_XRDS_Mapper
{
protected $urls; protected $urls;
public function __construct($oauth_base, $omb_base) { /**
* Constructor
*
* Initialize the XRDS mapper with base URLs for OAuth and OMB endpoints.
*
* @param string $oauth_base The base URL for OAuth endpoints
* @param string $omb_base The base URL for OMB endpoints
*/
public function __construct($oauth_base, $omb_base)
{
$this->urls = array( $this->urls = array(
OAUTH_ENDPOINT_REQUEST => $oauth_base . 'requesttoken', OAUTH_ENDPOINT_REQUEST => $oauth_base . 'requesttoken',
OAUTH_ENDPOINT_AUTHORIZE => $oauth_base . 'userauthorization', OAUTH_ENDPOINT_AUTHORIZE => $oauth_base . 'userauthorization',
@ -44,7 +55,17 @@ class OMB_Base_URL_XRDS_Mapper implements OMB_XRDS_Mapper {
OMB_ENDPOINT_UPDATEPROFILE => $omb_base . 'updateprofile'); OMB_ENDPOINT_UPDATEPROFILE => $omb_base . 'updateprofile');
} }
public function getURL($action) { /**
* Fetch an URL for a specified action
*
* Returns the action URL for an action specified by the endpoint URI.
*
* @param string $action The endpoint URI
*
* @return string The action URL
*/
public function getURL($action)
{
return $this->urls[$action]; return $this->urls[$action];
} }
} }

View File

@ -22,13 +22,14 @@
* *
* @package OMB * @package OMB
* @author Adrian Lang <mail@adrianlang.de> * @author Adrian Lang <mail@adrianlang.de>
* @copyright 2009 Adrian Lang
* @license http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0
**/ * @version 0.1a-20090828
* @link http://adrianlang.de/libomb
*/
/** /**
* The OMB constants. * The OMB constants.
**/ */
define('OMB_VERSION_01', 'http://openmicroblogging.org/protocol/0.1'); define('OMB_VERSION_01', 'http://openmicroblogging.org/protocol/0.1');
@ -40,7 +41,7 @@ define('OMB_ENDPOINT_POSTNOTICE', OMB_VERSION . '/postNotice');
/** /**
* The OAuth constants. * The OAuth constants.
**/ */
define('OAUTH_NAMESPACE', 'http://oauth.net/core/1.0/'); define('OAUTH_NAMESPACE', 'http://oauth.net/core/1.0/');

View File

@ -1,4 +1,28 @@
<?php <?php
/**
* This file is part of libomb
*
* PHP version 5
*
* LICENSE: 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/>.
*
* @package OMB
* @author Adrian Lang <mail@adrianlang.de>
* @license http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0
* @version 0.1a-20090828
* @link http://adrianlang.de/libomb
*/
require_once 'OAuth.php'; require_once 'OAuth.php';
@ -27,29 +51,9 @@ require_once 'OAuth.php';
* Most of the parameters passed to these methods are unescaped and unverified * Most of the parameters passed to these methods are unescaped and unverified
* user input. Therefore they should be handled with extra care to avoid * user input. Therefore they should be handled with extra care to avoid
* security problems like SQL injections. * security problems like SQL injections.
* */
* PHP version 5 class OMB_Datastore extends OAuthDataStore
* {
* LICENSE: 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/>.
*
* @package OMB
* @author Adrian Lang <mail@adrianlang.de>
* @copyright 2009 Adrian Lang
* @license http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0
**/
class OMB_Datastore extends OAuthDataStore {
/********* /*********
* OAUTH * * OAUTH *
@ -64,8 +68,9 @@ class OMB_Datastore extends OAuthDataStore {
* @param string $token_key The key of the token to be revoked * @param string $token_key The key of the token to be revoked
* *
* @access public * @access public
**/ */
public function revoke_token($token_key) { public function revoke_token($token_key)
{
throw new Exception(); throw new Exception();
} }
@ -78,8 +83,9 @@ class OMB_Datastore extends OAuthDataStore {
* @param string $token_key The key of the token to be authorized * @param string $token_key The key of the token to be authorized
* *
* @access public * @access public
**/ */
public function authorize_token($token_key) { public function authorize_token($token_key)
{
throw new Exception(); throw new Exception();
} }
@ -101,8 +107,9 @@ class OMB_Datastore extends OAuthDataStore {
* @access public * @access public
* *
* @return OMB_Profile The corresponding profile * @return OMB_Profile The corresponding profile
**/ */
public function getProfile($identifier_uri) { public function getProfile($identifier_uri)
{
throw new Exception(); throw new Exception();
} }
@ -115,25 +122,27 @@ class OMB_Datastore extends OAuthDataStore {
* @param OMB_Profile $profile The OMB profile which should be saved * @param OMB_Profile $profile The OMB profile which should be saved
* *
* @access public * @access public
**/ */
public function saveProfile($profile) { public function saveProfile($profile)
{
throw new Exception(); throw new Exception();
} }
/** /**
* Save passed notice * Save passed notice
* *
* Stores the OMB notice $notice. The datastore may change the passed notice. * Stores the OMB notice $notice. The datastore may change the passed
* This might by neccessary for URIs depending on a database key. Note that * notice. This might by necessary for URIs depending on a database key.
* it is the users duty to present a mechanism for his OMB_Datastore to * Note that it is the users duty to present a mechanism for his
* appropriately change his OMB_Notice. TODO: Ugly. * OMB_Datastore to appropriately change his OMB_Notice.
* Throws exceptions in case of error. * Throws exceptions in case of error.
* *
* @param OMB_Notice $notice The OMB notice which should be saved * @param OMB_Notice &$notice The OMB notice which should be saved
* *
* @access public * @access public
**/ */
public function saveNotice(&$notice) { public function saveNotice(&$notice)
{
throw new Exception(); throw new Exception();
} }
@ -154,8 +163,9 @@ class OMB_Datastore extends OAuthDataStore {
* *
* @return mixed An array containing the subscriptions or 0 if no * @return mixed An array containing the subscriptions or 0 if no
* subscription has been found. * subscription has been found.
**/ */
public function getSubscriptions($subscribed_user_uri) { public function getSubscriptions($subscribed_user_uri)
{
throw new Exception(); throw new Exception();
} }
@ -172,8 +182,9 @@ class OMB_Datastore extends OAuthDataStore {
* subscribed profile * subscribed profile
* *
* @access public * @access public
**/ */
public function deleteSubscription($subscriber_uri, $subscribed_user_uri) { public function deleteSubscription($subscriber_uri, $subscribed_user_uri)
{
throw new Exception(); throw new Exception();
} }
@ -191,9 +202,10 @@ class OMB_Datastore extends OAuthDataStore {
* @param OAuthToken $token The access token * @param OAuthToken $token The access token
* *
* @access public * @access public
**/ */
public function saveSubscription($subscriber_uri, $subscribed_user_uri, public function saveSubscription($subscriber_uri, $subscribed_user_uri,
$token) { $token)
{
throw new Exception(); throw new Exception();
} }
} }

View File

@ -1,11 +1,6 @@
<?php <?php
require_once 'Validate.php';
/** /**
* Helper functions for libomb * This file is part of libomb
*
* This file contains helper functions for libomb.
* *
* PHP version 5 * PHP version 5
* *
@ -24,11 +19,20 @@ require_once 'Validate.php';
* *
* @package OMB * @package OMB
* @author Adrian Lang <mail@adrianlang.de> * @author Adrian Lang <mail@adrianlang.de>
* @copyright 2009 Adrian Lang
* @license http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0
**/ * @version 0.1a-20090828
* @link http://adrianlang.de/libomb
*/
class OMB_Helper { require_once 'Validate.php';
/**
* Helper functions for libomb
*
* This class contains helper functions for libomb.
*/
class OMB_Helper
{
/** /**
* Non-scalar constants * Non-scalar constants
@ -36,10 +40,11 @@ class OMB_Helper {
* The set of OMB and OAuth Services an OMB Server has to implement. * The set of OMB and OAuth Services an OMB Server has to implement.
*/ */
public static $OMB_SERVICES = public static $OMB_SERVICES = array(OMB_ENDPOINT_UPDATEPROFILE,
array(OMB_ENDPOINT_UPDATEPROFILE, OMB_ENDPOINT_POSTNOTICE); OMB_ENDPOINT_POSTNOTICE);
public static $OAUTH_SERVICES = public static $OAUTH_SERVICES = array(OAUTH_ENDPOINT_REQUEST,
array(OAUTH_ENDPOINT_REQUEST, OAUTH_ENDPOINT_AUTHORIZE, OAUTH_ENDPOINT_ACCESS); OAUTH_ENDPOINT_AUTHORIZE,
OAUTH_ENDPOINT_ACCESS);
/** /**
* Validate URL * Validate URL
@ -53,15 +58,17 @@ class OMB_Helper {
* *
* @access public * @access public
*/ */
public static function validateURL($url) { public static function validateURL($url)
return Validate::uri($url, array('allowed_schemes' => array('http', 'https', {
'gopher', 'ftp'))); return Validate::uri($url, array('allowed_schemes' => array('http',
'https', 'gopher', 'ftp')));
} }
/** /**
* Validate Media type * Validate Media type
* *
* Basic Media type validation. Checks for valid maintype and correct format. * Basic Media type validation. Checks for valid maintype and correct
* format.
* *
* @param string $mediatype The Media type which is to be validated. * @param string $mediatype The Media type which is to be validated.
* *
@ -69,15 +76,12 @@ class OMB_Helper {
* *
* @access public * @access public
*/ */
public static function validateMediaType($mediatype) { public static function validateMediaType($mediatype)
if (0 === preg_match('/^(\w+)\/([\w\d-+.]+)$/', $mediatype, $subtypes)) { {
return false; return preg_match('/^(\w+)\/([\w\d-+.]+)$/', $mediatype, $subtypes) > 0
} &&
if (!in_array(strtolower($subtypes[1]), array('application', 'audio', 'image', in_array(strtolower($subtypes[1]), array('application', 'audio',
'message', 'model', 'multipart', 'text', 'video'))) { 'image', 'message', 'model', 'multipart', 'text', 'video'));
return false;
}
return true;
} }
/** /**
@ -89,8 +93,9 @@ class OMB_Helper {
* *
* @access public * @access public
*/ */
public static function removeMagicQuotesFromRequest() { public static function removeMagicQuotesFromRequest()
if(get_magic_quotes_gpc() == 1) { {
if (get_magic_quotes_gpc() === 1) {
$_POST = array_map('stripslashes', $_POST); $_POST = array_map('stripslashes', $_POST);
$_GET = array_map('stripslashes', $_GET); $_GET = array_map('stripslashes', $_GET);
} }

View File

@ -1,8 +1,6 @@
<?php <?php
/** /**
* Exception stating that a passed parameter is invalid * This file is part of libomb
*
* This exception is raised when a parameter does not obey the OMB standard.
* *
* PHP version 5 * PHP version 5
* *
@ -21,12 +19,34 @@
* *
* @package OMB * @package OMB
* @author Adrian Lang <mail@adrianlang.de> * @author Adrian Lang <mail@adrianlang.de>
* @copyright 2009 Adrian Lang
* @license http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0
**/ * @version 0.1a-20090828
class OMB_InvalidParameterException extends Exception { * @link http://adrianlang.de/libomb
public function __construct($value, $type, $parameter) { */
parent::__construct("Invalid value $value for parameter $parameter in $type");
/**
* Exception stating that a passed parameter is invalid
*
* This exception is raised when a parameter does not obey the OMB standard.
*/
class OMB_InvalidParameterException extends Exception
{
/**
* Constructor
*
* Creates a new exception based on a parameter name, value, and object
* type.
*
* @param string $value The wrong value passed
* @param string $type The object type the parameter belongs to;
* Currently OMB uses profiles and notices
* @param string $parameter The name of the parameter the wrong value has
* been passed for
*/
public function __construct($value, $type, $parameter)
{
parent::__construct("Invalid value ${value} for parameter " .
"${parameter} in $type");
} }
} }
?> ?>

View File

@ -1,9 +1,6 @@
<?php <?php
/** /**
* Exception stating that a requested url does not resolve to a valid yadis * This file is part of libomb
*
* This exception is raised when OMB_Service is not able to discover a valid
* yadis location with XRDS.
* *
* PHP version 5 * PHP version 5
* *
@ -22,10 +19,19 @@
* *
* @package OMB * @package OMB
* @author Adrian Lang <mail@adrianlang.de> * @author Adrian Lang <mail@adrianlang.de>
* @copyright 2009 Adrian Lang
* @license http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0
**/ * @version 0.1a-20090828
class OMB_InvalidYadisException extends Exception { * @link http://adrianlang.de/libomb
*/
/**
* Exception stating that a requested url does not resolve to a valid yadis
*
* This exception is raised when OMB_Service is not able to discover a valid
* yadis location with XRDS.
*/
class OMB_InvalidYadisException extends Exception
{
} }
?> ?>

View File

@ -1,15 +1,6 @@
<?php <?php
require_once 'invalidparameterexception.php';
require_once 'Validate.php';
require_once 'helper.php';
/** /**
* OMB Notice representation * This file is part of libomb
*
* This class represents an OMB notice.
*
* Do not call the setters with null values. Instead, if you want to delete a
* field, pass an empty string. The getters will return null for empty fields.
* *
* PHP version 5 * PHP version 5
* *
@ -28,11 +19,25 @@ require_once 'helper.php';
* *
* @package OMB * @package OMB
* @author Adrian Lang <mail@adrianlang.de> * @author Adrian Lang <mail@adrianlang.de>
* @copyright 2009 Adrian Lang
* @license http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0
**/ * @version 0.1a-20090828
* @link http://adrianlang.de/libomb
*/
class OMB_Notice { require_once 'invalidparameterexception.php';
require_once 'Validate.php';
require_once 'helper.php';
/**
* OMB Notice representation
*
* This class represents an OMB notice.
*
* Do not call the setters with null values. Instead, if you want to delete a
* field, pass an empty string. The getters will return null for empty fields.
*/
class OMB_Notice
{
protected $author; protected $author;
protected $uri; protected $uri;
protected $content; protected $content;
@ -53,16 +58,17 @@ class OMB_Notice {
* Initializes the OMB_Notice object with author, uri and content. * Initializes the OMB_Notice object with author, uri and content.
* These parameters are mandatory for postNotice. * These parameters are mandatory for postNotice.
* *
* @param object $author An OMB_Profile object representing the author of the * @param object $author An OMB_Profile object representing the author of
* notice. * the notice.
* @param string $uri The notice URI as defined by the OMB. A unique and * @param string $uri The notice URI as defined by the OMB. A unique and
* unchanging identifier for a notice. * never changing identifier for a notice.
* @param string $content The content of the notice. 140 chars recommended, * @param string $content The content of the notice. 140 chars recommended,
* but there is no limit. * but there is no limit.
* *
* @access public * @access public
*/ */
public function __construct($author, $uri, $content) { public function __construct($author, $uri, $content)
{
$this->content = $content; $this->content = $content;
if (is_null($author)) { if (is_null($author)) {
throw new OMB_InvalidParameterException('', 'notice', 'omb_listenee'); throw new OMB_InvalidParameterException('', 'notice', 'omb_listenee');
@ -78,16 +84,17 @@ class OMB_Notice {
} }
/** /**
* Returns the notice as array * Return the notice as array
* *
* The method returns an array which contains the whole notice as array. The * Returns an array which contains the whole notice as array. The array is
* array is cached and only rebuilt on changes of the notice. * cached and only rebuilt on changes of the notice.
* Empty optional values are not passed. * Empty optional values are not passed.
* *
* @access public * @access public
* @returns array The notice as parameter array * @return array The notice as parameter array
*/ */
public function asParameters() { public function asParameters()
{
if ($this->param_array !== false) { if ($this->param_array !== false) {
return $this->param_array; return $this->param_array;
} }
@ -105,40 +112,43 @@ class OMB_Notice {
if (!is_null($this->seealso_url)) { if (!is_null($this->seealso_url)) {
$this->param_array['omb_seealso'] = $this->seealso_url; $this->param_array['omb_seealso'] = $this->seealso_url;
/* This is actually a free interpretation of the OMB standard. We assume /* This is actually a free interpretation of the OMB standard. We
that additional seealso parameters are not of any use if seealso itself assume that additional seealso parameters are not of any use if
is not set. */ seealso itself is not set. */
if (!is_null($this->seealso_disposition)) if (!is_null($this->seealso_disposition))
$this->param_array['omb_seealso_disposition'] = $this->param_array['omb_seealso_disposition'] =
$this->seealso_disposition; $this->seealso_disposition;
if (!is_null($this->seealso_mediatype)) if (!is_null($this->seealso_mediatype))
$this->param_array['omb_seealso_mediatype'] = $this->seealso_mediatype; $this->param_array['omb_seealso_mediatype'] =
$this->seealso_mediatype;
if (!is_null($this->seealso_license_url)) if (!is_null($this->seealso_license_url))
$this->param_array['omb_seealso_license'] = $this->seealso_license_url; $this->param_array['omb_seealso_license'] =
$this->seealso_license_url;
} }
return $this->param_array; return $this->param_array;
} }
/** /**
* Builds an OMB_Notice object from array * Build an OMB_Notice object from array
* *
* The method builds an OMB_Notice object from the passed parameters array. * Builds an OMB_Notice object from the passed parameters array. The array
* The array MUST provide a notice URI and content. The array fields HAVE TO * MUST provide a notice URI and content. The array fields HAVE TO be named
* be named according to the OMB standard, i. e. omb_notice_* and * according to the OMB standard, i. e. omb_notice_* and omb_seealso_*.
* omb_seealso_*. Values are handled as not passed if the corresponding array * Values are handled as not passed if the corresponding array fields are
* fields are not set or the empty string. * not set or the empty string.
* *
* @param object $author An OMB_Profile object representing the author of * @param object $author An OMB_Profile object representing the author
* the notice. * of the notice.
* @param string $parameters An array containing the notice parameters. * @param string $parameters An array containing the notice parameters.
* *
* @access public * @access public
* *
* @returns OMB_Notice The built OMB_Notice. * @returns OMB_Notice The built OMB_Notice.
*/ */
public static function fromParameters($author, $parameters) { public static function fromParameters($author, $parameters)
{
$notice = new OMB_Notice($author, $parameters['omb_notice'], $notice = new OMB_Notice($author, $parameters['omb_notice'],
$parameters['omb_notice_content']); $parameters['omb_notice_content']);
@ -168,105 +178,117 @@ class OMB_Notice {
return $notice; return $notice;
} }
public function getAuthor() { public function getAuthor()
{
return $this->author; return $this->author;
} }
public function getIdentifierURI() { public function getIdentifierURI()
{
return $this->uri; return $this->uri;
} }
public function getContent() { public function getContent()
{
return $this->content; return $this->content;
} }
public function getURL() { public function getURL()
{
return $this->url; return $this->url;
} }
public function getLicenseURL() { public function getLicenseURL()
{
return $this->license_url; return $this->license_url;
} }
public function getSeealsoURL() { public function getSeealsoURL()
{
return $this->seealso_url; return $this->seealso_url;
} }
public function getSeealsoDisposition() { public function getSeealsoDisposition()
{
return $this->seealso_disposition; return $this->seealso_disposition;
} }
public function getSeealsoMediatype() { public function getSeealsoMediatype()
{
return $this->seealso_mediatype; return $this->seealso_mediatype;
} }
public function getSeealsoLicenseURL() { public function getSeealsoLicenseURL()
{
return $this->seealso_license_url; return $this->seealso_license_url;
} }
public function setURL($url) { public function setURL($url)
if ($url === '') { {
$url = null; $this->setVal('notice_url', $url, 'OMB_Helper::validateURL', 'url');
} elseif (!OMB_Helper::validateURL($url)) {
throw new OMB_InvalidParameterException($url, 'notice', 'omb_notice_url');
}
$this->url = $url;
$this->param_array = false;
} }
public function setLicenseURL($license_url) { public function setLicenseURL($license_url)
if ($license_url === '') { {
$license_url = null; $this->setVal('license', $license_url, 'OMB_Helper::validateURL',
} elseif (!OMB_Helper::validateURL($license_url)) { 'license_url');
throw new OMB_InvalidParameterException($license_url, 'notice',
'omb_notice_license');
}
$this->license_url = $license_url;
$this->param_array = false;
} }
public function setSeealsoURL($seealso_url) { public function setSeealsoURL($seealso_url)
if ($seealso_url === '') { {
$seealso_url = null; $this->setVal('seealso', $seealso_url, 'OMB_Helper::validateURL',
} elseif (!OMB_Helper::validateURL($seealso_url)) { 'seealso_url');
throw new OMB_InvalidParameterException($seealso_url, 'notice',
'omb_seealso');
}
$this->seealso_url = $seealso_url;
$this->param_array = false;
} }
public function setSeealsoDisposition($seealso_disposition) { public function setSeealsoDisposition($seealso_disposition)
if ($seealso_disposition === '') { {
$seealso_disposition = null; $this->setVal('seealso_disposition', $seealso_disposition,
} elseif ($seealso_disposition !== 'link' && $seealso_disposition !== 'inline') { 'OMB_Notice::validateDisposition');
throw new OMB_InvalidParameterException($seealso_disposition, 'notice',
'omb_seealso_disposition');
}
$this->seealso_disposition = $seealso_disposition;
$this->param_array = false;
} }
public function setSeealsoMediatype($seealso_mediatype) { protected static function validateDisposition($str)
if ($seealso_mediatype === '') { {
$seealso_mediatype = null; return in_array($str, array('link', 'inline'));
} elseif (!OMB_Helper::validateMediaType($seealso_mediatype)) {
throw new OMB_InvalidParameterException($seealso_mediatype, 'notice',
'omb_seealso_mediatype');
}
$this->seealso_mediatype = $seealso_mediatype;
$this->param_array = false;
} }
public function setSeealsoLicenseURL($seealso_license_url) { public function setSeealsoMediatype($seealso_mediatype)
if ($seealso_license_url === '') { {
$seealso_license_url = null; $this->setVal('seealso_mediatype', $seealso_mediatype,
} elseif (!OMB_Helper::validateURL($seealso_license_url)) { 'OMB_Helper::validateMediaType');
throw new OMB_InvalidParameterException($seealso_license_url, 'notice',
'omb_seealso_license');
} }
$this->seealso_license_url = $seealso_license_url;
public function setSeealsoLicenseURL($seealso_license_url)
{
$this->setVal('seealso_license', $seealso_license_url,
'OMB_Helper::validateURL', 'seealso_license_url');
}
/**
* Set a value
*
* Updates a value specified by a parameter name and the new value.
*
* @param string $param The parameter name according to OMB
* @param string $value The new value
* @param callback $validator A validator function for the parameter
* @param string $field The name of the field in OMB_Notice
* @param bool $force Whether null values should be checked as well
*/
protected function setVal($param, $value, $validator, $field = null,
$force = false)
{
if (is_null($field)) {
$field = $param;
}
if ($value === '' && !$force) {
$value = null;
} elseif (!call_user_func($validator, $value)) {
throw new OMB_InvalidParameterException($value, 'notice', $param);
}
if ($this->$field !== $value) {
$this->$field = $value;
$this->param_array = false; $this->param_array = false;
} }
}
} }
?> ?>

View File

@ -1,14 +1,6 @@
<?php <?php
require_once 'Auth/Yadis/Yadis.php';
require_once 'unsupportedserviceexception.php';
require_once 'invalidyadisexception.php';
/** /**
* OMB XRDS representation * This file is part of libomb
*
* This class represents a Yadis XRDS file for OMB. It adds some useful methods to
* Auth_Yadis_XRDS.
* *
* PHP version 5 * PHP version 5
* *
@ -27,11 +19,23 @@ require_once 'invalidyadisexception.php';
* *
* @package OMB * @package OMB
* @author Adrian Lang <mail@adrianlang.de> * @author Adrian Lang <mail@adrianlang.de>
* @copyright 2009 Adrian Lang
* @license http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0
**/ * @version 0.1a-20090828
* @link http://adrianlang.de/libomb
*/
class OMB_Yadis_XRDS extends Auth_Yadis_XRDS { require_once 'Auth/Yadis/Yadis.php';
require_once 'unsupportedserviceexception.php';
require_once 'invalidyadisexception.php';
/**
* OMB XRDS representation
*
* This class represents a Yadis XRDS file for OMB. It adds some useful methods to
* Auth_Yadis_XRDS.
*/
class OMB_Yadis_XRDS extends Auth_Yadis_XRDS
{
protected $fetcher; protected $fetcher;
@ -52,8 +56,9 @@ class OMB_Yadis_XRDS extends Auth_Yadis_XRDS {
* *
* @return OMB_Yadis_XRDS The initialized object representing the given * @return OMB_Yadis_XRDS The initialized object representing the given
* resource * resource
**/ */
public static function fromYadisURL($url, $fetcher) { public static function fromYadisURL($url, $fetcher)
{
/* Perform a Yadis discovery. */ /* Perform a Yadis discovery. */
$yadis = Auth_Yadis_Yadis::discover($url, $fetcher); $yadis = Auth_Yadis_Yadis::discover($url, $fetcher);
if ($yadis->failed) { if ($yadis->failed) {
@ -74,16 +79,18 @@ class OMB_Yadis_XRDS extends Auth_Yadis_XRDS {
* *
* Returns the Auth_Yadis_Service object corresponding to the given service * Returns the Auth_Yadis_Service object corresponding to the given service
* URI. * URI.
* Throws an OMB_UnsupportedServiceException if the service is not available. * Throws an OMB_UnsupportedServiceException if the service is not
* available.
* *
* @param string $service URI specifier of the requested service * @param string $service URI specifier of the requested service
* *
* @access public * @access public
* *
* @return Auth_Yadis_Service The object representing the requested service * @return Auth_Yadis_Service The object representing the requested service
**/ */
public function getService($service) { public function getService($service)
$match = $this->services(array( create_function('$s', {
$match = $this->services(array(create_function('$s',
"return in_array('$service', \$s->getTypes());"))); "return in_array('$service', \$s->getTypes());")));
if ($match === array()) { if ($match === array()) {
throw new OMB_UnsupportedServiceException($service); throw new OMB_UnsupportedServiceException($service);
@ -103,16 +110,20 @@ class OMB_Yadis_XRDS extends Auth_Yadis_XRDS {
* @access public * @access public
* *
* @return OMB_Yadis_XRDS The object representing the requested XRD * @return OMB_Yadis_XRDS The object representing the requested XRD
**/ */
public function getXRD($uri) { public function getXRD($uri)
{
$nexthash = strpos($uri, '#'); $nexthash = strpos($uri, '#');
if ($nexthash !== 0) { if ($nexthash === false) {
if ($nexthash !== false) { throw new OMB_InvalidYadisException("$uri does not specify a " .
'valid XML node.');
}
if ($nexthash > 0) {
$cururi = substr($uri, 0, $nexthash); $cururi = substr($uri, 0, $nexthash);
$nexturi = substr($uri, $nexthash); $nexturi = substr($uri, $nexthash);
} return OMB_Yadis_XRDS::fromYadisURL($cururi, $this->fetcher)
return ->getXRD($nexturi);
OMB_Yadis_XRDS::fromYadisURL($cururi, $this->fetcher)->getXRD($nexturi);
} }
$id = substr($uri, 1); $id = substr($uri, 1);
@ -130,20 +141,22 @@ class OMB_Yadis_XRDS extends Auth_Yadis_XRDS {
/** /**
* Parse an XML string containing a XRDS document * Parse an XML string containing a XRDS document
* *
* Parse an XML string (XRDS document) and return either a * Parses an XML string (XRDS document) and returns either an
* Auth_Yadis_XRDS object or null, depending on whether the * Auth_Yadis_XRDS object or null, depending on whether the XRDS XML is
* XRDS XML is valid. * valid.
* Copy and paste from parent to select correct constructor. * This method is just copy and paste from the parent class to select the
* correct constructor.
* *
* @param string $xml_string An XRDS XML string. * @param string $xml_string An XRDS XML string
* @param array $extra_ns_map Additional namespace declarations
* *
* @access public * @access public
* *
* @return mixed An instance of OMB_Yadis_XRDS or null, * @return mixed An instance of OMB_Yadis_XRDS or null,
* depending on the validity of $xml_string * depending on the validity of $xml_string
**/ */
public function parseXRDS($xml_string, $extra_ns_map = null)
public function &parseXRDS($xml_string, $extra_ns_map = null) { {
$_null = null; $_null = null;
if (!$xml_string) { if (!$xml_string) {

View File

@ -1,13 +1,6 @@
<?php <?php
require_once 'xrds_writer.php';
/** /**
* Write OMB-specific XRDS using XMLWriter. * This file is part of libomb
*
* This class writes the XRDS file announcing the OMB server. It uses
* OMB_XMLWriter, which is a subclass of XMLWriter. An instance of
* OMB_Plain_XRDS_Writer should be passed to OMB_Service_Provider->writeXRDS.
* *
* PHP version 5 * PHP version 5
* *
@ -26,19 +19,39 @@ require_once 'xrds_writer.php';
* *
* @package OMB * @package OMB
* @author Adrian Lang <mail@adrianlang.de> * @author Adrian Lang <mail@adrianlang.de>
* @copyright 2009 Adrian Lang
* @license http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0
**/ * @version 0.1a-20090828
* @link http://adrianlang.de/libomb
*/
class OMB_Plain_XRDS_Writer implements OMB_XRDS_Writer { require_once 'xrds_writer.php';
public function writeXRDS($user, $mapper) {
/**
* Write OMB-specific XRDS using XMLWriter
*
* This class writes the XRDS file announcing the OMB server. It uses XMLWriter.
* An instance of OMB_Plain_XRDS_Writer should be passed to
* OMB_Service_Provider->writeXRDS.
*/
class OMB_Plain_XRDS_Writer implements OMB_XRDS_Writer
{
/**
* Write XRDS using XMLWriter
*
* Outputs a XRDS document specifying an OMB service.
*
* @param OMB_profile $user The target user for the OMB service
* @param OMB_XRDS_Mapper $mapper An OMB_XRDS_Mapper providing endpoint URLs
*/
public function writeXRDS($user, $mapper)
{
header('Content-Type: application/xrds+xml'); header('Content-Type: application/xrds+xml');
$xw = new XMLWriter(); $xw = new XMLWriter();
$xw->openURI('php://output'); $xw->openURI('php://output');
$xw->setIndent(true); $xw->setIndent(true);
$xw->startDocument('1.0', 'UTF-8'); $xw->startDocument('1.0', 'UTF-8');
$this->writeFullElement($xw, 'XRDS', array('xmlns' => 'xri://$xrds'), array( $this->_writeFullElement($xw, 'XRDS', array('xmlns' => 'xri://$xrds'), array(
array('XRD', array('xmlns' => 'xri://$xrd*($v*2.0)', array('XRD', array('xmlns' => 'xri://$xrd*($v*2.0)',
'xml:id' => 'oauth', 'xml:id' => 'oauth',
'xmlns:simple' => 'http://xrds-simple.net/core/1.0', 'xmlns:simple' => 'http://xrds-simple.net/core/1.0',
@ -98,13 +111,26 @@ class OMB_Plain_XRDS_Writer implements OMB_XRDS_Writer {
array('Type', null, OMB_VERSION), array('Type', null, OMB_VERSION),
array('URI', null, '#omb') array('URI', null, '#omb')
)) ))
)) ))));
));
$xw->endDocument(); $xw->endDocument();
$xw->flush(); $xw->flush();
} }
public static function writeFullElement($xw, $tag, $attributes, $content) { /**
* Write a complex XML element
*
* Outputs a XML element with attributes and content.
*
* @param XMLWriter $xw The XMLWriter used to output the element
* @param string $tag The tag name
* @param array|null $attributes A map of XML attributes
* @param array|string $content The content of the element; either an
* array of child nodes each specified by a
* three entry-array ($tag, $attributes,
* $content) or a string
*/
private function _writeFullElement($xw, $tag, $attributes, $content)
{
$xw->startElement($tag); $xw->startElement($tag);
if (!is_null($attributes)) { if (!is_null($attributes)) {
foreach ($attributes as $name => $value) { foreach ($attributes as $name => $value) {
@ -112,8 +138,8 @@ class OMB_Plain_XRDS_Writer implements OMB_XRDS_Writer {
} }
} }
if (is_array($content)) { if (is_array($content)) {
foreach ($content as $values) { foreach ($content as $val) {
OMB_Plain_XRDS_Writer::writeFullElement($xw, $values[0], $values[1], $values[2]); $this->_writeFullElement($xw, $val[0], $val[1], $val[2]);
} }
} else { } else {
$xw->text($content); $xw->text($content);

View File

@ -1,15 +1,6 @@
<?php <?php
require_once 'invalidparameterexception.php';
require_once 'Validate.php';
require_once 'helper.php';
/** /**
* OMB profile representation * This file is part of libomb
*
* This class represents an OMB profile.
*
* Do not call the setters with null values. Instead, if you want to delete a
* field, pass an empty string. The getters will return null for empty fields.
* *
* PHP version 5 * PHP version 5
* *
@ -28,11 +19,25 @@ require_once 'helper.php';
* *
* @package OMB * @package OMB
* @author Adrian Lang <mail@adrianlang.de> * @author Adrian Lang <mail@adrianlang.de>
* @copyright 2009 Adrian Lang
* @license http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0
**/ * @version 0.1a-20090828
* @link http://adrianlang.de/libomb
*/
class OMB_Profile { require_once 'invalidparameterexception.php';
require_once 'Validate.php';
require_once 'helper.php';
/**
* OMB profile representation
*
* This class represents an OMB profile.
*
* Do not call the setters with null values. Instead, if you want to delete a
* field, pass an empty string. The getters will return null for empty fields.
*/
class OMB_Profile
{
protected $identifier_uri; protected $identifier_uri;
protected $profile_url; protected $profile_url;
protected $nickname; protected $nickname;
@ -52,12 +57,14 @@ class OMB_Profile {
* *
* Initializes the OMB_Profile object with an identifier uri. * Initializes the OMB_Profile object with an identifier uri.
* *
* @param string $identifier_uri The profile URI as defined by the OMB. A unique * @param string $identifier_uri The profile URI as defined by the OMB;
* and unchanging identifier for a profile. * A unique and never changing identifier for
* a profile
* *
* @access public * @access public
*/ */
public function __construct($identifier_uri) { public function __construct($identifier_uri)
{
if (!Validate::uri($identifier_uri)) { if (!Validate::uri($identifier_uri)) {
throw new OMB_InvalidParameterException($identifier_uri, 'profile', throw new OMB_InvalidParameterException($identifier_uri, 'profile',
'omb_listenee or omb_listener'); 'omb_listenee or omb_listener');
@ -67,22 +74,22 @@ class OMB_Profile {
} }
/** /**
* Returns the profile as array * Return the profile as array
* *
* The method returns an array which contains the whole profile as array. The * Returns an array which contains the whole profile as array.
* array is cached and only rebuilt on changes of the profile. * The array is cached and only rebuilt on changes of the profile.
* *
* @param bool $force_all Specifies whether empty fields should be added to * @param string $prefix The common prefix to the key for all parameters
* the array as well. This is neccessary to clear * @param bool $force_all Specifies whether empty fields should be added
* fields via updateProfile. * to the array as well; This is necessary to
* * clear fields via updateProfile
* @param string $prefix The common prefix to the key for all parameters.
* *
* @access public * @access public
* *
* @return array The profile as parameter array * @return array The profile as parameter array
*/ */
public function asParameters($prefix, $force_all = false) { public function asParameters($prefix, $force_all = false)
{
if ($this->param_array === false) { if ($this->param_array === false) {
$this->param_array = array('' => $this->identifier_uri); $this->param_array = array('' => $this->identifier_uri);
@ -127,21 +134,22 @@ class OMB_Profile {
} }
/** /**
* Builds an OMB_Profile object from array * Build an OMB_Profile object from array
* *
* The method builds an OMB_Profile object from the passed parameters array. The * Builds an OMB_Profile object from the passed parameters array. The
* array MUST provide a profile URI. The array fields HAVE TO be named according * array MUST provide a profile URI. The array fields HAVE TO be named
* to the OMB standard. The prefix (omb_listener or omb_listenee) is passed as a * according to the OMB standard. The prefix (omb_listener or omb_listenee)
* parameter. * is passed as a parameter.
* *
* @param string $parameters An array containing the profile parameters. * @param string $parameters An array containing the profile parameters
* @param string $prefix The common prefix of the profile parameter keys. * @param string $prefix The common prefix of the profile parameter keys
* *
* @access public * @access public
* *
* @returns OMB_Profile The built OMB_Profile. * @returns OMB_Profile The built OMB_Profile
*/ */
public static function fromParameters($parameters, $prefix) { public static function fromParameters($parameters, $prefix)
{
if (!isset($parameters[$prefix])) { if (!isset($parameters[$prefix])) {
throw new OMB_InvalidParameterException('', 'profile', $prefix); throw new OMB_InvalidParameterException('', 'profile', $prefix);
} }
@ -159,12 +167,13 @@ class OMB_Profile {
* OMB standard. The prefix (omb_listener or omb_listenee) is passed as a * OMB standard. The prefix (omb_listener or omb_listenee) is passed as a
* parameter. * parameter.
* *
* @param string $parameters An array containing the profile parameters. * @param string $parameters An array containing the profile parameters
* @param string $prefix The common prefix of the profile parameter keys. * @param string $prefix The common prefix of the profile parameter keys
* *
* @access public * @access public
*/ */
public function updateFromParameters($parameters, $prefix) { public function updateFromParameters($parameters, $prefix)
{
if (isset($parameters[$prefix.'_profile'])) { if (isset($parameters[$prefix.'_profile'])) {
$this->setProfileURL($parameters[$prefix.'_profile']); $this->setProfileURL($parameters[$prefix.'_profile']);
} }
@ -198,120 +207,139 @@ class OMB_Profile {
} }
} }
public function getIdentifierURI() { public function getIdentifierURI()
{
return $this->identifier_uri; return $this->identifier_uri;
} }
public function getProfileURL() { public function getProfileURL()
{
return $this->profile_url; return $this->profile_url;
} }
public function getHomepage() { public function getHomepage()
{
return $this->homepage; return $this->homepage;
} }
public function getNickname() { public function getNickname()
{
return $this->nickname; return $this->nickname;
} }
public function getLicenseURL() { public function getLicenseURL()
{
return $this->license_url; return $this->license_url;
} }
public function getFullname() { public function getFullname()
{
return $this->fullname; return $this->fullname;
} }
public function getBio() { public function getBio()
{
return $this->bio; return $this->bio;
} }
public function getLocation() { public function getLocation()
{
return $this->location; return $this->location;
} }
public function getAvatarURL() { public function getAvatarURL()
{
return $this->avatar_url; return $this->avatar_url;
} }
public function setProfileURL($profile_url) { public function setProfileURL($profile_url)
if (!OMB_Helper::validateURL($profile_url)) { {
throw new OMB_InvalidParameterException($profile_url, 'profile', $this->setVal('profile', $profile_url, 'OMB_Helper::validateURL',
'omb_listenee_profile or omb_listener_profile'); 'profile_url');
}
$this->profile_url = $profile_url;
$this->param_array = false;
} }
public function setNickname($nickname) { public function setNickname($nickname)
if (!Validate::string($nickname, {
$this->setVal('nickname', $nickname, 'OMB_Profile::validateNickname',
'nickname', true);
}
public function setLicenseURL($license_url)
{
$this->setVal('license', $license_url, 'OMB_Helper::validateURL',
'license_url');
}
public function setFullname($fullname)
{
$this->setVal('fullname', $fullname, 'OMB_Profile::validate255');
}
public function setHomepage($homepage)
{
$this->setVal('homepage', $homepage, 'OMB_Helper::validateURL');
}
public function setBio($bio)
{
$this->setVal('bio', $bio, 'OMB_Profile::validate140');
}
public function setLocation($location)
{
$this->setVal('location', $location, 'OMB_Profile::validate255');
}
public function setAvatarURL($avatar_url)
{
$this->setVal('avatar', $avatar_url, 'OMB_Helper::validateURL',
'avatar_url');
}
protected static function validate255($str)
{
return Validate::string($str, array('max_length' => 255));
}
protected static function validate140($str)
{
return Validate::string($str, array('max_length' => 140));
}
protected static function validateNickname($str)
{
return Validate::string($str,
array('min_length' => 1, array('min_length' => 1,
'max_length' => 64, 'max_length' => 64,
'format' => VALIDATE_NUM . VALIDATE_ALPHA))) { 'format' => VALIDATE_NUM . VALIDATE_ALPHA));
throw new OMB_InvalidParameterException($nickname, 'profile', 'nickname');
} }
$this->nickname = $nickname; /**
* Set a value
*
* Updates a value specified by a parameter name and the new value.
*
* @param string $param The parameter name according to OMB
* @param string $value The new value
* @param callback $validator A validator function for the parameter
* @param string $field The name of the field in OMB_Profile
* @param bool $force Whether null values should be checked as well
*/
protected function setVal($param, $value, $validator, $field = null,
$force = false)
{
if (is_null($field)) {
$field = $param;
}
if ($value === '' && !$force) {
$value = null;
} elseif (!call_user_func($validator, $value)) {
throw new OMB_InvalidParameterException($value, 'profile', $param);
}
if ($this->$field !== $value) {
$this->$field = $value;
$this->param_array = false; $this->param_array = false;
} }
public function setLicenseURL($license_url) {
if (!OMB_Helper::validateURL($license_url)) {
throw new OMB_InvalidParameterException($license_url, 'profile',
'omb_listenee_license or omb_listener_license');
} }
$this->license_url = $license_url;
$this->param_array = false;
}
public function setFullname($fullname) {
if ($fullname === '') {
$fullname = null;
} elseif (!Validate::string($fullname, array('max_length' => 255))) {
throw new OMB_InvalidParameterException($fullname, 'profile', 'fullname');
}
$this->fullname = $fullname;
$this->param_array = false;
}
public function setHomepage($homepage) {
if ($homepage === '') {
$homepage = null;
}
$this->homepage = $homepage;
$this->param_array = false;
}
public function setBio($bio) {
if ($bio === '') {
$bio = null;
} elseif (!Validate::string($bio, array('max_length' => 140))) {
throw new OMB_InvalidParameterException($bio, 'profile', 'fullname');
}
$this->bio = $bio;
$this->param_array = false;
}
public function setLocation($location) {
if ($location === '') {
$location = null;
} elseif (!Validate::string($location, array('max_length' => 255))) {
throw new OMB_InvalidParameterException($location, 'profile', 'fullname');
}
$this->location = $location;
$this->param_array = false;
}
public function setAvatarURL($avatar_url) {
if ($avatar_url === '') {
$avatar_url = null;
} elseif (!OMB_Helper::validateURL($avatar_url)) {
throw new OMB_InvalidParameterException($avatar_url, 'profile',
'omb_listenee_avatar or omb_listener_avatar');
}
$this->avatar_url = $avatar_url;
$this->param_array = false;
}
} }
?> ?>

View File

@ -1,9 +1,6 @@
<?php <?php
/** /**
* Exception stating that the remote service had a failure * This file is part of libomb
*
* This exception is raised when a remote service failed to return a valid
* response to a request or send a valid request.
* *
* PHP version 5 * PHP version 5
* *
@ -22,21 +19,55 @@
* *
* @package OMB * @package OMB
* @author Adrian Lang <mail@adrianlang.de> * @author Adrian Lang <mail@adrianlang.de>
* @copyright 2009 Adrian Lang
* @license http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0
**/ * @version 0.1a-20090828
class OMB_RemoteServiceException extends Exception { * @link http://adrianlang.de/libomb
public static function fromYadis($request_uri, $result) { */
/**
* Exception stating that the remote service had a failure
*
* This exception is raised when a remote service failed to return a valid
* response to a request or send a valid request.
*/
class OMB_RemoteServiceException extends Exception
{
/**
* Create exception from Yadis response
*
* Creates an exception from a passed yadis result.
*
* @param string $request_uri The target URI for the failed
* request
* @param Auth_Yadis_HTTPResponse $result The result of the failed
* request
*
* @return OMB_RemoteServiceException A new exception
*/
public static function fromYadis($request_uri, $result)
{
if ($result->status == 200) { if ($result->status == 200) {
$err = 'Got wrong response ' . $result->body; $err = 'Got wrong response ' . $result->body;
} else { } else {
$err = 'Got error code ' . $result->status . ' with response ' . $result->body; $err = 'Got error code ' . $result->status . ' with response ' .
$result->body;
} }
return new OMB_RemoteServiceException($request_uri . ': ' . $err); return OMB_RemoteServiceException::forRequest($request_uri, $err);
} }
public static function forRequest($action_uri, $failure) { /**
return new OMB_RemoteServiceException("Handler for $action_uri: " . $failure); * Create exception for a call to a resource
*
* Creates an exception for a given error message and target URI.
*
* @param string $action_uri The target URI for the failed request
* @param string $failure An error message
*
* @return OMB_RemoteServiceException A new exception
*/
public static function forRequest($action_uri, $failure)
{
return new OMB_RemoteServiceException("Handler for $action_uri: $failure");
} }
} }
?> ?>

View File

@ -1,19 +1,6 @@
<?php <?php
require_once 'constants.php';
require_once 'Validate.php';
require_once 'Auth/Yadis/Yadis.php';
require_once 'OAuth.php';
require_once 'unsupportedserviceexception.php';
require_once 'remoteserviceexception.php';
require_once 'omb_yadis_xrds.php';
require_once 'helper.php';
/** /**
* OMB service representation * This file is part of libomb
*
* This class represents a complete remote OMB service. It provides discovery
* and execution of the services methods.
* *
* PHP version 5 * PHP version 5
* *
@ -32,11 +19,29 @@ require_once 'helper.php';
* *
* @package OMB * @package OMB
* @author Adrian Lang <mail@adrianlang.de> * @author Adrian Lang <mail@adrianlang.de>
* @copyright 2009 Adrian Lang
* @license http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0
**/ * @version 0.1a-20090828
* @link http://adrianlang.de/libomb
*/
class OMB_Service_Consumer { require_once 'Validate.php';
require_once 'Auth/Yadis/Yadis.php';
require_once 'OAuth.php';
require_once 'constants.php';
require_once 'helper.php';
require_once 'omb_yadis_xrds.php';
require_once 'profile.php';
require_once 'remoteserviceexception.php';
require_once 'unsupportedserviceexception.php';
/**
* OMB service representation
*
* This class represents a complete remote OMB service. It provides discovery
* and execution of the services methods.
*/
class OMB_Service_Consumer
{
protected $url; /* The service URL */ protected $url; /* The service URL */
protected $services; /* An array of strings mapping service URI to protected $services; /* An array of strings mapping service URI to
service URL */ service URL */
@ -50,13 +55,14 @@ class OMB_Service_Consumer {
local user during an auth request. */ local user during an auth request. */
/** /**
* According to OAuth Core 1.0, an user authorization request is no full-blown * According to OAuth Core 1.0, an user authorization request is no
* OAuth request. nonce, timestamp, consumer_key and signature are not needed * full-blown OAuth request. nonce, timestamp, consumer_key and signature
* in this step. See http://laconi.ca/trac/ticket/827 for more informations. * are not needed in this step. See http://laconi.ca/trac/ticket/827 for
* more informations.
* *
* Since Laconica up to version 0.7.2 performs a full OAuth request check, a * Since Laconica up to version 0.7.2 performs a full OAuth request check, a
* correct request would fail. * correct request would fail.
**/ */
public $performLegacyAuthRequest = true; public $performLegacyAuthRequest = true;
/* Helper stuff we are going to need. */ /* Helper stuff we are going to need. */
@ -79,8 +85,9 @@ class OMB_Service_Consumer {
* OMB_Datastore * OMB_Datastore
* *
* @access public * @access public
**/ */
public function __construct ($service_url, $consumer_url, $datastore) { public function __construct ($service_url, $consumer_url, $datastore)
{
$this->url = $service_url; $this->url = $service_url;
$this->fetcher = Auth_Yadis_Yadis::getHTTPFetcher(); $this->fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
$this->datastore = $datastore; $this->datastore = $datastore;
@ -100,8 +107,10 @@ class OMB_Service_Consumer {
foreach ($targetservices as $targetservice) { foreach ($targetservices as $targetservice) {
$yadis_service = $xrd->getService($targetservice); $yadis_service = $xrd->getService($targetservice);
if ($targetservice == OAUTH_ENDPOINT_REQUEST) { if ($targetservice == OAUTH_ENDPOINT_REQUEST) {
$localid = $yadis_service->getElements('xrd:LocalID'); $localid =
$this->listener_uri = $yadis_service->parser->content($localid[0]); $yadis_service->getElements('xrd:LocalID');
$this->listener_uri =
$yadis_service->parser->content($localid[0]);
} }
$uris = $yadis_service->getURIs(); $uris = $yadis_service->getURIs();
$this->services[$targetservice] = $uris[0]; $this->services[$targetservice] = $uris[0];
@ -120,8 +129,9 @@ class OMB_Service_Consumer {
* @access public * @access public
* *
* @return string The service handler URI * @return string The service handler URI
**/ */
public function getServiceURI($service) { public function getServiceURI($service)
{
return $this->services[$service]; return $this->services[$service];
} }
@ -133,8 +143,9 @@ class OMB_Service_Consumer {
* @access public * @access public
* *
* @return string The remote users URI * @return string The remote users URI
**/ */
public function getRemoteUserURI() { public function getRemoteUserURI()
{
return $this->listener_uri; return $this->listener_uri;
} }
@ -146,8 +157,9 @@ class OMB_Service_Consumer {
* @access public * @access public
* *
* @return string The local users URI * @return string The local users URI
**/ */
public function getListeneeURI() { public function getListeneeURI()
{
return $this->listenee_uri; return $this->listenee_uri;
} }
@ -160,8 +172,9 @@ class OMB_Service_Consumer {
* @access public * @access public
* *
* @return OAuthToken An unauthorized request token * @return OAuthToken An unauthorized request token
**/ */
public function requestToken() { public function requestToken()
{
/* Set the token to null just in case the user called setToken. */ /* Set the token to null just in case the user called setToken. */
$this->token = null; $this->token = null;
@ -172,7 +185,8 @@ class OMB_Service_Consumer {
$result); $result);
} }
parse_str($result->body, $return); parse_str($result->body, $return);
if (!isset($return['oauth_token']) || !isset($return['oauth_token_secret'])) { if (!isset($return['oauth_token']) ||
!isset($return['oauth_token_secret'])) {
throw OMB_RemoteServiceException::fromYadis(OAUTH_ENDPOINT_REQUEST, throw OMB_RemoteServiceException::fromYadis(OAUTH_ENDPOINT_REQUEST,
$result); $result);
} }
@ -181,7 +195,6 @@ class OMB_Service_Consumer {
} }
/** /**
*
* Request authorization * Request authorization
* *
* Returns an URL which equals to an authorization request. The end user * Returns an URL which equals to an authorization request. The end user
@ -197,29 +210,32 @@ class OMB_Service_Consumer {
* @access public * @access public
* *
* @return string An URL representing an authorization request * @return string An URL representing an authorization request
**/ */
public function requestAuthorization($profile, $finish_url) { public function requestAuthorization($profile, $finish_url)
{
if ($this->performLegacyAuthRequest) { if ($this->performLegacyAuthRequest) {
$params = $profile->asParameters('omb_listenee', false); $params = $profile->asParameters('omb_listenee',
false);
$params['omb_listener'] = $this->listener_uri; $params['omb_listener'] = $this->listener_uri;
$params['oauth_callback'] = $finish_url; $params['oauth_callback'] = $finish_url;
$url = $this->prepareAction(OAUTH_ENDPOINT_AUTHORIZE, $params, 'GET')->to_url(); $url = $this->prepareAction(OAUTH_ENDPOINT_AUTHORIZE, $params,
'GET')->to_url();
} else { } else {
$params = array('oauth_callback' => $finish_url,
$params = array(
'oauth_callback' => $finish_url,
'oauth_token' => $this->token->key, 'oauth_token' => $this->token->key,
'omb_version' => OMB_VERSION, 'omb_version' => OMB_VERSION,
'omb_listener' => $this->listener_uri); 'omb_listener' => $this->listener_uri);
$params = array_merge($profile->asParameters('omb_listenee', false). $params); $params = array_merge($profile->asParameters('omb_listenee', false),
$params);
/* Build result URL. */ /* Build result URL. */
$url = $this->services[OAUTH_ENDPOINT_AUTHORIZE]; $url = $this->services[OAUTH_ENDPOINT_AUTHORIZE] .
$url .= (strrpos($url, '?') === false ? '?' : '&'); (strrpos($url, '?') === false ? '?' : '&');
foreach ($params as $k => $v) { foreach ($params as $k => $v) {
$url .= OAuthUtil::urlencode_rfc3986($k) . '=' . OAuthUtil::urlencode_rfc3986($v) . '&'; $url .= OAuthUtil::urlencode_rfc3986($k) . '=' .
OAuthUtil::urlencode_rfc3986($v) . '&';
} }
} }
@ -238,15 +254,15 @@ class OMB_Service_Consumer {
* Throws exceptions on failure. * Throws exceptions on failure.
* *
* @access public * @access public
**/ */
public function finishAuthorization() { public function finishAuthorization()
{
OMB_Helper::removeMagicQuotesFromRequest(); OMB_Helper::removeMagicQuotesFromRequest();
$req = OAuthRequest::from_request(); $req = OAuthRequest::from_request();
if ($req->get_parameter('oauth_token') != if ($req->get_parameter('oauth_token') != $this->token->key) {
$this->token->key) {
/* Thats not the token I wanted to get authorized. */ /* Thats not the token I wanted to get authorized. */
throw new OAuthException('The authorized token does not equal the ' . throw new OAuthException('The authorized token does not equal ' .
'submitted token.'); 'the submitted token.');
} }
if ($req->get_parameter('omb_version') != OMB_VERSION) { if ($req->get_parameter('omb_version') != OMB_VERSION) {
@ -264,11 +280,10 @@ class OMB_Service_Consumer {
} }
$params['omb_listener'] = $this->listener_uri; $params['omb_listener'] = $this->listener_uri;
require_once 'profile.php';
$listener = OMB_Profile::fromParameters($params, 'omb_listener'); $listener = OMB_Profile::fromParameters($params, 'omb_listener');
/* Ask the remote service to convert the authorized request token into an /* Ask the remote service to convert the authorized request token into
access token. */ an access token. */
$result = $this->performAction(OAUTH_ENDPOINT_ACCESS, array()); $result = $this->performAction(OAUTH_ENDPOINT_ACCESS, array());
if ($result->status != 200) { if ($result->status != 200) {
@ -276,13 +291,14 @@ class OMB_Service_Consumer {
} }
parse_str($result->body, $return); parse_str($result->body, $return);
if (!isset($return['oauth_token']) || !isset($return['oauth_token_secret'])) { if (!isset($return['oauth_token']) ||
!isset($return['oauth_token_secret'])) {
throw new OAuthException('Could not get access token'); throw new OAuthException('Could not get access token');
} }
$this->setToken($return['oauth_token'], $return['oauth_token_secret']); $this->setToken($return['oauth_token'], $return['oauth_token_secret']);
/* Subscription is finished and valid. Now store the new subscriber and the /* Subscription is finished and valid. Now store the new subscriber and
subscription in the database. */ the subscription in the database. */
$this->datastore->saveProfile($listener); $this->datastore->saveProfile($listener);
$this->datastore->saveSubscription($this->listener_uri, $this->datastore->saveSubscription($this->listener_uri,
@ -300,8 +316,9 @@ class OMB_Service_Consumer {
* @access public * @access public
* *
* @return string the listeners URI * @return string the listeners URI
**/ */
public function getListenerURI() { public function getListenerURI()
{
return $this->listener_uri; return $this->listener_uri;
} }
@ -313,10 +330,12 @@ class OMB_Service_Consumer {
* @param OMB_Profile $profile The profile that has changed * @param OMB_Profile $profile The profile that has changed
* *
* @access public * @access public
**/ */
public function updateProfile($profile) { public function updateProfile($profile)
{
$params = $profile->asParameters('omb_listenee', true); $params = $profile->asParameters('omb_listenee', true);
$this->performOMBAction(OMB_ENDPOINT_UPDATEPROFILE, $params, $profile->getIdentifierURI()); $this->performOMBAction(OMB_ENDPOINT_UPDATEPROFILE, $params,
$profile->getIdentifierURI());
} }
/** /**
@ -327,11 +346,13 @@ class OMB_Service_Consumer {
* @param OMB_Notice $notice The notice * @param OMB_Notice $notice The notice
* *
* @access public * @access public
**/ */
public function postNotice($notice) { public function postNotice($notice)
{
$params = $notice->asParameters(); $params = $notice->asParameters();
$params['omb_listenee'] = $notice->getAuthor()->getIdentifierURI(); $params['omb_listenee'] = $notice->getAuthor()->getIdentifierURI();
$this->performOMBAction(OMB_ENDPOINT_POSTNOTICE, $params, $params['omb_listenee']); $this->performOMBAction(OMB_ENDPOINT_POSTNOTICE, $params,
$params['omb_listenee']);
} }
/** /**
@ -343,8 +364,9 @@ class OMB_Service_Consumer {
* @param string $secret The secret token * @param string $secret The secret token
* *
* @access public * @access public
**/ */
public function setToken($token, $secret) { public function setToken($token, $secret)
{
$this->token = new OAuthToken($token, $secret); $this->token = new OAuthToken($token, $secret);
} }
@ -362,8 +384,9 @@ class OMB_Service_Consumer {
* @access protected * @access protected
* *
* @return OAuthRequest the prepared request * @return OAuthRequest the prepared request
**/ */
protected function prepareAction($action_uri, $params, $method) { protected function prepareAction($action_uri, $params, $method)
{
$url = $this->services[$action_uri]; $url = $this->services[$action_uri];
$url_params = array(); $url_params = array();
@ -376,7 +399,8 @@ class OMB_Service_Consumer {
$url_params = array_merge($url_params, $params); $url_params = array_merge($url_params, $params);
$req = OAuthRequest::from_consumer_and_token($this->oauth_consumer, $req = OAuthRequest::from_consumer_and_token($this->oauth_consumer,
$this->token, $method, $url, $url_params); $this->token, $method,
$url, $url_params);
/* Sign the request. */ /* Sign the request. */
$req->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $req->sign_request(new OAuthSignatureMethod_HMAC_SHA1(),
@ -388,7 +412,8 @@ class OMB_Service_Consumer {
/** /**
* Perform a service call * Perform a service call
* *
* Creates an OAuthRequest object and execute the mapped call as POST request. * Creates an OAuthRequest object and execute the mapped call as POST
* request.
* *
* @param string $action_uri The URI specifying the target service * @param string $action_uri The URI specifying the target service
* @param array $params Additional parameters for the service call * @param array $params Additional parameters for the service call
@ -396,18 +421,21 @@ class OMB_Service_Consumer {
* @access protected * @access protected
* *
* @return Auth_Yadis_HTTPResponse The POST request response * @return Auth_Yadis_HTTPResponse The POST request response
**/ */
protected function performAction($action_uri, $params) { protected function performAction($action_uri, $params)
{
$req = $this->prepareAction($action_uri, $params, 'POST'); $req = $this->prepareAction($action_uri, $params, 'POST');
/* Return result page. */ /* Return result page. */
return $this->fetcher->post($req->get_normalized_http_url(), $req->to_postdata(), array()); return $this->fetcher->post($req->get_normalized_http_url(),
$req->to_postdata(), array());
} }
/** /**
* Perform an OMB action * Perform an OMB action
* *
* Executes an OMB action to date, its one of updateProfile or postNotice. * Executes an OMB action as of OMB 0.1, its one of updateProfile and
* postNotice.
* *
* @param string $action_uri The URI specifying the target service * @param string $action_uri The URI specifying the target service
* @param array $params Additional parameters for the service call * @param array $params Additional parameters for the service call
@ -415,12 +443,14 @@ class OMB_Service_Consumer {
* the action is performed * the action is performed
* *
* @access protected * @access protected
**/ */
protected function performOMBAction($action_uri, $params, $listenee_uri) { protected function performOMBAction($action_uri, $params, $listenee_uri)
{
$result = $this->performAction($action_uri, $params); $result = $this->performAction($action_uri, $params);
if ($result->status == 403) { if ($result->status == 403) {
/* The remote user unsubscribed us. */ /* The remote user unsubscribed us. */
$this->datastore->deleteSubscription($this->listener_uri, $listenee_uri); $this->datastore->deleteSubscription($this->listener_uri,
$listenee_uri);
} else if ($result->status != 200 || } else if ($result->status != 200 ||
strpos($result->body, 'omb_version=' . OMB_VERSION) === false) { strpos($result->body, 'omb_version=' . OMB_VERSION) === false) {
/* The server signaled an error or sent an incorrect response. */ /* The server signaled an error or sent an incorrect response. */
@ -428,3 +458,4 @@ class OMB_Service_Consumer {
} }
} }
} }
?>

View File

@ -1,13 +1,6 @@
<?php <?php
require_once 'constants.php';
require_once 'remoteserviceexception.php';
require_once 'helper.php';
/** /**
* OMB service realization * This file is part of libomb
*
* This class realizes a complete, simple OMB service.
* *
* PHP version 5 * PHP version 5
* *
@ -26,16 +19,28 @@ require_once 'helper.php';
* *
* @package OMB * @package OMB
* @author Adrian Lang <mail@adrianlang.de> * @author Adrian Lang <mail@adrianlang.de>
* @copyright 2009 Adrian Lang
* @license http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0
**/ * @version 0.1a-20090828
* @link http://adrianlang.de/libomb
*/
class OMB_Service_Provider { require_once 'constants.php';
require_once 'helper.php';
require_once 'notice.php';
require_once 'remoteserviceexception.php';
/**
* OMB service realization
*
* This class realizes a complete, simple OMB service.
*/
class OMB_Service_Provider
{
protected $user; /* An OMB_Profile representing the user */ protected $user; /* An OMB_Profile representing the user */
protected $datastore; /* AN OMB_Datastore */ protected $datastore; /* AN OMB_Datastore */
protected $remote_user; /* An OMB_Profile representing the remote user during protected $remote_user; /* An OMB_Profile representing the remote user
the authorization process */ during the authorization process */
protected $oauth_server; /* An OAuthServer; should only be accessed via protected $oauth_server; /* An OAuthServer; should only be accessed via
getOAuthServer. */ getOAuthServer. */
@ -56,14 +61,25 @@ class OMB_Service_Provider {
* default value if not set * default value if not set
* *
* @access public * @access public
**/ */
public function __construct ($user = null, $datastore = null, $oauth_server = null) { public function __construct ($user = null, $datastore = null,
$oauth_server = null)
{
$this->user = $user; $this->user = $user;
$this->datastore = $datastore; $this->datastore = $datastore;
$this->oauth_server = $oauth_server; $this->oauth_server = $oauth_server;
} }
public function getRemoteUser() { /**
* Return the remote user during user authorization
*
* Returns an OMB_Profile representing the remote user during the user
* authorization request.
*
* @return OMB_Profile The remote user
*/
public function getRemoteUser()
{
return $this->remote_user; return $this->remote_user;
} }
@ -82,8 +98,9 @@ class OMB_Service_Provider {
* *
* @return mixed Depends on the used OMB_XRDS_Writer; OMB_Plain_XRDS_Writer * @return mixed Depends on the used OMB_XRDS_Writer; OMB_Plain_XRDS_Writer
* returns nothing. * returns nothing.
**/ */
public function writeXRDS($xrds_mapper, $xrds_writer = null) { public function writeXRDS($xrds_mapper, $xrds_writer = null)
{
if ($xrds_writer == null) { if ($xrds_writer == null) {
require_once 'plain_xrds_writer.php'; require_once 'plain_xrds_writer.php';
$xrds_writer = new OMB_Plain_XRDS_Writer(); $xrds_writer = new OMB_Plain_XRDS_Writer();
@ -98,57 +115,65 @@ class OMB_Service_Provider {
* $_POST. * $_POST.
* *
* @access public * @access public
**/ */
public function writeRequestToken() { public function writeRequestToken()
{
OMB_Helper::removeMagicQuotesFromRequest(); OMB_Helper::removeMagicQuotesFromRequest();
echo $this->getOAuthServer()->fetch_request_token(OAuthRequest::from_request()); echo $this->getOAuthServer()->fetch_request_token(
OAuthRequest::from_request());
} }
/** /**
* Handle an user authorization request. * Handle an user authorization request.
* *
* Parses an authorization request. This includes OAuth and OMB verification. * Parses an authorization request. This includes OAuth and OMB
* verification.
* Throws exceptions on failures. Returns an OMB_Profile object representing * Throws exceptions on failures. Returns an OMB_Profile object representing
* the remote user. * the remote user.
* *
* The OMB_Profile passed to the constructor of OMB_Service_Provider should * The OMB_Profile passed to the constructor of OMB_Service_Provider should
* not represent the user specified in the authorization request, but the one * not represent the user specified in the authorization request, but the
* currently logged in to the service. This condition being satisfied, * one currently logged in to the service. This condition being satisfied,
* handleUserAuth will check whether the listener specified in the request is * handleUserAuth will check whether the listener specified in the request
* identical to the logged in user. * is identical to the logged in user.
* *
* @access public * @access public
* *
* @return OMB_Profile The profile of the soon-to-be subscribed, i. e. remote * @return OMB_Profile The profile of the soon-to-be subscribed, i. e.
* user * remote user
**/ */
public function handleUserAuth() { public function handleUserAuth()
{
OMB_Helper::removeMagicQuotesFromRequest(); OMB_Helper::removeMagicQuotesFromRequest();
/* Verify the request token. */ /* Verify the request token. */
$this->token = $this->datastore->lookup_token(null, "request", $_GET['oauth_token']); $this->token = $this->datastore->lookup_token(null, "request",
$_GET['oauth_token']);
if (is_null($this->token)) { if (is_null($this->token)) {
throw new OAuthException('The given request token has not been issued ' . throw new OAuthException('The given request token has not been ' .
'by this service.'); 'issued by this service.');
} }
/* Verify the OMB part. */ /* Verify the OMB part. */
if ($_GET['omb_version'] !== OMB_VERSION) { if ($_GET['omb_version'] !== OMB_VERSION) {
throw OMB_RemoteServiceException::forRequest(OAUTH_ENDPOINT_AUTHORIZE, throw OMB_RemoteServiceException::forRequest(OAUTH_ENDPOINT_AUTHORIZE,
'Wrong OMB version ' . $_GET['omb_version']); 'Wrong OMB version ' .
$_GET['omb_version']);
} }
if ($_GET['omb_listener'] !== $this->user->getIdentifierURI()) { if ($_GET['omb_listener'] !== $this->user->getIdentifierURI()) {
throw OMB_RemoteServiceException::forRequest(OAUTH_ENDPOINT_AUTHORIZE, throw OMB_RemoteServiceException::forRequest(OAUTH_ENDPOINT_AUTHORIZE,
'Wrong OMB listener ' . $_GET['omb_listener']); 'Wrong OMB listener ' .
$_GET['omb_listener']);
} }
foreach (array('omb_listenee', 'omb_listenee_profile', foreach (array('omb_listenee', 'omb_listenee_profile',
'omb_listenee_nickname', 'omb_listenee_license') as $param) { 'omb_listenee_nickname', 'omb_listenee_license') as $param) {
if (!isset($_GET[$param]) || is_null($_GET[$param])) { if (!isset($_GET[$param]) || is_null($_GET[$param])) {
throw OMB_RemoteServiceException::forRequest(OAUTH_ENDPOINT_AUTHORIZE, throw OMB_RemoteServiceException::forRequest(
OAUTH_ENDPOINT_AUTHORIZE,
"Required parameter '$param' not found"); "Required parameter '$param' not found");
} }
} }
@ -157,7 +182,8 @@ class OMB_Service_Provider {
if (isset($_GET['oauth_callback']) && $_GET['oauth_callback'] !== '') { if (isset($_GET['oauth_callback']) && $_GET['oauth_callback'] !== '') {
$this->callback = $_GET['oauth_callback']; $this->callback = $_GET['oauth_callback'];
if (!OMB_Helper::validateURL($this->callback)) { if (!OMB_Helper::validateURL($this->callback)) {
throw OMB_RemoteServiceException::forRequest(OAUTH_ENDPOINT_AUTHORIZE, throw OMB_RemoteServiceException::forRequest(
OAUTH_ENDPOINT_AUTHORIZE,
'Invalid callback URL specified'); 'Invalid callback URL specified');
} }
} }
@ -180,20 +206,20 @@ class OMB_Service_Provider {
* - callback The callback URL or null if none given * - callback The callback URL or null if none given
* - token The authorized request token or null if not * - token The authorized request token or null if not
* authorized. * authorized.
**/ */
public function continueUserAuth($accepted) { public function continueUserAuth($accepted)
{
$callback = $this->callback; $callback = $this->callback;
if (!$accepted) { if (!$accepted) {
$this->datastore->revoke_token($this->token->key); $this->datastore->revoke_token($this->token->key);
$this->token = null; $this->token = null;
/* TODO: The handling is probably wrong in terms of OAuth 1.0 but the way
laconica works. Moreover I dont know the right way either. */
} else { } else {
$this->datastore->authorize_token($this->token->key); $this->datastore->authorize_token($this->token->key);
$this->datastore->saveProfile($this->remote_user); $this->datastore->saveProfile($this->remote_user);
$this->datastore->saveSubscription($this->user->getIdentifierURI(), $this->datastore->saveSubscription($this->user->getIdentifierURI(),
$this->remote_user->getIdentifierURI(), $this->token); $this->remote_user->getIdentifierURI(),
$this->token);
if (!is_null($this->callback)) { if (!is_null($this->callback)) {
/* Callback wants to get some informations as well. */ /* Callback wants to get some informations as well. */
@ -216,12 +242,13 @@ class OMB_Service_Provider {
* Echo an access token * Echo an access token
* *
* Outputs an access token for the query found in $_POST. OMB 0.1 specifies * Outputs an access token for the query found in $_POST. OMB 0.1 specifies
* that the access token request has to be a POST even if OAuth allows GET as * that the access token request has to be a POST even if OAuth allows GET
* well. * as well.
* *
* @access public * @access public
**/ */
public function writeAccessToken() { public function writeAccessToken()
{
OMB_Helper::removeMagicQuotesFromRequest(); OMB_Helper::removeMagicQuotesFromRequest();
echo $this->getOAuthServer()->fetch_access_token( echo $this->getOAuthServer()->fetch_access_token(
OAuthRequest::from_request('POST')); OAuthRequest::from_request('POST'));
@ -236,8 +263,9 @@ class OMB_Service_Provider {
* @access public * @access public
* *
* @return OMB_Profile The updated profile * @return OMB_Profile The updated profile
**/ */
public function handleUpdateProfile() { public function handleUpdateProfile()
{
list($req, $profile) = $this->handleOMBRequest(OMB_ENDPOINT_UPDATEPROFILE); list($req, $profile) = $this->handleOMBRequest(OMB_ENDPOINT_UPDATEPROFILE);
$profile->updateFromParameters($req->get_parameters(), 'omb_listenee'); $profile->updateFromParameters($req->get_parameters(), 'omb_listenee');
$this->datastore->saveProfile($profile); $this->datastore->saveProfile($profile);
@ -254,13 +282,15 @@ class OMB_Service_Provider {
* @access public * @access public
* *
* @return OMB_Notice The received notice * @return OMB_Notice The received notice
**/ */
public function handlePostNotice() { public function handlePostNotice()
{
list($req, $profile) = $this->handleOMBRequest(OMB_ENDPOINT_POSTNOTICE); list($req, $profile) = $this->handleOMBRequest(OMB_ENDPOINT_POSTNOTICE);
require_once 'notice.php';
$notice = OMB_Notice::fromParameters($profile, $req->get_parameters()); $notice = OMB_Notice::fromParameters($profile, $req->get_parameters());
$this->datastore->saveNotice($notice); $this->datastore->saveNotice($notice);
$this->finishOMBRequest(); $this->finishOMBRequest();
return $notice; return $notice;
} }
@ -274,9 +304,9 @@ class OMB_Service_Provider {
* @access protected * @access protected
* *
* @return array(OAuthRequest, OMB_Profile) * @return array(OAuthRequest, OMB_Profile)
**/ */
protected function handleOMBRequest($uri) { protected function handleOMBRequest($uri)
{
OMB_Helper::removeMagicQuotesFromRequest(); OMB_Helper::removeMagicQuotesFromRequest();
$req = OAuthRequest::from_request('POST'); $req = OAuthRequest::from_request('POST');
$listenee = $req->get_parameter('omb_listenee'); $listenee = $req->get_parameter('omb_listenee');
@ -285,10 +315,6 @@ class OMB_Service_Provider {
list($consumer, $token) = $this->getOAuthServer()->verify_request($req); list($consumer, $token) = $this->getOAuthServer()->verify_request($req);
} catch (OAuthException $e) { } catch (OAuthException $e) {
header('HTTP/1.1 403 Forbidden'); header('HTTP/1.1 403 Forbidden');
// @debug hack
throw OMB_RemoteServiceException::forRequest($uri,
'Revoked accesstoken for ' . $listenee . ': ' . $e->getMessage());
// @end debug
throw OMB_RemoteServiceException::forRequest($uri, throw OMB_RemoteServiceException::forRequest($uri,
'Revoked accesstoken for ' . $listenee); 'Revoked accesstoken for ' . $listenee);
} }
@ -323,8 +349,9 @@ class OMB_Service_Provider {
* Performs common OMB request handling finishing. * Performs common OMB request handling finishing.
* *
* @access protected * @access protected
**/ */
protected function finishOMBRequest() { protected function finishOMBRequest()
{
header('HTTP/1.1 200 OK'); header('HTTP/1.1 200 OK');
header('Content-type: text/plain'); header('Content-type: text/plain');
/* There should be no clutter but the version. */ /* There should be no clutter but the version. */
@ -338,8 +365,9 @@ class OMB_Service_Provider {
* default value. Returns the OAuth server. * default value. Returns the OAuth server.
* *
* @access protected * @access protected
**/ */
protected function getOAuthServer() { protected function getOAuthServer()
{
if (is_null($this->oauth_server)) { if (is_null($this->oauth_server)) {
$this->oauth_server = new OAuthServer($this->datastore); $this->oauth_server = new OAuthServer($this->datastore);
$this->oauth_server->add_signature_method( $this->oauth_server->add_signature_method(
@ -358,10 +386,11 @@ class OMB_Service_Provider {
* *
* @access public * @access public
* *
* @return array An array mapping subscriber URIs to the exception posting to * @return array An array mapping subscriber URIs to the exception posting
* them has raised; Empty array if no exception occured * to them has raised; Empty array if no exception occured
**/ */
public function postNotice($notice) { public function postNotice($notice)
{
$uri = $this->user->getIdentifierURI(); $uri = $this->user->getIdentifierURI();
/* $notice is passed by reference and may change. */ /* $notice is passed by reference and may change. */
@ -376,9 +405,10 @@ class OMB_Service_Provider {
require_once 'service_consumer.php'; require_once 'service_consumer.php';
$err = array(); $err = array();
foreach($subscribers as $subscriber) { foreach ($subscribers as $subscriber) {
try { try {
$service = new OMB_Service_Consumer($subscriber['uri'], $uri, $this->datastore); $service = new OMB_Service_Consumer($subscriber['uri'], $uri,
$this->datastore);
$service->setToken($subscriber['token'], $subscriber['secret']); $service->setToken($subscriber['token'], $subscriber['secret']);
$service->postNotice($notice); $service->postNotice($notice);
} catch (Exception $e) { } catch (Exception $e) {
@ -392,15 +422,16 @@ class OMB_Service_Provider {
/** /**
* Publish a profile update * Publish a profile update
* *
* Posts the current profile as an OMB profile update. This includes updating * Posts the current profile as an OMB profile update. This includes
* the stored profile and posting it to subscribed users. * updating the stored profile and posting it to subscribed users.
* *
* @access public * @access public
* *
* @return array An array mapping subscriber URIs to the exception posting to * @return array An array mapping subscriber URIs to the exception posting
* them has raised; Empty array if no exception occured * to them has raised; Empty array if no exception occured
**/ */
public function updateProfile() { public function updateProfile()
{
$uri = $this->user->getIdentifierURI(); $uri = $this->user->getIdentifierURI();
$this->datastore->saveProfile($this->user); $this->datastore->saveProfile($this->user);
@ -414,9 +445,10 @@ class OMB_Service_Provider {
require_once 'service_consumer.php'; require_once 'service_consumer.php';
$err = array(); $err = array();
foreach($subscribers as $subscriber) { foreach ($subscribers as $subscriber) {
try { try {
$service = new OMB_Service_Consumer($subscriber['uri'], $uri, $this->datastore); $service = new OMB_Service_Consumer($subscriber['uri'], $uri,
$this->datastore);
$service->setToken($subscriber['token'], $subscriber['secret']); $service->setToken($subscriber['token'], $subscriber['secret']);
$service->updateProfile($this->user); $service->updateProfile($this->user);
} catch (Exception $e) { } catch (Exception $e) {

View File

@ -1,9 +1,6 @@
<?php <?php
/** /**
* Exception stating that a requested service is not available * This file is part of libomb
*
* This exception is raised when OMB_Service is asked to call a service the remote
* server does not provide.
* *
* PHP version 5 * PHP version 5
* *
@ -22,10 +19,18 @@
* *
* @package OMB * @package OMB
* @author Adrian Lang <mail@adrianlang.de> * @author Adrian Lang <mail@adrianlang.de>
* @copyright 2009 Adrian Lang
* @license http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0
**/ * @version 0.1a-20090828
class OMB_UnsupportedServiceException extends Exception { * @link http://adrianlang.de/libomb
*/
/**
* Exception stating that a requested service is not available
*
* This exception is raised when OMB_Service is asked to call a service the
* remote server does not provide.
*/
class OMB_UnsupportedServiceException extends Exception
{
} }
?> ?>

View File

@ -1,10 +1,6 @@
<?php <?php
/** /**
* Map XRDS actions to URLs * This file is part of libomb
*
* This interface specifies classes which write the XRDS file announcing
* the OMB server. An instance of an implementing class should be passed to
* OMB_Service_Provider->writeXRDS.
* *
* PHP version 5 * PHP version 5
* *
@ -23,11 +19,29 @@
* *
* @package OMB * @package OMB
* @author Adrian Lang <mail@adrianlang.de> * @author Adrian Lang <mail@adrianlang.de>
* @copyright 2009 Adrian Lang
* @license http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0
**/ * @version 0.1a-20090828
* @link http://adrianlang.de/libomb
*/
interface OMB_XRDS_Mapper { /**
* Map XRDS actions to URLs
*
* This interface specifies classes which write the XRDS file announcing
* the OMB server. An instance of an implementing class should be passed to
* OMB_Service_Provider->writeXRDS.
*/
interface OMB_XRDS_Mapper
{
/**
* Fetch an URL for a specified action
*
* Returns the action URL for an action specified by the endpoint URI.
*
* @param string $action The endpoint URI
*
* @return string The action URL
*/
public function getURL($action); public function getURL($action);
} }
?> ?>

View File

@ -1,10 +1,6 @@
<?php <?php
/** /**
* Write OMB-specific XRDS * This file is part of libomb
*
* This interface specifies classes which write the XRDS file announcing
* the OMB server. An instance of an implementing class should be passed to
* OMB_Service_Provider->writeXRDS.
* *
* PHP version 5 * PHP version 5
* *
@ -23,11 +19,28 @@
* *
* @package OMB * @package OMB
* @author Adrian Lang <mail@adrianlang.de> * @author Adrian Lang <mail@adrianlang.de>
* @copyright 2009 Adrian Lang
* @license http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0
**/ * @version 0.1a-20090828
* @link http://adrianlang.de/libomb
*/
interface OMB_XRDS_Writer { /**
* Write OMB-specific XRDS
*
* This interface specifies classes which write the XRDS file announcing
* the OMB server. An instance of an implementing class should be passed to
* OMB_Service_Provider->writeXRDS.
*/
interface OMB_XRDS_Writer
{
/**
* Write XRDS
*
* Outputs a XRDS document specifying an OMB service.
*
* @param OMB_profile $user The target user for the OMB service
* @param OMB_XRDS_Mapper $mapper An OMB_XRDS_Mapper providing endpoint URLs
*/
public function writeXRDS($user, $mapper); public function writeXRDS($user, $mapper);
} }
?> ?>