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: /**
27: * The path this engine finds files on.
28: *
29: * @var string
30: */
31: protected $_path = '';
32:
33: /**
34: * Get file path
35: *
36: * @param string $key The identifier to write to. If the key has a . it will be treated
37: * as a plugin prefix.
38: * @param bool $checkExists Whether to check if file exists. Defaults to false.
39: * @return string Full file path
40: * @throws \Cake\Core\Exception\Exception When files don't exist or when
41: * files contain '..' as this could lead to abusive reads.
42: */
43: protected function _getFilePath($key, $checkExists = false)
44: {
45: if (strpos($key, '..') !== false) {
46: throw new Exception('Cannot load/dump configuration files with ../ in them.');
47: }
48:
49: list($plugin, $key) = pluginSplit($key);
50:
51: if ($plugin) {
52: $file = Plugin::configPath($plugin) . $key;
53: } else {
54: $file = $this->_path . $key;
55: }
56:
57: $file .= $this->_extension;
58:
59: if (!$checkExists || is_file($file)) {
60: return $file;
61: }
62:
63: $realPath = realpath($file);
64: if ($realPath !== false && is_file($realPath)) {
65: return $realPath;
66: }
67:
68: throw new Exception(sprintf('Could not load configuration file: %s', $file));
69: }
70: }
71: