-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_driver.php
297 lines (211 loc) · 8.8 KB
/
add_driver.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
include('session.php');
include("config.php");
require __DIR__ . '/vendor/autoload.php';
function areAllParametersSet ($parametersArray)
{ $areSet = 1;
$parameterArrayLength = count ($parametersArray);
for($i = 0; $i < $parameterArrayLength; $i++) {
if (! isset ($_POST[$parametersArray[$i]])){$areSet = 0; break; }
}
return $areSet;
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
function send_mail($email,$message,$subject)
{
$mail = new PHPMailer;
$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = "smtp.gmail.com"; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username='[email protected]';
$mail->Password='kooora.com100plusfuck'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->SetFrom('[email protected]',"Driver");
$mail->AddReplyTo('[email protected]',"Driver");
$mail->Subject = $subject;
$mail->MsgHTML($message);
$mail->addAddress($email, '[email protected]');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject= $subject;
$mail->MsgHTML($message);
/*
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);*/
if(!$mail->send()) {
return $mail->ErrorInfo;
}else {
return 1;
}
}
function generateRandomCode($length, $keyspace = '0123456789abcdefghijklmnopqrstuvwxyz')
{
$str = '';
$max = mb_strlen($keyspace, '8bit') - 1;
for ($i = 0; $i < $length; ++$i) {
$str .= $keyspace[random_int(0, $max)];
}
return $str;
}
$error = "";
$emailError = $passwordError = $genderError = $phoneError = $carModelError = $carNumberError = $carYearError = $carColorError = $nameError = "";
$email = $password = $gender =$phone = $carModel = $carNumber = $carYear = $carColor = $name = "";
if($_SERVER["REQUEST_METHOD"] == "POST") {
// username and password sent from form
//echo $_POST['gender'];
// set the request excpected paramaters
$ExpectedParametersArray = array ('email','password','gender','fullname','phone','model','plate_number','year','color');
// check if all the paramaters are set
if (empty($_POST["email"])) {
$emailError = "Email is required";
} else {
$email = test_input($_POST["email"]);
}
if (empty($_POST["gender"])) {
$genderError = "gender is required";
} else {
$gender = test_input($_POST["gender"]);
}
if (empty($_POST["fullname"])) {
$nameError = "Name is required";
} else {
$name = test_input($_POST["fullname"]);
}
if (empty($_POST["phone"])) {
$phoneError = "Phone is required";
} else {
$phone = test_input($_POST["phone"]);
}
if (empty($_POST["model"])) {
$carModelError = "model is required";
} else {
$carModel = test_input($_POST["model"]);
}
if (empty($_POST["plate_number"])) {
$carNumberError = "plate number is required";
} else {
$carNumber = test_input($_POST["plate_number"]);
}
if ( $emailError == $passwordError && $passwordError == $genderError && $genderError == $phoneError &&
$phoneError == $carModelError && $carModelError == $carNumberError && $carNumberError == $carYearError &&
$carYearError == $carColorError && $carColorError == $nameError && $nameError =="")
//== $passwordError == $genderError == $phoneError == $carModelError == $carNumberError == $carYearError == $carColorError == $nameError == "")
{echo "we are good to go" ;
$continue =1;
if ($continue)
{
$driverStatement = $conn->prepare('SELECT * FROM drivers where email = ?');
$driverStatement->execute(array($email));
$numberOfRows = $driverStatement->fetchColumn();
if ($numberOfRows != 0) {
$continue =0;
$error= "some user has already registered with this email";
}
}
if ($continue){
$phoneStatement = $conn->prepare('SELECT * FROM drivers where phone = ?');
$phoneStatement->execute(array($phone));
$numberOfRows = $phoneStatement->fetchColumn();
if ($numberOfRows != 0) {
$continue = 0;
$error ='User already exist with this phone number';
}
}
if ($continue ){
$randomCode = generateRandomCode (6);
$message = ' welcome to Driver, we are blessed to have you here, your password is: ' . $randomCode;
//$continue = send_mail($email,$message,'welcome to Driver! ');
$hash = password_hash($randomCode, PASSWORD_DEFAULT);
$insertStatement = $conn->prepare('INSERT INTO `drivers`(`email`, `gender`, `fullname`, `password`,`phone` , `active`) VALUES(?,?,?,?,?,?)');
try {
$insertStatement->execute(array( $email,$gender , $name,$hash,$phone ,1));
$driverId =$conn->lastInsertId();
}catch(PDOException $ex) {
$continue =0;
$error = $ex->getMessage();
}
}
if ($continue == '1')
{
// add the car info
$addCarSql= 'INSERT INTO `cars`(`driverID`, `color`, `model`, `year`, `plateNumber`) VALUES (?,?,?,?,?)';
$addCarStmt = $conn->prepare($addCarSql);
try{
$addCarStmt->execute(array($driverId,$carColor,$carModel,$carYear,$carNumber));
}
catch (PDOException $ex)
{
$continue =0;
$error = $ex->getMessage();
echo $error;
}
}
if ($continue)
{
$email = "";
$phone = "";
$carNumber ="";
$carModel ="";
$carColor = "";
$carYear ="";
$error = 'driver has been succesfully added to the database';
}
}
}
?>
<html>
<head>
<title>add driver page </title>
<style type = "text/css">
body {
font-family:Arial, Helvetica, sans-serif;
font-size:14px;
}
label {
font-weight:bold;
width:100px;
font-size:14px;
}
.box {
border:#666666 solid 1px;
}
.error {color: #FF0000;}
</style>
</head>
<body bgcolor = "#FFFFFF">
<?php include('welcome.php'); ?>
<div align = "center">
<div style = "width:400px; border: solid 1px #333333; " align = "left">
<div style = "background-color:#D3D3D3; color:#000000; padding:3px;"><b>add a driver</b></div>
<div style = "margin:30px">
</b></b></b></b>
<form action = "" method = "post">
<label>email : </label></br><input type = "text" value = "<?php echo $email ;?>" name = "email" class = "box"/> <span class="error"><?php echo $emailError;?></span><br>
<label>gender : </label> <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">Female
<input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">Male
<span class="error">* <?php echo $genderError;?></span></br>
<label>full name : </label></br><input type = "text" name = "fullname" value = "<?php echo $name; ?>" class = "box" /><span class="error"><?php echo $nameError;?></span><br>
<label>phone : </label></br><input type = "text" name = "phone" value = "<?php echo $phone ;?>" class = "box" /><span class="error"><?php echo $phoneError;?></span><br>
<label>car model : </label></br><input type = "text" name = "model" value = "<?php echo $carModel ;?>" class = "box"/><span class="error"><?php echo $carModelError;?></span><br>
<label>car plate number : </label></br><input type = "text" name = "plate_number" value = "<?php echo $carNumber ;?>" class = "box" /><span class="error"><?php echo $carNumberError;?></span><br>
<label>car year : </label></br><input type = "text" name = "year" value = "<?php echo $carYear ;?>" class = "box" ><br />
<label>car color : </label></br><input type = "text" name = "color" value = "<?php echo $carColor ;?>" class = "box" /><br />
<input type = "submit" value = " Submit "/><br />
</form>
<div style = "font-size:11px; color:#cc0000; margin-top:10px"><?php echo $error; ?></div>
</div>
</div>
</div>
</body>
</html>