<?php
require 'vendor/autoload.php';
include_once './function.php';
include_once './getCert.php';
include_once './notice.php';
use QL\QueryList;

$dd = new DdNotice();
$getCert = new getCert($agentarry);

//   /gia/34346544
$requestUrl = $_SERVER['REQUEST_URI'];

$params = explode('/', $requestUrl);

$reportNo = $params[2];

if (!$reportNo) {
	echo "no reportNo!!!";exit;
}

//pdf文件
$pdf = "./gia/$reportNo.pdf";

// if (strpos($_SERVER['HTTP_REFERER'], 'szzmzb.com') === false) {
if (!$_SERVER['HTTP_REFERER']) {
	mkLog($_SERVER['HTTP_REFERER'].'来源为空,禁止访问', 'error');
	exit;
}

//下载(官网过来)
if (isset($params[3])) {
	if (!file_exists($pdf)) {
		$str = get_jiemi($params[3]);
		$obj = \explode(':', $str);
		if ($obj[0] == 'cert' && strlen($obj[1]) == 32) {
			mkLog('gia证书号'.$reportNo.'下载', 'szzmzb');
			// $downUrl = sprintf(
			// 	"https://www.gia.edu/otmm_wcs_int/proxy-pdf/?ReportNumber=%s&url=https://myapps.gia.edu/RptChkClient/reportClient.do?ReportNumber=%s",
			// 	$reportNo,
			// 	$obj[1]
			// );		
			$downUrl = sprintf(
						"https://pdf.gia.edu/?ReportNumber=%s",
						$obj[1]
					);

			$file = $getCert->downloadGIAPDF($downUrl);	
			// mkLog(json_encode($file), 'error');
			if ($file) {
				file_put_contents($pdf, $file);
			} else {
				mkLog('gia证书号'.$reportNo.'下载失败', 'error');
				header("Location:https://x.starjew.com/gia/?no=$reportNo");
				exit;
			}

		}
	}
	header("Content-type: application/pdf"); // 文件将被称为 downloaded.pdf
	header("Content-Disposition:inline;filename='".$reportNo.".pdf'");
	readfile($pdf);

	exit;
}

if (strpos($_SERVER['HTTP_REFERER'], 'cert.szzmzb.com') != false) {
	mkLog($_SERVER['HTTP_REFERER'].'不允许直接访问', 'error');
	exit;
}

mkLog($reportNo."查询, 来源:".$_SERVER['HTTP_REFERER'], 'cert');

$html = <<<Eof
	<!DOCTYPE html>
	<html>
	<head>
		<title>证书查询</title>
	</head>
	<body style="margin:0px;padding:0px;overflow:hidden">
		查询出错
	</body>
	</html>
Eof;
echo $html;




exit;

/**************************************注释********************************************************************/
if (!file_exists($pdf)) {
	try {
		mkLog($reportNo.'->获取时间:'.time(), 'cert');
		//获取gia编码加密字符串 这里取(亿金网)数据 gia官网太慢
		// $url = "https://x.starjew.com/gia/?no=$reportNo";
		// $url = "https://www.eegem.com/gia/?no=$reportNo";

		// $obj = explode('?', QueryList::get($url)->find('.download-btn')->attr('href'));
		
		// if (!isset($obj[1])) {

		// 	$downUrl = $getCert->downCert($reportNo);  //官网的获取方式(获取不到)

		// } else {

			// $downUrl = sprintf(
			// 	"https://www.gia.edu/otmm_wcs_int/proxy-pdf/?ReportNumber=%s&url=https://myapps.gia.edu/RptChkClient/reportClient.do?ReportNumber=%s",
			// 	$reportNo,
			// 	$obj[1]
			// );
			
			//亿金网
			// $downUrl = sprintf("http://p.onehuo.com/report/gia/%s.pdf?%s", $reportNo, $obj[1]);

		// }

		//查宝网
		$downUrl = "http://api.checkgems.com/static/pdfjs/web/viewer.html?file=/api/v2/certs/GIA/$reportNo.pdf";

		//下载证书
		$file = $getCert->downloadGIAPDF($downUrl);

		mkLog($reportNo.'->结束时间:'.time()."\n", 'cert');

		if (!$file) {
			$content = "故障通知:";
			$content .= '证书号: '.$reportNo." 证书获取失败\n";
			// $data = [
			// 	'msgtype'=>'text',
			// 	'text'=>[
			// 		'content'=>$content
			// 	],
			// 	'at'=>[
			// 		// "atMobiles"=>[
			// 		// 	'18664093579'
			// 		// ],
			// 		"isAtAll"=>false
			// 	]
			// ];

			// $dd->sendMsg($data);

			mkLog($content, 'error');

			header("Location:https://x.starjew.com/gia/?no=$reportNo");
			exit;
		}

		file_put_contents($pdf, $file);

	} catch (\Exception $e) {
		$content = "故障通知:\n";
		$content .= $e->getFile()."\n";
		$content .= $e->getMessage()."\n";
		$data = [
			'msgtype'=>'text',
			'text'=>[
				'content'=>$content
			],
			'at'=>[
				// "atMobiles"=>[
				// 	'18664093579'
				// ],
				"isAtAll"=>false
			]
		];
		$dd->sendMsg($data);

		mkLog(json_encode($content), 'error');
		header("Location:https://x.starjew.com/gia/?no=$reportNo");
		// header("Location:http://cert.szzmzb.com/index.html");
		exit;
	}
}

header("Content-type: application/pdf"); // 文件将被称为 downloaded.pdf
header("Content-Disposition:inline;filename='".$reportNo.".pdf'");
readfile($pdf);
