Completed
Push — master ( dc3cec...6a6184 )
by mains
03:04
created

php/jodel-web.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/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
function isTokenFresh(Location $location) {
22
	$db = new DatabaseConnect();  
23
	$result = $db->query("SELECT * FROM accounts WHERE id='1'");
24
	
25
	if ($result->num_rows > 0)
26
	{
27
			// output data of each row
28
			while($row = $result->fetch_assoc()) {
29
					//$access_token = $row["access_token"];
30
					$expiration_date = $row["expiration_date"];
31
					$deviceUid = $row["device_uid"];
32
					$access_token = $row["access_token"];
33
			}
34
	}
35
	else
36
	{
37
			echo '0 results';
38
	}
39
40
	if($expiration_date <= time()) {
41
		$accountCreator = new CreateUser();
42
		$accountCreator->setAccessToken($access_token);//$accountData->getAccessToken());
43
		$accountCreator->setDeviceUid($deviceUid);
44
		$accountCreator->setLocation($location);
45
		$data = $accountCreator->execute();
46
47
		$access_token = (string)$data[0]['access_token'];
48
		$expiration_date = $data[0]['expiration_date'];
49
		$device_uid = (string)$data[1];
50
		
51
		$db = new DatabaseConnect();  
52
		$result = $db->query("UPDATE accounts 
53
								SET access_token='" . $access_token . "',
54
									expiration_date='" . $expiration_date . "'
55
								WHERE device_uid='" . $device_uid . "'");
56
57
		if($result === false){
58
				echo "Adding account failed: (" . $db->errno . ") " . $db->error;
59
		}	
60
	}
61
	
62
	return TRUE;
63
}
64
65
function getKarma($accessToken)
66
{
67
	$accountCreator = new GetKarma();
68
	$accountCreator->setAccessToken($accessToken);
69
	$data = $accountCreator->execute();
70
	
71
	return $data["karma"];
72
}
73
74
function registerAccount(Location $location) {
75
	$accountCreator = new CreateUser();
76
	$accountCreator->setLocation($location);
77
	$data = $accountCreator->execute();
78
	
79
	$access_token = (string)$data[0]['access_token'];
80
	$refresh_token = (string)$data[0]['refresh_token'];
81
	$token_type = (string)$data[0]['token_type'];
82
	$expires_in = $data[0]['expires_in'];
83
	$expiration_date = $data[0]['expiration_date'];
84
	$distinct_id = (string)$data[0]['distinct_id'];
85
	$device_uid = (string)$data[1];
86
87
	$name = $location->cityName;
88
	$lat = $location->lat;
89
	$lng = $location->lng;
90
	
91
	$db = new DatabaseConnect();  
92
	$result = $db->query("INSERT INTO accounts (access_token, refresh_token, token_type,
93
					expires_in, expiration_date, distinct_id, device_uid, name, lat, lng)
94
					VALUES ('" . $access_token . "','" . $refresh_token . "','" . $token_type .
95
					"','" .  $expires_in . "','" . $expiration_date . "','" . $distinct_id .
96
					"','" . $device_uid . "','" . $name . "','" . $lat . "','" . $lng . "') ");
97
98
	$success = TRUE;
99
	if($result === false){
100
			$error = db_error();
101
			echo $error;
102
			echo "Adding account failed: (" . $result->errno . ") " . $result->error;
103
			$success = FALSE;
104
	}	
105
	
106
	return $success;
107
}
108
109
function getPosts($lastPostId, $accessToken, $url, $version = 'v2')
110
{	
111
	$accountCreator = new GetPosts();
112
	$accountCreator->setLastPostId($lastPostId);
113
	$accountCreator->setAccessToken($accessToken);
114
	$accountCreator->setUrl($url);
115
	$accountCreator->version = $version;
116
117
	$location = new Location();
118
	$location->setLat(52.520006);
119
	$location->setLng(13.404954);
120
	$location->setCityName('Berlin');
121
	$accountCreator->location = $location;
122
	$data = $accountCreator->execute();
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 21 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...
123
	
124
	return $data;
125
}
126
127
function createAccount()
128
{
129
	$location = new Location();
130
	$location->setLat(52.520006);
131
	$location->setLng(13.404954);
132
	$location->setCityName('Berlin');
133
134
	$account = registerAccount($location);
135
}
136
137
function jodelToHtml($post, $view = 'time', $isDetailedView = FALSE)
138
{	//ToDO
139
	//Replace # with link
140
	//preg_replace('~(\#)([^\s!,. /()"\'?]+)~', '<a href="tag/$2">#$2</a>', $text);
141
142
143
	//Time to time difference
144
	$now = new DateTime();
145
	$d = new DateTime($post["created_at"]);
146
	$timediff = $now->diff($d);
147
148
	$timediff_inSeconds = (string)$timediff->format('%s');
149
	$timediff_inMinutes = (string)$timediff->format('%i');
150
	$timediff_inHours = (string)$timediff->format('%h');
151
	$timediff_inDays = (string)$timediff->format('%d');
152
	$timediff_inMonth = (string)$timediff->format('%m');
153
154
	if($timediff_inMonth!=0)
155
	{
156
			$timediff = $timediff_inMonth . "m";
157
	}
158
	else
159
	{
160
		if($timediff_inDays!=0)
161
		{
162
			$timediff = $timediff_inDays . "d";
163
		}
164
		else
165
		{
166
			if($timediff_inHours!=0)
167
			{
168
				$timediff = $timediff_inHours . "h";
169
			}
170
			else
171
			{
172
				if($timediff_inMinutes!=0)
173
				{
174
					$timediff = $timediff_inMinutes . "m";
175
				}
176
				else
177
				{
178
					$timediff = $timediff_inSeconds . "s";
179
				}
180
			}
181
		}
182
	}
183
184
185
	?>
186
	<article id ="postId-<?php echo $post["post_id"]; ?>" class="jodel" style="background-color: #<?php echo $post["color"];?>;">
187
		<content>
188
			<?php 
189
			if(isset($post["image_url"])) {
190
				echo '<img src="' . $post["image_url"] . '">';
191
			}
192
			else {
193
				echo str_replace('  ', ' &nbsp;', nl2br(htmlspecialchars($post["message"])));
194
			}
195
			?>
196
		</content>
197
		<aside>
198
			<a href="index.php?vote=up&postID=<?php echo $post["post_id"];?>">
199
				<i class="fa fa-angle-up fa-3x"></i>
200
			</a>	
201
				<br />
202
			<?php echo $post["vote_count"];?><br />
203
			<a href="index.php?vote=down&postID=<?php echo $post["post_id"];?>">
204
				<i class="fa fa-angle-down fa-3x"></i>
205
			</a>
206
		</aside>
207
208
		<footer>
209
			<table>
210
				<tr>
211
					<td class="time">
212
						<span data-tooltip="Time">
213
							<i class="fa fa-clock-o"></i>
214
							<?php echo $timediff;?>
215
						</span> 
216
					</td>
217
					<td class="comments">
218
						<?php if(!$isDetailedView) {?>
219
						<span data-tooltip="Comments">
220
							<a href="index.php?getPostDetails=true&view=<?php echo $view;?>&postID=<?php echo $post["post_id"];?>">
221
								<i class="fa fa-commenting-o"></i>
222
								<?php if(array_key_exists("child_count", $post)) {
223
											echo $post["child_count"];
224
										} else echo "0";
225
								?>
226
								</a>
227
						</span>
228
						<?php } ?>
229
					</td>
230
					<td class="distance">
231
						<?php
232
							if($isDetailedView)
233
							{
234
								if(isset($post["parent_creator"]) && $post["parent_creator"] == 1)
235
								{
236
									?>
237
									<span data-tooltip="Author">
238
										<i class="fa fa-user-o"></i> OJ |
239
									</span>
240
									<?php 
241
						  		}
242
						  		else
243
						  		{
244
						  			//Is not parent Jodel in detailed View
245
									if(!array_key_exists('child_count', $post) && array_key_exists('parent_creator', $post))
246
									{
247
							  			?>
248
							  			<span data-tooltip="Author">
249
											<i class="fa fa-user-o"></i> #<?php echo $post["user_handle"];?> |
250
										</span>
251
										<?php
252
									}
253
						  		}
254
						  	}
255
					  		?>
256
257
						<span data-tooltip="Distance">
258
							<i class="fa fa-map-marker"></i>
259
							<?php echo $post["distance"];?> km
260
						</span>
261
					</td>
262
				</tr>
263
			</table>
264
		</footer>
265
	</article>
266
<?php
267
}