diff --git a/core/lib/Drupal/Core/FileTransfer/Form/FileTransferAuthorizeForm.php b/core/lib/Drupal/Core/FileTransfer/Form/FileTransferAuthorizeForm.php index 6965b7f..765d955 100644 --- a/core/lib/Drupal/Core/FileTransfer/Form/FileTransferAuthorizeForm.php +++ b/core/lib/Drupal/Core/FileTransfer/Form/FileTransferAuthorizeForm.php @@ -2,37 +2,44 @@ /** * @file - * Helper functions and form handlers used for the authorize.php script. + * Contains \Drupal\Core\FileTransfer\Form\FileTransferAuthorizeForm. */ +namespace Drupal\Core\FileTransfer\Form; + +use Drupal\Core\Form\FormBase; use Drupal\Core\Render\Element; /** - * Form constructor for the file transfer authorization form. - * - * Allows the user to choose a FileTransfer type and supply credentials. - * - * @see authorize_filetransfer_form_validate() - * @see authorize_filetransfer_form_submit() - * @ingroup forms + * Provides the file transfer authorization form. */ -function authorize_filetransfer_form($form, &$form_state) { - $form = array(); +class FileTransferAuthorizeForm extends FormBase { + /** + * {@inheritdoc} + */ + public function getFormID() { + return 'authorize_filetransfer_form'; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, array &$form_state) { // If possible, we want to post this form securely via HTTPS. $form['#https'] = TRUE; // Get all the available ways to transfer files. if (empty($_SESSION['authorize_filetransfer_info'])) { - drupal_set_message(t('Unable to continue, no available methods of file transfer'), 'error'); + drupal_set_message($this->t('Unable to continue, no available methods of file transfer'), 'error'); return array(); } $available_backends = $_SESSION['authorize_filetransfer_info']; - if (!\Drupal::request()->isSecure()) { + if (!$this->getRequest()->isSecure()) { $form['information']['https_warning'] = array( '#prefix' => '
', - '#markup' => t('WARNING: You are not using an encrypted connection, so your password will be sent in plain text. Learn more.', array('@https-link' => 'http://drupal.org/https-information')), + '#markup' => $this->t('WARNING: You are not using an encrypted connection, so your password will be sent in plain text. Learn more.', array('@https-link' => 'http://drupal.org/https-information')), '#suffix' => '
', ); } @@ -41,21 +48,21 @@ function authorize_filetransfer_form($form, &$form_state) { if (isset($form_state['values']['connection_settings']['authorize_filetransfer_default'])) { $authorize_filetransfer_default = $form_state['values']['connection_settings']['authorize_filetransfer_default']; } - elseif ($authorize_filetransfer_default = \Drupal::config('system.authorize')->get('filetransfer_default')); + elseif ($authorize_filetransfer_default = $this->config('system.authorize')->get('filetransfer_default')); else { $authorize_filetransfer_default = key($available_backends); } $form['information']['main_header'] = array( '#prefix' => '

', - '#markup' => t('To continue, provide your server connection details'), + '#markup' => $this->t('To continue, provide your server connection details'), '#suffix' => '

', ); $form['connection_settings']['#tree'] = TRUE; $form['connection_settings']['authorize_filetransfer_default'] = array( '#type' => 'select', - '#title' => t('Connection method'), + '#title' => $this->t('Connection method'), '#default_value' => $authorize_filetransfer_default, '#weight' => -10, ); @@ -71,14 +78,14 @@ function authorize_filetransfer_form($form, &$form_state) { '#prefix' => "
", '#name' => 'enter_connection_settings', '#type' => 'submit', - '#value' => t('Enter connection settings'), + '#value' => $this->t('Enter connection settings'), '#weight' => 100, ); $form['submit_process'] = array( '#name' => 'process_updates', '#type' => 'submit', - '#value' => t('Continue'), + '#value' => $this->t('Continue'), '#weight' => 100, ); @@ -97,10 +104,10 @@ function authorize_filetransfer_form($form, &$form_state) { // We can't use #prefix on the container itself since then the header won't // be hidden and shown when the containers are being manipulated via JS. $form['connection_settings'][$name]['header'] = array( - '#markup' => '

' . t('@backend connection settings', array('@backend' => $backend['title'])) . '

', + '#markup' => '

' . $this->t('@backend connection settings', array('@backend' => $backend['title'])) . '

', ); - $form['connection_settings'][$name] += _authorize_filetransfer_connection_settings($name); + $form['connection_settings'][$name] += $this->addConnectionSettings($name); // Start non-JS code. if (isset($form_state['values']['connection_settings']['authorize_filetransfer_default']) && $form_state['values']['connection_settings']['authorize_filetransfer_default'] == $name) { @@ -118,7 +125,7 @@ function authorize_filetransfer_form($form, &$form_state) { $form['connection_settings']['change_connection_type'] = array( '#name' => 'change_connection_type', '#type' => 'submit', - '#value' => t('Change connection type'), + '#value' => $this->t('Change connection type'), '#weight' => -5, '#attributes' => array('class' => array('filetransfer-change-connection-type')), ); @@ -126,74 +133,12 @@ function authorize_filetransfer_form($form, &$form_state) { // End non-JS code. } return $form; -} - -/** - * Generates the Form API array for a given connection backend's settings. - * - * @param $backend - * The name of the backend (e.g. 'ftp', 'ssh', etc). - * - * @return - * Form API array of connection settings for the given backend. - * - * @see hook_filetransfer_backends() - */ -function _authorize_filetransfer_connection_settings($backend) { - $auth_connection_config = \Drupal::config('system.authorize')->get('filetransfer_connection_settings_' . $backend); - $defaults = $auth_connection_config ? $auth_connection_config : array(); - $form = array(); - - // Create an instance of the file transfer class to get its settings form. - $filetransfer = authorize_get_filetransfer($backend); - if ($filetransfer) { - $form = $filetransfer->getSettingsForm(); - } - // Fill in the defaults based on the saved settings, if any. - _authorize_filetransfer_connection_settings_set_defaults($form, NULL, $defaults); - return $form; -} - -/** - * Sets the default settings on a file transfer connection form recursively. - * - * The default settings for the file transfer connection forms are saved in - * the database. The settings are stored as a nested array in the case of a - * settings form that has details or otherwise uses a nested structure. - * Therefore, to properly add defaults, we need to walk through all the - * children form elements and process those defaults recursively. - * - * @param $element - * Reference to the Form API form element we're operating on. - * @param $key - * The key for our current form element, if any. - * @param array $defaults - * The default settings for the file transfer backend we're operating on. - */ -function _authorize_filetransfer_connection_settings_set_defaults(&$element, $key, array $defaults) { - // If we're operating on a form element which isn't a details, and we have - // a default setting saved, stash it in #default_value. - if (!empty($key) && isset($defaults[$key]) && isset($element['#type']) && $element['#type'] != 'details') { - $element['#default_value'] = $defaults[$key]; - } - // Now, we walk through all the child elements, and recursively invoke - // ourself on each one. Since the $defaults settings array can be nested - // (because of #tree, any values inside details will be nested), if - // there's a subarray of settings for the form key we're currently - // processing, pass in that subarray to the recursive call. Otherwise, just - // pass on the whole $defaults array. - foreach (Element::children($element) as $child_key) { - _authorize_filetransfer_connection_settings_set_defaults($element[$child_key], $child_key, ((isset($defaults[$key]) && is_array($defaults[$key])) ? $defaults[$key] : $defaults)); } -} -/** - * Form validation handler for authorize_filetransfer_form(). - * - * @see authorize_filetransfer_form() - * @see authorize_filetransfer_submit() + /** + * {@inheritdoc} */ -function authorize_filetransfer_form_validate($form, &$form_state) { + public function validateForm(array &$form, array &$form_state) { // Only validate the form if we have collected all of the user input and are // ready to proceed with updating or installing. if ($form_state['triggering_element']['#name'] != 'process_updates') { @@ -202,31 +147,28 @@ function authorize_filetransfer_form_validate($form, &$form_state) { if (isset($form_state['values']['connection_settings'])) { $backend = $form_state['values']['connection_settings']['authorize_filetransfer_default']; - $filetransfer = authorize_get_filetransfer($backend, $form_state['values']['connection_settings'][$backend]); + $filetransfer = $this->getFiletransfer($backend, $form_state['values']['connection_settings'][$backend]); try { if (!$filetransfer) { - throw new Exception(t('The connection protocol %backend does not exist.', array('%backend' => $backend))); + throw new \Exception($this->t('The connection protocol %backend does not exist.', array('%backend' => $backend))); } $filetransfer->connect(); } - catch (Exception $e) { + catch (\Exception $e) { // The format of this error message is similar to that used on the // database connection form in the installer. - form_set_error('connection_settings', $form_state, t('Failed to connect to the server. The server reports the following message: !message For more help installing or updating code on your server, see the handbook.', array( + $this->setFormError('connection_settings', $form_state, $this->t('Failed to connect to the server. The server reports the following message: !message For more help installing or updating code on your server, see the handbook.', array( '!message' => '

' . $e->getMessage() . '

', '@handbook_url' => 'http://drupal.org/documentation/install/modules-themes', ))); } } -} + } -/** - * Form submission handler for authorize_filetransfer_form(). - * - * @see authorize_filetransfer_form() - * @see authorize_filetransfer_validate() + /** + * {@inheritdoc} */ -function authorize_filetransfer_form_submit($form, &$form_state) { + public function submitForm(array &$form, array &$form_state) { switch ($form_state['triggering_element']['#name']) { case 'process_updates': @@ -255,16 +197,16 @@ function authorize_filetransfer_form_submit($form, &$form_state) { } } // Set this one as the default authorize method. - \Drupal::config('system.authorize')->set('filetransfer_default', $filetransfer_backend); + $this->config('system.authorize')->set('filetransfer_default', $filetransfer_backend); // Save the connection settings minus the password. - \Drupal::config('system.authorize')->set('filetransfer_connection_settings_' . $filetransfer_backend, $connection_settings); + $this->config('system.authorize')->set('filetransfer_connection_settings_' . $filetransfer_backend, $connection_settings); - $filetransfer = authorize_get_filetransfer($filetransfer_backend, $form_state['values']['connection_settings'][$filetransfer_backend]); + $filetransfer = $this->getFiletransfer($filetransfer_backend, $form_state['values']['connection_settings'][$filetransfer_backend]); // Now run the operation. - authorize_run_operation($filetransfer); + $this->runOperation($filetransfer); } - catch (Exception $e) { + catch (\Exception $e) { // If there is no database available, we don't care and just skip // this part entirely. } @@ -280,23 +222,9 @@ function authorize_filetransfer_form_submit($form, &$form_state) { unset($form_state['values']['connection_settings']['authorize_filetransfer_default']); break; } -} - -/** - * Runs the operation specified in $_SESSION['authorize_operation']. - * - * @param $filetransfer - * The FileTransfer object to use for running the operation. - */ -function authorize_run_operation($filetransfer) { - $operation = $_SESSION['authorize_operation']; - unset($_SESSION['authorize_operation']); - - require_once DRUPAL_ROOT . '/' . $operation['file']; - call_user_func_array($operation['callback'], array_merge(array($filetransfer), $operation['arguments'])); -} + } -/** + /** * Gets a FileTransfer class for a specific transfer method and settings. * * @param $backend @@ -304,11 +232,11 @@ function authorize_run_operation($filetransfer) { * @param $settings * Array of settings for the FileTransfer. * - * @return + * @return \Drupal\Core\FileTransfer\FileTransfer|bool * An instantiated FileTransfer object for the requested method and settings, * or FALSE if there was an error finding or instantiating it. */ -function authorize_get_filetransfer($backend, $settings = array()) { + protected function getFiletransfer($backend, $settings = array()) { $filetransfer = FALSE; if (!empty($_SESSION['authorize_filetransfer_info'][$backend])) { $backend_info = $_SESSION['authorize_filetransfer_info'][$backend]; @@ -317,4 +245,79 @@ function authorize_get_filetransfer($backend, $settings = array()) { } } return $filetransfer; + } + + /** + * Generates the Form API array for a given connection backend's settings. + * + * @param string $backend + * The name of the backend (e.g. 'ftp', 'ssh', etc). + * + * @return array + * Form API array of connection settings for the given backend. + * + * @see hook_filetransfer_backends() + */ + protected function addConnectionSettings($backend) { + $auth_connection_config = $this->config('system.authorize')->get('filetransfer_connection_settings_' . $backend); + $defaults = $auth_connection_config ? $auth_connection_config : array(); + $form = array(); + + // Create an instance of the file transfer class to get its settings form. + $filetransfer = $this->getFiletransfer($backend); + if ($filetransfer) { + $form = $filetransfer->getSettingsForm(); + } + // Fill in the defaults based on the saved settings, if any. + $this->setConnectionSettingsDefaults($form, NULL, $defaults); + return $form; + } + + /** + * Sets the default settings on a file transfer connection form recursively. + * + * The default settings for the file transfer connection forms are saved in + * the database. The settings are stored as a nested array in the case of a + * settings form that has details or otherwise uses a nested structure. + * Therefore, to properly add defaults, we need to walk through all the + * children form elements and process those defaults recursively. + * + * @param $element + * Reference to the Form API form element we're operating on. + * @param $key + * The key for our current form element, if any. + * @param array $defaults + * The default settings for the file transfer backend we're operating on. + */ + protected function setConnectionSettingsDefaults(&$element, $key, array $defaults) { + // If we're operating on a form element which isn't a details, and we have + // a default setting saved, stash it in #default_value. + if (!empty($key) && isset($defaults[$key]) && isset($element['#type']) && $element['#type'] != 'details') { + $element['#default_value'] = $defaults[$key]; + } + // Now, we walk through all the child elements, and recursively invoke + // ourselves on each one. Since the $defaults settings array can be nested + // (because of #tree, any values inside details will be nested), if + // there's a subarray of settings for the form key we're currently + // processing, pass in that subarray to the recursive call. Otherwise, just + // pass on the whole $defaults array. + foreach (Element::children($element) as $child_key) { + $this->setConnectionSettingsDefaults($element[$child_key], $child_key, ((isset($defaults[$key]) && is_array($defaults[$key])) ? $defaults[$key] : $defaults)); + } + } + + /** + * Runs the operation specified in $_SESSION['authorize_operation']. + * + * @param $filetransfer + * The FileTransfer object to use for running the operation. + */ + protected function runOperation($filetransfer) { + $operation = $_SESSION['authorize_operation']; + unset($_SESSION['authorize_operation']); + + require_once DRUPAL_ROOT . '/' . $operation['file']; + call_user_func_array($operation['callback'], array_merge(array($filetransfer), $operation['arguments'])); + } + }