forked from Bkoech/gbvis
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate_users.php
executable file
·233 lines (195 loc) · 6.92 KB
/
create_users.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
<?php
//index.php
$page_title = "Create Users | GBV";
$current_page = "Create Users";
require_once 'includes/global.inc.php';
require_once('classes/dropdown.class.php');
if(isset($_SESSION['logged_in'])) {
//get the user object from the session
$user = unserialize($_SESSION['user']);
//initialize php variables used in the form
$lastname ="";
$username = "";
$password = "";
$user_group= "";
$password_confirm = "";
$email = "";
$error = "";
$NewUsersaved =false;
//check to see that the form has been submitted
if(isset($_POST['submit-form'])) {
//retrieve the $_POST variables
$date_created=date("Y-m-d");
$username = $_POST['username'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$user_group = $_POST['user_group'];
$sector = $_POST['sector'];
$password = $_POST['password'];
$password_confirm = $_POST['password-confirm'];
$email = $_POST['email'];
$mobile_phone = $_POST['mobile_phone'];
//initialize variables for form validation
$success = true;
$userTools = new UserTools();
//validate that the form was filled out correctly
//check to see if user name already exists
if($userTools->checkUsernameExists($username))
{
$error .= "That username is already taken.";
$success = false;
}
//check to see if passwords match
if($password != $password_confirm) {
$error .= "Passwords do not match.";
$success = false;
}
if($success){
//prep the data for saving in a new user object
$data['firstname'] = $firstname;
$data['lastname'] = $lastname;
$data['username'] = $username;
$data['user_group'] = $user_group;
$data['password'] = md5($password); //encrypt the password for storage
$data['email'] = $email;
$data['sector'] = $sector;
$data['join_date'] = $date_created;
$data['mobile_phon'] = $mobile_phone;
//create the new user object
$newUser = new User($data);
//save the new user to the database
$newUser->save(true);
echo "<script type='text/javascript'>alert('New user added successfully!')</script>";
}else{
echo "<script type='text/javascript'>alert('" . $error . "')</script>";
}
}
include "includes/Dash_header.php"; // TA:60:1
include "includes/topbar.php"; // TA:60:1
?>
<script type="text/javascript">
function validate(){
if(document.forms["form"]["firstname"].value == ''){
alert("First Name is required.");
return false;
}
if(document.forms["form"]["lastname"].value == ''){
alert("Last Name is required.");
return false;
}
if(document.forms["form"]["username"].value == ''){
alert("User name is required.");
return false;
}
var email = document.forms["form"]["email"].value;
if(email == ''){
alert("Email is required.");
return false;
}else{
var atpos = email.indexOf("@");
var dotpos = email.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=email.length) {
alert("Not a valid e-mail address");
return false;
}
}
if(document.forms["form"]["password"].value == ''){
alert("Password is required.");
return false;
}
if(document.forms["form"]["password"].value != document.forms["form"]["password-confirm"].value){
alert("Passwords do not match.");
return false;
}
return true;
}
function isNumberKey(e){
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
return false;
}
}
</script>
<div id="sidebar">
<div class="sidebar-nav">
<?php
include "includes/sidebar.php";
?>
</div>
</div>
<div id="main-content_with_side_bar">
<div id="content-body">
<div class="profile-data" align="left">
<form name="form" onsubmit="return validate();" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" >
<table width="700" style="">
<tr>
<td colspan="2" style="font:bold 21px arial; text-align:center; border-bottom:1px solid #eee; padding:5px 0 10px 0;">Add User</td></tr>
</tr>
<tr>
<td colspan="2" style="font:bold 21px arial; text-align:center; border-bottom:1px solid #eee; padding:5px 0 10px 0;"><p id="msgDsp" STYLE="color:#FF0000; font-size:14px; "></p>
</td></tr>
</tr>
<tr>
<td width="50%" class="table-td-input">First Name</td>
<td width="50%" class="table-td-input"><div id='t1'><input type="text" name="firstname" id="firstname" value="<?php echo $firstname; ?>" class=form_textbox_inputs></div></td>
</tr>
<tr>
<td width="50%" class="table-td-input">Last Name</td>
<td width="50%" class="table-td-input"><div id='t1'><input type="text" name="lastname" id="lastname" value="<?php echo $lastname; ?>" class=form_textbox_inputs></div></td>
</tr>
<tr>
<td width="50%" class="table-td-input">User name</td>
<td width="50%" class="table-td-input"><div id='t1'><input type="text" name="username" id="username" value="<?php echo $username; ?>" class=form_textbox_inputs></div></td>
</tr>
<tr>
<td width="50%" class="table-td-input">Email</td>
<td width="50%" class="table-td-input"><div id='t1'><input type="text" name="email" id="email" value="<?php echo $email; ?>" class=form_textbox_inputs></div></td>
</tr>
<tr>
<td width="50%" class="table-td-input">Mobile Phone No</td>
<td width="50%" class="table-td-input"><div id='t1'><input type="text" onkeypress='return isNumberKey(event)' name="mobile_phone" id="mobile_phone" value="<?php echo $mobile_phone; ?>" class=form_textbox_inputs></div></td>
</tr>
<tr>
<td width="50%" class="table-td-input">User group</td>
<?php
$ddl=new dropdown();
?>
<td width="50%" class="table-td-input"><div id='s2'><?php $ddl->dropdown('user_groups'); ?></div></td>
</tr>
<tr>
<td width="50%" class="table-td-input">Sectors</td>
<?php
$ddl=new dropdown();
?>
<td width="50%" class="table-td-input"><div id='s3'><?php $ddl->dropdown('sectors'); ?></div></td>
</tr>
<tr>
<td width="50%" class="table-td-input">Password</td>
<td width="50%" class="table-td-input"><div id='t1'><input type="password" name="password" id="password" value="<?php echo $password; ?>" class="form_textbox_inputs"></div></td>
</tr>
<tr>
<td width="50%" class="table-td-input">Confirm password</td>
<td width="50%" class="table-td-input"><div id='t1'><input type="password" name="password-confirm" id="password-confirm" value="<?php echo $password_confirm; ?>" class="form_textbox_inputs"></div></td>
</tr>
<tr>
<td class="table-td-label"></td>
<td width="50%" style=" padding:5px;">
<input type="submit" onClick="" value=Save name="submit-form" class="gbvis_general_button">
<input type="reset" name="Reset" id="" value="Reset" style="" class="gbvis_general_button">
</td>
</tr>
</table>
</form>
<br clear="all">
<br clear="all">
</div><br clear="all">
</div>
</div>
<?php
include "includes/footer.php";
}
else
{
//Redirect user back to login page if there is no valid session created
header("Location: login.php");
}
?>