Source code for file /openid/Auth/OpenID.php
Documentation is available at OpenID.php
* This is the PHP OpenID library by JanRain, Inc.
* This module contains core utility functionality used by the
* library. See Consumer.php and Server.php for the consumer and
* server implementations.
* LICENSE: See the COPYING file included in this distribution.
* @copyright 2005 Janrain, Inc.
* @license http://www.gnu.org/copyleft/lesser.html LGPL
* Require the fetcher code.
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
* an error page, since we do not have enough information to redirect
* back to the consumer. The associated value is an error message that
* should be displayed on an HTML error page.
* @see Auth_OpenID_Server
define('Auth_OpenID_LOCAL_ERROR', 'local_error');
* Status code returned when there is an error to return in key-value
* form to the consumer. The caller should return a 400 Bad Request
* response with content-type text/plain and the value as the body.
* @see Auth_OpenID_Server
define('Auth_OpenID_REMOTE_ERROR', 'remote_error');
* Status code returned when there is a key-value form OK response to
* the consumer. The value associated with this code is the
* response. The caller should return a 200 OK response with
* content-type text/plain and the value as the body.
* @see Auth_OpenID_Server
define('Auth_OpenID_REMOTE_OK', 'remote_ok');
* Status code returned when there is a redirect back to the
* consumer. The value is the URL to redirect back to. The caller
* should return a 302 Found redirect with a Location: header
* @see Auth_OpenID_Server
define('Auth_OpenID_REDIRECT', 'redirect');
* Status code returned when the caller needs to authenticate the
* user. The associated value is a {@link Auth_OpenID_ServerRequest}
* object that can be used to complete the authentication. If the user
* has taken some authentication action, use the retry() method of the
* {@link Auth_OpenID_ServerRequest} object to complete the request.
* @see Auth_OpenID_Server
define('Auth_OpenID_DO_AUTH', 'do_auth');
* Status code returned when there were no OpenID arguments
* passed. This code indicates that the caller should return a 200 OK
* response and display an HTML page that says that this is an OpenID
* @see Auth_OpenID_Server
define('Auth_OpenID_DO_ABOUT', 'do_about');
* Defines for regexes and format checking.
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
"!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~");
* These namespaces are automatically fixed in query arguments by
global $_Auth_OpenID_namespaces;
$_Auth_OpenID_namespaces =
array('openid',
* The OpenID utility function class.
* Rename query arguments back to 'openid.' from 'openid_'
* @param array $args An associative array of URL query arguments
global $_Auth_OpenID_namespaces;
foreach ($_Auth_OpenID_namespaces as $ns) {
* Create dir_name as a directory if it does not exist. If it
* exists, make sure that it is, in fact, a directory. Returns
* true if the operation succeeded; false if not.
function ensureDir($dir_name)
if (Auth_OpenID::ensureDir(dirname($dir_name))) {
* Convenience function for getting array values.
function arrayGet($arr, $key, $fallback =
null)
"array as first parameter", E_USER_WARNING);
* Implements the PHP 5 'http_build_query' functionality.
* @param array $data Either an array key/value pairs or an array
* of arrays, each of which holding two values: a key and a value,
* @return string $result The result of url-encoding the key/value
* pairs from $data into a URL query string
* (e.g. "username=bob&id=56").
function httpBuildQuery($data)
foreach ($data as $key =>
$value) {
* "Appends" query arguments onto a URL. The URL may or may not
* already have arguments (following a question mark).
* @param string $url A URL, which may or may not already have
* @param array $args Either an array key/value pairs or an array of
* arrays, each of which holding two values: a key and a value,
* sequentially. If $args is an ordinary key/value array, the
* parameters will be added to the URL in sorted alphabetical order;
* if $args is an array of arrays, their order will be preserved.
* @return string $url The original URL with the new parameters added.
function appendArgs($url, $args)
// Non-empty array; if it is an array of arrays, use
// multisort; otherwise use sort.
foreach ($keys as $key) {
$new_args[] =
array($key, $args[$key]);
if (strpos($url, '?') !==
false) {
return $url .
$sep .
Auth_OpenID::httpBuildQuery($args);
* Turn a string into an ASCII string.
* Replace non-ascii characters with a %-encoded, UTF-8
* encoding. This function will fail if the input is a string and
* there are non-7-bit-safe characters. It is assumed that the
* caller will have already translated the input into a Unicode
* character sequence, according to the encoding of the HTTP POST
* Do not escape anything that is already 7-bit safe, so we do the
* minimal transform on the identity URL
function quoteMinimal($s)
for ($i =
0; $i <
strlen($s); $i++
) {
* Implements python's urlunparse, which is not available in PHP.
* Given the specified components of a URL, this function rebuilds
* @param string $scheme The scheme (e.g. 'http'). Defaults to 'http'.
* @param string $host The host. Required.
* @param string $port The port.
* @param string $path The path.
* @param string $query The query.
* @param string $fragment The fragment.
* @return string $url The URL resulting from assembling the
function urlunparse($scheme, $host, $port =
null, $path =
'/',
$query =
'', $fragment =
'')
$result =
$scheme .
"://" .
$host;
$result .=
"#" .
$fragment;
* Given a URL, this "normalizes" it by adding a trailing slash
* and / or a leading http:// scheme where necessary. Returns
* null if the original URL is malformed and cannot be normalized.
* @param string $url The URL to be normalized.
* @return mixed $new_url The URL after normalization, or null if
function normalizeUrl($url)
if (strpos($url, "://") ===
false) {
if (($parsed['scheme'] ==
'') ||
($parsed['host'] ==
'')) {
if ($parsed['path'] ==
'' &&
$parsed['query'] ==
'' &&
$parsed['fragment'] ==
'') {
$tail =
array_map(array('Auth_OpenID', 'quoteMinimal'),
$url =
Auth_OpenID::urlunparse($parsed['scheme'], $parsed['host'],
$parsed['port'], $tail[0], $tail[1],