/phpBB/includes/auth/auth_apache.php

https://github.com/LeviathanX/phpbb3 · PHP · 248 lines · 167 code · 34 blank · 47 comment · 27 complexity · 83fa87d5af31f5b074a36bfbed603b9b MD5 · raw file

  1. <?php
  2. /**
  3. * Apache auth plug-in for phpBB3
  4. *
  5. * Authentication plug-ins is largely down to Sergey Kanareykin, our thanks to him.
  6. *
  7. * @package login
  8. * @version $Id$
  9. * @copyright (c) 2005 phpBB Group
  10. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  11. *
  12. */
  13. /**
  14. * @ignore
  15. */
  16. if (!defined('IN_PHPBB'))
  17. {
  18. exit;
  19. }
  20. /**
  21. * Checks whether the user is identified to apache
  22. * Only allow changing authentication to apache if the user is identified
  23. * Called in acp_board while setting authentication plugins
  24. *
  25. * @return boolean|string false if the user is identified and else an error message
  26. */
  27. function init_apache()
  28. {
  29. global $user, $request;
  30. if (!$request->is_set('PHP_AUTH_USER', phpbb_request_interface::SERVER) || $user->data['username'] !== htmlspecialchars_decode($request->server('PHP_AUTH_USER')))
  31. {
  32. return $user->lang['APACHE_SETUP_BEFORE_USE'];
  33. }
  34. return false;
  35. }
  36. /**
  37. * Login function
  38. */
  39. function login_apache(&$username, &$password)
  40. {
  41. global $db, $request;
  42. // do not allow empty password
  43. if (!$password)
  44. {
  45. return array(
  46. 'status' => LOGIN_ERROR_PASSWORD,
  47. 'error_msg' => 'NO_PASSWORD_SUPPLIED',
  48. 'user_row' => array('user_id' => ANONYMOUS),
  49. );
  50. }
  51. if (!$username)
  52. {
  53. return array(
  54. 'status' => LOGIN_ERROR_USERNAME,
  55. 'error_msg' => 'LOGIN_ERROR_USERNAME',
  56. 'user_row' => array('user_id' => ANONYMOUS),
  57. );
  58. }
  59. if (!$request->is_set('PHP_AUTH_USER', phpbb_request_interface::SERVER))
  60. {
  61. return array(
  62. 'status' => LOGIN_ERROR_EXTERNAL_AUTH,
  63. 'error_msg' => 'LOGIN_ERROR_EXTERNAL_AUTH_APACHE',
  64. 'user_row' => array('user_id' => ANONYMOUS),
  65. );
  66. }
  67. $php_auth_user = htmlspecialchars_decode($request->server('PHP_AUTH_USER'));
  68. $php_auth_pw = htmlspecialchars_decode($request->server('PHP_AUTH_PW'));
  69. if (!empty($php_auth_user) && !empty($php_auth_pw))
  70. {
  71. if ($php_auth_user !== $username)
  72. {
  73. return array(
  74. 'status' => LOGIN_ERROR_USERNAME,
  75. 'error_msg' => 'LOGIN_ERROR_USERNAME',
  76. 'user_row' => array('user_id' => ANONYMOUS),
  77. );
  78. }
  79. $sql = 'SELECT user_id, username, user_password, user_passchg, user_email, user_type
  80. FROM ' . USERS_TABLE . "
  81. WHERE username = '" . $db->sql_escape($php_auth_user) . "'";
  82. $result = $db->sql_query($sql);
  83. $row = $db->sql_fetchrow($result);
  84. $db->sql_freeresult($result);
  85. if ($row)
  86. {
  87. // User inactive...
  88. if ($row['user_type'] == USER_INACTIVE || $row['user_type'] == USER_IGNORE)
  89. {
  90. return array(
  91. 'status' => LOGIN_ERROR_ACTIVE,
  92. 'error_msg' => 'ACTIVE_ERROR',
  93. 'user_row' => $row,
  94. );
  95. }
  96. // Successful login...
  97. return array(
  98. 'status' => LOGIN_SUCCESS,
  99. 'error_msg' => false,
  100. 'user_row' => $row,
  101. );
  102. }
  103. // this is the user's first login so create an empty profile
  104. return array(
  105. 'status' => LOGIN_SUCCESS_CREATE_PROFILE,
  106. 'error_msg' => false,
  107. 'user_row' => user_row_apache($php_auth_user, $php_auth_pw),
  108. );
  109. }
  110. // Not logged into apache
  111. return array(
  112. 'status' => LOGIN_ERROR_EXTERNAL_AUTH,
  113. 'error_msg' => 'LOGIN_ERROR_EXTERNAL_AUTH_APACHE',
  114. 'user_row' => array('user_id' => ANONYMOUS),
  115. );
  116. }
  117. /**
  118. * Autologin function
  119. *
  120. * @return array containing the user row or empty if no auto login should take place
  121. */
  122. function autologin_apache()
  123. {
  124. global $db, $request;
  125. if (!$request->is_set('PHP_AUTH_USER', phpbb_request_interface::SERVER))
  126. {
  127. return array();
  128. }
  129. $php_auth_user = htmlspecialchars_decode($request->server('PHP_AUTH_USER'));
  130. $php_auth_pw = htmlspecialchars_decode($request->server('PHP_AUTH_PW'));
  131. if (!empty($php_auth_user) && !empty($php_auth_pw))
  132. {
  133. set_var($php_auth_user, $php_auth_user, 'string', true);
  134. set_var($php_auth_pw, $php_auth_pw, 'string', true);
  135. $sql = 'SELECT *
  136. FROM ' . USERS_TABLE . "
  137. WHERE username = '" . $db->sql_escape($php_auth_user) . "'";
  138. $result = $db->sql_query($sql);
  139. $row = $db->sql_fetchrow($result);
  140. $db->sql_freeresult($result);
  141. if ($row)
  142. {
  143. return ($row['user_type'] == USER_INACTIVE || $row['user_type'] == USER_IGNORE) ? array() : $row;
  144. }
  145. if (!function_exists('user_add'))
  146. {
  147. global $phpbb_root_path, $phpEx;
  148. include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
  149. }
  150. // create the user if he does not exist yet
  151. user_add(user_row_apache($php_auth_user, $php_auth_pw));
  152. $sql = 'SELECT *
  153. FROM ' . USERS_TABLE . "
  154. WHERE username_clean = '" . $db->sql_escape(utf8_clean_string($php_auth_user)) . "'";
  155. $result = $db->sql_query($sql);
  156. $row = $db->sql_fetchrow($result);
  157. $db->sql_freeresult($result);
  158. if ($row)
  159. {
  160. return $row;
  161. }
  162. }
  163. return array();
  164. }
  165. /**
  166. * This function generates an array which can be passed to the user_add function in order to create a user
  167. */
  168. function user_row_apache($username, $password)
  169. {
  170. global $db, $config, $user;
  171. // first retrieve default group id
  172. $sql = 'SELECT group_id
  173. FROM ' . GROUPS_TABLE . "
  174. WHERE group_name = '" . $db->sql_escape('REGISTERED') . "'
  175. AND group_type = " . GROUP_SPECIAL;
  176. $result = $db->sql_query($sql);
  177. $row = $db->sql_fetchrow($result);
  178. $db->sql_freeresult($result);
  179. if (!$row)
  180. {
  181. trigger_error('NO_GROUP');
  182. }
  183. // generate user account data
  184. return array(
  185. 'username' => $username,
  186. 'user_password' => phpbb_hash($password),
  187. 'user_email' => '',
  188. 'group_id' => (int) $row['group_id'],
  189. 'user_type' => USER_NORMAL,
  190. 'user_ip' => $user->ip,
  191. 'user_new' => ($config['new_member_post_limit']) ? 1 : 0,
  192. );
  193. }
  194. /**
  195. * The session validation function checks whether the user is still logged in
  196. *
  197. * @return boolean true if the given user is authenticated or false if the session should be closed
  198. */
  199. function validate_session_apache(&$user)
  200. {
  201. global $request;
  202. // Check if PHP_AUTH_USER is set and handle this case
  203. if ($request->is_set('PHP_AUTH_USER', phpbb_request_interface::SERVER))
  204. {
  205. $php_auth_user = $request->server('PHP_AUTH_USER');
  206. return ($php_auth_user === $user['username']) ? true : false;
  207. }
  208. // PHP_AUTH_USER is not set. A valid session is now determined by the user type (anonymous/bot or not)
  209. if ($user['user_type'] == USER_IGNORE)
  210. {
  211. return true;
  212. }
  213. return false;
  214. }