3 * Download documents from OpenEMR to the patient portal in a zip file(get_patient_documents.php)
5 * This program is used to download patient documents in a zip file in the Patient Portal.
6 * The original author did not pursue this but I thought it would be a good addition to
9 * Copyright (C) 2015-2017 Terry Hill <terry@lillysystems.com>
10 * Copyright (C) 2012 Giorgos Vasilakos <giorg.vasilakos@gmail.com>
12 * LICENSE: This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 3
15 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
24 * @author Terry Hill <terry@lilysystems.com>
25 * @author Giorgos Vasilakos <giorg.vasilakos@gmail.com>
26 * @link http://www.open-emr.org
30 require_once("verify_session.php");
31 include_once("$srcdir/documents.php");
32 require_once($GLOBALS['fileroot'] . "/controllers/C_Document.class.php");
34 // TODO: see if this can be removed (test in PHP 5 and 7)... throwing a warning
37 // get the temporary folder
38 $tmp = $GLOBALS['temporary_files_dir'];
39 // get all the documents of the patient
40 $sql = "SELECT url, id, mimetype FROM `documents` WHERE `foreign_id` = ?";
41 $fres = sqlStatement($sql, array($pid));
44 while ($file = sqlFetchArray($fres)) {
45 // find the document category
46 $sql = "SELECT name, lft, rght FROM `categories`, `categories_to_documents`
47 WHERE `categories_to_documents`.`category_id` = `categories`.`id`
48 AND `categories_to_documents`.`document_id` = ?";
49 $catres = sqlStatement($sql, array($file['id']));
50 $cat = sqlFetchArray($catres);
52 // find the tree of the documents category
53 $sql = "SELECT name FROM categories WHERE lft < ? AND rght > ? ORDER BY lft ASC";
54 $pathres = sqlStatement($sql, array($cat['lft'], $cat['rght']));
56 // create the tree of the categories
58 while ($parent = sqlFetchArray($pathres)) {
59 $path .= convert_safe_file_dir_name($parent['name'])."/";
62 $path .= convert_safe_file_dir_name($cat['name'])."/";
63 // create the folder structure at the temporary dir
64 if (!is_dir($tmp."/".$pid."/".$path)) {
65 if (!mkdir($tmp."/".$pid."/".$path, 0777, true)) {
66 echo xlt("Error creating directory!")."<br />";
71 $documentId = $file['id'];
72 $obj = new \
C_Document();
73 $document = $obj->retrieve_action("", $documentId, true, true, true);
75 $pos = strpos(substr($file['url'], -5), '.');
76 // check if has an extension or find it from the mimetype
78 $file['url'] = $file['url'].get_extension($file['mimetype']);
81 $dest = $tmp."/".$pid."/".$path."/".convert_safe_file_dir_name(basename($file['url']));
82 if (file_exists($dest)) {
85 $dest = $tmp."/".$pid."/".$path."/". $x ."_".convert_safe_file_dir_name(basename($file['url']));
87 } while (file_exists($dest));
90 file_put_contents($dest, $document);
92 echo xlt("Can't find file!")."<br />";
97 Zip($tmp."/".$pid."/", $tmp."/".$pid.'.zip');
99 // serve it to the patient
100 header('Content-type: application/zip');
101 header('Content-Disposition: attachment; filename="patient_documents.zip"');
102 readfile($tmp."/".$pid.'.zip');
104 // remove the temporary folders and files
105 recursive_remove_directory($tmp."/".$pid);
106 unlink($tmp."/".$pid.'.zip');
108 function recursive_remove_directory($directory, $empty = false)
110 if (substr($directory, -1) == '/') {
111 $directory = substr($directory, 0, -1);
114 if (!file_exists($directory) ||
!is_dir($directory)) {
116 } elseif (is_readable($directory)) {
117 $handle = opendir($directory);
118 while (false !== ($item = readdir($handle))) {
119 if ($item != '.' && $item != '..') {
120 $path = $directory.'/'.$item;
122 recursive_remove_directory($path);
130 if ($empty == false) {
131 if (!rmdir($directory)) {
141 function Zip($source, $destination)
143 if (!extension_loaded('zip') ||
!file_exists($source)) {
147 $zip = new ZipArchive();
148 if (!$zip->open($destination, ZIPARCHIVE
::CREATE
)) {
152 $source = str_replace('\\', '/', realpath($source));
153 if (is_dir($source) === true) {
154 $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator
::SELF_FIRST
);
155 foreach ($files as $file) {
156 if ($file == $source."/..") {
160 $file = str_replace('\\', '/', realpath($file));
161 if (is_dir($file) === true) {
162 $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
163 } else if (is_file($file) === true) {
164 $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
167 } else if (is_file($source) === true) {
168 $zip->addFromString(basename($source), file_get_contents($source));
171 return $zip->close();