Ejemplo n.º 1
0
 public function phimail_close($fp)
 {
     phimail_close($fp);
 }
Ejemplo n.º 2
0
 function transmitCCD($data = array())
 {
     $ccd = $data['ccd'];
     $recipient = $data['recipient'];
     $requested_by = $data['requested_by'];
     $xml_type = $data['xml_type'];
     if (UserService::valid($data[0]) == 'existingpatient') {
         try {
             $_SESSION['authProvider'] = 1;
             global $pid;
             //get patient name in Last_First format (used for CCDA filename) and
             //First Last for the message text.
             $patientData = getPatientPID(array("pid" => $pid));
             if (empty($patientData[0]['lname'])) {
                 $att_filename = "";
                 $patientName2 = "";
             } else {
                 //spaces are the argument delimiter for the phiMail API calls and must be removed
                 $extension = $xml_type == 'CCDA' ? 'xml' : strtolower($xml_type);
                 $att_filename = " " . str_replace(" ", "_", $xml_type . "_" . $patientData[0]['lname'] . "_" . $patientData[0]['fname']) . "." . $extension;
                 $patientName2 = $patientData[0]['fname'] . " " . $patientData[0]['lname'];
             }
             $config_err = xl("Direct messaging is currently unavailable.") . " EC:";
             if ($GLOBALS['phimail_enable'] == false) {
                 return "{$config_err} 1";
             }
             $fp = phimail_connect($err);
             if ($fp === false) {
                 return "{$config_err} {$err}";
             }
             $phimail_username = $GLOBALS['phimail_username'];
             $phimail_password = $GLOBALS['phimail_password'];
             $ret = phimail_write_expect_OK($fp, "AUTH {$phimail_username} {$phimail_password}\n");
             if ($ret !== TRUE) {
                 return "{$config_err} 4";
             }
             $ret = phimail_write_expect_OK($fp, "TO {$recipient}\n");
             if ($ret !== TRUE) {
                 return xl("Delivery is not allowed to the specified Direct Address.");
             }
             $ret = fgets($fp, 1024);
             //ignore extra server data
             if ($requested_by == "patient") {
                 $text_out = xl("Delivery of the attached clinical document was requested by the patient") . ($patientName2 == "" ? "." : ", " . $patientName2 . ".");
             } else {
                 $text_out = xl("A clinical document is attached") . ($patientName2 == "" ? "." : " " . xl("for patient") . " " . $patientName2 . ".");
             }
             $text_len = strlen($text_out);
             phimail_write($fp, "TEXT {$text_len}\n");
             $ret = @fgets($fp, 256);
             if ($ret != "BEGIN\n") {
                 phimail_close($fp);
                 return "{$config_err} 5";
             }
             $ret = phimail_write_expect_OK($fp, $text_out);
             if ($ret !== TRUE) {
                 return "{$config_err} 6";
             }
             if (in_array($xml_type, array('CCR', 'CCDA', 'CDA'))) {
                 $ccd = simplexml_load_string($ccd);
                 $ccd_out = $ccd->saveXml();
                 $ccd_len = strlen($ccd_out);
                 phimail_write($fp, "ADD " . ($xml_type == "CCR" ? $xml_type . ' ' : "CDA ") . $ccd_len . $att_filename . "\n");
                 //phimail_write($fp,"ADD " . (isset($xml_type) ? $xml_type . ' ' : "CDA ") . $ccd_len . $att_filename . "\n");
             } else {
                 if (strtolower($xml_type) == 'html' || strtolower($xml_type) == 'pdf') {
                     $ccd_out = base64_decode($ccd);
                     $message_length = strlen($ccd_out);
                     $add_type = strtolower($xml_type) == 'html' ? 'TEXT' : 'RAW';
                     phimail_write($fp, "ADD " . $add_type . " " . $message_length . "" . $att_filename . "\n");
                 }
             }
             $ret = fgets($fp, 256);
             if ($ret != "BEGIN\n") {
                 phimail_close($fp);
                 return "{$config_err} 7";
             }
             $ret = phimail_write_expect_OK($fp, $ccd_out);
             if ($ret !== TRUE) {
                 return "{$config_err} 8";
             }
             phimail_write($fp, "SEND\n");
             $ret = fgets($fp, 256);
             phimail_close($fp);
             if ($requested_by == "patient") {
                 $reqBy = "portal-user";
                 $sql = "SELECT id FROM users WHERE username='******'";
                 if (($r = sqlStatement($sql)) === FALSE || ($u = sqlFetchArray($r)) === FALSE) {
                     $reqID = 1;
                     //default if we don't have a service user
                 } else {
                     $reqID = $u['id'];
                 }
             } else {
                 $reqBy = $_SESSION['authUser'];
                 $reqID = $_SESSION['authUserID'];
             }
             if (substr($ret, 5) == "ERROR") {
                 //log the failure
                 newEvent("transmit-ccd", $reqBy, $_SESSION['authProvider'], 0, $ret, $pid);
                 return xl("The message could not be sent at this time.");
             }
             /**
              * If we get here, the message was successfully sent and the return
              * value $ret is of the form "QUEUED recipient message-id" which
              * is suitable for logging. 
              */
             $msg_id = explode(" ", trim($ret), 4);
             if ($msg_id[0] != "QUEUED" || !isset($msg_id[2])) {
                 //unexpected response
                 $ret = "UNEXPECTED RESPONSE: " . $ret;
                 newEvent("transmit-ccd", $reqBy, $_SESSION['authProvider'], 0, $ret, $pid);
                 return xl("There was a problem sending the message.");
             }
             newEvent("transmit-" . $xml_type, $reqBy, $_SESSION['authProvider'], 1, $ret, $pid);
             $adodb = $GLOBALS['adodb']['db'];
             //            $sql="INSERT INTO direct_message_log (msg_type,msg_id,sender,recipient,status,status_ts,patient_id,user_id) " .
             //             "VALUES ('S', ?, ?, ?, 'S', NOW(), ?, ?)";
             //            $res=@sqlStatement($sql,array($msg_id[2],$phimail_username,$recipient,$pid,$reqID));
             return "SUCCESS";
         } catch (Exception $e) {
             return 'Error: ' . $e->getMessage();
         }
     }
 }