1
|
|
|
<?php |
|
|
|
|
2
|
|
|
error_reporting(-1); |
3
|
|
|
include 'php/DatabaseConnect.php'; |
4
|
|
|
include 'php/Requests/AbstractRequest.php'; |
5
|
|
|
include 'php/Requests/CreateUser.php'; |
6
|
|
|
include 'php/AccountData.php'; |
7
|
|
|
include 'php/Location.php'; |
8
|
|
|
include 'php/Requests/GetPosts.php'; |
9
|
|
|
include 'php/Requests/GetKarma.php'; |
10
|
|
|
include 'php/Requests/UpdateLocation.php'; |
11
|
|
|
include 'php/Requests/Upvote.php'; |
12
|
|
|
include 'php/Requests/Downvote.php'; |
13
|
|
|
include 'php/Requests/GetPostDetails.php'; |
14
|
|
|
include 'php/Requests/SendJodel.php'; |
15
|
|
|
|
16
|
|
|
require_once 'php/Requests/libary/Requests.php'; |
17
|
|
|
Requests::register_autoloader(); |
18
|
|
|
|
19
|
|
|
$lastPostId = ''; |
20
|
|
|
|
21
|
|
|
function isTokenFresh(Location $location) { |
22
|
|
|
$db = new DatabaseConnect(); |
|
|
|
|
23
|
|
|
$result = $db->query("SELECT * FROM accounts WHERE id='1'"); |
24
|
|
|
|
25
|
|
|
if ($result->num_rows > 0) |
26
|
|
|
{ |
27
|
|
|
// output data of each row |
28
|
|
|
while($row = $result->fetch_assoc()) { |
29
|
|
|
//$access_token = $row["access_token"]; |
|
|
|
|
30
|
|
|
$expiration_date = $row["expiration_date"]; |
|
|
|
|
31
|
|
|
$deviceUid = $row["device_uid"]; |
|
|
|
|
32
|
|
|
$access_token = $row["access_token"]; |
|
|
|
|
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
else |
36
|
|
|
{ |
37
|
|
|
echo '0 results'; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if($expiration_date <= time()) { |
|
|
|
|
41
|
|
|
$accountCreator = new CreateUser(); |
42
|
|
|
$accountCreator->setAccessToken($access_token);//$accountData->getAccessToken()); |
|
|
|
|
43
|
|
|
$accountCreator->setDeviceUid($deviceUid); |
|
|
|
|
44
|
|
|
$accountCreator->setLocation($location); |
45
|
|
|
$data = $accountCreator->execute(); |
46
|
|
|
|
47
|
|
|
$access_token = (string)$data[0]['access_token']; |
|
|
|
|
48
|
|
|
$expiration_date = $data[0]['expiration_date']; |
49
|
|
|
$device_uid = (string)$data[1]; |
|
|
|
|
50
|
|
|
|
51
|
|
|
$db = new DatabaseConnect(); |
|
|
|
|
52
|
|
|
$result = $db->query("UPDATE accounts |
53
|
|
|
SET access_token='" . $access_token . "', |
54
|
|
|
expiration_date='" . $expiration_date . "' |
55
|
|
|
WHERE device_uid='" . $device_uid . "'"); |
56
|
|
|
|
57
|
|
|
if($result === false){ |
58
|
|
|
echo "Adding account failed: (" . $db->errno . ") " . $db->error; |
|
|
|
|
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return TRUE; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
function getKarma($accessToken) |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$accountCreator = new GetKarma(); |
68
|
|
|
$accountCreator->setAccessToken($accessToken); |
69
|
|
|
$data = $accountCreator->execute(); |
70
|
|
|
|
71
|
|
|
return $data["karma"]; |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
function registerAccount(Location $location) { |
75
|
|
|
$accountCreator = new CreateUser(); |
76
|
|
|
$accountCreator->setLocation($location); |
77
|
|
|
$data = $accountCreator->execute(); |
78
|
|
|
|
79
|
|
|
$access_token = (string)$data[0]['access_token']; |
|
|
|
|
80
|
|
|
$refresh_token = (string)$data[0]['refresh_token']; |
|
|
|
|
81
|
|
|
$token_type = (string)$data[0]['token_type']; |
|
|
|
|
82
|
|
|
$expires_in = $data[0]['expires_in']; |
|
|
|
|
83
|
|
|
$expiration_date = $data[0]['expiration_date']; |
84
|
|
|
$distinct_id = (string)$data[0]['distinct_id']; |
|
|
|
|
85
|
|
|
$device_uid = (string)$data[1]; |
|
|
|
|
86
|
|
|
|
87
|
|
|
$name = $location->cityName; |
88
|
|
|
$lat = $location->lat; |
|
|
|
|
89
|
|
|
$lng = $location->lng; |
|
|
|
|
90
|
|
|
|
91
|
|
|
$db = new DatabaseConnect(); |
|
|
|
|
92
|
|
|
$result = $db->query("INSERT INTO accounts (access_token, refresh_token, token_type, |
93
|
|
|
expires_in, expiration_date, distinct_id, device_uid, name, lat, lng) |
94
|
|
|
VALUES ('" . $access_token . "','" . $refresh_token . "','" . $token_type . |
95
|
|
|
"','" . $expires_in . "','" . $expiration_date . "','" . $distinct_id . |
96
|
|
|
"','" . $device_uid . "','" . $name . "','" . $lat . "','" . $lng . "') "); |
97
|
|
|
|
98
|
|
|
$success = TRUE; |
99
|
|
|
if($result === false){ |
100
|
|
|
$error = db_error(); |
101
|
|
|
echo $error; |
102
|
|
|
echo "Adding account failed: (" . $result->errno . ") " . $result->error; |
|
|
|
|
103
|
|
|
$success = FALSE; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $success; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
function getPosts($lastPostId, $accessToken, $url) |
110
|
|
|
{ |
111
|
|
|
$accountCreator = new GetPosts(); |
112
|
|
|
$accountCreator->setLastPostId($lastPostId); |
113
|
|
|
$accountCreator->setAccessToken($accessToken); |
114
|
|
|
$accountCreator->setUrl($url); |
115
|
|
|
$data = $accountCreator->execute(); |
116
|
|
|
|
117
|
|
|
return $data; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
function createAccount() |
121
|
|
|
{ |
122
|
|
|
$location = new Location(); |
123
|
|
|
$location->setLat(52.520006); |
124
|
|
|
$location->setLng(13.404954); |
125
|
|
|
$location->setCityName('Berlin'); |
126
|
|
|
|
127
|
|
|
$account = registerAccount($location); |
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
function jodelToHtml($post, $view = 'time', $isDetailedView = FALSE) |
131
|
|
|
{ //ToDO |
|
|
|
|
132
|
|
|
//Replace # with link |
133
|
|
|
//preg_replace('~(\#)([^\s!,. /()"\'?]+)~', '<a href="tag/$2">#$2</a>', $text); |
|
|
|
|
134
|
|
|
|
135
|
|
|
|
136
|
|
|
//Time to time difference |
137
|
|
|
$now = new DateTime(); |
|
|
|
|
138
|
|
|
$d = new DateTime($post["created_at"]); |
|
|
|
|
139
|
|
|
$timediff = $now->diff($d); |
140
|
|
|
|
141
|
|
|
$timediff_inSeconds = (string)$timediff->format('%s'); |
142
|
|
|
$timediff_inMinutes = (string)$timediff->format('%i'); |
143
|
|
|
$timediff_inHours = (string)$timediff->format('%h'); |
|
|
|
|
144
|
|
|
$timediff_inDays = (string)$timediff->format('%d'); |
|
|
|
|
145
|
|
|
$timediff_inMonth = (string)$timediff->format('%m'); |
|
|
|
|
146
|
|
|
|
147
|
|
|
if($timediff_inMonth!=0) |
148
|
|
|
{ |
149
|
|
|
$timediff = $timediff_inMonth . "m"; |
|
|
|
|
150
|
|
|
} |
151
|
|
|
else |
152
|
|
|
{ |
153
|
|
|
if($timediff_inDays!=0) |
154
|
|
|
{ |
155
|
|
|
$timediff = $timediff_inDays . "d"; |
|
|
|
|
156
|
|
|
} |
157
|
|
|
else |
158
|
|
|
{ |
159
|
|
|
if($timediff_inHours!=0) |
160
|
|
|
{ |
161
|
|
|
$timediff = $timediff_inHours . "h"; |
|
|
|
|
162
|
|
|
} |
163
|
|
|
else |
164
|
|
|
{ |
165
|
|
|
if($timediff_inMinutes!=0) |
166
|
|
|
{ |
167
|
|
|
$timediff = $timediff_inMinutes . "m"; |
|
|
|
|
168
|
|
|
} |
169
|
|
|
else |
170
|
|
|
{ |
171
|
|
|
$timediff = $timediff_inSeconds . "s"; |
|
|
|
|
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
|
178
|
|
|
?> |
179
|
|
|
<article id ="postId-<?php echo $post["post_id"]; ?>" class="jodel" style="background-color: #<?php echo $post["color"];?>;"> |
|
|
|
|
180
|
|
|
<content> |
181
|
|
|
<?php |
182
|
|
|
if(isset($post["image_url"])) { |
|
|
|
|
183
|
|
|
echo '<img src="' . $post["image_url"] . '">'; |
|
|
|
|
184
|
|
|
} |
185
|
|
|
else { |
186
|
|
|
echo str_replace(' ', ' ', nl2br(htmlspecialchars($post["message"]))); |
|
|
|
|
187
|
|
|
} |
188
|
|
|
?> |
189
|
|
|
</content> |
190
|
|
|
<aside> |
191
|
|
|
<a href="index.php?vote=up&postID=<?php echo $post["post_id"];?>"> |
|
|
|
|
192
|
|
|
<i class="fa fa-angle-up fa-3x"></i> |
193
|
|
|
</a> |
194
|
|
|
<br /> |
195
|
|
|
<?php echo $post["vote_count"];?><br /> |
|
|
|
|
196
|
|
|
<a href="index.php?vote=down&postID=<?php echo $post["post_id"];?>"> |
|
|
|
|
197
|
|
|
<i class="fa fa-angle-down fa-3x"></i> |
198
|
|
|
</a> |
199
|
|
|
</aside> |
200
|
|
|
|
201
|
|
|
<footer> |
202
|
|
|
<table> |
203
|
|
|
<tr> |
204
|
|
|
<td class="time"> |
205
|
|
|
<span data-tooltip="Time"> |
206
|
|
|
<i class="fa fa-clock-o"></i> |
207
|
|
|
<?php echo $timediff;?> |
208
|
|
|
</span> |
209
|
|
|
</td> |
210
|
|
|
<td class="comments"> |
211
|
|
|
<?php if(!$isDetailedView) {?> |
212
|
|
|
<span data-tooltip="Comments"> |
213
|
|
|
<a href="index.php?getPostDetails=true&view=<?php echo $view;?>&postID=<?php echo $post["post_id"];?>"> |
|
|
|
|
214
|
|
|
<i class="fa fa-commenting-o"></i> |
215
|
|
|
<?php if(array_key_exists("child_count", $post)) { |
|
|
|
|
216
|
|
|
echo $post["child_count"]; |
|
|
|
|
217
|
|
|
} else echo "0"; |
|
|
|
|
218
|
|
|
?> |
219
|
|
|
</a> |
220
|
|
|
</span> |
221
|
|
|
<?php } ?> |
222
|
|
|
</td> |
223
|
|
|
<td class="distance"> |
224
|
|
|
<?php |
225
|
|
|
if($isDetailedView) |
226
|
|
|
{ |
227
|
|
|
if(isset($post["parent_creator"]) && $post["parent_creator"] == 1) |
|
|
|
|
228
|
|
|
{ |
229
|
|
|
?> |
230
|
|
|
<span data-tooltip="Author"> |
231
|
|
|
<i class="fa fa-user-o"></i> OJ | |
232
|
|
|
</span> |
233
|
|
|
<?php |
234
|
|
|
} |
235
|
|
|
else |
236
|
|
|
{ |
237
|
|
|
//Is not parent Jodel in detailed View |
238
|
|
|
if(!array_key_exists('child_count', $post) && array_key_exists('parent_creator', $post)) |
239
|
|
|
{ |
240
|
|
|
?> |
241
|
|
|
<span data-tooltip="Author"> |
242
|
|
|
<i class="fa fa-user-o"></i> #<?php echo $post["user_handle"];?> | |
|
|
|
|
243
|
|
|
</span> |
244
|
|
|
<?php |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
?> |
249
|
|
|
|
250
|
|
|
<span data-tooltip="Distance"> |
251
|
|
|
<i class="fa fa-map-marker"></i> |
252
|
|
|
<?php echo $post["distance"];?> km |
|
|
|
|
253
|
|
|
</span> |
254
|
|
|
</td> |
255
|
|
|
</tr> |
256
|
|
|
</table> |
257
|
|
|
</footer> |
258
|
|
|
</article> |
259
|
|
|
<?php |
260
|
|
|
} |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.