/**
  * {@inheritdoc}
  */
 public function handle(\Input $input)
 {
     $this->handleRunOnce();
     // PATCH
     if ($input->post('FORM_SUBMIT') == 'database-update') {
         $count = 0;
         $sql = deserialize($input->post('sql'));
         if (is_array($sql)) {
             foreach ($sql as $key) {
                 if (isset($_SESSION['sql_commands'][$key])) {
                     $this->Database->query(str_replace('DEFAULT CHARSET=utf8;', 'DEFAULT CHARSET=utf8 COLLATE ' . $GLOBALS['TL_CONFIG']['dbCollation'] . ';', $_SESSION['sql_commands'][$key]));
                     $count++;
                 }
             }
         }
         $_SESSION['sql_commands'] = array();
         Messages::addConfirmation(sprintf($GLOBALS['TL_LANG']['composer_client']['databaseUpdated'], $count));
         $this->reload();
     }
     /** @var \Contao\Database\Installer $installer */
     $installer = \System::importStatic('Database\\Installer');
     $form = $installer->generateSqlForm();
     if (empty($form)) {
         Messages::addInfo($GLOBALS['TL_LANG']['composer_client']['databaseUptodate']);
         $this->redirect('contao/main.php?do=composer');
     }
     $form = preg_replace('#(<label for="sql_\\d+")>(CREATE TABLE)#', '$1 class="create_table">$2', $form);
     $form = preg_replace('#(<label for="sql_\\d+")>(ALTER TABLE `[^`]+` ADD)#', '$1 class="alter_add">$2', $form);
     $form = preg_replace('#(<label for="sql_\\d+")>(ALTER TABLE `[^`]+` DROP)#', '$1 class="alter_drop">$2', $form);
     $form = preg_replace('#(<label for="sql_\\d+")>(DROP TABLE)#', '$1 class="drop_table">$2', $form);
     $template = new \BackendTemplate('be_composer_client_update');
     $template->composer = $this->composer;
     $template->form = $form;
     return $template->parse();
 }
 /**
  * {@inheritdoc}
  */
 public function handle(\Input $input)
 {
     if (Runtime::clearComposerCache()) {
         Messages::addConfirmation($GLOBALS['TL_LANG']['composer_client']['composerCacheCleared']);
     }
     $this->redirect('contao/main.php?do=composer');
 }
 /**
  * {@inheritdoc}
  */
 public function handle(\Input $input)
 {
     $configFile = new \File($this->configPathname);
     if ($input->post('save')) {
         $tempPathname = $this->configPathname . '~';
         $tempFile = new \File($tempPathname);
         $config = $input->postRaw('config');
         $config = html_entity_decode($config, ENT_QUOTES, 'UTF-8');
         $tempFile->write($config);
         $tempFile->close();
         $validator = new ConfigValidator($this->io);
         list($errors, $publishErrors, $warnings) = $validator->validate(TL_ROOT . '/' . $tempPathname);
         if (!$errors && !$publishErrors) {
             Messages::addConfirmation($GLOBALS['TL_LANG']['composer_client']['configValid']);
             $this->import('Files');
             $this->Files->rename($tempPathname, $this->configPathname);
         } else {
             $tempFile->delete();
             $_SESSION['COMPOSER_EDIT_CONFIG'] = $config;
             if ($errors) {
                 foreach ($errors as $message) {
                     Messages::addError('Error: ' . $message);
                 }
             }
             if ($publishErrors) {
                 foreach ($publishErrors as $message) {
                     Messages::addError('Publish error: ' . $message);
                 }
             }
         }
         if ($warnings) {
             foreach ($warnings as $message) {
                 Messages::addWarning('Warning: ' . $message);
             }
         }
         $this->reload();
     }
     if (isset($_SESSION['COMPOSER_EDIT_CONFIG'])) {
         $config = $_SESSION['COMPOSER_EDIT_CONFIG'];
         unset($_SESSION['COMPOSER_EDIT_CONFIG']);
     } else {
         $config = $configFile->getContent();
     }
     $template = new \BackendTemplate('be_composer_client_editor');
     $template->composer = $this->composer;
     $template->config = $config;
     return $template->parse();
 }
 /**
  * {@inheritdoc}
  */
 public function handle(\Input $input)
 {
     try {
         $packages = $input->post('packages') ?: $input->get('packages');
         $packages = explode(',', $packages);
         $packages = array_filter($packages);
         $dryRun = $input->get('dry-run') || $input->post('dry-run');
         $mode = $this->determineRuntimeMode();
         switch ($mode) {
             case 'inline':
                 $this->runInline($packages, $dryRun);
                 break;
             case 'process':
                 $this->runProcess($packages, $dryRun);
                 break;
             case 'detached':
                 $this->runDetached($packages, $dryRun);
                 break;
         }
     } catch (DuplicateContaoException $e) {
         if (isset($_SESSION['COMPOSER_DUPLICATE_CONTAO_EXCEPTION']) && $_SESSION['COMPOSER_DUPLICATE_CONTAO_EXCEPTION']) {
             unset($_SESSION['COMPOSER_DUPLICATE_CONTAO_EXCEPTION']);
             do {
                 Messages::addError(str_replace(TL_ROOT, '', $e->getMessage()));
                 $e = $e->getPrevious();
             } while ($e);
             $this->redirect('contao/main.php?do=composer');
         } else {
             $_SESSION['COMPOSER_DUPLICATE_CONTAO_EXCEPTION'] = true;
             $this->reload();
         }
     } catch (ConfigUpdateException $e) {
         do {
             Messages::addConfirmation(str_replace(TL_ROOT, '', $e->getMessage()));
             $e = $e->getPrevious();
         } while ($e);
         $this->reload();
     } catch (\RuntimeException $e) {
         do {
             Messages::addError(str_replace(TL_ROOT, '', $e->getMessage()));
             $e = $e->getPrevious();
         } while ($e);
         $this->redirect('contao/main.php?do=composer');
     }
 }
 /**
  * Load composer and the composer class loader.
  */
 protected function loadComposer()
 {
     // search for composer build version
     $composerDevWarningTime = Runtime::readComposerDevWarningTime();
     $incompatibleVersion = false === $composerDevWarningTime || mktime(11, 0, 0, 6, 5, 2014) > $composerDevWarningTime - 30 * 86400;
     // Update if allowed or composer version is incompatible.
     if ($incompatibleVersion || $GLOBALS['TL_CONFIG']['composerAutoUpdateLibrary'] && time() > $composerDevWarningTime) {
         Runtime::updateComposer();
         Messages::addConfirmation($GLOBALS['TL_LANG']['composer_client']['composerUpdated']);
     }
     if ($composerDevWarningTime && !$GLOBALS['TL_CONFIG']['composerAutoUpdateLibrary'] && $incompatibleVersion) {
         Messages::addError($GLOBALS['TL_LANG']['composer_client']['composerUpdateNecessary']);
     }
     // register composer class loader
     Runtime::registerComposerClassLoader();
     // define pathname to config file
     $this->configPathname = COMPOSER_DIR_RELATIVE . '/' . Factory::getComposerFile();
     // create io interface
     $this->io = new BufferIO('', $this->getDebugLevel(), new HtmlOutputFormatter());
     // create composer
     $this->composer = Runtime::createComposer($this->io);
 }
 /**
  * {@inheritdoc}
  *
  * @SuppressWarnings(PHPMD.LongVariable)
  */
 public function handle(\Input $input)
 {
     if (\Database::getInstance()->tableExists('tl_repository_installs')) {
         $oldPackageCount = \Database::getInstance()->execute('SELECT COUNT(*) AS count FROM tl_repository_installs')->count;
         $commercialPackages = \Database::getInstance()->execute('SELECT * FROM tl_repository_installs WHERE lickey!=\'\'')->fetchEach('extension');
         $commercialPackages = count($commercialPackages) ? implode(', ', $commercialPackages) : false;
     } else {
         $oldPackageCount = 0;
         $commercialPackages = '';
     }
     $smhEnabled = Runtime::isSafeModeHackEnabled();
     $allowUrlFopenEnabled = ini_get('allow_url_fopen');
     $pharSupportEnabled = false;
     $apcOpcodeCacheEnabled = ini_get('apc.enabled') && ini_get('apc.cache_by_default');
     try {
         if (class_exists('Phar', false)) {
             new \Phar(TL_ROOT . '/system/modules/!composer/config/test.phar');
             $pharSupportEnabled = true;
         }
     } catch (\Exception $e) {
     }
     $composerSupported = !$smhEnabled && $allowUrlFopenEnabled && $pharSupportEnabled;
     $gitAvailable = Runtime::testProcess('git --version');
     $hgAvailable = Runtime::testProcess('hg --version');
     $svnAvailable = Runtime::testProcess('svn --version');
     $mode = 'upgrade';
     $setup = 'production';
     if ($composerSupported && $input->post('FORM_SUBMIT') == 'tl_composer_migrate') {
         $target = 'contao/main.php?do=composer';
         $mode = $input->post('mode');
         $setup = $input->post('setup');
         // load config
         $json = new JsonFile(TL_ROOT . '/' . $this->configPathname);
         $config = $json->read();
         if ($input->post('skip')) {
             // mark migration skipped
             $config['extra']['contao']['migrated'] = 'skipped';
             Messages::addConfirmation($GLOBALS['TL_LANG']['composer_client']['migrationSkipped']);
         } else {
             if (\Database::getInstance()->tableExists('tl_repository_installs')) {
                 switch ($mode) {
                     case 'upgrade':
                         $this->removeER2Files();
                         $install = \Database::getInstance()->query('SELECT * FROM tl_repository_installs WHERE lickey=""');
                         while ($install->next()) {
                             // skip the composer package
                             if ($install->extension == 'composer') {
                                 continue;
                             }
                             $packageName = 'contao-legacy/' . $install->extension;
                             /*
                             $packageName = preg_replace(
                                 '{(?:([a-z])([A-Z])|([A-Z])([A-Z][a-z]))}',
                                 '\\1\\3-\\2\\4',
                                 $packageName
                             );
                             */
                             $packageName = strtolower($packageName);
                             $oldVersion = $install->version;
                             $stability = $oldVersion % 10;
                             $oldVersion = (int) ($oldVersion / 10);
                             $oldVersion = (int) ($oldVersion / 1000);
                             $minor = $oldVersion % 1000;
                             $major = (int) ($oldVersion / 1000);
                             $version = sprintf('~%d.%d%s', $major, $minor, static::$versionNames[$stability]);
                             $config['require'][$packageName] = $version;
                         }
                         $target = 'contao/main.php?do=composer&update=packages';
                         break;
                     case 'clean':
                         $this->removeER2Files();
                         break;
                 }
             }
             switch ($setup) {
                 case 'production':
                     $config['minimum-stability'] = 'dev';
                     $config['prefer-stable'] = true;
                     $config['config']['preferred-install'] = 'dist';
                     break;
                 case 'development':
                     $config['minimum-stability'] = 'dev';
                     $config['prefer-stable'] = true;
                     $config['config']['preferred-install'] = 'source';
                     break;
             }
             // mark migration done
             $config['extra']['contao']['migrated'] = 'done';
             Messages::addConfirmation($GLOBALS['TL_LANG']['composer_client']['migrationDone']);
         }
         // write config
         $json->write($config);
         $this->redirect($target);
     }
     $template = new \BackendTemplate('be_composer_client_migrate');
     $template->composer = $this->composer;
     $template->smhEnabled = $smhEnabled;
     $template->allowUrlFopenEnabled = $allowUrlFopenEnabled;
     $template->pharSupportEnabled = $pharSupportEnabled;
     $template->composerSupported = $composerSupported;
     $template->apcOpcodeCacheEnabled = $apcOpcodeCacheEnabled;
     $template->oldPackageCount = $oldPackageCount;
     $template->commercialPackages = $commercialPackages;
     $template->gitAvailable = $gitAvailable;
     $template->hgAvailable = $hgAvailable;
     $template->svnAvailable = $svnAvailable;
     $template->mode = $mode;
     $template->setup = $setup;
     return $template->parse();
 }