/**
 * create_pluginsxml
 * 
 * If the plugins.xml file does not exists, read in each plugin 
 * and add it to the file. 
 * read_pluginsxml() is called again to repopulate $live_plugins
 *
 * @since 2.04
 * @uses $live_plugins
 *
 */
function create_pluginsxml($force = false)
{
    global $live_plugins;
    if (file_exists(GSPLUGINPATH)) {
        $pluginfiles = getFiles(GSPLUGINPATH);
    }
    $phpfiles = array();
    foreach ($pluginfiles as $fi) {
        if (lowercase(pathinfo($fi, PATHINFO_EXTENSION)) == 'php') {
            $phpfiles[] = $fi;
        }
    }
    if (!$force) {
        $livekeys = array_keys($live_plugins);
        if (count(array_diff($livekeys, $phpfiles)) > 0 || count(array_diff($phpfiles, $livekeys)) > 0) {
            $force = true;
        }
    }
    if ($force) {
        $xml = @new SimpleXMLExtended('<?xml version="1.0" encoding="UTF-8"?><channel></channel>');
        foreach ($phpfiles as $fi) {
            $plugins = $xml->addChild('item');
            $p_note = $plugins->addChild('plugin');
            $p_note->addCData($fi);
            $p_note = $plugins->addChild('enabled');
            if (isset($live_plugins[(string) $fi])) {
                $p_note->addCData($live_plugins[(string) $fi]);
            } else {
                $p_note->addCData('false');
            }
        }
        XMLsave($xml, GSDATAOTHERPATH . "plugins.xml");
        read_pluginsxml();
    }
}
/**
 * create_pluginsxml
 * 
 * If the plugins.xml file does not exists, read in each plugin 
 * and add it to the file. 
 * read_pluginsxml() is called again to repopulate $live_plugins
 *
 * @since 2.04
 * @uses $live_plugins
 *
 */
function create_pluginsxml()
{
    global $live_plugins;
    if (file_exists(GSPLUGINPATH)) {
        $pluginfiles = getFiles(GSPLUGINPATH);
    }
    $xml = @new SimpleXMLExtended('<?xml version="1.0" encoding="UTF-8"?><channel></channel>');
    foreach ($pluginfiles as $fi) {
        $pathExt = pathinfo($fi, PATHINFO_EXTENSION);
        $pathName = pathinfo_filename($fi);
        $count = 0;
        if ($pathExt == "php") {
            $components = $xml->addChild('item');
            $c_note = $components->addChild('plugin');
            $c_note->addCData($fi);
            $c_note = $components->addChild('enabled');
            if (isset($live_plugins[(string) $fi])) {
                $c_note->addCData($live_plugins[(string) $fi]);
            } else {
                $c_note->addCData('true');
            }
        }
    }
    XMLsave($xml, GSDATAOTHERPATH . "plugins.xml");
    read_pluginsxml();
}
/**
 * create_pluginsxml
 * 
 * Read in each plugin php file and add it to the plugins.xml file.
 * read_pluginsxml() is called to populate $live_plugins
 *
 * Does nothing if force is false and no file diff found
 * @todo  if this gets called before live plugins is loaded it will wipe your activated plugin state
 *
 * @since 2.04
 * @uses $live_plugins
 *
 * @param  bool $force force an update of plugins.xml regardless of diff check
 *
 */
function create_pluginsxml($force = false)
{
    global $live_plugins;
    $pluginfiles = array();
    $success = false;
    if (file_exists(GSPLUGINPATH)) {
        $pluginfiles = getFiles(GSPLUGINPATH, 'php');
    } else {
        return;
    }
    // plugin files path issue
    if (!$force) {
        $livekeys = array_keys($live_plugins);
        // check for file diff and use force to regen if count differs @todo better detection than just count
        if (count(array_diff($livekeys, $pluginfiles)) > 0 || count(array_diff($pluginfiles, $livekeys)) > 0) {
            $force = true;
        }
    }
    // create plugins.xml if missing or updating
    if ($force) {
        $xml = @new SimpleXMLExtended('<?xml version="1.0" encoding="UTF-8"?><channel></channel>');
        foreach ($pluginfiles as $fi) {
            $plugins = $xml->addChild('item');
            $p_note = $plugins->addChild('plugin');
            $p_note->addCData($fi);
            $p_note = $plugins->addChild('enabled');
            // check live_plugins and set enables
            if (isset($live_plugins[(string) $fi])) {
                $p_note->addCData($live_plugins[(string) $fi]);
            } else {
                $p_note->addCData('false');
            }
        }
        $success = XMLsave($xml, GSDATAOTHERPATH . "plugins.xml");
        read_pluginsxml($xml);
    }
    return $success;
}