forked from jas-/phpDHCPAdmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.manage.groups.php
277 lines (238 loc) · 13.7 KB
/
admin.manage.groups.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?PHP
/*
* phpDHCPAdmin
* Jason Gerfen [[email protected]]
*
* admin.manage.groups.php - Manage groups
*/
// load our config data
if( file_exists( "scripts/inc.config.php" ) ) {
require 'scripts/inc.config.php';
// ensure we are being called from our configured host
if( $defined['hostname'] === $_SERVER['SERVER_NAME'] ) {
// Initialize classes
$db = new dbConn;
$err = new GenerateErrors;
$tpl = new Template;
$skin = new PageSkinner;
$val = new ValidateStrings;
$menu = new GenerateNavMenu;
$auth = new Authenticate;
$encrypt = new Encryption;
$level = new AccessLevels;
$misc = new MiscFunctions;
$debug = new DebugData;
$enc = new Encryption;
// initialize a db connection handle
$dbconn = $db->dbConnect( $defined['dbhost'], $defined['username'], $defined['password'], $defined['dbname'] );
// ensure our sessions are present
if( empty( $_SESSION['token'] ) ) {
$sessions = new dbSession;
}
//define the template and cache directories
$tpl->strTemplateDir = $defined['virpath'] . 'templates';
$tpl->strCacheDir = '/tmp';
if( ( !empty( $_GET ) ) || ( !empty( $_POST ) ) ) { $flag = "TRUE"; } else { $flag = "FALSE"; }
// setup our template style data
if( ( $val->ValidateString( $_GET['skin'] ) === -1 ) || ( empty( $_GET['skin'] ) ) ) {
$style = $defined['templates'] . "/black";
if( !empty( $_GET['skin'] ) ) {
$skin_err = $err->GenerateErrorLink( "help/help.php", "#val_xss", $defined['error_small'], $errors['val_xss'], NULL, NULL );
}
} else {
$style = $skin->SelectSkin( $defined['templates'], $_GET['skin'], $_COOKIE['skin'] );
}
// call our header file and pass it some variables
$tpl->assign( 'TITLE', $defined['title'], NULL, NULL );
$tpl->assign( 'DESCRIPTION', "Manage Groups", NULL, NULL );
$tpl->assign( 'STYLE', $style, NULL, NULL );
// authentication template
$FILE = "auth.tpl";
// default is no error just diplay login form
if( $auth->AuthUser( $_POST['user'], $_POST['pass'], $_SESSION['token'] ) === -1 ) {
// well looks like at least one login attempt has been processed, show empty field error
if( $_SESSION['x']++ >= 1 ) {
$ERROR = $err->GenerateErrorLink( "help/help.html", "#missing", $defined['error'], $errors['auth_e'], NULL, NULL );
}
// timeout with authentication token
} elseif( $auth->AuthUser( $_POST['user'], $_POST['pass'], $_SESSION['token'] ) === -2 ) {
$ERROR = $err->GenerateErrorLink( "help/help.html", "#timeout", $defined['error'], $errors['auth_to'], NULL, NULL );
$misc->ExitApplication( $_SESSION['token'] );
// error in validation of authentication data
} elseif( $auth->AuthUser( $_POST['user'], $_POST['pass'], $_SESSION['token'] ) === -3 ) {
$ERROR = $err->GenerateErrorLink( "help/help.html", "#alphanum", $defined['error'], $errors['val_alp'], NULL, NULL );
// authentication data not found in database
} elseif( $auth->AuthUser( $_POST['user'], $_POST['pass'], $_SESSION['token'] ) === -4 ) {
$ERROR = $err->GenerateErrorLink( "help/help.html", "#user", $defined['error'], $errors['auth_n'], NULL, NULL );
// error in database query
} elseif( $auth->AuthUser( $_POST['user'], $_POST['pass'], $_SESSION['token'] ) === -5 ) {
$ERROR = $err->GenerateErrorLink( "help/help.html", "#undef", $defined['error'], $errors['undef_sql'], NULL, NULL );
// valid user found
} elseif( $auth->AuthUser( $_POST['user'], $_POST['pass'], $_SESSION['token'] ) === 0 ) {
// perform permissions check with access level and group data
// decode our authentication token to get our group membership
$user_details = $encrypt->DecodeAuthToken( $_SESSION['token'] );
$group = base64_decode( $user_details[3] );
if( ( $level->ChkLevel( $_SESSION['token'] ) === "admin" ) && ( $group === "admin" ) ) {
// define some variables for the template etc.
$JS = NULL;
$FILE = "admin.manage.groups.tpl";
$group_name_err = "*";
$group_manager_err = "*";
$group_contact_err = "*";
$group_description_err = "*";
// initialize a db connection handle
$dbconn = $db->dbConnect( $defined['dbhost'], $defined['username'], $defined['password'], $defined['dbname'] );
// provide count of online users
$online = "SELECT * FROM `admin_sessions`";
$ret = $db->dbQuery( $val->ValidateSQL( $online, $dbconn ), $dbconn );
$usersoline = $db->dbNumRows( $ret );
// Look for a GET id post to edit existing dnssec keys
if( !empty( $_GET['id'] ) ) {
if( $val->ValidateInteger( $_GET['id'] ) === -1 ) {
$message = $err->GenerateErrorLink( "help/help.html", "#group_edit", $defined['error'], $errors['val_num'], NULL, NULL );
} else {
// populate the form with database information if already configured
$query = "SELECT * FROM `auth_groups` WHERE `id` = \"" . $_GET['id'] . "\" LIMIT 1";
if( ( $value = $db->dbQuery( $val->ValidateSQL( $query, $dbconn ), $dbconn ) ) === -1 ) { echo $query;
$message = $err->GenerateErrorLink( "help/help.html", "#group_edit", $defined['error'], $errors['db_select'], NULL, NULL );
} else {
$data = $db->dbArrayResultsAssoc( $value );
$group_id = $data[0]['id'];
$group_name = $data[0]['group'];
$group_manager = $data[0]['manager'];
$group_contact = $data[0]['contact'];
$group_description = $data[0]['description'];
$message = $err->GenerateErrorLink( "help/help.html", "#host_search", $defined['good'], "You are currently editing record #" . $group_id, NULL, NULL );
}
}
}
// check for form submission first
if( !empty( $_POST ) ) {
// setup our form variables
$group_name = $_POST['group_name'];
$group_manager = $_POST['group_manager'];
$group_contact = $_POST['group_contact'];
$group_description = $_POST['group_description'];
$group_id = $_POST['group_id'];
// check for empty variables
if( ( !empty( $group_name ) ) && ( !empty( $group_manager ) ) && ( !empty( $group_description ) ) && ( !empty( $group_contact ) ) ) {
// do some validation checks on submitted data
if( ( $val->ValidateParagraph( $group_name ) !== -1 ) && ( $val->ValidatePhone( $group_contact ) !== -1 ) && ( $val->ValidateParagraph( $group_description ) !== -1 ) && ( $val->ValidateParagraph( $group_manager ) !== -1 ) ) {
// setup our SQL statements for add, edit and deleting records
$insert = "INSERT INTO `auth_groups` ( `group`, `manager`, `contact`, `description` ) VALUES ( \"" . $group_name . "\", \"" . $group_manager . "\", \"" . $group_contact . "\", \"" . $group_description . "\" )";
$update = "UPDATE `auth_groups` SET `group` = \"" . $group_name . "\", `contact` = \"" . $group_contact . "\", `description` = \"" . $group_description . "\", `manager` = \"" . $group_manager . "\" WHERE `id` = \"" . $group_id . "\" LIMIT 1";
$delete = "DELETE FROM `auth_groups` WHERE `id` = \"" . $group_id . "\" LIMIT 1";
// now perform a check to see which statement to use
if( !empty( $_POST['AddGroup'] ) ) { $sql = $insert; }
if( !empty( $_POST['EditGroup'] ) ) { $sql = $update; }
if( !empty( $_POST['DelGroup'] ) ) { $sql = $delete; }
// begin processing our SQL object
if( ( $sql_res = $db->dbQuery( $val->ValidateSQL( $sql, $dbconn ), $dbconn ) ) === -1 ) {
if( eregi( "duplicate", $db->dbCatchError() ) ) {
$sql = $update;
$sql = $val->ValidateSQL( $sql, $dbconn );
if( ( $sql_res = $db->dbQuery( $sql, $dbconn ) ) === -1 ) {
$message = $err->GenerateErrorLink( "help/help.php", "#sql_error", $defined['error'], $errors['db_edit_err'], '600', '600' );
}
} else {
$message = $err->GenerateErrorLink( "help/help.php", "#db_insert", $defined['good'], $errors['db_edit'], '600', '600' );
}
} else {
$message = $err->GenerateErrorLink( "help/help.php", "#db_insert", $defined['good'], $errors['db_insert'], '600', '600' );
}
} else {
// create a reusable error link
$erlink = $err->GenerateErrorImg( $defined['error'], "help/help.html#val_par", "#val_par", '600', '600' );
$list = "<ol>";
// determine our validate errors
if( $val->ValidateParagraph( $group_name ) === -1 ) { $list .= "<li>Name field is invalid</li>"; $group_name_err = $erlink; }
if( $val->ValidatePhone( $group_contact ) === -1 ) { $list .= "<li>Contact field is invalid, phone number expected xxx-xxx-xxxx</li>"; $group_contact_err = $erlink; }
if( $val->ValidateParagraph( $group_description ) === -1 ) { $list .= "<li>Description field is invalid</li>"; $group_description_err = $erlink; }
if( $val->ValidateParagraph( $group_manager ) === -1 ) { $list .= "<li>Manager field is invalid</li>"; $group_manager_err = $erlink; }
$list .= "</ol>";
// give them an error message with link to the help file
$message = $err->GenerateErrorLink( "help/help.php", "#val_par", $defined['error'], $errors['val_par'] . $list, '600', '600' );
}
} else {
// create a reusable error link
$erlink = $err->GenerateErrorImg( $defined['error'], "help/help.html#val_empty", "#val_empty", '600', '600' );
// determine which fields are missing
$list = "<ol>";
if( empty( $group_name ) ) { $list .= "<li>Name field is missing data</li>"; $group_name_err = $erlink; }
if( empty( $group_contact ) ) { $list .= "<li>Contact field is missing data</li>"; $group_contact_err = $erlink; }
if( empty( $group_description ) ) { $list .= "<li>Description field is missing data</li>"; $group_description_err = $erlink; }
if( empty( $group_manager ) ) { $list .= "<li>Manager field is missing data</li>"; $group_manager_err = $erlink; }
$list .= "</ol>";
// give them an error message with link to the help file
$message = $err->GenerateErrorLink( "help/help.php", "#val_missing", $defined['error'], $errors['val_missing'] . $list, '600', '600' );
}
}
// get a list of current groups
if( count( $group_list ) === 0 ) {
$group_query = "SELECT * FROM `auth_groups` ORDER BY `group` ASC";
if( ( $current = $db->dbQuery( $val->ValidateSQL( $group_query, $dbconn ), $dbconn ) ) !== -1 ) {
$group_list = $db->dbArrayResultsAssoc( $current );
}
$group_list = $misc->GenJumpMenuBoxGROUPS( $group_list, 'group_list', $_GET['skin'] );
}
// assign our data to the template
$tpl->assign( 'message', $message, NULL, NULL );
$tpl->assign( 'group_id', $val->ValidateXSS( $group_id ), NULL, NULL );
$tpl->assign( 'group_list', $group_list, NULL, NULL );
$tpl->assign( 'group_name', $val->ValidateXSS( $group_name ), NULL, NULL );
$tpl->assign( 'group_manager', $val->ValidateXSS ($group_manager ), NULL, NULL );
$tpl->assign( 'group_contact', $val->ValidateXSS( $group_contact ), NULL, NULL );
$tpl->assign( 'group_description', $val->ValidateXSS( $group_description ), NULL, NULL );
// and the corresponding errors if any
$tpl->assign( 'group_list_err', $group_list_err, NULL, NULL );
$tpl->assign( 'group_name_err', $group_name_err, NULL, NULL );
$tpl->assign( 'group_manager_err', $group_manager_err, NULL, NULL );
$tpl->assign( 'group_contact_err', $group_contact_err, NULL, NULL );
$tpl->assign( 'group_description_err', $group_description_err, NULL, NULL );
// Do some cleaning before leaving
$misc->CleanUpVars( $_POST, NULL );
// Perform analyze, repair and optimize on used tables
$db->dbFixTable( "auth_groups", $dbconn );
// Free db handle and close connection(s)
$db->dbFreeData( $dbconn );
$db->dbCloseConn( $dbconn );
} else {
// page view restricted by access level
$ERROR = $err->GenerateErrorLink( "help/help.html", "#undef", $defined['error'], $errors['level'], NULL, NULL );
}
} else {
// general authentication error
$ERROR = $err->GenerateErrorLink( "help/help.html", "#undef", $defined['error'], $errors['auth_n'], NULL, NULL );
}
} else {
// Possible XSS attack
$ERROR = $err->GenerateErrorLink( "help/help.html", "#undef", $defined['error'], $errors['xss_config'], NULL, NULL );
}
} else {
// File is missing for configuration params
$ERROR = $err->GenerateErrorLink( "help/help.html", "#undef", $defined['error'], $errors['config_file'], NULL, NULL );
}
// include our header file
$tpl->assign( 'JS', $JS, NULL, NULL );
$tpl->display( 'header.tpl', $flag, NULL );
// determine our menu system
$MENU = $menu->CreateNav( $_SESSION['token'], $_GET['skin'] );
$tpl->assign( 'MENU', $tpl->assign( 'SKIN', preg_replace( '/templates\//', '', $style ), $MENU, $flag ), NULL, NULL );
// assign some vars to our main template
$tpl->assign( 'URL', $_SERVER['PHP_SELF'] . "?skin=" . $_GET['skin'], NULL, NULL );
$tpl->assign( 'IP_ADDRESS', $_SERVER['REMOTE_ADDR'], NULL, NULL );
$tpl->assign( 'ERROR', $ERROR, NULL, NULL );
$tpl->assign( 'STYLE', $style, NULL, NULL );
$tpl->assign( 'usersonline', "<b>Users:</b> " . $usersoline . " online", NULL, NULL );
// call our main body template data
$tpl->assign( 'DATA', $tpl->assign( NULL, NULL, $FILE, $flag ), NULL, NULL );
$tpl->display( 'main.tpl', $flag, NULL );
// call our footer file
$tpl->assign( 'DISCLAIMER', $defined['disclaimer'], NULL, NULL );
$tpl->assign( 'SKIN_MENU', $skin->GenSkinMenu( $_GET['skin'], $defined['templates'] ), NULL, NULL );
$tpl->assign( 'SKIN_MENU_ERR', $skin_err, NULL, NULL );
$tpl->display( 'footer.tpl', $flag, NULL );
// show some debugging if enabled
if( $defined['debug'] === "TRUE" ) { $debug->ShowDebug( $_GET, $_POST, $_REQUEST, $_SESSION ); }
?>