/**
  * plugin uninstaller with best effort depending on what it finds.
  *
  * @param  int     $id
  * @param  string  $option
  * @param  int     $client
  * @param  string  $action
  * @return boolean
  */
 function uninstall($id, $option, $client = 0)
 {
     global $_CB_database;
     $db = false;
     if ($this->checkPluginGetXml($id, $option, $client)) {
         if ($this->i_xmldocument !== null && count($this->i_xmldocument->children()) > 0) {
             $cbInstallXML =& $this->i_xmldocument;
             // get the element name:
             $e =& $cbInstallXML->getElementByPath('name');
             $this->elementName($e->data());
             // $cleanedElementName = strtolower(str_replace(array(" ","."),array("","_"),$this->elementName()));
             // get the files element
             $files_element =& $cbInstallXML->getElementByPath('files');
             if ($files_element) {
                 if (count($files_element->children())) {
                     foreach ($files_element->children() as $file) {
                         if ($file->attributes("plugin")) {
                             $this->elementSpecial($file->attributes("plugin"));
                             break;
                         }
                     }
                     $cleanedMainFileName = strtolower(str_replace(array(" ", "."), array("", "_"), $this->elementSpecial()));
                 }
                 // Is there an uninstallfile
                 $uninstallfile_elemet =& $cbInstallXML->getElementByPath('uninstallfile');
                 if ($uninstallfile_elemet !== false) {
                     if (is_file($this->i_elementdir . $uninstallfile_elemet->data())) {
                         global $_PLUGINS;
                         // needed for the require_once below !
                         require_once $this->i_elementdir . $uninstallfile_elemet->data();
                         $ret = call_user_func_array("plug_" . $cleanedMainFileName . "_uninstall", array());
                         if ($ret != '') {
                             $this->setError(0, $ret);
                         }
                     }
                 }
                 $adminFS =& cbAdminFileSystem::getInstance();
                 foreach ($files_element->children() as $file) {
                     // delete the files
                     $filename = $file->data();
                     if (file_exists($this->i_elementdir . $filename)) {
                         $parts = pathinfo($filename);
                         $subpath = $parts['dirname'];
                         if ($subpath != '' && $subpath != '.' && $subpath != '..') {
                             //echo '<br />'. 'Deleting'  .': '. $this->i_elementdir . $subpath;
                             $result = $adminFS->deldir(_cbPathName($this->i_elementdir . $subpath . '/'));
                         } else {
                             //echo '<br />'. 'Deleting'  .': '. $this->i_elementdir . $filename;
                             $result = $adminFS->unlink(_cbPathName($this->i_elementdir . $filename, false));
                         }
                         //echo intval( $result );
                     }
                 }
                 // Are there any SQL queries??
                 $query_element =& $cbInstallXML->getElementByPath('uninstall/queries');
                 if ($query_element !== false) {
                     foreach ($query_element->children() as $query) {
                         $_CB_database->setQuery(trim($query->data()));
                         if (!$_CB_database->query()) {
                             $this->setError(1, "SQL Error " . $_CB_database->stderr(true));
                             return false;
                         }
                     }
                 }
                 // Are there any Database statements ??
                 $db =& $cbInstallXML->getElementByPath('database');
                 if ($db !== false && count($db->children()) > 0) {
                     cbimport('cb.sql.upgrader');
                     $sqlUpgrader = new CBSQLupgrader($_CB_database, false);
                     //$sqlUpgrader->setDryRun( true );
                     $success = $sqlUpgrader->checkXmlDatabaseDescription($db, $cleanedMainFileName, 'drop', null);
                     /*
                     var_dump( $success );
                     echo "<br>\nERRORS: " . $sqlUpgrader->getErrors( "<br /><br />\n\n", "<br />\n" );
                     echo "<br>\nLOGS: " . $sqlUpgrader->getLogs( "<br /><br />\n\n", "<br />\n" );
                     exit;
                     */
                     if (!$success) {
                         $this->setError(1, "Plugin database XML SQL Error " . $sqlUpgrader->getErrors());
                         return false;
                     }
                 }
                 // Delete tabs and private fields of plugin:
                 $this->deleteTabAndFieldsOfPlugin($id);
                 // remove XML file from front
                 $xmlRemoveResult = $adminFS->unlink(_cbPathName($this->i_installfilename, false));
                 $filesRemoveResult = true;
                 /*					// define folders that should not be removed
                 					$sysFolders = array(
                 					'content',
                 					'search'
                 					);
                 					if ( ! in_array( $row->folder, $sysFolders ) ) {
                 */
                 // delete the non-system folders if empty
                 if (count(cbReadDirectory($this->i_elementdir)) < 1) {
                     $filesRemoveResult = $adminFS->deldir($this->i_elementdir);
                 }
                 /*					}
                 */
                 if (!$xmlRemoveResult) {
                     HTML_comprofiler::showInstallMessage('Could not delete XML file: ' . _cbPathName($this->i_installfilename, false) . ' due to permission error. Please remove manually.', 'Uninstall -  warning', $this->returnTo($option, 'showPlugins'));
                 }
                 if (!$filesRemoveResult) {
                     HTML_comprofiler::showInstallMessage('Could not delete directory: ' . $this->i_elementdir . ' due to permission error. Please remove manually.', 'Uninstall -  warning', $this->returnTo($option, 'showPlugins'));
                 }
             }
         }
         $_CB_database->setQuery("DELETE FROM #__comprofiler_plugin WHERE id = " . (int) $id);
         if (!$_CB_database->query()) {
             $msg = $_CB_database->stderr;
             HTML_comprofiler::showInstallMessage('Cannot delete plugin database entry due to error: ' . $msg, 'Uninstall -  error', $this->returnTo($option, 'showPlugins'));
             return false;
         }
         if ($this->i_xmldocument !== null && $db !== false && count($db->children()) > 0) {
             HTML_comprofiler::fixcbdbShowResults($sqlUpgrader, true, false, $success, array(), array(), $this->elementName(), 1, false);
         }
         return true;
     }
     return false;
 }
function finishInstallation($option)
{
    global $_CB_framework, $ueConfig, $task;
    // Try extending time, as unziping/ftping took already quite some... :
    @set_time_limit(240);
    HTML_comprofiler::secureAboveForm('finishInstallation');
    $tgzFile = $_CB_framework->getCfg('absolute_path') . '/administrator/components/com_comprofiler/pluginsfiles.tgz';
    $installerFile = $_CB_framework->getCfg('absolute_path') . '/administrator/components/com_comprofiler/';
    if (file_exists($installerFile . 'comprofiler.xml')) {
        $installerFile .= 'comprofiler.xml';
    } elseif (file_exists($installerFile . 'comprofilej.xml')) {
        $installerFile .= 'comprofilej.xml';
    } elseif (file_exists($installerFile . 'comprofileg.xml')) {
        $installerFile .= 'comprofileg.xml';
    }
    if (!file_exists($tgzFile)) {
        echo _UE_NOT_AUTHORIZED;
        return;
    }
    $installer = new cbInstallerPlugin();
    $client = 2;
    // Check that the zlib is available
    if (!extension_loaded('zlib')) {
        HTML_comprofiler::showInstallMessage(CBTxt::T('The installer cannot continue before zlib is installed'), CBTxt::T('Installer - Error'), $installer->returnTo($option, $task, $client));
        exit;
    }
    if (!$installer->upload($tgzFile, true, false)) {
        HTML_comprofiler::showInstallMessage(sprintf(CBTxt::T("Uncompressing %s failed."), $tgzFile), CBTxt::T('Installer - Error'), $installer->returnTo($option, '', 2));
        exit;
    }
    $installFrom = $installer->installDir();
    $installTo = $_CB_framework->getCfg('absolute_path') . '/components/com_comprofiler/plugin';
    $filesList = cbReadDirectory($installFrom, '.', true);
    // create directories and remove them from file list:
    if (!$installer->mosMakePath(dirname($installTo) . '/', 'plugin')) {
        HTML_comprofiler::showInstallMessage(sprintf(CBTxt::T('Failed to create directory "%s"'), $installTo . '/plugin'), CBTxt::T('Installer - Error'), $installer->returnTo($option, '', 2));
        exit;
    }
    foreach ($filesList as $k => $file) {
        if (basename($file) != $file) {
            $newdir = dirname($file);
            if (!$installer->mosMakePath($installTo . '/', $newdir)) {
                HTML_comprofiler::showInstallMessage(sprintf(CBTxt::T('Failed to create directory "%s"'), $installTo . '/' . $newdir), CBTxt::T('Installer - Error'), $installer->returnTo($option, '', 2));
                exit;
            }
        }
        if (!is_file($installFrom . '/' . $file)) {
            unset($filesList[$k]);
        }
    }
    $result = $installer->copyFiles($installFrom, $installTo, $filesList, true);
    if ($result === false) {
        HTML_comprofiler::showInstallMessage(sprintf(CBTxt::T("Copying plugin files failed with error: %s"), $installer->getError()), CBTxt::T('Installer - Error'), $installer->returnTo($option, '', 2));
        exit;
    }
    $adminFS =& cbAdminFileSystem::getInstance();
    $result = $adminFS->deldir(_cbPathName($installFrom . '/'));
    if ($result === false) {
        HTML_comprofiler::showInstallMessage(CBTxt::T('Deleting expanded tgz file directory failed with an error.'), CBTxt::T('Installer - Error'), $installer->returnTo($option, '', 2));
    }
    $tgzFileOS = _cbPathName($tgzFile, false);
    $result = $adminFS->unlink($tgzFileOS);
    if ($result === false) {
        HTML_comprofiler::showInstallMessage(sprintf(CBTxt::T("Deleting file %s failed with an error."), $tgzFileOS), CBTxt::T('Installer - Error'), $installer->returnTo($option, '', 2));
    }
    // adapt published fields to global CB config (regarding name type)
    _cbAdaptNameFieldsPublished($ueConfig);
    $htmlToDisplay = $_CB_framework->getUserState('com_comprofiler_install');
    // clears the session buffer memory after installaion done:
    $_CB_framework->setUserState('com_comprofiler_install', '');
    cbimport('cb.xml.simplexml');
    $installerXml = new CBSimpleXMLElement(file_get_contents($installerFile));
    if (is_object($installerXml)) {
        $description = $installerXml->getElementByPath('description');
        if ($description !== false) {
            echo '<h2>' . $description->data() . '</h2>';
        }
    }
    echo $htmlToDisplay;
    ?>
<div style="font-weight:bold;font-size:110%;background:#ffffe4;border:2px green solid;padding:5px;margin-bottom:20px;"><font color="green"><?php 
    echo CBTxt::T('Second and last installation step of Community Builder Component (comprofiler) done successfully.');
    ?>
</font></div><br />
<div style="font-weight:bold;font-size:125%;background:#ffffe4;border:2px green solid;padding:5px;">
<font color="green"><b><?php 
    echo CBTxt::T('Installation finished. Important: Please read README.TXT and installation manual for further settings.');
    ?>
 <br /><br /><?php 
    echo CBTxt::T('We also have a PDF installation guide as well as a complete documentation available on');
    ?>
 <a href="http://www.joomlapolis.com">www.joomlapolis.com</a> <?php 
    echo CBTxt::T('which will help you making the most out of your Community Builder installation, while supporting this project, as well as plugins and templates.');
    ?>
</b></font>
</div>
<?php 
    $_CB_framework->setUserState("com_comprofiler_install", '');
}