<?php
@set_time_limit(0);
//error_reporting(0);
include_once("includes/global.php");

if(!empty($_POST['search_flag'])&&!empty($_POST['key']))
{	
	$sql="select * from ".SWORD." where keyword like '$_POST[key]%' or char_index like '$_POST[key]%' order by nums desc limit 0,10";
	$db->query($sql);
	$re=$db->getRows();
	foreach($re as $v)
	{
		echo "<a onclick=\"select_word('$v[keyword]')\" href='#'>$v[keyword]</a>";
	}
	die;
}
if(!empty($_POST['add_inquery']))
{
	if($_POST['type']=='info')
	{
		$_POST['tid']=trim($_POST['tid']);
		
		if(!isset($_COOKIE['offer_inquery']))
		{
			setcookie("offer_inquery",$_POST['tid'],NULL,"/",$config['baseurl']);
		}
		else
		{
			$str=explode(',',$_COOKIE['offer_inquery'].','.$_POST['tid']);
			$str=array_unique($str);
			$inum=count($str);
			$str=implode(',',$str);
			@setcookie("offer_inquery",$str,NULL,"/",$config['baseurl']);
		}
	}
	if($inum==0)
		$inum=count(array_unique(explode(",",$_POST['tid'])));
	echo $inum*1;
	die;
}

if(isset($_POST["prov_id"]))
{	
	$sql="select city_id,city from ".CITY." a, ".PROVINCE." b where a.province_id=b.province_id and b.province='$_POST[prov_id]' order by a.nums";
	$db->query($sql);
	$num=$db->num_rows();
	$str="{";
	$i=0;
	$res=$db->getRows();
	foreach($res as $k)
	{
		$i++;
		$city_id=$k["city"];
		//$city=$k["city"];
		if($i<$num)
			$str.="'$i':{'0':'$city_id','1':'$city_id'},";
		else
			$str.="'$i':{'0':'$city_id','1':'$city_id'}";
	}
	//------------------------------------------------------------
	$str.="};";
	echo $str;
}
if(isset($_POST["country_id"]))
{	
	$sql="select a.province_id,a.province from ".PROVINCE." a, ".COUNTRY." b where a.country_id=b.id and b.id='$_POST[country_id]'";
	$db->query($sql);
	$num=$db->num_rows();
	$str="{";
	$i=0;
	$res=$db->getRows();
	foreach($res as $k)
	{
		$i++;
		$province_id=$k["province_id"];
		$province_name=$k["province"];
		//$city=$k["city"];
		if($i<$num)
			$str.="\"$i\":{\"0\":\"$province_id\",\"1\":\"$province_name\"},";
		else
			$str.="\"$i\":{\"0\":\"$province_id\",\"1\":\"$province_name\"}";
	}
	//------------------------------------------------------------
	$str.="};";
	echo $str;
}

if(isset($_POST["catid"]))
{
	$i=0;
	$s=$_POST["catid"]."00";
	$b=$_POST["catid"]."99";
	$db->query("SELECT * FROM ".COMPANYCAT." WHERE catid>=$s and catid<=$b ORDER BY nums ASC");
	$num=$db->num_rows();
	$str="{";
	$res=$db->getRows();
	foreach($res as $k)
	{
		$i++;
		$catid=$k["catid"];
		$cat=$k["cat"];
		if($i<$num)
			$str.="\"$i\":{\"0\":\"$catid\",\"1\":\"$cat\"},";
		else
			$str.="\"$i\":{\"0\":\"$catid\",\"1\":\"$cat\"}";
	}
	//------------------------------------------------------------
	$str.="};";
	echo $str;
}
if(!empty($_POST["pcatid"]))
{
	if(!empty($_POST['cattype'])&&$_POST['cattype']=='hr')
		$db->query("select catname as cat,id as catid from ".HRCAT." where pid='$_POST[pcatid]' order by posid");
	if(!empty($_POST['cattype'])&&$_POST['cattype']=='quoted_price')
		$db->query("select catname as cat,id as catid from ".QPCAT." where pid='$_POST[pcatid]' order by posid");
		
	$s=$_POST["pcatid"]."00";$b=$_POST["pcatid"]."99";
	if(!empty($_POST['cattype'])&&$_POST['cattype']=='offer')
		$db->query("SELECT * FROM ".OCAT." WHERE catid>'$s' and catid<'$b' ORDER BY nums ASC");
	if(!empty($_POST['cattype'])&&$_POST['cattype']=='album')
		$db->query("SELECT * FROM ".ALBUMCAT." WHERE catid>'$s' and catid<'$b' ORDER BY nums ASC");
	if(!empty($_POST['cattype'])&&$_POST['cattype']=='com')
		$db->query("SELECT * FROM ".COMPANYCAT." WHERE catid>'$s' and catid<'$b' ORDER BY nums ASC");
	$num=$db->num_rows();
	$str="{";
	$i=0;
	$res=$db->getRows();
	foreach($res as $k)
	{
		$i++;
		$city_id=$k["catid"];
		$cat=str_replace("\r",'',$k['cat']);
		if($i<$num)
			$str.="\"$i\":{\"0\":\"$city_id\",\"1\":\"$cat\"},";
		else
			$str.="\"$i\":{\"0\":\"$city_id\",\"1\":\"$cat\"}";
	}
	$str.="};";
	echo $str;
}

//发送短信验证码
if(!empty($_POST['mobile']))
{
	if(strlen($_POST['mobile']*1)==11)
	{
		$rand=rand(10000,99999);
		$_SESSION['YZSMS']=$rand;
		$mail_temp=get_mail_template('active');
		$con=$mail_temp['message'];
		$ar1=array('[username]','[email]','[weburl]','[link]','[rand]');
		$ar2=array($user,$email,$config['weburl'],$link,$rand);
		$con=str_replace($ar1,$ar2,$con);
		
		include_once("includes/plugin_sms_class.php");
		$sms=new sms();
		echo $str=$sms->send($_POST['mobile'],$con);
		if($str)
			echo 1;
		die;
	}
}

//检查短信码是不是正确
if(isset($_POST["sms_yzm"]))
{	
	if($_POST["sms_yzm"]==$_SESSION['YZSMS'])
		echo "true";
	else
		echo "false";
	die;
}

//检查邮件合法性，是不是被注册
if(isset($_POST["check_email"])&&$_POST["check_email"]=='1')
{	
	$_POST[email]=trim($_POST[email]);
	if(strlen($_POST[email])>50)
	{
		echo 2;die;
	}
	$sql="select * from ".ALLUSER." where email='$_POST[email]'";
	$db->query($sql);
	$re=$db->fetchRow();
	if($re['email']!='')
		echo 2;//不可以注册，已被使用
	else
		echo 1;//正常，可以注册。
}

//随机显示验证问题
if(isset($_POST["wtyz"])&&$_POST["wtyz"]=='1')
{	
	$sql="select * from ".REGVERFCODE." order by rand() limit 0,1";
	$db->query($sql);
	$re=$db->fetchRow();
	$_SESSION['YZWT']=$re['answer'];
	echo $re['question'];
}

//检查问题是不是合法
if(isset($_POST["ckyzwt"]))
{	
	if($_POST["ckyzwt"]==$_SESSION['YZWT'])
		echo "true";
	else
		echo "false";
}

//验证码检查合法性。
if(isset($_GET["yzm"]))
{
	if(strtolower($_GET["yzm"])!=strtolower($_SESSION["auth"]))
	{
		echo 1;
	}
}

//检查用户名合法性。
if(isset($_GET['user']))
{
	include_once("config/reg_config.php");
	$config = array_merge($config,$reg_config);
	$un=trim($_GET['user']);
	if(strlen($_GET['user'])>30)
	{
		echo 1;die;
	}
	
	$sql="select * from ".RESERVE_USERNAME." where username='$un'"; 
	$db->query($sql);
	if($db->num_rows()>0)
	{
		echo 1;
	}
	else
	{
	    if(!empty($config['openbbs'])&&$config['openbbs']==2)
	    {
			$sql="select * from ".ALLUSER." where user='$un'";
		    $db->query($sql);
		    $bbnum=$db->num_rows();
			
			include_once('uc_client/client.php');
		    if(uc_user_checkname($un)==1&&!$bbnum)
			    echo 0;//成功
		    else
			    echo 1;//失败
	    }
	    else
	    {
		    $sql="select * from ".ALLUSER." where user='$un'";
		    $db->query($sql);
		    if($db->num_rows()>0)
			   echo 1;//失败
		    else
			   echo 0;//成功
	    }
	}
}
?>