<?

$dataToWrite = $str->fromHTML($_POST['fileContent']);

$fileSizeToWrite = strlen($dataToWrite);
$existingFileSize = @filesize($filepath);


if ($permissions['unlimitedSpace']){
	$continueSaving = true;
} else {
	if ($existingFileSize < $fileSizeToWrite){
		/* check the size of the added text */
		if ($quota->canAddToSpace(($fileSizeToWrite-$existingFileSize))) {
			$continueSaving = true;
		} else {
			$continueSaving = false;
			$tmp['msg'] = $lang->get("You have excedeed your space quota. The text file was not updated.");
		}
	} else {
		$continueSaving = true;
	}
}


if ($continueSaving) {
	if (!file_exists($filepath)) {
		$msg = "<span style=\"color:red;\">".$lang->get("The file named \"%1\" does not exist anymore.", array($sys['filename']))."</span><br>".$lang->get("You can create a new file with the edited text.")."";
		$statusBarMessage[] = array($msg, 0);
		$showTextFromFile = false;
		$_GET['new'] = "1";
	} else {
		if ($fm->newFileContent($filepath, $dataToWrite)) {
			$newFileSize = filesize($filepath);
			$log->act("0", "text file update", $filepath);
			$msg = $lang->get("Text file updated.");
			$statusBarMessage[] = array($msg, 0);
			$showTextFromFile = true;
		} else {
			$msg = $lang->get("Failed to update the text file. System error: %1", array($fm->errorMsg));
			$statusBarMessage[] = array($msg, 1);
		}
	}
} else {
	$msg = $tmp['msg'];
	$statusBarMessage[] = array($msg, 1);
}
?>