helper_seo.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: helper_seo.php 36278 2016-12-09 07:52:35Z nemohou $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class helper_seo {
  12. public static function get_seosetting($page, $data = array(), $defset = array()) {
  13. global $_G;
  14. $searchs = array('{bbname}');
  15. $replaces = array($_G['setting']['bbname']);
  16. $seotitle = $seodescription = $seokeywords = '';
  17. $titletext = $defset['seotitle'] ? $defset['seotitle'] : $_G['setting']['seotitle'][$page];
  18. $descriptiontext = $defset['seodescription'] ? $defset['seodescription'] : $_G['setting']['seodescription'][$page];
  19. $keywordstext = $defset['seokeywords'] ? $defset['seokeywords'] : $_G['setting']['seokeywords'][$page];
  20. preg_match_all("/\{([a-z0-9_-]+?)\}/", $titletext.$descriptiontext.$keywordstext, $pageparams);
  21. if($pageparams) {
  22. foreach($pageparams[1] as $var) {
  23. $searchs[] = '{'.$var.'}';
  24. if($var == 'page') {
  25. $data['page'] = $data['page'] > 1 ? lang('core', 'page', array('page' => $data['page'])) : '';
  26. }
  27. $replaces[] = $data[$var] ? strip_tags($data[$var]) : '';
  28. }
  29. if($titletext) {
  30. $seotitle = helper_seo::strreplace_strip_split($searchs, $replaces, $titletext);
  31. }
  32. //if($descriptiontext && (isset($_G['makehtml']) || CURSCRIPT == 'forum' || IS_ROBOT || $_G['adminid'] == 1)) {
  33. // $seodescription = helper_seo::strreplace_strip_split($searchs, $replaces, $descriptiontext);
  34. //}
  35. //if($keywordstext && (isset($_G['makehtml']) || CURSCRIPT == 'forum' || IS_ROBOT || $_G['adminid'] == 1)) {
  36. // $seokeywords = helper_seo::strreplace_strip_split($searchs, $replaces, $keywordstext);
  37. //}
  38. if($descriptiontext ) {
  39. $seodescription = helper_seo::strreplace_strip_split($searchs, $replaces, $descriptiontext);
  40. }
  41. if($keywordstext ) {
  42. $seokeywords = helper_seo::strreplace_strip_split($searchs, $replaces, $keywordstext);
  43. }
  44. }
  45. return array($seotitle, $seodescription, $seokeywords);
  46. }
  47. public static function strreplace_strip_split($searchs, $replaces, $str) {
  48. $searchspace = array('(((\s)*\-(\s)*)+)', '(((\s)*\,(\s)*)+)', '(((\s)*\|(\s)*)+)', '(((\s)*\t(\s)*)+)', '(((\s)*_(\s)*)+)');
  49. $replacespace = array('$3-$3', '$3,$3', '$3|$3', '$3 $3', '$3_$3');
  50. return trim(preg_replace($searchspace, $replacespace, str_replace($searchs, $replaces, $str)), ' ,-|_');
  51. }
  52. public static function get_title_page($navtitle, $page){
  53. if($page > 1) {
  54. $navtitle .= ' - '.lang('core', 'page', array('page' => $page));
  55. }
  56. return $navtitle;
  57. }
  58. public static function get_related_link($extent) {
  59. global $_G;
  60. loadcache('relatedlink');
  61. $allextent = array('article' => 0, 'forum' => 1, 'group' => 2, 'blog' => 3);
  62. $links = array();
  63. if($_G['cache']['relatedlink'] && isset($allextent[$extent])) {
  64. foreach($_G['cache']['relatedlink'] as $link) {
  65. $link['extent'] = sprintf('%04b', $link['extent']);
  66. if($link['extent'][$allextent[$extent]] && $link['name'] && $link['url']) {
  67. $links[] = daddslashes($link);
  68. }
  69. }
  70. }
  71. rsort($links);
  72. return $links;
  73. }
  74. public static function parse_related_link($content, $extent) {
  75. global $_G;
  76. loadcache('relatedlink');
  77. $allextent = array('article' => 0, 'forum' => 1, 'group' => 2, 'blog' => 3);
  78. if($_G['cache']['relatedlink'] && isset($allextent[$extent])) {
  79. $searcharray = $replacearray = array();
  80. foreach($_G['cache']['relatedlink'] as $link) {
  81. $link['extent'] = sprintf('%04b', $link['extent']);
  82. if($link['extent'][$allextent[$extent]] && $link['name'] && $link['url']) {
  83. $searcharray[$link[name]] = '/('.preg_quote($link['name']).')/i';
  84. $replacearray[$link[name]] = "<a href=\"$link[url]\" target=\"_blank\" class=\"relatedlink\">$link[name]</a>";
  85. }
  86. }
  87. if($searcharray && $replacearray) {
  88. $_G['trunsform_tmp'] = array();
  89. $content = preg_replace_callback("/(<script\s+.*?>.*?<\/script>)|(<a\s+.*?>.*?<\/a>)|(<img\s+.*?[\/]?>)|(\[attach\](\d+)\[\/attach\])/is", array(__CLASS__, 'parse_related_link_callback_base64_transform_1234'), $content);
  90. $content = preg_replace($searcharray, $replacearray, $content, 1);
  91. $content = preg_replace_callback("/<relatedlink>(.*?)<\/relatedlink>/is", array(__CLASS__, 'parse_related_link_callback_base64_transform_1'), $content);
  92. }
  93. }
  94. return $content;
  95. }
  96. static public function parse_related_link_callback_base64_transform_1234($matches) {
  97. return self::base64_transform('encode', '<relatedlink>', $matches[1].$matches[2].$matches[3].$matches[4], '</relatedlink>');
  98. }
  99. static function parse_related_link_callback_base64_transform_1($matches) {
  100. return self::base64_transform('decode', '', $matches[1], '');
  101. }
  102. public static function base64_transform($type, $prefix, $string, $suffix) {
  103. global $_G;
  104. if($type == 'encode') {
  105. $_G['trunsform_tmp'][] = base64_encode(str_replace("\\\"", "\"", $string));
  106. return $prefix.(count($_G['trunsform_tmp']) - 1).$suffix;
  107. } elseif($type == 'decode') {
  108. return $prefix.base64_decode($_G['trunsform_tmp'][$string]).$suffix;
  109. }
  110. }
  111. }
  112. ?>