<?php
/*
* Cln File Upload Class
*
* Copyright (c) 2003-4 St. Christopher House
*
* Developed by The Working Group Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* @version $Id: Cln_File_Upload.php,v 1.15 2005/01/22 23:39:30 cbooth7575 Exp $
*
*/
class Cln_File_Upload
{
/*
*
* Class Attributes: Cln_File_Upload
*
* The attributes for this class are:
*
* TBD
*
*/
var $name;
var $type;
var $passedTempName;
var $error;
var $size;
var $acceptable_extensions;
var $acceptable_types;
var $acceptable_size;
var $destinationDir;
var $seeded;
/*
*
* Function: Cln_File_Upload()
*
* Class Constructor
*
* @access public
* @return TBD
*
*/
function Cln_File_Upload($formName)
{
$this->captureFile($formName);
$this->seeded = FALSE;
}
/*
*
* Function: captureFile()
*
* Gets the file info
*
* @access public
* @return TBD
*
*/
function captureFile($formName)
{
// If the form field doesn't exist
if (!isset($_FILES[$formName])) {
PEAR::raiseError('New Image not submitted');
return FALSE;
}
$this->name = stripslashes($_FILES[$formName]['name']);
$this->type = $_FILES[$formName]['type'];
$this->passedTempName = $_FILES[$formName]['tmp_name'];
if (isset($_FILES[$formName]['error'])) {
$this->error = $_FILES[$formName]['error'];
}
else {
$this->error = '';
}
$this->size = $_FILES[$formName]['size'];
}
/*
*
* Function: validate()
*
* Validates, sets errors and returns T or F
*
* @access public
* @return TBD
*
*/
function validate()
{
$success = TRUE;
// Is a file there? If not, just return right away
if ($this->name == '') {
PEAR::raiseError('No file was submitted.', E_USER_WARNING);
$success = FALSE;
}
// Else, a file is there
else {
// Are there any PHP errors?
if (!empty($this->error)) {
PEAR::raiseError('There was an upload error. It\'s possible that you are trying to upload a file that is too big.', E_USER_WARNING);
$success = FALSE;
}
// Is it a valid type? (if types are set)
if (count($this->acceptable_types) > 0 && !in_array($this->type, $this->acceptable_types)) {
PEAR::raiseError('The file you submitted was not a valid type: '.$this->type, E_USER_WARNING);
$success = FALSE;
}
// If a size is set, is this smaller?
if (isset($this->acceptable_size) && $this->size > $this->acceptable_size) {
PEAR::raiseError('The file you submitted was larger than the space you have available ({$this->acceptable_size} bytes). If you wish to upload this file, you will need to clear enough room. You may have to talk to your system administrator for this..', E_USER_WARNING);
$success = FALSE;
}
// Is there a destinationDir set?
if (empty($this->destinationDir)) {
PEAR::raiseError('There is no destination upload directory set.', E_USER_WARNING);
$success = FALSE;
}
// Else, is the destinationDir writable?
else if (!is_writable($this->destinationDir)) {
PEAR::raiseError("The destination directory {$this->destinationDir} is not writable.", E_USER_WARNING);
$success = FALSE;
}
// If not errors yet, generate a name, and move the file to the tempDir
if ($success) {
$this->name = $this->generateFinalName();
$this->moveFile();
}
}
return $success;
}
/*
*
* Function: generateFinalName()
*
* Generates a uniquefinal
*
* @access public
* @return TBD
*
*/
function generateFinalName()
{
$this->fixName();
return $this->getDestinationName($this->destinationDir);
}
/*
*
* Function: moveFile()
*
* Moves the file to the destination_dir
*
* @access public
* @return TBD
*
*/
function moveFile()
{
d('Cln_File_Upload: moveFile name: ' . $this->name, 5);
//$this->name = 'tmp_' . $this->name;
$dest = $this->destinationDir . $this->name;
$source = $this->passedTempName;
copy ($source, $dest);
}
/*
*
* Function: fixName()
*
* Returns a filename with any problem characters removed
*
* @access public
* @return TBD
*
*/
function fixName()
{
$noalpha = 'áéíóúàèìòùäëïöüÁÉÍÓÚÀÈÌÒÙÄËÏÖÜâêîôûÂÊÎÔÛñçÇ@';
$alpha = 'aeiouaeiouaeiouAEIOUAEIOUAEIOUaeiouAEIOUncCa';
$maxlen = 100;
$this->name = substr ($this->name, 0, $maxlen);
$this->name = strtr ($this->name, $noalpha, $alpha);
$this->name = ereg_replace ('[^a-zA-Z0-9,._\+\()\-]', '_', $this->name);
return TRUE;
}
/*
*
* Function: getDestinationName()
*
* Returns a complete file path, checking if the file exists or not
*
* @access public
* @return TBD
*
*/
function getDestinationName($path, $num = '')
{
$parsedFile = $this->parseExtension();
$copy = '';
$n = '';
while(file_exists($path . $parsedFile['base'] . $copy . '.' . $parsedFile['ext'])) {
$copy = '_copy' . $n;
$n++;
}
$this->name = $parsedFile['base'] . $copy . '.' . $parsedFile['ext'];
return $this->name;
}
/*
*
* Function: parseExtension()
*
* Returns an array, with the first element the filename, and the second the extension
*
* @access public
* @return TBD
*
*/
function parseExtension()
{
$fileArray = explode('.', $this->name);
if (count($fileArray) == 1) {
$parsedFile['base'] = $fileArray[0];
$parsedFile['ext'] = '';
}
elseif (count($fileArray) >= 2) {
$parsedFile['ext'] = array_pop($fileArray);
$parsedFile['base'] = implode('.', $fileArray);
}
else {
$parsedFile = FALSE;
}
return $parsedFile;
}
}
?>