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

@@ -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;
}
}
?>