Completed
Push — master ( 81a7e0...24e24a )
by mains
02:43
created

php/jodel-web.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 View Code Duplication
function isTokenFresh(Location $location)
22
{
23
	$db = new DatabaseConnect();  
24
	$result = $db->query("SELECT * FROM accounts WHERE id='1'");
25
	
26
	$access_token;
27
28
	if ($result->num_rows > 0)
29
	{
30
			// output data of each row
31
			while($row = $result->fetch_assoc()) {
32
					//$access_token = $row["access_token"];
33
					$expiration_date = $row["expiration_date"];
34
					$deviceUid = $row["device_uid"];
35
					$access_token = $row["access_token"];
36
			}
37
	}
38
	else
39
	{
40
			echo '0 results';
41
	}
42
43
	if($expiration_date <= time()) {
44
		$accountCreator = new CreateUser();
45
		$accountCreator->setAccessToken($access_token);//$accountData->getAccessToken());
46
		$accountCreator->setDeviceUid($deviceUid);
47
		$accountCreator->setLocation($location);
48
		$data = $accountCreator->execute();
49
50
		$access_token = (string)$data[0]['access_token'];
51
		$expiration_date = $data[0]['expiration_date'];
52
		$device_uid = (string)$data[1];
53
		
54
		$db = new DatabaseConnect();  
55
		$result = $db->query("UPDATE accounts 
56
								SET access_token='" . $access_token . "',
57
									expiration_date='" . $expiration_date . "'
58
								WHERE device_uid='" . $device_uid . "'");
59
60
		if($result === false){
61
				echo "Adding account failed: (" . $db->errno . ") " . $db->error;
62
		}	
63
	}
64
	
65
	return $access_token;
66
}
67
68 View Code Duplication
function isTokenFreshByAccessToken(Location $location, $accessToken)
69
{
70
	$db = new DatabaseConnect();  
71
	$result = $db->query("SELECT * FROM accounts WHERE access_token='" . $accessToken . "'");
72
	
73
	if ($result->num_rows > 0)
74
	{
75
			// output data of each row
76
			while($row = $result->fetch_assoc()) {
77
					//$access_token = $row["access_token"];
78
					$expiration_date = $row["expiration_date"];
79
					$deviceUid = $row["device_uid"];
80
					$access_token = $row["access_token"];
81
			}
82
	}
83
	else
84
	{
85
			echo '0 results';
86
	}
87
88
	if($expiration_date <= time()) {
89
		$accountCreator = new CreateUser();
90
		$accountCreator->setAccessToken($access_token);//$accountData->getAccessToken());
91
		$accountCreator->setDeviceUid($deviceUid);
92
		$accountCreator->setLocation($location);
93
		$data = $accountCreator->execute();
94
95
		$access_token = (string)$data[0]['access_token'];
96
		$expiration_date = $data[0]['expiration_date'];
97
		$device_uid = (string)$data[1];
98
		
99
		$db = new DatabaseConnect();  
100
		$result = $db->query("UPDATE accounts 
101
								SET access_token='" . $access_token . "',
102
									expiration_date='" . $expiration_date . "'
103
								WHERE device_uid='" . $device_uid . "'");
104
105
		if($result === false){
106
				echo "Adding account failed: (" . $db->errno . ") " . $db->error;
107
		}	
108
	}
109
	
110
	return $access_token;
111
}
112
113 View Code Duplication
function isTokenFreshByDeviceUid(Location $location, $deviceUid)
114
{
115
	$db = new DatabaseConnect();  
116
	$result = $db->query("SELECT * FROM accounts WHERE device_uid='" . $deviceUid . "'");
117
118
	$access_token;
119
120
	if ($result->num_rows > 0)
121
	{
122
			// output data of each row
123
			while($row = $result->fetch_assoc()) {
124
					//$access_token = $row["access_token"];
125
					$expiration_date = $row["expiration_date"];
126
					$deviceUid = $row["device_uid"];
127
					$access_token = $row["access_token"];
128
			}
129
	}
130
	else
131
	{
132
			echo '0 results';
133
	}
134
135
	if($expiration_date <= time()) {
136
		$accountCreator = new CreateUser();
137
		$accountCreator->setAccessToken($access_token);
138
		$accountCreator->setDeviceUid($deviceUid);
139
		$accountCreator->setLocation($location);
140
		$data = $accountCreator->execute();
141
142
		$access_token = (string)$data[0]['access_token'];
143
		$expiration_date = $data[0]['expiration_date'];
144
		$device_uid = (string)$data[1];
145
		
146
		$db = new DatabaseConnect();  
147
		$result = $db->query("UPDATE accounts 
148
								SET access_token='" . $access_token . "',
149
									expiration_date='" . $expiration_date . "'
150
								WHERE device_uid='" . $device_uid . "'");
151
152
		if($result === false){
153
				echo "Adding account failed: (" . $db->errno . ") " . $db->error;
154
		}	
155
	}
156
	
157
	return $access_token;
158
}
159
160 View Code Duplication
function getLocationByAccessToken($accessToken)
161
{
162
	$db = new DatabaseConnect();
163
	$result = $db->query("SELECT * FROM accounts WHERE access_token='" . $accessToken  . "'");
164
	
165
	$location = new Location();
166
	
167
	if ($result->num_rows > 0)
168
	{
169
		// output data of each row
170
		while($row = $result->fetch_assoc())
171
		{
172
			$location->setLat($row['lat']);
173
			$location->setLng($row['lng']);
174
			$location->setCityName($row['name']);
175
		}
176
	}
177
	else
178
	{
179
		echo "Error: 0 results";
180
	}
181
182
	return $location;
183
}
184
185 View Code Duplication
function getLocationByDeviceUid($deviceUid)
186
{
187
	$db = new DatabaseConnect();
188
	$result = $db->query("SELECT * FROM accounts WHERE device_uid='" . $deviceUid  . "'");
189
	
190
	$location = new Location();
191
	
192
	if ($result->num_rows > 0)
193
	{
194
		// output data of each row
195
		while($row = $result->fetch_assoc())
196
		{
197
			$location->setLat($row['lat']);
198
			$location->setLng($row['lng']);
199
			$location->setCityName($row['name']);
200
		}
201
	}
202
	else
203
	{
204
		echo "Error: 0 results";
205
	}
206
207
	return $location;
208
}
209
210
function getAccessTokenByDeviceUid($deviceUid)
211
{
212
	$db = new DatabaseConnect();
213
	$result = $db->query("SELECT * FROM accounts WHERE device_uid='" . $deviceUid  . "'");
214
	
215
	$accessToken;
216
	
217
	if ($result->num_rows > 0)
218
	{
219
		// output data of each row
220
		while($row = $result->fetch_assoc())
221
		{
222
			$accessToken = $row['access_token'];
223
		}
224
	}
225
	else
226
	{
227
		echo "Error: 0 results";
228
	}
229
230
	return $accessToken;
231
}
232
233
234
function getKarma($accessToken)
235
{
236
	$accountCreator = new GetKarma();
237
	$accountCreator->setAccessToken($accessToken);
238
	$data = $accountCreator->execute();
239
	
240
	return $data["karma"];
241
}
242
243
function registerAccount(Location $location) {
244
	$accountCreator = new CreateUser();
245
	$accountCreator->setLocation($location);
246
	$data = $accountCreator->execute();
247
	
248
	$access_token = (string)$data[0]['access_token'];
249
	$refresh_token = (string)$data[0]['refresh_token'];
250
	$token_type = (string)$data[0]['token_type'];
251
	$expires_in = $data[0]['expires_in'];
252
	$expiration_date = $data[0]['expiration_date'];
253
	$distinct_id = (string)$data[0]['distinct_id'];
254
	$device_uid = (string)$data[1];
255
256
	$name = $location->cityName;
257
	$lat = $location->lat;
258
	$lng = $location->lng;
259
	
260
	$db = new DatabaseConnect();  
261
	$result = $db->query("INSERT INTO accounts (access_token, refresh_token, token_type,
262
					expires_in, expiration_date, distinct_id, device_uid, name, lat, lng)
263
					VALUES ('" . $access_token . "','" . $refresh_token . "','" . $token_type .
264
					"','" .  $expires_in . "','" . $expiration_date . "','" . $distinct_id .
265
					"','" . $device_uid . "','" . $name . "','" . $lat . "','" . $lng . "') ");
266
267
	$success = TRUE;
268
	if($result === false){
269
			$error = db_error();
270
			echo $error;
271
			echo "Adding account failed: (" . $result->errno . ") " . $result->error;
272
			$success = FALSE;
273
	}	
274
	
275
	return $device_uid;
276
}
277
278
function getPosts($lastPostId, $accessToken, $url, $version = 'v2')
279
{	
280
	$accountCreator = new GetPosts();
281
	$accountCreator->setLastPostId($lastPostId);
282
	$accountCreator->setAccessToken($accessToken);
283
	$accountCreator->setUrl($url);
284
	$accountCreator->version = $version;
285
286
	$location = new Location();
287
	$location->setLat(52.520006);
288
	$location->setLng(13.404954);
289
	$location->setCityName('Berlin');
290
	$accountCreator->location = $location;
291
	$data = $accountCreator->execute();
292
	
293
	return $data;
294
}
295
296
function createAccount()
297
{
298
	$location = new Location();
299
	$location->setLat(52.520006);
300
	$location->setLng(13.404954);
301
	$location->setCityName('Berlin');
302
303
	$device_uid = registerAccount($location);
304
305
	return $device_uid;
306
}
307
308
function isUserBot()
309
{
310
	preg_match('/bot|spider|google|twitter/i', $_SERVER['HTTP_USER_AGENT'], $matches);
311
312
    return (isset($matches[0])) ? true : false;
313
}
314
315
function botDeviceUidIsSet($config)
316
{
317
	if(!array_key_exists('botDeviceUid', $config) || !isset($config['botDeviceUid']) || $config['botDeviceUid'] == '' || $config['botDeviceUid'] == 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
318
	{
319
		return FALSE;
320
	}
321
	else
322
	{
323
		return TRUE;
324
	}
325
}
326
327
function jodelToHtml($post, $view = 'time', $isDetailedView = FALSE)
0 ignored issues
show
jodelToHtml uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
328
{	//ToDO
329
	//Replace # with link
330
	//preg_replace('~(\#)([^\s!,. /()"\'?]+)~', '<a href="tag/$2">#$2</a>', $text);
331
332
	//Time to time difference
333
	$now = new DateTime();
334
	$d = new DateTime($post['created_at']);
335
	$timediff = $now->diff($d);
336
337
	$timediff_inSeconds = (string)$timediff->format('%s');
338
	$timediff_inMinutes = (string)$timediff->format('%i');
339
	$timediff_inHours = (string)$timediff->format('%h');
340
	$timediff_inDays = (string)$timediff->format('%d');
341
	$timediff_inMonth = (string)$timediff->format('%m');
342
343
	if($timediff_inMonth!=0)
344
	{
345
			$timediff = $timediff_inMonth . "m";
346
	}
347
	else
348
	{
349
		if($timediff_inDays!=0)
350
		{
351
			$timediff = $timediff_inDays . "d";
352
		}
353
		else
354
		{
355
			if($timediff_inHours!=0)
356
			{
357
				$timediff = $timediff_inHours . "h";
358
			}
359
			else
360
			{
361
				if($timediff_inMinutes!=0)
362
				{
363
					$timediff = $timediff_inMinutes . "m";
364
				}
365
				else
366
				{
367
					$timediff = $timediff_inSeconds . "s";
368
				}
369
			}
370
		}
371
	}
372
373
374
	?>
375
	<article id ="postId-<?php echo $post["post_id"]; ?>" class="jodel" style="background-color: #<?php echo $post["color"];?>;">
376
		<content>
377
			<?php 
378
			if(isset($post["image_url"])) {
379
				echo '<img src="' . $post["image_url"] . '">';
380
			}
381
			else {
382
				echo str_replace('  ', ' &nbsp;', nl2br(htmlspecialchars($post["message"])));
383
			}
384
			?>
385
		</content>
386
		<aside>
387
			<?php
388 View Code Duplication
				if($isDetailedView)
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
389
				{?>
390
					<a href="index.php?vote=up&getPostDetails=true&postID=<?php echo $post['post_id'];?>&postID_parent=<?php echo htmlspecialchars($_GET['postID']);?>">
0 ignored issues
show
This line exceeds maximum limit of 120 characters; contains 153 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
391
		  <?php }
392
				else
393
				{?>
394
					<a href="index.php?vote=up&postID=<?php echo $post['post_id'];?>">
395
		  <?php } ?>
396
						<i class="fa fa-angle-up fa-3x"></i>
397
					</a>	
398
						<br />
399
					<?php echo $post["vote_count"];?><br />
400
			<?php
401 View Code Duplication
				if($isDetailedView)
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
402
				{?>
403
					<a href="index.php?vote=down&getPostDetails=true&postID=<?php echo $post['post_id'];?>&postID_parent=<?php echo htmlspecialchars($_GET['postID']);?>">
0 ignored issues
show
This line exceeds maximum limit of 120 characters; contains 155 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
404
		  <?php }
405
				else
406
				{?>
407
					<a href="index.php?vote=down&postID=<?php echo $post['post_id'];?>">
408
		  <?php } ?>
409
						<i class="fa fa-angle-down fa-3x"></i>
410
					</a>
411
		</aside>
412
413
		<footer>
414
			<table>
415
				<tr>
416
					<td class="time">
417
						<span class="tip" data-tooltip="Time">
418
							<i class="fa fa-clock-o"></i>
419
							<?php echo $timediff;?>
420
							<span class="tiptext"><?php echo $d->format('Y-m-d H:i:s');?></span>
421
						</span> 
422
					</td>
423
					<td class="comments">
424
						<?php if(!$isDetailedView) {?>
425
						<span data-tooltip="Comments">
426
							<a href="index.php?getPostDetails=true&view=<?php echo $view;?>&postID=<?php echo $post["post_id"];?>">
427
								<i class="fa fa-commenting-o"></i>
428
								<?php if(array_key_exists("child_count", $post)) {
429
											echo $post["child_count"];
430
										} else echo "0";
431
								?>
432
								</a>
433
						</span>
434
						<?php } ?>
435
					</td>
436
					<td class="distance">
437
						<?php
438
							if($isDetailedView)
439
							{
440
								if(isset($post["parent_creator"]) && $post["parent_creator"] == 1)
441
								{
442
									?>
443
									<span data-tooltip="Author">
444
										<i class="fa fa-user-o"></i> OJ |
445
									</span>
446
									<?php 
447
						  		}
448
						  		else
449
						  		{
450
						  			//Is not parent Jodel in detailed View
451
									if(!array_key_exists('child_count', $post) && array_key_exists('parent_creator', $post))
452
									{
453
							  			?>
454
							  			<span data-tooltip="Author">
455
											<i class="fa fa-user-o"></i> #<?php echo $post["user_handle"];?> |
456
										</span>
457
										<?php
458
									}
459
						  		}
460
						  	}
461
					  		?>
462
463
						<span class="tip" data-tooltip="Distance">
464
							<i class="fa fa-map-marker"></i>
465
							<?php echo $post['distance'];?> km
466
							<span class="tiptext"><?php echo $post['location']['name'];?></span>
467
						</span>
468
					</td>
469
				</tr>
470
			</table>
471
		</footer>
472
	</article>
473
<?php
474
}