csrf ongoing work (#1803)
[openemr.git] / library / ajax / ccr_import_ajax.php
blob16baae3aec869af4e5101ff33916654048d4ddb9
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 //verify csrf
32 if (!verifyCsrfToken($_GET["csrf_token_form"])) {
33 die(xlt('Authentication Error'));
36 if ($_REQUEST["ccr_ajax"] == "yes") {
37 $doc_id = $_REQUEST["document_id"];
38 $d = new Document($doc_id);
39 $url = $d->get_url();
40 $storagemethod = $d->get_storagemethod();
41 $couch_docid = $d->get_couch_docid();
42 $couch_revid = $d->get_couch_revid();
43 if ($storagemethod == 1) {
44 $couch = new CouchDB();
45 $data = array($GLOBALS['couchdb_dbase'],$couch_docid);
46 $resp = $couch->retrieve_doc($data);
47 $content = $resp->data;
48 if ($content=='' && $GLOBALS['couchdb_log']==1) {
49 $log_content = date('Y-m-d H:i:s')." ==> Retrieving document\r\n";
50 $log_content = date('Y-m-d H:i:s')." ==> URL: ".$url."\r\n";
51 $log_content .= date('Y-m-d H:i:s')." ==> CouchDB Document Id: ".$couch_docid."\r\n";
52 $log_content .= date('Y-m-d H:i:s')." ==> CouchDB Revision Id: ".$couch_revid."\r\n";
53 $log_content .= date('Y-m-d H:i:s')." ==> Failed to fetch document content from CouchDB.\r\n";
54 $log_content .= date('Y-m-d H:i:s')." ==> Will try to download file from HardDisk if exists.\r\n\r\n";
55 $this->document_upload_download_log($d->get_foreign_id(), $log_content);
56 die(xlt("File retrieval from CouchDB failed"));
59 $content = base64_decode($content);
60 } else {
61 $url = preg_replace("|^(.*)://|", "", $url);
62 $from_all = explode("/", $url);
63 $from_filename = array_pop($from_all);
64 $from_pathname_array = array();
65 for ($i=0; $i<$d->get_path_depth(); $i++) {
66 $from_pathname_array[] = array_pop($from_all);
69 $from_pathname_array = array_reverse($from_pathname_array);
70 $from_pathname = implode("/", $from_pathname_array);
71 $temp_url = $GLOBALS['OE_SITE_DIR'] . '/documents/' . $from_pathname . '/' . $from_filename;
72 if (!file_exists($temp_url)) {
73 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;
74 } else {
75 $content = file_get_contents($temp_url);
79 //fields to which the corresponding elements are to be inserted
80 //format - level 1 key is the main tag in the XML eg:- //Problems or //Problems/Problem according to the content in the XML.
81 //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
82 //eg:- 'Type/Text' if the XML format is '//Problems/Problem/Type/Text' or 'id/@extension' if it is an attribute
83 //level 2 key can be 'table name:#some value' for checking whether a particular tag exits in the XML section
84 $field_mapping = array(
85 '//Problems/Problem' => array(
86 'lists1:diagnosis' => 'Description/Code/Value',
87 'lists1:comments' => 'CommentID',
88 'lists1:activity' => 'Status/Text',
90 '//Alerts/Alert' => array(
91 'lists2:type' => 'Type/Text',
92 'lists2:diagnosis' => 'Description/Code/Value',
93 'lists2:date' => 'Agent/EnvironmentalAgents/EnvironmentalAgent/DateTime/ExactDateTime',
94 'lists2:title' => 'Agent/EnvironmentalAgents/EnvironmentalAgent/Description/Text',
95 'lists2:reaction' => 'Reaction/Description/Text',
97 '//Medications/Medication' => array(
98 'prescriptions:date_added' => 'DateTime/ExactDateTime',
99 'prescriptions:active' => 'Status/Text',
100 'prescriptions:drug' => 'Product/ProductName/Text',
101 'prescriptions:size' => 'Product/Strength/Value',
102 'prescriptions:unit' => 'Product/Strength/Units/Unit',
103 'prescriptions:form' => 'Product/Form/Text',
104 'prescriptions:quantity' => 'Quantity/Value',
105 'prescriptions:note' => 'PatientInstructions/Instruction/Text',
106 'prescriptions:refills' => 'Refills/Refill/Number',
108 '//Immunizations/Immunization' => array(
109 'immunizations:administered_date' => 'DateTime/ExactDateTime',
110 'immunizations:note' => 'Directions/Direction/Description/Text',
112 '//Results/Result' => array(
113 'procedure_result:date' => 'DateTime/ExactDateTime',
114 'procedure_type:name' => 'Test/Description/Text',
115 'procedure_result:result' => 'Test/TestResult/Value',
116 'procedure_result:range' => 'Test/NormalResult/Normal/Value',
117 'procedure_result:abnormal' => 'Test/Flag/Text',
119 '//Actors/Actor' => array(
120 'patient_data:fname' => 'Person/Name/CurrentName/Given',
121 'patient_data:lname' => 'Person/Name/CurrentName/Family',
122 'patient_data:DOB' => 'Person/DateOfBirth/ExactDateTime',
123 'patient_data:sex' => 'Person/Gender/Text',
124 'patient_data:abname' => 'InformationSystem/Name',
125 'patient_data:#Type' => 'InformationSystem/Type',
126 'patient_data:pubpid' => 'IDs/ID',
127 'patient_data:street' => 'Address/Line1',
128 'patient_data:city' => 'Address/City',
129 'patient_data:state' => 'Address/State',
130 'patient_data:postal_code' => 'Address/PostalCode',
131 'patient_data:phone_contact' => 'Telephone/Value',
134 if (!empty($content)) {
135 $var = array();
136 $res = parseXmlStream($content, $field_mapping);
137 $var = array(
138 'approval_status' => 1,
139 'type' => 11,
140 'ip_address' => $_SERVER['REMOTE_ADDR'],
142 foreach ($res as $sections => $details) {
143 foreach ($details as $cnt => $vals) {
144 foreach ($vals as $key => $val) {
145 if (array_key_exists('#Type', $res[$sections][$cnt])) {
146 if ($key == 'postal_code') {
147 $var['field_name_value_array']['misc_address_book'][$cnt]['zip'] = $val;
148 } elseif ($key == 'phone_contact') {
149 $var['field_name_value_array']['misc_address_book'][$cnt]['phone'] = $val;
150 } elseif ($key == 'abname') {
151 $values = explode(' ', $val);
152 if ($values[0]) {
153 $var['field_name_value_array']['misc_address_book'][$cnt]['lname'] = $values[0];
156 if ($values[1]) {
157 $var['field_name_value_array']['misc_address_book'][$cnt]['fname'] = $values[1];
159 } else {
160 $var['field_name_value_array']['misc_address_book'][$cnt][$key] = $val;
163 $var['entry_identification_array']['misc_address_book'][$cnt] = $cnt;
164 } else {
165 if ($sections == 'lists1' && $key == 'activity') {
166 if ($val == 'Active') {
167 $val = 1;
168 } else {
169 $val = 0;
173 if ($sections == 'lists2' && $key == 'type') {
174 if (strpos($val, "-")) {
175 $vals = explode("-", $val);
176 $val = $vals[0];
177 } else {
178 $val = "";
182 if ($sections == 'prescriptions' && $key == 'active') {
183 if ($val == 'Active') {
184 $val = 1;
185 } else {
186 $val = 0;
190 $var['field_name_value_array'][$sections][$cnt][$key] = $val;
191 $var['entry_identification_array'][$sections][$cnt] = $cnt;
195 if (array_key_exists('#Type', $var['field_name_value_array']['misc_address_book'][$cnt])) {
196 unset($var['field_name_value_array']['misc_address_book'][$cnt]['#Type']);
201 $var['field_name_value_array']['documents'][0]['id'] = $doc_id;
202 insert_ccr_into_audit_data($var);
203 $d->update_imported($doc_id);
204 echo xlt('Successfully Imported the details. Please approve the patient from the Pending Approval Screen').'.';
205 } else {
206 exit(xlt('Could not read the file'));
209 exit;