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