Completed
Push — master ( a9f269...e11ad9 )
by mains
02:56
created
php/JodelAccount.php 2 patches
Indentation   +447 added lines, -447 removed lines patch added patch discarded remove patch
@@ -2,496 +2,496 @@
 block discarded – undo
2 2
 
3 3
 class JodelAccount
4 4
 {
5
-    public $accessToken;
6
-    public $expirationDate;
7
-    public $refreshToken;
8
-    public $distinctId;
9
-    public $deviceUid;
10
-
11
-    //is the Account a Bot or Spider?
12
-    public $isBot;
5
+	public $accessToken;
6
+	public $expirationDate;
7
+	public $refreshToken;
8
+	public $distinctId;
9
+	public $deviceUid;
10
+
11
+	//is the Account a Bot or Spider?
12
+	public $isBot;
13 13
     
14
-    // array of voted Jodels
15
-    public $votes;
16
-
17
-    //Location of the Account
18
-    public $location;
19
-
20
-    function __construct($deviceUid = NULL, $isBot = FALSE)
21
-    {
22
-        if($deviceUid == NULL)
23
-        {
24
-            $this->deviceUid = $this->createAccount();
25
-        }
26
-        else
27
-        {
28
-            $this->deviceUid = $deviceUid;
29
-        }
30
-
31
-        $this->isBot        = $isBot;
32
-        $this->location     = $this->getLocation();
33
-
34
-        if(!$this->isTokenFresh())
35
-        {
36
-            $this->refreshToken();
37
-        }
38
-        $this->accessToken  = $this->getAccessToken();
39
-    }
40
-
41
-    function isAccountVerified()
42
-    {
43
-        $accountCreator = new GetUserConfig();
44
-        $accountCreator->setAccessToken($this->accessToken);
45
-        $data = $accountCreator->execute();
46
-
47
-        return $data['verified'];
48
-    }
49
-
50
-    function locationEquals($city)
51
-    {
52
-        $url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' . htmlspecialchars($city) . '&key=AIzaSyCwhnja-or07012HqrhPW7prHEDuSvFT4w';
53
-        $result = Requests::post($url);
54
-        if(json_decode($result->body, true)['status'] == 'ZERO_RESULTS' || json_decode($result->body, true)['status'] == 'INVALID_REQUEST')
55
-        {
56
-            error_log('Error locationEquals');
57
-            return FALSE;
58
-        }
59
-        else
60
-        {
61
-            $name = json_decode($result->body, true)['results']['0']['address_components']['0']['long_name'];
62
-            $lat = json_decode($result->body, true)['results']['0']['geometry']['location']['lat'];
63
-            $lng = json_decode($result->body, true)['results']['0']['geometry']['location']['lng'];
64
-        }
65
-
66
-        $db = new DatabaseConnect();
67
-        $result = $db->query("SELECT * FROM accounts WHERE device_uid='" . $this->deviceUid  . "'");
14
+	// array of voted Jodels
15
+	public $votes;
16
+
17
+	//Location of the Account
18
+	public $location;
19
+
20
+	function __construct($deviceUid = NULL, $isBot = FALSE)
21
+	{
22
+		if($deviceUid == NULL)
23
+		{
24
+			$this->deviceUid = $this->createAccount();
25
+		}
26
+		else
27
+		{
28
+			$this->deviceUid = $deviceUid;
29
+		}
30
+
31
+		$this->isBot        = $isBot;
32
+		$this->location     = $this->getLocation();
33
+
34
+		if(!$this->isTokenFresh())
35
+		{
36
+			$this->refreshToken();
37
+		}
38
+		$this->accessToken  = $this->getAccessToken();
39
+	}
40
+
41
+	function isAccountVerified()
42
+	{
43
+		$accountCreator = new GetUserConfig();
44
+		$accountCreator->setAccessToken($this->accessToken);
45
+		$data = $accountCreator->execute();
46
+
47
+		return $data['verified'];
48
+	}
49
+
50
+	function locationEquals($city)
51
+	{
52
+		$url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' . htmlspecialchars($city) . '&key=AIzaSyCwhnja-or07012HqrhPW7prHEDuSvFT4w';
53
+		$result = Requests::post($url);
54
+		if(json_decode($result->body, true)['status'] == 'ZERO_RESULTS' || json_decode($result->body, true)['status'] == 'INVALID_REQUEST')
55
+		{
56
+			error_log('Error locationEquals');
57
+			return FALSE;
58
+		}
59
+		else
60
+		{
61
+			$name = json_decode($result->body, true)['results']['0']['address_components']['0']['long_name'];
62
+			$lat = json_decode($result->body, true)['results']['0']['geometry']['location']['lat'];
63
+			$lng = json_decode($result->body, true)['results']['0']['geometry']['location']['lng'];
64
+		}
65
+
66
+		$db = new DatabaseConnect();
67
+		$result = $db->query("SELECT * FROM accounts WHERE device_uid='" . $this->deviceUid  . "'");
68 68
         
69
-        $location = new Location();
69
+		$location = new Location();
70 70
         
71
-        if ($result->num_rows > 0)
72
-        {
73
-            // output data of each row
74
-            while($row = $result->fetch_assoc())
75
-            {
76
-                $location->setLat($row['lat']);
77
-                $location->setLng($row['lng']);
78
-                $location->setCityName($row['name']);
79
-            }
80
-        }
81
-        else
82
-        {
83
-            echo "Error: 0 results";
84
-            error_log("Error no Location found - getLocation");
85
-        }
86
-
87
-        if($location->getLat() == $lat && $location->getLng() == $lng && $location->getCityName() == $name)
88
-        {
89
-            return TRUE;
90
-        }  
91
-        else
92
-        {
93
-            return FALSE;
94
-        }
95
-    }
96
-
97
-    function setLocation()
98
-    {
99
-        //Is Channel or City
100
-        if(substr($_GET['city'], 0, 1) === '#')
101
-        {
102
-            return htmlspecialchars($_GET['city']) . " " . $this->location->cityName;
103
-        }                
104
-        else
105
-        {
106
-            $url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' . htmlspecialchars($_GET['city']) . '&key=AIzaSyCwhnja-or07012HqrhPW7prHEDuSvFT4w';
107
-            $result = Requests::post($url);
108
-            if(json_decode($result->body, true)['status'] == 'ZERO_RESULTS' || json_decode($result->body, true)['status'] == 'INVALID_REQUEST')
109
-            {
110
-                return "0 results";
111
-            }
112
-            else
113
-            {
114
-                $name = json_decode($result->body, true)['results']['0']['address_components']['0']['long_name'];
115
-                $lat = json_decode($result->body, true)['results']['0']['geometry']['location']['lat'];
116
-                $lng = json_decode($result->body, true)['results']['0']['geometry']['location']['lng'];
117
-
118
-                $location = new Location();
119
-                $location->setLat($lat);
120
-                $location->setLng($lng);
121
-                $location->setCityName($name);
122
-                $accountCreator = new UpdateLocation();
123
-                $accountCreator->setLocation($location);
124
-                $accountCreator->setAccessToken($this->accessToken);
125
-                $data = $accountCreator->execute();
126
-
127
-                //safe location to db
128
-                $db = new DatabaseConnect();
129
-
130
-                if($data == 'Success')
131
-                {
132
-                    $result = $db->query("UPDATE accounts 
71
+		if ($result->num_rows > 0)
72
+		{
73
+			// output data of each row
74
+			while($row = $result->fetch_assoc())
75
+			{
76
+				$location->setLat($row['lat']);
77
+				$location->setLng($row['lng']);
78
+				$location->setCityName($row['name']);
79
+			}
80
+		}
81
+		else
82
+		{
83
+			echo "Error: 0 results";
84
+			error_log("Error no Location found - getLocation");
85
+		}
86
+
87
+		if($location->getLat() == $lat && $location->getLng() == $lng && $location->getCityName() == $name)
88
+		{
89
+			return TRUE;
90
+		}  
91
+		else
92
+		{
93
+			return FALSE;
94
+		}
95
+	}
96
+
97
+	function setLocation()
98
+	{
99
+		//Is Channel or City
100
+		if(substr($_GET['city'], 0, 1) === '#')
101
+		{
102
+			return htmlspecialchars($_GET['city']) . " " . $this->location->cityName;
103
+		}                
104
+		else
105
+		{
106
+			$url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' . htmlspecialchars($_GET['city']) . '&key=AIzaSyCwhnja-or07012HqrhPW7prHEDuSvFT4w';
107
+			$result = Requests::post($url);
108
+			if(json_decode($result->body, true)['status'] == 'ZERO_RESULTS' || json_decode($result->body, true)['status'] == 'INVALID_REQUEST')
109
+			{
110
+				return "0 results";
111
+			}
112
+			else
113
+			{
114
+				$name = json_decode($result->body, true)['results']['0']['address_components']['0']['long_name'];
115
+				$lat = json_decode($result->body, true)['results']['0']['geometry']['location']['lat'];
116
+				$lng = json_decode($result->body, true)['results']['0']['geometry']['location']['lng'];
117
+
118
+				$location = new Location();
119
+				$location->setLat($lat);
120
+				$location->setLng($lng);
121
+				$location->setCityName($name);
122
+				$accountCreator = new UpdateLocation();
123
+				$accountCreator->setLocation($location);
124
+				$accountCreator->setAccessToken($this->accessToken);
125
+				$data = $accountCreator->execute();
126
+
127
+				//safe location to db
128
+				$db = new DatabaseConnect();
129
+
130
+				if($data == 'Success')
131
+				{
132
+					$result = $db->query("UPDATE accounts 
133 133
                             SET name='" . $name . "',
134 134
                                 lat='" . $lat . "',
135 135
                                 lng='" . $lng . "'
136 136
                             WHERE access_token='" . $this->accessToken . "'");
137 137
 
138
-                    if($result === false)
139
-                    {
140
-                            echo "Updating location failed: (" . $db->errno . ") " . $db->error;
141
-                    }
142
-                    else
143
-                    {
144
-                        user_log('User with JodelDeviceId:' . $this->deviceUid .  ' [' . $_SERVER['REMOTE_ADDR'] . '][' . $_SERVER ['HTTP_USER_AGENT'] . '] changed to Location: ' . $name);
145
-                    }
146
-                }
147
-
148
-                return $name;
149
-            }
150
-        }
151
-    }
152
-
153
-    function getLocation()
154
-    {
155
-        $db = new DatabaseConnect();
156
-        $result = $db->query("SELECT * FROM accounts WHERE device_uid='" . $this->deviceUid  . "'");
138
+					if($result === false)
139
+					{
140
+							echo "Updating location failed: (" . $db->errno . ") " . $db->error;
141
+					}
142
+					else
143
+					{
144
+						user_log('User with JodelDeviceId:' . $this->deviceUid .  ' [' . $_SERVER['REMOTE_ADDR'] . '][' . $_SERVER ['HTTP_USER_AGENT'] . '] changed to Location: ' . $name);
145
+					}
146
+				}
147
+
148
+				return $name;
149
+			}
150
+		}
151
+	}
152
+
153
+	function getLocation()
154
+	{
155
+		$db = new DatabaseConnect();
156
+		$result = $db->query("SELECT * FROM accounts WHERE device_uid='" . $this->deviceUid  . "'");
157 157
         
158
-        $location = new Location();
158
+		$location = new Location();
159 159
         
160
-        if ($result->num_rows > 0)
161
-        {
162
-            // output data of each row
163
-            while($row = $result->fetch_assoc())
164
-            {
165
-                $location->setLat($row['lat']);
166
-                $location->setLng($row['lng']);
167
-                $location->setCityName($row['name']);
168
-            }
169
-        }
170
-        else
171
-        {
172
-            echo "Error: 0 results";
173
-            error_log("Error no Location found - getLocation");
174
-        }
175
-
176
-        return $location;
177
-    }
178
-
179
-    function verifyCaptcha()
180
-    {
181
-        if(isset($_GET['deviceUid']))
182
-        {
183
-            $deviceUid = $_GET['deviceUid'];
184
-        }
185
-        if(isset($_POST['deviceUid']))
186
-        {
187
-            $deviceUid = $_POST['deviceUid'];
188
-        }
189
-        $jodelAccountForVerify = new JodelAccount($deviceUid);
190
-
191
-        $solution = $_GET['solution'];
192
-        $solution = array_map('intval', explode('-', $solution));
193
-
194
-        $accountCreator = new PostCaptcha();
195
-        $accountCreator->setAccessToken($jodelAccountForVerify->accessToken);
196
-        $accountCreator->captchaKey = $_GET['key'];
197
-        $accountCreator->captchaSolution = $solution;
198
-        $verified = $accountCreator->execute();
199
-
200
-        if(isset($verified->status_code))
201
-        {
202
-            return $verified->status_code;
203
-        }
204
-        return $verified['verified'];
205
-    }
206
-
207
-    //ToDo Spider Check
208
-    function votePostId($postId, $vote)
209
-    {
210
-        if(!$this->isAccountVerified())
211
-        {
212
-            $view = new View();
213
-            $view->showCaptcha($this->accessToken, $this->deviceUid);
214
-        }
215
-
216
-        if(!$this->hasVoted($postId))
217
-        {
218
-            if($vote == "up")
219
-            {
220
-                $accountCreator = new Upvote();
221
-            }
222
-            else if($vote == "down")
223
-            {
224
-                $accountCreator = new Downvote();
225
-            }
226
-
227
-            $accountCreator->setAccessToken($this->accessToken);
228
-            $accountCreator->postId = htmlspecialchars($postId);
229
-            $data = $accountCreator->execute();
230
-
231
-            if(array_key_exists('post', $data))
232
-            {
233
-                $this->addVoteWithPostIdAndType($postId, $vote);
234
-                return TRUE;
235
-            }
236
-            else
237
-            {
238
-                error_log("Could not vote: " . print_r($data, true));
239
-                return FALSE;
240
-            } 
241
-        }
242
-        else
243
-        {
244
-            return FALSE;
245
-        }
246
-    }
247
-
248
-    //ToDo Spider Check
249
-    function sendJodel($location, $view)
250
-    {
251
-        if(!$this->isAccountVerified())
252
-        {
253
-            showCaptcha($this->accessToken);
254
-        }
255
-
256
-        $accountCreator = new SendJodel();
257
-
258
-        if(isset($_POST['ancestor']))
259
-        {
260
-            $ancestor = $_POST['ancestor'];
261
-            $accountCreator->ancestor = $ancestor;
262
-        }
263
-        if(isset($_POST['color']))
264
-        {
265
-            $color = $_POST['color'];
266
-            switch ($color) {
267
-                case '8ABDB0':
268
-                    $color = '8ABDB0';
269
-                    break;
270
-                case '9EC41C':
271
-                    $color = '9EC41C';
272
-                    break;
273
-                case '06A3CB':
274
-                    $color = '06A3CB';
275
-                    break;
276
-                case 'FFBA00':
277
-                    $color = 'FFBA00';
278
-                    break;
279
-                case 'DD5F5F':
280
-                    $color = 'DD5F5F';
281
-                    break;
282
-                case 'FF9908':
283
-                    $color = 'FF9908';
284
-                    break;
285
-                default:
286
-                    $color = '8ABDB0';
287
-                    break;
288
-            }
289
-            $accountCreator->color = $color;
290
-        }
291
-
292
-        $accountCreatorLocation = new UpdateLocation();
293
-        $accountCreatorLocation->setLocation($location);
294
-        $accountCreatorLocation->setAccessToken($this->accessToken);
295
-        $data = $accountCreatorLocation->execute();
160
+		if ($result->num_rows > 0)
161
+		{
162
+			// output data of each row
163
+			while($row = $result->fetch_assoc())
164
+			{
165
+				$location->setLat($row['lat']);
166
+				$location->setLng($row['lng']);
167
+				$location->setCityName($row['name']);
168
+			}
169
+		}
170
+		else
171
+		{
172
+			echo "Error: 0 results";
173
+			error_log("Error no Location found - getLocation");
174
+		}
175
+
176
+		return $location;
177
+	}
178
+
179
+	function verifyCaptcha()
180
+	{
181
+		if(isset($_GET['deviceUid']))
182
+		{
183
+			$deviceUid = $_GET['deviceUid'];
184
+		}
185
+		if(isset($_POST['deviceUid']))
186
+		{
187
+			$deviceUid = $_POST['deviceUid'];
188
+		}
189
+		$jodelAccountForVerify = new JodelAccount($deviceUid);
190
+
191
+		$solution = $_GET['solution'];
192
+		$solution = array_map('intval', explode('-', $solution));
193
+
194
+		$accountCreator = new PostCaptcha();
195
+		$accountCreator->setAccessToken($jodelAccountForVerify->accessToken);
196
+		$accountCreator->captchaKey = $_GET['key'];
197
+		$accountCreator->captchaSolution = $solution;
198
+		$verified = $accountCreator->execute();
199
+
200
+		if(isset($verified->status_code))
201
+		{
202
+			return $verified->status_code;
203
+		}
204
+		return $verified['verified'];
205
+	}
206
+
207
+	//ToDo Spider Check
208
+	function votePostId($postId, $vote)
209
+	{
210
+		if(!$this->isAccountVerified())
211
+		{
212
+			$view = new View();
213
+			$view->showCaptcha($this->accessToken, $this->deviceUid);
214
+		}
215
+
216
+		if(!$this->hasVoted($postId))
217
+		{
218
+			if($vote == "up")
219
+			{
220
+				$accountCreator = new Upvote();
221
+			}
222
+			else if($vote == "down")
223
+			{
224
+				$accountCreator = new Downvote();
225
+			}
226
+
227
+			$accountCreator->setAccessToken($this->accessToken);
228
+			$accountCreator->postId = htmlspecialchars($postId);
229
+			$data = $accountCreator->execute();
230
+
231
+			if(array_key_exists('post', $data))
232
+			{
233
+				$this->addVoteWithPostIdAndType($postId, $vote);
234
+				return TRUE;
235
+			}
236
+			else
237
+			{
238
+				error_log("Could not vote: " . print_r($data, true));
239
+				return FALSE;
240
+			} 
241
+		}
242
+		else
243
+		{
244
+			return FALSE;
245
+		}
246
+	}
247
+
248
+	//ToDo Spider Check
249
+	function sendJodel($location, $view)
250
+	{
251
+		if(!$this->isAccountVerified())
252
+		{
253
+			showCaptcha($this->accessToken);
254
+		}
255
+
256
+		$accountCreator = new SendJodel();
257
+
258
+		if(isset($_POST['ancestor']))
259
+		{
260
+			$ancestor = $_POST['ancestor'];
261
+			$accountCreator->ancestor = $ancestor;
262
+		}
263
+		if(isset($_POST['color']))
264
+		{
265
+			$color = $_POST['color'];
266
+			switch ($color) {
267
+				case '8ABDB0':
268
+					$color = '8ABDB0';
269
+					break;
270
+				case '9EC41C':
271
+					$color = '9EC41C';
272
+					break;
273
+				case '06A3CB':
274
+					$color = '06A3CB';
275
+					break;
276
+				case 'FFBA00':
277
+					$color = 'FFBA00';
278
+					break;
279
+				case 'DD5F5F':
280
+					$color = 'DD5F5F';
281
+					break;
282
+				case 'FF9908':
283
+					$color = 'FF9908';
284
+					break;
285
+				default:
286
+					$color = '8ABDB0';
287
+					break;
288
+			}
289
+			$accountCreator->color = $color;
290
+		}
291
+
292
+		$accountCreatorLocation = new UpdateLocation();
293
+		$accountCreatorLocation->setLocation($location);
294
+		$accountCreatorLocation->setAccessToken($this->accessToken);
295
+		$data = $accountCreatorLocation->execute();
296 296
         
297 297
 		if($data != 'Success')
298 298
 		{
299 299
 			error_log(print_r($data, true));
300 300
 		}
301 301
 
302
-        $accountCreator->location = $this->location;
302
+		$accountCreator->location = $this->location;
303 303
         
304
-        $accountCreator->setAccessToken($this->accessToken);
305
-        $data = $accountCreator->execute();
306
-
307
-        if(isset($_POST['ancestor']))
308
-        {
309
-            header('Location: ' . $view->toUrl());
310
-            exit;
311
-        }
312
-        else
313
-        {
314
-            header('Location: ' . $view->baseUrl);
315
-            exit;
316
-        }
317
-    }
318
-
319
-    function isTokenFresh()
320
-    {
321
-        $db = new DatabaseConnect();  
322
-        $result = $db->query("SELECT * FROM accounts WHERE device_uid='" . $this->deviceUid . "'");
323
-
324
-        if ($result->num_rows > 0)
325
-        {
326
-            // output data of each row
327
-            while($row = $result->fetch_assoc())
328
-            {
329
-                    $expiration_date = $row["expiration_date"];
330
-            }
331
-        }
332
-        else
333
-        {
334
-            error_log('0 results');
335
-        }
336
-
337
-        if($expiration_date <= time())
338
-        {
339
-           return FALSE;
340
-        }
304
+		$accountCreator->setAccessToken($this->accessToken);
305
+		$data = $accountCreator->execute();
306
+
307
+		if(isset($_POST['ancestor']))
308
+		{
309
+			header('Location: ' . $view->toUrl());
310
+			exit;
311
+		}
312
+		else
313
+		{
314
+			header('Location: ' . $view->baseUrl);
315
+			exit;
316
+		}
317
+	}
318
+
319
+	function isTokenFresh()
320
+	{
321
+		$db = new DatabaseConnect();  
322
+		$result = $db->query("SELECT * FROM accounts WHERE device_uid='" . $this->deviceUid . "'");
323
+
324
+		if ($result->num_rows > 0)
325
+		{
326
+			// output data of each row
327
+			while($row = $result->fetch_assoc())
328
+			{
329
+					$expiration_date = $row["expiration_date"];
330
+			}
331
+		}
332
+		else
333
+		{
334
+			error_log('0 results');
335
+		}
336
+
337
+		if($expiration_date <= time())
338
+		{
339
+		   return FALSE;
340
+		}
341 341
         
342
-        return TRUE;
343
-    }
344
-
345
-    function refreshToken()
346
-    {
347
-        $accountCreator = new CreateUser();
348
-        $accountCreator->setAccessToken($this->accessToken);
349
-        $accountCreator->setDeviceUid($this->deviceUid);
350
-        $accountCreator->setLocation($this->location);
351
-        $data = $accountCreator->execute();
352
-
353
-        $access_token = (string)$data[0]['access_token'];
354
-        $expiration_date = $data[0]['expiration_date'];
355
-        $device_uid = (string)$data[1];
342
+		return TRUE;
343
+	}
344
+
345
+	function refreshToken()
346
+	{
347
+		$accountCreator = new CreateUser();
348
+		$accountCreator->setAccessToken($this->accessToken);
349
+		$accountCreator->setDeviceUid($this->deviceUid);
350
+		$accountCreator->setLocation($this->location);
351
+		$data = $accountCreator->execute();
352
+
353
+		$access_token = (string)$data[0]['access_token'];
354
+		$expiration_date = $data[0]['expiration_date'];
355
+		$device_uid = (string)$data[1];
356 356
         
357
-        $db = new DatabaseConnect();  
358
-        $result = $db->query("UPDATE accounts 
357
+		$db = new DatabaseConnect();  
358
+		$result = $db->query("UPDATE accounts 
359 359
                                 SET access_token='" . $access_token . "',
360 360
                                     expiration_date='" . $expiration_date . "'
361 361
                                 WHERE device_uid='" . $device_uid . "'");
362 362
 
363
-        if($result === false){
364
-                error_log("Adding account failed: (" . $db->errno . ") " . $db->error);
365
-        }   
366
-    }
363
+		if($result === false){
364
+				error_log("Adding account failed: (" . $db->errno . ") " . $db->error);
365
+		}   
366
+	}
367 367
 
368 368
 
369 369
 
370
-    function getAccessToken()
371
-    {
372
-        $db = new DatabaseConnect();
373
-        $result = $db->query("SELECT * FROM accounts WHERE device_uid='" . $this->deviceUid  . "'");
370
+	function getAccessToken()
371
+	{
372
+		$db = new DatabaseConnect();
373
+		$result = $db->query("SELECT * FROM accounts WHERE device_uid='" . $this->deviceUid  . "'");
374 374
         
375
-        $accessToken;
375
+		$accessToken;
376 376
         
377
-        if ($result->num_rows > 0)
378
-        {
379
-            // output data of each row
380
-            while($row = $result->fetch_assoc())
381
-            {
382
-                $accessToken = $row['access_token'];
383
-            }
384
-        }
385
-        else
386
-        {
387
-            error_log('Error: 0 results');
388
-        }
389
-
390
-        return $accessToken;
391
-    }
392
-
393
-
394
-    function getKarma()
395
-    {
396
-        $accountCreator = new GetKarma();
397
-        $accountCreator->setAccessToken($this->accessToken);
398
-        $data = $accountCreator->execute();
377
+		if ($result->num_rows > 0)
378
+		{
379
+			// output data of each row
380
+			while($row = $result->fetch_assoc())
381
+			{
382
+				$accessToken = $row['access_token'];
383
+			}
384
+		}
385
+		else
386
+		{
387
+			error_log('Error: 0 results');
388
+		}
389
+
390
+		return $accessToken;
391
+	}
392
+
393
+
394
+	function getKarma()
395
+	{
396
+		$accountCreator = new GetKarma();
397
+		$accountCreator->setAccessToken($this->accessToken);
398
+		$data = $accountCreator->execute();
399 399
         
400
-        return $data["karma"];
401
-    }
400
+		return $data["karma"];
401
+	}
402 402
 
403
-    function hasVoted($postId)
404
-    {
405
-        $db = new DatabaseConnect();
403
+	function hasVoted($postId)
404
+	{
405
+		$db = new DatabaseConnect();
406 406
 
407
-        $postId = $db->real_escape_string($postId);
407
+		$postId = $db->real_escape_string($postId);
408 408
 
409
-        $result = $db->query("SELECT id FROM votes WHERE (postId = '" . $postId . "' AND device_uid = '" . $this->deviceUid . "')");
409
+		$result = $db->query("SELECT id FROM votes WHERE (postId = '" . $postId . "' AND device_uid = '" . $this->deviceUid . "')");
410 410
         
411
-        if($result === false)
412
-        {
413
-            $error = db_error();
414
-            echo $error;
415
-            error_log("Adding Vote failed: (" . $result->errno . ") " . $result->error);
416
-        }
417
-
418
-        if($result->num_rows == 0)
419
-        {
420
-            return FALSE;
421
-        }
422
-        else
423
-        {
424
-            return TRUE;
425
-        }
426
-    }
427
-
428
-    function addVoteWithPostIdAndType($postId, $voteType)
429
-    {
430
-        $db = new DatabaseConnect();  
431
-
432
-        $postId = $db->real_escape_string($postId);
433
-        $voteType = $db->real_escape_string($voteType);
411
+		if($result === false)
412
+		{
413
+			$error = db_error();
414
+			echo $error;
415
+			error_log("Adding Vote failed: (" . $result->errno . ") " . $result->error);
416
+		}
417
+
418
+		if($result->num_rows == 0)
419
+		{
420
+			return FALSE;
421
+		}
422
+		else
423
+		{
424
+			return TRUE;
425
+		}
426
+	}
427
+
428
+	function addVoteWithPostIdAndType($postId, $voteType)
429
+	{
430
+		$db = new DatabaseConnect();  
431
+
432
+		$postId = $db->real_escape_string($postId);
433
+		$voteType = $db->real_escape_string($voteType);
434 434
         
435
-        if($this->hasVoted($postId))
436
-        {
437
-            return "Already voted";
438
-        }
435
+		if($this->hasVoted($postId))
436
+		{
437
+			return "Already voted";
438
+		}
439 439
 
440
-        $result = $db->query("INSERT INTO votes (device_uid, postId, type)
440
+		$result = $db->query("INSERT INTO votes (device_uid, postId, type)
441 441
                         VALUES ('" . $this->deviceUid . "','" . $postId . "','" . $voteType . "')");
442 442
         
443
-        if($result === false){
444
-                $error = db_error();
445
-                echo $error;
446
-                echo "Adding Vote failed: (" . $result->errno . ") " . $result->error;
447
-        }       
448
-    }
449
-
450
-    function registerAccount($location) {
451
-        $accountCreator = new CreateUser();
452
-        $accountCreator->setLocation($location);
453
-        $data = $accountCreator->execute();
443
+		if($result === false){
444
+				$error = db_error();
445
+				echo $error;
446
+				echo "Adding Vote failed: (" . $result->errno . ") " . $result->error;
447
+		}       
448
+	}
449
+
450
+	function registerAccount($location) {
451
+		$accountCreator = new CreateUser();
452
+		$accountCreator->setLocation($location);
453
+		$data = $accountCreator->execute();
454 454
         
455
-        $access_token = (string)$data[0]['access_token'];
456
-        $refresh_token = (string)$data[0]['refresh_token'];
457
-        $token_type = (string)$data[0]['token_type'];
458
-        $expires_in = $data[0]['expires_in'];
459
-        $expiration_date = $data[0]['expiration_date'];
460
-        $distinct_id = (string)$data[0]['distinct_id'];
461
-        $device_uid = (string)$data[1];
462
-
463
-        $name = $location->cityName;
464
-        $lat = $location->lat;
465
-        $lng = $location->lng;
455
+		$access_token = (string)$data[0]['access_token'];
456
+		$refresh_token = (string)$data[0]['refresh_token'];
457
+		$token_type = (string)$data[0]['token_type'];
458
+		$expires_in = $data[0]['expires_in'];
459
+		$expiration_date = $data[0]['expiration_date'];
460
+		$distinct_id = (string)$data[0]['distinct_id'];
461
+		$device_uid = (string)$data[1];
462
+
463
+		$name = $location->cityName;
464
+		$lat = $location->lat;
465
+		$lng = $location->lng;
466 466
         
467
-        $db = new DatabaseConnect();  
468
-        $result = $db->query("INSERT INTO accounts (access_token, refresh_token, token_type,
467
+		$db = new DatabaseConnect();  
468
+		$result = $db->query("INSERT INTO accounts (access_token, refresh_token, token_type,
469 469
                         expires_in, expiration_date, distinct_id, device_uid, name, lat, lng)
470 470
                         VALUES ('" . $access_token . "','" . $refresh_token . "','" . $token_type .
471
-                        "','" .  $expires_in . "','" . $expiration_date . "','" . $distinct_id .
472
-                        "','" . $device_uid . "','" . $name . "','" . $lat . "','" . $lng . "') ");
473
-
474
-        $success = TRUE;
475
-        if($result === false){
476
-                $error = db_error();
477
-                echo $error;
478
-                echo "Adding account failed: (" . $result->errno . ") " . $result->error;
479
-                $success = FALSE;
480
-        }   
471
+						"','" .  $expires_in . "','" . $expiration_date . "','" . $distinct_id .
472
+						"','" . $device_uid . "','" . $name . "','" . $lat . "','" . $lng . "') ");
473
+
474
+		$success = TRUE;
475
+		if($result === false){
476
+				$error = db_error();
477
+				echo $error;
478
+				echo "Adding account failed: (" . $result->errno . ") " . $result->error;
479
+				$success = FALSE;
480
+		}   
481 481
         
482
-        return $device_uid;
483
-    }
482
+		return $device_uid;
483
+	}
484 484
 
485
-    function createAccount()
486
-    {
487
-        $config = parse_ini_file('config/config.ini.php');
488
-        $location = new Location();
489
-        $location->setLat($config['default_lat']);
490
-        $location->setLng($config['default_lng']);
491
-        $location->setCityName($config['default_location']);
485
+	function createAccount()
486
+	{
487
+		$config = parse_ini_file('config/config.ini.php');
488
+		$location = new Location();
489
+		$location->setLat($config['default_lat']);
490
+		$location->setLng($config['default_lng']);
491
+		$location->setCityName($config['default_location']);
492 492
 
493
-        $deviceUid = $this->registerAccount($location);
493
+		$deviceUid = $this->registerAccount($location);
494 494
 
495
-        return $deviceUid;
496
-    }
495
+		return $deviceUid;
496
+	}
497 497
 }
498 498
\ No newline at end of file
Please login to merge, or discard this patch.
Upper-Lower-Casing   +15 added lines, -15 removed lines patch added patch discarded remove patch
@@ -51,16 +51,16 @@  discard block
 block discarded – undo
51 51
     {
52 52
         $url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' . htmlspecialchars($city) . '&key=AIzaSyCwhnja-or07012HqrhPW7prHEDuSvFT4w';
53 53
         $result = Requests::post($url);
54
-        if(json_decode($result->body, true)['status'] == 'ZERO_RESULTS' || json_decode($result->body, true)['status'] == 'INVALID_REQUEST')
54
+        if(json_decode($result->body, TRUE)['status'] == 'ZERO_RESULTS' || json_decode($result->body, TRUE)['status'] == 'INVALID_REQUEST')
55 55
         {
56 56
             error_log('Error locationEquals');
57 57
             return FALSE;
58 58
         }
59 59
         else
60 60
         {
61
-            $name = json_decode($result->body, true)['results']['0']['address_components']['0']['long_name'];
62
-            $lat = json_decode($result->body, true)['results']['0']['geometry']['location']['lat'];
63
-            $lng = json_decode($result->body, true)['results']['0']['geometry']['location']['lng'];
61
+            $name = json_decode($result->body, TRUE)['results']['0']['address_components']['0']['long_name'];
62
+            $lat = json_decode($result->body, TRUE)['results']['0']['geometry']['location']['lat'];
63
+            $lng = json_decode($result->body, TRUE)['results']['0']['geometry']['location']['lng'];
64 64
         }
65 65
 
66 66
         $db = new DatabaseConnect();
@@ -105,15 +105,15 @@  discard block
 block discarded – undo
105 105
         {
106 106
             $url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' . htmlspecialchars($_GET['city']) . '&key=AIzaSyCwhnja-or07012HqrhPW7prHEDuSvFT4w';
107 107
             $result = Requests::post($url);
108
-            if(json_decode($result->body, true)['status'] == 'ZERO_RESULTS' || json_decode($result->body, true)['status'] == 'INVALID_REQUEST')
108
+            if(json_decode($result->body, TRUE)['status'] == 'ZERO_RESULTS' || json_decode($result->body, TRUE)['status'] == 'INVALID_REQUEST')
109 109
             {
110 110
                 return "0 results";
111 111
             }
112 112
             else
113 113
             {
114
-                $name = json_decode($result->body, true)['results']['0']['address_components']['0']['long_name'];
115
-                $lat = json_decode($result->body, true)['results']['0']['geometry']['location']['lat'];
116
-                $lng = json_decode($result->body, true)['results']['0']['geometry']['location']['lng'];
114
+                $name = json_decode($result->body, TRUE)['results']['0']['address_components']['0']['long_name'];
115
+                $lat = json_decode($result->body, TRUE)['results']['0']['geometry']['location']['lat'];
116
+                $lng = json_decode($result->body, TRUE)['results']['0']['geometry']['location']['lng'];
117 117
 
118 118
                 $location = new Location();
119 119
                 $location->setLat($lat);
@@ -135,7 +135,7 @@  discard block
 block discarded – undo
135 135
                                 lng='" . $lng . "'
136 136
                             WHERE access_token='" . $this->accessToken . "'");
137 137
 
138
-                    if($result === false)
138
+                    if($result === FALSE)
139 139
                     {
140 140
                             echo "Updating location failed: (" . $db->errno . ") " . $db->error;
141 141
                     }
@@ -235,7 +235,7 @@  discard block
 block discarded – undo
235 235
             }
236 236
             else
237 237
             {
238
-                error_log("Could not vote: " . print_r($data, true));
238
+                error_log("Could not vote: " . print_r($data, TRUE));
239 239
                 return FALSE;
240 240
             } 
241 241
         }
@@ -296,7 +296,7 @@  discard block
 block discarded – undo
296 296
         
297 297
 		if($data != 'Success')
298 298
 		{
299
-			error_log(print_r($data, true));
299
+			error_log(print_r($data, TRUE));
300 300
 		}
301 301
 
302 302
         $accountCreator->location = $this->location;
@@ -360,7 +360,7 @@  discard block
 block discarded – undo
360 360
                                     expiration_date='" . $expiration_date . "'
361 361
                                 WHERE device_uid='" . $device_uid . "'");
362 362
 
363
-        if($result === false){
363
+        if($result === FALSE){
364 364
                 error_log("Adding account failed: (" . $db->errno . ") " . $db->error);
365 365
         }   
366 366
     }
@@ -408,7 +408,7 @@  discard block
 block discarded – undo
408 408
 
409 409
         $result = $db->query("SELECT id FROM votes WHERE (postId = '" . $postId . "' AND device_uid = '" . $this->deviceUid . "')");
410 410
         
411
-        if($result === false)
411
+        if($result === FALSE)
412 412
         {
413 413
             $error = db_error();
414 414
             echo $error;
@@ -440,7 +440,7 @@  discard block
 block discarded – undo
440 440
         $result = $db->query("INSERT INTO votes (device_uid, postId, type)
441 441
                         VALUES ('" . $this->deviceUid . "','" . $postId . "','" . $voteType . "')");
442 442
         
443
-        if($result === false){
443
+        if($result === FALSE){
444 444
                 $error = db_error();
445 445
                 echo $error;
446 446
                 echo "Adding Vote failed: (" . $result->errno . ") " . $result->error;
@@ -472,7 +472,7 @@  discard block
 block discarded – undo
472 472
                         "','" . $device_uid . "','" . $name . "','" . $lat . "','" . $lng . "') ");
473 473
 
474 474
         $success = TRUE;
475
-        if($result === false){
475
+        if($result === FALSE){
476 476
                 $error = db_error();
477 477
                 echo $error;
478 478
                 echo "Adding account failed: (" . $result->errno . ") " . $result->error;
Please login to merge, or discard this patch.