/**
  * Installs the plugin By in-place Discovery
  *
  * @param  string   $plgFile  Directory discovered
  * @return boolean            Success
  */
 private function installPluginDisc($plgFile)
 {
     global $_CB_framework;
     // Try extending time, as unziping/ftping took already quite some... :
     @set_time_limit(240);
     _CBsecureAboveForm('showPlugins');
     outputCbTemplate(2);
     outputCbJs(2);
     initToolTip(2);
     $installer = new cbInstallerPlugin();
     // Check if file xml exists
     if (!$plgFile) {
         cbInstaller::showInstallMessage(CBTxt::T('No file selected'), CBTxt::T('Install new plugin from discovery - error'), false);
         return false;
     }
     $path = _cbPathName($_CB_framework->getCfg('absolute_path') . '/components/com_comprofiler/plugin/' . $plgFile);
     if (!is_dir($path)) {
         $path = dirname($path);
     }
     if (!is_dir($path)) {
         cbInstaller::showInstallMessage(CBTxt::T('FILE_DOES_NOT_EXIST_FILE', 'File does not exist - [file]', array('[file]' => $path)), CBTxt::T('INSTALL_NEW_PLUGIN_FROM_DISCOVERY_ERROR', 'Install new plugin from discovery - error'), false);
         return false;
     }
     $ret = $installer->install($path, true);
     cbInstaller::showInstallMessage($installer->getError(), CBTxt::T('INSTALL_NEW_PLUGIN_FROM_DISCOVERY_ERROR_FILE_STATUS', 'Install new plugin from discovery - [file] - [status]', array('[file]' => $path, '[status]' => $ret ? CBTxt::T('Success') : CBTxt::T('Failed'))), $ret);
     return $ret;
 }
function finishInstallation($option)
{
    global $_CB_framework, $ueConfig, $task;
    // Try extending time, as unziping/ftping took already quite some... :
    @set_time_limit(240);
    _CBsecureAboveForm('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 CBTxt::T('UE_NOT_AUTHORIZED', 'You are not authorized to view this page!');
        return;
    }
    $installer = new cbInstallerPlugin();
    $client = 2;
    // Check that the zlib is available
    if (!extension_loaded('zlib')) {
        cbInstaller::renderInstallMessage(CBTxt::T('The installer cannot continue before zlib is installed'), CBTxt::T('Installer - Error'), $installer->returnTo($option, $task, $client));
        return;
    }
    if (!$installer->upload($tgzFile, true, false)) {
        cbInstaller::renderInstallMessage(sprintf(CBTxt::T('Uncompressing %s failed.'), $tgzFile), CBTxt::T('Installer - Error'), $installer->returnTo($option, '', 2));
        return;
    }
    $adminFS = cbAdminFileSystem::getInstance();
    $installFrom = $installer->installDir();
    $filesList = cbReadDirectory($installFrom, '.', true);
    // check if core directories exist as are needed to install plugins:
    $baseDir = $_CB_framework->getCfg('absolute_path') . '/components/com_comprofiler';
    if (!$adminFS->is_dir($baseDir . '/plugin')) {
        if (!$adminFS->mkdir($baseDir . '/plugin')) {
            cbInstaller::renderInstallMessage(sprintf(CBTxt::T('Failed to create directory "%s"'), $baseDir . '/plugin'), CBTxt::T('Installer - Error'), $installer->returnTo($option, '', 2));
            return;
        }
        if (!$adminFS->copy($baseDir . '/index.html', $baseDir . '/plugin/index.html')) {
            cbInstaller::renderInstallMessage(sprintf(CBTxt::T('Failed to create index "%s"'), $baseDir . '/plugin/index.html'), CBTxt::T('Installer - Error'), $installer->returnTo($option, '', 2));
            return;
        }
    }
    if (!$adminFS->is_dir($baseDir . '/plugin/language')) {
        if (!$adminFS->mkdir($baseDir . '/plugin/language')) {
            cbInstaller::renderInstallMessage(sprintf(CBTxt::T('Failed to create directory "%s"'), $baseDir . '/plugin/language'), CBTxt::T('Installer - Error'), $installer->returnTo($option, '', 2));
            return;
        }
        if (!$adminFS->copy($baseDir . '/index.html', $baseDir . '/plugin/language/index.html')) {
            cbInstaller::renderInstallMessage(sprintf(CBTxt::T('Failed to create index "%s"'), $baseDir . '/plugin/language/index.html'), CBTxt::T('Installer - Error'), $installer->returnTo($option, '', 2));
            return;
        }
    }
    if (!$adminFS->is_dir($baseDir . '/plugin/templates')) {
        if (!$adminFS->mkdir($baseDir . '/plugin/templates')) {
            cbInstaller::renderInstallMessage(sprintf(CBTxt::T('Failed to create directory "%s"'), $baseDir . '/plugin/templates'), CBTxt::T('Installer - Error'), $installer->returnTo($option, '', 2));
            return;
        }
        if (!$adminFS->copy($baseDir . '/index.html', $baseDir . '/plugin/templates/index.html')) {
            cbInstaller::renderInstallMessage(sprintf(CBTxt::T('Failed to create index "%s"'), $baseDir . '/plugin/templates/index.html'), CBTxt::T('Installer - Error'), $installer->returnTo($option, '', 2));
            return;
        }
    }
    if (!$adminFS->is_dir($baseDir . '/plugin/user')) {
        if (!$adminFS->mkdir($baseDir . '/plugin/user')) {
            cbInstaller::renderInstallMessage(sprintf(CBTxt::T('Failed to create directory "%s"'), $baseDir . '/plugin/user'), CBTxt::T('Installer - Error'), $installer->returnTo($option, '', 2));
            return;
        }
        if (!$adminFS->copy($baseDir . '/index.html', $baseDir . '/plugin/user/index.html')) {
            cbInstaller::renderInstallMessage(sprintf(CBTxt::T('Failed to create index "%s"'), $baseDir . '/plugin/user/index.html'), CBTxt::T('Installer - Error'), $installer->returnTo($option, '', 2));
            return;
        }
    }
    // install core plugins:
    foreach ($filesList as $file) {
        if (preg_match('/^.+\\.xml$/i', $file)) {
            $plgPath = $installFrom . (substr($installFrom, -1, 1) == '/' ? '' : '/') . $file;
            $plgXml = new \CBLib\Xml\SimpleXMLElement(trim(file_get_contents($plgPath)));
            if ($plgXml->getName() == 'cbinstall') {
                $plgDir = dirname($plgPath) . '/';
                $plgInstaller = new cbInstallerPlugin();
                if (!$plgInstaller->install($plgDir)) {
                    cbInstaller::renderInstallMessage(sprintf(CBTxt::T('Installing plugin failed with error: %s : %s'), $plgInstaller->i_elementname ? $plgInstaller->i_elementname : $file, $plgInstaller->getError()), CBTxt::T('Installer - Error'), $installer->returnTo($option, '', 2));
                    return;
                }
            }
        }
    }
    $result = $adminFS->deldir(_cbPathName($installFrom . '/'));
    if ($result === false) {
        cbInstaller::renderInstallMessage(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) {
        cbInstaller::renderInstallMessage(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', '');
    $installerXml = new SimpleXMLElement(file_get_contents($installerFile));
    if (is_object($installerXml)) {
        $description = $installerXml->getElementByPath('description');
        if ($description !== false) {
            echo '<h2>' . $description->data() . '</h2>';
        }
    }
    echo $htmlToDisplay;
    echo '<div style="color:green;font-size:18px;font-weight:bold;margin-top:15px;margin-bottom:15px;">' . CBTxt::Th('Installation done.') . '</div>' . '<div style="color:green;font-size:18px;font-weight:bold;margin-top:15px;margin-bottom:15px;">' . CBTxt::Th('Now is a great time to checkout the <a href="[help_url]" target="_blank">Getting Started</a> resources.', null, array('[help_url]' => 'http://www.joomlapolis.com/documentation/community-builder/getting-started?pk_campaign=in-cb&amp;pk_kwd=installedwelcomescreen')) . '</div>' . '<div style="margin-bottom:10px;">' . '<div style="font-size:12px;"><a href="http://www.joomlapolis.com/cb-solutions?pk_campaign=in-cb&amp;pk_kwd=installedwelcomescreen" target="_blank">' . CBTxt::Th('Click here to see more CB Plugins (Languages, Fields, Tabs, Signup-Connect, Paid Memberships and over 30 more) by CB Team at joomlapolis.com') . '</a></div>' . '<div style="font-size:12px;"><a href="http://extensions.joomla.org/extensions/clients-a-communities/communities/210" target="_blank">' . CBTxt::Th('Click here to see our CB listing on the Joomla! Extensions Directory (JED) and find third-party add-ons for your website.') . '</a></div>' . '<div style="font-size:12px;margin:10px 0 25px;">or &nbsp; <a href="index.php?option=com_comprofiler&view=showconfig" class="btn btn-primary">' . CBTxt::Th('Start to Configure Community Builder') . '</a></div>' . '</div>';
    $_CB_framework->setUserState("com_comprofiler_install", '');
}