Show link to php info in phpmyadmin. contributed by pfwilliams
[openemr.git] / ccr / transmitCCD.php
blobf9b5d390f7add1a8cfbbf79bacacd3f07866c526
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");
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) {
40 global $pid;
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'];
49 break;
50 case "https": $server="ssl://".$phimail_server['host'];
51 break;
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");
57 @fflush($fp);
58 $ret=@fgets($fp,256);
59 if($ret!="OK\n") {
60 @fwrite($fp,"BYE\n");
61 @fclose($fp);
62 return("$config_err 4");
64 @fwrite($fp,"TO $recipient\n");
65 @fflush($fp);
66 $ret=@fgets($fp,256);
67 if($ret!="OK\n") {
68 @fwrite($fp,"BYE\n");
69 @fclose($fp);
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.");
76 else
77 $text_out = xl("A clinical document is attached.");
79 $text_len=strlen($text_out);
80 @fwrite($fp,"TEXT $text_len\n");
81 @fflush($fp);
82 $ret=@fgets($fp,256);
83 if($ret!="BEGIN\n") {
84 @fwrite($fp,"BYE\n");
85 @fclose($fp);
86 return("$config_err 5");
88 @fwrite($fp,$text_out);
89 @fflush($fp);
90 $ret=@fgets($fp,256);
91 if($ret!="OK\n") {
92 @fwrite($fp,"BYE\n");
93 @fclose($fp);
94 return("$config_err 6");
97 $ccd_out=$ccd->saveXml();
98 $ccd_len=strlen($ccd_out);
100 @fwrite($fp,"CDA $ccd_len\n");
101 @fflush($fp);
102 $ret=@fgets($fp,256);
103 if($ret!="BEGIN\n") {
104 @fwrite($fp,"BYE\n");
105 @fclose($fp);
106 return("$config_err 7");
108 @fwrite($fp,$ccd_out);
109 @fflush($fp);
110 $ret=@fgets($fp,256);
111 if($ret!="OK\n") {
112 @fwrite($fp,"BYE\n");
113 @fclose($fp);
114 return("$config_err 8");
116 @fwrite($fp,"SEND\n");
117 @fflush($fp);
118 $ret=@fgets($fp,256);
119 @fwrite($fp,"BYE\n");
120 @fclose($fp);
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);
133 else
134 newEvent("transmit-ccd",$_SESSION['authUser'],$_SESSION['authProvider'],1,$ret,$pid);
135 return("SUCCESS");