3 namespace Drupal\Core\FileTransfer\Form;
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Render\Element;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
9 use Symfony\Component\HttpFoundation\Response;
12 * Provides the file transfer authorization form.
16 class FileTransferAuthorizeForm extends FormBase {
26 * Constructs a new FileTransferAuthorizeForm object.
31 public function __construct($root) {
38 public static function create(ContainerInterface $container) {
39 return new static ($container->get('app.root'));
45 public function getFormId() {
46 return 'authorize_filetransfer_form';
52 public function buildForm(array $form, FormStateInterface $form_state) {
53 // Get all the available ways to transfer files.
54 if (empty($_SESSION['authorize_filetransfer_info'])) {
55 drupal_set_message($this->t('Unable to continue, no available methods of file transfer'), 'error');
58 $available_backends = $_SESSION['authorize_filetransfer_info'];
60 if (!$this->getRequest()->isSecure()) {
61 $form['information']['https_warning'] = [
62 '#prefix' => '<div class="messages messages--error">',
63 '#markup' => $this->t('WARNING: You are not using an encrypted connection, so your password will be sent in plain text. <a href=":https-link">Learn more</a>.', [':https-link' => 'https://www.drupal.org/https-information']),
64 '#suffix' => '</div>',
68 // Decide on a default backend.
69 $authorize_filetransfer_default = $form_state->getValue(['connection_settings', 'authorize_filetransfer_default']);
70 if (!$authorize_filetransfer_default) {
71 $authorize_filetransfer_default = key($available_backends);
74 $form['information']['main_header'] = [
76 '#markup' => $this->t('To continue, provide your server connection details'),
80 $form['connection_settings']['#tree'] = TRUE;
81 $form['connection_settings']['authorize_filetransfer_default'] = [
83 '#title' => $this->t('Connection method'),
84 '#default_value' => $authorize_filetransfer_default,
89 * Here we create two submit buttons. For a JS enabled client, they will
90 * only ever see submit_process. However, if a client doesn't have JS
91 * enabled, they will see submit_connection on the first form (when picking
92 * what filetransfer type to use, and submit_process on the second one (which
93 * leads to the actual operation).
95 $form['submit_connection'] = [
96 '#prefix' => "<br style='clear:both'/>",
97 '#name' => 'enter_connection_settings',
99 '#value' => $this->t('Enter connection settings'),
103 $form['submit_process'] = [
104 '#name' => 'process_updates',
106 '#value' => $this->t('Continue'),
110 // Build a container for each connection type.
111 foreach ($available_backends as $name => $backend) {
112 $form['connection_settings']['authorize_filetransfer_default']['#options'][$name] = $backend['title'];
113 $form['connection_settings'][$name] = [
114 '#type' => 'container',
115 '#attributes' => ['class' => ["filetransfer-$name", 'filetransfer']],
118 'select[name="connection_settings[authorize_filetransfer_default]"]' => ['value' => $name],
122 // We can't use #prefix on the container itself since then the header won't
123 // be hidden and shown when the containers are being manipulated via JS.
124 $form['connection_settings'][$name]['header'] = [
125 '#markup' => '<h4>' . $this->t('@backend connection settings', ['@backend' => $backend['title']]) . '</h4>',
128 $form['connection_settings'][$name] += $this->addConnectionSettings($name);
130 // Start non-JS code.
131 if ($form_state->getValue(['connection_settings', 'authorize_filetransfer_default']) == $name) {
133 // Change the submit button to the submit_process one.
134 $form['submit_process']['#attributes'] = [];
135 unset($form['submit_connection']);
137 // Activate the proper filetransfer settings form.
138 $form['connection_settings'][$name]['#attributes']['style'] = 'display:block';
139 // Disable the select box.
140 $form['connection_settings']['authorize_filetransfer_default']['#disabled'] = TRUE;
142 // Create a button for changing the type of connection.
143 $form['connection_settings']['change_connection_type'] = [
144 '#name' => 'change_connection_type',
146 '#value' => $this->t('Change connection type'),
148 '#attributes' => ['class' => ['filetransfer-change-connection-type']],
159 public function validateForm(array &$form, FormStateInterface $form_state) {
160 // Only validate the form if we have collected all of the user input and are
161 // ready to proceed with updating or installing.
162 if ($form_state->getTriggeringElement()['#name'] != 'process_updates') {
166 if ($form_connection_settings = $form_state->getValue('connection_settings')) {
167 $backend = $form_connection_settings['authorize_filetransfer_default'];
168 $filetransfer = $this->getFiletransfer($backend, $form_connection_settings[$backend]);
170 if (!$filetransfer) {
171 throw new \Exception($this->t('The connection protocol %backend does not exist.', ['%backend' => $backend]));
173 $filetransfer->connect();
175 catch (\Exception $e) {
176 // The format of this error message is similar to that used on the
177 // database connection form in the installer.
178 $form_state->setErrorByName('connection_settings', $this->t('Failed to connect to the server. The server reports the following message: <p class="error">@message</p> For more help installing or updating code on your server, see the <a href=":handbook_url">handbook</a>.', [
179 '@message' => $e->getMessage(),
180 ':handbook_url' => 'https://www.drupal.org/documentation/install/modules-themes',
189 public function submitForm(array &$form, FormStateInterface $form_state) {
190 $form_connection_settings = $form_state->getValue('connection_settings');
191 switch ($form_state->getTriggeringElement()['#name']) {
192 case 'process_updates':
194 // Save the connection settings to the DB.
195 $filetransfer_backend = $form_connection_settings['authorize_filetransfer_default'];
197 // If the database is available then try to save our settings. We have
198 // to make sure it is available since this code could potentially (will
199 // likely) be called during the installation process, before the
200 // database is set up.
202 $filetransfer = $this->getFiletransfer($filetransfer_backend, $form_connection_settings[$filetransfer_backend]);
204 // Now run the operation.
205 $response = $this->runOperation($filetransfer);
206 if ($response instanceof Response) {
207 $form_state->setResponse($response);
210 catch (\Exception $e) {
211 // If there is no database available, we don't care and just skip
212 // this part entirely.
217 case 'enter_connection_settings':
218 $form_state->setRebuild();
221 case 'change_connection_type':
222 $form_state->setRebuild();
223 $form_state->unsetValue(['connection_settings', 'authorize_filetransfer_default']);
229 * Gets a FileTransfer class for a specific transfer method and settings.
232 * The FileTransfer backend to get the class for.
234 * Array of settings for the FileTransfer.
236 * @return \Drupal\Core\FileTransfer\FileTransfer|bool
237 * An instantiated FileTransfer object for the requested method and settings,
238 * or FALSE if there was an error finding or instantiating it.
240 protected function getFiletransfer($backend, $settings = []) {
241 $filetransfer = FALSE;
242 if (!empty($_SESSION['authorize_filetransfer_info'][$backend])) {
243 $backend_info = $_SESSION['authorize_filetransfer_info'][$backend];
244 if (class_exists($backend_info['class'])) {
245 $filetransfer = $backend_info['class']::factory($this->root, $settings);
248 return $filetransfer;
252 * Generates the Form API array for a given connection backend's settings.
254 * @param string $backend
255 * The name of the backend (e.g. 'ftp', 'ssh', etc).
258 * Form API array of connection settings for the given backend.
260 * @see hook_filetransfer_backends()
262 protected function addConnectionSettings($backend) {
266 // Create an instance of the file transfer class to get its settings form.
267 $filetransfer = $this->getFiletransfer($backend);
269 $form = $filetransfer->getSettingsForm();
271 // Fill in the defaults based on the saved settings, if any.
272 $this->setConnectionSettingsDefaults($form, NULL, $defaults);
277 * Sets the default settings on a file transfer connection form recursively.
279 * The default settings for the file transfer connection forms are saved in
280 * the database. The settings are stored as a nested array in the case of a
281 * settings form that has details or otherwise uses a nested structure.
282 * Therefore, to properly add defaults, we need to walk through all the
283 * children form elements and process those defaults recursively.
286 * Reference to the Form API form element we're operating on.
288 * The key for our current form element, if any.
289 * @param array $defaults
290 * The default settings for the file transfer backend we're operating on.
292 protected function setConnectionSettingsDefaults(&$element, $key, array $defaults) {
293 // If we're operating on a form element which isn't a details, and we have
294 // a default setting saved, stash it in #default_value.
295 if (!empty($key) && isset($defaults[$key]) && isset($element['#type']) && $element['#type'] != 'details') {
296 $element['#default_value'] = $defaults[$key];
298 // Now, we walk through all the child elements, and recursively invoke
299 // ourselves on each one. Since the $defaults settings array can be nested
300 // (because of #tree, any values inside details will be nested), if
301 // there's a subarray of settings for the form key we're currently
302 // processing, pass in that subarray to the recursive call. Otherwise, just
303 // pass on the whole $defaults array.
304 foreach (Element::children($element) as $child_key) {
305 $this->setConnectionSettingsDefaults($element[$child_key], $child_key, ((isset($defaults[$key]) && is_array($defaults[$key])) ? $defaults[$key] : $defaults));
310 * Runs the operation specified in $_SESSION['authorize_operation'].
312 * @param $filetransfer
313 * The FileTransfer object to use for running the operation.
315 * @return \Symfony\Component\HttpFoundation\Response|null
316 * The result of running the operation. If this is an instance of
317 * \Symfony\Component\HttpFoundation\Response the calling code should use
318 * that response for the current page request.
320 protected function runOperation($filetransfer) {
321 $operation = $_SESSION['authorize_operation'];
322 unset($_SESSION['authorize_operation']);
324 require_once $operation['file'];
325 return call_user_func_array($operation['callback'], array_merge([$filetransfer], $operation['arguments']));