"/ExtraSpace/www.growfit.fi",
"beta" => "/ExtraSpace/beta.growfit.fi",
"alpha" => "/ExtraSpace/alpha.growfit.fi",
];
if (!isset($folders[$target])) {
echo "
â INTERNAL ERROR: Deployment folder doesn't exist.
";
return;
}
$gitWorkTree = $folders[$target];
if ($commit) {
echo "đ Deploying commit: $commit to $target
";
$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 "â
Commit $commit deployed to $target.
";
} else {
echo "â One or more commands failed.
";
}
}
}
// 3. GET COMMIT INFO
if (isset($_GET['get_commit_info'])) {
$hash = sanitizeRepoInput($_GET['get_commit_info'], 'hash');
if (!$hash) {
echo 'Invalid commit hash.
'; 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 'Error fetching commit info.
'; exit;
}
echo ''.htmlspecialchars(implode("\n", $output)).''; 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 "đĨ Downloading ZIP from: $zipUrl
";
$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 "â
ZIP merged and committed as: $commitMsg
";
} else {
echo "â One or more commands failed.
";
}
} else {
echo "â Invalid input for username/repo/hash.
";
}
}
// 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 "đ¤ Uploading & extracting: $zipName
";
$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 "â
ZIP merged and committed: " . htmlspecialchars($commitMsg) . "
";
} else {
echo "âšī¸ No changes to commit, or an error occurred.
";
}
}
?>