vendor/bugbuster/contao-botdetection-bundle/src/Resources/contao/modules/ModuleBotDetection.php line 123

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of a BugBuster Contao Bundle (Resources\contao)
  5.  *
  6.  * @copyright  Glen Langer 2020 <http://contao.ninja>
  7.  * @author     Glen Langer (BugBuster)
  8.  * @package    Contao BotDetection Bundle
  9.  * @license    LGPL-3.0-or-later
  10.  * @see        https://github.com/BugBuster1701/contao-botdetection-bundle
  11.  */
  12. /**
  13.  * Run in a custom namespace
  14.  */
  15. namespace BugBuster\BotDetection;
  16. use BugBuster\BotDetection\Referrer\ProviderCommunication;
  17. use BugBuster\BotDetection\Referrer\ProviderParser;
  18. use Contao\System;
  19. /**
  20.  * Class ModuleBotDetection
  21.  *
  22.  * @copyright  Glen Langer 2007..2020 <http://contao.ninja>
  23.  * @author     Glen Langer (BugBuster)
  24.  */
  25. class ModuleBotDetection extends System
  26. {
  27.     /**
  28.      * Current version of the class.
  29.      */
  30.     const BOTDETECTION_VERSION  '1.8.0';
  31.     const BOT_REFERRER_LIST     "/vendor/bugbuster/contao-botdetection-bundle/src/Resources/contao/config/bot-referrer-list.php";
  32.     const BOT_REFERRER_PROVIDER "/vendor/bugbuster/contao-botdetection-bundle/src/Resources/contao/config/referrer-provider.php";
  33.     const BOT_IP4_LIST          "/vendor/bugbuster/contao-botdetection-bundle/src/Resources/contao/config/bot-ip-list-ipv4.txt";
  34.     const BOT_IP6_LIST          "/vendor/bugbuster/contao-botdetection-bundle/src/Resources/contao/config/bot-ip-list-ipv6.txt";
  35.     /**
  36.      * TL_ROOT over Container
  37.      *
  38.      * @var string
  39.      */
  40.     protected $rootDir;
  41.     /**
  42.      * Initialize object
  43.      */
  44.     public function __construct($rootDir null)
  45.     {
  46.         parent::__construct();
  47.         if (null === $rootDir) {
  48.             $this->rootDir System::getContainer()->getParameter('kernel.project_dir');
  49.         }
  50.         else {
  51.             $this->rootDir $rootDir;
  52.         }
  53.         $this->prefillCache();
  54.         $this->deleteOldCache();
  55.     }
  56.     /**
  57.      * Returns the version number
  58.      *
  59.      * @return string
  60.      */
  61.     public function getVersion(): string
  62.     {
  63.         return self::BOTDETECTION_VERSION;
  64.     }
  65.     /**
  66.      * Spider Bot Agent/Advanced/Referrer/IP Check
  67.      *
  68.      * @param string   UserAgent, optional for tests
  69.      * @return boolean true when bot found
  70.      */
  71.     public function checkBotAllTests($UserAgent false): bool
  72.     {
  73.         $objUserAgent = new UserAgent($UserAgent);
  74.         if (false === $UserAgent
  75.         {            
  76.             $UserAgent $objUserAgent->getUserAgent();
  77.         }
  78.         else
  79.         {
  80.             $UserAgent $objUserAgent->setUserAgent($UserAgent);
  81.         }
  82.         if (CheckBotAgentSimple::checkAgent($UserAgent) === true//(BotsRough, BotsFine)
  83.         {
  84.             return true;
  85.         }
  86.         if (true === (bool) CheckBotReferrer::checkReferrer(false
  87.                                                              $this->rootDir self::BOT_REFERRER_LIST,
  88.                                                              $this->rootDir self::BOT_REFERRER_PROVIDER
  89.            )
  90.         {
  91.             return true;
  92.         }
  93.         if (false === $this->checkGetPostRequest()) // #153
  94.         {
  95.             return true;
  96.         }
  97.         CheckBotIp::setBotIpv4List($this->rootDir self::BOT_IP4_LIST);
  98.         CheckBotIp::setBotIpv6List($this->rootDir self::BOT_IP6_LIST);
  99.         if (true === CheckBotIp::checkIP())
  100.         {
  101.             return true;
  102.         }
  103.         //CheckBotAgentExtended (Browscap + eigene Liste)
  104.         return CheckBotAgentExtended::checkAgent($UserAgent);
  105.     }
  106.     /**
  107.      * Check if Request a GET/POST Request
  108.      * 
  109.      * @return boolean true when GET/POST
  110.      */
  111.     public function checkGetPostRequest(): bool
  112.     {
  113.         $RequestMethod \Contao\Environment::get('requestMethod');
  114.         if ($RequestMethod == 'GET' || $RequestMethod == 'POST'
  115.         {
  116.             return true;
  117.         }
  118.         return false;
  119.     }
  120.     private function prefillCache()
  121.     {
  122.         if (!is_dir($this->rootDir '/vendor/bugbuster/contao-botdetection-bundle/src/Resources/contao/config'))
  123.         {
  124.             return; // call from IDE for tests
  125.         }
  126.         $referrerProvider = array();
  127.         include_once($this->rootDir '/vendor/bugbuster/contao-botdetection-bundle/src/Resources/contao/config/referrer-provider.php');
  128.         $proCom = new ProviderCommunication($referrerProviderfalse$this->rootDir);
  129.         $cachePath false;
  130.         if (true === $proCom->loadProviderFiles())
  131.         {
  132.             $cachePath $proCom->getCachePath();
  133.         }
  134.         if (false !== $cachePath)
  135.         {
  136.             $proPar = new ProviderParser($referrerProvider$cachePath);
  137.             if (true === $proPar->isUpdateProviderListNecessary() &&
  138.                 true === $proPar->generateProviderList()
  139.                 )
  140.             {
  141.                 $proPar->cleanProviderList();
  142.                 $proPar->writeProviderList();
  143.             }
  144.         }
  145.     }
  146.     private function deleteOldCache()
  147.     {
  148.         if (!is_dir($this->rootDir '/vendor/bugbuster/contao-botdetection-bundle/src/Resources/contao/config'))
  149.         {
  150.             return; // call from IDE for tests
  151.         }
  152.         //Only once a month.
  153.         //runonce replacement, until I know it better. (The package has modified files...., abort...)
  154.         $day = (int) date('j');
  155.         if (== $day)
  156.         {
  157.             $olddirs = array('largebrowscap_v6006_1.0.4'
  158.                              'largebrowscap_v6008_1.0.4'
  159.                              'largebrowscap_v6015_1.0.4'
  160.                              'largebrowscap_v6021_1.0.5'
  161.                              'largebrowscap_v6026_1.0.5'
  162.                              'largebrowscap_v6030_1.0.5',
  163.                              'largebrowscap_v6036_1.0.5',
  164.                              'largebrowscap_v6040_1.0.5',
  165.                              'largebrowscap_v6040_1.1.0'
  166.                         );
  167.             foreach ($olddirs as $olddir)
  168.             {
  169.                 if (is_dir($this->rootDir '/vendor/bugbuster/contao-botdetection-bundle/src/Resources/contao/cache/'.$olddir))
  170.                 {
  171.                     $folder = new \Contao\Folder('vendor/bugbuster/contao-botdetection-bundle/src/Resources/contao/cache/'.$olddir);
  172.                     if (!$folder->isEmpty())
  173.                     {
  174.                         $folder->purge();
  175.                     }
  176.                     $folder->delete();
  177.                     $folder=null;
  178.                     unset($folder);
  179.                 }
  180.             }
  181.         }
  182.     }
  183. }