vendor/numero2/contao-marketing-suite/src/Resources/contao/library/Contao/CMSConfig.php line 123

Open in your IDE?
  1. <?php
  2. /**
  3.  * Contao Open Source CMS
  4.  *
  5.  * Copyright (c) 2005-2020 Leo Feyer
  6.  *
  7.  * @package   Contao Marketing Suite
  8.  * @author    Benny Born <benny.born@numero2.de>
  9.  * @author    Michael Bösherz <michael.boesherz@numero2.de>
  10.  * @license   Commercial
  11.  * @copyright 2020 numero2 - Agentur für digitales Marketing
  12.  */
  13. namespace Contao;
  14. use numero2\MarketingSuite\Backend\License as voudu;
  15. class CMSConfig {
  16.     /**
  17.      * Object instance (Singleton)
  18.      * @var Config
  19.      */
  20.     protected static $objInstance;
  21.     /**
  22.      * Files object
  23.      * @var Files
  24.      */
  25.     protected $Files;
  26.     /**
  27.      * Top content
  28.      * @var string
  29.      */
  30.     protected $strTop '';
  31.     /**
  32.      * Bottom content
  33.      * @var string
  34.      */
  35.     protected $strBottom '';
  36.     /**
  37.      * Modification indicator
  38.      * @var boolean
  39.      */
  40.     protected $blnIsModified false;
  41.     /**
  42.      * Local file existance
  43.      * @var boolean
  44.      */
  45.     protected static $blnHasLcf;
  46.     /**
  47.      * Data
  48.      * @var array
  49.      */
  50.     protected $arrData = [];
  51.     /**
  52.      * Cache
  53.      * @var array
  54.      */
  55.     protected $arrCache = [];
  56.     /**
  57.      * Prevent direct instantiation (Singleton)
  58.      */
  59.     protected function __construct() {}
  60.     /**
  61.      * Automatically save the local configuration
  62.      */
  63.     public function __destruct() {
  64.         if( $this->blnIsModified ) {
  65.             $this->save();
  66.         }
  67.     }
  68.     /**
  69.      * Prevent cloning of the object (Singleton)
  70.      */
  71.     final public function __clone() {}
  72.     /**
  73.      * Return the current object instance (Singleton)
  74.      *
  75.      * @return static The object instance
  76.      */
  77.     public static function getInstance() {
  78.         if( static::$objInstance === null ) {
  79.             static::$objInstance = new static();
  80.             static::$objInstance->initialize();
  81.         }
  82.         return static::$objInstance;
  83.     }
  84.     /**
  85.      * Load all configuration files
  86.      */
  87.     protected function initialize() {
  88.         if( static::$blnHasLcf === null ) {
  89.             static::preload();
  90.         }
  91.         $strCacheDir \System::getContainer()->getParameter('kernel.cache_dir');
  92.         voudu::con();
  93.         if( file_exists($strCacheDir '/contao/config/cmsconfig.php') ) {
  94.             include $strCacheDir '/contao/config/cmsconfig.php';
  95.         } else {
  96.             try {
  97.                 $files \System::getContainer()->get('contao.resource_locator')->locate('config/cmsconfig.php'nullfalse);
  98.             } catch (\InvalidArgumentException $e) {
  99.                 $files = [];
  100.             }
  101.             foreach( $files as $file ) {
  102.                 include $file;
  103.             }
  104.         }
  105.         // Include the local configuration file again
  106.         if( static::$blnHasLcf ) {
  107.             include TL_ROOT '/system/config/cmsconfig.php';
  108.         }
  109.         \Controller::loadDataContainer('tl_cms_settings');
  110.         $oDCA = new \DC_CMSFile('tl_cms_settings');
  111.         foreach( $GLOBALS['TL_CMSCONFIG'] as $fieldKey => $fieldConfig ) {
  112.             if( empty($GLOBALS['TL_DCA']['tl_cms_settings']['fields'][$fieldKey]) ) {
  113.                 continue;
  114.             }
  115.             $fieldValue = static::get('fieldKey');
  116.             $oDCA->strField $fieldKey;
  117.             $oDCA->strInputName $fieldKey;
  118.             $oDCA->varValue = static::get('fieldKey');
  119.             if( !empty($GLOBALS['TL_DCA']['tl_cms_settings']['fields'][$fieldKey]['load_callback']) ) {
  120.                 if( is_array($GLOBALS['TL_DCA']['tl_cms_settings']['fields'][$fieldKey]['load_callback']) ) {
  121.                     $varValue $fieldValue;
  122.                     foreach( $GLOBALS['TL_DCA']['tl_cms_settings']['fields'][$fieldKey]['load_callback'] as $callback ) {
  123.                         if( is_array($callback) ) {
  124.                             $class \System::importStatic($callback[0]);
  125.                             $varValue $class->{$callback[1]}($varValue$oDCA);
  126.                         } else if( is_callable($callback) ) {
  127.                             $varValue $callback($varValue$oDCA);
  128.                         }
  129.                     }
  130.                     if( $fieldValue !== $varValue ) {
  131.                         static::set($fieldKey$varValue);
  132.                         // static::persist($fieldKey, $varValue);
  133.                     }
  134.                 }
  135.             }
  136.         }
  137.     }
  138.     /**
  139.      * Mark the object as modified
  140.      */
  141.     protected function markModified() {
  142.         // Return if marked as modified already
  143.         if( $this->blnIsModified === true ) {
  144.             return;
  145.         }
  146.         $this->blnIsModified true;
  147.         // Reset the top and bottom content (see #344)
  148.         $this->strTop '';
  149.         $this->strBottom '';
  150.         // Import the Files object (required in the destructor)
  151.         $this->Files \Files::getInstance();
  152.         // Parse the local configuration file
  153.         if( static::$blnHasLcf ) {
  154.             $strMode 'top';
  155.             $resFile fopen(TL_ROOT '/system/config/cmsconfig.php''rb');
  156.             while( !feof($resFile) ) {
  157.                 $strLine fgets($resFile);
  158.                 $strTrim trim($strLine);
  159.                 if( $strTrim == '?>' ) {
  160.                     continue;
  161.                 }
  162.                 if( $strTrim == '### INSTALL SCRIPT START ###' ) {
  163.                     $strMode 'data';
  164.                     continue;
  165.                 }
  166.                 if( $strTrim == '### INSTALL SCRIPT STOP ###' ) {
  167.                     $strMode 'bottom';
  168.                     continue;
  169.                 }
  170.                 if( $strMode == 'top' ) {
  171.                     $this->strTop .= $strLine;
  172.                 } else if( $strMode == 'bottom' ) {
  173.                     $this->strBottom .= $strLine;
  174.                 } else if( $strTrim != '' ) {
  175.                     $arrChunks array_map('trim'explode('='$strLine2));
  176.                     $this->arrData[$arrChunks[0]] = $arrChunks[1];
  177.                 }
  178.             }
  179.             fclose($resFile);
  180.         }
  181.     }
  182.     /**
  183.      * Save the local configuration file
  184.      */
  185.     public function save() {
  186.         if( $this->strTop == '' ) {
  187.             $this->strTop '<?php';
  188.         }
  189.         $strFile  trim($this->strTop) . "\n\n";
  190.         $strFile .= "### INSTALL SCRIPT START ###\n";
  191.         foreach( $this->arrData as $k=>$v ) {
  192.             $strFile .= "$k = $v\n";
  193.         }
  194.         $strFile .= "### INSTALL SCRIPT STOP ###\n";
  195.         $this->strBottom trim($this->strBottom);
  196.         if( $this->strBottom != '' ) {
  197.             $strFile .= "\n" $this->strBottom "\n";
  198.         }
  199.         $strTemp md5(uniqid(mt_rand(), true));
  200.         // Write to a temp file first
  201.         $objFile fopen(TL_ROOT '/system/tmp/' $strTemp'wb');
  202.         fwrite($objFile$strFile);
  203.         fclose($objFile);
  204.         // Make sure the file has been written (see #4483)
  205.         if( !filesize(TL_ROOT '/system/tmp/' $strTemp) ) {
  206.             \System::log('The local cms configuration file could not be written. Have your reached your quota limit?'__METHOD__TL_ERROR);
  207.             return;
  208.         }
  209.         // Adjust the file permissions (see #8178)
  210.         $this->Files->chmod('system/tmp/' $strTemp\Config::get('defaultFileChmod'));
  211.         // Then move the file to its final destination
  212.         $this->Files->rename('system/tmp/' $strTemp'system/config/cmsconfig.php');
  213.         // Reset the Zend OPcache
  214.         if( \function_exists('opcache_invalidate') ) {
  215.             opcache_invalidate(TL_ROOT '/system/config/cmsconfig.php'true);
  216.         }
  217.         // Recompile the APC file (thanks to Trenker)
  218.         if( \function_exists('apc_compile_file') && !ini_get('apc.stat') ) {
  219.             apc_compile_file(TL_ROOT '/system/config/cmsconfig.php');
  220.         }
  221.         $this->blnIsModified false;
  222.     }
  223.     /**
  224.      * Add a configuration variable to the local configuration file
  225.      *
  226.      * @param string $strKey   The full variable name
  227.      * @param mixed  $varValue The configuration value
  228.      */
  229.     public function add$strKey$varValue ) {
  230.         $this->markModified();
  231.         $this->arrData[$strKey] = $this->escape($varValue) . ';';
  232.     }
  233.     /**
  234.      * Alias for Config::add()
  235.      *
  236.      * @param string $strKey   The full variable name
  237.      * @param mixed  $varValue The configuration value
  238.      */
  239.     public function update$strKey$varValue ) {
  240.         $this->add($strKey$varValue);
  241.     }
  242.     /**
  243.      * Remove a configuration variable
  244.      *
  245.      * @param string $strKey The full variable name
  246.      */
  247.     public function delete$strKey ) {
  248.         $this->markModified();
  249.         unset($this->arrData[$strKey]);
  250.     }
  251.     /**
  252.      * Check whether a configuration value exists
  253.      *
  254.      * @param string $strKey The short key
  255.      *
  256.      * @return boolean True if the configuration value exists
  257.      */
  258.     public static function has$strKey ) {
  259.         return array_key_exists($strKey$GLOBALS['TL_CMSCONFIG']);
  260.     }
  261.     /**
  262.      * Return a configuration value
  263.      *
  264.      * @param string $strKey The short key
  265.      *
  266.      * @return mixed|null The configuration value
  267.      */
  268.     public static function get$strKey ) {
  269.         if( isset($GLOBALS['TL_CMSCONFIG'][$strKey]) ) {
  270.             return $GLOBALS['TL_CMSCONFIG'][$strKey];
  271.         }
  272.         return null;
  273.     }
  274.     /**
  275.      * Temporarily set a configuration value
  276.      *
  277.      * @param string $strKey The short key
  278.      * @param string $varValue The configuration value
  279.      */
  280.     public static function set$strKey$varValue ) {
  281.         $GLOBALS['TL_CMSCONFIG'][$strKey] = $varValue;
  282.     }
  283.     /**
  284.      * Permanently set a configuration value
  285.      *
  286.      * @param string $strKey The short key or full variable name
  287.      * @param mixed  $varValue The configuration value
  288.      */
  289.     public static function persist$strKey$varValue ) {
  290.         $objConfig = static::getInstance();
  291.         if( strncmp($strKey'$GLOBALS'8) !== ) {
  292.             $strKey "\$GLOBALS['TL_CMSCONFIG']['$strKey']";
  293.         }
  294.         $objConfig->add($strKey$varValue);
  295.     }
  296.     /**
  297.      * Permanently remove a configuration value
  298.      *
  299.      * @param string $strKey The short key or full variable name
  300.      */
  301.     public static function remove$strKey ) {
  302.         $objConfig = static::getInstance();
  303.         if( strncmp($strKey'$GLOBALS'8) !== ) {
  304.             $strKey "\$GLOBALS['TL_CMSCONFIG']['$strKey']";
  305.         }
  306.         $objConfig->delete($strKey);
  307.     }
  308.     /**
  309.      * Preload the default and local configuration
  310.      */
  311.     public static function preload() {
  312.         $blnHasLcf file_exists(TL_ROOT '/system/config/cmsconfig.php');
  313.         // Include the local configuration file
  314.         if( $blnHasLcf === true ) {
  315.             include TL_ROOT '/system/config/cmsconfig.php';
  316.         }
  317.         static::$blnHasLcf $blnHasLcf;
  318.     }
  319.     /**
  320.      * Escape a value depending on its type
  321.      *
  322.      * @param mixed $varValue The value
  323.      *
  324.      * @return mixed The escaped value
  325.      */
  326.     protected function escape$varValue ) {
  327.         if( is_numeric($varValue) && !preg_match('/e|^[+-]?0[^.]/'$varValue) && $varValue PHP_INT_MAX ) {
  328.             return $varValue;
  329.         }
  330.         if( \is_bool($varValue) ) {
  331.             return $varValue 'true' 'false';
  332.         }
  333.         if( $varValue == 'true' ) {
  334.             return 'true';
  335.         }
  336.         if( $varValue == 'false' ) {
  337.             return 'false';
  338.         }
  339.         return "'" str_replace('\\"''"'preg_replace('/[\n\r\t ]+/'' 'addslashes($varValue))) . "'";
  340.     }
  341. }