Skip to content

fix: address PHP 8.2 deprecation warnings and improve AI API robustness#1131

Merged
zhblue merged 6 commits intozhblue:masterfrom
jepeng-miao:master
Feb 12, 2026
Merged

fix: address PHP 8.2 deprecation warnings and improve AI API robustness#1131
zhblue merged 6 commits intozhblue:masterfrom
jepeng-miao:master

Conversation

@jepeng-miao
Copy link
Copy Markdown
Contributor

@jepeng-miao jepeng-miao commented Feb 12, 2026

fix: address PHP 8.2 deprecation warnings and improve AI API robustness

  • Replace deprecated ${var} string interpolation with {$var} in common.php to fix PHP 8.2+ compatibility issues.
  • Add strict validation for pdo_query results in common.php and yuanqi.php to prevent "非法参数" errors when querying non-existent solution IDs or problem IDs.
  • Refactor permission check logic in AI API endpoints to ensure safe access to source code and runtime info.

fix: resolve permission errors and undefined variable in problem_add.php

  • Fixed "Undefined variable $MSG_DATA" by using $MSG_TESTDATA from language files.
  • Improved directory creation logic in problem_add.php with error checking.

修复内容:
这些错误主要是由于在 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 变量,使逻辑更加清晰且安全。
- 增加对 $_POST['showInMenu'] 的 isset 检查,解决未勾选复选框时的警告。
- 引入 ob_start() 并将 header 跳转改为 JS alert + location 方式,彻底解决 "headers already sent" 问题。
- 优化代码结构,将数据查询逻辑移至 kindeditor 包含之前。
- 清理 textarea 渲染时的多余空白字符。
- 增加 ob_start() 确保输出缓冲。
- 修复 $_POST['showInMenu'] 的未定义键警告。
- 将保存后的跳转方式改为带提醒的 JS 跳转,提升用户体验并避免跳转失效。
- 在文件顶部增加 ob_start(),确保在包含 admin-header.php 或其他包含文件时不会因意外输出导致 header 相关错误。
- 移除文件开头的空白字符,防止该文件被其他 PHP 脚本 include 时触发提前输出,从而引发 "Cannot modify header information" 错误。
Copy link
Copy Markdown
Owner

@zhblue zhblue left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

textarea>后面要紧跟<?php
否则每次编辑会在头部多出空白字符

@zhblue zhblue merged commit f51a49a into zhblue:master Feb 12, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants