Browse Source

Make code DRYer.

ADmad 11 years ago
parent
commit
fe545c579e

+ 1 - 8
src/Core/Configure/Engine/IniConfig.php

@@ -100,14 +100,7 @@ class IniConfig implements ConfigEngineInterface
      */
     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])) {

+ 1 - 8
src/Core/Configure/Engine/PhpConfig.php

@@ -65,14 +65,7 @@ class PhpConfig implements ConfigEngineInterface
      */
     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)) {

+ 16 - 3
src/Core/Configure/FileConfigTrait.php

@@ -14,6 +14,7 @@
  */
 namespace Cake\Core\Configure;
 
+use Cake\Core\Exception\Exception;
 use Cake\Core\Plugin;
 
 /**
@@ -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) {
@@ -46,6 +53,12 @@ trait FileConfigTrait
             $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;
     }
 }