AAAAget( 'upload_maxsize', '100000' );
$upload_filetypes = $params->get( 'upload_filetypes', '' );
$upload_fileexist = $params->get( 'upload_fileexist', '' );
$upload_email = $params->get( 'upload_email', '' );
$upload_emailmsg = $params->get( 'upload_emailmsg', '0' );
$upload_emailhtml = $params->get( 'upload_emailhtml', '1' );
$upload_unzip = $params->get( 'upload_unzip', '0' );
$upload_showerrmsg = $params->get( 'upload_showerrmsg', '1' );
$upload_showdircontent = $params->get( 'upload_showdircontent', '0' );
$upload_popshowpath = $params->get( 'upload_popshowpath', '1' );
$upload_popshowbytes = $params->get( 'upload_popshowbytes', '0' );
$upload_blacklist = $params->get( 'upload_blacklist', '.php .php3 .php4 .php5 .php6 .phtml .pl .py .jsp .asp .htm .shtml .sh .cgi .exe .bat .cmd .htaccess' );
$upload_doubleext = $params->get( 'upload_doubleext', '1' );
$upload_phpext = $params->get( 'upload_phpext', '1' );
$upload_gifcomment = $params->get( 'upload_gifcomment', '1' );
$upload_mailfrom = $params->get( 'upload_mailfrom' , 'noreply@simplefileupload.com' );
$upload_maximgwidth = $params->get( 'upload_maximgwidth', '0' );
$upload_maximgheight = $params->get( 'upload_maximgheight', '0' );
$upload_compressimg = $params->get( 'upload_compressimg', '' );
$upload_disablegdlib = $params->get( 'upload_disablegdlib', '0' );
$upload_disablegdthreshold = $params->get( 'upload_diablegdthreshold', '0' );
$upload_thumbcreate = $params->get( 'upload_thumbcreate', '0' );
$upload_thumbsize = $params->get( 'upload_thumbsize', '40x40' );
$upload_thumbname = $params->get( 'upload_thumbname', 'sfuthumb' );
$upload_debug = $params->get( 'upload_debug', '0' );
$upload_formfields = $params->get( 'upload_formfields', '' );
$upload_useformsfields = $params->get( 'upload_useformsfields', '0' );
if ($upload_useformsfields == 0)
$upload_formfields = "";
$upload_formfieldsfile = $params->get( 'upload_formfieldsfile', '' );
$upload_formfieldsdiv = $params->get( 'upload_formfieldsdiv', '|' );
$upload_nohtmlencoding = $params->get( 'upload_nohtmlencoding', '0' );
$upload_replacetag = $params->get( 'upload_replacetag', '0' );
$results = "";
$fileCnt = 0;
$fileErr = 0;
$written = 0;
$filename = "";
$fileList = "";
$fileInfo = "";
$filetypeok = true;
$filetype = "";
$blacklist = explode(" ", $upload_blacklist);
$formfieldsval = array();
$formfieldsemail = array();
$chkfileexist = "";
$baseurl = "";
$serverurl = "";
$protocol = "";
$protocol = "http://";
if (substr($_SERVER["HTTP_REFERER"], 0, 5) === "https") $protocol = "https://";
$folder = substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], "/"));
if ($folder === "//") $folder = "";
// Check if relative path
if (substr($upload_location, 0, 1) === ".") {
$serverurl .= str_replace(".", $protocol.$_SERVER["HTTP_HOST"].$folder, $upload_location);
// Fix Windows path...
$baseurl .= str_replace("\\", "", $serverurl);
} else {
if ((substr($upload_location, 1, 2) === ":\\") || (substr($upload_location, 0, 1) === "/")) {
// Server root path
$baseurl = "file://".str_replace("\\", "/", $upload_location);
} else {
$serverurl = str_replace("\\", "/", $_SERVER["DOCUMENT_ROOT"]);
$baseurl = str_replace("\\", "/", $upload_location);
$baseurl = str_replace($serverurl, "", $baseurl);
//$baseurl = dirname($_SERVER["HTTP_REFERER"])."/".$baseurl;
$baseurl = $protocol.$_SERVER["HTTP_HOST"].$folder."/".$baseurl;
}
}
//Replace space with %20 for URL
$baseurl = str_replace(" ", "%20", $baseurl);
// Make sure it ends with front slash
if ( substr( $baseurl , strlen($baseurl) - 1) !== "/" ) {
$baseurl .= "/";
}
if(is_array($_FILES["uploadedfile$mid"]["name"])) {
foreach($_FILES["uploadedfile$mid"]["name"] as $value) {
/* Not really useful since I need type and size as well... just use $_FILES
if(strlen($value) > 0) {
//Check that we have a filename
$filenames[] = $value;
}*/
$fileCnt += 1;
}
}
for ($cnt = 0; $cnt<$fileCnt; $cnt++) {
if ((strlen($_FILES["uploadedfile$mid"]["name"][$cnt]) > 0) && ($upload_users === "true")) {
// Check blacklist first
foreach ($blacklist as $file) {
$filename = $_FILES["uploadedfile$mid"]["name"][$cnt];
if (preg_match("/$file\$/i", $filename)) {
$filetypeok = false;
break;
}
}
// Check double extension
if ($upload_doubleext === "1" || $upload_phpext === "1") {
$exts = explode('.', $filename);
// There is more than one dot!
if (count($exts) > 2) {
// Any double extension blocked
if ($upload_doubleext === "1")
$filetypeok = false;
if ($upload_phpext === "1") {
// Block .php.
if (strtolower($exts[count($exts)-2]) === "php")
$filetypeok = false;
}
} else {
// Check and block any .php combination
if (stripos($filename, ".php") !== false)
$filetypeok = false;
}
}
if ($_FILES["uploadedfile$mid"]["error"][$cnt] > 0) {
// Check if there was any error
$filetypeok = false;
}
if ($filetypeok) {
$fileList .= $_FILES["uploadedfile$mid"]["name"][$cnt] . "|";
$filetype = $_FILES["uploadedfile$mid"]["type"][$cnt];
$fileInfo .= "(" . JText::_('TYPE') . ": " . $filetype . " " . JText::_('SIZE') . ": " . $_FILES["uploadedfile$mid"]["size"][$cnt] . " " . JText::_('BYTES') . ")|";
if ($filetype === "") $filetype = "false";
if (stripos($upload_filetypes, $filetype) === false) {
$filetypeok = false;
} else {
$filetypeok = true;
}
if ($upload_filetypes === "*") {
$filetypeok = true;
}
//Check if GIF and block GIF Comment
if ($upload_gifcomment === "1" && (preg_match("/.gif\$/i", $_FILES["uploadedfile$mid"]["name"][$cnt]))) {
$comment = ModSimpleFileUploaderHelperv13::getGIFComment($_FILES["uploadedfile$mid"]["tmp_name"][$cnt], $upload_debug);
if(stripos($comment, "getGIFComment:BLOCK") !== false) {
$filetypeok = false;
}
if ($upload_debug == 1) $results .= $comment;
}
}
if (($filetypeok) && ($_FILES["uploadedfile$mid"]["size"][$cnt] < $upload_maxsize)) {
$errmsg = "";
$new_filename = "";
if ($_FILES["uploadedfile$mid"]["error"][$cnt] > 0) {
if (($_FILES["uploadedfile$mid"]["size"][$cnt] == 0) && ($_FILES["uploadedfile$mid"]["error"][$cnt] == 2)) {
$errmsg = "(".$_FILES["uploadedfile$mid"]["name"][$cnt].")".JText::sprintf('ERROR_TOO_BIG', "
[PHP Error: " . $_FILES["uploadedfile$mid"]["error"][$cnt]) . "]
";
} else {
$errmsg = "(".$_FILES["uploadedfile$mid"]["name"][$cnt].")".JText::sprintf('ERROR_LABEL', $_FILES["uploadedfile$mid"]["error"][$cnt]) . "
";
}
if ($upload_showerrmsg == 1)
$results .= $errmsg;
else
$results .= JText::_('UPLOAD_FAILED');
$fileErr = 1;
} else {
$bytesfilesize = $_FILES["uploadedfile$mid"]["size"][$cnt];
// Check to see if GD lib functions should be skipped
if ($upload_disablegdthreshold > 0) {
if ($bytesfilesize >= $upload_disablegdthreshold) $upload_disablegdlib = 1;
}
$filesize = ModSimpleFileUploaderHelperv13::getFileSizePP($bytesfilesize);
if (($upload_popshowbytes == 1) && ($bytesfilesize != $filesize)) $filesize = $filesize . " (" . $bytesfilesize . " " . JText::_('BYTES') . ")";
//$results .= "" . JText::_('FILE_OK_MSG') . "
";
$results .= "".JText::sprintf('FILE_UPLOAD_LABEL', $_FILES["uploadedfile$mid"]["name"][$cnt]) . "
";
$results .= JText::sprintf('FILE_TYPE_LABEL', $_FILES["uploadedfile$mid"]["type"][$cnt]) . "
";
$results .= JText::sprintf('FILE_SIZE_LABEL', $filesize) . "
";
#$results .= "uploaded to: " . $_FILES["uploadedfile$mid"]["tmp_name"][$cnt] . "
";
if (file_exists($upload_location . $_FILES["uploadedfile$mid"]["name"][$cnt])) {
if ( $upload_fileexist === "0" ) {
// FAIL
$results .= "
" . JText::sprintf('FILE_EXISTS_MSG', $_FILES["uploadedfile$mid"]["name"][$cnt]) . "
" . JText::_('FILE_EXISTS_CORR');
$fileErr = 1;
}
/* // Don't delete until new file has been created!
if ( $upload_fileexist === "1" ) {
// REPLACE
unlink($upload_location . $_FILES["uploadedfile$mid"]["name"][$cnt]);
$results .= JText::_('FILE_EXISTS_REPLACE') . "
";
$chkfileexist = "no";
}*/
if ( $upload_fileexist === "2" || $upload_fileexist === "1" ) {
// BACKUP
$new_filename = $_FILES["uploadedfile$mid"]["name"][$cnt] . microtime();
rename($upload_location . $_FILES["uploadedfile$mid"]["name"][$cnt], $upload_location . $new_filename);
if ($upload_fileexist === "1")
$results .= JText::_('FILE_EXISTS_REPLACE') . "
";
else
$results .= JText::sprintf('FILE_EXISTS_BACKUP', $new_filename) . "
";
$new_filename = $upload_location . $new_filename;
$chkfileexist = "no";
}
} else {
$chkfileexist = "no";
}
if ( $chkfileexist === "no" ) {
// Resize and/or compress?
$image_resize = false;
$img_compressimg = 0;
$img_maximgheight = 0;
$img_maximgwidth = 0;
// Check size of images before moving
if (ModSimpleFileUploaderHelperv13::gd_get_info() && $upload_disablegdlib == 0) {
if (($img = @getimagesize($_FILES["uploadedfile$mid"]["tmp_name"][$cnt])) && (((is_numeric($upload_maximgwidth) && $upload_maximgwidth > 0) || (is_numeric($upload_maximgheight) && $upload_maximgheight > 0)) || ($upload_thumbcreate == 1))) {
list($width, $height, $type, $attr) = getimagesize($_FILES["uploadedfile$mid"]["tmp_name"][$cnt]);
// Make sure we have a valid compression ratio
if (!is_numeric($upload_compressimg) || $upload_compressimg > 100 || $upload_compressimg <= 0)
$img_compressimg = 100;
else
$img_compressimg = $upload_compressimg;
if ($upload_maximgheight == 0)
$img_maximgheight = $height;
else
$img_maximgheight = $upload_maximgheight;
if ($upload_maximgwidth == 0)
$img_maximgwidth = $width;
else
$img_maximgwidth = $upload_maximgwidth;
$ratioh = $img_maximgheight/$height;
$ratiow = $img_maximgwidth/$width;
$ratio = min($ratioh, $ratiow);
// New dimensions
$n_width = intval($ratio*$width);
$n_height = intval($ratio*$height);
$errmsg = "";
switch ($type) {
case 1: //'image/gif'
if (imagetypes() & IMG_GIF) { // not the same as IMAGETYPE
$oim = imageCreateFromGIF($_FILES["uploadedfile$mid"]["tmp_name"][$cnt]) ;
} else {
$errmsg = "GIF ".JText::_('IMG_TYPE_FAIL')."
";
}
break;
case 2: //'image/jpeg'
if (imagetypes() & IMG_JPG) {
$oim = imageCreateFromJPEG($_FILES["uploadedfile$mid"]["tmp_name"][$cnt]) ;
} else {
$errmsg = "JPEG ".JText::_('IMG_TYPE_FAIL')."
";
}
break;
case 3: //'image/png'
if (imagetypes() & IMG_PNG) {
$oim = imageCreateFromPNG($_FILES["uploadedfile$mid"]["tmp_name"][$cnt]) ;
$img_compressimg = round($img_compressimg / 10); // Quality is 0-9 for PNG
if ($img_compressimg >= 10) $img_compressimg = 9; // If user has set quality to 100
} else {
$errmsg = "PNG ".JText::_('IMG_TYPE_FAIL')."
";
}
break;
case 15: //'image/wbmp'
if (imagetypes() & IMG_WBMP) {
$oim = imageCreateFromWBMP($_FILES["uploadedfile$mid"]["tmp_name"][$cnt]) ;
} else {
$errmsg = "WBMP ".JText::_('IMG_TYPE_FAIL')."
";
}
break;
default:
$errmsg = $type." ".JText::_('IMG_TYPE_FAIL')."
";
break;
}
if ($errmsg === "") {
// If thumbnail
$thumbfilename = "";
if ($upload_thumbcreate == 1) {
$img_thumbsize = strtolower($upload_thumbsize);
$img_thumbsize = explode("x", $img_thumbsize);
if (is_array($img_thumbsize)) {
if (count($img_thumbsize) == 2) {
if (is_numeric($img_thumbsize[0]) && is_numeric($img_thumbsize[1])) {
$ttim=imagecreatetruecolor($img_thumbsize[0],$img_thumbsize[1]);
imagecopyresampled($ttim,$oim,0,0,0,0,$img_thumbsize[0],$img_thumbsize[1],$width,$height);
$ext = substr(strrchr($_FILES["uploadedfile$mid"]["name"][$cnt], '.'), 1);
$thumbfilename .= substr($_FILES["uploadedfile$mid"]["name"][$cnt], 0, (strlen($_FILES["uploadedfile$mid"]["name"][$cnt])-(strlen($ext) +1))) . "_" . $upload_thumbname . "." . $ext;
// Make thumb as a link
$results .= JText::_('IMG_THUMB_FILE') . ' '.$thumbfilename.'
';
//$results .= JText::_('IMG THUMB FILE') . " " . $thumbfilename . "
";
// Add path to thumb filename
$thumbfilename = $upload_location . $thumbfilename;
}
}
}
}
// Only thumbnail, no resize
if ($upload_maximgheight !== 0 && $upload_maximgwidth !== 0) {
$tim=imagecreatetruecolor($n_width,$n_height);
imagecopyresampled($tim,$oim,0,0,0,0,$n_width,$n_height,$width,$height);
}
switch ($type) {
case 1:
// Only thumbnail, no resize
if ($upload_maximgheight !== 0 && $upload_maximgwidth !== 0)
imageGIF($tim, $upload_location . $_FILES["uploadedfile$mid"]["name"][$cnt]);
if ($thumbfilename !== "") imageGIF($ttim, $thumbfilename);
$image_resize = true;
break;
case 2:
// Only thumbnail, no resize
if ($upload_maximgheight !== 0 && $upload_maximgwidth !== 0)
imageJPEG($tim, $upload_location . $_FILES["uploadedfile$mid"]["name"][$cnt], $img_compressimg);
if ($thumbfilename !== "") imageJPEG($ttim, $thumbfilename);
$img_compressimg = "";
$image_resize = true;
break;
case 3:
// Only thumbnail, no resize
if ($upload_maximgheight !== 0 && $upload_maximgwidth !== 0)
imagePNG($tim, $upload_location . $_FILES["uploadedfile$mid"]["name"][$cnt], $img_compressimg);
if ($thumbfilename !== "") imagePNG($ttim, $thumbfilename);
$img_compressimg = "";
$image_resize = true;
break;
case 15:
// Only thumbnail, no resize
if ($upload_maximgheight !== 0 && $upload_maximgwidth !== 0)
imageWBMP($tim, $upload_location . $_FILES["uploadedfile$mid"]["name"][$cnt]);
if ($thumbfilename !== "") imageWBMP($ttim, $thumbfilename);
break;
$image_resize = true;
default:
$image_resize = false;
break;
}
// Only thumbnail, no resize
if ($upload_maximgheight !== 0 && $upload_maximgwidth !== 0) {
imagedestroy($tim);
if ($image_resize)
$results .= JText::_('IMG_ORIG_RESIZE')."
";
else
$results .= JText::_('IMG_RESIZE_FAIL')."
";
} else {
$image_resize = false;
}
imagedestroy($oim);
if ($thumbfilename !== "") imagedestroy($ttim);
} else {
$fileErr = 1;
$results .= JText::_('FAIL_REQUEST') . "
";
$_SESSION["failedfile"] .= $_FILES["uploadedfile$mid"]["name"][$cnt]." (".JText::_('IMG_SAVE_FAIL').", ".$errmsg.")";
}
}
if (($img = @getimagesize($_FILES["uploadedfile$mid"]["tmp_name"][$cnt])) && (!$image_resize) && ($upload_compressimg !== "") && is_numeric($upload_compressimg)) {
// Compress JPEG? This only happens if no resize!
list($width, $height, $type, $attr) = getimagesize($_FILES["uploadedfile$mid"]["tmp_name"][$cnt]);
// Make sure we have a valid compression ratio
if ($upload_compressimg > 100 || $upload_compressimg <= 0) $img_compressimg = 100;
if (($type == 2) && (is_numeric($img_compressimg)) && ($fileErr == 0)) {
$oim = imagecreatefromjpeg($_FILES["uploadedfile$mid"]["tmp_name"][$cnt]);
if (imagejpeg($oim, $upload_location . $_FILES["uploadedfile$mid"]["name"][$cnt], $img_compressimg)) {
$fileErr = 0;
$image_resize = true;
} else {
$fileErr = 1;
$results .= JText::_('FAIL_REQUEST') . "
";
$_SESSION["failedfile"] .= $_FILES["uploadedfile$mid"]["name"][$cnt]." (".JText::_('IMG_COMPRESS_FAIL').")";
}
imagedestroy($oim);
}
// Compress PNG? This only happens if no resize!
if (($type == 3) && (is_numeric($upload_compressimg)) && ($fileErr == 0)) {
$oim = imagecreatefrompng($_FILES["uploadedfile$mid"]["tmp_name"][$cnt]);
$img_compressimg = round($upload_compressimg / 100); // Quality is 0-9 for PNG
if ($img_compressimg >= 10) $img_compressimg = 9; // If user has set quality to 100
if (imagepng($oim, $upload_location . $_FILES["uploadedfile$mid"]["name"][$cnt], $img_compressimg)) {
$fileErr = 0;
$image_resize = true;
} else {
$fileErr = 1;
$results .= JText::_('FAIL_REQUEST') . "
";
$_SESSION["failedfile"] .= $_FILES["uploadedfile$mid"]["name"][$cnt]." (".JText::_('IMG_COMPRESS_FAIL').")";
}
imagedestroy($oim);
}
if ($image_resize)
$results .= JText::_('IMG_ORIG_COMPRESS')."
";
else
$results .= JText::_('IMG_COMPRESS_FAIL')."
";
}
}
// If image has been resized it is already saved
if (!$image_resize) {
if (move_uploaded_file($_FILES["uploadedfile$mid"]["tmp_name"][$cnt], $upload_location . $_FILES["uploadedfile$mid"]["name"][$cnt])) {
$fileErr = 0;
} else {
$fileErr = 1;
$results .= JText::_('FAIL_REQUEST') . "
";
$_FILES["uploadedfile$mid"]["name"][$cnt] = "";
}
}
// Form Fields
if (($upload_useformsfields == 1) && (strlen($upload_formfields) > 0) && ($fileErr == 0)) {
$fields = explode(";", $upload_formfields);
$valname = "";
$valfile = "";
$ffform = "";
$fffield = "";
$formfieldsemail[$cnt] = "";
if ($upload_nohtmlencoding == 1)
$ffform = trim ( $_REQUEST["sfuFormFields$mid"] );
else
$ffform = htmlentities ( trim ( $_REQUEST["sfuFormFields$mid"] ) , ENT_NOQUOTES , "utf-8" );
if ($upload_debug == 1) print_r($_REQUEST);
if ($upload_replacetag == 1) $ffform = str_replace(">", ">", str_replace("<", "<", $ffform));
if ($upload_debug == 1) $results .= "ffform=$ffform
";
$ffform = explode("[||]", $ffform);
// If create a row in the form fileds file...
if (strlen($upload_formfieldsfile) > 0)
$valfile = $_FILES["uploadedfile$mid"]["name"][$cnt].$upload_formfieldsdiv;
for ($iff = 0; $iff < count($ffform); $iff++) {
$valname = $ffform[$iff];
$fffield = explode("=", $valname);
foreach ($fields as $fld) {
$valname = "sfuff".$mid."_".$fld."_".$_FILES["uploadedfile$mid"]["name"][$cnt];
if ($valname === $fffield[0]) {
if (strlen($upload_formfieldsfile) > 0)
$valfile .= $fffield[1].$upload_formfieldsdiv;
else
$valfile .= $fld."=".$fffield[1]."\n";
// Store for e-mail, use same counter as file
$formfieldsemail[$cnt] .= $fld."=".$fffield[1]."\n";
}
}
if ($upload_debug == 1) $results .= "valname=$valname
";
}
if (strlen($upload_formfieldsfile) > 0) {
// Remove last pipe
$valfile = substr($valfile, 0, -1);
// Check if we got something
if ($valfile === $_FILES["uploadedfile$mid"]["name"][$cnt])
$valfile = "";
}
if ($upload_debug == 1) $results .= "valfile=$valfile
";
if ((strlen($valfile) > 0) && (strlen($upload_formfieldsfile) == 0)) {
// Write the file:
$ffFile = $upload_location . $_FILES["uploadedfile$mid"]["name"][$cnt] . ".txt";
if ($upload_debug == 1) $results .= "ffFile=$ffFile
";
$fh = fopen($ffFile, 'w') or $fileErr = 1;
if ($fileErr == 1) {
$results .= JText::_('FAIL_FORMFIELDS_FILE') . "
";
} else {
// For some mysterious reason PHP refuses to write UTF-8 as UTF-8. Some stupid work-around below found at php.net
$valfile = mb_convert_encoding( $valfile, 'UTF-8');
fwrite($fh, $valfile);
fclose($fh);
}
}
// Store for Form Fields File, use same counter as file
$formfieldsval[$cnt] = $valfile;
}
if (($upload_popshowpath == 1) && ($fileErr == 0)) {
$results .= JText::sprintf('FILE_SAVE_AS', ''.$baseurl.$_FILES["uploadedfile$mid"]["name"][$cnt].'').'
';
}
if ($upload_fileexist === "1" && file_exists($new_filename)) {
if ($fileErr == 0) {
// Delete (=replace) of old file
unlink($new_filename);
} else {
// Put original back if something went wrong
rename($new_filename, $upload_location . $_FILES["uploadedfile$mid"]["name"][$cnt]);
}
}
//$results .= "
".JText::_('MSG_UNZIP_ERROR')."
"; } else { $results .= "".JText::_('MSG_UNZIP')."
"; } } if (($_FILES["uploadedfile$mid"]["type"][$cnt] === "application/x-zip") || ($_FILES["uploadedfile$mid"]["type"][$cnt] === "application/x-zip-compressed") || ($_FILES["uploadedfile$mid"]["type"][$cnt] === "application/zip-compressed")) { $zip = new ZipArchive; $res = $zip->open($upload_location.$_FILES["uploadedfile$mid"]["name"][$cnt]); if ($res === TRUE) { $zip->extractTo($upload_location); $zip->close(); $results .= "".JText::_('MSG_UNZIP')."
"; } else { $results .= "".JText::_('MSG_UNZIP_ERROR')."
"; } } } $_SESSION["uploaderr$mid"] = $fileErr; } else { $fileErr = 1; $errmsg = "(".$_FILES["uploadedfile$mid"]["name"][$cnt].")| ".$upload_location.$_FILES["uploadedfile$mid"]["name"][$cnt]." (".$baseurl.str_replace(" ", "%20", $_FILES["uploadedfile$mid"]["name"][$cnt]).") | ".$infos[$cnt]." |
| " . $f . " |