示例#1
0
 protected function _createAudit($providerId, $personId, $visitId, $type)
 {
     $providerId = (int) $providerId;
     $personId = (int) $personId;
     $visitId = (int) $visitId;
     $audit = array();
     $audit['objectClass'] = 'GenericAccessAudit';
     $audit['objectId'] = $personId . ';' . $visitId;
     $audit['type'] = (int) $type;
     $audit['userId'] = $providerId;
     $audit['patientId'] = $personId;
     $values = array();
     $provider = new Provider();
     $provider->personId = $audit['userId'];
     $provider->populate();
     $values['provider'] = $provider->toArray();
     $patient = new Patient();
     $patient->personId = $personId;
     $patient->populate();
     $values['patient'] = $patient->toArray();
     $values['personId'] = $patient->personId;
     $visit = new Visit();
     $visit->visitId = $visitId;
     $visit->populate();
     $values['visit'] = $visit->toArray();
     $values['visitId'] = $visit->visitId;
     $audit['auditValues'] = $values;
     Audit::persistManualAuditArray($audit);
 }
 public function persist()
 {
     $db = Zend_Registry::get('dbAdapter');
     if ($this->_persistMode == WebVista_Model_ORM::DELETE) {
         return parent::persist();
     }
     $data = $this->toArray();
     if (!strlen($data['uuid']) > 0) {
         $this->uuid = NSDR::create_guid();
         $data['uuid'] = $this->uuid;
         $db->insert($this->_table, $data);
     } else {
         $db->update($this->_table, $data, 'uuid = ' . $db->quote($data['uuid']));
     }
     if ($this->shouldAudit()) {
         $audit = array();
         $audit['objectClass'] = get_class($this);
         $audit['objectId'] = $data['uuid'];
         $audit['auditValues'] = $data;
         Audit::persistManualAuditArray($audit);
     }
     return $this;
 }
 protected function _dispatch()
 {
     file_put_contents('/tmp/smtp.log', "\ndispatched started for filename: {$this->messageFilename}", FILE_APPEND);
     $contents = file($this->messageFilename);
     file_put_contents('/tmp/smtp.log', "\ncontents: " . print_r($contents, true), FILE_APPEND);
     $boundary = null;
     $boundaryLen = 0;
     $boundaryStarted = false;
     $data = array();
     $tmpData = array();
     $headers = array('subject' => '', 'from' => '', 'to' => '', 'date' => '', 'messageId' => '');
     $contentsCtr = count($contents);
     $subjectStarted = false;
     for ($i = 0; $i < $contentsCtr; $i++) {
         $line = trim($contents[$i]);
         $nextI = $i + 1;
         if (isset($contents[$nextI]) && preg_match('/^Content-(.*):/i', $line) && ($contents[$nextI][0] == "\t" || $contents[$nextI][0] == ' ')) {
             $contents[$nextI][0] = ' ';
             $line .= $contents[$nextI];
             $i = $nextI;
         }
         if (!$boundaryStarted) {
             if ($boundary !== null && '--' . $boundary == substr($line, 0, $boundaryLen + 2)) {
                 $boundaryStarted = true;
                 continue;
             }
             if (preg_match('/^Subject:(.*)/i', $line, $matches)) {
                 $headers['subject'] = ltrim($matches[1]);
                 $subjectStarted = true;
             } else {
                 if (preg_match('/^From:(.*)/i', $line, $matches)) {
                     $headers['from'] = ltrim($matches[1]);
                 } else {
                     if (preg_match('/^To:(.*)/i', $line, $matches)) {
                         $headers['to'] = ltrim($matches[1]);
                     } else {
                         if (preg_match('/^Content-Type:(.*)/i', $line, $matches)) {
                             $contentType = ltrim($matches[1]);
                             if (preg_match('/boundary=(.*)/i', $contentType, $matches)) {
                                 $boundary = $matches[1];
                                 if (preg_match('/^"(.*)"/', $boundary, $matches)) {
                                     // some boundaries are enclosed in ""
                                     $boundary = $matches[1];
                                 }
                                 $boundaryLen = strlen($boundary);
                             }
                         } else {
                             if (preg_match('/^Date:(.*)/i', $line, $matches)) {
                                 $headers['date'] = ltrim($matches[1]);
                             } else {
                                 if (preg_match('/^Message-Id:(.*)/i', $line, $matches)) {
                                     $headers['messageId'] = ltrim($matches[1]);
                                 } else {
                                     if (preg_match('/^X-Mailer:(.*)/i', $line, $matches)) {
                                         $headers['mailer'] = ltrim($matches[1]);
                                     } else {
                                         if ($subjectStarted) {
                                             $tmpData[] = $line;
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         } else {
             if (substr($line, 0, $boundaryLen + 2) == '--' . $boundary) {
                 if (isset($tmpData[0])) {
                     $data[] = $tmpData;
                     $tmpData = array();
                 }
             } else {
                 $tmpData[] = $line;
             }
         }
     }
     if (isset($tmpData[0])) {
         $data[] = $tmpData;
         $tmpData = array();
     }
     $dataContents = array();
     $i = 0;
     foreach ($data as $lines) {
         $content = '';
         $contentTypes = array();
         foreach ($lines as $line) {
             if (preg_match('/^Content-(.*):(.*)/i', $line, $matches)) {
                 $contentTypes['content-' . strtolower($matches[1])] = ltrim($matches[2]);
             } else {
                 if ($line == '') {
                     $lineStart = true;
                     $lineEnd = true;
                 } else {
                     $content .= $line;
                 }
             }
         }
         if (isset($contentTypes['content-disposition']) && (isset($contentTypes['content-transfer-encoding']) && strtolower($contentTypes['content-transfer-encoding']) == 'base64')) {
             // temporarily accept base64 encoding that serves as an attachment
             $tmpData[] = array('contentTypes' => $contentTypes, 'content' => $content);
             continue;
         }
         $dataContents[] = array('contentTypes' => $contentTypes, 'content' => $content);
     }
     if (isset($tmpData[0])) {
         $dataContents = $tmpData;
     }
     $recipients = array();
     $addresses = explode(',', $headers['to']);
     $db = Zend_Registry::get('dbAdapter');
     foreach ($addresses as $address) {
         if (preg_match('/<(.*)>/', $address, $matches)) {
             $address = $matches[1];
         }
         $x = explode('@', $address);
         $username = $x[0];
         $domain = $x[1];
         $sqlSelect = $db->select()->from('patient', 'person_id')->where('record_number = ?', $username);
         $personId = 0;
         if ($row = $db->fetchRow($sqlSelect)) {
             $personId = $row['person_id'];
         }
         $recipients[] = array('personId' => $personId, 'address' => $address, 'username' => $username, 'domain' => $domain);
     }
     file_put_contents('/tmp/smtp.log', "\ndata contents: " . print_r($dataContents, true), FILE_APPEND);
     foreach ($dataContents as $content) {
         $contentTypes = $content['contentTypes'];
         $filename = null;
         // email has an attachments
         if (isset($contentTypes['content-disposition']) && (isset($contentTypes['content-transfer-encoding']) && strtolower($contentTypes['content-transfer-encoding']) == 'base64')) {
             // temporarily accept base64 encoding that serves as an attachment
             $contentType = $contentTypes['content-type'];
             // image/jpeg; name="sample-signature.jpeg"
             $types = explode(';', $contentType);
             $mimeType = array_shift($types);
             foreach ($types as $type) {
                 $type = ltrim($type);
                 if (preg_match('/^name="(.*)"/', $type, $matches)) {
                     $filename = '/tmp/' . $matches[1];
                     break;
                 }
             }
             if ($filename === null) {
                 // try to create a random filename with specific extension based on mime type
                 $extension = MimeType::extension($mimeType);
                 $tmpFile = tempnam('/tmp', 'ch30_attach_');
                 $filename = $tmpFile . '.' . $extension;
                 //rename($tmpFile,$filename);
                 unlink($tmpFile);
             }
             $content['content'] = base64_decode($content['content']);
             // decode the base64 encoded file
         }
         foreach ($recipients as $recipient) {
             $personId = $recipient['personId'];
             $messagingId = WebVista_Model_ORM::nextSequenceId();
             $messaging = array();
             $messaging['note'] = 'Email message: ' . $content['content'];
             $attachmentId = 0;
             if ($filename !== null) {
                 $attachmentId = WebVista_Model_ORM::nextSequenceId();
                 $attachment = array();
                 $attachment['attachmentId'] = $attachmentId;
                 $attachment['attachmentReferenceId'] = md5($headers['messageId']);
                 $attachment['name'] = basename($filename);
                 $attachment['dateTime'] = date('Y-m-d H:i:s', strtotime($headers['date']));
                 $attachment['mimeType'] = $mimeType;
                 $attachment['md5sum'] = md5($content['content']);
                 $db->insert('attachments', $attachment);
                 $audit = array();
                 $audit['objectClass'] = 'Attachment';
                 $audit['objectId'] = $attachment['attachmentId'];
                 $audit['auditValues'] = $attachment;
                 Audit::persistManualAuditArray($audit);
                 $attachmentBlob = array();
                 $attachmentBlob['attachmentId'] = $attachment['attachmentId'];
                 $attachmentBlob['data'] = $content['content'];
                 $db->insert('attachmentBlobs', $attachmentBlob);
                 $messaging['note'] = 'Scanned document for ' . $recipient['address'];
             }
             $messaging['messagingId'] = $messagingId;
             $messaging['objectType'] = Messaging::TYPE_INBOUND_FAX;
             $messaging['status'] = 'Fax/Email Received';
             $messaging['dateStatus'] = date('Y-m-d H:i:s');
             $db->insert('messaging', $messaging);
             $messagingInboundFax = array();
             $messagingInboundFax['messagingId'] = $messaging['messagingId'];
             $messagingInboundFax['personId'] = $personId;
             $messagingInboundFax['mrn'] = $recipient['username'];
             $messagingInboundFax['subject'] = $headers['subject'];
             $messagingInboundFax['from'] = $headers['from'];
             $messagingInboundFax['to'] = $recipient['address'];
             $messagingInboundFax['messageId'] = $headers['messageId'];
             $messagingInboundFax['attachmentId'] = $attachmentId;
             $db->insert('messagingInboundFaxes', $messagingInboundFax);
             $audit = array();
             $audit['objectClass'] = 'MessagingInboundFax';
             $audit['objectId'] = $messagingInboundFax['messagingId'];
             $audit['auditValues'] = $messagingInboundFax;
             Audit::persistManualAuditArray($audit);
         }
     }
     unlink($this->messageFilename);
     return $this;
 }