check_auth.php 11 KB

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