Mercurial > hg > isophonics-drupal-site
comparison core/modules/update/src/Form/UpdateManagerInstall.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 1fec387a4317 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\update\Form; | |
4 | |
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; | |
12 | |
13 /** | |
14 * Configure update settings for this site. | |
15 */ | |
16 class UpdateManagerInstall extends FormBase { | |
17 | |
18 /** | |
19 * The module handler. | |
20 * | |
21 * @var \Drupal\Core\Extension\ModuleHandlerInterface | |
22 */ | |
23 protected $moduleHandler; | |
24 | |
25 /** | |
26 * The root location under which installed projects will be saved. | |
27 * | |
28 * @var string | |
29 */ | |
30 protected $root; | |
31 | |
32 /** | |
33 * The site path. | |
34 * | |
35 * @var string | |
36 */ | |
37 protected $sitePath; | |
38 | |
39 /** | |
40 * Constructs a new UpdateManagerInstall. | |
41 * | |
42 * @param string $root | |
43 * The root location under which installed projects will be saved. | |
44 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler | |
45 * The module handler. | |
46 * @param string $site_path | |
47 * The site path. | |
48 */ | |
49 public function __construct($root, ModuleHandlerInterface $module_handler, $site_path) { | |
50 $this->root = $root; | |
51 $this->moduleHandler = $module_handler; | |
52 $this->sitePath = $site_path; | |
53 } | |
54 | |
55 /** | |
56 * {@inheritdoc} | |
57 */ | |
58 public function getFormId() { | |
59 return 'update_manager_install_form'; | |
60 } | |
61 | |
62 /** | |
63 * {@inheritdoc} | |
64 */ | |
65 public static function create(ContainerInterface $container) { | |
66 return new static( | |
67 $container->get('update.root'), | |
68 $container->get('module_handler'), | |
69 $container->get('site.path') | |
70 ); | |
71 } | |
72 | |
73 /** | |
74 * {@inheritdoc} | |
75 */ | |
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')) { | |
79 return $form; | |
80 } | |
81 | |
82 $form['help_text'] = [ | |
83 '#prefix' => '<p>', | |
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(), | |
89 ]), | |
90 '#suffix' => '</p>', | |
91 ]; | |
92 | |
93 $form['project_url'] = [ | |
94 '#type' => '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']), | |
97 ]; | |
98 | |
99 $form['information'] = [ | |
100 '#prefix' => '<strong>', | |
101 '#markup' => $this->t('Or'), | |
102 '#suffix' => '</strong>', | |
103 ]; | |
104 | |
105 $form['project_upload'] = [ | |
106 '#type' => 'file', | |
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']), | |
109 ]; | |
110 | |
111 $form['actions'] = ['#type' => 'actions']; | |
112 $form['actions']['submit'] = [ | |
113 '#type' => 'submit', | |
114 '#button_type' => 'primary', | |
115 '#value' => $this->t('Install'), | |
116 ]; | |
117 | |
118 return $form; | |
119 } | |
120 | |
121 /** | |
122 * {@inheritdoc} | |
123 */ | |
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.')); | |
128 } | |
129 } | |
130 | |
131 /** | |
132 * {@inheritdoc} | |
133 */ | |
134 public function submitForm(array &$form, FormStateInterface $form_state) { | |
135 $local_cache = NULL; | |
136 if ($form_state->getValue('project_url')) { | |
137 $local_cache = update_manager_file_get($form_state->getValue('project_url')); | |
138 if (!$local_cache) { | |
139 drupal_set_message($this->t('Unable to retrieve Drupal project from %url.', ['%url' => $form_state->getValue('project_url')]), 'error'); | |
140 return; | |
141 } | |
142 } | |
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. | |
148 return; | |
149 } | |
150 $local_cache = $finfo->getFileUri(); | |
151 } | |
152 | |
153 $directory = _update_manager_extract_directory(); | |
154 try { | |
155 $archive = update_manager_archive_extract($local_cache, $directory); | |
156 } | |
157 catch (\Exception $e) { | |
158 drupal_set_message($e->getMessage(), 'error'); | |
159 return; | |
160 } | |
161 | |
162 $files = $archive->listContents(); | |
163 if (!$files) { | |
164 drupal_set_message($this->t('Provided archive contains no files.'), 'error'); | |
165 return; | |
166 } | |
167 | |
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], '/\\'); | |
172 | |
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'); | |
181 } | |
182 } | |
183 return; | |
184 } | |
185 | |
186 // Make sure the Updater registry is loaded. | |
187 drupal_get_updaters(); | |
188 | |
189 $project_location = $directory . '/' . $project; | |
190 try { | |
191 $updater = Updater::factory($project_location, $this->root); | |
192 } | |
193 catch (\Exception $e) { | |
194 drupal_set_message($e->getMessage(), 'error'); | |
195 return; | |
196 } | |
197 | |
198 try { | |
199 $project_title = Updater::getProjectTitle($project_location); | |
200 } | |
201 catch (\Exception $e) { | |
202 drupal_set_message($e->getMessage(), 'error'); | |
203 return; | |
204 } | |
205 | |
206 if (!$project_title) { | |
207 drupal_set_message($this->t('Unable to determine %project name.', ['%project' => $project]), 'error'); | |
208 } | |
209 | |
210 if ($updater->isInstalled()) { | |
211 drupal_set_message($this->t('%project is already installed.', ['%project' => $project_title]), 'error'); | |
212 return; | |
213 } | |
214 | |
215 $project_real_location = drupal_realpath($project_location); | |
216 $arguments = [ | |
217 'project' => $project, | |
218 'updater_name' => get_class($updater), | |
219 'local_url' => $project_real_location, | |
220 ]; | |
221 | |
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); | |
226 } | |
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); | |
238 } | |
239 } | |
240 | |
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. | |
244 else { | |
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 | |
247 // form. | |
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()); | |
250 } | |
251 } | |
252 | |
253 } |