View source
<?php
namespace Drupal\Core\FileTransfer\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Response;
class FileTransferAuthorizeForm extends FormBase {
protected $root;
public function __construct($root) {
$this->root = $root;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('app.root'));
}
public function getFormId() {
return 'authorize_filetransfer_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
if (empty($_SESSION['authorize_filetransfer_info'])) {
drupal_set_message($this
->t('Unable to continue, no available methods of file transfer'), 'error');
return array();
}
$available_backends = $_SESSION['authorize_filetransfer_info'];
if (!$this
->getRequest()
->isSecure()) {
$form['information']['https_warning'] = array(
'#prefix' => '<div class="messages messages--error">',
'#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>.', array(
':https-link' => 'https://www.drupal.org/https-information',
)),
'#suffix' => '</div>',
);
}
if ($authorize_filetransfer_default = $form_state
->getValue(array(
'connection_settings',
'authorize_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' => '<h3>',
'#markup' => $this
->t('To continue, provide your server connection details'),
'#suffix' => '</h3>',
);
$form['connection_settings']['#tree'] = TRUE;
$form['connection_settings']['authorize_filetransfer_default'] = array(
'#type' => 'select',
'#title' => $this
->t('Connection method'),
'#default_value' => $authorize_filetransfer_default,
'#weight' => -10,
);
$form['submit_connection'] = array(
'#prefix' => "<br style='clear:both'/>",
'#name' => 'enter_connection_settings',
'#type' => 'submit',
'#value' => $this
->t('Enter connection settings'),
'#weight' => 100,
);
$form['submit_process'] = array(
'#name' => 'process_updates',
'#type' => 'submit',
'#value' => $this
->t('Continue'),
'#weight' => 100,
);
foreach ($available_backends as $name => $backend) {
$form['connection_settings']['authorize_filetransfer_default']['#options'][$name] = $backend['title'];
$form['connection_settings'][$name] = array(
'#type' => 'container',
'#attributes' => array(
'class' => array(
"filetransfer-{$name}",
'filetransfer',
),
),
'#states' => array(
'visible' => array(
'select[name="connection_settings[authorize_filetransfer_default]"]' => array(
'value' => $name,
),
),
),
);
$form['connection_settings'][$name]['header'] = array(
'#markup' => '<h4>' . $this
->t('@backend connection settings', array(
'@backend' => $backend['title'],
)) . '</h4>',
);
$form['connection_settings'][$name] += $this
->addConnectionSettings($name);
if ($form_state
->getValue(array(
'connection_settings',
'authorize_filetransfer_default',
)) == $name) {
$form['submit_process']['#attributes'] = array();
unset($form['submit_connection']);
$form['connection_settings'][$name]['#attributes']['style'] = 'display:block';
$form['connection_settings']['authorize_filetransfer_default']['#disabled'] = TRUE;
$form['connection_settings']['change_connection_type'] = array(
'#name' => 'change_connection_type',
'#type' => 'submit',
'#value' => $this
->t('Change connection type'),
'#weight' => -5,
'#attributes' => array(
'class' => array(
'filetransfer-change-connection-type',
),
),
);
}
}
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
if ($form_state
->getTriggeringElement()['#name'] != 'process_updates') {
return;
}
if ($form_connection_settings = $form_state
->getValue('connection_settings')) {
$backend = $form_connection_settings['authorize_filetransfer_default'];
$filetransfer = $this
->getFiletransfer($backend, $form_connection_settings[$backend]);
try {
if (!$filetransfer) {
throw new \Exception($this
->t('The connection protocol %backend does not exist.', array(
'%backend' => $backend,
)));
}
$filetransfer
->connect();
} catch (\Exception $e) {
$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>.', array(
'@message' => $e
->getMessage(),
':handbook_url' => 'https://www.drupal.org/documentation/install/modules-themes',
)));
}
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$form_connection_settings = $form_state
->getValue('connection_settings');
switch ($form_state
->getTriggeringElement()['#name']) {
case 'process_updates':
$filetransfer_backend = $form_connection_settings['authorize_filetransfer_default'];
try {
$connection_settings = array();
foreach ($form_connection_settings[$filetransfer_backend] as $key => $value) {
if (!isset($form['connection_settings'][$filetransfer_backend][$key]['#filetransfer_save'])) {
if ($form['connection_settings'][$filetransfer_backend][$key]['#type'] != 'password') {
$connection_settings[$key] = $value;
}
}
elseif ($form['connection_settings'][$filetransfer_backend][$key]['#filetransfer_save']) {
$connection_settings[$key] = $value;
}
}
$this
->config('system.authorize')
->set('filetransfer_default', $filetransfer_backend);
$this
->config('system.authorize')
->set('filetransfer_connection_settings_' . $filetransfer_backend, $connection_settings);
$filetransfer = $this
->getFiletransfer($filetransfer_backend, $form_connection_settings[$filetransfer_backend]);
$response = $this
->runOperation($filetransfer);
if ($response instanceof Response) {
$form_state
->setResponse($response);
}
} catch (\Exception $e) {
}
break;
case 'enter_connection_settings':
$form_state
->setRebuild();
break;
case 'change_connection_type':
$form_state
->setRebuild();
$form_state
->unsetValue(array(
'connection_settings',
'authorize_filetransfer_default',
));
break;
}
}
protected function getFiletransfer($backend, $settings = array()) {
$filetransfer = FALSE;
if (!empty($_SESSION['authorize_filetransfer_info'][$backend])) {
$backend_info = $_SESSION['authorize_filetransfer_info'][$backend];
if (class_exists($backend_info['class'])) {
$filetransfer = $backend_info['class']::factory($this->root, $settings);
}
}
return $filetransfer;
}
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();
$filetransfer = $this
->getFiletransfer($backend);
if ($filetransfer) {
$form = $filetransfer
->getSettingsForm();
}
$this
->setConnectionSettingsDefaults($form, NULL, $defaults);
return $form;
}
protected function setConnectionSettingsDefaults(&$element, $key, array $defaults) {
if (!empty($key) && isset($defaults[$key]) && isset($element['#type']) && $element['#type'] != 'details') {
$element['#default_value'] = $defaults[$key];
}
foreach (Element::children($element) as $child_key) {
$this
->setConnectionSettingsDefaults($element[$child_key], $child_key, isset($defaults[$key]) && is_array($defaults[$key]) ? $defaults[$key] : $defaults);
}
}
protected function runOperation($filetransfer) {
$operation = $_SESSION['authorize_operation'];
unset($_SESSION['authorize_operation']);
require_once $this->root . '/' . $operation['file'];
return call_user_func_array($operation['callback'], array_merge(array(
$filetransfer,
), $operation['arguments']));
}
}