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.
14 class FileTransferAuthorizeForm extends FormBase {
24 * Constructs a new FileTransferAuthorizeForm object.
29 public function __construct($root) {
36 public static function create(ContainerInterface $container) {
37 return new static ($container->get('app.root'));
43 public function getFormId() {
44 return 'authorize_filetransfer_form';
50 public function buildForm(array $form, FormStateInterface $form_state) {
51 // Get all the available ways to transfer files.
52 if (empty($_SESSION['authorize_filetransfer_info'])) {
53 drupal_set_message($this->t('Unable to continue, no available methods of file transfer'), 'error');
56 $available_backends = $_SESSION['authorize_filetransfer_info'];
58 if (!$this->getRequest()->isSecure()) {
59 $form['information']['https_warning'] = [
60 '#prefix' => '<div class="messages messages--error">',
61 '#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']),
62 '#suffix' => '</div>',
66 // Decide on a default backend.
67 $authorize_filetransfer_default = $form_state->getValue(['connection_settings', 'authorize_filetransfer_default']);
68 if (!$authorize_filetransfer_default) {
69 $authorize_filetransfer_default = key($available_backends);
72 $form['information']['main_header'] = [
74 '#markup' => $this->t('To continue, provide your server connection details'),
78 $form['connection_settings']['#tree'] = TRUE;
79 $form['connection_settings']['authorize_filetransfer_default'] = [
81 '#title' => $this->t('Connection method'),
82 '#default_value' => $authorize_filetransfer_default,
87 * Here we create two submit buttons. For a JS enabled client, they will
88 * only ever see submit_process. However, if a client doesn't have JS
89 * enabled, they will see submit_connection on the first form (when picking
90 * what filetransfer type to use, and submit_process on the second one (which
91 * leads to the actual operation).
93 $form['submit_connection'] = [
94 '#prefix' => "<br style='clear:both'/>",
95 '#name' => 'enter_connection_settings',
97 '#value' => $this->t('Enter connection settings'),
101 $form['submit_process'] = [
102 '#name' => 'process_updates',
104 '#value' => $this->t('Continue'),
108 // Build a container for each connection type.
109 foreach ($available_backends as $name => $backend) {
110 $form['connection_settings']['authorize_filetransfer_default']['#options'][$name] = $backend['title'];
111 $form['connection_settings'][$name] = [
112 '#type' => 'container',
113 '#attributes' => ['class' => ["filetransfer-$name", 'filetransfer']],
116 'select[name="connection_settings[authorize_filetransfer_default]"]' => ['value' => $name],
120 // We can't use #prefix on the container itself since then the header won't
121 // be hidden and shown when the containers are being manipulated via JS.
122 $form['connection_settings'][$name]['header'] = [
123 '#markup' => '<h4>' . $this->t('@backend connection settings', ['@backend' => $backend['title']]) . '</h4>',
126 $form['connection_settings'][$name] += $this->addConnectionSettings($name);
128 // Start non-JS code.
129 if ($form_state->getValue(['connection_settings', 'authorize_filetransfer_default']) == $name) {
131 // Change the submit button to the submit_process one.
132 $form['submit_process']['#attributes'] = [];
133 unset($form['submit_connection']);
135 // Activate the proper filetransfer settings form.
136 $form['connection_settings'][$name]['#attributes']['style'] = 'display:block';
137 // Disable the select box.
138 $form['connection_settings']['authorize_filetransfer_default']['#disabled'] = TRUE;
140 // Create a button for changing the type of connection.
141 $form['connection_settings']['change_connection_type'] = [
142 '#name' => 'change_connection_type',
144 '#value' => $this->t('Change connection type'),
146 '#attributes' => ['class' => ['filetransfer-change-connection-type']],
157 public function validateForm(array &$form, FormStateInterface $form_state) {
158 // Only validate the form if we have collected all of the user input and are
159 // ready to proceed with updating or installing.
160 if ($form_state->getTriggeringElement()['#name'] != 'process_updates') {
164 if ($form_connection_settings = $form_state->getValue('connection_settings')) {
165 $backend = $form_connection_settings['authorize_filetransfer_default'];
166 $filetransfer = $this->getFiletransfer($backend, $form_connection_settings[$backend]);
168 if (!$filetransfer) {
169 throw new \Exception($this->t('The connection protocol %backend does not exist.', ['%backend' => $backend]));
171 $filetransfer->connect();
173 catch (\Exception $e) {
174 // The format of this error message is similar to that used on the
175 // database connection form in the installer.
176 $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>.', [
177 '@message' => $e->getMessage(),
178 ':handbook_url' => 'https://www.drupal.org/documentation/install/modules-themes',
187 public function submitForm(array &$form, FormStateInterface $form_state) {
188 $form_connection_settings = $form_state->getValue('connection_settings');
189 switch ($form_state->getTriggeringElement()['#name']) {
190 case 'process_updates':
192 // Save the connection settings to the DB.
193 $filetransfer_backend = $form_connection_settings['authorize_filetransfer_default'];
195 // If the database is available then try to save our settings. We have
196 // to make sure it is available since this code could potentially (will
197 // likely) be called during the installation process, before the
198 // database is set up.
200 $filetransfer = $this->getFiletransfer($filetransfer_backend, $form_connection_settings[$filetransfer_backend]);
202 // Now run the operation.
203 $response = $this->runOperation($filetransfer);
204 if ($response instanceof Response) {
205 $form_state->setResponse($response);
208 catch (\Exception $e) {
209 // If there is no database available, we don't care and just skip
210 // this part entirely.
215 case 'enter_connection_settings':
216 $form_state->setRebuild();
219 case 'change_connection_type':
220 $form_state->setRebuild();
221 $form_state->unsetValue(['connection_settings', 'authorize_filetransfer_default']);
227 * Gets a FileTransfer class for a specific transfer method and settings.
230 * The FileTransfer backend to get the class for.
232 * Array of settings for the FileTransfer.
234 * @return \Drupal\Core\FileTransfer\FileTransfer|bool
235 * An instantiated FileTransfer object for the requested method and settings,
236 * or FALSE if there was an error finding or instantiating it.
238 protected function getFiletransfer($backend, $settings = []) {
239 $filetransfer = FALSE;
240 if (!empty($_SESSION['authorize_filetransfer_info'][$backend])) {
241 $backend_info = $_SESSION['authorize_filetransfer_info'][$backend];
242 if (class_exists($backend_info['class'])) {
243 $filetransfer = $backend_info['class']::factory($this->root, $settings);
246 return $filetransfer;
250 * Generates the Form API array for a given connection backend's settings.
252 * @param string $backend
253 * The name of the backend (e.g. 'ftp', 'ssh', etc).
256 * Form API array of connection settings for the given backend.
258 * @see hook_filetransfer_backends()
260 protected function addConnectionSettings($backend) {
264 // Create an instance of the file transfer class to get its settings form.
265 $filetransfer = $this->getFiletransfer($backend);
267 $form = $filetransfer->getSettingsForm();
269 // Fill in the defaults based on the saved settings, if any.
270 $this->setConnectionSettingsDefaults($form, NULL, $defaults);
275 * Sets the default settings on a file transfer connection form recursively.
277 * The default settings for the file transfer connection forms are saved in
278 * the database. The settings are stored as a nested array in the case of a
279 * settings form that has details or otherwise uses a nested structure.
280 * Therefore, to properly add defaults, we need to walk through all the
281 * children form elements and process those defaults recursively.
284 * Reference to the Form API form element we're operating on.
286 * The key for our current form element, if any.
287 * @param array $defaults
288 * The default settings for the file transfer backend we're operating on.
290 protected function setConnectionSettingsDefaults(&$element, $key, array $defaults) {
291 // If we're operating on a form element which isn't a details, and we have
292 // a default setting saved, stash it in #default_value.
293 if (!empty($key) && isset($defaults[$key]) && isset($element['#type']) && $element['#type'] != 'details') {
294 $element['#default_value'] = $defaults[$key];
296 // Now, we walk through all the child elements, and recursively invoke
297 // ourselves on each one. Since the $defaults settings array can be nested
298 // (because of #tree, any values inside details will be nested), if
299 // there's a subarray of settings for the form key we're currently
300 // processing, pass in that subarray to the recursive call. Otherwise, just
301 // pass on the whole $defaults array.
302 foreach (Element::children($element) as $child_key) {
303 $this->setConnectionSettingsDefaults($element[$child_key], $child_key, ((isset($defaults[$key]) && is_array($defaults[$key])) ? $defaults[$key] : $defaults));
308 * Runs the operation specified in $_SESSION['authorize_operation'].
310 * @param $filetransfer
311 * The FileTransfer object to use for running the operation.
313 * @return \Symfony\Component\HttpFoundation\Response|null
314 * The result of running the operation. If this is an instance of
315 * \Symfony\Component\HttpFoundation\Response the calling code should use
316 * that response for the current page request.
318 protected function runOperation($filetransfer) {
319 $operation = $_SESSION['authorize_operation'];
320 unset($_SESSION['authorize_operation']);
322 require_once $operation['file'];
323 return call_user_func_array($operation['callback'], array_merge([$filetransfer], $operation['arguments']));