Merge pull request #899 from stephenwaite/admin_fix
[openemr.git] / ccr / transmitCCD.php
blob3439dccc8a68c6e91622884b8254b1c6167fe965
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") {
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");
61 $fp = phimail_connect($err);
62 if ($fp===false) return("$config_err $err");
64 $phimail_username = $GLOBALS['phimail_username'];
65 $phimail_password = $GLOBALS['phimail_password'];
66 $ret = phimail_write_expect_OK($fp,"AUTH $phimail_username $phimail_password\n");
67 if($ret!==TRUE) return("$config_err 4");
69 $ret = phimail_write_expect_OK($fp,"TO $recipient\n");
70 if($ret!==TRUE) return( xl("Delivery is not allowed to the specified Direct Address.") );
72 $ret=fgets($fp,1024); //ignore extra server data
74 if($requested_by=="patient")
75 $text_out = xl("Delivery of the attached clinical document was requested by the patient") .
76 ($patientName2=="" ? "." : ", " . $patientName2 . ".");
77 else
78 $text_out = xl("A clinical document is attached") .
79 ($patientName2=="" ? "." : " " . xl("for patient") . " " . $patientName2 . ".");
81 $text_len=strlen($text_out);
82 phimail_write($fp,"TEXT $text_len\n");
83 $ret=@fgets($fp,256);
84 if($ret!="BEGIN\n") {
85 phimail_close($fp);
86 return("$config_err 5");
88 $ret=phimail_write_expect_OK($fp,$text_out);
89 if($ret!==TRUE) return("$config_err 6");
91 $ccd_out=$ccd->saveXml();
92 $ccd_len=strlen($ccd_out);
94 phimail_write($fp,"ADD " . ($xml_type=="CCR" ? "CCR " : "CDA ") . $ccd_len . $att_filename . "\n");
95 $ret=fgets($fp,256);
96 if($ret!="BEGIN\n") {
97 phimail_close($fp);
98 return("$config_err 7");
100 $ret=phimail_write_expect_OK($fp,$ccd_out);
101 if($ret!==TRUE) return("$config_err 8");
103 phimail_write($fp,"SEND\n");
104 $ret=fgets($fp,256);
105 phimail_close($fp);
107 if($requested_by=="patient") {
108 $reqBy="portal-user";
109 $sql = "SELECT id FROM users WHERE username='portal-user'";
110 if (($r = sqlStatementNoLog($sql)) === FALSE ||
111 ($u = sqlFetchArray($r)) === FALSE) {
112 $reqID = 1; //default if we don't have a service user
113 } else {
114 $reqID = $u['id'];
117 } else {
118 $reqBy=$_SESSION['authUser'];
119 $reqID=$_SESSION['authUserID'];
122 if(substr($ret,5)=="ERROR") {
123 //log the failure
124 newEvent("transmit-ccd",$reqBy,$_SESSION['authProvider'],0,$ret,$pid);
125 return( xl("The message could not be sent at this time."));
129 * If we get here, the message was successfully sent and the return
130 * value $ret is of the form "QUEUED recipient message-id" which
131 * is suitable for logging.
133 $msg_id=explode(" ",trim($ret),4);
134 if($msg_id[0]!="QUEUED" || !isset($msg_id[2])) { //unexpected response
135 $ret = "UNEXPECTED RESPONSE: " . $ret;
136 newEvent("transmit-ccd",$reqBy,$_SESSION['authProvider'],0,$ret,$pid);
137 return( xl("There was a problem sending the message."));
139 newEvent("transmit-".$xml_type,$reqBy,$_SESSION['authProvider'],1,$ret,$pid);
140 $adodb=$GLOBALS['adodb']['db'];
141 $sql="INSERT INTO direct_message_log (msg_type,msg_id,sender,recipient,status,status_ts,patient_id,user_id) " .
142 "VALUES ('S', ?, ?, ?, 'S', NOW(), ?, ?)";
143 $res=@sqlStatementNoLog($sql,array($msg_id[2],$phimail_username,$recipient,$pid,$reqID));
145 return("SUCCESS");