/
admin-usradd2.php
executable file
·258 lines (223 loc) · 8.91 KB
/
admin-usradd2.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
<?
#This program is copyright by Andre Coetzee email: ac@main.me
#and is licensed under the GPL v3
#
#
#
#
#Please add yourself to: http://www.accounting-123.com
#Developers, Software Vendors, Support, Accountants, Users
#
#
#The full software license can be found here:
#http://www.accounting-123.com/a.php?a=153/GPLv3
#
#
#
#
#
#
#
#
#
#
#
# admin-usradd.php :: Module to add users to the system
##
require ("settings.php");
if ($_POST) {
switch ($_POST["key"]) {
case "confirm":
$OUTPUT = confirmUser ($_POST);
break;
case "write":
$OUTPUT = writeUser ($_POST);
break;
default:
$OUTPUT = enterUser ();
}
}elseif(isset($_GET["err"])){
# get vars from _GET
foreach($_GET as $key => $value){
$$key = $value;
}
$OUTPUT = enterUser ($username,$err);
} else {
$OUTPUT = enterUser ();
}
require ("template.php");
##
# functions
##
# enter new user's details
function enterUser ($username="",$err="")
{
# connect to db
db_connect ();
$enterUser = "
<h3>Add new user to database</h3>
<table border=0 cellpadding='".TMPL_tblCellPadding."' cellspacing='".TMPL_tblCellSpacing."'>
<form action='".SELF."' method=post>
<input type=hidden name=key value=confirm>
$err
<tr><th>Field</th><th>Value</th></tr>
<tr class='bg-even'><td>Username</td><td><input type=text size=20 name=username value='$username'></td></tr>
<tr class='bg-odd'><td>Password</td><td><input type=password size=20 name=password> (MIN 6 Characters MAX 20 Characters)</td></tr>
<tr class='bg-even'><td>Confirm password</td><td><input type=password size=20 name=password2></td></tr>
<tr><td align=right colspan=2><input type=submit value='Confirm »'></td></tr>
</form>
</table>
<p>
<table border=0 cellpadding='".TMPL_tblCellPadding."' cellspacing='".TMPL_tblCellSpacing."'>
<tr><th>Quick Links</th></tr>
<script>document.write(getQuicklinkSpecial());</script>
<tr class='bg-odd'><td><a href='main.php'>Main Menu</a></td></tr>
</table>";
return $enterUser;
}
# confirm entered info
function confirmUser ($_POST)
{
# get vars
foreach ($_POST as $key => $value) {
$$key = $value;
}
# validate input
require_lib("validate");
$v = new validate ();
$v->isOk ($username, "string", 1, 20, "Invalid user name.");
$v->isOk ($password, "string", 6, 20, "Invalid password.");
$v->isOk ($password2, "string", 6, 20, "Invalid password 2.");
$v->pwMatch ($password, $password2, "Passwords do not match.");
# display errors, if any
if ($v->isError ()) {
$theseErrors = "";
$errors = $v->getErrors();
foreach ($errors as $e) {
$theseErrors .= "-".$e["msg"]."<br>";
}
$theseErrors = "<tr><td class=err colspan=2>$theseErrors</td></tr>
<tr><td colspan=2><br></td></tr>";
header("Location: ".SELF."?username=$username&err=$theseErrors");
exit;
}
# connect to db
db_connect ();
$confirmUser ="
<h3>Add user to database</h3>
<h4>Confirm entry</h4>
<table border=0 cellpadding='".TMPL_tblCellPadding."' cellspacing='".TMPL_tblCellSpacing."' width=300>
<form action='".SELF."' method=post>
<input type=hidden name=key value=write>
<input type=hidden name=username value='$username'>
<input type=hidden name=password value='$password'>
<input type=hidden name=password2 value='$password2'>
<tr><th>Field</th><th>Value</th></tr>
<tr class='bg-even'><td>Username</td><td>$username</td>
<tr class='bg-odd'><td>Password</td><td>*</td>
<tr><td colspan=2><br></td></tr>
<tr><th colspan=2>Select Department(s)</th></tr>";
// list scripts
db_connect();
$sql = "SELECT * FROM depts ORDER BY dept";
$rslt = db_exec($sql);
$i = 0;
while($dep = pg_fetch_array($rslt)){
$confirmUser .="<tr class='".bg_class()."'><td colspan=2><input type=checkbox name=perm[] value='$dep[deptid]'>$dep[dept]</td></tr>";
$i++;
}
$confirmUser .="
<tr><td align=right colspan=3><input type=submit value='Add user »'></td></tr>
</form>
</table>
<p>
<table border=0 cellpadding='".TMPL_tblCellPadding."' cellspacing='".TMPL_tblCellSpacing."'>
<tr><th>Quick Links</th></tr>
<script>document.write(getQuicklinkSpecial());</script>
<tr class='bg-odd'><td><a href='main.php'>Main Menu</a></td></tr>
</table>";
return $confirmUser;
}
# write user to db
function writeUser ($_POST)
{
# get vars
foreach ($_POST as $key => $value) {
$$key = $value;
}
# validate input
require_lib("validate");
$v = new validate ();
$v->isOk ($username, "string", 1, 20, "Invalid user name.");
$v->isOk ($password, "string", 6, 20, "Invalid password.");
$v->isOk ($password2, "string", 6, 20, "Invalid password 2.");
$v->pwMatch ($password, $password2, "Passwords do not match.");
# display errors, if any
if ($v->isError ()) {
$theseErrors = "";
$errors = $v->getErrors();
foreach ($errors as $e) {
$theseErrors .= "<li class=err>".$e["msg"];
}
$theseErrors .= "<p><input type=button onClick='JavaScript:history.back();' value='« Correct submission'>";
return $theseErrors;
}
# connect to db
db_connect ();
# exit if user exists
$sql = "SELECT username FROM users WHERE username='$username'";
$usrRslt = db_exec ($sql) or errDie ("Unable to check database for existing username.");
if (pg_numrows ($usrRslt) > 0) {
return "User, $username, already exists in database.";
}
# get md5 hash of password
$password = md5 ($password);
$sql = "INSERT INTO users (username, password) VALUES ('$username', '$password')";
$nwUsrRslt = db_exec ($sql) or errDie ("Unable to add user to database.");
# write defualt permissions
$Sql = "INSERT INTO userscripts (username, script) VALUES ('$username', 'new_con.php')";
$Ex = db_exec ($Sql) or errDie ("Unable to add user to database.");
$Sql = "INSERT INTO userscripts (username, script) VALUES ('$username', 'die_day.php')";
$Ex = db_exec ($Sql) or errDie ("Unable to add user to database.");
$Sql = "INSERT INTO userscripts (username, script) VALUES ('$username', 'req_gen.php')";
$Ex = db_exec ($Sql) or errDie ("Unable to add user to database.");
$Sql = "INSERT INTO userscripts (username, script) VALUES ('$username', 'die_one.php')";
$Ex = db_exec ($Sql) or errDie ("Unable to add user to database.");
$Sql = "INSERT INTO userscripts (username, script) VALUES ('$username', 'die_view.php')";
$Ex = db_exec ($Sql) or errDie ("Unable to add user to database.");
$Sql = "INSERT INTO userscripts (username, script) VALUES ('$username', 'index_die.php')";
$Ex = db_exec ($Sql) or errDie ("Unable to add user to database.");
$Sql = "INSERT INTO userscripts (username, script) VALUES ('$username', 'index_cons.php')";
$Ex = db_exec ($Sql) or errDie ("Unable to add user to database.");
$Sql = "INSERT INTO userscripts (username, script) VALUES ('$username', 'index_reqs.php')";
$Ex = db_exec ($Sql) or errDie ("Unable to add user to database.");
$Sql = "INSERT INTO userscripts (username, script) VALUES ('$username', 'view_req.php')";
$Ex = db_exec ($Sql) or errDie ("Unable to add user to database.");
# write permissions
if(isset($perm)){
foreach($perm as $key => $value){
$sql = "SELECT script FROM deptscripts WHERE dept = '$value'";
$depRs = db_exec($sql);
while($depscr = pg_fetch_array($depRs)){
$sql = "INSERT INTO userscripts (username, script) VALUES ('$username', '$depscr[script]')";
$nwUsrRslt = db_exec ($sql) or errDie ("Unable to add user to database.");
}
}
}
# status report
$writeUser ="
<table border=0 cellpadding='".TMPL_tblCellPadding."' cellspacing='".TMPL_tblCellSpacing."' width='50%'>
<tr><th>New user added to database</th></tr>
<tr class=datacell><td>New user, $username, was successfully added to Cubit.</td></tr>
</table>
<p>
<tr>
<table border=0 cellpadding='2' cellspacing='1'>
<tr><th>Quick Links</th></tr>
<tr bgcolor='#88BBFF'><td><a href='admin-usradd.php'>Add another user</a></td></tr>
<script>document.write(getQuicklinkSpecial());</script>
<tr bgcolor='#88BBFF'><td><a href='main.php'>Main Menu</a></td></tr>
</tr>";
return $writeUser;
}
?>