Skip to content

Commit

Permalink
Merge pull request #25 from gnanet/admin-extension
Browse files Browse the repository at this point in the history
Extend configuration to specify admin accounts
  • Loading branch information
akoeb committed Mar 20, 2018
2 parents 4a6b265 + efcca94 commit 582331d
Show file tree
Hide file tree
Showing 17 changed files with 314 additions and 267 deletions.
12 changes: 6 additions & 6 deletions AccountConfig.php
Expand Up @@ -7,8 +7,8 @@
This file is part of the amacube Roundcube plugin
Copyright (C) 2013, Alexander Köb <nerdkram@koeb.me>
Licensed under the GNU General Public License version 3.
See the COPYING file for a full license statement.
Licensed under the GNU General Public License version 3.
See the COPYING file for a full license statement.
*/
include_once('AmacubeAbstract.php');
Expand All @@ -25,9 +25,9 @@ function __construct($db_config) {
parent::__construct($db_config);
// Check config
$this->initialized = $this->get_account();

}

function get_account() {

$table_account = $this->rc->config->get('amacube_accounts_db_account_table');
Expand Down Expand Up @@ -78,10 +78,10 @@ function get_account() {
return true;
}
}
}
}
}
return false;
}

}
?>
8 changes: 4 additions & 4 deletions AmacubeAbstract.php
Expand Up @@ -7,8 +7,8 @@
This file is part of the amacube Roundcube plugin
Copyright (C) 2013, Alexander Köb <nerdkram@koeb.me>
Licensed under the GNU General Public License version 3.
See the COPYING file for a full license statement.
Licensed under the GNU General Public License version 3.
See the COPYING file for a full license statement.
*/
class AmacubeAbstract {
Expand All @@ -17,7 +17,7 @@ class AmacubeAbstract {

private $db_config; // Store DB config
protected $db_conn; // DB connection

public $errors = array(); // Store errors

// Constructor
Expand Down Expand Up @@ -56,7 +56,7 @@ function init_db()
}

// Return the last database error
function db_error()
function db_error()
{
if (!$this->db_conn) {
return false;
Expand Down
50 changes: 25 additions & 25 deletions AmavisConfig.php
Expand Up @@ -7,8 +7,8 @@
This file is part of the amacube Roundcube plugin
Copyright (C) 2013, Alexander Köb <nerdkram@koeb.me>
Licensed under the GNU General Public License version 3.
See the COPYING file for a full license statement.
Licensed under the GNU General Public License version 3.
See the COPYING file for a full license statement.
*/
include_once('AmacubeAbstract.php');
Expand Down Expand Up @@ -38,17 +38,17 @@ class AmavisConfig extends AmacubeAbstract
'bypass_header_checks' => false, // bool
// 'spam_modifies_subj' => false, // bool
'spam_tag_level' => -999, // float
'spam_tag2_level' => 6, // float
'spam_tag3_level' => 12, // float
'spam_kill_level' => 12, // float
'spam_dsn_cutoff_level' => 20, // float
'spam_tag2_level' => 7, // float
'spam_tag3_level' => 7, // float
'spam_kill_level' => 7, // float
'spam_dsn_cutoff_level' => 10, // float
'spam_quarantine_cutoff_level' => 20, // float

'virus_quarantine_to' => true, // string 'sql:', but treated as boolean
'spam_quarantine_to' => false, // string 'sql:', but treated as boolean
'banned_quarantine_to' => false, // string 'sql:', but treated as boolean
'spam_quarantine_to' => true, // string 'sql:', but treated as boolean
'banned_quarantine_to' => true, // string 'sql:', but treated as boolean
'bad_header_quarantine_to' => true, // string 'sql:', but treated as boolean

'unchecked_quarantine_to' => false, // string 'sql:', but treated as boolean
'clean_quarantine_to' => false, // string 'sql:', but treated as boolean
'archive_quarantine_to' => false, // string 'sql:', but treated as boolean
Expand Down Expand Up @@ -76,7 +76,7 @@ class AmavisConfig extends AmacubeAbstract
'clean_quarantine_to',
'archive_quarantine_to'
);
/*
/*
The following settings are unused, I added the lines for later implementation if needed
addr_extension_virus; // unused
Expand Down Expand Up @@ -118,7 +118,7 @@ function __construct($db_config)
$this->verify_policy_array();
}
}

// Method for verifying policy config
function verify_policy_array($array = null)
{
Expand Down Expand Up @@ -159,7 +159,7 @@ function verify_policy_array($array = null)
}
// Return true
return true;

}


Expand All @@ -173,20 +173,20 @@ function set_policy($array)
} else {
// Write policy array to instance variable
$this->policy_setting = $array;
return true;
return true;
}
}

// read amavis settings from database
function read_from_db()
function read_from_db()
{
if (!is_resource($this->db_conn)) {
if (!$this->init_db()) { return false; }
}
// Get query for user and policy config
$query = "SELECT users.id as user_id, users.priority, users.email, users.fullname, policy.*
FROM users, policy
WHERE users.policy_id = policy.id
WHERE users.policy_id = policy.id
AND users.email = ? ";
$res = $this->db_conn->query($query, $this->user_email);
// Error check
Expand Down Expand Up @@ -300,19 +300,19 @@ function write_to_db()
}
// Convenience methods
function is_delivery($type,$method) {

if ($type == 'banned') { $lover = $type.'_files_lover'; }
else { $lover = $type.'_lover'; }

if ($method == 'deliver' && $this->policy_setting[$lover]) { return true; }
if ($method == 'quarantine' && !$this->policy_setting[$lover] && $this->policy_setting[$type.'_quarantine_to']) { return true; }
if ($method == 'discard' && !$this->policy_setting[$lover] && !$this->policy_setting[$type.'_quarantine_to']) { return true; }
return false;

}

function is_active($type) {

if ($type == 'virus' || $type == 'spam') {
return !$this->policy_setting['bypass_'.$type.'_checks'];
}
Expand All @@ -321,9 +321,9 @@ function is_active($type) {

// Mapping function for internal representation -> database content
function map_to_db($key, $value)
{
{
$retval = null;
// Map boolean settings to Y/N
// Map boolean settings to Y/N
if (in_array($key, self::$boolean_settings)) {
if ($value) { $retval = 'Y'; }
else { $retval = 'N'; }
Expand All @@ -339,11 +339,11 @@ function map_to_db($key, $value)
return $retval;
}

// Mapping function for internal representation <- database content
// Mapping function for internal representation <- database content
function map_from_db($key, $value)
{
$retval = null;
// Map boolean settings from Y/N
// Map boolean settings from Y/N
if (in_array($key, self::$boolean_settings)) {
if (!empty($value) && $value == 'Y') { $retval = true; }
else { $retval = false; }
Expand Down
68 changes: 49 additions & 19 deletions AmavisQuarantine.php
@@ -1,4 +1,4 @@
<?php
<?php
/**
* AmavisQuarantine - class to load, delete or release Amavis quarantined emails in/from DB
*/
Expand All @@ -8,16 +8,16 @@
Copyright (C) 2013, Alexander Köb <nerdkram@koeb.me>
Licensed under the GNU General Public License version 3.
See the COPYING file for a full license statement.
See the COPYING file for a full license statement.
*/
include_once('AmacubeAbstract.php');
class AmavisQuarantine extends AmacubeAbstract
{
// Amavis
// Amavis
private $amavis_host = '';
private $amavis_port = '';

// Constructor
function __construct($db_config, $amavis_host, $amavis_port)
{
Expand All @@ -35,7 +35,13 @@ function __construct($db_config, $amavis_host, $amavis_port)

// Returns a list of (all) quarantined emails
function list_quarantines($start_index = 0, $rows_displayed = 0) {

$ama_admin = false;
foreach ( $this->rc->config->get('amacube_amavis_admins') as $s_admin ) {
if ( strtolower($s_admin) == strtolower($this->user_email) ) {
$ama_admin = true;
}
}

if (!is_resource($this->db_conn)) {
if (!$this->init_db()) { return false; }
}
Expand All @@ -47,25 +53,29 @@ function list_quarantines($start_index = 0, $rows_displayed = 0) {
msgs.content AS content,
dsn_sent AS dsn,
ds AS delivery_status,
bspam_level AS level,
msgs.spam_level AS level,
size,
sender.email AS sender,
from_addr AS sender,
recip.email AS recipient,
msgs.subject AS subject,
msgs.mail_id AS id
FROM msgs LEFT JOIN msgrcpt ON msgs.mail_id=msgrcpt.mail_id
FROM msgs LEFT JOIN msgrcpt AS msgrcpt ON msgs.mail_id=msgrcpt.mail_id
LEFT JOIN maddr AS sender ON msgs.sid=sender.id
LEFT JOIN maddr AS recip ON msgrcpt.rid=recip.id
LEFT JOIN quarantine AS quar ON quar.mail_id = msgs.mail_id
WHERE msgs.content IS NOT NULL
AND ds = 'D'
AND rs IN ('','v')
AND quar.chunk_ind = 1
AND msgs.quar_type = 'Q'";
AND msgs.quar_type = 'Q' ";
if ($ama_admin !== true) {
if ($this->rc->amacube->catchall) {
$id = '%'.$this->user_email;
$query .= "AND recip.email LIKE ?";
$query .= " AND recip.email LIKE ? ";
} else {
$id = $this->user_email;
$query .= "AND recip.email = ?";
$query .= " AND recip.email = ? ";
}
}
$query .= " ORDER BY msgs.time_num DESC";
// prepare statement and execute
Expand Down Expand Up @@ -108,19 +118,20 @@ function delete($mails) {
if (!$this->init_db()) { return false; }
}
//rcube::write_log('errors','AMACUBE: Delete: '.implode(',',$mails));

if (is_array($mails)) {
if (count($mails) < 1) { return true; }
$query_start = 'DELETE FROM ';
$query_end = ' WHERE mail_id in ('.implode(',',array_fill(0, count($mails), '?')).')';
foreach(array('quarantine', 'msgrcpt', 'msgs') as $table) {
$res = $this->db_conn->query($query_start.$table.$query_end, $mails);

// logical delete => set RS to D
$query_start = 'UPDATE ';
$table = 'msgrcpt';
$query_end = ' SET rs = "D" WHERE mail_id in ('.implode(',',array_fill(0, count($mails), '?')).')';
$res = $this->db_conn->query($query_start.$table.$query_end, $mails);
// Error check
if ($error = $this->db_conn->is_error()) {
$this->rc->amacube->errors[] = 'db_delete_error';
rcube::write_log('errors','AMACUBE: Delete: Database error: '.$error);
}
}
}
if ($error) { return false; }
return count($mails);
Expand All @@ -132,7 +143,7 @@ function delete($mails) {
* calls delete on success
*/
function release($mails) {

if (!is_resource($this->db_conn)) {
if (!$this->init_db()) { return false; }
}
Expand Down Expand Up @@ -202,7 +213,26 @@ function release($mails) {
$this->rc->amacube->errors[] = $error = 'release_error';
rcube::write_log('errors',"AMACUBE: Release: Socket open error: $errstr ($errno)\n");
}
$this->delete($success_ids);

// $this->delete($success_ids);
if (is_array($success_ids)) {
if (count($success_ids) < 1) { return true; }

// logical release => set RS to R
$query_start = 'UPDATE ';
$table = 'msgrcpt';
$query_end = ' SET rs = "R" WHERE mail_id in ('.implode(',',array_fill(0, count($success_ids), '?')).')';
$res = $this->db_conn->query($query_start.$table.$query_end, $success_ids);
// Error check
if ($error = $this->db_conn->is_error()) {
$this->rc->amacube->errors[] = 'db_release_error';
rcube::write_log('errors','AMACUBE: Release: Database error: '.$error);
}


}


if (count($error_ids) > 0) {
// Error check
$this->rc->amacube->errors[] = $error = 'release_error';
Expand Down
3 changes: 2 additions & 1 deletion amacube.js
Expand Up @@ -103,4 +103,5 @@ if (window.rcmail) {
}
}
});
}
}

4 changes: 2 additions & 2 deletions amacube.mysql.schema
Expand Up @@ -3,9 +3,9 @@ following structure:

CREATE TABLE IF NOT EXISTS `mailbox` (
`email` varbinary(255) NOT NULL,
`catchall` int(1) NOT NULL DEFAULT '1',
`catchall` int(1) NOT NULL DEFAULT '0',
`filter` int(1) NOT NULL DEFAULT '1'
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
) ENGINE=InnoDB DEFAULT CHARSET=utf8

You can put this table in your existing amavis database, or recreate a new one.

Expand Down

0 comments on commit 582331d

Please sign in to comment.