Pages.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021 勾股工作室
  4. * @license https://opensource.org/licenses/Apache-2.0
  5. * @link https://www.gougucms.com
  6. */
  7. declare (strict_types = 1);
  8. namespace app\admin\controller;
  9. use app\admin\BaseController;
  10. use app\admin\model\Pages as PagesModel;
  11. use app\admin\validate\PagesValidate;
  12. use think\exception\ValidateException;
  13. use HTMLPurifier;
  14. use HTMLPurifier_Config;
  15. use think\facade\Db;
  16. use think\facade\View;
  17. class Pages extends BaseController
  18. {
  19. /**
  20. * 构造函数
  21. */
  22. public function __construct()
  23. {
  24. $this->model = new PagesModel();
  25. $this->uid = get_login_admin('id');
  26. }
  27. /**
  28. * 数据列表
  29. */
  30. public function datalist()
  31. {
  32. if (request()->isAjax()) {
  33. $param = get_params();
  34. $where = [];
  35. if (!empty($param['keywords'])) {
  36. $where[] = ['a.id|a.title|a.desc|a.content', 'like', '%' . $param['keywords'] . '%'];
  37. }
  38. $where[] = ['a.delete_time', '=', 0];
  39. $list = $this->model->getPagesList($where, $param);
  40. return table_assign(0, '', $list);
  41. }
  42. else{
  43. return view();
  44. }
  45. }
  46. /**
  47. * 添加
  48. */
  49. public function add()
  50. {
  51. if (request()->isAjax()) {
  52. $param = get_params();
  53. // 检验完整性
  54. try {
  55. validate(PagesValidate::class)->check($param);
  56. } catch (ValidateException $e) {
  57. // 验证失败 输出错误信息
  58. return to_assign(1, $e->getError());
  59. }
  60. // 创建HTMLPurifier配置对象
  61. $config = HTMLPurifier_Config::createDefault();
  62. $config->set('HTML.DefinitionID', 'html5-definitions');
  63. $config->set('HTML.DefinitionRev', 1);
  64. $config->set('HTML.ForbiddenAttributes', ['width', 'height']);
  65. //$config->set('HTML.Allowed', 'p,b,a[href],pre[class],code,blockquote,img[src],table,tr,th,td,ul,li,ol,dl,dt,dd');
  66. $config->set('HTML.ForbiddenElements',array('script'),true);//设置拒绝使用的tagname
  67. if ($def = $config->maybeGetRawHTMLDefinition()) {
  68. $def->addElement('video', 'Block', 'Optional: (source, Flow) | (Flow, source) | Flow', 'Common', [
  69. 'src' => 'URI',
  70. 'type' => 'Text',
  71. 'poster' => 'URI',
  72. 'preload' => 'Enum#auto,metadata,none',
  73. 'controls' => 'Bool',
  74. ]);
  75. $def->addElement('source', 'Block', 'Flow', 'Common', [
  76. 'src' => 'URI',
  77. 'type' => 'Text',
  78. ]);
  79. }
  80. // 创建HTMLPurifier对象
  81. $purifier = new HTMLPurifier($config);
  82. //防止xss,过滤输入并输出结果
  83. //$param['content'] = '测试<script>alert(0);</script>';
  84. $param['content'] = $purifier->purify($param['content']);
  85. $param['admin_id'] = $this->uid;
  86. $this->model->addPages($param);
  87. }else{
  88. $templates = get_file_list(CMS_ROOT . '/app/home/view/pages/');
  89. View::assign('templates', $templates);
  90. return view();
  91. }
  92. }
  93. /**
  94. * 编辑
  95. */
  96. public function edit()
  97. {
  98. $param = get_params();
  99. if (request()->isAjax()) {
  100. // 检验完整性
  101. try {
  102. validate(PagesValidate::class)->check($param);
  103. } catch (ValidateException $e) {
  104. // 验证失败 输出错误信息
  105. return to_assign(1, $e->getError());
  106. }
  107. // 创建HTMLPurifier配置对象
  108. $config = HTMLPurifier_Config::createDefault();
  109. $config->set('HTML.DefinitionID', 'html5-definitions');
  110. $config->set('HTML.DefinitionRev', 1);
  111. $config->set('HTML.ForbiddenAttributes', ['width', 'height']);
  112. //$config->set('HTML.Allowed', 'p,b,a[href],pre[class],code,blockquote,img[src],table,tr,th,td,ul,li,ol,dl,dt,dd');
  113. $config->set('HTML.ForbiddenElements',array('script'),true);//设置拒绝使用的tagname
  114. if ($def = $config->maybeGetRawHTMLDefinition()) {
  115. $def->addElement('video', 'Block', 'Optional: (source, Flow) | (Flow, source) | Flow', 'Common', [
  116. 'src' => 'URI',
  117. 'type' => 'Text',
  118. 'poster' => 'URI',
  119. 'preload' => 'Enum#auto,metadata,none',
  120. 'controls' => 'Bool',
  121. ]);
  122. $def->addElement('source', 'Block', 'Flow', 'Common', [
  123. 'src' => 'URI',
  124. 'type' => 'Text',
  125. ]);
  126. }
  127. // 创建HTMLPurifier对象
  128. $purifier = new HTMLPurifier($config);
  129. //防止xss,过滤输入并输出结果
  130. //$param['content'] = '测试<script>alert(0);</script>';
  131. $param['content'] = $purifier->purify($param['content']);
  132. $this->model->editPages($param);
  133. }else{
  134. $id = isset($param['id']) ? $param['id'] : 0;
  135. $detail = $this->model->getPagesById($id);
  136. if (!empty($detail)) {
  137. //轮播图
  138. if(!empty($detail['banner'])) {
  139. $detail['banner_array'] = explode(',',$detail['banner']);
  140. }
  141. //关键字
  142. $keyword_array = Db::name('PagesKeywords')
  143. ->field('i.aid,i.keywords_id,k.title')
  144. ->alias('i')
  145. ->join('keywords k', 'k.id = i.keywords_id', 'LEFT')
  146. ->order('i.create_time asc')
  147. ->where(array('i.aid' => $id, 'k.status' => 1))
  148. ->select()->toArray();
  149. $detail['keyword_ids'] = implode(",", array_column($keyword_array, 'keywords_id'));
  150. $detail['keyword_names'] = implode(',', array_column($keyword_array, 'title'));
  151. $detail['keyword_array'] = $keyword_array;
  152. $templates = get_file_list(CMS_ROOT . '/app/home/view/pages/');
  153. View::assign('templates', $templates);
  154. View::assign('detail', $detail);
  155. return view();
  156. }
  157. else{
  158. throw new \think\exception\HttpException(404, '找不到页面');
  159. }
  160. }
  161. }
  162. /**
  163. * 查看信息
  164. */
  165. public function read()
  166. {
  167. $param = get_params();
  168. $id = isset($param['id']) ? $param['id'] : 0;
  169. $detail = $this->model->getPagesById($id);
  170. if (!empty($detail)) {
  171. //轮播图
  172. if(!empty($detail['banner'])) {
  173. $detail['banner_array'] = explode(',',$detail['banner']);
  174. }
  175. //关键字
  176. $keyword_array = Db::name('PagesKeywords')
  177. ->field('i.aid,i.keywords_id,k.title')
  178. ->alias('i')
  179. ->join('keywords k', 'k.id = i.keywords_id', 'LEFT')
  180. ->order('i.create_time asc')
  181. ->where(array('i.aid' => $id, 'k.status' => 1))
  182. ->select()->toArray();
  183. $detail['keyword_ids'] = implode(",", array_column($keyword_array, 'keywords_id'));
  184. $detail['keyword_names'] = implode(',', array_column($keyword_array, 'title'));
  185. $detail['keyword_array'] = $keyword_array;
  186. View::assign('detail', $detail);
  187. return view();
  188. }
  189. else{
  190. throw new \think\exception\HttpException(404, '找不到页面');
  191. }
  192. }
  193. /**
  194. * 删除
  195. */
  196. public function del()
  197. {
  198. $param = get_params();
  199. $param = get_params();
  200. $id = isset($param['id']) ? $param['id'] : 0;
  201. $type = isset($param['type']) ? $param['type'] : 0;
  202. $this->model->delPagesById($id,$type);
  203. }
  204. }