Direct Messaging update: add CCR transmit, more meaningful attachment filenames
[openemr.git] / ccr / transmitCCD.php
blobfae7847035263e4155bd30d6a396240dbddc69b3
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/sql.inc");
29 require_once(dirname(__FILE__) . "/../library/patient.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") {
42 global $pid;
44 //get patient name in Last_First format (used for CCDA filename) and
45 //First Last for the message text.
46 $patientData = getPatientPID(array("pid"=>$pid));
47 if (empty($patientData[0]['lname'])) {
48 $att_filename = "";
49 $patientName2 = "";
50 } else {
51 //spaces are the argument delimiter for the phiMail API calls and must be removed
52 $att_filename = " " .
53 str_replace(" ", "_", $xml_type . "_" . $patientData[0]['lname']
54 . "_" . $patientData[0]['fname']) . ".xml";
55 $patientName2 = $patientData[0]['fname'] . " " . $patientData[0]['lname'];
58 $config_err = xl("Direct messaging is currently unavailable.")." EC:";
59 if ($GLOBALS['phimail_enable']==false) return("$config_err 1");
60 $phimail_server=@parse_url($GLOBALS['phimail_server_address']);
61 $phimail_username=$GLOBALS['phimail_username'];
62 $phimail_password=$GLOBALS['phimail_password'];
63 switch ($phimail_server['scheme']) {
64 case "http": $server="tcp://".$phimail_server['host'];
65 break;
66 case "https": $server="ssl://".$phimail_server['host'];
67 break;
68 default: return("$config_err 2");
70 $fp=@fsockopen($server,$phimail_server['port']);
71 if ($fp===false) return("$config_err 3");
72 @fwrite($fp,"AUTH $phimail_username $phimail_password\n");
73 fflush($fp);
74 $ret=fgets($fp,256);
75 if($ret!="OK\n") {
76 fwrite($fp,"BYE\n");
77 fclose($fp);
78 return("$config_err 4");
80 fwrite($fp,"TO $recipient\n");
81 fflush($fp);
82 $ret=fgets($fp,256);
83 if($ret!="OK\n") {
84 fwrite($fp,"BYE\n");
85 fclose($fp);
86 return( xl("Delivery is not currently permitted to the specified Direct Address.") );
88 $ret=fgets($fp,1024); //ignore extra server data
90 if($requested_by=="patient")
91 $text_out = xl("Delivery of the attached clinical document was requested by the patient") .
92 ($patientName2=="" ? "." : ", " . $patientName2 . ".");
93 else
94 $text_out = xl("A clinical document is attached") .
95 ($patientName2=="" ? "." : " " . xl("for patient") . " " . $patientName2 . ".");
97 $text_len=strlen($text_out);
98 fwrite($fp,"TEXT $text_len\n");
99 fflush($fp);
100 $ret=@fgets($fp,256);
101 if($ret!="BEGIN\n") {
102 fwrite($fp,"BYE\n");
103 fclose($fp);
104 return("$config_err 5");
106 fwrite($fp,$text_out);
107 fflush($fp);
108 $ret=@fgets($fp,256);
109 if($ret!="OK\n") {
110 fwrite($fp,"BYE\n");
111 fclose($fp);
112 return("$config_err 6");
115 $ccd_out=$ccd->saveXml();
116 $ccd_len=strlen($ccd_out);
118 fwrite($fp,"ADD " . ($xml_type=="CCR" ? "CCR " : "CDA ") . $ccd_len . $att_filename . "\n");
119 fflush($fp);
120 $ret=fgets($fp,256);
121 if($ret!="BEGIN\n") {
122 fwrite($fp,"BYE\n");
123 fclose($fp);
124 return("$config_err 7");
126 fwrite($fp,$ccd_out);
127 fflush($fp);
128 $ret=fgets($fp,256);
129 if($ret!="OK\n") {
130 fwrite($fp,"BYE\n");
131 fclose($fp);
132 return("$config_err 8");
135 fwrite($fp,"SEND\n");
136 fflush($fp);
137 $ret=fgets($fp,256);
138 fwrite($fp,"BYE\n");
139 fclose($fp);
141 if($requested_by=="patient") {
142 $reqBy="portal-user";
143 $sql = "SELECT id FROM users WHERE username='portal-user'";
144 if (($r = sqlStatementNoLog($sql)) === FALSE ||
145 ($u = sqlFetchArray($r)) === FALSE) {
146 $reqID = 1; //default if we don't have a service user
147 } else {
148 $reqID = $u['id'];
151 } else {
152 $reqBy=$_SESSION['authUser'];
153 $reqID=$_SESSION['authUserID'];
156 if(substr($ret,5)=="ERROR") {
157 //log the failure
158 newEvent("transmit-ccd",$reqBy,$_SESSION['authProvider'],0,$ret,$pid);
159 return( xl("The message could not be sent at this time."));
163 * If we get here, the message was successfully sent and the return
164 * value $ret is of the form "QUEUED recipient message-id" which
165 * is suitable for logging.
167 $msg_id=explode(" ",trim($ret),4);
168 if($msg_id[0]!="QUEUED" || !isset($msg_id[2])) { //unexpected response
169 $ret = "UNEXPECTED RESPONSE: " . $ret;
170 newEvent("transmit-ccd",$reqBy,$_SESSION['authProvider'],0,$ret,$pid);
171 return( xl("There was a problem sending the message."));
173 newEvent("transmit-".$xml_type,$reqBy,$_SESSION['authProvider'],1,$ret,$pid);
174 $adodb=$GLOBALS['adodb']['db'];
175 $sql="INSERT INTO direct_message_log (msg_type,msg_id,sender,recipient,status,status_ts,patient_id,user_id) " .
176 "VALUES ('S', ?, ?, ?, 'S', NOW(), ?, ?)";
177 $res=@sqlStatementNoLog($sql,array($msg_id[2],$phimail_username,$recipient,$pid,$reqID));
179 return("SUCCESS");