Fix for pubpid assignment by CMS Portal. Cosmetic fix for procedure_order.control_id...
[openemr.git] / ccr / transmitCCD.php
blob0891260da7ea6012cab695a1be228cd23fded16e
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");
30 require_once(dirname(__FILE__) . "/../library/direct_message_check.inc");
33 * Connect to a phiMail Direct Messaging server and transmit
34 * a CCD document to the specified recipient. If the message is accepted by the
35 * server, the script will return "SUCCESS", otherwise it will return an error msg.
36 * @param DOMDocument ccd the xml data to transmit, a CCDA document is assumed
37 * @param string recipient the Direct Address of the recipient
38 * @param string requested_by user | patient
39 * @return string result of operation
42 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) return("$config_err 1");
62 $fp = phimail_connect($err);
63 if ($fp===false) return("$config_err $err");
65 $phimail_username = $GLOBALS['phimail_username'];
66 $phimail_password = $GLOBALS['phimail_password'];
67 $ret = phimail_write_expect_OK($fp,"AUTH $phimail_username $phimail_password\n");
68 if($ret!==TRUE) return("$config_err 4");
70 $ret = phimail_write_expect_OK($fp,"TO $recipient\n");
71 if($ret!==TRUE) return( xl("Delivery is not allowed 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") .
77 ($patientName2=="" ? "." : ", " . $patientName2 . ".");
78 else
79 $text_out = xl("A clinical document is attached") .
80 ($patientName2=="" ? "." : " " . xl("for patient") . " " . $patientName2 . ".");
82 $text_len=strlen($text_out);
83 phimail_write($fp,"TEXT $text_len\n");
84 $ret=@fgets($fp,256);
85 if($ret!="BEGIN\n") {
86 phimail_close($fp);
87 return("$config_err 5");
89 $ret=phimail_write_expect_OK($fp,$text_out);
90 if($ret!==TRUE) return("$config_err 6");
92 $ccd_out=$ccd->saveXml();
93 $ccd_len=strlen($ccd_out);
95 phimail_write($fp,"ADD " . ($xml_type=="CCR" ? "CCR " : "CDA ") . $ccd_len . $att_filename . "\n");
96 $ret=fgets($fp,256);
97 if($ret!="BEGIN\n") {
98 phimail_close($fp);
99 return("$config_err 7");
101 $ret=phimail_write_expect_OK($fp,$ccd_out);
102 if($ret!==TRUE) return("$config_err 8");
104 phimail_write($fp,"SEND\n");
105 $ret=fgets($fp,256);
106 phimail_close($fp);
108 if($requested_by=="patient") {
109 $reqBy="portal-user";
110 $sql = "SELECT id FROM users WHERE username='portal-user'";
111 if (($r = sqlStatementNoLog($sql)) === FALSE ||
112 ($u = sqlFetchArray($r)) === FALSE) {
113 $reqID = 1; //default if we don't have a service user
114 } else {
115 $reqID = $u['id'];
118 } else {
119 $reqBy=$_SESSION['authUser'];
120 $reqID=$_SESSION['authUserID'];
123 if(substr($ret,5)=="ERROR") {
124 //log the failure
125 newEvent("transmit-ccd",$reqBy,$_SESSION['authProvider'],0,$ret,$pid);
126 return( xl("The message could not be sent at this time."));
130 * If we get here, the message was successfully sent and the return
131 * value $ret is of the form "QUEUED recipient message-id" which
132 * is suitable for logging.
134 $msg_id=explode(" ",trim($ret),4);
135 if($msg_id[0]!="QUEUED" || !isset($msg_id[2])) { //unexpected response
136 $ret = "UNEXPECTED RESPONSE: " . $ret;
137 newEvent("transmit-ccd",$reqBy,$_SESSION['authProvider'],0,$ret,$pid);
138 return( xl("There was a problem sending the message."));
140 newEvent("transmit-".$xml_type,$reqBy,$_SESSION['authProvider'],1,$ret,$pid);
141 $adodb=$GLOBALS['adodb']['db'];
142 $sql="INSERT INTO direct_message_log (msg_type,msg_id,sender,recipient,status,status_ts,patient_id,user_id) " .
143 "VALUES ('S', ?, ?, ?, 'S', NOW(), ?, ?)";
144 $res=@sqlStatementNoLog($sql,array($msg_id[2],$phimail_username,$recipient,$pid,$reqID));
146 return("SUCCESS");