function.sitemap.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. if(!defined('IN_DISCUZ')) {
  3. exit('Access Denied');
  4. }
  5. function update_sitemap($sitemap_set = ''){
  6. global $_G;
  7. if(!$sitemap_set) { //计划任务调用的时候
  8. $sitemap_set = tool_common_get('sitemap');
  9. $sitemap_set['max_article_num'] = $sitemap_set['max_article_num'] ? $sitemap_set['max_article_num'] : 500;
  10. $sitemap_set['max_threads_num'] = $sitemap_set['max_threads_num'] ? $sitemap_set['max_threads_num'] : 500;
  11. }
  12. $sitemap_set['sitemap_type'] = dunserialize($sitemap_set['sitemap_type']);
  13. if(!function_exists('gzopen')) unset($sitemap_set['sitemap_type'][array_search('gz', $sitemap_set['sitemap_type'])]);
  14. $sitemap_data = array();
  15. $sitemap_data['catid'] = article_catid_data();
  16. $sitemap_data['fid'] = get_forum_data();
  17. $sitemap_data['thread'] = sitemap_article_data($sitemap_set['max_threads_num'], 0, 0);
  18. $sitemap_data['aritcle'] = sitemap_article_data($sitemap_set['max_article_num'], 0, 1);
  19. $charset = GBK ? 'gb2312' : 'UTF-8';
  20. $xml = "<?xml version=\"1.0\" encoding=\"$charset\"?>\n";
  21. $xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"'."\r\n".'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'."\r\n".'xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9'."\r\n".'http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">'."\r\n".'<!-- created with http://www.56php.com/ -->'."\r\n";
  22. $txt = $html = '';
  23. $url_count = 0;
  24. $xml_is_open = in_array('xml', (array)$sitemap_set['sitemap_type']);//xml
  25. $txt_is_open = in_array('txt',(array) $sitemap_set['sitemap_type']);//txt
  26. $gz_is_open = in_array('gz', (array)$sitemap_set['sitemap_type']);//gz
  27. $html_is_open = in_array('html', (array)$sitemap_set['sitemap_type']);//html
  28. foreach($sitemap_data as $k => $v){
  29. foreach($v as $k1 => $v1){
  30. $url_count++;
  31. //xml地图
  32. $xml .= xml_url_output($v1);
  33. //txt地图
  34. $txt .= $v1['url']."\r\n";
  35. //html地图
  36. $html .= '<tr><td class="lpage"><a href="'.$v1['url'].'" title="'.dhtmlspecialchars($v1['title']).'">'.$v1['title'].'</a></td><td class="lpage" >'.$v1['lastmod'].'</td></tr>'."\r\n";
  37. }
  38. }
  39. $xml .= "</urlset>\n";
  40. $robots_str = @file_get_contents(DISCUZ_ROOT.'/robots.txt');
  41. //html地图
  42. if($html_is_open){
  43. $sitemap['title'] = stlang('sitemap').'-'.$_G['setting']['bbname'];
  44. $sitemap['url_count'] = $url_count;
  45. $sitemap['update_dateline'] = dgmdate($_G['timestamp']);
  46. $sitemap['show_url'] = $html;
  47. ob_start();
  48. include template('milu_seotool:sitemap_tpl');
  49. $sitemap_html = ob_get_contents();
  50. ob_end_clean () ;
  51. file_put_contents(DISCUZ_ROOT.'/sitemap.html', $sitemap_html);
  52. $robots_str .= !strexists($robots_str, 'sitemap.html') ? "\r\n".'Sitemap: '.$_G['siteurl'].'sitemap.html' : '';
  53. }else{
  54. @unlink(DISCUZ_ROOT.'/sitemap.html');
  55. $robots_str = str_replace("\r\n".'Sitemap: '.$_G['siteurl'].'sitemap.html', '', $robots_str);
  56. }
  57. //txt
  58. if($txt_is_open) {
  59. file_put_contents(DISCUZ_ROOT.'/sitemap.txt', $txt);
  60. $robots_str .= !strexists($robots_str, 'sitemap.txt') ? "\r\n".'Sitemap: '.$_G['siteurl'].'sitemap.txt' : '';
  61. }else{
  62. $robots_str = str_replace("\r\n".'Sitemap: '.$_G['siteurl'].'sitemap.txt', '', $robots_str);
  63. @unlink(DISCUZ_ROOT.'/sitemap.txt');
  64. }
  65. //xml
  66. if($xml_is_open) {
  67. file_put_contents(DISCUZ_ROOT.'/sitemap.xml', $xml);
  68. $robots_str .= !strexists($robots_str, 'sitemap.xml') ? "\r\n".'Sitemap: '.$_G['siteurl'].'sitemap.xml' : '';
  69. }else{
  70. @unlink(DISCUZ_ROOT.'/sitemap.xml');
  71. $robots_str = str_replace("\r\n".'Sitemap: '.$_G['siteurl'].'sitemap.xml', '', $robots_str);
  72. }
  73. //gz
  74. if($gz_is_open) {
  75. $robots_str .= !strexists($robots_str, 'sitemap.xml.gz') ? "\r\n".'Sitemap: '.$_G['siteurl'].'sitemap.xml.gz' : '';
  76. create_gzfile(DISCUZ_ROOT.'/sitemap.xml', $xml);
  77. }else{
  78. @unlink(DISCUZ_ROOT.'/sitemap.xml.gz');
  79. $robots_str = str_replace("\r\n".'Sitemap: '.$_G['siteurl'].'sitemap.xml.gz', '', $robots_str);
  80. }
  81. $set['sitemap_url_count'] = $url_count;
  82. $set['updateline'] = $_G['timestamp'];
  83. file_put_contents(DISCUZ_ROOT.'/robots.txt', $robots_str);
  84. return $set;
  85. }
  86. function article_catid_data(){
  87. global $_G;
  88. loadcache('portalcategory');
  89. $cat_arr = $_G['cache']['portalcategory'];
  90. $data = array();
  91. foreach($cat_arr as $k => $v){
  92. $priority = rand(1,10)/10;
  93. $data[] = array('title' => $v['catname'], 'url' => $v['caturl'], 'priority' => $priority, 'changefreq' => 'daily', 'lastmod' => date("Y-m-d"));
  94. }
  95. return $data;
  96. }
  97. function get_forum_data(){
  98. global $_G;
  99. loadcache('forums');
  100. $cat_arr = $_G['cache']['forums'];
  101. $data = array();
  102. foreach($cat_arr as $k => $v){
  103. $priority = rand(1,10)/10;
  104. $data[] = array('title' => $v['name'], 'url' => get_forum_url($v['fid']), 'priority' => $priority, 'changefreq' => 'daily', 'lastmod' => date("Y-m-d"));
  105. }
  106. return $data;
  107. }
  108. function create_gzfile($file, $text){
  109. if(!function_exists('gzopen') ) return FALSE;
  110. $gzfile = $file.'.gz';
  111. // Open the gz file (w9 is the highest compression)
  112. $fp = gzopen ($gzfile, 'w9');
  113. gzwrite ($fp, file_get_contents($file));
  114. gzclose($fp);
  115. return;
  116. }
  117. function get_forum_url($fid){
  118. global $_G;
  119. if(in_array('forum_forumdisplay', (array)$_G['setting']['rewritestatus'])) {
  120. $url = $_G['siteurl'].rewriteoutput('forum_forumdisplay', 1, '', $fid, 1, '', '');
  121. } else {
  122. $url = $_G['siteurl'].'forum.php?mod=forumdisplay&fid='.$fid;
  123. }
  124. return $url;
  125. }
  126. //$data_type 0论坛 1门户
  127. function sitemap_article_data($max_num, $dateline = 0, $data_type = 0){
  128. $where_sql = $dateline > 0 ? " AND dateline>'$dateline'" : '';
  129. sload('F:included');
  130. $count = get_article_count($data_type, $where_sql);
  131. if(!$count) return FALSE;
  132. if($data_type == 1){//门户
  133. $query = DB::query("SELECT aid,title,dateline FROM ".DB::table('portal_article_title')." WHERE status='0' $where_sql ORDER BY dateline DESC LIMIT $max_num");
  134. }else{
  135. $query = DB::query("SELECT tid as aid,subject as title,dateline FROM ".DB::table('forum_thread')." WHERE displayorder='0' $where_sql ORDER BY dateline DESC LIMIT $max_num");
  136. }
  137. while($rs = DB::fetch($query)) {
  138. $priority = rand(1,10)/10;
  139. $data[] = array('title' => $rs['title'], 'url' => get_article_url($rs['aid'], $data_type), 'priority' => $priority, 'changefreq' => 'daily', 'lastmod' => date("Y-m-d", $rs['dateline']));
  140. }
  141. return $data;
  142. }
  143. function xml_url_output($info){
  144. $info['url'] = dhtmlspecialchars($info['url']);
  145. $xml = "<url>\n";
  146. $xml .= "<loc>$info[url]</loc>\n";
  147. $xml .= "<priority>$info[priority]</priority>\n";
  148. $xml .= "<lastmod>$info[lastmod]</lastmod>\n";
  149. $xml .= "<changefreq>$info[changefreq]</changefreq>\n";
  150. $xml .= "</url>\n";
  151. return $xml;
  152. }
  153. ?>