html.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <?php
  2. defined('IN_PHPCMS') or exit('No permission resources.');
  3. pc_base::load_app_func('util','content');
  4. pc_base::load_sys_func('dir');
  5. class html {
  6. private $siteid,$url,$html_root,$queue,$categorys;
  7. public function __construct() {
  8. $this->queue = pc_base::load_model('queue_model');
  9. define('HTML',true);
  10. self::set_siteid();
  11. $this->categorys = getcache('category_content_'.$this->siteid,'commons');
  12. $this->url = pc_base::load_app_class('url', 'content');
  13. $this->html_root = pc_base::load_config('system','html_root');
  14. $this->sitelist = getcache('sitelist','commons');
  15. }
  16. /**
  17. * 生成内容页
  18. * @param $file 文件地址
  19. * @param $data 数据
  20. * @param $array_merge 是否合并
  21. * @param $action 方法
  22. * @param $upgrade 是否是升级数据
  23. */
  24. public function show($file, $data = '', $array_merge = 1,$action = 'add',$upgrade = 0) {
  25. if($upgrade) $file = '/'.ltrim($file,WEB_PATH);
  26. $allow_visitor = 1;
  27. $id = $data['id'];
  28. if($array_merge) {
  29. $data = new_stripslashes($data);
  30. $data = array_merge($data['system'],$data['model']);
  31. }
  32. //通过rs获取原始值
  33. $rs = $data;
  34. if(isset($data['paginationtype'])) {
  35. $paginationtype = $data['paginationtype'];
  36. $maxcharperpage = $data['maxcharperpage'];
  37. } else {
  38. $paginationtype = 0;
  39. }
  40. $catid = $data['catid'];
  41. $CATEGORYS = $this->categorys;
  42. $CAT = $CATEGORYS[$catid];
  43. $CAT['setting'] = string2array($CAT['setting']);
  44. define('STYLE',$CAT['setting']['template_list']);
  45. //最顶级栏目ID
  46. $arrparentid = explode(',', $CAT['arrparentid']);
  47. $top_parentid = $arrparentid[1] ? $arrparentid[1] : $catid;
  48. //$file = '/'.$file;
  49. //添加到发布点队列
  50. //当站点为非系统站点
  51. if($this->siteid!=1) {
  52. $site_dir = $this->sitelist[$this->siteid]['dirname'];
  53. $file = $this->html_root.'/'.$site_dir.$file;
  54. }
  55. $this->queue->add_queue($action,$file,$this->siteid);
  56. $modelid = $CAT['modelid'];
  57. require_once CACHE_MODEL_PATH.'content_output.class.php';
  58. $content_output = new content_output($modelid,$catid,$CATEGORYS);
  59. $output_data = $content_output->get($data);
  60. extract($output_data);
  61. if(module_exists('comment')) {
  62. $allow_comment = isset($allow_comment) ? $allow_comment : 1;
  63. } else {
  64. $allow_comment = 0;
  65. }
  66. $this->db = pc_base::load_model('content_model');
  67. $this->db->set_model($modelid);
  68. //上一页
  69. $previous_page = $this->db->get_one("`catid` = '$catid' AND `id`<'$id' AND `status`=99",'*','id DESC');
  70. //下一页
  71. $next_page = $this->db->get_one("`catid`= '$catid' AND `id`>'$id' AND `status`=99",'*','id ASC');
  72. if(empty($previous_page)) {
  73. $previous_page = array('title'=>L('first_page','','content'), 'thumb'=>IMG_PATH.'nopic_small.gif', 'url'=>'javascript:alert(\''.L('first_page','','content').'\');');
  74. }
  75. if(empty($next_page)) {
  76. $next_page = array('title'=>L('last_page','','content'), 'thumb'=>IMG_PATH.'nopic_small.gif', 'url'=>'javascript:alert(\''.L('last_page','','content').'\');');
  77. }
  78. $title = strip_tags($title);
  79. //SEO
  80. $seo_keywords = '';
  81. if(!empty($keywords)) $seo_keywords = implode(',',$keywords);
  82. $siteid = $this->siteid;
  83. $SEO = seo($siteid, $catid, $title, $description, $seo_keywords);
  84. $ishtml = 1;
  85. $template = $template ? $template : $CAT['setting']['show_template'];
  86. //分页处理
  87. $pages = $titles = '';
  88. if($paginationtype==1) {
  89. //自动分页
  90. if($maxcharperpage < 10) $maxcharperpage = 500;
  91. $contentpage = pc_base::load_app_class('contentpage');
  92. $content = $contentpage->get_data($content,$maxcharperpage);
  93. }
  94. if($paginationtype!=0) {
  95. //手动分页
  96. $CONTENT_POS = strpos($content, '[page]');
  97. if($CONTENT_POS !== false) {
  98. $this->url = pc_base::load_app_class('url', 'content');
  99. $contents = array_filter(explode('[page]', $content));
  100. $pagenumber = count($contents);
  101. if (strpos($content, '[/page]')!==false && ($CONTENT_POS<7)) {
  102. $pagenumber--;
  103. }
  104. for($i=1; $i<=$pagenumber; $i++) {
  105. $upgrade = $upgrade ? '/'.ltrim($file,WEB_PATH) : '';
  106. $pageurls[$i] = $this->url->show($id, $i, $catid, $data['inputtime'],'','','edit',$upgrade);
  107. }
  108. $END_POS = strpos($content, '[/page]');
  109. if($END_POS !== false) {
  110. if($CONTENT_POS>7) {
  111. $content = '[page]'.$title.'[/page]'.$content;
  112. }
  113. if(preg_match_all("|\[page\](.*)\[/page\]|U", $content, $m, PREG_PATTERN_ORDER)) {
  114. foreach($m[1] as $k=>$v) {
  115. $p = $k+1;
  116. $titles[$p]['title'] = strip_tags($v);
  117. $titles[$p]['url'] = $pageurls[$p][0];
  118. }
  119. }
  120. }
  121. //生成分页
  122. foreach ($pageurls as $page=>$urls) {
  123. $pages = content_pages($pagenumber,$page, $pageurls);
  124. //判断[page]出现的位置是否在第一位
  125. if($CONTENT_POS<7) {
  126. $content = $contents[$page];
  127. } else {
  128. if ($page==1 && !empty($titles)) {
  129. $content = $title.'[/page]'.$contents[$page-1];
  130. } else {
  131. $content = $contents[$page-1];
  132. }
  133. }
  134. if($titles) {
  135. list($title, $content) = explode('[/page]', $content);
  136. $content = trim($content);
  137. if(strpos($content,'</p>')===0) {
  138. $content = '<p>'.$content;
  139. }
  140. if(stripos($content,'<p>')===0) {
  141. $content = $content.'</p>';
  142. }
  143. }
  144. $pagefile = $urls[1];
  145. if($this->siteid!=1) {
  146. $pagefile = $this->html_root.'/'.$site_dir.$pagefile;
  147. }
  148. $this->queue->add_queue($action,$pagefile,$this->siteid);
  149. $pagefile = PHPCMS_PATH.$pagefile;
  150. ob_start();
  151. include template('content', $template);
  152. $this->createhtml($pagefile);
  153. }
  154. return true;
  155. }
  156. }
  157. //分页处理结束
  158. $file = PHPCMS_PATH.$file;
  159. ob_start();
  160. include template('content', $template);
  161. return $this->createhtml($file);
  162. }
  163. /**
  164. * 生成栏目列表
  165. * @param $catid 栏目id
  166. * @param $page 当前页数
  167. */
  168. public function category($catid, $page = 0) {
  169. $CAT = $this->categorys[$catid];
  170. @extract($CAT);
  171. if(!$ishtml) return false;
  172. if(!$catid) showmessage(L('category_not_exists','content'),'blank');
  173. $CATEGORYS = $this->categorys;
  174. if(!isset($CATEGORYS[$catid])) showmessage(L('information_does_not_exist', 'content'),'blank');
  175. $siteid = $CAT['siteid'];
  176. $copyjs = '';
  177. $setting = string2array($setting);
  178. if(!$setting['meta_title']) $setting['meta_title'] = $catname;
  179. $SEO = seo($siteid, '',$setting['meta_title'],$setting['meta_description'],$setting['meta_keywords']);
  180. define('STYLE',$setting['template_list']);
  181. $page = intval($page);
  182. $parentdir = $CAT['parentdir'];
  183. $catdir = $CAT['catdir'];
  184. //检查是否生成到根目录
  185. $create_to_html_root = $CAT['sethtml'];
  186. //$base_file = $parentdir.$catdir.'/';
  187. //生成地址
  188. if($CAT['create_to_html_root']) $parentdir = '';
  189. //获取父级的配置,看是否生成静态,如果是动态则直接把父级目录调过来为生成静态目录所用
  190. $parent_setting = string2array($CATEGORYS[$CAT['parentid']]['setting']);
  191. if($parent_setting['ishtml']==0 && $setting['ishtml']==1){
  192. $parentdir = $CATEGORYS[$CAT['parentid']]['catdir'].'/';
  193. }
  194. $base_file = $this->url->get_list_url($setting['category_ruleid'],$parentdir, $catdir, $catid, $page);
  195. $base_file = '/'.$base_file;
  196. //非系统站点时,生成到指定目录
  197. if($this->siteid!=1) {
  198. $site_dir = $this->sitelist[$this->siteid]['dirname'];
  199. if($create_to_html_root) {
  200. $base_file = '/'.$site_dir.$base_file;
  201. } else {
  202. $base_file = '/'.$site_dir.$this->html_root.$base_file;
  203. }
  204. }
  205. //判断二级域名是否直接绑定到该栏目
  206. $root_domain = preg_match('/^((http|https):\/\/)([a-z0-9\-\.]+)\/$/',$CAT['url']) ? 1 : 0;
  207. $count_number = substr_count($CAT['url'], '/');
  208. $urlrules = getcache('urlrules','commons');
  209. $urlrules = explode('|',$urlrules[$category_ruleid]);
  210. if($create_to_html_root) {
  211. if($this->siteid==1) {
  212. $file = PHPCMS_PATH.$base_file;
  213. } else {
  214. $file = PHPCMS_PATH.substr($this->html_root,1).$base_file;
  215. }
  216. //添加到发布点队列
  217. $this->queue->add_queue('add',$base_file,$this->siteid);
  218. //评论跨站调用所需的JS文件
  219. if(substr($base_file, -10)=='index.html' && $count_number==3) {
  220. $copyjs = 1;
  221. $this->queue->add_queue('add',$base_file,$this->siteid);
  222. }
  223. //URLRULES
  224. if($CAT['isdomain']) {
  225. $second_domain = 1;
  226. foreach ($urlrules as $_k=>$_v) {
  227. $urlrules[$_k] = $_v;
  228. }
  229. } else {
  230. foreach ($urlrules as $_k=>$_v) {
  231. $urlrules[$_k] = '/'.$_v;
  232. }
  233. }
  234. } else {
  235. $file = PHPCMS_PATH.substr($this->html_root,1).$base_file;
  236. //添加到发布点队列
  237. $this->queue->add_queue('add',$this->html_root.$base_file,$this->siteid);
  238. //评论跨站调用所需的JS文件
  239. if(substr($base_file, -10)=='index.html' && $count_number==3) {
  240. $copyjs = 1;
  241. $this->queue->add_queue('add',$this->html_root.$base_file,$this->siteid);
  242. }
  243. //URLRULES
  244. $htm_prefix = $root_domain ? '' : $this->html_root;
  245. $htm_prefix = rtrim(WEB_PATH,'/').$htm_prefix;
  246. if($CAT['isdomain']) {
  247. $second_domain = 1;
  248. } else {
  249. $second_domain = 0;//判断该栏目是否绑定了二级域名或者上级栏目绑定了二级域名,存在的话,重新构造列表页url规则
  250. foreach ($urlrules as $_k=>$_v) {
  251. $urlrules[$_k] = $htm_prefix.'/'.$_v;
  252. }
  253. }
  254. }
  255. if($type==0) {
  256. $template = $setting['category_template'] ? $setting['category_template'] : 'category';
  257. $template_list = $setting['list_template'] ? $setting['list_template'] : 'list';
  258. $template = $child ? $template : $template_list;
  259. $arrparentid = explode(',', $arrparentid);
  260. $top_parentid = $arrparentid[1] ? $arrparentid[1] : $catid;
  261. $array_child = array();
  262. $self_array = explode(',', $arrchildid);
  263. foreach ($self_array as $arr) {
  264. if($arr!=$catid) $array_child[] = $arr;
  265. }
  266. $arrchildid = implode(',', $array_child);
  267. //URL规则
  268. $urlrules = implode('~', $urlrules);
  269. define('URLRULE', $urlrules);
  270. //绑定域名时,设置$catdir 为空
  271. if($root_domain) $parentdir = $catdir = '';
  272. if($second_domain) {
  273. $parentdir = '';
  274. $parentdir = str_replace($catdir.'/', '', $CAT['url']);
  275. }
  276. $GLOBALS['URL_ARRAY'] = array('categorydir'=>$parentdir, 'catdir'=>$catdir, 'catid'=>$catid);
  277. } else {
  278. //单网页
  279. $datas = $this->page($catid);
  280. if($datas) extract($datas);
  281. $template = $setting['page_template'] ? $setting['page_template'] : 'page';
  282. $parentid = $CATEGORYS[$catid]['parentid'];
  283. $arrchild_arr = $CATEGORYS[$parentid]['arrchildid'];
  284. if($arrchild_arr=='') $arrchild_arr = $CATEGORYS[$catid]['arrchildid'];
  285. $arrchild_arr = explode(',',$arrchild_arr);
  286. array_shift($arrchild_arr);
  287. $keywords = $keywords ? $keywords : $setting['meta_keywords'];
  288. $SEO = seo($siteid, 0, $title,$setting['meta_description'],$keywords);
  289. }
  290. ob_start();
  291. include template('content',$template);
  292. return $this->createhtml($file, $copyjs);
  293. }
  294. /**
  295. * 更新首页
  296. */
  297. public function index() {
  298. if($this->siteid==1) {
  299. $file = PHPCMS_PATH.'index.html';
  300. //添加到发布点队列
  301. $this->queue->add_queue('edit','/index.html',$this->siteid);
  302. } else {
  303. $site_dir = $this->sitelist[$this->siteid]['dirname'];
  304. $file = $this->html_root.'/'.$site_dir.'/index.html';
  305. //添加到发布点队列
  306. $this->queue->add_queue('edit',$file,$this->siteid);
  307. $file = PHPCMS_PATH.$file;
  308. }
  309. define('SITEID', $this->siteid);
  310. //SEO
  311. $SEO = seo($this->siteid);
  312. $siteid = $this->siteid;
  313. $CATEGORYS = $this->categorys;
  314. $style = $this->sitelist[$siteid]['default_style'];
  315. ob_start();
  316. include template('content','index',$style);
  317. return $this->createhtml($file, 1);
  318. }
  319. /**
  320. * 单网页
  321. * @param $catid
  322. */
  323. public function page($catid) {
  324. $this->page_db = pc_base::load_model('page_model');
  325. $data = $this->page_db->get_one(array('catid'=>$catid));
  326. return $data;
  327. }
  328. /**
  329. * 写入文件
  330. * @param $file 文件路径
  331. * @param $copyjs 是否复制js,跨站调用评论时,需要该js
  332. */
  333. private function createhtml($file, $copyjs = '') {
  334. $data = ob_get_contents();
  335. ob_clean();
  336. $dir = dirname($file);
  337. if(!is_dir($dir)) {
  338. mkdir($dir, 0777,1);
  339. }
  340. if ($copyjs && !file_exists($dir.'/js.html')) {
  341. @copy(PC_PATH.'modules/content/templates/js.html', $dir.'/js.html');
  342. }
  343. $strlen = file_put_contents($file, $data);
  344. @chmod($file,0777);
  345. if(!is_writable($file)) {
  346. $file = str_replace(PHPCMS_PATH,'',$file);
  347. showmessage(L('file').':'.$file.'<br>'.L('not_writable'));
  348. }
  349. return $strlen;
  350. }
  351. /**
  352. * 设置当前站点id
  353. */
  354. private function set_siteid() {
  355. if(defined('IN_ADMIN')) {
  356. $this->siteid = $GLOBALS['siteid'] = get_siteid();
  357. } else {
  358. if (param::get_cookie('siteid')) {
  359. $this->siteid = $GLOBALS['siteid'] = param::get_cookie('siteid');
  360. } else {
  361. $this->siteid = $GLOBALS['siteid'] = 1;
  362. }
  363. }
  364. }
  365. /**
  366. * 生成相关栏目列表、只生成前5页
  367. * @param $catid
  368. */
  369. public function create_relation_html($catid) {
  370. for($page = 1; $page < 6; $page++) {
  371. $this->category($catid,$page);
  372. }
  373. //检查当前栏目的父栏目,如果存在则生成
  374. $arrparentid = $this->categorys[$catid]['arrparentid'];
  375. if($arrparentid) {
  376. $arrparentid = explode(',', $arrparentid);
  377. foreach ($arrparentid as $catid) {
  378. if($catid) $this->category($catid,1);
  379. }
  380. }
  381. }
  382. }