feat: replace chb with consent groups and preserve runtime config (api#317, #17)
deploy-preland / deploy zoomstage.online (push) Successful in 42s
deploy-preland / deploy zoomstage.online (push) Successful in 42s
This commit is contained in:
1 parent
188b78a115
commit
89cc16eb4a
12 files changed
+208
-9
No files matched your search
@@ -0,0 +1,48 @@
|
||||
<?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;
|
||||
}
|
||||
+12
-1
@@ -72,6 +72,16 @@ $basePickerAccept = 'image/jpeg,image/png,image/gif,image/webp,image/bmp,image/a
|
||||
const utmData = <?= $utmJson ?>;
|
||||
const redirectUrlBase = <?= json_encode($redirectUrlBase, JSON_UNESCAPED_UNICODE) ?>;
|
||||
const redirectUrlUtm = <?= json_encode($redirectUrlUtm, JSON_UNESCAPED_UNICODE) ?>;
|
||||
// Сохранённая до перехода на cons загрузка не должна вернуть legacy chb.
|
||||
const normalizeConsentRedirect = query => {
|
||||
const params = new URLSearchParams(query || '');
|
||||
params.delete('chb');
|
||||
const value = params.get('cons');
|
||||
if (!/^[1-9][0-9]{0,9}$/.test(value || '') || Number(value) > 2147483647) {
|
||||
params.set('cons', String(utmData.cons || '2'));
|
||||
}
|
||||
return params.toString();
|
||||
};
|
||||
const createApiUrl = <?= json_encode($createApiUrl, JSON_UNESCAPED_UNICODE) ?>;
|
||||
const uploadPolicyApiUrl = <?= json_encode($uploadPolicyApiUrl, JSON_UNESCAPED_UNICODE) ?>;
|
||||
|
||||
@@ -202,7 +212,8 @@ $basePickerAccept = 'image/jpeg,image/png,image/gif,image/webp,image/bmp,image/a
|
||||
|
||||
function redirectToTour(record) {
|
||||
let targetUrl = record.meta.redirectBase + encodeURIComponent(record.key);
|
||||
if (record.meta.redirectUtm) targetUrl += '&' + record.meta.redirectUtm;
|
||||
const redirectQuery = normalizeConsentRedirect(record.meta.redirectUtm);
|
||||
if (redirectQuery) targetUrl += '&' + redirectQuery;
|
||||
const shownTheme = window.ZoomzillaTheme.current();
|
||||
targetUrl += '&shown_theme=' + encodeURIComponent(shownTheme);
|
||||
window.PublicNavigationGuard.allowNavigation(() => { window.location.href = targetUrl; });
|
||||
|
||||
Reference in new issue
Block a user