1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class JodelAccount |
|
|
|
|
4
|
|
|
{ |
5
|
|
|
public $accessToken; |
6
|
|
|
public $expirationDate; |
7
|
|
|
public $refreshToken; |
8
|
|
|
public $distinctId; |
9
|
|
|
public $deviceUid; |
10
|
|
|
|
11
|
|
|
//is the Account a Bot or Spider? |
12
|
|
|
public $isBot; |
13
|
|
|
|
14
|
|
|
// array of voted Jodels |
15
|
|
|
public $votes; |
16
|
|
|
|
17
|
|
|
//Location of the Account |
18
|
|
|
public $location; |
19
|
|
|
|
20
|
|
|
function __construct($deviceUid = NULL, $isBot = FALSE) |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
if($deviceUid == NULL) |
23
|
|
|
{ |
24
|
|
|
$this->deviceUid = $this->createAccount(); |
25
|
|
|
} |
26
|
|
|
else |
27
|
|
|
{ |
28
|
|
|
$this->deviceUid = $deviceUid; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$this->isBot = $isBot; |
|
|
|
|
32
|
|
|
$this->location = $this->getLocation(); |
|
|
|
|
33
|
|
|
|
34
|
|
|
if(!$this->isTokenFresh()) |
35
|
|
|
{ |
36
|
|
|
$this->refreshToken(); |
37
|
|
|
} |
38
|
|
|
$this->accessToken = $this->getAccessToken(); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
function isAccountVerified() |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$accountCreator = new GetUserConfig(); |
44
|
|
|
$accountCreator->setAccessToken($this->accessToken); |
45
|
|
|
$data = $accountCreator->execute(); |
46
|
|
|
|
47
|
|
|
return $data['verified']; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
function setLocation() |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
//Is Channel or City |
53
|
|
|
if(substr($_GET['city'], 0, 1) === '#') |
54
|
|
|
{ |
55
|
|
|
return htmlspecialchars($_GET['city']) . " " . $this->location->cityName; |
|
|
|
|
56
|
|
|
} |
57
|
|
|
else |
58
|
|
|
{ |
59
|
|
|
$url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' . htmlspecialchars($_GET['city']) . '&key=AIzaSyCwhnja-or07012HqrhPW7prHEDuSvFT4w'; |
|
|
|
|
60
|
|
|
$result = Requests::post($url); |
61
|
|
|
if(json_decode($result->body, true)['status'] == 'ZERO_RESULTS' || json_decode($result->body, true)['status'] == 'INVALID_REQUEST') |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
return "0 results"; |
|
|
|
|
64
|
|
|
} |
65
|
|
|
else |
66
|
|
|
{ |
67
|
|
|
$name = json_decode($result->body, true)['results']['0']['address_components']['0']['long_name']; |
68
|
|
|
$lat = json_decode($result->body, true)['results']['0']['geometry']['location']['lat']; |
|
|
|
|
69
|
|
|
$lng = json_decode($result->body, true)['results']['0']['geometry']['location']['lng']; |
|
|
|
|
70
|
|
|
|
71
|
|
|
$location = new Location(); |
72
|
|
|
$location->setLat($lat); |
73
|
|
|
$location->setLng($lng); |
74
|
|
|
$location->setCityName($name); |
75
|
|
|
$accountCreator = new UpdateLocation(); |
76
|
|
|
$accountCreator->setLocation($location); |
77
|
|
|
$accountCreator->setAccessToken($this->accessToken); |
78
|
|
|
$data = $accountCreator->execute(); |
79
|
|
|
|
80
|
|
|
//safe location to db |
81
|
|
|
$db = new DatabaseConnect(); |
|
|
|
|
82
|
|
|
|
83
|
|
|
if($data == 'Success') |
84
|
|
|
{ |
85
|
|
|
$result = $db->query("UPDATE accounts |
86
|
|
|
SET name='" . $name . "', |
87
|
|
|
lat='" . $lat . "', |
88
|
|
|
lng='" . $lng . "' |
89
|
|
|
WHERE access_token='" . $this->accessToken . "'"); |
90
|
|
|
|
91
|
|
|
if($result === false) |
92
|
|
|
{ |
93
|
|
|
echo "Updating location failed: (" . $db->errno . ") " . $db->error; |
|
|
|
|
94
|
|
|
} |
95
|
|
|
else |
96
|
|
|
{ |
97
|
|
|
error_log('User with JodelDeviceId:' . $this->deviceUid . ' [' . $_SERVER['REMOTE_ADDR'] . '][' . $_SERVER ['HTTP_USER_AGENT'] . '] changed to Location: ' . $name); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $name; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
function getLocation() |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
$db = new DatabaseConnect(); |
|
|
|
|
109
|
|
|
$result = $db->query("SELECT * FROM accounts WHERE device_uid='" . $this->deviceUid . "'"); |
110
|
|
|
|
111
|
|
|
$location = new Location(); |
112
|
|
|
|
113
|
|
|
if ($result->num_rows > 0) |
114
|
|
|
{ |
115
|
|
|
// output data of each row |
116
|
|
|
while($row = $result->fetch_assoc()) |
117
|
|
|
{ |
118
|
|
|
$location->setLat($row['lat']); |
119
|
|
|
$location->setLng($row['lng']); |
120
|
|
|
$location->setCityName($row['name']); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
else |
124
|
|
|
{ |
125
|
|
|
echo "Error: 0 results"; |
|
|
|
|
126
|
|
|
error_log("Error no Location found - getLocation"); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $location; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
function verifyCaptcha() |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
if(isset($_GET['deviceUid'])) |
135
|
|
|
{ |
136
|
|
|
$deviceUid = $_GET['deviceUid']; |
137
|
|
|
} |
138
|
|
|
if(isset($_POST['deviceUid'])) |
139
|
|
|
{ |
140
|
|
|
$deviceUid = $_POST['deviceUid']; |
141
|
|
|
} |
142
|
|
|
$jodelAccountForVerify = new JodelAccount($deviceUid); |
|
|
|
|
143
|
|
|
|
144
|
|
|
$solution = $_GET['solution']; |
145
|
|
|
$solution = array_map('intval', explode('-', $solution)); |
146
|
|
|
|
147
|
|
|
$accountCreator = new PostCaptcha(); |
148
|
|
|
$accountCreator->setAccessToken($jodelAccountForVerify->accessToken); |
149
|
|
|
$accountCreator->captchaKey = $_GET['key']; |
|
|
|
|
150
|
|
|
$accountCreator->captchaSolution = $solution; |
151
|
|
|
$verified = $accountCreator->execute(); |
|
|
|
|
152
|
|
|
|
153
|
|
|
if(isset($verified->status_code)) |
154
|
|
|
{ |
155
|
|
|
return $verified->status_code; |
156
|
|
|
} |
157
|
|
|
return $verified['verified']; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
//ToDo Spider Check |
|
|
|
|
161
|
|
|
function votePostId($postId, $vote) |
|
|
|
|
162
|
|
|
{ |
163
|
|
|
if(!$this->isAccountVerified()) |
164
|
|
|
{ |
165
|
|
|
$view = new View(); |
166
|
|
|
$view->showCaptcha($this->accessToken, $this->deviceUid); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
if(!$this->deviceUidHasVotedThisPostId($postId)) |
170
|
|
|
{ |
171
|
|
|
if($vote == "up") |
|
|
|
|
172
|
|
|
{ |
173
|
|
|
$accountCreator = new Upvote(); |
174
|
|
|
} |
175
|
|
|
else if($vote == "down") |
|
|
|
|
176
|
|
|
{ |
177
|
|
|
$accountCreator = new Downvote(); |
178
|
|
|
} |
179
|
|
|
$accountCreator->setAccessToken($this->accessToken); |
|
|
|
|
180
|
|
|
$accountCreator->postId = htmlspecialchars($postId); |
181
|
|
|
$data = $accountCreator->execute(); |
|
|
|
|
182
|
|
|
|
183
|
|
|
if(array_key_exists('post', $data)) |
184
|
|
|
{ |
185
|
|
|
$this->addVoteWithPostIdAndTypeToDeviceUid($postId, $vote); |
186
|
|
|
return TRUE; |
187
|
|
|
} |
188
|
|
|
else |
189
|
|
|
{ |
190
|
|
|
return FALSE; |
191
|
|
|
error_log("Could not vote: " . var_dump($data)); |
|
|
|
|
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
//ToDo Spider Check |
|
|
|
|
197
|
|
|
function sendJodel() |
|
|
|
|
198
|
|
|
{ |
199
|
|
|
if(!$this->isAccountVerified()) |
200
|
|
|
{ |
201
|
|
|
showCaptcha($this->accessToken); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
$accountCreator = new SendJodel(); |
205
|
|
|
|
206
|
|
|
if(isset($_POST['ancestor'])) |
207
|
|
|
{ |
208
|
|
|
$ancestor = $_POST['ancestor']; |
|
|
|
|
209
|
|
|
$accountCreator->ancestor = $ancestor; |
210
|
|
|
} |
211
|
|
|
if(isset($_POST['color'])) |
212
|
|
|
{ |
213
|
|
|
$color = $_POST['color']; |
214
|
|
|
switch ($color) { |
215
|
|
|
case '8ABDB0': |
216
|
|
|
$color = '8ABDB0'; |
217
|
|
|
break; |
218
|
|
|
case '9EC41C': |
219
|
|
|
$color = '9EC41C'; |
220
|
|
|
break; |
221
|
|
|
case '06A3CB': |
222
|
|
|
$color = '06A3CB'; |
223
|
|
|
break; |
224
|
|
|
case 'FFBA00': |
225
|
|
|
$color = 'FFBA00'; |
226
|
|
|
break; |
227
|
|
|
case 'DD5F5F': |
228
|
|
|
$color = 'DD5F5F'; |
229
|
|
|
break; |
230
|
|
|
case 'FF9908': |
231
|
|
|
$color = 'FF9908'; |
232
|
|
|
break; |
233
|
|
|
default: |
234
|
|
|
$color = '8ABDB0'; |
235
|
|
|
break; |
236
|
|
|
} |
237
|
|
|
$accountCreator->color = $color; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
$accountCreatorLocation = new UpdateLocation(); |
|
|
|
|
241
|
|
|
$accountCreatorLocation->setLocation($this->location); |
242
|
|
|
$accountCreatorLocation->setAccessToken($this->accessToken); |
243
|
|
|
$data = $accountCreatorLocation->execute(); |
|
|
|
|
244
|
|
|
|
245
|
|
|
$accountCreator->location = $this->location; |
246
|
|
|
|
247
|
|
|
$accountCreator->setAccessToken($this->accessToken); |
248
|
|
|
$data = $accountCreator->execute(); |
249
|
|
|
|
250
|
|
|
if(isset($_POST['ancestor'])) |
251
|
|
|
{ |
252
|
|
|
$actual_link = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
253
|
|
|
header('Location: ' . $actual_link . '#postId-' . htmlspecialchars($data['post_id'])); |
254
|
|
|
exit; |
|
|
|
|
255
|
|
|
} |
256
|
|
|
else |
257
|
|
|
{ |
258
|
|
|
header('Location: ./#'); |
259
|
|
|
exit; |
|
|
|
|
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
function isTokenFresh() |
|
|
|
|
264
|
|
|
{ |
265
|
|
|
$db = new DatabaseConnect(); |
|
|
|
|
266
|
|
|
$result = $db->query("SELECT * FROM accounts WHERE device_uid='" . $this->deviceUid . "'"); |
267
|
|
|
|
268
|
|
|
$access_token; |
|
|
|
|
269
|
|
|
|
270
|
|
|
if ($result->num_rows > 0) |
271
|
|
|
{ |
272
|
|
|
// output data of each row |
273
|
|
|
while($row = $result->fetch_assoc()) { |
274
|
|
|
//$access_token = $row["access_token"]; |
|
|
|
|
275
|
|
|
$expiration_date = $row["expiration_date"]; |
|
|
|
|
276
|
|
|
$deviceUid = $row["device_uid"]; |
|
|
|
|
277
|
|
|
$access_token = $row["access_token"]; |
|
|
|
|
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
else |
281
|
|
|
{ |
282
|
|
|
echo '0 results'; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
if($expiration_date <= time()) { |
|
|
|
|
286
|
|
|
$accountCreator = new CreateUser(); |
287
|
|
|
$accountCreator->setAccessToken($access_token); |
|
|
|
|
288
|
|
|
$accountCreator->setDeviceUid($deviceUid); |
|
|
|
|
289
|
|
|
$accountCreator->setLocation($this->location); |
290
|
|
|
$data = $accountCreator->execute(); |
291
|
|
|
|
292
|
|
|
$access_token = (string)$data[0]['access_token']; |
|
|
|
|
293
|
|
|
$expiration_date = $data[0]['expiration_date']; |
294
|
|
|
$device_uid = (string)$data[1]; |
|
|
|
|
295
|
|
|
|
296
|
|
|
$db = new DatabaseConnect(); |
|
|
|
|
297
|
|
|
$result = $db->query("UPDATE accounts |
298
|
|
|
SET access_token='" . $access_token . "', |
299
|
|
|
expiration_date='" . $expiration_date . "' |
300
|
|
|
WHERE device_uid='" . $device_uid . "'"); |
301
|
|
|
|
302
|
|
|
if($result === false){ |
303
|
|
|
echo "Adding account failed: (" . $db->errno . ") " . $db->error; |
|
|
|
|
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
return $access_token; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
function refreshToken() |
|
|
|
|
311
|
|
|
{ |
312
|
|
|
|
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
|
316
|
|
|
|
317
|
|
|
function getAccessToken() |
|
|
|
|
318
|
|
|
{ |
319
|
|
|
$db = new DatabaseConnect(); |
|
|
|
|
320
|
|
|
$result = $db->query("SELECT * FROM accounts WHERE device_uid='" . $this->deviceUid . "'"); |
321
|
|
|
|
322
|
|
|
$accessToken; |
|
|
|
|
323
|
|
|
|
324
|
|
|
if ($result->num_rows > 0) |
325
|
|
|
{ |
326
|
|
|
// output data of each row |
327
|
|
|
while($row = $result->fetch_assoc()) |
328
|
|
|
{ |
329
|
|
|
$accessToken = $row['access_token']; |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
else |
333
|
|
|
{ |
334
|
|
|
echo "Error: 0 results"; |
|
|
|
|
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
return $accessToken; |
|
|
|
|
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
|
341
|
|
|
function getKarma() |
|
|
|
|
342
|
|
|
{ |
343
|
|
|
$accountCreator = new GetKarma(); |
344
|
|
|
$accountCreator->setAccessToken($this->accessToken); |
345
|
|
|
$data = $accountCreator->execute(); |
346
|
|
|
|
347
|
|
|
return $data["karma"]; |
|
|
|
|
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
function deviceUidHasVotedThisPostId($postId) |
|
|
|
|
351
|
|
|
{ |
352
|
|
|
$db = new DatabaseConnect(); |
|
|
|
|
353
|
|
|
|
354
|
|
|
$postId = $db->real_escape_string($postId); |
|
|
|
|
355
|
|
|
|
356
|
|
|
$result = $db->query("SELECT id |
357
|
|
|
FROM votes |
358
|
|
|
WHERE (postId = '" . $postId . "' AND device_uid = '" . $this->deviceUid . "')"); |
359
|
|
|
|
360
|
|
View Code Duplication |
if($result === false) |
|
|
|
|
361
|
|
|
{ |
362
|
|
|
$error = db_error(); |
363
|
|
|
echo $error; |
364
|
|
|
echo "Adding Vote failed: (" . $result->errno . ") " . $result->error; |
|
|
|
|
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
if($result->num_rows == 0) |
|
|
|
|
368
|
|
|
{ |
369
|
|
|
return FALSE; |
370
|
|
|
} |
371
|
|
|
else |
372
|
|
|
{ |
373
|
|
|
return TRUE; |
374
|
|
|
} |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
function addVoteWithPostIdAndTypeToDeviceUid($postId, $voteType) |
|
|
|
|
378
|
|
|
{ |
379
|
|
|
$db = new DatabaseConnect(); |
|
|
|
|
380
|
|
|
|
381
|
|
|
$postId = $db->real_escape_string($postId); |
|
|
|
|
382
|
|
|
$voteType = $db->real_escape_string($voteType); |
|
|
|
|
383
|
|
|
|
384
|
|
|
if($this->deviceUidHasVotedThisPostId($postId)) |
385
|
|
|
{ |
386
|
|
|
return "Already voted"; |
|
|
|
|
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
$result = $db->query("INSERT INTO votes (device_uid, postId, type) |
390
|
|
|
VALUES ('" . $this->deviceUid . "','" . $postId . "','" . $voteType . "')"); |
391
|
|
|
|
392
|
|
View Code Duplication |
if($result === false){ |
|
|
|
|
393
|
|
|
$error = db_error(); |
394
|
|
|
echo $error; |
395
|
|
|
echo "Adding Vote failed: (" . $result->errno . ") " . $result->error; |
|
|
|
|
396
|
|
|
} |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
function registerAccount($location) { |
|
|
|
|
400
|
|
|
$accountCreator = new CreateUser(); |
401
|
|
|
$accountCreator->setLocation($location); |
402
|
|
|
$data = $accountCreator->execute(); |
403
|
|
|
|
404
|
|
|
$access_token = (string)$data[0]['access_token']; |
|
|
|
|
405
|
|
|
$refresh_token = (string)$data[0]['refresh_token']; |
|
|
|
|
406
|
|
|
$token_type = (string)$data[0]['token_type']; |
|
|
|
|
407
|
|
|
$expires_in = $data[0]['expires_in']; |
|
|
|
|
408
|
|
|
$expiration_date = $data[0]['expiration_date']; |
409
|
|
|
$distinct_id = (string)$data[0]['distinct_id']; |
|
|
|
|
410
|
|
|
$device_uid = (string)$data[1]; |
|
|
|
|
411
|
|
|
|
412
|
|
|
$name = $location->cityName; |
413
|
|
|
$lat = $location->lat; |
|
|
|
|
414
|
|
|
$lng = $location->lng; |
|
|
|
|
415
|
|
|
|
416
|
|
|
$db = new DatabaseConnect(); |
|
|
|
|
417
|
|
|
$result = $db->query("INSERT INTO accounts (access_token, refresh_token, token_type, |
418
|
|
|
expires_in, expiration_date, distinct_id, device_uid, name, lat, lng) |
419
|
|
|
VALUES ('" . $access_token . "','" . $refresh_token . "','" . $token_type . |
420
|
|
|
"','" . $expires_in . "','" . $expiration_date . "','" . $distinct_id . |
421
|
|
|
"','" . $device_uid . "','" . $name . "','" . $lat . "','" . $lng . "') "); |
422
|
|
|
|
423
|
|
|
$success = TRUE; |
|
|
|
|
424
|
|
View Code Duplication |
if($result === false){ |
|
|
|
|
425
|
|
|
$error = db_error(); |
426
|
|
|
echo $error; |
427
|
|
|
echo "Adding account failed: (" . $result->errno . ") " . $result->error; |
|
|
|
|
428
|
|
|
$success = FALSE; |
|
|
|
|
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
return $device_uid; |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
function createAccount() |
|
|
|
|
435
|
|
|
{ |
436
|
|
|
$config = parse_ini_file('config/config.ini.php'); |
|
|
|
|
437
|
|
|
$location = new Location(); |
438
|
|
|
$location->setLat($config['default_lat']); |
439
|
|
|
$location->setLng($config['default_lng']); |
440
|
|
|
$location->setCityName($config['default_location']); |
441
|
|
|
|
442
|
|
|
$deviceUid = $this->registerAccount($location); |
443
|
|
|
|
444
|
|
|
return $deviceUid; |
445
|
|
|
} |
446
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.