<?
$tofolder	= $str->fromHTML($_GET['tofolder']);
$sys['fullPath'] = gluePath($permissions['path'], $tofolder);


/* check user permission*/
if (!$permissions['upload']) {
	$msg = "<div style=\"white-space:nowrap;text-align:left;\">".$lang->get("You are not allowed to upload files")."</div>";
	$statusBarMessage[] = array($msg, 1);
}

if (!isCleanPath($tofolder)) {
	$msg = "<div style=\"white-space:nowrap;text-align:left;\">".$lang->get("Ilegal path: ")."".$tofolder."</div>";
	$statusBarMessage[] = array($msg, 1);
}

if ($permissions['unlimitedTraffic'] == false && $quota->canAddTraffic() == false) {
	$msg = "<div style=\"white-space:nowrap;text-align:left;\">".$lang->get("Traffic quota excedeed")."</div>";
	$statusBarMessage[] = array($msg, 1);
}

/* check folder*/
if (!is_dir($sys['fullPath'])) {
	$msg = "<div style=\"white-space:nowrap;text-align:left;\">".$lang->get("The folder where you want to upload files does not exist any more.")."</div>";
	$statusBarMessage[] = array($msg, 1);
}


$uploadedFiles = 0;
		for ($i=0 ; $i < sizeof($_FILES['uploadfiles']) ; $i++) {

			$uploadItem = $_FILES['uploadfiles'];
			$uploadFilename = $uploadItem['tmp_name'][$i];
			$niceFilename = $str->fromHTML($uploadItem['name'][$i]);
			$toFile = gluePath($sys['fullPath'],$niceFilename);
			$filesize = $uploadItem['size'][$i];



			if ($uploadItem['error'][$i] && $niceFilename != "") {
				$msg = "<div style=\"white-space:nowrap;text-align:left;\">".$lang->get("Maximum upload limit reached. File \"".$niceFilename."\" could not be uploaded.")."</div>";
				$statusBarMessage[] = array($msg, 1);
			}
			if ($niceFilename != "") {
			if ($permissions['unlimitedSpace'] || $quota->canAddToSpace()) {
				if (is_uploaded_file($uploadFilename)) {
				if ($fm->copyFile($uploadFilename, $toFile)) {
				
				
				
				$msg = "<div style=\"white-space:nowrap;text-align:left;\">".$lang->get("File \"".$niceFilename."\" uploaded.")."</div>";
				$statusBarMessage[] = array($msg, 0);
	
				$quota->recalcUsedSpace();
				$quota->addTraffic($filesize);
				$log->act("0", "upload", $toFile);
				$log->lastUpload($toFile);
					
					/* send email notice to admin */
					if ($options['uploadNotice'] && $config->settings['adminemail']) {
						$mail = new htmlMimeMail();
						$message = "\n";
						$message .= "User \"".$auth->currentUserInfo['username']."\" uploaded:\n";
						$message .= "Date: " . date("d/n/Y H:i") ."\n";
						$message .= "Filename: " . $niceFilename ."\n";
						$message .= "Filesize: " . $fm->formatFileSize(@filesize($toFile)) ."\n";
						$message .= "Uploaded to folder: " . $sys['fullPath'] ."\n";
						$mail->setText($message);
						$mail->setFrom(($sys['license']['unbranded']?$sys['license']['unbranded']:"Afian"));
						$mail->setSubject("Upload from ".$auth->currentUserInfo['username']);
						$result = @$mail->send(array($config->settings['adminemail']));
					}
					
					} else {
if (file_exists($toFile)) {
	$msg = "<div style=\"white-space:nowrap;text-align:left;\">".$lang->get("Upload failed: '%1' already exists.", array(gluePath($tofolder, $niceFilename)))."</div>";
	$statusBarMessage[] = array($msg, 1);
}
						$log->act("1", "upload", $toFile." (".$fm->errorMsg.")");
					}
				}
			} else {
				$msg = "<div style=\"white-space:nowrap;text-align:left;\">".$lang->get("Not enough space available to upload '%1'", array($niceFilename))."</div>";
				$statusBarMessage[] = array($msg, 1);
			}
			}
		}
?>

<?iframeAlert($iframeAlertMsg);?>