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

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