Images.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. <?php
  2. /**
  3. * 易优CMS
  4. * ============================================================================
  5. * 版权所有 2016-2028 海南赞赞网络科技有限公司,并保留所有权利。
  6. * 网站地址: http://www.eyoucms.com
  7. * ----------------------------------------------------------------------------
  8. * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
  9. * ============================================================================
  10. * Author: 小虎哥 <1105415366@qq.com>
  11. * Date: 2018-4-3
  12. */
  13. namespace app\admin\controller;
  14. use think\Page;
  15. use think\Db;
  16. class Images extends Base
  17. {
  18. // 模型标识
  19. public $nid = 'images';
  20. // 模型ID
  21. public $channeltype = '';
  22. public function _initialize() {
  23. parent::_initialize();
  24. $channeltype_list = config('global.channeltype_list');
  25. $this->channeltype = $channeltype_list[$this->nid];
  26. empty($this->channeltype) && $this->channeltype = 3;
  27. $this->assign('nid', $this->nid);
  28. $this->assign('channeltype', $this->channeltype);
  29. }
  30. /**
  31. * 列表
  32. */
  33. public function index()
  34. {
  35. $assign_data = array();
  36. $condition = array();
  37. // 获取到所有GET参数
  38. $param = input('param.');
  39. $flag = input('flag/s');
  40. $typeid = input('typeid/d', 0);
  41. $begin = strtotime(input('add_time_begin'));
  42. $end = strtotime(input('add_time_end'));
  43. // 应用搜索条件
  44. foreach (['keywords','typeid','flag','is_release'] as $key) {
  45. if (isset($param[$key]) && $param[$key] !== '') {
  46. if ($key == 'keywords') {
  47. $condition['a.title'] = array('LIKE', "%{$param[$key]}%");
  48. } else if ($key == 'typeid') {
  49. $typeid = $param[$key];
  50. $hasRow = model('Arctype')->getHasChildren($typeid);
  51. $typeids = get_arr_column($hasRow, 'id');
  52. /*权限控制 by 小虎哥*/
  53. $admin_info = session('admin_info');
  54. if (0 < intval($admin_info['role_id'])) {
  55. $auth_role_info = $admin_info['auth_role_info'];
  56. if(! empty($auth_role_info)){
  57. if(! empty($auth_role_info['permission']['arctype'])){
  58. if (!empty($typeid)) {
  59. $typeids = array_intersect($typeids, $auth_role_info['permission']['arctype']);
  60. }
  61. }
  62. }
  63. }
  64. /*--end*/
  65. $condition['a.typeid'] = array('IN', $typeids);
  66. } else if ($key == 'flag') {
  67. if ('is_release' == $param[$key]) {
  68. $condition['a.users_id'] = array('gt', 0);
  69. } else {
  70. $condition['a.'.$param[$key]] = array('eq', 1);
  71. }
  72. // } else if ($key == 'is_release') {
  73. // if (0 < intval($param[$key])) {
  74. // $condition['a.users_id'] = array('gt', intval($param[$key]));
  75. // }
  76. } else {
  77. $condition['a.'.$key] = array('eq', $param[$key]);
  78. }
  79. }
  80. }
  81. /*权限控制 by 小虎哥*/
  82. $admin_info = session('admin_info');
  83. if (0 < intval($admin_info['role_id'])) {
  84. $auth_role_info = $admin_info['auth_role_info'];
  85. if(! empty($auth_role_info)){
  86. if(isset($auth_role_info['only_oneself']) && 1 == $auth_role_info['only_oneself']){
  87. $condition['a.admin_id'] = $admin_info['admin_id'];
  88. }
  89. }
  90. }
  91. /*--end*/
  92. // 时间检索
  93. if ($begin > 0 && $end > 0) {
  94. $condition['a.add_time'] = array('between',"$begin,$end");
  95. } else if ($begin > 0) {
  96. $condition['a.add_time'] = array('egt', $begin);
  97. } else if ($end > 0) {
  98. $condition['a.add_time'] = array('elt', $end);
  99. }
  100. // 模型ID
  101. $condition['a.channel'] = array('eq', $this->channeltype);
  102. // 多语言
  103. $condition['a.lang'] = array('eq', $this->admin_lang);
  104. // 回收站
  105. $condition['a.is_del'] = array('eq', 0);
  106. /*自定义排序*/
  107. $orderby = input('param.orderby/s');
  108. $orderway = input('param.orderway/s');
  109. if (!empty($orderby)) {
  110. $orderby = "a.{$orderby} {$orderway}";
  111. $orderby .= ", a.aid desc";
  112. } else {
  113. $orderby = "a.aid desc";
  114. }
  115. /*end*/
  116. /**
  117. * 数据查询,搜索出主键ID的值
  118. */
  119. $count = DB::name('archives')->alias('a')->where($condition)->count('aid');// 查询满足要求的总记录数
  120. $Page = new Page($count, config('paginate.list_rows'));// 实例化分页类 传入总记录数和每页显示的记录数
  121. $list = DB::name('archives')
  122. ->field("a.aid")
  123. ->alias('a')
  124. ->where($condition)
  125. ->order($orderby)
  126. ->limit($Page->firstRow.','.$Page->listRows)
  127. ->getAllWithIndex('aid');
  128. /**
  129. * 完善数据集信息
  130. * 在数据量大的情况下,经过优化的搜索逻辑,先搜索出主键ID,再通过ID将其他信息补充完整;
  131. */
  132. if ($list) {
  133. $aids = array_keys($list);
  134. $fields = "b.*, a.*, a.aid as aid";
  135. $row = DB::name('archives')
  136. ->field($fields)
  137. ->alias('a')
  138. ->join('__ARCTYPE__ b', 'a.typeid = b.id', 'LEFT')
  139. ->where('a.aid', 'in', $aids)
  140. ->getAllWithIndex('aid');
  141. foreach ($list as $key => $val) {
  142. $row[$val['aid']]['arcurl'] = get_arcurl($row[$val['aid']]);
  143. $row[$val['aid']]['litpic'] = handle_subdir_pic($row[$val['aid']]['litpic']); // 支持子目录
  144. $list[$key] = $row[$val['aid']];
  145. }
  146. }
  147. $show = $Page->show(); // 分页显示输出
  148. $assign_data['page'] = $show; // 赋值分页输出
  149. $assign_data['list'] = $list; // 赋值数据集
  150. $assign_data['pager'] = $Page; // 赋值分页对象
  151. // 栏目ID
  152. $assign_data['typeid'] = $typeid; // 栏目ID
  153. /*当前栏目信息*/
  154. $arctype_info = array();
  155. if ($typeid > 0) {
  156. $arctype_info = M('arctype')->field('typename')->find($typeid);
  157. }
  158. $assign_data['arctype_info'] = $arctype_info;
  159. /*--end*/
  160. /*选项卡*/
  161. $tab = input('param.tab/d', 3);
  162. $assign_data['tab'] = $tab;
  163. /*--end*/
  164. $this->assign($assign_data);
  165. return $this->fetch();
  166. }
  167. /**
  168. * 添加
  169. */
  170. public function add()
  171. {
  172. if (IS_POST) {
  173. $post = input('post.');
  174. $content = input('post.addonFieldExt.content', '', null);
  175. // 根据标题自动提取相关的关键字
  176. $seo_keywords = $post['seo_keywords'];
  177. if (!empty($seo_keywords)) {
  178. $seo_keywords = str_replace(',', ',', $seo_keywords);
  179. } else {
  180. // $seo_keywords = get_split_word($post['title'], $content);
  181. }
  182. // 自动获取内容第一张图片作为封面图
  183. $is_remote = !empty($post['is_remote']) ? $post['is_remote'] : 0;
  184. $litpic = '';
  185. if ($is_remote == 1) {
  186. $litpic = $post['litpic_remote'];
  187. } else {
  188. $litpic = $post['litpic_local'];
  189. }
  190. if (empty($litpic)) {
  191. $litpic = get_html_first_imgurl($content);
  192. }
  193. $post['litpic'] = $litpic;
  194. /*是否有封面图*/
  195. if (empty($post['litpic'])) {
  196. $is_litpic = 0; // 无封面图
  197. } else {
  198. $is_litpic = 1; // 有封面图
  199. }
  200. // SEO描述
  201. $seo_description = '';
  202. if (empty($post['seo_description']) && !empty($content)) {
  203. $seo_description = @msubstr(checkStrHtml($content), 0, config('global.arc_seo_description_length'), false);
  204. } else {
  205. $seo_description = $post['seo_description'];
  206. }
  207. // 外部链接跳转
  208. $jumplinks = '';
  209. $is_jump = isset($post['is_jump']) ? $post['is_jump'] : 0;
  210. if (intval($is_jump) > 0) {
  211. $jumplinks = $post['jumplinks'];
  212. }
  213. // 模板文件,如果文档模板名与栏目指定的一致,默认就为空。让它跟随栏目的指定而变
  214. if ($post['type_tempview'] == $post['tempview']) {
  215. unset($post['type_tempview']);
  216. unset($post['tempview']);
  217. }
  218. //处理自定义文件名,仅由字母数字下划线和短横杆组成,大写强制转换为小写
  219. if (!empty($post['htmlfilename'])) {
  220. $post['htmlfilename'] = preg_replace("/[^a-zA-Z0-9_-]+/", "", $post['htmlfilename']);
  221. $post['htmlfilename'] = strtolower($post['htmlfilename']);
  222. //判断是否存在相同的自定义文件名
  223. $filenameCount = Db::name('archives')->where('htmlfilename', $post['htmlfilename'])->count();
  224. if (!empty($filenameCount)) {
  225. $this->error("自定义文件名已存在,请重新设置!");
  226. }
  227. }
  228. // --存储数据
  229. $newData = array(
  230. 'typeid'=> empty($post['typeid']) ? 0 : $post['typeid'],
  231. 'channel' => $this->channeltype,
  232. 'is_b' => empty($post['is_b']) ? 0 : $post['is_b'],
  233. 'is_head' => empty($post['is_head']) ? 0 : $post['is_head'],
  234. 'is_special' => empty($post['is_special']) ? 0 : $post['is_special'],
  235. 'is_recom' => empty($post['is_recom']) ? 0 : $post['is_recom'],
  236. 'is_jump' => $is_jump,
  237. 'is_litpic' => $is_litpic,
  238. 'jumplinks' => $jumplinks,
  239. 'seo_keywords' => $seo_keywords,
  240. 'seo_description' => $seo_description,
  241. 'admin_id' => session('admin_info.admin_id'),
  242. 'lang' => $this->admin_lang,
  243. 'sort_order' => 100,
  244. 'add_time' => strtotime($post['add_time']),
  245. 'update_time' => strtotime($post['add_time']),
  246. );
  247. $data = array_merge($post, $newData);
  248. $aid = Db::name('archives')->insertGetId($data);
  249. $_POST['aid'] = $aid;
  250. if ($aid) {
  251. // ---------后置操作
  252. model('Images')->afterSave($aid, $data, 'add');
  253. // ---------end
  254. adminLog('新增图集:'.$data['title']);
  255. // 生成静态页面代码
  256. $successData = [
  257. 'aid' => $aid,
  258. 'tid' => $post['typeid'],
  259. ];
  260. $this->success("操作成功!", null, $successData);
  261. exit;
  262. }
  263. $this->error("操作失败!");
  264. exit;
  265. }
  266. $typeid = input('param.typeid/d', 0);
  267. $assign_data['typeid'] = $typeid; // 栏目ID
  268. // 栏目信息
  269. $arctypeInfo = Db::name('arctype')->find($typeid);
  270. /*允许发布文档列表的栏目*/
  271. $arctype_html = allow_release_arctype($typeid, array($this->channeltype));
  272. $assign_data['arctype_html'] = $arctype_html;
  273. /*--end*/
  274. /*自定义字段*/
  275. $addonFieldExtList = model('Field')->getChannelFieldList($this->channeltype);
  276. $channelfieldBindRow = Db::name('channelfield_bind')->where([
  277. 'typeid' => ['IN', [0,$typeid]],
  278. ])->column('field_id');
  279. if (!empty($channelfieldBindRow)) {
  280. foreach ($addonFieldExtList as $key => $val) {
  281. if (!in_array($val['id'], $channelfieldBindRow)) {
  282. unset($addonFieldExtList[$key]);
  283. }
  284. }
  285. }
  286. $assign_data['addonFieldExtList'] = $addonFieldExtList;
  287. $assign_data['aid'] = 0;
  288. /*--end*/
  289. // 阅读权限
  290. $arcrank_list = get_arcrank_list();
  291. $assign_data['arcrank_list'] = $arcrank_list;
  292. /*模板列表*/
  293. $archivesLogic = new \app\admin\logic\ArchivesLogic;
  294. $templateList = $archivesLogic->getTemplateList($this->nid);
  295. $this->assign('templateList', $templateList);
  296. /*--end*/
  297. /*默认模板文件*/
  298. $tempview = 'view_'.$this->nid.'.'.config('template.view_suffix');
  299. !empty($arctypeInfo['tempview']) && $tempview = $arctypeInfo['tempview'];
  300. $this->assign('tempview', $tempview);
  301. /*--end*/
  302. // URL模式
  303. $tpcache = config('tpcache');
  304. $assign_data['seo_pseudo'] = !empty($tpcache['seo_pseudo']) ? $tpcache['seo_pseudo'] : 1;
  305. $this->assign($assign_data);
  306. return $this->fetch();
  307. }
  308. /**
  309. * 编辑
  310. */
  311. public function edit()
  312. {
  313. if (IS_POST) {
  314. $post = input('post.');
  315. $typeid = input('post.typeid/d', 0);
  316. $content = input('post.addonFieldExt.content', '', null);
  317. // 根据标题自动提取相关的关键字
  318. $seo_keywords = $post['seo_keywords'];
  319. if (!empty($seo_keywords)) {
  320. $seo_keywords = str_replace(',', ',', $seo_keywords);
  321. } else {
  322. // $seo_keywords = get_split_word($post['title'], $content);
  323. }
  324. // 自动获取内容第一张图片作为封面图
  325. $is_remote = !empty($post['is_remote']) ? $post['is_remote'] : 0;
  326. $litpic = '';
  327. if ($is_remote == 1) {
  328. $litpic = $post['litpic_remote'];
  329. } else {
  330. $litpic = $post['litpic_local'];
  331. }
  332. if (empty($litpic)) {
  333. $litpic = get_html_first_imgurl($content);
  334. }
  335. $post['litpic'] = $litpic;
  336. /*是否有封面图*/
  337. if (empty($post['litpic'])) {
  338. $is_litpic = 0; // 无封面图
  339. } else {
  340. $is_litpic = !empty($post['is_litpic']) ? $post['is_litpic'] : 0; // 有封面图
  341. }
  342. // SEO描述
  343. $seo_description = '';
  344. if (empty($post['seo_description']) && !empty($content)) {
  345. $seo_description = @msubstr(checkStrHtml($content), 0, config('global.arc_seo_description_length'), false);
  346. } else {
  347. $seo_description = $post['seo_description'];
  348. }
  349. // --外部链接
  350. $jumplinks = '';
  351. $is_jump = isset($post['is_jump']) ? $post['is_jump'] : 0;
  352. if (intval($is_jump) > 0) {
  353. $jumplinks = $post['jumplinks'];
  354. }
  355. // 模板文件,如果文档模板名与栏目指定的一致,默认就为空。让它跟随栏目的指定而变
  356. if ($post['type_tempview'] == $post['tempview']) {
  357. unset($post['type_tempview']);
  358. unset($post['tempview']);
  359. }
  360. //处理自定义文件名,仅由字母数字下划线和短横杆组成,大写强制转换为小写
  361. if (!empty($post['htmlfilename'])) {
  362. $post['htmlfilename'] = preg_replace("/[^a-zA-Z0-9_-]+/", "", $post['htmlfilename']);
  363. $post['htmlfilename'] = strtolower($post['htmlfilename']);
  364. //判断是否存在相同的自定义文件名
  365. $filenameCount = Db::name('archives')->where([
  366. 'aid' => ['NEQ', $post['aid']],
  367. 'htmlfilename' => $post['htmlfilename'],
  368. ])->count();
  369. if (!empty($filenameCount)) {
  370. $this->error("自定义文件名已存在,请重新设置!");
  371. }
  372. }
  373. // 同步栏目切换模型之后的文档模型
  374. $channel = Db::name('arctype')->where(['id'=>$typeid])->getField('current_channel');
  375. // --存储数据
  376. $newData = array(
  377. 'typeid'=> $typeid,
  378. 'channel' => $channel,
  379. 'is_b' => empty($post['is_b']) ? 0 : $post['is_b'],
  380. 'is_head' => empty($post['is_head']) ? 0 : $post['is_head'],
  381. 'is_special' => empty($post['is_special']) ? 0 : $post['is_special'],
  382. 'is_recom' => empty($post['is_recom']) ? 0 : $post['is_recom'],
  383. 'is_jump' => $is_jump,
  384. 'is_litpic' => $is_litpic,
  385. 'jumplinks' => $jumplinks,
  386. 'seo_keywords' => $seo_keywords,
  387. 'seo_description' => $seo_description,
  388. 'add_time' => strtotime($post['add_time']),
  389. 'update_time' => getTime(),
  390. );
  391. $data = array_merge($post, $newData);
  392. $r = Db::name('archives')->where([
  393. 'aid' => $data['aid'],
  394. 'lang' => $this->admin_lang,
  395. ])->update($data);
  396. if ($r) {
  397. // ---------后置操作
  398. model('Images')->afterSave($data['aid'], $data, 'edit');
  399. // ---------end
  400. adminLog('编辑图集:'.$data['title']);
  401. // 生成静态页面代码
  402. $successData = [
  403. 'aid' => $data['aid'],
  404. 'tid' => $typeid,
  405. ];
  406. $this->success("操作成功!", null, $successData);
  407. exit;
  408. }
  409. $this->error("操作失败!");
  410. exit;
  411. }
  412. $assign_data = array();
  413. $id = input('id/d');
  414. $info = model('Images')->getInfo($id);
  415. if (empty($info)) {
  416. $this->error('数据不存在,请联系管理员!');
  417. exit;
  418. }
  419. /*兼容采集没有归属栏目的文档*/
  420. if (empty($info['channel'])) {
  421. $channelRow = Db::name('channeltype')->field('id as channel')
  422. ->where('id',$this->channeltype)
  423. ->find();
  424. $info = array_merge($info, $channelRow);
  425. }
  426. /*--end*/
  427. $typeid = $info['typeid'];
  428. // 栏目信息
  429. $arctypeInfo = Db::name('arctype')->find($typeid);
  430. $info['channel'] = $arctypeInfo['current_channel'];
  431. if (is_http_url($info['litpic'])) {
  432. $info['is_remote'] = 1;
  433. $info['litpic_remote'] = handle_subdir_pic($info['litpic']);
  434. } else {
  435. $info['is_remote'] = 0;
  436. $info['litpic_local'] = handle_subdir_pic($info['litpic']);
  437. }
  438. // SEO描述
  439. if (!empty($info['seo_description'])) {
  440. $info['seo_description'] = @msubstr(checkStrHtml($info['seo_description']), 0, config('global.arc_seo_description_length'), false);
  441. }
  442. $assign_data['field'] = $info;
  443. // 图集相册
  444. $imgupload_list = model('ImagesUpload')->getImgUpload($id);
  445. foreach ($imgupload_list as $key => $val) {
  446. $imgupload_list[$key]['image_url'] = handle_subdir_pic($val['image_url']); // 支持子目录
  447. }
  448. $assign_data['imgupload_list'] = $imgupload_list;
  449. /*允许发布文档列表的栏目,文档所在模型以栏目所在模型为主,兼容切换模型之后的数据编辑*/
  450. $arctype_html = allow_release_arctype($typeid, array($info['channel']));
  451. $assign_data['arctype_html'] = $arctype_html;
  452. /*--end*/
  453. /*自定义字段*/
  454. $addonFieldExtList = model('Field')->getChannelFieldList($info['channel'], 0, $id, $info);
  455. $channelfieldBindRow = Db::name('channelfield_bind')->where([
  456. 'typeid' => ['IN', [0,$typeid]],
  457. ])->column('field_id');
  458. if (!empty($channelfieldBindRow)) {
  459. foreach ($addonFieldExtList as $key => $val) {
  460. if (!in_array($val['id'], $channelfieldBindRow)) {
  461. unset($addonFieldExtList[$key]);
  462. }
  463. }
  464. }
  465. $assign_data['addonFieldExtList'] = $addonFieldExtList;
  466. $assign_data['aid'] = $id;
  467. /*--end*/
  468. // 阅读权限
  469. $arcrank_list = get_arcrank_list();
  470. $assign_data['arcrank_list'] = $arcrank_list;
  471. /*模板列表*/
  472. $archivesLogic = new \app\admin\logic\ArchivesLogic;
  473. $templateList = $archivesLogic->getTemplateList($this->nid);
  474. $this->assign('templateList', $templateList);
  475. /*--end*/
  476. /*默认模板文件*/
  477. $tempview = $info['tempview'];
  478. empty($tempview) && $tempview = $arctypeInfo['tempview'];
  479. $this->assign('tempview', $tempview);
  480. /*--end*/
  481. // URL模式
  482. $tpcache = config('tpcache');
  483. $assign_data['seo_pseudo'] = !empty($tpcache['seo_pseudo']) ? $tpcache['seo_pseudo'] : 1;
  484. $this->assign($assign_data);
  485. return $this->fetch();
  486. }
  487. /**
  488. * 删除
  489. */
  490. public function del()
  491. {
  492. if (IS_POST) {
  493. $archivesLogic = new \app\admin\logic\ArchivesLogic;
  494. $archivesLogic->del();
  495. }
  496. }
  497. /**
  498. * 删除图集相册图
  499. */
  500. public function del_imgupload()
  501. {
  502. if (IS_POST) {
  503. $filename= input('filename/s');
  504. $filename= str_replace('../','',$filename);
  505. $filename= trim($filename,'.');
  506. if(eyPreventShell($filename) && !empty($filename)){
  507. $filename_new = trim($filename,'/');
  508. $filetype = preg_replace('/^(.*)\.(\w+)$/i', '$2', $filename);
  509. $phpfile = strtolower(strstr($filename,'.php')); //排除PHP文件
  510. $size = getimagesize($filename_new);
  511. $fileInfo = explode('/',$size['mime']);
  512. if((file_exists($filename_new) && $fileInfo[0] != 'image') || $phpfile || !in_array($filetype, explode(',', config('global.image_ext')))){
  513. exit;
  514. }
  515. if (!empty($filename)) {
  516. M('images_upload')->where("image_url = '$filename'")->delete();
  517. }
  518. }
  519. }
  520. }
  521. }