<?php
//====== 公共函数 ======
function Qry($s){global $conn;return mysqli_query($conn,$s);}
function Qrc($s){return $s?mysqli_num_rows($s):0;}
function Qra($s,$c=1){return $s?mysqli_fetch_array($s,[MYSQLI_NUM,MYSQLI_ASSOC][$c]):false;}
function Qrs($s){return $s?Qra($s,0)[0]:false;}
function Qri(){global $conn;return mysqli_insert_id($conn);}
function Rnd($n=16){$t="1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";$s="";for($i=0;$i<$n;$i++)$s=$s.substr($t,mt_rand(0,61),1);return $s;}
function Req($n,$c=""){$s=isset($_POST[$n])?$_POST[$n]:(isset($_GET[$n])?$_GET[$n]:"");if($s=="")$s=$c;$s=Rpl(["str"=>$s]);return $s["str"];}
function Rpl($s){foreach($s as $k=>$v){if(is_array($v)){$s[$k]=Rpl($v);}elseif(is_string($v)){for($i=0;$i<33;$i++)$v=str_replace(chr($i),"&#".$i.";",$v);$s[$k]=str_replace(chr(194).chr(177),"&#32;",str_replace(chr(194).chr(160),"&#32;",str_replace("'","&#39;",str_replace("%","&#37;",str_replace("\"","&#34;",$v)))));}}return $s;}
function Enc($s){return json_encode($s,JSON_UNESCAPED_UNICODE);}
function Dec($s,$c=false){$s=json_decode($s,true);if(json_last_error())die('{"msg":"Request JSON Parse Error."}');return $c?Rpl($s):$s;}
function X2J($s){return Dec(Enc(simplexml_load_string($s,'SimpleXMLElement',LIBXML_NOCDATA)));}
function Sha($n,$c){$f=openssl_get_privatekey(file_exists($c)?file_get_contents($c):$c);openssl_sign($n,$s,$f,OPENSSL_ALGO_SHA256);openssl_free_key($c);return base64_encode($s);}
function Cul($n,$c=false,$h=false,$r=false){$m=curl_init();curl_setopt($m,CURLOPT_TIMEOUT,30);curl_setopt($m,CURLOPT_RETURNTRANSFER,TRUE);if(substr($n,0,8)=="https://"){curl_setopt($m,CURLOPT_SSL_VERIFYPEER,FALSE);curl_setopt($m,CURLOPT_SSL_VERIFYHOST,FALSE);}curl_setopt($m,CURLOPT_URL,$n);if($c){curl_setopt($m,CURLOPT_POST,TRUE);curl_setopt($m,CURLOPT_POSTFIELDS,$c);}if($h){curl_setopt($m,CURLOPT_HTTPHEADER,$h);curl_setopt($m,CURLOPT_FAILONERROR,false);}if($r)return $m;$s=curl_exec($m);curl_close($m);return $s;}
function Pay($o,$m,$i,$e=""){global $cfg;if($cfg["Ap"]["Fs"]==1){return Cul($cfg["AL"]==""?"https://www.pays.top/app/api.php":($cfg["Aw"]."onepay.php"),Enc(["act"=>"","pay"=>$cfg["Ap"],"odr"=>["api"=>$i,"oid"=>$o,"mny"=>$m,"ntf"=>$cfg["Aw"]."notify.php","wid"=>$e,"web"=>$cfg["An"]]]));}else{$s="gw_mchid=".$cfg["Ap"]["Gi"]."&gw_notify=".$cfg["Aw"]."notify.php&gw_order=".$o."&gw_price=".$m."&gw_rand=".Rnd()."&gw_type=".$i;return Cul("https://www.pays.top/app/pay.php",$s."&gw_sign=".md5($s."&key=".$cfg["Ap"]["Gk"]));}}
function Snd($s){global $cfg;if($cfg["Ag"]["Be"]<time()){$e=Dec(Cul("https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=".$cfg["Ag"]["Bi"]."&client_secret=".$cfg["Ag"]["Bk"]));if(isset($e["access_token"])){$cfg["Ag"]["Bt"]=$e["access_token"];$cfg["Ag"]["Be"]=time()+$e["expires_in"];file_put_contents("config.php","<?php".PHP_EOL."\$cfg='".Enc($cfg)."';");}else return '{"msg":"ok","mp3":"images/warn.mp3"}';}return '{"msg":"ok","mp3":"https://tsn.baidu.com/text2audio?tex='.urlencode(urlencode($s)).'&lan=zh&cuid='.md5($cfg["Ak"]).'&ctp=1&tok='.$cfg["Ag"]["Bt"].'"}';}
function Tkn(){global $cfg;if(time()>$cfg["Ag"]["We"]){$k=Dec(Cul("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$cfg["Ag"]["Wa"]."&secret=".$cfg["Ag"]["Wk"]));$cfg["Ag"]["Wt"]=$k["access_token"];$cfg["Ag"]["We"]=time()+$k["expires_in"]*1;file_put_contents("config.php","<?php".PHP_EOL."\$cfg='".Enc($cfg)."';");}return $cfg["Ag"]["Wt"];}
if(!file_exists("config.php")){header("Location: install.php");die();}else require "config.php";
date_default_timezone_set("Asia/Shanghai");
$cfg=Dec($cfg);$pre=$cfg["DB"]["pre"];
$conn=mysqli_connect($cfg["DB"]["host"],$cfg["DB"]["user"],$cfg["DB"]["pass"],$cfg["DB"]["name"]);
Qry("SET NAMES UTF8");
header("Content-Type:text/html;charset=UTF-8");
?>