-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
329 lines (313 loc) · 9.53 KB
/
Copy pathindex.php
File metadata and controls
329 lines (313 loc) · 9.53 KB
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
/*
@Auther: Gabriel Rodriguez
Page: Index
Project: Collaborate 2017-2018
Date: 3/6/2018
Index page displays introduction images to user within a "Poloroid" type
of container. There are options to register as a new user as well as a signhp
area
*/
include ("./inc/header.php");
//prevent users from going to the index page without logging out
if (!isset($_SESSION["username"])) {
echo "";
}
else
{
echo "<meta http-equiv='refresh' content='0; url='inc/home.php'>";
}
// define variables and set to empty values
$firstN = $lastN = $password = $password2 = $email = $email2 = $userN = $user_check = $user_check2 = $email_check = $email_check2 = $date = "";
$fnameErr = $lnameErr = $userErr = $emailErr = $email2Err = $passErr = $pass2Err = "";
$userLogErr = $passLogErr = $md5password_login = $sql = $password_login = $userCount = $row = $id = "";
$date = date("Y-m-d"); // Year - Month - Day
//POST data from user registration or log in
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$con = Connect();
if(isset($_POST["reg"]))
{
if (empty($_POST["fname"]))
{
$fnameErr = "Name is required";
}
else
{
$firstN = test_input($_POST["fname"]);
}
if (empty($_POST["lname"]))
{
$lnameErr = "Name is required";
}
else
{
$lastN = test_input($_POST["lname"]);
}
if (empty($_POST["uname"]))
{
$userErr = "Username is required";
}
else
{
$userN= test_input($_POST["uname"]);
}
if (empty($_POST["email"]))
{
$emailErr = "Email is required";
}
else
{
$email = test_input($_POST["email"]);
}
if (empty($_POST["email2"]))
{
$email2Err = "Email is required";
}
else
{
// Remove all illegal characters from email
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
// Validate e-mail
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false)
{
$email2 = test_input($_POST["email2"]);
}
}
if (empty($_POST["password"]))
{
$passErr = "Password is required";
}
else
{
$password = test_input($_POST["password"]);
}
if (empty($_POST["password2"]))
{
$pass2Err = "Password verification is required";
}
else
{
$password2 = test_input($_POST["password2"]);
}
if ($email == $email2)
{
// Check if user already exists
$user_check = mysqli_query($con, "SELECT `username` FROM `collaborate`.`users` WHERE `username`='$userN'");
//Check whether Email already exists in the database
$email_check = mysqli_query($con, "SELECT `email` FROM `collaborate`.`users` WHERE `email`='$email'");
if (!$user_check2 = mysqli_fetch_assoc($user_check))
{
if (!$email_check2 = mysqli_fetch_assoc($email_check))
{
// check that passwords match
if ($password === $password2)
{
// check the maximum length of username/first name/last name does not exceed 25 characters
if (strlen($userN) > 25 || strlen($firstN) > 25 || strlen($lastN) > 25)
{
$userLogErr = "The maximum amount of characters has been exceeded";
}
else
{
// check the maximum length of password does not exceed 25 characters and is not less than 5 characters
if (strlen($password) > 30 || strlen($password2) < 5)
{
$userLogErr = "Your password must be between 5 and 30 characters long!";
}
else
{
//encrypt password and password 2 using md5 before sending to database
$password = md5($password);
$mysqli_query = "INSERT INTO `collaborate`.`users` (`username`, `first_name`, `last_name`, `email`, `password`, `sign_up_date`, `activated`) VALUES ('$userN', '$firstN', '$lastN', '$email', '$password', '$date', '1')";
if(mysqli_query ($con, $mysqli_query))
{
SESSION_START();
$username = $userN;
$_SESSION['username'] = $username;
header("Location: ./inc/user_form.php");
}
}
}
}
else
{
$passErr = "Your passwords do not match.";
}
}
else
{
$emailErr = "An account exists with that email address.";
}
}
else
{
$userErr = "That username has been taken.";
}
}
else
{
$email2Err = "Your E-mails do not match.";
}
}
else
{
if (empty($_POST["user_login"]))
{
$userLogErr = "Username is required.";
}
else
{
$user_login = test_input($_POST["user_login"]);
}
if (empty($_POST["password_login"]))
{
$passLogErr = "Password is a required.";
}
else
{
$password_login = test_input($_POST["password_login"]);
}
$md5password_login = md5($password_login);
//query database for user
$sql = "SELECT `username` FROM `collaborate`.`users` WHERE `username`='$user_login' AND `password`='$md5password_login' AND `activated`='0'";
$query = "SELECT `username` FROM `collaborate`.`users` WHERE `username`='$user_login' AND `password`='$md5password_login' AND `activated`='1'";
//return username if account is active and exists
$user = SetUser($sql);
if($user)//if user is activated and credentials match login
{
$_SESSION['username'] = $user;
header("Location: ./inc/home.php");
}
else//check user to see if the account is de-activated and return username, direct them to register
if($check_Active = Activated($query))
{
$_SESSION['username'] = $check_Active;
header("Location: ./inc/user_form.php");
}
else
{
$userLogErr = "OOPS... It looks like the email or password you've provided doesn't match a user in our system. Please verify your information and try again.";
}
}
}
//see if the user has an account that was started and not activated
function Activated($query)
{
try
{
$con = Connect();
if($mysqli_query = mysqli_query ($con, $query))
{
while($row = mysqli_fetch_assoc($mysqli_query))
{
$username = $row["username"];
}
return $username;
}
else
{
return false;
}
}
catch (Exception $e)
{
return false;
//TODO add log for catch statement
}
finally
{
$con = NULL;
}
}
/*
START login DATA
*/
function SetUser($sql)
{
$username = "";
try
{
//open database connection
$con = Connect();
//mysqli_query database
if($mysqli_query = mysqli_query ($con, $sql))
{
while($row = mysqli_fetch_assoc($mysqli_query))
{
$username = $row["username"];
}
if($username != "")
{
//get users profile_pictures
$check_pic = mysqli_query ($con, "SELECT `user_picture` FROM `collaborate`.`profile_pictures` WHERE `user`='$username'");
$get_pic_row = mysqli_fetch_assoc($check_pic);
$profile_pic_db = $get_pic_row['user_picture'];
if($profile_pic_db != "")
{
$get_user_pic = $profile_pic_db;
}
else
{
$get_user_pic = "";
}
return $username;
}
}
else
{
return false;
}
}
catch (Exception $e)
{
return false;
//TODO add log for catch statement
}
finally
{
$con = NULL;
}
}
/*
END login DATA
*/
?>
<!--
Registration start located on the right of screen
-->
<div id="join">
<span class="err"><?php echo $userLogErr;?></span>
<table >
<tr>
<td >
<h2 id="h2sign">Sign Up Below or<span id="in" onclick="SignIn()">Sign In</span></h2>
<form id="signIn" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
<input type="text" name="user_login" size="50" placeholder="Username" autocomplete="on"><br /><br />
<input type="password" name="password_login" size="50" placeholder="Password" ><br /><br />
<input type="submit" name="log" value="Login" >
</form>
<form id="signUp" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST" >
<input type="text" name="fname" placeholder="First Name" autocomplete="on"><span class="err"><?php echo $fnameErr;?></span><br /><br />
<input type="text" name="lname" placeholder="Last Name" autocomplete="on"><span class="err"><?php echo $lnameErr;?></span><br /><br />
<input type="text" name="uname" placeholder="Username" autocomplete="on"><span class="err"><?php echo $userErr;?></span><br /><br />
<input type="text" name="email" placeholder="Email Address" autocomplete="on"><span class="err"><?php echo $emailErr;?></span><br /><br />
<input type="text" name="email2" placeholder="Re-Enter Email" autocomplete="off"><span class="err"><?php echo $email2Err;?></span><br /><br />
<input type="password" name="password" placeholder="Password" autocomplete="on"><span class="err"><?php echo $passErr;?></span><br /><br />
<input type="password" name="password2" placeholder="Password Verification" autocomplete="off"><span class="err"><?php echo $pass2Err;?></span><br /><br />
<input type="submit" name="reg" value="Sign Up!" >
<input type="reset">
</form>
</td>
</tr>
</table>
</div>
<div class="bgstyleIndex">
<p>Where ideas bring us together to
meet like-minded individuals helping you to achieve your creative dreams.
Join Collaborate and bring your project to life.
</p>
</div>
<?php
_html_end();
?>