Files
itflow/admin/post/settings_warranty.php

88 lines
4.2 KiB
PHP

<?php
/*
* Admin > Warranty Lookup (admin/settings_warranty.php). edit_warranty_settings saves the keys: a
* secret typed in is sealed (secretSeal(), 'warranty'), one left empty is kept, and an empty client
* ID forgets its secret. test_warranty_lookup saves the same way, then looks the serial number typed
* beside it up with that manufacturer (functions/warranty.php) and says what came back - HP is
* waited on for up to half a minute.
*/
defined('FROM_POST_HANDLER') || die("Direct file access is not allowed");
if (isset($_POST['edit_warranty_settings']) || isset($_POST['test_warranty_lookup'])) {
validateCSRFToken();
$before = settingsCurrent(['config_warranty_dell_client_id', 'config_warranty_dell_client_secret', 'config_warranty_hp_client_id',
'config_warranty_hp_client_secret', 'config_warranty_lenovo']);
$after = [];
$errors = [];
foreach (['dell', 'hp'] as $maker) {
$id = trim((string) ($_POST["config_warranty_{$maker}_client_id"] ?? ''));
$secret = trim((string) ($_POST["config_warranty_{$maker}_client_secret"] ?? ''));
if (mb_strlen($id) > 200) {
$errors["config_warranty_{$maker}_client_id"] = 'Up to 200 characters.';
}
if (mb_strlen($secret) > 500) {
$errors["config_warranty_{$maker}_client_secret"] = 'Up to 500 characters.';
}
if ($id !== '' && $secret === '' && trim((string) $before["config_warranty_{$maker}_client_secret"]) === '') {
$errors["config_warranty_{$maker}_client_secret"] = 'Its secret is needed too.';
}
// Sealed with this install's key - or as typed on an install without one, which reads either
$sealed = $secret !== '' ? (secretSeal($secret, 'warranty') ?: $secret) : (string) $before["config_warranty_{$maker}_client_secret"];
$after["config_warranty_{$maker}_client_id"] = $id;
$after["config_warranty_{$maker}_client_secret"] = $id === '' ? '' : $sealed;
}
$after['config_warranty_lenovo'] = !empty($_POST['config_warranty_lenovo']) ? 1 : 0;
if ($errors) {
settingsRefuse('warranty', $errors);
}
$changed = settingsSave('warranty', $before, $after);
if (isset($_POST['test_warranty_lookup'])) {
$maker = (string) $_POST['test_warranty_lookup'];
$makers = warrantyMakers();
$serial = strtoupper(trim((string) ($_POST["warranty_test_$maker"] ?? '')));
$saved = $changed ? 'Saved. ' : '';
if (!isset($makers[$maker])) {
redirect();
}
if (!warrantySettings()['ready'][$maker]) {
flashAlert($saved . escapeHtml($maker === 'lenovo' ? 'Lenovo lookups are off - switch them on to test.' : "{$makers[$maker]} isn't set up - put in its key and secret first."), 'error');
redirect();
}
if ($serial === '') {
flashAlert($saved . 'Type a serial number beside <strong>Save and test</strong> to look one up.', 'warning');
redirect();
}
if ($maker === 'hp') {
// HP works through a job - waited on for up to half a minute
$job = warrantyHpStart([$serial]);
$result = warrantyNotFound($job['error'], 'error');
if ($job['job'] !== '') {
$result = warrantyNotFound("HP took the lookup and is still working through it - try again in a minute.", 'skip');
for ($waited = 0; $waited < 30; $waited += 3) {
sleep(3);
$check = warrantyHpCheck($job['job'], [$serial]);
if ($check['done']) {
$result = $check['error'] !== '' ? warrantyNotFound($check['error'], 'error') : ($check['results'][$serial] ?? warrantyNotFound("doesn't know serial number $serial"));
break;
}
}
}
} else {
$result = warrantyLookup(['test' => ['maker' => $maker, 'serial' => $serial]])['test'];
}
$tone = $result['found'] ? 'success' : ($result['kind'] === 'error' ? 'error' : 'warning');
flashAlert($saved . escapeHtml(warrantySays($maker, $result)) . ($result['found'] && $result['product'] !== '' ? ' - ' . escapeHtml($result['product']) : ''), $tone);
}
redirect();
}