fix to prior acl commit
[openemr.git] / custom / zutil.cli.doc_import.php
blobe31c128d5e248cc561a82db7c9430b8e203dde7a
1 <?php
2 /**
3 * Command-line / Unattended document import utility.
5 * Expected arguments:
6 * site - As required for all cronjobs
7 * path - $GLOBALS key specifying the path
8 * pid - patient id to be used for all selected documents
9 * category - encoded category description to assign all selected documents
10 * limit - Maximum number of files to be imported
11 * in_situ - Retain documents in current folder
13 * Examples:
14 * 1. To import received faxes:
15 * a. Set 'Scanner Directory' to network location where a fax machine can save received files
16 * b. Schedule cronjob '/usr/bin/php -f /var/www/html/emr/custom/zutil.cli.doc_import.php site=default'
17 * c. View and process received faxes using 'New Documents' menu.
19 * 2. To import collection of medical record files for a specific patient:
20 * a. Save files in Scanner Directory
21 * b. For patient nnn and category 'Patient Records', access this script online with request parameters as:
22 * zutil.cli.doc_import?site=default&pid=nnn&category=Patient+Records&limit=1000
24 * 3. Use in_situ=true option to create document records without relocating the files.
25 * This option requires good understanding of current functionality.
27 * @package OpenEMR
28 * @link http://www.open-emr.org
29 * @author MD Support <mdsupport@users.sf.net>
30 * @copyright Copyright (c) 2017 MD Support <mdsupport@users.sf.net>
31 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
34 // Allow this script to be run as a cronjob
35 require_once(dirname(__FILE__, 2)."/library/allow_cronjobs.php");
37 // Defaults
38 $arg = array(
39 'path' => 'scanner_output_directory',
40 'pid' => '00',
41 'category' => 1,
42 'owner' => null,
43 'limit' => 10,
44 'in_situ' => 0,
47 foreach ($arg as $key => $def) {
48 if (isset($_GET[$key])) {
49 $arg[$key] = $_GET[$key];
53 require_once(dirname(__FILE__, 2)."/interface/globals.php");
54 require_once("$srcdir/documents.php");
56 if (isset($GLOBALS[$arg['path']])) {
57 $arg['path'] = $GLOBALS[$arg['path']];
60 if ($arg['category'] != 1) {
61 $rec_cat = sqlQuery('SELECT id FROM categories WHERE name=?', array(urldecode($arg['category'])));
62 if (isset($rec_cat['id'])) {
63 $arg['category'] = $rec_cat['id'];
66 // Defined here as fallback
67 $ext2mime = array(
68 "pdf" => "application/pdf",
69 "exe" => "application/octet-stream",
70 "zip" => "application/zip",
71 "docx" => "application/msword",
72 "doc" => "application/msword",
73 "xls" => "application/vnd.ms-excel",
74 "ppt" => "application/vnd.ms-powerpoint",
75 "gif" => "image/gif",
76 "png" => "image/png",
77 "jpeg" => "image/jpg",
78 "jpg" => "image/jpg",
79 "mp3" => "audio/mpeg",
80 "wav" => "audio/x-wav",
81 "mpeg" => "video/mpeg",
82 "mpg" => "video/mpeg",
83 "mpe" => "video/mpeg",
84 "mov" => "video/quicktime",
85 "avi" => "video/x-msvideo",
86 "3gp" => "video/3gpp",
87 "css" => "text/css",
88 "jsc" => "application/javascript",
89 "js" => "application/javascript",
90 "php" => "text/html",
91 "htm" => "text/html",
92 "html" => "text/html"
95 printf('%s %s %s (%s)%s', xlt('Import'), text($arg['limit']), xlt('documents'), text($arg['path']), "\n");
97 $docs = new DirectoryIterator($arg['path']);
98 foreach ($docs as $doc) {
99 if ($doc->isDot()) {
100 continue;
103 $doc_pathname = $doc->getPathname();
104 $doc_url = "file://".$doc_pathname;
106 $finfo = finfo_open();
107 $str_mime = finfo_file($finfo, $doc_pathname, FILEINFO_MIME_TYPE);
108 finfo_close($finfo);
110 if (!$str_mime) {
111 $str_mime = $ext2mime[$doc->getExtension()];
114 if ($arg['in_situ']) {
115 // Skip prior documents
116 // mdsupport - Check both formats since there is no consistent code for managing urls.
117 $rec_doc = sqlQuery('SELECT id FROM documents WHERE url=? or url=?', array($doc_pathname, $doc_url));
118 if (isset($rec_doc['id'])) {
119 continue;
121 // mdsupport - This should be a standard method with DocStore variations. Until then,
122 // Create a document record for file
123 $objDoc = new Document();
124 $objDoc->set_storagemethod('0');
125 $objDoc->set_mimetype($str_mime);
126 $objDoc->set_url($doc_url);
127 $objDoc->set_size($doc->getSize());
128 $objDoc->set_hash(sha1_file($doc_pathname));
129 $objDoc->set_type($objDoc->type_array['file_url']);
130 $objDoc->set_owner('');
131 $objDoc->set_foreign_id($arg['pid']);
132 $objDoc->persist();
133 $objDoc->populate();
134 // mdsupport - Need set_category method for the Document
135 if (is_numeric($objDoc->get_id())) {
136 sqlInsert("INSERT INTO categories_to_documents(category_id, document_id) VALUES(?,?)", array($arg['category'], $objDoc->get_id()));
138 printf('%s - %s%s', text($doc_pathname), (is_numeric($objDoc->get_id()) ? text($objDoc->get_url()) : xlt('Documents setup error')), "\n");
139 } else {
140 // Too many parameters for the function make the following setup necessary for readability.
141 $doc_params = array(
142 'name' => $doc->getFilename(),
143 'mime_type' => $str_mime,
144 'full_path' => $doc_pathname,
145 'upload_error' => '',
146 'size' => $doc->getSize(),
147 'owner' => '',
148 'patient_id' => $arg['pid'],
149 'category_id' => '1',
150 'higher_level_path' => '',
151 'path_depth' => '1',
153 $new_doc = call_user_func_array('addNewDocument', $doc_params);
154 printf('%s - %s%s', text($doc_pathname), (isset($new_doc) ? text($new_doc->get_url()) : xlt('Documents setup error')), "\n");
155 if (!$new_doc) {
156 die();
157 } elseif (!unlink($doc_pathname)) {
158 printf('%s - %s', text($doc_pathname), xlt('Original file deletion error'));
159 die();
162 if (--$arg['limit'] < 1) {
163 break;