Wbl patch

This commit is contained in:
Mikko Husari
2008-12-10 20:19:59 +02:00
parent 773cf4217a
commit 7f07671b68
9 changed files with 611 additions and 44 deletions

View File

@@ -623,6 +623,17 @@ class CmnFns {
return $result;
}
/**
* Gets the 'wb_action' value
* @param none
* @return value
*/
function get_wb_action($get_name = 'wb_action') {
// If there isnt one set, return NULL
$result = (isset($_POST[$get_name])) ? $_POST[$get_name] : NULL;
return $result;
}
/**
* Gets the 'query_string' value
* @param none
@@ -704,5 +715,62 @@ class CmnFns {
echo "parent.location.href = '" . $location . "';";
echo "</SCRIPT>";
}
/**
* Generate HTML for search engine
* @param $content_type: 'B' (attachment) or 'S' (spam)
*/
function rulesSearchEngine($content_type, $submit_page) {
global $conf;
$fields_array = array("f" => translate('From'), "t" => translate('To'));
?>
<table border=0 width="100%">
<form action="<? echo $submit_page ?>" method="get" name="wblist">
<tr><td colspan=2 align="center"><? echo translate('Search for rules whose:'); ?>&nbsp;</td></tr>
<tr><td align="right">&nbsp;
<?
$i = 1;
$array_size = count($fields_array);
foreach ($fields_array as $k => $name) {
echo "\t\t\t$name: \n";
echo "\t\t\t<select name='" . $k . "_criterion' class='button'>\n";
echo "\t\t\t<option value='contains'";
echo "contains" == CmnFns::getGlobalVar($k . '_criterion', GET) ? " selected='true'>" : ">";
echo translate('contains') . "</option>\n";
echo "\t\t\t<option value='not_contain'";
echo "not_contain" == CmnFns::getGlobalVar($k . '_criterion', GET) ? " selected='true'>" : ">";
echo translate('doesn\'t contain') . "</option>\n";
echo "\t\t\t<option value='equals'";
echo "equals" == CmnFns::getGlobalVar($k . '_criterion', GET) ? " selected='true'>" : ">";
echo translate('equals') . "</option>\n";
echo "\t\t\t<option value='not_equal'";
echo "not_equal" == CmnFns::getGlobalVar($k . '_criterion', GET) ? " selected='true'>" : ">";
echo translate('doesn\'t equal') . "</option>\n";
echo "\t\t\t</select>\n";
echo "\t\t\t<input type='text' name='" . $k . "_string' size='20' value='"
. CmnFns::getGlobalVar($k . '_string', GET) . "' />\n";
echo ($i % 2) ? "\t\t\t&nbsp;</td>\n\t\t\t<td align='left'>&nbsp\n" : "\t\t\t&nbsp;</td></tr>\n\t\t\t<tr><td align='right'>&nbsp\n";
$i ++;
}
$i ++;
echo ($i % 2) ? "&nbsp;</td></tr>\n\t\t\t<tr><td colspan='2' align='center'>&nbsp\n" : "&nbsp;</td><td align='left'>&nbsp";
?>
<input type="submit" class="button" name="search_action" value="<? echo translate('Search'); ?>" />
<? if (CmnFns::didSearch())
echo "<input type=\"submit\" class=\"button\" name=\"search_action\" value=\"" . translate('Clear search results') . "\" />";
?>
&nbsp;</td></tr>
</form>
</table>
<?
}
}
?>

View File

@@ -113,7 +113,6 @@ class DBEngine {
$this->db = $db;
}
/**
* Return counts for spam, banned, viruses, bad headers, and pending
* @return array of the 5 counts
@@ -222,8 +221,6 @@ class DBEngine {
return $return;
}
// User methods -------------------------------------------
/**
@@ -350,6 +347,7 @@ class DBEngine {
return $return;
}
/**
* Return all message in quarantine associated with $emailaddress
* @param string $content_type message type ('B', 'S', ...)
@@ -435,6 +433,7 @@ class DBEngine {
$search_clause
AND msgs.quar_type <> ''
ORDER BY $order $vert ";
// Prepare query
$q = $this->db->prepare($query);
@@ -771,6 +770,214 @@ class DBEngine {
return "($result)";
}
/**
* Fetch amavis userid according to email address,
* if not found, then it returns domain-id or
* creates user record and retrieves new id
* @param string $recip_email
* @return string containing user / domain id
*/
function mail2userid($recip_email, $domain_only = false) {
global $conf;
/*
1) Return user-id from amavis.users that corresponds to $recip_email
# select id from users where email = $recip_email limit 1;
*/
$query = 'select id from users where email = ? limit 1;';
if ($domain_only) {
$recip_email = substr($recip_email,strpos($recip_email,"@"));
}
// Query
$q = $this->db->prepare($query);
$result = $this->db->execute($q, array($recip_email));
$this->check_for_error($result, $query);
if ($result->numRows() == 1) {
$return = $result->fetchRow();
return $return['id'];
} else if ($result->numRows() == 0
&& strpos($recip_email,"@")) {
// this result set is no longer needed
$result->free();
$create_or_use_domain = false;
if (!$conf['app']['autocreate_wbl_users']) {
/* If user-id was not found, then we fetch/return the domain id */
return $this->mail2userid(substr($recip_email,strpos($recip_email,"@")));
} else {
/*
this should handle creation of user records, but im not sure what to
do about email aliases...
1. Fetch domain policy_id, priority, local */
$query = 'select priority, policy_id, ("'.$recip_email.'") AS email, local from users where email = ? limit 1;';
$q = $this->db->prepare($query);
$result = $this->db->execute($q, array(substr($recip_email,strpos($recip_email,"@"))));
$this->check_for_error($result, $query);
$policy = $result->fetchRow();
/*
2. Insert new user
*/
$query = 'insert into users (priority, policy_id, email, fullname, local) values (?, ?, ?, "mailzu autocreated user", ?);';
$q = $this->db->prepare($query);
$result = $this->db->execute($q, $policy);
$this->check_for_error($result, $query);
return $this->mail2userid($recip_email);
}
} else if (strpos($recip_email,"@") == 0) {
return 0;
}
}
//function wblist($flag, $r, $mail_id) {
function mailid2sid($mail_id, $domain_only = false) {
/**
return sender id from mailaddr for whitelisting purposes
*/
$query =
'SELECT mailaddr.id AS sid, '
.($domain_only ? 'SUBSTRING(maddr.email, INSTR(maddr.email, "@")) AS email' : 'maddr.email AS email')
.' FROM msgs'
.' LEFT JOIN maddr ON msgs.sid=maddr.id'
.' LEFT JOIN mailaddr ON '.($domain_only ? 'SUBSTRING(maddr.email, INSTR(maddr.email, "@"))' : 'maddr.email').'=mailaddr.email'
.' WHERE mail_id = ?'
. 'LIMIT 1;';
$q = $this->db->prepare($query);
$result = $this->db->execute($q, array($mail_id));
$this->check_for_error($result, $query);
$row = $result->fetchRow();
if ($row['sid'] > 0) {
return $row['sid'];
} else {
$query = 'INSERT INTO mailaddr (priority, email) VALUES (5, ?);';
$q = $this->db->prepare($query);
$result = $this->db->execute($q, array($row['email']));
$this->check_for_error($result, $query);
$query = 'SELECT id FROM mailaddr WHERE email LIKE ?;';
$q = $this->db->prepare($query);
$result = $this->db->execute($q, array($row['email']));
$this->check_for_error($result, $query);
$row = $result->fetchRow();
return $row['id'];
}
}
function wbinsert($wb, $rid, $sid) {
/**
check for existence of the wb-listing rule.
insert/update wblist entry with approriate flag and id:s.
*/
$query = 'select wb from wblist where rid = ? and sid = ? limit 1;';
$q = $this->db->prepare($query);
$result = $this->db->execute($q, array($rid,$sid));
$this->check_for_error($result, $query);
if (is_array($result->fetchRow())) {
$query = 'update wblist set wb = ? where rid = ? and sid = ? limit 1;';
} else {
$query = 'insert into wblist (wb, rid, sid) values (?, ?, ?);';
}
$q = $this->db->prepare($query);
$result = $this->db->execute($q, array($wb,$rid,$sid));
$this->check_for_error($result, $query);
}
function wbdelete($wb, $rid, $sid) {
$query = "DELETE FROM wblist WHERE wb=? AND rid=? AND sid=? LIMIT 1;";
// Prepare query
$q = $this->db->prepare($query);
// Execute query
$result = $this->db->execute($q, array($wb,$rid,$sid));
// Check if error
$this->check_for_error($result, $query);
}
/**
* Return all wblist entries associated with $emailaddresses
* used to delete wblist entries
*/
function get_user_control_list( $emailaddresses, $order = 'sender', $vert = 'ASC', $search_array, $page, $all = false) {
global $conf;
$return = Array();
// grab the display size limit set in config.php
$sizeLimit = (isset ( $conf['app']['displaySizeLimit'] ) && is_numeric( $conf['app']['displaySizeLimit'] ) ?
$conf['app']['displaySizeLimit'] : 50);
if (is_array($search_array)) {
$search_clause = "";
foreach($search_array as $filter) {
$search_clause .= ' AND ' . $filter;
}
}
if ( !$all ) {
// Get where clause for recipient email address(es)
$recipEmailClause = $this->convertEmailaddresses2SQL($emailaddresses);
} else {
$recipEmailClause = 1; // palikka hakee kaikki, milloin tukee domaineja, no domain patsissa tietty!
}
$query = "
SELECT mailaddr.email AS sender,
recip.email AS recipient,
wblist.wb AS rule,
mailaddr.id AS sender_id,
recip.id AS recip_id
FROM wblist
LEFT JOIN mailaddr ON wblist.sid=mailaddr.id
LEFT JOIN users AS recip ON wblist.rid=recip.id
WHERE $recipEmailClause $search_clause
ORDER BY $order $vert;
";
//. ($msgs_all ? ' ' : $emailaddr_clause)
// Prepare query
$q = $this->db->prepare($query);
// Execute query
$result = $this->db->execute($q);
// Check if error
$this->check_for_error($result, $query);
$this->numRows = $result->numRows();
// the row to start fetching
$from = $page * $sizeLimit;
// how many results per page
$res_per_page = $sizeLimit;
// the last row to fetch for this page
$to = $from + $res_per_page - 1;
foreach (range($from, $to) as $rownum) {
if (!$row = $result->fetchrow(DB_FETCHMODE_ASSOC, $rownum)) {
break;
}
$return[] = $this->cleanRow($row);
}
$result->free();
return $return;
}
}
?>

View File

@@ -294,6 +294,65 @@ function updateMessages($flag, $content_type, $emailaddresses, $mail_id_array, $
return $result_array;
}
/**
* Update white/blaclist function
* @param string $flag 'W', 'B'
* @param array $emailaddresses recipient email address(es)
* @param array $mail_id_array containing mail_id of messages to be whitelisted
* @result return array of messages whose release failed
*/
function updateWblist($flag, $emailaddresses, $mail_id_array, $action) {
$result_array = array();
$db = new DBEngine();
$i = 0;
foreach ($mail_id_array as $mail_id_recip) {
// Get mail_id and recipient email address
$temp = preg_split('/_/', $mail_id_recip, 2);
$mail_id = $temp[0];
$recip_email = $temp[1];
// Check if logged in user is admin or logged in user is trying to delete his own messages
if ( Auth::isMailAdmin() || in_array($recip_email, $emailaddresses) ) {
switch ($action) {
case translate('Whitelist by strict addressing'):
case translate('Blacklist by strict addressing'):
$rid = $db->mail2userid($recip_email);
$sid = $db->mailid2sid($mail_id);
$db->wbinsert($flag, $rid, $sid);
break;
case translate('Whitelist by sender domain'):
case translate('Blacklist by sender domain'):
$rid = $db->mail2userid($recip_email);
$sid = $db->mailid2sid($mail_id, true );
$db->wbinsert($flag, $rid, $sid);
break;
case translate('Whitelist by recipient domain'):
case translate('Blacklist by recipient domain'):
$rid = $db->mail2userid($recip_email, true);
$sid = $db->mailid2sid($mail_id);
$db->wbinsert($flag, $rid, $sid);
break;
case translate('Whitelist by both domains'):
case translate('Blacklist by both domains'):
$rid = $db->mail2userid($recip_email, true);
$sid = $db->mailid2sid($mail_id, true);
$db->wbinsert($flag, $rid, $sid);
break;
}
} else {
continue;
}
}
// Return array of messages whose release failed
return $result_array;
}
/**
* Function that sends: