-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
312 lines (287 loc) · 10.3 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
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
<?php
session_start();
include './config/database.php';
$_SESSION['page'] = "public";
try {
$dbh = new PDO($DB_DSN, $DB_USER, $DB_PASSWORD);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = $dbh->prepare('SELECT COUNT(*) FROM posts ORDER BY id DESC');
$query->execute();
$postlen = $query->fetchColumn();
$query = $dbh->prepare('SELECT * FROM posts ORDER BY id DESC LIMIT 5');
$query->execute();
} catch (PDOException $e) {
echo 'Error: '.$e->getMessage();
exit();
}
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.min.css" />
<title>Camagru</title>
<style media="screen">
html, body {
margin: 0;
height: 100%;
}
#page-content {
flex: 1 0 auto;
}
#justify {
width: 100%;
min-height: 100%;
margin: 0 auto;
overflow: hidden;
padding: 10px 0;
align-items: center;
justify-content: center;
display: flex;
float: none;
}
.heighmin {
min-height:58px;
}
.heighminp {
<?php if (empty($_SESSION['username'])) {?>
min-height:138px;
<?php } else { ?>
min-height:218px;
<?php } ?>
}
@media only screen and (min-width: 992px) {
.heighminp {
min-height:58px;
}
}
@media only screen and (max-width: 992px) {
.heighminp {
<?php if (empty($_SESSION['username'])) {?>
min-height:138px;
<?php } else { ?>
min-height:218px;
<?php } ?>
}
}
</style>
</head>
<body id="body" class="d-flex flex-column">
<?php include_once "./views/header.php"; ?>
<div id="page-content" class="bg-white">
<p class="text-center" style="color:#ffc107;margin-bottom: 0rem;font-size:2vw">GALLERY</p>
<hr style="margin-top: 0rem;margin-bottom: 0.5rem;">
<?php
$data = array();
while ($data = $query->fetch(PDO::FETCH_ASSOC)) {
?>
<main style="margin-top:15px" id="posts_list">
<div class="container">
<div class="row justify-content-center">
<div class="card card-body col-md-6 bg-light shadow">
<p class="bg-light" style="color:black;margin-bottom: 0rem;margin-top: 0rem;font-size:1.5vw"><strong><?php echo $data['username']; ?></strong></p>
<hr style="margin-top: 0rem;margin-bottom: 0.5rem;">
<img class="img-fluid border rounded-top" <?php echo "src='../images/".$data['picture'].".png'" ?> ></img>
<div class="cardbox-comments mt-2">
<textarea id="<?php echo $data['picture'];?>" class="form-control w-100 mb-2" placeholder="write a comment..." rows="1" style="resize: none;"></textarea>
<?php
try {
$newdbh = new PDO($DB_DSN, $DB_USER, $DB_PASSWORD);
$newdbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$like = $newdbh->prepare('SELECT COUNT(*) FROM likes WHERE picture_id = :lpicture');
$like->bindParam(':lpicture', $data['picture'], PDO::PARAM_STR);
$like->execute();
$totallikes = $like->fetchColumn();
$isliker = $newdbh->prepare('SELECT COUNT(*) FROM likes WHERE username = :liker AND picture_id = :lphoto');
$isliker->bindParam(':liker', $_SESSION['username'], PDO::PARAM_STR);
$isliker->bindParam(':lphoto', $data['picture'], PDO::PARAM_STR);
$isliker->execute();
$liker = $isliker->fetchColumn();
} catch (PDOException $e) {
echo 'Error: '.$e->getMessage();
exit();
}
?>
<button id="likebtn_<?php echo $data['picture'];?>" name="<?php echo $data['picture'];?>" onclick="like(this.name)" class="btn" <?php if ($liker) echo "style='color: red;'"; ?>><i id="counter_<?php echo $data['picture'];?>" class="fas fa-heart"> <?php if ($totallikes>0) {echo $totallikes;} ?> </i></button>
<button name="<?php echo $data['picture'];?>" onclick="comment(this.name)" class="btn"><i class="fas fa-paper-plane"></i></button>
<br/>
</div>
<div id="comment_list">
<?php
try {
$dbh = new PDO($DB_DSN, $DB_USER, $DB_PASSWORD);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$com = $dbh->prepare('SELECT * FROM comments WHERE picture_id = :picture ORDER BY id DESC');
$com->bindParam(':picture', $data['picture'], PDO::PARAM_STR);
$com->execute();
} catch (PDOException $e) {
echo 'Error: '.$e->getMessage();
exit();
}
$content = array();
while ($content = $com->fetch(PDO::FETCH_ASSOC)) {
?>
<hr style="margin-top: 0.2rem;margin-bottom: 0.5rem;">
<strong style="color:green"><?php echo $content['username'].": ";?></strong><?php echo $content['comment']; ?>
<?php
}
?>
</div>
</div>
</div>
</div>
</main>
<br/>
<?php } ?>
</div>
<nav aria-label="Page navigation example">
<ul class="pagination justify-content-center" style="margin-top: 1rem;">
<li class="page-item" style="color:blue"><a id="precedent" class="page-link" onclick="previous();">Previous</a></li>
<li class="page-item" style="color:blue"><a class="page-link" onclick="next();">Next</a></li>
</ul>
</nav>
<?php include_once "./views/footer.php"; ?>
</body>
<script type="text/javascript">
var page = 5;
var total = <?php echo $postlen; ?>;
function next() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("page-content").innerHTML = this.responseText;
}
};
var diff = total - page;
if (diff >= 5) {
page += 5;
var params = "number=" + page;
} else if (diff < 5 && diff > 0){
page += 5;
var params = "number=" + page;
} else {
return false;
}
xhttp.open('POST', 'http://localhost/control/pagination.php');
xhttp.withCredentials = true;
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send(params);
scrollToTop(500);
}
function previous() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("page-content").innerHTML = this.responseText;
}
};
if (page > 5) {
page -= 5;
var params = "number=" + page;
xhttp.open('POST', 'http://localhost/control/pagination.php');
xhttp.withCredentials = true;
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send(params);
scrollToTop(500);
} if (page == 5 || page < 5) {
return false;
}
}
function scrollToTop(scrollDuration) {
var scrollStep = -window.scrollY / (scrollDuration / 15),
scrollInterval = setInterval(function(){
if ( window.scrollY != 0 ) {
window.scrollBy( 0, scrollStep );
}
else clearInterval(scrollInterval);
},15);
}
function comment(post) {
var comment = document.getElementById(post).value;
var str = comment;
if (str.trim().length == 0) {
document.getElementById(post).value = "";
return false;
}
if (document.getElementById(post).value != "" && post != "") {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("body").innerHTML = this.responseText;
}
};
var params = "cpicture=" + post + "&ccontent=" + comment + "&page=" + page;
xhttp.open('POST', 'http://localhost/control/commentpublic.php');
xhttp.withCredentials = true;
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send(params);
<?php if (!empty($_SESSION['username'])) { ?>
var xmail = new XMLHttpRequest();
var paramsmail = "cpicture=" + post + "&ccontent=" + comment;
xmail.open('POST', 'http://localhost/control/commentmail.php');
xmail.withCredentials = true;
xmail.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmail.send(paramsmail);
<?php } ?>
}
document.getElementById(post).value = "";
}
function like(post) {
var str = post;
if (str.trim().length == 0) {
return false;
}
var node = document.getElementById('counter_'+post);
var icon = document.getElementById('likebtn_'+post);
var htmlContent;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if(this.readyState == 4 && this.status == 200) {
if (this.responseText === "like") {
htmlContent = node.innerHTML.trim();
htmlContent++;
icon.style.color = 'red';
} else if (this.responseText === "dislike"){
htmlContent = node.innerHTML.trim();
htmlContent--;
icon.style.color = '';
} else if (this.responseText === "not logged") {
window.location.replace("http://localhost/login.php?status=Please login or create an account to like and comment picture !");
}
if (htmlContent > 0)
node.innerHTML = " "+htmlContent+" ";
else
node.innerHTML = " ";
}
};
var params = "lpicture=" + post;
xhttp.open('POST', 'http://localhost/control/like.php');
xhttp.withCredentials = true;
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send(params);
}
</script>
<script>
var elem;
function recover() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
elem = document.getElementById("justify").innerHTML;
document.getElementById("errtext").innerHTML = "<div id='errtext' class='justify-content-center align-self-center' style='text-align:center;padding: 10px 0; color: red; -webkit-text-stroke-width: thin;'></div>";
document.getElementById("justify").innerHTML = this.responseText;
}
};
xhttp.open("GET", "./reset.php", true);
xhttp.send();
}
function login() {
document.getElementById("errtext").innerHTML = "<div id='errtext' class='justify-content-center align-self-center' style='text-align:center;padding: 10px 0; color: red; -webkit-text-stroke-width: thin;'></div>";
document.getElementById("justify").innerHTML = elem;
}
</script>
</html>