Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Failed Conditions
Push — page-classes ( c69fb3 )
by Dan
07:23
created

IpViewResults   A

Complexity

Total Complexity 37

Size/Duplication

Total Lines 251
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 160
dl 0
loc 251
rs 9.44
c 1
b 0
f 0
wmc 37

1 Method

Rating   Name   Duplication   Size   Complexity  
F build() 0 245 37
1
<?php declare(strict_types=1);
2
3
namespace Smr\Pages\Admin;
4
5
use Exception;
6
use Smr\Database;
7
use Smr\Page\AccountPage;
8
use Smr\Page\ReusableTrait;
9
use Smr\Session;
10
use Smr\Template;
11
use SmrAccount;
12
use SmrAlliance;
13
14
class IpViewResults extends AccountPage {
15
16
	use ReusableTrait;
17
18
	public string $file = 'admin/ip_view_results.php';
19
20
	public function build(SmrAccount $account, Template $template): void {
21
		$session = Session::getInstance();
22
		$variable = $session->getRequestVar('variable');
23
		$type = $session->getRequestVar('type');
24
25
		$db = Database::getInstance();
26
27
		$container = new IpView();
28
		$template->assign('BackHREF', $container->href());
29
30
		$container = new AccountCloseProcessor();
31
		$template->assign('CloseHREF', $container->href());
32
33
		$template->assign('type', $type);
34
35
		if ($type == 'comp_share') {
36
			//another script for comp share
37
			require('comp_share.php');
38
			return;
39
		}
40
41
		if ($type == 'list') {
42
			//=========================================================
43
			// List all IPs
44
			//=========================================================
45
46
			//we are listing ALL IPs
47
			$dbResult = $db->read('SELECT * FROM account_has_ip GROUP BY ip, account_id ORDER BY ip');
48
			$ip_array = [];
49
			//make sure we have enough but not too mant to reduce lag
50
			foreach ($dbResult->records() as $dbRecord) {
51
				$id = $dbRecord->getInt('account_id');
52
				$ip = $dbRecord->getString('ip');
53
				$host = $dbRecord->getString('host');
54
				$ip_array[] = ['ip' => $ip, 'id' => $id, 'host' => $host];
55
			}
56
57
			$rows = [];
58
			foreach ($ip_array as $db_ent) {
59
				$db_ip = $db_ent['ip'];
60
				$host = $db_ent['host'];
61
				$account_id = $db_ent['id'];
62
				$acc = SmrAccount::getAccount($account_id);
63
				$disabled = $acc->isDisabled();
64
				$close_reason = $disabled ? $disabled['Reason'] : '';
65
66
				$row = [
67
					'account_id' => $account_id,
68
					'login' => $acc->getLogin(),
69
					'ip' => $db_ip,
70
					'host' => $host,
71
					'close_reason' => $close_reason,
72
				];
73
74
				$match_id = 0;
75
				foreach ($ip_array as $db_ent2) {
76
					if ($db_ip == $db_ent2['ip'] && $account_id != $db_ent2['id']) {
77
						$match_id = $db_ent2['id'];
78
						break;
79
					}
80
				}
81
				$matches = $match_id > 0;
82
				$row['matches'] = $matches;
83
84
				if ($matches) {
85
					$dbResult2 = $db->read('SELECT * FROM account_exceptions WHERE account_id = ' . $db->escapeNumber($account_id));
86
					if ($dbResult2->hasRecord()) {
87
						$ex = $dbResult2->record()->getString('reason');
88
					} else {
89
						$ex = '';
90
					}
91
92
					if (empty($ex) && empty($close_reason)) {
93
						$checked = 'checked';
94
					} else {
95
						$checked = '';
96
					}
97
98
					if (!empty($ex)) {
99
						$suspicion = 'DB Exception - ' . $ex;
100
					} else {
101
						$suspicion = 'Match:' . $match_id;
102
					}
103
104
					$row['checked'] = $checked;
105
					$row['suspicion'] = $suspicion;
106
				}
107
				$rows[] = $row;
108
			}
109
			$template->assign('Rows', $rows);
110
111
		} elseif ($type == 'account_ips') {
112
			//=========================================================
113
			// List all IPs for a specific account (id)
114
			//=========================================================
115
			if (!is_numeric($variable)) {
116
				create_error('Account id must be numeric.');
117
			}
118
			$accountID = (int)$variable;
119
			$template->assign('BanAccountID', $accountID);
120
			$summary = 'Account ' . $accountID . ' has had the following IPs at the following times.';
121
			$template->assign('Summary', $summary);
122
			$dbResult = $db->read('SELECT * FROM account_exceptions WHERE account_id = ' . $db->escapeNumber($variable));
123
			if ($dbResult->hasRecord()) {
124
				$ex = $dbResult->record()->getString('reason');
125
				$template->assign('Exception', $ex);
126
			}
127
			$viewAccount = SmrAccount::getAccount($accountID);
128
			$disabled = $viewAccount->isDisabled();
129
			if ($disabled !== false) {
130
				$template->assign('CloseReason', $disabled['Reason']);
131
			}
132
			$rows = [];
133
			$dbResult = $db->read('SELECT * FROM account_has_ip WHERE account_id = ' . $db->escapeNumber($variable) . ' ORDER BY time');
134
			foreach ($dbResult->records() as $dbRecord) {
135
				$rows[] = [
136
					'ip' => $dbRecord->getString('ip'),
137
					'date' => date($account->getDateTimeFormat(), $dbRecord->getInt('time')),
138
					'host' => $dbRecord->getString('host'),
139
				];
140
			}
141
			$template->assign('Rows', $rows);
142
143
		} elseif (in_array($type, ['search', 'alliance_ips', 'wild_log', 'wild_in', 'compare', 'compare_log', 'wild_ip', 'wild_host'])) {
144
			if ($type == 'search') {
145
				//=========================================================
146
				// Search for a specific IP
147
				//=========================================================
148
				$ip = $variable;
149
				$host = gethostbyaddr($ip);
150
				if ($host == $ip) {
151
					$host = 'unknown';
152
				}
153
				$summary = 'The following accounts have the IP address ' . $ip . ' (' . $host . ')';
154
				$dbResult = $db->read('SELECT * FROM account_has_ip WHERE ip = ' . $db->escapeString($ip) . ' ORDER BY account_id');
155
156
			} elseif ($type == 'alliance_ips') {
157
				//=========================================================
158
				// List all IPs for a specific alliance
159
				//=========================================================
160
				[$allianceID, $gameID] = preg_split('/[\/]/', $variable);
161
				if (!is_numeric($gameID) || !is_numeric($allianceID)) {
162
					create_error('Incorrect format used.');
163
				}
164
				$allianceID = (int)$allianceID;
165
				$gameID = (int)$gameID;
166
				$name = SmrAlliance::getAlliance($allianceID, $gameID)->getAllianceDisplayName();
167
				$dbResult = $db->read('SELECT ip.* FROM account_has_ip ip JOIN player USING(account_id) WHERE game_id = ' . $db->escapeNumber($gameID) . ' AND alliance_id = ' . $db->escapeNumber($allianceID) . ' ORDER BY ip');
168
				$summary = 'Listing all IPs for alliance ' . $name . ' in game with ID ' . $gameID;
169
170
			} elseif ($type == 'wild_log') {
171
				//=========================================================
172
				// List all IPs for a wildcard login name
173
				//=========================================================
174
				$dbResult = $db->read('SELECT ip.* FROM account_has_ip ip JOIN account USING(account_id) WHERE login LIKE ' . $db->escapeString($variable) . ' ORDER BY login, ip');
175
				$summary = 'Listing all IPs for login names LIKE ' . $variable;
176
177
			} elseif ($type == 'wild_in') {
178
				//=========================================================
179
				// List all IPs for a wildcard ingame name
180
				//=========================================================
181
				$dbResult = $db->read('SELECT ip.* FROM account_has_ip ip JOIN player USING(account_id) WHERE player_name LIKE ' . $db->escapeString($variable) . ' ORDER BY player_name, ip');
182
				$summary = 'Listing all IPs for ingame names LIKE ' . $variable;
183
184
			} elseif ($type == 'compare') {
185
				//=========================================================
186
				// List all IPs for specified players
187
				//=========================================================
188
				$list = preg_split('/[,]+[\s]/', $variable);
189
				$dbResult = $db->read('SELECT ip.* FROM account_has_ip ip JOIN player USING(account_id) WHERE player_name IN (' . $db->escapeArray($list) . ') ORDER BY ip');
190
				$summary = 'Listing all IPs for ingame names ' . $variable;
191
192
			} elseif ($type == 'compare_log') {
193
				//=========================================================
194
				// List all IPs for specified logins
195
				//=========================================================
196
				$list = preg_split('/[,]+[\s]/', $variable);
197
				$dbResult = $db->read('SELECT ip.* FROM account_has_ip ip JOIN account USING(account_id) WHERE login IN (' . $db->escapeArray($list) . ') ORDER BY ip');
198
				$summary = 'Listing all IPs for logins ' . $variable;
199
200
			} elseif ($type == 'wild_ip') {
201
				//=========================================================
202
				// Wildcard IP search
203
				//=========================================================
204
				$dbResult = $db->read('SELECT * FROM account_has_ip WHERE ip LIKE ' . $db->escapeString($variable) . ' GROUP BY account_id, ip ORDER BY time DESC, ip');
205
				$summary = 'Listing all IPs LIKE ' . $variable;
206
207
			} elseif ($type == 'wild_host') {
208
				//=========================================================
209
				// Wildcard host search
210
				//=========================================================
211
				$dbResult = $db->read('SELECT * FROM account_has_ip WHERE host LIKE ' . $db->escapeString($variable) . ' GROUP BY account_id, ip ORDER BY time, ip');
212
				$summary = 'Listing all hosts LIKE ' . $variable;
213
			} else {
214
				throw new Exception('Unknown type: ' . $type);
215
			}
216
			$template->assign('Summary', $summary);
217
218
			// initialize history variables
219
			$last_id = null;
220
			$last_ip = null;
221
222
			$rows = [];
223
			foreach ($dbResult->records() as $dbRecord) {
224
				$id = $dbRecord->getInt('account_id');
225
				$time = $dbRecord->getInt('time');
226
				$ip = $dbRecord->getString('ip');
227
				$host = $dbRecord->getString('host');
228
229
				if ($id === $last_id && $ip === $last_ip) {
230
					continue;
231
				}
232
				$acc = SmrAccount::getAccount($id);
233
				$disabled = $acc->isDisabled();
234
				$close_reason = $disabled ? $disabled['Reason'] : '';
235
				$dbResult2 = $db->read('SELECT * FROM player WHERE account_id = ' . $db->escapeNumber($id));
236
				$names = [];
237
				foreach ($dbResult2->records() as $dbRecord2) {
238
					$names[] = $dbRecord2->getString('player_name');
239
				}
240
				$dbResult2 = $db->read('SELECT * FROM account_exceptions WHERE account_id = ' . $db->escapeNumber($id));
241
				if ($dbResult2->hasRecord()) {
242
					$ex = $dbResult2->record()->getString('reason');
243
				} else {
244
					$ex = '';
245
				}
246
				$last_ip = $ip;
247
				$last_id = $id;
248
249
				$rows[] = [
250
					'account_id' => $id,
251
					'login' => $acc->getLogin(),
252
					'date' => date($account->getDateTimeFormat(), $time),
253
					'ip' => $ip,
254
					'host' => $host,
255
					'names' => implode(', ', array_unique($names)),
256
					'exception' => $ex,
257
					'close_reason' => $close_reason,
258
				];
259
			}
260
			$template->assign('Rows', $rows);
261
262
		}
263
264
		$template->assign('PageTopic', 'IP Search Results');
265
	}
266
267
}
268