/
index.php
284 lines (271 loc) · 9.32 KB
/
index.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
<?php
session_start();
include($_SERVER['DOCUMENT_ROOT'].'/chronosynk/includes/top.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>ChronoSynk</title>
<script type="text/javascript">
function validateUsername(input)
{
var usernameRegex = /^[a-zA-Z0-9_-]{3,20}$/;
var valid = input.value.match(usernameRegex);
if (valid)
document.getElementById('usernameError').innerHTML = '';
else
document.getElementById('usernameError').innerHTML = '<img alt="" src="/chronosynk/media/images/x.png">letters, numbers, -, and _ 3 - 20 characters';
};
</script>
</head>
<body>
<?php
if ($_POST)
{
//register code (processing)
if (isset($_POST['register']))
{
$valid = true;
$len = strlen($_POST['rusername']);
$_SESSION['registerError'] = '';
if ($len > 20 || $len < 3)
{
$_SESSION['registerError'] .= 'Username must be 3-20 characters<br/>';
$valid = false;
}
if (!preg_match("/^[a-zA-Z0-9_-]{3,20}$/", $_POST['rusername']))
{
$_SESSION['registerError'] .= 'Username can only contain a-z, 0-9, -, and _<br/>';
$valid = false;
}
if (!filter_var($_POST['remail'], FILTER_VALIDATE_EMAIL))
{
$_SESSION['registerError'] .= 'Email is not valid<br/>';
$valid = false;
}
if (strcmp($_POST['rpassword1'], $_POST['rpassword2']) != 0)
{
$_SESSION['registerError'] .= 'Passwords do not match<br/>';
$valid = false;
}
else if (strlen($_POST['rpassword1']) < 6)
{
$_SESSION['registerError'] .= 'Password must be 6+ characters<br/>';
$valid = false;
}
if (empty($_POST['policies']))
{
$_SESSION['registerError'] .= 'You must agree to ChronoSynk\'s Policies<br/>';
$valid = false;
}
//connect to DB
$dbConnection = new mysqli("localhost", "chronoRead", "password", "chronosynk");
//start prepared statement
$stmt = $dbConnection->prepare("select * from user where username = ? or email = ?");
//bind variables
$stmt->bind_param('ss', $_POST['rusername'], $_POST['remail']);
//execute prepared statement
$executed = $stmt->execute();
$result = $stmt->get_result();
$usertaken = false;
$emailtaken = false;
while ($row = $result->fetch_assoc())
{
if (strcmp($row['username'], $_POST['rusername']) == 0)
{
$usertaken = true;
}
if (strcmp($row['email'], $_POST['remail']) == 0)
{
$emailtaken = true;
}
}
if ($emailtaken)
{
$_SESSION['registerError'] .= 'Email is already registered<br/>';
$valid = false;
}
if ($usertaken)
{
$_SESSION['registerError'] .= 'Username taken<br/>';
$valid = false;
}
if ($valid)
{
//connect to DB
$dbConnection = new mysqli("localhost", "chronoWrite", "password", "chronosynk");
//start prepared statement
$stmt = $dbConnection->prepare("INSERT INTO user (username, email, password, emailVerify) VALUES (?, ?, ?, ?)");
//initialize variables
$rusername = $_POST['rusername'];
$remail = $_POST['remail'];
//generate salt
$salt = mcrypt_create_iv(16, MCRYPT_DEV_URANDOM);
//salt password and hash 1000 times then salt again
$hashedpassword = $salt.hash_pbkdf2("sha256", $_POST['rpassword1'], $salt, 1000);
//bind variables
$emailVerify = md5(mcrypt_create_iv(16, MCRYPT_DEV_URANDOM));
$stmt->bind_param('ssss', $rusername, $remail, $hashedpassword, $emailVerify);
//execute prepared statement
$executed = $stmt->execute();
//if statement was successful. log new user in by setting session variables (used by header)
if ($executed)
{
echo 'executed';
$_POST['username'] = $_POST['rusername'];
$_POST['password'] = $_POST['rpassword1'];
$_POST['login'] = 1;
$headers = 'Content-type: text/html; charset=utf-8'."\r\n";
$headers .= "From: ChronoSynk do-not-reply@chronosynk.com";
$mailed = mail('gaf3@pct.edu',//$_POST['remail'],
'Welcome To ChronoSynk",
"Click <a href="127.0.0.1/chronosynk/email/?x='.$emailVerify.'">here to verify your email or type this URL into your browser: 127.0.0.1/chronosynk/email/?x='.$emailVerify.'',
$headers);
}
}
}
}
include('/includes/header.php');
//if user is logged in
if(isset($_SESSION['username']))
{
$results = false;
//connect to DB
$dbConnection = new mysqli("localhost", "chronoRead", "password", "chronosynk");
//prepare statement
//find new friends, new sessions, new comments, updated sessions?, new participants,
//get comments
$stmt = $dbConnection->prepare('
select commentID, text, u1.username as poster, u2.username as target, u1.userid as posterID, u2.userid as targetID, comment.timestamp, "comment"
from comment
join user as u1
on comment.userid = u1.userid
join user as u2
on comment.parentid = u2.userid
where comment.userid in
(select user.userid
from user
where user.userid in
(select toid
from friend
where (fromid = ?) and status = 1)
or user.userid in
(select fromid
from friend
where toid = ? and status = 1))
order by comment.timestamp desc');
$stmt->bind_param('ii', $_SESSION['userID'], $_SESSION['userID']);
$executed = $stmt->execute();
$result = $stmt->get_result();
$i = 0;
//store comments
while ($row = $result->fetch_assoc())
{
$results[$i] = $row;
$i++;
}
//get sessions
$stmt = $dbConnection->prepare('
select session.timestamp, title, username, "session", userid, sessionid
from session
join user
on user.userid = session.leader
where user.userid in
(select user.userid
from user
where user.userid in
(select toid
from friend
where (fromid = ?) and status = 1)
or user.userid in
(select fromid
from friend
where toid = ? and status = 1))
order by session.timestamp;');
$stmt->bind_param('ii', $_SESSION['userID'], $_SESSION['userID']);
$stmt->execute();
$result = $stmt->get_result();
//store sessions
while ($row = $result->fetch_assoc())
{
$results[$i] = $row;
$i++;
}
//sort
$sorted = false;
//while its not sorted
while (!$sorted)
{
$sorted = true;
for ($i = 0; $i < count($results) - 1; $i++)
{
//if the current time is older than the next time, swap them
if ($results[$i]['timestamp'] < $results[$i + 1]['timestamp'])
{
$temp = $results[$i];
$results[$i] = $results[$i + 1];
$results[$i + 1] = $temp;
//items were swapped so array is not sorted yet
$sorted = false;
}
}
}
//print activity
for ($i = 0; $i < count($results) && $results; $i++)
{
echo '<div class="activity">';
$row = $results[$i];
if (!empty($row['comment']))
{
echo '<p><a href="/chronosynk/user/?user='.$row['posterID'].'">'.$row['poster'].'</a> commented on <a href="/chronosynk/user/?user='.$row['targetID'].'">'.$row['target'].'\'s</a> profile</p>
<p>'.htmlspecialchars($row['text']).'</p>';
}
else if (!empty($row['session']))
{
echo '<a href="/chronosynk/user/?user='.$row['userid'].'">'.$row['username'].
'</a> created the session <a href="/chronosynk/sessions/view/?session='.$row['sessionid'].'">"'.$row['title'].'"</a>';
}
echo '</div>';
}
if (!$results)
{
echo '<p>No recent activity</p>';
}
//print news feed items
/*while ($row = $result->fetch_assoc())
{
echo '<div style="border: thin black solid;">
<p>'.$row['poster'].' commented on '.$row['target'].'\'s profile</p>
<p>'.htmlspecialchars($row['text']).'</p>
</div>';
}*/
}
//if user isnt logged in
else
{
//register form
if (empty($_POST['rusername'])) $_POST['rusername'] = '';
if (empty($_POST['remail'])) $_POST['remail'] = '';
if (empty($_SESSION['registerError'])) $_SESSION['registerError'] = '';
echo '
<div id="register" style="text-align: right">
<h2>Register</h2>
<form method="POST" style="text-align:right">
<label class="registerInput" maxlength="20" id="usernameError">'.$_SESSION['registerError'].'</label><br/>
<input class="registerInput" name="rusername" oninput="validateUsername(this);" id="rusername" placeholder="Username" type="text" value="'.$_POST['rusername'].'"><br/>
<input class="registerInput" maxlength="50" name="remail" id="remail" placeholder="Email" type="text" value="'.$_POST['remail'].'"><br/>
<input class="registerInput" name="rpassword1" id="rpassword1" placeholder="Password" type="password"><br/>
<input class="registerInput" name="rpassword2" id="rpassword2" placeholder="Re-Enter Password" type="password"><br/>
<input name="policies" type="checkbox">I Agree to Chronosynk\'s <a href="/chronosynk/policies/">policies</a><br/>
<input name="register" type="hidden" value="1"/>
<button type="submit">Register</button>
</form>
</div>
<div id="welcomeImageDiv"><img id="welcomeImage" alt="Welcome Image" src="media/images/welcome_image.jpg"/></div>';
unset($_SESSION['registerError']);
}
include('/includes/footer.php');
?>
</body>
</html>