<?php

require_once('inc/conn.php');
$opendb = new mydb();
require_once('inc/char_set.php');
require_once('inc/ls_syscode.php');

$action=$_GET['action'];

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

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

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

// 处理title
$thisTitle= str_replace("|",",",$config_rows['companyName']);
$thisTitle= str_replace("，",",",$thisTitle);
$thisTitle= str_replace("-",",",$thisTitle);
if(substr_count($thisTitle,",") > 0){
$keywords1 = explode(",",$thisTitle);}

$keywords =array_merge($keywords1,$keywords); //合并两个数组

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

		for($index = 0; $index < count($keywords); $index++){
			
			$keyw = trim($keywords[$index]);
			
			$select_sql2="select * from ls_internal where title='".$keyw."' ";
			$select_query2=$opendb->query($select_sql2);
			$select_rows2=mysql_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=mysql_num_rows($select_query2); //总数
				$insert_sql="insert into ls_internal(title,Num,enable) values ('".$keyw."',".($select_rows2+1).",1)";
		
				if ($opendb->query($insert_sql)){
					//echo "success";
				}else{
					//echo "fail";
				}
			}
		}
		
	break;
}

$opendb->close();

?>