check_auth.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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-2019
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. //includes
  22. require_once "resources/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. ini_set("session.use_only_cookies", True);
  32. ini_set("session.cookie_httponly", True);
  33. if ($_SERVER["HTTPS"] == "on") { ini_set("session.cookie_secure", True); }
  34. if (!isset($_SESSION)) { session_start(); }
  35. //define variables
  36. if (!isset($_SESSION['login']['destination']['url'])) { $_SESSION['login']['destination']['url'] = null; }
  37. if (!isset($_SESSION['template_content'])) { $_SESSION["template_content"] = null; }
  38. //if the username is not provided then send to login.php
  39. if (strlen($_SESSION['username']) == 0 && strlen($_REQUEST["username"]) == 0 && strlen($_REQUEST["key"]) == 0) {
  40. $target_path = ($_REQUEST["path"] != '') ? $_REQUEST["path"] : $_SERVER["REQUEST_URI"];
  41. header("Location: ".PROJECT_PATH."/login.php?path=".urlencode($target_path));
  42. exit;
  43. }
  44. //if the username session is not set the check username and password
  45. if (strlen($_SESSION['username']) == 0) {
  46. //clear the menu
  47. unset($_SESSION["menu"]);
  48. //clear the template only if the template has not been assigned by the superadmin
  49. if (strlen($_SESSION['domain']['template']['name']) == 0) {
  50. $_SESSION["template_content"] = '';
  51. }
  52. //validate the username and password
  53. $auth = new authentication;
  54. if (isset($_REQUEST["username"]) && isset($_REQUEST["password"])) {
  55. $auth->username = $_REQUEST["username"];
  56. $auth->password = $_REQUEST["password"];
  57. }
  58. if (isset($_REQUEST["key"])) {
  59. $auth->key = $_REQUEST["key"];
  60. }
  61. $auth->debug = false;
  62. $result = $auth->validate();
  63. if ($result["authorized"] == "true") {
  64. //set the session variables
  65. $_SESSION["domain_uuid"] = $result["domain_uuid"];
  66. $_SESSION["user_uuid"] = $result["user_uuid"];
  67. //user session array
  68. $_SESSION["user"]["domain_uuid"] = $result["domain_uuid"];
  69. $_SESSION["user"]["user_uuid"] = $result["user_uuid"];
  70. $_SESSION["user"]["username"] = $result["username"];
  71. $_SESSION["user"]["contact_uuid"] = $result["contact_uuid"];
  72. }
  73. else {
  74. //debug
  75. if ($debug) {
  76. echo "<pre>";
  77. print_r($result);
  78. echo "</pre>";
  79. exit;
  80. }
  81. //log the failed auth attempt to the system, to be available for fail2ban.
  82. openlog('FusionPBX', LOG_NDELAY, LOG_AUTH);
  83. syslog(LOG_WARNING, '['.$_SERVER['REMOTE_ADDR']."] authentication failed for ".$result["username"]);
  84. closelog();
  85. //redirect the user to the login page
  86. $target_path = ($_REQUEST["path"] != '') ? $_REQUEST["path"] : $_SERVER["PHP_SELF"];
  87. message::add($text['message-invalid_credentials'], 'negative');
  88. header("Location: ".PROJECT_PATH."/login.php?path=".urlencode($target_path));
  89. exit;
  90. }
  91. //get the groups assigned to the user and then set the groups in $_SESSION["groups"]
  92. $sql = "select u.user_group_uuid, u.domain_uuid, u.user_uuid, u.group_uuid, g.group_name, g.group_level ";
  93. $sql .= "from v_user_groups as u, v_groups as g ";
  94. $sql .= "where u.domain_uuid = :domain_uuid ";
  95. $sql .= "and u.user_uuid = :user_uuid ";
  96. $sql .= "and u.group_uuid = g.group_uuid ";
  97. $prep_statement = $db->prepare($sql);
  98. $prep_statement->bindParam(':domain_uuid', $_SESSION["domain_uuid"] );
  99. $prep_statement->bindParam(':user_uuid', $_SESSION["user_uuid"]);
  100. $prep_statement->execute();
  101. $result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
  102. $_SESSION["groups"] = $result;
  103. $_SESSION["user"]["groups"] = $result;
  104. unset($sql, $row_count, $prep_statement);
  105. //get the users group level
  106. $_SESSION["user"]["group_level"] = 0;
  107. foreach ($_SESSION['user']['groups'] as $row) {
  108. if ($_SESSION["user"]["group_level"] < $row['group_level']) {
  109. $_SESSION["user"]["group_level"] = $row['group_level'];
  110. }
  111. }
  112. //get the permissions assigned to the groups that the user is a member of set the permissions in $_SESSION['permissions']
  113. if (count($_SESSION["groups"]) > 0) {
  114. $x = 0;
  115. $sql = "select distinct(permission_name) from v_group_permissions ";
  116. foreach($_SESSION["groups"] as $field) {
  117. if (strlen($field['group_name']) > 0) {
  118. if ($x == 0) {
  119. $sql .= "where (domain_uuid = '".$_SESSION["domain_uuid"]."' and domain_uuid = null) ";
  120. }
  121. else {
  122. $sql .= "or (domain_uuid = '".$_SESSION["domain_uuid"]."' and domain_uuid = null) ";
  123. }
  124. $sql .= "or group_name = '".$field['group_name']."' ";
  125. $x++;
  126. }
  127. }
  128. $prep_statement_sub = $db->prepare($sql);
  129. $prep_statement_sub->execute();
  130. $result = $prep_statement_sub->fetchAll(PDO::FETCH_NAMED);
  131. if (is_array($result)) {
  132. foreach ($result as $row) {
  133. $_SESSION['permissions'][$row["permission_name"]] = true;
  134. $_SESSION["user"]["permissions"][$row["permission_name"]] = true;
  135. }
  136. }
  137. unset($sql, $prep_statement_sub);
  138. }
  139. //get the user settings
  140. $sql = "select * from v_user_settings ";
  141. $sql .= "where domain_uuid = '" . $_SESSION["domain_uuid"] . "' ";
  142. $sql .= "and user_uuid = '" . $_SESSION["user_uuid"] . "' ";
  143. $sql .= "and user_setting_enabled = 'true' ";
  144. $prep_statement = $db->prepare($sql);
  145. if ($prep_statement) {
  146. $prep_statement->execute();
  147. $result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
  148. foreach ($result as $row) {
  149. $name = $row['user_setting_name'];
  150. $category = $row['user_setting_category'];
  151. $subcategory = $row['user_setting_subcategory'];
  152. if (strlen($row['user_setting_value']) > 0) {
  153. if (strlen($subcategory) == 0) {
  154. //$$category[$name] = $row['domain_setting_value'];
  155. if ($name == "array") {
  156. $_SESSION[$category][] = $row['user_setting_value'];
  157. }
  158. else {
  159. $_SESSION[$category][$name] = $row['user_setting_value'];
  160. }
  161. } else {
  162. //$$category[$subcategory][$name] = $row['domain_setting_value'];
  163. if ($name == "array") {
  164. $_SESSION[$category][$subcategory][] = $row['user_setting_value'];
  165. }
  166. else {
  167. $_SESSION[$category][$subcategory][$name] = $row['user_setting_value'];
  168. }
  169. }
  170. }
  171. }
  172. }
  173. //get the extensions that are assigned to this user
  174. if (file_exists($_SERVER["PROJECT_ROOT"]."/app/extensions/app_config.php")) {
  175. if (isset($_SESSION["user"]) && isset($_SESSION["user_uuid"]) && $db && strlen($_SESSION["domain_uuid"]) > 0 && strlen($_SESSION["user_uuid"]) > 0 && count($_SESSION['user']['extension']) == 0) {
  176. //get the user extension list
  177. $_SESSION['user']['extension'] = null;
  178. $sql = "select ";
  179. $sql .= " e.extension_uuid, ";
  180. $sql .= " e.extension, ";
  181. $sql .= " e.number_alias, ";
  182. $sql .= " e.user_context, ";
  183. $sql .= " e.outbound_caller_id_name, ";
  184. $sql .= " e.outbound_caller_id_number, ";
  185. $sql .= " e.description ";
  186. $sql .= "from ";
  187. $sql .= " v_extension_users as u, ";
  188. $sql .= " v_extensions as e ";
  189. $sql .= "where ";
  190. $sql .= " e.domain_uuid = '".$_SESSION['domain_uuid']."' ";
  191. $sql .= " and e.extension_uuid = u.extension_uuid ";
  192. $sql .= " and u.user_uuid = '".$_SESSION['user_uuid']."' ";
  193. $sql .= " and e.enabled = 'true' ";
  194. $sql .= "order by ";
  195. $sql .= " e.extension asc ";
  196. $query = $db->query($sql);
  197. if($query !== false) {
  198. $result = $db->query($sql)->fetchAll(PDO::FETCH_ASSOC);
  199. $x = 0;
  200. foreach($result as $row) {
  201. //set the destination
  202. $destination = $row['extension'];
  203. if (strlen($row['number_alias']) > 0) {
  204. $destination = $row['number_alias'];
  205. }
  206. //build the uers array
  207. $_SESSION['user']['extension'][$x]['user'] = $row['extension'];
  208. $_SESSION['user']['extension'][$x]['number_alias'] = $row['number_alias'];
  209. $_SESSION['user']['extension'][$x]['destination'] = $destination;
  210. $_SESSION['user']['extension'][$x]['extension_uuid'] = $row['extension_uuid'];
  211. $_SESSION['user']['extension'][$x]['outbound_caller_id_name'] = $row['outbound_caller_id_name'];
  212. $_SESSION['user']['extension'][$x]['outbound_caller_id_number'] = $row['outbound_caller_id_number'];
  213. $_SESSION['user']['extension'][$x]['user_context'] = $row['user_context'];
  214. $_SESSION['user']['extension'][$x]['description'] = $row['description'];
  215. //set the user context
  216. $_SESSION['user']['user_context'] = $row["user_context"];
  217. $_SESSION['user_context'] = $row["user_context"];
  218. $x++;
  219. }
  220. }
  221. }
  222. }
  223. //redirect the user
  224. if (check_str($_REQUEST["rdr"]) !== 'n'){
  225. $path = check_str($_POST["path"]);
  226. if (isset($path) && !empty($path) && $path!="index2.php" && $path!="/install.php") {
  227. header("Location: ".$path);
  228. exit();
  229. }
  230. else if ($_SESSION['login']['destination']['url'] != '') {
  231. header("Location: ".$_SESSION['login']['destination']['url']);
  232. exit();
  233. }
  234. }
  235. //get the domains
  236. if (file_exists($_SERVER["PROJECT_ROOT"]."/app/domains/app_config.php") && !is_cli()){
  237. require_once "app/domains/resources/domains.php";
  238. }
  239. }
  240. //set the time zone
  241. if (!isset($_SESSION["time_zone"]["user"])) { $_SESSION["time_zone"]["user"] = null; }
  242. if (strlen($_SESSION["time_zone"]["user"]) == 0) {
  243. //set the domain time zone as the default time zone
  244. date_default_timezone_set($_SESSION['domain']['time_zone']['name']);
  245. }
  246. else {
  247. //set the user defined time zone
  248. date_default_timezone_set($_SESSION["time_zone"]["user"]);
  249. }
  250. //hide the path unless logged in as a superadmin.
  251. if (!if_group("superadmin")) {
  252. $v_path_show = false;
  253. }
  254. ?>