vendor/contao/core-bundle/src/Resources/contao/library/Contao/RequestToken.php line 15

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Contao.
  4.  *
  5.  * (c) Leo Feyer
  6.  *
  7.  * @license LGPL-3.0-or-later
  8.  */
  9. namespace Contao;
  10. use Symfony\Component\Security\Csrf\CsrfToken;
  11. @trigger_error('Using the "Contao\RequestToken" class has been deprecated and will no longer work in Contao 5.0. Use the Symfony CSRF service via the container instead.'E_USER_DEPRECATED);
  12. /**
  13.  * Generates and validates request tokens
  14.  *
  15.  * The class tries to read and validate the request token from the user session
  16.  * and creates a new token if there is none.
  17.  *
  18.  * Usage:
  19.  *
  20.  *     echo RequestToken::get();
  21.  *
  22.  *     if (!RequestToken::validate('TOKEN'))
  23.  *     {
  24.  *         throw new Exception("Invalid request token");
  25.  *     }
  26.  *
  27.  * @author Leo Feyer <https://github.com/leofeyer>
  28.  *
  29.  * @deprecated Deprecated since Contao 4.0, to be removed in Contao 5.0.
  30.  *             Use the Symfony CSRF service via the container instead.
  31.  */
  32. class RequestToken
  33. {
  34.     /**
  35.      * Read the token from the session or generate a new one
  36.      */
  37.     public static function initialize()
  38.     {
  39.         // ignore
  40.     }
  41.     /**
  42.      * Return the token
  43.      *
  44.      * @return string The request token
  45.      */
  46.     public static function get()
  47.     {
  48.         $container System::getContainer();
  49.         return $container->get('contao.csrf.token_manager')->getToken($container->getParameter('contao.csrf_token_name'))->getValue();
  50.     }
  51.     /**
  52.      * Validate a token
  53.      *
  54.      * @param string $strToken The request token
  55.      *
  56.      * @return boolean True if the token matches the stored one
  57.      */
  58.     public static function validate($strToken)
  59.     {
  60.         // The feature has been disabled
  61.         if (\defined('BYPASS_TOKEN_CHECK') || Config::get('disableRefererCheck'))
  62.         {
  63.             return true;
  64.         }
  65.         // Check against the whitelist (thanks to Tristan Lins) (see #3164)
  66.         if (Config::get('requestTokenWhitelist'))
  67.         {
  68.             $strHostname gethostbyaddr($_SERVER['REMOTE_ADDR']);
  69.             foreach (Config::get('requestTokenWhitelist') as $strDomain)
  70.             {
  71.                 if ($strDomain == $strHostname || preg_match('/\.' preg_quote($strDomain'/') . '$/'$strHostname))
  72.                 {
  73.                     return true;
  74.                 }
  75.             }
  76.         }
  77.         $container System::getContainer();
  78.         return $container->get('contao.csrf.token_manager')->isTokenValid(new CsrfToken($container->getParameter('contao.csrf_token_name'), $strToken));
  79.     }
  80. }
  81. class_alias(RequestToken::class, 'RequestToken');