PhysicalDocumentManager.inc
6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/**
*
* Class PhysicalDocumentManager.inc
*
* Contains all functions required to upload, alter and
* delete a physical document.
*
* Form variables that will be required:
* o When calling uploadDocument:
* o $fDescription - description of uploaded file
* o $fFolderID - primary key of folder into which file will be placed
* o When calling deleteDocument:
*
* @author Rob Cherry, Jam Warehouse (Pty) Ltd, South Africa
* @date 13 January 2003
* @package lib.documentmanagement
*/
class PhysicalDocumentManager {
/**
* Upload and store a new document
*
* @param Array containing file information. Must match $_FILES exactly
* @param Primary key of folder in which document will be stored
* @param Document description (should be passed through as a form variable)
* @param Temporary path of file on server (get from $aFileArray['fFile']['tmp_name'])
*
* @return boolean true on successful upload and storage, false otherwise and set $_SESSION["errorMessage"]
*
* @todo add URL functionality
*/
/*function uploadPhysicalDocument($aFileArray, $iFolderID, $sDescription, $sTmpFilePath) {
global $lang_fileexists, $lang_err_upload, $lang_err_database;
//create the document object
$oDocument = & PhysicalDocumentManager::createDocumentFromUploadedFile($aFileArray, $iFolderID);
$oDocument->setDescription($sDescription);
//find the path on the system where the document should be stored
$sDocumentFileSystemPath = Folder::getFolderPath($oDocument->getFolderID()) . "/" . $oDocument->getName();
//copy the file accross
if (copy($sTmpFilePath, $sDocumentFileSystemPath)) {
//create the object in the database
if ($oDocument->create()) {
//remove the temporary file
unlink($sTmpFilePath);
return true;
} else {
//if object creation failed, delete the file on the system
$_SESSION["errorMessage"] = $lang_err_database;
unlink($sDocumentFileSystemPath);
//remove the temporary file as well
unlink($sTmpFilePath);
return false;
}
} else {
$_SESSION["errorMessage"] = $lang_err_upload;
return false;
}
}*/
function uploadPhysicalDocument($oDocument, $iFolderID, $sDescription, $sTmpFilePath) {
global $lang_fileexists, $lang_err_upload, $lang_err_database;
//find the path on the system where the document should be stored
$sDocumentFileSystemPath = Folder::getFolderPath($oDocument->getFolderID()) . "/" . $oDocument->getName();
//copy the file accross
if (copy($sTmpFilePath, $sDocumentFileSystemPath)) {
//remove the temporary file
unlink($sTmpFilePath);
return true;
} else {
$_SESSION["errorMessage"] = $lang_err_upload;
return false;
}
}
/**
* Stream a document to a client over http
*
* @param Primary key of document to stream
*
* @return int number of bytes read from file on success or false otherwise;
*
* @todo investigate possible problem in MSIE 5.5 concerning Content-Disposition header
*/
function downloadPhysicalDocument($iDocumentID) {
//get the document
$oDocument = & Document::get($iDocumentID);
//get the path to the document on the server
$sDocumentFileSystemPath = Folder::getFolderPath($oDocument->getFolderID()) . "/" . $oDocument->getFileName();
//set the correct headers
if (strstr($_SERVER["HTTP_USER_AGENT"], "MSIE")) {
Header ("Content-type: application/" . PhysicalDocumentManager::getMimeTypeName($oDocument->getMimeTypeID()));
} else {
Header ("Content-type: application/octect-stream");
}
Header("Content-Length: ". $oDocument->getFileSize());
Header("Content-Disposition: attachment; filename=" . $oDocument->getFileName());
Header("Pragma: no-cache");
Header("Expires: 0");
readfile($sDocumentFileSystemPath);
}
/**
* View a document using an inline viewer
*
* @param Primary key of document to view
*
* @return int number of bytes read from file on success or false otherwise;
*
* @todo investigate possible problem in MSIE 5.5 concerning Content-Disposition header
*/
function inlineViewPhysicalDocument($iDocumentID) {
//get the document
$oDocument = & Document::get($iDocumentID);
//get the path to the document on the server
$sDocumentFileSystemPath = Folder::getFolderPath($oDocument->getFolderID()) . "/" . $oDocument->getName();
$total=$url . $files[$i];
if (strstr($_SERVER["HTTP_USER_AGENT"], "MSIE")) {
Header ("Content-type: application/" . PhysicalDocumentManager::getMimeTypeName($oDocument->getMimeTypeID()));
} else {
Header ("Content-type: application/octect-stream");
}
Header("Content-Length: " . $oDocument->getFileSize() * 1024);
Header("Content-Disposition: inline; filename=" . $oDocument->getName());
return readfile($sDocumentFileSystemPath);
}
/**
* Get the uploaded file information and place it into a document object
*
* @param Array containing uploaded file information (use $aFileArray)
* par Primary key of folder into which document will be placed
*
* @return Document Document object containing uploaded file information
*/
function & createDocumentFromUploadedFile($aFileArray, $iFolderID) {
//get the uploaded document information and put it into a document object
//$oDocument = & new Document($aFileArray['fFile']['name'], $aFileArray['fFiler']['name'], $aFileArray['fFile']['size'], $_SESSION["userID"], PhysicalDocumentManager::getMimeTypeID($aFileArray['fFile']['type']), $iFolderID);
$oDocument = & new Document($aFileArray['name'], $aFileArray['name'], $aFileArray['size'], $_SESSION["userID"], PhysicalDocumentManager::getMimeTypeID($aFileArray['type']), $iFolderID);
return $oDocument;
}
/**
* Get the mime type primary key for a specific mime type
*
* @param mime type
*
* @return int mime type primary key if found, else default mime type primary key (text/plain)
*/
function getMimeTypeID($sName) {
global $default;
$sql = $default->db;
//get the mime type
if (isset($sName)) {
$sql->query("SELECT id FROM " . $default->owl_mime_table . " WHERE mimetypes = '$sName'");
if ($sql->next_record()) {
//get the mime type id
return $sql->f("id");
}
}
//otherwise return the default mime type
return PhysicalDocumentManager::getDefaultMimeTypeID();
}
/**
* Get the default mime type, which is text/plain
*
* @return int default mime type
*
*/
function getDefaultMimeTypeID() {
global $default;
$sql = $default->db;
$sql->query("SELECT id FROM " . $default->owl_mime_table . " WHERE mimetypes = 'text/plain'");
$sql->next_record();
//get the mime type id
return $sql->f("id");
}
function getMimeTypeName($iMimeTypeID) {
global $default;
$sql = $default->db;
$sql->query("SELECT mimetypes FROM " . $default->owl_mime_table . " WHERE id = " . $iMimeTypeID);
if ($sql->next_record()) {
return $sql->f("mimetypes");
}
return "text/plain";
}
}
?>