Find this useful? Enter your email to receive occasional updates for securing PHP code.

Signing you up...

Thank you for signing up!

PHP Decode

<?php namespace Piwik\Config; use Piwik\Common; use Matomo\Ini\IniReader; use Matomo\Ini\..

Decoded Output download

<?php
 namespace Piwik\Config; use Piwik\Common; use Matomo\Ini\IniReader; use Matomo\Ini\IniReadingException; use Matomo\Ini\IniWriter; use Piwik\Piwik; class IniFileChain { const CONFIG_CACHE_KEY = "config.ini"; protected $settingsChain = array(); protected $mergedSettings = array(); public function __construct(array $defaultSettingsFiles = array(), $userSettingsFile = null) { $this->reload($defaultSettingsFiles, $userSettingsFile); } public function &get($name) { if (!isset($this->mergedSettings[$name])) { $this->mergedSettings[$name] = array(); } $result =& $this->mergedSettings[$name]; return $result; } public function getFrom($file, $name) { return @$this->settingsChain[$file][$name]; } public function set($name, $value) { $name = $this->replaceSectionInvalidChars($name); if ($value !== null) { $value = $this->replaceInvalidChars($value); } $this->mergedSettings[$name] = $value; } public function &getAll() { return $this->mergedSettings; } public function dump($header = '') { return $this->dumpSettings($this->mergedSettings, $header); } public function dumpChanges($header = '') { $userSettingsFile = $this->getUserSettingsFile(); $defaultSettings = $this->getMergedDefaultSettings(); $existingMutableSettings = $this->settingsChain[$userSettingsFile]; $dirty = false; $configToWrite = array(); foreach ($this->mergedSettings as $sectionName => $changedSection) { if (isset($existingMutableSettings[$sectionName])) { $existingMutableSection = $existingMutableSettings[$sectionName]; } else { $existingMutableSection = array(); } if (isset($defaultSettings[$sectionName])) { $changedSection = $this->arrayUnmerge($defaultSettings[$sectionName], $changedSection); $existingMutableSection = $this->arrayUnmerge($defaultSettings[$sectionName], $existingMutableSection); } if (empty($changedSection) xor empty($existingMutableSection) || !empty($changedSection) && !empty($existingMutableSection) && self::compareElements($changedSection, $existingMutableSection)) { $dirty = true; } $configToWrite[$sectionName] = $changedSection; } if ($dirty) { $self = $this; uksort($configToWrite, function ($sectionNameLhs, $sectionNameRhs) use($self) { $lhsIndex = $self->findIndexOfFirstFileWithSection($sectionNameLhs); $rhsIndex = $self->findIndexOfFirstFileWithSection($sectionNameRhs); if ($lhsIndex == $rhsIndex) { $lhsIndexInFile = $self->getIndexOfSectionInFile($lhsIndex, $sectionNameLhs); $rhsIndexInFile = $self->getIndexOfSectionInFile($rhsIndex, $sectionNameRhs); if ($lhsIndexInFile == $rhsIndexInFile) { return 0; } elseif ($lhsIndexInFile < $rhsIndexInFile) { return -1; } else { return 1; } } elseif ($lhsIndex < $rhsIndex) { return -1; } else { return 1; } }); return $this->dumpSettings($configToWrite, $header); } else { return null; } } public function reload($defaultSettingsFiles = array(), $userSettingsFile = null) { if (!empty($defaultSettingsFiles) || !empty($userSettingsFile)) { $this->resetSettingsChain($defaultSettingsFiles, $userSettingsFile); } $hasAbsoluteConfigFile = !empty($userSettingsFile) && strpos($userSettingsFile, DIRECTORY_SEPARATOR) === 0; $useConfigCache = !empty($GLOBALS["ENABLE_CONFIG_PHP_CACHE"]) && $hasAbsoluteConfigFile; if ($useConfigCache && is_file($userSettingsFile)) { $cache = new Cache(); $values = $cache->doFetch(self::CONFIG_CACHE_KEY); if (!empty($values) && isset($values["mergedSettings"]) && isset($values["settingsChain"][$userSettingsFile])) { $this->mergedSettings = $values["mergedSettings"]; $this->settingsChain = $values["settingsChain"]; return; } } $reader = new IniReader(); foreach ($this->settingsChain as $file => $ignore) { if (is_readable($file)) { try { $contents = $reader->readFile($file); $this->settingsChain[$file] = $this->decodeValues($contents); } catch (IniReadingException $ex) { throw new IniReadingException("Unable to read INI file {" . $file . "}: " . $ex->getMessage() . "
 Your host may have disabled parse_ini_file()."); } $this->decodeValues($this->settingsChain[$file]); } } $merged = $this->mergeFileSettings(); $this->mergedSettings = $this->copy($merged); if (!empty($GLOBALS["MATOMO_MODIFY_CONFIG_SETTINGS"]) && !empty($this->mergedSettings)) { $this->mergedSettings = call_user_func($GLOBALS["MATOMO_MODIFY_CONFIG_SETTINGS"], $this->mergedSettings); } if ($useConfigCache && !empty($this->mergedSettings) && !empty($this->settingsChain) && Cache::hasHostConfig($this->mergedSettings)) { $ttlOneHour = 3600; $cache = new Cache(); if ($cache->isValidHost($this->mergedSettings)) { $data = array("mergedSettings" => $this->mergedSettings, "settingsChain" => $this->settingsChain); $cache->doSave(self::CONFIG_CACHE_KEY, $data, $ttlOneHour); } } } public function deleteConfigCache() { if (!empty($GLOBALS["ENABLE_CONFIG_PHP_CACHE"])) { $cache = new Cache(); $cache->doDelete(IniFileChain::CONFIG_CACHE_KEY); } } private function copy($merged) { $copy = array(); foreach ($merged as $index => $value) { if (is_array($value)) { $copy[$index] = $this->copy($value); } else { $copy[$index] = $value; } } return $copy; } private function resetSettingsChain($defaultSettingsFiles, $userSettingsFile) { $this->settingsChain = array(); if (!empty($defaultSettingsFiles)) { foreach ($defaultSettingsFiles as $file) { $this->settingsChain[$file] = null; } } if (!empty($userSettingsFile)) { $this->settingsChain[$userSettingsFile] = null; } } protected function mergeFileSettings() { $mergedSettings = $this->getMergedDefaultSettings(); $userSettings = end($this->settingsChain) ?: array(); foreach ($userSettings as $sectionName => $section) { if (!isset($mergedSettings[$sectionName])) { $mergedSettings[$sectionName] = $section; } else { $mergedSettings[$sectionName] = array_merge($mergedSettings[$sectionName], $section); } } return $mergedSettings; } protected function getMergedDefaultSettings() { $userSettingsFile = $this->getUserSettingsFile(); $mergedSettings = array(); foreach ($this->settingsChain as $file => $settings) { if ($file == $userSettingsFile || empty($settings)) { continue; } foreach ($settings as $sectionName => $section) { if (!isset($mergedSettings[$sectionName])) { $mergedSettings[$sectionName] = $section; } else { $mergedSettings[$sectionName] = $this->array_merge_recursive_distinct($mergedSettings[$sectionName], $section); } } } return $mergedSettings; } protected function getUserSettingsFile() { end($this->settingsChain); return key($this->settingsChain); } public static function compareElements($elem1, $elem2) { if (is_array($elem1)) { if (is_array($elem2)) { return strcmp(serialize($elem1), serialize($elem2)); } return 1; } if (is_array($elem2)) { return -1; } if ((string) $elem1 === (string) $elem2) { return 0; } return (string) $elem1 > (string) $elem2 ? 1 : -1; } public function arrayUnmerge($original, $modified) { if (empty($original) || !is_array($original)) { $original = array(); } if (empty($modified) || !is_array($modified)) { $modified = array(); } return array_udiff_assoc($modified, $original, array(__CLASS__, "compareElements")); } private function array_merge_recursive_distinct(array &$array1, array &$array2) { $merged = $array1; foreach ($array2 as $key => &$value) { if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) { $merged[$key] = $this->array_merge_recursive_distinct($merged[$key], $value); } else { $merged[$key] = $value; } } return $merged; } public function findIndexOfFirstFileWithSection($sectionName) { $count = 0; foreach ($this->settingsChain as $file => $settings) { if (isset($settings[$sectionName])) { break; } ++$count; } return $count; } public function getIndexOfSectionInFile($fileIndex, $sectionName) { reset($this->settingsChain); for ($i = 0; $i != $fileIndex; ++$i) { next($this->settingsChain); } $settingsData = current($this->settingsChain); if (empty($settingsData)) { return -1; } $settingsDataSectionNames = array_keys($settingsData); return array_search($sectionName, $settingsDataSectionNames); } protected function encodeValues(&$values) { if (is_array($values)) { foreach ($values as &$value) { $value = $this->encodeValues($value); } } elseif (is_float($values)) { $values = Common::forceDotAsSeparatorForDecimalPoint($values); } elseif (is_string($values)) { $values = htmlentities($values, ENT_COMPAT, "UTF-8"); $values = str_replace("$", "&#36;", $values); } return $values; } protected function decodeValues(&$values) { if (is_array($values)) { foreach ($values as &$value) { $value = $this->decodeValues($value); } return $values; } elseif (is_string($values)) { return html_entity_decode($values, ENT_COMPAT, "UTF-8"); } return $values; } private function dumpSettings($values, $header) { Piwik::postEvent("Config.beforeSave", array(&$values)); $values = $this->encodeValues($values); $writer = new IniWriter(); return $writer->writeToString($values, $header); } private function replaceInvalidChars($value) { if (is_array($value)) { $result = array(); foreach ($value as $key => $arrayValue) { $key = $this->replaceInvalidChars($key); if (is_array($arrayValue)) { $arrayValue = $this->replaceInvalidChars($arrayValue); } $result[$key] = $arrayValue; } return $result; } else { return preg_replace("/[^a-zA-Z0-9_\[\]-]/", '', $value); } } private function replaceSectionInvalidChars($value) { return preg_replace("/[^a-zA-Z0-9_-]/", '', $value); } } ?>

Did this file decode correctly?

Original Code

<?php
 namespace Piwik\Config; use Piwik\Common; use Matomo\Ini\IniReader; use Matomo\Ini\IniReadingException; use Matomo\Ini\IniWriter; use Piwik\Piwik; class IniFileChain { const CONFIG_CACHE_KEY = "\143\157\156\x66\151\147\56\x69\156\151"; protected $settingsChain = array(); protected $mergedSettings = array(); public function __construct(array $defaultSettingsFiles = array(), $userSettingsFile = null) { $this->reload($defaultSettingsFiles, $userSettingsFile); } public function &get($name) { if (!isset($this->mergedSettings[$name])) { $this->mergedSettings[$name] = array(); } $result =& $this->mergedSettings[$name]; return $result; } public function getFrom($file, $name) { return @$this->settingsChain[$file][$name]; } public function set($name, $value) { $name = $this->replaceSectionInvalidChars($name); if ($value !== null) { $value = $this->replaceInvalidChars($value); } $this->mergedSettings[$name] = $value; } public function &getAll() { return $this->mergedSettings; } public function dump($header = '') { return $this->dumpSettings($this->mergedSettings, $header); } public function dumpChanges($header = '') { $userSettingsFile = $this->getUserSettingsFile(); $defaultSettings = $this->getMergedDefaultSettings(); $existingMutableSettings = $this->settingsChain[$userSettingsFile]; $dirty = false; $configToWrite = array(); foreach ($this->mergedSettings as $sectionName => $changedSection) { if (isset($existingMutableSettings[$sectionName])) { $existingMutableSection = $existingMutableSettings[$sectionName]; } else { $existingMutableSection = array(); } if (isset($defaultSettings[$sectionName])) { $changedSection = $this->arrayUnmerge($defaultSettings[$sectionName], $changedSection); $existingMutableSection = $this->arrayUnmerge($defaultSettings[$sectionName], $existingMutableSection); } if (empty($changedSection) xor empty($existingMutableSection) || !empty($changedSection) && !empty($existingMutableSection) && self::compareElements($changedSection, $existingMutableSection)) { $dirty = true; } $configToWrite[$sectionName] = $changedSection; } if ($dirty) { $self = $this; uksort($configToWrite, function ($sectionNameLhs, $sectionNameRhs) use($self) { $lhsIndex = $self->findIndexOfFirstFileWithSection($sectionNameLhs); $rhsIndex = $self->findIndexOfFirstFileWithSection($sectionNameRhs); if ($lhsIndex == $rhsIndex) { $lhsIndexInFile = $self->getIndexOfSectionInFile($lhsIndex, $sectionNameLhs); $rhsIndexInFile = $self->getIndexOfSectionInFile($rhsIndex, $sectionNameRhs); if ($lhsIndexInFile == $rhsIndexInFile) { return 0; } elseif ($lhsIndexInFile < $rhsIndexInFile) { return -1; } else { return 1; } } elseif ($lhsIndex < $rhsIndex) { return -1; } else { return 1; } }); return $this->dumpSettings($configToWrite, $header); } else { return null; } } public function reload($defaultSettingsFiles = array(), $userSettingsFile = null) { if (!empty($defaultSettingsFiles) || !empty($userSettingsFile)) { $this->resetSettingsChain($defaultSettingsFiles, $userSettingsFile); } $hasAbsoluteConfigFile = !empty($userSettingsFile) && strpos($userSettingsFile, DIRECTORY_SEPARATOR) === 0; $useConfigCache = !empty($GLOBALS["\105\x4e\101\102\114\x45\137\x43\x4f\116\106\111\x47\137\120\x48\x50\x5f\x43\x41\103\110\105"]) && $hasAbsoluteConfigFile; if ($useConfigCache && is_file($userSettingsFile)) { $cache = new Cache(); $values = $cache->doFetch(self::CONFIG_CACHE_KEY); if (!empty($values) && isset($values["\x6d\145\162\147\x65\144\123\145\164\164\151\156\147\x73"]) && isset($values["\163\x65\164\x74\x69\x6e\147\163\103\150\x61\151\x6e"][$userSettingsFile])) { $this->mergedSettings = $values["\155\145\162\147\145\x64\x53\145\x74\164\151\156\x67\x73"]; $this->settingsChain = $values["\163\x65\x74\x74\x69\156\x67\163\x43\x68\141\151\156"]; return; } } $reader = new IniReader(); foreach ($this->settingsChain as $file => $ignore) { if (is_readable($file)) { try { $contents = $reader->readFile($file); $this->settingsChain[$file] = $this->decodeValues($contents); } catch (IniReadingException $ex) { throw new IniReadingException("\125\x6e\x61\142\x6c\145\40\164\x6f\40\x72\145\141\144\x20\111\x4e\111\x20\146\x69\x6c\145\40\x7b" . $file . "\x7d\72\x20" . $ex->getMessage() . "\12\x20\x59\x6f\x75\x72\x20\x68\x6f\x73\164\x20\155\141\x79\40\x68\141\166\145\40\144\x69\163\141\142\154\x65\x64\40\x70\x61\162\x73\x65\137\151\156\x69\137\x66\151\x6c\x65\x28\51\x2e"); } $this->decodeValues($this->settingsChain[$file]); } } $merged = $this->mergeFileSettings(); $this->mergedSettings = $this->copy($merged); if (!empty($GLOBALS["\x4d\101\124\117\x4d\117\137\x4d\x4f\x44\x49\106\x59\x5f\x43\x4f\x4e\x46\111\107\x5f\x53\105\x54\124\x49\116\107\123"]) && !empty($this->mergedSettings)) { $this->mergedSettings = call_user_func($GLOBALS["\115\101\124\x4f\x4d\x4f\137\115\117\104\111\106\131\x5f\x43\x4f\116\106\x49\x47\137\x53\105\x54\x54\111\x4e\107\123"], $this->mergedSettings); } if ($useConfigCache && !empty($this->mergedSettings) && !empty($this->settingsChain) && Cache::hasHostConfig($this->mergedSettings)) { $ttlOneHour = 3600; $cache = new Cache(); if ($cache->isValidHost($this->mergedSettings)) { $data = array("\x6d\145\x72\147\145\x64\x53\x65\x74\164\151\x6e\147\163" => $this->mergedSettings, "\x73\x65\164\164\x69\156\147\163\103\150\141\151\156" => $this->settingsChain); $cache->doSave(self::CONFIG_CACHE_KEY, $data, $ttlOneHour); } } } public function deleteConfigCache() { if (!empty($GLOBALS["\105\116\101\102\114\x45\137\x43\x4f\116\x46\x49\x47\x5f\120\x48\120\x5f\103\101\103\x48\x45"])) { $cache = new Cache(); $cache->doDelete(IniFileChain::CONFIG_CACHE_KEY); } } private function copy($merged) { $copy = array(); foreach ($merged as $index => $value) { if (is_array($value)) { $copy[$index] = $this->copy($value); } else { $copy[$index] = $value; } } return $copy; } private function resetSettingsChain($defaultSettingsFiles, $userSettingsFile) { $this->settingsChain = array(); if (!empty($defaultSettingsFiles)) { foreach ($defaultSettingsFiles as $file) { $this->settingsChain[$file] = null; } } if (!empty($userSettingsFile)) { $this->settingsChain[$userSettingsFile] = null; } } protected function mergeFileSettings() { $mergedSettings = $this->getMergedDefaultSettings(); $userSettings = end($this->settingsChain) ?: array(); foreach ($userSettings as $sectionName => $section) { if (!isset($mergedSettings[$sectionName])) { $mergedSettings[$sectionName] = $section; } else { $mergedSettings[$sectionName] = array_merge($mergedSettings[$sectionName], $section); } } return $mergedSettings; } protected function getMergedDefaultSettings() { $userSettingsFile = $this->getUserSettingsFile(); $mergedSettings = array(); foreach ($this->settingsChain as $file => $settings) { if ($file == $userSettingsFile || empty($settings)) { continue; } foreach ($settings as $sectionName => $section) { if (!isset($mergedSettings[$sectionName])) { $mergedSettings[$sectionName] = $section; } else { $mergedSettings[$sectionName] = $this->array_merge_recursive_distinct($mergedSettings[$sectionName], $section); } } } return $mergedSettings; } protected function getUserSettingsFile() { end($this->settingsChain); return key($this->settingsChain); } public static function compareElements($elem1, $elem2) { if (is_array($elem1)) { if (is_array($elem2)) { return strcmp(serialize($elem1), serialize($elem2)); } return 1; } if (is_array($elem2)) { return -1; } if ((string) $elem1 === (string) $elem2) { return 0; } return (string) $elem1 > (string) $elem2 ? 1 : -1; } public function arrayUnmerge($original, $modified) { if (empty($original) || !is_array($original)) { $original = array(); } if (empty($modified) || !is_array($modified)) { $modified = array(); } return array_udiff_assoc($modified, $original, array(__CLASS__, "\x63\x6f\x6d\x70\x61\x72\145\105\154\145\155\x65\156\x74\163")); } private function array_merge_recursive_distinct(array &$array1, array &$array2) { $merged = $array1; foreach ($array2 as $key => &$value) { if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) { $merged[$key] = $this->array_merge_recursive_distinct($merged[$key], $value); } else { $merged[$key] = $value; } } return $merged; } public function findIndexOfFirstFileWithSection($sectionName) { $count = 0; foreach ($this->settingsChain as $file => $settings) { if (isset($settings[$sectionName])) { break; } ++$count; } return $count; } public function getIndexOfSectionInFile($fileIndex, $sectionName) { reset($this->settingsChain); for ($i = 0; $i != $fileIndex; ++$i) { next($this->settingsChain); } $settingsData = current($this->settingsChain); if (empty($settingsData)) { return -1; } $settingsDataSectionNames = array_keys($settingsData); return array_search($sectionName, $settingsDataSectionNames); } protected function encodeValues(&$values) { if (is_array($values)) { foreach ($values as &$value) { $value = $this->encodeValues($value); } } elseif (is_float($values)) { $values = Common::forceDotAsSeparatorForDecimalPoint($values); } elseif (is_string($values)) { $values = htmlentities($values, ENT_COMPAT, "\125\124\106\x2d\70"); $values = str_replace("\x24", "\x26\x23\63\x36\73", $values); } return $values; } protected function decodeValues(&$values) { if (is_array($values)) { foreach ($values as &$value) { $value = $this->decodeValues($value); } return $values; } elseif (is_string($values)) { return html_entity_decode($values, ENT_COMPAT, "\125\124\x46\55\x38"); } return $values; } private function dumpSettings($values, $header) { Piwik::postEvent("\x43\x6f\156\146\151\147\56\142\x65\146\x6f\162\x65\x53\x61\166\x65", array(&$values)); $values = $this->encodeValues($values); $writer = new IniWriter(); return $writer->writeToString($values, $header); } private function replaceInvalidChars($value) { if (is_array($value)) { $result = array(); foreach ($value as $key => $arrayValue) { $key = $this->replaceInvalidChars($key); if (is_array($arrayValue)) { $arrayValue = $this->replaceInvalidChars($arrayValue); } $result[$key] = $arrayValue; } return $result; } else { return preg_replace("\57\133\x5e\141\x2d\x7a\101\x2d\x5a\x30\x2d\71\x5f\x5c\x5b\134\x5d\55\x5d\x2f", '', $value); } } private function replaceSectionInvalidChars($value) { return preg_replace("\x2f\133\x5e\141\x2d\x7a\x41\x2d\x5a\x30\x2d\71\137\55\135\x2f", '', $value); } }

Function Calls

None

Variables

None

Stats

MD5 8982a0ebf89ba3121216bfb1c40d3d98
Eval Count 0
Decode Time 99 ms