-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile.php
497 lines (443 loc) · 14.9 KB
/
profile.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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
<?php
// We need to use sessions, so you should always start sessions using the below code.
session_start();
// Include GitHub API config file
require_once 'require/githubConfig.php';
// Include and initialize user class
require_once 'require/github.user.class.php';
$user = new User();
// If the user is not logged in redirect to the login page...
if (!isset($_SESSION['loggedin'])) {
$none = 'block';
$logi = 'none';
$usern = 'none';
$OUT1 = 'none';
} else {
$none = 'none';
$logi = $_SESSION['loggedin'];
$usern = $_SESSION['username'];
$OUT1 = 'none';
}
if(isset($accessToken)){
// Get the user profile info from Github
$gitUser = $gitClient->apiRequest($accessToken);
if(!empty($gitUser)){
// User profile data
$gitUserData = array();
$gitUserData['oauth_provider'] = 'github';
$gitUserData['oauth_uid'] = !empty($gitUser->id)?$gitUser->id:'';
$gitUserData['name'] = !empty($gitUser->name)?$gitUser->name:'';
$gitUserData['username'] = !empty($gitUser->login)?$gitUser->login:'';
$gitUserData['email'] = !empty($gitUser->email)?$gitUser->email:'';
$gitUserData['location'] = !empty($gitUser->location)?$gitUser->location:'';
$gitUserData['picture'] = !empty($gitUser->avatar_url)?$gitUser->avatar_url:'';
$gitUserData['link'] = !empty($gitUser->html_url)?$gitUser->html_url:'';
// Insert or update user data to the database
$userData = $user->checkUser($gitUserData);
// Put user data into the session
$_SESSION['userData'] = $userData;
$OUT1 = 'none';
}else{
$OUT1 = 'block';
$output = '<h3 style="color:red">Some problem occurred, please try again.</h3>';
}
$none = 'none';
$PROP = 'block';
$IN = 'none';
$OUT = 'block';
}elseif(isset($_GET['code'])){
// Verify the state matches the stored state
if(!$_GET['state'] || $_SESSION['state'] != $_GET['state']) {
header("Location: ".$_SERVER['PHP_SELF']);
}
// Exchange the auth code for a token
$accessToken = $gitClient->getAccessToken($_GET['state'], $_GET['code']);
$_SESSION['access_token'] = $accessToken;
header('Location: ./');
}else{
// Generate a random hash and store in the session for security
$_SESSION['state'] = hash('sha256', microtime(TRUE) . rand() . $_SERVER['REMOTE_ADDR']);
// Remove access token from the session
unset($_SESSION['access_token']);
// Get the URL to authorize
$loginURL = $gitClient->getAuthorizeURL($_SESSION['state']);
// Render Github login button
$output2 = '<a type="button" href="'.htmlspecialchars($loginURL).'" class="col-1" style="color: rgb(51, 51, 51);"><i class="fab fa-2x fa-github"></i></a>';
$PROP = 'none';
$OUT = 'none';
}
require('require/serverconnect.php');
if (mysqli_connect_errno()) {
die ('Failed to connect to MySQL: ' . mysqli_connect_error());
}
// We don't have the password or email info stored in sessions so instead we can get the results from the database.
$stmt = $con->prepare('SELECT name, password, email, date FROM accounts WHERE id = ?');
// In this case we can use the account ID to get the account info.
$stmt->bind_param('i', $_SESSION['id']);
$stmt->execute();
$stmt->bind_result($name, $password, $email, $date);
$stmt->fetch();
$stmt->close();
if (!isset($_SESSION['loggedin'])) {
$PROP = 'none';
$OUT = 'none';
} else {
$PROP = 'block';
$IN = 'none';
$OUT = 'block';
}
if(isset($accessToken)){
$PROP = 'block';
$IN = 'none';
$OUT = 'block';
}
$id = $_SESSION['id'];
// read data from collumn profile_pic from accounts table only at id=11
$sql = "SELECT profile_pic FROM accounts WHERE id='$id'";
$result = $con->query($sql);
// output data of each row
while($row = $result->fetch_assoc()) {
$prof_pic = $row["profile_pic"];
}
require "include/header.php";
?>
<body class="loggedin">
<style>
* {
font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";
}
body {
background-color: #435165;
}
.register {
width: 400px;
background-color: #ffffff;
box-shadow: 0 0 9px 0 rgba(0, 0, 0, 0.3);
margin: 100px auto;
}
.register h1 {
text-align: center;
color: #5b6574;
font-size: 24px;
padding: 20px 0 20px 0;
border-bottom: 1px solid #dee0e4;
}
.register form {
display: flex;
flex-wrap: wrap;
justify-content: center;
padding-top: 20px;
}
.register form label {
display: flex;
justify-content: center;
align-items: center;
width: 50px;
height: 50px;
background-color: #3274d6;
color: #ffffff;
}
.register form input[type="password"], .register form input[type="text"], .register form input[type="email"] {
width: 310px;
height: 50px;
border: 1px solid #dee0e4;
margin-bottom: 20px;
padding: 0 15px;
}
.register form input[type="submit"] {
width: 100%;
padding: 15px;
margin-top: 20px;
background-color: #3274d6;
border: 0;
cursor: pointer;
font-weight: bold;
color: #ffffff;
transition: background-color 0.2s;
}
.register form input[type="submit"]:hover {
background-color: #2868c7;
transition: background-color 0.2s;
}
body.loggedin {
background-color: #f3f4f7;
}
.content {
width: 1000px;
margin: 0 auto;
}
.content h2 {
margin: 0;
padding: 25px 0;
font-size: 22px;
border-bottom: 1px solid #e0e0e3;
color: #4a536e;
}
.content > p, .content > div {
box-shadow: 0 0 5px 0 rgba(0, 0, 0, 0.1);
margin: 25px 0;
padding: 25px;
background-color: #fff;
}
.content > p table td, .content > div table td {
padding: 5px;
}
.content > p table td:first-child, .content > div table td:first-child {
font-weight: bold;
color: #4a536e;
padding-right: 15px;
}
.content > div p {
padding: 5px;
margin: 0 0 10px 0;
}
@media only screen and (max-width: 790px) {
.content {
width: auto;
margin: 0 auto;
padding-left: 25px;
padding-right: 25px;
}
.cpca {
}
}
.cpca {
display:<?=$OUT?>;
}
}
</style>
<?php
$trangcanhan = 'active';
require "include/navbar.php";
?>
<div class="content">
<h2>Trang cá nhân</h2>
<div>
<div>
<div class="wrapper" style='display:<?=$OUT1?>'><?php echo $output; ?></div></div>
<style type="text/css">
#tunnna {
width: 150px;
height: 150px;
}
#daubuoi{
width: 30%;
}
@media screen and (max-width: 768px){
#tunnna {
width: 70px;
height: 70px;
}
#daubuoi{
width: 60%;
}
</style>
<table>
<?php
if(isset($accessToken)){
$usern = 'none';
} elseif ($logi) {
$query = "SELECT * FROM accounts WHERE username='".$usern."'";
$results = mysqli_query($con, $query);
if (mysqli_num_rows($results) !=0) {
while($row = mysqli_fetch_assoc($results)){
$login = 'block';
if(@$_GET['id']){
$login = 'none';
}
echo "<div style='display:$login'>";
echo "<div><img id='tunnna' src='".$row["profile_pic"]."' style='margin-bottom: 15px;float: left; border-radius: 50%; margin-right: 25px'></div>";
echo "<h1 style='margin-top: 0px;display:inline'>".$row["username"]." <i id='tunganh' data-toggle='tooltip' title='Tài khoản đã xác minh' style='color:#07f;font-size:22px;display:none;display:inline' class='fas fa-check-circle'></i></h1>";
?>
<h4 id="xk" style="display: none;color: #989696">Xung kích</h4>
<h4 id="qtv" style="display: none;color: #989696">Quản trị viên</h4>
<script>
var veri = '<?php echo $row["verified"] ?>';
var perm = '<?php echo $row["permission"] ?>';
if (veri == 'yes'){
document.getElementById("tunganh").style.display = "inline";
} else {
document.getElementById("tunganh").style.display = "none";
}
if (perm == 'xungkich'){
document.getElementById("xk").style.display = "block";
}
if (perm == 'admin'){
document.getElementById("qtv").style.display = "block";
}
$(document).ready(function(){
$('[data-toggle="tooltip"]').tooltip();
});
</script>
<?php
echo "<tr style='display:$login'><td>Họ và tên :</td> <td>".$row["name"]."</td>";
echo "<tr style='display:$login'><td>Ngày tham gia :</td> <td>".$row["date"]."</td>";
echo "<tr style='display:$login'><td>ID : </td><td>".$row["id"]."</td>";
echo "<tr style='display:$login'><td>Email : </td><td>". $row["email"]."</td>";
//echo "Điểm : ". $row["score"]."<br/>";
//echo "Số câu trả lời : ". $row["replies"]."<br/>";
//echo "Số lượng bài viết : ". $row["topics"]."<br/>";
echo "</div>";
}
}
}
if(isset($accessToken)){
// Render Github profile data
$login = 'block';
$tunna = 1;
if(@$_GET['id']){
$login = 'none';
$tunna = 0;
$OUT = 'none';
}
if($tunna == 1){
echo "<div style='display:$login'><div><img id='tunnna' src='".$userData["profile_pic"]."' width ='150' height='150' style='margin-bottom: 15px;float: left; border-radius: 50%; margin-right: 25px'></div>";
echo "<h1 style='margin-top: 0px;'>".$userData["username"]."</h1>";
echo "<table><tr><td style=''>Họ và tên :</td> <td>".$userData['name'].'</td>';
echo "<tr><td>ID : </td><td>".$userData['oauth_uid'].'</td>';
echo "<tr><td>Email : </td><td>".$userData['email'].'</td>';
echo "<tr><td>Địa chỉ : </td><td>".$userData['location'].'</td>';
echo '<tr><td>Profile Link : </td><td><a href="'.$userData['link'].'" target="_blank">Click để mở GitHub</a></td>';
echo '</table></div>';
}
}
if(@$_GET['id']){
$login = 'none';
$OUT = 'none';
$query = "SELECT * FROM accounts WHERE id='".$_GET['id']."'";
$results = mysqli_query($con, $query);
if (mysqli_num_rows($results) !=0) {
while($row = mysqli_fetch_assoc($results)){
echo "<div><img id='tunnna' src='".$row["profile_pic"]."' width ='150' height='150' style='margin-bottom: 15px;float: left; border-radius: 50%; margin-right: 25px'></div>";
echo "<h1 style='
margin-top: 0px;
'>".$row["username"]." <i id='tunganh2' data-toggle='tooltip' title='Tài khoản đã xác minh' style='color:#07f;font-size:22px;display:none;display:inline' class='fas fa-check-circle'></i></h1>";
?>
<h4 id="xk2" style="display: none;color: #989696">Xung kích</h4>
<h4 id="qtv2" style="display: none;color: #989696">Quản trị viên</h4>
<script>
var perm = '<?php echo $row['permission'] ?>';
var veri2 = '<?php echo $row["verified"] ?>';
if (veri2 == 'yes'){
document.getElementById("tunganh2").style.display = "inline";
} else {
document.getElementById("tunganh2").style.display = "none";
}
if (perm == 'xungkich'){
document.getElementById("xk2").style.display = "block";
}
if (perm == 'admin'){
document.getElementById("qtv2").style.display = "block";
}
$(document).ready(function(){
$('[data-toggle="tooltip"]').tooltip();
});
</script>
<?php
echo "<tr><td>Họ và tên :</td> <td>".$row["name"]."</td>";
echo "<tr><td>Ngày tham gia :</td> <td>".$row["date"]."</td>";
echo "<tr><td>ID : </td><td>".$row["id"]."</td>";
echo "<tr><td>Email : </td><td>". $row["email"]."</td>";
//echo "Điểm : ". $row["score"]."<br/>";
//echo "Số câu trả lời : ". $row["replies"]."<br/>";
//echo "Số lượng bài viết : ". $row["topics"]."<br/>";
}
}else{
echo "Không tìm thấy thông tin của ID này!";
}
}else {
echo "<p style='display:$none'>Bạn chưa đăng nhập!</p>";
}
if ($logi) {
$login = 'block';
}
?>
</table>
<br>
<?php
if(@$_GET['action']=="cp"){
echo "<form action='profile.php?action=cp' method='POST'><center>";
echo "<div id='daubuoi' style=''>
Mật khẩu cũ: <input type='password' class='form-control form-control-sm' name='pass' ><br/>
Mật khẩu mới: <input type='password' class='form-control form-control-sm' name='newpass'><br/>
Nhập lại mật khẩu: <input type='password' class='form-control form-control-sm' name='repass'><br/>
<button type='submit' name='change_pass' class='btn btn-primary' value='change' >Đổi mật khẩu</button>
</div>
";
// See the password_hash() example to see where this came from.
$query = "SELECT password FROM accounts WHERE username='".$_SESSION['username']."'";
$results = mysqli_query($con, $query);
if( $results ){
while($row = mysqli_fetch_assoc($results)){
$hash = $row['password'];
}
}
if(isset($_POST['change_pass'])){
$pass = mysqli_real_escape_string($con, $_POST['pass']);
$newpass = mysqli_real_escape_string($con, $_POST['newpass']);
$repass = mysqli_real_escape_string($con, $_POST['repass']);
$query = "SELECT * FROM accounts WHERE username='".$_SESSION['username']."'";
$results = mysqli_query($con, $query);
if (password_verify($pass, $hash)) {
if($newpass==$repass){
$upd = "UPDATE accounts SET password='".password_hash($newpass, PASSWORD_DEFAULT)."' WHERE username='".$_SESSION['username']."'";
mysqli_query($con, $upd);
echo "Thay đổi mật khẩu mới thành công!";
} else {
echo 'Mật khẩu nhập lại không khớp';
}
} else {
echo 'Mật khẩu hiện tại không chính xác';
}
}
}
echo "</center></form>";
if(@$_GET['action']=="ca"){
echo "<form action='profile.php?action=ca' method='POST' enctype='multipart/form-data' ><center>
<br/>
Chỉ có thể tải được ảnh dạng <b> .PNG .JPG .JPEG </b><br/>
<input type ='file' name ='image'><br/>
<input type ='submit' class='btn btn-primary' name ='change_pic' value='Tải lên'><br/>
";
if(isset($_POST['change_pic'])){
$errors=array();
$allowed_e=array('png','jpg','jpeg');
$file_name=$_FILES['image']['name'];
$file_e=strtolower(pathinfo($file_name,PATHINFO_EXTENSION));
$file_s= $_FILES['image']['size'];
$file_tmp=$_FILES['image']['tmp_name'];
if(in_array($file_e, $allowed_e)==false){
$errors[]='Phần mở rộng của ảnh không hợp lệ';
}
if($file_s>5097152){
$errors[]='Chỉ có thể upload ảnh <5MB';
}
if(empty($errors)){
move_uploaded_file($file_tmp,'images/'.$file_name);
$image_up='images/'.$file_name;
$query = "SELECT * FROM accounts WHERE username='".$_SESSION['username']."'";
$results = mysqli_query($con, $query);
$rows= mysqli_num_rows($results);
while($row = mysqli_fetch_assoc($results)){
$db_image=$row['profile_pic'];
}
$upd = "UPDATE accounts SET profile_pic='".$image_up." 'WHERE username='".$_SESSION['username']."'";
mysqli_query($con, $upd);
echo "Đổi ảnh đại diện thành công";
}else{
foreach($errors as $error){
echo $error,'<br/>';
}
}
}
echo "</form></center>";
}
?>
<div class="cpca" style="display:<?=$OUT?>;">
<a href='profile.php?action=cp'><i class="fas fa-key"></i> Thay đổi mật khẩu </a><br/>
<a href='profile.php?action=ca'><i class="fas fa-user"></i> Thay đổi ảnh đại diện </a>
</div>
</div>
</div>
</body>
</html>