3 namespace Drupal\update\Form;
5 use Drupal\Core\Extension\ModuleHandlerInterface;
6 use Drupal\Core\FileTransfer\Local;
7 use Drupal\Core\Form\FormBase;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Updater\Updater;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11 use Symfony\Component\HttpFoundation\Response;
14 * Configure update settings for this site.
16 class UpdateManagerInstall extends FormBase {
21 * @var \Drupal\Core\Extension\ModuleHandlerInterface
23 protected $moduleHandler;
26 * The root location under which installed projects will be saved.
40 * Constructs a new UpdateManagerInstall.
43 * The root location under which installed projects will be saved.
44 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
46 * @param string $site_path
49 public function __construct($root, ModuleHandlerInterface $module_handler, $site_path) {
51 $this->moduleHandler = $module_handler;
52 $this->sitePath = $site_path;
58 public function getFormId() {
59 return 'update_manager_install_form';
65 public static function create(ContainerInterface $container) {
67 $container->get('update.root'),
68 $container->get('module_handler'),
69 $container->get('site.path')
76 public function buildForm(array $form, FormStateInterface $form_state) {
77 $this->moduleHandler->loadInclude('update', 'inc', 'update.manager');
78 if (!_update_manager_check_backends($form, 'install')) {
82 $form['help_text'] = [
84 '#markup' => $this->t('You can find <a href=":module_url">modules</a> and <a href=":theme_url">themes</a> on <a href=":drupal_org_url">drupal.org</a>. The following file extensions are supported: %extensions.', [
85 ':module_url' => 'https://www.drupal.org/project/modules',
86 ':theme_url' => 'https://www.drupal.org/project/themes',
87 ':drupal_org_url' => 'https://www.drupal.org',
88 '%extensions' => archiver_get_extensions(),
93 $form['project_url'] = [
95 '#title' => $this->t('Install from a URL'),
96 '#description' => $this->t('For example: %url', ['%url' => 'https://ftp.drupal.org/files/projects/name.tar.gz']),
99 $form['information'] = [
100 '#prefix' => '<strong>',
101 '#markup' => $this->t('Or'),
102 '#suffix' => '</strong>',
105 $form['project_upload'] = [
107 '#title' => $this->t('Upload a module or theme archive to install'),
108 '#description' => $this->t('For example: %filename from your local computer', ['%filename' => 'name.tar.gz']),
111 $form['actions'] = ['#type' => 'actions'];
112 $form['actions']['submit'] = [
114 '#button_type' => 'primary',
115 '#value' => $this->t('Install'),
124 public function validateForm(array &$form, FormStateInterface $form_state) {
125 $all_files = $this->getRequest()->files->get('files', []);
126 if (!($form_state->getValue('project_url') xor !empty($all_files['project_upload']))) {
127 $form_state->setErrorByName('project_url', $this->t('You must either provide a URL or upload an archive file to install.'));
134 public function submitForm(array &$form, FormStateInterface $form_state) {
136 if ($form_state->getValue('project_url')) {
137 $local_cache = update_manager_file_get($form_state->getValue('project_url'));
139 drupal_set_message($this->t('Unable to retrieve Drupal project from %url.', ['%url' => $form_state->getValue('project_url')]), 'error');
143 elseif ($_FILES['files']['name']['project_upload']) {
144 $validators = ['file_validate_extensions' => [archiver_get_extensions()]];
145 if (!($finfo = file_save_upload('project_upload', $validators, NULL, 0, FILE_EXISTS_REPLACE))) {
146 // Failed to upload the file. file_save_upload() calls
147 // drupal_set_message() on failure.
150 $local_cache = $finfo->getFileUri();
153 $directory = _update_manager_extract_directory();
155 $archive = update_manager_archive_extract($local_cache, $directory);
157 catch (\Exception $e) {
158 drupal_set_message($e->getMessage(), 'error');
162 $files = $archive->listContents();
164 drupal_set_message($this->t('Provided archive contains no files.'), 'error');
168 // Unfortunately, we can only use the directory name to determine the
169 // project name. Some archivers list the first file as the directory (i.e.,
170 // MODULE/) and others list an actual file (i.e., MODULE/README.TXT).
171 $project = strtok($files[0], '/\\');
173 $archive_errors = $this->moduleHandler->invokeAll('verify_update_archive', [$project, $local_cache, $directory]);
174 if (!empty($archive_errors)) {
175 drupal_set_message(array_shift($archive_errors), 'error');
176 // @todo: Fix me in D8: We need a way to set multiple errors on the same
177 // form element and have all of them appear!
178 if (!empty($archive_errors)) {
179 foreach ($archive_errors as $error) {
180 drupal_set_message($error, 'error');
186 // Make sure the Updater registry is loaded.
187 drupal_get_updaters();
189 $project_location = $directory . '/' . $project;
191 $updater = Updater::factory($project_location, $this->root);
193 catch (\Exception $e) {
194 drupal_set_message($e->getMessage(), 'error');
199 $project_title = Updater::getProjectTitle($project_location);
201 catch (\Exception $e) {
202 drupal_set_message($e->getMessage(), 'error');
206 if (!$project_title) {
207 drupal_set_message($this->t('Unable to determine %project name.', ['%project' => $project]), 'error');
210 if ($updater->isInstalled()) {
211 drupal_set_message($this->t('%project is already installed.', ['%project' => $project_title]), 'error');
215 $project_real_location = drupal_realpath($project_location);
217 'project' => $project,
218 'updater_name' => get_class($updater),
219 'local_url' => $project_real_location,
222 // This process is inherently difficult to test therefore use a state flag.
223 $test_authorize = FALSE;
224 if (drupal_valid_test_ua()) {
225 $test_authorize = \Drupal::state()->get('test_uploaders_via_prompt', FALSE);
227 // If the owner of the directory we extracted is the same as the owner of
228 // our configuration directory (e.g. sites/default) where we're trying to
229 // install the code, there's no need to prompt for FTP/SSH credentials.
230 // Instead, we instantiate a Drupal\Core\FileTransfer\Local and invoke
231 // update_authorize_run_install() directly.
232 if (fileowner($project_real_location) == fileowner($this->sitePath) && !$test_authorize) {
233 $this->moduleHandler->loadInclude('update', 'inc', 'update.authorize');
234 $filetransfer = new Local($this->root);
235 $response = call_user_func_array('update_authorize_run_install', array_merge([$filetransfer], $arguments));
236 if ($response instanceof Response) {
237 $form_state->setResponse($response);
241 // Otherwise, go through the regular workflow to prompt for FTP/SSH
242 // credentials and invoke update_authorize_run_install() indirectly with
243 // whatever FileTransfer object authorize.php creates for us.
245 // The page title must be passed here to ensure it is initially used when
246 // authorize.php loads for the first time with the FTP/SSH credentials
248 system_authorized_init('update_authorize_run_install', __DIR__ . '/../../update.authorize.inc', $arguments, $this->t('Update manager'));
249 $form_state->setRedirectUrl(system_authorized_get_url());