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