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

105 lines
3.3 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";
// Move to the root directory (one level up from public)
$rootPath = realpath(__DIR__ . '/../');
if ($rootPath) {
chdir($rootPath);
}
echo "Working 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";
}
// 1. Check current status
execute("git status 2>&1");
// 2. Pull latest changes
execute("git pull origin main 2>&1");
// 3. 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";