PageRenderTime 60ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/tmp/busines13_bundle_installer/lib_gantry/core/utilities/registry/format/ini.php

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