#!/usr/bin/php
<?PHP
if(count($argv) < 3) {
    echo 
"first parameter needs to be the from wildfire exported user file xml. Named like\n";
    echo 
"<REALM>.xml, e.g. jabber.thefungamer.de.xml\n";
    echo 
"The second parameter needs to be the path to the spool folder which will be filled with\n";
    echo 
"the created user xml files.\n";
    exit;
}

if(!
file_exists($argv[1])) {
    echo 
"Wildfire Export User File not found.\n";
    exit;
}

if(!
is_dir($argv[2])) {
    echo 
"second Parameter is not a directory.\n";
    exit;
}
$xml File($argv[1]);
$xml implode("\n"$xml);
$xml preg_replace_callback("/>[^<]*</""innerXMLtoHtmlEntites"$xml);
$xml simplexml_load_string($xml);

$realm basename($argv[1]);
$realm substr($realm0strrpos($realm"."));
$spooldir $argv[2]."/".$realm;

echo 
"creating userfiles for Realm: '$realm' in Directory: '$spooldir' ...\n";

@
mkdir($spooldir);
foreach (
$xml->User as $user) {
    echo 
"creating userfile for User: '".$user->Username."' ...\n";
    
$userfile fopen("$spooldir/".$user->Username.".xml""w");
    
$text "<xdb>\n";
    
$text .= "\t<password xmlns='jabber:iq:auth' xdbns='jabber:iq:auth'>".$user->Password."</password>\n";
    
$text .= "\t<query xmlns='jabber:iq:register' xdbns='jabber:iq:register'>\n";
    
$text .= "\t\t<username>".$user->Username."</username>\n";
    
$text .= "\t\t<password xmlns='jabber:iq:auth'>".$user->Password."</password>\n";
    
$text .= "\t</query>\n";
    
$text .= "\t<query xmlns='jabber:iq:roster' xdbns='jabber:iq:roster'>\n";
    
fwrite($userfile$text);

    foreach (
$user->Roster->Item as $item) {
        
$jid "";
        
$name "";
        
$subscription "";
        foreach (
$item->Attributes() as $key=>$value) {
            if(
$key == "jid")
                
$jid $value;
            else if(
$key == "name")
                
$name htmlentities($valueENT_QUOTES);
            else if(
$key == "substatus" && $value == && $subscription == "")
                
$subscription "both";
            else if(
$key == "recvstatus" && $value != -&& $subscription == "")
                
$subscription "from";
            else if(
$key == "askstatus" && $value != -&& $subscription == "")
                
$subscription "to";
        }
         
        
$text "              <item jid='$jid' name='$name' subscription='$subscription'><group>".htmlentities($item->GroupENT_QUOTES)."</group></item>\n";
        
fwrite($userfile$text);
    }
    
$text "\t</query>\n";
    
$text .= "</xdb>\n";
    
fwrite($userfile$text);
    
fclose($userfile);
}


function 
innerXMLtoHtmlEntites($text)
{
    
$text trim($text[0], "><\n\r\t ");
    
$text htmlentities($textENT_QUOTES);
    return 
">$text<";
}
?>