vendor/bugbuster/contao-banner-bundle/src/Resources/contao/classes/BannerLogic.php line 155

Open in your IDE?
  1. <?php
  2. /**
  3.  * Extension for Contao Open Source CMS, Copyright (C) 2005-2022 Leo Feyer
  4.  *
  5.  * BannerLogic - Frontend Helper Class
  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. namespace BugBuster\Banner;
  14. use BugBuster\Banner\BannerLog;
  15. use BugBuster\Banner\BannerReferrer;
  16. /**
  17.  * Class BannerLogic
  18.  *
  19.  * @copyright  Glen Langer 2022 <http://contao.ninja>
  20.  * @author     Glen Langer (BugBuster)
  21.  * @license    LGPL
  22.  */
  23. class BannerLogic
  24. {
  25.     private $_session = [];
  26.     public $statusRandomBlocker false;
  27.     public $statusBannerFirstView;
  28.     /**
  29.      * Get weighting for single banner
  30.      *
  31.      * @param $arrAllBannersBasic [id,weighting]
  32.      *
  33.      * @return integer 0|1|2|3    0 on error
  34.      */
  35.     public function getSingleWeighting($arrAllBannersBasic)
  36.     {
  37.         $arrPrio = [];
  38.         $arrPrioW = [];
  39.         $arrWeights array_flip($arrAllBannersBasic);
  40.         //welche Wichtungen gibt es?
  41.         if (\array_key_exists(1$arrWeights)) {
  42.             $arrPrioW[1] = 1;
  43.         }
  44.         if (\array_key_exists(2$arrWeights)) {
  45.             $arrPrioW[2] = 2;
  46.         }
  47.         if (\array_key_exists(3$arrWeights)) {
  48.             $arrPrioW[3] = 3;
  49.         }
  50.         $arrPrio[0] = ['start'=>0,  'stop'=>0];
  51.         $arrPrio[1] = ['start'=>1,  'stop'=>90];
  52.         $arrPrio[2] = ['start'=>91'stop'=>150];
  53.         $arrPrio[3] = ['start'=>151'stop'=>180];
  54.         if (!\array_key_exists(2$arrPrioW)) {
  55.             // no prio 2 banner
  56.             $arrPrio[2] = ['start'=>0,  'stop'=>0];
  57.             $arrPrio[3] = ['start'=>91'stop'=>120];
  58.         }
  59.         $intPrio1 = (\count($arrPrioW)) ? min($arrPrioW) : 0;
  60.         $intPrio2 = (\count($arrPrioW)) ? max($arrPrioW) : 0;
  61.         //wenn Wichtung vorhanden, dann per Zufall eine auswählen
  62.         if ($intPrio1>0) {
  63.             $intWeightingHigh mt_rand($arrPrio[$intPrio1]['start'], $arrPrio[$intPrio2]['stop']);
  64.             // 1-180 auf 1-3 umrechnen
  65.             if ($intWeightingHigh<=$arrPrio[3]['stop']) {
  66.                 $intWeighting=3;
  67.             }
  68.             if ($intWeightingHigh<=$arrPrio[2]['stop']) {
  69.                 $intWeighting=2;
  70.             }
  71.             if ($intWeightingHigh<=$arrPrio[1]['stop']) {
  72.                 $intWeighting=1;
  73.             }
  74.         } else {
  75.             $intWeighting=0;
  76.         }
  77.         return $intWeighting;
  78.     }
  79.     /**
  80.      * Get session
  81.      *
  82.      * @param  string $session_name e.g.: 'RandomBlocker'
  83.      * @return void
  84.      */
  85.     public function getSession($session_name)
  86.     {
  87.         $this->_session = (array) \Session::getInstance()->get($session_name);
  88.         return $this->_session;
  89.     }
  90.     /**
  91.      * Set session
  92.      *
  93.      * @param  string $session_name e.g.: 'RandomBlocker'
  94.      * @param  array  $arrData      array('key' => array(Value1,Value2,...))
  95.      * @return void
  96.      */
  97.     public function setSession($session_name$arrData$merge false)
  98.     {
  99.         if ($merge) {
  100.             $this->_session \Session::getInstance()->get($session_name);
  101.             // numerische Schlüssel werden neu numeriert, daher
  102.             // geht nicht: array_merge($this->_session, $arrData)
  103.             $merge_array = (array) $this->_session $arrData;
  104.             \Session::getInstance()->set($session_name$merge_array);
  105.         } else {
  106.             \Session::getInstance()->set($session_name$arrData);
  107.         }
  108.     }
  109.     /**
  110.      * BannerLogic::setRandomBlockerId
  111.      *
  112.      * Random Blocker, Set Banner-ID
  113.      *
  114.      * @param integer $BannerID
  115.      * @param integer $module_id
  116.      */
  117.     public function setRandomBlockerId($BannerID$module_id)
  118.     {
  119.         if ($BannerID==0) {
  120.             return;
  121.         }// kein Banner, nichts zu tun
  122.         $this->statusRandomBlocker true;
  123.         $this->setSession('RandomBlocker'.$module_id, [$BannerID => time()]);
  124.         BannerLog::writeLog(__METHOD____LINE__'setRandomBlockerId BannerID:'.$BannerID);
  125.         return;
  126.     }
  127.     /**
  128.      * BannerLogic::getRandomBlockerId
  129.      *
  130.      * Random Blocker, Get Banner-ID
  131.      *
  132.      * @param  integer $module_id
  133.      * @return integer Banner-ID
  134.      */
  135.     public function getRandomBlockerId($module_id)
  136.     {
  137.         $this->getSession('RandomBlocker'.$module_id);
  138.         if (\count($this->_session)) {
  139.             $key   key($this->_session);
  140.             $value current($this->_session);
  141.             unset($value);
  142.             reset($this->_session);
  143.             BannerLog::writeLog(__METHOD____LINE__'getRandomBlockerId BannerID:'.$key);
  144.             //DEBUG log_message('getRandomBlockerId BannerID:'.$key,'Banner.log');
  145.             return $key;
  146.         }
  147.         return 0;
  148.     }
  149.     /**
  150.      * BannerLogic::setFirstViewBlockerId
  151.      *
  152.      * First View Blocker, Set Banner Categorie-ID and timestamp
  153.      *
  154.      * @param integer $banner_categorie
  155.      * @param integer $module_id
  156.      */
  157.     public function setFirstViewBlockerId($banner_categorie$module_id)
  158.     {
  159.         if ($banner_categorie==0) {
  160.             return;
  161.         }// keine Banner Kategorie, nichts zu tun
  162.         $this->statusFirstViewBlocker true;
  163.         $this->setSession('FirstViewBlocker'.$module_id, [$banner_categorie => time()]);
  164.         return;
  165.     }
  166.     /**
  167.      * BannerLogic::getFirstViewBlockerId
  168.      *
  169.      * First View Blocker, Get Banner Categorie-ID if the timestamp ....
  170.      *
  171.      * @param integer $module_id
  172.      */
  173.     public function getFirstViewBlockerId($module_id)
  174.     {
  175.         $this->getSession('FirstViewBlocker'.$module_id);
  176.         if (\count($this->_session)) {
  177.             // each deprecated in PHP 7.2, daher
  178.             $key    key($this->_session);
  179.             $tstmap current($this->_session);
  180.             reset($this->_session);
  181.             if ($this->removeOldFirstViewBlockerId($key$tstmap) === true) {
  182.                 // Key ist noch gültig und es muss daher geblockt werden
  183.                 //DEBUG log_message('getFirstViewBlockerId Banner Kat ID: '.$key,'Banner.log');
  184.                 return $key;
  185.             }
  186.         }
  187.         return false;
  188.     }
  189.     /**
  190.      * BannerLogic::removeOldFirstViewBlockerId
  191.      *
  192.      * First View Blocker, Remove old Banner Categorie-ID
  193.      *
  194.      * @param  integer $banner_categorie
  195.      * @return boolean true = Key is valid, it must be blocked | false = key is invalid
  196.      */
  197.     public function removeOldFirstViewBlockerId($key$tstmap)
  198.     {
  199.         // 5 Minuten Blockierung, älter >= 5 Minuten wird gelöscht
  200.         $FirstViewBlockTime time() - 60*5;
  201.         if ($tstmap >  $FirstViewBlockTime) {
  202.             return true;
  203.         } else {
  204.             \Session::getInstance()->remove($key);
  205.         }
  206.         return false;
  207.     }
  208.     /**
  209.      * BannerLogic::getSetFirstView
  210.      *
  211.      * Get FirstViewBanner status and set cat id as blocker
  212.      *
  213.      * @param   char(1)     DB Feld, '' / 1 checkbox Firstview Banner
  214.      * @return boolean true = if requested and not blocked | false = if requested but blocked
  215.      */
  216.     public function getSetFirstView($banner_firstview$banner_categories$module_id)
  217.     {
  218.         //return true; // for Test only
  219.         //FirstViewBanner gewünscht?
  220.         if ($banner_firstview !=1) {
  221.             return false;
  222.         }
  223.         $this->BannerReferrer = new BannerReferrer();
  224.         $this->BannerReferrer->checkReferrer();
  225.         $ReferrerDNS $this->BannerReferrer->getReferrerDNS();
  226.         // o own , w wrong
  227.         if ($ReferrerDNS === 'o') {
  228.             // eigener Referrer, Begrenzung auf First View nicht nötig.
  229.             $this->statusBannerFirstView false;
  230.             return false;
  231.         }
  232.         if ($this->getFirstViewBlockerId($module_id) === false) {
  233.             // nichts geblockt, also blocken fürs den nächsten Aufruf
  234.             $this->setFirstViewBlockerId($banner_categories$module_id);
  235.             // kein firstview block gefunden, Anzeigen erlaubt
  236.             $this->statusBannerFirstView true;
  237.             return true;
  238.         } else {
  239.             $this->statusBannerFirstView false;
  240.             return false;
  241.         }
  242.     }
  243. }