Beispiel #1
0
 private function _handleImportTicket($xml)
 {
     $settings = CerberusSettings::getInstance();
     $logger = DevblocksPlatform::getConsoleLog();
     $workers = DAO_Worker::getAll();
     static $email_to_worker_id = null;
     static $group_name_to_id = null;
     static $bucket_name_to_id = null;
     // Hash Workers so we can ID their incoming tickets
     if (null == $email_to_worker_id) {
         $email_to_worker_id = array();
         if (is_array($workers)) {
             foreach ($workers as $worker) {
                 /* @var $worker CerberusWorker */
                 $email_to_worker_id[strtolower($worker->email)] = intval($worker->id);
             }
         }
     }
     // Hash Group names
     if (null == $group_name_to_id) {
         $groups = DAO_Group::getAll();
         $group_name_to_id = array();
         if (is_array($groups)) {
             foreach ($groups as $group) {
                 $group_name_to_id[strtolower($group->name)] = intval($group->id);
             }
         }
     }
     // Hash Bucket names
     if (null == $bucket_name_to_id) {
         $buckets = DAO_Bucket::getAll();
         $bucket_name_to_id = array();
         if (is_array($buckets)) {
             foreach ($buckets as $bucket) {
                 /* @var $bucket CerberusCategory */
                 // Hash by team ID and bucket name
                 $hash = md5($bucket->team_id . strtolower($bucket->name));
                 $bucket_to_id[$hash] = intval($bucket->id);
             }
         }
     }
     $sMask = (string) $xml->mask;
     $sSubject = substr((string) $xml->subject, 0, 255);
     $sGroup = (string) $xml->group;
     $sBucket = (string) $xml->bucket;
     $iCreatedDate = (int) $xml->created_date;
     $iUpdatedDate = (int) $xml->updated_date;
     $isWaiting = (int) $xml->is_waiting;
     $isClosed = (int) $xml->is_closed;
     if (empty($sMask)) {
         $sMask = CerberusApplication::generateTicketMask();
     }
     // Find the destination Group + Bucket (or create them)
     if (empty($sGroup)) {
         $iDestGroupId = 0;
         if (null != ($iDestGroup = DAO_Group::getDefaultGroup())) {
             $iDestGroupId = $iDestGroup->id;
         }
     } elseif (null == ($iDestGroupId = @$group_name_to_id[strtolower($sGroup)])) {
         $iDestGroupId = DAO_Group::createTeam(array(DAO_Group::TEAM_NAME => $sGroup));
         // Give all superusers manager access to this new group
         if (is_array($workers)) {
             foreach ($workers as $worker) {
                 if ($worker->is_superuser) {
                     DAO_Group::setTeamMember($iDestGroupId, $worker->id, true);
                 }
             }
         }
         // Rehash
         DAO_Group::getAll(true);
         $group_name_to_id[strtolower($sGroup)] = $iDestGroupId;
     }
     if (empty($sBucket)) {
         $iDestBucketId = 0;
         // Inbox
     } elseif (null == ($iDestBucketId = @$bucket_name_to_id[md5($iDestGroupId . strtolower($sBucket))])) {
         $iDestBucketId = DAO_Bucket::create($sBucket, $iDestGroupId);
         // Rehash
         DAO_Bucket::getAll(true);
         $bucket_name_to_id[strtolower($sBucket)] = $iDestBucketId;
     }
     // Xpath the first and last "from" out of "/ticket/messages/message/headers/from"
     $aMessageNodes = $xml->xpath("/ticket/messages/message");
     $iNumMessages = count($aMessageNodes);
     @($eFirstMessage = reset($aMessageNodes));
     if (is_null($eFirstMessage)) {
         $logger->warn('[Importer] Ticket ' . $sMask . " doesn't have any messages.  Skipping.");
         return false;
     }
     if (is_null($eFirstMessage->headers) || is_null($eFirstMessage->headers->from)) {
         $logger->warn('[Importer] Ticket ' . $sMask . " first message doesn't provide a sender address.");
         return false;
     }
     $sFirstWrote = self::_parseRfcAddressList($eFirstMessage->headers->from, true);
     if (null == ($firstWroteInst = CerberusApplication::hashLookupAddress($sFirstWrote, true))) {
         $logger->warn('[Importer] Ticket ' . $sMask . " - Invalid sender adddress: " . $sFirstWrote);
         return false;
     }
     $eLastMessage = end($aMessageNodes);
     if (is_null($eLastMessage)) {
         $logger->warn('[Importer] Ticket ' . $sMask . " doesn't have any messages.  Skipping.");
         return false;
     }
     if (is_null($eLastMessage->headers) || is_null($eLastMessage->headers->from)) {
         $logger->warn('[Importer] Ticket ' . $sMask . " last message doesn't provide a sender address.");
         return false;
     }
     $sLastWrote = self::_parseRfcAddressList($eLastMessage->headers->from, true);
     if (null == ($lastWroteInst = CerberusApplication::hashLookupAddress($sLastWrote, true))) {
         $logger->warn('[Importer] Ticket ' . $sMask . ' last message has an invalid sender address: ' . $sLastWrote);
         return false;
     }
     // Last action code + last worker
     $sLastActionCode = CerberusTicketActionCode::TICKET_OPENED;
     $iLastWorkerId = 0;
     if ($iNumMessages > 1) {
         if (null != @($iLastWorkerId = $email_to_worker_id[strtolower($lastWroteInst->email)])) {
             $sLastActionCode = CerberusTicketActionCode::TICKET_WORKER_REPLY;
         } else {
             $sLastActionCode = CerberusTicketActionCode::TICKET_CUSTOMER_REPLY;
             $iLastWorkerId = 0;
         }
     }
     // Dupe check by ticket mask
     if (null != DAO_Ticket::getTicketByMask($sMask)) {
         $logger->warn("[Importer] Ticket mask '" . $sMask . "' already exists.  Making it unique.");
         $uniqueness = 1;
         $origMask = $sMask;
         // Append new uniqueness to the ticket mask:  LLL-NNNNN-NNN-1, LLL-NNNNN-NNN-2, ...
         do {
             $sMask = $origMask . '-' . ++$uniqueness;
         } while (null != DAO_Ticket::getTicketIdByMask($sMask));
         $logger->info("[Importer] The unique mask for '" . $origMask . "' is now '" . $sMask . "'");
     }
     // Create ticket
     $fields = array(DAO_Ticket::MASK => $sMask, DAO_Ticket::SUBJECT => $sSubject, DAO_Ticket::IS_WAITING => $isWaiting, DAO_Ticket::IS_CLOSED => $isClosed, DAO_Ticket::FIRST_WROTE_ID => intval($firstWroteInst->id), DAO_Ticket::LAST_WROTE_ID => intval($lastWroteInst->id), DAO_Ticket::CREATED_DATE => $iCreatedDate, DAO_Ticket::UPDATED_DATE => $iUpdatedDate, DAO_Ticket::TEAM_ID => intval($iDestGroupId), DAO_Ticket::CATEGORY_ID => intval($iDestBucketId), DAO_Ticket::LAST_ACTION_CODE => $sLastActionCode, DAO_Ticket::LAST_WORKER_ID => intval($iLastWorkerId));
     $ticket_id = DAO_Ticket::createTicket($fields);
     //		echo "Ticket: ",$ticket_id,"<BR>";
     //		print_r($fields);
     // Create requesters
     if (!is_null($xml->requesters)) {
         foreach ($xml->requesters->address as $eAddress) {
             /* @var $eAddress SimpleXMLElement */
             $sRequesterAddy = (string) $eAddress;
             // [TODO] RFC822
             // Insert requesters
             if (null == ($requesterAddyInst = CerberusApplication::hashLookupAddress($sRequesterAddy, true))) {
                 $logger->warn('[Importer] Ticket ' . $sMask . ' - Ignoring malformed requester: ' . $sRequesterAddy);
                 continue;
             }
             DAO_Ticket::createRequester($requesterAddyInst->id, $ticket_id);
         }
     }
     // Create messages
     $is_first = true;
     if (!is_null($xml->messages)) {
         foreach ($xml->messages->message as $eMessage) {
             /* @var $eMessage SimpleXMLElement */
             $eHeaders =& $eMessage->headers;
             /* @var $eHeaders SimpleXMLElement */
             $sMsgFrom = (string) $eHeaders->from;
             $sMsgDate = (string) $eHeaders->date;
             $sMsgFrom = self::_parseRfcAddressList($sMsgFrom, true);
             if (NULL == $sMsgFrom) {
                 $logger->warn('[Importer] Ticket ' . $sMask . ' - Invalid message sender: ' . $sMsgFrom . ' (skipping)');
                 continue;
             }
             if (null == ($msgFromInst = CerberusApplication::hashLookupAddress($sMsgFrom, true))) {
                 $logger->warn('[Importer] Ticket ' . $sMask . ' - Invalid message sender: ' . $sMsgFrom . ' (skipping)');
                 continue;
             }
             @($msgWorkerId = intval($email_to_worker_id[strtolower($msgFromInst->email)]));
             //			$logger->info('Checking if '.$msgFromInst->email.' is a worker');
             $fields = array(DAO_Message::TICKET_ID => $ticket_id, DAO_Message::CREATED_DATE => strtotime($sMsgDate), DAO_Message::ADDRESS_ID => $msgFromInst->id, DAO_Message::IS_OUTGOING => !empty($msgWorkerId) ? 1 : 0, DAO_Message::WORKER_ID => !empty($msgWorkerId) ? $msgWorkerId : 0);
             $email_id = DAO_Message::create($fields);
             // First thread
             if ($is_first) {
                 DAO_Ticket::updateTicket($ticket_id, array(DAO_Ticket::FIRST_MESSAGE_ID => $email_id));
                 $is_first = false;
             }
             // Create attachments
             if (!is_null($eMessage->attachments)) {
                 foreach ($eMessage->attachments->attachment as $eAttachment) {
                     /* @var $eAttachment SimpleXMLElement */
                     $sFileName = (string) $eAttachment->name;
                     $sMimeType = (string) $eAttachment->mimetype;
                     $sFileSize = (int) $eAttachment->size;
                     $sFileContentB64 = (string) $eAttachment->content;
                     // [TODO] This could be a little smarter about detecting extensions
                     if (empty($sMimeType)) {
                         $sMimeType = "application/octet-stream";
                     }
                     $sFileContent = base64_decode($sFileContentB64);
                     unset($sFileContentB64);
                     $fields = array(DAO_Attachment::MESSAGE_ID => $email_id, DAO_Attachment::DISPLAY_NAME => $sFileName, DAO_Attachment::FILE_SIZE => intval($sFileSize), DAO_Attachment::FILEPATH => '', DAO_Attachment::MIME_TYPE => $sMimeType);
                     $file_id = DAO_Attachment::create($fields);
                     // Write file to disk using ID (Model)
                     $file_path = Model_Attachment::saveToFile($file_id, $sFileContent);
                     unset($sFileContent);
                     // Update attachment table
                     DAO_Attachment::update($file_id, array(DAO_Attachment::FILEPATH => $file_path));
                 }
             }
             // Create message content
             $sMessageContentB64 = (string) $eMessage->content;
             $sMessageContent = base64_decode($sMessageContentB64);
             // Content-type specific handling
             if (isset($eMessage->content['content-type'])) {
                 // do we have a content-type?
                 if (strtolower($eMessage->content['content-type']) == 'html') {
                     // html?
                     // Force to plaintext part
                     $sMessageContent = CerberusApplication::stripHTML($sMessageContent);
                 }
             }
             unset($sMessageContentB64);
             DAO_MessageContent::create($email_id, $sMessageContent);
             unset($sMessageContent);
             // Headers
             foreach ($eHeaders->children() as $eHeader) {
                 /* @var $eHeader SimpleXMLElement */
                 DAO_MessageHeader::create($email_id, $eHeader->getName(), (string) $eHeader);
             }
         }
     }
     // Create comments
     if (!is_null($xml->comments)) {
         foreach ($xml->comments->comment as $eComment) {
             /* @var $eMessage SimpleXMLElement */
             $iCommentDate = (int) $eComment->created_date;
             $sCommentAuthor = (string) $eComment->author;
             // [TODO] Address Hash Lookup
             $sCommentTextB64 = (string) $eComment->content;
             $sCommentText = base64_decode($sCommentTextB64);
             unset($sCommentTextB64);
             $commentAuthorInst = CerberusApplication::hashLookupAddress($sCommentAuthor, true);
             // [TODO] Sanity checking
             $fields = array(DAO_TicketComment::TICKET_ID => intval($ticket_id), DAO_TicketComment::CREATED => intval($iCommentDate), DAO_TicketComment::ADDRESS_ID => intval($commentAuthorInst->id), DAO_TicketComment::COMMENT => $sCommentText);
             $comment_id = DAO_TicketComment::create($fields);
             unset($sCommentText);
         }
     }
     $logger->info('[Importer] Imported ticket #' . $ticket_id);
     return true;
 }
Beispiel #2
0
 function saveTeamAction()
 {
     $translate = DevblocksPlatform::getTranslationService();
     $worker = CerberusApplication::getActiveWorker();
     if (!$worker || !$worker->is_superuser) {
         echo $translate->_('common.access_denied');
         return;
     }
     if (DEMO_MODE) {
         DevblocksPlatform::setHttpResponse(new DevblocksHttpResponse(array('config', 'workflow')));
         return;
     }
     @($id = DevblocksPlatform::importGPC($_POST['id']));
     @($name = DevblocksPlatform::importGPC($_POST['name']));
     @($delete = DevblocksPlatform::importGPC($_POST['delete_box']));
     @($delete_move_id = DevblocksPlatform::importGPC($_POST['delete_move_id'], 'integer', 0));
     if (empty($name)) {
         $name = "No Name";
     }
     if (!empty($id) && !empty($delete)) {
         if (!empty($delete_move_id)) {
             $fields = array(DAO_Ticket::TEAM_ID => $delete_move_id);
             $where = sprintf("%s=%d", DAO_Ticket::TEAM_ID, $id);
             DAO_Ticket::updateWhere($fields, $where);
             DAO_Group::deleteTeam($id);
         }
     } elseif (!empty($id)) {
         $fields = array(DAO_Group::TEAM_NAME => $name);
         DAO_Group::updateTeam($id, $fields);
     } else {
         $fields = array(DAO_Group::TEAM_NAME => $name);
         $id = DAO_Group::createTeam($fields);
     }
     @($worker_ids = DevblocksPlatform::importGPC($_POST['worker_ids'], 'array', array()));
     @($worker_levels = DevblocksPlatform::importGPC($_POST['worker_levels'], 'array', array()));
     @($members = DAO_Group::getTeamMembers($id));
     if (is_array($worker_ids) && !empty($worker_ids)) {
         foreach ($worker_ids as $idx => $worker_id) {
             @($level = $worker_levels[$idx]);
             if (isset($members[$worker_id]) && empty($level)) {
                 DAO_Group::unsetTeamMember($id, $worker_id);
             } elseif (!empty($level)) {
                 // member|manager
                 DAO_Group::setTeamMember($id, $worker_id, 1 == $level ? false : true);
             }
         }
     }
     DevblocksPlatform::setHttpResponse(new DevblocksHttpResponse(array('config', 'groups')));
 }
Beispiel #3
0
     // Dispatch Group
     $dispatch_gid = DAO_Group::createTeam(array(DAO_Group::TEAM_NAME => 'Dispatch'));
     // Dispatch Spam Bucket
     $dispatch_spam_bid = DAO_Bucket::create('Spam', $dispatch_gid);
     DAO_GroupSettings::set($dispatch_gid, DAO_GroupSettings::SETTING_SPAM_ACTION, '2');
     DAO_GroupSettings::set($dispatch_gid, DAO_GroupSettings::SETTING_SPAM_ACTION_PARAM, $dispatch_spam_bid);
     DAO_GroupSettings::set($dispatch_gid, DAO_GroupSettings::SETTING_SPAM_THRESHOLD, '85');
     // Support Group
     $support_gid = DAO_Group::createTeam(array(DAO_Group::TEAM_NAME => 'Support'));
     // Support Spam Bucket
     $support_spam_bid = DAO_Bucket::create('Spam', $support_gid);
     DAO_GroupSettings::set($support_gid, DAO_GroupSettings::SETTING_SPAM_ACTION, '2');
     DAO_GroupSettings::set($support_gid, DAO_GroupSettings::SETTING_SPAM_ACTION_PARAM, $support_spam_bid);
     DAO_GroupSettings::set($support_gid, DAO_GroupSettings::SETTING_SPAM_THRESHOLD, '85');
     // Sales Group
     $sales_gid = DAO_Group::createTeam(array(DAO_Group::TEAM_NAME => 'Sales'));
     // Sales Spam Bucket
     $sales_spam_bid = DAO_Bucket::create('Spam', $sales_gid);
     DAO_GroupSettings::set($sales_gid, DAO_GroupSettings::SETTING_SPAM_ACTION, '2');
     DAO_GroupSettings::set($sales_gid, DAO_GroupSettings::SETTING_SPAM_ACTION_PARAM, $sales_spam_bid);
     DAO_GroupSettings::set($sales_gid, DAO_GroupSettings::SETTING_SPAM_THRESHOLD, '85');
     // Default catchall
     DAO_Group::updateTeam($dispatch_gid, array(DAO_Group::IS_DEFAULT => 1));
 }
 // If this worker doesn't exist, create them
 if (null === ($lookup = DAO_Worker::lookupAgentEmail($worker_email))) {
     $worker_id = DAO_Worker::create($worker_email, $worker_pass, 'Super', 'User', 'Administrator');
     // Superuser bit
     $fields = array(DAO_Worker::IS_SUPERUSER => 1);
     DAO_Worker::updateAgent($worker_id, $fields);
     // Add the worker e-mail to the addresses table