-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetchposts.php
257 lines (219 loc) · 7.4 KB
/
fetchposts.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
<?php
date_default_timezone_set('Europe/Berlin');
require_once __DIR__ . '/framework/randomID.php';
$randomRequestIdentifier = 'R'.random_str(10);
// Set up all variables
$response = array();
$response['request'] = 'failed';
$response['error'] = 'unknown';
$response['requestIdentifier'] = $randomRequestIdentifier;
$response['content'] = array();
$response['content']['posts'] = '';
$response['content']['corner-menus'] = '';
$responseString = '';
$responseCornerMenus = '';
if (isset($_POST["alreadyLoadedPosts"])) {
$alreadyLoadedPosts = json_decode($_POST["alreadyLoadedPosts"], true);
} else {
$response['error'] = 'Parameter \'alreadyLoadedPosts\' wrong';
goto end;
}
if (isset($_POST["ammount"])) {
$ammountOfPosts = $_POST["ammount"];
} else {
$response['error'] = 'Parameter \'ammount\' wrong';
goto end;
}
session_start();
if (isset($_SESSION['UID'])) {
$UID = $_SESSION['UID'];
}
// Connenct to database
include 'framework/mysqlcredentials.php';
// Check connection
if ($pdo === false) {
$response['error'] = 'Could not connect to database.';
goto end;
} else {
$response['request'] = 'success';
unset($response['error']);
// prepare IN condition: Insert parameter for each
// $alreadyLoadedPosts = []; // DEBUG to return infinite posts
$INparam = '';
$integerKey = 0;
if (sizeof($alreadyLoadedPosts) === 0) {
$INparam = '""';
} else {
foreach ($alreadyLoadedPosts as $i => $post) {
$INparam .= ":post$integerKey, ";
$integerKey += 1;
}
$INparam = substr($INparam, 0, -2);
}
// prepare statement
$stmntposts = $pdo->prepare("SELECT PID as ID, postedon as 'time', owner, 'post' as 'type', text, NULL as 'title'
FROM posts
WHERE status = 'public' AND PID NOT IN($INparam)
UNION ALL
SELECT AID as ID, publishedon as 'time', owner, 'article' as 'type', previewdata as 'text', title
FROM articles
WHERE status = 'public' AND AID NOT IN($INparam)
ORDER BY time desc
LIMIT :ammountOfPosts");
// bind a parameter for each posts in array
foreach ($alreadyLoadedPosts as $i => $post) {
$postVariable = 'post'.$i;
$$postVariable = $post;
$stmntposts->bindParam(":post$i", $$postVariable);
}
$stmntposts->bindParam(':ammountOfPosts', $ammountOfPosts, PDO::PARAM_INT);
$stmntposts->execute();
// // debug code for printin out mysql parameters
// ob_start();
// $stmntposts->debugDumpParams();
// $r = ob_get_contents();
// ob_end_clean();
// error_log($r);
// error_log("--------------------------------------\n\n\n\n");
// fetch
while ($row = $stmntposts->fetch()) {
// put data in variable
$ID = $row['ID'];
$owner = $row['owner'];
$unixTimeStamp = strtotime($row['time']);
$unixTimeStampMs = $unixTimeStamp * 1000;
$type = $row['type'];
$text = $row['text'];
$text_sanitized = htmlspecialchars($text);
$text_short = mb_substr($text, 0, 256, 'UTF-8');
$text_short_sanitized = htmlspecialchars($text_short);
$title = $row['title'];
$title_sanitized = htmlspecialchars($title);
$titleLink = rawurlencode($title);
$titleLink_sanitized = htmlspecialchars($titleLink);
// get name of owner
$stmntowner = $pdo->prepare("SELECT username, firstname, lastname FROM user WHERE UID = ?");
// execute statement
$stmntowner->execute(array($owner));
// fetch
$row = $stmntowner->fetch();
if ($stmntowner->rowCount() > 0) {
$username = $row['username'];
$firstname= $row['firstname'];
$lastname = $row['lastname'];
$fullname = $firstname." ".$lastname;
} else {
$username = 'unknown.user';
$firstname= $row['Unknown'];
$lastname = $row['User'];
$fullname = $firstname." ".$lastname;
}
if ($type == "article") {
if (isset($UID)) {
if ($owner == $UID) {
addDropDownMenu($ID, 'article', $username, true);
$isOwnerClass = " card--post--isOwner";
$isOwnerDiv = <<<HTML
<!-- <div onclick="deleteArticle('$ID')" class="card__delete">
<i class="material-icons">delete_forever</i>
</div> -->
<div class="card__corner-menu">
<i class="material-icons">more_horiz</i>
</div>
HTML;
} else {
$isOwnerClass = "";
$isOwnerDiv = "";
}
} else {
$isOwnerClass = "";
$isOwnerDiv = "";
}
$responseString .= <<<HTML
<div data-pid="$ID" data-id="$ID" data-titlelink="$titleLink_sanitized" data-username="$username" data-postedOn="$unixTimeStamp" class="$ID card card--article $isOwnerClass">
<div class="card__info">
<img class="card__info__picture" src="user/$owner/pb-small.jpg" alt="profile picture">
<div class="card__info__textbox">
<div class="card__info__textbox__name">$fullname</div>
<div data-timeago="$unixTimeStampMs" class="$randomRequestIdentifier card__info__textbox__time"></div>
</div>
</div>
$isOwnerDiv
HTML;
if (file_exists('artikel/bilder/'.$ID.'/thumbnail.jpg')) {
$responseString .= '<img class="card__picture" src="/artikel/bilder/'.$ID.'/thumbnail.jpg" alt="">'."\n";
}
$responseString .= <<<HTML
<h3>$title_sanitized</h3>
<span class="card__text">$text_short_sanitized... <a href="artikel/$titleLink_sanitized">Weiter lesen</a></span>
</div>\n
HTML;
} elseif ($type == "post") {
if (isset($UID)) {
if ($owner == $UID) {
$isOwnerClass = " card--post--isOwner";
$isOwnerDiv = <<<HTML
<div class="card__corner-menu">
<i class="material-icons">more_horiz</i>
</div>
HTML;
addDropDownMenu($ID, 'post', $username, true);
} else {
$isOwnerClass = "";
$isOwnerDiv = '';
}
} else {
$isOwnerClass = "";
$isOwnerDiv = '';
}
$responseString .= <<<HTML
<div data-pid="$ID" data-id="$ID" data-postedOn="$unixTimeStamp" class="$ID card card--post$isOwnerClass">
<div class="card__info" onclick="linkTo('/profil/$username')">
<img class="card__info__picture" src="user/$owner/pb-small.jpg" alt="profile picture">
<div class="card__info__textbox">
<div class="card__info__textbox__name">$fullname</div>
<div data-timeago="$unixTimeStampMs" class="$randomRequestIdentifier card__info__textbox__time"></div>
</div>
</div>
$isOwnerDiv
<span class="card__text">$text_sanitized</span>
</div>\n
HTML;
}
}
}
function addDropDownMenu($ID, $type, $username, $isOwner)
{
global $responseCornerMenus;
if ($type == 'post')
$deleteFunction = 'deletePost';
else if ($type == 'article') {
$deleteFunction = 'deleteArticle';
} else {
error_log("fetchpost.php addDropDownMenu error");
}
$responseCornerMenus .= <<<HTML
<div class="card__corner-menu__container-wrapper $ID" data-id="$ID">
<div class="card__corner-menu__container-arrow"></div>
<div class="card__corner-menu__container-arrow--shadow-blocker"></div>
<div class="card__corner-menu__container">
HTML;
if ($isOwner == true) {
$responseCornerMenus .= <<<HTML
<div class="card__corner-menu__container__item" onclick="$deleteFunction('$ID')">
<div class="card__corner-menu__container__item__text">Löschen <i class="material-icons">delete_forever</i></div>
</div>
HTML;}
$responseCornerMenus .= <<<HTML
<div class="card__corner-menu__container__item" onclick="linkTo('/profil/$username')">
<div class="card__corner-menu__container__item__text">Zum Profil <i class="material-icons">person</i></div>
</div>
</div>
</div>
HTML;
}
$response['content']['posts'] = $responseString;
$response['content']['cornerMenus'] = $responseCornerMenus;
end:
// Encode response array into json
echo json_encode($response, JSON_PRETTY_PRINT);