Remove freeb and some other legacy code.
[openemr.git] / ccr / createCCR.php
blobc13a4f5619aa46be592838220d0d7e5c3d6b6d2e
1 <?php
2 /**
3 * CCR Script.
5 * Copyright (C) 2010 Garden State Health Systems <http://www.gshsys.com/>
7 * LICENSE: This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 3
10 * of the 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 General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
18 * @package OpenEMR
19 * @author Garden State Health Systems <http://www.gshsys.com/>
20 * @link http://www.open-emr.org
24 //SANITIZE ALL ESCAPES
25 $sanitize_all_escapes=true;
28 //STOP FAKE REGISTER GLOBALS
29 $fake_register_globals=false;
32 // check if using the patient portal
33 //(if so, then use the portal authorization)
34 if (isset($_GET['portal_auth'])) {
35 $landingpage = "../patients/index.php";
36 session_start();
37 if ( isset($_SESSION['pid']) && isset($_SESSION['patient_portal_onsite']) ) {
38 $pid = $_SESSION['pid'];
39 $ignoreAuth=true;
40 global $ignoreAuth;
42 else {
43 session_destroy();
44 header('Location: '.$landingpage.'?w');
45 exit;
49 require_once(dirname(__FILE__) . "/../interface/globals.php");
50 require_once(dirname(__FILE__) . "/../library/sql-ccr.inc");
51 require_once(dirname(__FILE__) . "/uuid.php");
52 require_once(dirname(__FILE__) . "/transmitCCD.php");
53 require_once(dirname(__FILE__) . "/../custom/code_types.inc.php");
55 function createCCR($action,$raw="no",$requested_by=""){
57 $authorID = getUuid();
58 $patientID = getUuid();
59 $sourceID = getUuid();
60 $oemrID = getUuid();
62 $result = getActorData();
63 while($res = sqlFetchArray($result[2])){
64 ${"labID{$res['id']}"} = getUuid();
67 $ccr = new DOMDocument('1.0','UTF-8');
68 $e_styleSheet = $ccr->createProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="stylesheet/ccr.xsl"');
69 $ccr->appendChild($e_styleSheet);
71 $e_ccr = $ccr->createElementNS('urn:astm-org:CCR', 'ContinuityOfCareRecord');
72 $ccr->appendChild($e_ccr);
74 /////////////// Header
76 require_once("createCCRHeader.php");
77 $e_Body = $ccr->createElement('Body');
78 $e_ccr->appendChild($e_Body);
80 /////////////// Problems
82 $e_Problems = $ccr->createElement('Problems');
83 require_once("createCCRProblem.php");
84 $e_Body->appendChild($e_Problems);
86 /////////////// Alerts
88 $e_Alerts = $ccr->createElement('Alerts');
89 require_once("createCCRAlerts.php");
90 $e_Body->appendChild($e_Alerts);
92 ////////////////// Medication
94 $e_Medications = $ccr->createElement('Medications');
95 require_once("createCCRMedication.php");
96 $e_Body->appendChild($e_Medications);
98 ///////////////// Immunization
100 $e_Immunizations = $ccr->createElement('Immunizations');
101 require_once("createCCRImmunization.php");
102 $e_Body->appendChild($e_Immunizations);
105 /////////////////// Results
107 $e_Results = $ccr->createElement('Results');
108 require_once("createCCRResult.php");
109 $e_Body->appendChild($e_Results);
112 /////////////////// Procedures
114 //$e_Procedures = $ccr->createElement('Procedures');
115 //require_once("createCCRProcedure.php");
116 //$e_Body->appendChild($e_Procedures);
118 //////////////////// Footer
120 // $e_VitalSigns = $ccr->createElement('VitalSigns');
121 // $e_Body->appendChild($e_VitalSigns);
123 /////////////// Actors
125 $e_Actors = $ccr->createElement('Actors');
126 require_once("createCCRActor.php");
127 $e_ccr->appendChild($e_Actors);
129 if ($action=="generate"){
130 gnrtCCR($ccr,$raw,$requested_by);
133 if($action == "viewccd"){
134 viewCCD($ccr,$raw,$requested_by);
138 function gnrtCCR($ccr,$raw="no",$requested_by=""){
139 global $pid;
141 $ccr->preserveWhiteSpace = false;
142 $ccr->formatOutput = true;
144 if ($raw == "yes") {
145 // simply send the xml to a textarea (nice debugging tool)
146 echo "<textarea rows='35' cols='500' style='width:95%' readonly>";
147 echo $ccr->saveXml();
148 echo "</textarea>";
149 return;
152 else if ($raw == "hybrid") {
153 // send a file that contains a hybrid file of the raw xml and the xsl stylesheet
154 createHybridXML($ccr);
157 else if ($raw == "pure") {
158 // send a zip file that contains a separate xml data file and xsl stylesheet
159 if (! (class_exists('ZipArchive')) ) {
160 displayError(xl("ERROR: Missing ZipArchive PHP Module"));
161 return;
163 $tempDir = $GLOBALS['temporary_files_dir'];
164 $zipName = $tempDir . "/" . getReportFilename() . "-ccr.zip";
165 if (file_exists($zipName)) {
166 unlink($zipName);
168 $zip = new ZipArchive();
169 if (!($zip)) {
170 displayError(xl("ERROR: Unable to Create Zip Archive."));
171 return;
173 if ( $zip->open($zipName, ZIPARCHIVE::CREATE) ) {
174 $zip->addFile("stylesheet/ccr.xsl", "stylesheet/ccr.xsl");
175 $xmlName = $tempDir . "/" . getReportFilename() . "-ccr.xml";
176 if (file_exists($xmlName)) {
177 unlink($xmlName);
179 $ccr->save($xmlName);
180 $zip->addFile($xmlName, basename($xmlName) );
181 $zip->close();
182 header("Pragma: public");
183 header("Expires: 0");
184 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
185 header("Content-Type: application/force-download");
186 header("Content-Length: " . filesize($zipName));
187 header("Content-Disposition: attachment; filename=" . basename($zipName) . ";");
188 header("Content-Description: File Transfer");
189 readfile($zipName);
190 unlink($zipName);
191 unlink($xmlName);
192 exit(0);
194 else {
195 displayError(xl("ERROR: Unable to Create Zip Archive."));
196 return;
200 else if (substr($raw,0,4)=="send") {
201 $recipient = trim(stripslashes(substr($raw,5)));
202 $result=transmitCCD($ccr,$recipient,$requested_by,"CCR");
203 echo htmlspecialchars($result,ENT_NOQUOTES);
204 return;
207 else {
208 header("Content-type: application/xml");
209 echo $ccr->saveXml();
214 function viewCCD($ccr,$raw="no",$requested_by=""){
215 global $pid;
217 $ccr->preserveWhiteSpace = false;
218 $ccr->formatOutput = true;
220 $ccr->save(dirname(__FILE__) .'/generatedXml/ccrForCCD.xml');
222 $xmlDom = new DOMDocument();
223 $xmlDom->loadXML($ccr->saveXML());
225 $ccr_ccd = new DOMDocument();
226 $ccr_ccd->load(dirname(__FILE__) .'/ccd/ccr_ccd.xsl');
228 $xslt = new XSLTProcessor();
229 $xslt->importStylesheet($ccr_ccd);
231 $ccd = new DOMDocument();
232 $ccd->preserveWhiteSpace = false;
233 $ccd->formatOutput = true;
235 $ccd->loadXML($xslt->transformToXML($xmlDom));
237 $ccd->save(dirname(__FILE__) .'/generatedXml/ccdDebug.xml');
239 if ($raw == "yes") {
240 // simply send the xml to a textarea (nice debugging tool)
241 echo "<textarea rows='35' cols='500' style='width:95%' readonly>";
242 echo $ccd->saveXml();
243 echo "</textarea>";
244 return;
247 if ($raw == "pure") {
248 // send a zip file that contains a separate xml data file and xsl stylesheet
249 if (! (class_exists('ZipArchive')) ) {
250 displayError(xl("ERROR: Missing ZipArchive PHP Module"));
251 return;
253 $tempDir = $GLOBALS['temporary_files_dir'];
254 $zipName = $tempDir . "/" . getReportFilename() . "-ccd.zip";
255 if (file_exists($zipName)) {
256 unlink($zipName);
258 $zip = new ZipArchive();
259 if (!($zip)) {
260 displayError(xl("ERROR: Unable to Create Zip Archive."));
261 return;
263 if ( $zip->open($zipName, ZIPARCHIVE::CREATE) ) {
264 $zip->addFile("stylesheet/cda.xsl", "stylesheet/cda.xsl");
265 $xmlName = $tempDir . "/" . getReportFilename() . "-ccd.xml";
266 if (file_exists($xmlName)) {
267 unlink($xmlName);
269 $e_styleSheet = $ccd->createProcessingInstruction('xml-stylesheet',
270 'type="text/xsl" href="stylesheet/cda.xsl"');
271 $ccd->insertBefore($e_styleSheet,$ccd->firstChild);
272 $ccd->save($xmlName);
273 $zip->addFile($xmlName, basename($xmlName) );
274 $zip->close();
275 header("Pragma: public");
276 header("Expires: 0");
277 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
278 header("Content-Type: application/force-download");
279 header("Content-Length: " . filesize($zipName));
280 header("Content-Disposition: attachment; filename=" . basename($zipName) . ";");
281 header("Content-Description: File Transfer");
282 readfile($zipName);
283 unlink($zipName);
284 unlink($xmlName);
285 exit(0);
287 else {
288 displayError(xl("ERROR: Unable to Create Zip Archive."));
289 return;
293 if (substr($raw,0,4)=="send") {
294 $recipient = trim(stripslashes(substr($raw,5)));
295 $result=transmitCCD($ccd,$recipient,$requested_by);
296 echo htmlspecialchars($result,ENT_NOQUOTES);
297 return;
300 $ss = new DOMDocument();
301 $ss->load(dirname(__FILE__) ."/stylesheet/cda.xsl");
303 $xslt->importStyleSheet($ss);
305 $html = $xslt->transformToXML($ccd);
307 echo $html;
312 function sourceType($ccr, $uuid){
314 $e_Source = $ccr->createElement('Source');
316 $e_Actor = $ccr->createElement('Actor');
317 $e_Source->appendChild($e_Actor);
319 $e_ActorID = $ccr->createElement('ActorID',$uuid);
320 $e_Actor->appendChild($e_ActorID);
322 return $e_Source;
326 function displayError($message) {
327 echo '<script type="text/javascript">alert("' . addslashes($message) . '");</script>';
331 function createHybridXML($ccr) {
333 // save the raw xml
334 $main_xml = $ccr->saveXml();
336 // save the stylesheet
337 $main_stylesheet = file_get_contents('stylesheet/ccr.xsl');
339 // replace stylesheet link in raw xml file
340 $substitute_string = '<?xml-stylesheet type="text/xsl" href="#style1"?>
341 <!DOCTYPE ContinuityOfCareRecord [
342 <!ATTLIST xsl:stylesheet id ID #REQUIRED>
345 $replace_string = '<?xml-stylesheet type="text/xsl" href="stylesheet/ccr.xsl"?>';
346 $main_xml = str_replace($replace_string,$substitute_string,$main_xml);
348 // remove redundant xml declaration from stylesheet
349 $replace_string = '<?xml version="1.0" encoding="UTF-8"?>';
350 $main_stylesheet = str_replace($replace_string,'',$main_stylesheet);
352 // embed the stylesheet in the raw xml file
353 $replace_string ='<ContinuityOfCareRecord xmlns="urn:astm-org:CCR">';
354 $main_stylesheet = $replace_string.$main_stylesheet;
355 $main_xml = str_replace($replace_string,$main_stylesheet,$main_xml);
357 // insert style1 id into the stylesheet parameter
358 $substitute_string = 'xsl:stylesheet id="style1" exclude-result-prefixes';
359 $replace_string = 'xsl:stylesheet exclude-result-prefixes';
360 $main_xml = str_replace($replace_string,$substitute_string,$main_xml);
362 // prepare the filename to use
363 // LASTNAME-FIRSTNAME-PID-DATESTAMP-ccr.xml
364 $main_filename = getReportFilename()."-ccr.xml";
366 // send the output as a file to the user
367 header("Content-type: text/xml");
368 header("Content-Disposition: attachment; filename=" . $main_filename . "");
369 echo $main_xml;
372 if($_POST['ccrAction']) {
373 $raw=$_POST['raw'];
374 /* If transmit requested, fail fast if the recipient address fails basic validation */
375 if (substr($raw,0,4)=="send") {
376 $send_to = trim(stripslashes(substr($raw,5)));
377 if (!PHPMailer::ValidateAddress($send_to)) {
378 echo(htmlspecialchars( xl('Invalid recipient address. Please try again.'), ENT_QUOTES));
379 return;
381 createCCR($_POST['ccrAction'],$raw,$_POST['requested_by']);
382 } else {
383 createCCR($_POST['ccrAction'],$raw);