Skip to content
Extraits de code Groupes Projets
Valider ab95aee4 rédigé par diosmosis's avatar diosmosis
Parcourir les fichiers

Encode/decode INI properties in IniFileChain since the encoding/decoding is...

Encode/decode INI properties in IniFileChain since the encoding/decoding is used to avoid issues w/ parse_ini_file() use.
parent ff8a24c8
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -334,9 +334,6 @@ class Config extends Singleton ...@@ -334,9 +334,6 @@ class Config extends Singleton
} }
} }
// decode section datas
$this->decodeValues($this->settings->getAll());
// Check config.ini.php last // Check config.ini.php last
if (!$inTrackerRequest) { if (!$inTrackerRequest) {
$this->checkLocalConfigFound(); $this->checkLocalConfigFound();
...@@ -361,46 +358,6 @@ class Config extends Singleton ...@@ -361,46 +358,6 @@ class Config extends Singleton
} }
} }
/**
* Decode HTML entities
*
* @param mixed $values
* @return mixed
*/
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;
}
/**
* Encode HTML entities
*
* @param mixed $values
* @return mixed
*/
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('$', '$', $values);
}
return $values;
}
/** /**
* Returns a configuration value or section by name. * Returns a configuration value or section by name.
* *
...@@ -455,21 +412,9 @@ class Config extends Singleton ...@@ -455,21 +412,9 @@ class Config extends Singleton
*/ */
public function dumpConfig() public function dumpConfig()
{ {
$this->encodeValues($this->settings->getAll()); $header = "; <?php exit; ?> DO NOT REMOVE THIS LINE\n";
$header .= "; file automatically generated or modified by Piwik; you can manually override the default values in global.ini.php by redefining them in this file.\n";
try { return $this->settings->dumpChanges($header);
$header = "; <?php exit; ?> DO NOT REMOVE THIS LINE\n";
$header .= "; file automatically generated or modified by Piwik; you can manually override the default values in global.ini.php by redefining them in this file.\n";
$dumpedString = $this->settings->dumpChanges($header);
$this->decodeValues($this->settings->getAll());
} catch (Exception $ex) {
$this->decodeValues($this->settings->getAll());
throw $ex;
}
return $dumpedString;
} }
/** /**
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
*/ */
namespace Piwik\Config; namespace Piwik\Config;
use Piwik\Common;
use Piwik\Ini\IniReader; use Piwik\Ini\IniReader;
use Piwik\Ini\IniReadingException; use Piwik\Ini\IniReadingException;
use Piwik\Ini\IniWriter; use Piwik\Ini\IniWriter;
...@@ -113,8 +114,7 @@ class IniFileChain ...@@ -113,8 +114,7 @@ class IniFileChain
*/ */
public function dump($header = '') public function dump($header = '')
{ {
$writer = new IniWriter(); return $this->dumpSettings($this->mergedSettings, $header);
return $writer->writeToString($this->mergedSettings, $header);
} }
/** /**
...@@ -184,8 +184,7 @@ class IniFileChain ...@@ -184,8 +184,7 @@ class IniFileChain
} }
}); });
$writer = new IniWriter(); return $this->dumpSettings($configToWrite, $header);
return $writer->writeToString($configToWrite, $header);
} else { } else {
return null; return null;
} }
...@@ -206,7 +205,8 @@ class IniFileChain ...@@ -206,7 +205,8 @@ class IniFileChain
foreach ($this->settingsChain as $file => $ignore) { foreach ($this->settingsChain as $file => $ignore) {
if (is_readable($file)) { if (is_readable($file)) {
try { try {
$this->settingsChain[$file] = $reader->readFile($file); $contents = $reader->readFile($file);
$this->settingsChain[$file] = $this->decodeValues($contents);
} catch (IniReadingException $ex) { } catch (IniReadingException $ex) {
$message = Piwik::translate('General_ExceptionUnreadableFileDisabledMethod', array($file, "parse_ini_file()")); $message = Piwik::translate('General_ExceptionUnreadableFileDisabledMethod', array($file, "parse_ini_file()"));
throw new IniReadingException($message, $code = 0, $ex); throw new IniReadingException($message, $code = 0, $ex);
...@@ -399,4 +399,52 @@ class IniFileChain ...@@ -399,4 +399,52 @@ class IniFileChain
return array_search($sectionName, $settingsDataSectionNames); return array_search($sectionName, $settingsDataSectionNames);
} }
/**
* Encode HTML entities
*
* @param mixed $values
* @return mixed
*/
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;
}
/**
* Decode HTML entities
*
* @param mixed $values
* @return mixed
*/
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)
{
$values = $this->encodeValues($values);
$writer = new IniWriter();
return $writer->writeToString($values, $header);
}
} }
\ No newline at end of file
...@@ -141,7 +141,7 @@ class IniFileChainTest extends PHPUnit_Framework_TestCase ...@@ -141,7 +141,7 @@ class IniFileChainTest extends PHPUnit_Framework_TestCase
) )
), ),
'Section2' => array( 'Section2' => array(
'var4' => 'value5' 'var4' => 'val$ue5'
) )
) )
), ),
...@@ -160,7 +160,7 @@ class IniFileChainTest extends PHPUnit_Framework_TestCase ...@@ -160,7 +160,7 @@ class IniFileChainTest extends PHPUnit_Framework_TestCase
) )
), ),
'Section2' => array( 'Section2' => array(
'var4' => 'value5' 'var4' => 'val$ue5'
) )
) )
) )
...@@ -194,7 +194,7 @@ class IniFileChainTest extends PHPUnit_Framework_TestCase ...@@ -194,7 +194,7 @@ class IniFileChainTest extends PHPUnit_Framework_TestCase
$data =& $fileChain->get('Section1'); $data =& $fileChain->get('Section1');
$this->assertEquals(array('var1' => 'value2', 'var3' => array('value3', 'value4')), $data); $this->assertEquals(array('var1' => 'val"ue2', 'var3' => array('value3', 'value4')), $data);
$data['var1'] = 'changed'; $data['var1'] = 'changed';
$data['var3'][] = 'newValue'; $data['var3'][] = 'newValue';
...@@ -241,7 +241,17 @@ class IniFileChainTest extends PHPUnit_Framework_TestCase ...@@ -241,7 +241,17 @@ class IniFileChainTest extends PHPUnit_Framework_TestCase
__DIR__ . '/test_files/default_settings_2.ini.php' __DIR__ . '/test_files/default_settings_2.ini.php'
); );
$this->assertEquals(array('var1' => 'value2', 'var3' => array('value3', 'value4')), $fileChain->getFrom($defaultSettingsPath, 'Section1')); $this->assertEquals(array('var1' => 'val"ue2', 'var3' => array('value3', 'value4')), $fileChain->getFrom($defaultSettingsPath, 'Section1'));
}
public function test_getFrom_CorrectlyReturnsUnencodedValue()
{
$userSettingsPath = __DIR__ . '/test_files/special_values.ini.php';
$fileChain = new IniFileChain(array(), $userSettingsPath);
$this->assertEquals(array(
'value1' => 'a"bc', 'value2' => array('<script>', '${@piwik(crash))}'
)), $fileChain->getFrom($userSettingsPath, 'Section'));
} }
public function getTestDataForDumpTest() public function getTestDataForDumpTest()
...@@ -253,7 +263,7 @@ class IniFileChainTest extends PHPUnit_Framework_TestCase ...@@ -253,7 +263,7 @@ class IniFileChainTest extends PHPUnit_Framework_TestCase
), ),
__DIR__ . '/test_files/default_settings_2.ini.php', // user settings __DIR__ . '/test_files/default_settings_2.ini.php', // user settings
"; some header\n", "; some header\n",
"; some header\n[Section1]\nvar1 = \"overriddenValue1\"\nvar3[] = \"overriddenValue2\"\nvar3[] = \"overriddenValue3\"\n\n[Section2]\nvar4 = \"value5\"\n\n", "; some header\n[Section1]\nvar1 = \"overriddenValue1\"\nvar3[] = \"overriddenValue2\"\nvar3[] = \"overriddenValue3\"\n\n[Section2]\nvar4 = \"val&#36;ue5\"\n\n",
"; some header\n[Section1]\nvar1 = \"overriddenValue1\"\nvar3[] = \"overriddenValue2\"\nvar3[] = \"overriddenValue3\"\n\n" "; some header\n[Section1]\nvar1 = \"overriddenValue1\"\nvar3[] = \"overriddenValue2\"\nvar3[] = \"overriddenValue3\"\n\n"
) )
); );
......
[Section1] [Section1]
var1 = "value2" var1 = "val&quot;ue2"
var3[] = "value3" var3[] = "value3"
var3[] = "value4" var3[] = "value4"
[Section2] [Section2]
var4 = "value5" var4 = "val&#36;ue5"
\ No newline at end of file \ No newline at end of file
...@@ -145,7 +145,7 @@ class ConfigTest extends PHPUnit_Framework_TestCase ...@@ -145,7 +145,7 @@ class ConfigTest extends PHPUnit_Framework_TestCase
/** /**
* Dataprovider for testDumpConfig * Dataprovider for testDumpConfig
*/ $ */
public function getDumpConfigData() public function getDumpConfigData()
{ {
$header = "; <?php exit; ?> DO NOT REMOVE THIS LINE\n" . $header = "; <?php exit; ?> DO NOT REMOVE THIS LINE\n" .
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter