admin-portal/tech/tabs/01-deploy/02_supportFuncs.php
Frederico Falcao f2a6525224 init
2025-05-30 10:46:17 +01:00

58 lines
1.9 KiB
PHP

<?php
// FILE: 02_supportFuncs.php
// Description: Support and utility functions
function listGitCommits($repoDir, $limit = 20) {
global $gitDir;
$cmd = "GIT_DIR=$gitDir git log --pretty=format:\"%H|%ad|%an|%s\" --date=short -n " . intval($limit);
exec($cmd, $output);
$commits = [];
foreach ($output as $line) {
list($hash, $date, $author, $subject) = explode('|', $line, 4);
$commits[] = compact('hash', 'date', 'author', 'subject');
}
return $commits;
}
function getLastDeployData($repoDir) {
$jsonFile = $repoDir . '/deploy_last.json';
if (file_exists($jsonFile)) {
$data = json_decode(file_get_contents($jsonFile), true);
if (is_array($data)) return $data;
}
return ['username'=>'', 'repo'=>'', 'hash'=>''];
}
function setLastDeployData($repoDir, $data) {
$jsonFile = $repoDir . '/deploy_last.json';
file_put_contents($jsonFile, json_encode($data));
}
function runShellSteps($steps, $cwd = null) {
// Runs an array of ['desc'=>..., 'cmd'=>...] steps, prints output and returns overall success
$allOk = true;
echo "<pre class='bg-light p-3 border'><strong>Shell Output:</strong>\n";
foreach ($steps as $step) {
$desc = $step['desc'] ?? '';
$cmd = $step['cmd'];
echo "\n# $desc\n$ $cmd\n";
passthru(($cwd ? "cd ".escapeshellarg($cwd)." && " : "") . "$cmd 2>&1", $ret);
if ($ret !== 0 && stripos($desc, 'commit') === false) {
echo "\n❌ Error in step: $desc\n";
$allOk = false;
break;
}
}
echo "</pre>";
return $allOk;
}
function sanitizeRepoInput($input, $type = 'alphanum') {
// Usage: sanitizeRepoInput($_POST['repo']), etc
if ($type === 'hash') return preg_replace('/[^a-fA-F0-9]/', '', $input);
if ($type === 'repo') return preg_replace('/[^a-zA-Z0-9_-]/', '', $input);
return trim($input);
}
?>