Fork of FusionPBX but with LDAP kinda working
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

335 lines
12 KiB

2 years ago
  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 <markjcrane@fusionpbx.com>
  16. Portions created by the Initial Developer are Copyright (C) 2008-2021
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <markjcrane@fusionpbx.com>
  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['template_content'])) { $_SESSION["template_content"] = null; }
  34. //if the username is not provided then send to login.php
  35. if (strlen($_SESSION['username']) == 0 && strlen($_REQUEST["username"]) == 0 && strlen($_REQUEST["key"]) == 0) {
  36. $target_path = ($_REQUEST["path"] != '') ? $_REQUEST["path"] : $_SERVER["REQUEST_URI"];
  37. header("Location: ".PROJECT_PATH."/login.php?path=".urlencode($target_path));
  38. exit;
  39. }
  40. //if the username session is not set the check username and password
  41. if (strlen($_SESSION['username']) == 0) {
  42. //clear the menu
  43. unset($_SESSION["menu"]);
  44. //clear the template only if the template has not been assigned by the superadmin
  45. if (strlen($_SESSION['domain']['template']['name']) == 0) {
  46. $_SESSION["template_content"] = '';
  47. }
  48. //validate the username and password
  49. $auth = new authentication;
  50. if (isset($_REQUEST["username"]) && isset($_REQUEST["password"])) {
  51. $auth->username = $_REQUEST["username"];
  52. $auth->password = $_REQUEST["password"];
  53. }
  54. if (isset($_REQUEST["key"])) {
  55. $auth->key = $_REQUEST["key"];
  56. }
  57. $auth->debug = false;
  58. $result = $auth->validate();
  59. if ($result["authorized"] === "true") {
  60. //get the user settings
  61. $sql = "select * from v_user_settings ";
  62. $sql .= "where domain_uuid = :domain_uuid ";
  63. $sql .= "and user_uuid = :user_uuid ";
  64. $sql .= "and user_setting_enabled = 'true' ";
  65. $parameters['domain_uuid'] = $result["domain_uuid"];
  66. $parameters['user_uuid'] = $result["user_uuid"];
  67. $database = new database;
  68. $user_settings = $database->select($sql, $parameters, 'all');
  69. unset($sql, $parameters);
  70. //build the user cidr array
  71. if (is_array($user_settings) && @sizeof($user_settings) != 0) {
  72. foreach ($user_settings as $row) {
  73. if ($row['user_setting_category'] == "domain" && $row['user_setting_subcategory'] == "cidr" && $row['user_setting_name'] == "array") {
  74. $cidr_array[] = $row['user_setting_value'];
  75. }
  76. }
  77. }
  78. //check to see if user address is in the cidr array
  79. if (isset($cidr_array) && !defined('STDIN')) {
  80. $found = false;
  81. foreach($cidr_array as $cidr) {
  82. if (check_cidr($cidr, $_SERVER['REMOTE_ADDR'])) {
  83. $found = true;
  84. break;
  85. }
  86. }
  87. if (!$found) {
  88. //destroy session
  89. session_unset();
  90. session_destroy();
  91. //send http 403
  92. header('HTTP/1.0 403 Forbidden', true, 403);
  93. //redirect to the root of the website
  94. header("Location: ".PROJECT_PATH."/login.php");
  95. //exit the code
  96. exit();
  97. }
  98. }
  99. //set the session variables
  100. $_SESSION["domain_uuid"] = $result["domain_uuid"];
  101. //$_SESSION["domain_name"] = $result["domain_name"];
  102. $_SESSION["user_uuid"] = $result["user_uuid"];
  103. $_SESSION["context"] = $result['domain_name'];
  104. //user session array
  105. $_SESSION["user"]["domain_uuid"] = $result["domain_uuid"];
  106. $_SESSION["user"]["domain_name"] = $result["domain_name"];
  107. $_SESSION["user"]["user_uuid"] = $result["user_uuid"];
  108. $_SESSION["user"]["username"] = $result["username"];
  109. $_SESSION["user"]["contact_uuid"] = $result["contact_uuid"];
  110. }
  111. else {
  112. //debug
  113. if ($debug) {
  114. view_array($result);
  115. }
  116. //log the failed auth attempt to the system, to be available for fail2ban.
  117. openlog('FusionPBX', LOG_NDELAY, LOG_AUTH);
  118. syslog(LOG_WARNING, '['.$_SERVER['REMOTE_ADDR']."] authentication failed for ".$result["username"]);
  119. closelog();
  120. //redirect the user to the login page
  121. $target_path = ($_REQUEST["path"] != '') ? $_REQUEST["path"] : $_SERVER["PHP_SELF"];
  122. message::add($text['message-invalid_credentials'], 'negative');
  123. header("Location: ".PROJECT_PATH."/login.php?path=".urlencode($target_path));
  124. exit;
  125. }
  126. //get the groups assigned to the user and then set the groups in $_SESSION["groups"]
  127. $sql = "select ";
  128. $sql .= "u.user_group_uuid, ";
  129. $sql .= "u.domain_uuid, ";
  130. $sql .= "u.user_uuid, ";
  131. $sql .= "u.group_uuid, ";
  132. $sql .= "g.group_name, ";
  133. $sql .= "g.group_level ";
  134. $sql .= "from ";
  135. $sql .= "v_user_groups as u, ";
  136. $sql .= "v_groups as g ";
  137. $sql .= "where u.domain_uuid = :domain_uuid ";
  138. $sql .= "and u.user_uuid = :user_uuid ";
  139. $sql .= "and u.group_uuid = g.group_uuid ";
  140. $parameters['domain_uuid'] = $_SESSION["domain_uuid"];
  141. $parameters['user_uuid'] = $_SESSION["user_uuid"];
  142. $database = new database;
  143. $result = $database->select($sql, $parameters, 'all');
  144. $_SESSION["groups"] = $result;
  145. $_SESSION["user"]["groups"] = $result;
  146. unset($sql, $parameters);
  147. //get the users group level
  148. $_SESSION["user"]["group_level"] = 0;
  149. foreach ($_SESSION['user']['groups'] as $row) {
  150. if ($_SESSION["user"]["group_level"] < $row['group_level']) {
  151. $_SESSION["user"]["group_level"] = $row['group_level'];
  152. }
  153. }
  154. //get the permissions assigned to the groups that the user is a member of set the permissions in $_SESSION['permissions']
  155. if (is_array($_SESSION["groups"]) && @sizeof($_SESSION["groups"]) != 0) {
  156. $x = 0;
  157. $sql = "select distinct(permission_name) from v_group_permissions ";
  158. $sql .= "where (domain_uuid = :domain_uuid or domain_uuid is null) ";
  159. foreach ($_SESSION["groups"] as $field) {
  160. if (strlen($field['group_name']) > 0) {
  161. $sql_where_or[] = "group_name = :group_name_".$x;
  162. $parameters['group_name_'.$x] = $field['group_name'];
  163. $x++;
  164. }
  165. }
  166. if (is_array($sql_where_or) && @sizeof($sql_where_or) != 0) {
  167. $sql .= "and (".implode(' or ', $sql_where_or).") ";
  168. }
  169. $sql .= "and permission_assigned = 'true' ";
  170. $parameters['domain_uuid'] = $_SESSION["domain_uuid"];
  171. $database = new database;
  172. $result = $database->select($sql, $parameters, 'all');
  173. if (is_array($result) && @sizeof($result) != 0) {
  174. foreach ($result as $row) {
  175. $_SESSION['permissions'][$row["permission_name"]] = true;
  176. $_SESSION["user"]["permissions"][$row["permission_name"]] = true;
  177. }
  178. }
  179. unset($sql, $parameters, $result, $row);
  180. }
  181. //get the domains
  182. if (file_exists($_SERVER["PROJECT_ROOT"]."/app/domains/app_config.php") && !is_cli()){
  183. require_once "app/domains/resources/domains.php";
  184. }
  185. //get the user settings
  186. if (is_array($user_settings) && @sizeof($user_settings) != 0) {
  187. foreach ($user_settings as $row) {
  188. $name = $row['user_setting_name'];
  189. $category = $row['user_setting_category'];
  190. $subcategory = $row['user_setting_subcategory'];
  191. if (strlen($row['user_setting_value']) > 0) {
  192. if (strlen($subcategory) == 0) {
  193. //$$category[$name] = $row['domain_setting_value'];
  194. if ($name == "array") {
  195. $_SESSION[$category][] = $row['user_setting_value'];
  196. }
  197. else {
  198. $_SESSION[$category][$name] = $row['user_setting_value'];
  199. }
  200. }
  201. else {
  202. //$$category[$subcategory][$name] = $row['domain_setting_value'];
  203. if ($name == "array") {
  204. $_SESSION[$category][$subcategory][] = $row['user_setting_value'];
  205. }
  206. else {
  207. $_SESSION[$category][$subcategory][$name] = $row['user_setting_value'];
  208. }
  209. }
  210. }
  211. }
  212. }
  213. unset($user_settings);
  214. //get the extensions that are assigned to this user
  215. if (file_exists($_SERVER["PROJECT_ROOT"]."/app/extensions/app_config.php")) {
  216. if (isset($_SESSION["user"]) && is_uuid($_SESSION["user_uuid"]) && is_uuid($_SESSION["domain_uuid"]) && !isset($_SESSION['user']['extension'])) {
  217. //get the user extension list
  218. $_SESSION['user']['extension'] = null;
  219. $sql = "select ";
  220. $sql .= "e.extension_uuid, ";
  221. $sql .= "e.extension, ";
  222. $sql .= "e.number_alias, ";
  223. $sql .= "e.user_context, ";
  224. $sql .= "e.outbound_caller_id_name, ";
  225. $sql .= "e.outbound_caller_id_number, ";
  226. $sql .= "e.description ";
  227. $sql .= "from ";
  228. $sql .= "v_extension_users as u, ";
  229. $sql .= "v_extensions as e ";
  230. $sql .= "where ";
  231. $sql .= "e.domain_uuid = :domain_uuid ";
  232. $sql .= "and e.extension_uuid = u.extension_uuid ";
  233. $sql .= "and u.user_uuid = :user_uuid ";
  234. $sql .= "and e.enabled = 'true' ";
  235. $sql .= "order by ";
  236. $sql .= "e.extension asc ";
  237. $parameters['domain_uuid'] = $_SESSION['domain_uuid'];
  238. $parameters['user_uuid'] = $_SESSION['user_uuid'];
  239. $database = new database;
  240. $result = $database->select($sql, $parameters, 'all');
  241. if (is_array($result) && @sizeof($result) != 0) {
  242. foreach($result as $x => $row) {
  243. //set the destination
  244. $destination = $row['extension'];
  245. if (strlen($row['number_alias']) > 0) {
  246. $destination = $row['number_alias'];
  247. }
  248. //build the user array
  249. $_SESSION['user']['extension'][$x]['user'] = $row['extension'];
  250. $_SESSION['user']['extension'][$x]['number_alias'] = $row['number_alias'];
  251. $_SESSION['user']['extension'][$x]['destination'] = $destination;
  252. $_SESSION['user']['extension'][$x]['extension_uuid'] = $row['extension_uuid'];
  253. $_SESSION['user']['extension'][$x]['outbound_caller_id_name'] = $row['outbound_caller_id_name'];
  254. $_SESSION['user']['extension'][$x]['outbound_caller_id_number'] = $row['outbound_caller_id_number'];
  255. $_SESSION['user']['extension'][$x]['user_context'] = $row['user_context'];
  256. $_SESSION['user']['extension'][$x]['description'] = $row['description'];
  257. //set the context
  258. $_SESSION['user']['user_context'] = $row["user_context"];
  259. $_SESSION['user_context'] = $row["user_context"];
  260. }
  261. }
  262. unset($sql, $parameters, $result, $row);
  263. }
  264. }
  265. //if logged in, redirect to login destination
  266. if (!isset($_REQUEST["key"])) {
  267. if (isset($_SESSION['redirect_path'])) {
  268. $redirect_path = $_SESSION['redirect_path'];
  269. unset($_SESSION['redirect_path']);
  270. // prevent open redirect attacks. redirect url shouldn't contain a hostname
  271. $parsed_url = parse_url($redirect_path);
  272. if ($parsed_url['host']) {
  273. die("Was someone trying to hack you?");
  274. }
  275. header("Location: ".$redirect_path);
  276. }
  277. elseif (isset($_SESSION['login']['destination']['url'])) {
  278. header("Location: ".$_SESSION['login']['destination']['url']);
  279. } elseif (file_exists($_SERVER["PROJECT_ROOT"]."/core/dashboard/app_config.php")) {
  280. header("Location: ".PROJECT_PATH."/core/dashboard/");
  281. }
  282. else {
  283. require_once "resources/header.php";
  284. require_once "resources/footer.php";
  285. }
  286. }
  287. }
  288. //set the time zone
  289. if (!isset($_SESSION["time_zone"]["user"])) { $_SESSION["time_zone"]["user"] = null; }
  290. if (strlen($_SESSION["time_zone"]["user"]) == 0) {
  291. //set the domain time zone as the default time zone
  292. date_default_timezone_set($_SESSION['domain']['time_zone']['name']);
  293. }
  294. else {
  295. //set the user defined time zone
  296. date_default_timezone_set($_SESSION["time_zone"]["user"]);
  297. }
  298. ?>