Skip to content

Commit

Permalink
Make code DRYer.
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Jan 4, 2015
1 parent 4d75585 commit fe545c5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
9 changes: 1 addition & 8 deletions src/Core/Configure/Engine/IniConfig.php
Expand Up @@ -100,14 +100,7 @@ public function __construct($path = null, $section = null)
*/
public function read($key)
{
if (strpos($key, '..') !== false) {
throw new Exception('Cannot load configuration files with ../ in them.');
}

$file = $this->_getFilePath($key);
if (!is_file($file)) {
throw new Exception(sprintf('Could not load configuration file: %s', $file));
}
$file = $this->_getFilePath($key, true);

$contents = parse_ini_file($file, true);
if (!empty($this->_section) && isset($contents[$this->_section])) {
Expand Down
9 changes: 1 addition & 8 deletions src/Core/Configure/Engine/PhpConfig.php
Expand Up @@ -65,14 +65,7 @@ public function __construct($path = null)
*/
public function read($key)
{
if (strpos($key, '..') !== false) {
throw new Exception('Cannot load configuration files with ../ in them.');
}

$file = $this->_getFilePath($key);
if (!is_file($file)) {
throw new Exception(sprintf('Could not load configuration file: %s', $file));
}
$file = $this->_getFilePath($key, true);

include $file;
if (!isset($config)) {
Expand Down
19 changes: 16 additions & 3 deletions src/Core/Configure/FileConfigTrait.php
Expand Up @@ -14,6 +14,7 @@
*/
namespace Cake\Core\Configure;

use Cake\Core\Exception\Exception;
use Cake\Core\Plugin;

/**
Expand All @@ -33,11 +34,17 @@ trait FileConfigTrait
*
* @param string $key The identifier to write to. If the key has a . it will be treated
* as a plugin prefix.
* @param string $ext File extension.
* @param bool $checkExists Whether to check if file exists. Defaults to false.
* @return string Full file path
* @throws \Cake\Core\Exception\Exception When files don't exist or when
* files contain '..' as this could lead to abusive reads.
*/
protected function _getFilePath($key)
protected function _getFilePath($key, $checkExists = false)
{
if (strpos($key, '..') !== false) {
throw new Exception('Cannot load/dump configuration files with ../ in them.');
}

list($plugin, $key) = pluginSplit($key);

if ($plugin) {
Expand All @@ -46,6 +53,12 @@ protected function _getFilePath($key)
$file = $this->_path . $key;
}

return $file . $this->_extension;
$file .= $this->_extension;

if ($checkExists && !is_file($file)) {
throw new Exception(sprintf('Could not load configuration file: %s', $file));
}

return $file;
}
}

0 comments on commit fe545c5

Please sign in to comment.