sqlite-web 0.7.2
gitea.db
issue
Create
Query
access
access_token
action
action_artifact
action_run
action_run_index
action_run_job
action_runner
action_runner_token
action_schedule
action_schedule_spec
action_task
action_task_output
action_task_step
action_tasks_version
action_variable
app_state
attachment
auth_token
badge
branch
collaboration
comment
commit_status
commit_status_index
commit_status_summary
commit_sync_log
commit_sync_status
dbfs_data
dbfs_meta
deploy_key
email_address
email_hash
external_login_user
follow
gpg_key
gpg_key_import
hook_task
issue
issue_assignees
issue_content_history
issue_dependency
issue_index
issue_label
issue_pin
issue_user
issue_watch
label
language_stat
lfs_lock
lfs_meta_object
login_source
milestone
mirror
notice
notification
oauth2_application
oauth2_authorization_code
oauth2_grant
org_user
package
package_blob
package_blob_upload
package_cleanup_rule
package_file
package_property
package_version
project
project_board
project_issue
protected_branch
protected_tag
public_key
pull_auto_merge
pull_request
push_mirror
reaction
release
renamed_branch
repo_archiver
repo_hidden_file
repo_indexer_status
repo_license
repo_redirect
repo_topic
repo_transfer
repo_unit
repository
review
review_state
secret
session
sqlite_sequence
star
stopwatch
system_setting
task
team
team_invite
team_repo
team_unit
team_user
topic
tracked_time
two_factor
upload
user
user_badge
user_blocking
user_open_id
user_redirect
user_setting
version
watch
webauthn_credential
webhook
Toggle helper tables
Structure
Content
Query
Insert
Drop
Import
Export
Delete row 471 from issue
id
471
repo_id
18
index
178
poster_id
1
original_author
original_author_id
0
name
🔍 代码审查报告:pc-260616 - revert 71ebc9a81e26e7d1caa646
🔍 代码审查报告:pc-260616 - revert 71ebc9a81e26e7d1caa646d4031160c759ae06d9 r
...
content
## 自动代码审查报告 **分支**: pc-260616 **提交**: `4d9ad20551
## 自动代码审查报告 **分支**: pc-260616 **提交**: `4d9ad2055104c89b673a5fed8b1717b64f24034c` **提交人**: huangliujian (hlj@g-hi.com) **时间**: 2026-06-02 17:07:27 --- ## 1. 审查摘要 - **代码质量评分**:5.5 / 10 分 - **总体评价**:代码实现了完整的报表查询与导出、定时任务及业务配置功能,业务逻辑基本闭环。但存在大量重复代码、直接操作超全局变量 `$_GET`、N+1 查询性能瓶颈、事务处理不规范等问题。整体架构偏向传统 CI3 写法,缺乏现代 PHP 的输入过滤、依赖注入与 DRY 原则实践。 - **风险等级**:🔴 高(存在 SQL 注入隐患、性能瓶颈及事务状态异常风险) > 📌 **框架说明**:根据代码特征(`BASEPATH`、`$this->load->model()`、`get_instance()`、`$this->db->trans_start()` 等),当前项目实际基于 **CodeIgniter 3 (CI3)** 架构。以下审查建议将严格遵循 CI3 最佳实践与 PSR-12 规范。若确为 `phpci` 定制版,请核对底层加载器是否兼容。 ## 2. 问题详情 | 严重程度 | 文件/行号 | 问题描述 | 建议修改方案 | 代码示例 (可选) | | :--- | :--- | :--- | :--- | :--- | | 🔴 严重 | `FinanceReport2.php` (多处) | 直接使用 `$_GET` 获取参数,未做任何过滤或类型校验。若参数直接拼接至 SQL 或输出至前端,极易引发 SQL 注入与 XSS 攻击。 | 统一使用 CI3 输入类 `$this->input->get()` 进行 XSS 过滤,并对关键参数(如时间、ID、导出字段)进行白名单/类型强校验。 | `$params = $this->input->get(null, true);`<br>`$exportFields = array_intersect($exportFields, array_keys($columnArr));` | | 🔴 严重 | `Ahead_book_order_model.php` (~L45) | `get_list()` 循环内调用 `$this->ahead_user_model->get_one()` 查询用户手机号,导致典型的 **N+1 查询**,数据量稍大即拖垮数据库。 | 提取所有 `ahead_user_id`,使用 `WHERE IN` 批量查询,或在主 SQL 中 `LEFT JOIN` 用户表。 | 见下方优化示例 | | 🟠 警告 | `RoomTiming.php` (~L115) | 事务处理不规范:`trans_start()` 后在 `catch` 中手动调用 `trans_rollback()`,随后又调用 `trans_complete()`。CI3 事务状态机可能因此产生冲突或重复提交。 | 统一使用 `trans_start()` + `trans_complete()`(自动回滚),或改用 `trans_begin()` + `trans_commit()`/`trans_rollback()` 显式控制。 | 见下方优化示例 | | 🟠 警告 | `FinanceReport2.php` (导出方法) | 多个导出方法(如 `getWaresCountExport`、`getSaleWaresExport`、`standLogExport`)逻辑高度重复,违反 DRY 原则,维护成本极高。 | 抽取公共导出基类或 Trait,将标题、字段映射、宽度配置、模型方法名作为参数传入,统一处理表头、合计行与文件生成。 | 建议重构为 `BaseExportController::doExport($config)` | | 🟠 警告 | `TimedTask.php` (L2, L18) | `set_time_limit(0);` 在 Web 控制器中直接调用,若被恶意请求触发将长期占用 PHP-FPM 进程。定时任务应通过 CLI 模式运行。 | 移除 `set_time_limit`,通过 CI3 CLI 路由执行(如 `php index.php timedtask upStockByOrder`),并在基类中限制仅 CLI 可访问。 | `if (is_cli() === FALSE) exit('CLI only');` | | 🟡 建议 | `RoomTiming.php` (L10) | 类名 `roomTiming` 首字母未大写,违反 PSR-12 规范;`FinanceReport2` 类名含数字,不利于自动加载与语义化。 | 重命名为 `RoomTiming`,移除类名数字,采用语义化版本控制或路由分组替代。 | `class RoomTiming extends PcServer` | | 🟡 建议 | `Ahead_book_order_model.php` (L4) | 文件顶部直接执行 `$CI = &get_instance(); $CI->load->model(...);`,在文件被 `include` 时即触发加载,非标准做法且可能引发依赖时序问题。 | 移至 `__construct()` 中,或依赖 CI3 自动加载机制。 | `public function __construct() { parent::__construct(); $this->load->model('Simple_model'); }` | | 🟡 建议 | `FinanceReport2.php` (L4) | 使用 `include FCPATH . 'application' . DIRECTORY_SEPARATOR ...` 引入父控制器,路径硬编码且未使用 `require_once`,易导致重复声明。 | 使用 CI3 常量 `APPPATH` 并配合 `require_once`,或配置自动加载。 | `require_once APPPATH . 'controllers/FinanceReport.php';` | ### 🔍 关键代码优化示例 **1. 修复 N+1 查询 (`Ahead_book_order_model.php`)** ```php // 优化前:循环内查询 foreach ($order_info as &$v) { if (empty($v['book_mobile'])) { $user_data = $this->ahead_user_model->get_one(['_id' => $v['ahead_user_id']], '_mobile'); // ... } } // 优化后:批量查询 + 内存映射 $missingMobileIds = array_filter(array_column($order_info, 'ahead_user_id'), function($id) use ($order_info) { // 实际应结合业务逻辑筛选,此处简化 return true; }); if (!empty($missingMobileIds)) { $users = $this->ahead_user_model->get_user_by_ids($missingMobileIds, '_id,_mobile', true); foreach ($order_info as &$v) { if (empty($v['book_mobile']) && isset($users[$v['ahead_user_id']])) { $v['book_mobile'] = $users[$v['ahead_user_id']]['_mobile']; $this->update_book_mobile($v['book_id'], $v['id'], $v['book_mobile']); } } } ``` **2. 规范事务处理 (`RoomTiming.php`)** ```php // 推荐写法:利用 CI3 自动回滚机制 $this->db->trans_start(); try { // 业务逻辑... if ($id) { $res = $this->ahead_room_timing_model->update($update_data, $where); } else { $res = $this->ahead_room_timing_model->insert($add_data); } if (!$res) { throw new Exception('操作失败'); } $this->db->trans_complete(); // 自动检测异常并回滚 $this->success_response('操作成功'); } catch (Exception $e) { $this->db->trans_complete(); // 触发回滚 $this->error_response($e->getMessage()); } ``` ## 3. 总结与行动建议 ### 🚨 优先修复项(P0/P1) 1. **输入安全加固**:全局替换 `$_GET` / `$_POST` 为 `$this->input->get()` / `$this->input->post()`,开启 CI3 的 XSS 过滤配置。对 `json_decode` 增加 `json_last_error()` 校验,防止非法 JSON 导致解析崩溃。 2. **消除 N+1 查询**:立即重构 `Ahead_book_order_model::get_list()` 中的循环查询逻辑,改用批量查询或 SQL `JOIN`,预计可提升列表接口 5~10 倍响应速度。 3. **统一事务控制**:修正 `RoomTiming.php` 及其他控制器中的事务写法,避免 `trans_rollback()` 与 `trans_complete()` 混用导致数据库连接状态不一致。 ### 🛠 后续重构与优化方向 1. **导出逻辑抽象化**:当前导出代码重复率 >70%。建议创建 `ExportService` 或 `BaseExportController`,将字段配置、合计计算、Excel/PDF 渲染、文件流输出封装为可复用组件。支持分块查询(Chunk)与流式写入(如 `PhpSpreadsheet` 的 `Writer\Xlsx` 流模式),彻底移除 `ini_set('memory_limit', '500M')` 的暴力解法。 2. **定时任务 CLI 化**:`TimedTask.php` 中的方法应通过 CI3 CLI 路由调用,移除 `set_time_limit(0)` 和 `exit('OK')`,改用 `$this->output->set_status_header(200)` 或标准 CLI 输出。增加任务锁机制(如 Redis `SETNX`)防止并发重复执行。 3. **规范与可维护性**: - 严格遵循 PSR-12:类名 `PascalCase`,方法名 `camelCase`,移除类名数字。 - 清理注释掉的代码与 `exit()` 硬中断,改用框架标准响应方法。 - 将硬编码的魔法数字(如 `86400`、`50`、`610`)提取为常量或配置文件。 - 建议引入静态分析工具(如 `PHPStan` 或 `Psalm`)与代码规范检查(`PHP_CodeSniffer`)纳入 CI 流程。 > 💡 **提示**:若代码片段因截断未能完整展示部分逻辑(如 `FinanceReport2.php` 末尾、`TimedTask.php` 后半段),建议补充完整文件以便进行更精准的边界条件与异常流审查。当前建议已覆盖可见代码的核心风险点,可直接落地实施。 --- *此 Issue 由代码审查服务自动创建*
...
milestone_id
0
priority
0
is_closed
0
is_pull
0
num_comments
0
ref
deadline_unix
0
created_unix
1780391247
updated_unix
1780391247
closed_unix
0
is_locked
0
content_version
0
time_estimate
0
Delete
Cancel