Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

BuildhtmlLogic.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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\common\logic;
  14. use think\Model;
  15. use think\Db;
  16. /**
  17. * 逻辑定义
  18. * @package common\Logic
  19. */
  20. load_trait('controller/Jump');
  21. class BuildhtmlLogic extends Model
  22. {
  23. use \traits\controller\Jump;
  24. /**
  25. * 初始化操作
  26. */
  27. public function _initialize() {
  28. parent::_initialize();
  29. }
  30. /*
  31. * 保存URL配置
  32. */
  33. public function seo_handle($post = [])
  34. {
  35. $param = $post;
  36. $inc_type = 'seo';
  37. $globalConfig = tpCache('global');
  38. $seo_pseudo_new = $param['seo_pseudo'];
  39. /*伪静态格式*/
  40. if (3 == $seo_pseudo_new && in_array($param['seo_rewrite_format'], [1,3,4])) {
  41. $param['seo_rewrite_format'] = !empty($param['seo_rewrite_view_format']) ? $param['seo_rewrite_view_format'] : 1;
  42. }
  43. /*--end*/
  44. /* 生成静态页面代码 */
  45. unset($param['seo_html_arcdir_limit']);
  46. if (!empty($param['seo_html_arcdir']) && !preg_match('/^([0-9a-zA-Z\_\-\/]+)$/i', $param['seo_html_arcdir'])) {
  47. $this->error('页面保存路径的格式错误!');
  48. }
  49. if (!empty($param['seo_html_arcdir'])) {
  50. if (preg_match('/^([0-9a-zA-Z\_\-\/]+)$/i', $param['seo_html_arcdir'])) {
  51. // $param['seo_html_arcdir'] = ROOT_DIR.'/'.trim($param['seo_html_arcdir'], '/');
  52. $param['seo_html_arcdir'] = '/'.trim($param['seo_html_arcdir'], '/');
  53. } else {
  54. $this->error('页面保存路径的格式错误!');
  55. }
  56. }
  57. $seo_html_arcdir_old = !empty($globalConfig['seo_html_arcdir']) ? $globalConfig['seo_html_arcdir'] : '';
  58. /* end */
  59. /*检测是否开启pathinfo模式*/
  60. try {
  61. if (3 == $seo_pseudo_new || (1 == $seo_pseudo_new && 2 == $param['seo_dynamic_format'])) {
  62. $fix_pathinfo = ini_get('cgi.fix_pathinfo');
  63. if (stristr(request()->host(), '.mylightsite.com')) {
  64. $this->error('腾讯云空间不支持伪静态!');
  65. } else if ('' != $fix_pathinfo && 0 === $fix_pathinfo) {
  66. $this->error('空间不支持伪静态,请开启pathinfo,或者在php.ini里修改cgi.fix_pathinfo=1');
  67. }
  68. }
  69. /* 生成静态页面代码 - URL模式切换时删掉根目录下的index.html静态文件 */
  70. if(1 == $seo_pseudo_new || 3 == $seo_pseudo_new){
  71. if(file_exists('./index.html')){
  72. @unlink('./index.html');
  73. }
  74. }
  75. /* end */
  76. } catch (\Exception $e) {}
  77. /*--end*/
  78. /*强制去除index.php*/
  79. if (isset($param['seo_force_inlet'])) {
  80. $seo_force_inlet = $param['seo_force_inlet'];
  81. $seo_force_inlet_old = !empty($globalConfig['seo_force_inlet']) ? $globalConfig['seo_force_inlet'] : '';
  82. if ($seo_force_inlet_old != $seo_force_inlet) {
  83. $param['seo_inlet'] = $seo_force_inlet;
  84. }
  85. }
  86. /*--end*/
  87. // 用于静态模式,发布编辑文档后更新
  88. $param['seo_uphtml_after_home'] = !empty($param['seo_uphtml_after_home']) ? $param['seo_uphtml_after_home'] : 0;
  89. $param['seo_uphtml_after_channel'] = !empty($param['seo_uphtml_after_channel']) ? $param['seo_uphtml_after_channel'] : 0;
  90. $param['seo_uphtml_after_pernext'] = !empty($param['seo_uphtml_after_pernext']) ? $param['seo_uphtml_after_pernext'] : 0;
  91. $param['seo_uphtml_editafter_home'] = !empty($param['seo_uphtml_editafter_home']) ? $param['seo_uphtml_editafter_home'] : 0;
  92. $param['seo_uphtml_editafter_channel'] = !empty($param['seo_uphtml_editafter_channel']) ? $param['seo_uphtml_editafter_channel'] : 0;
  93. $param['seo_uphtml_editafter_pernext'] = !empty($param['seo_uphtml_editafter_pernext']) ? $param['seo_uphtml_editafter_pernext'] : 0;
  94. // 用于动态、伪静态模式,在运营模式下 - 发布编辑文档后清除缓存
  95. $param['seo_uphtml_after_home13'] = !empty($param['seo_uphtml_after_home13']) ? $param['seo_uphtml_after_home13'] : 0;
  96. $param['seo_uphtml_after_channel13'] = !empty($param['seo_uphtml_after_channel13']) ? $param['seo_uphtml_after_channel13'] : 0;
  97. $param['seo_uphtml_after_pernext13'] = !empty($param['seo_uphtml_after_pernext13']) ? $param['seo_uphtml_after_pernext13'] : 0;
  98. /*多语言*/
  99. if (is_language()) {
  100. $seo_pseudo_lang = !empty($param['seo_pseudo_lang']) ? $param['seo_pseudo_lang'] : 1;
  101. unset($param['seo_pseudo_lang']);
  102. $langRow = \think\Db::name('language')->order('id asc')
  103. ->cache(true, EYOUCMS_CACHE_TIME, 'language')
  104. ->select();
  105. foreach ($langRow as $key => $val) {
  106. if (2 != $seo_pseudo_new) { // 非生成静态模式下,所有语言的URL模式一致
  107. tpCache($inc_type,$param,$val['mark']);
  108. } else {
  109. if($key == 0){ // 主体语言(第一个语言)是生成静态模式
  110. tpCache($inc_type,$param,$val['mark']);
  111. }else{//其他语言统一设置URL模式非静态模式
  112. $param['seo_pseudo'] = $seo_pseudo_lang;
  113. tpCache($inc_type,$param,$val['mark']);
  114. }
  115. }
  116. }
  117. } else {
  118. tpCache($inc_type,$param);
  119. }
  120. /*--end*/
  121. }
  122. /**
  123. * 生成静态专用 - 获取全部栏目的数据
  124. */
  125. public function get_arctype_all($field = '*')
  126. {
  127. $cacheKey = md5('common_buildhtmllogic_get_arctype_all_'.$field);
  128. $result = cache($cacheKey);
  129. if (empty($result)) {
  130. $result = Db::name('arctype')->field($field)->getAllWithIndex('id');
  131. cache($cacheKey, $result, null, 'arctype');
  132. }
  133. return !empty($result) ? $result : [];
  134. }
  135. /**
  136. * 生成静态专用 - 获取全部文档对应的栏目id
  137. */
  138. public function get_archives_all()
  139. {
  140. $empty_num = 0;
  141. $pagesize = 15000;
  142. for ($i=0; $i < 1000; $i++) {
  143. $result = [];
  144. $start = $i * $pagesize;
  145. $end = ($i + 1) * $pagesize;
  146. $field = 'aid,typeid';
  147. $row = Db::name('archives')->where([
  148. 'aid' => ['BETWEEN', [$start + 1, $end]],
  149. ])->field($field)->select();
  150. if (empty($row)) {
  151. if ($empty_num < 2) {
  152. $empty_num++;
  153. continue;
  154. } else {
  155. break;
  156. }
  157. }
  158. foreach ($row as $key => $val) {
  159. $result[$val['aid']] = $val['typeid'];
  160. }
  161. $cacheKey = "table_archives_{$start}_{$end}";
  162. cache($cacheKey, $result, null, 'archives');
  163. }
  164. }
  165. /**
  166. * 删除文档对应的html页面文件
  167. * @param array $aids [description]
  168. * @return [type] [description]
  169. */
  170. public function delViewHtml($aids = [], $globalConfig = [])
  171. {
  172. if (empty($globalConfig)) {
  173. $globalConfig = tpCache('global');
  174. }
  175. if (2 == $globalConfig['seo_pseudo']) {
  176. $info = Db::name('archives')->field('b.topid,b.dirpath,b.diy_dirpath,b.rulelist,b.ruleview,a.*')
  177. ->alias('a')
  178. ->join('__ARCTYPE__ b', 'a.typeid = b.id', 'LEFT')
  179. ->where([
  180. 'a.aid' => ['in', $aids],
  181. ])
  182. ->select();
  183. $seo_html_pagename = $globalConfig['seo_html_pagename'];
  184. foreach ($info as $key => $row) {
  185. $filename = $row['aid'];
  186. if (!empty($row['htmlfilename'])) {
  187. $filename = $row['htmlfilename'];
  188. }
  189. $dir = $this->getArticleDir($row, $globalConfig);
  190. if (4 == $seo_html_pagename) {
  191. if (!empty($row['ruleview'])) {
  192. $path = $dir;
  193. }else{
  194. $path = $dir . "/" . $filename . ".html";
  195. }
  196. } else {
  197. $path = $dir . "/" . $filename . ".html";
  198. }
  199. if (file_exists($path)) @unlink($path);
  200. }
  201. }
  202. }
  203. public function getArticleDir($row = [], $globalConfig = [])
  204. {
  205. if (empty($globalConfig)) {
  206. $globalConfig = tpCache('global');
  207. }
  208. $dir = "";
  209. $seo_html_pagename = $globalConfig['seo_html_pagename'];
  210. $seo_html_arcdir = $globalConfig['seo_html_arcdir'];
  211. $dirpath = !empty($row['dirpath']) ? $row['dirpath'] : '';
  212. $aid = !empty($row['htmlfilename']) ? $row['htmlfilename'] : $row['aid'];
  213. if ($seo_html_pagename == 1) {//存放顶级目录
  214. $dirpath_arr = explode('/', $dirpath);
  215. if (count($dirpath_arr) > 2) {
  216. $dir = '.' . $seo_html_arcdir . '/' . $dirpath_arr[1];
  217. } else {
  218. $dir = '.' . $seo_html_arcdir . $dirpath;
  219. }
  220. } else if ($seo_html_pagename == 3) { //存放子级目录
  221. $dirpath_arr = explode('/', $dirpath);
  222. if (count($dirpath_arr) > 2) {
  223. $dir = '.' . $seo_html_arcdir . '/' . end($dirpath_arr);
  224. } else {
  225. $dir = '.' . $seo_html_arcdir . $dirpath;
  226. }
  227. } else if ($seo_html_pagename == 4) { //自定义存放目录
  228. $dir = '.' . $seo_html_arcdir;
  229. $diy_dirpath = !empty($row['diy_dirpath']) ? $row['diy_dirpath'] : '';
  230. if (!empty($row['ruleview'])) {
  231. $y = $m = $d = 1;
  232. if (!empty($row['add_time'])) {
  233. $y = date('Y', $row['add_time']);
  234. $m = date('m', $row['add_time']);
  235. $d = date('d', $row['add_time']);
  236. }
  237. $ruleview = ltrim($row['ruleview'], '/');
  238. $ruleview = str_ireplace("{aid}", $aid, $ruleview);
  239. $ruleview = str_ireplace("{Y}", $y, $ruleview);
  240. $ruleview = str_ireplace("{M}", $m, $ruleview);
  241. $ruleview = str_ireplace("{D}", $d, $ruleview);
  242. $ruleview = preg_replace('/{(栏目目录|typedir)}(\/?)/i', $diy_dirpath.'/', $ruleview);
  243. $ruleview = '/'.ltrim($ruleview, '/');
  244. $dir .= $ruleview;
  245. }else{
  246. $dir .= $diy_dirpath;
  247. }
  248. } else { //存放父级目录
  249. $dir = '.' . $seo_html_arcdir . $dirpath;
  250. }
  251. return $dir;
  252. }
  253. }