/**
  * Constructor (do not call directly, use DI to call it)
  *
  * Code in here was previously in plugin.foundation.php
  *
  * @param  ApplicationContainerInterface  $di  (This is injected by DI)
  */
 function __construct(ApplicationContainerInterface $di)
 {
     if (self::$loaded) {
         return;
     }
     self::$loaded = true;
     define('_CB_JQUERY_VERSION', '1.11.2');
     // IMPORTANT: when changing version here also change in the 2 XML installation files
     /**
      * CB GLOBALS and initializations
      */
     global $mainframe;
     $mainframe = JFactory::getApplication();
     /** @noinspection PhpDeprecationInspection */
     $acl = JFactory::getACL();
     $sefFunc = array('JRoute', '_');
     $getVarFunction = array('JRequest', 'getVar');
     $Jdocument = JFactory::getDocument();
     if ($Jdocument->getType() == 'html') {
         $getDocFunction = array('JFactory', 'getDocument');
     } else {
         $getDocFunction = false;
     }
     $editor = JFactory::getEditor();
     $editorDisplay = array('display' => array('call' => array($editor, 'display'), 'args' => 'noid'), 'save' => array('call' => array($editor, 'save'), 'args' => 'noid'), 'returns' => true);
     $aclParams = array('canEditUsers' => array('com_comprofiler', 'core.edit', 'users', null), 'canBlockUsers' => array('com_comprofiler', 'core.edit.state', 'users', null), 'canReceiveAdminEmails' => array('com_comprofiler', 'core.admin', 'users', null), 'canEditOwnContent' => array('com_content', 'core.edit.own', 'users', null, 'content', 'own'), 'canAddAllContent' => array('com_content', 'core.create', 'users', null, 'content', 'all'), 'canEditAllContent' => array('com_content', 'core.edit', 'users', null, 'content', 'all'), 'canPublishContent' => array('com_content', 'core.edit.state', 'users', null, 'content', 'all'), 'canInstallPlugins' => array('com_installer', 'core.manage', 'users', null), 'canManageUsers' => array('com_comprofiler', 'core.manage', 'users', null));
     /**
      * CB framework
      * @global CBframework $_CB_framework
      */
     global $_CB_framework;
     $_CB_framework = new CBframework($mainframe, $aclParams, $sefFunc, array('option' => 'com_comprofiler'), $getVarFunction, $getDocFunction, $editorDisplay);
     /** @see CBACL */
     $_CB_framework->acl = $di->get('CBACL', array('acl' => $acl));
     $di->set('CBFramework', $_CB_framework, true);
     /**
      * CB Config
      * @deprecated 2.0, use Application::Config() to get the Configuration Registry object
      * @see Application::Config()
      * @var array
      */
     global $ueConfig;
     $ueConfig['version'] = CBLIB;
     // This doesn't load if yet if in installer, as config database table may not yet be created:
     CBConfig::loadLegacyCBueConfig(true);
     // Lazy-loads later the language files:
     // This is useful if CBLib is loaded too early by plugins before language selection happened in Joomla (solves bug #5360).
     $di->get('Language')->addLanguageFile(function () use($di) {
         if ($di->getCms()->getClientId() === 0) {
             cbimport('language.front');
         } else {
             cbimport('language.all');
         }
     });
     define('_CB_SPOOFCHECKS', isset($ueConfig['enableSpoofCheck']) && $ueConfig['enableSpoofCheck'] ? 1 : 0);
 }
 /**
  * Initialization function
  *
  * @return ApplicationContainerInterface
  */
 public static function init()
 {
     AutoLoader::setup();
     new LegacyLoader();
     return CBLib::createApplication('Web', null, function (ApplicationContainerInterface $app) {
         return CBConfig::getConfig($app->getDatabase());
     });
     // done in plugin.foundation.php for now:
     // Application::DI->get( 'LegacyFoundationFunctions' );
 }
 public function install($parent)
 {
     global $_CB_framework, $_CB_adminpath, $ueConfig;
     // Ensure PHP version is adaquete for CB:
     if (version_compare(phpversion(), '5.3.3', '<')) {
         JFactory::getApplication()->enqueueMessage(sprintf('As stated in README and prerequisites, PHP Version %s, which is obsolete since before 2009-11-19 and insecure, is not compatible with %s: Please upgrade to PHP %s or greater (CB is also compatible with PHP 5.4 and 5.5) before installing Community Builder.', phpversion(), 'Community Builder', sprintf('at least version %s, recommended version %s', '5.3.1', '5.3.10')), 'error');
         JFactory::getApplication()->enqueueMessage(sprintf('Installation failed. In all cases, please require your hoster to upgrade your PHP version as soon as possible.'), 'error');
         return false;
     }
     // Initialize CB Appplication library (which has delayed config lazy loading):
     if (is_readable(JPATH_SITE . '/libraries/CBLib/CB/Application/CBApplication.php')) {
         /** @noinspection PhpIncludeInspection */
         include_once JPATH_SITE . '/libraries/CBLib/CB/Application/CBApplication.php';
         \CB\Application\CBApplication::init();
     } else {
         JFactory::getApplication()->enqueueMessage("Mandatory Community Builder lib_CBLib not installed!", 'error');
         return false;
     }
     // Determine path to CBs backend file structure:
     $_CB_adminpath = JPATH_ADMINISTRATOR . '/components/com_comprofiler/';
     // Disable loading of config immediately after foundation include as the db may not exist yet:
     \CB\Application\CBConfig::setCbConfigReadyToLoad(false);
     // Check if CBLib can load:
     /** @noinspection PhpIncludeInspection */
     if (false === (include_once $_CB_adminpath . 'plugin.foundation.php')) {
         return false;
     }
     // Check if CBLib is up to date:
     if (version_compare(constant('CBLIB'), $ueConfig['version'], '<')) {
         JFactory::getApplication()->enqueueMessage(sprintf('Community Builder library lib_CBLib version %s is older than the Community Builder version %s tried to be installed.', constant('CBLIB'), $ueConfig['version']), 'error');
         return false;
     }
     // Set location to backend:
     $_CB_framework->cbset('_ui', 2);
     if ($_CB_framework->getCfg('debug')) {
         ini_set('display_errors', true);
         error_reporting(E_ALL);
     }
     // Load in CB API:
     cbimport('cb.tabs');
     cbimport('cb.adminfilesystem');
     cbimport('cb.dbchecker');
     // Define CB backend filesystem API:
     $adminFS = cbAdminFileSystem::getInstance();
     // Delete removed files on upgrade:
     $filesToDelete = array($_CB_adminpath . 'comprofileg.xml', $_CB_adminpath . 'comprofilej.xml', $_CB_adminpath . 'admin.comprofiler.php', $_CB_adminpath . 'ue_config_first.php');
     foreach ($filesToDelete as $deleteThisFile) {
         if ($adminFS->file_exists($deleteThisFile)) {
             $adminFS->unlink($deleteThisFile);
         }
     }
     $liveSite = $_CB_framework->getCfg('live_site');
     $return = '<div style="margin-bottom:10px;width:100%;text-align:center;"><img alt="' . htmlspecialchars(CBTxt::T('CB Logo')) . '" src="' . $liveSite . '/components/com_comprofiler/images/smcblogo.gif" /></div>' . '<div style="font-size:14px;margin-bottom:10px;">Copyright 2004-2015 Joomlapolis.com. ' . CBTxt::T('This component is released under the GNU/GPL version 2 License. All copyright statements must be kept. Derivate work must prominently duly acknowledge original work and include visible online links.') . '</div>';
     $cbDatabase = \CBLib\Application\Application::Database();
     // Core database fixes:
     $dbChecker = new \CB\Database\CBDatabaseChecker($cbDatabase);
     $result = $dbChecker->checkDatabase(true, false, null, null);
     if ($result == true) {
         // All ok, Nothing to alarm user here:
         // $return							.=	'<div style="font-size:18px;color:green;margin-bottom:10px;">' . CBTxt::T( 'Automatic database upgrade applied successfully.' ) . '</div>';
     } elseif (is_string($result)) {
         $return .= '<div style="font-size:18px;color:red;margin-bottom:10px;">' . $result . '</div>';
     } else {
         $errors = $dbChecker->getErrors(false);
         if ($errors) {
             $return .= '<div style="color:red;margin-bottom:10px;">' . '<div style="font-size:18px;font-weight:bold;padding-bottom:5px;margin-bottom:5px;border-bottom:1px solid red;">' . CBTxt::T('Database fixing errors') . '</div>';
             foreach ($errors as $error) {
                 $return .= '<div style="margin-bottom:10px;">' . '<div style="font-size:14px;">' . $error[0] . '</div>';
                 if ($error[1]) {
                     $return .= '<div style="font-size:12px;text-indent:15px;">' . $error[1] . '</div>';
                 }
                 $return .= '</div>';
             }
             $return .= '</div>';
         }
     }
     if ($_CB_framework->getCfg('session_handler') != 'database') {
         $logs = $dbChecker->getLogs(false);
         if (count($logs) > 0) {
             $return .= '<div style="margin-bottom:10px;">' . '<div style="font-size:14px;margin-bottom:5px;">' . '<a href="javascript: void(0);" id="cbdetailsLinkShow" onclick="this.style.display=\'none\';document.getElementById(\'cbdetailsdbcheck\').style.display=\'block\';document.getElementById(\'cbdetailsLinkHide\').style.display=\'block\';return false;">' . CBTxt::T('Click to Show details') . '</a>' . '<a href="javascript: void(0);" id="cbdetailsLinkHide" onclick="this.style.display=\'none\';document.getElementById(\'cbdetailsdbcheck\').style.display=\'block\';document.getElementById(\'cbdetailsLinkShow\').style.display=\'block\';return false;" style="display:none;">' . CBTxt::T('Click to Hide details') . '</a>' . '</div>' . '<div id="cbdetailsdbcheck" style="dsiplay:none;color:green;">';
             foreach ($logs as $log) {
                 $return .= '<div style="margin-bottom:10px;">' . '<div style="font-size:14px;">' . $log[0] . '</div>';
                 if ($log[1]) {
                     $return .= '<div style="font-size:12px;text-indent:15px;">' . $log[1] . '</div>';
                 }
                 $return .= '</div>';
             }
             $return .= '</div>' . '</div>';
         }
     }
     // Fix old 1.x usergroups-based permissions to 2.x access-levels in lists and in tabs:
     $this->convertUserGroupsToViewAccessLevels(new \CB\Database\Table\TabTable(), 'CB Tab access');
     $this->convertUserGroupsToViewAccessLevels(new \CB\Database\Table\ListTable(), 'CB Users list access');
     // Synchronize users to CB:
     $query = 'INSERT IGNORE INTO ' . $cbDatabase->NameQuote('#__comprofiler') . "\n (" . $cbDatabase->NameQuote('id') . ', ' . $cbDatabase->NameQuote('user_id') . ')' . "\n SELECT " . $cbDatabase->NameQuote('id') . ', ' . $cbDatabase->NameQuote('id') . "\n FROM " . $cbDatabase->NameQuote('#__users');
     $cbDatabase->setQuery($query);
     if (!$cbDatabase->query()) {
         $cbSpoofField = cbSpoofField();
         $cbSpoofString = cbSpoofString(null, 'plugin');
         $return .= '<div style="font-size:14px;color:red;margin-bottom:10px;">' . CBTxt::P('User synchronization failed. Please <a href="[url]" target="_blank">click here</a> to manually synchronize.', array('[url]' => $_CB_framework->backendUrl("index.php?option=com_comprofiler&view=syncUsers&{$cbSpoofField}={$cbSpoofString}"))) . '</div>';
     }
     // Fix images:
     $imagesPath = $_CB_framework->getCfg('absolute_path') . '/images';
     $cbImages = $imagesPath . '/comprofiler';
     $cbImagesGallery = $cbImages . '/gallery';
     $cbImagesCanvasGallery = $cbImages . '/gallery/canvas';
     if ($adminFS->isUsingStandardPHP() && !$adminFS->file_exists($cbImages) && !$adminFS->is_writable($_CB_framework->getCfg('absolute_path') . '/images/')) {
         $return .= '<div style="font-size:14px;color:red;margin-bottom:10px;">' . CBTxt::P('[path]/ is not writable.', array('[path]' => $imagesPath)) . '</div>';
     } else {
         if (!$adminFS->file_exists($cbImages)) {
             if ($adminFS->mkdir($cbImages)) {
                 // All ok, Nothing to alarm user here.
                 // $return						.=	'<div style="font-size:14px;color:green;margin-bottom:10px;">' . CBTxt::P( '[path]/ successfully added.', array( '[path]' => $cbImages ) ) . '</div>';
             } else {
                 $return .= '<div style="font-size:14px;color:red;margin-bottom:10px;">' . CBTxt::P('[path]/ failed to create, please do so manually.', array('[path]' => $cbImages)) . '</div>';
             }
         }
         if (!$adminFS->file_exists($cbImagesGallery)) {
             if ($adminFS->mkdir($cbImagesGallery)) {
                 // All ok, Nothing to alarm user here:
                 // $return					.=	'<div style="font-size:14px;color:green;margin-bottom:10px;">' . CBTxt::P( '[path]/ successfully added.', array( '[path]' => $cbImagesGallery ) ) . '</div>';
             } else {
                 $return .= '<div style="font-size:14px;color:red;margin-bottom:10px;">' . CBTxt::P('[path]/ failed to create, please do so manually.', array('[path]' => $cbImagesGallery)) . '</div>';
             }
         }
         if (!$adminFS->file_exists($cbImagesCanvasGallery)) {
             if ($adminFS->mkdir($cbImagesCanvasGallery)) {
                 // All ok, Nothing to alarm user here:
                 // $return					.=	'<div style="font-size:14px;color:green;margin-bottom:10px;">' . CBTxt::P( '[path]/ successfully added.', array( '[path]' => $cbImagesCanvasGallery ) ) . '</div>';
             } else {
                 $return .= '<div style="font-size:14px;color:red;margin-bottom:10px;">' . CBTxt::P('[path]/ failed to create, please do so manually.', array('[path]' => $cbImagesCanvasGallery)) . '</div>';
             }
         }
         if ($adminFS->file_exists($cbImages)) {
             if (!is_writable($cbImages)) {
                 if (!$adminFS->chmod($cbImages, 0775)) {
                     if (!@chmod($cbImages, 0775)) {
                         $return .= '<div style="font-size:14px;color:red;margin-bottom:10px;">' . CBTxt::P('[path]/ failed to chmod to 775 please do so manually.', array('[path]' => $cbImages)) . '</div>';
                     }
                 }
             }
             if (!is_writable($cbImages)) {
                 $return .= '<div style="font-size:14px;color:red;margin-bottom:10px;">' . CBTxt::P('[path]/ is not writable and failed to chmod to 775 please do so manually.', array('[path]' => $cbImages)) . '</div>';
             }
             if (!$adminFS->file_exists($cbImages . '/index.html')) {
                 $result = @copy($imagesPath . '/index.html', $cbImages . '/index.html');
                 if (!$result) {
                     $result = $adminFS->copy($imagesPath . '/index.html', $cbImages . '/index.html');
                 }
                 if (!$result) {
                     $return .= '<div style="font-size:14px;color:red;margin-bottom:10px;">' . CBTxt::P('index.html failed to be added to [path] please do so manually.', array('[path]' => $cbImages)) . '</div>';
                 }
             }
         }
         if ($adminFS->file_exists($cbImagesGallery)) {
             if (!is_writable($cbImagesGallery)) {
                 if (!$adminFS->chmod($cbImagesGallery, 0775)) {
                     if (!@chmod($cbImagesGallery, 0775)) {
                         $return .= '<div style="font-size:14px;color:red;margin-bottom:10px;">' . CBTxt::P('[path]/ failed to chmod to 775 please do so manually.', array('[path]' => $cbImagesGallery)) . '</div>';
                     }
                 }
             }
             if (!is_writable($cbImagesGallery)) {
                 $return .= '<div style="font-size:14px;color:red;margin-bottom:10px;">' . CBTxt::P('[path]/ is not writable and failed to chmod to 775 please do so manually.', array('[path]' => $cbImagesGallery)) . '</div>';
             }
             $galleryPath = $_CB_framework->getCfg('absolute_path') . '/components/com_comprofiler/images/gallery';
             $galleryDir = @opendir($galleryPath);
             $galleryFiles = array();
             while (true == ($file = @readdir($galleryDir))) {
                 if ($file != '.' && $file != '..') {
                     $galleryFiles[] = $file;
                 }
             }
             @closedir($galleryDir);
             foreach ($galleryFiles as $galleryFile) {
                 if (!(file_exists($cbImagesGallery . '/' . $galleryFile) && is_readable($cbImagesGallery . '/' . $galleryFile))) {
                     $result = @copy($galleryPath . '/' . $galleryFile, $cbImagesGallery . '/' . $galleryFile);
                     if (!$result) {
                         $result = $adminFS->copy($galleryPath . '/' . $galleryFile, $cbImagesGallery . '/' . $galleryFile);
                     }
                     if (!$result) {
                         $return .= '<div style="font-size:14px;color:red;margin-bottom:10px;">' . CBTxt::P('[file] failed to be added to the gallery please do so manually.', array('[file]' => $galleryFile)) . '</div>';
                     }
                 }
             }
             if (!$adminFS->file_exists($cbImagesGallery . '/index.html')) {
                 $result = @copy($imagesPath . '/index.html', $cbImagesGallery . '/index.html');
                 if (!$result) {
                     $result = $adminFS->copy($imagesPath . '/index.html', $cbImagesGallery . '/index.html');
                 }
                 if (!$result) {
                     $return .= '<div style="font-size:14px;color:red;margin-bottom:10px;">' . CBTxt::P('index.html failed to be added to [path] please do so manually.', array('[path]' => $cbImagesGallery)) . '</div>';
                 }
             }
         }
         if ($adminFS->file_exists($cbImagesCanvasGallery)) {
             if (!is_writable($cbImagesCanvasGallery)) {
                 if (!$adminFS->chmod($cbImagesCanvasGallery, 0775)) {
                     if (!@chmod($cbImagesCanvasGallery, 0775)) {
                         $return .= '<div style="font-size:14px;color:red;margin-bottom:10px;">' . CBTxt::P('[path]/ failed to chmod to 775 please do so manually.', array('[path]' => $cbImagesCanvasGallery)) . '</div>';
                     }
                 }
             }
             if (!is_writable($cbImagesCanvasGallery)) {
                 $return .= '<div style="font-size:14px;color:red;margin-bottom:10px;">' . CBTxt::P('[path]/ is not writable and failed to chmod to 775 please do so manually.', array('[path]' => $cbImagesCanvasGallery)) . '</div>';
             }
             $galleryPath = $_CB_framework->getCfg('absolute_path') . '/components/com_comprofiler/images/gallery/canvas';
             $galleryDir = @opendir($galleryPath);
             $galleryFiles = array();
             while (true == ($file = @readdir($galleryDir))) {
                 if ($file != '.' && $file != '..') {
                     $galleryFiles[] = $file;
                 }
             }
             @closedir($galleryDir);
             foreach ($galleryFiles as $galleryFile) {
                 if (!(file_exists($cbImagesCanvasGallery . '/' . $galleryFile) && is_readable($cbImagesCanvasGallery . '/' . $galleryFile))) {
                     $result = @copy($galleryPath . '/' . $galleryFile, $cbImagesCanvasGallery . '/' . $galleryFile);
                     if (!$result) {
                         $result = $adminFS->copy($galleryPath . '/' . $galleryFile, $cbImagesCanvasGallery . '/' . $galleryFile);
                     }
                     if (!$result) {
                         $return .= '<div style="font-size:14px;color:red;margin-bottom:10px;">' . CBTxt::P('[file] failed to be added to the gallery please do so manually.', array('[file]' => $galleryFile)) . '</div>';
                     }
                 }
             }
             if (!$adminFS->file_exists($cbImagesCanvasGallery . '/index.html')) {
                 $result = @copy($imagesPath . '/index.html', $cbImagesCanvasGallery . '/index.html');
                 if (!$result) {
                     $result = $adminFS->copy($imagesPath . '/index.html', $cbImagesCanvasGallery . '/index.html');
                 }
                 if (!$result) {
                     $return .= '<div style="font-size:14px;color:red;margin-bottom:10px;">' . CBTxt::P('index.html failed to be added to [path] please do so manually.', array('[path]' => $cbImagesCanvasGallery)) . '</div>';
                 }
             }
         }
     }
     if (!($adminFS->file_exists($cbImages) && is_writable($cbImages) && $adminFS->file_exists($cbImagesGallery) && $adminFS->file_exists($cbImagesCanvasGallery))) {
         $return .= '<div style="margin-bottom:10px;">' . '<div style="font-size:14px;">' . CBTxt::T('Manually do the following:') . '</div>' . '<div style="font-size:12px;text-indent:15px;">' . CBTxt::P('1. create [path]/ directory', array('[path]' => $cbImages)) . '</div>' . '<div style="font-size:12px;text-indent:15px;">' . CBTxt::T('2. chmod it to 755 or if needed to 775') . '</div>' . '<div style="font-size:12px;text-indent:15px;">' . CBTxt::P('3. create [path]/ directory', array('[path]' => $cbImagesGallery)) . '</div>' . '<div style="font-size:12px;text-indent:15px;">' . CBTxt::T('4. chmod it to 755 or if needed to 775') . '</div>' . '<div style="font-size:12px;text-indent:15px;">' . CBTxt::P('5. copy [from_path]/ and its contents to [to_path]/', array('[from_path]' => $_CB_framework->getCfg('absolute_path') . '/components/com_comprofiler/images/gallery', '[to_path]' => $cbImagesGallery)) . '</div>' . '<div style="font-size:12px;text-indent:15px;">' . CBTxt::P('6. create [path]/ directory', array('[path]' => $cbImagesCanvasGallery)) . '</div>' . '<div style="font-size:12px;text-indent:15px;">' . CBTxt::T('7. chmod it to 755 or if needed to 775') . '</div>' . '<div style="font-size:12px;text-indent:15px;">' . CBTxt::P('8. copy [from_path]/ and its contents to [to_path]/', array('[from_path]' => $_CB_framework->getCfg('absolute_path') . '/components/com_comprofiler/images/gallery/canvas', '[to_path]' => $cbImagesCanvasGallery)) . '</div>' . '</div>';
     }
     $pluginMessages = null;
     if (cbInstaller_install_plugins($pluginMessages)) {
         // All ok, Nothing to alarm user here-
         // $return							.=	'<div style="font-size:18px;color:green;margin-bottom:10px;">' . CBTxt::T( 'Core plugins installed successfully.' ) . '</div>';
     } else {
         $return .= '<div style="font-size:14px;color:red;margin-bottom:10px;">' . CBTxt::P('Core plugins installation failed. Please <a href="[url]" target="_blank">click here</a> to manually install.', array('[url]' => $_CB_framework->backendUrl('index.php?option=com_comprofiler&view=finishinstallation'))) . '</div>';
     }
     $return .= $pluginMessages . '<div style="color:green;font-size:18px;font-weight:bold;margin-top:15px;margin-bottom:15px;">' . CBTxt::P('Installation done.') . '</div>' . '<div style="color:green;font-size:18px;font-weight:bold;margin-top:15px;margin-bottom:15px;">' . CBTxt::P('Now is a great time to checkout the <a href="[help_url]" target="_blank">Getting Started</a> resources.', 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::T('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::T('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::T('Start to Configure Community Builder') . '</a></div>' . '</div>';
     echo $return;
     // For display in packager:
     $_CB_framework->setUserState('com_comprofiler_install', $return);
     return true;
 }