Files
HRM-System/public/git-pull.php

152 lines
5.1 KiB
PHP

<?php
/**
* Standalone Git Pull Deployment Script for Shared Hosting
* Location: public/git-pull.php
*/
// Enable full error reporting for debugging
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
define('SECURE_TOKEN', '7f3b89e902a2cdb8b5de9f2e30f14d8a245fca9b');
// Verify token
if (!isset($_GET['token']) || $_GET['token'] !== SECURE_TOKEN) {
header('HTTP/1.0 403 Forbidden');
echo "Access Denied: Invalid or missing security token.";
exit;
}
header('Content-Type: text/plain; charset=utf-8');
echo "=========================================\n";
echo "Starting Git Pull Deployment Workflow\n";
echo "Current Time: " . date('Y-m-d H:i:s') . "\n";
echo "=========================================\n\n";
// Print system diagnostics
echo "System Diagnostics:\n";
echo "PHP Version: " . PHP_VERSION . "\n";
echo "Disabled Functions: " . (ini_get('disable_functions') ?: '(none)') . "\n";
echo "shell_exec status: " . (function_exists('shell_exec') ? 'Enabled' : 'Disabled') . "\n";
echo "exec status: " . (function_exists('exec') ? 'Enabled' : 'Disabled') . "\n";
echo "system status: " . (function_exists('system') ? 'Enabled' : 'Disabled') . "\n";
echo "proc_open status: " . (function_exists('proc_open') ? 'Enabled' : 'Disabled') . "\n";
echo "-----------------------------------------\n\n";
// Robust Git Root Directory Auto-Detection
$targetDir = __DIR__;
for ($i = 0; $i < 4; $i++) {
if (is_dir($targetDir . '/.git')) {
break;
}
$parent = realpath($targetDir . '/../');
if (!$parent || $parent === $targetDir) {
break;
}
$targetDir = $parent;
}
if (is_dir($targetDir . '/.git')) {
chdir($targetDir);
echo "Git Repository Root Detected: " . getcwd() . "\n\n";
} else {
chdir(__DIR__);
echo "Warning: .git directory not found in path hierarchy. Falling back to script directory: " . getcwd() . "\n\n";
}
// Helper function to execute commands with proc_open or standard fallbacks
function execute($cmd) {
echo "$ {$cmd}\n";
$output = '';
if (function_exists('proc_open')) {
$descriptorspec = [
0 => ["pipe", "r"], // stdin
1 => ["pipe", "w"], // stdout
2 => ["pipe", "w"] // stderr
];
$process = proc_open($cmd, $descriptorspec, $pipes);
if (is_resource($process)) {
fclose($pipes[0]);
$stdout = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[2]);
proc_close($process);
$output = $stdout . $stderr;
} else {
$output = "Error: Failed to open process using proc_open.";
}
} elseif (function_exists('shell_exec')) {
$output = shell_exec($cmd);
} elseif (function_exists('exec')) {
$outputLines = [];
exec($cmd, $outputLines);
$output = implode("\n", $outputLines);
} elseif (function_exists('system')) {
ob_start();
system($cmd);
$output = ob_get_clean();
} else {
echo "Error: No command execution functions (proc_open, shell_exec, exec, system) are enabled on this hosting server.\n";
echo "-----------------------------------------\n";
return;
}
echo $output ? trim($output) : "(no output)";
echo "\n-----------------------------------------\n";
}
// Check if user is requesting to update the remote origin URL (to embed credentials)
if (isset($_GET['remote'])) {
$newRemote = $_GET['remote'];
// Simple verification to prevent command injections
if (filter_var($newRemote, FILTER_VALIDATE_URL) && (strpos($newRemote, 'https://') === 0 || strpos($newRemote, 'http://') === 0)) {
echo "Updating git origin remote URL...\n";
execute("git remote set-url origin " . escapeshellarg($newRemote) . " 2>&1");
} else {
echo "Error: Invalid remote URL format. Must start with http:// or https://\n";
echo "-----------------------------------------\n";
}
}
// 1. Check current status
execute("git status 2>&1");
// Delete untracked local files that might cause merge conflicts
if (file_exists('git-pull.php')) {
echo "Cleaning up local untracked git-pull.php root file to prevent conflict...\n";
@unlink('git-pull.php');
echo "-----------------------------------------\n";
}
if (file_exists('public/optimize.php')) {
echo "Cleaning up local untracked public/optimize.php file to prevent conflict...\n";
@unlink('public/optimize.php');
echo "-----------------------------------------\n";
}
// Discard any local modifications on the server to prevent pull aborts
execute("git reset --hard HEAD 2>&1");
execute("git clean -fd 2>&1"); // Force clean untracked files and directories
// 2. Pull latest changes
execute("git pull origin main 2>&1");
// 3. Build frontend assets
execute("npm run build 2>&1");
// 4. Re-optimize Laravel cache and run migrations
execute("php artisan optimize:clear 2>&1");
execute("php artisan migrate --force 2>&1");
echo "Deployment workflow finished.\n";