Exemplo n.º 1
0
Arquivo: App.php Projeto: Hildy/cerb5
 /**
  * @return Model_WatcherMailFilter[]|false
  */
 static function getMatches(CerberusTicket $ticket, $event, $only_worker_id = null)
 {
     $matches = array();
     if (!empty($only_worker_id)) {
         $filters = DAO_WatcherMailFilter::getWhere(sprintf("%s = %d AND %s = %d", DAO_WatcherMailFilter::WORKER_ID, $only_worker_id, DAO_WatcherMailFilter::IS_DISABLED, 0));
     } else {
         $filters = DAO_WatcherMailFilter::getWhere(sprintf("%s = %d", DAO_WatcherMailFilter::IS_DISABLED, 0));
     }
     // [JAS]: Don't send obvious spam to watchers.
     if ($ticket->spam_score >= 0.9) {
         return false;
     }
     // Build our objects
     $ticket_from = DAO_Address::get($ticket->last_wrote_address_id);
     $ticket_group_id = $ticket->team_id;
     // [TODO] These expensive checks should only populate when needed
     $messages = DAO_Ticket::getMessagesByTicket($ticket->id);
     $message_headers = array();
     if (empty($messages)) {
         return false;
     }
     if (null != @($message_last = array_pop($messages))) {
         /* @var $message_last CerberusMessage */
         $message_headers = $message_last->getHeaders();
     }
     // Clear the rest of the message manifests
     unset($messages);
     $custom_fields = DAO_CustomField::getAll();
     // Lazy load when needed on criteria basis
     $ticket_field_values = null;
     $address_field_values = null;
     $org_field_values = null;
     // Worker memberships (for checking permissions)
     $workers = DAO_Worker::getAll();
     $group_rosters = DAO_Group::getRosters();
     // Check filters
     if (is_array($filters)) {
         foreach ($filters as $filter) {
             /* @var $filter Model_WatcherMailFilter */
             $passed = 0;
             // check the worker's group memberships
             if (!isset($workers[$filter->worker_id]) || $workers[$filter->worker_id]->is_disabled || !$workers[$filter->worker_id]->is_superuser && !isset($group_rosters[$ticket->team_id][$filter->worker_id])) {
                 // no membership
                 continue;
             }
             // check criteria
             foreach ($filter->criteria as $rule_key => $rule) {
                 @($value = $rule['value']);
                 switch ($rule_key) {
                     case 'dayofweek':
                         $current_day = strftime('%w');
                         //$current_day = 1;
                         // Forced to English abbrevs as indexes
                         $days = array('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat');
                         // Is the current day enabled?
                         if (isset($rule[$days[$current_day]])) {
                             $passed++;
                         }
                         break;
                     case 'timeofday':
                         $current_hour = strftime('%H');
                         $current_min = strftime('%M');
                         //$current_hour = 17;
                         //$current_min = 5;
                         if (null != ($from_time = @$rule['from'])) {
                             list($from_hour, $from_min) = explode(':', $from_time);
                         }
                         if (null != ($to_time = @$rule['to'])) {
                             if (list($to_hour, $to_min) = explode(':', $to_time)) {
                             }
                         }
                         // Do we need to wrap around to the next day's hours?
                         if ($from_hour > $to_hour) {
                             // yes
                             $to_hour += 24;
                             // add 24 hrs to the destination (1am = 25th hour)
                         }
                         // Are we in the right 24 hourly range?
                         if ((int) $current_hour >= $from_hour && (int) $current_hour <= $to_hour) {
                             // If we're in the first hour, are we minutes early?
                             if ($current_hour == $from_hour && (int) $current_min < $from_min) {
                                 break;
                             }
                             // If we're in the last hour, are we minutes late?
                             if ($current_hour == $to_hour && (int) $current_min > $to_min) {
                                 break;
                             }
                             $passed++;
                         }
                         break;
                     case 'event':
                         if (!empty($event) && is_array($rule) && isset($rule[$event])) {
                             $passed++;
                         }
                         break;
                     case 'groups':
                         if (null !== @($group_buckets = $rule['groups'][$ticket->team_id]) && (empty($group_buckets) || in_array($ticket->category_id, $group_buckets))) {
                             $passed++;
                         }
                         break;
                     case 'next_worker_id':
                         // If it's an assigned event, we only care about the filter's owner
                         if (!empty($event) && 0 == strcasecmp($event, 'ticket_assignment')) {
                             if (intval($value) == intval($filter->worker_id)) {
                                 $passed++;
                                 break;
                             }
                         }
                         if (intval($value) == intval($ticket->next_worker_id)) {
                             $passed++;
                         }
                         break;
                     case 'mask':
                         $regexp_mask = DevblocksPlatform::strToRegExp($value);
                         if (@preg_match($regexp_mask, $ticket->mask)) {
                             $passed++;
                         }
                         break;
                     case 'from':
                         $regexp_from = DevblocksPlatform::strToRegExp($value);
                         if (@preg_match($regexp_from, $ticket_from->email)) {
                             $passed++;
                         }
                         break;
                     case 'subject':
                         $regexp_subject = DevblocksPlatform::strToRegExp($value);
                         if (@preg_match($regexp_subject, $ticket->subject)) {
                             $passed++;
                         }
                         break;
                     case 'body':
                         if (null == ($message_body = $message_last->getContent())) {
                             break;
                         }
                         // Line-by-line body scanning (sed-like)
                         $lines = preg_split("/[\r\n]/", $message_body);
                         if (is_array($lines)) {
                             foreach ($lines as $line) {
                                 if (@preg_match($value, $line)) {
                                     $passed++;
                                     break;
                                 }
                             }
                         }
                         break;
                     case 'header1':
                     case 'header2':
                     case 'header3':
                     case 'header4':
                     case 'header5':
                         @($header = strtolower($rule['header']));
                         if (empty($header)) {
                             $passed++;
                             break;
                         }
                         if (empty($value)) {
                             // we're checking for null/blanks
                             if (!isset($message_headers[$header]) || empty($message_headers[$header])) {
                                 $passed++;
                             }
                         } elseif (isset($message_headers[$header]) && !empty($message_headers[$header])) {
                             $regexp_header = DevblocksPlatform::strToRegExp($value);
                             // Flatten CRLF
                             if (@preg_match($regexp_header, str_replace(array("\r", "\n"), ' ', $message_headers[$header]))) {
                                 $passed++;
                             }
                         }
                         break;
                     default:
                         // ignore invalids
                         // Custom Fields
                         if (0 == strcasecmp('cf_', substr($rule_key, 0, 3))) {
                             $field_id = substr($rule_key, 3);
                             // Make sure it exists
                             if (null == @($field = $custom_fields[$field_id])) {
                                 continue;
                             }
                             // Lazy values loader
                             $field_values = array();
                             switch ($field->source_extension) {
                                 case ChCustomFieldSource_Address::ID:
                                     if (null == $address_field_values) {
                                         $address_field_values = array_shift(DAO_CustomFieldValue::getValuesBySourceIds(ChCustomFieldSource_Address::ID, $ticket_from->id));
                                     }
                                     $field_values =& $address_field_values;
                                     break;
                                 case ChCustomFieldSource_Org::ID:
                                     if (null == $org_field_values) {
                                         $org_field_values = array_shift(DAO_CustomFieldValue::getValuesBySourceIds(ChCustomFieldSource_Org::ID, $ticket_from->contact_org_id));
                                     }
                                     $field_values =& $org_field_values;
                                     break;
                                 case ChCustomFieldSource_Ticket::ID:
                                     if (null == $ticket_field_values) {
                                         $ticket_field_values = array_shift(DAO_CustomFieldValue::getValuesBySourceIds(ChCustomFieldSource_Ticket::ID, $ticket->id));
                                     }
                                     $field_values =& $ticket_field_values;
                                     break;
                             }
                             // Type sensitive value comparisons
                             // [TODO] Operators
                             switch ($field->type) {
                                 case 'S':
                                     // string
                                 // string
                                 case 'T':
                                     // clob
                                 // clob
                                 case 'U':
                                     // URL
                                     $field_val = isset($field_values[$field_id]) ? $field_values[$field_id] : '';
                                     $oper = isset($rule['oper']) ? $rule['oper'] : "=";
                                     if ($oper == "=" && @preg_match(DevblocksPlatform::strToRegExp($value, true), $field_val)) {
                                         $passed++;
                                     } elseif ($oper == "!=" && @(!preg_match(DevblocksPlatform::strToRegExp($value, true), $field_val))) {
                                         $passed++;
                                     }
                                     break;
                                 case 'N':
                                     // number
                                     $field_val = isset($field_values[$field_id]) ? $field_values[$field_id] : 0;
                                     $oper = isset($rule['oper']) ? $rule['oper'] : "=";
                                     if ($oper == "=" && intval($field_val) == intval($value)) {
                                         $passed++;
                                     } elseif ($oper == "!=" && intval($field_val) != intval($value)) {
                                         $passed++;
                                     } elseif ($oper == ">" && intval($field_val) > intval($value)) {
                                         $passed++;
                                     } elseif ($oper == "<" && intval($field_val) < intval($value)) {
                                         $passed++;
                                     }
                                     break;
                                 case 'E':
                                     // date
                                     $field_val = isset($field_values[$field_id]) ? intval($field_values[$field_id]) : 0;
                                     $from = isset($rule['from']) ? $rule['from'] : "0";
                                     $to = isset($rule['to']) ? $rule['to'] : "now";
                                     if (intval(@strtotime($from)) <= $field_val && intval(@strtotime($to)) >= $field_val) {
                                         $passed++;
                                     }
                                     break;
                                 case 'C':
                                     // checkbox
                                     $field_val = isset($field_values[$field_id]) ? $field_values[$field_id] : 0;
                                     if (intval($value) == intval($field_val)) {
                                         $passed++;
                                     }
                                     break;
                                 case 'D':
                                     // dropdown
                                 // dropdown
                                 case 'X':
                                     // multi-checkbox
                                 // multi-checkbox
                                 case 'M':
                                     // multi-picklist
                                 // multi-picklist
                                 case 'W':
                                     // worker
                                     $field_val = isset($field_values[$field_id]) ? $field_values[$field_id] : array();
                                     if (!is_array($value)) {
                                         $value = array($value);
                                     }
                                     if (is_array($field_val)) {
                                         // if multiple things set
                                         foreach ($field_val as $v) {
                                             // loop through possible
                                             if (isset($value[$v])) {
                                                 // is any possible set?
                                                 $passed++;
                                                 break;
                                             }
                                         }
                                     } else {
                                         // single
                                         if (isset($value[$field_val])) {
                                             // is our set field in possibles?
                                             $passed++;
                                             break;
                                         }
                                     }
                                     break;
                             }
                         }
                         break;
                 }
             }
             // If our rule matched every criteria, stop and return the filter
             if ($passed == count($filter->criteria)) {
                 DAO_WatcherMailFilter::increment($filter->id);
                 // ++ the times we've matched
                 $matches[$filter->id] = $filter;
             }
         }
     }
     if (!empty($matches)) {
         return $matches;
     }
     // No matches
     return false;
 }