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");
30 * Connect to a phiMail Direct Messaging server and transmit
31 * a CCD document to the specified recipient. If the message is accepted by the
32 * server, the script will return "SUCCESS", otherwise it will return an error msg.
33 * @param DOMDocument ccd the xml data to transmit, a CCDA document is assumed
34 * @param string recipient the Direct Address of the recipient
35 * @param string requested_by user | patient
36 * @return string result of operation
39 function transmitCCD($ccd,$recipient,$requested_by) {
42 $config_err = xl("Direct messaging is currently unavailable.")." EC:";
43 if ($GLOBALS['phimail_enable']==false) return("$config_err 1");
44 $phimail_server=@parse_url
($GLOBALS['phimail_server_address']);
45 $phimail_username=$GLOBALS['phimail_username'];
46 $phimail_password=$GLOBALS['phimail_password'];
47 switch ($phimail_server['scheme']) {
48 case "http": $server="tcp://".$phimail_server['host'];
50 case "https": $server="ssl://".$phimail_server['host'];
52 default: return("$config_err 2");
54 $fp=@fsockopen
($server,$phimail_server['port']);
55 if ($fp===false) return("$config_err 3");
56 @fwrite
($fp,"AUTH $phimail_username $phimail_password\n");
62 return("$config_err 4");
64 @fwrite
($fp,"TO $recipient\n");
70 return( xl("Delivery is not currently permitted 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.");
77 $text_out = xl("A clinical document is attached.");
79 $text_len=strlen($text_out);
80 @fwrite
($fp,"TEXT $text_len\n");
86 return("$config_err 5");
88 @fwrite
($fp,$text_out);
94 return("$config_err 6");
97 $ccd_out=$ccd->saveXml();
98 $ccd_len=strlen($ccd_out);
100 @fwrite
($fp,"CDA $ccd_len\n");
102 $ret=@fgets
($fp,256);
103 if($ret!="BEGIN\n") {
104 @fwrite
($fp,"BYE\n");
106 return("$config_err 7");
108 @fwrite
($fp,$ccd_out);
110 $ret=@fgets
($fp,256);
112 @fwrite
($fp,"BYE\n");
114 return("$config_err 8");
116 @fwrite
($fp,"SEND\n");
118 $ret=@fgets
($fp,256);
119 @fwrite
($fp,"BYE\n");
122 if(substr($ret,5)=="ERROR")
123 return( xl("The message could not be sent at this time."));
126 * If we get here, the message was successfully sent and the return
127 * value $ret is of the form "QUEUED recipient message-id" which
128 * is suitable for logging.
131 if($requested_by=="patient")
132 newEvent("transmit-ccd","portal-user",$_SESSION['authProvider'],1,$ret,$pid);
134 newEvent("transmit-ccd",$_SESSION['authUser'],$_SESSION['authProvider'],1,$ret,$pid);