Bug reported by Ginger Singletary APRN (#2446)
[openemr.git] / ccdaservice / ccda_gateway.php
blob24b4b6bada8e72f41be59914ef642dec062dffa5
1 <?php
2 /**
3 * ccda_gateway.php
5 * @package OpenEMR
6 * @link https://www.open-emr.org
7 * @author Jerry Padgett <sjpadgett@gmail.com>
8 * @copyright Copyright (c) 2016-2017 Jerry Padgett <sjpadgett@gmail.com>
9 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
11 //authencate for portal or main- never know where it gets used
12 session_start();
13 if (isset($_SESSION['pid']) && isset($_SESSION['patient_portal_onsite_two'])) {
14 $pid = $_SESSION['pid'];
15 $ignoreAuth = true;
16 require_once(dirname(__FILE__) . "/../interface/globals.php");
17 define('IS_DASHBOARD', false);
18 define('IS_PORTAL', $_SESSION['pid']);
19 } else {
20 session_destroy();
21 $ignoreAuth = false;
22 require_once(dirname(__FILE__) . "/../interface/globals.php");
23 if (!isset($_SESSION['authUserID'])) {
24 $landingpage = "index.php";
25 header('Location: ' . $landingpage);
26 exit;
29 define('IS_DASHBOARD', $_SESSION['authUserID']);
30 define('IS_PORTAL', false);
33 // give me something to do.
34 $dowhat = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
35 if ($dowhat && $GLOBALS['ccda_alt_service_enable'] > 0) {
36 if (!checkService()) { // woops, try again
37 if (!checkService()) { // thats 10 seconds of wasted time.
38 die("Document service start failed. Click back to return home."); // nuts! give up
41 } else {
42 // maybe next time
43 die("Cda generation service turned off: Verify in Administration->Globals! Click back to return home."); // Die an honorable death!!
46 //eventually below will qualify what document to fetch
47 $parameterArray = array();
48 $parameterArray ['encounter'];
49 $parameterArray ['combination'] = $pid;
50 $parameterArray ['components']; // = 'progress_note|consultation_note|continuity_care_document|diagnostic_image_reporting|discharge_summary|history_physical_note|operative_note|procedure_note|unstructured_document';
51 $parameterArray ['sections']; // = 'allergies|medications|problems|immunizations|procedures|results|plan_of_care|vitals|social_history|encounters|functional_status|referral|instructions';
52 $parameterArray ['downloadccda'] = 1;
53 $parameterArray ['sent_by'];
54 $parameterArray ['send'];
55 $parameterArray ['view'] = 1;
56 $parameterArray ['recipients'] = 'patient'; // emr_direct or hie else if not set $_SESSION['authUserID']
57 $parameterArray [0][6] = $_SESSION ['portal_username']; // set to an onsite portal user
59 if (!isset($_SESSION['site_id'])) {
60 $_SESSION ['site_id'] = 'default';
63 $server_url = $_SERVER['HTTP_HOST'] . $GLOBALS['webroot'];
64 // CCM returns entire cda with service doing templates
65 $ccdaxml = portalccdafetching($pid, $server_url, $parameterArray);
66 // disposal decisions will be here.
67 $h = '';
68 if (!$parameterArray ['view']) {
69 header('Content-Type: application/xml');
70 } else {
71 $h = '<a href="./../portal/home.php" </a><button style="color: red; background: white;" >' . xlt("Return Home") . '</button><br>';
74 print_r($h . $ccdaxml . $h);
75 //service_shutdown(1); //In ssmanager 0= terminate and disable 1 = soft=terminate but still active w/no restart, > 1 just restart based on B.S timer
76 exit;
78 function portalccdafetching($pid, $server_url, $parameterArray)
80 session_write_close();
81 $site_id = $_SESSION['site_id'];
82 $parameters = http_build_query($parameterArray); // future use
83 try {
84 $ch = curl_init();
85 $url = $server_url . "/interface/modules/zend_modules/public/encounterccdadispatch/index?site=$site_id&me=" . session_id() . "&param=1&view=1&combination=$pid&recipient=patient";
86 curl_setopt($ch, CURLOPT_URL, $url);
87 curl_setopt($ch, CURLOPT_HEADER, 0); // set true for look see
88 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
89 curl_setopt($ch, CURLOPT_COOKIESESSION, true);
90 curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie");
91 curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie");
92 //curl_setopt ($ch, CURLOPT_COOKIE, 'XDEBUG_SESSION=1'); // break on first line in public/index.php - uncomment and start any xdebug session and fetch a ccda in app.
93 curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
94 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
96 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
97 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
98 $result = curl_exec($ch) or die(curl_error($ch));
99 curl_close($ch);
100 } catch (Exception $e) {
101 return false;
104 return $result;
107 function checkService($ip = "localhost", $port = '6661')
109 $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
110 if ($socket === false) {
111 throw new Exception("Socket Creation Failed");
114 // Connect to the node server.
115 $result = socket_connect($socket, $ip, $port);
116 if ($result === false) {
117 $path = $GLOBALS['fileroot'] . "/ccdaservice";
118 if (IS_WINDOWS) {
119 $cmd = "node " . escapeshellarg($path . "/serveccda.js");
120 pclose(popen("start /B " . $cmd, "r"));
121 } else {
122 $cmd = "nodejs " . escapeshellarg($path . "/serveccda.js");
123 exec($cmd . " > /dev/null &");
125 sleep(2); // give cpu a rest
126 $result = socket_connect($socket, $ip, $port);
127 if ($result === false) { // hmm something is amist with service.
128 throw new Exception("Connection Failed");
131 socket_close($socket);
132 unset($socket);
133 return true;
136 return 0;