more accurate error messages when critical key errors (#4508)
[openemr.git] / ccdaservice / ccda_gateway.php
blob26d9ede20304907287e506a449faaaf966b055e2
1 <?php
3 /**
4 * ccda_gateway.php
6 * @package OpenEMR
7 * @link https://www.open-emr.org
8 * @author Jerry Padgett <sjpadgett@gmail.com>
9 * @author Brady Miller <brady.g.miller@gmail.com>
10 * @copyright Copyright (c) 2016-2017 Jerry Padgett <sjpadgett@gmail.com>
11 * @copyright Copyright (c) 2019 Brady Miller <brady.g.miller@gmail.com>
12 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
15 //authenticate for portal or main- never know where it gets used
17 // Will start the (patient) portal OpenEMR session/cookie.
18 require_once(dirname(__FILE__) . "/../src/Common/Session/SessionUtil.php");
19 OpenEMR\Common\Session\SessionUtil::portalSessionStart();
21 if (isset($_SESSION['pid']) && isset($_SESSION['patient_portal_onsite_two'])) {
22 $pid = $_SESSION['pid'];
23 $ignoreAuth = true;
24 require_once(dirname(__FILE__) . "/../interface/globals.php");
25 define('IS_DASHBOARD', false);
26 define('IS_PORTAL', $_SESSION['pid']);
27 } else {
28 OpenEMR\Common\Session\SessionUtil::portalSessionCookieDestroy();
29 $ignoreAuth = false;
30 require_once(dirname(__FILE__) . "/../interface/globals.php");
31 if (!isset($_SESSION['authUserID'])) {
32 $landingpage = "index.php";
33 header('Location: ' . $landingpage);
34 exit;
37 define('IS_DASHBOARD', $_SESSION['authUserID']);
38 define('IS_PORTAL', false);
41 // give me something to do.
42 $dowhat = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
43 if ($dowhat && $GLOBALS['ccda_alt_service_enable'] > 0) {
44 if (!checkService()) { // woops, try again
45 if (!checkService()) { // thats 10 seconds of wasted time.
46 die("Document service start failed. Click back to return home."); // nuts! give up
49 } else {
50 // maybe next time
51 die("Cda generation service turned off: Verify in Administration->Globals! Click back to return home."); // Die an honorable death!!
54 //eventually below will qualify what document to fetch
55 $parameterArray = array();
56 $parameterArray ['encounter'];
57 $parameterArray ['combination'] = $pid;
58 $parameterArray ['components']; // = 'progress_note|consultation_note|continuity_care_document|diagnostic_image_reporting|discharge_summary|history_physical_note|operative_note|procedure_note|unstructured_document';
59 $parameterArray ['sections']; // = 'allergies|medications|problems|immunizations|procedures|results|plan_of_care|vitals|social_history|encounters|functional_status|referral|instructions';
60 $parameterArray ['downloadccda'] = 1;
61 $parameterArray ['sent_by'];
62 $parameterArray ['send'];
63 $parameterArray ['view'] = 1;
64 $parameterArray ['recipients'] = 'patient'; // emr_direct or hie else if not set $_SESSION['authUserID']
65 $parameterArray [0][6] = $_SESSION ['portal_username']; // set to an onsite portal user
67 if (!isset($_SESSION['site_id'])) {
68 $_SESSION ['site_id'] = 'default';
71 // This is to close session write access when called from portal (core session already can not write to session)
72 session_write_close();
74 $server_url = $_SERVER['HTTP_HOST'] . $GLOBALS['webroot'];
75 // CCM returns entire cda with service doing templates
76 $ccdaxml = portalccdafetching($pid, $server_url, $parameterArray);
77 // disposal decisions will be here.
78 $h = '';
79 if (!$parameterArray ['view']) {
80 header('Content-Type: application/xml');
81 } else {
82 $h = '<a class="text-danger bg-white" href="./../portal/home.php">' . xlt("Return Home") . '</a><br />';
85 print_r($h . $ccdaxml . $h);
86 //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
87 exit;
89 function portalccdafetching($pid, $server_url, $parameterArray)
91 $site_id = $_SESSION['site_id'];
92 $parameters = http_build_query($parameterArray); // future use
93 try {
94 $ch = curl_init();
95 $url = $server_url . "/interface/modules/zend_modules/public/encounterccdadispatch/index?site=" . urlencode($site_id) . "&me=" . urlencode(session_id()) . "&param=1&view=1&combination=" . urlencode($pid) . "&recipient=patient";
96 curl_setopt($ch, CURLOPT_URL, $url);
97 curl_setopt($ch, CURLOPT_HEADER, 0); // set true for look see
98 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
99 curl_setopt($ch, CURLOPT_COOKIESESSION, true);
100 curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie");
101 curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie");
102 //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.
103 curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
104 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
106 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
107 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
108 $result = curl_exec($ch) or die(curl_error($ch));
109 curl_close($ch);
110 } catch (Exception $e) {
111 return false;
114 return $result;
117 function checkService($ip = "localhost", $port = '6661')
119 $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
120 if ($socket === false) {
121 throw new Exception("Socket Creation Failed");
124 // Connect to the node server.
125 $result = socket_connect($socket, $ip, $port);
126 if ($result === false) {
127 $path = $GLOBALS['fileroot'] . "/ccdaservice";
128 if (IS_WINDOWS) {
129 $cmd = "node " . escapeshellarg($path . "/serveccda.js");
130 pclose(popen("start /B " . $cmd, "r"));
131 } else {
132 $cmd = "nodejs " . escapeshellarg($path . "/serveccda.js");
133 exec($cmd . " > /dev/null &");
135 sleep(2); // give cpu a rest
136 $result = socket_connect($socket, $ip, $port);
137 if ($result === false) { // hmm something is amist with service.
138 throw new Exception("Connection Failed");
141 socket_close($socket);
142 unset($socket);
143 return true;
146 return 0;