folder = ''; $this->isRequired = false; $this->allowedExtensions = array(); $this->autoRename = false; $this->minSize= 1; $this->maxSize=-1; $this->error = 0; $this->errorType = array(); $this->develErrorMessage = array(); } /** * setter. set the fileinfo array * @param string $fileInfo the name of the form field; * @return nothing; * @access public */ function setFileInfo($fileInfo) { if (isset($_FILES[$fileInfo])) { $this->fileInfo = $_FILES[$fileInfo]; } else { $this->setError('PHP_UPLOAD_NO_UPLOAD', array(), array($fileInfo)); if (count($_POST) == 0) { $this->setError('PHP_UPLOAD_NO_POST', array(), array()); } $file_uploads = @ini_get('file_uploads'); if (strlen(trim($file_uploads)) == 0) { $this->setError('PHP_UPLOAD_UPLOAD_DISABLED', array(), array()); } } } /** * setter. set the destination folder * @param string $folder the name of the folder; * @return nothing; * @access public */ function setFolder($folder) { $this->folder = $folder; if (strtolower(substr(PHP_OS, 0, 1))=='w') { $this->folder = str_replace('/', '\\', $this->folder); } else { $this->folder = str_replace('\\', '/', $this->folder); } } /** * setter. * @param boolean $isRequired the name of the form field; * @return nothing; * @access public */ function setRequired($isRequired) { $this->isRequired = $isRequired; } /** * setter. set the allowed extendions * @param array ; * @return nothing; * @access public */ function setAllowedExtensions($allowedExtensions) { $this->allowedExtensions = $allowedExtensions; } /** * setter. * @param boolean ; * @return nothing; * @access public */ function setAutoRename($autoRename) { $this->autoRename = $autoRename; } /** * setter. * @param integer the minimum size of the files in KB; * @return nothing; * @access public */ function setMinSize($minSize) { $this->minSize = $minSize; } /** * setter. * @param integer the maximum size of the file in KB; * @return nothing; * @access public */ function setMaxSize($maxSize) { $this->maxSize = $maxSize; } /** * Handle the uploaded file by moving it to a destination file. * The destination file = folder + fileName * @param string fileName the name for saving the uploaded file; * @param string $oldFileName the previous file name, or null on insert * @return string file name if succeded or null if not; * @access public */ function uploadFile($fileName, $oldFileName="") { if ($this->hasError()) { return; } $this->checkUpload(); $this->checkFolder(); $this->checkSize(); $this->checkExtensions(); $this->checkFileName($fileName); if ($this->hasError()) { return; } if ($this->fileExists) { $folder = KT_realpath($this->folder); $fileName = KT_replaceSpecialChars($fileName, 'filter'); $destinationName = KT_realpath($folder . $fileName, false); if (file_exists($destinationName)) { if (strtolower($fileName) != strtolower($oldFileName)) { // if the destination file really exists if (!$this->autoRename) { $this->setError('UPLOAD_FILE_EXISTS', array(), array($fileName)); return; } else { $destinationName = $this->getTempName($destinationName); } } } if ($oldFileName!='') { @unlink($folder . DIRECTORY_SEPARATOR . $oldFileName); } if (!@move_uploaded_file($this->fileInfo['tmp_name'], $destinationName)) { unlink($this->fileInfo['tmp_name']); $this->setError('PHP_UPLOAD_MOVE_TMP_ERROR', array(), array()); return; } else { $arr = split("[\\/]", $destinationName); $this->destinationName = $destinationName; KT_setFilePermissions($this->destinationName); return array_pop($arr); } } } /** * Gets a temporary file name starting from the given file name. * Ex: filename = 'zone.jpg', and zone_1.jpg alreagy exists, the new file name is zone_2.jpg. * @param string $filename the filename on which to create the new temporary file name. * @return string the temporary file name; * @access public */ function getTempName($filename) { $pos = strrpos($filename,'.'); if ($pos !== false) { $tmpName = substr($filename,0,$pos); $tmpExt = substr($filename, $pos); } else { $tmpName = $filename; $tmpExt = ''; } $i=1; while (file_exists($tmpName.'_'.$i.$tmpExt) && $i < 1000) { $i++; } $retVar = $tmpName.'_'.$i.$tmpExt; return $retVar; } /** * Check if the destination folder exists and has write permissions. * If the folder does not exists, try to create it. * If the folder does not have write permissions or if could not create it, set error. * @return nothing; * @access public */ function checkFolder() { if ($this->fileExists) { $folder = new KT_folder(); $folder->createFolder($this->folder); $right = $folder->checkRights($this->folder, 'write'); if ($folder->hasError()) { $arr = $folder->getError(); $this->setError('PHP_UPLOAD_FOLDER_ERROR', array($arr[0]), array($arr[1])); } if ($right !== true) { $this->setError('PHP_UPLOAD_CHECK_FOLDER_ERROR', array(), array($this->folder)); } } } /** * Checks the size of the uploaded file. * The size is checked against the this.minSize and this.maxSize * @return nothing; * @access public */ function checkSize() { if ($this->fileExists) { if ($this->minSize >0 && $this->fileInfo['size'] < $this->minSize) { $this->setError('UPLOAD_CHECK_SIZE_S', array($this->minSize-1), array($this->minSize-1)); return; } if ($this->maxSize >0 && $this->fileInfo['size'] > $this->maxSize * 1024) { $this->setError('UPLOAD_CHECK_SIZE_G', array($this->maxSize), array($this->maxSize)); return; } } } /** * Checks the type of the uploaded file. * The name is checked against the this.allowedTypes array. * @return nothing; * @access public */ function checkExtensions() { if ($this->fileExists) { $path_info = KT_pathinfo($this->fileInfo['name']); foreach ($this->allowedExtensions as $key => $extension) { if (strtolower($extension) == strtolower($path_info['extension'])) { return; } } $this->setError('UPLOAD_EXT_NOT_ALLOWED', array(), array($path_info['extension'])); } } /** * check if a filename is valid. invalid chars: .., \, /, :, *, ?, ", <, >, | * @param string $filename the name of the file; * @return nothing; * @access public */ function checkFileName($fileName) { if ($this->fileExists) { $arrInvalid = array('..','\\','/',':','*','?','"','<','>','|'); if ((string)$fileName == '') { $this->setError('PHP_UPLOAD_FILENAME_EMPTY', array(), array()); return; } foreach($arrInvalid as $key => $tmp) { if (strpos($fileName,$tmp) !== false) { $this->setError('PHP_UPLOAD_FILENAME_INV', array(), array($tmp)); return; } } } } /** * Checks if the upload has performed OK and if the file is required. * @return nothing; * @access public */ function checkUpload() { if ($this->fileInfo['tmp_name'] == '') { $this->fileExists = false; if (isset($this->fileInfo['error']) && !is_array($this->fileInfo['error']) && $this->fileInfo['error'] >0) { switch ($this->fileInfo['error']){ case 1: $this->setError('PHP_UPLOAD_ERR_INI_SIZE', array(), array()); return; case 2: $this->setError('PHP_UPLOAD_ERR_FORM_SIZE', array(), array()); return; case 3: $this->setError('PHP_UPLOAD_ERR_PARTIAL', array(), array()); return; case 4: break; case 6: $this->setError('PHP_UPLOAD_ERR_NO_