src.dualinventive.com/mtinfo/dist/webroot/rc-4.05/include/document.php

351 lines
10 KiB
PHP

<?php
/** \file include\document.php
* \brief DI webinterface document functions
* \author Rob Schalken, Core|Vision
* \version 1.0
* \date 17-10-2008
*
* This file contains the upload/download document functions
*/
/**
* Valid document types (extensions of file types that can be uploaded)
*/
global $_VALID_EXT_UPLOAD;
$_VALID_EXT_UPLOAD = array(
"pdf",
"doc", "docx", "rtf", "xls", "xlsx", "ppt", "pptx",
"csv", "txt",
"html",
"mp3", "mp4", "m4a", "wma", "mpg", "flv", "avi", "mov", "jpg", "jpeg", "png", "gif"
);
/**
* upload document
*
* Inputs:
* - $file_array $_FILES are
* - $id id of file input object
*
* Return: array containing document/mime-type/filename, or FALSE on error
*/
function upload_document($file_array, $id = "filename", $allowed_ext = "") {
global $_PAGE_INFO;
// Initial value
$file = FALSE;
// Remove previous errors
unset($_PAGE_INFO['errormsg']);
if (strlen($file_array[$id]['name'])) {
if ($file_array[$id]['error']) {
// Show alert
$_PAGE_INFO['errormsg']['type'] = "alert";
// Define error
switch($file_array[$id]['error']) {
case 1:
$_PAGE_INFO['errormsg']['text'] = sprintf(_("The file \"%s\" is bigger than %d bytes."), $file_array[$id]['name'], ini_get("upload_max_filesize"));
break;
case 2:
$_PAGE_INFO['errormsg']['text'] = sprintf(_("The file \"%s\" is too big."), $file_array[$id]['name']);
break;
case 3:
$_PAGE_INFO['errormsg']['text'] = sprintf(_("The file \"%s\" is partly received."), $file_array[$id]['name']);
break;
case 4:
$_PAGE_INFO['errormsg']['text'] = sprintf(_("The file \"%s\" does not exist."), $file_array[$id]['name']);
break;
default:
$_PAGE_INFO['errormsg']['text'] = _("Error");
break;
}
}
else {
// Check file size
if ((filesize($file_array[$id]['tmp_name']) < MAX_FILE_SIZE) && (filesize($file_array[$id]['tmp_name']))) {
// Initial value
$ext_ok = 1;
// Check extension
$ext = strtolower(substr(strrchr($file_array[$id]['name'], "."), 1));
if (is_array($allowed_ext)) {
if (!in_array($ext, $allowed_ext)) {
$ext_ok = 0;
}
}
if ($ext_ok) {
$file = array();
// Retrieve file content
$fp = fopen($file_array[$id]['tmp_name'], 'r');
$file['document'] = fread($fp, filesize($file_array[$id]['tmp_name']));
fclose($fp);
// It seems that Mozilla SeaMonkey and Firefox have a bug that set the MIME type for PDFs
// to "text/plain".
if( $ext == "pdf" && substr($file['document'], 0, 4) == "%PDF" ) {
// huh? bug in Mozilla SeaMonkey? (and perhaps others...)
$file['mimetype'] = "application/pdf";
}
else $file['mimetype'] = $file_array[$id]['type'];
$file[$id] = $file_array[$id]['name'];
}
else {
$_PAGE_INFO['errormsg']['type'] = "alert";
$_PAGE_INFO['errormsg']['text'] = _("Invalid extension") . " (" . implode(", ", $allowed_ext) . ")";
}
}
else {
$_PAGE_INFO['errormsg']['type'] = "alert";
$_PAGE_INFO['errormsg']['text'] = _("Invalid file size");
}
}
}
return $file;
}
/**
* upload multiple documents/files
*
* Inputs:
* - $file_array $_FILES are
*
* Return: array containing document/mime-type/filename, or FALSE on error
*/
function upload_multiple_document($file_array, $id = "filename", $allowed_ext = "") {
global $_PAGE_INFO;
// Initial value
$files = FALSE;
// Remove previous errors
unset($_PAGE_INFO['errormsg']);
if (is_array($file_array[$id]['tmp_name'])) {
// Check for errors
for($i=0; $i<sizeof($file_array[$id]['tmp_name']); $i++) {
// Initial value
$error = "";
// Handle error/download file
switch($file_array[$id]['error'][$i]) {
case 0:
// Everything ok! => File
if ((isset($file_array[$id]['tmp_name'][$i])) && (filesize($file_array[$id]['tmp_name'][$i]) < MAX_FILE_SIZE) && (filesize($file_array[$id]['tmp_name'][$i]))) {
// Initial value
$ext_ok = 1;
// Check extension
$ext = strtolower(substr(strrchr($file_array[$id]['name'][$i], "."), 1));
if (is_array($allowed_ext)) {
if (!in_array($ext, $allowed_ext)) {
$ext_ok = 0;
}
}
if ($ext_ok) {
$file = array();
// Retrieve file content
$fp = fopen($file_array[$id]['tmp_name'][$i], 'r');
$file['document'] = fread($fp, filesize($file_array[$id]['tmp_name'][$i]));
fclose($fp);
// It seems that Mozilla SeaMonkey and Firefox have a bug that set the MIME type for PDFs
// to "text/plain".
if( $ext == "pdf" && substr($file['document'], 0, 4) == "%PDF" ) {
// huh? bug in Mozilla SeaMonkey? (and perhaps others...)
$file['mimetype'] = "application/pdf";
}
else {
$file['mimetype'] = $file_array[$id]['type'][$i];
}
$file['name'] = $file_array[$id]['name'][$i];
$file['tmp_name'] = $file_array[$id]['tmp_name'][$i];
// Store values
if (!is_array($files)) {
$files = array();
}
array_push($files, $file);
}
else {
$error = sprintf(_("The file \"%s\" has an invalid extension"), $file_array[$id]['name'][$i]) . " (" . implode(", ", $allowed_ext) . ").";
}
}
else {
$error = sprintf(_("The file \"%s\" has an invalid size."), $file_array[$id]['name'][$i]);
}
break;
case 1:
$error = sprintf(_("The file \"%s\" is bigger than %d bytes."), $file_array[$id]['name'][$i], ini_get("upload_max_filesize"));
break;
case 2:
$error = sprintf(_("The file \"%s\" is too big."), $file_array[$id]['name'][$i]);
break;
case 3:
$error = sprintf(_("The file \"%s\" is partly received."), $file_array[$id]['name'][$i]);
break;
case 4:
$error = sprintf(_("The file \"%s\" does not exist."), $file_array[$id]['name'][$i]);
break;
default:
$error = sprintf(_("The file \"%s\" has an undefined error."), $file_array[$id]['name'][$i]);
break;
}
if (strlen($error)) {
// Add LF/CR to error handling?
if (isset($_PAGE_INFO['errormsg']['text'])) {
$_PAGE_INFO['errormsg']['text'] .= "<br><br>";
}
// Define error message
$_PAGE_INFO['errormsg']['text'] .= $error;
// Show alert
$_PAGE_INFO['errormsg']['type'] = "alert";
}
}
}
return $files;
}
/**
* download document
*
* Inputs:
* - $file_id file db id, or array containing document data
*
*/
function download_document($file_id, $doc_table = NULL, $inline = FALSE) {
if( is_numeric($file_id) && !is_null($doc_table) ) {
// Fetch file info
$file = db_fetch_file($file_id, $doc_table, TRUE);
}
else if( is_array($file_id) ) {
$file = $file_id;
}
else {
trigger_error("Bad function call", E_USER_ERROR);
return FALSE;
}
if( $file === FALSE || !$file['document'] ) {
DBG("File does not exist; id=" . $file_id . " in table \"" . $doc_table . "\"");
$_PAGE_INFO['errormsg']['type'] = "alert";
$_PAGE_INFO['errormsg']['text'] = _("File does not exist");
return FALSE;
}
// Download file (pop-up open/save as)
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: private");
header("Content-length: " . strlen($file['document']) . "");
header("Content-transfer-encoding: binary");
header("Content-type: " . $file['mimetype'] . "");
if ($inline) {
header("Content-disposition: inline; target:_blank; filename=\"" . $file['filename'] . "\"");
}
else {
header("Content-disposition: attachment; filename=\"" . $file['filename'] . "\"");
}
echo $file['document'];
return TRUE;
}
/**
* download document header (split up download function!)
*
* Inputs:
* - $mimetype File mimetype
* - $filename Filename
* - $action Download/write
*
*/
function download_document_header($mimetype, $filename, $action = "download", $handle = "temp_file", $inline = FALSE) {
GLOBAL $_PAGE_INFO;
if ($action == "download") {
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: private");
header("Content-transfer-encoding: binary");
header("Content-type: " . $mimetype . "");
if ($inline) {
header("Content-disposition: inline; target:_blank; filename=\"" . $filename . "\"");
}
else {
header("Content-disposition: attachment; filename=\"" . $filename . "\"");
}
}
else {
// Initial values
$file = RemoveExtension($filename);
$ext = GetExtension($filename);
$counter = 0;
$valid_file = 0;
do {
$tmp = session_save_path() . "/" . $file;
if ($counter) {
$tmp .= "(" . $counter .")";
}
$tmp .= $ext;
if (file_exists($tmp)) {
$valid_file = 0;
$counter++;
}
else {
$valid_file = 1;
}
} while(!$valid_file);
// Store filename
$_PAGE_INFO[$handle] = $tmp;
$fh = fopen($_PAGE_INFO[$handle], "w");
fclose($fh);
}
}
/**
* download document data
*
* Inputs:
* - $data Data
* - $action Download/write
*
*/
function download_document_data($data, $action = "download", $handle = "temp_file") {
GLOBAL $_PAGE_INFO;
if ($action == "download") {
echo $data;
}
else {
if ((isset($_PAGE_INFO[$handle])) && (file_exists($_PAGE_INFO[$handle]))) {
$fh = fopen($_PAGE_INFO[$handle], "a");
fwrite($fh, $data);
fclose($fh);
}
}
}
?>