chore(*.php): use pint for php formatting
This commit is contained in:
parent
bd1855a65e
commit
753f3433ce
40 changed files with 2261 additions and 1900 deletions
106
api/umami.php
106
api/umami.php
|
@ -1,21 +1,21 @@
|
|||
<?php
|
||||
|
||||
$umamiHost = 'https://stats.coryd.dev';
|
||||
$requestUri = $_SERVER['REQUEST_URI'];
|
||||
$method = $_SERVER['REQUEST_METHOD'];
|
||||
$proxyPrefix = '/assets/scripts';
|
||||
$forwardPath = parse_url($requestUri, PHP_URL_PATH);
|
||||
$forwardPath = str_replace($proxyPrefix, '', $forwardPath);
|
||||
$targetUrl = $umamiHost . $forwardPath;
|
||||
$umamiHost = 'https://stats.coryd.dev';
|
||||
$requestUri = $_SERVER['REQUEST_URI'];
|
||||
$method = $_SERVER['REQUEST_METHOD'];
|
||||
$proxyPrefix = '/assets/scripts';
|
||||
$forwardPath = parse_url($requestUri, PHP_URL_PATH);
|
||||
$forwardPath = str_replace($proxyPrefix, '', $forwardPath);
|
||||
$targetUrl = $umamiHost.$forwardPath;
|
||||
|
||||
if (isset($contentType) && preg_match('#^(application/json|text/|application/javascript)#', $contentType)) {
|
||||
if (isset($contentType) && preg_match('#^(application/json|text/|application/javascript)#', $contentType)) {
|
||||
ob_start('ob_gzhandler');
|
||||
} else {
|
||||
} else {
|
||||
ob_start();
|
||||
}
|
||||
}
|
||||
|
||||
if ($method === 'GET' && preg_match('#^/utils\.js$#', $forwardPath)) {
|
||||
$remoteUrl = $umamiHost . '/script.js';
|
||||
if ($method === 'GET' && preg_match('#^/utils\.js$#', $forwardPath)) {
|
||||
$remoteUrl = $umamiHost.'/script.js';
|
||||
$cacheKey = 'remote_stats_script';
|
||||
$ttl = 3600;
|
||||
$js = null;
|
||||
|
@ -23,73 +23,83 @@
|
|||
$redis = null;
|
||||
|
||||
try {
|
||||
if (extension_loaded('redis')) {
|
||||
$redis = new Redis();
|
||||
$redis->connect('127.0.0.1', 6379);
|
||||
if (extension_loaded('redis')) {
|
||||
$redis = new Redis();
|
||||
$redis->connect('127.0.0.1', 6379);
|
||||
|
||||
if ($redis->exists($cacheKey)) $js = $redis->get($cacheKey);
|
||||
}
|
||||
if ($redis->exists($cacheKey)) {
|
||||
$js = $redis->get($cacheKey);
|
||||
}
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
error_log("Redis unavailable: " . $e->getMessage());
|
||||
error_log('Redis unavailable: '.$e->getMessage());
|
||||
}
|
||||
|
||||
if (!is_string($js)) {
|
||||
$ch = curl_init($remoteUrl);
|
||||
if (! is_string($js)) {
|
||||
$ch = curl_init($remoteUrl);
|
||||
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_HEADER, false);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_HEADER, false);
|
||||
|
||||
$js = curl_exec($ch);
|
||||
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
$js = curl_exec($ch);
|
||||
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
|
||||
curl_close($ch);
|
||||
curl_close($ch);
|
||||
|
||||
if ($redis && $code === 200 && $js) $redis->setex($cacheKey, $ttl, $js);
|
||||
if ($redis && $code === 200 && $js) {
|
||||
$redis->setex($cacheKey, $ttl, $js);
|
||||
}
|
||||
}
|
||||
|
||||
if (!is_string($js) || trim($js) === '') {
|
||||
$js = '// Failed to fetch remote script';
|
||||
$code = 502;
|
||||
if (! is_string($js) || trim($js) === '') {
|
||||
$js = '// Failed to fetch remote script';
|
||||
$code = 502;
|
||||
}
|
||||
|
||||
http_response_code($code);
|
||||
header('Content-Type: application/javascript; charset=UTF-8');
|
||||
echo $js;
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
$headers = [
|
||||
$headers = [
|
||||
'Content-Type: application/json',
|
||||
'Accept: application/json',
|
||||
];
|
||||
];
|
||||
|
||||
if (isset($_SERVER['HTTP_USER_AGENT'])) $headers[] = 'User-Agent: ' . $_SERVER['HTTP_USER_AGENT'];
|
||||
if (isset($_SERVER['HTTP_USER_AGENT'])) {
|
||||
$headers[] = 'User-Agent: '.$_SERVER['HTTP_USER_AGENT'];
|
||||
}
|
||||
|
||||
$ch = curl_init($targetUrl);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
$ch = curl_init($targetUrl);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
|
||||
if ($method === 'POST') {
|
||||
if ($method === 'POST') {
|
||||
$body = file_get_contents('php://input');
|
||||
$data = json_decode($body, true);
|
||||
|
||||
if (strpos($forwardPath, '/api/send') === 0 && is_array($data)) $data['payload'] = array_merge($data['payload'] ?? [], [
|
||||
'ip' => $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0',
|
||||
]);
|
||||
if (strpos($forwardPath, '/api/send') === 0 && is_array($data)) {
|
||||
$data['payload'] = array_merge($data['payload'] ?? [], [
|
||||
'ip' => $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0',
|
||||
]);
|
||||
}
|
||||
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||
} else {
|
||||
} else {
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||
}
|
||||
}
|
||||
|
||||
$response = curl_exec($ch);
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
|
||||
$response = curl_exec($ch);
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
|
||||
|
||||
curl_close($ch);
|
||||
http_response_code($httpCode);
|
||||
curl_close($ch);
|
||||
http_response_code($httpCode);
|
||||
|
||||
if ($contentType) header("Content-Type: $contentType");
|
||||
if ($contentType) {
|
||||
header("Content-Type: $contentType");
|
||||
}
|
||||
|
||||
echo $response ?: '';
|
||||
echo $response ?: '';
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue