check_auth.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /*
  3. FusionPBX
  4. Version: MPL 1.1
  5. The contents of this file are subject to the Mozilla Public License Version
  6. 1.1 (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. for the specific language governing rights and limitations under the
  12. License.
  13. The Original Code is FusionPBX
  14. The Initial Developer of the Original Code is
  15. Mark J Crane <[email protected]>
  16. Portions created by the Initial Developer are Copyright (C) 2008-2023
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. //includes files
  22. require_once __DIR__ . "/require.php";
  23. //add multi-lingual support
  24. $language = new text;
  25. $text = $language->get(null, 'resources');
  26. //for compatibility require this library if less than version 5.5
  27. if (version_compare(phpversion(), '5.5', '<')) {
  28. require_once "resources/functions/password.php";
  29. }
  30. //start the session
  31. if (function_exists('session_start')) {
  32. if (!isset($_SESSION)) {
  33. session_start();
  34. }
  35. }
  36. //regenerate sessions to avoid session id attacks such as session fixation
  37. if (isset($_SESSION['authorized']) && $_SESSION['authorized']) {
  38. //set the last activity time
  39. $_SESSION['session']['last_activity'] = time();
  40. //if session created is not set then set the time
  41. if (!isset($_SESSION['session']['created'])) {
  42. $_SESSION['session']['created'] = time();
  43. }
  44. //check the elapsed time if exceeds limit then rotate the session
  45. if (time() - $_SESSION['session']['created'] > 900) {
  46. //build the user log array
  47. $log_array['domain_uuid'] = $_SESSION['user']['domain_uuid'];
  48. $log_array['domain_name'] = $_SESSION['user']['domain_name'];
  49. $log_array['username'] = $_SESSION['user']['username'];
  50. $log_array['user_uuid'] = $_SESSION['user']['user_uuid'];
  51. $log_array['authorized'] = true;
  52. //session started more than 15 minutes
  53. session_regenerate_id(true);
  54. // update creation time
  55. $_SESSION['session']['created'] = time();
  56. //add the result to the user logs
  57. user_logs::add($log_array);
  58. }
  59. }
  60. //set the domains session
  61. if (!isset($_SESSION['domains'])) {
  62. $domain = new domains();
  63. $domain->session();
  64. $domain->set();
  65. }
  66. //set the domain_uuid variable from the session
  67. if (!empty($_SESSION["domain_uuid"])) {
  68. $domain_uuid = $_SESSION["domain_uuid"];
  69. }
  70. //define variables
  71. if (!isset($_SESSION['template_content'])) { $_SESSION["template_content"] = null; }
  72. //if session authorized is not set then set the default value to false
  73. if (!isset($_SESSION['authorized'])) {
  74. $_SESSION['authorized'] = false;
  75. }
  76. //session validate: use HTTP_USER_AGENT as a default value
  77. if (!isset($conf['session.validate'])) {
  78. $conf['session.validate'][] = 'HTTP_USER_AGENT';
  79. }
  80. //session validate: prepare the server array
  81. foreach($conf['session.validate'] as $name) {
  82. $server_array[$name] = $_SERVER[$name];
  83. }
  84. unset($name);
  85. //session validate: check to see if the session is valid
  86. if ($_SESSION['authorized'] && $_SESSION["user_hash"] !== hash('sha256', implode($server_array))) {
  87. session_destroy();
  88. header("Location: ".PROJECT_PATH."/logout.php");
  89. }
  90. //if the session is not authorized then verify the identity
  91. if (!$_SESSION['authorized']) {
  92. //clear the menu
  93. unset($_SESSION["menu"]);
  94. //clear the template only if the template has not been assigned by the superadmin
  95. if (empty($_SESSION['domain']['template']['name'])) {
  96. $_SESSION["template_content"] = '';
  97. }
  98. //validate the username and password
  99. $auth = new authentication;
  100. $result = $auth->validate();
  101. //if not authorized
  102. if (empty($_SESSION['authorized']) || !$_SESSION['authorized']) {
  103. //log the failed auth attempt to the system to the syslog server
  104. openlog('FusionPBX', LOG_NDELAY, LOG_AUTH);
  105. syslog(LOG_WARNING, '['.$_SERVER['REMOTE_ADDR']."] authentication failed for ".$result["username"]);
  106. closelog();
  107. //redirect the user to the login page
  108. $target_path = !empty($_REQUEST["path"]) ? $_REQUEST["path"] : $_SERVER["PHP_SELF"];
  109. message::add($text['message-authentication_failed'], 'negative');
  110. header("Location: ".PROJECT_PATH."/?path=".urlencode($target_path));
  111. exit;
  112. }
  113. //if logged in, redirect to login destination
  114. if (!isset($_REQUEST["key"])) {
  115. if (isset($_SESSION['redirect_path'])) {
  116. $redirect_path = $_SESSION['redirect_path'];
  117. unset($_SESSION['redirect_path']);
  118. // prevent open redirect attacks. redirect url shouldn't contain a hostname
  119. $parsed_url = parse_url($redirect_path);
  120. if ($parsed_url['host']) {
  121. die("Was someone trying to hack you?");
  122. }
  123. header("Location: ".$redirect_path);
  124. }
  125. elseif (isset($_SESSION['login']['destination']['text'])) {
  126. header("Location: ".$_SESSION['login']['destination']['text']);
  127. }
  128. elseif (file_exists($_SERVER["PROJECT_ROOT"]."/core/dashboard/app_config.php")) {
  129. header("Location: ".PROJECT_PATH."/core/dashboard/");
  130. }
  131. else {
  132. require_once "resources/header.php";
  133. require_once "resources/footer.php";
  134. }
  135. }
  136. }
  137. ?>