vendor/bugbuster/contao-banner-bundle/src/Resources/contao/classes/BannerCount.php line 58

Open in your IDE?
  1. <?php
  2. /**
  3.  * Extension for Contao Open Source CMS, Copyright (C) 2005-2022 Leo Feyer
  4.  *
  5.  * Class BannerCount - Frontend
  6.  *
  7.  * @copyright  Glen Langer 2022 <http://contao.ninja>
  8.  * @author     Glen Langer (BugBuster)
  9.  * @licence    LGPL
  10.  * @filesource
  11.  * @see           https://github.com/BugBuster1701/contao-banner-bundle
  12.  */
  13. /**
  14.  * Run in a custom namespace, so the class can be replaced
  15.  */
  16. namespace BugBuster\Banner;
  17. use BugBuster\Banner\BannerLog;
  18. use BugBuster\Banner\BannerLogic;
  19. use BugBuster\BotDetection\ModuleBotDetection;
  20. /**
  21.  * Class BannerCount
  22.  *
  23.  * @copyright  Glen Langer 2022 <http://contao.ninja>
  24.  * @author     Glen Langer (BugBuster)
  25.  * @license    LGPL
  26.  */
  27. class BannerCount extends \System
  28. {
  29.     /**
  30.      * Banner Data, for BannerStatViewUpdate
  31.      */
  32.     protected $arrBannerData = [];
  33.     protected $banner_useragent '';
  34.     protected $module_id 0;
  35.     /**
  36.      * public constructor for phpunit
  37.      */
  38.     public function __construct($arrBannerData$banner_useragent$module_id)
  39.     {
  40.         $this->arrBannerData    $arrBannerData;
  41.         $this->banner_useragent $banner_useragent;
  42.         $this->module_id        $module_id;
  43.         parent::__construct();
  44.     }
  45.     /**
  46.      * Insert/Update Banner View Stat
  47.      */
  48.     public function setStatViewUpdate()
  49.     {
  50.         if ($this->bannerCheckBot() === true) {
  51.             BannerLog::writeLog(__METHOD____LINE__'Bot gefunden, wird nicht gezaehlt');
  52.             return; //Bot gefunden, wird nicht gezaehlt
  53.         }
  54.         if ($this->checkUserAgent() === true) {
  55.             BannerLog::writeLog(__METHOD____LINE__'User Agent Filterung, wird nicht gezaehlt');
  56.             return; //User Agent Filterung
  57.         }
  58.         $BannerChecks = new BannerChecks();
  59.         if ($BannerChecks->checkBE() === true) {
  60.             BannerLog::writeLog(__METHOD____LINE__'Backend Login, wird nicht gezaehlt');
  61.             return; //Backend Login
  62.         }
  63.         $BannerChecks null;
  64.         unset($BannerChecks);
  65.         // Blocker
  66.         $lastBanner array_pop($this->arrBannerData);
  67.         $BannerID $lastBanner['banner_id'];
  68.         if ($BannerID == 0) { // kein Banner, nichts zu tun
  69.             BannerLog::writeLog(__METHOD____LINE__'kein Banner, nichts zu tun');
  70.             return;
  71.         }
  72.         if ($this->getStatViewUpdateBlockerId($BannerID$this->module_id) === true) {
  73.             // Eintrag innerhalb der Blockzeit
  74.             BannerLog::writeLog(__METHOD____LINE__'Eintrag innerhalb der Blockzeit, wird nicht gezaehlt');
  75.             return; // blocken, nicht zählen, raus hier
  76.         } else {
  77.             // nichts geblockt, also blocken für den nächsten Aufruf
  78.             BannerLog::writeLog(__METHOD____LINE__'blocken fuer den naechsten Aufruf');
  79.             $this->setStatViewUpdateBlockerId($BannerID$this->module_id);
  80.         }
  81.         //Zählung, Insert
  82.         $arrSet =
  83.         [
  84.                 'id' => $BannerID,
  85.                 'tstamp' => time(),
  86.                 'banner_views' => 1
  87.         ];
  88.         $objInsert \Database::getInstance()->prepare("INSERT IGNORE INTO tl_banner_stat %s")
  89.                                             ->set($arrSet)
  90.                                             ->execute();
  91.         if ($objInsert->insertId == 0) {
  92.             //Zählung, Update
  93.             \Database::getInstance()->prepare("UPDATE
  94.                                                 `tl_banner_stat`
  95.                                                 SET
  96.                                                 `tstamp`=?
  97.                                                 , `banner_views` = `banner_views`+1
  98.                                                 WHERE
  99.                                                 `id`=?")
  100.                                     ->execute(time(), $BannerID);
  101.         }
  102.         BannerLog::writeLog(__METHOD____LINE__'Zaehlung erfolgt fuer Banner ID: '.$BannerID);
  103.     }//BannerStatViewUpdate()
  104.     /**
  105.      * StatViewUpdate Blocker, Set Banner ID and timestamp
  106.      *
  107.      * @param integer $banner_id
  108.      */
  109.     protected function setStatViewUpdateBlockerId($banner_id=0)
  110.     {
  111.         if ($banner_id==0) {
  112.             return;
  113.         }// keine Banner ID, nichts zu tun
  114.         //das können mehrere sein!, mergen!
  115.         $objBannerLogic = new BannerLogic();
  116.         $objBannerLogic->setSession('StatViewUpdateBlocker'.$this->module_id, [$banner_id => time()], true);
  117.         return;
  118.     }
  119.     /**
  120.      * StatViewUpdate Blocker, Get Banner ID if the timestamp ....
  121.      *
  122.      * @param boolean    true if blocked | false
  123.      */
  124.     protected function getStatViewUpdateBlockerId($banner_id=0)
  125.     {
  126.         $objBannerLogic = new BannerLogic();
  127.         $session $objBannerLogic->getSession('StatViewUpdateBlocker'.$this->module_id);
  128.         if (\count($session)) {
  129.             reset($session);
  130.             foreach ($session as $key => $val) {
  131.                 if ($key == $banner_id &&
  132.                     true === $this->removeStatViewUpdateBlockerId($key$val$session)
  133.                 ) {
  134.                     // Key ist noch gültig und es muss daher geblockt werden
  135.                     //DEBUG log_message('getStatViewUpdateBlockerId Banner ID:'.$key,'Banner.log');
  136.                     return true;
  137.                 }
  138.             }
  139.         }
  140.         return false;
  141.     }
  142.     /**
  143.      * StatViewUpdate Blocker, Remove old Banner ID
  144.      *
  145.      * @param  integer $banner_id
  146.      * @return boolean true = Key is valid, it must be blocked | false = key is invalid
  147.      */
  148.     protected function removeStatViewUpdateBlockerId($banner_id$tstmap$session)
  149.     {
  150.         $BannerBlockTime time() - 60*5;  // 5 Minuten, 0-5 min wird geblockt
  151.         if (isset($GLOBALS['TL_CONFIG']['mod_banner_block_time'])
  152.          && (int) ($GLOBALS['TL_CONFIG']['mod_banner_block_time'])>0
  153.         ) {
  154.             $BannerBlockTime time() - 60*1*(int) ($GLOBALS['TL_CONFIG']['mod_banner_block_time']);
  155.         }
  156.         if ($tstmap >  $BannerBlockTime) {
  157.             return true;
  158.         } else {
  159.             //wenn mehrere dann nur den Teil, nicht die ganze Session
  160.             unset($session[$banner_id]);
  161.             //wenn Anzahl Banner in Session nun 0 dann Session loeschen
  162.             if (\count($session) == 0) {
  163.                 //komplett löschen
  164.                 \Session::getInstance()->remove('StatViewUpdateBlocker'.$this->module_id);
  165.             } else { //sonst neu setzen
  166.                 //gekuerzte Session neu setzen
  167.                 $objBannerLogic = new BannerLogic();
  168.                 $objBannerLogic->setSession('StatViewUpdateBlocker'.$this->module_id$sessionfalse);
  169.                 $objBannerLogic null;
  170.                 unset($objBannerLogic);
  171.             }
  172.         }
  173.         return false;
  174.     }
  175.     /**
  176.      * Spider Bot Check
  177.      */
  178.     protected function bannerCheckBot()
  179.     {
  180.         if (isset($GLOBALS['TL_CONFIG']['mod_banner_bot_check'])
  181.           && (int) $GLOBALS['TL_CONFIG']['mod_banner_bot_check'] == 0
  182.         ) {
  183.             //DEBUG log_message('bannerCheckBot abgeschaltet','Banner.log');
  184.             return false//Bot Suche abgeschaltet ueber localconfig.php
  185.         }
  186.         $bundles array_keys(\System::getContainer()->getParameter('kernel.bundles')); // old \ModuleLoader::getActive()
  187.         if (!\in_array('BugBusterBotdetectionBundle'$bundles)) {
  188.             //botdetection Modul fehlt, Abbruch
  189.             BannerLog::log('contao-botdetection-bundle extension required for extension: contao-banner-bundle!''BannerCount::bannerCheckBot'TL_ERROR);
  190.             return false;
  191.         }
  192.         // Import Helperclass ModuleBotDetection
  193.         $ModuleBotDetection = new ModuleBotDetection();
  194.         if ($ModuleBotDetection->checkBotAllTests()) {
  195.             //DEBUG log_message('bannerCheckBot True','Banner.log');
  196.             return true;
  197.         }
  198.         //DEBUG log_message('bannerCheckBot False','Banner.log');
  199.         return false;
  200.     } //bannerCheckBot
  201.     /**
  202.      * HTTP_USER_AGENT Special Check
  203.      */
  204.     protected function checkUserAgent()
  205.     {
  206.         if (\Environment::get('httpUserAgent')) {
  207.             $UserAgent trim(\Environment::get('httpUserAgent'));
  208.         } else {
  209.             return false// Ohne Absender keine Suche
  210.         }
  211.         $arrUserAgents explode(","$this->banner_useragent);
  212.         if (\strlen(trim($arrUserAgents[0])) == 0) {
  213.             return false// keine Angaben im Modul
  214.         }
  215.         array_walk($arrUserAgents, ['self''trimBannerArrayValue']);  // trim der array values
  216.         // grobe Suche
  217.         $CheckUserAgent str_replace($arrUserAgents'#'$UserAgent);
  218.         if ($UserAgent != $CheckUserAgent) {   // es wurde ersetzt also was gefunden
  219.             //DEBUG log_message('CheckUserAgent Filterung; Treffer!','Banner.log');
  220.             return true;
  221.         }
  222.         return false;
  223.     } //checkUserAgent
  224.     public static function trimBannerArrayValue(&$data)
  225.     {
  226.         $data trim($data);
  227.         return;
  228.     }
  229. }