151 lines
4.4 KiB
PHP
151 lines
4.4 KiB
PHP
<?php
|
|
// File name: upload.inc.php
|
|
// Description: PHP subroutines to handle file uploads
|
|
// Date: 2004-07-06
|
|
// Author: imacat <imacat@pristine.com.tw>
|
|
// Copyright: Copyright (C) 2004-2007 Pristine Communications
|
|
|
|
// Set the include path
|
|
if (!defined("INCPATH_SET")) {
|
|
require_once dirname(__FILE__) . "/incpath.inc.php";
|
|
}
|
|
// Referenced subroutines
|
|
require_once "monica/chkfunc.inc.php";
|
|
require_once "monica/gettext.inc.php";
|
|
require_once "monica/htmlchar.inc.php";
|
|
require_once "monica/newsn.inc.php";
|
|
require_once "monica/mimetype.inc.php";
|
|
require_once "monica/requri.inc.php";
|
|
|
|
// Settings
|
|
if (!defined("UPLOAD_DEPOSIT_MAX")) {
|
|
define("UPLOAD_DEPOSIT_MAX", 20971520);
|
|
}
|
|
$FILE_TYPESUF_MAP = array(
|
|
"application/vnd.oasis.opendocument.text" => "odt",
|
|
"application/vnd.oasis.opendocument.spreadsheet" => "ods",
|
|
"application/vnd.oasis.opendocument.presentation" => "odp",
|
|
"application/vnd.sun.xml.writer" => "sxw",
|
|
"application/vnd.sun.xml.calc" => "sxc",
|
|
"application/vnd.sun.xml.impress" => "sxi",
|
|
"application/msword" => "doc",
|
|
"application/vnd.ms-excel" => "xls",
|
|
"application/vnd.ms-powerpoint" => "ppt",
|
|
"image/png" => "png",
|
|
"image/jpeg" => "jpg",
|
|
"image/gif" => "gif",
|
|
"application/pdf" => "pdf",
|
|
"application/x-zip" => "zip");
|
|
|
|
// suspend_file: Suspend a file to $_SESSION
|
|
function suspend_file($file)
|
|
{
|
|
$FILES =& file_deposit();
|
|
// Generate a new random file ID
|
|
$fileid = new_sn_assoc($FILES);
|
|
$file["sn"] = $fileid;
|
|
// Save the current file
|
|
$FILES[$fileid] =& $file;
|
|
// Return the file ID
|
|
return $fileid;
|
|
}
|
|
|
|
// savefile_exists: Check if a file exists
|
|
function savefile_exists($sn)
|
|
{
|
|
// Check the validity of the serial number first
|
|
if (!check_sn($sn)) {
|
|
return false;
|
|
}
|
|
if (!array_key_exists($sn, file_deposit())) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// echo_file: Output a file element
|
|
function echo_file($file)
|
|
{
|
|
$lines = array();
|
|
$lines[] = sprintf(h(C_("MIME file type: %s")), h($file["type"]));
|
|
$lines[] = sprintf(h(C_("File name: %s")), h($file["name"]));
|
|
$lines[] = sprintf(h(C_("File size: %s bytes")), h($file["size"]));
|
|
echo implode("<br />\n ", $lines);
|
|
return;
|
|
}
|
|
|
|
// file_deposit: Return the file deposit
|
|
function &file_deposit()
|
|
{
|
|
// Session in use
|
|
if (isset($_SESSION)) {
|
|
if (!array_key_exists("savefile", $_SESSION)) {
|
|
$_SESSION["savefile"] = array();
|
|
}
|
|
return $_SESSION["savefile"];
|
|
|
|
// Session not in use
|
|
} else {
|
|
static $FILES = array();
|
|
return $FILES;
|
|
}
|
|
}
|
|
|
|
// clear_file_deposit: Clear the file deposit
|
|
function clear_file_deposit()
|
|
{
|
|
$FILES =& file_deposit();
|
|
$FILES = array();
|
|
return;
|
|
}
|
|
|
|
// filecache: Return an appropriate fule cache
|
|
function &filecache($table)
|
|
{
|
|
$FILES =& file_deposit();
|
|
// Package specified
|
|
$package = defined("PACKAGE")? PACKAGE: REQUEST_HOST;
|
|
if (!array_key_exists("_cache", $FILES)) {
|
|
$FILES["_cache"] = array();
|
|
}
|
|
if (!array_key_exists($package, $FILES["_cache"])) {
|
|
$FILES["_cache"][$package] = array();
|
|
}
|
|
if (!array_key_exists($table, $FILES["_cache"][$package])) {
|
|
$FILES["_cache"][$package][$table] = array();
|
|
}
|
|
return $FILES["_cache"][$package][$table];
|
|
}
|
|
|
|
// readfile_content: Read a file from its content into the file deposit
|
|
// and return the fileid
|
|
function readfile_content($data, $filename, $sugtype, $sn, $table)
|
|
{
|
|
$FILES =& file_deposit();
|
|
$filecache =& filecache($table);
|
|
|
|
// Cached
|
|
if ( array_key_exists($sn, $filecache)
|
|
&& $data == $FILES[$filecache[$sn]]["content"]) {
|
|
$fileid = $filecache[$sn];
|
|
|
|
} else {
|
|
// Create the file
|
|
$file = array();
|
|
$file["content"] = $data;
|
|
$file["type"] = check_mime_type_content($data, $filename, $sugtype);
|
|
$file["size"] = strlen($data);
|
|
if (!is_null($filename)) {
|
|
$file["name"] = $filename;
|
|
}
|
|
|
|
// Suspend the created picture
|
|
$fileid = suspend_file($file);
|
|
$filecache[$sn] = $fileid;
|
|
}
|
|
|
|
return $fileid;
|
|
}
|
|
|
|
?>
|