Seo.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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\Db;
  15. use think\Cache;
  16. use app\common\logic\ArctypeLogic;
  17. use think\paginator\driver; // 生成静态页面代码
  18. class Seo extends Base
  19. {
  20. public function _initialize() {
  21. parent::_initialize();
  22. $this->language_access(); // 多语言功能操作权限
  23. }
  24. /*
  25. * 生成总站
  26. */
  27. public function site(){
  28. return $this->fetch();
  29. }
  30. /*
  31. * 生成栏目页
  32. */
  33. public function channel(){
  34. $typeid = input('param.typeid/d','0'); // 栏目ID
  35. $this->assign("typeid",$typeid);
  36. return $this->fetch();
  37. }
  38. /*
  39. * 生成文档页
  40. */
  41. public function article()
  42. {
  43. $typeid = input('param.typeid/d','0'); // 栏目ID
  44. $this->assign("typeid",$typeid);
  45. return $this->fetch();
  46. }
  47. /*
  48. * URL配置
  49. */
  50. public function seo()
  51. {
  52. /* 纠正栏目的HTML目录路径字段值 */
  53. $this->correctArctypeDirpath();
  54. /* end */
  55. $inc_type = 'seo';
  56. $config = tpCache($inc_type);
  57. $config['seo_pseudo'] = tpCache('seo.seo_pseudo');
  58. $seo_pseudo_list = get_seo_pseudo_list();
  59. $this->assign('seo_pseudo_list', $seo_pseudo_list);
  60. /* 生成静态页面代码 - 多语言统一设置URL模式 */
  61. $seo_pseudo_lang = '';
  62. $web_language_switch = tpCache('web.web_language_switch');
  63. if (is_language() && !empty($web_language_switch)) {
  64. $markArr = Db::name('language')->field('mark')->order('id asc')->limit('1,1')->select();
  65. if (!empty($markArr[0]['mark'])) {
  66. $seo_pseudo_lang = tpCache('seo.seo_pseudo', [], $markArr[0]['mark']);
  67. }
  68. $seo_pseudo_lang = !empty($seo_pseudo_lang) ? $seo_pseudo_lang : 1;
  69. }
  70. $this->assign('seo_pseudo_lang', $seo_pseudo_lang);
  71. /* end */
  72. /* 限制文档HTML保存路径的名称 */
  73. $wwwroot_dir = config('global.wwwroot_dir'); // 网站根目录的目录列表
  74. $disable_dirname = config('global.disable_dirname'); // 栏目伪静态时的路由路径
  75. $wwwroot_dir = array_merge($wwwroot_dir, $disable_dirname);
  76. // 不能与栏目的一级目录名称重复
  77. $arctypeDirnames = Db::name('arctype')->where(['parent_id'=>0])->column('dirname');
  78. is_array($arctypeDirnames) && $wwwroot_dir = array_merge($wwwroot_dir, $arctypeDirnames);
  79. // 不能与多语言的标识重复
  80. $markArr = Db::name('language_mark')->column('mark');
  81. is_array($markArr) && $wwwroot_dir = array_merge($wwwroot_dir, $markArr);
  82. $wwwroot_dir = array_unique($wwwroot_dir);
  83. $this->assign('seo_html_arcdir_limit', implode(',', $wwwroot_dir));
  84. /* end */
  85. $seo_html_arcdir_1 = '';
  86. if (!empty($config['seo_html_arcdir'])) {
  87. $config['seo_html_arcdir'] = trim($config['seo_html_arcdir'], '/');
  88. $seo_html_arcdir_1 = '/'.$config['seo_html_arcdir'];
  89. }
  90. $this->assign('seo_html_arcdir_1', $seo_html_arcdir_1);
  91. // 栏目列表
  92. $map = array(
  93. 'status' => 1,
  94. 'is_del' => 0, // 回收站功能
  95. );
  96. $arctypeLogic = new ArctypeLogic();
  97. $select_html = $arctypeLogic->arctype_list(0, 0, true, config('global.arctype_max_level'), $map);
  98. $this->assign('select_html',$select_html);
  99. // 允许发布文档列表的栏目
  100. $arc_select_html = allow_release_arctype();
  101. $this->assign('arc_select_html', $arc_select_html);
  102. // 生成完页面之后,清除缓存
  103. $this->buildhtml_clear_cache();
  104. /*标记是否第一次切换静态页面模式*/
  105. if (!isset($config['seo_html_arcdir'])) {
  106. $init_html = 1; // 第一次切换
  107. } else {
  108. $init_html = 2; // 多次切换
  109. }
  110. $this->assign('init_html', $init_html);
  111. /*--end*/
  112. $this->assign('config',$config);//当前配置项
  113. return $this->fetch();
  114. }
  115. /*
  116. * 保存URL配置
  117. */
  118. public function handle()
  119. {
  120. if (IS_POST) {
  121. $inc_type = 'seo';
  122. $param = input('post.');
  123. $globalConfig = tpCache('global');
  124. $seo_pseudo_new = $param['seo_pseudo'];
  125. /* 生成静态页面代码 */
  126. unset($param['seo_html_arcdir_limit']);
  127. if (!empty($param['seo_html_arcdir']) && !preg_match('/^([0-9a-zA-Z\_\-\/]+)$/i', $param['seo_html_arcdir'])) {
  128. $this->error('页面保存路径的格式错误!');
  129. }
  130. if (!empty($param['seo_html_arcdir'])) {
  131. if (preg_match('/^([0-9a-zA-Z\_\-\/]+)$/i', $param['seo_html_arcdir'])) {
  132. // $param['seo_html_arcdir'] = ROOT_DIR.'/'.trim($param['seo_html_arcdir'], '/');
  133. $param['seo_html_arcdir'] = '/'.trim($param['seo_html_arcdir'], '/');
  134. } else {
  135. $this->error('页面保存路径的格式错误!');
  136. }
  137. }
  138. $seo_html_arcdir_old = !empty($globalConfig['seo_html_arcdir']) ? $globalConfig['seo_html_arcdir'] : '';
  139. /* end */
  140. /*检测是否开启pathinfo模式*/
  141. try {
  142. if (3 == $seo_pseudo_new || (1 == $seo_pseudo_new && 2 == $param['seo_dynamic_format'])) {
  143. $fix_pathinfo = ini_get('cgi.fix_pathinfo');
  144. if (stristr($_SERVER['HTTP_HOST'], '.mylightsite.com')) {
  145. $this->error('腾讯云空间不支持伪静态!');
  146. } else if ('' != $fix_pathinfo && 0 === $fix_pathinfo) {
  147. $this->error('空间不支持伪静态,请开启pathinfo,或者在php.ini里修改cgi.fix_pathinfo=1');
  148. }
  149. }
  150. /* 生成静态页面代码 - URL模式切换时删掉根目录下的index.html静态文件 */
  151. if(1 == $seo_pseudo_new || 3 == $seo_pseudo_new){
  152. if(file_exists('./index.html')){
  153. @unlink('./index.html');
  154. }
  155. }
  156. /* end */
  157. } catch (\Exception $e) {}
  158. /*--end*/
  159. /*强制去除index.php*/
  160. if (isset($param['seo_force_inlet'])) {
  161. $seo_force_inlet = $param['seo_force_inlet'];
  162. $seo_force_inlet_old = !empty($globalConfig['seo_force_inlet']) ? $globalConfig['seo_force_inlet'] : '';
  163. if ($seo_force_inlet_old != $seo_force_inlet) {
  164. $param['seo_inlet'] = $seo_force_inlet;
  165. }
  166. }
  167. /*--end*/
  168. /*多语言*/
  169. if (is_language()) {
  170. $seo_pseudo_lang = !empty($param['seo_pseudo_lang']) ? $param['seo_pseudo_lang'] : 1;
  171. unset($param['seo_pseudo_lang']);
  172. $langRow = \think\Db::name('language')->order('id asc')
  173. ->cache(true, EYOUCMS_CACHE_TIME, 'language')
  174. ->select();
  175. foreach ($langRow as $key => $val) {
  176. if (2 != $seo_pseudo_new) { // 非生成静态模式下,所有语言的URL模式一致
  177. tpCache($inc_type,$param,$val['mark']);
  178. } else {
  179. if($key == 0){ // 主体语言(第一个语言)是生成静态模式
  180. tpCache($inc_type,$param,$val['mark']);
  181. }else{//其他语言统一设置URL模式非静态模式
  182. $param['seo_pseudo'] = $seo_pseudo_lang;
  183. tpCache($inc_type,$param,$val['mark']);
  184. }
  185. }
  186. }
  187. } else {
  188. tpCache($inc_type,$param);
  189. }
  190. /*--end*/
  191. /* 生成静态页面代码 - 更新分页php文件支持生成静态功能*/
  192. $this->update_paginatorfile();
  193. /* end */
  194. // 清空缓存
  195. delFile(rtrim(HTML_ROOT, '/'));
  196. \think\Cache::clear();
  197. $this->success('操作成功', url('Seo/seo'));
  198. }
  199. $this->error('操作失败');
  200. }
  201. /**
  202. * 生成静态页面代码 - 更新分页php文件支持生成静态功能
  203. */
  204. private function update_paginatorfile()
  205. {
  206. $dirpath = CORE_PATH . 'paginator/driver/*.php';
  207. $files = glob($dirpath);
  208. foreach ($files as $key => $file) {
  209. if (is_writable($file)) {
  210. $strContent = @file_get_contents($file);
  211. if (false != $strContent && !stristr($strContent, 'data-ey_fc35fdc="html"')) {
  212. $replace = 'htmlentities($url) . \'" data-ey_fc35fdc="html" data-tmp="1\'';
  213. $strContent = str_replace('htmlentities($url)', $replace, $strContent);
  214. @chmod($file,0777);
  215. @file_put_contents($file, $strContent);
  216. }
  217. }
  218. }
  219. }
  220. /*
  221. * 生成整站静态文件
  222. */
  223. public function buildSite(){
  224. $type = input("param.type/s");
  225. if($type != 'site'){
  226. $this->error('操作失败');
  227. }
  228. $this->success('操作成功');
  229. }
  230. /*
  231. * 获取生成栏目或文章的栏目id
  232. */
  233. public function getAllType(){
  234. $id = input("param.id/d");//栏目id
  235. $type = input("param.type/d");//1栏目2文章
  236. if(empty($id)) {
  237. if($id == 0){
  238. $mark = Db::name('language')->order('id asc')->value('mark');
  239. if($type == 1){
  240. $arctype = Db::name('arctype')->where(['is_del'=>0,'status'=>1,'lang'=>$mark])->getfield('id',true);
  241. }else{
  242. $where['is_del'] = 0;
  243. $where['status'] = 1;
  244. $where['lang'] = $mark;
  245. $where['current_channel'] = array(array('neq',6),array('neq',8));
  246. $arctype = Db::name('arctype')->where($where)->getfield('id',true);
  247. }
  248. if(empty($arctype)){
  249. $this->error('没有要更新的栏目!');
  250. }else{
  251. $arctype = implode(',',$arctype);
  252. $this->success($arctype);
  253. }
  254. }else{
  255. $this->error('栏目ID不能为空!');
  256. }
  257. }else{
  258. //递归查询所有的子类
  259. $arctype_child_all = array($id);
  260. getAllChild($arctype_child_all,$id,$type);
  261. $arctype_child_all = implode(',',$arctype_child_all);
  262. if(empty($arctype_child_all)) {
  263. $this->error('没有要更新的栏目!');
  264. }else{
  265. $this->success($arctype_child_all);
  266. }
  267. }
  268. }
  269. /**
  270. * 纠正栏目的HTML目录路径字段值
  271. */
  272. private function correctArctypeDirpath()
  273. {
  274. $system_correctArctypeDirpath = tpCache('system.system_correctArctypeDirpath');
  275. if (!empty($system_correctArctypeDirpath)) {
  276. return false;
  277. }
  278. $saveData = [];
  279. $arctypeList = Db::name('arctype')->field('id,parent_id,dirname,dirpath,grade')
  280. ->order('grade asc')
  281. ->getAllWithIndex('id');
  282. foreach ($arctypeList as $key => $val) {
  283. if (empty($val['parent_id'])) { // 一级栏目
  284. $saveData[] = [
  285. 'id' => $val['id'],
  286. 'dirpath' => '/'.$val['dirname'],
  287. 'grade' => 0,
  288. 'update_time' => getTime(),
  289. ];
  290. } else {
  291. $parentRow = $arctypeList[$val['parent_id']];
  292. if (empty($parentRow['parent_id'])) { // 二级栏目
  293. $saveData[] = [
  294. 'id' => $val['id'],
  295. 'dirpath' => '/'.$parentRow['dirname'].'/'.$val['dirname'],
  296. 'grade' => 1,
  297. 'update_time' => getTime(),
  298. ];
  299. } else { // 三级栏目
  300. $topRow = $arctypeList[$parentRow['parent_id']];
  301. $saveData[] = [
  302. 'id' => $val['id'],
  303. 'dirpath' => '/'.$topRow['dirname'].'/'.$parentRow['dirname'].'/'.$val['dirname'],
  304. 'grade' => 2,
  305. 'update_time' => getTime(),
  306. ];
  307. }
  308. }
  309. }
  310. $r = model('Arctype')->saveAll($saveData);
  311. if (false !== $r) {
  312. /*多语言*/
  313. if (is_language()) {
  314. $langRow = \think\Db::name('language')->order('id asc')
  315. ->cache(true, EYOUCMS_CACHE_TIME, 'language')
  316. ->select();
  317. foreach ($langRow as $key => $val) {
  318. tpCache('system', ['system_correctArctypeDirpath'=>1],$val['mark']);
  319. }
  320. } else {
  321. tpCache('system',['system_correctArctypeDirpath'=>1]);
  322. }
  323. /*--end*/
  324. }
  325. }
  326. /**
  327. * 静态页面模式切换为其他模式时,检测之前生成的静态目录是否存在,并提示手工删除还是自动删除
  328. */
  329. public function ajax_checkHtmlDirpath()
  330. {
  331. $seo_pseudo_new = input('param.seo_pseudo_new/d');
  332. if (3 == $seo_pseudo_new) {
  333. $dirArr = [];
  334. $seo_html_listname = tpCache('seo.seo_html_listname');
  335. $row = Db::name('arctype')->field('dirpath')->select();
  336. foreach ($row as $key => $val) {
  337. $dirpathArr = explode('/', $val['dirpath']);
  338. if (3 == $seo_html_listname) {
  339. $dir = end($dirpathArr);
  340. } else {
  341. $dir = !empty($dirpathArr[1]) ? $dirpathArr[1] : '';
  342. }
  343. if (!empty($dir) && !in_array($dir, $dirArr)) {
  344. array_push($dirArr, $dir);
  345. }
  346. }
  347. $data = [];
  348. $data['msg'] = '';
  349. $num = 0;
  350. $wwwroot = glob('*', GLOB_ONLYDIR);
  351. foreach ($wwwroot as $key => $val) {
  352. if (in_array($val, $dirArr)) {
  353. if (0 == $num) {
  354. $data['msg'] .= "<font color='red'>根目录下有HTML静态目录,请先删除:</font><br/>";
  355. }
  356. $data['msg'] .= ($num+1)."、{$val}<br/>";
  357. $num++;
  358. }
  359. }
  360. $data['height'] = $num * 24;
  361. $this->success('检测成功!', null, $data);
  362. }
  363. }
  364. /**
  365. * 自动删除静态HTML存放目录
  366. */
  367. public function ajax_delHtmlDirpath()
  368. {
  369. if (IS_AJAX_POST) {
  370. $error = false;
  371. $dirArr = [];
  372. $seo_html_listname = tpCache('seo.seo_html_listname');
  373. $row = Db::name('arctype')->field('dirpath')->select();
  374. foreach ($row as $key => $val) {
  375. $dirpathArr = explode('/', $val['dirpath']);
  376. if (3 == $seo_html_listname) {
  377. $dir = end($dirpathArr);
  378. } else {
  379. $dir = !empty($dirpathArr[1]) ? $dirpathArr[1] : '';
  380. }
  381. $filepath = "./{$dir}";
  382. if (!empty($dir) && !in_array($dir, $dirArr) && file_exists($filepath)) {
  383. @unlink($filepath."/index.html");
  384. $bool = delFile($filepath, true);
  385. if (false !== $bool) {
  386. array_push($dirArr, $dir);
  387. } else {
  388. $error = true;
  389. }
  390. }
  391. }
  392. $data = [];
  393. $data['msg'] = '';
  394. if ($error) {
  395. $num = 0;
  396. $wwwroot = glob('*', GLOB_ONLYDIR);
  397. foreach ($wwwroot as $key => $val) {
  398. if (in_array($val, $dirArr)) {
  399. if (0 == $num) {
  400. $data['msg'] .= "<font color='red'>部分目录删除失败,请手工删除:</font><br/>";
  401. }
  402. $data['msg'] .= ($num+1)."、{$val}<br/>";
  403. $num++;
  404. }
  405. }
  406. $data['height'] = $num * 24;
  407. $this->error('删除失败!', null, $data);
  408. }
  409. $this->success('删除成功!', null, $data);
  410. }
  411. }
  412. /**
  413. * 生成完页面之后,清除缓存
  414. */
  415. private function buildhtml_clear_cache()
  416. {
  417. // 文档参数缓存
  418. cache("article_info_serialize",null);
  419. cache("article_page_total_serialize",null);
  420. cache("article_content_serialize",null);
  421. cache("article_tags_serialize",null);
  422. cache("article_attr_info_serialize",null);
  423. cache("article_children_row_serialize",null);
  424. // 栏目参数缓存
  425. cache("channel_page_total_serialize",null);
  426. cache("channel_info_serialize",null);
  427. cache("has_children_Row_serialize",null);
  428. }
  429. }