d165fca56360f076d4113b7f49b01898b2336426
[yaffs-website] / web / core / modules / media / src / Form / MediaSettingsForm.php
1 <?php
2
3 namespace Drupal\media\Form;
4
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Form\ConfigFormBase;
8 use Drupal\media\IFrameUrlHelper;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10
11 /**
12  * Provides a form to configure Media settings.
13  *
14  * @internal
15  */
16 class MediaSettingsForm extends ConfigFormBase {
17
18   /**
19    * The iFrame URL helper service.
20    *
21    * @var \Drupal\media\IFrameUrlHelper
22    */
23   protected $iFrameUrlHelper;
24
25   /**
26    * MediaSettingsForm constructor.
27    *
28    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
29    *   The config factory service.
30    * @param \Drupal\media\IFrameUrlHelper $iframe_url_helper
31    *   The iFrame URL helper service.
32    */
33   public function __construct(ConfigFactoryInterface $config_factory, IFrameUrlHelper $iframe_url_helper) {
34     parent::__construct($config_factory);
35     $this->iFrameUrlHelper = $iframe_url_helper;
36   }
37
38   /**
39    * {@inheritdoc}
40    */
41   public static function create(ContainerInterface $container) {
42     return new static(
43       $container->get('config.factory'),
44       $container->get('media.oembed.iframe_url_helper')
45     );
46   }
47
48   /**
49    * {@inheritdoc}
50    */
51   public function getFormId() {
52     return 'media_settings_form';
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   protected function getEditableConfigNames() {
59     return ['media.settings'];
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public function buildForm(array $form, FormStateInterface $form_state) {
66     $domain = $this->config('media.settings')->get('iframe_domain');
67
68     if (!$this->iFrameUrlHelper->isSecure($domain)) {
69       $message = $this->t('It is potentially insecure to display oEmbed content in a frame that is served from the same domain as your main Drupal site, as this may allow execution of third-party code. <a href="https://oembed.com/#section3" target="_blank">Take a look here for more information</a>.');
70       $this->messenger()->addWarning($message);
71     }
72
73     $description = '<p>' . $this->t('Displaying media assets from third-party services, such as YouTube or Twitter, can be risky. This is because many of these services return arbitrary HTML to represent those assets, and that HTML may contain executable JavaScript code. If handled improperly, this can increase the risk of your site being compromised.') . '</p>';
74     $description .= '<p>' . $this->t('In order to mitigate the risks, third-party assets are displayed in an iFrame, which effectively sandboxes any executable code running inside it. For even more security, the iFrame can be served from an alternate domain (that also points to your Drupal site), which you can configure on this page. This helps safeguard cookies and other sensitive information.') . '</p>';
75
76     $form['security'] = [
77       '#type' => 'details',
78       '#title' => $this->t('Security'),
79       '#description' => $description,
80       '#open' => TRUE,
81     ];
82     // @todo Figure out how and if we should validate that this domain actually
83     // points back to Drupal.
84     // See https://www.drupal.org/project/drupal/issues/2965979 for more info.
85     $form['security']['iframe_domain'] = [
86       '#type' => 'url',
87       '#title' => $this->t('iFrame domain'),
88       '#size' => 40,
89       '#maxlength' => 255,
90       '#default_value' => $domain,
91       '#description' => $this->t('Enter a different domain from which to serve oEmbed content, including the <em>http://</em> or <em>https://</em> prefix. This domain needs to point back to this site, or existing oEmbed content may not display correctly, or at all.'),
92     ];
93
94     return parent::buildForm($form, $form_state);
95   }
96
97   /**
98    * {@inheritdoc}
99    */
100   public function submitForm(array &$form, FormStateInterface $form_state) {
101     $this->config('media.settings')
102       ->set('iframe_domain', $form_state->getValue('iframe_domain'))
103       ->save();
104
105     parent::submitForm($form, $form_state);
106   }
107
108 }