ContentController.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. <?php
  2. /**
  3. * @copyright (C)2016-2099 Hnaoyun Inc.
  4. * @author XingMeng
  5. * @email hnxsh@foxmail.com
  6. * @date 2017年12月15日
  7. * 文章控制器
  8. */
  9. namespace app\admin\controller\content;
  10. use core\basic\Controller;
  11. use app\admin\model\content\ContentModel;
  12. class ContentController extends Controller
  13. {
  14. private $model;
  15. private $blank;
  16. public function __construct()
  17. {
  18. $this->model = new ContentModel();
  19. }
  20. // 文章列表
  21. public function index()
  22. {
  23. if ((! ! $id = get('id', 'int')) && $result = $this->model->getContent($id)) {
  24. $this->assign('more', true);
  25. $this->assign('content', $result);
  26. } else {
  27. $this->assign('list', true);
  28. if (! $mcode = get('mcode', 'var')) {
  29. error('传递的模型编码参数有误,请核对后重试!');
  30. }
  31. if (isset($_GET['keyword'])) {
  32. if (! ! $scode = get('scode', 'var')) {
  33. $result = $this->model->findContent($mcode, $scode, get('keyword', 'vars'));
  34. } else {
  35. $result = $this->model->findContentAll($mcode, get('keyword', 'vars'));
  36. }
  37. } elseif (! ! $scode = get('scode', 'int')) {
  38. $result = $this->model->findContent($mcode, $scode, '');
  39. } else {
  40. $result = $this->model->getList($mcode);
  41. }
  42. $this->assign('contents', $result);
  43. // 文章分类下拉列表
  44. $sort_model = model('admin.content.ContentSort');
  45. $sort_select = $sort_model->getListSelect($mcode);
  46. $this->assign('search_select', $this->makeSortSelect($sort_select, get('scode')));
  47. $this->assign('sort_select', $this->makeSortSelect($sort_select, session('addscode')));
  48. $this->assign('subsort_select', $this->makeSortSelect($sort_select));
  49. // 模型名称
  50. $this->assign('model_name', model('admin.content.Model')->getName($mcode));
  51. // 扩展字段
  52. $this->assign('extfield', model('admin.content.ExtField')->getModelField($mcode));
  53. $this->assign('baidu_zz_token', $this->config('baidu_zz_token'));
  54. $this->assign('baidu_ks_token', $this->config('baidu_ks_token'));
  55. // 前端地址连接符判断
  56. $url_break_char = $this->config('url_break_char') ?: '_';
  57. $this->assign('url_break_char', $url_break_char);
  58. // 获取会员分组
  59. $this->assign('groups', model('admin.member.MemberGroup')->getSelect());
  60. }
  61. $this->display('content/content.html');
  62. }
  63. // 文章增加
  64. public function add()
  65. {
  66. if ($_POST) {
  67. // 获取数据
  68. $scode = post('scode');
  69. $subscode = post('subscode');
  70. $title = post('title');
  71. $titlecolor = post('titlecolor');
  72. $subtitle = post('subtitle');
  73. $filename = trim(post('filename'), '/');
  74. $author = post('author');
  75. $source = post('source');
  76. $outlink = post('outlink');
  77. $date = post('date');
  78. $ico = post('ico');
  79. $pics = post('pics');
  80. // 获取多图标题
  81. $picstitle = post('picstitle');
  82. if ($picstitle) {
  83. $picstitle = implode(',', $picstitle);
  84. }
  85. $content = post('content');
  86. $tags = str_replace(',', ',', post('tags'));
  87. $enclosure = post('enclosure');
  88. $keywords = post('keywords');
  89. $description = post('description');
  90. $status = post('status', 'int');
  91. $istop = post('istop', 'int', '', '', 0);
  92. $isrecommend = post('isrecommend', 'int', '', '', 0);
  93. $isheadline = post('isheadline', 'int', '', '', 0);
  94. $gid = post('gid', 'int') ?: 0;
  95. $gtype = post('gtype', 'int') ?: 4;
  96. $gnote = post('gnote');
  97. if (! $scode) {
  98. alert_back('内容分类不能为空!');
  99. }
  100. if (! $title) {
  101. alert_back('文章标题不能为空!');
  102. }
  103. if ($filename && ! preg_match('/^[a-zA-Z0-9\-_\/]+$/', $filename)) {
  104. alert_back('内容URL名称只允许字母、数字、横线、下划线组成!');
  105. }
  106. // 自动提起前一百个字符为描述
  107. if (! $description && isset($_POST['content'])) {
  108. $description = escape_string(clear_html_blank(substr_both(strip_tags($_POST['content']), 0, 150)));
  109. }
  110. // 无缩略图时,自动提取文章第一张图为缩略图
  111. if (! $ico && preg_match('/<img\s+.*?src=\s?[\'|\"](.*?(\.gif|\.jpg|\.png|\.jpeg))[\'|\"].*?[\/]?>/i', decode_string($content), $srcs) && isset($srcs[1])) {
  112. $ico = $srcs[1];
  113. }
  114. // 缩放缩略图
  115. if ($ico) {
  116. resize_img(ROOT_PATH . $ico, '', $this->config('ico.max_width'), $this->config('ico.max_height'));
  117. }
  118. // 检查自定义URL名称
  119. if ($filename) {
  120. while ($this->model->checkFilename($filename)) {
  121. $filename = $filename . '-' . mt_rand(1, 20);
  122. }
  123. }
  124. // 记住新增栏目
  125. session('addscode', $scode);
  126. // 构建数据
  127. $data = array(
  128. 'acode' => session('acode'),
  129. 'scode' => $scode,
  130. 'subscode' => $subscode,
  131. 'title' => $title,
  132. 'titlecolor' => $titlecolor,
  133. 'subtitle' => $subtitle,
  134. 'filename' => $filename,
  135. 'author' => $author,
  136. 'source' => $source,
  137. 'outlink' => $outlink,
  138. 'date' => $date,
  139. 'ico' => $ico,
  140. 'pics' => $pics,
  141. 'picstitle' => $picstitle,
  142. 'content' => $content,
  143. 'tags' => $tags,
  144. 'enclosure' => $enclosure,
  145. 'keywords' => $keywords,
  146. 'description' => clear_html_blank($description),
  147. 'sorting' => 255,
  148. 'status' => $status,
  149. 'istop' => $istop,
  150. 'isrecommend' => $isrecommend,
  151. 'isheadline' => $isheadline,
  152. 'gid' => $gid,
  153. 'gtype' => $gtype,
  154. 'gnote' => $gnote,
  155. 'visits' => 0,
  156. 'likes' => 0,
  157. 'oppose' => 0,
  158. 'create_user' => session('username'),
  159. 'update_user' => session('username')
  160. );
  161. // 执行添加
  162. if (! ! $id = $this->model->addContent($data)) {
  163. // 扩展内容添加
  164. foreach ($_POST as $key => $value) {
  165. if (preg_match('/^ext_[\w\-]+$/', $key)) {
  166. if (! isset($data2['contentid'])) {
  167. $data2['contentid'] = $id;
  168. }
  169. $temp = post($key);
  170. if (is_array($temp)) {
  171. $data2[$key] = implode(',', $temp);
  172. } else {
  173. $data2[$key] = str_replace("\r\n", '<br>', $temp);
  174. }
  175. }
  176. }
  177. if (isset($data2)) {
  178. if (! $this->model->addContentExt($data2)) {
  179. $this->model->delContent($id);
  180. $this->log('新增文章失败!');
  181. error('新增失败!', - 1);
  182. }
  183. }
  184. $this->log('新增文章成功!');
  185. if (! ! $backurl = get('backurl')) {
  186. success('新增成功!', base64_decode($backurl));
  187. } else {
  188. success('新增成功!', url('/admin/Content/index/mcode/' . get('mcode')));
  189. }
  190. } else {
  191. $this->log('新增文章失败!');
  192. error('新增失败!', - 1);
  193. }
  194. }
  195. }
  196. // 生成分类选择
  197. private function makeSortSelect($tree, $selectid = null)
  198. {
  199. $list_html = '';
  200. foreach ($tree as $value) {
  201. // 默认选择项
  202. if ($selectid == $value->scode) {
  203. $select = "selected='selected'";
  204. } else {
  205. $select = '';
  206. }
  207. $list_html .= "<option value='{$value->scode}' $select>{$this->blank}{$value->name}";
  208. // 子菜单处理
  209. if ($value->son) {
  210. $this->blank .= '  ';
  211. $list_html .= $this->makeSortSelect($value->son, $selectid);
  212. }
  213. }
  214. // 循环完后回归位置
  215. $this->blank = substr($this->blank, 0, - 6);
  216. return $list_html;
  217. }
  218. // 文章删除
  219. public function del()
  220. {
  221. // 执行批量删除
  222. if ($_POST) {
  223. if (! ! $list = post('list')) {
  224. if ($this->model->delContentList($list)) {
  225. $this->model->delContentExtList($list);
  226. $this->log('批量删除文章成功!');
  227. success('批量删除成功!', - 1);
  228. } else {
  229. $this->log('批量删除文章失败!');
  230. error('批量删除失败!', - 1);
  231. }
  232. } else {
  233. alert_back('请选择要删除的内容!');
  234. }
  235. }
  236. if (! $id = get('id', 'int')) {
  237. error('传递的参数值错误!', - 1);
  238. }
  239. if ($this->model->delContent($id)) {
  240. $this->model->delContentExt($id);
  241. $this->log('删除文章' . $id . '成功!');
  242. success('删除成功!', - 1);
  243. } else {
  244. $this->log('删除文章' . $id . '失败!');
  245. error('删除失败!', - 1);
  246. }
  247. }
  248. // 文章修改
  249. public function mod()
  250. {
  251. if (! ! $submit = post('submit')) {
  252. switch ($submit) {
  253. case 'sorting': // 修改列表排序
  254. $listall = post('listall');
  255. if ($listall) {
  256. $sorting = post('sorting');
  257. foreach ($listall as $key => $value) {
  258. if ($sorting[$key] === '' || ! is_numeric($sorting[$key]))
  259. $sorting[$key] = 255;
  260. $this->model->modContent($value, "sorting=" . $sorting[$key]);
  261. }
  262. $this->log('修改内容排序成功!');
  263. success('修改成功!', - 1);
  264. } else {
  265. alert_back('排序失败,无任何内容!');
  266. }
  267. break;
  268. case 'copy':
  269. $list = post('list');
  270. $scode = post('scode');
  271. if (! $list) {
  272. alert_back('请选择要复制的内容!');
  273. }
  274. if (! $scode) {
  275. alert_back('请选择目标栏目!');
  276. }
  277. if ($this->model->copyContent($list, $scode)) {
  278. $this->log('复制内容成功!');
  279. success('复制内容成功!', - 1);
  280. } else {
  281. alert_back('复制内容失败!');
  282. }
  283. break;
  284. case 'move':
  285. $list = post('list');
  286. $scode = post('scode');
  287. if (! $list) {
  288. alert_back('请选择要移动的内容!');
  289. }
  290. if (! $scode) {
  291. alert_back('请选择目标栏目!');
  292. }
  293. if ($this->model->modContent($list, "scode='" . $scode . "'")) {
  294. $this->log('移动内容成功!');
  295. success('移动内容成功!', - 1);
  296. } else {
  297. alert_back('移动内容失败!');
  298. }
  299. break;
  300. case 'baiduzz':
  301. $list = post('list');
  302. $urls = post('urls');
  303. if (! $list) {
  304. alert_back('请选择要推送的内容!');
  305. }
  306. // 依次推送
  307. $domain = get_http_url();
  308. if (! $token = $this->config('baidu_zz_token')) {
  309. alert_back('请先到系统配置中填写百度普通收录推送token值!');
  310. }
  311. $api = "http://data.zz.baidu.com/urls?site=$domain&token=$token";
  312. foreach ($list as $key => $value) {
  313. $url = $domain . $urls[$value];
  314. $this->log('百度普通收录推送:' . $url);
  315. $post_urls[] = $url;
  316. }
  317. $result = post_baidu($api, $post_urls);
  318. if (isset($result->error)) {
  319. alert_back('百度普通收录推送发生错误:' . $result->message);
  320. } elseif (isset($result->success)) {
  321. alert_back('成功推送' . $result->success . '条,今天剩余可推送' . $result->remain . '条数!');
  322. } else {
  323. alert_back('发生未知错误!');
  324. }
  325. case 'baiduks':
  326. $list = post('list');
  327. $urls = post('urls');
  328. if (! $list) {
  329. alert_back('请选择要推送的内容!');
  330. }
  331. // 依次推送
  332. $domain = get_http_url();
  333. if (! $token = $this->config('baidu_ks_token')) {
  334. alert_back('请先到系统配置中填写百度快速收录推送token值!');
  335. }
  336. $api = "http://data.zz.baidu.com/urls?site=$domain&token=$token&type=daily";
  337. foreach ($list as $key => $value) {
  338. $url = $domain . $urls[$value];
  339. $this->log('百度快速收录推送:' . $url);
  340. $post_urls[] = $url;
  341. }
  342. $result = post_baidu($api, $post_urls);
  343. if (isset($result->error)) {
  344. alert_back('百度快速收录推送发生错误:' . $result->message);
  345. } elseif (isset($result->success_daily)) {
  346. alert_back('成功推送' . $result->success_daily . '条,今天剩余可推送' . $result->remain_daily . '条数!');
  347. } else {
  348. alert_back('发生未知错误!');
  349. }
  350. }
  351. }
  352. if (! $id = get('id', 'int')) {
  353. error('传递的参数值错误!', - 1);
  354. }
  355. // 单独修改状态
  356. if (($field = get('field', 'var')) && ! is_null($value = get('value', 'var'))) {
  357. if ($this->model->modContent($id, "$field='$value',update_user='" . session('username') . "'")) {
  358. location(- 1);
  359. } else {
  360. alert_back('修改失败!');
  361. }
  362. }
  363. // 修改操作
  364. if ($_POST) {
  365. // 获取数据
  366. $scode = post('scode');
  367. $subscode = post('subscode');
  368. $title = post('title');
  369. $titlecolor = post('titlecolor');
  370. $subtitle = post('subtitle');
  371. $filename = trim(post('filename'), '/');
  372. $author = post('author');
  373. $source = post('source');
  374. $outlink = post('outlink');
  375. $date = post('date');
  376. $ico = post('ico');
  377. $pics = post('pics');
  378. // 获取多图标题
  379. $picstitle = post('picstitle');
  380. if ($picstitle) {
  381. $picstitle = implode(',', $picstitle);
  382. }
  383. $content = post('content');
  384. $tags = str_replace(',', ',', post('tags'));
  385. $enclosure = post('enclosure');
  386. $keywords = post('keywords');
  387. $description = post('description');
  388. $status = post('status', 'int');
  389. $istop = post('istop', 'int', '', '', 0);
  390. $isrecommend = post('isrecommend', 'int', '', '', 0);
  391. $isheadline = post('isheadline', 'int', '', '', 0);
  392. $gid = post('gid', 'int') ?: 0;
  393. $gtype = post('gtype', 'int') ?: 4;
  394. $gnote = post('gnote');
  395. if (! $scode) {
  396. alert_back('内容分类不能为空!');
  397. }
  398. if (! $title) {
  399. alert_back('文章标题不能为空!');
  400. }
  401. if ($filename && ! preg_match('/^[a-zA-Z0-9\-_\/]+$/', $filename)) {
  402. alert_back('内容URL名称只允许字母、数字、横线、下划线组成!');
  403. }
  404. // 自动提起前一百个字符为描述
  405. if (! $description && isset($_POST['content'])) {
  406. $description = escape_string(clear_html_blank(substr_both(strip_tags($_POST['content']), 0, 150)));
  407. }
  408. // 无缩略图时,自动提取文章第一张图为缩略图
  409. if (! $ico && preg_match('/<img\s+.*?src=\s?[\'|\"](.*?(\.gif|\.jpg|\.png|\.jpeg))[\'|\"].*?[\/]?>/i', decode_string($content), $srcs) && isset($srcs[1])) {
  410. $ico = $srcs[1];
  411. }
  412. // 缩放缩略图
  413. if ($ico) {
  414. resize_img(ROOT_PATH . $ico, '', $this->config('ico.max_width'), $this->config('ico.max_height'));
  415. }
  416. if ($filename) {
  417. while ($this->model->checkFilename($filename, "id<>$id")) {
  418. $filename = $filename . '-' . mt_rand(1, 20);
  419. }
  420. }
  421. // 构建数据
  422. $data = array(
  423. 'scode' => $scode,
  424. 'subscode' => $subscode,
  425. 'title' => $title,
  426. 'titlecolor' => $titlecolor,
  427. 'subtitle' => $subtitle,
  428. 'filename' => $filename,
  429. 'author' => $author,
  430. 'source' => $source,
  431. 'outlink' => $outlink,
  432. 'date' => $date,
  433. 'ico' => $ico,
  434. 'pics' => $pics,
  435. 'picstitle' => $picstitle,
  436. 'content' => $content,
  437. 'tags' => $tags,
  438. 'enclosure' => $enclosure,
  439. 'keywords' => $keywords,
  440. 'description' => clear_html_blank($description),
  441. 'status' => $status,
  442. 'istop' => $istop,
  443. 'isrecommend' => $isrecommend,
  444. 'isheadline' => $isheadline,
  445. 'gid' => $gid,
  446. 'gtype' => $gtype,
  447. 'gnote' => $gnote,
  448. 'update_user' => session('username')
  449. );
  450. // 执行添加
  451. if ($this->model->modContent($id, $data)) {
  452. // 扩展内容修改
  453. foreach ($_POST as $key => $value) {
  454. if (preg_match('/^ext_[\w\-]+$/', $key)) {
  455. $temp = post($key);
  456. if (is_array($temp)) {
  457. $data2[$key] = implode(',', $temp);
  458. } else {
  459. $data2[$key] = str_replace("\r\n", '<br>', $temp);
  460. }
  461. }
  462. }
  463. if (isset($data2)) {
  464. if ($this->model->findContentExt($id)) {
  465. $this->model->modContentExt($id, $data2);
  466. } else {
  467. $data2['contentid'] = $id;
  468. $this->model->addContentExt($data2);
  469. }
  470. }
  471. $this->log('修改文章' . $id . '成功!');
  472. if (! ! $backurl = get('backurl')) {
  473. success('修改成功!', base64_decode($backurl));
  474. } else {
  475. success('修改成功!', url('/admin/Content/index/mcode/2'));
  476. }
  477. } else {
  478. location(- 1);
  479. }
  480. } else {
  481. // 调取修改内容
  482. $this->assign('mod', true);
  483. if (! $result = $this->model->getContent($id)) {
  484. error('编辑的内容已经不存在!', - 1);
  485. }
  486. $this->assign('content', $result);
  487. if (! $mcode = get('mcode', 'var')) {
  488. error('传递的模型编码参数有误,请核对后重试!');
  489. }
  490. // 文章分类
  491. $sort_model = model('admin.content.ContentSort');
  492. $sort_select = $sort_model->getListSelect($mcode);
  493. $this->assign('sort_select', $this->makeSortSelect($sort_select, $result->scode));
  494. $this->assign('subsort_select', $this->makeSortSelect($sort_select, $result->subscode));
  495. // 模型名称
  496. $this->assign('model_name', model('admin.content.Model')->getName($mcode));
  497. // 扩展字段
  498. $this->assign('extfield', model('admin.content.ExtField')->getModelField($mcode));
  499. // 获取会员分组
  500. $this->assign('groups', model('admin.member.MemberGroup')->getSelect());
  501. $this->display('content/content.html');
  502. }
  503. }
  504. }