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
Pull Request — main (#1494)
by Dan
10:59 queued 05:29
created

IpViewResults   A

Complexity

Total Complexity 37

Size/Duplication

Total Lines 249
Duplicated Lines 0 %

Importance

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

1 Method

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