Skip to content

Instantly share code, notes, and snippets.

@thegallagher
Created August 24, 2011 02:49
Show Gist options
  • Save thegallagher/1167196 to your computer and use it in GitHub Desktop.
Save thegallagher/1167196 to your computer and use it in GitHub Desktop.
Filezilla site manager duplicates remover.
<?php
/*
* Filezilla site manager duplicates remover.
* Please backup your sitemanager.xml file before using.
*/
// OPTIONS: change here as required.
define('OUTPUT_XML', true); // If set to true then output the browser, otherwise false to save directly back to the file.
// Uncomment the next define if your not using Windows or if the script doesn't work. Set it to the path to your sitemanager.xml file.
// Alternativly copy your sitemanager.xml to your working directory.
//define('XML_FILE', '');
// Show errors
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Find sitesmanager.xml
if((!defined('XML_FILE') || XML_FILE == ''))
{
$file = './sitemanager.xml';
if(!file_exists($file))
{
$userprofile = getenv('APPDATA');
if($userprofile)
{
$file = $userprofile . '/FileZilla/sitemanager.xml';
}
}
}
else
{
$file = XML_FILE;
}
// Load the file if it exists
if(!file_exists($file))
{
die('<pre>Could not find ' . $file . '.</pre>');
}
$xml = simplexml_load_file($file);
// Find and remove the duplicates
$sites = array();
$children = $xml->Servers[0]->children();
$length = count($children);
$duplicates = 0;
for($i = 0; $i < $length; $i++)
{
$server = $children[$i];
$host = (string) $server->Host;
$user = (string) $server->User;
if(isset($sites[$host][$user]))
{
$dom = dom_import_simplexml($server);
$dom->parentNode->removeChild($dom);
$i--;
$length--;
$duplicates++;
}
else
{
if(!isset($sites[$host]))
{
$sites[$host] = array();
}
$sites[$host][$user] = true;
}
}
// Output new XML to screen or file.
if(!defined('OUTPUT_XML') || !OUTPUT_XML)
{
if($duplicates > 0)
{
$xml->asXml($file);
echo '<pre>Removed ' . $duplicates . ' duplicates.</pre>';
}
else
{
echo '<pre>No duplicates to remove.</pre>';
}
}
else
{
header('Content-Type: text/xml');
echo $xml->asXml();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment