feat: add admin-only git pull interface and remove auto-pull
- Add new `gitpull.php` page for manual git pull execution by administrators - Remove automatic git pull on homepage visit to prevent unintended updates - Include git pull link in admin navigation menu for easy access - Secure interface with admin-only access and sudo credentials configuration
This commit is contained in:
57
web/gitpull.php
Normal file
57
web/gitpull.php
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
<?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);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ====================================================
|
||||||
|
// 请在下方填写 sudo 用户名和密码
|
||||||
|
$SUDO_USER = 'jieer'; // 填写拥有 sudo 权限的用户名
|
||||||
|
$SUDO_PASSWORD = 'cmf[420;'; // 填写该用户的 sudo 密码
|
||||||
|
// ====================================================
|
||||||
|
|
||||||
|
$src_path = '/home/judge/src';
|
||||||
|
$output = [];
|
||||||
|
$return_code = -1;
|
||||||
|
|
||||||
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||||
|
$cmd = "echo " . escapeshellarg($SUDO_PASSWORD)
|
||||||
|
. " | sudo -S -u " . escapeshellarg($SUDO_USER)
|
||||||
|
. " 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>
|
||||||
|
|
||||||
|
<?php if ($_SERVER['REQUEST_METHOD'] === 'POST'): ?>
|
||||||
|
<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>
|
||||||
|
</div>
|
||||||
|
<?php endif; ?>
|
||||||
|
|
||||||
|
<form method="post" action="gitpull.php" style="margin-top:16px;">
|
||||||
|
<button type="submit" class="ui primary button">
|
||||||
|
<i class="refresh icon"></i> 立即执行 git pull
|
||||||
|
</button>
|
||||||
|
<a class="ui button" href="javascript:history.back()">返回</a>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<?php require("template/".$OJ_TEMPLATE."/footer.php"); ?>
|
||||||
@@ -3,14 +3,6 @@
|
|||||||
$cache_time = 30;
|
$cache_time = 30;
|
||||||
$OJ_CACHE_SHARE = false;
|
$OJ_CACHE_SHARE = false;
|
||||||
|
|
||||||
// Auto git pull on homepage visit (at most once per 60 seconds)
|
|
||||||
$git_pull_lock = '/tmp/hustoj_git_pull.lock';
|
|
||||||
if (!file_exists($git_pull_lock) || (time() - filemtime($git_pull_lock)) > 60) {
|
|
||||||
touch($git_pull_lock);
|
|
||||||
$repo_dir = dirname(__DIR__);
|
|
||||||
exec("cd " . escapeshellarg($repo_dir) . " && git pull > /tmp/hustoj_git_pull.log 2>&1 &");
|
|
||||||
}
|
|
||||||
|
|
||||||
require_once( './include/cache_start.php' );
|
require_once( './include/cache_start.php' );
|
||||||
require_once( './include/db_info.inc.php' );
|
require_once( './include/db_info.inc.php' );
|
||||||
require_once( './include/memcache.php' );
|
require_once( './include/memcache.php' );
|
||||||
|
|||||||
@@ -210,6 +210,9 @@
|
|||||||
<?php } ?>
|
<?php } ?>
|
||||||
<?php if(isset($_SESSION[$OJ_NAME.'_'.'administrator'])||isset($_SESSION[$OJ_NAME.'_'.'contest_creator'])||isset($_SESSION[$OJ_NAME.'_'.'problem_editor'])){ ?>
|
<?php if(isset($_SESSION[$OJ_NAME.'_'.'administrator'])||isset($_SESSION[$OJ_NAME.'_'.'contest_creator'])||isset($_SESSION[$OJ_NAME.'_'.'problem_editor'])){ ?>
|
||||||
<a class="item" href="admin/"><i class="settings icon"></i><?php echo $MSG_ADMIN;?></a>
|
<a class="item" href="admin/"><i class="settings icon"></i><?php echo $MSG_ADMIN;?></a>
|
||||||
|
<?php } ?>
|
||||||
|
<?php if(isset($_SESSION[$OJ_NAME.'_'.'administrator'])){ ?>
|
||||||
|
<a class="item" href="gitpull.php"><i class="cloud download icon"></i>更新系统</a>
|
||||||
<?php }
|
<?php }
|
||||||
if(isset($_SESSION[$OJ_NAME.'_'.'balloon'])){
|
if(isset($_SESSION[$OJ_NAME.'_'.'balloon'])){
|
||||||
echo "<a class=item href='balloon.php'><i class='golf ball icon'></i>$MSG_BALLOON</a>";
|
echo "<a class=item href='balloon.php'><i class='golf ball icon'></i>$MSG_BALLOON</a>";
|
||||||
|
|||||||
Reference in New Issue
Block a user