Vyacheslav Slinko

One class — one file

Prepare for standards for PHP 5.3 namespaces and class names
http://groups.google.com/group/php-standards/web/psr-0-final-proposal
<?php
/**
* Copyright (c) 2010 VZnet Netzwerke Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author Bastian Hofmann <bhfomann@vz.net>
* @copyright 2010 VZnet Netzwerke Ltd.
* @license http://www.opensource.org/licenses/mit-license.html MIT License
*/
class OAuth2_Client
{
/**
* @var string
*/
private $_clientKey;
/**
* @var string
*/
private $_clientSecret;
/**
* @var string
*/
private $_callbackUrl;
/**
*
* @param string $clientKey
* @param string $clientSecret
* @param string $callbackUrl
*/
public function __construct($clientKey, $clientSecret, $callbackUrl) {
$this->_clientKey = $clientKey;
$this->_clientSecret = $clientSecret;
$this->_callbackUrl = $callbackUrl;
}
/**
* @return string
*/
public function getClientKey() {
return $this->_clientKey;
}
/**
* @return string
*/
public function getClientSecret() {
return $this->_clientSecret;
}
/**
* @return string
*/
public function getCallbackUrl() {
return $this->_callbackUrl;
}
}
... ...
<?php
/**
* Copyright (c) 2010 VZnet Netzwerke Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author Bastian Hofmann <bhfomann@vz.net>
* @copyright 2010 VZnet Netzwerke Ltd.
* @license http://www.opensource.org/licenses/mit-license.html MIT License
*/
interface OAuth2_DataStore_Interface
{
/**
* @param OAuth2_Token $token
*/
function storeAccessToken(OAuth2_Token $token);
/**
* @return OAuth2_Token
*/
function retrieveAccessToken();
}
... ...
<?php
/**
* Copyright (c) 2010 VZnet Netzwerke Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author Bastian Hofmann <bhfomann@vz.net>
* @copyright 2010 VZnet Netzwerke Ltd.
* @license http://www.opensource.org/licenses/mit-license.html MIT License
*/
class OAuth2_DataStore_Session implements OAuth2_DataStore_Interface
{
public function __construct() {
session_start();
}
/**
*
* @return OAuth2_Token
*/
public function retrieveAccessToken() {
return isset($_SESSION['oauth2_token']) ? $_SESSION['oauth2_token'] : new OAuth2_Token();
}
/**
* @param OAuth2_Token $token
*/
public function storeAccessToken(OAuth2_Token $token) {
$_SESSION['oauth2_token'] = $token;
}
public function __destruct() {
session_write_close();
}
}
... ...
<?php
/**
* Copyright (c) 2010 VZnet Netzwerke Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author Bastian Hofmann <bhfomann@vz.net>
* @copyright 2010 VZnet Netzwerke Ltd.
* @license http://www.opensource.org/licenses/mit-license.html MIT License
*/
class OAuth2_Exception extends Exception {}
... ...
<?php
/**
* Copyright (c) 2010 VZnet Netzwerke Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author Bastian Hofmann <bhfomann@vz.net>
* @copyright 2010 VZnet Netzwerke Ltd.
* @license http://www.opensource.org/licenses/mit-license.html MIT License
*/
class OAuth2_HttpClient
{
/**
* @var string
*/
private $_url;
/**
* @var string
*/
private $_method;
/**
* @var string
*/
private $_parameters;
/**
* @var array
*/
private $_requestHeader;
/**
* @var string
*/
private $_response;
/**
* @var array
*/
private $_headers;
/**
* @var array
*/
private $_info;
/**
* @var boolean
*/
private $_debug = false;
/**
* @param string $url
* @param string $method
* @param string $parameters
* @param array $header any additional header which should be set
*/
public function __construct($url, $method, $parameters = null, array $header = array()) {
$this->_url = $url;
$this->_method = $method;
$this->_parameters = $parameters;
$this->_requestHeader = $header;
}
/**
* parses a string with two delimiters to an array
*
* example:
*
* param1=value1&param2=value2
*
* will result with delimiters & and = to
*
* array(
* 'param1' => 'value1',
* 'param2' => 'value2',
* )
*
* @param string $string
* @param string $firstDelimiter
* @param string $secondDelimiter
* @return array
*/
public static function parseStringToArray($string, $firstDelimiter, $secondDelimiter) {
$resultArray = array();
$parts = explode($firstDelimiter, $string);
foreach ($parts as $part) {
$partsPart = explode($secondDelimiter, $part);
$resultArray[$partsPart[0]] = isset($partsPart[1]) ? trim($partsPart[1]) : '';
}
return $resultArray;
}
/**
* executes the curl request
*/
public function execute() {
$ch = curl_init();
if ($this->_method === 'POST') {
curl_setopt($ch, CURLOPT_URL, $this->_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->_parameters);
} else {
curl_setopt($ch, CURLOPT_URL, $this->_url . ($this->_parameters ? '?' . $this->_parameters : ''));
}
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if (! empty($this->_requestHeader)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->_requestHeader);
}
$fullResponse = curl_exec($ch);
$this->_info = curl_getinfo($ch);
$this->_response = substr($fullResponse, $this->_info['header_size'], strlen($fullResponse));
if ($this->_response === false) {
$this->_response = '';
}
$headers = rtrim(substr($fullResponse, 0, $this->_info['header_size']));
$this->_headers = OAuth2_HttpClient::parseStringToArray($headers, PHP_EOL, ':');
if ($this->_debug) {
echo "<pre>";
print_r($this->_url);
echo PHP_EOL;
print_r($this->_headers);
echo PHP_EOL;
print_r($this->_response);
echo "</pre>";
}
curl_close($ch);
}
/**
* @return string
*/
public function getResponse() {
return $this->_response;
}
/**
* @return array
*/
public function getHeaders() {
return $this->_headers;
}
/**
* @param boolean $debug
*/
public function setDebug($debug) {
$this->_debug = $debug;
}
}
... ...
... ... @@ -25,65 +25,6 @@
* @license http://www.opensource.org/licenses/mit-license.html MIT License
*/
class OAuth2_Service_Configuration
{
const AUTHORIZATION_METHOD_HEADER = 1;
const AUTHORIZATION_METHOD_ALTERNATIVE = 2;
/**
* @var string
*/
private $_authorizeEndpoint;
/**
* @var string
*/
private $_accessTokenEndpoint;
/**
* @var string
*/
private $_authorizationMethod = self::AUTHORIZATION_METHOD_HEADER;
/**
* @param string $authorizeEndpoint
* @param string $accessTokenEndpoint
*/
public function __construct($authorizeEndpoint, $accessTokenEndpoint) {
$this->_authorizeEndpoint = $authorizeEndpoint;
$this->_accessTokenEndpoint = $accessTokenEndpoint;
}
/**
* @return string
*/
public function getAuthorizeEndpoint() {
return $this->_authorizeEndpoint;
}
/**
* @return string
*/
public function getAccessTokenEndpoint() {
return $this->_accessTokenEndpoint;
}
/**
* @return string
*/
public function setAuthorizationMethod($authorizationMethod) {
$this->_authorizationMethod = $authorizationMethod;
}
/**
* @return string
*/
public function getAuthorizationMethod() {
return $this->_authorizationMethod;
}
}
class OAuth2_Service
{
/**
... ... @@ -311,336 +252,3 @@ class OAuth2_Service
return $http->getResponse();
}
}
class OAuth2_Token
{
/**
* @var string
*/
private $_accessToken;
/**
* @var string
*/
private $_refreshToken;
/**
* @var string
*/
private $_lifeTime;
/**
* @var array
*/
private $_additionalParams = array();
/**
*
* @param string $accessToken
* @param string $refreshToken
* @param int $lifeTime
*/
public function __construct($accessToken = null, $refreshToken = null, $lifeTime = null) {
$this->_accessToken = $accessToken;
$this->_refreshToken = $refreshToken;
if ($lifeTime) {
$this->_lifeTime = ((int)$lifeTime) + time();
}
}
/**
* magic method for setting and getting additional parameters returned from
* service
*
* e.g. user_id parameter with scope openid
*
* @param string $name
* @param array $arguments
* @return mixed
*/
public function __call($name, $arguments) {
if (strlen($name) < 4) {
throw new OAuth2_Exception('undefined magic method called');
}
$method = substr($name, 0, 3);
$param = substr($name, 3);
switch ($method) {
case 'get':
if (! isset($this->_additionalParams[$param])) {
throw new OAuth2_Exception($param . ' was not returned by service');
}
return $this->_additionalParams[$param];
case 'set':
if (! array_key_exists(0, $arguments)) {
throw new OAuth2_Exception('magic setter has no argument');
}
$this->_additionalParams[$param] = $arguments[0];
break;
default:
throw new OAuth2_Exception('undefined magic method called');
}
}
/**
* @return string
*/
public function getAccessToken() {
return $this->_accessToken;
}
/**
* @return string
*/
public function getRefreshToken() {
return $this->_refreshToken;
}
/**
* @return int
*/
public function getLifeTime() {
return $this->_lifeTime;
}
}
class OAuth2_DataStore_Session implements OAuth2_DataStore_Interface
{
public function __construct() {
session_start();
}
/**
*
* @return OAuth2_Token
*/
public function retrieveAccessToken() {
return isset($_SESSION['oauth2_token']) ? $_SESSION['oauth2_token'] : new OAuth2_Token();
}
/**
* @param OAuth2_Token $token
*/
public function storeAccessToken(OAuth2_Token $token) {
$_SESSION['oauth2_token'] = $token;
}
public function __destruct() {
session_write_close();
}
}
interface OAuth2_DataStore_Interface
{
/**
* @param OAuth2_Token $token
*/
function storeAccessToken(OAuth2_Token $token);
/**
* @return OAuth2_Token
*/
function retrieveAccessToken();
}
class OAuth2_Client
{
/**
* @var string
*/
private $_clientKey;
/**
* @var string
*/
private $_clientSecret;
/**
* @var string
*/
private $_callbackUrl;
/**
*
* @param string $clientKey
* @param string $clientSecret
* @param string $callbackUrl
*/
public function __construct($clientKey, $clientSecret, $callbackUrl) {
$this->_clientKey = $clientKey;
$this->_clientSecret = $clientSecret;
$this->_callbackUrl = $callbackUrl;
}
/**
* @return string
*/
public function getClientKey() {
return $this->_clientKey;
}
/**
* @return string
*/
public function getClientSecret() {
return $this->_clientSecret;
}
/**
* @return string
*/
public function getCallbackUrl() {
return $this->_callbackUrl;
}
}
class OAuth2_HttpClient
{
/**
* @var string
*/
private $_url;
/**
* @var string
*/
private $_method;
/**
* @var string
*/
private $_parameters;
/**
* @var array
*/
private $_requestHeader;
/**
* @var string
*/
private $_response;
/**
* @var array
*/
private $_headers;
/**
* @var array
*/
private $_info;
/**
* @var boolean
*/
private $_debug = false;
/**
* @param string $url
* @param string $method
* @param string $parameters
* @param array $header any additional header which should be set
*/
public function __construct($url, $method, $parameters = null, array $header = array()) {
$this->_url = $url;
$this->_method = $method;
$this->_parameters = $parameters;
$this->_requestHeader = $header;
}
/**
* parses a string with two delimiters to an array
*
* example:
*
* param1=value1&param2=value2
*
* will result with delimiters & and = to
*
* array(
* 'param1' => 'value1',
* 'param2' => 'value2',
* )
*
* @param string $string
* @param string $firstDelimiter
* @param string $secondDelimiter
* @return array
*/
public static function parseStringToArray($string, $firstDelimiter, $secondDelimiter) {
$resultArray = array();
$parts = explode($firstDelimiter, $string);
foreach ($parts as $part) {
$partsPart = explode($secondDelimiter, $part);
$resultArray[$partsPart[0]] = isset($partsPart[1]) ? trim($partsPart[1]) : '';
}
return $resultArray;
}
/**
* executes the curl request
*/
public function execute() {
$ch = curl_init();
if ($this->_method === 'POST') {
curl_setopt($ch, CURLOPT_URL, $this->_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->_parameters);
} else {
curl_setopt($ch, CURLOPT_URL, $this->_url . ($this->_parameters ? '?' . $this->_parameters : ''));
}
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if (! empty($this->_requestHeader)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->_requestHeader);
}
$fullResponse = curl_exec($ch);
$this->_info = curl_getinfo($ch);
$this->_response = substr($fullResponse, $this->_info['header_size'], strlen($fullResponse));
if ($this->_response === false) {
$this->_response = '';
}
$headers = rtrim(substr($fullResponse, 0, $this->_info['header_size']));
$this->_headers = OAuth2_HttpClient::parseStringToArray($headers, PHP_EOL, ':');
if ($this->_debug) {
echo "<pre>";
print_r($this->_url);
echo PHP_EOL;
print_r($this->_headers);
echo PHP_EOL;
print_r($this->_response);
echo "</pre>";
}
curl_close($ch);
}
/**
* @return string
*/