123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564 |
- <?php
- define('Auth_OpenID_VERSION', '2.2.2');
- require_once "Auth/Yadis/PlainHTTPFetcher.php";
- require_once "Auth/Yadis/ParanoidHTTPFetcher.php";
- require_once "Auth/OpenID/BigMath.php";
- require_once "Auth/OpenID/URINorm.php";
- define('Auth_OpenID_LOCAL_ERROR', 'local_error');
- define('Auth_OpenID_REMOTE_ERROR', 'remote_error');
- define('Auth_OpenID_REMOTE_OK', 'remote_ok');
- define('Auth_OpenID_REDIRECT', 'redirect');
- define('Auth_OpenID_DO_AUTH', 'do_auth');
- define('Auth_OpenID_DO_ABOUT', 'do_about');
- define('Auth_OpenID_letters',
- "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
- define('Auth_OpenID_digits',
- "0123456789");
- define('Auth_OpenID_punct',
- "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~");
- Auth_OpenID_include_init();
- class Auth_OpenID {
-
- static function isFailure($thing)
- {
- return is_a($thing, 'Auth_OpenID_FailureResponse');
- }
-
- static function getQuery($query_str=null)
- {
- $data = array();
- if ($query_str !== null) {
- $data = Auth_OpenID::params_from_string($query_str);
- } else if (!array_key_exists('REQUEST_METHOD', $_SERVER)) {
-
- } else {
-
-
-
-
-
-
-
-
-
- $data = Auth_OpenID::params_from_string($_SERVER['QUERY_STRING']);
- if ($_SERVER['REQUEST_METHOD'] == 'POST') {
- $str = file_get_contents('php://input');
- if ($str === false) {
- $post = array();
- } else {
- $post = Auth_OpenID::params_from_string($str);
- }
- $data = array_merge($data, $post);
- }
- }
- return $data;
- }
- static function params_from_string($str)
- {
- $chunks = explode("&", $str);
- $data = array();
- foreach ($chunks as $chunk) {
- $parts = explode("=", $chunk, 2);
- if (count($parts) != 2) {
- continue;
- }
- list($k, $v) = $parts;
- $data[urldecode($k)] = urldecode($v);
- }
- return $data;
- }
-
- static function ensureDir($dir_name)
- {
- if (is_dir($dir_name) || @mkdir($dir_name)) {
- return true;
- } else {
- $parent_dir = dirname($dir_name);
-
- if ($parent_dir == $dir_name) {
- return true;
- }
- return (Auth_OpenID::ensureDir($parent_dir) && @mkdir($dir_name));
- }
- }
-
- static function addPrefix($values, $prefix)
- {
- $new_values = array();
- foreach ($values as $s) {
- $new_values[] = $prefix . $s;
- }
- return $new_values;
- }
-
- static function arrayGet($arr, $key, $fallback = null)
- {
- if (is_array($arr)) {
- if (array_key_exists($key, $arr)) {
- return $arr[$key];
- } else {
- return $fallback;
- }
- } else {
- trigger_error("Auth_OpenID::arrayGet (key = ".$key.") expected " .
- "array as first parameter, got " .
- gettype($arr), E_USER_WARNING);
- return false;
- }
- }
-
- static function parse_str($query)
- {
- if ($query === null) {
- return null;
- }
- $parts = explode('&', $query);
- $new_parts = array();
- for ($i = 0; $i < count($parts); $i++) {
- $pair = explode('=', $parts[$i]);
- if (count($pair) != 2) {
- continue;
- }
- list($key, $value) = $pair;
- $new_parts[urldecode($key)] = urldecode($value);
- }
- return $new_parts;
- }
-
- static function httpBuildQuery($data)
- {
- $pairs = array();
- foreach ($data as $key => $value) {
- if (is_array($value)) {
- $pairs[] = urlencode($value[0])."=".urlencode($value[1]);
- } else {
- $pairs[] = urlencode($key)."=".urlencode($value);
- }
- }
- return implode("&", $pairs);
- }
-
- static function appendArgs($url, $args)
- {
- if (count($args) == 0) {
- return $url;
- }
-
-
- if (array_key_exists(0, $args) &&
- is_array($args[0])) {
-
- } else {
- $keys = array_keys($args);
- sort($keys);
- $new_args = array();
- foreach ($keys as $key) {
- $new_args[] = array($key, $args[$key]);
- }
- $args = $new_args;
- }
- $sep = '?';
- if (strpos($url, '?') !== false) {
- $sep = '&';
- }
- return $url . $sep . Auth_OpenID::httpBuildQuery($args);
- }
-
- static function urlunparse($scheme, $host, $port = null, $path = '/',
- $query = '', $fragment = '')
- {
- if (!$scheme) {
- $scheme = 'http';
- }
- if (!$host) {
- return false;
- }
- if (!$path) {
- $path = '';
- }
- $result = $scheme . "://" . $host;
- if ($port) {
- $result .= ":" . $port;
- }
- $result .= $path;
- if ($query) {
- $result .= "?" . $query;
- }
- if ($fragment) {
- $result .= "#" . $fragment;
- }
- return $result;
- }
-
- static function normalizeUrl($url)
- {
- @$parsed = parse_url($url);
- if (!$parsed) {
- return null;
- }
- if (isset($parsed['scheme']) &&
- isset($parsed['host'])) {
- $scheme = strtolower($parsed['scheme']);
- if (!in_array($scheme, array('http', 'https'))) {
- return null;
- }
- } else {
- $url = 'http://' . $url;
- }
- $normalized = Auth_OpenID_urinorm($url);
- if ($normalized === null) {
- return null;
- }
- list($defragged, $frag) = Auth_OpenID::urldefrag($normalized);
- return $defragged;
- }
-
- static function intval($value)
- {
- $re = "/^\\d+$/";
- if (!preg_match($re, $value)) {
- return false;
- }
- return intval($value);
- }
-
- static function bytes($str)
- {
- return strlen(bin2hex($str)) / 2;
- }
-
- static function toBytes($str)
- {
- $hex = bin2hex($str);
- if (!$hex) {
- return array();
- }
- $b = array();
- for ($i = 0; $i < strlen($hex); $i += 2) {
- $b[] = chr(base_convert(substr($hex, $i, 2), 16, 10));
- }
- return $b;
- }
- static function urldefrag($url)
- {
- $parts = explode("#", $url, 2);
- if (count($parts) == 1) {
- return array($parts[0], "");
- } else {
- return $parts;
- }
- }
- static function filter($callback, &$sequence)
- {
- $result = array();
- foreach ($sequence as $item) {
- if (call_user_func_array($callback, array($item))) {
- $result[] = $item;
- }
- }
- return $result;
- }
- static function update(&$dest, &$src)
- {
- foreach ($src as $k => $v) {
- $dest[$k] = $v;
- }
- }
-
- static function log($format_string)
- {
- $args = func_get_args();
- $message = call_user_func_array('sprintf', $args);
- error_log($message);
- }
- static function autoSubmitHTML($form, $title="OpenId transaction in progress")
- {
- return("<html>".
- "<head><title>".
- $title .
- "</title></head>".
- "<body onload='document.forms[0].submit();'>".
- $form .
- "<script>".
- "var elements = document.forms[0].elements;".
- "for (var i = 0; i < elements.length; i++) {".
- " elements[i].style.display = \"none\";".
- "}".
- "</script>".
- "</body>".
- "</html>");
- }
- }
- function Auth_OpenID_include_init() {
- if (Auth_OpenID_getMathLib() === null) {
- Auth_OpenID_setNoMathSupport();
- }
- }
|