You are here

class RemoveXFrameOptionsSubscriber in Allow site iframing 8

Same name and namespace in other branches
  1. 3.0.x src/EventSubscriber/RemoveXFrameOptionsSubscriber.php \Drupal\allow_iframed_site\EventSubscriber\RemoveXFrameOptionsSubscriber

An event subscriber to remove the X-Frame-Options header.

Hierarchy

  • class \Drupal\allow_iframed_site\EventSubscriber\RemoveXFrameOptionsSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of RemoveXFrameOptionsSubscriber

1 string reference to 'RemoveXFrameOptionsSubscriber'
allow_iframed_site.services.yml in ./allow_iframed_site.services.yml
allow_iframed_site.services.yml
1 service uses RemoveXFrameOptionsSubscriber
remove_x_frame_options_subscriber in ./allow_iframed_site.services.yml
Drupal\allow_iframed_site\EventSubscriber\RemoveXFrameOptionsSubscriber

File

src/EventSubscriber/RemoveXFrameOptionsSubscriber.php, line 15

Namespace

Drupal\allow_iframed_site\EventSubscriber
View source
class RemoveXFrameOptionsSubscriber implements EventSubscriberInterface {

  /**
   * Array of Config options.
   *
   * @var array $config
   */
  protected $config;

  /**
   * Condition manager.
   *
   * @var \Drupal\Core\Executable\ExecutableManagerInterface
   */
  protected $conditionManager;

  /**
   * RemoveXFrameOptionsSubscriber constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   ConfigFactory.
   * @param \Drupal\Core\Executable\ExecutableManagerInterface $condition_manager
   *   ConditionManager.
   */
  public function __construct(ConfigFactoryInterface $config_factory, ExecutableManagerInterface $condition_manager) {
    $this->config = $config_factory
      ->get('allow_iframed_site.settings');
    $this->conditionManager = $condition_manager;
  }

  /**
   * Remove the X-Frame-Options header.
   *
   * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
   *
   * @throws \Drupal\Component\Plugin\Exception\PluginException
   */
  public function RemoveXFrameOptions(FilterResponseEvent $event) {
    $xframe = TRUE;
    foreach ($this->config
      ->getRawData() as $key => $config) {
      try {

        /* @var \Drupal\system\Plugin\Condition\RequestPath $condition */
        $condition = $this->conditionManager
          ->createInstance($key);
        $condition
          ->setConfiguration($this->config
          ->get($key));
        if ($condition
          ->evaluate() && $condition
          ->isNegated() || !$condition
          ->evaluate() && !$condition
          ->isNegated()) {
          $xframe = FALSE;
        }
      } catch (PluginException $exception) {

        // Just ignore it, there's probably not much else to do.
      }
    }

    // If we got here we should be fine, but check it anyway.
    if ($xframe) {
      $response = $event
        ->getResponse();
      $response->headers
        ->remove('X-Frame-Options');
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::RESPONSE][] = [
      'RemoveXFrameOptions',
      -10,
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RemoveXFrameOptionsSubscriber::$conditionManager protected property Condition manager.
RemoveXFrameOptionsSubscriber::$config protected property Array of Config options.
RemoveXFrameOptionsSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
RemoveXFrameOptionsSubscriber::RemoveXFrameOptions public function Remove the X-Frame-Options header.
RemoveXFrameOptionsSubscriber::__construct public function RemoveXFrameOptionsSubscriber constructor.