summaryrefslogtreecommitdiffstats
path: root/Auth
diff options
context:
space:
mode:
authortailor <cygnus@janrain.com>2006-08-11 18:12:24 +0000
committertailor <cygnus@janrain.com>2006-08-11 18:12:24 +0000
commita0d078b6693af6a72e9f4790d28a3af8bf5817d8 (patch)
tree6c2765c2260e186817ee6d077fc5f9ad92ac970d /Auth
parentd955eb77e94fe5e04c3d9525fe2cec378af9948d (diff)
downloadphp-openid-a0d078b6693af6a72e9f4790d28a3af8bf5817d8.zip
php-openid-a0d078b6693af6a72e9f4790d28a3af8bf5817d8.tar.gz
php-openid-a0d078b6693af6a72e9f4790d28a3af8bf5817d8.tar.bz2
[project @ Merged yadis discovery code and updated test framework to run tests in both packages]
Diffstat (limited to 'Auth')
-rw-r--r--Auth/OpenID.php12
-rw-r--r--Auth/OpenID/ParanoidHTTPFetcher.php189
-rw-r--r--Auth/OpenID/PlainHTTPFetcher.php237
3 files changed, 6 insertions, 432 deletions
diff --git a/Auth/OpenID.php b/Auth/OpenID.php
index 534fc9d..3cd296f 100644
--- a/Auth/OpenID.php
+++ b/Auth/OpenID.php
@@ -20,8 +20,8 @@
/**
* Require the fetcher code.
*/
-require_once "Auth/OpenID/PlainHTTPFetcher.php";
-require_once "Auth/OpenID/ParanoidHTTPFetcher.php";
+require_once "Services/Yadis/PlainHTTPFetcher.php";
+require_once "Services/Yadis/ParanoidHTTPFetcher.php";
/**
* Status code returned by the server when the only option is to show
@@ -117,11 +117,11 @@ class Auth_OpenID {
*/
function getHTTPFetcher()
{
- if (defined('Auth_OpenID_CURL_PRESENT') &&
- Auth_OpenID_CURL_PRESENT) {
- $fetcher = new Auth_OpenID_ParanoidHTTPFetcher();
+ if (defined('Services_Yadis_CURL_PRESENT') &&
+ Services_Yadis_CURL_PRESENT) {
+ $fetcher = new Services_Yadis_ParanoidHTTPFetcher();
} else {
- $fetcher = new Auth_OpenID_PlainHTTPFetcher();
+ $fetcher = new Services_Yadis_PlainHTTPFetcher();
}
return $fetcher;
}
diff --git a/Auth/OpenID/ParanoidHTTPFetcher.php b/Auth/OpenID/ParanoidHTTPFetcher.php
deleted file mode 100644
index 1f25785..0000000
--- a/Auth/OpenID/ParanoidHTTPFetcher.php
+++ /dev/null
@@ -1,189 +0,0 @@
-<?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'));
-
-/**
- * A paranoid {@link 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);
- }
-
- $this->headers = array();
- $this->data = "";
-
- $this->reset();
- }
-
- function reset()
- {
- $this->headers = array();
- $this->data = "";
- }
-
- /**
- * @access private
- */
- function _writeHeader($ch, $header)
- {
- array_push($this->headers, rtrim($header));
- return strlen($header);
- }
-
- /**
- * @access private
- */
- function _writeData($ch, $data)
- {
- $this->data .= $data;
- return strlen($data);
- }
-
- function get($url, $extra_headers = null)
- {
- $stop = time() + $this->timeout;
- $off = $this->timeout;
-
- $redir = true;
-
- while ($redir && ($off > 0)) {
- $this->reset();
-
- $c = curl_init();
- curl_setopt($c, CURLOPT_NOSIGNAL, true);
-
- if (!$this->allowedURL($url)) {
- trigger_error(sprintf("Fetching URL not allowed: %s", $url),
- E_USER_WARNING);
- return null;
- }
-
- curl_setopt($c, CURLOPT_WRITEFUNCTION,
- array(&$this, "_writeData"));
- curl_setopt($c, CURLOPT_HEADERFUNCTION,
- array(&$this, "_writeHeader"));
-
- if ($extra_headers) {
- curl_setopt($c, CURLOPT_HTTPHEADER, $extra_headers);
- }
-
- curl_setopt($c, CURLOPT_TIMEOUT, $off);
- curl_setopt($c, CURLOPT_URL, $url);
-
- curl_exec($c);
-
- $code = curl_getinfo($c, CURLINFO_HTTP_CODE);
- $body = $this->data;
- $headers = $this->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);
- $redir = true;
- } else {
- $redir = false;
- curl_close($c);
-
- $new_headers = array();
-
- foreach ($headers as $header) {
- if (preg_match("/:/", $header)) {
- list($name, $value) = explode(": ", $header, 2);
- $new_headers[$name] = $value;
- }
- }
-
- return new Auth_OpenID_HTTPResponse($url, $code,
- $new_headers, $body);
- }
-
- $off = $stop - time();
- }
-
- trigger_error(sprintf("Timed out fetching: %s", $url),
- E_USER_WARNING);
-
- return null;
- }
-
- function post($url, $body)
- {
- $this->reset();
-
- if (!$this->allowedURL($url)) {
- trigger_error(sprintf("Fetching URL not allowed: %s", $url),
- E_USER_WARNING);
- return null;
- }
-
- $c = curl_init();
-
- 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,
- array(&$this, "_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 = $this->data;
-
- curl_close($c);
-
- $new_headers = array();
-
- foreach ($this->headers as $header) {
- if (preg_match("/:/", $header)) {
- list($name, $value) = explode(": ", $header, 2);
- $new_headers[$name] = $value;
- }
-
- }
-
- return new Auth_OpenID_HTTPResponse($url, $code,
- $new_headers, $body);
- }
-}
-
-?> \ No newline at end of file
diff --git a/Auth/OpenID/PlainHTTPFetcher.php b/Auth/OpenID/PlainHTTPFetcher.php
deleted file mode 100644
index 9919066..0000000
--- a/Auth/OpenID/PlainHTTPFetcher.php
+++ /dev/null
@@ -1,237 +0,0 @@
-<?php
-
-/**
- * This module contains the plain non-curl 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";
-
-/**
- * This class implements a plain, hand-built socket-based fetcher
- * which will be used in the event that CURL is unavailable.
- *
- * @package OpenID
- */
-class Auth_OpenID_PlainHTTPFetcher extends Auth_OpenID_HTTPFetcher {
- function get($url, $extra_headers = null)
- {
- if (!$this->allowedURL($url)) {
- trigger_error("Bad URL scheme in url: " . $url,
- E_USER_WARNING);
- return null;
- }
-
- $redir = true;
-
- $stop = time() + $this->timeout;
- $off = $this->timeout;
-
- while ($redir && ($off > 0)) {
-
- $parts = parse_url($url);
-
- $specify_port = true;
-
- // Set a default port.
- if (!array_key_exists('port', $parts)) {
- $specify_port = false;
- if ($parts['scheme'] == 'http') {
- $parts['port'] = 80;
- } elseif ($parts['scheme'] == 'https') {
- $parts['port'] = 443;
- } else {
- trigger_error("fetcher post method doesn't support " .
- " scheme '" . $parts['scheme'] .
- "', no default port available",
- E_USER_WARNING);
- return null;
- }
- }
-
- $host = $parts['host'];
-
- if ($parts['scheme'] == 'https') {
- $host = 'ssl://' . $host;
- }
-
- $user_agent = "PHP OpenID Library Fetcher";
-
- $headers = array(
- "GET ".$parts['path'].
- (array_key_exists('query', $parts) ?
- "?".$parts['query'] : "").
- " HTTP/1.1",
- "User-Agent: $user_agent",
- "Host: ".$parts['host'].
- ($specify_port ? ":".$parts['port'] : ""),
- "Port: ".$parts['port'],
- "Connection: close",
- "Cache-Control: no-cache");
-
- $errno = 0;
- $errstr = '';
-
- if ($extra_headers) {
- foreach ($extra_headers as $h) {
- $headers[] = $h;
- }
- }
-
- $sock = fsockopen($host, $parts['port'], $errno, $errstr,
- $this->timeout);
- if ($sock === false) {
- return false;
- }
-
- stream_set_timeout($sock, $this->timeout);
-
- fputs($sock, implode("\r\n", $headers) . "\r\n\r\n");
-
- $data = "";
- while (!feof($sock)) {
- $data .= fgets($sock, 1024);
- }
-
- fclose($sock);
-
- // Split response into header and body sections
- list($headers, $body) = explode("\r\n\r\n", $data, 2);
- $headers = explode("\r\n", $headers);
-
- $http_code = explode(" ", $headers[0]);
- $code = $http_code[1];
-
- if (in_array($code, array('301', '302'))) {
- $url = $this->_findRedirect($headers);
- $redir = true;
- } else {
- $redir = false;
- }
-
- $off = $stop - time();
- }
-
- $new_headers = array();
-
- foreach ($headers as $header) {
- if (preg_match("/:/", $header)) {
- list($name, $value) = explode(": ", $header, 2);
- $new_headers[$name] = $value;
- }
-
- }
-
- return new Auth_OpenID_HTTPResponse($url, $code, $new_headers, $body);
- }
-
- function post($url, $body)
- {
- if (!$this->allowedURL($url)) {
- trigger_error("Bad URL scheme in url: " . $url,
- E_USER_WARNING);
- return null;
- }
-
- $parts = parse_url($url);
-
- $headers = array();
-
- $headers[] = "POST ".$parts['path']." HTTP/1.1";
- $headers[] = "Host: " . $parts['host'];
- $headers[] = "Content-type: application/x-www-form-urlencoded";
- $headers[] = "Content-length: " . strval(strlen($body));
-
- // Join all headers together.
- $all_headers = implode("\r\n", $headers);
-
- // Add headers, two newlines, and request body.
- $request = $all_headers . "\r\n\r\n" . $body;
-
- // Set a default port.
- if (!array_key_exists('port', $parts)) {
- if ($parts['scheme'] == 'http') {
- $parts['port'] = 80;
- } elseif ($parts['scheme'] == 'https') {
- $parts['port'] = 443;
- } else {
- trigger_error("fetcher post method doesn't support scheme '" .
- $parts['scheme'] .
- "', no default port available",
- E_USER_WARNING);
- return null;
- }
- }
-
- if ($parts['scheme'] == 'https') {
- $parts['host'] = sprintf("ssl://%s", $parts['host']);
- }
-
- // Connect to the remote server.
- $errno = 0;
- $errstr = '';
-
- $sock = fsockopen($parts['host'], $parts['port'], $errno, $errstr,
- $this->timeout);
-
- if ($sock === false) {
- trigger_error("Could not connect to " . $parts['host'] .
- " port " . $parts['port'],
- E_USER_WARNING);
- return null;
- }
-
- stream_set_timeout($sock, $this->timeout);
-
- // Write the POST request.
- fputs($sock, $request);
-
- // Get the response from the server.
- $response = "";
- while (!feof($sock)) {
- if ($data = fgets($sock, 128)) {
- $response .= $data;
- } else {
- break;
- }
- }
-
- // Split the request into headers and body.
- list($headers, $response_body) = explode("\r\n\r\n", $response, 2);
-
- $headers = explode("\r\n", $headers);
-
- // Expect the first line of the headers data to be something
- // like HTTP/1.1 200 OK. Split the line on spaces and take
- // the second token, which should be the return code.
- $http_code = explode(" ", $headers[0]);
- $code = $http_code[1];
-
- $new_headers = array();
-
- foreach ($headers as $header) {
- if (preg_match("/:/", $header)) {
- list($name, $value) = explode(": ", $header, 2);
- $new_headers[$name] = $value;
- }
-
- }
-
- return new Auth_OpenID_HTTPResponse($url, $code,
- $headers, $response_body);
- }
-}
-
-?> \ No newline at end of file