register_action('plugin.sender_access_list.blacklist', array($this, 'to_blacklist')); $this->register_action('plugin.sender_access_list.whitelist', array($this, 'to_whitelist')); $rcmail = rcmail::get_instance(); $this->load_config(); $this->debug = $rcmail->config->get('sender_access_list_debug'); $this->dsn = $rcmail->config->get('sender_access_list_db_dsn', ''); $this->whitelist_query = $rcmail->config->get('sender_access_list_whitelist_query', ''); $this->blacklist_query = $rcmail->config->get('sender_access_list_blacklist_query', ''); if (!$this->dsn) { if ($this->debug) write_log('sender_access_list', 'sender_access_list_db_dsn parameter not found in /plugins/sender_access_list/config.inc.php'); } elseif (!$this->whitelist_query) { if ($this->debug) write_log('sender_access_list', 'sender_access_list_whitelist_query parameter not found in /plugins/sender_access_list/config.inc.php'); } elseif (!$this->blacklist_query) { if ($this->debug) write_log('sender_access_list', 'sender_access_list_blacklist_query parameter not found in /plugins/sender_access_list/config.inc.php'); } else { $this->spam_mbox = $rcmail->config->get('sender_access_list_spam_mbox', $rcmail->config->get('junk_mbox', null)); $this->toolbar = $rcmail->config->get('sender_access_list_mb_toolbar', true); $this->markmenu = $rcmail->config->get('sender_access_list_mb_markmenu', true); $this->messageheaders = $rcmail->config->get('sender_access_list_mb_messageheaders', true); if ($rcmail->action == '' || $rcmail->action == 'show' || $rcmail->action == 'preview') { $this->add_hook('storage_init', array($this, 'storage_init')); } if (($rcmail->action == 'show' || $rcmail->action == 'preview') and $this->messageheaders) { $this->add_hook('message_headers_output', array($this, 'message_headers')); } if ($rcmail->action == '' || $rcmail->action == 'show') { $this->include_script('sender_access_list.js'); $this->add_texts('localization', true); $this->include_stylesheet($this->local_skin_path() .'/sender_access_list.css'); if ($rcmail->output->browser->ie && $rcmail->output->browser->ver == 6) $this->include_stylesheet($this->local_skin_path() . '/ie6hacks.css'); if ($this->toolbar) { $this->add_button(array('command' => 'plugin.sender_access_list.blacklist', 'type' => 'link', 'class' => 'button buttonPas senderblacklist disabled', 'classact' => 'button senderblacklist', 'classsel' => 'button senderblacklistSel', 'title' => 'sender_access_list.buttonblacklist', 'label' => 'sender_access_list.blacklist', 'style' => ''), 'toolbar'); $this->add_button(array('command' => 'plugin.sender_access_list.whitelist', 'type' => 'link', 'class' => 'button buttonPas senderwhitelist disabled', 'classact' => 'button senderwhitelist', 'classsel' => 'button senderwhitelistSel', 'title' => 'sender_access_list.buttonwhitelist', 'label' => 'sender_access_list.whitelist', 'style' => ''), 'toolbar'); } if ($this->markmenu) { $blacklist = $this->api->output->button(array('command' => 'plugin.sender_access_list.blacklist', 'label' => 'sender_access_list.menublacklist', 'id' => 'senderblacklist', 'class' => 'icon senderblacklist', 'classact' => 'icon senderblacklist active', 'innerclass' => 'icon senderblacklist')); $whitelist = $this->api->output->button(array('command' => 'plugin.sender_access_list.whitelist', 'label' => 'sender_access_list.menuwhitelist', 'id' => 'senderwhitelist', 'class' => 'icon senderwhitelist', 'classact' => 'icon senderwhitelist active', 'innerclass' => 'icon senderwhitelist')); $this->api->add_content(html::tag('li', array('style' => ''), $blacklist), 'markmenu'); $this->api->add_content(html::tag('li', array('style' => ''), $whitelist), 'markmenu'); } $this->api->output->set_env('sender_access_list_markmenu', $this->markmenu); } } } function storage_init($p) { $rcmail = rcmail::get_instance(); $p['fetch_headers'] = trim($p['fetch_headers'].' RETURN-PATH'); return $p; } function message_headers($p) { $rcmail = rcmail::get_instance(); $header = 'Return-Path'; if ($value = $p['headers']->get($header)) { $value = preg_replace('/^\s*<(\S+)>\s*$/', '$1', $value); $icon_blacklist = sprintf("", $this->gettext('buttonblacklist'), $this->local_skin_path()); $icon_whitelist = sprintf("", $this->gettext('buttonwhitelist'), $this->local_skin_path()); $p['output'][$header."-accesslist"] = array('title' => $this->gettext('realsenderaddress'), 'value' => ($value ? sprintf("%s %s %s", Q($value), Q($value), Q($value), $icon_blacklist, $icon_whitelist) : '<>')); } return $p; } function to_blacklist() { $this->add_texts('localization'); $uids = get_input_value('_uid', RCUBE_INPUT_POST); $mbox = get_input_value('_mbox', RCUBE_INPUT_POST); $this->_list_sender_address($uids, $mbox, true, $this->blacklist_query); $this->api->output->command('display_message', $this->gettext('reportedasblacklisted'), 'confirmation'); $this->api->output->send(); } function to_whitelist() { $this->add_texts('localization'); $uids = get_input_value('_uid', RCUBE_INPUT_POST); $mbox = get_input_value('_mbox', RCUBE_INPUT_POST); $this->_list_sender_address($uids, $mbox, false, $this->whitelist_query); $this->api->output->command('display_message', $this->gettext('reportedaswhitelisted'), 'confirmation'); $this->api->output->send(); } private function _list_sender_address($uids, $mbox_name = NULL, $blacklist = true, $query) { $rcmail = rcmail::get_instance(); $storage = $rcmail->storage; $db = new rcube_mdb2($this->dsn, '', FALSE); $db->set_debug((bool)$this->debug); $dsn = MDB2::parseDSN($this->dsn); $db->db_connect('w'); if ($error = $db->is_error()) { $error_message = "unable to connect to db. Check DSN and verify that necessary privileges are granted to user '".$dsn['username']."'. Error message: " . $error; raise_error(array('code' => 603, 'type' => 'db', 'file' => __FILE__, 'line' => __LINE__, 'message' => "sender_access_list.php plugin: ".$error_message), true, false); if ($this->debug) write_log('sender_access_list', "DB Error: ".$error_message); $this->api->output->command('display_message', $this->gettext('unable_to_connect_to_db'), 'error'); $this->api->output->send(); } else { $query = str_replace('%u', $db->quote($_SESSION['username'], 'text'), $query); foreach (explode(",", $uids) as $uid) { $message = new rcube_message($uid); $sender_address = $message->headers->get('Return-path'); $sender_address = preg_replace('/^\s*<(\S+)>\s*$/', '$1', $sender_address); if (!$sender_address) { $error_message = "unable to get header Return-path from message with UID ".$uid." for user '".$_SESSION['username']."'"; raise_error(array('code' => 600, 'type' => 'imap', 'file' => __FILE__, 'line' => __LINE__, 'message' => "sender_access_list.php plugin: ".$error_message), true, false); if ($this->debug) write_log('sender_access_list', "IMAP Error: ".$error_message); $this->api->output->command('display_message', $this->gettext('unable_to_get_sender_address'), 'error'); $this->api->output->send(); } elseif ($sender_address == '<>') { $error_message = "can not add an empty sender address to access list for user '".$_SESSION['username']."'"; raise_error(array('code' => 600, 'type' => 'user', 'file' => __FILE__, 'line' => __LINE__, 'message' => "sender_access_list.php plugin: ".$error_message), true, false); if ($this->debug) write_log('sender_access_list', "USER Error: ".$error_message); $this->api->output->command('display_message', $this->gettext('cat_not_add_empty_sender_address'), 'error'); $this->api->output->send(); } else { $sql = str_replace('%sender', $db->quote($sender_address, 'text'), $query); $res = $db->query($sql); if ($error = $db->is_error()) { $error_message = "query '$sql' failed. Check DSN and verify that necessary privileges are granted to user '".$dsn['username']."'. Error message: " . $error; raise_error(array('code' => 600, 'type' => 'db', 'file' => __FILE__, 'line' => __LINE__, 'message' => "sender_access_list.php plugin: ".$error_message), true, false); if ($this->debug) write_log('sender_access_list', "DB Error: ".$error_message); $this->api->output->command('display_message', $this->gettext($blacklist ? "unable_to_save_sender_address_to_blacklist" : "unable_to_save_sender_address_to_whitelist"), 'error'); $this->api->output->send(); } else { if ($this->debug) write_log('sender_access_list', "sender address '".$sender_address."' ".($blacklist ? "blacklisted" : "whitelisted")." by user '".$_SESSION['username']."'"); } } } } // $this->api->output->command('command', 'list', $mbox_name); } } ?>