Files
preland/index.php
T
2026-10-03 15:41:23 +00:00

108 lines
3.2 KiB
PHP

<?php
require_once __DIR__ . '/i18n.php';
$configFile = __DIR__ . '/config.json';
$config = file_exists($configFile) ? json_decode(file_get_contents($configFile), true) : [];
if (json_last_error() !== JSON_ERROR_NONE) {
throw new RuntimeException('JSON syntax error: ' . json_last_error_msg());
}
Yii::init($config['default_language'] ?? 'ru');
function getWeightedRandom($weights)
{
if (empty($weights)) {
return '';
}
$totalWeight = array_sum($weights);
if ($totalWeight <= 0) {
$keys = array_keys($weights);
return (string)$keys[0];
}
$rand = (mt_rand() / mt_getrandmax()) * $totalWeight;
$current = 0.0;
foreach ($weights as $key => $weight) {
$current += (float)$weight;
if ($rand <= $current) {
return (string)$key;
}
}
$keys = array_keys($weights);
return (string)end($keys);
}
$get_params = $_GET;
if (isset($config['utm']) && is_array($config['utm'])) {
foreach ($get_params as $key => $value) {
if (!isset($config['utm'][$key])) {
continue;
}
if (isset($config['utm'][$key]['weights'][$value])) {
continue;
}
if ($value !== 'rnd') {
unset($get_params[$key]);
continue;
}
$weights = $config['utm'][$key]['weights'] ?? [];
$get_params[$key] = getWeightedRandom($weights);
}
foreach ($config['utm'] as $key => $value) {
if (isset($get_params[$key])) {
continue;
}
$get_params[$key] = $config['utm'][$key]['default'];
}
}
$UTM = $get_params;
$host = $config['host'] ?? ($_SERVER['HTTP_HOST'] ?? '');
$apiHost = $host ? (preg_match('#^https?://#', $host) ? rtrim($host, '/') : 'https://' . rtrim($host, '/')) : '';
$redirect_url_base = ($apiHost ? $apiHost : '') . '/public/upscale?id=';
$redirect_url_utm = http_build_query($get_params);
$assetsUriPrefix = $config['assetsUriPrefix'] ?? '';
if ($assetsUriPrefix === '' && !is_dir(__DIR__ . '/images') && !empty($apiHost)) {
$assetsUriPrefix = $apiHost;
}
$fontsUrl = $config['fontsUrl'] ?? ($assetsUriPrefix ? $assetsUriPrefix . '/fonts' : 'fonts');
$favicon = (object)[
'baseUrl' => $config['faviconUrl'] ?? ($assetsUriPrefix ? $assetsUriPrefix . '/favicon' : 'favicon')
];
$logoAltExt = $config['logoAltExt'] ?? 'svg';
$spritePath = $config['spritePath'] ?? ($assetsUriPrefix ? $assetsUriPrefix . '/images/sprite.svg' : 'images/sprite.svg');
$default_title = $config['title'] ?? 'Улучшение изображения';
$indpic = isset($UTM['indpic']) && preg_match('/^[a-zA-Z0-9_-]+$/', (string)$UTM['indpic'])
? (string)$UTM['indpic']
: ($config['utm']['indpic']['default'] ?? '2');
function renderComponent(string $name, array $params = []): void
{
global $assetsUriPrefix, $fontsUrl, $favicon, $logoAltExt, $spritePath, $UTM, $config, $indpic, $apiHost, $redirect_url_base, $redirect_url_utm;
extract($params, EXTR_SKIP);
$componentFile = __DIR__ . '/components/' . $name . '.php';
if (file_exists($componentFile)) {
require $componentFile;
}
}
require_once __DIR__ . '/template.php';
?>
<style>
<?= file_get_contents(__DIR__ . '/style.min.css'); ?>
</style>