Completed
Push — master ( 18a827...20d307 )
by mains
02:53
created

php/jodel-web.php (3 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
	if ($result->num_rows > 0)
27
	{
28
			// output data of each row
29
			while($row = $result->fetch_assoc()) {
30
					//$access_token = $row["access_token"];
31
					$expiration_date = $row["expiration_date"];
32
					$deviceUid = $row["device_uid"];
33
					$access_token = $row["access_token"];
34
			}
35
	}
36
	else
37
	{
38
			echo '0 results';
39
	}
40
41
	if($expiration_date <= time()) {
42
		$accountCreator = new CreateUser();
43
		$accountCreator->setAccessToken($access_token);//$accountData->getAccessToken());
44
		$accountCreator->setDeviceUid($deviceUid);
45
		$accountCreator->setLocation($location);
46
		$data = $accountCreator->execute();
47
48
		$access_token = (string)$data[0]['access_token'];
49
		$expiration_date = $data[0]['expiration_date'];
50
		$device_uid = (string)$data[1];
51
		
52
		$db = new DatabaseConnect();  
53
		$result = $db->query("UPDATE accounts 
54
								SET access_token='" . $access_token . "',
55
									expiration_date='" . $expiration_date . "'
56
								WHERE device_uid='" . $device_uid . "'");
57
58
		if($result === false){
59
				echo "Adding account failed: (" . $db->errno . ") " . $db->error;
60
		}	
61
	}
62
	
63
	return TRUE;
64
}
65
66 View Code Duplication
function isTokenFreshByAccessToken(Location $location, $accessToken)
67
{
68
	$db = new DatabaseConnect();  
69
	$result = $db->query("SELECT * FROM accounts WHERE access_token='" . $accessToken . "'");
70
	
71
	if ($result->num_rows > 0)
72
	{
73
			// output data of each row
74
			while($row = $result->fetch_assoc()) {
75
					//$access_token = $row["access_token"];
76
					$expiration_date = $row["expiration_date"];
77
					$deviceUid = $row["device_uid"];
78
					$access_token = $row["access_token"];
79
			}
80
	}
81
	else
82
	{
83
			echo '0 results';
84
	}
85
86
	if($expiration_date <= time()) {
87
		$accountCreator = new CreateUser();
88
		$accountCreator->setAccessToken($access_token);//$accountData->getAccessToken());
89
		$accountCreator->setDeviceUid($deviceUid);
90
		$accountCreator->setLocation($location);
91
		$data = $accountCreator->execute();
92
93
		$access_token = (string)$data[0]['access_token'];
94
		$expiration_date = $data[0]['expiration_date'];
95
		$device_uid = (string)$data[1];
96
		
97
		$db = new DatabaseConnect();  
98
		$result = $db->query("UPDATE accounts 
99
								SET access_token='" . $access_token . "',
100
									expiration_date='" . $expiration_date . "'
101
								WHERE device_uid='" . $device_uid . "'");
102
103
		if($result === false){
104
				echo "Adding account failed: (" . $db->errno . ") " . $db->error;
105
		}	
106
	}
107
	
108
	return TRUE;
109
}
110
111
function getLocationByAccessToken($accessToken)
112
{
113
	$db = new DatabaseConnect();
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
114
	$result = $db->query("SELECT * FROM accounts WHERE access_token='" . $accessToken  . "'");
115
	
116
	$location = new Location();
117
	
118
	if ($result->num_rows > 0)
119
	{
120
		// output data of each row
121
		while($row = $result->fetch_assoc())
122
		{
123
			$location->setLat($row['lat']);
124
			$location->setLng($row['lng']);
125
			$location->setCityName($row['name']);
126
		}
127
	}
128
	else
129
	{
130
		echo "Error: 0 results";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Error: 0 results 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...
131
	}
132
133
	return $location;
134
}
135
136
137
function getKarma($accessToken)
138
{
139
	$accountCreator = new GetKarma();
140
	$accountCreator->setAccessToken($accessToken);
141
	$data = $accountCreator->execute();
142
	
143
	return $data["karma"];
144
}
145
146
function registerAccount(Location $location) {
147
	$accountCreator = new CreateUser();
148
	$accountCreator->setLocation($location);
149
	$data = $accountCreator->execute();
150
	
151
	$access_token = (string)$data[0]['access_token'];
152
	$refresh_token = (string)$data[0]['refresh_token'];
153
	$token_type = (string)$data[0]['token_type'];
154
	$expires_in = $data[0]['expires_in'];
155
	$expiration_date = $data[0]['expiration_date'];
156
	$distinct_id = (string)$data[0]['distinct_id'];
157
	$device_uid = (string)$data[1];
158
159
	$name = $location->cityName;
160
	$lat = $location->lat;
161
	$lng = $location->lng;
162
	
163
	$db = new DatabaseConnect();  
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $db. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
164
	$result = $db->query("INSERT INTO accounts (access_token, refresh_token, token_type,
165
					expires_in, expiration_date, distinct_id, device_uid, name, lat, lng)
166
					VALUES ('" . $access_token . "','" . $refresh_token . "','" . $token_type .
167
					"','" .  $expires_in . "','" . $expiration_date . "','" . $distinct_id .
168
					"','" . $device_uid . "','" . $name . "','" . $lat . "','" . $lng . "') ");
169
170
	$success = TRUE;
171
	if($result === false){
172
			$error = db_error();
173
			echo $error;
174
			echo "Adding account failed: (" . $result->errno . ") " . $result->error;
175
			$success = FALSE;
176
	}	
177
	
178
	return $access_token;
179
}
180
181
function getPosts($lastPostId, $accessToken, $url, $version = 'v2')
182
{	
183
	$accountCreator = new GetPosts();
184
	$accountCreator->setLastPostId($lastPostId);
185
	$accountCreator->setAccessToken($accessToken);
186
	$accountCreator->setUrl($url);
187
	$accountCreator->version = $version;
188
189
	$location = new Location();
190
	$location->setLat(52.520006);
191
	$location->setLng(13.404954);
192
	$location->setCityName('Berlin');
193
	$accountCreator->location = $location;
194
	$data = $accountCreator->execute();
195
	
196
	return $data;
197
}
198
199
function createAccount()
200
{
201
	$location = new Location();
202
	$location->setLat(52.520006);
203
	$location->setLng(13.404954);
204
	$location->setCityName('Berlin');
205
206
	$accessToken = registerAccount($location);
207
208
	return $accessToken;
209
}
210
211
function jodelToHtml($post, $view = 'time', $isDetailedView = FALSE)
212
{	//ToDO
213
	//Replace # with link
214
	//preg_replace('~(\#)([^\s!,. /()"\'?]+)~', '<a href="tag/$2">#$2</a>', $text);
215
216
217
	//Time to time difference
218
	$now = new DateTime();
219
	$d = new DateTime($post["created_at"]);
220
	$timediff = $now->diff($d);
221
222
	$timediff_inSeconds = (string)$timediff->format('%s');
223
	$timediff_inMinutes = (string)$timediff->format('%i');
224
	$timediff_inHours = (string)$timediff->format('%h');
225
	$timediff_inDays = (string)$timediff->format('%d');
226
	$timediff_inMonth = (string)$timediff->format('%m');
227
228
	if($timediff_inMonth!=0)
229
	{
230
			$timediff = $timediff_inMonth . "m";
231
	}
232
	else
233
	{
234
		if($timediff_inDays!=0)
235
		{
236
			$timediff = $timediff_inDays . "d";
237
		}
238
		else
239
		{
240
			if($timediff_inHours!=0)
241
			{
242
				$timediff = $timediff_inHours . "h";
243
			}
244
			else
245
			{
246
				if($timediff_inMinutes!=0)
247
				{
248
					$timediff = $timediff_inMinutes . "m";
249
				}
250
				else
251
				{
252
					$timediff = $timediff_inSeconds . "s";
253
				}
254
			}
255
		}
256
	}
257
258
259
	?>
260
	<article id ="postId-<?php echo $post["post_id"]; ?>" class="jodel" style="background-color: #<?php echo $post["color"];?>;">
261
		<content>
262
			<?php 
263
			if(isset($post["image_url"])) {
264
				echo '<img src="' . $post["image_url"] . '">';
265
			}
266
			else {
267
				echo str_replace('  ', ' &nbsp;', nl2br(htmlspecialchars($post["message"])));
268
			}
269
			?>
270
		</content>
271
		<aside>
272
			<a href="index.php?vote=up&postID=<?php echo $post["post_id"];?>">
273
				<i class="fa fa-angle-up fa-3x"></i>
274
			</a>	
275
				<br />
276
			<?php echo $post["vote_count"];?><br />
277
			<a href="index.php?vote=down&postID=<?php echo $post["post_id"];?>">
278
				<i class="fa fa-angle-down fa-3x"></i>
279
			</a>
280
		</aside>
281
282
		<footer>
283
			<table>
284
				<tr>
285
					<td class="time">
286
						<span data-tooltip="Time">
287
							<i class="fa fa-clock-o"></i>
288
							<?php echo $timediff;?>
289
						</span> 
290
					</td>
291
					<td class="comments">
292
						<?php if(!$isDetailedView) {?>
293
						<span data-tooltip="Comments">
294
							<a href="index.php?getPostDetails=true&view=<?php echo $view;?>&postID=<?php echo $post["post_id"];?>">
295
								<i class="fa fa-commenting-o"></i>
296
								<?php if(array_key_exists("child_count", $post)) {
297
											echo $post["child_count"];
298
										} else echo "0";
299
								?>
300
								</a>
301
						</span>
302
						<?php } ?>
303
					</td>
304
					<td class="distance">
305
						<?php
306
							if($isDetailedView)
307
							{
308
								if(isset($post["parent_creator"]) && $post["parent_creator"] == 1)
309
								{
310
									?>
311
									<span data-tooltip="Author">
312
										<i class="fa fa-user-o"></i> OJ |
313
									</span>
314
									<?php 
315
						  		}
316
						  		else
317
						  		{
318
						  			//Is not parent Jodel in detailed View
319
									if(!array_key_exists('child_count', $post) && array_key_exists('parent_creator', $post))
320
									{
321
							  			?>
322
							  			<span data-tooltip="Author">
323
											<i class="fa fa-user-o"></i> #<?php echo $post["user_handle"];?> |
324
										</span>
325
										<?php
326
									}
327
						  		}
328
						  	}
329
					  		?>
330
331
						<span data-tooltip="Distance">
332
							<i class="fa fa-map-marker"></i>
333
							<?php echo $post["distance"];?> km
334
						</span>
335
					</td>
336
				</tr>
337
			</table>
338
		</footer>
339
	</article>
340
<?php
341
}