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

$method=$_GET["method"];
$url=$_GET["url"];
$key=t($_GET["key"]);
$M_id=intval(getrs("select * from sl_member where M_key='".$key."'","M_id"));

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

if($M_id>0){
	$M=getrs("select * from sl_member where M_id=$M_id");
	if($method=="list"){
		$sql="select S_short,S_url,S_vip,S_time from sl_short where S_mid=".$M_id;
			$result = mysqli_query($conn, $sql);
			$arr = array();  
			while($row = mysqli_fetch_array($result)) {
			$row["S_url"]=urldecode($row["S_url"]);
			$count=count($row);
			  for($i=0;$i<$count;$i++){ 
			    unset($row[$i]);
			  }
		    array_push($arr,$row);
		}
		die(json_encode($arr));
	}

	if($method=="add"){
		$vip=intval($_GET["vip"]);
		$domain=intval($_GET["domain"]);
		if($vip==0 || ($vip==1 && $M["M_money"]>$C_urlprice)){
			if(getrs("select * from sl_domain where D_id=".intval($domain))!="" || $domain==0 || $domain==-101 || $domain==-102){
				if(CheckUrl($url)){
					$url=urlencode($url);
					$sql="select * from sl_short where S_url='".$url."' and S_mid=".$M_id." and S_d=".$domain;

					$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"];
						$api="{\"code\":\"success\",\"id\":\"".$S_id."\",\"vip\":\"".$S_vip."\",\"short\":\"".$S_short."\",\"long\":\"".urldecode($url)."\",\"time\":\"".$S_time."\"}";
					}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) values($S_id,'".$url."','".date('Y-m-d H:i:s')."',".$S_vip.",".$M_id.",".$domain.")");

						if(getrs("select count(D_id) as D_count from sl_domain","D_count")>0){
							$D=getrs("select D_domain from sl_domain 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($domain){
							case -101:
							if($C_jump==0){
								$short_url=$url;
							}else{
								$short_url=gethttp().$_SERVER["HTTP_HOST"].$r.dec62($S_id);
							}
							$info=getbody("http://check.uomg.com/api/dwz/vxrand?token=$C_swkey&format=json&longurl=$short_url","","GET");
							$short_url=json_decode($info)->ae_url;
							break;
							
							case -102:
							if($C_jump==0){
								$short_url=$url;
							}else{
								$short_url=gethttp().$_SERVER["HTTP_HOST"].$r.dec62($S_id);
							}
							$info=getbody("http://check.uomg.com/api/dwz/qqrand?token=$C_swkey&format=json&longurl=$short_url","","GET");
							$short_url=json_decode($info)->ae_url;
							break;

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

							default:
							$D=getrs("select * from sl_domain where D_id=".intval($domain));
							$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' where S_id=".$S_id);
						if($vip==1){
							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_urlprice,'".date('Y-m-d H:i:s')."','".date('YmdHis') . rand(10000000,99999999)."',1,$M_id)");
							mysqli_query($conn,"update sl_member set M_money=M_money-$C_urlprice where M_id=$M_id");
						}
						$api="{\"code\":\"success\",\"id\":\"".$S_id."\",\"vip\":\"".$S_vip."\",\"short\":\"".$S_short."\",\"long\":\"".urldecode($url)."\",\"time\":\"".$S_time."\"}";
					}
				}else{
					$api="{\"code\":\"error\",\"msg\":\"网址格式不正确！\",\"long\":\"".urldecode($u[$i])."\",\"time\":\"".$S_time."\"}";
				}
			}else{
				$api="{\"code\":\"error\",\"msg\":\"domain参数有误！\",\"long\":\"".urldecode($u[$i])."\",\"time\":\"".$S_time."\"}";
			}
		}else{
			$api="{\"code\":\"error\",\"msg\":\"账户余额不足，无法生成永久短链！\",\"long\":\"".urldecode($u[$i])."\",\"time\":\"".$S_time."\"}";
		}
		die($api);
	}
}else{
	die("{\"code\":\"error\",\"msg\":\"key不正确！\"}");
}
?>