.php. The file starts with * "", so it prints nothing if a web server ever runs it, and continues with JSON that is read here * as text and never executed. The folder also holds a deny-all .htaccess and an empty index.html. * * No WordPress functions are used here: the core's own entry point (public/index.php) loads this file as well. */ if (!function_exists('aismart_runtime_settings_dir')) { /** * The settings folder, worked out from the plugin's own location (wp-content/plugins/) so that the * plugin and the core always agree on it: wp-content/uploads/aismart-content-studio. */ function aismart_runtime_settings_dir(string $plugin_dir): string { return dirname(rtrim($plugin_dir, '/\\'), 2) . '/uploads/aismart-content-studio'; } } if (!function_exists('aismart_runtime_settings_guard')) { /** First line of every settings file. */ function aismart_runtime_settings_guard(): string { return "\n"; } } if (!function_exists('aismart_runtime_settings_file')) { /** The settings file in $dir (the newest when there are several), or '' when there is none. */ function aismart_runtime_settings_file(string $dir): string { $dir = rtrim($dir, '/\\'); $names = is_dir($dir) ? @scandir($dir) : false; if (!is_array($names)) { return ''; } $found = []; foreach ($names as $name) { if (preg_match('/^runtime-[0-9a-f]{32}\.php$/', (string) $name) && is_file($dir . '/' . $name)) { $found[$dir . '/' . $name] = (int) @filemtime($dir . '/' . $name); } } if (!$found) { return ''; } arsort($found); return (string) array_key_first($found); } } if (!function_exists('aismart_runtime_normalize_host')) { /** Lower-case host name without port, or '' when the value is not a plain host name. */ function aismart_runtime_normalize_host(string $host): string { $host = rtrim((string) preg_replace('/:\d+$/', '', strtolower(trim($host))), '.'); $label = '[a-z0-9](?:[a-z0-9-]*[a-z0-9])?'; return preg_match('/^' . $label . '(?:\.' . $label . ')*$/', $host) ? $host : ''; } } if (!function_exists('aismart_runtime_settings_read')) { /** * Reads a settings file as text: ['env' => [KEY => value], 'hosts' => [host => site address]], * or null when the file is missing or not in the expected format. */ function aismart_runtime_settings_read(string $file): ?array { if ($file === '' || !is_file($file) || !is_readable($file)) { return null; } $raw = @file_get_contents($file); $guard = aismart_runtime_settings_guard(); if (!is_string($raw) || strncmp($raw, $guard, strlen($guard)) !== 0) { return null; } $data = json_decode(substr($raw, strlen($guard)), true); if (!is_array($data) || !isset($data['env']) || !is_array($data['env'])) { return null; } $env = []; foreach ($data['env'] as $key => $value) { if (is_string($key) && preg_match('/^[A-Z][A-Z0-9_]*$/', $key) && (is_scalar($value) || $value === null)) { $env[$key] = is_bool($value) ? ($value ? 'true' : 'false') : (string) $value; } } $hosts = []; if (isset($data['hosts']) && is_array($data['hosts'])) { foreach ($data['hosts'] as $host => $url) { $host = aismart_runtime_normalize_host((string) $host); if ($host !== '' && is_string($url) && preg_match('#^https?://\S+$#i', $url)) { $hosts[$host] = $url; } } } return ['env' => $env, 'hosts' => $hosts]; } } if (!function_exists('aismart_runtime_settings_apply')) { /** * Puts the runtime settings into the environment the core reads (getenv, $_ENV, $_SERVER). * APP_URL follows the request's host when that host is one of the sites that wrote the settings; any other * host gets the default address. With $override false, values that are already set are kept. * Returns true when a settings file was found and applied. */ function aismart_runtime_settings_apply(string $plugin_dir, string $request_host = '', bool $override = true): bool { $settings = aismart_runtime_settings_read(aismart_runtime_settings_file(aismart_runtime_settings_dir($plugin_dir))); if ($settings === null) { return false; } $env = $settings['env']; $host = aismart_runtime_normalize_host($request_host); if ($host !== '' && isset($settings['hosts'][$host])) { $env['APP_URL'] = $settings['hosts'][$host]; } foreach ($env as $key => $value) { if (!$override && (getenv($key) !== false || array_key_exists($key, $_ENV) || array_key_exists($key, $_SERVER))) { continue; } putenv($key . '=' . $value); $_ENV[$key] = $value; $_SERVER[$key] = $value; } return true; } }