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

Completed
Push — master ( 747cd9...d4c09f )
by Dan
34s queued 15s
created

array_rand_value()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
use Smr\Container\DiContainer;
4
5
function logException(Throwable $e) : void {
6
	global $account, $var, $player;
7
	$message = '';
8
	$delim = "\n\n-----------\n\n";
9
10
	if (is_object($account)) {
11
		$message .= 'Login: ' . $account->getLogin() . "\n" .
12
			'Account ID: ' . $account->getAccountID() . "\n" .
13
			'E-Mail: ' . $account->getEmail() . $delim;
14
	}
15
	$message .= 'Error Message: ' . $e . $delim;
16
17
	$message .= '$var: ' . var_export($var, true);
18
19
	// Don't display passwords input by users in the log message!
20
	if (isset($_REQUEST['password'])) {
21
		$_REQUEST['password'] = '*****';
22
	}
23
	$message .= "\n\n" . '$_REQUEST: ' . var_export($_REQUEST, true);
24
	$message .= $delim;
25
26
	$message .=
27
		'User IP: ' . getIpAddress() . "\n" .
28
		'USING_AJAX: ' . (defined('USING_AJAX') ? var_export(USING_AJAX, true) : 'undefined') . "\n" .
29
		'URL: ' . (defined('URL') ? URL : 'undefined');
30
31
	try {
32
		if (function_exists('release_lock')) {
33
			release_lock(); //Try to release lock so they can carry on normally
34
		}
35
	} catch (Throwable $ee) {
36
		$message .= $delim .
37
					'Releasing Lock Failed' . "\n" .
38
					'Message: ' . $ee . "\n";
39
	}
40
41
	if (defined('SCRIPT_ID')) {
42
		$message = 'Script: ' . SCRIPT_ID . $delim . $message . "\n\n";
43
	}
44
45
	// Unconditionally send error message to the log
46
	error_log($message);
47
48
	if (ENABLE_DEBUG) {
49
		// Display error message on the page (redundant with error_log for CLI)
50
		if (php_sapi_name() !== 'cli') {
51
			echo nl2br($message);
52
		}
53
		// Skip remaining log methods (too disruptive during development)
54
		return;
55
	}
56
57
	// Send error message to the in-game auto bugs mailbox
58
	if (is_object($player) && method_exists($player, 'sendMessageToBox')) {
59
		$player->sendMessageToBox(BOX_BUGS_AUTO, $message);
60
	} elseif (is_object($account) && method_exists($account, 'sendMessageToBox')) {
61
		// Will be logged without a game_id
62
		$account->sendMessageToBox(BOX_BUGS_AUTO, $message);
63
	} else {
64
		// Will be logged without a game_id or sender_id
65
		SmrAccount::doMessageSendingToBox(0, BOX_BUGS_AUTO, $message, 0);
66
	}
67
68
	// Send error message to e-mail so that we have a permanent record
69
	if (!empty(BUG_REPORT_TO_ADDRESSES)) {
0 ignored issues
show
introduced by
The condition empty(BUG_REPORT_TO_ADDRESSES) is always true.
Loading history...
70
		$mail = setupMailer();
71
		$mail->Subject = (defined('PAGE_PREFIX') ? PAGE_PREFIX : '??? ') .
72
		                 'Automatic Bug Report';
73
		$mail->setFrom('[email protected]');
74
		$mail->Body = $message;
75
		foreach (BUG_REPORT_TO_ADDRESSES as $toAddress) {
76
			$mail->addAddress($toAddress);
77
		}
78
		$mail->send();
79
	}
80
}
81
82
function handleException(Throwable $e) {
83
	// The real error message may display sensitive information, so we
84
	// need to catch any exceptions that are thrown while logging the error.
85
	try {
86
		logException($e);
87
		$errorType = 'Unexpected Error!';
88
	} catch (Throwable $e) {
89
		error_log($e);
90
		$errorType = 'This error cannot be automatically reported. Please notify an admin!';
91
	}
92
93
	// If this is an ajax update, we don't really have a way to redirect
94
	// to an error page at this time.
95
	if (!ENABLE_DEBUG && (!defined('USING_AJAX') || !USING_AJAX)) {
96
		header('location: /error.php?msg=' . urlencode($errorType));
97
	}
98
}
99
100
/**
101
 * Can be used to convert any type of notice into an exception.
102
 */
103
function exception_error_handler($errno, $errstr, $errfile, $errline) {
104
	throw new ErrorException($errstr, $errno, E_ERROR, $errfile, $errline);
105
}
106
107
function setupMailer() {
108
	$mail = new \PHPMailer\PHPMailer\PHPMailer(true);
109
	if (!empty(SMTP_HOSTNAME)) {
0 ignored issues
show
introduced by
The condition empty(SMTP_HOSTNAME) is always false.
Loading history...
110
		$mail->isSMTP();
111
		$mail->Host = SMTP_HOSTNAME;
112
	}
113
	return $mail;
114
}
115
116
function getIpAddress() {
117
	foreach (['HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR'] as $key) {
118
		if (array_key_exists($key, $_SERVER) === true) {
119
			foreach (explode(',', $_SERVER[$key]) as $ip) {
120
				if (filter_var($ip, FILTER_VALIDATE_IP) !== false) {
121
					return $ip;
122
				}
123
			}
124
		}
125
	}
126
	return 'unknown';
127
}
128
129
function dumpMemDiff($msg) {
130
	static $memory;
131
	@ob_end_clean();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for ob_end_clean(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

131
	/** @scrutinizer ignore-unhandled */ @ob_end_clean();

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
132
	var_dump($msg);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($msg) looks like debug code. Are you sure you do not want to remove it?
Loading history...
133
	var_dump(($memory2 = memory_get_usage()) - $memory);
134
	$memory = $memory2;
135
	ob_start();
136
}
137
138
/**
139
 * Wrapper around the floor() builtin for returning an integer type.
140
 */
141
function IFloor(float $val) : int {
142
	return (int)floor($val);
143
}
144
145
/**
146
 * Wrapper around the ceil() builtin for returning an integer type.
147
 */
148
function ICeil(float $val) : int {
149
	return (int)ceil($val);
150
}
151
152
/**
153
 * Wrapper around the round() builtin for returning an integer type.
154
 */
155
function IRound(float $val) : int {
156
	return (int)round($val);
157
}
158
159
/**
160
 * Convert a numeric string to an int with input validation.
161
 */
162
function str2int(string $val) : int {
163
	$result = filter_var($val, FILTER_VALIDATE_INT);
164
	if ($result === false) {
165
		throw new Exception('Input value is not an integer: ' . $val);
166
	}
167
	return $result;
168
}
169
170
/**
171
 * Generate a cryptographically strong random hexadecimal string.
172
 * The requested length must be a multiple of 2.
173
 */
174
function random_string(int $length) : string {
175
	if ($length % 2 != 0) {
176
		throw new Exception('Length must be a multiple of 2!');
177
	}
178
	return bin2hex(random_bytes($length / 2));
179
}
180
181
/**
182
 * Return the value of a random key from an array.
183
 * @return mixed
184
 */
185
function array_rand_value(array $arr) {
186
	if (empty($arr)) {
187
		throw new Exception('Cannot pick random value from empty array!');
188
	}
189
	return $arr[array_rand($arr)];
190
}
191
192
// Defines all constants
193
require_once('config.php');
194
195
// Set up vendor and class autoloaders
196
require_once(ROOT . 'vendor/autoload.php');
197
require_once(LIB . 'autoload.inc.php');
198
spl_autoload_register('get_class_loc');
199
200
// Set up dependency injection container
201
DiContainer::initializeContainer();
202