<?php 
    $UploadMaxSize = AJXP_Utils::convertBytes(ini_get('upload_max_filesize'));
    $UploadMaxPostSize = AJXP_Utils::convertBytes(ini_get('post_max_size'));
    if($UploadMaxPostSize > 0 && $UploadMaxPostSize < $UploadMaxSize) $UploadMaxSize = $UploadMaxPostSize;
    $confMaxSize = ConfService::getConf("UPLOAD_MAX_FILE");
    if($confMaxSize != 0 &&  $confMaxSize < $UploadMaxSize) $UploadMaxSize = $confMaxSize;
    $confTotalSize = ConfService::getConf("UPLOAD_MAX_TOTAL");
    $confTotalNumber = ConfService::getConf("UPLOAD_MAX_NUMBER");    
    
	$repository = ConfService::getRepository();
	$accessType = $repository->getAccessType();
	if($accessType == "fs"){
		$partitionLength = $UploadMaxSize - 1000;	
	}else if($accessType == "remotefs"){
		$maxFileLength = $UploadMaxSize;
	}else if($accessType == "ftp"){
		$maxFileLength = $UploadMaxSize;
		/*
		// Here we could switch to FTP, but that would mean, by a way or another,
		// to display in clear the ftp credentials... 
		// Also, beware the ftp_z.jar is needed for this to work.
		$ftpWrapper = new ftpAccessWrapper();
		$plugin = AJXP_PluginsService::findPlugin("access", $repository->getAccessType());
		$streamData = $plugin->detectStreamWrapper(true);		
    	$destStreamURL = $streamData["protocol"]."://".$repository->getId()."/";
    	// Not working now, as ftpAccessWrapper::buildRealUrl is in fact protected
    	$ftpURL = $ftpWrapper->buildRealUrl($destStreamURL);
    	// Then pass this URL to the javascript uploadUrl => to the applet.
    	*/
	}
	
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">	
</head>
<body bgcolor="ffffff" style="overflow:hidden; padding: 0px; padding-left: 0px; margin: 0px;">

<link rel="stylesheet" href="plugins/uploader.plupload/plupload/jquery.plupload.queue/css/jquery.plupload.queue.css?v=2" type="text/css" media="screen" />
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript"> google.load("jquery", "1.3"); </script>
<script type="text/javascript" src="plugins/uploader.plupload/plupload/plupload.full.js"></script>
<script type="text/javascript" src="plugins/uploader.plupload/plupload/jquery.plupload.queue/jquery.plupload.queue.js"></script>
<script type="text/javascript" src="plugins/uploader.plupload/plupload/plupload.flash.js"></script>
<script type="text/javascript" src="plugins/uploader.plupload/plupload/plupload.silverlight.js"></script>
<script language="javascript" type="text/javascript">

// Fix height of modal box
//$(window.parent.document).find("#generic_dialog_box").height(355); 


var clientId = '<?php echo session_id(); ?>';

var partitionLength = <?php echo ($partitionLength?$partitionLength:-1); ?>;
var maxFileLength = <?php echo ($maxFileLength?$maxFileLength:-1); ?>;

var ftpUrl = '<?php echo ($ftpURL?$ftpURL:""); ?>';

var uploadUrl = '<?php print($_SERVER["SCRIPT_NAME"]); ?>?get_action=upload_chunks_unify&secure_token=<?php echo AuthService::getSecureToken();?>&ajxp_sessid=<?php echo session_id(); ?>';
var upurl = '<?php print($_SERVER["SCRIPT_NAME"]); ?>?get_action=upload&secure_token=<?php echo AuthService::getSecureToken();?>&ajxp_sessid=<?php echo session_id(); ?>';

var maxFileSize = '<?PHP echo ($UploadMaxSize/1048576) . "mb"; ?>';
var maxHTML4 = '<?PHP echo $UploadMaxSize; ?>';
var chunkSize = '<?PHP echo (($UploadMaxSize/1048576) - 1) . "mb"; ?>';
var currentDir = parent.ajaxplorer.getContextNode().getPath();

$(function() {
	$("#plupload_uploader").pluploadQueue({
		// General settings
		runtimes : 'silverlight,flash,html5,html4',
		url : uploadUrl,
        //max_file_size : maxFileSize, //Uncomment this line and comment the following to use php.ini max file size for maximum filesize
		max_file_size : '2048mb',
		//chunk_size : chunkSize, //Uncomment this line to use php.ini maxsize-1 for chunk size
		chunk_size : '2mb',
		unique_names : false,
        multiple_queues : true,
		multipart : true,
		multipart_params : { dir: currentDir },

		// Specify what files to browse for
		filters : [],

		// Flash settings
		flash_swf_url : 'plugins/uploader.plupload/plupload/plupload.flash.swf',
		silverlight_xap_url : 'plugins/uploader.plupload/plupload/plupload.silverlight.xap',
		
		// attach callbacks before queue
		preinit: attachCallbacks
	});
	
	// attach callbacks for FileUploaded
	function attachCallbacks(Uploader) {
		Uploader.bind('FileUploaded', function(up, file, res) {
			if(this.total.queued <= 1) {
				parent.ajaxplorer.fireContextRefresh();
			}
			$.get(upurl);	
		});
	}

});
</script>

<div id="plupload_uploader">Your browser does not support uploads, please install Flash Player or Microsoft Silverlight.</div>
</body>
</html>