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>;.
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");
31 * Connect to a phiMail Direct Messaging server and transmit
32 * a CCD document to the specified recipient. If the message is accepted by the
33 * server, the script will return "SUCCESS", otherwise it will return an error msg.
34 * @param DOMDocument ccd the xml data to transmit, a CCDA document is assumed
35 * @param string recipient the Direct Address of the recipient
36 * @param string requested_by user | patient
37 * @return string result of operation
40 function transmitCCD($ccd,$recipient,$requested_by) {
43 $config_err = xl("Direct messaging is currently unavailable.")." EC:";
44 if ($GLOBALS['phimail_enable']==false) return("$config_err 1");
45 $phimail_server=@parse_url
($GLOBALS['phimail_server_address']);
46 $phimail_username=$GLOBALS['phimail_username'];
47 $phimail_password=$GLOBALS['phimail_password'];
48 switch ($phimail_server['scheme']) {
49 case "http": $server="tcp://".$phimail_server['host'];
51 case "https": $server="ssl://".$phimail_server['host'];
53 default: return("$config_err 2");
55 $fp=@fsockopen
($server,$phimail_server['port']);
56 if ($fp===false) return("$config_err 3");
57 @fwrite
($fp,"AUTH $phimail_username $phimail_password\n");
63 return("$config_err 4");
65 fwrite($fp,"TO $recipient\n");
71 return( xl("Delivery is not currently permitted to the specified Direct Address.") );
73 $ret=fgets($fp,1024); //ignore extra server data
75 if($requested_by=="patient")
76 $text_out = xl("Delivery of the attached clinical document was requested by the patient.");
78 $text_out = xl("A clinical document is attached.");
80 $text_len=strlen($text_out);
81 fwrite($fp,"TEXT $text_len\n");
87 return("$config_err 5");
89 fwrite($fp,$text_out);
95 return("$config_err 6");
98 $ccd_out=$ccd->saveXml();
99 $ccd_len=strlen($ccd_out);
101 fwrite($fp,"CDA $ccd_len\n");
104 if($ret!="BEGIN\n") {
107 return("$config_err 7");
109 fwrite($fp,$ccd_out);
115 return("$config_err 8");
117 fwrite($fp,"SEND\n");
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
134 $reqBy=$_SESSION['authUser'];
135 $reqID=$_SESSION['authUserID'];
138 if(substr($ret,5)=="ERROR") {
140 newEvent("transmit-ccd",$reqBy,$_SESSION['authProvider'],0,$ret,$pid);
141 return( xl("The message could not be sent at this time."));
145 * If we get here, the message was successfully sent and the return
146 * value $ret is of the form "QUEUED recipient message-id" which
147 * is suitable for logging.
149 $msg_id=explode(" ",trim($ret),4);
150 if($msg_id[0]!="QUEUED" ||
!isset($msg_id[2])) { //unexpected response
151 $ret = "UNEXPECTED RESPONSE: " . $ret;
152 newEvent("transmit-ccd",$reqBy,$_SESSION['authProvider'],0,$ret,$pid);
153 return( xl("There was a problem sending the message."));
155 newEvent("transmit-ccd",$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));