1: <?php
2: /**
3: * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4: * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
5: *
6: * Licensed under The MIT License
7: * For full copyright and license information, please see the LICENSE.txt
8: * Redistributions of files must retain the above copyright notice.
9: *
10: * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
11: * @link https://cakephp.org CakePHP(tm) Project
12: * @since 3.0.0
13: * @license https://opensource.org/licenses/mit-license.php MIT License
14: */
15: namespace Cake\Core\Configure;
16:
17: use Cake\Core\Exception\Exception;
18: use Cake\Core\Plugin;
19:
20: /**
21: * Trait providing utility methods for file based config engines.
22: */
23: trait FileConfigTrait
24: {
25: /**
26: * The path this engine finds files on.
27: *
28: * @var string
29: */
30: protected $_path = '';
31:
32: /**
33: * Get file path
34: *
35: * @param string $key The identifier to write to. If the key has a . it will be treated
36: * as a plugin prefix.
37: * @param bool $checkExists Whether to check if file exists. Defaults to false.
38: * @return string Full file path
39: * @throws \Cake\Core\Exception\Exception When files don't exist or when
40: * files contain '..' as this could lead to abusive reads.
41: */
42: protected function _getFilePath($key, $checkExists = false)
43: {
44: if (strpos($key, '..') !== false) {
45: throw new Exception('Cannot load/dump configuration files with ../ in them.');
46: }
47:
48: list($plugin, $key) = pluginSplit($key);
49:
50: if ($plugin) {
51: $file = Plugin::configPath($plugin) . $key;
52: } else {
53: $file = $this->_path . $key;
54: }
55:
56: $file .= $this->_extension;
57:
58: if (!$checkExists || is_file($file)) {
59: return $file;
60: }
61:
62: $realPath = realpath($file);
63: if ($realPath !== false && is_file($realPath)) {
64: return $realPath;
65: }
66:
67: throw new Exception(sprintf('Could not load configuration file: %s', $file));
68: }
69: }
70: