<?php
$link = mysqli_connect("localhost",  "runtian", "runtian@123", "runtian_db");	

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());  
    exit();
}

if (!empty($GLOBALS["HTTP_RAW_POST_DATA"]))//
{
	$newData = "";
	$data = $GLOBALS["HTTP_RAW_POST_DATA"];
	$newData = byteDecrypt($data);
	$rtnstr = dboperate($link, $newData);
	$rtnstr = byteEncrypt($rtnstr); 
	echo $rtnstr;
} else {
   
	echo byteEncrypt("error");
}


mysqli_close($link);

function dboperate($link,$data) {
	
	$sqlparam = split("&", $data);
	$paramarr;
	foreach ($sqlparam as $param) {
		$tmparr = split("=", $param);
		$paramarr[$tmparr[0]] = $tmparr[1];
	}
	$type = $paramarr["type"];
	
	if($type == "insert") {
		$username=$paramarr["username"];
		$itemid=$paramarr["itemid"];
		$title=$paramarr["title"];
		$couponcut=$paramarr["couponcut"];
		$price=$paramarr["price"];
		$feestring=$paramarr["feestring"];
		$sellerid=$paramarr["sellerid"];
		$activityid=$paramarr["activityid"];
		$couponused=intval($paramarr["couponused"]);
		$couponnum=intval($paramarr["couponnum"]);
		$sttnum =$paramarr["sttnum"];
		$qq=$paramarr["qq"];
		$hid=$paramarr["hid"];
		$aud=$paramarr["aud"];
		$pic=$paramarr["pic"];
		if($aud!="1") $aud="0";
		$selSql = "select * from nightorder where ItemId='".$itemid."' and ndate=".date("Ymd");
		$result= mysqli_query($link,$selSql);
		if( mysqli_fetch_array($result)!="") {
			return "dup";
		}
		$sql="INSERT INTO nightorder (ndate, Hid, ItemId, Title, CouponCut, AfterPrice, FeeString, SellerId, ActivityId, CouponUsed, CouponNum,sttnum,hot,hide,username,qq,aud,PicUrl) VALUES (".date("Ymd").",'".$hid."','".$itemid."','".$title."',".$couponcut.", '".$price."','".$feestring."','".$sellerid."','".$activityid."', ".$couponused.",".$couponnum.",".$sttnum.",0,0,'".$username."', '".$qq."',".$aud.",'".$pic."')";
		$result= mysqli_query($link,$sql);
		if($result)  return "ok";
	} else if ($type=="select"){
		$admin=$paramarr["admin"];
		$query = "select * from nightorder where delflg=0 and aud=1 and ndate>".date("Ymd",strtotime("-3 days"))." order by hot desc, ndate desc, ding desc, Hid desc";
		if($admin=="1") {
			$query = "select * from nightorder where delflg=0 and ndate>".date("Ymd",strtotime("-3 days"))." order by hot desc, ndate desc, ding desc, Hid desc";
		}
		$result = mysqli_query($link, $query);
		$itemlist="";
		while(($trow = mysqli_fetch_array($result))!="") {
			//$curdate = date("Ymd",strtotime("-1 days"));
			//$datestr = substr($trow["Hid"],0,8);
			$ordernum= 0+$trow["OderNum"];
			//if($curdate==$datestr &&  $ordernum<200) {
				//$ordernum=$ordernum+200;
			//}
			$title = str_replace("'", "", $trow["Title"]);
            $title = str_replace("\n", "", $title);
            $title = str_replace("\t", "", $title);
			if($itemlist == ""){
				$itemlist = $trow["Hid"]."\t".$trow["ItemId"]."\t".$title."\t".$trow["CouponCut"]."\t".$trow["AfterPrice"]."\t".$trow["FeeString"]."\t".$trow["CouponUsed"]."\t".$trow["CouponNum"]."\t".$ordernum."\t".$trow["SellerId"]."\t".$trow["ActivityId"]."\t".$trow["qq"]."\t".$trow["ding"]."\t".$trow["cai"]."\t".$trow["aud"]."\t".$trow["sttnum"];
			}
			else {
				$itemlist=$itemlist."\n".$trow["Hid"]."\t".$trow["ItemId"]."\t".$title."\t".$trow["CouponCut"]."\t".$trow["AfterPrice"]."\t".$trow["FeeString"]."\t".$trow["CouponUsed"]."\t".$trow["CouponNum"]."\t".$ordernum."\t".$trow["SellerId"]."\t".$trow["ActivityId"]."\t".$trow["qq"]."\t".$trow["ding"]."\t".$trow["cai"]."\t".$trow["aud"]."\t".$trow["sttnum"];
			}
		}
		return "ok".$itemlist;
	
	} else if ($type=="modtitle"){
		$hid = $paramarr["hid"];
		$title = $paramarr["title"];
		$cmsrate = $paramarr["cmsrate"];
		$zkprice = $paramarr["zkprice"];
		$query = "update nightorder set Title='".$title."',FeeString=".$cmsrate.", AfterPrice=".$zkprice." where Hid='".$hid."'";
		$result = mysqli_query($link, $query);
		return "ok";
	
	}else if ($type=="delete"){
	
		$hid = $paramarr["hid"];
		$query = "delete from nightorder where Hid='".$hid."'";
		$result = mysqli_query($link, $query);
		return "ok";
	
	}else if ($type=="hot"){
	    $hid = $paramarr["hid"];
		$query = "update nightorder set hot=1 where Hid='".$hid."'";
		$result = mysqli_query($link, $query);
		return "ok";
	}else if ($type=="cancelhot"){
	    $hid = $paramarr["hid"];
		$query = "update nightorder set hot=0 where Hid='".$hid."'";
		$result = mysqli_query($link, $query);
		return "ok";
	
	}else if ($type=="audhot"){
	    $hid = $paramarr["hid"];
        $aud = $paramarr["aud"];
		$query = "update nightorder set aud=".$aud." where Hid='".$hid."'";
		$result = mysqli_query($link, $query);
		return "ok";
	
	}else if($type=="hide"){
		$hid = $paramarr["hid"];
		$query = "update nightorder set hide=1 where Hid='".$hid."'";
			$result = mysqli_query($link, $query);
		return "ok";
	}else if($type=="ding"){
		$hid = $paramarr["hid"];
		$ding = $paramarr["ding"];
		$query = "update nightorder set ding=".$ding." where Hid='".$hid."'";
			$result = mysqli_query($link, $query);
		return "ok";
	}else if($type == "deletenightitembycouponid") {
		return "ok";
	}else if($type == "deletenightitembycouponid2") {
		$itemid = $paramarr["itemid"];
		//$query = "update nightorder set delflg=1 where ItemId='".$itemid."'";
		//$result = mysqli_query($link, $query);
		return "ok";
	}else if($type == "deletenightitembycouponid3") {
		$itemid = $paramarr["itemid"];
		$query = "update nightorder set delflg=1 where ItemId='".$itemid."'";
		$result = mysqli_query($link, $query);
		return "ok";
	}else if($type == "updatenightitemcoupon") {
		$itemid = $paramarr["itemid"];
		$couponused = $paramarr["couponused"];
		$couponnum = $paramarr["couponnum"];
		$updSql = "update nightorder set CouponUsed=".$couponused.", CouponNum=".$couponnum." where ItemId='".$itemid."'";
		mysqli_query($link, $updSql);
		return "ok";
	}
}


function writeTopItemFile($file, $content) {
	$topItemFile = fopen($file, "w") or die("Unable to open file!");
	fwrite($topItemFile, $content);
	fclose($topItemFile);
}

function readTopItemFile($file) {
	$topItemFile = fopen($file, "r") or die("Unable to open file!");
	$content = fread($topItemFile,filesize($file));
	fclose($topItemFile);
	return $content;
}


function byteDecrypt($data) {
	$newData = "";
	for ($i=0; $i<strlen($data); $i++) {
		$newData = $newData.chr(ord($data[$i])+$i%100+100);
	}
	return $newData;
}

function byteEncrypt($data) {
	$newData = "";
	for ($i=0; $i<strlen($data); $i++) {
		$newData = $newData.chr(ord($data[$i])-$i%100-100);
	}
	return $newData;
}

?>