Skip to content

Instantly share code, notes, and snippets.

@jtai
Created November 14, 2011 10:21
Show Gist options
  • Save jtai/1363677 to your computer and use it in GitHub Desktop.
Save jtai/1363677 to your computer and use it in GitHub Desktop.
Script to convert Openfire user export file to Prosody's file format
<?php
if ($argc < 3) {
die("Usage: openfire2prosody.php INPUT_XML OUTPUT_PATH\n");
}
$xml = simplexml_load_file($argv[1]);
foreach (array($argv[2], $argv[2] . '/accounts', $argv[2] . '/roster') as $path) {
if (!is_dir($path)) {
mkdir($path);
}
}
foreach ($xml->User as $user) {
$account = 'return { ["password"] = "' . q($user->Password) . '"; }';
file_put_contents($argv[2] . '/accounts/' . $user->Username . '.dat', $account);
$roster = 'return { [false] = { ["version"] = 1; }; ';
foreach ($user->Roster[0]->Item as $item) {
/*
* <http://www.igniterealtime.org/projects/openfire/plugins/userimportexport/readme.html>
* substatus
* -1 Indicates the roster item should be removed.
* 0 No subscription is established.
*/
if ((int) $item['substatus'] == -1 || (int) $item['substatus'] == 0) {
continue;
}
$roster .= '["' . q($item['jid']) . '"] = { ';
// nickname
$roster .= '["name"] = "' . q($item['name']) . '"; ';
/*
* <http://www.igniterealtime.org/projects/openfire/plugins/userimportexport/readme.html>
* substatus
* 1 The roster owner has a subscription to the roster item's presence.
* 2 The roster item has a subscription to the roster owner's presence.
* 3 The roster item and owner have a mutual subscription.
*/
$substatuses = array(
1 => 'to',
2 => 'from', // not tested
3 => 'both',
);
$substatus = $substatuses[(int) $item['substatus']];
$roster .= '["subscription"] = "' . $substatus . '"; ';
// groups
$roster .= '["groups"] = { ';
foreach ($item->Group as $group) {
$roster .= '["' . q($group) . '"] = true; ';
}
$roster .= '}; ';
$roster .= '}; '; // end roster item for $item['jid']
}
$roster .= '["pending"] = { ';
foreach ($user->Roster[0]->Item as $item) {
/*
* <http://www.igniterealtime.org/projects/openfire/plugins/userimportexport/readme.html>
* askstatus
* 0 The roster item has been asked for permission to subscribe to their presence but no response has been received.
*/
if ((int) $item['askstatus'] != 0) {
continue;
}
$roster .= '["' . q($item['jid']) . '"] = true; ';
}
$roster .= '}; '; // end pending item
$roster .= '}';
file_put_contents($argv[2] . '/roster/' . $user->Username . '.dat', $roster);
}
function q($string) {
return str_replace('"', '\\"', $string);
}
@jtai
Copy link
Author

jtai commented Nov 14, 2011

The input file is generated with Openfire's user export plugin. http://www.igniterealtime.org/projects/openfire/plugins/userimportexport/readme.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment