language = strtolower($language);
$this->error = '';
}
////
// If a file has already been uploaded, this returns the tmp_name
// which is the filename of where PHP stored the file.
// If no file was uploaded or there was an error retrieving the name
// an empty string is returned.
////
function getTempName(){
$retVal = "";
if(isset($this->file['tmp_name'])){
$retVal = $this->file['tmp_name'];
}
return $retVal;
} // emd getTempName()
function getExtention(){
$retVal = "";
if(isset($this->file['extention'])){
$retVal = $this->file['extention'];
}
return $retVal;
}
/**
* void max_filesize ( int size);
*
* Set the maximum file size in bytes ($size), allowable by the object.
* NOTE: PHP's configuration file also can control the maximum upload size, which is set to 2 or 4
* megs by default. To upload larger files, you'll have to change the php.ini file first.
*
* @param size (int) file size in bytes
*/
function max_filesize($size){
$this->max_filesize = (int) $size;
}
/**
* void max_image_size ( int width, int height );
*
* Sets the maximum pixel dimensions. Will only be checked if the
* uploaded file is an image
*
* @param width (int) maximum pixel width of image uploads
* @param height (int) maximum pixel height of image uploads
*/
function max_image_size($width, $height){
$this->max_image_width = (int) $width;
$this->max_image_height = (int) $height;
}
/**
* bool upload (string filename[, string accept_type[, string extension]]);
*
* Checks if the file is acceptable and uploads it to PHP's default upload diretory
*
* @param filename (string) form field name of uploaded file
* @param accept_type (string) acceptable mime-types
* @param extension (string) default filename extenstion
*/
function upload($filename='', $accept_type='', $extention='') {
$this->acceptable_file_types = trim($accept_type); // used by error messages
if (!isset($_FILES) || !isset($_FILES[$filename]) ||!is_array($_FILES[$filename]) || !$_FILES[$filename]['name']) {
$this->error = $this->get_error(0);
$this->accepted = FALSE;
return FALSE;
}
// Copy PHP's global $_FILES array to a local array
$this->file = $_FILES[$filename];
$this->file['file'] = $filename;
// Initialize empty array elements
if (!isset($this->file['extention'])) $this->file['extention'] = "";
if (!isset($this->file['type'])) $this->file['type'] = "";
if (!isset($this->file['size'])) $this->file['size'] = "";
if (!isset($this->file['width'])) $this->file['width'] = "";
if (!isset($this->file['height'])) $this->file['height'] = "";
if (!isset($this->file['tmp_name'])) $this->file['tmp_name'] = "";
if (!isset($this->file['raw_name'])) $this->file['raw_name'] = "";
// test max size
// NOTE: FILESIZE LIMIT IGNORED NOW - SWC 20060518 (TODO: REINSTATE THIS BEFORE RELEASING MOTIVE_FS)
//if($this->max_filesize && ($this->file["size"] > $this->max_filesize)) {
// $this->error = $this->get_error(1);
// $this->accepted = FALSE;
// return FALSE;
//}
if(stristr($this->file["type"], "image")) {
/* IMAGES */
$image = getimagesize($this->file["tmp_name"]);
$this->file["width"] = $image[0];
$this->file["height"] = $image[1];
// test max image size
if(($this->max_image_width || $this->max_image_height) && (($this->file["width"] > $this->max_image_width) || ($this->file["height"] > $this->max_image_height))) {
$this->error = $this->get_error(2);
$this->accepted = FALSE;
return FALSE;
}
// Image Type is returned from getimagesize() function
switch($image[2]) {
case 1:
$this->file["extention"] = ".gif"; break;
case 2:
$this->file["extention"] = ".jpg"; break;
case 3:
$this->file["extention"] = ".png"; break;
case 4:
$this->file["extention"] = ".swf"; break;
case 5:
$this->file["extention"] = ".psd"; break;
case 6:
$this->file["extention"] = ".bmp"; break;
case 7:
$this->file["extention"] = ".tif"; break;
case 8:
$this->file["extention"] = ".tif"; break;
default:
$this->file["extention"] = $extention; break;
}
} elseif(!ereg("(\.)([a-z0-9]{3,5})$", $this->file["name"]) && !$extention) {
// Try to autmatically figure out the file type
// For more on mime-types: http://httpd.apache.org/docs/mod/mod_mime_magic.html
switch($this->file["type"]) {
case "text/plain":
$this->file["extention"] = ".txt"; break;
case "text/richtext":
$this->file["extention"] = ".txt"; break;
default:
break;
}
} else {
$this->file["extention"] = $extention;
}
// check to see if the file is of type specified
if($this->acceptable_file_types) {
if(trim($this->file["type"]) && (stristr($this->acceptable_file_types, $this->file["type"]) || stristr($this->file["type"], $this->acceptable_file_types)) ) {
$this->accepted = TRUE;
} else {
$this->accepted = FALSE;
if($this->file["type"] == ""){
$this->error = $this->get_error(6); // file too big
} else {
$this->error = $this->get_error(3); // file-type problem
logEvent("Mimetype rejected during upload: ".$this->file["type"], 2);
}
}
} else {
$this->accepted = TRUE;
}
return (bool) $this->accepted;
}
/**
* bool save_file ( string path[, int overwrite_mode] );
*
* Cleans up the filename, copies the file from PHP's temp location to $path,
* and checks the overwrite_mode
*
* @param path (string) File path to your upload directory
* @param overwrite_mode (int) 1 = overwrite existing file (UPLOAD_MODE_OVERWRITE)
* 2 = rename if filename already exists (file.txt becomes file_copy0.txt) (UPLOAD_MODE_RENAME)
* 3 = do nothing if a file exists (UPLOAD_MODE_ABORT)
// If it can't move the file, it deletes the original file to keep the tmp directory clean.
*/
function save_file($path, $overwrite_mode=UPLOAD_MODE_ABORT, $destFileName='', $origFileName=''){
if ($this->error) {
return false;
}
// If the original filename wasn't specified, use where PHP had put it.
if($origFileName == ""){
$origFileName = $this->file["tmp_name"];
}
if($destFileName != ""){
$this->file["name"] = $destFileName;
}
if (strlen($path)>0) {
if ($path[strlen($path)-1] != "/") {
$path = $path . "/";
}
}
$this->path = $path;
$copy = "";
$n = 1;
$success = false;
if($this->accepted) {
// Clean up file name (only lowercase letters, numbers, underscores and slashes for directories)
$this->file["name"] = ereg_replace("[^a-z0-9._\/#]", "", str_replace(" ", "_", str_replace("%20", "_", strtolower($this->file["name"]))));
// Clean up text file line-breaks
if(stristr($this->file["type"], "text")) {
$this->cleanup_text_file($origFileName);
}
// get the raw name of the file (without its extenstion)
if(ereg("(\.)([a-z0-9]{2,5})$", $this->file["name"])) {
$pos = strrpos($this->file["name"], ".");
if(!$this->file["extention"]) {
$this->file["extention"] = substr($this->file["name"], $pos, strlen($this->file["name"]));
}
$this->file['raw_name'] = substr($this->file["name"], 0, $pos);
} else {
$this->file['raw_name'] = $this->file["name"];
if ($this->file["extention"]) {
$this->file["name"] = $this->file["name"] . $this->file["extention"];
}
}
switch((int) $overwrite_mode) {
case UPLOAD_MODE_OVERWRITE: // overwrite mode
if (@copy($origFileName, $this->path . $this->file["name"])) {
$success = true;
} else {
$success = false;
$this->error = $this->get_error(5);
}
break;
case UPLOAD_MODE_RENAME: // create new with incremental extention
while(file_exists($this->path . $this->file['raw_name'] . $copy . $this->file["extention"])) {
$copy = "_copy" . $n;
$n++;
}
$this->file["name"] = $this->file['raw_name'] . $copy . $this->file["extention"];
if (@copy($origFileName, $this->path . $this->file["name"])) {
$success = true;
} else {
$success = false;
$this->error = $this->get_error(5);
}
break;
case UPLOAD_MODE_ABORT: // continues to default
default: // do nothing if exists, highest protection
if(file_exists($this->path . $this->file["name"])){
$this->error = $this->get_error(4);
$success = false;
} else {
if (@copy($origFileName, $this->path . $this->file["name"])) {
$success = true;
} else {
$success = false;
$this->error = $this->get_error(5);
}
}
break;
}
if(!$success) { unset($origFileName); }
return (bool) $success;
} else {
$this->error = $this->get_error(3);
return FALSE;
}
}
/**
* string get_error(int error_code);
*
* Gets the correct error message for language set by constructor
*
* @param error_code (int) error code
*/
function get_error($error_code='') {
// SWC 20060518 - Removed other languages.
$error_code = (int) $error_code;
$error_message = array();
$error_message[0] = "No file was uploaded";
$error_message[1] = "Maximum file size exceeded. File may be no larger than " . $this->max_filesize/1000 . " KB (" . $this->max_filesize . " bytes).";
$error_message[2] = "Maximum image size exceeded. Image may be no more than " . $this->max_image_width . " x " . $this->max_image_height . " pixels.";
$error_message[3] = "Only " . str_replace(",", ", ", $this->acceptable_file_types) . " files may be uploaded.";
$error_message[4] = "File '" . $this->path . $this->file["name"] . "' already exists.";
$error_message[5] = "Permission denied. Unable to copy file to '" . $this->path . "'";
$error_message[6] = "File is too large. You see... the internet isn't a big truck, it's a series of tubes.";
return $error_message[$error_code];
}
/**
* void cleanup_text_file (string file);
*
* Convert Mac and/or PC line breaks to UNIX by opening
* and rewriting the file on the server
*
* @param file (string) Path and name of text file
*/
function cleanup_text_file($file){
// chr(13) = CR (carridge return) = Macintosh
// chr(10) = LF (line feed) = Unix
// Win line break = CRLF
$new_file = '';
$old_file = '';
$fcontents = file($file);
while (list ($line_num, $line) = each($fcontents)) {
$old_file .= $line;
$new_file .= str_replace(chr(13), chr(10), $line);
}
if ($old_file != $new_file) {
// Open the uploaded file, and re-write it with the new changes
$fp = fopen($file, "w");
fwrite($fp, $new_file);
fclose($fp);
}
}
} // end class 'MotiveUploader'
/*
// SWC 20060518 - Cut. See original file "fileupload-class.php" for readme info.
///// fileupload-class.php /////
Copyright (c) 1999, 2002, 2003 David Fox, Angryrobot Productions
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
3. Neither the name of author nor the names of its contributors
may be used to endorse or promote products derived from this
software without specific prior written permission.
DISCLAIMER:
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
*/
?>