49 lines
2.2 KiB
PHP
49 lines
2.2 KiB
PHP
<?php
|
|
/** Собственный контракт cons Preland; существование группы проверяет основной API. */
|
|
declare(strict_types=1);
|
|
|
|
/** Переходный runtime-конфиг: новый код работает и до точечного обновления host config. */
|
|
function prelandNormalizeConsConfig(array $config): array
|
|
{
|
|
$config['utm'] = is_array($config['utm'] ?? null) ? $config['utm'] : [];
|
|
unset($config['utm']['chb']);
|
|
if (!is_array($config['utm']['cons'] ?? null)) {
|
|
$config['utm']['cons'] = ['weights' => ['1' => 100, '2' => 0], 'default' => '2'];
|
|
}
|
|
return $config;
|
|
}
|
|
|
|
/** Concrete номера не ограничены random-весами: новые группы создаются в CPanel API. */
|
|
function prelandResolveCons(mixed $value, array $settings): string
|
|
{
|
|
$valid = static fn (mixed $candidate): bool => (is_string($candidate) || is_int($candidate))
|
|
&& preg_match('/^[1-9][0-9]{0,9}$/D', (string) $candidate) === 1
|
|
&& (float) $candidate <= 2147483647;
|
|
$default = $valid($settings['default'] ?? null) ? (string) $settings['default'] : '2';
|
|
if ($valid($value)) return (string) $value;
|
|
if ($value !== 'rnd') return $default;
|
|
|
|
$weights = [];
|
|
foreach ((array) ($settings['weights'] ?? []) as $number => $weight) {
|
|
if ($valid($number) && is_numeric($weight) && is_finite((float) $weight) && (float) $weight > 0) {
|
|
$weights[(string) $number] = (float) $weight;
|
|
}
|
|
}
|
|
$total = array_sum($weights);
|
|
if ($total <= 0 || !is_finite($total)) return $default;
|
|
$point = (mt_rand() / (mt_getrandmax() + 1.0)) * $total;
|
|
foreach ($weights as $number => $weight) {
|
|
if ($point < $weight) return (string) $number;
|
|
$point -= $weight;
|
|
}
|
|
return (string) array_key_last($weights);
|
|
}
|
|
|
|
/** Legacy chb не должен проходить как неизвестный внешний рекламный параметр. */
|
|
function prelandNormalizeConsQuery(array $query, array $settings): array
|
|
{
|
|
unset($query['chb']);
|
|
$query['cons'] = prelandResolveCons($query['cons'] ?? null, $settings);
|
|
return $query;
|
|
}
|