Restyled Form Vitals (#1714)
[openemr.git] / ccr / transmitCCD.php
blobc8a0aa3d03a0c719b3bddc026d23c8cf3411f36a
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/log.inc");
28 require_once(dirname(__FILE__) . "/../library/patient.inc");
29 require_once(dirname(__FILE__) . "/../library/direct_message_check.inc");
32 * Connect to a phiMail Direct Messaging server and transmit
33 * a CCD document to the specified recipient. If the message is accepted by the
34 * server, the script will return "SUCCESS", otherwise it will return an error msg.
35 * @param DOMDocument ccd the xml data to transmit, a CCDA document is assumed
36 * @param string recipient the Direct Address of the recipient
37 * @param string requested_by user | patient
38 * @return string result of operation
41 function transmitCCD($ccd, $recipient, $requested_by, $xml_type = "CCD")
43 global $pid;
45 //get patient name in Last_First format (used for CCDA filename) and
46 //First Last for the message text.
47 $patientData = getPatientPID(array("pid"=>$pid));
48 if (empty($patientData[0]['lname'])) {
49 $att_filename = "";
50 $patientName2 = "";
51 } else {
52 //spaces are the argument delimiter for the phiMail API calls and must be removed
53 $att_filename = " " .
54 str_replace(" ", "_", $xml_type . "_" . $patientData[0]['lname']
55 . "_" . $patientData[0]['fname']) . ".xml";
56 $patientName2 = $patientData[0]['fname'] . " " . $patientData[0]['lname'];
59 $config_err = xl("Direct messaging is currently unavailable.")." EC:";
60 if ($GLOBALS['phimail_enable']==false) {
61 return("$config_err 1");
64 $fp = phimail_connect($err);
65 if ($fp===false) {
66 return("$config_err $err");
69 $phimail_username = $GLOBALS['phimail_username'];
70 $phimail_password = $GLOBALS['phimail_password'];
71 $ret = phimail_write_expect_OK($fp, "AUTH $phimail_username $phimail_password\n");
72 if ($ret!==true) {
73 return("$config_err 4");
76 $ret = phimail_write_expect_OK($fp, "TO $recipient\n");
77 if ($ret!==true) {
78 return( xl("Delivery is not allowed to the specified Direct Address.") );
81 $ret=fgets($fp, 1024); //ignore extra server data
83 if ($requested_by=="patient") {
84 $text_out = xl("Delivery of the attached clinical document was requested by the patient") .
85 ($patientName2=="" ? "." : ", " . $patientName2 . ".");
86 } else {
87 $text_out = xl("A clinical document is attached") .
88 ($patientName2=="" ? "." : " " . xl("for patient") . " " . $patientName2 . ".");
91 $text_len=strlen($text_out);
92 phimail_write($fp, "TEXT $text_len\n");
93 $ret=@fgets($fp, 256);
94 if ($ret!="BEGIN\n") {
95 phimail_close($fp);
96 return("$config_err 5");
99 $ret=phimail_write_expect_OK($fp, $text_out);
100 if ($ret!==true) {
101 return("$config_err 6");
104 $ccd_out=$ccd->saveXml();
105 $ccd_len=strlen($ccd_out);
107 phimail_write($fp, "ADD " . ($xml_type=="CCR" ? "CCR " : "CDA ") . $ccd_len . $att_filename . "\n");
108 $ret=fgets($fp, 256);
109 if ($ret!="BEGIN\n") {
110 phimail_close($fp);
111 return("$config_err 7");
114 $ret=phimail_write_expect_OK($fp, $ccd_out);
115 if ($ret!==true) {
116 return("$config_err 8");
119 phimail_write($fp, "SEND\n");
120 $ret=fgets($fp, 256);
121 phimail_close($fp);
123 if ($requested_by=="patient") {
124 $reqBy="portal-user";
125 $sql = "SELECT id FROM users WHERE username='portal-user'";
126 if (($r = sqlStatementNoLog($sql)) === false ||
127 ($u = sqlFetchArray($r)) === false) {
128 $reqID = 1; //default if we don't have a service user
129 } else {
130 $reqID = $u['id'];
132 } else {
133 $reqBy=$_SESSION['authUser'];
134 $reqID=$_SESSION['authUserID'];
137 if (substr($ret, 5)=="ERROR") {
138 //log the failure
139 newEvent("transmit-ccd", $reqBy, $_SESSION['authProvider'], 0, $ret, $pid);
140 return( xl("The message could not be sent at this time."));
144 * If we get here, the message was successfully sent and the return
145 * value $ret is of the form "QUEUED recipient message-id" which
146 * is suitable for logging.
148 $msg_id=explode(" ", trim($ret), 4);
149 if ($msg_id[0]!="QUEUED" || !isset($msg_id[2])) { //unexpected response
150 $ret = "UNEXPECTED RESPONSE: " . $ret;
151 newEvent("transmit-ccd", $reqBy, $_SESSION['authProvider'], 0, $ret, $pid);
152 return( xl("There was a problem sending the message."));
155 newEvent("transmit-".$xml_type, $reqBy, $_SESSION['authProvider'], 1, $ret, $pid);
156 $adodb=$GLOBALS['adodb']['db'];
157 $sql="INSERT INTO direct_message_log (msg_type,msg_id,sender,recipient,status,status_ts,patient_id,user_id) " .
158 "VALUES ('S', ?, ?, ?, 'S', NOW(), ?, ?)";
159 $res=@sqlStatementNoLog($sql, array($msg_id[2],$phimail_username,$recipient,$pid,$reqID));
161 return("SUCCESS");