<?php
require 'conn/conn.php';
require 'conn/function.php';

$url=$_POST["url"];
$d=intval($_POST["d"]);
$type=intval($_POST["type"]);
$urls=$url;

$action=$_GET["action"];
$from=$_GET["from"];

if($_SESSION["A_login"]!="" && $from=="admin"){
	$M_id=-1;
	$vip=1;
}else{
	$M_id=intval($_SESSION["M_id"]);
	if($M_id>0){
	    $M=getrs("select * from sl_member where M_id=$M_id");
	    $money=$M["M_money"];
		$vip=$M["M_vip"];
	}else{
	    $vip=0;
	}
}

if($M_id==0 && $C_creatprice>0){
	die("[{\"code\":\"error\",\"msg\":\"生成短链".$C_creatprice."元/条，请先登录！\",\"long\":\"\",\"time\":\"\"}]");
}

if($C_rewrite==1){
	$r="/";
}else{
	$r="?";
}

if($action=="creat"){//单个生成及批量生成
	$u=explode("\r\n",$urls);
	if($M_id>0){
		if($C_creatprice>0){
			if($money-$C_creatprice*count($u)<0){
				die("[{\"code\":\"error\",\"msg\":\"生成短链".$C_creatprice."元/条，请保证余额充足！\",\"long\":\"\",\"time\":\"\"}]");
			}else{
				mysqli_query($conn,"update sl_member set M_money=M_money-".($C_creatprice*count($u))." where M_id=$M_id");
				mysqli_query($conn,"insert into sl_orders(O_title,O_attach,O_money,O_time,O_tradeno,O_state,O_mid) values('生成短链','多个链接',".($C_creatprice*count($u)).",'".date('Y-m-d H:i:s')."','".date('YmdHis') . rand(10000000,99999999)."',1,$M_id)");
			}
		}
	}

	for($i=0;$i<count($u);$i++){
		if(CheckUrl($u[$i])){
			if(time()-$_SESSION["time"]>$C_limit || $C_limit==0 || (time()-$_SESSION["time"]<$C_limit && intval($_SESSION["count"])<$C_limit2)){
				//1.未开启限制 2.时间限制已失效 3.时间限制未生效但是次数未超出
				$url=urlencode($u[$i]);
				$sql="select * from sl_short where S_url='".$url."' and S_mid=".$M_id." and S_d=".$d;
				$result = mysqli_query($conn, $sql);
				$row = mysqli_fetch_assoc($result);
				if (mysqli_num_rows($result) > 0) {//已存在的链接
					$S_id=$row["S_id"];
					$S_vip=$row["S_vip"];
					$S_short=$row["S_short"];
					$S_time=$row["S_time"];
					mysqli_query($conn,"update sl_short set S_type=$type where S_id=".$S_id);
				}else{//新链接
					$S_time=date('Y-m-d H:i:s');
					if($C_day==0 || $vip==1){
						$S_vip=1;
					}else{
						$S_vip=0;
					}

					if(getrs("select * from sl_short where S_id=1")==""){
						$S_id=1;
					}else{
						$S_id=getrs("select min(S_id+1) as S_id from sl_short c where not exists (select S_id from sl_short where S_id = c.S_id+1)","S_id");
					}

					mysqli_query($conn,"insert into sl_short(S_id,S_url,S_time,S_vip,S_mid,S_d,S_type) values($S_id,'".$url."','".date('Y-m-d H:i:s')."',".$S_vip.",".$M_id.",".$d.",".$type.")");

					if(getrs("select count(D_id) as D_count from sl_domain where D_show=1","D_count")>0){
						$D=getrs("select D_domain,D_http from sl_domain where D_show=1 order by rand()");
						$D_domain=$D["D_domain"];
						$D_http=$D["D_http"];
						$short=$D_http.$D_domain.$r.dec62($S_id);
					}else{
						$short=gethttp().$_SERVER["HTTP_HOST"].$r.dec62($S_id);
					}
					
					switch($d){
						case -101:
						if($C_jump==0){
							$short_url=$url;
						}else{
							$short_url=gethttp().$_SERVER["HTTP_HOST"].$r.dec62($S_id);
						}

						$info=file_get_contents("http://two.ft12.com/api.php?url=$short_url&apikey=".$C_swkey);

						$short_url=$info;
						break;
						
						case -102:
						if($C_jump==0){
							$short_url=$url;
						}else{
							$short_url=gethttp().$_SERVER["HTTP_HOST"].$r.dec62($S_id);
						}

						$info=file_get_contents("http://two.ft12.com/api.php?url=$short_url&apikey=".$C_swkey);
						$short_url=$info;
						break;

						case -2:
						$short_url=gethttp().$_SERVER["HTTP_HOST"].$r.dec62($S_id);
						break;

						default:
						$D=getrs("select * from sl_domain where D_id=".intval($d));
						$short_url=$D["D_http"].$D["D_domain"].$r.dec62($S_id);
						break;
					}

					if($short_url==""){
						$S_short=$short;
					}else{
						$S_short=$short_url;
					}
					mysqli_query($conn,"update sl_short set S_short='$S_short',S_type=$type where S_id=".$S_id);
				}

				if(time()-$_SESSION["time"]>$C_limit){//如果时间限制已失效，则重新计时间和次数
					$_SESSION["time"]=time();
					$_SESSION["count"]=1;
				}else{
					$_SESSION["count"]=intval($_SESSION["count"])+1;
				}
				
				$api=$api."{\"code\":\"success\",\"id\":\"".$S_id."\",\"vip\":\"".$S_vip."\",\"short\":\"".$S_short."\",\"type\":\"".$type."\",\"long\":\"".urldecode($url)."\",\"time\":\"".$S_time."\"},";
			}else{
				$api=$api."{\"code\":\"error\",\"msg\":\"生成过于频繁，请".($C_limit-(time()-$_SESSION["time"]))."秒后再试！\",\"long\":\"".urldecode($u[$i])."\",\"time\":\"".$S_time."\"},";
			}
		}else{
			$api=$api."{\"code\":\"error\",\"msg\":\"网址格式不正确！\",\"long\":\"".urldecode($u[$i])."\",\"time\":\"".$S_time."\"},";
		}
	}
	$api=substr($api,0,strlen($api)-1);
	die("[".$api."]");
}

if($action=="creat2"){//自定义后缀
	$url=$_POST["url"];
	$domain=$_POST["domain"];
	$attach=$_POST["attach"];
	$S_time=date('Y-m-d H:i:s');

	if($C_day==0 || $vip==1){
		$S_vip=1;
	}else{
		$S_vip=0;
	}

	$S_id=intval(dec10($attach));
	if(CheckUrl($url)){
		if(time()-$_SESSION["time"]>$C_limit || $C_limit==0 || (time()-$_SESSION["time"]<$C_limit && intval($_SESSION["count"])<$C_limit2)){
			if($S_id>0){
				if(getrs("select * from sl_short where S_id=$S_id and not S_url='".urldecode($url)."'","S_id")==""){
					$S_short=$domain.$r.$attach;
					mysqli_query($conn,"insert into sl_short(S_id,S_url,S_short,S_time,S_vip,S_mid,S_d,S_type) values($S_id,'".urldecode($url)."','$S_short','$S_time',$S_vip,$M_id,999,$type)");
					if(time()-$_SESSION["time"]>$C_limit){//如果时间限制已失效，则重新计时间和次数
						$_SESSION["time"]=time();
						$_SESSION["count"]=1;
					}else{
						$_SESSION["count"]=intval($_SESSION["count"])+1;
					}

					if($M_id>0){
						$money=getrs("select * from sl_member where M_id=$M_id","M_money");
						if($C_creatprice>0){
							if($money-$C_creatprice<0){
								die("[{\"code\":\"error\",\"msg\":\"生成短链".$C_creatprice."元/条，请保证余额充足！\",\"long\":\"\",\"time\":\"\"}]");
							}else{
								mysqli_query($conn,"update sl_member set M_money=M_money-".$C_creatprice." where M_id=$M_id");
								mysqli_query($conn,"insert into sl_orders(O_title,O_attach,O_money,O_time,O_tradeno,O_state,O_mid) values('生成短链','$S_id',".$C_creatprice.",'".date('Y-m-d H:i:s')."','".date('YmdHis') . rand(10000000,99999999)."',1,$M_id)");
							}
						}
					}

					die("[{\"code\":\"success\",\"id\":\"".$S_id."\",\"vip\":\"".$S_vip."\",\"short\":\"".$S_short."\",\"long\":\"".urldecode($url)."\",\"time\":\"".$S_time."\"}]");
				}else{
					die("[{\"code\":\"error\",\"msg\":\"该后缀已被使用，请尝试更换！\",\"long\":\"".urldecode($url)."\",\"time\":\"".$S_time."\"}]");
				}
			}else{
				die("[{\"code\":\"error\",\"msg\":\"后缀不符合格式或过长，请重新填写！\",\"long\":\"".urldecode($url)."\",\"time\":\"".$S_time."\"}]");
			}
		}else{
			die("[{\"code\":\"error\",\"msg\":\"生成过于频繁，请".($C_limit-(time()-$_SESSION["time"]))."秒后再试！\",\"long\":\"".urldecode($url)."\",\"time\":\"".$S_time."\"}]");
		}
	}else{
		die("[{\"code\":\"error\",\"msg\":\"网址格式不正确！\",\"long\":\"".urldecode($url)."\",\"time\":\"".$S_time."\"}]");
	}
}
?>