1<?php 2/** 3 * Matomo - free/libre analytics platform 4 * 5 * @link https://matomo.org 6 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later 7 * 8 */ 9namespace Piwik; 10 11use Piwik\Container\StaticContainer; 12use Piwik\Plugin\Manager; 13 14class CacheId 15{ 16 public static function languageAware($cacheId) 17 { 18 return $cacheId . '-' . StaticContainer::get('Piwik\Translation\Translator')->getCurrentLanguage(); 19 } 20 21 public static function pluginAware($cacheId) 22 { 23 $pluginManager = Manager::getInstance(); 24 $pluginNames = $pluginManager->getLoadedPluginsName(); 25 $cacheId = $cacheId . '-' . md5(implode('', $pluginNames)); 26 $cacheId = self::languageAware($cacheId); 27 28 return $cacheId; 29 } 30 31 public static function siteAware($cacheId, array $idSites = null) 32 { 33 if ($idSites === null) { 34 $idSites = self::getIdSiteList('idSite'); 35 $cacheId .= self::idSiteListCacheKey($idSites); 36 37 $idSites = self::getIdSiteList('idSites'); 38 $cacheId .= self::idSiteListCacheKey($idSites); 39 40 $idSites = self::getIdSiteList('idsite'); // tracker param 41 $cacheId .= self::idSiteListCacheKey($idSites); 42 } else { 43 $cacheId .= self::idSiteListCacheKey($idSites); 44 } 45 46 return $cacheId; 47 } 48 49 private static function getIdSiteList($queryParamName) 50 { 51 if (empty($_GET[$queryParamName]) 52 && empty($_POST[$queryParamName]) 53 ) { 54 return []; 55 } 56 57 $idSiteGetParam = []; 58 if (!empty($_GET[$queryParamName])) { 59 $value = $_GET[$queryParamName]; 60 $idSiteGetParam = is_array($value) ? $value : explode(',', $value); 61 } 62 63 $idSitePostParam = []; 64 if (!empty($_POST[$queryParamName])) { 65 $value = $_POST[$queryParamName]; 66 $idSitePostParam = is_array($value) ? $value : explode(',', $value); 67 } 68 69 $idSiteList = array_merge($idSiteGetParam, $idSitePostParam); 70 $idSiteList = array_map('intval', $idSiteList); 71 $idSiteList = array_unique($idSiteList); 72 sort($idSiteList); 73 return $idSiteList; 74 } 75 76 private static function idSiteListCacheKey($idSites) 77 { 78 if (empty($idSites)) { 79 return ''; 80 } 81 82 if (count($idSites) <= 5) { 83 return '-' . implode('_', $idSites); // we keep the cache key readable when possible 84 } else { 85 return '-' . md5(implode('_', $idSites)); // we need to shorten it 86 } 87 } 88} 89