#!/usr/local/bin/php -nc/usr/local/directadmin/plugins/db-manager/php.ini
[],
'present' => [],
];
if (!is_readable($path)) {
return $result;
}
$lines = file($path, FILE_IGNORE_NEW_LINES);
if ($lines === false) {
return $result;
}
foreach ($lines as $line) {
$line = trim($line);
if ($line === '' || $line[0] === '#') {
continue;
}
if (!preg_match('/^([A-Za-z0-9_]+)=(.*)$/', $line, $m)) {
continue;
}
$key = trim((string)$m[1]);
if ($key === '') {
continue;
}
$result['values'][$key] = parse_sh_value((string)$m[2]);
$result['present'][$key] = true;
}
return $result;
}
function parse_plugins_list(string $value): array
{
$value = trim($value);
if ($value === '') {
return [];
}
$out = [];
foreach (explode(':', $value) as $part) {
$part = strtolower(trim($part));
if ($part === '') {
continue;
}
$out[] = $part;
}
return array_values(array_unique($out));
}
function conf_explicit_bool(array $conf, array $keys, ?bool &$out): bool
{
$values = $conf['values'] ?? [];
$present = $conf['present'] ?? [];
$keys_lc = array_map('strtolower', $keys);
foreach ($present as $conf_key => $is_present) {
if (!$is_present) {
continue;
}
$conf_key_lc = strtolower((string)$conf_key);
if (!in_array($conf_key_lc, $keys_lc, true)) {
continue;
}
$out = parse_bool((string)($values[$conf_key] ?? ''), false);
return true;
}
return false;
}
function is_custom_package_item_enabled(array $conf): bool
{
$enabled = null;
if (conf_explicit_bool($conf, ['db_manager'], $enabled)) {
return (bool)$enabled;
}
return false;
}
function is_plugin_allowed_by_conf(array $conf, string $plugin_id): bool
{
$values = $conf['values'] ?? [];
$present = $conf['present'] ?? [];
$plugin_id = strtolower(trim($plugin_id));
if ($plugin_id === '') {
return false;
}
if (!empty($present['plugins_allow'])) {
$allow = parse_plugins_list((string)($values['plugins_allow'] ?? ''));
return in_array($plugin_id, $allow, true);
}
if (!empty($present['plugins_deny'])) {
$deny = parse_plugins_list((string)($values['plugins_deny'] ?? ''));
return !in_array($plugin_id, $deny, true);
}
return true;
}
function resolve_package_conf_path(array $user_conf, string $da_user): string
{
$values = $user_conf['values'] ?? [];
$package_name = trim((string)($values['package'] ?? $values['user_package'] ?? ''));
if ($package_name === '' || !preg_match('/^[A-Za-z0-9._-]+$/', $package_name)) {
return '';
}
$owners = [];
foreach (['creator', 'owner', 'reseller', 'username'] as $key) {
$owner = trim((string)($values[$key] ?? ''));
if ($owner !== '' && preg_match('/^[A-Za-z0-9._-]+$/', $owner)) {
$owners[] = $owner;
}
}
if (preg_match('/^[A-Za-z0-9._-]+$/', $da_user)) {
$owners[] = $da_user;
}
$owners[] = 'admin';
$owners = array_values(array_unique($owners));
foreach ($owners as $owner) {
foreach (['', '.conf'] as $ext) {
$candidate = '/usr/local/directadmin/data/users/' . $owner . '/packages/' . $package_name . $ext;
if (is_readable($candidate)) {
return $candidate;
}
}
}
return '';
}
function is_plugin_enabled_for_user(string $da_user, string $plugin_id): bool
{
$base = '/usr/local/directadmin/data/users/' . $da_user;
$user_conf_path = $base . '/user.conf';
$user_conf = load_conf_with_presence($user_conf_path);
if (!is_custom_package_item_enabled($user_conf)) {
return false;
}
if (!is_plugin_allowed_by_conf($user_conf, $plugin_id)) {
return false;
}
$package_conf_path = resolve_package_conf_path($user_conf, $da_user);
if ($package_conf_path === '') {
return true;
}
$package_conf = load_conf_with_presence($package_conf_path);
if (!is_custom_package_item_enabled($package_conf)) {
return false;
}
return is_plugin_allowed_by_conf($package_conf, $plugin_id);
}
function load_lang_map(string $lang, string $plugin_dir): array
{
$lang = normalize_lang_code($lang);
$lang_dir = $plugin_dir . '/lang';
$fallback_path = $lang_dir . '/en.php';
$lang_path = $lang_dir . '/' . $lang . '.php';
$fallback = [];
if (is_readable($fallback_path)) {
$fallback_map = include $fallback_path;
if (is_array($fallback_map)) {
$fallback = $fallback_map;
}
}
if ($lang === 'en') {
return $fallback;
}
if (!is_readable($lang_path)) {
return $fallback;
}
$selected = include $lang_path;
if (!is_array($selected)) {
return $fallback;
}
return array_merge($fallback, $selected);
}
function translate_text(string $text, array $map): string
{
if (empty($map)) {
return $text;
}
uksort($map, function ($a, $b) {
return strlen((string)$b) <=> strlen((string)$a);
});
return str_replace(array_keys($map), array_values($map), $text);
}
function translate_html_safe(string $html, array $map): string
{
if (empty($map)) {
return $html;
}
$parts = preg_split('~()~is', $html, -1, PREG_SPLIT_DELIM_CAPTURE);
if ($parts === false) {
return translate_text($html, $map);
}
$out = '';
foreach ($parts as $part) {
if (preg_match('~^