$hash) { if (! hash_equals($hash, hash_file('sha256', $root.'/'.$file))) { $correctionPath = $root.'/docs/proofs/approval-deadlines-task-encoding-fix.json'; $correction = is_file($correctionPath) ? json_decode(file_get_contents($correctionPath), true, 32, JSON_THROW_ON_ERROR) : []; if ($file !== 'scripts/install-approval-deadlines-task.php' || ($correction['scope_unchanged'] ?? false) !== true || ($correction['original_installer_sha256'] ?? null) !== $hash || ($correction['original_proposal_sha256'] ?? null) !== hash_file('sha256', $planPath) || ! hash_equals($correction['corrected_installer_sha256'] ?? '', hash_file('sha256', $root.'/'.$file)) || ! hash_equals($correction['xml_transport_sha256'] ?? '', hash_file('sha256', $root.'/app/Services/WindowsTaskXml.php'))) { throw new LogicException('DEADLINE_TASK_SOURCE_CHANGED'); } } } $run = static function (array $command, int $timeout = 15) use ($root): string { $process = new Process($command, $root, timeout: $timeout); $process->run(); if (! $process->isSuccessful()) { throw new RuntimeException('DEADLINE_TASK_NATIVE_COMMAND_FAILED'); } return $process->getOutput(); }; $scheduler = getenv('SystemRoot').'/System32/schtasks.exe'; $tasks = $run([$scheduler, '/Query', '/FO', 'CSV', '/NH']); foreach (preg_split('/\r?\n/', trim($tasks)) as $row) { $name = str_getcsv($row, escape: '')[0] ?? ''; if ($name === '\\'.$plan['task_name']) { throw new LogicException('DEADLINE_TASK_ALREADY_EXISTS_NEVER_OVERWRITE'); } } $health = json_decode(trim($run([PHP_BINARY, 'scripts/approval-deadlines-evaluation.php', '--check'])), true, 32, JSON_THROW_ON_ERROR); if ($health['state'] !== 'ready' || $health['database_enforced_read_only'] !== true) { throw new LogicException('DEADLINE_TASK_PILOT_NOT_READY'); } $identity = str_getcsv(trim($run([getenv('SystemRoot').'/System32/whoami.exe', '/user', '/fo', 'csv', '/nh'])), escape: ''); $sid = $identity[1] ?? ''; if (! preg_match('/^S-1-\d+(?:-\d+)+$/D', $sid)) { throw new LogicException('DEADLINE_TASK_CURRENT_USER_UNAVAILABLE'); } $escape = static fn (string $value) => htmlspecialchars($value, ENT_XML1 | ENT_QUOTES, 'UTF-8'); $start = (new DateTimeImmutable('now', new DateTimeZone('Africa/Johannesburg')))->modify('+1 minute')->format('c'); $php = $escape($plan['php_executable']); $arguments = $escape('"'.$root.'/scripts/approval-deadlines-evaluation.php" --once'); $directory = 'C:/laragon/data/compras/tasks/approval-deadlines-'.gmdate('Ymd-His').'-'.bin2hex(random_bytes(6)); if (! mkdir($directory, 0700, true)) { throw new LogicException('DEADLINE_TASK_PRIVATE_STORAGE'); } $working = $escape($root); $xml = ''. ''. 'Compras pilot: existing configured approval deadlines only.'. ''.$start.'truePT1Mfalse'. 'true'.$sid.''. ''.$sid.'InteractiveTokenLeastPrivilege'. 'IgnoreNewfalsefalsetruetruetruetruePT1M'. ''.$php.''.$arguments.''.$working.''; $document = WindowsTaskXml::parse($xml); $xpath = new DOMXPath($document); $xpath->registerNamespace('t', 'http://schemas.microsoft.com/windows/2004/02/mit/task'); if ($xpath->query('//t:Exec')->length !== 1 || $xpath->evaluate('string(//t:LogonType)') !== 'InteractiveToken' || $xpath->evaluate('string(//t:RunLevel)') !== 'LeastPrivilege' || $xpath->query('//t:Triggers/*')->length !== 2) { throw new LogicException('DEADLINE_TASK_XML_SCOPE'); } $xmlPath = $directory.'/task.private.xml'; $fileBytes = WindowsTaskXml::encode($xml); file_put_contents($xmlPath, $fileBytes); $record = ['date' => gmdate('c'), 'state' => 'preview', 'task_name' => $plan['task_name'], 'current_user_only' => true, 'credentials_stored' => false, 'run_level' => 'Limited', 'poll_interval' => 'PT1M', 'overlap' => 'IgnoreNew', 'time_limit' => 'PT1M', 'private_definition' => $xmlPath, 'xml_sha256' => hash('sha256', $fileBytes), 'pilot_ready' => true, 'eligible_requests' => $health['pending_requests_with_configured_cycle_policy'], 'native_command' => $plan['business_command'], 'task_installed_or_started' => false, 'powershell_execution_policy_changed' => false]; if (! $apply) { file_put_contents($root.'/docs/proofs/approval-deadlines-task-preflight.json', json_encode($record, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR).PHP_EOL); echo json_encode($record, JSON_THROW_ON_ERROR).PHP_EOL; exit; } $approvalPath = $root.'/docs/proofs/approval-deadlines-task-authorization.json'; $approval = is_file($approvalPath) ? json_decode(file_get_contents($approvalPath), true, 32, JSON_THROW_ON_ERROR) : []; if (($approval['state'] ?? null) !== 'approved' || ($approval['scope'] ?? null) !== $plan['scope'] || ! hash_equals($approval['proposal_sha256'], hash_file('sha256', $planPath))) { throw new LogicException('DEADLINE_TASK_NOT_AUTHORIZED'); } $resultPath = $root.'/docs/proofs/approval-deadlines-task-installation.json'; if (is_file($resultPath)) { throw new LogicException('DEADLINE_TASK_PREVIOUS_RESULT'); } $run([$scheduler, '/Create', '/TN', $plan['task_name'], '/XML', $xmlPath]); // No /F: never replace another task. $installed = $run([$scheduler, '/Query', '/TN', $plan['task_name'], '/XML']); $installedDocument = WindowsTaskXml::parse($installed); $installedXpath = new DOMXPath($installedDocument); $installedXpath->registerNamespace('t', 'http://schemas.microsoft.com/windows/2004/02/mit/task'); foreach (['Command', 'Arguments', 'WorkingDirectory', 'LogonType', 'RunLevel', 'MultipleInstancesPolicy', 'ExecutionTimeLimit'] as $element) { if ($installedXpath->evaluate('string(//t:'.$element.')') !== $xpath->evaluate('string(//t:'.$element.')')) { throw new LogicException('DEADLINE_TASK_INSTALLED_DEFINITION_DIFFERS'); } } $record['state'] = 'installed'; $record['task_installed_or_started'] = true; file_put_contents($resultPath, json_encode($record, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR).PHP_EOL); $run([$scheduler, '/Run', '/TN', $plan['task_name']]); $record['initial_pass_requested'] = true; $record['state'] = 'awaiting_passage_verification'; file_put_contents($resultPath, json_encode($record, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR).PHP_EOL); echo json_encode($record, JSON_THROW_ON_ERROR).PHP_EOL;