Example #1
0
 /**
  * First we check the references and in-reply-to headers to find a 
  * historical match in the database. If those don't match we check 
  * the subject line for a mask (if one exists). If none of those
  * options match we return null.
  *
  * @param array $headers
  * @return array
  */
 private static function findParentMessage($headers)
 {
     @($aSubject = $headers['subject']);
     @($sMessageId = trim($headers['message-id']));
     @($sInReplyTo = trim($headers['in-reply-to']));
     @($sReferences = trim($headers['references']));
     @($sThreadTopic = trim($headers['thread-topic']));
     // [TODO] Could turn string comparisons into hashes here for simple equality checks
     $aReferences = array();
     // Add all References
     if (!empty($sReferences)) {
         if (preg_match("/(\\<.*?\\@.*?\\>)/", $sReferences, $matches)) {
             unset($matches[0]);
             // who cares about the pattern
             foreach ($matches as $ref) {
                 $ref = trim($ref);
                 if (!empty($ref) && 0 != strcasecmp($ref, $sMessageId)) {
                     $aReferences[$ref] = 1;
                 }
             }
         }
     }
     unset($matches);
     // Append first <*> from In-Reply-To
     if (!empty($sInReplyTo)) {
         if (preg_match("/(\\<.*?\\@.*?\\>)/", $sInReplyTo, $matches)) {
             if (isset($matches[1])) {
                 // only use the first In-Reply-To
                 $ref = trim($matches[1]);
                 if (!empty($ref) && 0 != strcasecmp($ref, $sMessageId)) {
                     $aReferences[$ref] = 1;
                 }
             }
         }
     }
     // Try matching our references or in-reply-to
     if (is_array($aReferences) && !empty($aReferences)) {
         foreach (array_keys($aReferences) as $ref) {
             if (empty($ref)) {
                 continue;
             }
             if (null != ($ids = DAO_Ticket::getTicketByMessageId($ref))) {
                 return $ids;
             }
         }
     }
     // Try matching the subject line
     // [TODO] This should only happen if the destination has subject masks enabled
     if (!is_array($aSubject)) {
         $aSubject = array($aSubject);
     }
     foreach ($aSubject as $sSubject) {
         if (preg_match("/.*\\[.*?\\#(.*?)\\].*/", $sSubject, $matches)) {
             if (isset($matches[1])) {
                 $mask = $matches[1];
                 if (null != ($ticket = DAO_Ticket::getTicketByMask($mask))) {
                     return array('ticket_id' => intval($ticket->id), 'message_id' => intval($ticket->first_message_id));
                 }
             }
         }
     }
     return NULL;
 }