Files
YwxAppThink/addon/haonav/Addon.php
T
yangwx 18101cf11a feat: 上线前安全加固与发布工程化;appmall 由插件迁移为独立应用
安全(P0)
- 发布包签名:新增 scripts/package_keygen.php、scripts/package_verify.php、
  ywxapp\service\PackageVerifier;FrameworkService::apply() 解压前强制验签
  (ywxapp.framework_package_verify,默认开启)
- JWT 收紧:config/jwt.php 移除公开默认密钥,JwtService::assertStrongSecret()
  校验非空 / 长度≥32 / 已知弱值;.env.production.example 键名由 JWT_SECRET_KEY 修正为 JWT_SECRET
- /api/v1/Article:免登录白名单收窄为只读接口,写操作字段白名单 + assertOwner() 越权校验
- 开发者令牌:查找接口不再回显明文、统一失败文案 + 限流
- TemplateInstaller:文件名白名单 + realpath 归一化 + 目录内断言,修复 Zip Slip;补 use ZipArchive
- 后台登录:接入验证码、失败锁定与登录限流(config/ratelimit.php)
- 缓存 / Session 驱动可由 .env 覆盖(CACHE_DRIVER / SESSION_TYPE),集群部署改 redis
- paycenter 支付日志开关改为 PAY_LOG,不再跟随 APP_DEBUG

工程化 / 交付
- 新增 CHANGELOG.md 与 .env.production.example;composer.lock 入库锁定依赖版本
- 插件离线安装 CLI 增加 --uid,后台上传安装透传 uid,付费插件授权校验可通过
- 授权拒绝文案区分「未携带 uid」与「未找到有效授权」,便于排障
- .gitignore 放行 .env.production.example / .env.addon.example / .gitattributes / public/.htaccess

结构
- appmall 由 addon/appmall 迁移为独立应用 app/appmall(市场 / 开发者中心 / 审核 / 分润)
2026-10-22 00:53:39 +08:00

122 lines
4.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// +----------------------------------------------------------------------
// | YwxApp [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: ywxapp <admin@ywxapp.cn>
// +----------------------------------------------------------------------
declare(strict_types=1);
namespace addon\haonav;
use think\facade\Config;
use think\facade\Db;
use ywxapp\AddonBase;
use ywxapp\model\BaseModel;
/**
* 网址导航插件
*
* 安装于框架自动导入 install.sql 建表(wxapp_haonav_category / links / config /
* favorites / favorite_shares / ads / apply / click_stats);
* 卸载时按统一前缀清理全部表;升级时经 callAddonHook('upgrade') 收敛表结构。
*/
class Addon extends AddonBase
{
/**
* 安装钩子(框架自动导入 install.sql 后调用)。
*/
public function install(): bool
{
return true;
}
/**
* 卸载钩子:清理本插件全部表。
* 与 install.sql 表名前缀严格对齐(wxapp_haonav_),直接复用统一前缀,
* 避免依赖 database.prefix 配置(install.sql 为硬写前缀)。
*/
public function uninstall(): bool
{
$prefix = 'wxapp_haonav_';
$tables = [
'category', // 网址分类表
'config', // 导航配置表
'links', // 网址信息表
'favorites', // 用户云端收藏表
'favorite_shares', // 收藏夹分享配置表
'ads', // 广告位表
'apply', // 自助申请(友链/广告)表
'click_stats', // 点击统计聚合表
'ratings', // 网址评分记录表
];
foreach ($tables as $t) {
try {
Db::execute("DROP TABLE IF EXISTS `{$prefix}{$t}`");
} catch (\Exception $e) {
// 忽略
}
}
return true;
}
/**
* 升级时确保全部表存在(读 install.sql,CREATE TABLE IF NOT EXISTS 幂等建表)。
* 单一事实源 = install.sql,避免复制 DDL 导致漂移。
*/
private function ensureTablesFromInstallSql(): void
{
$sqlFile = __DIR__ . DIRECTORY_SEPARATOR . 'install.sql';
if (!is_file($sqlFile)) {
return;
}
$content = (string) file_get_contents($sqlFile);
$content = preg_replace('/--.*|\/\*[\s\S]*?\*\//', '', $content);
$stmts = array_filter(
array_map('trim', explode(';', $content)),
function ($s) {
return strlen($s) > 5 && preg_match('/^CREATE\s+TABLE/i', $s);
}
);
foreach ($stmts as $sql) {
if (preg_match('/CREATE\s+TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?`?([\w]+)`?/i', $sql, $m)) {
BaseModel::ensureTable($m[1], $sql);
}
}
}
/**
* 升级钩子(在线升级 / 后台手动升级均会触发)。
*
* 在线升级(AddonService::onlineUpgrade)已重导 install.sql 补全表;
* 但「已存在表新增列」install.sql 的 CREATE TABLE IF NOT EXISTS 对已有表无效。
* 此处作为升级收敛点:确保全部表存在 + 对已知易漂移列做幂等补列兜底。
* 未来新增列统一在 $columnFixes 登记(须带 DEFAULT x 或 NULL,保证降级安全),
* 避免各控制器/模型重复 ensureColumn/ALTER 导致 DDL 漂移。
*
* @param string $currentVersion 升级前版本号
*/
public function upgrade($currentVersion = ''): bool
{
// 1) 确保全部表存在(幂等,单一事实源 install.sql)
$this->ensureTablesFromInstallSql();
// 2) 补列兜底:'完整表名(含前缀)' => ['列名' => '列定义']
// BaseModel::ensureColumn 先探测存在性,重复执行安全。
// install.sql 当前已含全部列,故此处留空;新增列在此登记即可。
$columnFixes = [
// 'wxapp_haonav_links' => [
// 'new_col' => "varchar(50) NOT NULL DEFAULT '' COMMENT '示例'",
// ],
];
foreach ($columnFixes as $table => $cols) {
foreach ($cols as $column => $def) {
BaseModel::ensureColumn($table, $column, $def);
}
}
return true;
}
}