Convert a WS_FTP configuration file to a format for FileZilla.

I originally read about this script here.  Decided to include the script below:

#!/usr/bin/php
<?
/*
 This script takes a config file from WS_FTP (WS_FTP.ini) and converts it to filezilla format
 Version 1.0

 Author: Andrew Kerr (andrew@arrkerr.com)
  Credit is given where due in the function headers

 I make no claims about this program, use it at your own risk.
 If for some reason anything in this script violates any rules on the part of wsftp or filezilla,
  please do not use it and let me know so that I can remove it.
*/

if ($argc != 2) {
	die("Usage: ". $argv[0] ." <wsftp config file>\n");
}

$inFile = $argv[1];
$outFile = "FileZilla.xml";

// open up the input file
if (!$inFH = fopen($inFile, "r")) { die("Unable to open the wsftp config file\n"); }

// open up the output file
if (!$outFH = fopen($outFile, 'w')) { die("Unable to open the wsftp config file\n"); }

// a variable to keep track of if we're working on the very first host or not
$firstHost = true;

// print the config file header
printHeader($outFH);

// loop through the file one line at a time
while ($currentLine = fgets($inFH))
{
	// check for an empty line
	if (strLen($currentLine) > 1) {
		// if the line starts with a [ then it is a new site, and a new site name
		if (substr($currentLine,0,1) == "[") {
			if ($firstHost == false) {
				//write out the site config
				writeSite($siteName, $hostName, $userName, $password, $remoteDir, $passive, $outFH);
			}
			$firstHost = false;

			$siteName = ltrim(trim(rtrim($currentLine), "\]"), "\[");
		}
		// otherwise it is a parameter to a previous site name
		else {
			// seperate the paramter from the value
			list($param, $val) = split("=", $currentLine, 2);

			switch ($param) {
				case "HOST":
					$hostName = chop($val);
					break;
				case "rdir0":
					$remoteDir = chop($val);
					break;
				case "UID":
					$userName = chop($val);
					break;
				case "PWD":
					$password = encryptFilezilla(decryptWSFTP("PWD=" . chop($val)));
					break;
				case "PASVMODE":
					$passive = chop($val);
					break;
			}
		}
	}
}

// write out the last site once we hit the end of the file
if ($firstHost == false) {
	//write out the site config
	writeSite($siteName, $hostName, $userName, $password, $remoteDir, $passive, $outFH);
}

// print the footer
printFooter($outFH);

// close the input and output files
fclose($inFH);
fclose($outFH);

/*
	Write the filezilla site config
*/
function writeSite($siteName, $hostName, $userName, $password, $remoteDir, $passive, $outFH)
{
	if (($passive != 0) && ($passive != 1)) {
		// If PASVMODE was blank in the WS_FTP config (which it wouldn't ever be?) set it to 0
		// 0 in FileZilla is "default"
		$passive = 0;
	}
	if ($passive = 0) {
		// WS_FTP uses 0 for active, FileZilla uses 2
		$passive = 2;
	}
	// $passive=2; // if you need to convert everything to active (I did because of our firewall)
	fwrite($outFH, '<Site Name="' . $siteName . '" Host="' . $hostName . '" Port="21" User="' . $userName . '" Account="" RemoteDir='. $remoteDir .' LocalDir="" Pass="' . $password . '" Logontype="1" FWBypass="0" DontSavePass="0" ServerType="" PasvMode="' . $passive . '" TimeZoneOffset="0" TimeZoneOffsetMinutes="0" Comments="" UTF8="0" DefaultSite="0" />' . "\n");
}

/*
	Convert the ws_ftp password to plain-text

	Converted from the javascript version at:
		http://www.tele-pro.co.uk/software/ws%5Fftp%5Fpass/
*/
function decryptWSFTP($str) {
	$out = "";
	$pw = substr($str, 37, strlen($str));
	for ($i=0; $i<(strlen($pw)/2); $i++) {
		$character = substr($pw, $i*2, 2);
		$sal = substr($str, 5+$i, 1);

		$claro = ("0x". $character)-$i-1-((47+parseInt("0x" . $sal))%57);
		$out = $out . chr($claro);
	}
	return $out;
}

/*
	php version of the javascript parseInt() function that convers an int to a char
*/
function parseInt($data)
{
	return intval(substr($data,0,8),16);
}

/*
	Convert a plain-text password to filezilla format

	Based on a version from
		http://www.sourceshock.com/details_snippet/39/
*/
function encryptFilezilla($str)
{
	$encryptionKey = "FILEZILLA1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	$pos = strlen($str) % strlen($encryptionKey);
	$ret = "";
	for ($i=0; $i<strlen($str); $i++) {
		$x =  sprintf("%s",$str[$i] ^ $encryptionKey[($i + $pos) % strlen($encryptionKey)]);
		if (preg_match("/[^a-z]/",$str[$i])) {
			$min = 3 - strlen(ord($x));
			$z = str_repeat('0',$min) . ord($x);
		} else {
			$z = "0" . ord($x);
		}
		$ret .= $z; 
	}
	return $ret;
}

/*
	Print the filezilla config file footer
*/
function printFooter($outFH) {
	fwrite($outFH, '</Sites>' . "\n" . '<Settings>' . "\n" . '</Settings>' . "\n" . '<RecentServers/>' . "\n" . '</FileZilla>' . "\n");
}

/*
	Print the filezilla config file header
*/
function printHeader($outFH) {
	fwrite($outFH, '<?xml version="1.0" encoding="ISO-8859-1"?>' . "\n" . '<FileZilla>'. "\n" . '<Sites>'. "\n");
}
?>

 


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *