- Remove hardcoded sudo username and password from gitpull.php - Switch to using `sudo git pull` with NOPASSWD sudoers configuration - Add helpful error message with setup instructions when git pull fails - Improve security by eliminating password storage in source code
61 lines
2.5 KiB
PHP
61 lines
2.5 KiB
PHP
<?php
|
||
require_once('./include/db_info.inc.php');
|
||
require_once('./include/const.inc.php');
|
||
require_once('./include/memcache.php');
|
||
require_once('./include/setlang.php');
|
||
|
||
// 仅允许管理员访问
|
||
if (!isset($_SESSION[$OJ_NAME.'_'.'administrator'])) {
|
||
$view_errors = "Permission denied.";
|
||
require("template/".$OJ_TEMPLATE."/error.php");
|
||
exit(0);
|
||
}
|
||
|
||
// ====================================================
|
||
// 前置配置(在服务器上执行一次):
|
||
// echo "www-data ALL=(ALL) NOPASSWD: /usr/bin/git" | sudo tee /etc/sudoers.d/www-data-git
|
||
// sudo chmod 440 /etc/sudoers.d/www-data-git
|
||
// ====================================================
|
||
|
||
$src_path = '/home/judge/src';
|
||
$output = [];
|
||
$return_code = -1;
|
||
|
||
// 打开页面或点击重试按钮时均以 root 身份执行 git pull
|
||
$cmd = "sudo git -C " . escapeshellarg($src_path) . " pull 2>&1";
|
||
exec($cmd, $output, $return_code);
|
||
|
||
$show_title = "Git Pull - " . $OJ_NAME;
|
||
require("template/".$OJ_TEMPLATE."/header.php");
|
||
?>
|
||
|
||
<div class="ui segment" style="margin-top:20px;">
|
||
<h2 class="ui header">
|
||
<i class="cloud download icon"></i>
|
||
<div class="content">服务器更新 <small style="font-size:0.6em; color:#888;">(git pull <?php echo htmlspecialchars($src_path); ?>)</small></div>
|
||
</h2>
|
||
|
||
<div class="ui <?php echo $return_code === 0 ? 'success' : 'error'; ?> message">
|
||
<div class="header"><?php echo $return_code === 0 ? '更新成功' : '更新失败(返回码:' . $return_code . ')'; ?></div>
|
||
<pre style="margin-top:8px; white-space:pre-wrap; word-break:break-all;"><?php echo htmlspecialchars(implode("\n", $output)); ?></pre>
|
||
<?php if ($return_code !== 0): ?>
|
||
<div style="margin-top:10px; padding:10px; background:#f8f8f8; border-radius:4px; font-size:0.9em;">
|
||
<b>若提示 sudo 权限不足,请在服务器上执行以下命令(仅需一次):</b><br>
|
||
<code>echo "www-data ALL=(ALL) NOPASSWD: /usr/bin/git" | sudo tee /etc/sudoers.d/www-data-git</code><br>
|
||
<code>sudo chmod 440 /etc/sudoers.d/www-data-git</code>
|
||
</div>
|
||
<?php endif; ?>
|
||
</div>
|
||
|
||
<div style="margin-top:16px;">
|
||
<?php if ($return_code !== 0): ?>
|
||
<a class="ui primary button" href="gitpull.php">
|
||
<i class="refresh icon"></i> 重试 git pull
|
||
</a>
|
||
<?php endif; ?>
|
||
<a class="ui button" href="javascript:history.back()">返回</a>
|
||
</div>
|
||
</div>
|
||
|
||
<?php require("template/".$OJ_TEMPLATE."/footer.php"); ?>
|