<?php
class ESDFileupload
{
	var $_allowed = array('image/pjpeg','image/jpeg','image/x-png','image/png','image/gif');
	
	function ESDFileupload($updir,$fhidden='',$divid,$resize='',$ending='',$path)
	{
		$error = "";
		$msg = "";
		
		$updir = $path;
		
		$fileElementName = 'fileToUpload_' . $divid;
		
		if(!empty($_FILES[$fileElementName]['error']))
		{
			switch($_FILES[$fileElementName]['error'])
			{
	
				case '1':$error = $GLOBALS['config_vars']['Upload_ToBig1'];break;
				case '2':$error = $GLOBALS['config_vars']['Upload_ToBig2'];break;
				case '3':$error = $GLOBALS['config_vars']['Upload_The uploaded file was only partially uploaded'];break;
				case '4':$error = $GLOBALS['config_vars']['Upload_NoFile'];break;
				case '6':$error = $GLOBALS['config_vars']['Upload_Missing_TFolder'];break;
				case '7':$error = $GLOBALS['config_vars']['Upload_Failed_Disk'];break;
				case '8':$error = $GLOBALS['config_vars']['Upload_File_StoppedExtension'];break;
				case '999':default:$error = 'No error code avaiable';break;
			}
		} elseif(empty($_FILES[$fileElementName]['tmp_name']) || $_FILES[$fileElementName]['tmp_name'] == 'none') {
			$error = $GLOBALS['config_vars']['Upload_NoFile'];
		} else {
			$new_name = preg_replace('/([^-A-Za-zÀ-ÖØ-öø-ÿ0-9_.]*)/','',$_FILES[$fileElementName]['name']);
			
			$f_check     = trim(strtolower($_FILES[$fileElementName]['name']));
			$forbidden_1 = strtolower(substr($f_check, strlen($f_check) - 4));
			$forbidden_2 = strtolower(substr($f_check, strlen($f_check) - 5));
			
			if(($forbidden_1 != 'jpeg' && $forbidden_2 != 'pjpeg') && ($forbidden_1 != '.php' && $forbidden_2 != '.php4'))
			{
				
				if(!@move_uploaded_file($_FILES[$fileElementName]['tmp_name'], BASE_DIR . $updir . $new_name))
				{
					$msg .= $GLOBALS['config_vars']['Upload_Failed_Disk'];
				} else {
					$msg .= $GLOBALS['config_vars']['Upload_Succes'] . '(' . $new_name . ') ';
				}
			} else {
				$error = $GLOBALS['config_vars']['Upload_ForbiddenFile'];
			}
		}
		
		$_REQUEST['noout'] = 1;
			echo "{";
			echo				"error: '" . $error . "',\n";
			echo				"msg: '". @$msg . "',\n";
			echo				"html: '" . @$new_name . "'\n";
			echo "}";
	}
}
?>