Completed
Push — master ( 50907f...5052c4 )
by mains
02:42
created

get-posts-ajax.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
4
include 'php/DatabaseConnect.php';
5
include 'php/Requests/AbstractRequest.php';
6
include 'php/Requests/CreateUser.php';
7
include 'php/AccountData.php';
8
include 'php/Location.php';
9
include 'php/Requests/GetPosts.php';
10
include 'php/Requests/GetKarma.php';
11
include 'php/Requests/UpdateLocation.php';
12
include 'php/Requests/Upvote.php';
13
include 'php/Requests/Downvote.php';
14
include 'php/Requests/GetPostDetails.php';
15
include 'php/Requests/SendJodel.php';
16
17
require_once 'php/Requests/libary/Requests.php';
18
Requests::register_autoloader();
19
20
function getPosts($lastPostId, $url) {
21
	$db = new DatabaseConnect();
22
	if ($db->connect_errno) {
23
		echo 'Sorry, die Verbindung zu unserem superfetten endgeilen 
24
					Server ist hops gegangen. Wegen '. $db -> connect_error;
25
	}
26
	
27
	$result = $db->query("SELECT * FROM accounts WHERE id='1'");
28
	
29
	if ($result->num_rows > 0) {
30
		// output data of each row
31
		while($row = $result->fetch_assoc()) {
32
			$access_token = $row["access_token"];
33
		}
34
	}
35
	else
36
	{
37
		echo "0 results";
38
	}
39
	
40
	$accountCreator = new GetPosts();
41
	$accountCreator->setLastPostID($lastPostId);
42
	$accountCreator->setUrl($url);
43
	$accountCreator->setAccessToken($access_token);
44
	$data = $accountCreator->execute();
45
46
	return $data;
47
}
48
	$posts;
49
50 View Code Duplication
	if(isset($_GET['view']))
51
	{
52
		switch ($_GET['view']) {
53
			case 'comment':
54
				$view = 'comment';
55
				break;
56
			
57
			case 'upVote':
58
				$view = 'upVote';
59
				break;
60
61
			default:
62
				$view = 'time';
63
				break;
64
		}
65
	}
66
	else
67
	{
68
		$view = 'time';
69
	}
70
71 View Code Duplication
	if($view=='comment')
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...
72
	{
73
		$url = "/v2/posts/location/discussed/";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal /v2/posts/location/discussed/ 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...
74
	}
75
	else
76
	{
77
		if($view=='upVote')
78
		{
79
			$url = "/v2/posts/location/popular/";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal /v2/posts/location/popular/ 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...
80
		}
81
		else
82
		{
83
			$url = "/v2/posts/location/";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal /v2/posts/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...
84
		}
85
	}
86
87
	if(isset($_GET['lastPostId'])) {
88
	
89
		$lastPostId = htmlspecialchars($_GET['lastPostId']);
90
		
91
		$posts = getPosts($lastPostId, $url)['posts'];
92
		$loops = 29;
93
		$showCommentIcon = TRUE;
94
		?>
95
		<div class="nextPosts">
96
		<?php
97
		for($i = 0; $i<$loops; $i++) {
98
		
99
			if(isset($posts[$i])) {
100
				$lastPostId = $posts[$i]['post_id'];
101
102
				
103
				$now = new DateTime();
104
				$d = new DateTime($posts[$i]["created_at"]);
105
				
106
				
107
				//Time to time difference
108
							$timediff = $now->diff($d);
109
110
							$timediff_inSeconds = (string)$timediff->format('%s');
111
							$timediff_inMinutes = (string)$timediff->format('%i');
112
							$timediff_inHours = (string)$timediff->format('%h');
113
							$timediff_inDays = (string)$timediff->format('%d');
114
							$timediff_inMonth = (string)$timediff->format('%m');
115 View Code Duplication
							if($timediff_inMonth!=0) {
116
									$timediff = $timediff_inMonth . "m";
117
							}
118
							else
119
							{
120
								if($timediff_inDays!=0)
121
								{
122
									$timediff = $timediff_inDays . "d";
123
								}
124
								else
125
								{
126
									if($timediff_inHours!=0)
127
									{
128
										$timediff = $timediff_inHours . "h";
129
									}
130
									else
131
									{
132
										if($timediff_inMinutes!=0)
133
										{
134
											$timediff = $timediff_inMinutes . "m";
135
										}
136
										else
137
										{
138
											$timediff = $timediff_inSeconds . "s";
139
										}
140
									}
141
								}
142
							}
143
						?>
144
145
				<article class="jodel" style="background-color: #<?php echo $posts[$i]["color"];?>;">
146
					<content>
147
						<?php 
148 View Code Duplication
						if(isset($posts[$i]["image_url"])) {
149
							echo '<img src="' . $posts[$i]["image_url"] . '">';
150
						}
151
						else {
152
							echo str_replace('  ', ' &nbsp;', nl2br(htmlspecialchars($posts[$i]["message"])));
153
						}
154
						?>
155
					</content>
156
					<aside>
157
						<a href="index.php?vote=up&postID=<?php echo $posts[$i]["post_id"];?>">
158
							<i class="fa fa-angle-up fa-3x"></i>
159
						</a>	
160
							<br />
161
						<?php echo $posts[$i]["vote_count"];?><br />
162
						<a href="index.php?vote=down&postID=<?php echo $posts[$i]["post_id"];?>">
163
							<i class="fa fa-angle-down fa-3x"></i>
164
						</a>
165
					</aside>
166
167
					<footer>
168
						<table>
169
							<tr>
170
								<td class="time">
171
									<span data-tooltip="Time">
172
										<i class="fa fa-clock-o"></i>
173
										<?php echo $timediff;?>
174
									</span> 
175
								</td>
176
								<td class="comments">
177 View Code Duplication
									<?php if($showCommentIcon) {?>
178
									<span data-tooltip="Comments">
179
										<a href="index.php?getPostDetails=true&view=<?php echo $view;?>&postID=<?php echo $posts[$i]["post_id"];?>">
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal post_id 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...
180
											<i class="fa fa-commenting-o"></i>
181
											<?php if(array_key_exists("child_count", $posts[$i])) {
182
														echo $posts[$i]["child_count"];
183
													} else echo "0";
184
											?>
185
											</a>
186
									</span>
187
									<?php } ?>
188
								</td>
189
								<td class="distance">
190
									<span data-tooltip="Distance">
191
										<i class="fa fa-map-marker"></i>
192
										<?php echo $posts[$i]["distance"];?> km
193
									</span>
194
								</td>
195
							</tr>
196
						</table>
197
					</footer>
198
				</article>
199
200
201
202
				<?php 
203
			}
204
		}
205
		?>
206
		</div>
207
		<div class="lastPostId">
208
		<?php echo $lastPostId; ?>
209
		</div>
210
		<?php
211
	}
212