File migrateconfig.sh of Package roundcubemail-selfcontained

#!/usr/bin/env php
<?php

$home = '/etc/roundcubemail/';

if (!file_exists("{$home}/config.inc.php")) {
    return;
}

// default_host/default_port -> imap_host
// smtp_server/smtp_port -> smtp_host

// For Kolab v3 we deal with a different format, so we first have to read the config
$imap_host = null;
$smtp_host = null;

foreach (file("{$home}/config.inc.php") as $line) {
    if (strpos($line, "\$config['imap_host']") !== false) {
        // Already migrated
        return;
    }

    if (preg_match("~\\\$config\\['default_host'\\]\s*=\s*['\"]?([^'\";]*)['\"]?~", $line, $matches)) {
        $imap_host = $matches[1];
    } elseif (preg_match("~\\\$config\\['default_port'\\]\s*=\s*['\"]?([^'\";]*)['\"]?~", $line, $matches)) {
        $imap_port = trim($matches[1], "'");
    } elseif (preg_match("~\\\$config\\['smtp_server'\\]\s*=\s*['\"]?([^'\";]*)['\"]?~", $line, $matches)) {
        $smtp_host = $matches[1];
    } elseif (preg_match("~\\\$config\\['smtp_port'\\]\s*=\s*['\"]?([^'\";]*)['\"]?~", $line, $matches)) {
        $smtp_port = trim($matches[1], "'");
    }
}

if (!empty($imap_port) && !empty($imap_host)) {
    if (strpos($imap_host, ':' . $imap_port) === false) {
        $imap_host .= ':' . $imap_port;
    }

    if (strpos($imap_host, '://') === false) {
        switch ($imap_port) {
            case 993:
            case 9993:
                $imap_host = 'ssl://' . $imap_host;
                break;
        }
    }
}

if (!empty($smtp_port) && !empty($smtp_host)) {
    if (strpos($smtp_host, ':' . $smtp_port) === false) {
        $smtp_host .= ':' . $smtp_port;
    }

    if (strpos($smtp_host, '://') === false) {
        switch ($smtp_port) {
            case 587:
                $smtp_host = 'tls://' . $smtp_host;
                break;
            case 465:
                $smtp_host = 'ssl://' . $smtp_host;
                break;
        }
    }
}

// TODO: removed `port` option from `ldap_public` array (non-standard port can be set via `host`)
// TODO: removed `use_tls` option from `ldap_public` array (use tls:// prefix in `host`)

// For Kolab v4 we can just replace strings as we have a "constant" content
$replace = [
    "\$config['default_host'] = (getenv('IMAP_TLS') == \"true\" ? \"ssl://\" : \"\") . getenv('IMAP_HOST');"
        => "\$config['imap_host'] = (getenv('IMAP_TLS') == \"true\" ? \"ssl://\" : \"\") . getenv('IMAP_HOST') . ':' . getenv('IMAP_PORT');",
    "\$config['default_port'] = getenv('IMAP_PORT');" => '',
    "\$config['smtp_server'] = \"tls://\" . getenv('SUBMISSION_HOST');"
        => "\$config['smtp_host'] = \"tls://\" . getenv('SUBMISSION_HOST') . ':' . getenv('SUBMISSION_PORT');",
    "\$config['smtp_server'] = getenv('SUBMISSION_HOST');"
        => "\$config['smtp_host'] = getenv('SUBMISSION_HOST') . ':' . getenv('SUBMISSION_PORT');",
    "\$config['smtp_port'] = getenv('SUBMISSION_PORT');" => '',
];

$content = '';
foreach (file("{$home}/config.inc.php") as $line) {
    $line = str_replace(array_keys($replace), array_values($replace), $line);
    if ($imap_host && strpos($line, "\$config['default_host']") !== false) {
        $content .= "    \$config['imap_host'] = '{$imap_host}';\n";
    } elseif ($smtp_host && strpos($line, "\$config['smtp_server']") !== false) {
        $content .= "    \$config['smtp_host'] = '{$smtp_host}';\n";
    } elseif (!preg_match("~\\\$config\\['(default_port|smtp_port)'\\]~", $line)) {
        $content .= $line;
    }
}

file_put_contents("{$home}/config.inc.php", $content);

// managesieve_usetls/managesieve_port

$sieve_host = null;

foreach (file("{$home}/managesieve.inc.php") as $line) {
    if (preg_match("~\\\$config\\['managesieve_host'\\]\s*=\s*['\"]?([^'\";]*)['\"]?~", $line, $matches)) {
        $sieve_host = $matches[1];
    } elseif (preg_match("~\\\$config\\['managesieve_port'\\]\s*=\s*['\"]?([^'\";]*)['\"]?~", $line, $matches)) {
        $sieve_port = trim($matches[1], "'");
    } elseif (preg_match("~\\\$config\\['managesieve_usetls'\\]\s*=\s*true~", $line, $matches)) {
        $sieve_tls = true;
    }
}

if (!empty($sieve_host)) {
    if (!empty($sieve_port) && strpos($sieve_host, ":$sieve_port") === false) {
        $sieve_host .= ':' . $sieve_port;
    }
    if (!empty($sieve_tls) && strpos($sieve_host, "://") === false) {
        $sieve_host = 'tls://' . $sieve_host;
    }
}

$replace = [
    "\$config['managesieve_host'] = str_replace(\"ssl://\", \"\", getenv('IMAP_HOST'));"
        => "\$config['managesieve_host'] = str_replace(\"ssl://\", \"\", getenv('IMAP_HOST')) . ':4190';",
];

$content = '';
foreach (file("{$home}/managesieve.inc.php") as $line) {
    $line = str_replace(array_keys($replace), array_values($replace), $line);
    if ($sieve_host && strpos($line, "\$config['managesieve_host'] = '") !== false) {
        $content .= "    \$config['managesieve_host'] = '{$sieve_host}';\n";
    } elseif (!preg_match("~\\\$config\\['(managesieve_port|managesieve_usetls)'\\]~", $line)) {
        $content .= $line;
    }
}

file_put_contents("{$home}/managesieve.inc.php", $content);