Chris@0: root = $root; Chris@0: $this->moduleHandler = $module_handler; Chris@0: $this->sitePath = $site_path; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getFormId() { Chris@0: return 'update_manager_install_form'; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function create(ContainerInterface $container) { Chris@0: return new static( Chris@0: $container->get('update.root'), Chris@0: $container->get('module_handler'), Chris@0: $container->get('site.path') Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function buildForm(array $form, FormStateInterface $form_state) { Chris@0: $this->moduleHandler->loadInclude('update', 'inc', 'update.manager'); Chris@0: if (!_update_manager_check_backends($form, 'install')) { Chris@0: return $form; Chris@0: } Chris@0: Chris@0: $form['help_text'] = [ Chris@0: '#prefix' => '
', Chris@0: '#markup' => $this->t('You can find modules and themes on drupal.org. The following file extensions are supported: %extensions.', [ Chris@0: ':module_url' => 'https://www.drupal.org/project/modules', Chris@0: ':theme_url' => 'https://www.drupal.org/project/themes', Chris@0: ':drupal_org_url' => 'https://www.drupal.org', Chris@0: '%extensions' => archiver_get_extensions(), Chris@0: ]), Chris@0: '#suffix' => '
', Chris@0: ]; Chris@0: Chris@0: $form['project_url'] = [ Chris@0: '#type' => 'url', Chris@0: '#title' => $this->t('Install from a URL'), Chris@0: '#description' => $this->t('For example: %url', ['%url' => 'https://ftp.drupal.org/files/projects/name.tar.gz']), Chris@0: ]; Chris@0: Chris@0: $form['information'] = [ Chris@0: '#prefix' => '', Chris@0: '#markup' => $this->t('Or'), Chris@0: '#suffix' => '', Chris@0: ]; Chris@0: Chris@0: $form['project_upload'] = [ Chris@0: '#type' => 'file', Chris@0: '#title' => $this->t('Upload a module or theme archive to install'), Chris@0: '#description' => $this->t('For example: %filename from your local computer', ['%filename' => 'name.tar.gz']), Chris@0: ]; Chris@0: Chris@0: $form['actions'] = ['#type' => 'actions']; Chris@0: $form['actions']['submit'] = [ Chris@0: '#type' => 'submit', Chris@0: '#button_type' => 'primary', Chris@0: '#value' => $this->t('Install'), Chris@0: ]; Chris@0: Chris@0: return $form; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function validateForm(array &$form, FormStateInterface $form_state) { Chris@0: $all_files = $this->getRequest()->files->get('files', []); Chris@0: if (!($form_state->getValue('project_url') xor !empty($all_files['project_upload']))) { Chris@0: $form_state->setErrorByName('project_url', $this->t('You must either provide a URL or upload an archive file to install.')); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function submitForm(array &$form, FormStateInterface $form_state) { Chris@0: $local_cache = NULL; Chris@17: $all_files = $this->getRequest()->files->get('files', []); Chris@0: if ($form_state->getValue('project_url')) { Chris@0: $local_cache = update_manager_file_get($form_state->getValue('project_url')); Chris@0: if (!$local_cache) { Chris@17: $this->messenger()->addError($this->t('Unable to retrieve Drupal project from %url.', ['%url' => $form_state->getValue('project_url')])); Chris@0: return; Chris@0: } Chris@0: } Chris@17: elseif (!empty($all_files['project_upload'])) { Chris@0: $validators = ['file_validate_extensions' => [archiver_get_extensions()]]; Chris@0: if (!($finfo = file_save_upload('project_upload', $validators, NULL, 0, FILE_EXISTS_REPLACE))) { Chris@0: // Failed to upload the file. file_save_upload() calls Chris@17: // \Drupal\Core\Messenger\MessengerInterface::addError() on failure. Chris@0: return; Chris@0: } Chris@0: $local_cache = $finfo->getFileUri(); Chris@0: } Chris@0: Chris@0: $directory = _update_manager_extract_directory(); Chris@0: try { Chris@0: $archive = update_manager_archive_extract($local_cache, $directory); Chris@0: } Chris@0: catch (\Exception $e) { Chris@17: $this->messenger()->addError($e->getMessage()); Chris@0: return; Chris@0: } Chris@0: Chris@0: $files = $archive->listContents(); Chris@0: if (!$files) { Chris@17: $this->messenger()->addError($this->t('Provided archive contains no files.')); Chris@0: return; Chris@0: } Chris@0: Chris@0: // Unfortunately, we can only use the directory name to determine the Chris@0: // project name. Some archivers list the first file as the directory (i.e., Chris@0: // MODULE/) and others list an actual file (i.e., MODULE/README.TXT). Chris@0: $project = strtok($files[0], '/\\'); Chris@0: Chris@0: $archive_errors = $this->moduleHandler->invokeAll('verify_update_archive', [$project, $local_cache, $directory]); Chris@0: if (!empty($archive_errors)) { Chris@17: $this->messenger()->addError(array_shift($archive_errors)); Chris@0: // @todo: Fix me in D8: We need a way to set multiple errors on the same Chris@0: // form element and have all of them appear! Chris@0: if (!empty($archive_errors)) { Chris@0: foreach ($archive_errors as $error) { Chris@17: $this->messenger()->addError($error); Chris@0: } Chris@0: } Chris@0: return; Chris@0: } Chris@0: Chris@0: // Make sure the Updater registry is loaded. Chris@0: drupal_get_updaters(); Chris@0: Chris@0: $project_location = $directory . '/' . $project; Chris@0: try { Chris@0: $updater = Updater::factory($project_location, $this->root); Chris@0: } Chris@0: catch (\Exception $e) { Chris@17: $this->messenger()->addError($e->getMessage()); Chris@0: return; Chris@0: } Chris@0: Chris@0: try { Chris@0: $project_title = Updater::getProjectTitle($project_location); Chris@0: } Chris@0: catch (\Exception $e) { Chris@17: $this->messenger()->addError($e->getMessage()); Chris@0: return; Chris@0: } Chris@0: Chris@0: if (!$project_title) { Chris@17: $this->messenger()->addError($this->t('Unable to determine %project name.', ['%project' => $project])); Chris@0: } Chris@0: Chris@0: if ($updater->isInstalled()) { Chris@17: $this->messenger()->addError($this->t('%project is already installed.', ['%project' => $project_title])); Chris@0: return; Chris@0: } Chris@0: Chris@14: $project_real_location = \Drupal::service('file_system')->realpath($project_location); Chris@0: $arguments = [ Chris@0: 'project' => $project, Chris@0: 'updater_name' => get_class($updater), Chris@0: 'local_url' => $project_real_location, Chris@0: ]; Chris@0: Chris@0: // This process is inherently difficult to test therefore use a state flag. Chris@0: $test_authorize = FALSE; Chris@0: if (drupal_valid_test_ua()) { Chris@0: $test_authorize = \Drupal::state()->get('test_uploaders_via_prompt', FALSE); Chris@0: } Chris@0: // If the owner of the directory we extracted is the same as the owner of Chris@0: // our configuration directory (e.g. sites/default) where we're trying to Chris@0: // install the code, there's no need to prompt for FTP/SSH credentials. Chris@0: // Instead, we instantiate a Drupal\Core\FileTransfer\Local and invoke Chris@0: // update_authorize_run_install() directly. Chris@0: if (fileowner($project_real_location) == fileowner($this->sitePath) && !$test_authorize) { Chris@0: $this->moduleHandler->loadInclude('update', 'inc', 'update.authorize'); Chris@0: $filetransfer = new Local($this->root); Chris@0: $response = call_user_func_array('update_authorize_run_install', array_merge([$filetransfer], $arguments)); Chris@0: if ($response instanceof Response) { Chris@0: $form_state->setResponse($response); Chris@0: } Chris@0: } Chris@0: Chris@0: // Otherwise, go through the regular workflow to prompt for FTP/SSH Chris@0: // credentials and invoke update_authorize_run_install() indirectly with Chris@0: // whatever FileTransfer object authorize.php creates for us. Chris@0: else { Chris@0: // The page title must be passed here to ensure it is initially used when Chris@0: // authorize.php loads for the first time with the FTP/SSH credentials Chris@0: // form. Chris@0: system_authorized_init('update_authorize_run_install', __DIR__ . '/../../update.authorize.inc', $arguments, $this->t('Update manager')); Chris@0: $form_state->setRedirectUrl(system_authorized_get_url()); Chris@0: } Chris@0: } Chris@0: Chris@0: }