<?php
	session_start(); //Iniciamos la session
	if (isset($_POST["PHPSESSID"])) {
		session_id($_POST["PHPSESSID"]);
	}
	include('Net/SFTP.php');
	$sftp = new Net_SFTP('Equipo'); //cambiar esto luego por "Sun01"
	if (!$sftp->login($_SESSION['user'], $_SESSION['pass'])) {
		exit('Login Failed');
	}
	
	// The Demos don't save files
        $name = $_FILES["Filedata"]["name"];
        $destino =  "/home/".($_SESSION['user'])."/scripts/".$name;
		
		$permisos = $sftp->stat('/home/'.($_SESSION['user']).'/scripts/');
		$sftp->chmod(0777, '/home/'.($_SESSION['user']).'/scripts/');
        copy($_FILES["Filedata"]["tmp_name"],$destino);
		
	// In this demo we trigger the uploadError event in SWFUpload by returning a status code other than 200 (which is the default returned by PHP)
	if (!isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) {
		// Usually we'll only get an invalid upload if our PHP.INI upload sizes are smaller than the size of the file we allowed
		// to be uploaded.
		header("HTTP/1.1 500 File Upload Error");
		if (isset($_FILES["Filedata"])) {
			echo $_FILES["Filedata"]["error"];
		}
		exit(0);
	}
	
	$sftp->chmod($permisos['permissions'], '/home/'.($_SESSION['user']).'/scripts/');
?>