<?php

require_once('_approot.php');
$action=$_GET['action'];
$skiplist=explode(",","ltd,co,co.,ltd.,company,hebei,china,shijiazhuang,tt,lanscend,Lanscend 001");

$config_sql="select * from ls_config ";
$config_query=$opendb->query($config_sql);
$config_row=mysqli_fetch_assoc($config_query);
$key = false;

// 处理siteKeywords
$thisSiteKeywords= str_replace("|",",",$config_row['siteKeywords']);
$thisSiteKeywords= str_replace("，",",",$thisSiteKeywords);
$thisSiteKeywords= str_replace("-",",",$thisSiteKeywords);

if(substr_count($thisSiteKeywords,",") > 0){
	$keywords = explode(",",$thisSiteKeywords);
	$key = false;
}elseif(strlen($config_row['siteKeywords']) > 0){
	$keywords[] = $config_row['siteKeywords'];
	$key = true;
}

// 处理title
$thisTitle= str_replace("|",",",$config_row['companyName']);
$thisTitle= str_replace("，",",",$thisTitle);
$thisTitle= str_replace("-",",",$thisTitle);
if(substr_count($thisTitle,",") > 0){
$keywords1 = explode(",",$thisTitle);}
$keywords =array_merge($keywords1,$keywords); //合并两个数组

$F = create_function('$a, $b', 'return(strLen($a)< strLen($b));');   
// 临时函数 回调
usort($keywords, $F);  

switch($action){
	case "clear":
		//清空数据表
		$sql = "TRUNCATE TABLE ls_internal";
		$opendb->query($sql);
		//$code = "update  ls_functioncode  set code='' where codeID='internal' ";
		//$opendb->query($code);
		$ls_msg="已清除全部内链关键词";
		
	break;
	
	case "add":
		
		//array_push($keywords, $config_row['companyName'], mb_substr(strip_tags($config_row['companyName']),0,5, 'utf-8'));

		for($index = 0; $index < count($keywords); $index++){
			
			$keyw = trim($keywords[$index]);
			if(strin($keyw,$skiplist)) continue; // 在排除列表 跳过
			
			$select_sql2="select * from ls_internal where title='".$keyw."' ";
			$select_query2=$opendb->query($select_sql2);
			$select_rows2=mysqli_num_rows($select_query2); //总数
			if($select_rows2>0 || $keyw==""  || strtolower($keyw) =="and"|| strtolower($keyw) =="or"|| strtolower($keyw) =="not"){ //有相同值，不安全值 360safe
				//echo "repeat";
			}
			else{
				$select_sql2="select * from ls_internal ";
				$select_query2=$opendb->query($select_sql2);
				$select_rows2=mysqli_num_rows($select_query2); //总数
				$url=getproducturl($keyw);
				$insert_sql="insert into ls_internal(title,url,Num,enable) values ('".$keyw."','".$url."',".($select_rows2+1).",1)";			
		
				if ($opendb->query($insert_sql)){
					//echo "success";
				}else{
					//echo "fail";
				}
			}
		}
				$ls_msg="已提取全部内链关键词";

	break;
}
echo $ls_msg;
$opendb->close();

?>