official_website/app/admin/controller/TemplateFile.php

191 lines
7.3 KiB
PHP
Raw Normal View History

2024-12-02 13:57:08 +08:00
<?php
/**
* @Descripttion : FOXCMS 是一款高效的 PHP 多端跨平台内容管理系统
* @Author : FoxCMS Team
* @Date : 2023/6/26 15:34
* @version : V1.08
* @copyright : ©2021-现在 贵州黔狐科技股份有限公司 版权所有
* @LastEditTime : 2023/6/26 15:34
*/
namespace app\admin\controller;
use app\common\controller\AdminBase;
use think\facade\View;
class TemplateFile extends AdminBase
{
private $filters = ['.php', 'php.', '.php.', 'php']; //过滤
public function index()
{
$activepath = ($this->request->param('activepath') ?? $this->relativeTemplateHtml) . DIRECTORY_SEPARATOR;
$activepath = replaceSymbol($activepath);
if (str_ends_with($activepath, "/")) {
$activepath = substr($activepath, 0, -1);
}
$basePath = root_path() . "templates" . $activepath;
$fArr = array();
$arr_file = getDirFile($basePath, $activepath, $fArr, $this->template['template']);
$templetFilelist = xn_cfg("templet-filelist");
$view_suffix = config('view.view_suffix'); //文件后缀
$filenameList = []; //文件目录
$r_file = []; //返回文件
foreach ($arr_file as $key => $file) {
foreach ($templetFilelist as $k => $v) {
if ($file['filename'] == $k . ".{$view_suffix}") {
$file['intro'] = $v;
break;
}
}
if (empty($file['intro'])) { //没有描述
foreach ($templetFilelist as $k => $v) {
if (str_starts_with($file['filename'], "list_")) {
if (str_ends_with($file['filename'], "_m.{$view_suffix}")) {
$file['intro'] = "手机端列表页模板";
} else {
$file['intro'] = "列表页模板";
}
} elseif (str_starts_with($file['filename'], "view_")) {
if (str_ends_with($file['filename'], "_m.{$view_suffix}")) {
$file['intro'] = "手机端内容页模板";
} else {
$file['intro'] = "内容页模板";
}
} elseif (str_starts_with($file['filename'], "index_")) {
if (!("index_m.{$view_suffix}" == $file['filename']) && str_ends_with($file['filename'], "_m.{$view_suffix}")) {
$file['intro'] = "手机端单页面模板";
} else {
$file['intro'] = "单页面模板";
}
} else {
if (str_ends_with($file['filename'], "_m.{$view_suffix}")) {
$file['intro'] = "手机端其他模板";
} else {
$file['intro'] = "其他模板";
}
}
}
}
if ($file['filemine'] == "file") {
array_push($filenameList, $file['filename']);
} else {
array_push($r_file, $file);
}
$arr_file[$key] = $file;
}
sort($filenameList);
foreach ($filenameList as $key => $filename) {
foreach ($arr_file as $kk => $af) {
if ($af['filename'] == $filename) {
array_push($r_file, $af);
break;
}
}
}
View::assign("arrFile", $r_file);
View::assign("activepath", $activepath);
return view('index');
}
// 新增文件
public function addFile()
{
$activepath = $this->request->param("activepath") ?? $this->relativeTemplateHtml;
if ($this->request->isAjax()) {
$fileName = input("fileName", '', null);
if (empty($fileName)) {
$this->error("文件名称为空");
}
foreach ($this->filters as $filter) {
if ((strpos($fileName, $filter) != false)) {
$fileName = str_replace($filter, "", $fileName);
}
}
if (!((strpos($fileName, ".html") != false) || (strpos($fileName, ".htm") != false))) {
$fileName = $fileName . ".html";
}
$content = input("content", '', null);
$file = root_path() . DIRECTORY_SEPARATOR . 'templates' . $activepath . DIRECTORY_SEPARATOR . $fileName;
if (!is_writable(dirname($file))) {
return "请把模板文件目录设置为可写入权限!" . $file;
}
if (preg_match('#<([^?]*)\?php#i', $content) || (preg_match('#<\?#i', $content) && preg_match(
'#\?>#i',
$content
)) || preg_match('#\{fox\:php([^\}]*)\}#i', $content) || preg_match('#\{php([^\}]*)\}#i', $content)) {
return "模板里不允许有php语法为了安全考虑请通过FTP工具进行编辑上传。";
}
$fp = fopen($file, "w");
fputs($fp, $content);
fclose($fp);
$this->success('操作成功!');
}
View::assign('filePosition', $activepath);
return view("add_file");
}
// 删除文件
public function deleteFile()
{
$filePath = $this->request->param("filePath");
$file = root_path() . DIRECTORY_SEPARATOR . 'templates' . $filePath;
if (!unlink($file)) {
$this->error('操作失败!');
} else {
$this->success('操作成功!');
}
}
// 编辑文件
public function editFile()
{
$activepath = $this->request->param("activepath");
$file = root_path() . DIRECTORY_SEPARATOR . 'templates' . $activepath;
$arr = explode("/", $activepath);
$fileName = $arr[count($arr) - 1];
if ($this->request->isAjax()) {
if (!(str_ends_with($fileName, ".html") || str_ends_with($fileName, ".htm"))) {
$this->error("{$fileName}不能被修改");
}
$content = input("content", '', null);
if (!is_writable(dirname($file))) {
return "请把模板文件目录设置为可写入权限!";
}
if (
preg_match('#<([^?]*)\?php#i', $content) || (preg_match('#<\?#i', $content) && preg_match('#\?>#i', $content)) ||
preg_match('#\{fox\:php([^\}]*)\}#i', $content) || preg_match('#\{php([^\}]*)\}#i', $content)
) {
return "模板里不允许有php语法为了安全考虑请通过FTP工具进行编辑上传。";
}
$fp = fopen($file, "w");
fputs($fp, $content);
fclose($fp);
$this->success('操作成功!');
}
/*读取文件内容*/
$content = "";
if (is_file($file)) {
$filesize = filesize($file);
if (0 < $filesize) {
$fp = fopen($file, "r");
$content = fread($fp, $filesize);
fclose($fp);
}
}
View::assign('fileName', $fileName);
View::assign('filePosition', $activepath);
View::assign(
'content',
$content
);
return view('edit_file');
}
}