forked from fusionpbx/fusionpbx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_auth.php
262 lines (237 loc) · 9.55 KB
/
check_auth.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
/*
FusionPBX
Version: MPL 1.1
The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the
License.
The Original Code is FusionPBX
The Initial Developer of the Original Code is
Mark J Crane <[email protected]>
Portions created by the Initial Developer are Copyright (C) 2008-2016
the Initial Developer. All Rights Reserved.
Contributor(s):
Mark J Crane <[email protected]>
*/
//includes
require_once "resources/require.php";
//for compatability require this library if less than version 5.5
if (version_compare(phpversion(), '5.5', '<')) {
require_once "resources/functions/password.php";
}
//start the session
ini_set("session.use_only_cookies", True);
ini_set("session.cookie_httponly", True);
if ($_SERVER["HTTPS"] == "on") { ini_set("session.cookie_secure", True); }
if (!isset($_SESSION)) { session_start(); }
//define variables
if (!isset($_SESSION['login']['destination']['url'])) { $_SESSION['login']['destination']['url'] = null; }
if (!isset($_SESSION['template_content'])) { $_SESSION["template_content"] = null; }
//if the username session is not set the check username and password
if (strlen($_SESSION['username']) == 0) {
//clear the menu
$_SESSION["menu"] = "";
//clear the template only if the template has not been assigned by the superadmin
if (strlen($_SESSION['domain']['template']['name']) == 0) {
$_SESSION["template_content"] = '';
}
//if the username is not provided then send to login.php
if (strlen($_REQUEST["username"]) == 0 && strlen($_REQUEST["key"]) == 0) {
$target_path = ($_REQUEST["path"] != '') ? $_REQUEST["path"] : $_SERVER["REQUEST_URI"];
$_SESSION["message_mood"] = "negative";
$_SESSION["message"] = "Invalid Username and/or Password";
header("Location: ".PROJECT_PATH."/login.php?path=".urlencode($target_path));
exit;
}
//validate the username and password
$auth = new authentication;
if (strlen($_REQUEST["username"]) > 0) {
$auth->username = $_REQUEST["username"];
$auth->password = $_REQUEST["password"];
}
if (strlen($_REQUEST["key"]) > 0) {
$auth->key = $_REQUEST["key"];
}
$auth->debug = false;
$result = $auth->validate();
if ($result["authorized"] == "true") {
// set the session variables
$_SESSION["domain_uuid"] = $result["domain_uuid"];
$_SESSION["user_uuid"] = $result["user_uuid"];
// user session array
$_SESSION["user"]["username"] = $result["username"];
$_SESSION["user"]["user_uuid"] = $result["user_uuid"];
$_SESSION["user"]["contact_uuid"] = $result["contact_uuid"];
}
else {
//debug
if ($debug) {
echo "<pre>";
print_r($result);
echo "</pre>";
exit;
}
//log the failed auth attempt to the system, to be available for fail2ban.
openlog('FusionPBX', LOG_NDELAY, LOG_AUTH);
syslog(LOG_WARNING, '['.$_SERVER['REMOTE_ADDR']."] authentication failed for ".$result["username"]);
closelog();
//redirect the user to the login page
$target_path = ($_REQUEST["path"] != '') ? $_REQUEST["path"] : $_SERVER["PHP_SELF"];
$_SESSION["message_mood"] = "negative";
$_SESSION["message"] = "Invalid Username and/or Password";
header("Location: ".PROJECT_PATH."/login.php?path=".urlencode($target_path));
exit;
}
//get the groups assigned to the user and then set the groups in $_SESSION["groups"]
$sql = "SELECT * FROM v_group_users ";
//$sql .= "where domain_uuid='".$domain_uuid."' ";
//$sql .= "and user_uuid='".$_SESSION["user_uuid"]."' ";
$sql .= "where domain_uuid=:domain_uuid ";
$sql .= "and user_uuid=:user_uuid ";
$prep_statement = $db->prepare(check_sql($sql));
$prep_statement->bindParam(':domain_uuid', $_SESSION["domain_uuid"] );
$prep_statement->bindParam(':user_uuid', $_SESSION["user_uuid"]);
$prep_statement->execute();
$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
$_SESSION["groups"] = $result;
unset($sql, $row_count, $prep_statement);
//get the permissions assigned to the groups that the user is a member of set the permissions in $_SESSION['permissions']
if (count($_SESSION["groups"]) > 0) {
$x = 0;
$sql = "select distinct(permission_name) from v_group_permissions ";
foreach($_SESSION["groups"] as $field) {
if (strlen($field['group_name']) > 0) {
if ($x == 0) {
$sql .= "where (domain_uuid = '".$_SESSION["domain_uuid"]."' and domain_uuid = null) ";
}
else {
$sql .= "or (domain_uuid = '".$_SESSION["domain_uuid"]."' and domain_uuid = null) ";
}
$sql .= "or group_name = '".$field['group_name']."' ";
$x++;
}
}
$prep_statement_sub = $db->prepare($sql);
$prep_statement_sub->execute();
$result = $prep_statement_sub->fetchAll(PDO::FETCH_NAMED);
if (is_array($result)) {
foreach ($result as $row) {
$_SESSION['permissions'][$row["permission_name"]] = true;
}
}
unset($sql, $prep_statement_sub);
}
//get the user settings
$sql = "select * from v_user_settings ";
$sql .= "where domain_uuid = '" . $_SESSION["domain_uuid"] . "' ";
$sql .= "and user_uuid = '" . $_SESSION["user_uuid"] . "' ";
$sql .= "and user_setting_enabled = 'true' ";
$prep_statement = $db->prepare($sql);
if ($prep_statement) {
$prep_statement->execute();
$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
foreach ($result as $row) {
$name = $row['user_setting_name'];
$category = $row['user_setting_category'];
$subcategory = $row['user_setting_subcategory'];
if (strlen($row['user_setting_value']) > 0) {
if (strlen($subcategory) == 0) {
//$$category[$name] = $row['domain_setting_value'];
if ($name == "array") {
$_SESSION[$category][] = $row['user_setting_value'];
}
else {
$_SESSION[$category][$name] = $row['user_setting_value'];
}
} else {
//$$category[$subcategory][$name] = $row['domain_setting_value'];
if ($name == "array") {
$_SESSION[$category][$subcategory][] = $row['user_setting_value'];
}
else {
$_SESSION[$category][$subcategory][$name] = $row['user_setting_value'];
}
}
}
}
}
//get the extensions that are assigned to this user
if (file_exists($_SERVER["PROJECT_ROOT"]."/app/extensions/app_config.php")) {
if (isset($_SESSION["user"]) && isset($_SESSION["user_uuid"]) && $db && strlen($_SESSION["domain_uuid"]) > 0 && strlen($_SESSION["user_uuid"]) > 0 && count($_SESSION['user']['extension']) == 0) {
//get the user extension list
$_SESSION['user']['extension'] = null;
$sql = "select ";
$sql .= " e.extension, ";
$sql .= " e.number_alias, ";
$sql .= " e.user_context, ";
$sql .= " e.extension_uuid, ";
$sql .= " e.outbound_caller_id_name, ";
$sql .= " e.outbound_caller_id_number ";
$sql .= "from ";
$sql .= " v_extension_users as u, ";
$sql .= " v_extensions as e ";
$sql .= "where ";
$sql .= " e.domain_uuid = '".$_SESSION['domain_uuid']."' ";
$sql .= " and e.extension_uuid = u.extension_uuid ";
$sql .= " and u.user_uuid = '".$_SESSION['user_uuid']."' ";
$sql .= " and e.enabled = 'true' ";
$sql .= "order by ";
$sql .= " e.extension asc ";
$query = $db->query($sql);
if($query !== false) {
$result = $db->query($sql)->fetchAll(PDO::FETCH_ASSOC);
$x = 0;
foreach($result as $row) {
$destination = $row['extension'];
if (strlen($row['number_alias']) > 0) {
$destination = $row['number_alias'];
}
$_SESSION['user']['extension'][$x]['user'] = $row['extension'];
$_SESSION['user']['extension'][$x]['number_alias'] = $row['number_alias'];
$_SESSION['user']['extension'][$x]['destination'] = $destination;
$_SESSION['user']['extension'][$x]['extension_uuid'] = $row['extension_uuid'];
$_SESSION['user']['extension'][$x]['outbound_caller_id_name'] = $row['outbound_caller_id_name'];
$_SESSION['user']['extension'][$x]['outbound_caller_id_number'] = $row['outbound_caller_id_number'];
$_SESSION['user_context'] = $row["user_context"];
$x++;
}
}
}
}
//redirect the user
if (check_str($_REQUEST["rdr"]) !== 'n'){
$path = check_str($_POST["path"]);
if (isset($path) && !empty($path) && $path!="index2.php" && $path!="/install.php") {
header("Location: ".$path);
exit();
}
else if ($_SESSION['login']['destination']['url'] != '') {
header("Location: ".$_SESSION['login']['destination']['url']);
exit();
}
}
//get the domains
if (file_exists($_SERVER["PROJECT_ROOT"]."/app/domains/app_config.php")){
require_once "app/domains/resources/domains.php";
}
}
//set the time zone
if (!isset($_SESSION["time_zone"]["user"])) { $_SESSION["time_zone"]["user"] = null; }
if (strlen($_SESSION["time_zone"]["user"]) == 0) {
//set the domain time zone as the default time zone
date_default_timezone_set($_SESSION['domain']['time_zone']['name']);
}
else {
//set the user defined time zone
date_default_timezone_set($_SESSION["time_zone"]["user"]);
}
//hide the path unless logged in as a superadmin.
if (!if_group("superadmin")) {
$v_path_show = false;
}
?>