vendor/pimcore/pimcore/lib/Config.php line 474

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Enterprise License (PEL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  * @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  * @license    http://www.pimcore.org/license     GPLv3 and PEL
  13.  */
  14. namespace Pimcore;
  15. use Pimcore\Config\EnvironmentConfig;
  16. use Pimcore\Config\EnvironmentConfigInterface;
  17. use Pimcore\Model\WebsiteSetting;
  18. use Symfony\Cmf\Bundle\RoutingBundle\Routing\DynamicRouter;
  19. use Symfony\Component\Console\Input\ArgvInput;
  20. use Symfony\Component\HttpFoundation\IpUtils;
  21. use Symfony\Component\Yaml\Yaml;
  22. class Config
  23. {
  24.     /**
  25.      * @var array
  26.      */
  27.     protected static $configFileCache = [];
  28.     /**
  29.      * @var string
  30.      */
  31.     protected static $environment null;
  32.     /**
  33.      * @var EnvironmentConfigInterface
  34.      */
  35.     private static $environmentConfig;
  36.     /**
  37.      * @param $name - name of configuration file. slash is allowed for subdirectories.
  38.      *
  39.      * @return mixed
  40.      */
  41.     public static function locateConfigFile($name)
  42.     {
  43.         if (!isset(self::$configFileCache[$name])) {
  44.             $pathsToCheck = [
  45.                 PIMCORE_CUSTOM_CONFIGURATION_DIRECTORY,
  46.                 PIMCORE_CONFIGURATION_DIRECTORY,
  47.             ];
  48.             $file null;
  49.             // check for environment configuration
  50.             $env self::getEnvironment();
  51.             if ($env) {
  52.                 $fileExt File::getFileExtension($name);
  53.                 $pureName str_replace('.' $fileExt''$name);
  54.                 foreach ($pathsToCheck as $path) {
  55.                     $tmpFile $path '/' $pureName '_' $env '.' $fileExt;
  56.                     if (file_exists($tmpFile)) {
  57.                         $file $tmpFile;
  58.                         break;
  59.                     }
  60.                 }
  61.             }
  62.             //check for config file without environment configuration
  63.             if (!$file) {
  64.                 foreach ($pathsToCheck as $path) {
  65.                     $tmpFile $path '/' $name;
  66.                     if (file_exists($tmpFile)) {
  67.                         $file $tmpFile;
  68.                         break;
  69.                     }
  70.                 }
  71.             }
  72.             //get default path in pimcore configuration directory
  73.             if (!$file) {
  74.                 $file PIMCORE_CONFIGURATION_DIRECTORY '/' $name;
  75.             }
  76.             self::$configFileCache[$name] = $file;
  77.         }
  78.         return self::$configFileCache[$name];
  79.     }
  80.     /**
  81.      * @internal
  82.      *
  83.      * @return null|array
  84.      */
  85.     public static function getSystemConfiguration()
  86.     {
  87.         $config null;
  88.         if ($container = \Pimcore::getContainer()) {
  89.             $config $container->getParameter('pimcore.config');
  90.             $adminConfig $container->getParameter('pimcore_admin.config');
  91.             $config array_merge_recursive($config$adminConfig);
  92.         }
  93.         return $config;
  94.     }
  95.     /**
  96.      * @internal
  97.      * @static
  98.      *
  99.      * @param \Pimcore\Config\Config $config
  100.      */
  101.     public static function setSystemConfig(\Pimcore\Config\Config $config)
  102.     {
  103.         \Pimcore\Cache\Runtime::set('pimcore_config_system'$config);
  104.     }
  105.     /**
  106.      * @internal
  107.      *
  108.      * @param null $languange
  109.      *
  110.      * @return string
  111.      */
  112.     public static function getWebsiteConfigRuntimeCacheKey($languange null)
  113.     {
  114.         $cacheKey 'pimcore_config_website';
  115.         if ($languange) {
  116.             $cacheKey .= '_' $languange;
  117.         }
  118.         return $cacheKey;
  119.     }
  120.     /**
  121.      * @static
  122.      *
  123.      * @return mixed|\Pimcore\Config\Config
  124.      */
  125.     public static function getWebsiteConfig($language null)
  126.     {
  127.         if (\Pimcore\Cache\Runtime::isRegistered(self::getWebsiteConfigRuntimeCacheKey($language))) {
  128.             $config = \Pimcore\Cache\Runtime::get(self::getWebsiteConfigRuntimeCacheKey($language));
  129.         } else {
  130.             $cacheKey 'website_config';
  131.             if ($language) {
  132.                 $cacheKey .= '_' $language;
  133.             }
  134.             $siteId null;
  135.             if (Model\Site::isSiteRequest()) {
  136.                 $siteId Model\Site::getCurrentSite()->getId();
  137.             } elseif (Tool::isFrontendRequestByAdmin()) {
  138.                 // this is necessary to set the correct settings in editmode/preview (using the main domain)
  139.                 // we cannot use the document resolver service here, because we need the document on the master request
  140.                 $originDocument = \Pimcore::getContainer()->get('request_stack')->getMasterRequest()->get(DynamicRouter::CONTENT_KEY);
  141.                 if ($originDocument) {
  142.                     $site Tool\Frontend::getSiteForDocument($originDocument);
  143.                     if ($site) {
  144.                         $siteId $site->getId();
  145.                     }
  146.                 }
  147.             }
  148.             if ($siteId) {
  149.                 $cacheKey $cacheKey '_site_' $siteId;
  150.             }
  151.             if (!$config Cache::load($cacheKey)) {
  152.                 $settingsArray = [];
  153.                 $cacheTags = ['website_config''system''config''output'];
  154.                 $list = new Model\WebsiteSetting\Listing();
  155.                 $list $list->load();
  156.                 /** @var $item WebsiteSetting */
  157.                 foreach ($list as $item) {
  158.                     $itemSiteId $item->getSiteId();
  159.                     if ($itemSiteId !== && $itemSiteId !== $siteId) {
  160.                         continue;
  161.                     }
  162.                     $itemLanguage $item->getLanguage();
  163.                     if ($itemLanguage && $language !== $itemLanguage) {
  164.                         continue;
  165.                     }
  166.                     $key $item->getName();
  167.                     if (!$itemLanguage && isset($settingsArray[$key])) {
  168.                         continue;
  169.                     }
  170.                     switch ($item->getType()) {
  171.                         case 'document':
  172.                         case 'asset':
  173.                         case 'object':
  174.                             $s $item->getData();
  175.                             break;
  176.                         case 'bool':
  177.                             $s = (bool) $item->getData();
  178.                             break;
  179.                         case 'text':
  180.                             $s = (string) $item->getData();
  181.                             break;
  182.                         default:
  183.                             $s null;
  184.                             break;
  185.                     }
  186.                     if ($s instanceof Model\Element\ElementInterface) {
  187.                         $cacheTags $s->getCacheTags($cacheTags);
  188.                     }
  189.                     if (isset($s)) {
  190.                         $settingsArray[$key] = $s;
  191.                     }
  192.                 }
  193.                 //TODO resolve for all langs, current lang first, then no lang
  194.                 $config = new \Pimcore\Config\Config($settingsArraytrue);
  195.                 Cache::save($config$cacheKey$cacheTagsnull998);
  196.             }
  197.             self::setWebsiteConfig($config$language);
  198.         }
  199.         return $config;
  200.     }
  201.     /**
  202.      * @internal
  203.      *
  204.      * @param Config\Config $config
  205.      * @param null $language
  206.      */
  207.     public static function setWebsiteConfig(\Pimcore\Config\Config $config$language null)
  208.     {
  209.         \Pimcore\Cache\Runtime::set(self::getWebsiteConfigRuntimeCacheKey($language), $config);
  210.     }
  211.     /**
  212.      * Returns whole website config or only a given setting for the current site
  213.      *
  214.      * @param null|mixed $key       Config key to directly load. If null, the whole config will be returned
  215.      * @param null|mixed $default   Default value to use if the key is not set
  216.      * @param null|string $language   Language
  217.      *
  218.      * @return Config\Config|mixed
  219.      */
  220.     public static function getWebsiteConfigValue($key null$default null$language null)
  221.     {
  222.         $config self::getWebsiteConfig($language);
  223.         if (null !== $key) {
  224.             return $config->get($key$default);
  225.         }
  226.         return $config;
  227.     }
  228.     private static function getArrayValue($keys$array)
  229.     {
  230.         $len count($keys);
  231.         $pointer $array;
  232.         for ($i 0$i $len$i++) {
  233.             $key $keys[$i];
  234.             if (array_key_exists($key$pointer)) {
  235.                 $pointer $pointer[$key];
  236.             } else {
  237.                 return null;
  238.             }
  239.         }
  240.         return $pointer;
  241.     }
  242.     /**
  243.      * @param $config
  244.      *
  245.      * @return array|Config\Config
  246.      */
  247.     private static function mapLegacyConfiguration($config)
  248.     {
  249.         $systemConfig = [];
  250.         if (is_array($config)) {
  251.             //legacy system configuration mapping
  252.             $systemConfig = new \Pimcore\Config\Config([
  253.                 'general' => [
  254.                     'timezone' => self::getArrayValue(['general''timezone'], $config),
  255.                     'path_variable' => self::getArrayValue(['general''path_variable'], $config),
  256.                     'domain' => self::getArrayValue(['general''domain'], $config),
  257.                     'redirect_to_maindomain' => self::getArrayValue(['general''redirect_to_maindomain'], $config),
  258.                     'language' => self::getArrayValue(['general''language'], $config),
  259.                     'validLanguages' => self::getArrayValue(['general''valid_languages'], $config),
  260.                     'fallbackLanguages' => self::getArrayValue(['general''fallback_languages'], $config),
  261.                     'defaultLanguage' => self::getArrayValue(['general''default_language'], $config),
  262.                     'loginscreencustomimage' => self::getArrayValue(['branding''login_screen_custom_image'], $config),
  263.                     'disableusagestatistics' => self::getArrayValue(['general''disable_usage_statistics'], $config),
  264.                     'debug_admin_translations' => self::getArrayValue(['general''debug_admin_translations'], $config),
  265.                     'instanceIdentifier' => self::getArrayValue(['general''instance_identifier'], $config),
  266.                     'show_cookie_notice' => self::getArrayValue(['general''show_cookie_notice'], $config)
  267.                 ],
  268.                 'documents' => [
  269.                     'versions' => [
  270.                         'days' => self::getArrayValue(['documents''versions''days'], $config),
  271.                         'steps' => self::getArrayValue(['documents''versions''steps'], $config)
  272.                     ],
  273.                     'error_pages' => self::getArrayValue(['documents''error_pages'], $config),
  274.                     'createredirectwhenmoved' => self::getArrayValue(['documents''create_redirect_when_moved'], $config),
  275.                     'allowtrailingslash' => self::getArrayValue(['documents''allow_trailing_slash'], $config),
  276.                     'generatepreview' => self::getArrayValue(['documents''generate_preview'], $config)
  277.                 ],
  278.                 'objects' => [
  279.                     'versions' => [
  280.                         'days' => self::getArrayValue(['objects''versions''days'], $config),
  281.                         'steps' => self::getArrayValue(['objects''versions''steps'], $config)
  282.                     ]
  283.                 ],
  284.                 'assets' => [
  285.                     'versions' => [
  286.                         'days' => self::getArrayValue(['assets''versions''days'], $config),
  287.                         'steps' => self::getArrayValue(['assets''versions''steps'], $config)
  288.                     ],
  289.                     'icc_rgb_profile' => self::getArrayValue(['assets''icc_rgb_profile'], $config),
  290.                     'icc_cmyk_profile' => self::getArrayValue(['assets''icc_cmyk_profile'], $config),
  291.                     'hide_edit_image' => self::getArrayValue(['assets''hide_edit_image'], $config),
  292.                     'disable_tree_preview' => self::getArrayValue(['assets''disable_tree_preview'], $config)
  293.                 ],
  294.                 'services' => [
  295.                     'google' => [
  296.                         'client_id' => self::getArrayValue(['services''google''client_id'], $config),
  297.                         'email' => self::getArrayValue(['services''google''email'], $config),
  298.                         'simpleapikey' => self::getArrayValue(['services''google''simple_api_key'], $config),
  299.                         'browserapikey' => self::getArrayValue(['services''google''browser_api_key'], $config)
  300.                     ]
  301.                 ],
  302.                 'cache' => [
  303.                     'enabled' => self::getArrayValue(['cache''enabled'], $config),
  304.                     'lifetime' => self::getArrayValue(['cache''lifetime'], $config),
  305.                     'excludePatterns' => self::getArrayValue(['cache''exclude_patterns'], $config),
  306.                     'excludeCookie' => self::getArrayValue(['cache''exclude_cookie'], $config)
  307.                 ],
  308.                 'webservice' => [
  309.                     'enabled' => self::getArrayValue(['webservice''enabled'], $config)
  310.                 ],
  311.                 'httpclient' => [
  312.                     'adapter' => self::getArrayValue(['httpclient''adapter'], $config),
  313.                     'proxy_host' => self::getArrayValue(['httpclient''proxy_host'], $config),
  314.                     'proxy_port' => self::getArrayValue(['httpclient''proxy_port'], $config),
  315.                     'proxy_user' => self::getArrayValue(['httpclient''proxy_user'], $config),
  316.                     'proxy_pass' => self::getArrayValue(['httpclient''proxy_pass'], $config)
  317.                 ],
  318.                 'email' => [
  319.                     'sender' => [
  320.                         'name' => self::getArrayValue(['email''sender''name'], $config),
  321.                         'email' => self::getArrayValue(['email''sender''email'], $config)
  322.                     ],
  323.                     'return' => [
  324.                         'name' => self::getArrayValue(['email''return''name'], $config),
  325.                         'email' => self::getArrayValue(['email''return''email'], $config)
  326.                     ],
  327.                     'method' => self::getArrayValue(['email''method'], $config),
  328.                     'smtp' => [
  329.                         'host' => self::getArrayValue(['email''smtp''host'], $config),
  330.                         'port' => self::getArrayValue(['email''smtp''port'], $config),
  331.                         'ssl' => self::getArrayValue(['email''smtp''encryption'], $config),
  332.                         'name' => 'smtp',
  333.                         'auth' => [
  334.                             'method' => self::getArrayValue(['email''smtp''auth_mode'], $config),
  335.                             'username' => self::getArrayValue(['email''smtp''username'], $config),
  336.                             'password' => self::getArrayValue(['email''smtp''password'], $config)
  337.                         ],
  338.                     ],
  339.                     'debug' => [
  340.                         'emailaddresses' => self::getArrayValue(['email''debug''email_addresses'], $config)
  341.                     ]
  342.                 ],
  343.                 'newsletter' => [
  344.                     'sender' => [
  345.                         'name' => self::getArrayValue(['newsletter''sender''name'], $config),
  346.                         'email' => self::getArrayValue(['newsletter''sender''email'], $config)
  347.                     ],
  348.                     'return' => [
  349.                         'name' => self::getArrayValue(['newsletter''return''name'], $config),
  350.                         'email' => self::getArrayValue(['newsletter''return''name'], $config)
  351.                     ],
  352.                     'method' => self::getArrayValue(['newsletter''method'], $config),
  353.                     'smtp' => [
  354.                         'host' => self::getArrayValue(['newsletter''smtp''host'], $config),
  355.                         'port' => self::getArrayValue(['newsletter''smtp''port'], $config),
  356.                         'ssl' => self::getArrayValue(['newsletter''smtp''encryption'], $config),
  357.                         'name' => 'smtp',
  358.                         'auth' => [
  359.                             'method' => self::getArrayValue(['newsletter''smtp''auth_mode'], $config),
  360.                             'username' => self::getArrayValue(['newsletter''smtp''username'], $config),
  361.                             'password' => self::getArrayValue(['newsletter''smtp''password'], $config)
  362.                         ],
  363.                     ],
  364.                     'debug' => self::getArrayValue(['newsletter''debug''email_addresses'], $config),
  365.                     'usespecific' => self::getArrayValue(['newsletter''use_specific'], $config)
  366.                 ],
  367.                 'branding' => [
  368.                     'login_screen_invert_colors' => self::getArrayValue(['branding''login_screen_invert_colors'], $config),
  369.                     'color_login_screen' => self::getArrayValue(['branding''color_login_screen'], $config),
  370.                     'color_admin_interface' => self::getArrayValue(['branding''color_admin_interface'], $config)
  371.                 ],
  372.                 'applicationlog' => [
  373.                     'mail_notification' => [
  374.                         'send_log_summary' => self::getArrayValue(['applicationlog''mail_notification''send_log_summary'], $config),
  375.                         'filter_priority' => self::getArrayValue(['applicationlog''mail_notification''filter_priority'], $config),
  376.                         'mail_receiver' => self::getArrayValue(['applicationlog''mail_notification''mail_receiver'], $config)
  377.                     ],
  378.                     'archive_treshold' => self::getArrayValue(['applicationlog''archive_treshold'], $config),
  379.                     'archive_alternative_database' => self::getArrayValue(['applicationlog''archive_alternative_database'], $config)
  380.                 ]
  381.             ]);
  382.         }
  383.         return $systemConfig;
  384.     }
  385.     /**
  386.      * @return mixed|null|\Pimcore\Config\Config
  387.      *
  388.      * @throws \Exception
  389.      */
  390.     public static function getSystemConfig()
  391.     {
  392.         $systemConfig null;
  393.         //try {
  394.         if (\Pimcore\Cache\Runtime::isRegistered('pimcore_config_system')) {
  395.             $systemConfig = \Pimcore\Cache\Runtime::get('pimcore_config_system');
  396.         } else {
  397.             if ($config self::getSystemConfiguration()) {
  398.                 $container = \Pimcore::getContainer();
  399.                 //add email settings
  400.                 foreach (['email' => 'pimcore_mailer''newsletter' => 'newsletter_mailer'] as $key => $group) {
  401.                     if ($container->hasParameter('swiftmailer.mailer.'.$group.'.transport.smtp.host')) {
  402.                         $config[$key]['smtp'] = [
  403.                             'host' => $container->getParameter('swiftmailer.mailer.' $group '.transport.smtp.host'),
  404.                             'username' => $container->getParameter('swiftmailer.mailer.' $group '.transport.smtp.username'),
  405.                             'password' => $container->getParameter('swiftmailer.mailer.' $group '.transport.smtp.password'),
  406.                             'port' => $container->getParameter('swiftmailer.mailer.' $group '.transport.smtp.port'),
  407.                             'encryption' => $container->getParameter('swiftmailer.mailer.' $group '.transport.smtp.encryption'),
  408.                             'auth_mode' => $container->getParameter('swiftmailer.mailer.' $group '.transport.smtp.auth_mode'),
  409.                         ];
  410.                     }
  411.                 }
  412.                 $systemConfig self::mapLegacyConfiguration($config);
  413.                 self::setSystemConfig($systemConfig);
  414.             }
  415.         }
  416.         return $systemConfig;
  417.     }
  418.     /**
  419.      * @internal
  420.      * @static
  421.      *
  422.      * @return \Pimcore\Config\Config
  423.      */
  424.     public static function getReportConfig()
  425.     {
  426.         if (\Pimcore\Cache\Runtime::isRegistered('pimcore_config_report')) {
  427.             $config = \Pimcore\Cache\Runtime::get('pimcore_config_report');
  428.         } else {
  429.             try {
  430.                 $file self::locateConfigFile('reports.php');
  431.                 $config = static::getConfigInstance($file);
  432.             } catch (\Exception $e) {
  433.                 $config = new \Pimcore\Config\Config([]);
  434.             }
  435.             self::setReportConfig($config);
  436.         }
  437.         return $config;
  438.     }
  439.     /**
  440.      * @internal
  441.      * @static
  442.      *
  443.      * @param \Pimcore\Config\Config $config
  444.      */
  445.     public static function setReportConfig(\Pimcore\Config\Config $config)
  446.     {
  447.         \Pimcore\Cache\Runtime::set('pimcore_config_report'$config);
  448.     }
  449.     /**
  450.      * @static
  451.      *
  452.      * @return \Pimcore\Config\Config
  453.      */
  454.     public static function getRobotsConfig()
  455.     {
  456.         if (\Pimcore\Cache\Runtime::isRegistered('pimcore_config_robots')) {
  457.             $config = \Pimcore\Cache\Runtime::get('pimcore_config_robots');
  458.         } else {
  459.             try {
  460.                 $file self::locateConfigFile('robots.php');
  461.                 $config = static::getConfigInstance($file);
  462.             } catch (\Exception $e) {
  463.                 $config = new \Pimcore\Config\Config([]);
  464.             }
  465.             self::setRobotsConfig($config);
  466.         }
  467.         return $config;
  468.     }
  469.     /**
  470.      * @static
  471.      *
  472.      * @param \Pimcore\Config\Config $config
  473.      */
  474.     public static function setRobotsConfig(\Pimcore\Config\Config $config)
  475.     {
  476.         \Pimcore\Cache\Runtime::set('pimcore_config_robots'$config);
  477.     }
  478.     /**
  479.      * @internal
  480.      * @static
  481.      *
  482.      * @return \Pimcore\Config\Config
  483.      */
  484.     public static function getWeb2PrintConfig()
  485.     {
  486.         if (\Pimcore\Cache\Runtime::isRegistered('pimcore_config_web2print')) {
  487.             $config = \Pimcore\Cache\Runtime::get('pimcore_config_web2print');
  488.         } else {
  489.             try {
  490.                 $file self::locateConfigFile('web2print.php');
  491.                 $config = static::getConfigInstance($file);
  492.             } catch (\Exception $e) {
  493.                 $config = new \Pimcore\Config\Config([]);
  494.             }
  495.             self::setWeb2PrintConfig($config);
  496.         }
  497.         return $config;
  498.     }
  499.     /**
  500.      * @internal
  501.      * @static
  502.      *
  503.      * @param \Pimcore\Config\Config $config
  504.      */
  505.     public static function setWeb2PrintConfig(\Pimcore\Config\Config $config)
  506.     {
  507.         \Pimcore\Cache\Runtime::set('pimcore_config_web2print'$config);
  508.     }
  509.     /**
  510.      * @internal
  511.      * @static
  512.      *
  513.      * @param \Pimcore\Config\Config $config
  514.      */
  515.     public static function setModelClassMappingConfig($config)
  516.     {
  517.         \Pimcore\Cache\Runtime::set('pimcore_config_model_classmapping'$config);
  518.     }
  519.     /**
  520.      * @internal
  521.      * @static
  522.      *
  523.      * @return mixed|\Pimcore\Config\Config
  524.      */
  525.     public static function getPerspectivesConfig()
  526.     {
  527.         if (\Pimcore\Cache\Runtime::isRegistered('pimcore_config_perspectives')) {
  528.             $config = \Pimcore\Cache\Runtime::get('pimcore_config_perspectives');
  529.         } else {
  530.             $file self::locateConfigFile('perspectives.php');
  531.             try {
  532.                 $config = static::getConfigInstance($file);
  533.                 self::setPerspectivesConfig($config);
  534.             } catch (\Exception $e) {
  535.                 Logger::info('Cannot find perspectives configuration, should be located at: ' $file);
  536.                 if (is_file($file)) {
  537.                     $m 'Your perspectives.php located at ' $file ' is invalid, please check and correct it manually!';
  538.                     Tool::exitWithError($m);
  539.                 }
  540.                 $config = new \Pimcore\Config\Config(self::getStandardPerspective());
  541.                 self::setPerspectivesConfig($config);
  542.             }
  543.         }
  544.         return $config;
  545.     }
  546.     /**
  547.      * @internal
  548.      *
  549.      * @return array
  550.      */
  551.     public static function getStandardPerspective()
  552.     {
  553.         $elementTree = [
  554.             [
  555.                 'type' => 'documents',
  556.                 'position' => 'left',
  557.                 'expanded' => false,
  558.                 'hidden' => false,
  559.                 'sort' => -3,
  560.                 'treeContextMenu' => [
  561.                     'document' => [
  562.                         'items' => [
  563.                             'addPrintPage' => self::getWeb2PrintConfig()->enableInDefaultView true false // hide add print documents by default
  564.                         ]
  565.                     ]
  566.                 ]
  567.             ],
  568.             [
  569.                 'type' => 'assets',
  570.                 'position' => 'left',
  571.                 'expanded' => false,
  572.                 'hidden' => false,
  573.                 'sort' => -2
  574.             ],
  575.             [
  576.                 'type' => 'objects',
  577.                 'position' => 'left',
  578.                 'expanded' => false,
  579.                 'hidden' => false,
  580.                 'sort' => -1
  581.             ],
  582.         ];
  583.         $cvConfigs Tool::getCustomViewConfig();
  584.         if ($cvConfigs) {
  585.             foreach ($cvConfigs as $cvConfig) {
  586.                 $cvConfig['type'] = 'customview';
  587.                 $elementTree[] = $cvConfig;
  588.             }
  589.         }
  590.         return [
  591.             'default' => [
  592.                 'iconCls' => 'pimcore_nav_icon_perspective',
  593.                 'elementTree' => $elementTree,
  594.                 'dashboards' => [
  595.                     'predefined' => [
  596.                         'welcome' => [
  597.                             'positions' => [
  598.                                 [
  599.                                     [
  600.                                         'id' => 1,
  601.                                         'type' => 'pimcore.layout.portlets.modificationStatistic',
  602.                                         'config' => null
  603.                                     ],
  604.                                     [
  605.                                         'id' => 2,
  606.                                         'type' => 'pimcore.layout.portlets.modifiedAssets',
  607.                                         'config' => null
  608.                                     ]
  609.                                 ],
  610.                                 [
  611.                                     [
  612.                                         'id' => 3,
  613.                                         'type' => 'pimcore.layout.portlets.modifiedObjects',
  614.                                         'config' => null
  615.                                     ],
  616.                                     [
  617.                                         'id' => 4,
  618.                                         'type' => 'pimcore.layout.portlets.modifiedDocuments',
  619.                                         'config' => null
  620.                                     ]
  621.                                 ]
  622.                             ]
  623.                         ]
  624.                     ]
  625.                 ]
  626.             ]
  627.         ];
  628.     }
  629.     /**
  630.      * @internal
  631.      *
  632.      * @param Model\User $currentUser
  633.      *
  634.      * @return array
  635.      */
  636.     public static function getRuntimePerspective(Model\User $currentUser null)
  637.     {
  638.         if (null === $currentUser) {
  639.             $currentUser Tool\Admin::getCurrentUser();
  640.         }
  641.         $currentConfigName $currentUser->getActivePerspective() ? $currentUser->getActivePerspective() : $currentUser->getFirstAllowedPerspective();
  642.         $config self::getPerspectivesConfig()->toArray();
  643.         $result = [];
  644.         if ($config[$currentConfigName]) {
  645.             $result $config[$currentConfigName];
  646.         } else {
  647.             $availablePerspectives self::getAvailablePerspectives($currentUser);
  648.             if ($availablePerspectives) {
  649.                 $currentPerspective reset($availablePerspectives);
  650.                 $currentConfigName $currentPerspective['name'];
  651.                 if ($currentConfigName && $config[$currentConfigName]) {
  652.                     $result $config[$currentConfigName];
  653.                 }
  654.             }
  655.         }
  656.         if ($result && $currentConfigName != $currentUser->getActivePerspective()) {
  657.             $currentUser->setActivePerspective($currentConfigName);
  658.             $currentUser->save();
  659.         }
  660.         $result['elementTree'] = self::getRuntimeElementTreeConfig($currentConfigName);
  661.         return $result;
  662.     }
  663.     /**
  664.      * @internal
  665.      *
  666.      * @param $name
  667.      *
  668.      * @return array
  669.      */
  670.     protected static function getRuntimeElementTreeConfig($name)
  671.     {
  672.         $masterConfig self::getPerspectivesConfig()->toArray();
  673.         $config $masterConfig[$name];
  674.         if (!$config) {
  675.             $config = [];
  676.         }
  677.         $tmpResult $config['elementTree'];
  678.         if (is_null($tmpResult)) {
  679.             $tmpResult = [];
  680.         }
  681.         $result = [];
  682.         $cfConfigMapping = [];
  683.         $cvConfigs Tool::getCustomViewConfig();
  684.         if ($cvConfigs) {
  685.             foreach ($cvConfigs as $node) {
  686.                 $tmpData $node;
  687.                 if (!isset($tmpData['id'])) {
  688.                     Logger::error('custom view ID is missing ' var_export($tmpDatatrue));
  689.                     continue;
  690.                 }
  691.                 if (!empty($tmpData['hidden'])) {
  692.                     continue;
  693.                 }
  694.                 // backwards compatibility
  695.                 $treeType $tmpData['treetype'] ? $tmpData['treetype'] : 'object';
  696.                 $rootNode Model\Element\Service::getElementByPath($treeType$tmpData['rootfolder']);
  697.                 if ($rootNode) {
  698.                     $tmpData['type'] = 'customview';
  699.                     $tmpData['rootId'] = $rootNode->getId();
  700.                     $tmpData['allowedClasses'] = isset($tmpData['classes']) ? explode(','$tmpData['classes']) : null;
  701.                     $tmpData['showroot'] = (bool)$tmpData['showroot'];
  702.                     $customViewId $tmpData['id'];
  703.                     $cfConfigMapping[$customViewId] = $tmpData;
  704.                 }
  705.             }
  706.         }
  707.         foreach ($tmpResult as $resultItem) {
  708.             if (!empty($resultItem['hidden'])) {
  709.                 continue;
  710.             }
  711.             if ($resultItem['type'] == 'customview') {
  712.                 $customViewId $resultItem['id'];
  713.                 if (!$customViewId) {
  714.                     Logger::error('custom view id missing ' var_export($resultItemtrue));
  715.                     continue;
  716.                 }
  717.                 $customViewCfg $cfConfigMapping[$customViewId];
  718.                 if (!$customViewCfg) {
  719.                     Logger::error('no custom view config for id  ' $customViewId);
  720.                     continue;
  721.                 }
  722.                 foreach ($resultItem as $specificConfigKey => $specificConfigValue) {
  723.                     $customViewCfg[$specificConfigKey] = $specificConfigValue;
  724.                 }
  725.                 $result[] = $customViewCfg;
  726.             } else {
  727.                 $result[] = $resultItem;
  728.             }
  729.         }
  730.         usort($result, function ($treeA$treeB) {
  731.             $a $treeA['sort'] ? $treeA['sort'] : 0;
  732.             $b $treeB['sort'] ? $treeB['sort'] : 0;
  733.             if ($a $b) {
  734.                 return 1;
  735.             } elseif ($a $b) {
  736.                 return -1;
  737.             } else {
  738.                 return 0;
  739.             }
  740.         });
  741.         return $result;
  742.     }
  743.     /**
  744.      * @internal
  745.      * @static
  746.      *
  747.      * @param \Pimcore\Config\Config $config
  748.      */
  749.     public static function setPerspectivesConfig(\Pimcore\Config\Config $config)
  750.     {
  751.         \Pimcore\Cache\Runtime::set('pimcore_config_perspectives'$config);
  752.     }
  753.     /**
  754.      * @internal
  755.      *
  756.      * @param Model\User $user
  757.      *
  758.      * @return array
  759.      */
  760.     public static function getAvailablePerspectives($user)
  761.     {
  762.         $currentConfigName null;
  763.         $masterConfig self::getPerspectivesConfig()->toArray();
  764.         if ($user instanceof  Model\User) {
  765.             if ($user->isAdmin()) {
  766.                 $config self::getPerspectivesConfig()->toArray();
  767.             } else {
  768.                 $config = [];
  769.                 $roleIds $user->getRoles();
  770.                 $userIds = [$user->getId()];
  771.                 $userIds array_merge($userIds$roleIds);
  772.                 foreach ($userIds as $userId) {
  773.                     if (in_array($userId$roleIds)) {
  774.                         $userOrRoleToCheck Model\User\Role::getById($userId);
  775.                     } else {
  776.                         $userOrRoleToCheck Model\User::getById($userId);
  777.                     }
  778.                     $perspectives $userOrRoleToCheck $userOrRoleToCheck->getPerspectives() : null;
  779.                     if ($perspectives) {
  780.                         foreach ($perspectives as $perspectiveName) {
  781.                             $masterDef $masterConfig[$perspectiveName];
  782.                             if ($masterDef) {
  783.                                 $config[$perspectiveName] = $masterDef;
  784.                             }
  785.                         }
  786.                     }
  787.                 }
  788.                 if (!$config) {
  789.                     $config self::getPerspectivesConfig()->toArray();
  790.                 }
  791.             }
  792.             if ($config) {
  793.                 $tmpConfig = [];
  794.                 $validPerspectiveNames array_keys($config);
  795.                 // sort the stuff
  796.                 foreach ($masterConfig as $masterConfigName => $masterConfiguration) {
  797.                     if (in_array($masterConfigName$validPerspectiveNames)) {
  798.                         $tmpConfig[$masterConfigName] = $masterConfiguration;
  799.                     }
  800.                 }
  801.                 $config $tmpConfig;
  802.             }
  803.             $currentConfigName $user->getActivePerspective();
  804.             if ($config && !in_array($currentConfigNamearray_keys($config))) {
  805.                 $currentConfigName reset(array_keys($config));
  806.             }
  807.         } else {
  808.             $config self::getPerspectivesConfig()->toArray();
  809.         }
  810.         $result = [];
  811.         foreach ($config as $configName => $configItem) {
  812.             $item = [
  813.                 'name' => $configName,
  814.                 'icon' => isset($configItem['icon']) ? $configItem['icon'] : null,
  815.                 'iconCls' => isset($configItem['iconCls']) ? $configItem['iconCls'] : null
  816.             ];
  817.             if ($user) {
  818.                 $item['active'] = $configName == $currentConfigName;
  819.             }
  820.             $result[] = $item;
  821.         }
  822.         return $result;
  823.     }
  824.     /**
  825.      * @internal
  826.      *
  827.      * @param $runtimeConfig
  828.      * @param $key
  829.      *
  830.      * @return bool
  831.      */
  832.     public static function inPerspective($runtimeConfig$key)
  833.     {
  834.         if (!isset($runtimeConfig['toolbar']) || !$runtimeConfig['toolbar']) {
  835.             return true;
  836.         }
  837.         $parts explode('.'$key);
  838.         $menuItems $runtimeConfig['toolbar'];
  839.         for ($i 0$i count($parts); $i++) {
  840.             $part $parts[$i];
  841.             if (!isset($menuItems[$part])) {
  842.                 break;
  843.             }
  844.             $menuItem $menuItems[$part];
  845.             if (is_array($menuItem)) {
  846.                 if ($menuItem['hidden']) {
  847.                     return false;
  848.                 }
  849.                 if (!$menuItem['items']) {
  850.                     break;
  851.                 }
  852.                 $menuItems $menuItem['items'];
  853.             } else {
  854.                 return $menuItem;
  855.             }
  856.         }
  857.         return true;
  858.     }
  859.     /**
  860.      * @param bool $reset
  861.      * @param string|null $default
  862.      *
  863.      * @return string
  864.      */
  865.     public static function getEnvironment(bool $reset falsestring $default null)
  866.     {
  867.         if (null === static::$environment || $reset) {
  868.             $environment false;
  869.             if (php_sapi_name() === 'cli') {
  870.                 $input = new ArgvInput();
  871.                 $environment $input->getParameterOption(['--env''-e'], nulltrue);
  872.             }
  873.             // check env vars - fall back to default (prod)
  874.             if (!$environment) {
  875.                 foreach (['PIMCORE_ENVIRONMENT''SYMFONY_ENV''APP_ENV'] as $envVarName) {
  876.                     $environment self::resolveEnvVarValue($envVarName);
  877.                     if ($environment) {
  878.                         break;
  879.                     }
  880.                 }
  881.             }
  882.             if (!$environment) {
  883.                 if (null !== $default) {
  884.                     $environment $default;
  885.                 } else {
  886.                     $environment = static::getEnvironmentConfig()->getDefaultEnvironment();
  887.                 }
  888.             }
  889.             static::$environment $environment;
  890.         }
  891.         return static::$environment;
  892.     }
  893.     /**
  894.      * @internal
  895.      *
  896.      * @param string $environment
  897.      */
  898.     public static function setEnvironment($environment)
  899.     {
  900.         static::$environment $environment;
  901.     }
  902.     /**
  903.      * @internal
  904.      *
  905.      * @return EnvironmentConfigInterface
  906.      */
  907.     public static function getEnvironmentConfig(): EnvironmentConfigInterface
  908.     {
  909.         if (null === static::$environmentConfig) {
  910.             static::$environmentConfig = new EnvironmentConfig();
  911.         }
  912.         return static::$environmentConfig;
  913.     }
  914.     /**
  915.      * @internal
  916.      *
  917.      * @param EnvironmentConfigInterface $environmentConfig
  918.      */
  919.     public static function setEnvironmentConfig(EnvironmentConfigInterface $environmentConfig)
  920.     {
  921.         self::$environmentConfig $environmentConfig;
  922.     }
  923.     /**
  924.      * @return mixed
  925.      */
  926.     public static function getFlag($key)
  927.     {
  928.         $config = \Pimcore::getContainer()->getParameter('pimcore.config');
  929.         if (isset($config['flags'])) {
  930.             if (isset($config['flags'][$key])) {
  931.                 return $config['flags'][$key];
  932.             }
  933.         }
  934.         return null;
  935.     }
  936.     /**
  937.      * @internal
  938.      *
  939.      * @param string $file
  940.      * @param bool $asArray
  941.      *
  942.      * @return null|Config\Config
  943.      *
  944.      * @throws \Exception
  945.      */
  946.     public static function getConfigInstance($filebool $asArray false)
  947.     {
  948.         $fileType pathinfo($filePATHINFO_EXTENSION);
  949.         if (file_exists($file)) {
  950.             if ($fileType == 'yml') {
  951.                 $content Yaml::parseFile($file);
  952.             } else {
  953.                 $content = include($file);
  954.             }
  955.             if (is_array($content)) {
  956.                 if ($asArray) {
  957.                     return $content;
  958.                 }
  959.                 return new \Pimcore\Config\Config($content);
  960.             }
  961.         } else {
  962.             throw new \Exception($file " doesn't exist");
  963.         }
  964.         throw new \Exception($file ' is invalid');
  965.     }
  966.     /**
  967.      * @internal
  968.      *
  969.      * @param string $varName
  970.      * @param mixed $default
  971.      *
  972.      * @return string|null
  973.      */
  974.     public static function resolveEnvVarValue(string $varName$default null): ?string
  975.     {
  976.         $value $_SERVER[$varName] ?? $_ENV[$varName] ?? $_SERVER['REDIRECT_' $varName]
  977.             ?? $_ENV['REDIRECT_' $varName] ?? $default;
  978.         return $value;
  979.     }
  980.     /**
  981.      * @internal
  982.      */
  983.     public static function initDebugDevMode()
  984.     {
  985.         if (defined('PIMCORE_CONFIGURATION_DIRECTORY')) {
  986.             $configDir PIMCORE_CONFIGURATION_DIRECTORY;
  987.         } else {
  988.             // this is called via Pimcore::inDebugMode() before the constants get initialized, so we try to get the
  989.             // path from the environment variables (if customized) or we use the default structure
  990.             $privateVar self::resolveEnvVarValue('PIMCORE_PRIVATE_VAR'PIMCORE_PROJECT_ROOT '/var');
  991.             $configDir self::resolveEnvVarValue('PIMCORE_CONFIGURATION_DIRECTORY'$privateVar '/config');
  992.         }
  993.         $debug false;
  994.         $devMode false;
  995.         $debugModeFile $configDir '/debug-mode.php';
  996.         if (file_exists($debugModeFile)) {
  997.             $confTemp = include $debugModeFile;
  998.             if (is_array($confTemp)) {
  999.                 $conf $confTemp;
  1000.                 // init debug mode
  1001.                 if (isset($conf['active'])) {
  1002.                     $debug $conf['active'];
  1003.                     // enable debug mode only for a comma-separated list of IP addresses/ranges
  1004.                     if ($debug && $conf['ip']) {
  1005.                         $debug false;
  1006.                         $clientIp Tool::getClientIp();
  1007.                         if (null !== $clientIp) {
  1008.                             $debugIpAddresses explode_and_trim(','$conf['ip']);
  1009.                             if (IpUtils::checkIp($clientIp$debugIpAddresses)) {
  1010.                                 $debug true;
  1011.                             }
  1012.                         }
  1013.                     }
  1014.                 }
  1015.                 // init dev mode
  1016.                 if ($debug && isset($conf['devmode'])) {
  1017.                     $devMode $conf['devmode'];
  1018.                 }
  1019.             }
  1020.         }
  1021.         if (\Pimcore::getDebugMode() === null) {
  1022.             \Pimcore::setDebugMode($debug);
  1023.             /**
  1024.              * @deprecated
  1025.              */
  1026.             define('PIMCORE_DEBUG'$debug);
  1027.         }
  1028.         if (\Pimcore::getDevMode() === null) {
  1029.             \Pimcore::setDevMode($devMode);
  1030.             /**
  1031.              * @deprecated
  1032.              */
  1033.             define('PIMCORE_DEVMODE'$devMode);
  1034.         }
  1035.     }
  1036. }