Framework  3.9
document_handler.inc
Go to the documentation of this file.
1 <?php
2 /**************************************************************
3 
4  Copyright (c) 2007-2010 Sonjara, Inc
5 
6  Permission is hereby granted, free of charge, to any person
7  obtaining a copy of this software and associated documentation
8  files (the "Software"), to deal in the Software without
9  restriction, including without limitation the rights to use,
10  copy, modify, merge, publish, distribute, sublicense, and/or sell
11  copies of the Software, and to permit persons to whom the
12  Software is furnished to do so, subject to the following
13  conditions:
14 
15  The above copyright notice and this permission notice shall be
16  included in all copies or substantial portions of the Software.
17 
18  Except as contained in this notice, the name(s) of the above
19  copyright holders shall not be used in advertising or otherwise
20  to promote the sale, use or other dealings in this Software
21  without prior written authorization.
22 
23  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
25  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
27  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
28  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
30  OTHER DEALINGS IN THE SOFTWARE.
31 
32 *****************************************************************/
33 
37 $doc_types = array( "application/msword",
38  "application/vnd.ms-excel",
39  "application/excel",
40  "application/ms-excel",
41  "application/pdf",
42  "application/zip",
43  "application/vnd.ms-powerpoint",
44  "application/ms-powerpoint",
45  "application/mspowerpoint",
46  "text/html",
47  "text/rtf",
48  "text/csv",
49  "audio/mpeg",
50  "video/flv",
51  "audio/wav",
52  );
53 
57 $doc_extensions = array(
58  ".doc" => "Microsoft Word Document",
59  ".xls" => "Microsoft Excel Spreadsheet",
60  ".ppt" => "Microsoft Powerpoint Presentation",
61  ".pdf" => "Adobe Acrobat PDF",
62  ".rtf" => "Rich Text Format",
63  ".htm" => "HTML File",
64  ".zip" => "ZIP Archive",
65  ".jpg" => "JPEG Image File",
66  ".gif" => "GIF Image File",
67  ".png" => "PNG Image File",
68  ".xlsx" => "Microsoft Excel Spreadsheet",
69  ".pptx" => "Microsoft Powerpoint Presentation",
70  ".docx" => "Microsoft Word Document",
71  ".mp3" => "MP3 Audio File",
72  ".flv" => "Flash Video",
73  ".wav" => "Audio",
74  ".csv" => "Comma-Separated Values"
75  );
76 
78  ".doc" => "application/msword",
79  ".xls" => "application/vnd.ms-excel",
80  ".ppt" => "application/vnd.ms-powerpoint",
81  ".pdf" => "application/pdf",
82  ".rtf" => "text/rtf",
83  ".htm" => "text/html",
84  ".zip" => "application/zip",
85  ".jpg" => "image/jpeg",
86  ".gif" => "image/gif",
87  ".png" => "image/x-png",
88  ".xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
89  ".pptx" => "application/vnd.openxmlformats-officedocument.presentationml.presentation",
90  ".docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
91  ".mp3" => "audio/mpeg",
92  ".m4a" => "audio/mpeg",
93  ".flv" => "video/x-flv",
94  ".wav" => "audio/wav",
95  ".csv" => "text/csv"
96 );
97 
98 $doc_icons = array( ".doc" => "/fakoli/images/msword_icon.png",
99  ".docx" => "/fakoli/images/msword_icon.png",
100  ".xls" => "/fakoli/images/msexcel_icon.png",
101  ".xlsx" => "/fakoli/images/msexcel_icon.png",
102  ".pdf" => "/fakoli/images/pdf_icon.png",
103  ".ppt" => "/fakoli/images/ppt_icon.png",
104  ".pptx" => "/fakoli/images/ppt_icon.png",
105  ".mp3" => "/fakoli/images/mp3.png",
106  ".flv" => "/fakoli/images/icon_video.png",
107  ".jpg" => "/fakoli/images/jpeg_icon.png",
108  ".jpeg" => "/fakoli/images/jpeg_icon.png",
109  ".png" => "/fakoli/images/image_icon.png",
110  "default" => "/fakoli/images/file_icon.png");
111 
112 
114 {
115  function DocumentHandler()
116  {
117 
118  }
119 
126  static function getDocType($doc)
127  {
128  global $doc_extensions;
129 
130  $d = strtolower($doc);
131 
132  $ext = substr($d, strrpos($d, "."));
133 
134  $doc_type = $doc_extensions[$ext];
135 
136  if (!$doc_type) $doc_type = "Unknown";
137 
138  return $doc_type;
139  }
140 
141 
147  static function getDocSize($doc)
148  {
149  global $config;
150 
151  // Find the file name for the file on disk
152 
153  $doc_file = $doc;
154 
155  $doc_file = str_replace($config["uploadurl"], $config["uploaddir"], $doc_file);
156 
157  // If the file exists, return its size
158 
159  if (file_exists($doc_file))
160  {
161  $doc_size = (int)(filesize($doc_file)/1024);
162  }
163  else
164  {
165  $doc_size = 0;
166  }
167 
168  return $doc_size;
169  }
170 
171 
178  function getDocMimeType($name)
179  {
180  global $doc_mime_types;
181 
182  $n = strtolower($name);
183  $extension = substr($n, strrpos($n, "."));
184  $type = $doc_mime_types[$extension];
185  if (!$type) $type="application/octet-stream";
186  return $type;
187  }
188 
189  static function getDocIcon($doc)
190  {
191  global $doc_icons;
192  $n = strtolower($doc);
193  $extension = substr($n, strrpos($n, "."));
194  $icon = $doc_icons[$extension];
195  if (!$icon) $icon = "/fakoli/images/file_icon.png";
196  return $icon;
197  }
198 
204  static function getIcon($file)
205  {
206  global $_icons;
207  global $auto_form_defaults;
208 
209  $ext = substr($file, strrpos($file, "."));
210  $icon = $_icons[$ext];
211  trace("getIcon($file) $ext $icon", 3);
212 
213  if (!isset($icon))
214  {
215  $icon = $_icons["default"];
216  }
217 
218  return $auto_form_defaults["componentPath"].$icon;
219  }
220 
221  static function getMIMEType($filename)
222  {
223  $mime_types = array(
224 
225  'txt' => 'text/plain',
226  'htm' => 'text/html',
227  'html' => 'text/html',
228  'css' => 'text/css',
229  'js' => 'application/javascript',
230  'json' => 'application/json',
231  'xml' => 'application/xml',
232  'swf' => 'application/x-shockwave-flash',
233  'flv' => 'video/x-flv',
234 
235  // images
236  'png' => 'image/png',
237  'jpe' => 'image/jpeg',
238  'jpeg' => 'image/jpeg',
239  'jpg' => 'image/jpeg',
240  'gif' => 'image/gif',
241  'bmp' => 'image/bmp',
242  'ico' => 'image/vnd.microsoft.icon',
243  'tiff' => 'image/tiff',
244  'tif' => 'image/tiff',
245  'svg' => 'image/svg+xml',
246  'svgz' => 'image/svg+xml',
247 
248  // archives
249  'zip' => 'application/zip',
250  'rar' => 'application/x-rar-compressed',
251  'exe' => 'application/x-msdownload',
252  'msi' => 'application/x-msdownload',
253  'cab' => 'application/vnd.ms-cab-compressed',
254 
255  // audio/video
256  'mp3' => 'audio/mpeg',
257  'qt' => 'video/quicktime',
258  'mov' => 'video/quicktime',
259  'mp4' => 'video/mp4',
260  'm4a' => 'audio/m4a',
261 
262  // adobe
263  'pdf' => 'application/pdf',
264  'psd' => 'image/vnd.adobe.photoshop',
265  'ai' => 'application/postscript',
266  'eps' => 'application/postscript',
267  'ps' => 'application/postscript',
268 
269  // ms office
270  'doc' => 'application/msword',
271  'rtf' => 'application/rtf',
272  'xls' => 'application/vnd.ms-excel',
273  'ppt' => 'application/vnd.ms-powerpoint',
274 
275  // open office
276  'odt' => 'application/vnd.oasis.opendocument.text',
277  'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
278  );
279 
280  $ext = strtolower(array_pop(explode('.',$filename)));
281 
282  if (array_key_exists($ext, $mime_types))
283  {
284  return $mime_types[$ext];
285  }
286  elseif (function_exists('finfo_open'))
287  {
288  $finfo = finfo_open(FILEINFO_MIME);
289  $mimetype = finfo_file($finfo, $filename);
290  finfo_close($finfo);
291  return $mimetype;
292  }
293  else
294  {
295  // Prevent other file types (such as source code or configuration files) from being served
296  die("");
297  }
298  }
299 }
300 ?>
static getDocIcon($doc)
static getMIMEType($filename)
static getDocSize($doc)
Returns the file size in Kb for a document.
static getDocType($doc)
Returns a human-readable type name for a document, based on the file extension of the supplied file n...
getDocMimeType($name)
Replaces getMimeType because name conflicts with getMIMEType.
static getIcon($file)
Retrieves a graphic icon appropriate for the given file.
$doc_extensions
Mapping of common file extensions to human-readable document type.
$doc_icons
$doc_mime_types
$doc_types
MIME types allowable for document uploads.
trace($msg, $lvl=3, $callStack=null)
Send output to the trace log.
Definition: functions.inc:1010
$_icons
Definition: functions.inc:1138