Article.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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\Article as ArticleModel;
  11. use app\admin\validate\ArticleValidate;
  12. use think\exception\ValidateException;
  13. use HTMLPurifier;
  14. use HTMLPurifier_Config;
  15. use think\facade\Db;
  16. use think\facade\View;
  17. class Article extends BaseController
  18. {
  19. /**
  20. * 构造函数
  21. */
  22. public function __construct()
  23. {
  24. $this->model = new ArticleModel();
  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|c.title', 'like', '%' . $param['keywords'] . '%'];
  37. }
  38. if (!empty($param['cate_id'])) {
  39. $where[] = ['a.cate_id', '=', $param['cate_id']];
  40. }
  41. $where[] = ['a.delete_time', '=', 0];
  42. $ArticleModel = new ArticleModel();
  43. $list = $ArticleModel->getArticleList($where, $param);
  44. // halt($list);
  45. return table_assign(0, '', $list);
  46. }
  47. else{
  48. return view();
  49. }
  50. }
  51. /**
  52. * 添加
  53. */
  54. public function add()
  55. {
  56. if (request()->isAjax()) {
  57. $param = get_params();
  58. if (isset($param['table-align'])) {
  59. unset($param['table-align']);
  60. }
  61. if (isset($param['content'])) {
  62. $param['md_content'] = '';
  63. }
  64. if (isset($param['docContent-html-code'])) {
  65. $param['content'] = $param['docContent-html-code'];
  66. $param['md_content'] = $param['docContent-markdown-doc'];
  67. unset($param['docContent-html-code']);
  68. unset($param['docContent-markdown-doc']);
  69. }
  70. // 创建HTMLPurifier配置对象
  71. $config = HTMLPurifier_Config::createDefault();
  72. $config->set('HTML.DefinitionID', 'html5-definitions');
  73. $config->set('HTML.DefinitionRev', 1);
  74. $config->set('HTML.ForbiddenAttributes', ['width', 'height']);
  75. //$config->set('HTML.Allowed', 'p,b,a[href],pre[class],code,blockquote,img[src],table,tr,th,td,ul,li,ol,dl,dt,dd');
  76. $config->set('HTML.ForbiddenElements',array('script'),true);//设置拒绝使用的tagname
  77. if ($def = $config->maybeGetRawHTMLDefinition()) {
  78. $def->addElement('video', 'Block', 'Optional: (source, Flow) | (Flow, source) | Flow', 'Common', [
  79. 'src' => 'URI',
  80. 'type' => 'Text',
  81. 'poster' => 'URI',
  82. 'preload' => 'Enum#auto,metadata,none',
  83. 'controls' => 'Bool',
  84. ]);
  85. $def->addElement('source', 'Block', 'Flow', 'Common', [
  86. 'src' => 'URI',
  87. 'type' => 'Text',
  88. ]);
  89. }
  90. // 创建HTMLPurifier对象
  91. $purifier = new HTMLPurifier($config);
  92. //防止xss,过滤输入并输出结果
  93. //$param['content'] = '测试<script>alert(0);</script>';
  94. $param['content'] = $purifier->purify($param['content']);
  95. $param['admin_id'] = $this->uid;
  96. // 检验完整性
  97. try {
  98. validate(ArticleValidate::class)->check($param);
  99. } catch (ValidateException $e) {
  100. // 验证失败 输出错误信息
  101. return to_assign(1, $e->getError());
  102. }
  103. // halt($param);
  104. $ArticleModel = new ArticleModel();
  105. $ArticleModel->addArticle($param);
  106. }else{
  107. View::assign('editor', get_system_config('other','editor'));
  108. return view();
  109. }
  110. }
  111. /**
  112. * 编辑
  113. */
  114. public function edit()
  115. {
  116. $param = get_params();
  117. $ArticleModel = new ArticleModel();
  118. if (request()->isAjax()) {
  119. if (isset($param['table-align'])) {
  120. unset($param['table-align']);
  121. }
  122. if (isset($param['content'])) {
  123. $param['md_content'] = '';
  124. }
  125. if (isset($param['docContent-html-code'])) {
  126. $param['content'] = $param['docContent-html-code'];
  127. $param['md_content'] = $param['docContent-markdown-doc'];
  128. unset($param['docContent-html-code']);
  129. unset($param['docContent-markdown-doc']);
  130. }
  131. // 检验完整性
  132. try {
  133. validate(ArticleValidate::class)->check($param);
  134. } catch (ValidateException $e) {
  135. // 验证失败 输出错误信息
  136. return to_assign(1, $e->getError());
  137. }
  138. // 创建HTMLPurifier配置对象
  139. $config = HTMLPurifier_Config::createDefault();
  140. $config->set('HTML.DefinitionID', 'html5-definitions');
  141. $config->set('HTML.DefinitionRev', 1);
  142. $config->set('HTML.ForbiddenAttributes', ['width', 'height']);
  143. //$config->set('HTML.Allowed', 'p,b,a[href],pre[class],code,blockquote,img[src],table,tr,th,td,ul,li,ol,dl,dt,dd');
  144. $config->set('HTML.ForbiddenElements',array('script'),true);//设置拒绝使用的tagname
  145. if ($def = $config->maybeGetRawHTMLDefinition()) {
  146. $def->addElement('video', 'Block', 'Optional: (source, Flow) | (Flow, source) | Flow', 'Common', [
  147. 'src' => 'URI',
  148. 'type' => 'Text',
  149. 'poster' => 'URI',
  150. 'preload' => 'Enum#auto,metadata,none',
  151. 'controls' => 'Bool',
  152. ]);
  153. $def->addElement('source', 'Block', 'Flow', 'Common', [
  154. 'src' => 'URI',
  155. 'type' => 'Text',
  156. ]);
  157. }
  158. // 创建HTMLPurifier对象
  159. $purifier = new HTMLPurifier($config);
  160. //防止xss,过滤输入并输出结果
  161. //$param['content'] = '测试<script>alert(0);</script>';
  162. $param['content'] = $purifier->purify($param['content']);
  163. $ArticleModel->editArticle($param);
  164. }else{
  165. $id = isset($param['id']) ? $param['id'] : 0;
  166. $detail = $ArticleModel->getArticleById($id);
  167. View::assign('editor', get_system_config('other','editor'));
  168. if (!empty($detail)) {
  169. if(!empty($article['md_content'])){
  170. View::assign('editor',1);
  171. }
  172. $keyword_array = Db::name('ArticleKeywords')
  173. ->field('i.aid,i.keywords_id,k.title')
  174. ->alias('i')
  175. ->join('keywords k', 'k.id = i.keywords_id', 'LEFT')
  176. ->order('i.create_time asc')
  177. ->where(array('i.aid' => $id, 'k.status' => 1))
  178. ->select()->toArray();
  179. $detail['keyword_ids'] = implode(",", array_column($keyword_array, 'keywords_id'));
  180. $detail['keyword_names'] = implode(',', array_column($keyword_array, 'title'));
  181. $detail['keyword_array'] = $keyword_array;
  182. View::assign('detail', $detail);
  183. return view();
  184. }
  185. else{
  186. throw new \think\exception\HttpException(404, '找不到页面');
  187. }
  188. }
  189. }
  190. /**
  191. * 查看信息
  192. */
  193. public function read()
  194. {
  195. $param = get_params();
  196. $id = isset($param['id']) ? $param['id'] : 0;
  197. $ArticleModel = new ArticleModel();
  198. $detail = $ArticleModel->getArticleById($id);
  199. if (!empty($detail)) {
  200. $keyword_array = Db::name('ArticleKeywords')
  201. ->field('i.aid,i.keywords_id,k.title')
  202. ->alias('i')
  203. ->join('keywords k', 'k.id = i.keywords_id', 'LEFT')
  204. ->order('i.create_time asc')
  205. ->where(array('i.aid' => $id, 'k.status' => 1))
  206. ->select()->toArray();
  207. $detail['keyword_ids'] = implode(",", array_column($keyword_array, 'keywords_id'));
  208. $detail['keyword_names'] = implode(',', array_column($keyword_array, 'title'));
  209. $detail['keyword_array'] = $keyword_array;
  210. View::assign('detail', $detail);
  211. return view();
  212. }
  213. else{
  214. throw new \think\exception\HttpException(404, '找不到页面');
  215. }
  216. }
  217. /**
  218. * 删除
  219. */
  220. public function del()
  221. {
  222. $param = get_params();
  223. $param = get_params();
  224. $id = isset($param['id']) ? $param['id'] : 0;
  225. $type = isset($param['type']) ? $param['type'] : 0;
  226. $ArticleModel = new ArticleModel();
  227. $ArticleModel->delArticleById($id,$type);
  228. }
  229. }