fixed typo in security checks for prescription access
[openemr.git] / library / classes / Document.class.php
blob477b56504d9141e1370ed03de94fcc2c67a58fc6
1 <?php
3 require_once(dirname(__FILE__) . "/ORDataObject.class.php");
5 /**
6 * class Document
7 * This class is the logical representation of a physical file on some system somewhere that can be referenced with a URL
8 * of some type. This URL is not necessarily a web url, it could be a file URL or reference to a BLOB in a db.
9 * It is implicit that a document can have other related tables to it at least a one document to many notes which join on a documents
10 * id and categories which do the same.
13 class Document extends ORDataObject{
16 * Database unique identifier
17 * @var id
19 var $id;
22 * DB unique identifier reference to some other table, this is not unique in the document table
23 * @var int
25 var $foreign_id;
28 * Enumerated DB field which is met information about how to use the URL
29 * @var int can also be a the properly enumerated string
31 var $type;
34 * Array mapping of possible for values for the type variable
35 * mapping is array text name to index
36 * @var array
38 var $type_array = array();
41 * Size of the document in bytes if that is available
42 * @var int
44 var $size;
47 * Date the document was first persisted
48 * @var string
50 var $date;
53 * URL which point to the document, may be a file URL, a web URL, a db BLOB URL, or others
54 * @var string
56 var $url;
59 * Mimetype of the document if available
60 * @var string
62 var $mimetype;
65 * If the document is a multi-page format like tiff and has at least 1 page this will be 1 or greater, if a non-multi-page format this should be null or empty
66 * @var int
68 var $pages;
71 * Foreign key identifier of who initially persisited the document,
72 * potentially ownership could be changed but that would be up to an external non-document object process
73 * @var int
75 var $owner;
78 * Timestamp of the last time the document was changed and persisted, auto maintained by DB, manually change at your own peril
79 * @var int
81 var $revision;
84 * Date (YYYY-MM-DD) logically associated with the document, e.g. when a picture was taken.
85 * @var string
87 var $docdate;
90 * DB identifier reference to the lists table (the related issue), 0 if none.
91 * @var int
93 var $list_id;
95 /**
96 * Constructor sets all Document attributes to their default value
97 * @param int $id optional existing id of a specific document, if omitted a "blank" document is created
99 function Document($id = "") {
100 //call the parent constructor so we have a _db to work with
101 parent::ORDataObject();
103 //shore up the most basic ORDataObject bits
104 $this->id = $id;
105 $this->_table = "documents";
107 //load the enum type from the db using the parent helper function, this uses psuedo-class variables so it is really cheap
108 $this->type_array = $this->_load_enum("type");
110 $this->type = $this->type_array[0];
111 $this->size = 0;
112 $this->date = date("Y-m-d H:i:s");
113 $this->url = "";
114 $this->mimetype = "";
115 $this->docdate = date("Y-m-d");
116 $this->list_id = 0;
118 if ($id != "") {
119 $this->populate();
124 * Convenience function to get an array of many document objects
125 * For really large numbers of documents there is a way more efficient way to do this by overwriting the populate method
126 * @param int $foreign_id optional id use to limit array on to a specific relation, otherwise every document object is returned
128 function documents_factory($foreign_id = "") {
129 $documents = array();
131 if (empty($foreign_id)) {
132 $foreign_id= "like '%'";
134 else {
135 $foreign_id= " = '" . mysql_real_escape_string(strval($foreign_id)) . "'";
138 $d = new Document();
139 $sql = "SELECT id FROM " . $d->_table . " WHERE foreign_id " .$foreign_id ;
140 $result = $d->_db->Execute($sql);
142 while ($result && !$result->EOF) {
143 $documents[] = new Document($result->fields['id']);
144 $result->MoveNext();
147 return $documents;
151 * Convenience function to get a document object from a url
152 * Checks to see if there is an existing document with that URL and if so returns that object, otherwise
153 * creates a new one, persists it and returns it
154 * @param string $url
155 * @return object new or existing document object with the specified URL
157 function document_factory_url($url) {
158 $d = new Document();
159 //strip url handler, for now we always assume file://
160 $filename = preg_replace("|^(.*)://|","",$url);
162 if (!file_exists($filename)) {
163 die("An invalid URL was specified to crete a new document, this would only be caused if files are being deleted as you are working through the queue. '$filename'\n");
166 $sql = "SELECT id FROM " . $d->_table . " WHERE url= '" . mysql_real_escape_string($url) ."'" ;
167 $result = $d->_db->Execute($sql);
169 if ($result && !$result->EOF) {
170 if (file_exists($filename)) {
171 $d = new Document($result->fields['id']);
173 else {
174 $sql = "DELETE FROM " . $d->_table . " WHERE id= '" . $result->fields['id'] ."'";
175 $result = $d->_db->Execute($sql);
176 echo("There is a database for the file but it no longer exists on the file system. Its document entry has been deleted. '$filename'\n");
179 else {
180 $file_command = $GLOBALS['oer_config']['document']['file_command_path'] ;
181 $cmd_args = "-i ".escapeshellarg($new_path.$fname);
183 $command = $file_command." ".$cmd_args;
184 $mimetype = exec($command);
185 $mime_array = split(":", $mimetype);
186 $mimetype = $mime_array[1];
188 $d->set_mimetype($mimetype);
189 $d->url = $url;
190 $d->size = filesize($filename);
191 $d->type = $d->type_array['file_url'];
192 $d->persist();
193 $d->populate();
196 return $d;
200 * Convenience function to generate string debug data about the object
202 function toString($html = false) {
203 $string .= "\n"
204 . "ID: " . $this->id."\n"
205 . "FID: " . $this->foreign_id."\n"
206 . "type: " . $this->type . "\n"
207 . "type_array: " . print_r($this->type_array,true) . "\n"
208 . "size: " . $this->size . "\n"
209 . "date: " . $this->date . "\n"
210 . "url: " . $this->url . "\n"
211 . "mimetype: " . $this->mimetype . "\n"
212 . "pages: " . $this->pages . "\n"
213 . "owner: " . $this->owner . "\n"
214 . "revision: " . $this->revision . "\n"
215 . "docdate: " . $this->docdate . "\n"
216 . "list_id: " . $this->list_id . "\n";
218 if ($html) {
219 return nl2br($string);
221 else {
222 return $string;
226 /**#@+
227 * Getter/Setter methods used by reflection to affect object in persist/poulate operations
228 * @param mixed new value for given attribute
230 function set_id($id) {
231 $this->id = $id;
233 function get_id() {
234 return $this->id;
236 function set_foreign_id($fid) {
237 $this->foreign_id = $fid;
239 function get_foreign_id() {
240 return $this->foreign_id;
242 function set_type($type) {
243 $this->type = $type;
245 function get_type() {
246 return $this->type;
248 function set_size($size) {
249 $this->size = $size;
251 function get_size() {
252 return $this->size;
254 function set_date($date) {
255 $this->date = $date;
257 function get_date() {
258 return $this->date;
260 function set_url($url) {
261 $this->url = $url;
263 function get_url() {
264 return $this->url;
267 * this returns the url stripped down to basename
269 function get_url_web() {
270 return basename($this->url);
273 * get the url without the protocol handler
275 function get_url_filepath() {
276 return preg_replace("|^(.*)://|","",$this->url);
279 * get the url filename only
281 function get_url_file() {
282 return basename(preg_replace("|^(.*)://|","",$this->url));
285 * get the url path only
287 function get_url_path() {
288 return dirname(preg_replace("|^(.*)://|","",$this->url)) ."/";
290 function set_mimetype($mimetype) {
291 $this->mimetype = $mimetype;
293 function get_mimetype() {
294 return $this->mimetype;
296 function set_pages($pages) {
297 $this->pages = $pages;
299 function get_pages() {
300 return $this->pages;
302 function set_owner($owner) {
303 $this->owner = $owner;
305 function get_owner() {
306 return $this->owner;
309 * No getter for revision because it is updated automatically by the DB.
311 function set_revision($revision) {
312 $this->revision = $revision;
314 function set_docdate($docdate) {
315 $this->docdate = $docdate;
317 function get_docdate() {
318 return $this->docdate;
320 function set_list_id($list_id) {
321 $this->list_id = $list_id;
323 function get_list_id() {
324 return $this->list_id;
328 * Overridden function to stor current object state in the db.
329 * current overide is to allow for a just in time foreign id, often this is needed
330 * when the object is never directly exposed and is handled as part of a larger
331 * object hierarchy.
332 * @param int $fid foreign id that should be used so that this document can be related (joined) on it later
335 function persist($fid ="") {
336 if (!empty($fid)) {
337 $this->foreign_id = $fid;
339 parent::persist();
342 } // end of Document
345 $d = new Document(3);
346 $d->type = $d->type_array[1];
347 $d->url = "file:///tmp/test.gif";
348 $d->pages = 0;
349 $d->owner = 60;
350 $d->size = 8000;
351 $d->foreign_id = 25;
352 $d->persist();
353 $d->populate();
355 echo $d->toString(true);*/