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
Update row 478 in issue
id
Primary key.
INTEGER NOT NULL
repo_id
INTEGER
index
INTEGER
poster_id
INTEGER
original_author
TEXT
original_author_id
INTEGER
name
🔍 代码审查报告:app-260616 - 自助报表
TEXT
content
## 自动代码审查报告 **分支**: app-260616 **提交**: `6c1575fa3fcb52898b64e380e2fc77cf85718a86` **提交人**: LITTLEMAIDI (11833999+littlemaidi@user.noreply.gitee.com) **时间**: 2026-06-02 18:50:14 --- ## 1. 审查摘要 - **代码质量评分**:6.5 / 10 分 - **总体评价**:业务逻辑覆盖较全,报表统计与多维度聚合功能实现完整。但存在明显的 **SQL 注入隐患**、**N+1 查询性能瓶颈** 以及 **面向对象设计不规范** 问题。部分查询构造器使用方式偏离现代 PHP 安全规范,需优先修复。 - **风险等级**:🔴 高 ## 2. 问题详情 | 严重程度 | 文件/行号 | 问题描述 | 建议修改方案 | 代码示例 (可选) | | :--- | :--- | :--- | :--- | :--- | | 🔴 严重 | `Ahead_songs_sales_pay_log_model.php`<br>约第 268 行 (`refund` 方法) | **SQL 注入风险**:`$refund_reason` 与 `$CI->admin_data` 直接字符串拼接进 `UPDATE` 语句,未做转义或参数绑定。若输入含双引号或特殊字符将导致语法错误或注入。 | 使用框架查询构造器的参数绑定机制,或调用内置的 `escape()` 方法。避免手动拼接 SQL。 | ```php<br>// 推荐写法<br>$this->db->set('_refund_reason', $refund_reason)<br> ->set('_refund_admin_id', $CI->admin_data['_id'] ?? 0)<br> ->set('_refund_admin_name', $CI->admin_data['_name'] ?? '')<br> ->where($where)<br> ->update($this->table_name);<br>``` | | 🔴 严重 | `Ahead_songs_sales_pay_log_model.php`<br>约第 200 行<br>`Jh_community_shop_revenues_detail_model.php`<br>约第 150 行 | **SQL 注入/语法错误**:`$pay_platform_where` 循环中直接拼接 `$params['pay_platform_arr']`,且**未添加引号**。若值为字符串将引发 SQL 语法报错;若未做类型过滤则存在注入风险。 | 强制类型转换 `(int)`,或改用框架提供的 `or_where()` 链式调用,由底层驱动处理转义。 | ```php<br>// 推荐写法<br$this->db->group_start();<br>foreach ($params['pay_platform_arr'] as $p) {<br> $parts = explode('_', $p);<br> $this->db->or_where('a._pay_platform', (int)$parts[0]);<br> if (!empty($parts[1])) {<br> $this->db->or_where('a._second_pay_platform', (int)$parts[1]);<br> }<br>}<br>$this->db->group_end();<br>``` | | 🟠 警告 | `Ahead_songs_sales_pay_log_model.php`<br>约第 115 行 (`get_shop_incomes_statement_trend`) | **N+1 查询性能瓶颈**:在 `foreach ($result as &$v)` 循环中逐段执行 `$this->select()`。若查询跨度为 30 天,将产生 30+ 次独立 SQL 请求,严重拖慢接口响应。 | 改为**单次查询**拉取全量时间段数据,利用 `GROUP BY` 时间表达式,在 PHP 层按 Key 映射填充;或引入 Redis 缓存趋势数据。 | ```php<br>// 优化思路:一次性查询<br>$where['_consume_time >='] = $min_start;<br>$where['_consume_time <'] = $max_end;<br>$all_data = $this->select($where, $fields, $group_by);<br>// 在 PHP 中按 create_hour/date 建立索引映射<br>$mapped = array_column($all_data, 'amount', $group_by);<br>``` | | 🟠 警告 | 两文件顶部 | **反模式:全局 `$CI` 实例化**:在类外部声明 `$CI = &get_instance();` 违反 OOP 原则。在文件被 `include` 时即执行,易在 CLI/多进程环境下引发状态污染或内存泄漏。 | 移除文件顶部全局声明。在类内部通过 `$this->load->model()` 按需加载,或在 `__construct()` 中统一初始化。 | 删除顶部 `$CI = &get_instance();`,方法内直接使用 `$this->load->model('xxx');` | | 🟠 警告 | `Jh_community_shop_revenues_detail_model.php`<br>多个统计方法 | **重复聚合逻辑**:`get_community_revenues_data`、`get_community_revenues_sum_data` 中计算 `wx_amount`、`tuangou_amount` 等逻辑高度重复,违反 DRY 原则。 | 抽取私有方法 `private function aggregate_revenue_stats(array $data): array` 统一处理收入/退款分类汇总。 | ```php<br>private function aggregate_revenue_stats($data) {<br> $stats = ['wx'=>0, 'tuangou'=>0, 'other'=>0];<br> foreach($data as $v) { /* 统一分类累加 */ }<br> return $stats;<br>}<br>``` | | 🟡 建议 | 全局多处 | **魔法数字/字符串泛滥**:大量使用 `'1'`, `'4'`, `'8'`, `'9'` 表示状态与支付渠道,缺乏语义化,后期维护成本高。 | 定义类常量或 PHP 8.1+ 枚举(Enum)。如 `const STATUS_PAID = 1; const PAY_WECHAT = '1';`。 | ```php<br>class Ahead_songs_sales_pay_log_model {<br> const PAY_WECHAT = '1';<br> const PAY_VIP = '3';<br> // 替换代码中的硬编码<br>}<br>``` | | 🟡 建议 | `Jh_community_shop_revenues_detail_model.php`<br>约第 240 行 (`get_date_type_info`) | **静态缓存潜在风险**:`self::$date_type_info` 在 PHP-FPM 下安全,但若项目未来迁移至 Swoole/Workerman 等常驻进程,将导致跨请求数据污染。 | 改为实例属性 `$this->date_type_info`,或增加请求级重置逻辑。若确定仅运行于 FPM 可保留。 | `private $date_type_info = [];` 替代 `public static $date_type_info = [];` | | 🟡 建议 | `Ahead_songs_sales_pay_log_model.php`<br>约第 140 行 | **频繁加载模型**:同一方法内多次调用 `$this->load->model()`。虽 CI 有内部缓存,但影响代码整洁度与可读性。 | 将高频依赖模型移至构造函数或父类中预加载,或使用依赖注入容器管理。 | 在 `__construct()` 中集中加载 `ahead_shop_group_model` 等。 | ## 3. 总结与行动建议 ### 🔑 优先修复项(P0/P1) 1. **彻底修复 SQL 拼接漏洞**:立即替换 `refund()` 方法中的字符串拼接,以及所有 `$pay_platform_where` 的 `implode(' or ', ...)` 逻辑。务必使用框架提供的参数绑定或查询构造器链式方法。 2. **消除 N+1 查询**:重构 `get_shop_incomes_statement_trend()`,将循环内的数据库查询改为**单次范围查询 + PHP 内存映射**。预计可降低 70% 以上的数据库 I/O 开销。 3. **移除全局 `$CI` 声明**:将文件顶部的 `get_instance()` 调用清理,改为标准的模型内部加载机制,避免潜在的状态泄漏。 ### 🛠 后续重构与优化方向 - **统一查询构造器规范**:确认 `phpci` 框架底层 `DB_driver` 是否支持 `?` 占位符或命名参数绑定。若不支持,建议封装一层安全查询辅助类,强制拦截未转义的输入。 - **业务常量集中管理**:将支付渠道、订单状态、时间类型等硬编码提取至独立的 `Config` 类或 `Enum` 文件中,提升代码自解释能力。 - **报表计算逻辑抽象**:当前多个模型中存在相似的 `type=1(收入)` 与 `type=2(退款)` 分类汇总逻辑。建议抽象为 `RevenueCalculator` 服务类,通过策略模式或简单工厂处理不同维度的统计,降低模型耦合度。 - **异常处理规范化**:代码中使用了全局函数 `throwError()`。建议逐步替换为 PHP 原生 `throw new \InvalidArgumentException()` 或自定义业务异常类,配合全局异常处理器统一返回格式。 > 💡 **框架适配提示**:代码呈现典型的 CodeIgniter 3 架构特征(如 `get_instance()`、`load->model()`、`select()` 等)。若 `phpci` 为内部定制框架,请重点核对 `system/database/DB_driver.php` 中 `select()`、`up()` 等方法的底层实现是否默认开启参数绑定。若未开启,上述安全修复必须手动调用 `$this->db->escape()` 或 `(int)` 强转。 --- *此 Issue 由代码审查服务自动创建*
TEXT
milestone_id
INTEGER
priority
INTEGER
is_closed
INTEGER
is_pull
INTEGER
num_comments
INTEGER
ref
TEXT
deadline_unix
INTEGER
created_unix
INTEGER
updated_unix
INTEGER
closed_unix
INTEGER
is_locked
INTEGER NOT NULL (default 0
content_version
INTEGER NOT NULL (default 0
time_estimate
INTEGER NOT NULL (default 0
Update
Cancel