fix: For prior PR #6749 (#6803)
[openemr.git] / ccr / createCCR.php
blob9a5629fbe4cdf208c858f546216f7ed2d6db0a2a
1 <?php
3 /**
4 * CCR Script.
6 * @package OpenEMR
7 * @link http://www.open-emr.org
8 * @author Garden State Health Systems <http://www.gshsys.com/>
9 * @author Brady Miller <brady.g.miller@gmail.com>
10 * @copyright Copyright (c) 2010 Garden State Health Systems <http://www.gshsys.com/>
11 * @copyright Copyright (c) 2018-2019 Brady Miller <brady.g.miller@gmail.com>
12 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
15 // check if using the patient portal
16 //(if so, then use the portal authorization)
17 if (isset($_GET['portal_auth'])) {
18 $landingpage = "../portal/index.php";
20 // Will start the (patient) portal OpenEMR session/cookie.
21 require_once(dirname(__FILE__) . "/../src/Common/Session/SessionUtil.php");
22 OpenEMR\Common\Session\SessionUtil::portalSessionStart();
24 if (isset($_SESSION['pid']) && isset($_SESSION['patient_portal_onsite_two'])) {
25 $pid = $_SESSION['pid'];
26 $ignoreAuth = true;
27 global $ignoreAuth;
28 } else {
29 OpenEMR\Common\Session\SessionUtil::portalSessionCookieDestroy();
30 header('Location: ' . $landingpage . '?w');
31 exit;
35 require_once(dirname(__FILE__) . "/../interface/globals.php");
36 require_once(dirname(__FILE__) . "/../library/sql-ccr.inc.php");
37 require_once(dirname(__FILE__) . "/uuid.php");
38 require_once(dirname(__FILE__) . "/transmitCCD.php");
39 require_once(dirname(__FILE__) . "/../custom/code_types.inc.php");
41 use PHPMailer\PHPMailer\PHPMailer;
43 function createCCR($action, $raw = "no", $requested_by = "")
46 $authorID = getUuid();
47 $patientID = getUuid();
48 $sourceID = getUuid();
49 $oemrID = getUuid();
51 $result = getActorData();
52 while ($res = sqlFetchArray($result[2])) {
53 ${"labID{$res['id']}"} = getUuid();
56 $ccr = new DOMDocument('1.0', 'UTF-8');
57 $e_styleSheet = $ccr->createProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="stylesheet/ccr.xsl"');
58 $ccr->appendChild($e_styleSheet);
60 $e_ccr = $ccr->createElementNS('urn:astm-org:CCR', 'ContinuityOfCareRecord');
61 $ccr->appendChild($e_ccr);
63 /////////////// Header
65 require_once("createCCRHeader.php");
66 $e_Body = $ccr->createElement('Body');
67 $e_ccr->appendChild($e_Body);
69 /////////////// Problems
71 $e_Problems = $ccr->createElement('Problems');
72 require_once("createCCRProblem.php");
73 $e_Body->appendChild($e_Problems);
75 /////////////// Alerts
77 $e_Alerts = $ccr->createElement('Alerts');
78 require_once("createCCRAlerts.php");
79 $e_Body->appendChild($e_Alerts);
81 ////////////////// Medication
83 $e_Medications = $ccr->createElement('Medications');
84 require_once("createCCRMedication.php");
85 $e_Body->appendChild($e_Medications);
87 ///////////////// Immunization
89 $e_Immunizations = $ccr->createElement('Immunizations');
90 require_once("createCCRImmunization.php");
91 $e_Body->appendChild($e_Immunizations);
94 /////////////////// Results
96 $e_Results = $ccr->createElement('Results');
97 require_once("createCCRResult.php");
98 $e_Body->appendChild($e_Results);
101 /////////////////// Procedures
103 //$e_Procedures = $ccr->createElement('Procedures');
104 //require_once("createCCRProcedure.php");
105 //$e_Body->appendChild($e_Procedures);
107 //////////////////// Footer
109 // $e_VitalSigns = $ccr->createElement('VitalSigns');
110 // $e_Body->appendChild($e_VitalSigns);
112 /////////////// Actors
114 $e_Actors = $ccr->createElement('Actors');
115 require_once("createCCRActor.php");
116 $e_ccr->appendChild($e_Actors);
118 if ($action == "generate") {
119 gnrtCCR($ccr, $raw, $requested_by);
122 if ($action == "viewccd") {
123 viewCCD($ccr, $raw, $requested_by);
127 function gnrtCCR($ccr, $raw = "no", $requested_by = "")
129 global $pid;
131 $ccr->preserveWhiteSpace = false;
132 $ccr->formatOutput = true;
134 if ($raw == "yes") {
135 // simply send the xml to a textarea (nice debugging tool)
136 echo "<textarea rows='35' cols='500' style='width:95%' readonly>";
137 echo $ccr->saveXml();
138 echo "</textarea>";
139 return;
140 } elseif ($raw == "hybrid") {
141 // send a file that contains a hybrid file of the raw xml and the xsl stylesheet
142 createHybridXML($ccr);
143 } elseif ($raw == "pure") {
144 // send a zip file that contains a separate xml data file and xsl stylesheet
145 if (! (class_exists('ZipArchive'))) {
146 displayError(xl("ERROR: Missing ZipArchive PHP Module"));
147 return;
150 $tempDir = $GLOBALS['temporary_files_dir'];
151 $zipName = $tempDir . "/" . getReportFilename() . "-ccr.zip";
152 if (file_exists($zipName)) {
153 unlink($zipName);
156 $zip = new ZipArchive();
157 if (!($zip)) {
158 displayError(xl("ERROR: Unable to Create Zip Archive."));
159 return;
162 if ($zip->open($zipName, ZipArchive::CREATE)) {
163 $zip->addFile("stylesheet/ccr.xsl", "stylesheet/ccr.xsl");
164 $xmlName = $tempDir . "/" . getReportFilename() . "-ccr.xml";
165 if (file_exists($xmlName)) {
166 unlink($xmlName);
169 $ccr->save($xmlName);
170 $zip->addFile($xmlName, basename($xmlName));
171 $zip->close();
172 header("Pragma: public");
173 header("Expires: 0");
174 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
175 header("Content-Type: application/force-download");
176 header("Content-Length: " . filesize($zipName));
177 header("Content-Disposition: attachment; filename=" . basename($zipName) . ";");
178 header("Content-Description: File Transfer");
179 readfile($zipName);
180 unlink($zipName);
181 unlink($xmlName);
182 exit(0);
183 } else {
184 displayError(xl("ERROR: Unable to Create Zip Archive."));
185 return;
187 } elseif (substr($raw, 0, 4) == "send") {
188 $recipient = trim(stripslashes(substr($raw, 5)));
189 $ccd_out = $ccr->saveXml();
190 $result = transmitCCD($pid, $ccd_out, $recipient, $requested_by, "CCR");
191 echo htmlspecialchars($result, ENT_NOQUOTES);
192 return;
193 } else {
194 header("Content-type: application/xml");
195 echo $ccr->saveXml();
199 function viewCCD($ccr, $raw = "no", $requested_by = "")
201 global $pid;
203 $ccr->preserveWhiteSpace = false;
204 $ccr->formatOutput = true;
206 if (file_exists(dirname(__FILE__) . '/generatedXml')) {
207 $ccr->save(dirname(__FILE__) . '/generatedXml/ccrForCCD.xml');
210 $xmlDom = new DOMDocument();
211 $xmlDom->loadXML($ccr->saveXML());
213 $ccr_ccd = new DOMDocument();
214 $ccr_ccd->load(dirname(__FILE__) . '/ccd/ccr_ccd.xsl');
216 $xslt = new XSLTProcessor();
217 $xslt->importStylesheet($ccr_ccd);
219 $ccd = new DOMDocument();
220 $ccd->preserveWhiteSpace = false;
221 $ccd->formatOutput = true;
223 $ccd->loadXML($xslt->transformToXML($xmlDom));
225 if (file_exists(dirname(__FILE__) . '/generatedXml')) {
226 $ccd->save(dirname(__FILE__) . '/generatedXml/ccdDebug.xml');
229 if ($raw == "yes") {
230 // simply send the xml to a textarea (nice debugging tool)
231 echo "<textarea rows='35' cols='500' style='width:95%' readonly>";
232 echo $ccd->saveXml();
233 echo "</textarea>";
234 return;
237 if ($raw == "pure") {
238 // send a zip file that contains a separate xml data file and xsl stylesheet
239 if (! (class_exists('ZipArchive'))) {
240 displayError(xl("ERROR: Missing ZipArchive PHP Module"));
241 return;
244 $tempDir = $GLOBALS['temporary_files_dir'];
245 $zipName = $tempDir . "/" . getReportFilename() . "-ccd.zip";
246 if (file_exists($zipName)) {
247 unlink($zipName);
250 $zip = new ZipArchive();
251 if (!($zip)) {
252 displayError(xl("ERROR: Unable to Create Zip Archive."));
253 return;
256 if ($zip->open($zipName, ZipArchive::CREATE)) {
257 $zip->addFile("stylesheet/cda.xsl", "stylesheet/cda.xsl");
258 $xmlName = $tempDir . "/" . getReportFilename() . "-ccd.xml";
259 if (file_exists($xmlName)) {
260 unlink($xmlName);
263 $e_styleSheet = $ccd->createProcessingInstruction(
264 'xml-stylesheet',
265 'type="text/xsl" href="stylesheet/cda.xsl"'
267 $ccd->insertBefore($e_styleSheet, $ccd->firstChild);
268 $ccd->save($xmlName);
269 $zip->addFile($xmlName, basename($xmlName));
270 $zip->close();
271 header("Pragma: public");
272 header("Expires: 0");
273 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
274 header("Content-Type: application/force-download");
275 header("Content-Length: " . filesize($zipName));
276 header("Content-Disposition: attachment; filename=" . basename($zipName) . ";");
277 header("Content-Description: File Transfer");
278 readfile($zipName);
279 unlink($zipName);
280 unlink($xmlName);
281 exit(0);
282 } else {
283 displayError(xl("ERROR: Unable to Create Zip Archive."));
284 return;
288 if (substr($raw, 0, 4) == "send") {
289 $recipient = trim(stripslashes(substr($raw, 5)));
290 $ccd_out = $ccd->saveXml();
291 $result = transmitCCD($pid, $ccd_out, $recipient, $requested_by);
292 echo htmlspecialchars($result, ENT_NOQUOTES);
293 return;
296 $ss = new DOMDocument();
297 $ss->load(dirname(__FILE__) . "/stylesheet/cda.xsl");
299 $xslt->importStyleSheet($ss);
301 $html = $xslt->transformToXML($ccd);
303 echo $html;
307 function sourceType($ccr, $uuid)
310 $e_Source = $ccr->createElement('Source');
312 $e_Actor = $ccr->createElement('Actor');
313 $e_Source->appendChild($e_Actor);
315 $e_ActorID = $ccr->createElement('ActorID', $uuid);
316 $e_Actor->appendChild($e_ActorID);
318 return $e_Source;
322 function displayError($message)
324 echo '<script>alert("' . addslashes($message) . '");</script>';
328 function createHybridXML($ccr)
331 // save the raw xml
332 $main_xml = $ccr->saveXml();
334 // save the stylesheet
335 $main_stylesheet = file_get_contents('stylesheet/ccr.xsl');
337 // replace stylesheet link in raw xml file
338 $substitute_string = '<?xml-stylesheet type="text/xsl" href="#style1"?>
339 <!DOCTYPE ContinuityOfCareRecord [
340 <!ATTLIST xsl:stylesheet id ID #REQUIRED>
343 $replace_string = '<?xml-stylesheet type="text/xsl" href="stylesheet/ccr.xsl"?>';
344 $main_xml = str_replace($replace_string, $substitute_string, $main_xml);
346 // remove redundant xml declaration from stylesheet
347 $replace_string = '<?xml version="1.0" encoding="UTF-8"?>';
348 $main_stylesheet = str_replace($replace_string, '', $main_stylesheet);
350 // embed the stylesheet in the raw xml file
351 $replace_string = '<ContinuityOfCareRecord xmlns="urn:astm-org:CCR">';
352 $main_stylesheet = $replace_string . $main_stylesheet;
353 $main_xml = str_replace($replace_string, $main_stylesheet, $main_xml);
355 // insert style1 id into the stylesheet parameter
356 $substitute_string = 'xsl:stylesheet id="style1" exclude-result-prefixes';
357 $replace_string = 'xsl:stylesheet exclude-result-prefixes';
358 $main_xml = str_replace($replace_string, $substitute_string, $main_xml);
360 // prepare the filename to use
361 // LASTNAME-FIRSTNAME-PID-DATESTAMP-ccr.xml
362 $main_filename = getReportFilename() . "-ccr.xml";
364 // send the output as a file to the user
365 header("Content-type: text/xml");
366 header("Content-Disposition: attachment; filename=" . $main_filename . "");
367 echo $main_xml;
370 if ($_POST['ccrAction']) {
371 $raw = $_POST['raw'];
372 /* If transmit requested, fail fast if the recipient address fails basic validation */
373 if (substr($raw, 0, 4) == "send") {
374 $send_to = trim(stripslashes(substr($raw, 5)));
375 if (!PHPMailer::ValidateAddress($send_to)) {
376 echo(htmlspecialchars(xl('Invalid recipient address. Please try again.'), ENT_QUOTES));
377 return;
380 createCCR($_POST['ccrAction'], $raw, $_POST['requested_by']);
381 } else {
382 createCCR($_POST['ccrAction'], $raw);