["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"); // 4. Re-optimize Laravel cache and run migrations $phpBin = PHP_BINARY; execute("$phpBin artisan optimize:clear 2>&1"); execute("$phpBin artisan migrate --force 2>&1"); if (function_exists('opcache_reset')) { opcache_reset(); echo "PHP OPCache successfully reset.\n"; } echo "Deployment workflow finished.\n";