127 lines
5.8 KiB
PHP
127 lines
5.8 KiB
PHP
<?php
|
||
/*
|
||
* FILE: 03_handler.php
|
||
* Description: Handles the input provided from the browser/user
|
||
*/
|
||
|
||
|
||
|
||
// 1. Handle deploy of specific commit
|
||
if (isset($_POST['deploy_commit'])) {
|
||
global $gitDir;
|
||
$commit = sanitizeRepoInput($_POST['deploy_commit'], 'hash');
|
||
$target = $_POST['deploy_target'] ?? 'default';
|
||
$folders = [
|
||
"www" => "/ExtraSpace/www.growfit.fi",
|
||
"beta" => "/ExtraSpace/beta.growfit.fi",
|
||
"alpha" => "/ExtraSpace/alpha.growfit.fi",
|
||
];
|
||
if (!isset($folders[$target])) {
|
||
echo "<div class='alert alert-danger'>❌ INTERNAL ERROR: Deployment folder doesn't exist.</div>";
|
||
return;
|
||
}
|
||
$gitWorkTree = $folders[$target];
|
||
|
||
if ($commit) {
|
||
echo "<div class='alert alert-info'>🚀 Deploying commit: <strong>$commit</strong> to <strong>$target</strong></div>";
|
||
$steps = [
|
||
['desc'=>"Clear the target folder", 'cmd'=>"rm -rf $gitWorkTree; mkdir $gitWorkTree"],
|
||
['desc'=>"Checkout commit", 'cmd'=>"GIT_DIR=$gitDir git archive $commit | tar -x -C $gitWorkTree"],
|
||
['desc'=>"Install NPM packages", 'cmd'=>"cd $gitWorkTree && npm install"],
|
||
['desc'=>"Set Astro prerender", 'cmd'=>"cd $gitWorkTree && find src/ -type f -name \"*.astro\" -exec sed -i 's/prerender = false/prerender = true/g' {} +"],
|
||
['desc'=>"Build site", 'cmd'=>"cd $gitWorkTree && npm run build"]
|
||
];
|
||
$allOk = runShellSteps($steps);
|
||
|
||
if ($allOk) {
|
||
echo "<div class='alert alert-success'>✅ Commit $commit deployed to $target.</div>";
|
||
} else {
|
||
echo "<div class='alert alert-danger'>❌ One or more commands failed.</div>";
|
||
}
|
||
}
|
||
}
|
||
|
||
// 3. GET COMMIT INFO
|
||
if (isset($_GET['get_commit_info'])) {
|
||
$hash = sanitizeRepoInput($_GET['get_commit_info'], 'hash');
|
||
if (!$hash) {
|
||
echo '<div class="alert alert-danger">Invalid commit hash.</div>'; exit;
|
||
}
|
||
$cmd = gitCmd($repoDir, 'show --stat --pretty=format:"Commit: %H%nAuthor: %an <%ae>%nDate: %ad%n%n%s%n%b" --date=iso ' . escapeshellarg($hash) . ' -m');
|
||
exec($cmd . ' 2>&1', $output, $retval);
|
||
if ($retval !== 0) {
|
||
echo '<div class="alert alert-danger">Error fetching commit info.</div>'; exit;
|
||
}
|
||
echo '<pre style="white-space: pre-wrap;">'.htmlspecialchars(implode("\n", $output)).'</pre>'; exit;
|
||
}
|
||
|
||
// 4. PULL MERGE ZIP
|
||
if (isset($_POST['pull_merge_zip'])) {
|
||
$username = sanitizeRepoInput($_POST['username'], 'repo');
|
||
$repo = sanitizeRepoInput($_POST['repo'], 'repo');
|
||
$hash = sanitizeRepoInput($_POST['hash'], 'hash');
|
||
if ($username && $repo && $hash) {
|
||
setLastDeployData($repoDir, ['username'=>$username,'repo'=>$repo,'hash'=>$hash]);
|
||
$zipUrl = "https://github.com/$username/$repo/archive/$hash.zip";
|
||
$tmpZip = "/tmp/{$repo}_$hash.zip";
|
||
$tmpExtract = "/tmp/{$repo}_$hash";
|
||
$extractedSubdir = "$tmpExtract/{$repo}-$hash";
|
||
$commitMsg = "Pulled ZIP from $username/$repo@$hash";
|
||
|
||
echo "<div class='alert alert-info'>📥 Downloading ZIP from: <code>$zipUrl</code></div>";
|
||
|
||
$steps = [
|
||
['desc'=>"Download ZIP", 'cmd'=>"wget -q -O ".escapeshellarg($tmpZip)." ".escapeshellarg($zipUrl)],
|
||
['desc'=>"Clean old extraction", 'cmd'=>"rm -rf ".escapeshellarg($tmpExtract)],
|
||
['desc'=>"Extract ZIP", 'cmd'=>"unzip -q ".escapeshellarg($tmpZip)." -d ".escapeshellarg($tmpExtract)],
|
||
['desc'=>"Rsync to repo", 'cmd'=>"rsync -a --delete --exclude='.git' $extractedSubdir/ $repoDir/"],
|
||
['desc'=>"Git add", 'cmd'=>gitCmd($repoDir, "add -A")],
|
||
['desc'=>"Git commit", 'cmd'=>gitCmd($repoDir, "commit -m " . escapeshellarg($commitMsg))]
|
||
];
|
||
|
||
$allOk = runShellSteps($steps);
|
||
|
||
if ($allOk) {
|
||
echo "<div class='alert alert-success'>✅ ZIP merged and committed as: <code>$commitMsg</code></div>";
|
||
} else {
|
||
echo "<div class='alert alert-danger'>❌ One or more commands failed.</div>";
|
||
}
|
||
} else {
|
||
echo "<div class='alert alert-danger'>❌ Invalid input for username/repo/hash.</div>";
|
||
}
|
||
}
|
||
|
||
// 5. Handle direct ZIP upload & merge
|
||
if (isset($_POST['upload_zip_btn']) && isset($_FILES['upload_zip']) && $_FILES['upload_zip']['error'] === UPLOAD_ERR_OK) {
|
||
$uploadedFile = $_FILES['upload_zip'];
|
||
$commitMsg = trim($_POST['commit_msg'] ?? '');
|
||
if (!$commitMsg) $commitMsg = "Uploaded ZIP: " . $uploadedFile['name'] . " on " . date('Y-m-d H:i');
|
||
$tmpZip = $uploadedFile['tmp_name'];
|
||
$zipName = basename($uploadedFile['name']);
|
||
$tmpExtract = "/tmp/upload_zip_" . uniqid();
|
||
mkdir($tmpExtract, 0777, true);
|
||
|
||
echo "<div class='alert alert-info'>📤 Uploading & extracting: <code>$zipName</code></div>";
|
||
|
||
$extractDir = $tmpExtract . ((strpos($uploadedFile['name'], "project-bolt-") === 0) ? "/project" : "");
|
||
|
||
|
||
|
||
$steps = [
|
||
['desc'=>"Extract ZIP", 'cmd'=>"unzip -q " . escapeshellarg($tmpZip) . " -d " . escapeshellarg($tmpExtract)],
|
||
['desc'=>"Git add", 'cmd'=>"GIT_DIR=$gitDir GIT_WORK_TREE=".escapeshellarg($extractDir)." git add -A"],
|
||
['desc'=>"Git commit", 'cmd'=>"GIT_DIR=$gitDir GIT_WORK_TREE=".escapeshellarg($extractDir)." git commit -m ".escapeshellarg($commitMsg)],
|
||
['desc'=>"Pushing to GITHUB", 'cmd'=>"GIT_DIR=$gitDir GIT_WORK_TREE=".escapeshellarg($extractDir)." git push"],
|
||
['desc'=>"Cleaning Up", 'cmd'=>"rm -rf " . escapeshellarg($tmpExtract)],
|
||
];
|
||
|
||
$allOk = runShellSteps($steps);
|
||
|
||
if ($allOk) {
|
||
echo "<div class='alert alert-success'>✅ ZIP merged and committed: <code>" . htmlspecialchars($commitMsg) . "</code></div>";
|
||
} else {
|
||
echo "<div class='alert alert-warning'>ℹ️ No changes to commit, or an error occurred.</div>";
|
||
}
|
||
}
|
||
?>
|