Percent-based price levels (#2577)
[openemr.git] / ccr / transmitCCD.php
blob82bbc00541ff8cac2c66f468fec4a8609f50d2e4
1 <?php
2 /**
3 * Functions to transmit a CCD as a Direct Protocol Message
5 * Copyright (C) 2013 EMR Direct <http://www.emrdirect.com/>
7 * Use of these functions requires an active phiMail Direct messaging
8 * account with EMR Direct. For information regarding this service,
9 * please visit http://www.emrdirect.com or email support@emrdirect.com
11 * LICENSE: This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 3
14 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
22 * @package OpenEMR
23 * @author EMR Direct <http://www.emrdirect.com/>
24 * @link http://www.open-emr.org
27 require_once(dirname(__FILE__) . "/../library/patient.inc");
28 require_once(dirname(__FILE__) . "/../library/direct_message_check.inc");
30 use OpenEMR\Common\Crypto\CryptoGen;
31 use OpenEMR\Common\Logging\EventAuditLogger;
34 * Connect to a phiMail Direct Messaging server and transmit
35 * a CCD document to the specified recipient. If the message is accepted by the
36 * server, the script will return "SUCCESS", otherwise it will return an error msg.
37 * @param DOMDocument ccd the xml data to transmit, a CCDA document is assumed
38 * @param string recipient the Direct Address of the recipient
39 * @param string requested_by user | patient
40 * @return string result of operation
42 function transmitCCD($ccd, $recipient, $requested_by, $xml_type = "CCD")
44 global $pid;
46 //get patient name in Last_First format (used for CCDA filename) and
47 //First Last for the message text.
48 $patientData = getPatientPID(array("pid"=>$pid));
49 if (empty($patientData[0]['lname'])) {
50 $att_filename = "";
51 $patientName2 = "";
52 } else {
53 //spaces are the argument delimiter for the phiMail API calls and must be removed
54 $att_filename = " " .
55 str_replace(" ", "_", $xml_type . "_" . $patientData[0]['lname']
56 . "_" . $patientData[0]['fname']) . ".xml";
57 $patientName2 = $patientData[0]['fname'] . " " . $patientData[0]['lname'];
60 $config_err = xl("Direct messaging is currently unavailable.")." EC:";
61 if ($GLOBALS['phimail_enable']==false) {
62 return("$config_err 1");
65 $fp = phimail_connect($err);
66 if ($fp===false) {
67 return("$config_err $err");
70 $phimail_username = $GLOBALS['phimail_username'];
71 $cryptoGen = new CryptoGen();
72 $phimail_password = $cryptoGen->decryptStandard($GLOBALS['phimail_password']);
73 $ret = phimail_write_expect_OK($fp, "AUTH $phimail_username $phimail_password\n");
74 if ($ret!==true) {
75 return("$config_err 4");
78 $ret = phimail_write_expect_OK($fp, "TO $recipient\n");
79 if ($ret!==true) {
80 return( xl("Delivery is not allowed to the specified Direct Address.") );
83 $ret=fgets($fp, 1024); //ignore extra server data
85 if ($requested_by=="patient") {
86 $text_out = xl("Delivery of the attached clinical document was requested by the patient") .
87 ($patientName2=="" ? "." : ", " . $patientName2 . ".");
88 } else {
89 $text_out = xl("A clinical document is attached") .
90 ($patientName2=="" ? "." : " " . xl("for patient") . " " . $patientName2 . ".");
93 $text_len=strlen($text_out);
94 phimail_write($fp, "TEXT $text_len\n");
95 $ret=@fgets($fp, 256);
96 if ($ret!="BEGIN\n") {
97 phimail_close($fp);
98 return("$config_err 5");
101 $ret=phimail_write_expect_OK($fp, $text_out);
102 if ($ret!==true) {
103 return("$config_err 6");
106 $ccd_out=$ccd->saveXml();
107 $ccd_len=strlen($ccd_out);
109 phimail_write($fp, "ADD " . ($xml_type=="CCR" ? "CCR " : "CDA ") . $ccd_len . $att_filename . "\n");
110 $ret=fgets($fp, 256);
111 if ($ret!="BEGIN\n") {
112 phimail_close($fp);
113 return("$config_err 7");
116 $ret=phimail_write_expect_OK($fp, $ccd_out);
117 if ($ret!==true) {
118 return("$config_err 8");
121 phimail_write($fp, "SEND\n");
122 $ret=fgets($fp, 256);
123 phimail_close($fp);
125 if ($requested_by=="patient") {
126 $reqBy="portal-user";
127 $sql = "SELECT id FROM users WHERE username='portal-user'";
128 if (($r = sqlStatementNoLog($sql)) === false ||
129 ($u = sqlFetchArray($r)) === false) {
130 $reqID = 1; //default if we don't have a service user
131 } else {
132 $reqID = $u['id'];
134 } else {
135 $reqBy=$_SESSION['authUser'];
136 $reqID=$_SESSION['authUserID'];
139 if (substr($ret, 5)=="ERROR") {
140 //log the failure
142 EventAuditLogger::instance()->newEvent("transmit-ccd", $reqBy, $_SESSION['authProvider'], 0, $ret, $pid);
143 return( xl("The message could not be sent at this time."));
147 * If we get here, the message was successfully sent and the return
148 * value $ret is of the form "QUEUED recipient message-id" which
149 * is suitable for logging.
151 $msg_id=explode(" ", trim($ret), 4);
152 if ($msg_id[0]!="QUEUED" || !isset($msg_id[2])) { //unexpected response
153 $ret = "UNEXPECTED RESPONSE: " . $ret;
154 EventAuditLogger::instance()->newEvent("transmit-ccd", $reqBy, $_SESSION['authProvider'], 0, $ret, $pid);
155 return( xl("There was a problem sending the message."));
158 EventAuditLogger::instance()->newEvent("transmit-".$xml_type, $reqBy, $_SESSION['authProvider'], 1, $ret, $pid);
159 $adodb=$GLOBALS['adodb']['db'];
160 $sql="INSERT INTO direct_message_log (msg_type,msg_id,sender,recipient,status,status_ts,patient_id,user_id) " .
161 "VALUES ('S', ?, ?, ?, 'S', NOW(), ?, ?)";
162 $res=@sqlStatementNoLog($sql, array($msg_id[2],$phimail_username,$recipient,$pid,$reqID));
164 return("SUCCESS");