diff options
-rw-r--r-- | Auth/OpenID.php | 19 | ||||
-rw-r--r-- | Auth/OpenID/Consumer.php | 4 | ||||
-rw-r--r-- | Auth/OpenID/ParanoidHTTPFetcher.php | 181 | ||||
-rw-r--r-- | Auth/OpenID/PlainHTTPFetcher.php (renamed from Auth/OpenID/Consumer/Fetchers.php) | 181 | ||||
-rw-r--r-- | Tests/Auth/OpenID/Consumer.php | 1 |
5 files changed, 206 insertions, 180 deletions
diff --git a/Auth/OpenID.php b/Auth/OpenID.php index b94f43c..3447b60 100644 --- a/Auth/OpenID.php +++ b/Auth/OpenID.php @@ -17,8 +17,27 @@ * @license http://www.gnu.org/copyleft/lesser.html LGPL */ +/** + * Detect the presence of Curl and set a flag accordingly. + */ +define('Auth_OpenID_CURL_PRESENT', function_exists('curl_init')); + class Auth_OpenID { + /** + * Factory function that will return an instance of the + * appropriate HTTP fetcher + */ + function getHTTPFetcher() + { + if (defined('Auth_OpenID_CURL_PRESENT') && + Auth_OpenID_CURL_PRESENT) { + $fetcher = new Auth_OpenID_ParanoidHTTPFetcher(); + } else { + $fetcher = new Auth_OpenID_PlainHTTPFetcher(); + } + return $fetcher; + } } ?>
\ No newline at end of file diff --git a/Auth/OpenID/Consumer.php b/Auth/OpenID/Consumer.php index 26369bd..3dbbc60 100644 --- a/Auth/OpenID/Consumer.php +++ b/Auth/OpenID/Consumer.php @@ -184,10 +184,10 @@ /** * Require utility classes and functions for the consumer. */ +require_once "Auth/OpenID.php"; require_once "Auth/OpenID/HMACSHA1.php"; require_once "Auth/OpenID/Association.php"; require_once "Auth/OpenID/AuthenticationRequest.php"; -require_once "Auth/OpenID/Consumer/Fetchers.php"; require_once "Auth/OpenID/Consumer/Parse.php"; require_once "Auth/OpenID/CryptUtil.php"; require_once "Auth/OpenID/DiffieHellman.php"; @@ -325,7 +325,7 @@ class Auth_OpenID_Consumer { $this->store->isDumb()); if ($fetcher === null) { - $this->fetcher = Auth_OpenID_getHTTPFetcher(); + $this->fetcher = Auth_OpenID::getHTTPFetcher(); } else { $this->fetcher =& $fetcher; } diff --git a/Auth/OpenID/ParanoidHTTPFetcher.php b/Auth/OpenID/ParanoidHTTPFetcher.php new file mode 100644 index 0000000..7b90c87 --- /dev/null +++ b/Auth/OpenID/ParanoidHTTPFetcher.php @@ -0,0 +1,181 @@ +<?php + +/** + * This module contains the CURL-based HTTP fetcher implementation. + * + * PHP versions 4 and 5 + * + * LICENSE: See the COPYING file included in this distribution. + * + * @package OpenID + * @author JanRain, Inc. <openid@janrain.com> + * @copyright 2005 Janrain, Inc. + * @license http://www.gnu.org/copyleft/lesser.html LGPL + */ + +/** + * Interface import + */ +require_once "Auth/OpenID/HTTPFetcher.php"; + +/** + * Define this based on whether the CURL extension is available. + */ +define('Auth_OpenID_CURL_PRESENT', function_exists('curl_init')); + +/** + * An array to store headers and data from Curl calls. + * + * @access private + */ +$_Auth_OpenID_curl_data = array(); + +/** + * A function to prepare a "slot" in the global $_Auth_OpenID_curl_data + * array so curl data can be stored there by curl callbacks in the + * paranoid fetcher. + * + * @access private + */ +function Auth_OpenID_initResponseSlot($ch) +{ + global $_Auth_OpenID_curl_data; + $key = strval($ch); + if (!array_key_exists($key, $_Auth_OpenID_curl_data)) { + $_Auth_OpenID_curl_data[$key] = array('headers' => array(), + 'body' => ""); + } + return $key; +} + +/** + * A callback function for curl so headers can be stored. + * + * @access private + */ +function Auth_OpenID_writeHeaders($ch, $data) +{ + global $_Auth_OpenID_curl_data; + $key = Auth_OpenID_initResponseSlot($ch); + $_Auth_OpenID_curl_data[$key]['headers'][] = rtrim($data); + return strlen($data); +} + +/** + * A callback function for curl so page data can be stored. + * + * @access private + */ +function Auth_OpenID_writeData($ch, $data) +{ + global $_Auth_OpenID_curl_data; + $key = Auth_OpenID_initResponseSlot($ch); + $_Auth_OpenID_curl_data[$key]['body'] .= $data; + return strlen($data); +} + +/** + * A paranoid Auth_OpenID_HTTPFetcher class which uses CURL for + * fetching. + * + * @package OpenID + */ +class Auth_OpenID_ParanoidHTTPFetcher extends Auth_OpenID_HTTPFetcher { + function Auth_OpenID_ParanoidHTTPFetcher() + { + if (!Auth_OpenID_CURL_PRESENT) { + trigger_error("Cannot use this class; CURL extension not found", + E_USER_ERROR); + } + } + + function get($url) + { + global $_Auth_OpenID_curl_data; + + $c = curl_init(); + + $curl_key = Auth_OpenID_initResponseSlot($c); + + curl_setopt($c, CURLOPT_NOSIGNAL, true); + + $stop = time() + $this->timeout; + $off = $this->timeout; + + while ($off > 0) { + if (!$this->allowedURL($url)) { + trigger_error(sprintf("Fetching URL not allowed: %s", $url), + E_USER_WARNING); + return null; + } + + curl_setopt($c, CURLOPT_WRITEFUNCTION, "Auth_OpenID_writeData"); + curl_setopt($c, CURLOPT_HEADERFUNCTION, "Auth_OpenID_writeHeaders"); + curl_setopt($c, CURLOPT_TIMEOUT, $off); + curl_setopt($c, CURLOPT_URL, $url); + + curl_exec($c); + + $code = curl_getinfo($c, CURLINFO_HTTP_CODE); + $body = $_Auth_OpenID_curl_data[$curl_key]['body']; + $headers = $_Auth_OpenID_curl_data[$curl_key]['headers']; + + if (!$code) { + trigger_error("No HTTP code returned", E_USER_WARNING); + return null; + } + + if (in_array($code, array(301, 302, 303, 307))) { + $url = $this->_findRedirect($headers); + } else { + curl_close($c); + return array($code, $url, $body); + } + + $off = $stop - time(); + } + + trigger_error(sprintf("Timed out fetching: %s", $url), + E_USER_WARNING); + + return null; + } + + function post($url, $body) + { + global $_Auth_OpenID_curl_data; + + if (!$this->allowedURL($url)) { + trigger_error(sprintf("Fetching URL not allowed: %s", $url), + E_USER_WARNING); + return null; + } + + $c = curl_init(); + + $curl_key = Auth_OpenID_initResponseSlot($c); + + curl_setopt($c, CURLOPT_NOSIGNAL, true); + curl_setopt($c, CURLOPT_POST, true); + curl_setopt($c, CURLOPT_POSTFIELDS, $body); + curl_setopt($c, CURLOPT_TIMEOUT, $this->timeout); + curl_setopt($c, CURLOPT_URL, $url); + curl_setopt($c, CURLOPT_WRITEFUNCTION, "Auth_OpenID_writeData"); + + curl_exec($c); + + $code = curl_getinfo($c, CURLINFO_HTTP_CODE); + + if (!$code) { + trigger_error("No HTTP code returned", E_USER_WARNING); + return null; + } + + $body = $_Auth_OpenID_curl_data[$curl_key]['body']; + + curl_close($c); + return array($code, $url, $body); + } +} + +?>
\ No newline at end of file diff --git a/Auth/OpenID/Consumer/Fetchers.php b/Auth/OpenID/PlainHTTPFetcher.php index 982b804..c6e1dce 100644 --- a/Auth/OpenID/Consumer/Fetchers.php +++ b/Auth/OpenID/PlainHTTPFetcher.php @@ -1,7 +1,8 @@ <?php + /** - * This module contains HTTP fetcher implementations - * XXX pear fixes needed + * This module contains the plain non-curl HTTP fetcher + * implementation. * * PHP versions 4 and 5 * @@ -19,26 +20,6 @@ require_once "Auth/OpenID/HTTPFetcher.php"; /** - * Detect the presence of Curl and set a flag accordingly. - */ -define('Auth_OpenID_CURL_PRESENT', function_exists('curl_init')); - -/** - * Factory function that will return an instance of the appropriate - * HTTP fetcher - */ -function Auth_OpenID_getHTTPFetcher() -{ - if (Auth_OpenID_CURL_PRESENT) { - $fetcher = new Auth_OpenID_ParanoidHTTPFetcher(); - } else { - $fetcher = new Auth_OpenID_PlainHTTPFetcher(); - } - - return $fetcher; -} - -/** * This class implements a plain, hand-built socket-based fetcher * which will be used in the event that CURL is unavailable. * @@ -218,160 +199,4 @@ class Auth_OpenID_PlainHTTPFetcher extends Auth_OpenID_HTTPFetcher { } } -/** - * An array to store headers and data from Curl calls. - * - * @access private - */ -$_Auth_OpenID_curl_data = array(); - -/** - * A function to prepare a "slot" in the global $_Auth_OpenID_curl_data - * array so curl data can be stored there by curl callbacks in the - * paranoid fetcher. - * - * @access private - */ -function Auth_OpenID_initResponseSlot($ch) -{ - global $_Auth_OpenID_curl_data; - $key = strval($ch); - if (!array_key_exists($key, $_Auth_OpenID_curl_data)) { - $_Auth_OpenID_curl_data[$key] = array('headers' => array(), - 'body' => ""); - } - return $key; -} - -/** - * A callback function for curl so headers can be stored. - * - * @access private - */ -function Auth_OpenID_writeHeaders($ch, $data) -{ - global $_Auth_OpenID_curl_data; - $key = Auth_OpenID_initResponseSlot($ch); - $_Auth_OpenID_curl_data[$key]['headers'][] = rtrim($data); - return strlen($data); -} - -/** - * A callback function for curl so page data can be stored. - * - * @access private - */ -function Auth_OpenID_writeData($ch, $data) -{ - global $_Auth_OpenID_curl_data; - $key = Auth_OpenID_initResponseSlot($ch); - $_Auth_OpenID_curl_data[$key]['body'] .= $data; - return strlen($data); -} - - -/** - * A paranoid Auth_OpenID_HTTPFetcher class which uses CURL for - * fetching. - * - * @package OpenID - */ -class Auth_OpenID_ParanoidHTTPFetcher extends Auth_OpenID_HTTPFetcher { - function Auth_OpenID_ParanoidHTTPFetcher() - { - if (!Auth_OpenID_CURL_PRESENT) { - trigger_error("Cannot use this class; CURL extension not found", - E_USER_ERROR); - } - } - - function get($url) - { - global $_Auth_OpenID_curl_data; - - $c = curl_init(); - - $curl_key = Auth_OpenID_initResponseSlot($c); - - curl_setopt($c, CURLOPT_NOSIGNAL, true); - - $stop = time() + $this->timeout; - $off = $this->timeout; - - while ($off > 0) { - if (!$this->allowedURL($url)) { - trigger_error(sprintf("Fetching URL not allowed: %s", $url), - E_USER_WARNING); - return null; - } - - curl_setopt($c, CURLOPT_WRITEFUNCTION, "Auth_OpenID_writeData"); - curl_setopt($c, CURLOPT_HEADERFUNCTION, "Auth_OpenID_writeHeaders"); - curl_setopt($c, CURLOPT_TIMEOUT, $off); - curl_setopt($c, CURLOPT_URL, $url); - - curl_exec($c); - - $code = curl_getinfo($c, CURLINFO_HTTP_CODE); - $body = $_Auth_OpenID_curl_data[$curl_key]['body']; - $headers = $_Auth_OpenID_curl_data[$curl_key]['headers']; - - if (!$code) { - trigger_error("No HTTP code returned", E_USER_WARNING); - return null; - } - - if (in_array($code, array(301, 302, 303, 307))) { - $url = $this->_findRedirect($headers); - } else { - curl_close($c); - return array($code, $url, $body); - } - - $off = $stop - time(); - } - - trigger_error(sprintf("Timed out fetching: %s", $url), - E_USER_WARNING); - - return null; - } - - function post($url, $body) - { - global $_Auth_OpenID_curl_data; - - if (!$this->allowedURL($url)) { - trigger_error(sprintf("Fetching URL not allowed: %s", $url), - E_USER_WARNING); - return null; - } - - $c = curl_init(); - - $curl_key = Auth_OpenID_initResponseSlot($c); - - curl_setopt($c, CURLOPT_NOSIGNAL, true); - curl_setopt($c, CURLOPT_POST, true); - curl_setopt($c, CURLOPT_POSTFIELDS, $body); - curl_setopt($c, CURLOPT_TIMEOUT, $this->timeout); - curl_setopt($c, CURLOPT_URL, $url); - curl_setopt($c, CURLOPT_WRITEFUNCTION, "Auth_OpenID_writeData"); - - curl_exec($c); - - $code = curl_getinfo($c, CURLINFO_HTTP_CODE); - - if (!$code) { - trigger_error("No HTTP code returned", E_USER_WARNING); - return null; - } - - $body = $_Auth_OpenID_curl_data[$curl_key]['body']; - - curl_close($c); - return array($code, $url, $body); - } -} - ?>
\ No newline at end of file diff --git a/Tests/Auth/OpenID/Consumer.php b/Tests/Auth/OpenID/Consumer.php index f14bd9a..7ffe676 100644 --- a/Tests/Auth/OpenID/Consumer.php +++ b/Tests/Auth/OpenID/Consumer.php @@ -19,6 +19,7 @@ require_once 'Auth/OpenID/Store/FileStore.php'; require_once 'Auth/OpenID/Util.php'; require_once 'Auth/OpenID/KVForm.php'; require_once 'Auth/OpenID/Consumer.php'; +require_once 'Auth/OpenID/HTTPFetcher.php'; require_once 'PHPUnit.php'; class Auth_OpenID_TestConsumer extends Auth_OpenID_Consumer { |