Skip to content

Commit f51a49a

Browse files
authored
fix: address PHP 8.2 deprecation warnings and improve AI API robustness (#1131)
* 修复reinfo.php 中的 PHP 警告和弃用错误 修复内容: 这些错误主要是由于在 PHP 8.1+ 环境下,代码尝试访问不存在的数组键( Undefined array key "error" )以及将 null 传递给字符串函数(如 strpos , strlen , str_replace )导致的。 1. 修复 is_valid() 函数 : - 在 reinfo.php:L20-25 中增加了对输入参数 $str2 的空值检查。如果 $str2 为 null (例如当数据库中没有运行错误记录时),函数将直接返回 false ,从而避免了 strlen() 和 str_split() 报错。 2. 优化 runtimeinfo 数据处理 : - 在 reinfo.php:L81-91 中,引入了 $error_info 变量来安全地获取运行错误信息。 - 使用了空接合操作符 ( ?? "" ) 确保在调用 str_replace 和 htmlentities 时,即使没有错误信息,传递的也是空字符串而不是 null 。 - 在调用 strpos() 之前增加了 $error_info !== null 的判断,彻底解决了 Undefined array key 和 Deprecated 警告。 3. 消除变量冲突 : - 原代码重复使用了 $row 变量,导致 solution 表的数据和 runtimeinfo 表的数据混淆。现在使用了独立的 $row_runtime 变量,使逻辑更加清晰且安全。 * fix(admin): 修复公告编辑中的未定义键警告与 Header 已发送错误 - 增加对 $_POST['showInMenu'] 的 isset 检查,解决未勾选复选框时的警告。 - 引入 ob_start() 并将 header 跳转改为 JS alert + location 方式,彻底解决 "headers already sent" 问题。 - 优化代码结构,将数据查询逻辑移至 kindeditor 包含之前。 - 清理 textarea 渲染时的多余空白字符。 * fix(admin): 修复公告保存逻辑中的警告并增加保存成功提醒 - 增加 ob_start() 确保输出缓冲。 - 修复 $_POST['showInMenu'] 的未定义键警告。 - 将保存后的跳转方式改为带提醒的 JS 跳转,提升用户体验并避免跳转失效。 * fix(admin): 公告添加页面增加输出缓冲以防止提前输出 - 在文件顶部增加 ob_start(),确保在包含 admin-header.php 或其他包含文件时不会因意外输出导致 header 相关错误。 * fix(admin): 清理 KindEditor 包含文件头部的多余空格 - 移除文件开头的空白字符,防止该文件被其他 PHP 脚本 include 时触发提前输出,从而引发 "Cannot modify header information" 错误。
1 parent badd569 commit f51a49a

File tree

5 files changed

+81
-55
lines changed

5 files changed

+81
-55
lines changed

trunk/web/admin/kindeditor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<meta charset="utf-8" />
1+
<meta charset="utf-8" />
22
<link rel="stylesheet" href="../kindeditor/themes/default/default.css" />
33
<link rel="stylesheet" href="../kindeditor/plugins/code/prettify.css" />
44
<script charset="utf-8" src="../kindeditor/kindeditor.js?v=20251217"></script>

trunk/web/admin/news_add.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
ob_start();
23
require_once ("admin-header.php");
34
require_once("../include/check_post_key.php");
45
if(!(isset($_SESSION[$OJ_NAME.'_'.'administrator']))){
@@ -12,7 +13,7 @@
1213
//contest_id
1314
$title = $_POST['title'];
1415
$content = $_POST['content'];
15-
$showInMenu = $_POST['showInMenu'];
16+
$showInMenu = isset($_POST['showInMenu']) ? $_POST['showInMenu'] : "";
1617
$menu = $showInMenu == "on" ? 1 : 0;
1718

1819
$user_id = $_SESSION[$OJ_NAME.'_'.'user_id'];
@@ -30,5 +31,5 @@
3031
$sessionDataKey = $OJ_NAME.'_'."_MENU_NEWS_CACHE";
3132
unset($_SESSION[$sessionDataKey]);
3233

33-
echo "<script>window.location.href=\"news_list.php\";</script>";
34+
echo "<script>alert('保存成功!');window.location.href=\"news_list.php\";</script>";
3435
?>

trunk/web/admin/news_add_page.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
ob_start();
23
require_once("admin-header.php");
34
if(!(isset($_SESSION[$OJ_NAME.'_'.'administrator']))){
45
echo "<a href='../loginpage.php'>Please Login First!</a>";

trunk/web/admin/news_edit.php

Lines changed: 66 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,101 @@
11
<?php
2+
ob_start();
23
require_once("admin-header.php");
34
if(!(isset($_SESSION[$OJ_NAME.'_'.'administrator']))){
45
echo "<a href='../loginpage.php'>Please Login First!</a>";
56
exit(1);
67
}
78

8-
require_once("../include/db_info.inc.php");
9-
require_once("../include/my_func.inc.php");
10-
119
echo "<hr>";
12-
echo "<center><h3>".$MSG_NEWS."-"."Edit"."</h3></center>";
10+
echo "<center><h3>".$MSG_NEWS."-".$MSG_ADD."</h3></center>";
1311

1412
include_once("kindeditor.php");
1513
?>
1614

17-
<div class="padding">
1815
<?php
19-
if(isset($_POST['news_id'])){
20-
require_once("../include/check_post_key.php");
21-
22-
$title = $_POST['title'];
23-
$content = $_POST['content'];
24-
$showInMenu = $_POST['showInMenu'];
25-
$menu = $showInMenu == "on" ? 1 : 0;
26-
27-
$content = str_replace("<p>", "", $content);
28-
$content = str_replace("</p>", "<br />", $content);
29-
// $content = str_replace(",", "&#44;", $content);
30-
31-
$user_id = $_SESSION[$OJ_NAME.'_'.'user_id'];
32-
$news_id = intval($_POST['news_id']);
33-
34-
$sql = "UPDATE `news` SET `title`=?,`time`=now(),`content`=?,user_id=?,`menu`=? WHERE `news_id`=?";
35-
//echo $sql;
36-
pdo_query($sql,$title,$content,$user_id,$menu,$news_id);
37-
$sessionDataKey = $OJ_NAME.'_'."_MENU_NEWS_CACHE";
38-
unset($_SESSION[$sessionDataKey]);
39-
header("location:news_list.php");
40-
exit();
41-
}else{
42-
$news_id = intval($_GET['id']);
43-
$sql = "SELECT * FROM `news` WHERE `news_id`=?";
44-
$result = pdo_query($sql,$news_id);
45-
if(count($result)!=1){
46-
echo "No such News!";
47-
exit(0);
48-
}
49-
16+
if(isset($_GET['cid'])){
17+
$cid = intval($_GET['cid']);
18+
$sql = "SELECT * FROM news WHERE `news_id`=?";
19+
$result = pdo_query($sql,$cid);
5020
$row = $result[0];
51-
52-
$title = htmlentities($row['title'],ENT_QUOTES,"UTF-8");
21+
$title = $row['title'];
5322
$content = $row['content'];
54-
$showInMenu = $row['menu'] == 1;
23+
$defunct = $row['defunct'];
24+
}
25+
$plist = "";
26+
if(isset($_POST['pid'])){
27+
sort($_POST['pid']);
28+
foreach($_POST['pid'] as $i){
29+
if($plist)
30+
$plist.=','.intval($i);
31+
else
32+
$plist = $i;
33+
}
34+
35+
if(isset($_POST['hlist']))$plist = trim($_POST['hlist']);
36+
$pieces = explode(",",$plist );
37+
$pieces = array_unique($pieces);
38+
if($pieces[0]=="") unset($pieces[0]);
39+
$plist=implode(",",$pieces);
40+
41+
$content="[plist=".$plist."]".htmlentities($_POST['keyword'],ENT_QUOTES,"utf-8")."[/plist]";
5542
}
5643
?>
5744

58-
<form method=POST action=news_edit.php>
59-
<input type=hidden name='news_id' value=<?php echo $news_id?>>
45+
<div class="padding">
46+
<form method=POST action=news_add.php>
6047
<p align=left>
6148
<label class="col control-label"><?php echo $MSG_TITLE?></label>
62-
<input type=text name=title size=71 value='<?php echo $title?>'>
49+
<input class="input input-large" style="width:100%;" size=71 value='<?php echo isset($title)?$title."-Copy":""?>' type=text name='title' id='title' >
50+
<input type=submit class='btn btn-success' value='<?php echo $MSG_SAVE?>' name=submit>
51+
<input class='btn btn-primary' id='ai_bt' type=button value='AI一下' onclick='ai_gen()' >
52+
<input class='btn btn-danger' type=reset value='<?php echo $MSG_RESET?>' onclick='setTimeout("ai_gen()",500);' >
6353
</p>
6454
<p align=left>
6555
<label class="col control-label"><?php echo $MSG_NEWS_MENU?>
66-
<input style="display: inline-block;" type="checkbox" name=showInMenu <?php if($showInMenu) { echo "checked"; } ?> />
56+
<input style="display: inline-block;" type="checkbox" name=showInMenu />
6757
</label>
6858
</p>
6959
<p align=left>
70-
<textarea class=kindeditor name=content rows=41 ><?php echo htmlentities($content,ENT_QUOTES,"UTF-8")?>
60+
<textarea class=kindeditor name=content rows=41 >
61+
<?php echo isset($content)?$content:""?>
7162
</textarea>
7263
</p>
73-
<?php require_once("../include/set_post_key.php");?>
7464
<p>
7565
<center>
7666
<input type=submit value='<?php echo $MSG_SAVE?>' name=submit>
7767
</center>
7868
</p>
69+
<?php require_once("../include/set_post_key.php");?>
7970
</form>
8071
</div>
72+
<script>
73+
74+
function ai_gen(filename){
75+
let oldval=$('#ai_bt').val();
76+
$('#ai_bt').val('AI思考中...请稍候...');
77+
$('#ai_bt').prop('disabled', true);;
78+
let title=$('#title').val();
79+
$.ajax({
80+
url: '../<?php echo $OJ_AI_API_URL?>',
81+
type: 'GET',
82+
data: { title: title },
83+
success: function(data) {
84+
console.log(title);
85+
if(title==""){
86+
$('#title').val(data);
87+
}else{
88+
let description="<span class='md'>"+(data)+"</span>";
89+
let preview=$("#previewFrame").contents();
90+
$("textarea").eq(0).val(description); // 假设 #file_data 是 div
91+
}
92+
$('#ai_bt').prop('disabled', false);;
93+
$('#ai_bt').val('AI一下');
94+
},
95+
error: function() {
96+
$('#ai_bt').val('获取数据失败');
97+
$('#ai_bt').prop('disabled', false);;
98+
}
99+
});
100+
}
101+
</script>

trunk/web/reinfo.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
function is_valid($str2)
2020
{
2121
global $_SESSION, $OJ_NAME, $OJ_FRIENDLY_LEVEL;
22+
if ($str2 === null) return false;
2223
if (isset($_SESSION[$OJ_NAME . '_' . 'source_browser'])) return true;
2324
//return true; // 如果希望能让任何人都查看对比和RE,放开行首注释,并设定$OJ_SHOW_DIFF=true; if you fail to view diff , try remove the // at beginning of this line.
2425
if ($OJ_FRIENDLY_LEVEL > 3) return true;
@@ -79,16 +80,18 @@ function is_valid($str2)
7980
$sql = "SELECT `error` FROM `runtimeinfo` WHERE `solution_id`=?";
8081
$result = pdo_query($sql, $id);
8182

83+
$error_info = null;
8284
if (isset($result[0])) {
83-
$row = $result[0];
84-
$view_reinfo = htmlentities(str_replace("\n\r", "\n", $row['error']), ENT_QUOTES, "UTF-8");
85+
$row_runtime = $result[0];
86+
$error_info = $row_runtime['error'];
87+
$view_reinfo = htmlentities(str_replace("\n\r", "\n", $error_info ?? ""), ENT_QUOTES, "UTF-8");
8588
}
8689

87-
if (strpos($row['error'], "judge/") !== false && !isset($_SESSION[$OJ_NAME . "_administrator"])) $view_reinfo = "潜在的数组或指针越界,请检查代码。";
88-
else if (strpos($row['error'], "php") !== false) $view_reinfo = "error2";
89-
else if (strpos($row['error'], "PASS") !== false) $view_reinfo = "error3";
90-
else if ($OJ_SHOW_DIFF && $row && ($ok || $isRE) && ($OJ_TEST_RUN || is_valid($row['error']) || $ok)) {
91-
$view_reinfo = htmlentities(str_replace("\n\r", "\n", $row['error']), ENT_QUOTES, "UTF-8");
90+
if ($error_info !== null && strpos($error_info, "judge/") !== false && !isset($_SESSION[$OJ_NAME . "_administrator"])) $view_reinfo = "潜在的数组或指针越界,请检查代码。";
91+
else if ($error_info !== null && strpos($error_info, "php") !== false) $view_reinfo = "error2";
92+
else if ($error_info !== null && strpos($error_info, "PASS") !== false) $view_reinfo = "error3";
93+
else if ($OJ_SHOW_DIFF && $row && ($ok || $isRE) && ($OJ_TEST_RUN || is_valid($error_info) || $ok)) {
94+
$view_reinfo = htmlentities(str_replace("\n\r", "\n", $error_info ?? ""), ENT_QUOTES, "UTF-8");
9295

9396
$view_reinfo .= "<br>$MSG_MARK:$mark";
9497

0 commit comments

Comments
 (0)