security fix in master branch
[openemr.git] / library / ajax / ccr_import_ajax.php
blob849ff402dad59ebc1674437eade84d64151eae96
1 <?php
2 /**
3 * library/ccr_import_ajax.php Functions related to patient CCR/CCD/CCDA parsing.
5 * Functions related to patient CCR/CCD/CCDA parsing and insert/update to corresponding tables.
7 * Copyright (C) 2013 Z&H Consultancy Services Private Limited <sam@zhservices.com>
9 * LICENSE: This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 3
12 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
20 * @package OpenEMR
21 * @author Eldho Chacko <eldho@zhservices.com>
22 * @author Ajil P M <ajilpm@zhservices.com>
23 * @link http://www.open-emr.org
28 require_once(dirname(__FILE__) . "/../../interface/globals.php");
29 require_once(dirname(__FILE__) . "/../parse_patient_xml.php");
31 if ($_REQUEST["ccr_ajax"] == "yes") {
32 $doc_id = $_REQUEST["document_id"];
33 $d = new Document($doc_id);
34 $url = $d->get_url();
35 $storagemethod = $d->get_storagemethod();
36 $couch_docid = $d->get_couch_docid();
37 $couch_revid = $d->get_couch_revid();
38 if ($storagemethod == 1) {
39 $couch = new CouchDB();
40 $data = array($GLOBALS['couchdb_dbase'],$couch_docid);
41 $resp = $couch->retrieve_doc($data);
42 $content = $resp->data;
43 if ($content=='' && $GLOBALS['couchdb_log']==1) {
44 $log_content = date('Y-m-d H:i:s')." ==> Retrieving document\r\n";
45 $log_content = date('Y-m-d H:i:s')." ==> URL: ".$url."\r\n";
46 $log_content .= date('Y-m-d H:i:s')." ==> CouchDB Document Id: ".$couch_docid."\r\n";
47 $log_content .= date('Y-m-d H:i:s')." ==> CouchDB Revision Id: ".$couch_revid."\r\n";
48 $log_content .= date('Y-m-d H:i:s')." ==> Failed to fetch document content from CouchDB.\r\n";
49 $log_content .= date('Y-m-d H:i:s')." ==> Will try to download file from HardDisk if exists.\r\n\r\n";
50 $this->document_upload_download_log($d->get_foreign_id(), $log_content);
51 die(xlt("File retrieval from CouchDB failed"));
54 $content = base64_decode($content);
55 } else {
56 $url = preg_replace("|^(.*)://|", "", $url);
57 $from_all = explode("/", $url);
58 $from_filename = array_pop($from_all);
59 $from_pathname_array = array();
60 for ($i=0; $i<$d->get_path_depth(); $i++) {
61 $from_pathname_array[] = array_pop($from_all);
64 $from_pathname_array = array_reverse($from_pathname_array);
65 $from_pathname = implode("/", $from_pathname_array);
66 $temp_url = $GLOBALS['OE_SITE_DIR'] . '/documents/' . $from_pathname . '/' . $from_filename;
67 if (!file_exists($temp_url)) {
68 echo xlt('The requested document is not present at the expected location on the filesystem or there are not sufficient permissions to access it') . '.' . $temp_url;
69 } else {
70 $content = file_get_contents($temp_url);
74 //fields to which the corresponding elements are to be inserted
75 //format - level 1 key is the main tag in the XML eg:- //Problems or //Problems/Problem according to the content in the XML.
76 //level 2 key is 'table name:field name' and level 2 value is the sub tag under the main tag given in level 1 key
77 //eg:- 'Type/Text' if the XML format is '//Problems/Problem/Type/Text' or 'id/@extension' if it is an attribute
78 //level 2 key can be 'table name:#some value' for checking whether a particular tag exits in the XML section
79 $field_mapping = array(
80 '//Problems/Problem' => array(
81 'lists1:diagnosis' => 'Description/Code/Value',
82 'lists1:comments' => 'CommentID',
83 'lists1:activity' => 'Status/Text',
85 '//Alerts/Alert' => array(
86 'lists2:type' => 'Type/Text',
87 'lists2:diagnosis' => 'Description/Code/Value',
88 'lists2:date' => 'Agent/EnvironmentalAgents/EnvironmentalAgent/DateTime/ExactDateTime',
89 'lists2:title' => 'Agent/EnvironmentalAgents/EnvironmentalAgent/Description/Text',
90 'lists2:reaction' => 'Reaction/Description/Text',
92 '//Medications/Medication' => array(
93 'prescriptions:date_added' => 'DateTime/ExactDateTime',
94 'prescriptions:active' => 'Status/Text',
95 'prescriptions:drug' => 'Product/ProductName/Text',
96 'prescriptions:size' => 'Product/Strength/Value',
97 'prescriptions:unit' => 'Product/Strength/Units/Unit',
98 'prescriptions:form' => 'Product/Form/Text',
99 'prescriptions:quantity' => 'Quantity/Value',
100 'prescriptions:note' => 'PatientInstructions/Instruction/Text',
101 'prescriptions:refills' => 'Refills/Refill/Number',
103 '//Immunizations/Immunization' => array(
104 'immunizations:administered_date' => 'DateTime/ExactDateTime',
105 'immunizations:note' => 'Directions/Direction/Description/Text',
107 '//Results/Result' => array(
108 'procedure_result:date' => 'DateTime/ExactDateTime',
109 'procedure_type:name' => 'Test/Description/Text',
110 'procedure_result:result' => 'Test/TestResult/Value',
111 'procedure_result:range' => 'Test/NormalResult/Normal/Value',
112 'procedure_result:abnormal' => 'Test/Flag/Text',
114 '//Actors/Actor' => array(
115 'patient_data:fname' => 'Person/Name/CurrentName/Given',
116 'patient_data:lname' => 'Person/Name/CurrentName/Family',
117 'patient_data:DOB' => 'Person/DateOfBirth/ExactDateTime',
118 'patient_data:sex' => 'Person/Gender/Text',
119 'patient_data:abname' => 'InformationSystem/Name',
120 'patient_data:#Type' => 'InformationSystem/Type',
121 'patient_data:pubpid' => 'IDs/ID',
122 'patient_data:street' => 'Address/Line1',
123 'patient_data:city' => 'Address/City',
124 'patient_data:state' => 'Address/State',
125 'patient_data:postal_code' => 'Address/PostalCode',
126 'patient_data:phone_contact' => 'Telephone/Value',
129 if (!empty($content)) {
130 $var = array();
131 $res = parseXmlStream($content, $field_mapping);
132 $var = array(
133 'approval_status' => 1,
134 'type' => 11,
135 'ip_address' => $_SERVER['REMOTE_ADDR'],
137 foreach ($res as $sections => $details) {
138 foreach ($details as $cnt => $vals) {
139 foreach ($vals as $key => $val) {
140 if (array_key_exists('#Type', $res[$sections][$cnt])) {
141 if ($key == 'postal_code') {
142 $var['field_name_value_array']['misc_address_book'][$cnt]['zip'] = $val;
143 } elseif ($key == 'phone_contact') {
144 $var['field_name_value_array']['misc_address_book'][$cnt]['phone'] = $val;
145 } elseif ($key == 'abname') {
146 $values = explode(' ', $val);
147 if ($values[0]) {
148 $var['field_name_value_array']['misc_address_book'][$cnt]['lname'] = $values[0];
151 if ($values[1]) {
152 $var['field_name_value_array']['misc_address_book'][$cnt]['fname'] = $values[1];
154 } else {
155 $var['field_name_value_array']['misc_address_book'][$cnt][$key] = $val;
158 $var['entry_identification_array']['misc_address_book'][$cnt] = $cnt;
159 } else {
160 if ($sections == 'lists1' && $key == 'activity') {
161 if ($val == 'Active') {
162 $val = 1;
163 } else {
164 $val = 0;
168 if ($sections == 'lists2' && $key == 'type') {
169 if (strpos($val, "-")) {
170 $vals = explode("-", $val);
171 $val = $vals[0];
172 } else {
173 $val = "";
177 if ($sections == 'prescriptions' && $key == 'active') {
178 if ($val == 'Active') {
179 $val = 1;
180 } else {
181 $val = 0;
185 $var['field_name_value_array'][$sections][$cnt][$key] = $val;
186 $var['entry_identification_array'][$sections][$cnt] = $cnt;
190 if (array_key_exists('#Type', $var['field_name_value_array']['misc_address_book'][$cnt])) {
191 unset($var['field_name_value_array']['misc_address_book'][$cnt]['#Type']);
196 $var['field_name_value_array']['documents'][0]['id'] = $doc_id;
197 insert_ccr_into_audit_data($var);
198 $d->update_imported($doc_id);
199 echo xlt('Successfully Imported the details. Please approve the patient from the Pending Approval Screen').'.';
200 } else {
201 exit(xlt('Could not read the file'));
204 exit;