15m44w 续2: git 推图改 shell_exec(服务端禁 proc_open 但 exec/shell_exec 可用)
- 用户解封 exec/shell_exec 后, ghImgRepoPushViaGit 原用 proc_open(仍被禁)会失败 - runGit 改 shell_exec: PAT 拼认证 URL, 命令 2>&1 合流+末尾 echo __EXIT__$? 判退出码 - 修闭包内层变量 $full 与外层仓库全名 $full 命名冲突(改 $fullCmd) - ghGitAvailable(exec git --version) 现 Web PHP exec 已可用→git 分支生效, 不再只靠 Contents API 兜底
This commit is contained in:
+33
-30
@@ -517,33 +517,36 @@ function ghImgRepoPush($player, $ticketId, $title, $tmpPath, $origName) {
|
||||
return ['ok' => false, 'url' => '', 'path' => '', 'msg' => $r2['msg']];
|
||||
}
|
||||
|
||||
/** git 方式推图(★ 15m44w:账号/key 走 img.json 的 PAT,拼进认证 URL,不依赖服务端全局 git 缓存) */
|
||||
/** git 方式推图(★ 15m44w:账号/key 走 img.json 的 PAT,拼进认证 URL,不依赖服务端全局 git 缓存)
|
||||
* ★ 15m44w 续:服务端 PHP disable_functions 禁了 proc_open/popen,但 exec/shell_exec 可用
|
||||
* → runGit 从 proc_open 改 shell_exec(PAT 直接拼认证 URL,不靠 env 注入 GIT_CONFIG)。
|
||||
* shell_exec 用 2>&1 合流 stderr,末尾 echo 退出码判成功。 */
|
||||
function ghImgRepoPushViaGit($full, $owner, $branch, $relPath, $tmpPath, $token, $commitMsg) {
|
||||
$workRoot = __DIR__ . '/db/gh_imgrepo_work';
|
||||
if (!is_dir($workRoot)) @mkdir($workRoot, 0755, true);
|
||||
$repoDir = $workRoot . '/' . preg_replace('/[^A-Za-z0-9_.-]/', '_', $full);
|
||||
$gitBin = 'git';
|
||||
// 跑 git 命令(Windows/Linux 通用,env 注入 GIT_TERMINAL_PROMPT=0 禁交互 + 走全局 credential)
|
||||
$runGit = function($args, $cwd, $timeout = 60) use ($gitBin) {
|
||||
$cmd = $gitBin . ' ' . implode(' ', array_map(function($a) {
|
||||
if (preg_match('/[^\x20-\x7E]/', $a) || strpos($a, ' ') !== false) return '"' . str_replace('"', '\\"', $a) . '"';
|
||||
return $a;
|
||||
}, $args));
|
||||
$env = array_merge($_SERVER, ['GIT_TERMINAL_PROMPT' => '0']);
|
||||
if ($token !== '') {
|
||||
// ★ 用 img.json 的 PAT 做认证(禁交互 + 不污染全局 credential)
|
||||
$env['GIT_CONFIG_COUNT'] = '1';
|
||||
$env['GIT_CONFIG_KEY_0'] = 'http.https://github.com/.extraheader';
|
||||
$env['GIT_CONFIG_VALUE_0'] = 'Authorization: Basic ' . base64_encode($owner . ':' . $token);
|
||||
// ★ shell_exec 跑 git:PAT 拼认证 URL,命令 2>&1 合流 + 末尾 echo 退出码判成功
|
||||
// 参数转义:非 [A-Za-z0-9_. /=:@-] 字符或含空格/中文 → 单引号包(shell 内 ' 转义为 '\')
|
||||
$q = function($a) {
|
||||
if ($a === '' || preg_match('/[^\x20-\x7E]/', $a) || strpos($a, ' ') !== false) {
|
||||
return "'" . str_replace("'", "'\\''", $a) . "'";
|
||||
}
|
||||
$desc = [0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w']];
|
||||
$proc = proc_open($cmd, $desc, $pipes, $cwd, $env);
|
||||
if (!is_resource($proc)) return ['ok' => false, 'msg' => 'proc_open 失败'];
|
||||
fclose($pipes[0]);
|
||||
$out = stream_get_contents($pipes[1]); fclose($pipes[1]);
|
||||
$err = stream_get_contents($pipes[2]); fclose($pipes[2]);
|
||||
$st = proc_close($proc);
|
||||
return ['ok' => ($st === 0), 'msg' => trim((string)$err . (string)$out)];
|
||||
return $a;
|
||||
};
|
||||
$runGit = function($args, $cwd, $q, $timeout = 60) {
|
||||
$cmd = implode(' ', array_map($q, $args));
|
||||
// 2>&1 合流 stderr;GIT_TERMINAL_PROMPT=0 禁交互(避免卡死);echo 退出码
|
||||
$fullCmd = 'cd ' . $q($cwd) . ' && GIT_TERMINAL_PROMPT=0 ' . $cmd . ' 2>&1; echo __EXIT__$?';
|
||||
$out = @shell_exec($fullCmd);
|
||||
$out = (string)$out;
|
||||
if (substr_count($out, '__EXIT__') === 0) {
|
||||
return ['ok' => false, 'msg' => 'shell_exec 不可用/未返回(可能仍被禁): ' . mb_substr($out, 0, 120)];
|
||||
}
|
||||
$exitStr = substr($out, strrpos($out, '__EXIT__'));
|
||||
preg_match('/__EXIT__(\d+)/', $exitStr, $mm);
|
||||
$code = isset($mm[1]) ? (int)$mm[1] : 1;
|
||||
$msg = trim(str_replace('__EXIT__' . $mm[1], '', $out));
|
||||
return ['ok' => ($code === 0), 'msg' => $msg];
|
||||
};
|
||||
|
||||
$cloneUrl = ($token !== '')
|
||||
@@ -552,23 +555,23 @@ function ghImgRepoPushViaGit($full, $owner, $branch, $relPath, $tmpPath, $token,
|
||||
try {
|
||||
// 1) 本地仓(首次 clone,已有则 fetch)
|
||||
if (is_dir($repoDir . '/.git')) {
|
||||
$runGit(['fetch', '--all'], $repoDir, 60);
|
||||
$runGit(['checkout', '-B', $branch, 'origin/' . $branch], $repoDir, 30);
|
||||
$runGit(['fetch', '--all'], $repoDir, 60, $q);
|
||||
$runGit(['checkout', '-B', $branch, 'origin/' . $branch], $repoDir, 30, $q);
|
||||
} else {
|
||||
@mkdir(dirname($repoDir), 0755, true);
|
||||
$r = $runGit(['clone', '--depth', '1', '--branch', $branch, '--single-branch', $cloneUrl, $repoDir], $workRoot, 120);
|
||||
$r = $runGit(['clone', '--depth', '1', '--branch', $branch, '--single-branch', $cloneUrl, $repoDir], $workRoot, $q, 120);
|
||||
if (!$r['ok']) return ['ok' => false, 'msg' => 'clone 失败: ' . mb_substr($r['msg'], 0, 160)];
|
||||
$runGit(['config', 'user.name', 'caoyuan-ticket-bot'], $repoDir, 10);
|
||||
$runGit(['config', 'user.email', 'bot@caoyuan.local'], $repoDir, 10);
|
||||
$runGit(['config', 'user.name', 'caoyuan-ticket-bot'], $repoDir, 10, $q);
|
||||
$runGit(['config', 'user.email', 'bot@caoyuan.local'], $repoDir, 10, $q);
|
||||
}
|
||||
// 2) 写文件到仓内
|
||||
$target = $repoDir . '/' . $relPath;
|
||||
@mkdir(dirname($target), 0755, true);
|
||||
if (!@copy($tmpPath, $target)) return ['ok' => false, 'msg' => '写仓内文件失败'];
|
||||
// 3) add + commit + push(认证走 img.json PAT:clone URL 已带 owner:token,push 复用 origin)
|
||||
$runGit(['add', '--', $relPath], $repoDir, 30);
|
||||
$runGit(['commit', '-m', $commitMsg, '--allow-empty'], $repoDir, 30);
|
||||
$rp = $runGit(['push', 'origin', 'HEAD:refs/heads/' . $branch], $repoDir, 90);
|
||||
$runGit(['add', '--', $relPath], $repoDir, 30, $q);
|
||||
$commit = $runGit(['commit', '-m', $commitMsg, '--allow-empty'], $repoDir, 30, $q);
|
||||
$rp = $runGit(['push', 'origin', 'HEAD:refs/heads/' . $branch], $repoDir, 90, $q);
|
||||
if (!$rp['ok']) {
|
||||
@unlink($target);
|
||||
return ['ok' => false, 'msg' => 'push 失败: ' . mb_substr($rp['msg'], 0, 200)];
|
||||
|
||||
Reference in New Issue
Block a user