Completed
Push — master ( e1cefd...355fa6 )
by mains
02:30
created

index.php (1 issue)

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
	if ($result->num_rows > 0)
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
		$ancestor;
73
		if(isset($_POST['ancestor']))
74
		{
75
			$ancestor = $_POST['ancestor'];
76
		}
77
		
78
		$location = new Location();
79
		$location->setLat('0.1');
80
		$location->setLng('0.1');
81
		$location->setCityName('Munich');
82
		$accountCreator = new SendJodel();
83
		$accountCreator->setLocation($location);
84
		$accountCreator->setAncestor($ancestor);
85
		$accountCreator->setAccessToken($accessToken);
86
		$data = $accountCreator->execute();
87
	}
88
?>
89
<!DOCTYPE html>
90
<html lang="de">
91
	<head>
92
		<title>JodelBlue WebClient - </title>
93
		
94
		<meta charset="utf8" />
95
		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
96
		<meta http-equiv="x-ua-compatible" content="ie=edge">
97
		
98
		<meta name="description" content=""/>
99
		<meta name="keywords" content=""/>
100
		
101
		<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">
102
		<link rel="stylesheet" href="css/font-awesome.min.css">
103
		<link rel="stylesheet" href="style.css" type="text/css">
104
		
105
		<link rel="shortcut icon" href="img/favicon/favicon.ico" type="image/x-icon">
106
		<link rel="icon" href="img/favicon/favicon.ico" type="image/x-icon">	
107
	</head>
108
	
109
	<body>
110
		<header>
111
			<nav class="navbar navbar-full navbar-dark navbar-fixed-top">
112
				<div class="container">
113
		  			<a href="index.php">
114
						<h1>
115
							JodelBlue
116
							<?php if(!isset($_GET['postID']) && !isset($_GET['getPostDetails'])) echo '<i class="fa fa-refresh fa-1x"></i>';?>
117
						</h1>					
118
					</a>
119
				</div>
120
			</nav>
121
		</header>
122
		
123
		<div class="mainContent container">		
124
			<div class="content row">
125
				<article class="topContent col-sm-8">
126
127
					<content id="posts">
128
						<?php
129
							$posts;
130
131
							//Set View
132 View Code Duplication
							if(isset($_GET['view']))
133
							{
134
								switch ($_GET['view']) {
135
									case 'comment':
136
										$view = 'comment';
137
										break;
138
									
139
									case 'upVote':
140
										$view = 'upVote';
141
										break;
142
143
									default:
144
										$view = 'time';
145
										break;
146
								}
147
							}
148
							else
149
							{
150
								$view = 'time';
151
							}
152
153
							//Get Post Details
154
							if(isset($_GET['postID']) && isset($_GET['getPostDetails'])) {
155
								//Header Nav in Comment View
156
								?>
157
								<a id="comment-back" href="index.php?view=<?php echo $view;?>#postId-<?php echo htmlspecialchars($_GET['postID']);?>">
158
									<i class="fa fa-angle-left fa-3x"></i>
159
								</a>
160
161
								<a id="comment-refresh" href="index.php?getPostDetails=<?php echo htmlspecialchars($_GET['getPostDetails']);?>&postID=<?php echo htmlspecialchars($_GET['postID']);?>">
162
									<i class="fa fa-refresh fa-2x"></i>
163
								</a>
164
								<?php
165
166
167
								$accountCreator = new GetPostDetails();
168
								$accountCreator->setAccessToken($accessToken);
169
								$data = $accountCreator->execute();
170
								
171
								$posts[0] = $data;
172
								if(isset($data['children'])) {
173
									foreach($data['children'] as $child) {
174
										array_push($posts, $child);
175
									}
176
									$loops = $data['child_count'] + 1;
177
								}
178
								else $loops = 1;
179
								$showCommentIcon = FALSE;
180
							}
181
							//Get Posts
182
							else
183
							{
184 View Code Duplication
								if($view=='comment')
185
								{
186
									$url = "/v2/posts/location/discussed/";
187
								}
188
								else
189
								{
190
									if($view=='upVote')
191
									{
192
										$url = "/v2/posts/location/popular/";
193
									}
194
									else
195
									{
196
										$url = "/v2/posts/location/";
197
									}
198
								}
199
200
								$posts = getPosts($lastPostId, $accessToken, $url)['posts'];
201
								$loops = 29;
202
								$showCommentIcon = TRUE;
203
							}
204
							
205
206 View Code Duplication
							for($i = 0; $i<$loops; $i++) {
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...
207
							
208
							if(isset($posts[$i])) {
209
							$lastPostId = $posts[$i]['post_id'];
210
211
							
212
							$now = new DateTime();
213
							$d = new DateTime($posts[$i]["created_at"]);
214
							
215
							
216
							//Time to time difference
217
							$timediff = $now->diff($d);
218
219
							$timediff_inSeconds = (string)$timediff->format('%s');
220
							$timediff_inMinutes = (string)$timediff->format('%i');
221
							$timediff_inHours = (string)$timediff->format('%h');
222
							$timediff_inDays = (string)$timediff->format('%d');
223
							$timediff_inMonth = (string)$timediff->format('%m');
224
							if($timediff_inMonth!=0) {
225
									$timediff = $timediff_inMonth . "m";
226
							}
227
							else
228
							{
229
								if($timediff_inDays!=0)
230
								{
231
									$timediff = $timediff_inDays . "d";
232
								}
233
								else
234
								{
235
									if($timediff_inHours!=0)
236
									{
237
										$timediff = $timediff_inHours . "h";
238
									}
239
									else
240
									{
241
										if($timediff_inMinutes!=0)
242
										{
243
											$timediff = $timediff_inMinutes . "m";
244
										}
245
										else
246
										{
247
											$timediff = $timediff_inSeconds . "s";
248
										}
249
									}
250
								}
251
							}
252
						?>
253
						
254
						<article id ="postId-<?php echo $posts[$i]["post_id"]; ?>" class="jodel" style="background-color: #<?php echo $posts[$i]["color"];?>;">
255
							<content>
256
								<?php 
257
								if(isset($posts[$i]["image_url"])) {
258
									echo '<img src="' . $posts[$i]["image_url"] . '">';
259
								}
260
								else {
261
									echo str_replace('  ', ' &nbsp;', nl2br(htmlspecialchars($posts[$i]["message"])));
262
								}
263
								?>
264
							</content>
265
							<aside>
266
								<a href="index.php?vote=up&postID=<?php echo $posts[$i]["post_id"];?>">
267
									<i class="fa fa-angle-up fa-3x"></i>
268
								</a>	
269
									<br />
270
								<?php echo $posts[$i]["vote_count"];?><br />
271
								<a href="index.php?vote=down&postID=<?php echo $posts[$i]["post_id"];?>">
272
									<i class="fa fa-angle-down fa-3x"></i>
273
								</a>
274
							</aside>
275
						
276
							<footer>
277
								<table>
278
									<tr>
279
										<td class="time">
280
											<span data-tooltip="Time">
281
												<i class="fa fa-clock-o"></i>
282
												<?php echo $timediff;?>
283
											</span> 
284
										</td>
285
										<td class="comments">
286
											<?php if($showCommentIcon) {?>
287
											<span data-tooltip="Comments">
288
												<a href="index.php?getPostDetails=true&view=<?php echo $view;?>&postID=<?php echo $posts[$i]["post_id"];?>">
289
													<i class="fa fa-commenting-o"></i>
290
													<?php if(array_key_exists("child_count", $posts[$i])) {
291
																echo $posts[$i]["child_count"];
292
															} else echo "0";
293
													?>
294
													</a>
295
											</span>
296
											<?php } ?>
297
										</td>
298
										<td class="distance">
299
											<span data-tooltip="Distance">
300
												<i class="fa fa-map-marker"></i>
301
												<?php echo $posts[$i]["distance"];?> km
302
											</span>
303
										</td>
304
									</tr>
305
								</table>
306
							</footer>
307
						</article>
308
						
309
310
						
311
						<?php }
312
						} ?>
313
314
					</content>
315
					
316
					<?php if(!isset($_GET['postID']) && !isset($_GET['getPostDetails'])) { ?>
317
						<p id="loading">
318
							Loading…
319
						</p>
320
					<?php } ?>
321
				</article>
322
			
323
				<aside class="topSidebar col-sm-4 sidebar-outer">
324
					<div class="fixed">
325
						<article>
326
							<div>
327
								<h2>Position</h2>
328
								<form method="get">
329
									<input type="text" id="city" name="city" placeholder="<?php if(isset($newPositionStatus)) echo $newPositionStatus; else echo htmlspecialchars($posts[0]["location"]["name"]); ?>" required>
330
331
									<input type="submit" value="Set Location" /> 
332
								</form>
333
							</div>
334
						</article>
335
336
						<article>
337
							<div>
338
								<h2>Karma</h2>
339
								<?php echo getKarma($accessToken); ?>
340
							</div>
341
						</article>
342
343
						<article>
344
							<div>
345
								<?php if(isset($_GET['postID']) && isset($_GET['getPostDetails'])) { ?>
346
								<h2>Comment on Jodel</h2>
347
								<form method="POST">				
348
										<input type="hidden" name="ancestor" value="<?php echo htmlspecialchars($_GET['postID']);?>" />
349
										<textarea id="message" name="message" placeholder="Send a comment on a Jodel to all students within 10km" required></textarea> 
350
									<br />
351
									<input type="submit" value="SEND" /> 
352
								</form>
353
									<?php } else { ?>
354
								<h2>New Jodel</h2>
355
								<form method="POST">
356
									<textarea id="message" name="message" placeholder="Send a Jodel to all students within 10km" required></textarea> 
357
									<br />
358
									<input type="submit" value="SEND" /> 
359
								</form>
360
								<?php } ?>
361
							</div>
362
						</article>
363
							
364
						<article>
365
							<div>
366
								<h2>Login</h2>
367
							</div>
368
						</article>
369
					</div>
370
				</aside>
371
			</div>
372
			<div id="sortJodelBy" class="row">
373
				<div class="col-sm-12">
374
					<div class="row">
375
						<div class="col-sm-3">
376
							<a href="index.php" <?php if($view=='time') echo 'class="active"';?>><i class="fa fa-clock-o fa-3x"></i></a>
377
						</div>
378
						<div class="col-sm-3">
379
							<a href="index.php?view=comment" <?php if($view=='comment') echo 'class="active"';?>><i class="fa fa-commenting-o fa-3x"></i></a>
380
						</div>
381
						<div class="col-sm-3">
382
							<a href="index.php?view=upVote" <?php if($view=='upVote') echo 'class="active"';?>><i class="fa fa-angle-up fa-3x"></i></a>
383
						</div>
384
						<div class="col-sm-3">
385
							<nav>
386
								<a href="./impressum.html">Impressum</a> | <a href="./datenschutz.html">Datenschutz</a>
387
							</nav>
388
						</div>
389
					</div>
390
				</div>	
391
			</div>
392
		</div>
393
		
394
		
395
		<!-- jQuery, Tether and Bootstrap JS -->
396
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js" integrity="sha384-3ceskX3iaEnIogmQchP8opvBy3Mi7Ce34nWjpBIwVTHfGYWQS9jwHDVRnpKKHJg7" crossorigin="anonymous"></script>
397
    	<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.3.7/js/tether.min.js" integrity="sha384-XTs3FgkjiBgo8qjEjBk0tGmf3wPrWtA6coPfQDfFEY8AnYJwjalXCiosYRBIBZX8" crossorigin="anonymous"></script>
398
    	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/js/bootstrap.min.js" integrity="sha384-BLiI7JTZm+JWlgKa0M0kGRpJbF2J8q+qreVrKBC47e3K6BW78kGLrCkeRX6I9RoK" crossorigin="anonymous"></script>
399
400
		<script>
401
402
403
			$('a').on('click', function(){
404
			    $('a').removeClass('selected');
405
			    $(this).addClass('selected');
406
			});
407
408
			function scrollToAnchor(aid){
409
			    var aTag = $("article[id='"+ aid +"']");
410
			    $('html,body').animate({scrollTop: aTag.offset().top-90},'slow');
411
			}
412
413
414
			<?php if(!isset($_GET['postID']) && !isset($_GET['getPostDetails'])) { ?>
415
			$(document).ready(function() {
416
				var win = $(window);
417
				var lastPostId = "<?php echo $lastPostId; ?>";
418
				var view = "<?php echo $view; ?>"
419
				var old_lastPostId = "";
420
				var morePostsAvailable = true;
421
422
				if(window.location.hash)
423
				{
424
					var hash = window.location.hash.slice(1);
425
426
					if(!$("article[id='"+ hash +"']").length)
427
					{
428
						for (var i = 5; i >= 0; i--)
429
						{
430
							if(!$("article[id='"+ hash +"']").length)
431
							{
432
								$.ajax({
433
									url: 'get-posts-ajax.php?lastPostId=' + lastPostId + '&view=' + view,
434
									dataType: 'html',
435
									async: false,
436
									success: function(html) {
437
										var div = document.createElement('div');
438
										div.innerHTML = html;
439
										var elements = div.childNodes;
440
										old_lastPostId = lastPostId;
441
										lastPostId = elements[3].textContent;
442
										lastPostId = lastPostId.replace(/\s+/g, '');
443
										//alert('Neu: ' + lastPostId + " Alt: " + old_lastPostId);
444
										if(lastPostId == old_lastPostId) {
445
											
446
											//morePostsAvailable = false;
447
										}
448
										else {
449
											//alert(elements[3].textContent);
450
											$('#posts').append(elements[1].innerHTML);
451
											$('#posts').hide().show(0);
452
										}
453
										$('#loading').hide();
454
									}
455
								});
456
							}
457
							
458
						}
459
						scrollToAnchor(hash);
460
461
					}						
462
				}
463
464
				// Each time the user scrolls
465
				win.scroll(function() {
466
					// End of the document reached?
467
					if (($(document).height() - win.height() == win.scrollTop()) && morePostsAvailable) {
468
						$('#loading').show();
469
470
						
471
						
472
						$.ajax({
473
							url: 'get-posts-ajax.php?lastPostId=' + lastPostId + '&view=' + view,
474
							dataType: 'html',
475
							async: true,
476
							success: function(html) {
477
								var div = document.createElement('div');
478
								div.innerHTML = html;
479
								var elements = div.childNodes;
480
								old_lastPostId = lastPostId;
481
								lastPostId = elements[3].textContent;
482
								lastPostId = lastPostId.replace(/\s+/g, '');
483
								//alert('Neu: ' + lastPostId + " Alt: " + old_lastPostId);
484
								if(lastPostId == old_lastPostId) {
485
									
486
									//morePostsAvailable = false;
487
								}
488
								else {
489
									//alert(elements[3].textContent);
490
									$('#posts').append(elements[1].innerHTML);
491
								}
492
								$('#loading').hide();
493
							}
494
						});
495
					}
496
				});
497
			});	
498
		<?php } ?>
499
		</script>
500
501
	</body>
502
</html>
503
504