functions_http.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /* Copyright (C) 2007 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/core/login/functions_http.php
  19. * \ingroup core
  20. * \brief Authentication functions for HTTP Basic
  21. */
  22. /**
  23. * Check validity of user/password/entity
  24. * If test is ko, reason must be filled into $_SESSION["dol_loginmesg"]
  25. *
  26. * @param string $usertotest Login
  27. * @param string $passwordtotest Password
  28. * @param int $entitytotest Number of instance (always 1 if module multicompany not enabled)
  29. * @return string Login if OK, '' if KO
  30. */
  31. function check_user_password_http($usertotest, $passwordtotest, $entitytotest)
  32. {
  33. global $db, $langs;
  34. dol_syslog("functions_http::check_user_password_http _SERVER[REMOTE_USER]=".(empty($_SERVER["REMOTE_USER"]) ? '' : $_SERVER["REMOTE_USER"]));
  35. $login = '';
  36. if (!empty($_SERVER["REMOTE_USER"])) {
  37. $login = $_SERVER["REMOTE_USER"];
  38. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  39. $tmpuser = new User($db);
  40. $tmpuser->fetch('', $login, '', 1, ($entitytotest > 0 ? $entitytotest : -1));
  41. $now = dol_now();
  42. if ($tmpuser->datestartvalidity && $db->jdate($tmpuser->datestartvalidity) >= $now) {
  43. // Load translation files required by the page
  44. $langs->loadLangs(array('main', 'errors'));
  45. $_SESSION["dol_loginmesg"] = $langs->transnoentitiesnoconv("ErrorLoginDateValidity");
  46. return '--bad-login-validity--';
  47. }
  48. if ($tmpuser->dateendvalidity && $db->jdate($tmpuser->dateendvalidity) <= dol_get_first_hour($now)) {
  49. // Load translation files required by the page
  50. $langs->loadLangs(array('main', 'errors'));
  51. $_SESSION["dol_loginmesg"] = $langs->transnoentitiesnoconv("ErrorLoginDateValidity");
  52. return '--bad-login-validity--';
  53. }
  54. }
  55. return $login;
  56. }
  57. /**
  58. * Decode the value found into the Authorization HTTP header.
  59. * Ex: "Authorization: Basic bG9naW46cGFzcw==", $value is "Basic bG9naW46cGFzcw==" and after base64decode is "login:pass"
  60. * Note: the $_SERVER["REMOTE_USER"] contains only the login used in the HTTP Basic form
  61. * Method not used yet, but we keep it for some dev/test purposes.
  62. *
  63. * @param string $value Ex: $_SERVER["REMOTE_USER"]
  64. * @return Object object.login & object.password
  65. */
  66. function decodeHttpBasicAuth($value)
  67. {
  68. $encoded_basic_auth = substr($value, 6); // Remove the "Basic " string
  69. $decoded_basic_auth = base64_decode($encoded_basic_auth);
  70. $credentials_basic_auth = explode(':', $decoded_basic_auth);
  71. return (object) [
  72. 'username'=> $credentials_basic_auth[0],
  73. 'password' => $credentials_basic_auth[1]
  74. ];
  75. }