Пример #1
0
 static function getMatches(Model_Address $fromAddress, CerberusParserMessage $message)
 {
     //		print_r($fromAddress);
     //		print_r($message);
     $matches = array();
     $rules = DAO_MailToGroupRule::getWhere();
     $message_headers = $message->headers;
     $custom_fields = DAO_CustomField::getAll();
     // Lazy load when needed on criteria basis
     $address_field_values = null;
     $org_field_values = null;
     // Check filters
     if (is_array($rules)) {
         foreach ($rules as $rule) {
             /* @var $rule Model_MailToGroupRule */
             $passed = 0;
             // check criteria
             foreach ($rule->criteria as $crit_key => $crit) {
                 @($value = $crit['value']);
                 switch ($crit_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($crit[$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 = @$crit['from'])) {
                             list($from_hour, $from_min) = explode(':', $from_time);
                         }
                         if (null != ($to_time = @$crit['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 'tocc':
                         $tocc = array();
                         $destinations = DevblocksPlatform::parseCsvString($value);
                         // Build a list of To/Cc addresses on this message
                         @($to_list = imap_rfc822_parse_adrlist($message_headers['to'], 'localhost'));
                         @($cc_list = imap_rfc822_parse_adrlist($message_headers['cc'], 'localhost'));
                         if (is_array($to_list)) {
                             foreach ($to_list as $addy) {
                                 $tocc[] = $addy->mailbox . '@' . $addy->host;
                             }
                         }
                         if (is_array($cc_list)) {
                             foreach ($cc_list as $addy) {
                                 $tocc[] = $addy->mailbox . '@' . $addy->host;
                             }
                         }
                         $dest_flag = false;
                         // bail out when true
                         if (is_array($destinations) && is_array($tocc)) {
                             foreach ($destinations as $dest) {
                                 if ($dest_flag) {
                                     break;
                                 }
                                 $regexp_dest = DevblocksPlatform::strToRegExp($dest);
                                 foreach ($tocc as $addy) {
                                     if (@preg_match($regexp_dest, $addy)) {
                                         $passed++;
                                         $dest_flag = false;
                                         break;
                                     }
                                 }
                             }
                         }
                         break;
                     case 'from':
                         $regexp_from = DevblocksPlatform::strToRegExp($value);
                         if (@preg_match($regexp_from, $fromAddress->email)) {
                             $passed++;
                         }
                         break;
                     case 'subject':
                         // [TODO] Decode if necessary
                         @($subject = $message_headers['subject']);
                         $regexp_subject = DevblocksPlatform::strToRegExp($value);
                         if (@preg_match($regexp_subject, $subject)) {
                             $passed++;
                         }
                         break;
                     case 'body':
                         // 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($crit['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($crit_key, 0, 3))) {
                             $field_id = substr($crit_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, $fromAddress->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, $fromAddress->contact_org_id));
                                     }
                                     $field_values =& $org_field_values;
                                     break;
                             }
                             // No values, default.
                             if (!isset($field_values[$field_id])) {
                                 continue;
                             }
                             // Type sensitive value comparisons
                             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($crit['oper']) ? $crit['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
                                     if (!isset($field_values[$field_id])) {
                                         break;
                                     }
                                     $field_val = isset($field_values[$field_id]) ? $field_values[$field_id] : 0;
                                     $oper = isset($crit['oper']) ? $crit['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($crit['from']) ? $crit['from'] : "0";
                                     $to = isset($crit['to']) ? $crit['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($rule->criteria)) {
                 DAO_MailToGroupRule::increment($rule->id);
                 // ++ the times we've matched
                 $matches[$rule->id] = $rule;
                 // Bail out if this rule had a move action
                 if (isset($rule->actions['move'])) {
                     return $matches;
                 }
             }
         }
     }
     // If we're at the end of rules and didn't bail out yet
     if (!empty($matches)) {
         return $matches;
     }
     // No matches
     return NULL;
 }