<?php
class inbox_settings extends rcube_plugin {
    public $task = 'settings';
    private $rcmail;

    function init() {
        $this->rcmail = rcmail::get_instance();

        $this->load_config();
        $this->add_texts('localization/');

        $this->add_hook('settings_actions', [$this, 'amend_settings_list']);
        if ($this->rcmail->config->get('inbox_settings_scrub_encryption_preference', true))
            $this->add_hook(
                'preferences_sections_list',
                [$this, 'scrub_encryption_preference']);

        $this->register_action('plugin.inbox_settings', [$this, 'render_settings_ui']);
        $this->register_action('plugin.inbox_keys', [$this, 'render_keys_ui']);
        $this->register_action('plugin.inbox_keys_import', [$this, 'render_import_ui']);
        $this->register_action('plugin.inbox_keys_delete', [$this, 'delete_selected_keys']);
    }

    function scrub_encryption_preference($params) {
        unset($params['list']['encryption']);
        return $params;
    }

    function amend_settings_list($params) {
        $params['actions'][] = [
            'action' => 'plugin.inbox_settings',
            'class' => 'inbox_settings',
            'label' => 'inbox_settings',
            'title' => 'inbox_settings',
            'domain' => 'inbox_settings'
        ];
        $params['actions'][] = [
            'action' => 'plugin.inbox_keys',
            'class' => 'enigma keys',
            'label' => 'inbox_keys',
            'title' => 'inbox_keys',
            'domain' => 'inbox_settings'
        ];
        return $params;
    }

    private function save_settings() {
        if (!isset($_POST['save']))
            return;

        $encrypt_inbox = isset($_POST['encrypt_inbox']) ? '1' : '0';
        if (!is_array($this->run_query('update_encryption', ['%se' => $encrypt_inbox])))
            return;

        if (empty($_POST['addr']) || !is_array($_POST['addr']))
            return;

        $success = true;
        foreach ($_POST['addr'] as $idx => $addr) {
            if (!empty($_POST['fwd_addr'][$idx])
                && !rcube_utils::check_email($_POST['fwd_addr'][$idx]))
                continue;

            $fwd_addr = $_POST['fwd_addr'][$idx];
            $fwd_addr = empty($fwd_addr) ? '' : $fwd_addr;
            $do_forward = isset($_POST['do_fwd'][$idx]) ? '1' : '0';
            
            $local_part = $addr;
            $domain_part = '';
            if ($at_separator = strrpos($addr, '@')) {
                $local_part = substr($addr, 0, $at_separator);
                $domain_part = substr($addr, $at_separator + 1);
            }

            $success &= is_array($this->run_query('update_forwarder', [
                '%aa' => $addr,
                '%al' => $local_part,
                '%ad' => $domain_part,
                '%sf' => $do_forward,
                '%sa' => $fwd_addr
            ]));
        }

        if ($success)
            $this->rcmail->output->command(
                'display_message', $this->gettext('settings_saved'), 'notice');
    }

    function render_settings_ui() {
        $this->register_handler('plugin.body', [$this, 'render_settings_form']);
        $this->rcmail->output->set_pagetitle($this->gettext('inbox_settings'));
        $this->save_settings();
        $this->rcmail->output->send('plugin');
    }

    function render_settings_form() {
        $page_title = html::tag('h1', 'voice', rcube::Q($this->gettext('inbox_settings')));
        $encryption_result = $this->run_query('encryption_enabled');
        $encrypt_inbox = 0;
        if (!is_array($encryption_result))
            $encryption_result = [];
        if (!empty($encryption_result) && !empty($encryption_result[0]))
            $encrypt_inbox = $encryption_result[0][0] ? 1 : 0;

        $encryption_form = new html_table(['cols' => 2, 'class' => 'propform']);
        $encryption_form->add('title col-sm-8', html::label(
            ['class' => 'col-form-label', 'for' => 'rcmfd_encrypt_inbox'],
            rcube::Q($this->gettext('encrypt_inbox'))));
        $encryption_form->add('col-sm-2 offset-1', self::get_checkbox(
            'rcmfd_encrypt_inbox', 'encrypt_inbox', $encrypt_inbox));

        $forwarders = $this->run_query('forwarding_addresses');
        if (!is_array($forwarders))
            $forwarders = [];

        $forwarding_form = new html_table(['cols' => 3, 'class' => 'propform']);
        foreach ($forwarders as $idx => $row) {
            $forwarding_form->add('title col-sm-4',
                html::label(
                    ['class' => 'col-form-label', 'for' => 'fwd_addr_' . $idx],
                    rcube::Q($row[0]))
                . self::get_hiddenfield('addr[' . $idx . ']', $row[0]));
            $forwarding_form->add('col-sm-4',
                self::get_textfield(
                    'fwd_addr_' . $idx,
                    'fwd_addr[' . $idx . ']',
                    $row[1],
                    ['placeholder' => 'anyone@gmail.com']));
            $forwarding_form->add('col-sm-2 offset-1',
                self::get_checkbox(
                    'do_fwd_' . $idx,
                    'do_fwd[' . $idx . ']',
                    $row[2],
                    ['title' => $this->gettext('fwd_addr')]));
        }

        $form = html::div(['class' => 'formcontent'],
                    html::tag('fieldset', null,
                        html::tag('legend', null, rcube::Q($this->gettext('encryption')))
                        . $encryption_form->show(null)
                    )
                  . html::tag('fieldset', null,
                        html::tag('legend', null, rcube::Q($this->gettext('forwarding')))
                        . $forwarding_form->show(null)));
        $save_button = (new html_button([
                'type' => 'submit',
                'name' => 'save',
                'class' => 'button mainaction submit'
            ]))->show($this->rcmail->gettext('save'));
        $form_buttons = html::div(['class' => 'formbuttons'], $save_button);
        $page_content = html::div(['class' => 'formcontainer'],
                            $page_title
                            . $this->rcmail->output->form_tag([
                                'action' => $this->rcmail->url(
                                    ['action' => 'plugin.inbox_settings']),
                                'method' => 'post'                            
                            ], $form . $form_buttons));
        return $page_content;
    }

    function render_keys_ui() {
        $this->rcmail->output->add_handler('keyslist', [$this, 'render_keys_list']);
        $this->rcmail->output->set_pagetitle($this->gettext('inbox_keys'));
        $this->rcmail->output->send('inbox_settings.keys');
    }

    function render_keys_list($attrib) {
        if (empty($attrib['id']))
            $attrib['id'] = 'rcpgpkeyslist';

        // info the js needs access to
        $this->rcmail->output->set_env('userIDs', $this->rcmail->user->list_emails());
        $this->rcmail->output->add_gui_object('keyslist', $attrib['id']);
        $this->rcmail->output->add_label(
            'inbox_settings.generating_key',
            'inbox_settings.importing_key',
            'inbox_settings.confirm_delete_key',
            'inbox_settings.deleting_key');

        $this->rcmail->output->include_script('list.js');
        $this->include_script('openpgp.min.js');
        $this->include_script('keys.js');

        $data = $this->run_query('keys', [], true);
        if (!is_array($data))
            $data = [];
        else if (empty($data))
            $this->rcmail->output->command(
                'display_message', $this->gettext('no_keys'), 'notice');

        $data = self::label_assoc_2d($data);
        return rcmail_action::table_output(
            $attrib,
            $data,
            ['inbox_settings.fingerprint', 'inbox_settings.comment'],
            'inbox_settings.id');
    }

    private function import_keys() {
        if (!isset($_POST['import']) || empty($_POST['key_data']))
            return false;

        try {
            $dearmored_key_data = self::parse_keys($_POST['key_data']);
        } catch (Throwable $e) {
            $this->rcmail->output->command(
                'display_message', $this->gettext('invalid_pgp_data'), 'error');
            return false;
        }
        if (empty($dearmored_key_data)) {
            $this->rcmail->output->command(
                'display_message', $this->gettext('no_key_data'), 'error');
            return false;
        }

        $comment = $_POST['comment'];
        if (empty($comment)) $comment = null;

        foreach ($dearmored_key_data as $parsed_key) {
            if (!is_array($this->run_query(
                'add_key',
                ['%f' => $parsed_key['fingerprint'],
                 '%k' => $parsed_key['data_blob'],
                 '%c' => $comment])))
                return false;
        }

        $this->rcmail->output->command(
            'display_message', $this->gettext('keys_imported'), 'notice');
        return !isset($_POST['no_refresh']);
    }

    function render_import_ui() {
        $this->rcmail->output->set_pagetitle($this->gettext('key_import'));
        if ($this->import_keys())
            $this->rcmail->output->redirect('plugin.inbox_keys');
        else
            $this->rcmail->output->send('inbox_settings.key_import');
    }

    function delete_selected_keys() {
        if (is_array($this->run_query('delete_keys', ['%k' => $_POST['keys']])))
            $this->rcmail->output->command(
                'display_message', $this->gettext('keys_deleted'), 'notice');

        $this->rcmail->output->send();
    }

    private static function label_assoc_2d($data) {
        $labeled_data = [];
        foreach ($data as $row) {
            $labeled_row = [];
            foreach ($row as $col_name => $value) {
                $labeled_row['inbox_settings.' . $col_name] = $value;
            }
            $labeled_data[] = $labeled_row;
        }
        return $labeled_data;
    }

    private static function get_checkbox($id, $name, $checked, $attrs = []) {
        return html::div(['class' => 'custom-control custom-switch'],
            (new html_checkbox([
                'id' => $id,
                'name' => $name,
                'class' => 'form-check-input custom-control-input',
                'value' => '1'
            ] + $attrs))->show($checked ? '1' : '0')
            . html::label(['class' => 'custom-control-label', 'for' => $id] + $attrs, ''));
    }

    private static function get_textfield($id, $name, $value = '', $attrs = []) {
        return (new html_inputfield([
            'id' => $id,
            'type' => 'text',
            'name' => $name,
            'size' => 40,
        ] + $attrs))->show($value);
    }

    private static function get_textarea($id, $name, $value = '', $attrs = []) {
        return (new html_textarea([
            'id' => $id,
            'rows' => 10,
            'name' => $name,
        ] + $attrs))->show($value);
    }

    private static function get_hiddenfield($name, $value) {
        return (new html_inputfield(['type' => 'hidden', 'name' => $name]))->show($value);
    }

    private function run_query($qid, $substitutions = [], $assoc = false) {
        if (!($sql = $this->rcmail->config->get('inbox_settings_' . $qid . '_query'))) {
            $this->rcmail->output->command(
                'display_message', $this->gettext('no_query'), 'error');
            return null;
        }

        if ($dsn = $this->rcmail->config->get('inbox_settings_db_dsn')) {
            $db = rcube_db::factory(self::parse_dsn($dsn), '', false);
            $db->set_debug((bool) $this->rcmail->config->get('sql_debug'));
        } else {
            $db = $this->rcmail->get_dbh();
        }

        if ($db->is_error()) {
            $this->rcmail->output->command(
                'display_message', $this->gettext('no_db'), 'error');
            return null;
        }

        $local_part = $this->rcmail->user->get_username('local');
        $domain_part = rcube_utils::idn_to_utf8($this->rcmail->user->get_username('domain'));
        $username = rcube_utils::idn_to_utf8($_SESSION['username']);
        $host = rcube_utils::idn_to_utf8($_SESSION['imap_host']);

        $sql = str_replace('%l', $db->quote($local_part, 'text'), $sql);
        $sql = str_replace('%d', $db->quote($domain_part, 'text'), $sql);
        $sql = str_replace('%u', $db->quote($username, 'text'), $sql);
        $sql = str_replace('%h', $db->quote($host, 'text'), $sql);

        foreach ($substitutions as $key => $value) {
            $sql = str_replace($key, self::quote_replacement($db, $value), $sql);
        }

        $result = $db->query($sql);
        if ($db->is_error($result)) {
            $this->rcmail->output->command(
                'display_message', $this->gettext('sql_error'), 'error');
            return null;
        }

        $ndresult = [];
        while ($row = $assoc ? $db->fetch_assoc($result) : $db->fetch_array($result))
            $ndresult[] = $row;

        return $ndresult;
    }

    private static function quote_replacement($db, $value) {
        if (!is_array($value)) return $db->quote($value, 'text');
        $quoted = [];
        foreach ($value as $item) {
            $quoted[] = self::quote_replacement($db, $item);
        }
        return '(' . implode(', ', $quoted) . ')';
    }

    private static function parse_dsn($dsn) {
        if (strpos($dsn, '%')) {
            // parse DSN and replace variables in hostname
            $parsed = rcube_db::parse_dsn($dsn);
            $host = rcube_utils::parse_host($parsed['hostspec']);

            // build back the DSN string
            if ($host != $parsed['hostspec']) {
                $dsn = str_replace('@' . $parsed['hostspec'], '@' . $host, $dsn);
            }
        }

        return $dsn;
    }

    private static function parse_keys($armored) {
        require_once(__DIR__ . '/openpgp.php');

        $dearmored = OpenPGP::unarmor($armored);
        if (empty($dearmored))
            return null;

        $parsed_data = OpenPGP_Message::parse($dearmored);
        if (!($parsed_data instanceof OpenPGP_Message) || !is_array($parsed_data->packets))
            throw new IllegalArgumentException(
                'Input data was not a PGP message with data packets.');

        $parsed_keys = [];
        $fingerprint = null;
        $data_message = null;
        foreach ($parsed_data->packets as $data_packet) {
            if ($data_packet instanceof OpenPGP_SecretKeyPacket)
                throw new IllegalArgumentException('Input data contains private keys.');

            if ($data_packet instanceof OpenPGP_PublicKeyPacket
                && !($data_packet instanceof OpenPGP_PublicSubkeyPacket)) {
                if (!is_null($fingerprint) && !empty($data_message->packets))
                    $parsed_keys[] = [
                        'fingerprint' => $fingerprint,
                        'data_blob' => $data_message->to_bytes()];

                $fingerprint = $data_packet->fingerprint;
                $data_message = new OpenPGP_Message();
            } else if ((!($data_packet instanceof OpenPGP_UserIDPacket)
                && !($data_packet instanceof OpenPGP_SignaturePacket)
                && !($data_packet instanceof OpenPGP_PublicSubkeyPacket))
                || is_null($fingerprint)) {
                // ignore unknown packet types for the time being
                continue;
            }

            $data_message[] = $data_packet;
        }

        if (!is_null($fingerprint) && !empty($data_message->packets)) {
            $parsed_keys[] = [
                'fingerprint' => $fingerprint, 'data_blob' => $data_message->to_bytes()];
        }
        return $parsed_keys;
    }
}
?>
