googlesitemap.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. defined('IN_PHPCMS') or exit('No permission resources.');
  3. pc_base::load_app_class('admin','admin',0);
  4. pc_base::load_sys_class('form', '', 0);
  5. class googlesitemap extends admin {
  6. function __construct() {
  7. parent::__construct();
  8. $this->header = "<\x3Fxml version=\"1.0\" encoding=\"UTF-8\"\x3F>\n\t<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">";
  9. $this->charset = "UTF-8";
  10. $this->footer = "\t</urlset>\n";
  11. $this->baidunews_footer = "</document>";
  12. $this->items = array();
  13. $this->baidunew_items = array();
  14. //生成栏目级别选项
  15. $this->siteid = $this->get_siteid();
  16. $this->categorys = getcache('category_content_'.$this->siteid,'commons');
  17. }
  18. function add_item2($new_item) {
  19. $this->items[] = $new_item;
  20. }
  21. function build( $file_name = null ) {
  22. $map = $this->header . "\n";
  23. foreach ($this->items AS $item){
  24. $map .= "\t\t<url>\n\t\t\t<loc>$item[loc]</loc>\n";
  25. $map .= "\t\t\t<lastmod>$item[lastmod]</lastmod>\n";
  26. $map .= "\t\t\t<changefreq>$item[changefreq]</changefreq>\n";
  27. $map .= "\t\t\t<priority>$item[priority]</priority>\n";
  28. $map .= "\t\t</url>\n\n";
  29. }
  30. $map .= $this->footer . "\n";
  31. if (!is_null($file_name)){
  32. return file_put_contents($file_name, $map);
  33. } else {
  34. return $map;
  35. }
  36. }
  37. function google_sitemap_item($loc, $lastmod = '', $changefreq = '', $priority = '') {
  38. $data = array();
  39. $data['loc'] = $loc;
  40. $data['lastmod'] = $lastmod;
  41. $data['changefreq'] = $changefreq;
  42. $data['priority'] = $priority;
  43. return $data;
  44. }
  45. /**
  46. *
  47. * 百度新闻数组 组成
  48. * @param $title
  49. * @param $link
  50. * @param $description
  51. * @param $text
  52. * @param $image
  53. * @param $keywords
  54. * @param $category
  55. * @param $author
  56. * @param $source
  57. * @param $pubDate
  58. */
  59. function baidunews_item($title, $link = '', $description = '',$text = '',$image = '', $keywords = '',$category = '',$author = '',$source='',$pubDate='') {
  60. $data = array();
  61. $data['title'] = $title;
  62. $data['link'] = $link;
  63. $data['description'] = $description;
  64. $data['text'] = $text;
  65. $data['image'] = $image;
  66. $data['keywords'] = $keywords;
  67. $data['category'] = $category;
  68. $data['author'] = $author;
  69. $data['source'] = $source;
  70. $data['pubDate'] = $pubDate;
  71. return $data;
  72. }
  73. function add_baidunews_item($new_item){
  74. $this->baidunew_items[] = $new_item;
  75. }
  76. function baidunews_build( $file_name = null ,$this_domain,$email,$time) {
  77. //百度头部
  78. $this->baidunews = '';
  79. $this->baidunews = "<?xml version=\"1.0\" encoding=\"".CHARSET."\" ?>\n";
  80. $this->baidunews .= "<document>\n";
  81. $this->baidunews .= "<webSite>".$this_domain."</webSite>\n";
  82. $this->baidunews .= "<webMaster>".$email."</webMaster>\n";
  83. $this->baidunews .= "<updatePeri>".$time."</updatePeri>\n";
  84. foreach ($this->baidunew_items AS $item){
  85. $this->baidunews .= "<item>\n";
  86. $this->baidunews .= "<title>".$item['title']."</title>\n";
  87. $this->baidunews .= "<link>".$item['link']."</link>\n";
  88. $this->baidunews .= "<description>".$item['description'] ."</description>\n";
  89. $this->baidunews .= "<text>".$item['text']."</text>\n";
  90. $this->baidunews .= "<image>".$item['image']."</image>\n";
  91. $this->baidunews .= "<keywords>".$item['keywords']."</keywords>\n";
  92. $this->baidunews .= "<category>".$item['category']."</category>\n";
  93. $this->baidunews .= "<author>".$item['author']."</author>\n";
  94. $this->baidunews .= "<source>".$item['source']."</source>\n";
  95. $this->baidunews .= "<pubDate>".$item['pubDate']."</pubDate>\n";
  96. $this->baidunews .= "</item>\n";
  97. }
  98. $this->baidunews .= $this->baidunews_footer . "\n";
  99. if (!is_null($file_name)){
  100. return file_put_contents($file_name, $this->baidunews);
  101. } else {
  102. return $this->baidunews;
  103. }
  104. }
  105. /**
  106. *
  107. * Enter 生成google sitemap, 百度新闻协议
  108. */
  109. function set () {
  110. $hits_db = pc_base::load_model('hits_model');
  111. $dosubmit = isset($_POST['dosubmit']) ? $_POST['dosubmit'] : $_GET['dosubmit'];
  112. //读站点缓存
  113. $siteid = $this->siteid;
  114. $sitecache = getcache('sitelist','commons');
  115. //根据当前站点,取得文件存放路径
  116. $systemconfig = pc_base::load_config('system');
  117. $html_root = substr($systemconfig['html_root'], 1);
  118. //判断当前站点目录,是PHPCMS则把文件写到根目录下, 不是则写到分站目录下.(分站目录用由静态文件路经html_root和分站目录dirname组成)
  119. if($siteid==1){
  120. $dir = PHPCMS_PATH;
  121. }else {
  122. $dir = PHPCMS_PATH.$html_root.DIRECTORY_SEPARATOR.$sitecache[$siteid]['dirname'].DIRECTORY_SEPARATOR;
  123. }
  124. //模型缓存
  125. $modelcache = getcache('model','commons');
  126. //获取当前站点域名,下面生成URL时会用到.
  127. $this_domain = substr($sitecache[$siteid]['domain'], 0,strlen($sitecache[$siteid]['domain'])-1);
  128. if($dosubmit) {
  129. //生成百度新闻
  130. if($_POST['mark']) {
  131. $baidunum = $_POST['baidunum'] ? intval($_POST['baidunum']) : 20;
  132. if($_POST['catids']=="")showmessage(L('choose_category'), HTTP_REFERER);
  133. $catids = $_POST['catids'];
  134. $catid_cache = $this->categorys;//栏目缓存
  135. $this->content_db = pc_base::load_model('content_model');
  136. foreach ($catids as $catid) {
  137. $modelid = $catid_cache[$catid]['modelid'];//根据栏目ID查出modelid 进而确定表名,并结合栏目ID:catid 检索出对应栏目下的新闻条数
  138. $this->content_db->set_model($modelid);
  139. $result = $this->content_db->select(array('catid'=>$catid,'status'=>99), '*', $limit = "0,$baidunum", 'id desc');
  140. //重设表前缀,for循环时用来查,文章正文
  141. $this->content_db->table_name = $this->content_db->table_name.'_data';
  142. foreach ($result as $arr){
  143. //把每一条数据都装入数组中
  144. extract($arr);
  145. if(!preg_match('/^(http|https):\/\//', $url)){
  146. $url = $this_domain.$url;
  147. }
  148. if($thumb != ""){
  149. if(!preg_match('/^(http|https):\/\//', $thumb)){
  150. $thumb = $this_domain.$thumb;
  151. }
  152. }
  153. //取当前新闻模型 附属表 取 新闻正文
  154. $url = new_html_special_chars($url);
  155. $description = new_html_special_chars(strip_tags($description));
  156. //根据本条ID,从对应tablename_data取出正文内容
  157. $content_arr = $this->content_db->get_one(array('id'=>$id),'content');
  158. $content = new_html_special_chars(strip_tags($content_arr['content']));
  159. //组合数据
  160. $smi = $this->baidunews_item($title,$url,$description,$content,$thumb, $keywords,$category,$author,$source,date('Y-m-d', $inputtime));//推荐文件
  161. $this->add_baidunews_item($smi);
  162. }
  163. }
  164. $baidunews_file = $dir.'baidunews.xml';
  165. @mkdir($dir,0777,true);
  166. $this->baidunews_build($baidunews_file,$this_domain,$_POST['email'],$_POST['time']);
  167. }
  168. //生成网站地图
  169. $content_priority = $_POST['content_priority'];
  170. $content_changefreq = $_POST['content_changefreq'];
  171. $num = $_POST['num'] ? intval($_POST['num']) : 100;
  172. $today = date('Y-m-d');
  173. $domain = $this_domain;
  174. //生成地图头部 -第一条
  175. $smi = $this->google_sitemap_item($domain, $today, 'daily', '1.0');
  176. $this->add_item2($smi);
  177. $this->content_db = pc_base::load_model('content_model');
  178. //只提取该站点的模型.再循环取数据,生成站点地图.
  179. $modelcache = getcache('model','commons');
  180. $new_model = array();
  181. foreach ($modelcache as $modelid => $mod){
  182. if($mod['siteid']==$siteid){
  183. $new_model[$modelid]['modelid'] = $modelid;
  184. $new_model[$modelid]['name'] = $mod['name'];
  185. }
  186. }
  187. foreach($new_model as $modelid=>$m) {//每个模块取出num条数据
  188. $this->content_db->set_model($modelid);// 或者 $this->conetnt_db->set_model($modelid);
  189. $result = $this->content_db->select(array('status'=>99), '*', $limit = "0,$num", $order = 'inputtime desc');
  190. foreach ($result as $arr){
  191. if(substr($arr['url'],0,1)=='/'){
  192. $url = new_html_special_chars(strip_tags($domain.$arr['url']));
  193. }else {
  194. $url = new_html_special_chars(strip_tags($arr['url']));
  195. }
  196. $hit_r = $hits_db->get_one(array('hitsid'=>'c-'.$modelid.'-'.$arr['id']));
  197. if($hit_r['views']>1000) $content_priority = 0.9;
  198. $smi = $this->google_sitemap_item($url, $today, $content_changefreq, $content_priority);//推荐文件
  199. $this->add_item2($smi);
  200. }
  201. }
  202. $sm_file = $dir.'sitemaps.xml';
  203. if($this->build($sm_file)){
  204. showmessage(L('create_success'), HTTP_REFERER);
  205. }
  206. } else {
  207. $tree = pc_base::load_sys_class('tree');
  208. $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ','&nbsp;&nbsp;&nbsp;├─ ','&nbsp;&nbsp;&nbsp;└─ ');
  209. $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
  210. $categorys = array();
  211. foreach($this->categorys as $catid=>$r) {
  212. if($this->siteid != $r['siteid']) continue;
  213. if($r['type'] && $r['child']=='0'){//如果是单网页并且,没有子类了
  214. continue;
  215. }
  216. if($modelid && $modelid != $r['modelid']) continue;
  217. $r['disabled'] = $r['child'] ? 'disabled' : '';
  218. $categorys[$catid] = $r;
  219. }
  220. $str = "<option value='\$catid' \$selected \$disabled>\$spacer \$catname</option>";
  221. $tree->init($categorys);
  222. $string .= $tree->get_tree(0, $str);
  223. include $this->admin_tpl('googlesitemap');
  224. }
  225. }
  226. }
  227. ?>