OpenID.php

Go to the documentation of this file.
00001 <?php
00002 
00023 define('Auth_OpenID_VERSION', '2.1.2');
00024 
00028 require_once "Auth/Yadis/PlainHTTPFetcher.php";
00029 require_once "Auth/Yadis/ParanoidHTTPFetcher.php";
00030 require_once "Auth/OpenID/BigMath.php";
00031 require_once "Auth/OpenID/URINorm.php";
00032 
00041 define('Auth_OpenID_LOCAL_ERROR', 'local_error');
00042 
00050 define('Auth_OpenID_REMOTE_ERROR', 'remote_error');
00051 
00060 define('Auth_OpenID_REMOTE_OK', 'remote_ok');
00061 
00070 define('Auth_OpenID_REDIRECT', 'redirect');
00071 
00081 define('Auth_OpenID_DO_AUTH', 'do_auth');
00082 
00091 define('Auth_OpenID_DO_ABOUT', 'do_about');
00092 
00096 define('Auth_OpenID_letters',
00097        "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
00098 
00099 define('Auth_OpenID_digits',
00100        "0123456789");
00101 
00102 define('Auth_OpenID_punct',
00103        "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~");
00104 
00105 if (Auth_OpenID_getMathLib() === null) {
00106     Auth_OpenID_setNoMathSupport();
00107 }
00108 
00115 class Auth_OpenID {
00116 
00123     function isFailure($thing)
00124     {
00125         return is_a($thing, 'Auth_OpenID_FailureResponse');
00126     }
00127 
00144     function getQuery($query_str=null)
00145     {
00146         $data = array();
00147 
00148         if ($query_str !== null) {
00149             $data = Auth_OpenID::params_from_string($query_str);
00150         } else if (!array_key_exists('REQUEST_METHOD', $_SERVER)) {
00151             // Do nothing.
00152         } else {
00153           // XXX HACK FIXME HORRIBLE.
00154           //
00155           // POSTing to a URL with query parameters is acceptable, but
00156           // we don't have a clean way to distinguish those parameters
00157           // when we need to do things like return_to verification
00158           // which only want to look at one kind of parameter.  We're
00159           // going to emulate the behavior of some other environments
00160           // by defaulting to GET and overwriting with POST if POST
00161           // data is available.
00162           $data = Auth_OpenID::params_from_string($_SERVER['QUERY_STRING']);
00163 
00164           if ($_SERVER['REQUEST_METHOD'] == 'POST') {
00165             $str = file_get_contents('php://input');
00166 
00167             if ($str === false) {
00168               $post = array();
00169             } else {
00170               $post = Auth_OpenID::params_from_string($str);
00171             }
00172 
00173             $data = array_merge($data, $post);
00174           }
00175         }
00176 
00177         return $data;
00178     }
00179 
00180     function params_from_string($str)
00181     {
00182         $chunks = explode("&", $str);
00183 
00184         $data = array();
00185         foreach ($chunks as $chunk) {
00186             $parts = explode("=", $chunk, 2);
00187 
00188             if (count($parts) != 2) {
00189                 continue;
00190             }
00191 
00192             list($k, $v) = $parts;
00193             $data[$k] = urldecode($v);
00194         }
00195 
00196         return $data;
00197     }
00198 
00206     function ensureDir($dir_name)
00207     {
00208         if (is_dir($dir_name) || @mkdir($dir_name)) {
00209             return true;
00210         } else {
00211             $parent_dir = dirname($dir_name);
00212 
00213             // Terminal case; there is no parent directory to create.
00214             if ($parent_dir == $dir_name) {
00215                 return true;
00216             }
00217 
00218             return (Auth_OpenID::ensureDir($parent_dir) && @mkdir($dir_name));
00219         }
00220     }
00221 
00228     function addPrefix($values, $prefix)
00229     {
00230         $new_values = array();
00231         foreach ($values as $s) {
00232             $new_values[] = $prefix . $s;
00233         }
00234         return $new_values;
00235     }
00236 
00244     function arrayGet($arr, $key, $fallback = null)
00245     {
00246         if (is_array($arr)) {
00247             if (array_key_exists($key, $arr)) {
00248                 return $arr[$key];
00249             } else {
00250                 return $fallback;
00251             }
00252         } else {
00253             trigger_error("Auth_OpenID::arrayGet (key = ".$key.") expected " .
00254                           "array as first parameter, got " .
00255                           gettype($arr), E_USER_WARNING);
00256 
00257             return false;
00258         }
00259     }
00260 
00264     function parse_str($query)
00265     {
00266         if ($query === null) {
00267             return null;
00268         }
00269 
00270         $parts = explode('&', $query);
00271 
00272         $new_parts = array();
00273         for ($i = 0; $i < count($parts); $i++) {
00274             $pair = explode('=', $parts[$i]);
00275 
00276             if (count($pair) != 2) {
00277                 continue;
00278             }
00279 
00280             list($key, $value) = $pair;
00281             $new_parts[$key] = urldecode($value);
00282         }
00283 
00284         return $new_parts;
00285     }
00286 
00298     function httpBuildQuery($data)
00299     {
00300         $pairs = array();
00301         foreach ($data as $key => $value) {
00302             if (is_array($value)) {
00303                 $pairs[] = urlencode($value[0])."=".urlencode($value[1]);
00304             } else {
00305                 $pairs[] = urlencode($key)."=".urlencode($value);
00306             }
00307         }
00308         return implode("&", $pairs);
00309     }
00310 
00326     function appendArgs($url, $args)
00327     {
00328         if (count($args) == 0) {
00329             return $url;
00330         }
00331 
00332         // Non-empty array; if it is an array of arrays, use
00333         // multisort; otherwise use sort.
00334         if (array_key_exists(0, $args) &&
00335             is_array($args[0])) {
00336             // Do nothing here.
00337         } else {
00338             $keys = array_keys($args);
00339             sort($keys);
00340             $new_args = array();
00341             foreach ($keys as $key) {
00342                 $new_args[] = array($key, $args[$key]);
00343             }
00344             $args = $new_args;
00345         }
00346 
00347         $sep = '?';
00348         if (strpos($url, '?') !== false) {
00349             $sep = '&';
00350         }
00351 
00352         return $url . $sep . Auth_OpenID::httpBuildQuery($args);
00353     }
00354 
00370     function urlunparse($scheme, $host, $port = null, $path = '/',
00371                         $query = '', $fragment = '')
00372     {
00373 
00374         if (!$scheme) {
00375             $scheme = 'http';
00376         }
00377 
00378         if (!$host) {
00379             return false;
00380         }
00381 
00382         if (!$path) {
00383             $path = '';
00384         }
00385 
00386         $result = $scheme . "://" . $host;
00387 
00388         if ($port) {
00389             $result .= ":" . $port;
00390         }
00391 
00392         $result .= $path;
00393 
00394         if ($query) {
00395             $result .= "?" . $query;
00396         }
00397 
00398         if ($fragment) {
00399             $result .= "#" . $fragment;
00400         }
00401 
00402         return $result;
00403     }
00404 
00415     function normalizeUrl($url)
00416     {
00417         @$parsed = parse_url($url);
00418 
00419         if (!$parsed) {
00420             return null;
00421         }
00422 
00423         if (isset($parsed['scheme']) &&
00424             isset($parsed['host'])) {
00425             $scheme = strtolower($parsed['scheme']);
00426             if (!in_array($scheme, array('http', 'https'))) {
00427                 return null;
00428             }
00429         } else {
00430             $url = 'http://' . $url;
00431         }
00432 
00433         $normalized = Auth_OpenID_urinorm($url);
00434         if ($normalized === null) {
00435             return null;
00436         }
00437         list($defragged, $frag) = Auth_OpenID::urldefrag($normalized);
00438         return $defragged;
00439     }
00440 
00446     function intval($value)
00447     {
00448         $re = "/^\\d+$/";
00449 
00450         if (!preg_match($re, $value)) {
00451             return false;
00452         }
00453 
00454         return intval($value);
00455     }
00456 
00464     function bytes($str)
00465     {
00466         return strlen(bin2hex($str)) / 2;
00467     }
00468 
00473     function toBytes($str)
00474     {
00475         $hex = bin2hex($str);
00476 
00477         if (!$hex) {
00478             return array();
00479         }
00480 
00481         $b = array();
00482         for ($i = 0; $i < strlen($hex); $i += 2) {
00483             $b[] = chr(base_convert(substr($hex, $i, 2), 16, 10));
00484         }
00485 
00486         return $b;
00487     }
00488 
00489     function urldefrag($url)
00490     {
00491         $parts = explode("#", $url, 2);
00492 
00493         if (count($parts) == 1) {
00494             return array($parts[0], "");
00495         } else {
00496             return $parts;
00497         }
00498     }
00499 
00500     function filter($callback, &$sequence)
00501     {
00502         $result = array();
00503 
00504         foreach ($sequence as $item) {
00505             if (call_user_func_array($callback, array($item))) {
00506                 $result[] = $item;
00507             }
00508         }
00509 
00510         return $result;
00511     }
00512 
00513     function update(&$dest, &$src)
00514     {
00515         foreach ($src as $k => $v) {
00516             $dest[$k] = $v;
00517         }
00518     }
00519 
00527     function log($format_string)
00528     {
00529         $args = func_get_args();
00530         $message = call_user_func_array('sprintf', $args);
00531         error_log($message);
00532     }
00533 
00534     function autoSubmitHTML($form, $title="OpenId transaction in progress")
00535     {
00536         return("<html>".
00537                "<head><title>".
00538                $title .
00539                "</title></head>".
00540                "<body onload='document.forms[0].submit();'>".
00541                $form .
00542                "<script>".
00543                "var elements = document.forms[0].elements;".
00544                "for (var i = 0; i < elements.length; i++) {".
00545                "  elements[i].style.display = \"none\";".
00546                "}".
00547                "</script>".
00548                "</body>".
00549                "</html>");
00550     }
00551 }
00552 ?>

Generated on Thu Feb 19 15:02:21 2009 for OXID eShop CE by  doxygen 1.5.5