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