-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
310 lines (263 loc) · 7.8 KB
/
functions.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
<?php
require_once("config.php");
session_start();
function checkSession() {
if(!isset($_SESSION)) {
session_start();
}
}
// kolla om du är inloggad
function loggedIn() {
$db = connectToDB();
$query = "SELECT * FROM user";
$result = mysqli_query($db, $query);
if(mysqli_errno($db)) {
print mysqli_error($result);
}
$get = mysqli_fetch_assoc($result);
// $userid = $get['id'];
@$userid = $_SESSION['userdata']['id'];
if(isset($userid)) {
print '<a class="item" href="profile.php?user=' . $userid . '">' . $_SESSION['userdata']['name'] . '</a>';
print "\n";
print '<a class="item" href="logout.php">Logout</a>';
} else {
print '<a class="item" href="registrering.php">Register</a>';
print '<a class="item" href="login.php">Login</a>';
}
}
// hämta info för varje användare
function userInfo($userid) {
$db = connectToDB();
$query = "SELECT
`id`,
`name`,
`email`
FROM user
WHERE id = {$userid}";
$result = mysqli_query($db, $query);
if(mysqli_errno($db)) {
print mysqli_error($result);
}
$info = mysqli_fetch_assoc($result);
$info['avatar'] = "avatar/{$info['id']}.jpg";
return $info;
}
// user settings, där användare kan uppdatera sin profil
// just nu mest för att ladda upp en bild
function updateProfile($avatar) {
$db = connectToDB();
$userid = (int)$_SESSION['userdata']['id'];
if (file_exists($avatar)) {
$src_size = getimagesize($avatar);
if ($src_size['mime'] === 'image/jpeg'){
$src_img = imagecreatefromjpeg($avatar);
}else if ($src_size['mime'] ==='image/png'){
$src_img = imagecreatefrompng($avatar);
}else if ($src_size['mime'] ==='image/gif'){
$src_img = imagecreatefromgif($avatar);
}else{
$src_img = false;
}
if ($src_img !== false){
$thumb_size = 200;
if ($src_size[0] <= $thumb_size){
$thumb = $src_img;
}else{
$new_size[0] = $thumb_size;
$new_size[1] = $thumb_size;
$thumb = imagecreatetruecolor($new_size[0], $new_size[1]);
imagecopyresampled($thumb, $src_img, 0, 0, 0, 0,
$new_size[0], $new_size[1], $src_size[0], $src_size[1]);
}
}
}
}
// hämta all post/inlägg information
function postData() {
$db = connectToDB();
$query = "SELECT * FROM posts";
$result = mysqli_query($db, $query);
if(mysqli_errno($db)) {
print mysqli_error($result);
}
$row = mysqli_fetch_assoc($result);
}
// funktions-jätten, hämtar och lägger upp posts,
// samt även hanterar designen för post-boxen, kallad post-content
function getPost() {
// hämtar post informationen som behövs
$db = connectToDB();
$query = "SELECT
posts.id,
rubrik,
link,
name,
userid,
posttime
FROM `posts`
INNER JOIN `user`
ON posts.userid = user.id
ORDER BY vote / DATEDIFF(NOW(), posttime) DESC";
$result = mysqli_query($db, $query);
if(mysqli_errno($db)) {
print mysqli_error($result);
}
while($row = mysqli_fetch_assoc($result)) {
$postid = (int)$row['id'];
$name = mysqli_real_escape_string($db, $row['name']);
$time = $row['posttime'];
$link = mysqli_real_escape_string($db, $row['link']);
$title = mysqli_real_escape_string($db, $row['rubrik']);
$userid = (int)$row['userid'];
// startar post-content diven där posts visas
print '<div class="post-content">';
// visar rubrik, och gör den klickbar
print "<h1><a href=" . $link . ">". $title . "</a></h1><br>";
print '<h2>Skickat av <a href="profile.php?user=' . $userid . '">' . $name . '</a> ' . $time . '</h2>';
// hämtar och lägger ut information angående postens kommentarer
$comment = "SELECT
COUNT(id)
FROM comments
WHERE postid = {$postid}";
$rcomment = mysqli_query($db, $comment);
if(mysqli_errno($db)) {
print mysqli_error($rcomment);
}
$row = mysqli_fetch_array($rcomment);
print '<h3>' . $row[0] . ' <a href="comments.php?id=' . $postid . '">comments</a></h3>';
// startar votearea diven, där man kommer kunna rösta och se antalet röster
print '<div class="votearea">';
$votecount = "SELECT
vote
FROM posts
WHERE id = {$postid}";
$votes = mysqli_query($db, $votecount);
if(mysqli_errno($db)) {
print mysqli_error($votes);
}
$count = mysqli_fetch_assoc($votes);
print '<p>'. $count['vote'] . '</p>';
if(isset($_SESSION['userdata'])) {
$sessID = $_SESSION['userdata']['id'];
$vote = "SELECT
vote,
postid,
userid
FROM vote_id
WHERE postid = {$postid}
AND userid = {$sessID}";
$rvote = mysqli_query($db, $vote);
if(mysqli_errno($db)) {
print mysqli_error($rvote);
}
$vrow = mysqli_fetch_assoc($rvote);
if($vrow > 1) {
print '<img src="img/plusvoted.png"
alt="voteup" />';
print '<img src="img/minusvoted.png"
alt="votedown" />';
} else {
print '<a href="?vote=up&id=' . $postid . '"><img src="img/plus2.png"
alt="voteup"/></a>';
print '<a href="?vote=down&id=' . $postid . '"><img src="img/minus2.png"
alt="votedown" /></a>';
}
}
print '</div>';
print "</div><br>";
}
}
// lägger in kommentarer i databasen
function addComment() {
$db = connectToDB();
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$comment = mysqli_real_escape_string($db, $_POST['comment']);
$user = (int)$_SESSION['userdata']['id'];
$postid = (int)$_POST["postid"];
$query = "INSERT INTO
comments (comments, userid, postid)
VALUES ('$comment', '$user', '$postid')";
$result = mysqli_query($db, $query);
if(mysqli_errno($db)) {
print mysqli_error($result);
}
}
}
// hämtar kommentarer från databasen och lägger upp dem
function getComment() {
$db = connectToDB();
$query = "SELECT
comments.id,
comments, name,
`comment-time`,
postid,
userid
FROM comments
INNER JOIN user
ON comments.userid = user.id
WHERE postid = '{$_GET["id"]}'";
$result = mysqli_query($db, $query);
if(mysqli_errno($db)) {
print mysqli_error($result);
}
while($row = mysqli_fetch_assoc($result)) {
$userid = (int)$row['userid'];
$name = mysqli_real_escape_string($db, $row['name']);
$time = $row['comment-time'];
print '<div class="comment-box">';
print '<h2><a href="profile.php?user=' . $userid . '">' . $name . '</a> ' . $time . '</h2>';
print "<h1>" . $row['comments'] . "<br></h1>";
print "</div><br>";
}
}
function userbox()
{
$db = connectToDB();
$query = "SELECT
*
FROM user
ORDER BY id DESC";
$result = mysqli_query($db, $query);
if(mysqli_errno($db)) {
print mysqli_error($result);
}
while($row = mysqli_fetch_assoc($result))
{
print '<h1><a href="profile.php?user=' . $row['id'] . '">' . $row['name'] . '</a><br><br></h1>';
}
}
// funktionen som gör att man kan rösta
function addVote($vote, $postid, $userid){
$db = connectToDB();
$vote = mysqli_real_escape_string($db, $_GET['vote']);
$postid = (int)$_GET['id'];
$userid = $_SESSION['userdata']['id'];
$query = "INSERT IGNORE INTO vote_id (vote, postid, userid)
VALUES ('1', '{$postid}', '{$userid}')";
$result = mysqli_query($db, $query);
$vote = ($vote === 'up') ? '+' : '-';
$votecount = "UPDATE posts SET vote = vote {$vote} 1 WHERE id = {$postid}";
$vres = mysqli_query($db, $votecount);
header('Location: index.php');
}
// validerar inputs i registreringen
function validateInput($input) {
$input = trim($input);
$input = stripslashes($input);
$input = htmlspecialchars($input);
return $input;
}
function searchBar()
{
$db = connectToDB();
if($_SERVER['REQUEST_METHOD'] == "POST") {
$search = $_POST['search'];
$query = "SELECT name FROM user WHERE name LIKE '%{$search}%' ";
$result = mysqli_query($db, $query);
$row = mysqli_fetch_assoc($result);
if($row >= 1) {
print $row['name'] . "<br>";
}
}
}