/**
  * Creates users based on given array.
  *
  * @param $packageKey
  * @param $configuration
  * @param $configurationController
  */
 public function createCLIUsers($packageKey, $configuration, $configurationController)
 {
     // Only create CLI users on configuration save of template bootstrap package
     if (TemplateBootstrapUtility::getPackageKey() !== $packageKey) {
         return;
     }
     // Get cli user names that supposedly need to be created
     $userNames = GeneralUtility::trimExplode(',', $configuration['createCLIUsers']['value']);
     foreach ($userNames as $userName) {
         $cliUserName = '******' . $userName;
         $cliUserNameQuoted = $GLOBALS['TYPO3_DB']->fullQuoteStr($cliUserName, 'be_users');
         // Check, if user exists already
         $userExistsResult = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'be_users', 'username='******'TYPO3_DB']->sql_error()) {
             PostInstallInfoLogger::log('Failed to check whether BE user "' . $cliUserName . '" exists. Therefore cancelled. Error: ' . $GLOBALS['TYPO3_DB']->sql_error(), PostInstallInfoLogger::MESSAGE_TYPE_SYSTEM_ERROR);
             continue;
         }
         // User exists - (re-) activate, in case it has been deleted previously
         if ($GLOBALS['TYPO3_DB']->sql_num_rows($userExistsResult) > 0) {
             $existingUserRow = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($userExistsResult);
             if ($existingUserRow['deleted']) {
                 $GLOBALS['TYPO3_DB']->exec_UPDATEquery('be_users', 'uid=' . $existingUserRow['uid'], array('deleted' => 0));
                 $updatedError = $GLOBALS['TYPO3_DB']->sql_error();
                 if ($updatedError) {
                     PostInstallInfoLogger::log('Failed to reactivate (un-delete) BE user "' . $cliUserName . '". Error: ' . $GLOBALS['TYPO3_DB']->sql_error(), PostInstallInfoLogger::MESSAGE_TYPE_SYSTEM_ERROR);
                 } else {
                     PostInstallInfoLogger::log('Reactivated (un-deleted) BE user "' . $cliUserName . '"' . $GLOBALS['TYPO3_DB']->sql_error(), PostInstallInfoLogger::MESSAGE_TYPE_OK);
                 }
             }
             // Skip to next user(name) as this one was handled by simply reactivating it.
             continue;
         }
         // Create user
         $saltedPassword = md5($this->getRandomPassword());
         if (PackageManager::isPackageActive('saltedpasswords')) {
             $saltingInstance = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance();
             $saltedPassword = $saltingInstance->getHashedPassword($saltedPassword);
         }
         $createdSqlResult = $GLOBALS['TYPO3_DB']->exec_INSERTquery('be_users', array('crdate' => time(), 'tstamp' => time(), 'cruser_id' => $GLOBALS['BE_USER']->user['uid'], 'username' => $cliUserName, 'password' => $saltedPassword));
         // Failed to create user
         if ($GLOBALS['TYPO3_DB']->sql_error()) {
             PostInstallInfoLogger::log('Failed to create BE user "' . $cliUserName . '". Error: ' . $GLOBALS['TYPO3_DB']->sql_error(), PostInstallInfoLogger::MESSAGE_TYPE_SYSTEM_ERROR);
             // User successfully created
         } else {
             PostInstallInfoLogger::log('Created BE user "' . $cliUserName . '"', PostInstallInfoLogger::MESSAGE_TYPE_OK);
         }
     }
     // foreach user that needs to be created
 }
 /**
  * Renders and writes configuration that needs to go into AdditionalConfiguration.php
  *
  * @param $packageKey
  * @param $configuration
  */
 public function writeAdditionalConfiguration($packageKey, $configuration)
 {
     // Only write additional configuration on configuration save of template bootstrap package
     if (TemplateBootstrapUtility::getPackageKey() !== $packageKey) {
         return;
     }
     global $BE_USER;
     $fileContentLines = array();
     // rewrite configuration, if necessary.
     $enableCustomErrorHandler = intval($configuration['enableCustomErrorHandling']['value']);
     if ($enableCustomErrorHandler) {
         $errorHandlerReference = 'USER_FUNCTION:typo3conf/ext/' . $packageKey . '/Classes/Utility/PageNotFoundHandler.php:Staempfli\\TemplateBootstrap\\Utility\\PageNotFoundHandler->pageNotFound';
         if ($errorHandlerReference !== $GLOBALS['TYPO3_CONF_VARS']['FE']['pageNotFound_handling']) {
             $fileContentLines[] = '$GLOBALS[\'TYPO3_CONF_VARS\'][\'FE\'][\'pageNotFound_handling\'] = \'' . $errorHandlerReference . '\';';
         }
     }
     // trustedHostsPattern
     if (intval($configuration['generateTrustedHostsPattern']['value'])) {
         $finalPattern = 'SERVER_NAME';
         $domainsResult = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'sys_domain', NULL);
         if ($GLOBALS['TYPO3_DB']->sql_num_rows($domainsResult)) {
             $domainNames = array();
             while ($domainRecord = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($domainsResult)) {
                 $domainNames[] = str_replace('.', '\\.', $domainRecord['domainName']);
             }
             $finalPattern = '^(' . implode('|', $domainNames) . ')$';
         }
         if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['trustedHostsPattern'] !== $finalPattern) {
             $fileContentLines[] = '$GLOBALS[\'TYPO3_CONF_VARS\'][\'SYS\'][\'trustedHostsPattern\'] = \'' . $finalPattern . '\';';
         }
     }
     // Nothing to write?
     // abort.
     if (!count($fileContentLines)) {
         return;
     }
     // Enclose content with php tags & comment
     array_unshift($fileContentLines, '# Automatically (re-)written by extension "' . $packageKey . '". (' . date('d.m.Y H:i:s') . ')');
     array_unshift($fileContentLines, '<?php');
     $fileContentLines[] = ' ?>';
     // Write file
     $file = @fopen(PATH_typo3conf . '/AdditionalConfiguration.php', 'a');
     $written = false;
     if ($file) {
         $written = @fwrite($file, implode(chr(10), $fileContentLines));
         @fclose($file);
     }
     // Write log
     if ($written) {
         PostInstallInfoLogger::log('AdditionalConfiguration.php updated.', PostInstallInfoLogger::MESSAGE_TYPE_OK, 20);
     } else {
         PostInstallInfoLogger::log('Attempted to extend AdditionalConfiguration.php, but failed!', PostInstallInfoLogger::MESSAGE_TYPE_SYSTEM_ERROR, 20);
     }
 }