Files
YwxAppThink/addon/forum/controller/backend/Editor.php
T

108 lines
3.7 KiB
PHP

<?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\forum\controller\backend;
use think\facade\Config;
use ywxapp\controller\BackendBase;
use ywxapp\service\AddonService;
/**
* 编辑器(UEditor)配置
*
* 在后台可视化管理发帖富文本编辑器的参数:
* 上传目录、图片/附件/视频的允许类型与大小上限、远程图片抓取开关、初始高度。
* 数据持久化到框架统一的插件配置表(wxapp_addon_config),运行时由
* AddonService::config('forum') 读取并与 config.php 默认值合并。
*
* @author ywxapp <admin@ywxapp.cn>
*/
class Editor extends BackendBase
{
/**
* 可被后台保存的配置字段 => 默认值
*/
protected array $fields = [
'editor_upload_dir' => 'uploads/forum',
'editor_image_ext' => 'jpg,jpeg,png,gif,bmp,webp',
'editor_image_size' => 10,
'editor_file_ext' => 'zip,rar,7z,pdf,doc,docx,xls,xlsx,ppt,pptx,txt,md',
'editor_file_size' => 50,
'editor_video_ext' => 'mp4,webm,ogg,mov',
'editor_video_size' => 100,
'editor_catch_image' => 1,
'editor_height' => 360,
];
/**
* 读取当前有效配置(config.php 默认 + 后台保存覆盖)
*/
protected function currentConfig(): array
{
$defaults = (array) Config::get('forum', []);
$saved = AddonService::config('forum');
// 用 $fields 的默认值兜底,确保所有后台字段都有值(模板无需 default 过滤器)
return array_merge($this->fields, $defaults, $saved);
}
/**
* 配置页
*/
public function index()
{
$cfg = $this->currentConfig();
$this->assign('cfg', $cfg);
return $this->fetch('editor/index');
}
/**
* 保存配置
*/
public function save()
{
$post = (array) $this->request->post();
$values = [];
foreach ($this->fields as $key => $default) {
if (!isset($post[$key])) {
continue;
}
$raw = $post[$key];
if (is_bool($default)) {
$values[$key] = $raw ? 1 : 0;
} elseif (is_int($default)) {
$values[$key] = (int) $raw;
} else {
// 字符串 / 逗号分隔扩展名列表:去空格、过滤空项、小写
$str = trim((string) $raw);
if (str_contains($str, ',')) {
$parts = array_filter(array_map(
fn($s) => strtolower(trim($s, " .\t\n\r\0\x0B")),
explode(',', $str)
), fn($s) => $s !== '');
$str = implode(',', $parts);
}
$values[$key] = $str;
}
}
// 上传目录安全校验:仅允许相对路径,禁止目录穿越
if (isset($values['editor_upload_dir'])) {
$dir = preg_replace('#\.+/#', '', str_replace('\\', '/', $values['editor_upload_dir']));
$values['editor_upload_dir'] = trim($dir, '/') ?: $this->fields['editor_upload_dir'];
}
AddonService::config('forum', $values);
return $this->result->success([], '编辑器配置已保存');
}
}