html.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. /**
  3. * html.class.php 生成静态类
  4. */
  5. defined('IN_PHPCMS') or exit('No permission resources.');
  6. class html {
  7. private $db, $type_db, $c_db, $data_db, $site, $queue;
  8. public function __construct() {
  9. $this->db = pc_base::load_model('special_model'); //专题数据模型
  10. $this->type_db = pc_base::load_model('type_model'); //专题分类数据模型
  11. $this->c_db = pc_base::load_model('special_content_model'); //专题内容数据模型
  12. $this->data_db = pc_base::load_model('special_c_data_model');
  13. $this->site = pc_base::load_app_class('sites', 'admin');
  14. $this->queue = pc_base::load_model('queue_model');
  15. define('HTML', true);
  16. }
  17. /**
  18. * 生成文章静态页
  19. * @param intval $contentid 文章ID
  20. * @return string 返回文章的url
  21. */
  22. public function _create_content($contentid = 0) {
  23. if (!$contentid) return false;
  24. pc_base::load_app_func('global', 'special');
  25. $r = $this->c_db->get_one(array('id'=>$contentid));
  26. $_special = $s_info = $this->db->get_one(array('id'=>$r['specialid']));
  27. if($s_info['ishtml']==0) return content_url($contentid, '1', 0, 'php');
  28. unset($arr_content);
  29. $arr_content = $this->data_db->get_one(array('id'=>$contentid));
  30. @extract($r);
  31. $title = strip_tags($title);
  32. if ($arr_content['paginationtype']) { //文章使用分页时
  33. if($arr_content['paginationtype']==1) {
  34. if (strpos($arr_content['content'], '[/page]')!==false) {
  35. $arr_content['content'] = preg_replace("|\[page\](.*)\[/page\]|U", '', $arr_content['content']);
  36. }
  37. if (strpos($arr_content['content'], '[page]')!==false) {
  38. $arr_content['content'] = str_replace('[page]', '', $data['content']);
  39. }
  40. $contentpage = pc_base::load_app_class('contentpage', 'content'); //调用自动分页类
  41. $arr_content['content'] = $contentpage->get_data($arr_content['content'], $arr_content['maxcharperpage']); //自动分页,自动添加上[page]
  42. }
  43. } else {
  44. if (strpos($arr_content['content'], '[/page]')!==false) {
  45. $arr_content['content'] = preg_replace("|\[page\](.*)\[/page\]|U", '', $arr_content['content']);
  46. }
  47. if (strpos($arr_content['content'], '[page]')!==false) {
  48. $arr_content['content'] = str_replace('[page]', '', $arr_content['content']);
  49. }
  50. }
  51. $template = $arr_content['show_template'] ? $arr_content['show_template'] : 'show'; //调用模板
  52. //分站时,计算分站路径
  53. if ($s_info['siteid']>1) {
  54. $site_info = $this->site->get_by_id($s_info['siteid']);
  55. }
  56. $siteid = $s_info['siteid'];
  57. $CONTENT_POS = strpos($arr_content['content'], '[page]');
  58. if ($CONTENT_POS !== false) {
  59. $contents = array_filter(explode('[page]', $arr_content['content']));
  60. $pagenumber = count($contents);
  61. $END_POS = strpos($arr_content['content'], '[/page]');
  62. if ($END_POS!==false && ($CONTENT_POS<7)) {
  63. $pagenumber--;
  64. }
  65. for ($i=1; $i<=$pagenumber; $i++) {
  66. $pageurls[$i] = content_url($contentid, $i, $inputtime, 'html', $site_info);
  67. }
  68. if ($END_POS !== false) {
  69. if($CONTENT_POS>7) {
  70. $arr_content['content'] = '[page]'.$title.'[/page]'.$arr_content['content'];
  71. }
  72. if (preg_match_all("|\[page\](.*)\[/page\]|U", $arr_content['content'], $m, PREG_PATTERN_ORDER)) {
  73. foreach ($m[1] as $k=>$v) {
  74. $p = $k+1;
  75. $titles[$p]['title'] = strip_tags($v);
  76. $titles[$p]['url'] = $pageurls[$p][1];
  77. }
  78. }
  79. }
  80. $currentpage = $filesize = 0;
  81. for ($i=1; $i<=$pagenumber; $i++) {
  82. $currentpage++;
  83. //判断[page]出现的位置是否在第一位
  84. if($CONTENT_POS<7) {
  85. $content = $contents[$currentpage];
  86. } else {
  87. if ($currentpage==1 && !empty($titles)) {
  88. $content = $title.'[/page]'.$contents[$currentpage-1];
  89. } else {
  90. $content = $contents[$currentpage-1];
  91. }
  92. }
  93. if($titles) {
  94. list($title, $content) = explode('[/page]', $content);
  95. $content = trim($content);
  96. if(strpos($content,'</p>')===0) {
  97. $content = '<p>'.$content;
  98. }
  99. if(stripos($content,'<p>')===0) {
  100. $content = $content.'</p>';
  101. }
  102. }
  103. $file_url = content_url($contentid, $currentpage, $inputtime, 'html', $site_info);
  104. if ($currentpage==1) $urls = $file_url;
  105. pc_base::load_app_func('util', 'content');
  106. $title_pages = content_pages($pagenumber,$currentpage, $pageurls);
  107. $SEO = seo($s_info['siteid'], '', $title);
  108. $file = $file_url[1];
  109. //如果是分站的文件,将文件写入到信息队列中
  110. $this->queue->add_queue('add', $file, $siteid);
  111. $file = PHPCMS_PATH.$file; //生成文件的路径
  112. ob_start();
  113. include template('special', $template);
  114. $this->create_html($file);
  115. }
  116. } else {
  117. $page = 1;
  118. $title = strip_tags($title);
  119. $SEO = seo($s_info['siteid'], '', $title);
  120. $content = $arr_content['content'];
  121. $urls = content_url($contentid, $page, $inputtime, 'html', $site_info);
  122. $file = $urls[1];
  123. //如果是分站的文件,将文件写入到信息队列中
  124. $this->queue->add_queue('add', $file, $siteid);
  125. $file = PHPCMS_PATH.$file;
  126. ob_start();
  127. include template('special', $template);
  128. $this->create_html($file);
  129. }
  130. //$this->_index($specialid, 20, 5); //更新专题首页
  131. //$this->_list($typeid, 20, 5); //更新所在的分类页
  132. return $urls;
  133. }
  134. /**
  135. * 生成静态文件
  136. * @param string $file 文件路径
  137. * @return boolen/intval 成功返回生成文件的大小
  138. */
  139. private function create_html($file) {
  140. $data = ob_get_contents();
  141. ob_end_clean();
  142. pc_base::load_sys_func('dir');
  143. dir_create(dirname($file));
  144. $strlen = file_put_contents($file, $data);
  145. @chmod($file, 0777);
  146. return $strlen;
  147. }
  148. /**
  149. * 生成专题首页
  150. * @param intval $specialid 专题ID
  151. * @param intval $pagesize 每页个数
  152. * @param intval $pages_num 最大更新页数
  153. * @return boolen/intval 成功返回生成文件的大小
  154. */
  155. public function _index($specialid = 0, $pagesize = 20, $pages_num = 0) {
  156. pc_base::load_app_func('global', 'special');
  157. $specialid = intval($specialid);
  158. if (!$specialid) return false;
  159. $r = $this->db->get_one(array('id'=>$specialid, 'siteid'=>get_siteid()));
  160. if (!$r['ishtml'] || $r['disabled'] != 0 ) return true;
  161. if (!$specialid) showmessage(L('illegal_action'));
  162. $info = $this->db->get_one(array('id'=>$specialid));
  163. if(!$info) showmessage(L('special_not_exist'), 'back');
  164. extract($info);
  165. if ($pics) {
  166. $pic_data = get_pic_content($pics);
  167. unset($pics);
  168. }
  169. if ($voteid) {
  170. $vote_info = explode('|', $voteid);
  171. $voteid = $vote_info[1];
  172. }
  173. $commentid = id_encode('special', $id, $siteid);
  174. //分站时计算路径
  175. if ($siteid>1) {
  176. $site_info = $this->site->get_by_id($siteid);
  177. $file = pc_base::load_config('system', 'html_root').'/'.$site_info['dirname'].'/special/'.$filename.'/index.html';
  178. } else {
  179. $file = pc_base::load_config('system', 'html_root').'/special/'.$filename.'/index.html';
  180. }
  181. if(!$ispage) {
  182. $type_db = pc_base::load_model('type_model');
  183. $types = $type_db->select(array('module'=>'special', 'parentid'=>$specialid), '*', '', '`listorder` ASC, `typeid` ASC', '', 'listorder');
  184. }
  185. $css = get_css(unserialize($css));
  186. $template = $index_template ? $index_template : 'index';
  187. $SEO = seo($siteid, '', $title, $description);
  188. if($ispage) {
  189. $re = $this->c_db->get_one(array('specialid'=>$specialid), 'COUNT(`id`) AS num');
  190. $total = $re['num'];
  191. $times = ceil($total/$pagesize);
  192. if ($pages_num) $pages_num = min($times, $pages_num);
  193. else $pages_num = $times;
  194. for ($i=1; $i<=$pages_num; $i++) {
  195. if ($i==1) $file_root = $file;
  196. else $file_root = str_replace('index', 'index-'.$i, $file);
  197. $this->queue->add_queue('add', $file_root, $siteid); //添加至信息队列
  198. $file_root = PHPCMS_PATH.$file_root;
  199. ob_start();
  200. include template('special', $template);
  201. $this->create_html($file_root);
  202. }
  203. return true;
  204. } else {
  205. $this->queue->add_queue('add', $file, $siteid); //添加至信息队列
  206. $file = PHPCMS_PATH.$file;
  207. ob_start();
  208. include template('special', $template, $style);
  209. return $this->create_html($file);
  210. }
  211. }
  212. /**
  213. * 生成列表页
  214. */
  215. public function create_list($page = 1) {
  216. $siteid = get_siteid();
  217. $site_info = $this->site->get_by_id($siteid);
  218. define('URLRULE', $site_info['domain'].substr(pc_base::load_config('system', 'html_root'), 1).'/special/index.html~'.$site_info['domain'].substr(pc_base::load_config('system', 'html_root'), 1).'/special/index-{$page}.html');
  219. //分站时计算路径
  220. if ($siteid>1) {
  221. if ($page==1) $file = pc_base::load_config('system', 'html_root').'/'.$site_info['dirname'].'/special/index.html';
  222. else $file = pc_base::load_config('system', 'html_root').'/'.$site_info['dirname'].'/special/index-'.$page.'.html';
  223. } else {
  224. if ($page==1) $file = pc_base::load_config('system', 'html_root').'/special/index.html';
  225. else $file = pc_base::load_config('system', 'html_root').'/special/index-'.$page.'.html';
  226. }
  227. $this->queue->add_queue('add', $file, $siteid);
  228. $file = PHPCMS_PATH.$file;
  229. ob_start();
  230. include template('special', 'special_list');
  231. return $this->create_html($file);
  232. }
  233. /**
  234. * 生成分类页
  235. * @param intval $typeid 分类ID
  236. * @param intval $page 页数
  237. */
  238. public function create_type($typeid = 0, $page = 1) {
  239. if (!$typeid) return false;
  240. $info = $this->type_db->get_one(array('typeid'=>$typeid));
  241. $s_info = $this->db->get_one(array('id'=>$info['parentid']));
  242. extract($s_info);
  243. $site_info = $this->site->get_by_id($siteid);
  244. define('URLRULE', $site_info['domain'].substr(pc_base::load_config('system', 'html_root'), 1).'/special/{$specialdir}/{$typedir}/type-{$typeid}.html~'.$site_info['domain'].substr(pc_base::load_config('system', 'html_root'), 1).'/special/{$specialdir}/{$typedir}/type-{$typeid}-{$page}.html');
  245. $GLOBALS['URL_ARRAY'] = array('specialdir'=>$filename, 'typedir'=>$info['typedir'], 'typeid'=>$typeid);
  246. $SEO = seo($siteid, '', $info['typename'], '');
  247. $template = $list_template ? $list_template : 'list';
  248. if ($siteid>1) {
  249. if ($page==1) $file = pc_base::load_config('system', 'html_root').'/'.$site_info['dirname'].'/special/'.$filename.'/'.$info['typedir'].'/type-'.$typeid.'.html';
  250. else $file = pc_base::load_config('system', 'html_root').'/'.$site_info['dirname'].'/special/'.$filename.'/'.$info['typedir'].'/type-'.$typeid.'-'.$page.'.html';
  251. } else {
  252. if ($page==1) $file = pc_base::load_config('system', 'html_root').'/special/'.$filename.'/'.$info['typedir'].'/type-'.$typeid.'.html';
  253. else $file = pc_base::load_config('system', 'html_root').'/special/'.$filename.'/'.$info['typedir'].'/type-'.$typeid.'-'.$page.'.html';
  254. }
  255. $this->queue->add_queue('add', $file, $siteid);
  256. $file = PHPCMS_PATH.$file;
  257. ob_start();
  258. include template('special', $template);
  259. $this->create_html($file);
  260. }
  261. /**
  262. * 生成分类静态页
  263. * @param intval $typeid 分类ID
  264. * @param intval $pagesize 每页篇数
  265. * @param intval $pages 最大更新页数
  266. */
  267. public function _list($typeid = 0, $pagesize = 20, $pages = 0) {
  268. if (!$typeid) return false;
  269. $r = $this->c_db->get_one(array('typeid'=>$typeid), 'COUNT(`id`) AS num');
  270. $total = $r['num'];
  271. $times = ceil($total/$pagesize);
  272. if ($pages) $pages = min($times, $pages);
  273. else $pages = $times;
  274. for ($i=1; $i<=$pages; $i++) {
  275. $this->create_type($typeid, $i);
  276. }
  277. return true;
  278. }
  279. }
  280. ?>