// REMOVE THIS LINE WHEN OUT OF DEV
//
if (!isset($_SESSION['ccuserid'])) { $_SESSION['ccuserid'] = 7; }

$phpftp_host = $_REQUEST["server"];
$phpftp_port = $_REQUEST["port"];
$phpftp_user = $_REQUEST["username"];
$phpftp_passwd = $_REQUEST["password"];
$phpftp_function = $_REQUEST["Function"];

if (!$_SESSION['ccuserid']) {
	echo getJSONError(array(1, "Not logged in"));
}
else {
	
	$rsConnections = FTPSiteDB::retrieveForUserId($_SESSION['ccuserid']);
	$arConnections = array();
	for ($i = 0; $i < sizeof($rsConnections); $i++) {
		array_push($arConnections, $rsConnections[$i]->toHashMap());
	}
	
	echo getJSONResult(array("connectionList" => $arConnections));
}

function getJSONResult($result) {
	return json_encode(array("result" => $result));
}

function getJSONError($errors) {
	return json_encode(array("errors" => $errors));
}
$ftpSite->setUserId($_SESSION["ccuserid"]);
$ftpSite->setFtpSiteId($_REQUEST["ftpSiteId"]);
$ftpSite->setServer($_REQUEST["server"]);
$ftpSite->setPort($_REQUEST["port"]);
$ftpSite->setUsername($_REQUEST["username"]);
$ftpSite->setPassword($_REQUEST["password"]);

if (!$_SESSION['ccuserid']) {
	echo getJSONError(array(1, "Not logged in"));
}
else if (strlen(trim($ftpSite->getServer())) == 0) {
	echo getJSONError(array(11, "Host required for FTP site"));
}
else {
	if (FTPSiteDB::save($ftpSite)) {
		echo getJSONResult(array("ftpSite" => $ftpSite->toHashMap()));
	}
	else {
		echo getJSONError(array(2, "Error saving record"));
	}
}

function getJSONResult($result) {
	return json_encode(array("result" => $result));
}

function getJSONError($errors) {
	return json_encode(array("errors" => $errors));
}