Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: OpenID

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /openid/Auth/OpenID.php

Documentation is available at OpenID.php

  1. <?php
  2.  
  3. /**
  4.  * This is the PHP OpenID library by JanRain, Inc.
  5.  *
  6.  * This module contains core utility functionality used by the
  7.  * library.  See Consumer.php and Server.php for the consumer and
  8.  * server implementations.
  9.  *
  10.  * PHP versions 4 and 5
  11.  *
  12.  * LICENSE: See the COPYING file included in this distribution.
  13.  *
  14.  * @package OpenID
  15.  * @author JanRain, Inc. <[email protected]>
  16.  * @copyright 2005 Janrain, Inc.
  17.  * @license http://www.gnu.org/copyleft/lesser.html LGPL
  18.  */
  19.  
  20. /**
  21.  * Require the fetcher code.
  22.  */
  23. require_once "Services/Yadis/PlainHTTPFetcher.php";
  24. require_once "Services/Yadis/ParanoidHTTPFetcher.php";
  25.  
  26. /**
  27.  * Status code returned by the server when the only option is to show
  28.  * an error page, since we do not have enough information to redirect
  29.  * back to the consumer. The associated value is an error message that
  30.  * should be displayed on an HTML error page.
  31.  *
  32.  * @see Auth_OpenID_Server
  33.  */
  34. define('Auth_OpenID_LOCAL_ERROR', 'local_error');
  35.  
  36. /**
  37.  * Status code returned when there is an error to return in key-value
  38.  * form to the consumer. The caller should return a 400 Bad Request
  39.  * response with content-type text/plain and the value as the body.
  40.  *
  41.  * @see Auth_OpenID_Server
  42.  */
  43. define('Auth_OpenID_REMOTE_ERROR', 'remote_error');
  44.  
  45. /**
  46.  * Status code returned when there is a key-value form OK response to
  47.  * the consumer. The value associated with this code is the
  48.  * response. The caller should return a 200 OK response with
  49.  * content-type text/plain and the value as the body.
  50.  *
  51.  * @see Auth_OpenID_Server
  52.  */
  53. define('Auth_OpenID_REMOTE_OK', 'remote_ok');
  54.  
  55. /**
  56.  * Status code returned when there is a redirect back to the
  57.  * consumer. The value is the URL to redirect back to. The caller
  58.  * should return a 302 Found redirect with a Location: header
  59.  * containing the URL.
  60.  *
  61.  * @see Auth_OpenID_Server
  62.  */
  63. define('Auth_OpenID_REDIRECT', 'redirect');
  64.  
  65. /**
  66.  * Status code returned when the caller needs to authenticate the
  67.  * user. The associated value is a {@link Auth_OpenID_ServerRequest}
  68.  * object that can be used to complete the authentication. If the user
  69.  * has taken some authentication action, use the retry() method of the
  70.  * {@link Auth_OpenID_ServerRequest} object to complete the request.
  71.  *
  72.  * @see Auth_OpenID_Server
  73.  */
  74. define('Auth_OpenID_DO_AUTH', 'do_auth');
  75.  
  76. /**
  77.  * Status code returned when there were no OpenID arguments
  78.  * passed. This code indicates that the caller should return a 200 OK
  79.  * response and display an HTML page that says that this is an OpenID
  80.  * server endpoint.
  81.  *
  82.  * @see Auth_OpenID_Server
  83.  */
  84. define('Auth_OpenID_DO_ABOUT', 'do_about');
  85.  
  86. /**
  87.  * Defines for regexes and format checking.
  88.  */
  89. define('Auth_OpenID_letters',
  90.        "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
  91.  
  92. define('Auth_OpenID_digits',
  93.        "0123456789");
  94.  
  95. define('Auth_OpenID_punct',
  96.        "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~");
  97.  
  98. /**
  99.  * These namespaces are automatically fixed in query arguments by
  100.  * Auth_OpenID::fixArgs.
  101.  */
  102. global $_Auth_OpenID_namespaces;
  103. $_Auth_OpenID_namespaces = array('openid',
  104.                                  'sreg');
  105.  
  106. /**
  107.  * The OpenID utility function class.
  108.  *
  109.  * @package OpenID
  110.  * @access private
  111.  */
  112. class Auth_OpenID {
  113.  
  114.     /**
  115.      * Rename query arguments back to 'openid.' from 'openid_'
  116.      *
  117.      * @access private
  118.      * @param array $args An associative array of URL query arguments
  119.      */
  120.     function fixArgs($args)
  121.     {
  122.         global $_Auth_OpenID_namespaces;
  123.         foreach (array_keys($args) as $key) {
  124.             $fixed = $key;
  125.             if (preg_match('/^openid/', $key)) {
  126.                 foreach ($_Auth_OpenID_namespaces as $ns) {
  127.                     if (preg_match('/'.$ns.'_/', $key)) {
  128.                         $fixed = preg_replace('/'.$ns.'_/', $ns.'.', $fixed);
  129.                     }
  130.                 }
  131.  
  132.                 if ($fixed != $key) {
  133.                     $val = $args[$key];
  134.                     unset($args[$key]);
  135.                     $args[$fixed] = $val;
  136.                 }
  137.             }
  138.         }
  139.  
  140.         return $args;
  141.     }
  142.  
  143.     /**
  144.      * Create dir_name as a directory if it does not exist. If it
  145.      * exists, make sure that it is, in fact, a directory.  Returns
  146.      * true if the operation succeeded; false if not.
  147.      *
  148.      * @access private
  149.      */
  150.     function ensureDir($dir_name)
  151.     {
  152.         if (is_dir($dir_name) || @mkdir($dir_name)) {
  153.             return true;
  154.         } else {
  155.             if (Auth_OpenID::ensureDir(dirname($dir_name))) {
  156.                 return is_dir($dir_name) || @mkdir($dir_name);
  157.             } else {
  158.                 return false;
  159.             }
  160.         }
  161.     }
  162.  
  163.     /**
  164.      * Convenience function for getting array values.
  165.      *
  166.      * @access private
  167.      */
  168.     function arrayGet($arr, $key, $fallback = null)
  169.     {
  170.         if (is_array($arr)) {
  171.             if (array_key_exists($key, $arr)) {
  172.                 return $arr[$key];
  173.             } else {
  174.                 return $fallback;
  175.             }
  176.         } else {
  177.             trigger_error("Auth_OpenID::arrayGet expected " .
  178.                           "array as first parameter", E_USER_WARNING);
  179.             return false;
  180.         }
  181.     }
  182.  
  183.     /**
  184.      * Implements the PHP 5 'http_build_query' functionality.
  185.      *
  186.      * @access private
  187.      * @param array $data Either an array key/value pairs or an array
  188.      *  of arrays, each of which holding two values: a key and a value,
  189.      *  sequentially.
  190.      * @return string $result The result of url-encoding the key/value
  191.      *  pairs from $data into a URL query string
  192.      *  (e.g. "username=bob&id=56").
  193.      */
  194.     function httpBuildQuery($data)
  195.     {
  196.         $pairs = array();
  197.         foreach ($data as $key => $value) {
  198.             if (is_array($value)) {
  199.                 $pairs[] = urlencode($value[0])."=".urlencode($value[1]);
  200.             } else {
  201.                 $pairs[] = urlencode($key)."=".urlencode($value);
  202.             }
  203.         }
  204.         return implode("&", $pairs);
  205.     }
  206.  
  207.     /**
  208.      * "Appends" query arguments onto a URL.  The URL may or may not
  209.      * already have arguments (following a question mark).
  210.      *
  211.      * @param string $url A URL, which may or may not already have
  212.      *  arguments.
  213.      * @param array $args Either an array key/value pairs or an array of
  214.      *  arrays, each of which holding two values: a key and a value,
  215.      *  sequentially.  If $args is an ordinary key/value array, the
  216.      *  parameters will be added to the URL in sorted alphabetical order;
  217.      *  if $args is an array of arrays, their order will be preserved.
  218.      * @return string $url The original URL with the new parameters added.
  219.      *
  220.      */
  221.     function appendArgs($url, $args)
  222.     {
  223.         if (count($args) == 0) {
  224.             return $url;
  225.         }
  226.  
  227.         // Non-empty array; if it is an array of arrays, use
  228.         // multisort; otherwise use sort.
  229.         if (array_key_exists(0, $args) &&
  230.             is_array($args[0])) {
  231.             // Do nothing here.
  232.         } else {
  233.             $keys = array_keys($args);
  234.             sort($keys);
  235.             $new_args = array();
  236.             foreach ($keys as $key) {
  237.                 $new_args[] = array($key, $args[$key]);
  238.             }
  239.             $args = $new_args;
  240.         }
  241.  
  242.         $sep = '?';
  243.         if (strpos($url, '?') !== false) {
  244.             $sep = '&';
  245.         }
  246.  
  247.         return $url . $sep . Auth_OpenID::httpBuildQuery($args);
  248.     }
  249.  
  250.     /**
  251.      * Turn a string into an ASCII string.
  252.      *
  253.      * Replace non-ascii characters with a %-encoded, UTF-8
  254.      * encoding. This function will fail if the input is a string and
  255.      * there are non-7-bit-safe characters. It is assumed that the
  256.      * caller will have already translated the input into a Unicode
  257.      * character sequence, according to the encoding of the HTTP POST
  258.      * or GET.
  259.      *
  260.      * Do not escape anything that is already 7-bit safe, so we do the
  261.      * minimal transform on the identity URL
  262.      *
  263.      * @access private
  264.      */
  265.     function quoteMinimal($s)
  266.     {
  267.         $res = array();
  268.         for ($i = 0; $i < strlen($s); $i++) {
  269.             $c = $s[$i];
  270.             if ($c >= "\x80") {
  271.                 for ($j = 0; $j < count(utf8_encode($c)); $j++) {
  272.                     array_push($res, sprintf("%02X", ord($c[$j])));
  273.                 }
  274.             } else {
  275.                 array_push($res, $c);
  276.             }
  277.         }
  278.  
  279.         return implode('', $res);
  280.     }
  281.  
  282.     /**
  283.      * Implements python's urlunparse, which is not available in PHP.
  284.      * Given the specified components of a URL, this function rebuilds
  285.      * and returns the URL.
  286.      *
  287.      * @access private
  288.      * @param string $scheme The scheme (e.g. 'http').  Defaults to 'http'.
  289.      * @param string $host The host.  Required.
  290.      * @param string $port The port.
  291.      * @param string $path The path.
  292.      * @param string $query The query.
  293.      * @param string $fragment The fragment.
  294.      * @return string $url The URL resulting from assembling the
  295.      *  specified components.
  296.      */
  297.     function urlunparse($scheme, $host, $port = null, $path = '/',
  298.                                     $query = '', $fragment = '')
  299.     {
  300.  
  301.         if (!$scheme) {
  302.             $scheme = 'http';
  303.         }
  304.  
  305.         if (!$host) {
  306.             return false;
  307.         }
  308.  
  309.         if (!$path) {
  310.             $path = '/';
  311.         }
  312.  
  313.         $result = $scheme . "://" . $host;
  314.  
  315.         if ($port) {
  316.             $result .= ":" . $port;
  317.         }
  318.  
  319.         $result .= $path;
  320.  
  321.         if ($query) {
  322.             $result .= "?" . $query;
  323.         }
  324.  
  325.         if ($fragment) {
  326.             $result .= "#" . $fragment;
  327.         }
  328.  
  329.         return $result;
  330.     }
  331.  
  332.     /**
  333.      * Given a URL, this "normalizes" it by adding a trailing slash
  334.      * and / or a leading http:// scheme where necessary.  Returns
  335.      * null if the original URL is malformed and cannot be normalized.
  336.      *
  337.      * @access private
  338.      * @param string $url The URL to be normalized.
  339.      * @return mixed $new_url The URL after normalization, or null if
  340.      *  $url was malformed.
  341.      */
  342.     function normalizeUrl($url)
  343.     {
  344.         if ($url === null) {
  345.             return null;
  346.         }
  347.  
  348.         assert(is_string($url));
  349.  
  350.         $old_url = $url;
  351.         $url = trim($url);
  352.  
  353.         if (strpos($url, "://") === false) {
  354.             $url = "http://" . $url;
  355.         }
  356.  
  357.         $parsed = @parse_url($url);
  358.  
  359.         if ($parsed === false) {
  360.             return null;
  361.         }
  362.  
  363.         $defaults = array(
  364.                           'scheme' => '',
  365.                           'host' => '',
  366.                           'path' => '',
  367.                           'query' => '',
  368.                           'fragment' => '',
  369.                           'port' => ''
  370.                           );
  371.  
  372.         $parsed = array_merge($defaults, $parsed);
  373.  
  374.         if (($parsed['scheme'] == '') ||
  375.             ($parsed['host'] == '')) {
  376.             if ($parsed['path'] == '' &&
  377.                 $parsed['query'] == '' &&
  378.                 $parsed['fragment'] == '') {
  379.                 return null;
  380.             }
  381.  
  382.             $url = 'http://' + $url;
  383.             $parsed = parse_url($url);
  384.  
  385.             $parsed = array_merge($defaults, $parsed);
  386.         }
  387.  
  388.         $tail = array_map(array('Auth_OpenID', 'quoteMinimal'),
  389.                           array($parsed['path'],
  390.                                 $parsed['query'],
  391.                                 $parsed['fragment']));
  392.         if ($tail[0] == '') {
  393.             $tail[0] = '/';
  394.         }
  395.  
  396.         $url = Auth_OpenID::urlunparse($parsed['scheme'], $parsed['host'],
  397.                                        $parsed['port'], $tail[0], $tail[1],
  398.                                        $tail[2]);
  399.  
  400.         assert(is_string($url));
  401.  
  402.         return $url;
  403.     }
  404. }
  405.  
  406. ?>

Documentation generated on Mon, 05 Mar 2007 21:12:19 +0000 by phpDocumentor 1.3.1