keyword.inc.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <?php
  2. if(!defined('IN_DISCUZ')) {
  3. exit('Access Denied');
  4. }
  5. require_once(DISCUZ_ROOT.'source/plugin/milu_seotool/config.inc.php');
  6. sload('F:keyword,F:copyright');
  7. $header_arr = array('keyword_set', 'forum_keyword', 'portal_keyword', 'trend_data_count');
  8. if(!VIP) unset($header_arr[array_search('portal_keyword', $header_arr)]);
  9. $args = array('default_ac' => 'keyword_set');
  10. $keyword_set = tool_common_get('keyword');
  11. $keyword_set['keyword_type'] = dunserialize($keyword_set['keyword_type']);
  12. seo_tpl($args);
  13. function keyword_set(){
  14. global $_G,$pluin_info,$keyword_type_arr,$keyword_set;
  15. if($_GET['editsubmit']){
  16. $set = $_POST['set'];
  17. $set['keyword_type'] = serialize($_POST['keyword_type']);
  18. //计划任务要重新设定
  19. tool_common_set('milu_seotool_cron', array('keyword_rank_check' => 0));
  20. tool_common_set('keyword', $set);
  21. cpmsg(stlang('op_success'), PLUGIN_GO."keyword", 'succeed');
  22. }else{
  23. $info = $keyword_set;
  24. $show .= seoOutput::show_title(stlang('keyword_set_title'));
  25. $show .= seoOutput::show_tr(
  26. array(
  27. 'name' => stlang('auto_check_rank'),
  28. 'desc' => stlang('auto_check_rank_notice'),
  29. 'arr' => array(
  30. 'name' => 'is_auto_rank',
  31. 'info' => $info,
  32. 'int_val' => 2,
  33. 'lang_type' => 2,
  34. ),
  35. )
  36. ,'radio');
  37. $show .= seoOutput::show_tr(
  38. array(
  39. 'name' => stlang('tag_auto_add_keyword'),
  40. 'desc' => stlang('tag_auto_add_keyword_notice'),
  41. 'arr' => array(
  42. 'name' => 'tag_add_keyword',
  43. 'info' => $info,
  44. 'int_val' => 2,
  45. 'lang_type' => 2,
  46. ),
  47. )
  48. ,'radio');
  49. $info['show'] = $show;
  50. $info['tpl'] = 'common_set';
  51. return $info;
  52. }
  53. }
  54. function forum_keyword(){
  55. global $_G;
  56. $update = intval($_GET['update']) ? 1 : 0;
  57. if($update) {
  58. tool_common_set('keyword', array('rank_data' => array(), 'keyword_data' => array(), 'zhishu_cache_data' => array()));
  59. }
  60. $cat_arr = forum_data_format($update);
  61. $info['cat_arr'] = $cat_arr;
  62. $info['show'] = $show;
  63. $info['data_type'] = 0;
  64. $info['tpl'] = 'keyword_list';
  65. return $info;
  66. }
  67. //数据趋势
  68. function trend_data_count(){
  69. global $_G;
  70. $keyword_set = tool_common_get('keyword');
  71. $view_info['keyword'] = dstripslashes($_GET['keyword']);
  72. $view_info['start_dateline'] = $_POST['start_dateline'] ? strtotime($_POST['start_dateline']) : '';
  73. $view_info['end_dateline'] = $_POST['end_dateline'] ? strtotime($_POST['end_dateline']) : '';
  74. $view_info['start_dateline'] = $view_info['start_dateline'] ? $view_info['start_dateline'] : $_G['timestamp'] - 3600*24*7;
  75. $view_info['end_dateline'] = $view_info['end_dateline'] ? $view_info['end_dateline'] : $_G['timestamp'];
  76. $dateline_arr = st_dayRange($view_info['start_dateline'], $view_info['end_dateline']);
  77. $from = $dateline_arr[1]['from'];
  78. $end = $dateline_arr[count($dateline_arr)]['end'];
  79. if($_GET['inajax'] || $_POST['submit'] || $view_info['keyword']){
  80. $title = date('Y-m-d', $from).' '.stlang('to_').' '.date('Y-m-d', $end). ' '.stlang('trend_flash_title2');
  81. $sql = " AND (daytime<'".date('Ymd', $end)."' OR daytime='".date('Ymd', $end)."') AND (daytime>'".date('Ymd', $from)."' OR daytime='".date('Ymd', $from)."')";
  82. if($view_info['keyword']){
  83. $keyword_arr = array($view_info['keyword']);
  84. }else{
  85. $query = DB::query("SELECT * FROM ".DB::table('milu_seotool_keyword')." LIMIT 0,150");
  86. $items = array();
  87. while($rs = DB::fetch($query)) {
  88. $keyword_arr[] = $rs['name'];
  89. }
  90. }
  91. $title_arr = $dateline_arr;
  92. $rank_value_arr = array();
  93. foreach($keyword_arr as $k0 => $kw){
  94. $query = DB::query("SELECT k.*,r.* FROM ".DB::table('milu_seotool_keyword')." as k Inner Join ".DB::table('milu_seotool_keyword_rank')." as r ON r.kid=k.kid WHERE name='".st_addslashes($kw)."' $sql ");
  95. while($rs = DB::fetch($query)) {
  96. foreach($dateline_arr as $k => $v){
  97. if($rs['daytime'] == date('Ymd',$v['end'])){
  98. $dateline_arr[$k]['rank'] = $rs['rank'];
  99. $rank_value_arr[$kw][$k] = $rs['rank'];
  100. }
  101. }
  102. }
  103. }
  104. if(is_array($dateline_arr)){
  105. $data_arr['keyword_rank'] = array(
  106. 0 => array(
  107. 'name' => stlang('keyword'),
  108. 'value' => $keyword_arr
  109. ),
  110. );
  111. foreach($dateline_arr as $k => $v){
  112. $i = 0;
  113. $data_arr['keyword_rank'][$k+1]['name'] = $v['name'];
  114. foreach($keyword_arr as $k0 => $v0){
  115. $data_arr['keyword_rank'][$k+1]['value'][$i] = $rank_value_arr[$v0][$k];
  116. $i++;
  117. }
  118. }
  119. }
  120. $show_arr[] = seoOutput::show_table($data_arr, 'keyword_rank', array('caption' => $_GET['inajax'] ? '<span style="float:right"><a href="?'.PLUGIN_GO.'keyword&myac=trend_data_count&keyword='.$view_info['keyword'].'">'.stlang('view_more').'</a></span>' : ''));
  121. $info['show'] = implode('', $show_arr);
  122. }
  123. if($_GET['inajax']) show_seotool_window(stlang('one_week_ranks'), $info['show'], array('f' => 1));
  124. $info['view_show'] = $view_info;
  125. $info['start_dateline'] = date('Y-m-d', $view_info['start_dateline']);
  126. $info['end_dateline'] = date('Y-m-d', $view_info['end_dateline']);
  127. $info['tpl'] = 'keyword_trend';
  128. return $info;
  129. }
  130. function forum_data_format($update = 0){
  131. global $_G;
  132. loadcache('milu_seotool_forum_catdata');
  133. if( !($cat_arr = $_G['cache']['milu_seotool_forum_catdata'] ) || $update == 1){
  134. $query = DB::query("SELECT f.fid, f.type, f.name, f.fup, ff.keywords, ff.seodescription, ff.seotitle FROM ".DB::table('forum_forum')." f
  135. LEFT JOIN ".DB::table('forum_forumfield')." ff ON ff.fid=f.fid LEFT JOIN ".DB::table('forum_access')." a ON a.fid=f.fid AND a.allowview>'0' WHERE f.status<>'3' ORDER BY f.type, f.displayorder");
  136. while($rs = DB::fetch($query)) {
  137. $forumseoset = array(
  138. 'seotitle' => $rs['seotitle'],
  139. 'seokeywords' => $rs['keywords'],
  140. 'seodescription' => $rs['seodescription']
  141. );
  142. list($navtitle, $metadescription, $metakeywords) = get_seosetting('threadlist', array('fgroup' => $rs['name']), array('seokeywords' => $rs['keywords']), $forumseoset);
  143. $caturl_str = $rs['fup'] > 0 ? 'forum.php?mod=forumdisplay&fid='.$rs['fid'] : 'forum.php?gid='.$rs['fid'];
  144. $cat_arr[$rs['fid']] = array('catid' => $rs['fid'], 'upid' => $rs['fup'], 'catname' => $rs['name'], 'keywords' => explode(',', $metakeywords), 'caturl' => $_G['siteurl'].$caturl_str);
  145. }
  146. foreach($cat_arr as $k => $v){
  147. if($v['upid'] > 0){
  148. $cat_arr[$v['upid']]['children'][$k] = $k;
  149. }
  150. }
  151. foreach($cat_arr as $k => $v){
  152. if($v['upid'] == 0 && !$v['children']) unset($cat_arr[$k]);
  153. }
  154. save_syscache('milu_seotool_forum_catdata', $cat_arr);
  155. }
  156. return $cat_arr;
  157. }
  158. function show_keyword_info_tr($keyword_info){
  159. if(!$keyword_info) return;
  160. $str = "<td width=\"60\"><p><a href=\"javascript:void(0);\" onclick=\"keyword_ajax_bat('".$keyword_info['args_info']."');\">".stlang('bat_query')."</a></td><td width=\"230\">".$keyword_info['keyword']."</td><td width=\"65\">".$keyword_info['zhishu']."</td><td width=\"65\">".$keyword_info['count']."</td><td width=\"100\">".$keyword_info['density']."</td>
  161. <td width=\"300\">".$keyword_info['rank']."</td><td width=\"65\">".$keyword_info['trend']."</td>";
  162. return $str;
  163. }
  164. function st_ajax_keyword(){
  165. global $_G;
  166. $data_type = intval($_GET['data_type']);
  167. $catid = intval($_GET['catid']);
  168. $keyword = st_format_url(st_iconv($_GET['keyword'], 'UTF-8', CHARSET), 1, 0);
  169. //关键词排名、排名趋势
  170. $rank_data = get_rank_data($keyword);
  171. //指数、密度、频率
  172. if($data_type == 1){//门户
  173. loadcache('milu_seotool_portal_catdata');
  174. $cat_arr = $_G['cache']['milu_seotool_portal_catdata'];
  175. $caturl = $cat_arr[$catid]['caturl'];
  176. }else{
  177. loadcache('milu_seotool_forum_catdata');
  178. $cat_arr = $_G['cache']['milu_seotool_forum_catdata'];
  179. $caturl = $cat_arr[$catid]['caturl'];
  180. }
  181. $keywords = $cat_arr[$catid]['keywords'];
  182. $keyword_data = keyword_density_set($caturl, $keywords, 2);
  183. $result_data = $keyword_data[$keyword];
  184. if(!$result_data) $result_data = array('count' => 0, 'density' => '0.0%', 'status' => 1);
  185. $result_data['zhishu'] = baidu_zhishu($keyword);//指数
  186. $result_data['rank'] = implode(',', $rank_data['rank']);
  187. $result_data['trend'] = $rank_data['trend'];
  188. return json_encode($result_data);
  189. }
  190. function st_redirect_zhishu(){
  191. $keyword = st_format_url(st_iconv($_GET['keyword'], 'UTF-8', 'GBK'), 1, 0);
  192. $url = 'http://index.baidu.com/main/word.php?word='.$keyword;
  193. dheader('location: '.$url);
  194. }
  195. function show_keyword($data_arr, $catid = 0){
  196. global $_G;
  197. if(!$data_arr || !$data_arr[0]) return;
  198. $str = '';
  199. $info = array();
  200. $data_type = $_GET['myac'] == 'portal_keyword' ? 1 : 0;
  201. $keyword_count_js = '<script type="text/javascript" language="javascript">cat_keyword_count('.$catid.')</script>';
  202. $keyword_data = tool_common_get('keyword');
  203. $count_keyword_data = $keyword_data['keyword_data'];
  204. $zhishu_cache_data = $keyword_data['zhishu_cache_data']['data'];
  205. $rank_cache_data = $keyword_data['rank_data']['data'];
  206. $data_flag = 1;
  207. if($keyword_data){
  208. foreach((array)$data_arr as $k => $v){
  209. $v_md5 = md5($v);
  210. if(!isset($count_keyword_data[$v]) || !isset($zhishu_cache_data[$v_md5])) {
  211. $data_flag = 0;
  212. continue;
  213. }
  214. }
  215. }else{
  216. $data_flag = 0;
  217. }
  218. if($data_flag == 1) $keyword_count_js = '';//存在缓存数据
  219. $result_data = array();
  220. foreach($data_arr as $k => $v){
  221. $key = $catid.md5($v);
  222. $v_md5 = md5($v);
  223. $info = $count_keyword_data[$v];
  224. $key_id = $catid.'_'.$key;
  225. $query_href = '<a class="query_keyword" key_id="'.$key_id.'" kw="'.$v.'" catid="'.$catid.'" href="javascript:void(0)">'.stlang('query').'</a>';
  226. //密度、频率
  227. if(!$info){
  228. $info['density'] = $query_href;
  229. $info['count'] = $query_href;
  230. }
  231. //指数
  232. if(!isset($zhishu_cache_data[$v_md5])){
  233. $info['zhishu'] = $query_href;
  234. }else{
  235. $info['zhishu'] = $zhishu_cache_data[$v_md5] > -1 ? $zhishu_cache_data[$v_md5] : stlang('view');
  236. $info['zhishu'] = '<a href="http://index.baidu.com/main/word.php?word='.$v.'" target="_blank">'.$info['zhishu'].'</a>';
  237. }
  238. //排名
  239. $rank_info = $rank_cache_data[$v_md5];
  240. if(!$rank_info){
  241. $rank_info['rank'] = $query_href;
  242. $rank_info['trend'] = $query_href;
  243. }else{
  244. $rank_info['rank'] = $rank_info['rank'][0] ? implode(',', $rank_info['rank']) : '-';
  245. $rank_info['rank'] = '<a href="http://www.baidu.com/s?wd='.$v.'" target="_brank">'.$rank_info['rank'].'</a>';
  246. $img_type = 'noupdown';
  247. $num = '';
  248. if(intval($rank_info['trend']) > 0 || (string)$rank_info['trend'] == '+'){
  249. $img_type = 'up';
  250. if($rank_info['trend'] != '+') $num = abs($rank_info['trend']);
  251. }else if($rank_info['trend'] < 0 || (string)$rank_info['trend'] == '-'){
  252. $img_type = 'down';
  253. if($rank_info['trend'] != '-') $num = abs($rank_info['trend']);
  254. }
  255. $rank_info['trend'] = '<a onclick="get_keyword_rank_trend(\''.$v.'\')" href="javascript:void(0)"><img style=" margin-top:5px;" name="" src="'.PLUGIN_URL.'static/image/'.$img_type.'.gif" alt="" />'.$num.'</a>';
  256. }
  257. $field_arr = array( 'zhishu', 'count', 'density', 'rank', 'trend');
  258. foreach($field_arr as $v2){
  259. $data_info = $v2 == 'rank' || $v2 == 'trend' ? $rank_info : $info;
  260. $result_data[$v2] .= '<div class="keyword_show"><span class="k_'.$v2.'" id="'.$v2.$key_id.'">'.$data_info[$v2].'</span></div>';
  261. }
  262. $result_data['keyword'] .= '<div class="keyword_show"><span class="keyword" title="'.$v.'">'.cutstr($v, 30).'</span></div>';
  263. $result_data['args_info'][] = $catid.'|'.$key_id.'|'.$v;
  264. }
  265. $result_data['args_info'] = implode('@', $result_data['args_info']);
  266. return $result_data;
  267. }
  268. function st_ajax_keyword_list(){
  269. $s = trim(st_format_url($_GET['q']));
  270. if(!$s) return;
  271. $query = DB::query("SELECT * FROM ".DB::table('milu_seotool_keyword')." WHERE name like '".st_addslashes($s)."%' LIMIT 0,20");
  272. $items = array();
  273. while($rs = DB::fetch($query)) {
  274. $name = st_iconv($rs['name'], CHARSET, 'UTF-8');
  275. $items[] = $name;
  276. }
  277. if(!$items) return;
  278. return json_encode($items);
  279. }
  280. function baidu_zhishu($keyword, $update = 0){
  281. global $_G;
  282. $client_info = get_client_info();
  283. if(!$client_info || !$client_info['domain']) return -3;
  284. $cache_key = 'zhishu_cache_data';
  285. $keyword_data = tool_common_get('keyword');
  286. $keyword_cache_data = $keyword_data[$cache_key];
  287. $key = md5($keyword);
  288. $data = $keyword_cache_data['data'][$key];
  289. $diff_dateline = $_G['timestamp'] - $keyword_cache_data['dateline'];
  290. if($diff_dateline > 24*3600*2) {
  291. unset($data);
  292. tool_common_set('keyword', array($cache_key => array()) );
  293. }
  294. if(!isset($data) || $update == 1 ){
  295. $key_data = get_site_key();
  296. $data = st_rpcClient()->RPC_baidu_zhishu($keyword, $key_data);
  297. if(is_object($data)){
  298. if($data->Message || $data->Number == 0) {
  299. return -2;
  300. }
  301. }
  302. if($data < 0) return -1;
  303. $keyword_cache_data['data'][$key] = $data;
  304. $st_cache_data['dateline'] = $keyword_cache_data['dateline'] ? $keyword_cache_data['dateline'] : $_G['timestamp'];
  305. $st_cache_data['data'] = $keyword_cache_data['data'];
  306. tool_common_set('keyword', array($cache_key => $st_cache_data));
  307. }
  308. return $data;
  309. }
  310. function keyword_density_set($caturl, $keyword_arr = array(), $get = 1){
  311. if(!$keyword_arr) return;
  312. $keyword_set = tool_common_get('keyword');
  313. $content = st_get_contents($caturl, array('cache' => 3600*24*5));
  314. if(!$content) return;
  315. $content = st_striptext($content);
  316. $content_lenth = strlen($content);
  317. $count_data = array();
  318. foreach($keyword_arr as $k => $v){
  319. $count = substr_count($content, $v);
  320. $density = strlen($v) * $count / $content_lenth;
  321. $status = ($density < 0.08 && $density > 0.02) ? 1 : 0;
  322. $density = sprintf("%01.1f", $density*100).'%';
  323. $count_data[$v] = $keyword_set['keyword_data'][$v] = array('count' => $count, 'density' => $density, 'status' => $status);
  324. }
  325. tool_common_set('keyword', $keyword_set);
  326. if($get != 1) return $count_data;
  327. return $keyword_set['keyword_data'];
  328. }
  329. ?>