PageRenderTime 54ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/joomla/joomla-platform/
PHP | 209 lines | 108 code | 27 blank | 74 comment | 33 complexity | 0ba7c70b55dd82b015b01b675a59185f MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause
  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. private 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 Data source object.
  27. * @param array Options used by the formatter.
  28. * @return string INI formatted string.
  29. * @since 11.1
  30. */
  31. public function objectToString($object, $options = array())
  32. {
  33. // Initialize variables.
  34. $local = array();
  35. $global = array();
  36. // Iterate over the object to set the properties.
  37. foreach (get_object_vars($object) as $key => $value) {
  38. // If the value is an object then we need to put it in a local section.
  39. if (is_object($value)) {
  40. // Add the section line.
  41. $local[] = '';
  42. $local[] = '['.$key.']';
  43. // Add the properties for this section.
  44. foreach (get_object_vars($value) as $k => $v) {
  45. $local[] = $k.'='.$this->_getValueAsINI($v);
  46. }
  47. } else {
  48. // Not in a section so add the property to the global array.
  49. $global[] = $key.'='.$this->_getValueAsINI($value);
  50. }
  51. }
  52. return implode("\n", array_merge($global, $local));
  53. }
  54. /**
  55. * Parse an INI formatted string and convert it into an object.
  56. *
  57. * @param string INI formatted string to convert.
  58. * @param mixed An array of options used by the formatter, or a boolean setting to process sections.
  59. *
  60. * @return object Data object.
  61. * @since 11.1
  62. *
  63. */
  64. public function stringToObject($data, $options = array())
  65. {
  66. // Initialise options.
  67. if (is_array($options)) {
  68. $sections = (isset($options['processSections'])) ? $options['processSections'] : false;
  69. } else {
  70. // Backward compatibility for 1.5 usage.
  71. //@deprecated
  72. $sections = (boolean) $options;
  73. }
  74. // Check the memory cache for already processed strings.
  75. $hash = md5($data.':'.(int) $sections);
  76. if (isset(self::$cache[$hash])) {
  77. return self::$cache[$hash];
  78. }
  79. // If no lines present just return the object.
  80. if (empty($data)) {
  81. return new stdClass;
  82. }
  83. // Initialize variables.
  84. $obj = new stdClass();
  85. $section = false;
  86. $lines = explode("\n", $data);
  87. // Process the lines.
  88. foreach ($lines as $line) {
  89. // Trim any unnecessary whitespace.
  90. $line = trim($line);
  91. // Ignore empty lines and comments.
  92. if (empty($line) || ($line{0} == ';')) {
  93. continue;
  94. }
  95. if ($sections) {
  96. $length = strlen($line);
  97. // If we are processing sections and the line is a section add the object and continue.
  98. if (($line[0] == '[') && ($line[$length-1] == ']')) {
  99. $section = substr($line, 1, $length-2);
  100. $obj->$section = new stdClass();
  101. continue;
  102. }
  103. } else if ($line{0} == '[') {
  104. continue;
  105. }
  106. // Check that an equal sign exists and is not the first character of the line.
  107. if (!strpos($line, '=')) {
  108. // Maybe throw exception?
  109. continue;
  110. }
  111. // Get the key and value for the line.
  112. list($key, $value) = explode('=', $line, 2);
  113. // Validate the key.
  114. if (preg_match('/[^A-Z0-9_]/i', $key)) {
  115. // Maybe throw exception?
  116. continue;
  117. }
  118. // If the value is quoted then we assume it is a string.
  119. $length = strlen($value);
  120. if ($length && ($value[0] == '"') && ($value[$length-1] == '"')) {
  121. // Strip the quotes and Convert the new line characters.
  122. $value = stripcslashes(substr($value, 1, ($length-2)));
  123. $value = str_replace('\n', "\n", $value);
  124. } else {
  125. // If the value is not quoted, we assume it is not a string.
  126. // If the value is 'false' assume boolean false.
  127. if ($value == 'false') {
  128. $value = false;
  129. }
  130. // If the value is 'true' assume boolean true.
  131. elseif ($value == 'true') {
  132. $value = true;
  133. }
  134. // If the value is numeric than it is either a float or int.
  135. elseif (is_numeric($value)) {
  136. // If there is a period then we assume a float.
  137. if (strpos($value, '.') !== false) {
  138. $value = (float) $value;
  139. }
  140. else {
  141. $value = (int) $value;
  142. }
  143. }
  144. }
  145. // If a section is set add the key/value to the section, otherwise top level.
  146. if ($section) {
  147. $obj->$section->$key = $value;
  148. } else {
  149. $obj->$key = $value;
  150. }
  151. }
  152. // Cache the string to save cpu cycles -- thus the world :)
  153. self::$cache[$hash] = clone($obj);
  154. return $obj;
  155. }
  156. /**
  157. * Method to get a value in an INI format.
  158. *
  159. * @param mixed The value to convert to INI format.
  160. * @return string The value in INI format.
  161. * @since 11.1
  162. */
  163. protected function _getValueAsINI($value)
  164. {
  165. // Initialize variables.
  166. $string = '';
  167. switch (gettype($value)) {
  168. case 'integer':
  169. case 'double':
  170. $string = $value;
  171. break;
  172. case 'boolean':
  173. $string = $value ? 'true' : 'false';
  174. break;
  175. case 'string':
  176. // Sanitize any CRLF characters..
  177. $string = '"'.str_replace(array("\r\n", "\n"), '\\n', $value).'"';
  178. break;
  179. }
  180. return $string;
  181. }
  182. }