Base for a static organization website

IniReader.php 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. /**
  3. * IniReader
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @package Cake.Configure
  15. * @since CakePHP(tm) v 2.0
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('Hash', 'Utility');
  19. App::uses('CakePlugin', 'Core');
  20. /**
  21. * Ini file configuration engine.
  22. *
  23. * Since IniReader uses parse_ini_file underneath, you should be aware that this
  24. * class shares the same behavior, especially with regards to boolean and null values.
  25. *
  26. * In addition to the native `parse_ini_file` features, IniReader also allows you
  27. * to create nested array structures through usage of `.` delimited names. This allows
  28. * you to create nested arrays structures in an ini config file. For example:
  29. *
  30. * `db.password = secret` would turn into `array('db' => array('password' => 'secret'))`
  31. *
  32. * You can nest properties as deeply as needed using `.`'s. In addition to using `.` you
  33. * can use standard ini section notation to create nested structures:
  34. *
  35. * ```
  36. * [section]
  37. * key = value
  38. * ```
  39. *
  40. * Once loaded into Configure, the above would be accessed using:
  41. *
  42. * `Configure::read('section.key');
  43. *
  44. * You can combine `.` separated values with sections to create more deeply
  45. * nested structures.
  46. *
  47. * IniReader also manipulates how the special ini values of
  48. * 'yes', 'no', 'on', 'off', 'null' are handled. These values will be
  49. * converted to their boolean equivalents.
  50. *
  51. * @package Cake.Configure
  52. * @see http://php.net/parse_ini_file
  53. */
  54. class IniReader implements ConfigReaderInterface {
  55. /**
  56. * The path to read ini files from.
  57. *
  58. * @var array
  59. */
  60. protected $_path;
  61. /**
  62. * The section to read, if null all sections will be read.
  63. *
  64. * @var string
  65. */
  66. protected $_section;
  67. /**
  68. * Build and construct a new ini file parser. The parser can be used to read
  69. * ini files that are on the filesystem.
  70. *
  71. * @param string $path Path to load ini config files from. Defaults to APP . 'Config' . DS
  72. * @param string $section Only get one section, leave null to parse and fetch
  73. * all sections in the ini file.
  74. */
  75. public function __construct($path = null, $section = null) {
  76. if (!$path) {
  77. $path = APP . 'Config' . DS;
  78. }
  79. $this->_path = $path;
  80. $this->_section = $section;
  81. }
  82. /**
  83. * Read an ini file and return the results as an array.
  84. *
  85. * For backwards compatibility, acl.ini.php will be treated specially until 3.0.
  86. *
  87. * @param string $key The identifier to read from. If the key has a . it will be treated
  88. * as a plugin prefix. The chosen file must be on the reader's path.
  89. * @return array Parsed configuration values.
  90. * @throws ConfigureException when files don't exist.
  91. * Or when files contain '..' as this could lead to abusive reads.
  92. */
  93. public function read($key) {
  94. if (strpos($key, '..') !== false) {
  95. throw new ConfigureException(__d('cake_dev', 'Cannot load configuration files with ../ in them.'));
  96. }
  97. $file = $this->_getFilePath($key);
  98. if (!is_file($file)) {
  99. throw new ConfigureException(__d('cake_dev', 'Could not load configuration file: %s', $file));
  100. }
  101. $contents = parse_ini_file($file, true);
  102. if (!empty($this->_section) && isset($contents[$this->_section])) {
  103. $values = $this->_parseNestedValues($contents[$this->_section]);
  104. } else {
  105. $values = array();
  106. foreach ($contents as $section => $attribs) {
  107. if (is_array($attribs)) {
  108. $values[$section] = $this->_parseNestedValues($attribs);
  109. } else {
  110. $parse = $this->_parseNestedValues(array($attribs));
  111. $values[$section] = array_shift($parse);
  112. }
  113. }
  114. }
  115. return $values;
  116. }
  117. /**
  118. * parses nested values out of keys.
  119. *
  120. * @param array $values Values to be exploded.
  121. * @return array Array of values exploded
  122. */
  123. protected function _parseNestedValues($values) {
  124. foreach ($values as $key => $value) {
  125. if ($value === '1') {
  126. $value = true;
  127. }
  128. if ($value === '') {
  129. $value = false;
  130. }
  131. unset($values[$key]);
  132. if (strpos($key, '.') !== false) {
  133. $values = Hash::insert($values, $key, $value);
  134. } else {
  135. $values[$key] = $value;
  136. }
  137. }
  138. return $values;
  139. }
  140. /**
  141. * Dumps the state of Configure data into an ini formatted string.
  142. *
  143. * @param string $key The identifier to write to. If the key has a . it will be treated
  144. * as a plugin prefix.
  145. * @param array $data The data to convert to ini file.
  146. * @return int Bytes saved.
  147. */
  148. public function dump($key, $data) {
  149. $result = array();
  150. foreach ($data as $k => $value) {
  151. $isSection = false;
  152. if ($k[0] !== '[') {
  153. $result[] = "[$k]";
  154. $isSection = true;
  155. }
  156. if (is_array($value)) {
  157. $kValues = Hash::flatten($value, '.');
  158. foreach ($kValues as $k2 => $v) {
  159. $result[] = "$k2 = " . $this->_value($v);
  160. }
  161. }
  162. if ($isSection) {
  163. $result[] = '';
  164. }
  165. }
  166. $contents = trim(implode("\n", $result));
  167. $filename = $this->_getFilePath($key);
  168. return file_put_contents($filename, $contents);
  169. }
  170. /**
  171. * Converts a value into the ini equivalent
  172. *
  173. * @param mixed $val Value to export.
  174. * @return string String value for ini file.
  175. */
  176. protected function _value($val) {
  177. if ($val === null) {
  178. return 'null';
  179. }
  180. if ($val === true) {
  181. return 'true';
  182. }
  183. if ($val === false) {
  184. return 'false';
  185. }
  186. return (string)$val;
  187. }
  188. /**
  189. * Get file path
  190. *
  191. * @param string $key The identifier to write to. If the key has a . it will be treated
  192. * as a plugin prefix.
  193. * @return string Full file path
  194. */
  195. protected function _getFilePath($key) {
  196. if (substr($key, -8) === '.ini.php') {
  197. $key = substr($key, 0, -8);
  198. list($plugin, $key) = pluginSplit($key);
  199. $key .= '.ini.php';
  200. } else {
  201. if (substr($key, -4) === '.ini') {
  202. $key = substr($key, 0, -4);
  203. }
  204. list($plugin, $key) = pluginSplit($key);
  205. $key .= '.ini';
  206. }
  207. if ($plugin) {
  208. $file = CakePlugin::path($plugin) . 'Config' . DS . $key;
  209. } else {
  210. $file = $this->_path . $key;
  211. }
  212. return $file;
  213. }
  214. }