QoL 45: intval and floatval in place of int and float casts and a string cast in place of strval, redirect in place of raw Location headers wherever functions.php is loaded, redirect fallback escapes the URL with json_encode and escapeHtml, escapeHtml for the last htmlspecialchars on a page, and the plain-HTTP cookie notice through flashAlert

This commit is contained in:
johnnyq
2026-11-21 00:20:16 -05:00
parent 5cb09cee0b
commit eb79252ca0
78 changed files with 169 additions and 193 deletions

View File

@@ -149,7 +149,7 @@ $type_noun = function ($type) {
$install_root = dirname(__DIR__);
$file_owner = '';
if (function_exists('posix_getpwuid')) {
$file_owner = (string) (posix_getpwuid((int) @fileowner($install_root . '/scripts/restore_cli.php'))['name'] ?? '');
$file_owner = (string) (posix_getpwuid(intval(@fileowner($install_root . '/scripts/restore_cli.php')))['name'] ?? '');
}
$shell_word = function ($word) {
return preg_match('/^[A-Za-z0-9_\/.@%+=:,-]+$/', $word) ? $word : escapeshellarg($word);

View File

@@ -137,7 +137,7 @@ $csrf = $_SESSION['csrf_token'];
};
$cover = array_filter([
$row['contract_template_support_hours'],
$row['contract_template_rate_standard'] !== null ? number_format((float) $row['contract_template_rate_standard'], 2) . '/hr'
$row['contract_template_rate_standard'] !== null ? number_format(floatval($row['contract_template_rate_standard']), 2) . '/hr'
: ($template_defaults['rate'] > 0 ? number_format($template_defaults['rate'], 2) . '/hr (default)' : ''),
$row['contract_template_net_terms'] !== null ? $net_text($row['contract_template_net_terms']) : $net_text($template_defaults['net_terms']) . ' (default)',
]);

View File

@@ -43,7 +43,7 @@ $managed_kept = $managed_mode === 'active' ? [] : (MANAGED_KEEP_JOBS[$managed_mo
$install_root = dirname(__DIR__);
$file_owner = '';
if (function_exists('posix_getpwuid')) {
$file_owner = (string) (posix_getpwuid((int) @fileowner($install_root . '/cron/cron.php'))['name'] ?? '');
$file_owner = (string) (posix_getpwuid(intval(@fileowner($install_root . '/cron/cron.php')))['name'] ?? '');
}
$shell_word = function ($word) {
return preg_match('/^[A-Za-z0-9_\/.@%+=:,-]+$/', $word) ? $word : escapeshellarg($word);
@@ -194,9 +194,9 @@ $took = function ($seconds) {
if ($seconds < 60) {
return round($seconds) . ' s';
}
$minutes = intdiv((int) round($seconds), 60);
$minutes = intdiv(intval(round($seconds)), 60);
if ($minutes < 60) {
return $minutes . ' min ' . ((int) round($seconds) % 60) . ' s';
return $minutes . ' min ' . (intval(round($seconds)) % 60) . ' s';
}
return intdiv($minutes, 60) . ' h ' . ($minutes % 60) . ' min';
};

View File

@@ -14,7 +14,7 @@ $t_value = function ($key, $default = '') use ($t) {
return $t !== null && array_key_exists("contract_template_$key", $t) && $t["contract_template_$key"] !== null ? $t["contract_template_$key"] : $default;
};
$t_money = function ($value) {
return $value === '' || $value === null ? '' : number_format((float) $value, 2, '.', '');
return $value === '' || $value === null ? '' : number_format(floatval($value), 2, '.', '');
};
$t_hours = function ($value) {
return floatval($value) > 0 ? rtrim(rtrim(number_format(floatval($value), 2, '.', ''), '0'), '.') : '';

View File

@@ -90,7 +90,7 @@ if (!$template) { ?>
break;
}
}
$default = $default ?: (int) array_key_first($options);
$default = $default ?: intval(array_key_first($options));
?>
<tr data-bill-id="<?= $software_id ?>">
<td class="ps-2"><input class="form-check-input" type="checkbox" name="software_ids[]" value="<?= $software_id ?>" aria-label="Bill <?= escapeHtml($row['client_name']) ?>" <?= $options ? 'checked' : 'disabled' ?>></td>

View File

@@ -83,7 +83,7 @@ if (!is_array($json) || empty($json['refresh_token']) || empty($json['access_tok
$refresh_token = (string) $json['refresh_token'];
$access_token = (string) $json['access_token'];
$expires_at = date('Y-m-d H:i:s', time() + (int)($json['expires_in'] ?? 3600));
$expires_at = date('Y-m-d H:i:s', time() + intval($json['expires_in'] ?? 3600));
$refresh_token_esc = mysqli_real_escape_string($mysqli, $refresh_token);
$access_token_esc = mysqli_real_escape_string($mysqli, $access_token);

View File

@@ -403,7 +403,7 @@ function demoIsFuture($date) {
// How much history a client gets - their whole life with us, capped at the two
// year window. Newer clients ramp up rather than appearing fully formed.
function demoClientMonths($profile) {
return max(3, min(24, (int)floor($profile['age_days'] / 30)));
return max(3, min(24, intval(floor($profile['age_days'] / 30))));
}
// ------------------------------
@@ -1978,8 +1978,8 @@ function demoBuildAssets($mysqli, $profile, $index, $client_id, $location_id, $c
// this properly is what makes the warranty, end of life and refresh
// reports show anything - a fleet where nothing is ever old is not a
// fleet anyone recognises.
$batch = (int)floor(($number - 1) / 4);
$purchase_months = min(70, 3 + ($batch * 12) + (($seed + $index) % 7) + (int)floor($profile['age_days'] / 120));
$batch = intval(floor(($number - 1) / 4));
$purchase_months = min(70, 3 + ($batch * 12) + (($seed + $index) % 7) + intval(floor($profile['age_days'] / 120)));
// A few of the oldest Windows machines never made the move to Windows 11
$os = $model[2];
if (strpos($os, 'Windows 11') === 0 && $purchase_months >= 50 && ($seed + $index) % 3 === 0) {
@@ -2561,8 +2561,8 @@ function demoSeedTicket($mysqli, $fields, $trail, $context, &$counts) {
if ($stopped_at) {
// Business minutes, approximated at eight hours to the day rather
// than pulling the business hours calculation in here
$elapsed = max(1, (int)round((strtotime($stopped_at) - strtotime($fields['ticket_created_at'])) / 60));
$days = max(1, (int)ceil($elapsed / 1440));
$elapsed = max(1, intval(round((strtotime($stopped_at) - strtotime($fields['ticket_created_at'])) / 60)));
$days = max(1, intval(ceil($elapsed / 1440)));
starterInsert($mysqli, 'sla_history', [
'sla_history_started_at' => $fields['ticket_created_at'],
'sla_history_ended_at' => $stopped_at,
@@ -2739,7 +2739,7 @@ function demoBuildTickets($mysqli, $profile, $index, $client_id, $contact_ids, $
}
// ---- Two years of closed work ----
$per_month = max(2, (int)floor($profile['seats'] / 10));
$per_month = max(2, intval(floor($profile['seats'] / 10)));
$sequence = 0;
for ($month = $months - 1; $month >= 1; $month--) {
@@ -3661,7 +3661,7 @@ function demoBuildCompanyFinancials($mysqli, $context, &$counts) {
continue;
}
// Last year's plan was set a little lower than this year's
$amount = $year < (int)date('Y') ? $budget[1] * 0.94 : $budget[1];
$amount = $year < intval(date('Y')) ? $budget[1] * 0.94 : $budget[1];
for ($month = 1; $month <= 12; $month++) {
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT budget_id FROM budget WHERE budget_category_id = $category_id AND budget_month = $month AND budget_year = $year LIMIT 1"));
if (!empty($row['budget_id'])) {
@@ -3885,7 +3885,7 @@ function demoBuildInternalDiary($mysqli, $context, &$counts) {
foreach ($fixtures as $fixture) {
// Monday a quarter back, then the weekday offset
$days_ago = (13 * 7) - $fixture[3] + (int)date('N') - 1;
$days_ago = (13 * 7) - $fixture[3] + intval(date('N')) - 1;
$start = demoDateTime($days_ago, $fixture[4], 0);
$end = date('Y-m-d H:i:s', strtotime($start . ' +' . $fixture[5] . ' hours'));
@@ -5310,7 +5310,7 @@ function demoExpandProfile($spec) {
}
// Quotes in flight
$refresh_count = max(2, (int)floor($seats / 5));
$refresh_count = max(2, intval(floor($seats / 5)));
$quotes = [
['Workstation refresh - machines past five years old', [
['Business Desktop', $refresh_count, 985.00, $foss ? 'Small form factor desktop supplied with Linux Mint and LibreOffice' : 'Small form factor desktop, 16GB memory, 512GB NVMe', 1],

View File

@@ -274,7 +274,7 @@ if (isset($_POST['test_email_imap'])) {
}
$host = $config_imap_host;
$port = (int) $config_imap_port;
$port = intval($config_imap_port);
$encryption = strtolower(trim($config_imap_encryption)); // e.g. "ssl", "tls", "none"
$username = $config_imap_username;
$password = $config_imap_password;
@@ -386,7 +386,7 @@ if (isset($_POST['test_email_imap'])) {
}
$password = $json['access_token'];
$expires_at = date('Y-m-d H:i:s', time() + (int)($json['expires_in'] ?? 3600));
$expires_at = date('Y-m-d H:i:s', time() + intval($json['expires_in'] ?? 3600));
$refresh_token_to_save = $json['refresh_token'] ?? null;
$token_esc = mysqli_real_escape_string($mysqli, $password);
@@ -501,7 +501,7 @@ if (isset($_POST['test_oauth_token_refresh'])) {
}
$new_access_token = escapeSql($json['access_token']);
$new_expires_at = date('Y-m-d H:i:s', time() + (int)($json['expires_in'] ?? 3600));
$new_expires_at = date('Y-m-d H:i:s', time() + intval($json['expires_in'] ?? 3600));
$new_refresh_token = !empty($json['refresh_token']) ? escapeSql($json['refresh_token']) : '';
$new_access_token_esc = mysqli_real_escape_string($mysqli, $new_access_token);

View File

@@ -409,7 +409,7 @@ if (!$config_module_enable_ticketing) {
<form action="post.php" method="post" autocomplete="off">
<input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
<input type="hidden" name="tab" value="tests">
<input type="hidden" name="oauth_provider" value="<?= htmlspecialchars($oauth_provider_for_test) ?>">
<input type="hidden" name="oauth_provider" value="<?= escapeHtml($oauth_provider_for_test) ?>">
<p class="text-muted mb-2">Validates the refresh token and stores a new access token for <?= $oauth_provider_for_test === 'microsoft_oauth' ? 'Microsoft 365' : 'Google Workspace' ?>.</p>
<button type="submit" name="test_oauth_token_refresh" class="btn btn-success"><i class="fas fa-fw fa-sync-alt me-2"></i>Test OAuth Token Refresh</button>
</form>

View File

@@ -170,7 +170,7 @@ $confirm_text .= " Make sure you have a backup first.";
$install_root = dirname(__DIR__);
$file_owner = '';
if (function_exists('posix_getpwuid')) {
$file_owner = (string) (posix_getpwuid((int) @fileowner($install_root . '/scripts/update_cli.php'))['name'] ?? '');
$file_owner = (string) (posix_getpwuid(intval(@fileowner($install_root . '/scripts/update_cli.php')))['name'] ?? '');
}
$shell_word = function ($word) {
return preg_match('/^[A-Za-z0-9_\/.@%+=:,-]+$/', $word) ? $word : escapeshellarg($word);