quick minor path updates (#1968)
[openemr.git] / ccdaservice / ccda_gateway.php
blob6efc575bec35d4ab31f72e2ef2756d691d16648e
1 <?php
2 /**
4 * Copyright (C) 2016-2017 Jerry Padgett <sjpadgett@gmail.com>
6 * LICENSE: This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @package OpenEMR
20 * @author Jerry Padgett <sjpadgett@gmail.com>
21 * @link http://www.open-emr.org
23 //authencate for portal or main- never know where it gets used
24 session_start();
25 if (isset($_SESSION['pid']) && isset($_SESSION['patient_portal_onsite_two'])) {
26 $pid = $_SESSION['pid'];
27 $ignoreAuth = true;
28 require_once(dirname(__FILE__) . "/../interface/globals.php");
29 define('IS_DASHBOARD', false);
30 define('IS_PORTAL', $_SESSION['pid']);
31 } else {
32 session_destroy();
33 $ignoreAuth = false;
34 require_once(dirname(__FILE__) . "/../interface/globals.php");
35 if (!isset($_SESSION['authUserID'])) {
36 $landingpage = "index.php";
37 header('Location: ' . $landingpage);
38 exit;
41 define('IS_DASHBOARD', $_SESSION['authUserID']);
42 define('IS_PORTAL', false);
45 // give me something to do.
46 $dowhat = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
47 if ($dowhat && $GLOBALS['ccda_alt_service_enable'] > 0) {
48 if (!checkService()) { // woops, try again
49 if (!checkService()) { // thats 10 seconds of wasted time.
50 die("Document service start failed. Click back to return home."); // nuts! give up
53 } else {
54 // maybe next time
55 die("Cda generation service turned off: Verify in Administration->Globals! Click back to return home."); // Die an honorable death!!
58 //eventually below will qualify what document to fetch
59 $parameterArray = array();
60 $parameterArray ['encounter'];
61 $parameterArray ['combination'] = $pid;
62 $parameterArray ['components']; // = 'progress_note|consultation_note|continuity_care_document|diagnostic_image_reporting|discharge_summary|history_physical_note|operative_note|procedure_note|unstructured_document';
63 $parameterArray ['sections']; // = 'allergies|medications|problems|immunizations|procedures|results|plan_of_care|vitals|social_history|encounters|functional_status|referral|instructions';
64 $parameterArray ['downloadccda'] = 1;
65 $parameterArray ['sent_by'];
66 $parameterArray ['send'];
67 $parameterArray ['view'] = 1;
68 $parameterArray ['recipients'] = 'patient'; // emr_direct or hie else if not set $_SESSION['authUserID']
69 $parameterArray [0][6] = $_SESSION ['portal_username']; // set to an onsite portal user
71 if (!isset($_SESSION['site_id'])) {
72 $_SESSION ['site_id'] = 'default';
75 $server_url = $_SERVER['HTTP_HOST'] . $GLOBALS['webroot'];
76 // CCM returns entire cda with service doing templates
77 $ccdaxml = portalccdafetching($pid, $server_url, $parameterArray);
78 // disposal decisions will be here.
79 $h = '';
80 if (!$parameterArray ['view']) {
81 header('Content-Type: application/xml');
82 } else {
83 $h = '<a href="./../portal/home.php" </a><button style="color: red; background: white;" >' . xlt("Return Home") . '</button><br>';
86 print_r($h . $ccdaxml . $h);
87 //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
88 exit;
90 function portalccdafetching($pid, $server_url, $parameterArray)
92 session_write_close();
93 $site_id = $_SESSION['site_id'];
94 $parameters = http_build_query($parameterArray); // future use
95 try {
96 $ch = curl_init();
97 $url = $server_url . "/interface/modules/zend_modules/public/encounterccdadispatch/index?site=$site_id&me=" . session_id() . "&param=1&view=1&combination=$pid&recipient=patient";
98 curl_setopt($ch, CURLOPT_URL, $url);
99 curl_setopt($ch, CURLOPT_HEADER, 0); // set true for look see
100 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
101 curl_setopt($ch, CURLOPT_COOKIESESSION, true);
102 curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie");
103 curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie");
104 //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.
105 curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
106 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
108 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
109 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
110 $result = curl_exec($ch) or die(curl_error($ch));
111 curl_close($ch);
112 } catch (Exception $e) {
113 return false;
116 return $result;
119 function checkService($ip = "localhost", $port = '6661')
121 $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
122 if ($socket === false) {
123 throw new Exception("Socket Creation Failed");
126 // Connect to the node server.
127 $result = socket_connect($socket, $ip, $port);
128 if ($result === false) {
129 $path = $GLOBALS['fileroot'] . "/ccdaservice";
130 if (IS_WINDOWS) {
131 $cmd = "node " . escapeshellarg($path . "/serveccda.js");
132 pclose(popen("start /B " . $cmd, "r"));
133 } else {
134 $cmd = "nodejs " . escapeshellarg($path . "/serveccda.js");
135 exec($cmd . " > /dev/null &");
137 sleep(2); // give cpu a rest
138 $result = socket_connect($socket, $ip, $port);
139 if ($result === false) { // hmm something is amist with service.
140 throw new Exception("Connection Failed");
143 socket_close($socket);
144 unset($socket);
145 return true;
148 return 0;