PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/rokcommon/RokCommon/Registry/Format/INI.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 208 lines | 108 code | 27 blank | 73 comment | 33 complexity | 501dd9b316bb8a21a7c0c44d6f168668 MD5 | raw file
  1. <?php
  2. /**
  3. * @version ${project.version} ${build_date}
  4. * @author RocketTheme http://www.rockettheme.com
  5. * @copyright Copyright (C) 2007 - ${copyright_year} RocketTheme, LLC
  6. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
  7. *
  8. * derived from JoomlaRTCacheDriver 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('ROKCOMMON') or die;
  14. /**
  15. * INI format handler for RokCommon_Registry.
  16. *
  17. * @package JoomlaRTCacheDriver.Framework
  18. * @subpackage Registry
  19. * @since 1.5
  20. */
  21. class RokCommon_Registry_Format_INI extends RokCommon_Registry_Format
  22. {
  23. protected 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. */
  34. public function objectToString($object, $options = array())
  35. {
  36. // Initialize variables.
  37. $local = array();
  38. $global = array();
  39. // Iterate over the object to set the properties.
  40. foreach (get_object_vars($object) as $key => $value) {
  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. $local[] = $k.'='.$this->_getValueAsINI($v);
  49. }
  50. } else {
  51. // Not in a section so add the property to the global array.
  52. $global[] = $key.'='.$this->_getValueAsINI($value);
  53. }
  54. }
  55. return implode("\n", array_merge($global, $local));
  56. }
  57. /**
  58. * Parse an INI formatted string and convert it into an object.
  59. *
  60. * @param string INI formatted string to convert.
  61. * @param mixed An array of options used by the formatter, or a boolean setting to process sections.
  62. * @return object Data object.
  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. $sections = (boolean) $options;
  72. }
  73. // Check the memory cache for already processed strings.
  74. $hash = md5($data.':'.(int) $sections);
  75. if (isset(self::$cache[$hash])) {
  76. return self::$cache[$hash];
  77. }
  78. // If no lines present just return the object.
  79. if (empty($data)) {
  80. return new stdClass;
  81. }
  82. // Initialize variables.
  83. $obj = new stdClass();
  84. $section = false;
  85. $lines = explode("\n", $data);
  86. // Process the lines.
  87. foreach ($lines as $line) {
  88. // Trim any unnecessary whitespace.
  89. $line = trim($line);
  90. // Ignore empty lines and comments.
  91. if (empty($line) || ($line{0} == ';')) {
  92. continue;
  93. }
  94. if ($sections) {
  95. $length = strlen($line);
  96. // If we are processing sections and the line is a section add the object and continue.
  97. if (($line[0] == '[') && ($line[$length-1] == ']')) {
  98. $section = substr($line, 1, $length-2);
  99. $obj->$section = new stdClass();
  100. continue;
  101. }
  102. } else if ($line{0} == '[') {
  103. continue;
  104. }
  105. // Check that an equal sign exists and is not the first character of the line.
  106. if (!strpos($line, '=')) {
  107. // Maybe throw exception?
  108. continue;
  109. }
  110. // Get the key and value for the line.
  111. list($key, $value) = explode('=', $line, 2);
  112. // Validate the key.
  113. if (preg_match('/[^A-Z0-9_]/i', $key)) {
  114. // Maybe throw exception?
  115. continue;
  116. }
  117. // If the value is quoted then we assume it is a string.
  118. $length = strlen($value);
  119. if ($length && ($value[0] == '"') && ($value[$length-1] == '"')) {
  120. // Strip the quotes and Convert the new line characters.
  121. $value = stripcslashes(substr($value, 1, ($length-2)));
  122. $value = str_replace('\n', "\n", $value);
  123. } else {
  124. // If the value is not quoted, we assume it is not a string.
  125. // If the value is 'false' assume boolean false.
  126. if ($value == 'false') {
  127. $value = false;
  128. }
  129. // If the value is 'true' assume boolean true.
  130. elseif ($value == 'true') {
  131. $value = true;
  132. }
  133. // If the value is numeric than it is either a float or int.
  134. elseif (is_numeric($value)) {
  135. // If there is a period then we assume a float.
  136. if (strpos($value, '.') !== false) {
  137. $value = (float) $value;
  138. }
  139. else {
  140. $value = (int) $value;
  141. }
  142. }
  143. }
  144. // If a section is set add the key/value to the section, otherwise top level.
  145. if ($section) {
  146. $obj->$section->$key = $value;
  147. } else {
  148. $obj->$key = $value;
  149. }
  150. }
  151. // Cache the string to save cpu cycles -- thus the world :)
  152. self::$cache[$hash] = clone($obj);
  153. return $obj;
  154. }
  155. /**
  156. * Method to get a value in an INI format.
  157. *
  158. * @param mixed The value to convert to INI format.
  159. * @return string The value in INI format.
  160. * @since 1.6
  161. */
  162. protected function _getValueAsINI($value)
  163. {
  164. // Initialize variables.
  165. $string = '';
  166. switch (gettype($value)) {
  167. case 'integer':
  168. case 'double':
  169. $string = $value;
  170. break;
  171. case 'boolean':
  172. $string = $value ? 'true' : 'false';
  173. break;
  174. case 'string':
  175. // Sanitize any CRLF characters..
  176. $string = '"'.str_replace(array("\r\n", "\n"), '\\n', $value).'"';
  177. break;
  178. }
  179. return $string;
  180. }
  181. }