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
10 * @author Terry Hill <terry@lilysystems.com>
11 * @author Giorgos Vasilakos <giorg.vasilakos@gmail.com>
12 * @copyright Copyright (C) 2015 Terry Hill <terry@lillysystems.com>
13 * @copyright Copyright (C) 2012 Giorgos Vasilakos <giorg.vasilakos@gmail.com>
14 * @link http://www.open-emr.org
15 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
18 require_once("verify_session.php");
19 include_once("$srcdir/documents.php");
20 require_once($GLOBALS['fileroot'] . "/controllers/C_Document.class.php");
22 // get the temporary folder
23 $tmp = $GLOBALS['temporary_files_dir'];
24 // get all the documents of the patient
25 $sql = "SELECT url, id, mimetype FROM `documents` WHERE `foreign_id` = ?";
26 $fres = sqlStatement($sql, array($pid));
29 while ($file = sqlFetchArray($fres)) {
30 // find the document category
31 $sql = "SELECT name, lft, rght FROM `categories`, `categories_to_documents`
32 WHERE `categories_to_documents`.`category_id` = `categories`.`id`
33 AND `categories_to_documents`.`document_id` = ?";
34 $catres = sqlStatement($sql, array($file['id']));
35 $cat = sqlFetchArray($catres);
37 // find the tree of the documents category
38 $sql = "SELECT name FROM categories WHERE lft < ? AND rght > ? ORDER BY lft ASC";
39 $pathres = sqlStatement($sql, array($cat['lft'], $cat['rght']));
41 // create the tree of the categories
43 while ($parent = sqlFetchArray($pathres)) {
44 $path .= convert_safe_file_dir_name($parent['name'])."/";
47 $path .= convert_safe_file_dir_name($cat['name'])."/";
48 // create the folder structure at the temporary dir
49 if (!is_dir($tmp."/".$pid."/".$path)) {
50 if (!mkdir($tmp."/".$pid."/".$path, 0777, true)) {
51 echo xlt("Error creating directory!")."<br />";
56 $documentId = $file['id'];
57 $obj = new \
C_Document();
58 $document = $obj->retrieve_action("", $documentId, true, true, true);
60 $pos = strpos(substr($file['url'], -5), '.');
61 // check if has an extension or find it from the mimetype
63 $file['url'] = $file['url'].get_extension($file['mimetype']);
66 $dest = $tmp."/".$pid."/".$path."/".convert_safe_file_dir_name(basename($file['url']));
67 if (file_exists($dest)) {
70 $dest = $tmp."/".$pid."/".$path."/". $x ."_".convert_safe_file_dir_name(basename($file['url']));
72 } while (file_exists($dest));
75 file_put_contents($dest, $document);
77 echo xlt("Can't find file!")."<br />";
82 Zip($tmp."/".$pid."/", $tmp."/".$pid.'.zip');
84 // serve it to the patient
85 header('Content-type: application/zip');
86 header('Content-Disposition: attachment; filename="patient_documents.zip"');
87 readfile($tmp."/".$pid.'.zip');
89 // remove the temporary folders and files
90 recursive_remove_directory($tmp."/".$pid);
91 unlink($tmp."/".$pid.'.zip');
93 function recursive_remove_directory($directory, $empty = false)
95 if (substr($directory, -1) == '/') {
96 $directory = substr($directory, 0, -1);
99 if (!file_exists($directory) ||
!is_dir($directory)) {
101 } elseif (is_readable($directory)) {
102 $handle = opendir($directory);
103 while (false !== ($item = readdir($handle))) {
104 if ($item != '.' && $item != '..') {
105 $path = $directory.'/'.$item;
107 recursive_remove_directory($path);
115 if ($empty == false) {
116 if (!rmdir($directory)) {
126 function Zip($source, $destination)
128 if (!extension_loaded('zip') ||
!file_exists($source)) {
132 $zip = new ZipArchive();
133 if (!$zip->open($destination, ZIPARCHIVE
::CREATE
)) {
137 $source = str_replace('\\', '/', realpath($source));
138 if (is_dir($source) === true) {
139 $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator
::SELF_FIRST
);
140 foreach ($files as $file) {
141 if ($file == $source."/..") {
145 $file = str_replace('\\', '/', realpath($file));
146 if (is_dir($file) === true) {
147 $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
148 } else if (is_file($file) === true) {
149 $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
152 } else if (is_file($source) === true) {
153 $zip->addFromString(basename($source), file_get_contents($source));
156 return $zip->close();