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