compile.php 3.15 KB
<?php
require_once dirname(__FILE__).'/../db/DbMysqli.class.php';
require_once dirname(__FILE__).'/keyword.class.php';
$host = str_replace("www.",'',$_SERVER['HTTP_HOST']);
$urls = array(
		'wap'=>'http://wap.'.$host.'/BASE/util/keyword/compile.php',
        'android'=>'http://android.'.$host.'BASE/util/keyword/compile.php',
		'html5'=>'http://s.'.$host.'/BASE/util/keyword/compile.php',
		'iphone'=>'http://iphone.'.$host.'/BASE/util/keyword/compile.php'
	);
if(isset($_POST['params']))
{
	$params = json_decode($_POST['params'], true);
	$actionName = $params['actionName'];
	$moduleID = intval($params['moduleID']);
	$moduleKey = $params['moduleKey'];
}
else
{
	exit();
}
error_log($_POST['params'],3,dirname(__FILE__).'/log.log');
$dictionaryDIR = '';
//模块
switch($actionName)
{
	case 'nickName':
		$dictionaryDIR = 'nickName';
		break;
	case 'register':
		$dictionaryDIR = 'register';
		break;
	case 'chat':
		$dictionaryDIR = '';
		break;
	case 'test':
		$dictionaryDIR = 'test';
		break;
	default:
		$dictionaryDIR = 'test';
		break;
		//exit();		
}
$dictionaryDIR = dirname(__FILE__).'/cache/'.$dictionaryDIR;
require $dictionaryDIR.'/dictionary.php';
$DBConn = new DbMysqli();
$keyword = new keyword();
$sql =" SELECT module_key FROM oss_module";
$sql.= " WHERE id=".$moduleID;
$dbModuleKey = $DBConn->getOne($sql);
//如果密钥是否相等
if(strcmp($dbModuleKey, $moduleKey)!=0)
{
	exit();
}
$updateTime = $keyword->readOver($dictionaryDIR.'/updateTime.txt');
$startPos = 0;
$length = 100;
while(true)
{
	$sql = " SELECT keyword, key_type, up_time FROM oss_module_keyword";
	$sql.= " WHERE module_id=".$moduleID;
	$sql.= " AND up_time>='$updateTime' ";
	$sql.=" ORDER BY up_time DESC ";
	$sql.=" LIMIT $startPos, $length"; 
	$DBWords = $DBConn->getAll($sql);
	if(empty($DBWords))
	{
		break;
	}
	else
	{
		$updateTime = $DBWords[count($DBWords)-1][2];
		$startPos+=$length;
	}
	$words = array();
	$adds = array(
			PROHIBITION_STATE =>0,
			REPLACE_STATE =>0,
			CHECK_STATE =>0,
		);
	foreach($DBWords as $word)
	{
	    $type = intval($word[1]);
	    $keywordValue = $word[0];
		$add = $adds[$type]++;
		$words[$type][$add] = $keywordValue;
	}
	if(!isset($dictionaryWords))
	{
		$dictionaryWords = array();
	}
	$dictionaryWords = $keyword->compileWord($words, $dictionaryWords);
	$keyword->writeDictionaryFile($dictionaryWords, $dictionaryDIR.'/dictionary.php');
}
$params = array(
	'actionName'=> $actionName,
    'moduleID'=> $moduleID,
	'moduleKey'=> $moduleKey,
	'updateTime'=>$updateTime
);
foreach($urls as $key=>$url)
{
	$result = connect($url, json_encode($params));
	if(intval($result))
	{
		echo $key;
	}
}
$keyword->writeOver($dictionaryDIR.'/updateTime.txt',$updateTime);
//清除key值
$sql = "UPDATE oss_module SET module_key='' where id=".$moduleID;
$DBConn->update($sql);
unset($DBConn);
function connect($url, $params)
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL,$url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_HEADER, false);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);//seconds
	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch,CURLOPT_POSTFIELDS, 'params='.$params);
	$result = curl_exec($ch);
	curl_close($ch);
	return $result;
}
?>