another minor fix to prior commit
[openemr.git] / patients / get_patient_documents.php
blobcdd336faa9a6a0034d95a0457eef0e6dd2c7fdf3
1 <?php
2 /*
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
7 * the patient portal
8 *
9 * Copyright (C) 2015 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>;.
23 * @package OpenEMR
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");
33 use C_Document;
35 // get the temporary folder
36 $tmp = $GLOBALS['temporary_files_dir'];
37 // get all the documents of the patient
38 $sql = "SELECT url, id, mimetype FROM `documents` WHERE `foreign_id` = ?";
39 $fres = sqlStatement($sql, array($pid));
41 // for every document
42 while ($file = sqlFetchArray($fres)) {
43 // find the document category
44 $sql = "SELECT name, lft, rght FROM `categories`, `categories_to_documents`
45 WHERE `categories_to_documents`.`category_id` = `categories`.`id`
46 AND `categories_to_documents`.`document_id` = ?";
47 $catres = sqlStatement($sql, array($file['id']));
48 $cat = sqlFetchArray($catres);
50 // find the tree of the documents category
51 $sql = "SELECT name FROM categories WHERE lft < ? AND rght > ? ORDER BY lft ASC";
52 $pathres = sqlStatement($sql, array($cat['lft'], $cat['rght']));
54 // create the tree of the categories
55 $path = "";
56 while ($parent = sqlFetchArray($pathres)) {
57 $path .= convert_safe_file_dir_name($parent['name'])."/";
59 $path .= convert_safe_file_dir_name($cat['name'])."/";
60 // create the folder structure at the temporary dir
61 if (!is_dir($tmp."/".$pid."/".$path)) {
62 if (!mkdir($tmp."/".$pid."/".$path, 0777, true )){
63 echo xlt("Error creating directory!")."<br />";
67 // copy the document
68 $documentId = $file['id'];
69 $obj = new \C_Document();
70 $document = $obj->retrieve_action("", $documentId, true, true, true);
71 if ($document) {
72 $pos = strpos(substr($file['url'], -5), '.');
73 // check if has an extension or find it from the mimetype
74 if ($pos === false) {
75 $file['url'] = $file['url'].get_extension($file['mimetype']);
77 $dest = $tmp."/".$pid."/".$path."/".convert_safe_file_dir_name(basename($file['url']));
78 if (file_exists($dest)) {
79 $x = 1;
80 do {
81 $dest = $tmp."/".$pid."/".$path."/". $x ."_".convert_safe_file_dir_name(basename($file['url']));
82 $x++;
83 } while (file_exists($dest));
85 file_put_contents($dest,$document);
87 else {
88 echo xlt("Can't find file!")."<br />";
92 // zip the folder
93 Zip($tmp."/".$pid."/", $tmp."/".$pid.'.zip');
95 // serve it to the patient
96 header('Content-type: application/zip');
97 header('Content-Disposition: attachment; filename="patient_documents.zip"');
98 readfile($tmp."/".$pid.'.zip');
100 // remove the temporary folders and files
101 recursive_remove_directory($tmp."/".$pid);
102 unlink($tmp."/".$pid.'.zip');
104 function recursive_remove_directory($directory, $empty=FALSE) {
105 if(substr($directory,-1) == '/') {
106 $directory = substr($directory,0,-1);
108 if(!file_exists($directory) || !is_dir($directory)) {
109 return FALSE;
110 } elseif(is_readable($directory)) {
111 $handle = opendir($directory);
112 while (FALSE !== ($item = readdir($handle))) {
113 if($item != '.' && $item != '..') {
114 $path = $directory.'/'.$item;
115 if(is_dir($path)) {
116 recursive_remove_directory($path);
117 } else {
118 unlink($path);
122 closedir($handle);
123 if($empty == FALSE) {
124 if(!rmdir($directory)) {
125 return FALSE;
129 return TRUE;
133 function Zip($source, $destination) {
134 if (!extension_loaded('zip') || !file_exists($source)) {
135 return false;
137 $zip = new ZipArchive();
138 if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
139 return false;
141 $source = str_replace('\\', '/', realpath($source));
142 if (is_dir($source) === true) {
143 $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
144 foreach ($files as $file) {
145 if($file == $source."/..")
146 continue;
147 $file = str_replace('\\', '/', realpath($file));
148 if (is_dir($file) === true) {
149 $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
151 else if (is_file($file) === true) {
152 $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
156 else if (is_file($source) === true) {
157 $zip->addFromString(basename($source), file_get_contents($source));
159 return $zip->close();