Completed
Push — master ( cd6dd3...38f7f4 )
by mains
96:30 queued 54:56
created

index.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/jodel-web.php';
4
5
	$location = new Location();
6
	$location->setLat('0.1');
7
	$location->setLng('0.1');
8
	$location->setCityName('Munich');
9
10
	isTokenFresh($location);
11
12
	$result = $db->query("SELECT * FROM accounts WHERE id='1'");
13
	
14
	$accessToken;
15
	$newPositionStatus;
16
	
17 View Code Duplication
	if ($result->num_rows > 0)
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...
18
	{
19
		// output data of each row
20
		while($row = $result->fetch_assoc())
21
		{
22
			$accessToken = $row["access_token"];
23
		}
24
	}
25
	else
26
	{
27
		echo "Error: 0 results";
28
	}
29
	
30
	
31
	//createAccount();
32
	
33
	//Set Location
34
	if(isset($_GET['city'])) {
35
		$url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' . htmlspecialchars($_GET['city']) . '&key=AIzaSyCwhnja-or07012HqrhPW7prHEDuSvFT4w';
36
		$result = Requests::post($url);
37
		if(json_decode($result->body, true)['status'] == 'ZERO_RESULTS' || json_decode($result->body, true)['status'] == 'INVALID_REQUEST')
38
		{
39
			$newPositionStatus = "0 results";
40
		}
41
		else
42
		{
43
			$location = new Location();
44
			$location->setLat(json_decode($result->body, true)['results']['0']['geometry']['location']['lat']);
45
			$location->setLng(json_decode($result->body, true)['results']['0']['geometry']['location']['lng']);
46
			$location->setCityName(htmlspecialchars($_GET['city']));
47
			$accountCreator = new UpdateLocation();
48
			$accountCreator->setLocation($location);
49
			$accountCreator->setAccessToken($accessToken);
50
			$data = $accountCreator->execute();
51
		}
52
	}
53
	
54
	//Vote
55
	if(isset($_GET['vote']) && isset($_GET['postID'])) {
56
		if($_GET['vote'] == "up") {
57
			$accountCreator = new Upvote();
58
		}
59
		else if($_GET['vote'] == "down") {
60
			$accountCreator = new Downvote();
61
		}
62
		$accountCreator->setAccessToken($accessToken);
63
		$data = $accountCreator->execute();
64
65
		header("Location: index.php#postId-" . htmlspecialchars($_GET['postID']));
66
		die();
67
	}
68
	
69
	
70
	//SendJodel
71
	if(isset($_POST['message'])) {
72
		$accountCreator = new SendJodel();
73
74
		if(isset($_POST['ancestor']))
75
		{
76
			$ancestor = $_POST['ancestor'];
77
			$accountCreator->ancestor = $ancestor;
78
		}
79
		if(isset($_POST['color']))
80
		{
81
			$color = $_POST['color'];
82
			switch ($color) {
83
				case '8ABDB0':
84
					$color = '8ABDB0';
85
					break;
86
				case '9EC41C':
87
					$color = '9EC41C';
88
					break;
89
				case '06A3CB':
90
					$color = '06A3CB';
91
					break;
92
				case 'FFBA00':
93
					$color = 'FFBA00';
94
					break;
95
				case 'DD5F5F':
96
					$color = 'DD5F5F';
97
					break;
98
				case 'FF9908':
99
					$color = 'FF9908';
100
					break;
101
				
102
				default:
103
					$color = '8ABDB0';
104
					break;
105
			}
106
			$accountCreator->color = $color;
107
			echo "Setting color:" . $color;
108
		}
109
		
110
		$location = new Location();
111
		$location->setLat('0.1');
112
		$location->setLng('0.1');
113
		$location->setCityName('Munich');
114
		
115
		$accountCreator->location = $location;
116
		
117
		$accountCreator->setAccessToken($accessToken);
118
		$data = $accountCreator->execute();
119
	}
120
?>
121
<!DOCTYPE html>
122
<html lang="de">
123
	<head>
124
		<title>JodelBlue WebClient - </title>
125
		
126
		<meta charset="utf8" />
127
		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
128
		<meta http-equiv="x-ua-compatible" content="ie=edge">
129
		
130
		<meta name="description" content=""/>
131
		<meta name="keywords" content=""/>
132
		
133
		<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/css/bootstrap.min.css" integrity="sha384-AysaV+vQoT3kOAXZkl02PThvDr8HYKPZhNT5h/CXfBThSRXQ6jW5DO2ekP5ViFdi" crossorigin="anonymous">
134
		<link rel="stylesheet" href="css/font-awesome.min.css">
135
		<link rel="stylesheet" href="style.css" type="text/css">
136
		
137
		<link rel="shortcut icon" href="img/favicon/favicon.ico" type="image/x-icon">
138
		<link rel="icon" href="img/favicon/favicon.ico" type="image/x-icon">	
139
	</head>
140
	
141
	<body>
142
		<header>
143
			<nav class="navbar navbar-full navbar-dark navbar-fixed-top">
144
				<div class="container">					
145
						<?php
146
							if(isset($_GET['postID']) && isset($_GET['getPostDetails']))
147
							{
148
								echo '<a id="comment-back" href="index.php?view=' . $view . '#postId-' . htmlspecialchars($_GET['postID']) . '">';
149
								echo '<i class="fa fa-angle-left fa-3x"></i>';
150
								echo '</a>';
151
								echo '<h1>';
152
								echo '<a href="index.php?getPostDetails=' . htmlspecialchars($_GET['getPostDetails']) . '&postID=' . htmlspecialchars($_GET['postID']) . '" class="spinnable">';
153
							}
154
							else
155
							{
156
								echo '<h1>';	
157
								echo '<a href="index.php" class="spinnable">';
158
							}
159
						?>
160
						JodelBlue <i class="fa fa-refresh fa-1x"></i></a>
161
					</h1>					
162
				</div>
163
			</nav>
164
		</header>
165
		
166
		<div class="mainContent container">		
167
			<div class="content row">
168
				<article class="topContent col-sm-8">
169
170
					<content id="posts">
171
						<?php
172
							$posts;
173
174
							//Set View
175 View Code Duplication
							if(isset($_GET['view']))
176
							{
177
								switch ($_GET['view']) {
178
									case 'comment':
179
										$view = 'comment';
180
										break;
181
									
182
									case 'upVote':
183
										$view = 'upVote';
184
										break;
185
186
									default:
187
										$view = 'time';
188
										break;
189
								}
190
							}
191
							else
192
							{
193
								$view = 'time';
194
							}
195
196
							//Get Post Details
197
							if(isset($_GET['postID']) && isset($_GET['getPostDetails']))
198
							{
199
								$accountCreator = new GetPostDetails();
200
								$accountCreator->setAccessToken($accessToken);
201
								$data = $accountCreator->execute();
202
								
203
								$posts[0] = $data;
204
								if(isset($data['children'])) {
205
									foreach($data['children'] as $child) {
206
										array_push($posts, $child);
207
									}
208
									$loops = $data['child_count'] + 1;
209
								}
210
								else $loops = 1;
211
								$showCommentIcon = FALSE;
212
							}
213
							//Get Posts
214
							else
215
							{
216 View Code Duplication
								if($view=='comment')
217
								{
218
									$url = "/v2/posts/location/discussed/";
219
								}
220
								else
221
								{
222
									if($view=='upVote')
223
									{
224
										$url = "/v2/posts/location/popular/";
225
									}
226
									else
227
									{
228
										$url = "/v2/posts/location/";
229
									}
230
								}
231
232
								$posts = getPosts($lastPostId, $accessToken, $url)['posts'];
233
								$loops = 29;
234
								$showCommentIcon = TRUE;
235
							}
236
							
237
238 View Code Duplication
							for($i = 0; $i<$loops; $i++) {
239
							
240
							if(isset($posts[$i])) {
241
							$lastPostId = $posts[$i]['post_id'];
242
243
							
244
							$now = new DateTime();
245
							$d = new DateTime($posts[$i]["created_at"]);
246
							
247
							
248
							//Time to time difference
249
							$timediff = $now->diff($d);
250
251
							$timediff_inSeconds = (string)$timediff->format('%s');
252
							$timediff_inMinutes = (string)$timediff->format('%i');
253
							$timediff_inHours = (string)$timediff->format('%h');
254
							$timediff_inDays = (string)$timediff->format('%d');
255
							$timediff_inMonth = (string)$timediff->format('%m');
256
							if($timediff_inMonth!=0) {
257
									$timediff = $timediff_inMonth . "m";
258
							}
259
							else
260
							{
261
								if($timediff_inDays!=0)
262
								{
263
									$timediff = $timediff_inDays . "d";
264
								}
265
								else
266
								{
267
									if($timediff_inHours!=0)
268
									{
269
										$timediff = $timediff_inHours . "h";
270
									}
271
									else
272
									{
273
										if($timediff_inMinutes!=0)
274
										{
275
											$timediff = $timediff_inMinutes . "m";
276
										}
277
										else
278
										{
279
											$timediff = $timediff_inSeconds . "s";
280
										}
281
									}
282
								}
283
							}
284
						?>
285
						
286
						<article id ="postId-<?php echo $posts[$i]["post_id"]; ?>" class="jodel" style="background-color: #<?php echo $posts[$i]["color"];?>;">
287
							<content>
288
								<?php 
289
								if(isset($posts[$i]["image_url"])) {
290
									echo '<img src="' . $posts[$i]["image_url"] . '">';
291
								}
292
								else {
293
									echo str_replace('  ', ' &nbsp;', nl2br(htmlspecialchars($posts[$i]["message"])));
294
								}
295
								?>
296
							</content>
297
							<aside>
298
								<a href="index.php?vote=up&postID=<?php echo $posts[$i]["post_id"];?>">
299
									<i class="fa fa-angle-up fa-3x"></i>
300
								</a>	
301
									<br />
302
								<?php echo $posts[$i]["vote_count"];?><br />
303
								<a href="index.php?vote=down&postID=<?php echo $posts[$i]["post_id"];?>">
304
									<i class="fa fa-angle-down fa-3x"></i>
305
								</a>
306
							</aside>
307
						
308
							<footer>
309
								<table>
310
									<tr>
311
										<td class="time">
312
											<span data-tooltip="Time">
313
												<i class="fa fa-clock-o"></i>
314
												<?php echo $timediff;?>
315
											</span> 
316
										</td>
317
										<td class="comments">
318
											<?php if($showCommentIcon) {?>
319
											<span data-tooltip="Comments">
320
												<a href="index.php?getPostDetails=true&view=<?php echo $view;?>&postID=<?php echo $posts[$i]["post_id"];?>">
321
													<i class="fa fa-commenting-o"></i>
322
													<?php if(array_key_exists("child_count", $posts[$i])) {
323
																echo $posts[$i]["child_count"];
324
															} else echo "0";
325
													?>
326
													</a>
327
											</span>
328
											<?php } ?>
329
										</td>
330
										<td class="distance">
331
											<span data-tooltip="Distance">
332
												<i class="fa fa-map-marker"></i>
333
												<?php echo $posts[$i]["distance"];?> km
334
											</span>
335
										</td>
336
									</tr>
337
								</table>
338
							</footer>
339
						</article>
340
						
341
342
						
343
						<?php }
344
						} ?>
345
346
					</content>
347
					
348
					<?php if(!isset($_GET['postID']) && !isset($_GET['getPostDetails'])) { ?>
349
						<p id="loading">
350
							Loading…
351
						</p>
352
					<?php } ?>
353
				</article>
354
			
355
				<aside class="topSidebar col-sm-4 sidebar-outer">
356
					<div class="fixed">
357
						<article>
358
							<div>
359
								<h2>Position</h2>
360
								<form method="get">
361
									<input type="text" id="city" name="city" placeholder="<?php if(isset($newPositionStatus)) echo $newPositionStatus; else echo htmlspecialchars($posts[0]["location"]["name"]); ?>" required>
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal location does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Comprehensibility introduced by
The string literal name does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
This line exceeds maximum limit of 120 characters; contains 196 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...
362
363
									<input type="submit" value="Set Location" /> 
364
								</form>
365
							</div>
366
						</article>
367
368
						<article>
369
							<div>
370
								<h2>Karma</h2>
371
								<?php echo getKarma($accessToken); ?>
372
							</div>
373
						</article>
374
375
						<article>
376
							<div>
377
								<?php if(isset($_GET['postID']) && isset($_GET['getPostDetails'])) { ?>
378
								<h2>Comment on Jodel</h2>
379
								<form method="POST">				
380
										<input type="hidden" name="ancestor" value="<?php echo htmlspecialchars($_GET['postID']);?>" />
381
										<textarea id="message" name="message" placeholder="Send a comment on a Jodel to all students within 10km" required></textarea> 
382
									<br />
383
									<input type="submit" value="SEND" /> 
384
								</form>
385
									<?php } else { ?>
386
								<h2>New Jodel</h2>
387
								<form method="POST">
388
									<textarea id="message" name="message" placeholder="Send a Jodel to all students within 10km" required></textarea> 
389
									<br />
390
									<select id="postColorPicker" name="color">
391
										<option value="06A3CB">Blue</option>
392
										<option value="8ABDB0">Teal</option>
393
										<option value="9EC41C">Green</option>
394
										<option value="FFBA00">Yellow</option>
395
										<option value="DD5F5F">Red</option>
396
										<option value="FF9908">Orange</option>
397
									</select> 
398
									<br />
399
									<input type="submit" value="SEND" /> 
400
								</form>
401
								<?php } ?>
402
							</div>
403
						</article>
404
							
405
						<article>
406
							<div>
407
								<h2>Login</h2>
408
							</div>
409
						</article>
410
					</div>
411
				</aside>
412
			</div>
413
			<div id="sortJodelBy" class="row">
414
				<div class="col-sm-12">
415
					<div class="row">
416
						<div class="col-sm-3">
417
							<a href="index.php" <?php if($view=='time') echo 'class="active"';?>><i class="fa fa-clock-o fa-3x"></i></a>
418
						</div>
419
						<div class="col-sm-3">
420
							<a href="index.php?view=comment" <?php if($view=='comment') echo 'class="active"';?>><i class="fa fa-commenting-o fa-3x"></i></a>
421
						</div>
422
						<div class="col-sm-3">
423
							<a href="index.php?view=upVote" <?php if($view=='upVote') echo 'class="active"';?>><i class="fa fa-angle-up fa-3x"></i></a>
424
						</div>
425
						<div class="col-sm-3">
426
							<nav>
427
								<a href="./impressum.html">Impressum</a> | <a href="./datenschutz.html">Datenschutz</a>
428
							</nav>
429
						</div>
430
					</div>
431
				</div>	
432
			</div>
433
		</div>
434
		
435
		
436
		<!-- jQuery, Tether and Bootstrap JS -->
437
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js" integrity="sha384-3ceskX3iaEnIogmQchP8opvBy3Mi7Ce34nWjpBIwVTHfGYWQS9jwHDVRnpKKHJg7" crossorigin="anonymous"></script>
438
    	<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.3.7/js/tether.min.js" integrity="sha384-XTs3FgkjiBgo8qjEjBk0tGmf3wPrWtA6coPfQDfFEY8AnYJwjalXCiosYRBIBZX8" crossorigin="anonymous"></script>
439
    	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/js/bootstrap.min.js" integrity="sha384-BLiI7JTZm+JWlgKa0M0kGRpJbF2J8q+qreVrKBC47e3K6BW78kGLrCkeRX6I9RoK" crossorigin="anonymous"></script>
440
441
		<script>
442
443
444
			$('a').on('click', function(){
445
			    $('a').removeClass('selected');
446
			    $(this).addClass('selected');
447
			});
448
449
			function scrollToAnchor(aid){
450
			    var aTag = $("article[id='"+ aid +"']");
451
			    $('html,body').animate({scrollTop: aTag.offset().top-90},'slow');
452
			}
453
454
455
			<?php if(!isset($_GET['postID']) && !isset($_GET['getPostDetails'])) { ?>
456
			$(document).ready(function() {
457
				var win = $(window);
458
				var lastPostId = "<?php echo $lastPostId; ?>";
459
				var view = "<?php echo $view; ?>"
460
				var old_lastPostId = "";
461
				var morePostsAvailable = true;
462
463
				if(window.location.hash)
464
				{
465
					var hash = window.location.hash.slice(1);
466
467
					if(!$("article[id='"+ hash +"']").length)
468
					{
469
						for (var i = 5; i >= 0; i--)
470
						{
471
							if(!$("article[id='"+ hash +"']").length)
472
							{
473
								$.ajax({
474
									url: 'get-posts-ajax.php?lastPostId=' + lastPostId + '&view=' + view,
475
									dataType: 'html',
476
									async: false,
477
									success: function(html) {
478
										var div = document.createElement('div');
479
										div.innerHTML = html;
480
										var elements = div.childNodes;
481
										old_lastPostId = lastPostId;
482
										lastPostId = elements[3].textContent;
483
										lastPostId = lastPostId.replace(/\s+/g, '');
484
										//alert('Neu: ' + lastPostId + " Alt: " + old_lastPostId);
485
										if(lastPostId == old_lastPostId) {
486
											
487
											//morePostsAvailable = false;
488
										}
489
										else {
490
											//alert(elements[3].textContent);
491
											$('#posts').append(elements[1].innerHTML);
492
											$('#posts').hide().show(0);
493
										}
494
										$('#loading').hide();
495
									}
496
								});
497
							}
498
							
499
						}
500
						scrollToAnchor(hash);
501
502
					}						
503
				}
504
505
				// Each time the user scrolls
506
				win.scroll(function() {
507
					// End of the document reached?
508
					if (($(document).height() - win.height() == win.scrollTop()) && morePostsAvailable) {
509
						$('#loading').show();
510
511
						
512
						
513
						$.ajax({
514
							url: 'get-posts-ajax.php?lastPostId=' + lastPostId + '&view=' + view,
515
							dataType: 'html',
516
							async: true,
517
							success: function(html) {
518
								var div = document.createElement('div');
519
								div.innerHTML = html;
520
								var elements = div.childNodes;
521
								old_lastPostId = lastPostId;
522
								lastPostId = elements[3].textContent;
523
								lastPostId = lastPostId.replace(/\s+/g, '');
524
								//alert('Neu: ' + lastPostId + " Alt: " + old_lastPostId);
525
								if(lastPostId == old_lastPostId) {
526
									
527
									//morePostsAvailable = false;
528
								}
529
								else {
530
									//alert(elements[3].textContent);
531
									$('#posts').append(elements[1].innerHTML);
532
								}
533
								$('#loading').hide();
534
							}
535
						});
536
					}
537
				});
538
			});	
539
		<?php } ?>
540
		</script>
541
542
	</body>
543
</html>
544
545