/libraries/joomla/registry/format/ini.php

https://github.com/bolis97/joomla-cms · PHP · 216 lines · 111 code · 27 blank · 78 comment · 31 complexity · 98db9bb11bfe143569613d0df5d2679f MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Registry
  5. *
  6. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. /**
  11. * INI format handler for JRegistry.
  12. *
  13. * @package Joomla.Platform
  14. * @subpackage Registry
  15. * @since 11.1
  16. */
  17. class JRegistryFormatINI extends JRegistryFormat
  18. {
  19. protected static $cache = array();
  20. /**
  21. * Converts an object into an INI formatted string
  22. * - Unfortunately, there is no way to have ini values nested further than two
  23. * levels deep. Therefore we will only go through the first two levels of
  24. * the object.
  25. *
  26. * @param object $object Data source object.
  27. * @param array $options Options used by the formatter.
  28. *
  29. * @return string INI formatted string.
  30. *
  31. * @since 11.1
  32. */
  33. public function objectToString($object, $options = array())
  34. {
  35. // Initialize variables.
  36. $local = array();
  37. $global = array();
  38. // Iterate over the object to set the properties.
  39. foreach (get_object_vars($object) as $key => $value)
  40. {
  41. // If the value is an object then we need to put it in a local section.
  42. if (is_object($value)) {
  43. // Add the section line.
  44. $local[] = '';
  45. $local[] = '['.$key.']';
  46. // Add the properties for this section.
  47. foreach (get_object_vars($value) as $k => $v)
  48. {
  49. $local[] = $k.'='.$this->getValueAsINI($v);
  50. }
  51. } else {
  52. // Not in a section so add the property to the global array.
  53. $global[] = $key.'='.$this->getValueAsINI($value);
  54. }
  55. }
  56. return implode("\n", array_merge($global, $local));
  57. }
  58. /**
  59. * Parse an INI formatted string and convert it into an object.
  60. *
  61. * @param string $data INI formatted string to convert.
  62. * @param mixed $options An array of options used by the formatter, or a boolean setting to process sections.
  63. *
  64. * @return object Data object.
  65. *
  66. * @since 11.1
  67. */
  68. public function stringToObject($data, $options = array())
  69. {
  70. // Initialise options.
  71. if (is_array($options)) {
  72. $sections = (isset($options['processSections'])) ? $options['processSections'] : false;
  73. } else {
  74. // Backward compatibility for 1.5 usage.
  75. //@deprecated
  76. $sections = (boolean) $options;
  77. }
  78. // Check the memory cache for already processed strings.
  79. $hash = md5($data.':'.(int) $sections);
  80. if (isset(self::$cache[$hash])) {
  81. return self::$cache[$hash];
  82. }
  83. // If no lines present just return the object.
  84. if (empty($data)) {
  85. return new stdClass;
  86. }
  87. // Initialize variables.
  88. $obj = new stdClass;
  89. $section = false;
  90. $lines = explode("\n", $data);
  91. // Process the lines.
  92. foreach ($lines as $line)
  93. {
  94. // Trim any unnecessary whitespace.
  95. $line = trim($line);
  96. // Ignore empty lines and comments.
  97. if (empty($line) || ($line{0} == ';')) {
  98. continue;
  99. }
  100. if ($sections) {
  101. $length = strlen($line);
  102. // If we are processing sections and the line is a section add the object and continue.
  103. if (($line[0] == '[') && ($line[$length-1] == ']')) {
  104. $section = substr($line, 1, $length-2);
  105. $obj->$section = new stdClass;
  106. continue;
  107. }
  108. } elseif ($line{0} == '[') {
  109. continue;
  110. }
  111. // Check that an equal sign exists and is not the first character of the line.
  112. if (!strpos($line, '=')) {
  113. // Maybe throw exception?
  114. continue;
  115. }
  116. // Get the key and value for the line.
  117. list($key, $value) = explode('=', $line, 2);
  118. // Validate the key.
  119. if (preg_match('/[^A-Z0-9_]/i', $key)) {
  120. // Maybe throw exception?
  121. continue;
  122. }
  123. // If the value is quoted then we assume it is a string.
  124. $length = strlen($value);
  125. if ($length && ($value[0] == '"') && ($value[$length-1] == '"')) {
  126. // Strip the quotes and Convert the new line characters.
  127. $value = stripcslashes(substr($value, 1, ($length-2)));
  128. $value = str_replace('\n', "\n", $value);
  129. } else {
  130. // If the value is not quoted, we assume it is not a string.
  131. // If the value is 'false' assume boolean false.
  132. if ($value == 'false') {
  133. $value = false;
  134. }
  135. // If the value is 'true' assume boolean true.
  136. elseif ($value == 'true') {
  137. $value = true;
  138. }
  139. // If the value is numeric than it is either a float or int.
  140. elseif (is_numeric($value)) {
  141. // If there is a period then we assume a float.
  142. if (strpos($value, '.') !== false) {
  143. $value = (float) $value;
  144. }
  145. else {
  146. $value = (int) $value;
  147. }
  148. }
  149. }
  150. // If a section is set add the key/value to the section, otherwise top level.
  151. if ($section) {
  152. $obj->$section->$key = $value;
  153. } else {
  154. $obj->$key = $value;
  155. }
  156. }
  157. // Cache the string to save cpu cycles -- thus the world :)
  158. self::$cache[$hash] = clone($obj);
  159. return $obj;
  160. }
  161. /**
  162. * Method to get a value in an INI format.
  163. *
  164. * @param mixed $value The value to convert to INI format.
  165. *
  166. * @return string The value in INI format.
  167. *
  168. * @since 11.1
  169. */
  170. protected function getValueAsINI($value)
  171. {
  172. // Initialize variables.
  173. $string = '';
  174. switch (gettype($value)) {
  175. case 'integer':
  176. case 'double':
  177. $string = $value;
  178. break;
  179. case 'boolean':
  180. $string = $value ? 'true' : 'false';
  181. break;
  182. case 'string':
  183. // Sanitize any CRLF characters..
  184. $string = '"'.str_replace(array("\r\n", "\n"), '\\n', $value).'"';
  185. break;
  186. }
  187. return $string;
  188. }
  189. }