PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/manage/phpmyadminlite/libraries/auth/http.auth.lib.php

https://gitlab.com/albert925/lading-ach
PHP | 229 lines | 126 code | 23 blank | 80 comment | 33 complexity | caa92372a0f685ae94414fa4e6eda5de MD5 | raw file
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Set of functions used to run http authentication.
  5. * NOTE: Requires PHP loaded as a Apache module.
  6. *
  7. * @package phpMyAdmin-Auth-HTTP
  8. * @version $Id$
  9. */
  10. /**
  11. * Displays authentication form
  12. *
  13. * @global string the font face to use in case of failure
  14. * @global string the default font size to use in case of failure
  15. * @global string the big font size to use in case of failure
  16. *
  17. * @return boolean always true (no return indeed)
  18. *
  19. * @access public
  20. */
  21. function PMA_auth()
  22. {
  23. /* Perform logout to custom URL */
  24. if (!empty($_REQUEST['old_usr']) && !empty($GLOBALS['cfg']['Server']['LogoutURL'])) {
  25. PMA_sendHeaderLocation($GLOBALS['cfg']['Server']['LogoutURL']);
  26. exit;
  27. }
  28. if (empty($GLOBALS['cfg']['Server']['verbose'])) {
  29. $server_message = $GLOBALS['cfg']['Server']['host'];
  30. } else {
  31. $server_message = $GLOBALS['cfg']['Server']['verbose'];
  32. }
  33. // remove non US-ASCII to respect RFC2616
  34. $server_message = preg_replace('/[^\x20-\x7e]/i', '', $server_message);
  35. header('WWW-Authenticate: Basic realm="phpMyAdmin ' . $server_message . '"');
  36. header('HTTP/1.0 401 Unauthorized');
  37. if (php_sapi_name() !== 'cgi-fcgi') {
  38. header('status: 401 Unauthorized');
  39. }
  40. // Defines the charset to be used
  41. header('Content-Type: text/html; charset=' . $GLOBALS['charset']);
  42. /* HTML header */
  43. $page_title = $GLOBALS['strAccessDenied'];
  44. require './libraries/header_meta_style.inc.php';
  45. ?>
  46. </head>
  47. <body>
  48. <?php
  49. if (file_exists('./config.header.inc.php')) {
  50. require './config.header.inc.php';
  51. }
  52. ?>
  53. <br /><br />
  54. <center>
  55. <h1><?php echo sprintf($GLOBALS['strWelcome'], ' phpMyAdmin'); ?></h1>
  56. </center>
  57. <br />
  58. <?php
  59. PMA_Message::error('strWrongUser')->display();
  60. if (file_exists('./config.footer.inc.php')) {
  61. require './config.footer.inc.php';
  62. }
  63. ?>
  64. </body>
  65. </html>
  66. <?php
  67. exit();
  68. } // end of the 'PMA_auth()' function
  69. /**
  70. * Gets advanced authentication settings
  71. *
  72. * @global string the username if register_globals is on
  73. * @global string the password if register_globals is on
  74. * @global array the array of server variables if register_globals is
  75. * off
  76. * @global array the array of environment variables if register_globals
  77. * is off
  78. * @global string the username for the ? server
  79. * @global string the password for the ? server
  80. * @global string the username for the WebSite Professional server
  81. * @global string the password for the WebSite Professional server
  82. * @global string the username of the user who logs out
  83. *
  84. * @return boolean whether we get authentication settings or not
  85. *
  86. * @access public
  87. */
  88. function PMA_auth_check()
  89. {
  90. global $PHP_AUTH_USER, $PHP_AUTH_PW;
  91. global $old_usr;
  92. // Grabs the $PHP_AUTH_USER variable whatever are the values of the
  93. // 'register_globals' and the 'variables_order' directives
  94. if (empty($PHP_AUTH_USER)) {
  95. if (PMA_getenv('PHP_AUTH_USER')) {
  96. $PHP_AUTH_USER = PMA_getenv('PHP_AUTH_USER');
  97. } elseif (PMA_getenv('REMOTE_USER')) {
  98. // CGI, might be encoded, see below
  99. $PHP_AUTH_USER = PMA_getenv('REMOTE_USER');
  100. } elseif (PMA_getenv('REDIRECT_REMOTE_USER')) {
  101. // CGI, might be encoded, see below
  102. $PHP_AUTH_USER = PMA_getenv('REDIRECT_REMOTE_USER');
  103. } elseif (PMA_getenv('AUTH_USER')) {
  104. // WebSite Professional
  105. $PHP_AUTH_USER = PMA_getenv('AUTH_USER');
  106. } elseif (PMA_getenv('HTTP_AUTHORIZATION')) {
  107. // IIS, might be encoded, see below
  108. $PHP_AUTH_USER = PMA_getenv('HTTP_AUTHORIZATION');
  109. } elseif (PMA_getenv('Authorization')) {
  110. // FastCGI, might be encoded, see below
  111. $PHP_AUTH_USER = PMA_getenv('Authorization');
  112. }
  113. }
  114. // Grabs the $PHP_AUTH_PW variable whatever are the values of the
  115. // 'register_globals' and the 'variables_order' directives
  116. if (empty($PHP_AUTH_PW)) {
  117. if (PMA_getenv('PHP_AUTH_PW')) {
  118. $PHP_AUTH_PW = PMA_getenv('PHP_AUTH_PW');
  119. } elseif (PMA_getenv('REMOTE_PASSWORD')) {
  120. // Apache/CGI
  121. $PHP_AUTH_PW = PMA_getenv('REMOTE_PASSWORD');
  122. } elseif (PMA_getenv('AUTH_PASSWORD')) {
  123. // WebSite Professional
  124. $PHP_AUTH_PW = PMA_getenv('AUTH_PASSWORD');
  125. }
  126. }
  127. // Decode possibly encoded information (used by IIS/CGI/FastCGI)
  128. // (do not use explode() because a user might have a colon in his password
  129. if (strcmp(substr($PHP_AUTH_USER, 0, 6), 'Basic ') == 0) {
  130. $usr_pass = base64_decode(substr($PHP_AUTH_USER, 6));
  131. if (! empty($usr_pass)) {
  132. $colon = strpos($usr_pass, ':');
  133. if ($colon) {
  134. $PHP_AUTH_USER = substr($usr_pass, 0, $colon);
  135. $PHP_AUTH_PW = substr($usr_pass, $colon + 1);
  136. }
  137. unset($colon);
  138. }
  139. unset($usr_pass);
  140. }
  141. // User logged out -> ensure the new username is not the same
  142. if (!empty($old_usr)
  143. && (isset($PHP_AUTH_USER) && $old_usr == $PHP_AUTH_USER)) {
  144. $PHP_AUTH_USER = '';
  145. // -> delete user's choices that were stored in session
  146. session_destroy();
  147. }
  148. // Returns whether we get authentication settings or not
  149. if (empty($PHP_AUTH_USER)) {
  150. return false;
  151. } else {
  152. return true;
  153. }
  154. } // end of the 'PMA_auth_check()' function
  155. /**
  156. * Set the user and password after last checkings if required
  157. *
  158. * @global array the valid servers settings
  159. * @global integer the id of the current server
  160. * @global array the current server settings
  161. * @global string the current username
  162. * @global string the current password
  163. *
  164. * @return boolean always true
  165. *
  166. * @access public
  167. */
  168. function PMA_auth_set_user()
  169. {
  170. global $cfg, $server;
  171. global $PHP_AUTH_USER, $PHP_AUTH_PW;
  172. // Ensures valid authentication mode, 'only_db', bookmark database and
  173. // table names and relation table name are used
  174. if ($cfg['Server']['user'] != $PHP_AUTH_USER) {
  175. $servers_cnt = count($cfg['Servers']);
  176. for ($i = 1; $i <= $servers_cnt; $i++) {
  177. if (isset($cfg['Servers'][$i])
  178. && ($cfg['Servers'][$i]['host'] == $cfg['Server']['host'] && $cfg['Servers'][$i]['user'] == $PHP_AUTH_USER)) {
  179. $server = $i;
  180. $cfg['Server'] = $cfg['Servers'][$i];
  181. break;
  182. }
  183. } // end for
  184. } // end if
  185. $cfg['Server']['user'] = $PHP_AUTH_USER;
  186. $cfg['Server']['password'] = $PHP_AUTH_PW;
  187. return true;
  188. } // end of the 'PMA_auth_set_user()' function
  189. /**
  190. * User is not allowed to login to MySQL -> authentication failed
  191. *
  192. * @return boolean always true (no return indeed)
  193. *
  194. * @access public
  195. */
  196. function PMA_auth_fails()
  197. {
  198. $error = PMA_DBI_getError();
  199. if ($error && $GLOBALS['errno'] != 1045) {
  200. PMA_fatalError($error);
  201. } else {
  202. PMA_auth();
  203. return true;
  204. }
  205. } // end of the 'PMA_auth_fails()' function
  206. ?>