/**
  * 输出debugBar,只有当页面有</body>标签时有效。
  *
  * @param FilterResponseEvent $event
  */
 public function onKernelResponse(FilterResponseEvent $event)
 {
     $response = $event->getResponse();
     $request = $event->getRequest();
     if (!$event->isMasterRequest()) {
         return;
     }
     if ($request->isXmlHttpRequest()) {
         return;
     }
     if ($response->isRedirection() || $response->headers->has('Content-Type') && false === strpos($response->headers->get('Content-Type'), 'html') || 'html' !== $request->getRequestFormat()) {
         return;
     }
     $path = null;
     if ($this->app['debug_bar.auto_res']) {
         $scriptName = $event->getRequest()->server->get('SCRIPT_NAME');
         $path = $scriptName . $this->app['debug_bar.path'];
     } else {
         $path = $this->app['debug_bar.path'];
     }
     $render = $this->app['debug_bar']->getJavascriptRenderer($path);
     ob_start();
     echo $render->renderHead();
     echo $render->render();
     $debugContent = ob_get_contents();
     ob_end_clean();
     $content = $response->getContent();
     if (false === strpos($content, '</body>')) {
         $content .= $debugContent;
     } else {
         $content = str_replace("</body>", $debugContent . '</body>', $content);
     }
     $event->getResponse()->setContent($content);
 }
Esempio n. 2
0
 /**
  * Trigger event for RESPONSE Kervenel vent
  *
  * @param FilterResponseEvent $event event
  */
 public function onResponse(FilterResponseEvent $event)
 {
     $response = $event->getResponse();
     $request = $event->getRequest();
     if (!$event->isMasterRequest()) {
         return;
     }
     if ($request->isXmlHttpRequest()) {
         return;
     }
     if ($response->isRedirection() || $response->headers->has('Content-Type') && false === strpos($response->headers->get('Content-Type'), 'html') || 'html' !== $request->getRequestFormat()) {
         return;
     }
     $configResolver = $this->container->get('ezpublish.config.resolver');
     $domain = $configResolver->getParameter('domain', 'edgar_ez_cdn');
     $extensions = $configResolver->getParameter('extensions', 'edgar_ez_cdn');
     $http = $request->isSecure() ? 'https' : 'http';
     if (empty($extensions)) {
         return;
     }
     if (!is_array($extensions)) {
         $extensions = array($extensions);
     }
     if (count($extensions) == 0) {
         return;
     }
     $extensions = implode('|', $extensions);
     $content = $response->getContent();
     $pattern = '/="[^"]*\\/(css|js|var|bundles)\\/(.*)\\.(' . $extensions . ')([^"]*)"/i';
     $replace = '="' . $http . '://' . $domain . '/${1}/${2}.${3}${4}"';
     $content = preg_replace($pattern, $replace, $content);
     $response->setContent($content);
 }
 /**
  * add a rel=self Link header to the response
  *
  * @param FilterResponseEvent $event response listener event
  *
  * @return void
  */
 public function onKernelResponse(FilterResponseEvent $event)
 {
     if (!$event->isMasterRequest()) {
         // don't do anything if it's not the master request
         return;
     }
     $response = $event->getResponse();
     $request = $event->getRequest();
     $linkHeader = LinkHeader::fromResponse($response);
     // extract various info from route
     $routeName = $request->get('_route');
     $routeParts = explode('.', $routeName);
     $routeType = end($routeParts);
     if ($routeType == 'post') {
         $routeName = substr($routeName, 0, -4) . 'get';
     }
     /** if the request failed in the RestController, $request will not have an record id in
         case of a POST and $router->generate() will fail. that's why we catch it and fail silently
         by not including our header in the response. i hope that's a good compromise. **/
     /** Nope, it's not a good compromise...catch and handle it where it happens.
      *  I will refactory this in another branch*/
     $addHeader = true;
     $url = '';
     try {
         $url = $this->router->generate($routeName, $this->generateParameters($routeType, $request), true);
     } catch (\Exception $e) {
         $addHeader = false;
     }
     if ($addHeader) {
         // append rel=self link to link headers
         $linkHeader->add(new LinkHeaderItem($url, array('rel' => 'self')));
         // overwrite link headers with new headers
         $response->headers->set('Link', (string) $linkHeader);
     }
 }
 /**
  * @param FilterResponseEvent $event
  */
 public function onKernelResponse(FilterResponseEvent $event)
 {
     if (!$this->wrapApiResponse) {
         return;
     }
     if (!$event->isMasterRequest()) {
         return;
     }
     $request = $event->getRequest();
     // Only send back HTML if the requestor allows it
     if (!$request->headers->has('Accept') || false === strpos($request->headers->get('Accept'), 'text/html')) {
         return;
     }
     $response = $event->getResponse();
     switch ($request->getRequestFormat()) {
         case 'json':
             $prettyprint_lang = 'js';
             $content = json_encode(json_decode($response->getContent()), JSON_PRETTY_PRINT);
             break;
         case 'xml':
             $prettyprint_lang = 'xml';
             $content = $response->getContent();
             break;
         default:
             return;
     }
     $response->setContent('<html><body>' . '<pre class="prettyprint lang-' . $prettyprint_lang . '">' . htmlspecialchars($content) . '</pre>' . '<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js"></script>' . '</body></html>');
     // Set the request type to HTML
     $response->headers->set('Content-Type', 'text/html; charset=UTF-8');
     $request->setRequestFormat('html');
     // Overwrite the original response
     $event->setResponse($response);
 }
Esempio n. 5
0
 public function onLateKernelResponse(FilterResponseEvent $event)
 {
     if (!$event->isMasterRequest()) {
         return;
     }
     $this->sub->inRequest = false;
 }
 /**
  * Redirects login attempts on already-logged-in session to the destination.
  */
 public function onRespond(FilterResponseEvent $event)
 {
     // Return early in most cases.
     if ($event->getRequest()->getMethod() !== 'POST') {
         return;
     }
     if (!$this->currentUser->isAuthenticated()) {
         return;
     }
     if (!$event->isMasterRequest()) {
         return;
     }
     if (!$event->getRequest()->query->has('destination')) {
         return;
     }
     if ($event->getResponse() instanceof RedirectResponse) {
         return;
     }
     // There has to be a better way to figure out if we landed on the 403/404 page.
     $page_403 = $this->configFactory->get('system.site')->get('page.403');
     $page_404 = $this->configFactory->get('system.site')->get('page.404');
     $path = $this->currentPath->getPath();
     $route = $this->currentRouteMatch->getRouteName();
     if ($route == 'system.403' || $page_403 && $path == $page_403 || $route == 'system.404' || $page_404 && $path == $page_404) {
         // RedirectResponseSubscriber will convert to absolute URL for us.
         $event->setResponse(new RedirectResponse($this->redirectDestination->get(), RedirectResponse::HTTP_SEE_OTHER));
     }
 }
 public function onKernelResponse(FilterResponseEvent $event)
 {
     if (!$event->isMasterRequest()) {
         return;
     }
     $this->affiliate->record($event->getResponse());
 }
 /**
  * @param FilterResponseEvent $event
  */
 public function onKernelResponse(FilterResponseEvent $event)
 {
     if (!$event->isMasterRequest()) {
         return;
     }
     try {
         if (!$this->authorizationChecker->isGranted('ROLE_ADMIN')) {
             return;
         }
     } catch (AuthenticationCredentialsNotFoundException $e) {
         return;
     }
     $request = $event->getRequest();
     if ($request->isXmlHttpRequest()) {
         return;
     }
     $response = $event->getResponse();
     if ($response->isRedirection() || false === strpos($response->headers->get('Content-Type', ''), 'text/html')) {
         return;
     }
     $html = $this->editor->renderEditor($response);
     if (!empty($html)) {
         $this->injectEditor($response, $html);
     }
 }
 /**
  * Adds a cache tag if the 'user.permissions' cache context is present.
  *
  * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
  *   The event to process.
  */
 public function onRespond(FilterResponseEvent $event)
 {
     if (!$event->isMasterRequest()) {
         return;
     }
     if (!$this->currentUser->isAnonymous()) {
         return;
     }
     $response = $event->getResponse();
     if (!$response instanceof CacheableResponseInterface) {
         return;
     }
     // The 'user.permissions' cache context ensures that if the permissions for
     // a role are modified, users are not served stale render cache content.
     // But, when entire responses are cached in reverse proxies, the value for
     // the cache context is never calculated, causing the stale response to not
     // be invalidated. Therefore, when varying by permissions and the current
     // user is the anonymous user, also add the cache tag for the 'anonymous'
     // role.
     if (in_array('user.permissions', $response->getCacheableMetadata()->getCacheContexts())) {
         $per_permissions_response_for_anon = new CacheableMetadata();
         $per_permissions_response_for_anon->setCacheTags(['config:user.role.anonymous']);
         $response->addCacheableDependency($per_permissions_response_for_anon);
     }
 }
Esempio n. 10
0
 /**
  * Logs master response on event KernelEvents::RESPONSE.
  *
  * @param FilterResponseEvent $event
  */
 public function onKernelResponse(FilterResponseEvent $event)
 {
     if (!$event->isMasterRequest()) {
         return;
     }
     $this->logResponse($event->getResponse());
 }
    public function onKernelResponse(FilterResponseEvent $event)
    {
        if ($this->url || !$event->isMasterRequest() || !\sfContext::hasInstance()) {
            return;
        }
        $response_headers = $event->getResponse()->headers;
        if ($response_headers->has('x-debug-token-link') && strpos(\sfContext::getInstance()->getResponse()->getContentType(), 'javascript') === false && !$event->getRequest()->isXmlHttpRequest()) {
            $this->url = $response_headers->get('x-debug-token-link');
            $link = json_encode($response_headers->get('x-debug-token-link'));
            echo <<<JAVASCRIPT
<script>
(function() {
  var bar_node = document.getElementById('sfWebDebugDetails'), link_node, li_node;
  if(bar_node) { // We have a debug bar
    link_node = document.createElement('a');
    link_node.href = {$link};
    link_node.appendChild(document.createTextNode('Symfony 2'));
    li_node = document.createElement('li');
    li_node.appendChild(link_node);
    bar_node.insertBefore(li_node,bar_node.firstChild);
  }
}())
</script>
JAVASCRIPT;
        }
    }
Esempio n. 12
0
 public function processCount(FilterResponseEvent $event)
 {
     if (!$event->isMasterRequest()) {
         return;
     }
     $connecte = new Connecte();
     $ip = $this->container->get('request')->getClientIp();
     $times = time();
     $doctrine = $this->container->get('doctrine.orm.entity_manager');
     $listIp = $this->doctrine->getRepository('SNSysNewsBundle:Connecte')->findAll();
     if (!in_array($ip, $listIp)) {
         $connecte->setIp($ip);
         $connecte->setTimes($times);
         $doctrine->persist($connecte);
         $doctrine->flush();
     } else {
         $times = time();
         $connecte->setTimes($times);
         $doctrine->persist($connecte);
         $doctrine->flush();
     }
     $time_5 = time() - 60 * 5;
     if ($times < $time_5) {
         $connecte = $this->doctrine->getRepository('SNSysNewsBundle:Connecte')->findOneByTimes($times);
         $doctrine->remove($connecte);
         $doctrine->flush();
     }
     $nbvisit = $doctrine->getRepository('SNSysNewsBundle:Connecte')->getcountnumbervisit();
     $rep = $this->counthtml->viewNbVisit($event->getResponse(), $nbvisit);
     $event->setResponse($rep);
 }
Esempio n. 13
0
 /**
  *
  * @param  FilterResponseEvent $event
  * @return void
  */
 public function onKernelResponse(FilterResponseEvent $event)
 {
     if (!$event->isMasterRequest()) {
         return;
     }
     $request = $event->getRequest();
     $response = $event->getResponse();
     if (!$this->isCorsRequest($request)) {
         return;
     }
     if ($this->isPreflightRequest($request)) {
         if (!empty($this->app['cors.options']['allow_methods'])) {
             $allowedMethods = $this->app['cors.options']['allow_methods'];
         } else {
             $allowedMethods = $this->app['cors.allowed_methods'];
         }
         if (!in_array($request->headers->get('Access-Control-Request-Method'), $allowedMethods)) {
             return;
         }
         $response->headers->set('Access-Control-Allow-Headers', $request->headers->get('Access-Control-Request-Headers'));
         $response->headers->set('Access-Control-Allow-Methods', $allowedMethods);
         if (!empty($this->app['cors.options']['max_age'])) {
             $response->headers->set('Access-Control-Max-Age', $this->app['cors.options']['max_age']);
         }
     } elseif (!empty($this->app['cors.options']['expose_headers'])) {
         $response->headers->set('Access-Control-Expose-Headers', $this->app['cors.options']['expose_headers']);
     }
     $response->headers->set('Access-Control-Allow-Origin', '*');
     if ($this->app['cors.options']['allow_credentials']) {
         $response->headers->set('Access-Control-Allow-Credentials', 'true');
     }
 }
Esempio n. 14
0
 /**
  * Trigger event for RESPONSE Kervenel vent
  *
  * @param FilterResponseEvent $event event
  */
 public function onResponse(FilterResponseEvent $event)
 {
     $response = $event->getResponse();
     $request = $event->getRequest();
     if (!$event->isMasterRequest()) {
         return;
     }
     if ($request->isXmlHttpRequest()) {
         return;
     }
     if ($response->isRedirection() || $response->headers->has('Content-Type') && false === strpos($response->headers->get('Content-Type'), 'html') || 'html' !== $request->getRequestFormat()) {
         return;
     }
     $http = $request->isSecure() ? 'https' : 'http';
     if (empty($this->extensions)) {
         return;
     }
     if (count($this->extensions) == 0) {
         return;
     }
     $extensions = implode('|', $this->extensions);
     $content = $response->getContent();
     $pattern = '/="[^"]*\\/(css|js|var|bundles)\\/(.*)\\.(' . $extensions . ')([^"]*)"/i';
     $replace = '="' . $http . '://' . $this->domain . '/${1}/${2}.${3}${4}"';
     $content = preg_replace($pattern, $replace, $content);
     $response->setContent($content);
 }
Esempio n. 15
0
 /**
  * Filters the Response.
  *
  * @param FilterResponseEvent $event A FilterResponseEvent instance
  */
 public function onKernelResponse(FilterResponseEvent $event)
 {
     if (!$event->isMasterRequest() || null === $this->surrogate) {
         return;
     }
     $this->surrogate->addSurrogateControl($event->getResponse());
 }
 /**
  * Sends the Hydra header on each response.
  *
  * @param FilterResponseEvent $event
  */
 public function onKernelResponse(FilterResponseEvent $event)
 {
     if (!$event->isMasterRequest()) {
         return;
     }
     $event->getResponse()->headers->set('Link', sprintf('<%s>; rel="%sapiDocumentation"', $this->router->generate('api_hydra_vocab', [], UrlGeneratorInterface::ABSOLUTE_URL), ContextBuilder::HYDRA_NS));
 }
Esempio n. 17
0
    /**
     * Handles the onKernelResponse event.
     *
     * @param FilterResponseEvent $event A FilterResponseEvent instance
     */
    public function onKernelResponse(FilterResponseEvent $event)
    {
        $master = $event->isMasterRequest();
        if ($this->onlyMasterRequests && !$master) {
            return;
        }

        if ($this->onlyException && null === $this->exception) {
            return;
        }

        $request = $event->getRequest();
        $exception = $this->exception;
        $this->exception = null;

        if (null !== $this->matcher && !$this->matcher->matches($request)) {
            return;
        }

        if (!$profile = $this->profiler->collect($request, $event->getResponse(), $exception)) {
            return;
        }

        $this->profiles[$request] = $profile;

        if (null !== $this->requestStack) {
            $this->parents[$request] = $this->requestStack->getParentRequest();
        } elseif (!$master) {
            // to be removed when requestStack is required
            array_pop($this->requests);

            $this->parents[$request] = end($this->requests);
        }
    }
 public function onKernelResponse(FilterResponseEvent $event)
 {
     $response = $event->getResponse();
     $request = $event->getRequest();
     if ($response->headers->has('X-Debug-Token') && null !== $this->urlGenerator) {
         try {
             $response->headers->set('X-Debug-Token-Link', $this->urlGenerator->generate('_profiler', array('token' => $response->headers->get('X-Debug-Token')), UrlGeneratorInterface::ABSOLUTE_URL));
         } catch (\Exception $e) {
             $response->headers->set('X-Debug-Error', get_class($e) . ': ' . $e->getMessage());
         }
     }
     if (!$event->isMasterRequest()) {
         return;
     }
     // do not capture redirects or modify XML HTTP Requests
     if ($request->isXmlHttpRequest()) {
         return;
     }
     if ($response->headers->has('X-Debug-Token') && $response->isRedirect() && $this->interceptRedirects) {
         $session = $request->getSession();
         if (null !== $session && $session->isStarted() && $session->getFlashBag() instanceof AutoExpireFlashBag) {
             // keep current flashes for one more request if using AutoExpireFlashBag
             $session->getFlashBag()->setAll($session->getFlashBag()->peekAll());
         }
         $response->setContent($this->twig->render('@WebProfiler/Profiler/toolbar_redirect.html.twig', array('location' => $response->headers->get('Location'))));
         $response->setStatusCode(200);
         $response->headers->remove('Location');
     }
     if (self::DISABLED === $this->mode || !$response->headers->has('X-Debug-Token') || $response->isRedirection() || $response->headers->has('Content-Type') && false === strpos($response->headers->get('Content-Type'), 'html') || 'html' !== $request->getRequestFormat()) {
         return;
     }
     $this->injectToolbar($response, $request);
 }
 public function onKernelResponse(FilterResponseEvent $event)
 {
     $ajaxEngineConfig = $this->container->getParameter('ajax_engine');
     if (isset($ajaxEngineConfig['flash_messages']['type']) && $ajaxEngineConfig['flash_messages']['type'] == 'header' && $event->isMasterRequest()) {
         $flashMessages = $event->getRequest()->getSession()->getFlashBag()->all();
         $event->getResponse()->headers->set('Flash-Messages', json_encode($flashMessages));
     }
 }
Esempio n. 20
0
 /**
  * If the structure has been picked up from the request, updated
  * the response using the configured handler.
  *
  * @param FilterResponseEvent
  */
 public function onResponse(FilterResponseEvent $event)
 {
     $request = $event->getRequest();
     if (!$event->isMasterRequest() || !$this->handler instanceof HandlerUpdateResponseInterface || !$request->isMethodSafe() || false === $request->attributes->has('structure') || true === $request->query->has('preview')) {
         return;
     }
     $this->handler->updateResponse($event->getResponse(), $request->attributes->get('structure'));
 }
 public function handleCookieConsent(FilterResponseEvent $event)
 {
     if (!$event->isMasterRequest() || $event->getRequest()->cookies->has($this->cookieName)) {
         return;
     }
     $response = $event->getResponse();
     $response->setContent($response->getContent() . $this->templating->render($this->cookieTemplate));
 }
Esempio n. 22
0
 /**
  * @param FilterResponseEvent $event
  */
 public function onKernelResponse(FilterResponseEvent $event)
 {
     if (!$event->isMasterRequest()) {
         return;
     }
     $route = $this->getRoute($event->getRequest());
     $this->logger->info('LOGIC END', array($route));
 }
Esempio n. 23
0
 /**
  * Add the session cookie to the response if it is started.
  *
  * @param FilterResponseEvent $event
  */
 public function onResponse(FilterResponseEvent $event)
 {
     if (!$event->isMasterRequest() || !$this->session->isStarted()) {
         return;
     }
     $this->session->save();
     $cookie = $this->generateCookie();
     $event->getResponse()->headers->setCookie($cookie);
 }
 /**
  * Sets security headers.
  *
  * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
  *
  * @return void
  */
 public function onKernelResponse(FilterResponseEvent $event)
 {
     if (!$event->isMasterRequest() || !$this->getIsHstsEnabled()) {
         return;
     }
     $headerBody = $this->renderHeaderBody($this->getHstsConfig());
     if ($headerBody !== '') {
         $event->getResponse()->headers->set(static::HEADER_HSTS, $headerBody);
     }
 }
 /**
  * Adds a X-Version header to the response.
  *
  * @param FilterResponseEvent $event Current emitted event.
  *
  * @return void
  */
 public function onKernelResponse(FilterResponseEvent $event)
 {
     if (!$event->isMasterRequest()) {
         // don't do anything if it's not the master request
         return;
     }
     /** @var \Symfony\Component\HttpFoundation\Response $response */
     $response = $event->getResponse();
     $response->headers->set('X-Version', $this->coreUtils->getVersion());
 }
 /**
  * Sets extra X-Generator header on successful responses.
  *
  * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
  *   The event to process.
  */
 public function onRespond(FilterResponseEvent $event)
 {
     if (!$event->isMasterRequest()) {
         return;
     }
     $response = $event->getResponse();
     // Set the generator in the HTTP header.
     list($version) = explode('.', \Drupal::VERSION, 2);
     $response->headers->set('X-Generator', 'Drupal ' . $version . ' (https://www.drupal.org)');
 }
 /**
  * Sets security headers.
  *
  * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
  *
  * @return void
  */
 public function onKernelResponse(FilterResponseEvent $event)
 {
     if (!$event->isMasterRequest()) {
         return;
     }
     // Can be changed to SAMEORIGIN to allow framing on the same domain.
     $event->getResponse()->headers->set(static::HEADER_X_FRAME_OPTIONS, 'DENY');
     $event->getResponse()->headers->set(static::HEADER_X_CONTENT_TYPE_OPTIONS, 'nosniff');
     $event->getResponse()->headers->set(static::HEADER_X_XSS_PROTECTION, '1; mode=block');
 }
 /**
  * Filters the Response.
  *
  * @param FilterResponseEvent $event A FilterResponseEvent instance
  */
 public function onKernelResponse(FilterResponseEvent $event)
 {
     if (!$event->isMasterRequest()) {
         return;
     }
     $response = $event->getResponse();
     if ($response instanceof StreamedResponse) {
         $response->send();
     }
 }
Esempio n. 29
0
 public function onKernelResponse(FilterResponseEvent $event)
 {
     if (!$event->isMasterRequest()) {
         return;
     }
     $session = $event->getRequest()->getSession();
     if ($session && $session->isStarted()) {
         $session->save();
     }
 }
Esempio n. 30
0
 /**
  * Attach session's flash bag to flash logger if it is started
  *
  * @param GetResponseEvent|FilterResponseEvent $event
  */
 public function onEvent($event)
 {
     if (!$event->isMasterRequest()) {
         return;
     }
     $session = $event->getRequest()->getSession();
     if ($session && $session->isStarted()) {
         $this->attachFlashBag($session);
     }
 }