FileTransferAuthorizeForm.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <?php
  2. namespace Drupal\Core\FileTransfer\Form;
  3. use Drupal\Core\Form\FormBase;
  4. use Drupal\Core\Form\FormStateInterface;
  5. use Drupal\Core\Render\Element;
  6. use Symfony\Component\DependencyInjection\ContainerInterface;
  7. use Symfony\Component\HttpFoundation\Response;
  8. /**
  9. * Provides the file transfer authorization form.
  10. *
  11. * @internal
  12. */
  13. class FileTransferAuthorizeForm extends FormBase {
  14. /**
  15. * The app root.
  16. *
  17. * @var string
  18. */
  19. protected $root;
  20. /**
  21. * Constructs a new FileTransferAuthorizeForm object.
  22. *
  23. * @param string $root
  24. * The app root.
  25. */
  26. public function __construct($root) {
  27. $this->root = $root;
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public static function create(ContainerInterface $container) {
  33. return new static($container->get('app.root'));
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function getFormId() {
  39. return 'authorize_filetransfer_form';
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function buildForm(array $form, FormStateInterface $form_state) {
  45. // Get all the available ways to transfer files.
  46. $available_backends = $this->getRequest()->getSession()->get('authorize_filetransfer_info', []);
  47. if (empty($available_backends)) {
  48. $this->messenger()->addError($this->t('Unable to continue, no available methods of file transfer'));
  49. return [];
  50. }
  51. if (!$this->getRequest()->isSecure()) {
  52. $form['information']['https_warning'] = [
  53. '#prefix' => '<div class="messages messages--error">',
  54. '#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']),
  55. '#suffix' => '</div>',
  56. ];
  57. }
  58. // Decide on a default backend.
  59. $authorize_filetransfer_default = $form_state->getValue(['connection_settings', 'authorize_filetransfer_default']);
  60. if (!$authorize_filetransfer_default) {
  61. $authorize_filetransfer_default = key($available_backends);
  62. }
  63. $form['information']['main_header'] = [
  64. '#prefix' => '<h3>',
  65. '#markup' => $this->t('To continue, provide your server connection details'),
  66. '#suffix' => '</h3>',
  67. ];
  68. $form['connection_settings']['#tree'] = TRUE;
  69. $form['connection_settings']['authorize_filetransfer_default'] = [
  70. '#type' => 'select',
  71. '#title' => $this->t('Connection method'),
  72. '#default_value' => $authorize_filetransfer_default,
  73. '#weight' => -10,
  74. ];
  75. /*
  76. * Here we create two submit buttons. For a JS enabled client, they will
  77. * only ever see submit_process. However, if a client doesn't have JS
  78. * enabled, they will see submit_connection on the first form (when picking
  79. * what filetransfer type to use, and submit_process on the second one (which
  80. * leads to the actual operation).
  81. */
  82. $form['submit_connection'] = [
  83. '#prefix' => "<br style='clear:both'/>",
  84. '#name' => 'enter_connection_settings',
  85. '#type' => 'submit',
  86. '#value' => $this->t('Enter connection settings'),
  87. '#weight' => 100,
  88. ];
  89. $form['submit_process'] = [
  90. '#name' => 'process_updates',
  91. '#type' => 'submit',
  92. '#value' => $this->t('Continue'),
  93. '#weight' => 100,
  94. ];
  95. // Build a container for each connection type.
  96. foreach ($available_backends as $name => $backend) {
  97. $form['connection_settings']['authorize_filetransfer_default']['#options'][$name] = $backend['title'];
  98. $form['connection_settings'][$name] = [
  99. '#type' => 'container',
  100. '#attributes' => ['class' => ["filetransfer-$name", 'filetransfer']],
  101. '#states' => [
  102. 'visible' => [
  103. 'select[name="connection_settings[authorize_filetransfer_default]"]' => ['value' => $name],
  104. ],
  105. ],
  106. ];
  107. // We can't use #prefix on the container itself since then the header won't
  108. // be hidden and shown when the containers are being manipulated via JS.
  109. $form['connection_settings'][$name]['header'] = [
  110. '#markup' => '<h4>' . $this->t('@backend connection settings', ['@backend' => $backend['title']]) . '</h4>',
  111. ];
  112. $form['connection_settings'][$name] += $this->addConnectionSettings($name);
  113. // Start non-JS code.
  114. if ($form_state->getValue(['connection_settings', 'authorize_filetransfer_default']) == $name) {
  115. // Change the submit button to the submit_process one.
  116. $form['submit_process']['#attributes'] = [];
  117. unset($form['submit_connection']);
  118. // Activate the proper filetransfer settings form.
  119. $form['connection_settings'][$name]['#attributes']['style'] = 'display:block';
  120. // Disable the select box.
  121. $form['connection_settings']['authorize_filetransfer_default']['#disabled'] = TRUE;
  122. // Create a button for changing the type of connection.
  123. $form['connection_settings']['change_connection_type'] = [
  124. '#name' => 'change_connection_type',
  125. '#type' => 'submit',
  126. '#value' => $this->t('Change connection type'),
  127. '#weight' => -5,
  128. '#attributes' => ['class' => ['filetransfer-change-connection-type']],
  129. ];
  130. }
  131. // End non-JS code.
  132. }
  133. return $form;
  134. }
  135. /**
  136. * {@inheritdoc}
  137. */
  138. public function validateForm(array &$form, FormStateInterface $form_state) {
  139. // Only validate the form if we have collected all of the user input and are
  140. // ready to proceed with updating or installing.
  141. if ($form_state->getTriggeringElement()['#name'] != 'process_updates') {
  142. return;
  143. }
  144. if ($form_connection_settings = $form_state->getValue('connection_settings')) {
  145. $backend = $form_connection_settings['authorize_filetransfer_default'];
  146. $filetransfer = $this->getFiletransfer($backend, $form_connection_settings[$backend]);
  147. try {
  148. if (!$filetransfer) {
  149. throw new \Exception("The connection protocol '$backend' does not exist.");
  150. }
  151. $filetransfer->connect();
  152. }
  153. catch (\Exception $e) {
  154. // The format of this error message is similar to that used on the
  155. // database connection form in the installer.
  156. $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>.', [
  157. '@message' => $e->getMessage(),
  158. ':handbook_url' => 'https://www.drupal.org/docs/8/extending-drupal-8/overview',
  159. ]));
  160. }
  161. }
  162. }
  163. /**
  164. * {@inheritdoc}
  165. */
  166. public function submitForm(array &$form, FormStateInterface $form_state) {
  167. $form_connection_settings = $form_state->getValue('connection_settings');
  168. switch ($form_state->getTriggeringElement()['#name']) {
  169. case 'process_updates':
  170. // Save the connection settings to the DB.
  171. $filetransfer_backend = $form_connection_settings['authorize_filetransfer_default'];
  172. // If the database is available then try to save our settings. We have
  173. // to make sure it is available since this code could potentially (will
  174. // likely) be called during the installation process, before the
  175. // database is set up.
  176. try {
  177. $filetransfer = $this->getFiletransfer($filetransfer_backend, $form_connection_settings[$filetransfer_backend]);
  178. // Now run the operation.
  179. $response = $this->runOperation($filetransfer);
  180. if ($response instanceof Response) {
  181. $form_state->setResponse($response);
  182. }
  183. }
  184. catch (\Exception $e) {
  185. // If there is no database available, we don't care and just skip
  186. // this part entirely.
  187. }
  188. break;
  189. case 'enter_connection_settings':
  190. $form_state->setRebuild();
  191. break;
  192. case 'change_connection_type':
  193. $form_state->setRebuild();
  194. $form_state->unsetValue(['connection_settings', 'authorize_filetransfer_default']);
  195. break;
  196. }
  197. }
  198. /**
  199. * Gets a FileTransfer class for a specific transfer method and settings.
  200. *
  201. * @param $backend
  202. * The FileTransfer backend to get the class for.
  203. * @param $settings
  204. * Array of settings for the FileTransfer.
  205. *
  206. * @return \Drupal\Core\FileTransfer\FileTransfer|bool
  207. * An instantiated FileTransfer object for the requested method and settings,
  208. * or FALSE if there was an error finding or instantiating it.
  209. */
  210. protected function getFiletransfer($backend, $settings = []) {
  211. $filetransfer = FALSE;
  212. $info = $this->getRequest()->getSession()->get('authorize_filetransfer_info', []);
  213. if (!empty($info[$backend])) {
  214. if (class_exists($info[$backend]['class'])) {
  215. $filetransfer = $info[$backend]['class']::factory($this->root, $settings);
  216. }
  217. }
  218. return $filetransfer;
  219. }
  220. /**
  221. * Generates the Form API array for a given connection backend's settings.
  222. *
  223. * @param string $backend
  224. * The name of the backend (e.g. 'ftp', 'ssh', etc).
  225. *
  226. * @return array
  227. * Form API array of connection settings for the given backend.
  228. *
  229. * @see hook_filetransfer_backends()
  230. */
  231. protected function addConnectionSettings($backend) {
  232. $defaults = [];
  233. $form = [];
  234. // Create an instance of the file transfer class to get its settings form.
  235. $filetransfer = $this->getFiletransfer($backend);
  236. if ($filetransfer) {
  237. $form = $filetransfer->getSettingsForm();
  238. }
  239. // Fill in the defaults based on the saved settings, if any.
  240. $this->setConnectionSettingsDefaults($form, NULL, $defaults);
  241. return $form;
  242. }
  243. /**
  244. * Sets the default settings on a file transfer connection form recursively.
  245. *
  246. * The default settings for the file transfer connection forms are saved in
  247. * the database. The settings are stored as a nested array in the case of a
  248. * settings form that has details or otherwise uses a nested structure.
  249. * Therefore, to properly add defaults, we need to walk through all the
  250. * children form elements and process those defaults recursively.
  251. *
  252. * @param $element
  253. * Reference to the Form API form element we're operating on.
  254. * @param $key
  255. * The key for our current form element, if any.
  256. * @param array $defaults
  257. * The default settings for the file transfer backend we're operating on.
  258. */
  259. protected function setConnectionSettingsDefaults(&$element, $key, array $defaults) {
  260. // If we're operating on a form element which isn't a details, and we have
  261. // a default setting saved, stash it in #default_value.
  262. if (!empty($key) && isset($defaults[$key]) && isset($element['#type']) && $element['#type'] != 'details') {
  263. $element['#default_value'] = $defaults[$key];
  264. }
  265. // Now, we walk through all the child elements, and recursively invoke
  266. // ourselves on each one. Since the $defaults settings array can be nested
  267. // (because of #tree, any values inside details will be nested), if
  268. // there's a subarray of settings for the form key we're currently
  269. // processing, pass in that subarray to the recursive call. Otherwise, just
  270. // pass on the whole $defaults array.
  271. foreach (Element::children($element) as $child_key) {
  272. $this->setConnectionSettingsDefaults($element[$child_key], $child_key, ((isset($defaults[$key]) && is_array($defaults[$key])) ? $defaults[$key] : $defaults));
  273. }
  274. }
  275. /**
  276. * Runs the operation specified in 'authorize_operation' session property.
  277. *
  278. * @param $filetransfer
  279. * The FileTransfer object to use for running the operation.
  280. *
  281. * @return \Symfony\Component\HttpFoundation\Response|null
  282. * The result of running the operation. If this is an instance of
  283. * \Symfony\Component\HttpFoundation\Response the calling code should use
  284. * that response for the current page request.
  285. */
  286. protected function runOperation($filetransfer) {
  287. $operation = $this->getRequest()->getSession()->remove('authorize_operation');
  288. require_once $operation['file'];
  289. return call_user_func_array($operation['callback'], array_merge([$filetransfer], $operation['arguments']));
  290. }
  291. }