forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_patient_documents.php
159 lines (137 loc) · 5.44 KB
/
get_patient_documents.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/**
* Download documents from OpenEMR to the patient portal in a zip file(get_patient_documents.php)
*
* This program is used to download patient documents in a zip file in the Patient Portal.
* The original author did not pursue this but I thought it would be a good addition to
* the patient portal
*
* @package OpenEMR
* @link https://www.open-emr.org
* @author Giorgos Vasilakos <[email protected]>
* @author Terry Hill <[email protected]>
* @author Stephen Waite <[email protected]>
* @copyright Copyright (c) 2012 Giorgos Vasilakos <[email protected]>
* @copyright Copyright (c) 2015-2017 Terry Hill <[email protected]>
* @copyright Copyright (c) 2019 Stephen Waite <[email protected]>
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
*/
require_once("verify_session.php");
require_once("$srcdir/documents.php");
require_once($GLOBALS['fileroot'] . "/controllers/C_Document.class.php");
// get the temporary folder
$tmp = $GLOBALS['temporary_files_dir'];
// get all the documents of the patient
$sql = "SELECT url, id, mimetype FROM `documents` WHERE `foreign_id` = ?";
$fres = sqlStatement($sql, array($pid));
// for every document
while ($file = sqlFetchArray($fres)) {
// find the document category
$sql = "SELECT name, lft, rght FROM `categories`, `categories_to_documents`
WHERE `categories_to_documents`.`category_id` = `categories`.`id`
AND `categories_to_documents`.`document_id` = ?";
$catres = sqlStatement($sql, array($file['id']));
$cat = sqlFetchArray($catres);
// find the tree of the documents category
$sql = "SELECT name FROM categories WHERE lft < ? AND rght > ? ORDER BY lft ASC";
$pathres = sqlStatement($sql, array($cat['lft'], $cat['rght']));
// create the tree of the categories
$path = "";
while ($parent = sqlFetchArray($pathres)) {
$path .= convert_safe_file_dir_name($parent['name'])."/";
}
$path .= convert_safe_file_dir_name($cat['name'])."/";
// create the folder structure at the temporary dir
if (!is_dir($tmp."/".$pid."/".$path)) {
if (!mkdir($tmp."/".$pid."/".$path, 0777, true)) {
echo xlt("Error creating directory!")."<br />";
}
}
// copy the document
$documentId = $file['id'];
$obj = new \C_Document();
$document = $obj->retrieve_action("", $documentId, true, true, true);
if ($document) {
$pos = strpos(substr($file['url'], -5), '.');
// check if has an extension or find it from the mimetype
if ($pos === false) {
$file['url'] = $file['url'].get_extension($file['mimetype']);
}
$dest = $tmp."/".$pid."/".$path."/".convert_safe_file_dir_name(basename($file['url']));
if (file_exists($dest)) {
$x = 1;
do {
$dest = $tmp."/".$pid."/".$path."/". $x ."_".convert_safe_file_dir_name(basename($file['url']));
$x++;
} while (file_exists($dest));
}
file_put_contents($dest, $document);
} else {
echo xlt("Can't find file!")."<br />";
}
}
// zip the folder
Zip($tmp."/".$pid."/", $tmp."/".$pid.'.zip');
// serve it to the patient
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="patient_documents.zip"');
readfile($tmp."/".$pid.'.zip');
// remove the temporary folders and files
recursive_remove_directory($tmp."/".$pid);
unlink($tmp."/".$pid.'.zip');
function recursive_remove_directory($directory, $empty = false)
{
if (substr($directory, -1) == '/') {
$directory = substr($directory, 0, -1);
}
if (!file_exists($directory) || !is_dir($directory)) {
return false;
} elseif (is_readable($directory)) {
$handle = opendir($directory);
while (false !== ($item = readdir($handle))) {
if ($item != '.' && $item != '..') {
$path = $directory.'/'.$item;
if (is_dir($path)) {
recursive_remove_directory($path);
} else {
unlink($path);
}
}
}
closedir($handle);
if ($empty == false) {
if (!rmdir($directory)) {
return false;
}
}
}
return true;
}
function Zip($source, $destination)
{
if (!extension_loaded('zip') || !file_exists($source)) {
return false;
}
$zip = new ZipArchive();
if (!$zip->open($destination, ZipArchive::CREATE)) {
return false;
}
$source = str_replace('\\', '/', realpath($source));
if (is_dir($source) === true) {
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file) {
if ($file == $source."/..") {
continue;
}
$file = str_replace('\\', '/', realpath($file));
if (is_dir($file) === true) {
$zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
} else if (is_file($file) === true) {
$zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
}
}
} else if (is_file($source) === true) {
$zip->addFromString(basename($source), file_get_contents($source));
}
return $zip->close();
}