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 564 from issue
id
564
repo_id
21
index
250
poster_id
1
original_author
original_author_id
0
name
🔍 代码审查报告:pay-260616 - 1
content
## 自动代码审查报告 **分支**: pay-260616 **提交**: `6ffae9168
## 自动代码审查报告 **分支**: pay-260616 **提交**: `6ffae9168e5eaa3c60090647f0ecd44a54ca37a9` **提交人**: LITTLEMAIDI (11833999+littlemaidi@user.noreply.gitee.com) **时间**: 2026-06-05 14:25:14 --- ## 1. 审查摘要 - **代码质量评分**:4/10 分 - **总体评价**:该模型类承载了大量核心业务逻辑,但代码结构臃肿,存在严重的安全隐患(SQL注入)、多处逻辑缺陷(如 `unset` 误用)、性能瓶颈(重复加载模型、未使用查询构建器)以及不符合现代 PHP 规范的编码风格。需进行系统性安全加固与架构重构。 - **风险等级**:🔴 高 - **局限性说明**:提供的代码在 `get_book_package_list_group_by_shop` 方法末尾被截断,部分逻辑(如 `$special_city_id` 数组后续处理)无法完整评估。以下审查基于已提供代码片段。 ## 2. 问题详情 | 严重程度 | 文件/行号 | 问题描述 | 建议修改方案 | 代码示例 (可选) | | :--- | :--- | :--- | :--- | :--- | | 🔴 严重 | `get_book_package_list` / `get_hot_sale_top5` 等多处 | **SQL 注入漏洞**:直接通过字符串拼接将 `$city_id`、`$shop_name`、`$param['special_merchant_id']` 等外部参数嵌入 SQL 语句,未做任何转义或参数绑定。 | 全面替换为 CI 查询构建器(Query Builder)或使用 `$this->db->query($sql, $bindings)` 进行预处理绑定。 | ```php<br>// 错误<br>$sql .= " AND `shop`.`_name` like '%" . $shop_name . "%'";<br><br>// 正确<br>$this->db->like('shop._name', $shop_name);<br>// 或<br>$this->db->query($sql, [$city_id, $shop_name]);<br>``` | | 🔴 严重 | `get_package_price_list` / `get_screen_list` | **逻辑缺陷:`unset($row)` 无法移除数组元素**。在 `foreach ($list as &$row)` 中调用 `unset($row)` 仅销毁引用变量,原数组 `$list` 保持不变,导致过滤逻辑失效。 | 改用键值遍历并 `unset($list[$key])`,或使用 `array_filter`。 | ```php<br>foreach ($list as $key => &$row) {<br> if ($book_arrival_time < $order_end_time) {<br> unset($list[$key]);<br> continue;<br> }<br>}<br>``` | | 🔴 严重 | 文件顶部 (第 5 行) | **全局调用 `$CI = &get_instance();`**:在类外部直接获取 CI 实例破坏封装性,且在 CLI 模式或非标准生命周期下可能引发致命错误。 | 移除全局调用。若需在类内使用,应在方法内部按需获取,或注入为类属性。 | ```php<br>// 移除文件顶部的 $CI = &get_instance();<br>// 类内部按需使用:<br>$CI =& get_instance();<br>$CI->operational_scene = ...;<br>``` | | 🟠 警告 | 多个方法内部 | **频繁在方法内调用 `$this->load->model()`**:每次请求重复执行模型加载逻辑,增加 I/O 开销,且不符合框架最佳实践。 | 将依赖模型统一移至 `__construct()` 中加载,或配置自动加载。 | ```php<br>public function __construct()<br>{<br> parent::__construct();<br> $this->load->model(['Ahead_vip_model', 'Ahead_yc_merchant_model', ...]);<br>}<br>``` | | 🟠 警告 | `get_package_price_list` & `get_price_set_detail` | **DRY 原则违反**:会员折扣计算逻辑(`vip_level1_price`、`goods_discount_rate` 等)在多处重复编写,维护成本极高且易产生计算偏差。 | 抽取为私有方法 `_apply_vip_discount($row, $vip_level, $discount_rate)` 统一处理。 | ```php<br>private function _apply_vip_discount(&$item, $level, $rate)<br>{<br> $discount = $rate / 100;<br> if ($level > 0 && $item['discount_status'] == 1) {<br> $item['actual_price'] = $item['vip_price'] * $discount;<br> }<br> $item['actual_price'] = change_number_format($item['actual_price']);<br>}<br>``` | | 🟠 警告 | `get_book_package_list` | **静态变量缓存失效**:`self::$_shop_id_arr` 在 PHP-FPM 环境下随请求结束销毁,无法实现跨请求缓存,且无并发安全控制。 | 改用框架 Cache 驱动(如 Redis/Memcached)或 CI 内置 `$this->cache->save()`。 | ```php<br>$cache_key = "shop_ids_by_dist_{$city_id}";<br>$shop_id_arr = $this->cache->get($cache_key);<br>if (!$shop_id_arr) {<br> $shop_id_arr = $this->ahead_shop_model->get_id_by_distance(...);<br> $this->cache->save($cache_key, $shop_id_arr, 300);<br>}<br>``` | | 🟡 建议 | 全局 | **命名与规范不统一**:方法名混用蛇形 (`get_package_price_list`) 与驼峰 (`getPackageInfoByIds`);缺乏 PHP 7+ 类型声明;魔法数字(如 `4`, `100`, `86400`)散落各处。 | 遵循 PSR-12,统一命名规范;添加参数/返回值类型提示;将业务常量提取为 `const`。 | ```php<br>const PACKAGE_TYPE_GROUP_BUY = 4;<br>const SECONDS_PER_DAY = 86400;<br><br>public function getPackageInfoByIds(array $ids, string $fields = '*', array $exceptWhere = []): array<br>``` | | 🟡 建议 | `get_package_price_list` | **复杂时间窗口判断耦合**:跨天营业逻辑与 `FIND_IN_SET` 直接拼接在 `$where['where']` 数组中,可读性差且易因框架底层转义机制引发 SQL 语法错误。 | 将时间窗口计算抽离为独立方法,或使用数据库原生时间函数(如 `HOUR()`, `DAYOFWEEK()`)优化查询。 | *(建议重构为独立方法 `_build_time_window_where($params)`)* | ## 3. 总结与行动建议 ### 🔑 优先修复项(P0) 1. **彻底修复 SQL 注入**:立即替换所有 `$sql .= "..." . $param . "..."` 的拼接写法,全面启用 CI 查询构建器或参数绑定。这是当前最高危的安全漏洞。 2. **修正 `unset` 逻辑**:将 `foreach` 中的 `unset($row)` 改为 `unset($list[$key])`,确保套餐过滤逻辑按预期执行。 3. **移除全局 `$CI` 实例**:清理文件顶部的 `&get_instance()` 调用,避免潜在的生命周期冲突。 ### 🛠 重构与优化方向(P1/P2) 1. **模型依赖集中化**:将散落在各方法中的 `$this->load->model()` 统一收敛至构造函数,降低运行时开销。 2. **逻辑解耦与 DRY**:将会员折扣计算、时间窗口判断、跨天逻辑抽离为私有辅助方法。当前 `get_package_price_list` 方法过长(超 150 行),违反单一职责原则(SRP),建议拆分为 `查询构建`、`数据过滤`、`价格计算` 三个独立步骤。 3. **缓存策略升级**:废弃 PHP 静态变量缓存,接入 Redis 或 CI Cache 驱动,并设置合理的 TTL 与缓存键命名规范。 4. **规范与现代化**: - 统一方法命名风格(推荐全小写蛇形或驼峰,保持项目一致)。 - 引入 PHP 7+ 类型声明(`int`, `array`, `string`, `bool` 等)提升静态分析能力。 - 清理 `//edit by nan` 等历史注释,改用 Git 提交记录追溯变更。 - 将硬编码的业务规则(如套餐类型 `4`、折扣基数 `100`)定义为类常量。 > 💡 **框架适配提示**:代码结构高度符合 **CodeIgniter 3** 规范。若 `phpci` 为内部定制框架,请重点核对 `$this->db->query()`、`$this->load->model()` 及 `$this->cache` 的底层实现是否与 CI3 一致。建议查阅 `phpci` 官方文档中关于 **查询构建器安全绑定** 与 **多数据库切换 (`set_select_db`)** 的最佳实践,以确保底层驱动兼容性。 --- *此 Issue 由代码审查服务自动创建*
...
milestone_id
0
priority
0
is_closed
0
is_pull
0
num_comments
0
ref
deadline_unix
0
created_unix
1780640714
updated_unix
1780640714
closed_unix
0
is_locked
0
content_version
0
time_estimate
0
Delete
Cancel