Failed Conditions
Pull Request — master (#326)
by
unknown
02:33
created

core/process/data.loader.php (2 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
3
// Include & load the variables
4
// ############################
5
6
$variables = SYS_PATH.'/core/json/variables.json';
7
$config = json_decode(file_get_contents($variables));
8
    
9
$pokedex_tree_file = file_get_contents(SYS_PATH.'/core/json/pokedex.tree.json');
10
$trees = json_decode($pokedex_tree_file);
11
12
if (!defined('SYS_PATH')) {
13
	echo 'Error: config.php does not exist or failed to load.<br>';
14
	echo 'Check whether you renamed the config.example.php file!';
15
	exit();
16
} 
17
if (!isset($config->system)) {
18
	echo 'Error: Could not load core/json/variables.json.<br>';
19
	echo 'json_last_error(): '.json_last_error().'<br>';
20
	echo 'Check the file encoding as well. It have to be UTF-8 without BOM!';
21
	exit();
22
}
23
24
25
// Manage Time Interval
26
// #####################
27
28
include_once('timezone.loader.php');
29
30
31
32
// Debug mode
33
#############
34
35
if (SYS_DEVELOPMENT_MODE) {
36
	error_reporting(E_ALL);
37
}
38
39
40
// MySQL Connect
41
#################
42
43
44
$mysqli = new mysqli(SYS_DB_HOST, SYS_DB_USER, SYS_DB_PSWD, SYS_DB_NAME, SYS_DB_PORT);
45
46
47
if ($mysqli->connect_error != '') {
48
	header('Location:'.HOST_URL.'offline.html');
49
	exit();
50
}
51
52
53
// Perform some tests to be sure that we got datas and rights
54
// If not we lock the website (HA-HA-HA evil laught)
55
// Those test are performed once.
56
##############################################################
57
58
if (!file_exists(SYS_PATH.'/install/done.lock')) {
59
	// run install tests
60
	include_once('install/tester.php');
61
	run_tests();
62
63
	// check for error
64
	if (file_exists(SYS_PATH.'/install/website.lock')) {
65
		echo file_get_contents(SYS_PATH.'/install/website.lock');
66
		exit();
67
	} else {
68
		$content = time();
69
		file_put_contents(SYS_PATH.'/install/done.lock', $content);
70
		// everything seems to be fine let's run an initial cronjob
71
		include_once(SYS_PATH.'/core/cron/crontabs.include.php');
72
	}
73
}
74
75
76
// Load the locale elements
77
############################
78
79
include_once('locales.loader.php');
80
81
82
83
##########################
84
//
85
// Pages data loading
86
//
87
##########################
88
89 View Code Duplication
if (isset($_GET['page'])) {
90
	$page = htmlentities($_GET['page']);
91
} else {
92
	$page = '';
93
}
94
95
if (!empty($page)) {
96
	switch ($page) {
97
		// Single Pokemon
98
		#################
99
100
		case 'pokemon':
101
			// Current Pokemon datas
102
			// ---------------------
103
104
			$pokemon_id = mysqli_real_escape_string($mysqli, $_GET['id']);
105
106
			if (!is_object($pokemons->pokemon->$pokemon_id)) {
107
				header('Location:/404');
108
				exit();
109
			}
110
111
112
			$pokemon = new stdClass();
113
			$pokemon = $pokemons->pokemon->$pokemon_id;
114
			$pokemon->id = $pokemon_id;
115
116
117
			// Some math
118
			// ----------
119
120
			$pokemon->max_cp_percent = percent(5441, $pokemon->max_cp);
121
			$pokemon->max_hp_percent = percent(411, $pokemon->max_hp);
122
123
124
			// Set tree
125
			// ----------
126
127
			$candy_id = $pokemon->candy_id;
128
			$pokemon->tree = $trees->$candy_id;
129
130
131
			// Get Dabase results
132
			//-------------------
133
134
			// Total gym protected
135
136
			$req = "SELECT COUNT(DISTINCT(gym_id)) AS total FROM gym WHERE guard_pokemon_id = '".$pokemon_id."'";
137
			$result = $mysqli->query($req);
138
			$data = $result->fetch_object();
139
140
			$pokemon->protected_gyms = $data->total;
141
142
			// Spawn rate
143
144
			if ($pokemon->spawn_count > 0 && $pokemon->per_day == 0) {
145
				$pokemon->spawns_per_day = "<1";
146
			} else {
147
				$pokemon->spawns_per_day = $pokemon->per_day;
148
			}
149
150
			// Last Raid seen
151
152
			$req = "SELECT r.end, (CONVERT_TZ(r.end, '+00:00', '".$time_offset."')) AS end_time_real, g.latitude, g.longitude
153
						FROM raid  r JOIN gym g
154
						ON r.gym_id = g.gym_id
155
						WHERE r.pokemon_id = '".$pokemon_id."'
156
						ORDER BY end DESC
157
						LIMIT 0,1";
158
			$result = $mysqli->query($req);
159
			$data = $result->fetch_object();
160
161 View Code Duplication
			if (isset($data)) {
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...
162
				$pokemon->last_raid_seen = strtotime($data->end_time_real);
163
				$pokemon->last_raid_position = new stdClass();
164
				$pokemon->last_raid_position->latitude = $data->latitude;
165
				$pokemon->last_raid_position->longitude = $data->longitude;
166
			}
167
            
168
			// Raid count
169
            
170
			$req = "SELECT Count(*) as count
171
						FROM raid
172
						WHERE pokemon_id = '".$pokemon_id."'";
173
			$result = $mysqli->query($req);
174
			$data = $result->fetch_object();
175
176
			if (isset($data)) {
177
						$pokemon->raid_count = $data->count;
178
			}
179
            
180
			// Last seen
181
            
182
			$req = "SELECT disappear_time, (CONVERT_TZ(disappear_time, '+00:00', '".$time_offset."')) AS disappear_time_real, latitude, longitude
183
						FROM pokemon
184
						WHERE pokemon_id = '".$pokemon_id."'
185
						ORDER BY disappear_time DESC
186
						LIMIT 0,1";
187
			$result = $mysqli->query($req);
188
			$data = $result->fetch_object();
189
190 View Code Duplication
			if (isset($data)) {
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...
191
				$pokemon->last_seen = strtotime($data->disappear_time_real);
192
				$pokemon->last_position = new stdClass();
193
				$pokemon->last_position->latitude = $data->latitude;
194
				$pokemon->last_position->longitude = $data->longitude;
195
			}
196
197
			// Related Pokemons
198
			// ----------------
199
200
201
			foreach ($pokemon->types as $type) {
202
				$types[] = $type;
203
			}
204
205
			$related = array();
206
			foreach ($pokemons->pokemon as $pokeid => $test_pokemon) {
207
				if (!empty($test_pokemon->types)) {
208
					foreach ($test_pokemon->types as $type) {
209
						if (in_array($type, $types) && $pokeid <= $config->system->max_pokemon) {
210
							if (!in_array($pokeid, $related)) {
211
								$related[] = $pokeid;
212
							}
213
						}
214
					}
215
				}
216
			}
217
			sort($related);
218
			
219
			// Top50 Pokemon List
220
			// Don't run the query for super common pokemon because it's too heavy
221
			if ($pokemon->spawn_rate < 0.20) {
222
				// Make it sortable; default sort: cp DESC
223
				$top_possible_sort = array('IV', 'cp', 'individual_attack', 'individual_defense', 'individual_stamina', 'move_1', 'move_2', 'disappear_time');
224
				$top_order = isset($_GET['order']) ? $_GET['order'] : '';
225
				$top_order_by = in_array($top_order, $top_possible_sort) ? $_GET['order'] : 'cp';
226
				$top_direction = isset($_GET['direction']) ? 'ASC' : 'DESC';
227
				$top_direction = !isset($_GET['order']) && !isset($_GET['direction']) ? 'DESC' : $top_direction;
228
229
				$req = "SELECT (CONVERT_TZ(disappear_time, '+00:00', '".$time_offset."')) AS distime, pokemon_id, disappear_time, latitude, longitude,
230
						cp, individual_attack, individual_defense, individual_stamina,
231
						ROUND(SUM(100*(individual_attack+individual_defense+individual_stamina)/45),1) AS IV, move_1, move_2, form
232
						FROM pokemon
233
						WHERE pokemon_id = '".$pokemon_id."' AND move_1 IS NOT NULL AND move_1 <> '0'
234
						GROUP BY encounter_id
235
						ORDER BY $top_order_by $top_direction, disappear_time DESC
236
						LIMIT 0,50";
237
238
				$result = $mysqli->query($req);
239
				$top = array();
240
				while ($data = $result->fetch_object()) {
241
					$top[] = $data;
242
				}
243
			}
244
			
245
			// Trainer with highest Pokemon
246
			
247
			// Make it sortable but use different variable names this time; default sort: cp DESC
248
			$best_possible_sort = array('trainer_name', 'IV', 'cp', 'move_1', 'move_2', 'last_seen');
249
			$best_order = isset($_GET['order']) ? $_GET['order'] : '';
250
			$best_order_by = in_array($best_order, $best_possible_sort) ? $_GET['order'] : 'cp';
251
			$best_direction = isset($_GET['direction']) ? 'ASC' : 'DESC';
252
			$best_direction = !isset($_GET['order']) && !isset($_GET['direction']) ? 'DESC' : $best_direction;
253
			
254
			$trainer_blacklist = "";
255 View Code Duplication
			if (!empty($config->system->trainer_blacklist)) {
256
				$trainer_blacklist = " AND trainer_name NOT IN ('".implode("','", $config->system->trainer_blacklist)."')";
257
			}
258
259
			$req = "SELECT trainer_name, ROUND(SUM(100*(iv_attack+iv_defense+iv_stamina)/45),1) AS IV, move_1, move_2, cp,
260
					DATE_FORMAT(last_seen, '%Y-%m-%d') AS lasttime, last_seen
261
					FROM gympokemon
262
					WHERE pokemon_id = '".$pokemon_id."'".$trainer_blacklist."
263
					GROUP BY pokemon_uid
264
					ORDER BY $best_order_by $best_direction, trainer_name ASC
265
					LIMIT 0,50";
266
			
267
			$result = $mysqli->query($req);
268
			$toptrainer = array();
269
			while ($data = $result->fetch_object()) {
270
				$toptrainer[] = $data;
271
			}
272
			
273
			break;
274
275
		// Pokedex
276
		##########
277
278
		case 'pokedex':
279
			// Pokemon List from the JSON file
280
			// --------------------------------
281
282
			$max = $config->system->max_pokemon;
283
			$pokedex = new stdClass();
284
285
			for ($i = 1; $i <= $max; $i++) {
286
				$pokedex->$i = new stdClass();
287
				$pokedex->$i->id = $i;
288
				$pokedex->$i->permalink = 'pokemon/'.$i;
289
				$pokedex->$i->img = $pokemons->pokemon->$i->img;
290
				$pokedex->$i->name = $pokemons->pokemon->$i->name;
291
				$pokedex->$i->spawn = ($pokemons->pokemon->$i->spawn_count > 0) ? 1 : 0;
292
				$pokedex->$i->spawn_count = $pokemons->pokemon->$i->spawn_count;
293
			}
294
295
296
			break;
297
298
299
		// Pokestops
300
		############
301
302
		case 'pokestops':
303
			$pokestop = new stdClass();
304
305
			$req = "SELECT COUNT(*) AS total FROM pokestop";
306
			$result = $mysqli->query($req);
307
			$data = $result->fetch_object();
308
309
			$pokestop->total = $data->total;
310
311
			$req = "SELECT COUNT(*) AS total FROM pokestop WHERE lure_expiration >= UTC_TIMESTAMP()";
312
			$result = $mysqli->query($req);
313
			$data = $result->fetch_object();
314
315
			$pokestop->lured = $data->total;
316
317
318
319
			break;
320
321
322
		// Gyms
323
		########
324
325
326
		case 'gym':
327
			// 3 Teams (teamm rocket is neutral)
328
			// 1 Fight
329
330
			$teams = new stdClass();
331
332
			$teams->mystic = new stdClass();
333
			$teams->mystic->guardians = new stdClass();
334
			$teams->mystic->id = 1;
335
336
			$teams->valor = new stdClass();
337
			$teams->valor->guardians = new stdClass();
338
			$teams->valor->id = 2;
339
340
			$teams->instinct = new stdClass();
341
			$teams->instinct->guardians = new stdClass();
342
			$teams->instinct->id = 3;
343
344
			$teams->rocket = new stdClass();
345
			$teams->rocket->guardians = new stdClass();
346
			$teams->rocket->id = 0;
347
348
349
350
			foreach ($teams as $team_key => $team_values) {
351
				// Team Guardians
352
353
				$req = "SELECT COUNT(*) AS total, guard_pokemon_id FROM gym WHERE team_id = '".$team_values->id."' GROUP BY guard_pokemon_id ORDER BY total DESC LIMIT 0,3";
354
				$result = $mysqli->query($req);
355
356
				$i = 0;
357
358
				while ($data = $result->fetch_object()) {
359
					$teams->$team_key->guardians->$i = $data->guard_pokemon_id;
360
361
					$i++;
362
				}
363
364
365
				// Gym owned and average points
366
367
				$req 	= "SELECT COUNT(DISTINCT(gym_id)) AS total, ROUND(AVG(total_cp),0) AS average_points FROM gym WHERE team_id = '".$team_values->id."'";
368
				$result = $mysqli->query($req);
369
				$data	= $result->fetch_object();
370
371
				$teams->$team_key->gym_owned = $data->total;
372
				$teams->$team_key->average = $data->average_points;
373
			}
374
375
376
			break;
377
378
379
380
381
382
		case 'dashboard':
383
			// This case is only used for test purpose.
384
385
			$stats_file = SYS_PATH.'/core/json/gym.stats.json';
386
387
			if (!is_file($stats_file)) {
388
				echo "Sorry, no Gym stats file was found. <br> Did you enable cron? ";
389
				exit();
390
			}
391
392
393
			$stats_file = SYS_PATH.'/core/json/pokemon.stats.json';
394
395
			if (!is_file($stats_file)) {
396
				echo "Sorry, no Pokémon stats file was found. <br> Did you enabled cron?";
397
				exit();
398
			}
399
400
401
			$stats_file = SYS_PATH.'/core/json/pokestop.stats.json';
402
403
			if (!is_file($stats_file)) {
404
				echo "Sorry, no Pokéstop stats file was found. <br> Did you enabled cron?";
405
				exit();
406
			}
407
408
			if ($config->system->captcha_support) {
409
				$stats_file = SYS_PATH.'/core/json/captcha.stats.json';
410
411
				if (!is_file($stats_file)) {
412
					echo "Sorry, no Captcha stats file were found  <br> Have you enable cron?";
413
					exit();
414
				}
415
			}
416
417
			break;
418
	}
419
} /////////////
420
// Homepage
421
/////////////
422
423
else {
424
	$home = new stdClass();
425
426
	// Right now
427
	// ---------
428
429
	$req = "SELECT COUNT(*) AS total FROM pokemon WHERE disappear_time >= UTC_TIMESTAMP()";
430
	$result = $mysqli->query($req);
431
	$data = $result->fetch_object();
432
433
434
	$home->pokemon_now = $data->total;
435
436
437
	// Lured stops
438
	// -----------
439
440
	$req = "SELECT COUNT(*) AS total FROM pokestop WHERE lure_expiration >= UTC_TIMESTAMP()";
441
	$result = $mysqli->query($req);
442
	$data = $result->fetch_object();
443
444
	$home->pokestop_lured = $data->total;
445
446
447
	// Gyms
448
	// ----
449
450
	$req = "SELECT COUNT(DISTINCT(gym_id)) AS total FROM gym";
451
	$result = $mysqli->query($req);
452
	$data = $result->fetch_object();
453
454
	$home->gyms = $data->total;
455
456
457
	// Recent spawns
458
	// ------------
459
460 View Code Duplication
	if ($config->system->recents_filter) {
461
		// get all mythic pokemon ids
462
		$mythic_pokemons = array();
463
		foreach ($pokemons->pokemon as $id => $pokemon) {
464
			if ($pokemon->spawn_rate < $config->system->recents_filter_rarity && $pokemon->rating >= $config->system->recents_filter_rating) {
465
				$mythic_pokemons[] = $id;
466
			}
467
		}
468
		// get all mythic pokemon
469
		$req = "SELECT DISTINCT pokemon_id, encounter_id, disappear_time, last_modified, (CONVERT_TZ(disappear_time, '+00:00', '".$time_offset."')) AS disappear_time_real,
470
				latitude, longitude, cp, individual_attack, individual_defense, individual_stamina
471
				FROM pokemon
472
				WHERE pokemon_id IN (".implode(",", $mythic_pokemons).")
473
				ORDER BY last_modified DESC
474
				LIMIT 0,12";
475
	} else {
476
		// get all pokemon
477
		$req = "SELECT DISTINCT pokemon_id, encounter_id, disappear_time, last_modified, (CONVERT_TZ(disappear_time, '+00:00', '".$time_offset."')) AS disappear_time_real,
478
				latitude, longitude, cp, individual_attack, individual_defense, individual_stamina
479
				FROM pokemon
480
				ORDER BY last_modified DESC
481
				LIMIT 0,12";
482
	}
483
	$result 	= $mysqli->query($req);
484
	$recents	= array();
485
486
	if ($result->num_rows > 0) {
487
		while ($data = $result->fetch_object()) {
488
			$recent = new stdClass();
489
			$recent->id = $data->pokemon_id;
490
			$recent->uid = $data->encounter_id;
491
			$recent->last_seen = strtotime($data->disappear_time_real);
492
493
			$location_link = isset($config->system->location_url) ? $config->system->location_url : 'https://maps.google.com/?q={latitude},{longitude}&ll={latitude},{longitude}&z=16';
494
			$location_link = str_replace('{latitude}', $data->latitude, $location_link);
495
			$location_link = str_replace('{longitude}', $data->longitude, $location_link);
496
			$recent->location_link = $location_link;
497
498
			if ($config->system->recents_encounter_details) {
499
				$recent->encdetails = new stdClass();
500
				$recent->encdetails->cp = $data->cp;
501
				$recent->encdetails->attack = $data->individual_attack;
502
				$recent->encdetails->defense = $data->individual_defense;
503
				$recent->encdetails->stamina = $data->individual_stamina;
504
				if (isset($recent->encdetails->cp) && isset($recent->encdetails->attack) && isset($recent->encdetails->defense) && isset($recent->encdetails->stamina)) {
505
					$recent->encdetails->available = true;
506
				} else {
507
					$recent->encdetails->available = false;
508
				}
509
			}
510
511
			$recents[] = $recent;
512
		}
513
	}
514
515
516
	// Team battle
517
	// -----------
518
519
	$home->teams = new stdClass();
520
521
	// Team
522
	// 1 = bleu
523
	// 2 = rouge
524
	// 3 = jaune
525
526
	$req = "SELECT COUNT(DISTINCT(gym_id)) AS total FROM gym WHERE team_id = '1'";
527
	$result = $mysqli->query($req);
528
	$data = $result->fetch_object();
529
530
	$home->teams->mystic = $data->total;
531
532
533
	$req = "SELECT COUNT(DISTINCT(gym_id)) AS total FROM gym WHERE team_id = '2'";
534
	$result = $mysqli->query($req);
535
	$data = $result->fetch_object();
536
537
	$home->teams->valor = $data->total;
538
539
540
	$req = "SELECT COUNT(DISTINCT(gym_id)) AS total FROM gym WHERE team_id = '3'";
541
	$result = $mysqli->query($req);
542
	$data = $result->fetch_object();
543
544
	$home->teams->instinct = $data->total;
545
546
547
	$req = "SELECT COUNT(DISTINCT(gym_id)) AS total FROM gym WHERE team_id = '0'";
548
	$result = $mysqli->query($req);
549
	$data = $result->fetch_object();
550
551
	$home->teams->rocket = $data->total;
552
}
553