Skip to content
Snippets Groups Projects
Commit f519363f authored by Peter Rotich's avatar Peter Rotich
Browse files

Merge pull request #2 from greezybacon/feature/filter-reject-ticket

Review - Reject tickets from banned emails. 

*Looks good 
parents b3a3c818 28911eec
No related branches found
No related tags found
No related merge requests found
...@@ -38,7 +38,7 @@ class Filter { ...@@ -38,7 +38,7 @@ class Filter {
return false; return false;
$this->ht=db_fetch_array($res); $this->ht=db_fetch_array($res);
$this->id=$info['id']; $this->id=$this->ht['id'];
return true; return true;
} }
...@@ -589,16 +589,46 @@ class EmailFilter { ...@@ -589,16 +589,46 @@ class EmailFilter {
array_push($this->filters, new Filter($id)); array_push($this->filters, new Filter($id));
return $this->filters; return $this->filters;
} }
/**
* Fetches the short list of filters that match the email received in the
* constructor. This function is memoized so subsequent calls will
* return immediately.
*/
function getMatchingFilterList() {
if (!isset($this->short_list)) {
$this->short_list = array();
foreach ($this->filters as $filter)
if ($filter->matches($this->email))
$this->short_list[] = $filter;
}
return $this->short_list;
}
/**
* Determine if the filters that match the received email indicate that
* the email should be rejected
*
* Returns FALSE if the email should be acceptable. If the email should
* be rejected, the first filter that matches and has rejectEmail set is
* returned.
*/
function shouldReject() {
foreach ($this->getMatchingFilterList() as $filter) {
# Set reject if this filter indicates that the email should
# be blocked; however, don't unset $reject, because if it
# was set by another rule that did not set stopOnMatch(), we
# should still honor its configuration
if ($filter->rejectEmail()) return $filter;
}
return false;
}
/** /**
* Determine if any filters match the received email, and if so, apply * Determine if any filters match the received email, and if so, apply
* actions defined in those filters to the ticket-to-be-created. * actions defined in those filters to the ticket-to-be-created.
*/ */
function apply(&$ticket) { function apply(&$ticket) {
foreach ($this->filters as $filter) { foreach ($this->getMatchingFilterList() as $filter) {
if ($filter->matches($this->email)) { $filter->apply($ticket, $this->email);
$filter->apply($ticket, $this->email); if ($filter->stopOnMatch()) break;
if ($filter->stopOnMatch()) break;
}
} }
} }
......
...@@ -1627,12 +1627,21 @@ class Ticket{ ...@@ -1627,12 +1627,21 @@ class Ticket{
function create($vars,&$errors, $origin, $autorespond=true, $alertstaff=true) { function create($vars,&$errors, $origin, $autorespond=true, $alertstaff=true) {
global $cfg,$thisclient,$_FILES; global $cfg,$thisclient,$_FILES;
//Make sure the email is not banned //Make sure the email address is not banned
if ($vars['email'] && EmailFilter::isBanned($vars['email'])) { if ($vars['email'] && EmailFilter::isBanned($vars['email'])) {
$errors['err']='Ticket denied. Error #403'; $errors['err']='Ticket denied. Error #403';
Sys::log(LOG_WARNING,'Ticket denied','Banned email - '.$vars['email']); Sys::log(LOG_WARNING,'Ticket denied','Banned email - '.$vars['email']);
return 0; return 0;
} }
// Make sure email contents should not be rejected
if (($email_filter=new EmailFilter($vars))
&& ($filter=$email_filter->shouldReject())) {
$errors['err']='Ticket denied. Error #403';
Sys::log(LOG_WARNING,'Ticket denied',
sprintf('Banned email - %s by filter "%s"', $vars['email'],
$filter->getName()));
return 0;
}
$id=0; $id=0;
$fields=array(); $fields=array();
...@@ -1692,7 +1701,7 @@ class Ticket{ ...@@ -1692,7 +1701,7 @@ class Ticket{
} }
# Perform email filter actions on the new ticket arguments XXX: Move filter to the top and check for reject... # Perform email filter actions on the new ticket arguments XXX: Move filter to the top and check for reject...
if (!$errors && $ef = new EmailFilter($vars)) $ef->apply($vars); if (!$errors && $email_filter) $email_filter->apply($vars);
# Some things will need to be unpacked back into the scope of this # Some things will need to be unpacked back into the scope of this
# function # function
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment