Skip to content

Instantly share code, notes, and snippets.

@tijme
Last active June 28, 2017 15:14
Show Gist options
  • Save tijme/806ad8a662a4a8668edd9276177dba55 to your computer and use it in GitHub Desktop.
Save tijme/806ad8a662a4a8668edd9276177dba55 to your computer and use it in GitHub Desktop.
Manage FileZilla servers using PHP
<?php
/**
* FileZillaXML library
*
* ### DESCRIPTION
*
* With the FileZillaXML library you can easily create an XML with servers and folders that can be
* imported into FileZilla.
*
* ### USAGE
*
* Example 1 (one server in a folder):
*
* `$xml = FileZillaXML::createServer('TestFolder', 'TestProduction', 'test.jetcat.nl', 'sftp', 21, 'myusername', 'mypassword');`
*
* Example 2 (multiple servers in a folder):
*
* `
* $servers = [
* [
* 'name' => 'TestProduction',
* 'host' => 'production.jetcat.nl',
* 'protocol' => 'sftp',
* 'port' => 21,
* 'username' => 'myusername',
* 'password' => 'mypassword'
* ],
* [
* 'name' => 'TestStaging',
* 'host' => 'staging.jetcat.nl',
* 'protocol' => 'sftp',
* 'port' => 21,
* 'username' => 'myusername',
* 'password' => 'mypassword'
* ],
* [
* 'name' => 'TestDevelopment',
* 'host' => 'development.jetcat.nl',
* 'protocol' => 'sftp',
* 'port' => 21,
* 'username' => 'myusername',
* 'password' => 'mypassword'
* ]
* ];
*
* $xml = FileZillaXML::createServers('TestFolder', $servers);
*
* Example 3 (multiple servers in multiple folders):
*
* `
* $folders = [
* 'FolderName1' => [
* [
* 'name' => 'TestProduction1',
* 'host' => 'production1.jetcat.nl',
* 'protocol' => 'sftp',
* 'port' => 21,
* 'username' => 'myusername',
* 'password' => 'mypassword'
* ],
* [
* 'name' => 'TestStaging1',
* 'host' => 'staging1.jetcat.nl',
* 'protocol' => 'sftp',
* 'port' => 21,
* 'username' => 'myusername',
* 'password' => 'mypassword'
* ]
* ],
* 'FolderName2' => [
* [
* 'name' => 'TestProduction2',
* 'host' => 'production2.jetcat.nl',
* 'protocol' => 'sftp',
* 'port' => 21,
* 'username' => 'myusername',
* 'password' => 'mypassword'
* ]
* ]
* ];
*
* $xml = FileZillaXML::createFolders($folders);
* `
*
* @author Tijme Gommers (t.gommers@jetcat.nl)
* @version 1.3.0
*/
class FileZillaXML
{
/**
* The XML version to use
* @const XML_VERSION
*/
const XML_VERSION = '1.0';
/**
* The XML encoding to use
* @const XML_ENCODING
*/
const XML_ENCODING = 'UTF-8';
/**
* The XML root element
* @const XML_ROOT_ELEMENT
*/
const XML_ROOT_ELEMENT = 'FileZilla3';
/**
* The XML servers element
* @const XML_SERVERS_ELEMENT
*/
const XML_SERVERS_ELEMENT = 'Servers';
/**
* The XML server element
* @const XML_SERVER_ELEMENT
*/
const XML_SERVER_ELEMENT = 'Server';
/**
* The XML folder element
* @const XML_FOLDER_ELEMENT
*/
const XML_FOLDER_ELEMENT = 'Folder';
/**
* Create a FileZilla XML for a single server
*
* @param string $folder The folder name for the server
* @param string $name The name for the server
* @param string $host The host for the server
* @param string $protocol The protocol: FTP or SFTP
* @param int $port The port for the server host
* @param string $username The username to login on the servers FTP service
* @param string $password The password to login on the servers FTP service
* @return string The XML file that can be imported
*/
public static final function createServer($folder, $name, $host, $protocol, $port, $username, $password) {
return static::createServers($folder, [
[
'name' => $name,
'host' => $host,
'protocol' => $protocol,
'port' => $port,
'username' => $username,
'password' => $password
]
]);
}
/**
* Create a FileZilla XML containing multiple servers.
* Defining the server options:
*
* array[]
* [name] string The name for the server
* [host] string The host for the server
* [protocol] string The protocol for the server: FTP or SFTP
* [port] int The port for the server host
* [username] string The username to login on the servers FTP service
* [password] string The password to login on the servers FTP service
*
* @param string $folder The folder name for the servers
* @param array $servers The array with server details
* @return string The XML file that can be imported
*/
public static final function createServers($folder, $servers) {
return static::createFolders([$folder => $servers]);
}
/**
* Create a FileZilla XML containing multiple servers
* Defining the folders:
*
* array[folderName]
* array[]
* [name] string The name for the server
* [host] string The host for the server
* [protocol] string The protocol for the server: FTP or SFTP
* [port] int The port for the server host
* [username] string The username to login on the servers FTP service
* [password] string The password to login on the servers FTP service
*
* @param array $folders The array containing all the folders with servers and server details
* @return string The XML file that can be imported
*/
public static final function createFolders($folders) {
$document = static::createDOMDocument(self::XML_VERSION, self::XML_ENCODING);
$serversElement = static::addServersRoot($document, self::XML_ROOT_ELEMENT);
foreach($folders as $folder => $servers) {
$folderElement = static::addFolder($document, $serversElement, $folder);
foreach($servers as $server) {
static::addServer($document, $folderElement, $server['name'], $server['host'], $server['protocol'], $server['port'], $server['username'], $server['password']);
}
}
return $document->saveXML();
}
/**
* Create the root DOMDocument
*
* @param string $version The XML version to use
* @param string $encoding How to encode the xml file
* @param boolean $standalone If DTD file should be used for validation only
* @param boolean $preserveWhiteSpace If whitespaces should be preserved
* @param boolean $formatOutput If the output should be human readable (nicely indented)
* @return DOMDocument
*/
private static final function createDOMDocument($version, $encoding, $standalone = true, $preserveWhiteSpace = false, $formatOutput = false) {
$document = new DOMDocument($version, $encoding);
$document->xmlStandalone = $standalone;
$document->preserveWhiteSpace = $preserveWhiteSpace;
$document->formatOutput = $formatOutput;
return $document;
}
/**
* Add the root element and the servers element to the given DOMDocument
*
* @param DOMDocument &$document The document to append on
* @param string $rootName The name of the root element
* @return DOMElement The servers element
*/
private static final function addServersRoot(&$document, $rootName) {
$rootElement = $document->createElement($rootName);
$rootElement = $document->appendChild($rootElement);
$serversElement = $document->createElement(static::XML_SERVERS_ELEMENT);
return $rootElement->appendChild($serversElement);
}
/**
* Add a folder to the given container
*
* @param DOMDocument &$document The dom document
* @param DOMElement &$container The container to append on
* @param string $folder The folder name
* @param boolean $expanded Sets the expanded attribute on the folder
* @return DOMElement The folder element
*/
private static final function addFolder(&$document, &$container, $folder, $expanded = false) {
$folderElement = $document->createElement(static::XML_FOLDER_ELEMENT);
$folderElement->setAttribute('expanded', ($expanded ? '1' : '0'));
$nameNode = $document->createTextNode($folder);
$folderElement->appendChild($nameNode);
return $container->appendChild($folderElement);
}
/**
* Add a server to the given container
*
* @param DOMDocument &$document The dom document
* @param DOMElement &$container The container to append on
* @param string $name The server name
* @param string $host The server host
* @param string $protocol The protocol: FTP or SFTP
* @param int $port The server port
* @param string $username The server username
* @param string $password The server password
* @return DOMElement The server element
*/
private static final function addServer(&$document, &$container, $name, $host, $protocol, $port, $username, $password) {
$serverElement = $document->createElement(static::XML_SERVER_ELEMENT);
# Set host node
$hostNode = $serverElement->appendChild($document->createElement('Host'));
$hostNode->appendChild($document->createTextNode($host));
# Set protocol node
$protocolNode = $serverElement->appendChild($document->createElement('Protocol'));
$protocolNode->appendChild($document->createTextNode((strtolower($protocol) == 'sftp') ? 1 : 0));
# Set login type to normal
$logonTypeNode = $serverElement->appendChild($document->createElement('Logontype'));
$logonTypeNode->appendChild($document->createTextNode('1'));
# Set port node
$portNode = $serverElement->appendChild($document->createElement('Port'));
$portNode->appendChild($document->createTextNode($port));
# Set username node
$usernameNode = $serverElement->appendChild($document->createElement('User'));
$usernameNode->appendChild($document->createTextNode($username));
# Set password node
$passwordNode = $serverElement->appendChild($document->createElement('Pass'));
$passwordNode->appendChild($document->createTextNode($password));
$nameNode = $document->createTextNode($name);
$serverElement->appendChild($nameNode);
return $container->appendChild($serverElement);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment