PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

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

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