75 lines
5.1 KiB
PHP
75 lines
5.1 KiB
PHP
<?php
|
|
|
|
use App\Services\DeadlineTaskGuiAction;
|
|
use App\Services\WindowsTaskXml;
|
|
use Symfony\Component\Process\Process;
|
|
|
|
require dirname(__DIR__).'/vendor/autoload.php';
|
|
$root = dirname(__DIR__);
|
|
if (count($argv) !== 2 || ! in_array($argv[1], ['--preview', '--apply-authorized'], true)) {
|
|
throw new LogicException('Explicit silent task preview or application required.');
|
|
}
|
|
$planPath = $root.'/docs/proofs/approval-deadlines-task-proposal.json';
|
|
$plan = json_decode(file_get_contents($planPath), true, 32, JSON_THROW_ON_ERROR);
|
|
$approval = json_decode(file_get_contents($root.'/docs/proofs/approval-deadlines-task-authorization.json'), true, 32, JSON_THROW_ON_ERROR);
|
|
$current = json_decode(file_get_contents($root.'/docs/proofs/inventory-conversion-pilot-authorization.json'), true, 32, JSON_THROW_ON_ERROR);
|
|
if ($approval['state'] !== 'approved' || ! hash_equals($approval['proposal_sha256'], hash_file('sha256', $planPath))
|
|
|| $current['state'] !== 'approved' || ! hash_equals($current['proposal_sha256'], hash_file('sha256', $root.'/'.$current['proposal']))
|
|
|| ! hash_equals($plan['file_sha256'][$plan['action_script']], hash_file('sha256', $root.'/'.$plan['action_script']))) {
|
|
throw new LogicException('Existing task and necessary activity components authorization required.');
|
|
}
|
|
$resultPath = $root.'/docs/proofs/approval-deadlines-task-gui-repair.json';
|
|
if (is_file($resultPath)) {
|
|
throw new LogicException('Preserve verified task correction; do not repeat.');
|
|
}
|
|
$old = 'C:/Users/macbook/.config/investmoz/php-8.5.11/php.exe';
|
|
$new = 'C:/Users/macbook/.config/investmoz/php-8.5.11/php-win.exe';
|
|
if (! is_file($new) || $plan['php_executable'] !== $old || $plan['task_name'] !== 'Compras-Pilot-Approval-Deadlines') {
|
|
throw new LogicException('Reviewed same-installation GUI binary required.');
|
|
}
|
|
$run = static function (array $args) use ($root): string {
|
|
$process = new Process($args, $root, timeout: 30);
|
|
$process->mustRun();
|
|
|
|
return $process->getOutput();
|
|
};
|
|
$scheduler = getenv('SystemRoot').'/System32/schtasks.exe';
|
|
$before = $run([$scheduler, '/Query', '/TN', $plan['task_name'], '/XML']);
|
|
$installation = json_decode(file_get_contents($root.'/docs/proofs/approval-deadlines-task-installation.json'), true, 32, JSON_THROW_ON_ERROR);
|
|
$original = file_get_contents($installation['private_definition']);
|
|
if (! hash_equals($installation['private_installed_xml_sha256'], hash('sha256', $original))
|
|
|| DeadlineTaskGuiAction::canonical($original) !== DeadlineTaskGuiAction::canonical($before)) {
|
|
throw new LogicException('Installed task differs from its original verified definition.');
|
|
}
|
|
DeadlineTaskGuiAction::replacement($before, $old, $new);
|
|
if ($argv[1] === '--preview') {
|
|
echo json_encode(['state' => 'preview', 'same_task_script_and_policy' => true, 'task_changed' => false], JSON_THROW_ON_ERROR).PHP_EOL;
|
|
exit;
|
|
}
|
|
$directory = 'C:/laragon/data/compras/tasks/deadline-gui-'.gmdate('Ymd-His').'-'.bin2hex(random_bytes(6));
|
|
if (! mkdir($directory, 0700, true) || file_put_contents($directory.'/before.private.xml', WindowsTaskXml::encode($before)) === false) {
|
|
throw new LogicException('Private original task backup required.');
|
|
}
|
|
// Native cmdlet changes the action only; no script file or execution-policy override.
|
|
$command = <<<'POWERSHELL'
|
|
$ErrorActionPreference='Stop'
|
|
$task=Get-ScheduledTask -TaskName 'Compras-Pilot-Approval-Deadlines' -TaskPath '\'
|
|
if($task.Actions.Count -ne 1 -or $task.Actions[0].Execute -ne 'C:/Users/macbook/.config/investmoz/php-8.5.11/php.exe'){throw 'Task action changed'}
|
|
$action=New-ScheduledTaskAction -Execute 'C:/Users/macbook/.config/investmoz/php-8.5.11/php-win.exe' -Argument $task.Actions[0].Arguments -WorkingDirectory $task.Actions[0].WorkingDirectory
|
|
Set-ScheduledTask -TaskName 'Compras-Pilot-Approval-Deadlines' -TaskPath '\' -Action $action | Out-Null
|
|
POWERSHELL;
|
|
$run([getenv('SystemRoot').'/System32/WindowsPowerShell/v1.0/powershell.exe', '-NoProfile', '-NonInteractive', '-Command', $command]);
|
|
$after = $run([$scheduler, '/Query', '/TN', $plan['task_name'], '/XML']);
|
|
DeadlineTaskGuiAction::assertConserved($before, $after, $old, $new);
|
|
file_put_contents($directory.'/after.private.xml', WindowsTaskXml::encode($after));
|
|
$run([$scheduler, '/Run', '/TN', $plan['task_name']]);
|
|
$result = ['date' => gmdate('c'), 'state' => 'definition_verified_awaiting_passage', 'task_name' => $plan['task_name'],
|
|
'authority' => ['approval-deadlines-task-authorization.json', 'inventory-conversion-pilot-authorization.json'],
|
|
'private_backup' => $directory, 'old_executable' => $old, 'new_executable' => $new, 'gui_binary_sha256' => hash_file('sha256', $new),
|
|
'only_executable_changed' => true, 'helper_sha256' => hash_file('sha256', $root.'/'.$plan['action_script']),
|
|
'arguments_working_directory_triggers_principal_privileges_overlap_and_limit_preserved' => true,
|
|
'powershell_execution_policy_changed' => false, 'business_policy_or_grants_changed' => false,
|
|
'reference' => 'https://www.php.net/manual/en/features.commandline.php'];
|
|
file_put_contents($resultPath, json_encode($result, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR).PHP_EOL);
|
|
echo json_encode($result, JSON_THROW_ON_ERROR).PHP_EOL;
|