home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / http.auth.lib.php < prev    next >
Encoding:
PHP Script  |  2003-11-26  |  8.4 KB  |  237 lines

  1. <?php
  2. /* $Id: http.auth.lib.php,v 2.3 2003/11/26 22:52:24 rabus Exp $ */
  3. // vim: expandtab sw=4 ts=4 sts=4:
  4.  
  5. // +--------------------------------------------------------------------------+
  6. // | Set of functions used to run http authentication.                        |
  7. // | NOTE: Requires PHP loaded as a Apache module.                            |
  8. // +--------------------------------------------------------------------------+
  9.  
  10.  
  11. /**
  12.  * Displays authentication form
  13.  *
  14.  * @global  string    the font face to use in case of failure
  15.  * @global  string    the default font size to use in case of failure
  16.  * @global  string    the big font size to use in case of failure
  17.  *
  18.  * @return  boolean   always true (no return indeed)
  19.  *
  20.  * @access  public
  21.  */
  22. function PMA_auth()
  23. {
  24.     global $right_font_family, $font_size, $font_bigger;
  25.  
  26.     header('WWW-Authenticate: Basic realm="phpMyAdmin ' . sprintf($GLOBALS['strRunning'], (empty($GLOBALS['cfg']['Server']['verbose']) ? str_replace('\'', '\\\'',$GLOBALS['cfg']['Server']['host']) : str_replace('\'', '\\\'', $GLOBALS['cfg']['Server']['verbose']))) .  '"');
  27.     header('HTTP/1.0 401 Unauthorized');
  28.     header('status: 401 Unauthorized');
  29.  
  30.     // Defines the charset to be used
  31.     header('Content-Type: text/html; charset=' . $GLOBALS['charset']);
  32.     ?>
  33. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  34.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  35. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php echo $GLOBALS['available_languages'][$GLOBALS['lang']][2]; ?>" lang="<?php echo $GLOBALS['available_languages'][$GLOBALS['lang']][2]; ?>" dir="<?php echo $GLOBALS['text_dir']; ?>">
  36.  
  37. <head>
  38. <title><?php echo $GLOBALS['strAccessDenied']; ?></title>
  39. <meta http-equiv="Content-Type" content="text/html; charset=<?php echo $GLOBALS['charset']; ?>" />
  40. <style type="text/css">
  41. <!--
  42. body     {font-family: <?php echo $right_font_family; ?>; font-size: <?php echo $font_size; ?>; color: #000000}
  43. h1       {font-family: <?php echo $right_font_family; ?>; font-size: <?php echo $font_bigger; ?>; font-weight: bold}
  44. //-->
  45. </style>
  46. </head>
  47.  
  48. <body bgcolor="<?php echo $GLOBALS['cfg']['RightBgColor']; ?>">
  49. <br /><br />
  50. <center>
  51.     <h1><?php echo sprintf($GLOBALS['strWelcome'], ' phpMyAdmin ' . PMA_VERSION); ?></h1>
  52. </center>
  53. <br />
  54. <p><?php echo $GLOBALS['strWrongUser']; ?></p>
  55. </body>
  56.  
  57. </html>
  58.     <?php
  59.     echo "\n";
  60.     exit();
  61.  
  62.     return TRUE;
  63. } // end of the 'PMA_auth()' function
  64.  
  65.  
  66. /**
  67.  * Gets advanced authentication settings
  68.  *
  69.  * @global  string    the username if register_globals is on
  70.  * @global  string    the password if register_globals is on
  71.  * @global  array     the array of server variables if register_globals is
  72.  *                    off
  73.  * @global  array     the array of environment variables if register_globals
  74.  *                    is off
  75.  * @global  string    the username for the ? server
  76.  * @global  string    the password for the ? server
  77.  * @global  string    the username for the WebSite Professional server
  78.  * @global  string    the password for the WebSite Professional server
  79.  * @global  string    the username of the user who logs out
  80.  *
  81.  * @return  boolean   whether we get authentication settings or not
  82.  *
  83.  * @access  public
  84.  */
  85. function PMA_auth_check()
  86. {
  87.     global $PHP_AUTH_USER, $PHP_AUTH_PW;
  88.     global $REMOTE_USER, $AUTH_USER, $REMOTE_PASSWORD, $AUTH_PASSWORD;
  89.     global $HTTP_AUTHORIZATION;
  90.     global $old_usr;
  91.  
  92.     // Grabs the $PHP_AUTH_USER variable whatever are the values of the
  93.     // 'register_globals' and the 'variables_order' directives
  94.     // loic1 - 2001/25/11: use the new globals arrays defined with php 4.1+
  95.     if (empty($PHP_AUTH_USER)) {
  96.         if (!empty($_SERVER) && isset($_SERVER['PHP_AUTH_USER'])) {
  97.             $PHP_AUTH_USER = $_SERVER['PHP_AUTH_USER'];
  98.         }
  99.         else if (isset($REMOTE_USER)) {
  100.             $PHP_AUTH_USER = $REMOTE_USER;
  101.         }
  102.         else if (!empty($_ENV) && isset($_ENV['REMOTE_USER'])) {
  103.             $PHP_AUTH_USER = $_ENV['REMOTE_USER'];
  104.         }
  105.         else if (@getenv('REMOTE_USER')) {
  106.             $PHP_AUTH_USER = getenv('REMOTE_USER');
  107.         }
  108.         // Fix from Matthias Fichtner for WebSite Professional - Part 1
  109.         else if (isset($AUTH_USER)) {
  110.             $PHP_AUTH_USER = $AUTH_USER;
  111.         }
  112.         else if (!empty($_ENV) && isset($_ENV['AUTH_USER'])) {
  113.             $PHP_AUTH_USER = $_ENV['AUTH_USER'];
  114.         }
  115.         else if (@getenv('AUTH_USER')) {
  116.             $PHP_AUTH_USER = getenv('AUTH_USER');
  117.         }
  118.     }
  119.     // Grabs the $PHP_AUTH_PW variable whatever are the values of the
  120.     // 'register_globals' and the 'variables_order' directives
  121.     // loic1 - 2001/25/11: use the new globals arrays defined with php 4.1+
  122.     if (empty($PHP_AUTH_PW)) {
  123.         if (!empty($_SERVER) && isset($_SERVER['PHP_AUTH_PW'])) {
  124.             $PHP_AUTH_PW = $_SERVER['PHP_AUTH_PW'];
  125.         }
  126.         else if (isset($REMOTE_PASSWORD)) {
  127.             $PHP_AUTH_PW = $REMOTE_PASSWORD;
  128.         }
  129.         else if (!empty($_ENV) && isset($_ENV['REMOTE_PASSWORD'])) {
  130.             $PHP_AUTH_PW = $_ENV['REMOTE_PASSWORD'];
  131.         }
  132.         else if (@getenv('REMOTE_PASSWORD')) {
  133.             $PHP_AUTH_PW = getenv('REMOTE_PASSWORD');
  134.         }
  135.         // Fix from Matthias Fichtner for WebSite Professional - Part 2
  136.         else if (isset($AUTH_PASSWORD)) {
  137.             $PHP_AUTH_PW = $AUTH_PASSWORD;
  138.         }
  139.         else if (!empty($_ENV) && isset($_ENV['AUTH_PASSWORD'])) {
  140.             $PHP_AUTH_PW = $_ENV['AUTH_PASSWORD'];
  141.         }
  142.         else if (@getenv('AUTH_PASSWORD')) {
  143.             $PHP_AUTH_PW = getenv('AUTH_PASSWORD');
  144.         }
  145.     }
  146.     // Gets authenticated user settings with IIS
  147.     if (empty($PHP_AUTH_USER) && empty($PHP_AUTH_PW)
  148.         && function_exists('base64_decode')) {
  149.         if (!empty($HTTP_AUTHORIZATION)
  150.             && substr($HTTP_AUTHORIZATION, 0, 6) == 'Basic ') {
  151.             list($PHP_AUTH_USER, $PHP_AUTH_PW) = explode(':', base64_decode(substr($HTTP_AUTHORIZATION, 6)));
  152.         }
  153.         else if (!empty($_ENV)
  154.              && isset($_ENV['HTTP_AUTHORIZATION'])
  155.              && substr($_ENV['HTTP_AUTHORIZATION'], 0, 6) == 'Basic ') {
  156.             list($PHP_AUTH_USER, $PHP_AUTH_PW) = explode(':', base64_decode(substr($_ENV['HTTP_AUTHORIZATION'], 6)));
  157.         }
  158.         else if (@getenv('HTTP_AUTHORIZATION')
  159.                  && substr(getenv('HTTP_AUTHORIZATION'), 0, 6) == 'Basic ') {
  160.             list($PHP_AUTH_USER, $PHP_AUTH_PW) = explode(':', base64_decode(substr(getenv('HTTP_AUTHORIZATION'), 6)));
  161.         }
  162.     } // end IIS
  163.  
  164.     // User logged out -> ensure the new username is not the same
  165.     if (!empty($old_usr)
  166.         && (isset($PHP_AUTH_USER) && $old_usr == $PHP_AUTH_USER)) {
  167.         $PHP_AUTH_USER = '';
  168.     }
  169.  
  170.     // Returns whether we get authentication settings or not
  171.     if (empty($PHP_AUTH_USER)) {
  172.         return FALSE;
  173.     } else {
  174.         if (get_magic_quotes_gpc()) {
  175.             $PHP_AUTH_USER = stripslashes($PHP_AUTH_USER);
  176.             $PHP_AUTH_PW   = stripslashes($PHP_AUTH_PW);
  177.         }
  178.         return TRUE;
  179.     }
  180. } // end of the 'PMA_auth_check()' function
  181.  
  182.  
  183. /**
  184.  * Set the user and password after last checkings if required
  185.  *
  186.  * @global  array     the valid servers settings
  187.  * @global  integer   the id of the current server
  188.  * @global  array     the current server settings
  189.  * @global  string    the current username
  190.  * @global  string    the current password
  191.  *
  192.  * @return  boolean   always true
  193.  *
  194.  * @access  public
  195.  */
  196. function PMA_auth_set_user()
  197. {
  198.     global $cfg, $server;
  199.     global $PHP_AUTH_USER, $PHP_AUTH_PW;
  200.  
  201.     // Ensures valid authentication mode, 'only_db', bookmark database and
  202.     // table names and relation table name are used
  203.     if ($cfg['Server']['user'] != $PHP_AUTH_USER) {
  204.         $servers_cnt = count($cfg['Servers']);
  205.         for ($i = 1; $i <= $servers_cnt; $i++) {
  206.             if (isset($cfg['Servers'][$i])
  207.                 && ($cfg['Servers'][$i]['host'] == $cfg['Server']['host'] && $cfg['Servers'][$i]['user'] == $PHP_AUTH_USER)) {
  208.                 $server        = $i;
  209.                 $cfg['Server'] = $cfg['Servers'][$i];
  210.                 break;
  211.             }
  212.         } // end for
  213.     } // end if
  214.  
  215.     $cfg['Server']['user']     = $PHP_AUTH_USER;
  216.     $cfg['Server']['password'] = $PHP_AUTH_PW;
  217.  
  218.     return TRUE;
  219. } // end of the 'PMA_auth_set_user()' function
  220.  
  221.  
  222. /**
  223.  * User is not allowed to login to MySQL -> authentication failed
  224.  *
  225.  * @return  boolean   always true (no return indeed)
  226.  *
  227.  * @access  public
  228.  */
  229. function PMA_auth_fails()
  230. {
  231.     PMA_auth();
  232.  
  233.     return TRUE;
  234. } // end of the 'PMA_auth_fails()' function
  235.  
  236. ?>
  237.