summaryrefslogtreecommitdiff
path: root/identity_addresses.php
diff options
context:
space:
mode:
Diffstat (limited to 'identity_addresses.php')
-rw-r--r--identity_addresses.php94
1 files changed, 74 insertions, 20 deletions
diff --git a/identity_addresses.php b/identity_addresses.php
index 600c450..e2c1e98 100644
--- a/identity_addresses.php
+++ b/identity_addresses.php
@@ -1,10 +1,10 @@
<?php
class identity_addresses extends rcube_plugin {
public $task = 'settings';
- private $rc;
+ private $rcmail;
function init() {
- $this->rc = rcmail::get_instance();
+ $this->rcmail = rcmail::get_instance();
$this->load_config();
@@ -13,30 +13,84 @@ class identity_addresses extends rcube_plugin {
$this->add_hook('identity_update', array($this, 'validate_update'));
}
- function fix_form($form, $record) {
- echo 'Form: ';
- var_dump($form);
- echo PHP_EOL . 'Record: ';
- var_dump($record);
- die(PHP_EOL);
+ function fix_form($params) {
+ $record = $params['record'];
+ $options = array($record['email'] => $record['email']);
+ $permitted_addresses = $this->get_permitted_addresses($record);
+ foreach ($permitted_addresses as $addr) {
+ $options[$addr] = $addr;
+ }
+
+ $form = $params['form'];
+ $form['addressing']['content']['email'] = array(
+ 'type' => 'select',
+ 'skip-empty' => true,
+ 'options' => $options
+ );
+ return array('form' => $form, 'record' => $params['record']);
}
- function validate_create($login, $record) {
- echo 'Login: ';
- var_dump($login);
- echo PHP_EOL . 'Record: ';
- var_dump($record);
- die(PHP_EOL);
+ function validate_create($params) {
+ return array('abort' => false, 'record' => $params['record']);
}
- function validate_update($id, $record) {
- echo 'ID: ';
- var_dump($id);
- echo PHP_EOL . 'Record: ';
- var_dump($record);
- die(PHP_EOL);
+ function validate_update($params) {
+ return array('abort' => false, 'record' => $params['record']);
}
function validate_modification() {}
+
+ private function get_permitted_addresses($record) {
+ if (!($sql = $this->rcmail->config->get('identity_addresses_query'))) {
+ return [];
+ }
+
+ if ($dsn = $this->rcmail->config->get('identity_addresses_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()) {
+ return [];
+ }
+
+
+ $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);
+
+ $result = $db->query($sql);
+ if ($db->is_error($result))
+ return [];
+
+ $permitted_addresses = [];
+ while ($row = $db->fetch_array($result))
+ $permitted_addresses[] = $row[0];
+
+ return $permitted_addresses;
+ }
+
+ 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;
+ }
}
?>