Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\update\Form;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Extension\ModuleHandlerInterface;
|
Chris@0
|
6 use Drupal\Core\FileTransfer\Local;
|
Chris@0
|
7 use Drupal\Core\Form\FormBase;
|
Chris@0
|
8 use Drupal\Core\Form\FormStateInterface;
|
Chris@0
|
9 use Drupal\Core\Updater\Updater;
|
Chris@0
|
10 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
11 use Symfony\Component\HttpFoundation\Response;
|
Chris@0
|
12
|
Chris@0
|
13 /**
|
Chris@0
|
14 * Configure update settings for this site.
|
Chris@14
|
15 *
|
Chris@14
|
16 * @internal
|
Chris@0
|
17 */
|
Chris@0
|
18 class UpdateManagerInstall extends FormBase {
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * The module handler.
|
Chris@0
|
22 *
|
Chris@0
|
23 * @var \Drupal\Core\Extension\ModuleHandlerInterface
|
Chris@0
|
24 */
|
Chris@0
|
25 protected $moduleHandler;
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * The root location under which installed projects will be saved.
|
Chris@0
|
29 *
|
Chris@0
|
30 * @var string
|
Chris@0
|
31 */
|
Chris@0
|
32 protected $root;
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * The site path.
|
Chris@0
|
36 *
|
Chris@0
|
37 * @var string
|
Chris@0
|
38 */
|
Chris@0
|
39 protected $sitePath;
|
Chris@0
|
40
|
Chris@0
|
41 /**
|
Chris@0
|
42 * Constructs a new UpdateManagerInstall.
|
Chris@0
|
43 *
|
Chris@0
|
44 * @param string $root
|
Chris@0
|
45 * The root location under which installed projects will be saved.
|
Chris@0
|
46 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
|
Chris@0
|
47 * The module handler.
|
Chris@0
|
48 * @param string $site_path
|
Chris@0
|
49 * The site path.
|
Chris@0
|
50 */
|
Chris@0
|
51 public function __construct($root, ModuleHandlerInterface $module_handler, $site_path) {
|
Chris@0
|
52 $this->root = $root;
|
Chris@0
|
53 $this->moduleHandler = $module_handler;
|
Chris@0
|
54 $this->sitePath = $site_path;
|
Chris@0
|
55 }
|
Chris@0
|
56
|
Chris@0
|
57 /**
|
Chris@0
|
58 * {@inheritdoc}
|
Chris@0
|
59 */
|
Chris@0
|
60 public function getFormId() {
|
Chris@0
|
61 return 'update_manager_install_form';
|
Chris@0
|
62 }
|
Chris@0
|
63
|
Chris@0
|
64 /**
|
Chris@0
|
65 * {@inheritdoc}
|
Chris@0
|
66 */
|
Chris@0
|
67 public static function create(ContainerInterface $container) {
|
Chris@0
|
68 return new static(
|
Chris@0
|
69 $container->get('update.root'),
|
Chris@0
|
70 $container->get('module_handler'),
|
Chris@0
|
71 $container->get('site.path')
|
Chris@0
|
72 );
|
Chris@0
|
73 }
|
Chris@0
|
74
|
Chris@0
|
75 /**
|
Chris@0
|
76 * {@inheritdoc}
|
Chris@0
|
77 */
|
Chris@0
|
78 public function buildForm(array $form, FormStateInterface $form_state) {
|
Chris@0
|
79 $this->moduleHandler->loadInclude('update', 'inc', 'update.manager');
|
Chris@0
|
80 if (!_update_manager_check_backends($form, 'install')) {
|
Chris@0
|
81 return $form;
|
Chris@0
|
82 }
|
Chris@0
|
83
|
Chris@0
|
84 $form['help_text'] = [
|
Chris@0
|
85 '#prefix' => '<p>',
|
Chris@0
|
86 '#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.', [
|
Chris@0
|
87 ':module_url' => 'https://www.drupal.org/project/modules',
|
Chris@0
|
88 ':theme_url' => 'https://www.drupal.org/project/themes',
|
Chris@0
|
89 ':drupal_org_url' => 'https://www.drupal.org',
|
Chris@0
|
90 '%extensions' => archiver_get_extensions(),
|
Chris@0
|
91 ]),
|
Chris@0
|
92 '#suffix' => '</p>',
|
Chris@0
|
93 ];
|
Chris@0
|
94
|
Chris@0
|
95 $form['project_url'] = [
|
Chris@0
|
96 '#type' => 'url',
|
Chris@0
|
97 '#title' => $this->t('Install from a URL'),
|
Chris@0
|
98 '#description' => $this->t('For example: %url', ['%url' => 'https://ftp.drupal.org/files/projects/name.tar.gz']),
|
Chris@0
|
99 ];
|
Chris@0
|
100
|
Chris@0
|
101 $form['information'] = [
|
Chris@0
|
102 '#prefix' => '<strong>',
|
Chris@0
|
103 '#markup' => $this->t('Or'),
|
Chris@0
|
104 '#suffix' => '</strong>',
|
Chris@0
|
105 ];
|
Chris@0
|
106
|
Chris@0
|
107 $form['project_upload'] = [
|
Chris@0
|
108 '#type' => 'file',
|
Chris@0
|
109 '#title' => $this->t('Upload a module or theme archive to install'),
|
Chris@0
|
110 '#description' => $this->t('For example: %filename from your local computer', ['%filename' => 'name.tar.gz']),
|
Chris@0
|
111 ];
|
Chris@0
|
112
|
Chris@0
|
113 $form['actions'] = ['#type' => 'actions'];
|
Chris@0
|
114 $form['actions']['submit'] = [
|
Chris@0
|
115 '#type' => 'submit',
|
Chris@0
|
116 '#button_type' => 'primary',
|
Chris@0
|
117 '#value' => $this->t('Install'),
|
Chris@0
|
118 ];
|
Chris@0
|
119
|
Chris@0
|
120 return $form;
|
Chris@0
|
121 }
|
Chris@0
|
122
|
Chris@0
|
123 /**
|
Chris@0
|
124 * {@inheritdoc}
|
Chris@0
|
125 */
|
Chris@0
|
126 public function validateForm(array &$form, FormStateInterface $form_state) {
|
Chris@0
|
127 $all_files = $this->getRequest()->files->get('files', []);
|
Chris@0
|
128 if (!($form_state->getValue('project_url') xor !empty($all_files['project_upload']))) {
|
Chris@0
|
129 $form_state->setErrorByName('project_url', $this->t('You must either provide a URL or upload an archive file to install.'));
|
Chris@0
|
130 }
|
Chris@0
|
131 }
|
Chris@0
|
132
|
Chris@0
|
133 /**
|
Chris@0
|
134 * {@inheritdoc}
|
Chris@0
|
135 */
|
Chris@0
|
136 public function submitForm(array &$form, FormStateInterface $form_state) {
|
Chris@0
|
137 $local_cache = NULL;
|
Chris@17
|
138 $all_files = $this->getRequest()->files->get('files', []);
|
Chris@0
|
139 if ($form_state->getValue('project_url')) {
|
Chris@0
|
140 $local_cache = update_manager_file_get($form_state->getValue('project_url'));
|
Chris@0
|
141 if (!$local_cache) {
|
Chris@17
|
142 $this->messenger()->addError($this->t('Unable to retrieve Drupal project from %url.', ['%url' => $form_state->getValue('project_url')]));
|
Chris@0
|
143 return;
|
Chris@0
|
144 }
|
Chris@0
|
145 }
|
Chris@17
|
146 elseif (!empty($all_files['project_upload'])) {
|
Chris@0
|
147 $validators = ['file_validate_extensions' => [archiver_get_extensions()]];
|
Chris@0
|
148 if (!($finfo = file_save_upload('project_upload', $validators, NULL, 0, FILE_EXISTS_REPLACE))) {
|
Chris@0
|
149 // Failed to upload the file. file_save_upload() calls
|
Chris@17
|
150 // \Drupal\Core\Messenger\MessengerInterface::addError() on failure.
|
Chris@0
|
151 return;
|
Chris@0
|
152 }
|
Chris@0
|
153 $local_cache = $finfo->getFileUri();
|
Chris@0
|
154 }
|
Chris@0
|
155
|
Chris@0
|
156 $directory = _update_manager_extract_directory();
|
Chris@0
|
157 try {
|
Chris@0
|
158 $archive = update_manager_archive_extract($local_cache, $directory);
|
Chris@0
|
159 }
|
Chris@0
|
160 catch (\Exception $e) {
|
Chris@17
|
161 $this->messenger()->addError($e->getMessage());
|
Chris@0
|
162 return;
|
Chris@0
|
163 }
|
Chris@0
|
164
|
Chris@0
|
165 $files = $archive->listContents();
|
Chris@0
|
166 if (!$files) {
|
Chris@17
|
167 $this->messenger()->addError($this->t('Provided archive contains no files.'));
|
Chris@0
|
168 return;
|
Chris@0
|
169 }
|
Chris@0
|
170
|
Chris@0
|
171 // Unfortunately, we can only use the directory name to determine the
|
Chris@0
|
172 // project name. Some archivers list the first file as the directory (i.e.,
|
Chris@0
|
173 // MODULE/) and others list an actual file (i.e., MODULE/README.TXT).
|
Chris@0
|
174 $project = strtok($files[0], '/\\');
|
Chris@0
|
175
|
Chris@0
|
176 $archive_errors = $this->moduleHandler->invokeAll('verify_update_archive', [$project, $local_cache, $directory]);
|
Chris@0
|
177 if (!empty($archive_errors)) {
|
Chris@17
|
178 $this->messenger()->addError(array_shift($archive_errors));
|
Chris@0
|
179 // @todo: Fix me in D8: We need a way to set multiple errors on the same
|
Chris@0
|
180 // form element and have all of them appear!
|
Chris@0
|
181 if (!empty($archive_errors)) {
|
Chris@0
|
182 foreach ($archive_errors as $error) {
|
Chris@17
|
183 $this->messenger()->addError($error);
|
Chris@0
|
184 }
|
Chris@0
|
185 }
|
Chris@0
|
186 return;
|
Chris@0
|
187 }
|
Chris@0
|
188
|
Chris@0
|
189 // Make sure the Updater registry is loaded.
|
Chris@0
|
190 drupal_get_updaters();
|
Chris@0
|
191
|
Chris@0
|
192 $project_location = $directory . '/' . $project;
|
Chris@0
|
193 try {
|
Chris@0
|
194 $updater = Updater::factory($project_location, $this->root);
|
Chris@0
|
195 }
|
Chris@0
|
196 catch (\Exception $e) {
|
Chris@17
|
197 $this->messenger()->addError($e->getMessage());
|
Chris@0
|
198 return;
|
Chris@0
|
199 }
|
Chris@0
|
200
|
Chris@0
|
201 try {
|
Chris@0
|
202 $project_title = Updater::getProjectTitle($project_location);
|
Chris@0
|
203 }
|
Chris@0
|
204 catch (\Exception $e) {
|
Chris@17
|
205 $this->messenger()->addError($e->getMessage());
|
Chris@0
|
206 return;
|
Chris@0
|
207 }
|
Chris@0
|
208
|
Chris@0
|
209 if (!$project_title) {
|
Chris@17
|
210 $this->messenger()->addError($this->t('Unable to determine %project name.', ['%project' => $project]));
|
Chris@0
|
211 }
|
Chris@0
|
212
|
Chris@0
|
213 if ($updater->isInstalled()) {
|
Chris@17
|
214 $this->messenger()->addError($this->t('%project is already installed.', ['%project' => $project_title]));
|
Chris@0
|
215 return;
|
Chris@0
|
216 }
|
Chris@0
|
217
|
Chris@14
|
218 $project_real_location = \Drupal::service('file_system')->realpath($project_location);
|
Chris@0
|
219 $arguments = [
|
Chris@0
|
220 'project' => $project,
|
Chris@0
|
221 'updater_name' => get_class($updater),
|
Chris@0
|
222 'local_url' => $project_real_location,
|
Chris@0
|
223 ];
|
Chris@0
|
224
|
Chris@0
|
225 // This process is inherently difficult to test therefore use a state flag.
|
Chris@0
|
226 $test_authorize = FALSE;
|
Chris@0
|
227 if (drupal_valid_test_ua()) {
|
Chris@0
|
228 $test_authorize = \Drupal::state()->get('test_uploaders_via_prompt', FALSE);
|
Chris@0
|
229 }
|
Chris@0
|
230 // If the owner of the directory we extracted is the same as the owner of
|
Chris@0
|
231 // our configuration directory (e.g. sites/default) where we're trying to
|
Chris@0
|
232 // install the code, there's no need to prompt for FTP/SSH credentials.
|
Chris@0
|
233 // Instead, we instantiate a Drupal\Core\FileTransfer\Local and invoke
|
Chris@0
|
234 // update_authorize_run_install() directly.
|
Chris@0
|
235 if (fileowner($project_real_location) == fileowner($this->sitePath) && !$test_authorize) {
|
Chris@0
|
236 $this->moduleHandler->loadInclude('update', 'inc', 'update.authorize');
|
Chris@0
|
237 $filetransfer = new Local($this->root);
|
Chris@0
|
238 $response = call_user_func_array('update_authorize_run_install', array_merge([$filetransfer], $arguments));
|
Chris@0
|
239 if ($response instanceof Response) {
|
Chris@0
|
240 $form_state->setResponse($response);
|
Chris@0
|
241 }
|
Chris@0
|
242 }
|
Chris@0
|
243
|
Chris@0
|
244 // Otherwise, go through the regular workflow to prompt for FTP/SSH
|
Chris@0
|
245 // credentials and invoke update_authorize_run_install() indirectly with
|
Chris@0
|
246 // whatever FileTransfer object authorize.php creates for us.
|
Chris@0
|
247 else {
|
Chris@0
|
248 // The page title must be passed here to ensure it is initially used when
|
Chris@0
|
249 // authorize.php loads for the first time with the FTP/SSH credentials
|
Chris@0
|
250 // form.
|
Chris@0
|
251 system_authorized_init('update_authorize_run_install', __DIR__ . '/../../update.authorize.inc', $arguments, $this->t('Update manager'));
|
Chris@0
|
252 $form_state->setRedirectUrl(system_authorized_get_url());
|
Chris@0
|
253 }
|
Chris@0
|
254 }
|
Chris@0
|
255
|
Chris@0
|
256 }
|