/forum/phpbb/auth/provider/apache.php

https://github.com/AJenbo/ubuntudanmark.dk · PHP · 264 lines · 172 code · 31 blank · 61 comment · 26 complexity · 8af0c23708ac3d17b49d6dd38941e322 MD5 · raw file

  1. <?php
  2. /**
  3. *
  4. * This file is part of the phpBB Forum Software package.
  5. *
  6. * @copyright (c) phpBB Limited <https://www.phpbb.com>
  7. * @license GNU General Public License, version 2 (GPL-2.0)
  8. *
  9. * For full copyright and license information, please see
  10. * the docs/CREDITS.txt file.
  11. *
  12. */
  13. namespace phpbb\auth\provider;
  14. /**
  15. * Apache authentication provider for phpBB3
  16. */
  17. class apache extends \phpbb\auth\provider\base
  18. {
  19. /**
  20. * phpBB passwords manager
  21. *
  22. * @var \phpbb\passwords\manager
  23. */
  24. protected $passwords_manager;
  25. /**
  26. * Apache Authentication Constructor
  27. *
  28. * @param \phpbb\db\driver\driver_interface $db Database object
  29. * @param \phpbb\config\config $config Config object
  30. * @param \phpbb\passwords\manager $passwords_manager Passwords Manager object
  31. * @param \phpbb\request\request $request Request object
  32. * @param \phpbb\user $user User object
  33. * @param string $phpbb_root_path Relative path to phpBB root
  34. * @param string $php_ext PHP file extension
  35. */
  36. public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\config\config $config, \phpbb\passwords\manager $passwords_manager, \phpbb\request\request $request, \phpbb\user $user, $phpbb_root_path, $php_ext)
  37. {
  38. $this->db = $db;
  39. $this->config = $config;
  40. $this->passwords_manager = $passwords_manager;
  41. $this->request = $request;
  42. $this->user = $user;
  43. $this->phpbb_root_path = $phpbb_root_path;
  44. $this->php_ext = $php_ext;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function init()
  50. {
  51. if (!$this->request->is_set('PHP_AUTH_USER', \phpbb\request\request_interface::SERVER) || $this->user->data['username'] !== htmlspecialchars_decode($this->request->server('PHP_AUTH_USER')))
  52. {
  53. return $this->user->lang['APACHE_SETUP_BEFORE_USE'];
  54. }
  55. return false;
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function login($username, $password)
  61. {
  62. // do not allow empty password
  63. if (!$password)
  64. {
  65. return array(
  66. 'status' => LOGIN_ERROR_PASSWORD,
  67. 'error_msg' => 'NO_PASSWORD_SUPPLIED',
  68. 'user_row' => array('user_id' => ANONYMOUS),
  69. );
  70. }
  71. if (!$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. if (!$this->request->is_set('PHP_AUTH_USER', \phpbb\request\request_interface::SERVER))
  80. {
  81. return array(
  82. 'status' => LOGIN_ERROR_EXTERNAL_AUTH,
  83. 'error_msg' => 'LOGIN_ERROR_EXTERNAL_AUTH_APACHE',
  84. 'user_row' => array('user_id' => ANONYMOUS),
  85. );
  86. }
  87. $php_auth_user = htmlspecialchars_decode($this->request->server('PHP_AUTH_USER'));
  88. $php_auth_pw = htmlspecialchars_decode($this->request->server('PHP_AUTH_PW'));
  89. if (!empty($php_auth_user) && !empty($php_auth_pw))
  90. {
  91. if ($php_auth_user !== $username)
  92. {
  93. return array(
  94. 'status' => LOGIN_ERROR_USERNAME,
  95. 'error_msg' => 'LOGIN_ERROR_USERNAME',
  96. 'user_row' => array('user_id' => ANONYMOUS),
  97. );
  98. }
  99. $sql = 'SELECT user_id, username, user_password, user_passchg, user_email, user_type
  100. FROM ' . USERS_TABLE . "
  101. WHERE username = '" . $this->db->sql_escape($php_auth_user) . "'";
  102. $result = $this->db->sql_query($sql);
  103. $row = $this->db->sql_fetchrow($result);
  104. $this->db->sql_freeresult($result);
  105. if ($row)
  106. {
  107. // User inactive...
  108. if ($row['user_type'] == USER_INACTIVE || $row['user_type'] == USER_IGNORE)
  109. {
  110. return array(
  111. 'status' => LOGIN_ERROR_ACTIVE,
  112. 'error_msg' => 'ACTIVE_ERROR',
  113. 'user_row' => $row,
  114. );
  115. }
  116. // Successful login...
  117. return array(
  118. 'status' => LOGIN_SUCCESS,
  119. 'error_msg' => false,
  120. 'user_row' => $row,
  121. );
  122. }
  123. // this is the user's first login so create an empty profile
  124. return array(
  125. 'status' => LOGIN_SUCCESS_CREATE_PROFILE,
  126. 'error_msg' => false,
  127. 'user_row' => $this->user_row($php_auth_user, $php_auth_pw),
  128. );
  129. }
  130. // Not logged into apache
  131. return array(
  132. 'status' => LOGIN_ERROR_EXTERNAL_AUTH,
  133. 'error_msg' => 'LOGIN_ERROR_EXTERNAL_AUTH_APACHE',
  134. 'user_row' => array('user_id' => ANONYMOUS),
  135. );
  136. }
  137. /**
  138. * {@inheritdoc}
  139. */
  140. public function autologin()
  141. {
  142. if (!$this->request->is_set('PHP_AUTH_USER', \phpbb\request\request_interface::SERVER))
  143. {
  144. return array();
  145. }
  146. $php_auth_user = htmlspecialchars_decode($this->request->server('PHP_AUTH_USER'));
  147. $php_auth_pw = htmlspecialchars_decode($this->request->server('PHP_AUTH_PW'));
  148. if (!empty($php_auth_user) && !empty($php_auth_pw))
  149. {
  150. set_var($php_auth_user, $php_auth_user, 'string', true);
  151. set_var($php_auth_pw, $php_auth_pw, 'string', true);
  152. $sql = 'SELECT *
  153. FROM ' . USERS_TABLE . "
  154. WHERE username = '" . $this->db->sql_escape($php_auth_user) . "'";
  155. $result = $this->db->sql_query($sql);
  156. $row = $this->db->sql_fetchrow($result);
  157. $this->db->sql_freeresult($result);
  158. if ($row)
  159. {
  160. return ($row['user_type'] == USER_INACTIVE || $row['user_type'] == USER_IGNORE) ? array() : $row;
  161. }
  162. if (!function_exists('user_add'))
  163. {
  164. include($this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext);
  165. }
  166. // create the user if he does not exist yet
  167. user_add($this->user_row($php_auth_user, $php_auth_pw));
  168. $sql = 'SELECT *
  169. FROM ' . USERS_TABLE . "
  170. WHERE username_clean = '" . $this->db->sql_escape(utf8_clean_string($php_auth_user)) . "'";
  171. $result = $this->db->sql_query($sql);
  172. $row = $this->db->sql_fetchrow($result);
  173. $this->db->sql_freeresult($result);
  174. if ($row)
  175. {
  176. return $row;
  177. }
  178. }
  179. return array();
  180. }
  181. /**
  182. * This function generates an array which can be passed to the user_add
  183. * function in order to create a user
  184. *
  185. * @param string $username The username of the new user.
  186. * @param string $password The password of the new user.
  187. * @return array Contains data that can be passed directly to
  188. * the user_add function.
  189. */
  190. private function user_row($username, $password)
  191. {
  192. // first retrieve default group id
  193. $sql = 'SELECT group_id
  194. FROM ' . GROUPS_TABLE . "
  195. WHERE group_name = '" . $this->db->sql_escape('REGISTERED') . "'
  196. AND group_type = " . GROUP_SPECIAL;
  197. $result = $this->db->sql_query($sql);
  198. $row = $this->db->sql_fetchrow($result);
  199. $this->db->sql_freeresult($result);
  200. if (!$row)
  201. {
  202. trigger_error('NO_GROUP');
  203. }
  204. // generate user account data
  205. return array(
  206. 'username' => $username,
  207. 'user_password' => $this->passwords_manager->hash($password),
  208. 'user_email' => '',
  209. 'group_id' => (int) $row['group_id'],
  210. 'user_type' => USER_NORMAL,
  211. 'user_ip' => $this->user->ip,
  212. 'user_new' => ($this->config['new_member_post_limit']) ? 1 : 0,
  213. );
  214. }
  215. /**
  216. * {@inheritdoc}
  217. */
  218. public function validate_session($user)
  219. {
  220. // Check if PHP_AUTH_USER is set and handle this case
  221. if ($this->request->is_set('PHP_AUTH_USER', \phpbb\request\request_interface::SERVER))
  222. {
  223. $php_auth_user = $this->request->server('PHP_AUTH_USER');
  224. return ($php_auth_user === $user['username']) ? true : false;
  225. }
  226. // PHP_AUTH_USER is not set. A valid session is now determined by the user type (anonymous/bot or not)
  227. if ($user['user_type'] == USER_IGNORE)
  228. {
  229. return true;
  230. }
  231. return false;
  232. }
  233. }