PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://github.com/ponlue/abktours
PHP | 281 lines | 203 code | 25 blank | 53 comment | 68 complexity | e60ca38f236e67597c017c5b5b37e682 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: ini.php 14401 2010-01-26 14:10:00Z louis $
  4. * @package Joomla.Framework
  5. * @subpackage Registry
  6. * @copyright Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
  7. * @license GNU/GPL, see LICENSE.php
  8. * Joomla! is free software. This version may have been modified pursuant
  9. * to the GNU General Public License, and as distributed it includes or
  10. * is derivative of works licensed under the GNU General Public License or
  11. * other free or open source software licenses.
  12. * See COPYRIGHT.php for copyright notices and details.
  13. */
  14. // Check to ensure this file is within the rest of the framework
  15. defined('JPATH_BASE') or die();
  16. /**
  17. * INI format handler for JRegistry
  18. *
  19. * @package Joomla.Framework
  20. * @subpackage Registry
  21. * @since 1.5
  22. */
  23. class JRegistryFormatINI extends JRegistryFormat
  24. {
  25. /**
  26. * Converts an object into an INI formatted string
  27. * - Unfortunately, there is no way to have ini values nested further than two
  28. * levels deep. Therefore we will only go through the first two levels of
  29. * the object.
  30. *
  31. * @access public
  32. * @param object $object Data Source Object
  33. * @param array $param Parameters used by the formatter
  34. * @return string INI Formatted String
  35. */
  36. function objectToString( &$object, $params )
  37. {
  38. // Initialize variables
  39. $retval = '';
  40. $prepend = '';
  41. // First handle groups (or first level key/value pairs)
  42. foreach (get_object_vars( $object ) as $key => $level1)
  43. {
  44. if (is_object($level1))
  45. {
  46. // This field is an object, so we treat it as a section
  47. $retval .= "[".$key."]\n";
  48. foreach (get_object_vars($level1) as $key => $level2)
  49. {
  50. if (!is_object($level2) && !is_array($level2))
  51. {
  52. // Join lines
  53. $level2 = str_replace('|', '\|', $level2);
  54. $level2 = str_replace(array("\r\n", "\n"), '\\n', $level2);
  55. $retval .= $key."=".$level2."\n";
  56. }
  57. }
  58. $retval .= "\n";
  59. }
  60. elseif (is_array($level1))
  61. {
  62. foreach ($level1 as $k1 => $v1)
  63. {
  64. // Escape any pipe characters before storing
  65. $level1[$k1] = str_replace('|', '\|', $v1);
  66. $level1[$k1] = str_replace(array("\r\n", "\n"), '\\n', $v1);
  67. }
  68. // Implode the array to store
  69. $prepend .= $key."=".implode('|', $level1)."\n";
  70. }
  71. else
  72. {
  73. // Join lines
  74. $level1 = str_replace('|', '\|', $level1);
  75. $level1 = str_replace(array("\r\n", "\n"), '\\n', $level1);
  76. $prepend .= $key."=".$level1."\n";
  77. }
  78. }
  79. return $prepend."\n".$retval;
  80. }
  81. /**
  82. * Parse an .ini string, based on phpDocumentor phpDocumentor_parse_ini_file function
  83. *
  84. * @access public
  85. * @param mixed The INI string or array of lines
  86. * @param boolean add an associative index for each section [in brackets]
  87. * @return object Data Object
  88. */
  89. function &stringToObject( $data, $process_sections = false )
  90. {
  91. static $inistocache;
  92. if (!isset( $inistocache )) {
  93. $inistocache = array();
  94. }
  95. if (is_string($data))
  96. {
  97. $lines = explode("\n", $data);
  98. $hash = md5($data);
  99. }
  100. else
  101. {
  102. if (is_array($data)) {
  103. $lines = $data;
  104. } else {
  105. $lines = array ();
  106. }
  107. $hash = md5(implode("\n",$lines));
  108. }
  109. if(array_key_exists($hash, $inistocache)) {
  110. return $inistocache[$hash];
  111. }
  112. $obj = new stdClass();
  113. $sec_name = '';
  114. $unparsed = 0;
  115. if (!$lines) {
  116. return $obj;
  117. }
  118. foreach ($lines as $line)
  119. {
  120. // ignore comments
  121. if ($line && $line{0} == ';') {
  122. continue;
  123. }
  124. $line = trim($line);
  125. if ($line == '') {
  126. continue;
  127. }
  128. $lineLen = strlen($line);
  129. if ($line && $line{0} == '[' && $line{$lineLen-1} == ']')
  130. {
  131. $sec_name = substr($line, 1, $lineLen - 2);
  132. if ($process_sections) {
  133. $obj-> $sec_name = new stdClass();
  134. }
  135. }
  136. else
  137. {
  138. if ($pos = strpos($line, '='))
  139. {
  140. $property = trim(substr($line, 0, $pos));
  141. // property is assumed to be ascii
  142. if ($property && $property{0} == '"')
  143. {
  144. $propLen = strlen( $property );
  145. if ($property{$propLen-1} == '"') {
  146. $property = stripcslashes(substr($property, 1, $propLen - 2));
  147. }
  148. }
  149. // AJE: 2006-11-06 Fixes problem where you want leading spaces
  150. // for some parameters, eg, class suffix
  151. // $value = trim(substr($line, $pos +1));
  152. $value = substr($line, $pos +1);
  153. if (strpos($value, '|') !== false && preg_match('#(?<!\\\)\|#', $value))
  154. {
  155. $newlines = explode('\n', $value);
  156. $values = array();
  157. foreach($newlines as $newlinekey=>$newline) {
  158. // Explode the value if it is serialized as an arry of value1|value2|value3
  159. $parts = preg_split('/(?<!\\\)\|/', $newline);
  160. $array = (strcmp($parts[0], $newline) === 0) ? false : true;
  161. $parts = str_replace('\|', '|', $parts);
  162. foreach ($parts as $key => $value)
  163. {
  164. if ($value == 'false') {
  165. $value = false;
  166. }
  167. else if ($value == 'true') {
  168. $value = true;
  169. }
  170. else if ($value && $value{0} == '"')
  171. {
  172. $valueLen = strlen( $value );
  173. if ($value{$valueLen-1} == '"') {
  174. $value = stripcslashes(substr($value, 1, $valueLen - 2));
  175. }
  176. }
  177. if(!isset($values[$newlinekey])) $values[$newlinekey] = array();
  178. $values[$newlinekey][] = str_replace('\n', "\n", $value);
  179. }
  180. if (!$array) {
  181. $values[$newlinekey] = $values[$newlinekey][0];
  182. }
  183. }
  184. if ($process_sections)
  185. {
  186. if ($sec_name != '') {
  187. $obj->$sec_name->$property = $values[$newlinekey];
  188. } else {
  189. $obj->$property = $values[$newlinekey];
  190. }
  191. }
  192. else
  193. {
  194. $obj->$property = $values[$newlinekey];
  195. }
  196. }
  197. else
  198. {
  199. //unescape the \|
  200. $value = str_replace('\|', '|', $value);
  201. if ($value == 'false') {
  202. $value = false;
  203. }
  204. else if ($value == 'true') {
  205. $value = true;
  206. }
  207. else if ($value && $value{0} == '"')
  208. {
  209. $valueLen = strlen( $value );
  210. if ($value{$valueLen-1} == '"') {
  211. $value = stripcslashes(substr($value, 1, $valueLen - 2));
  212. }
  213. }
  214. if ($process_sections)
  215. {
  216. $value = str_replace('\n', "\n", $value);
  217. if ($sec_name != '') {
  218. $obj->$sec_name->$property = $value;
  219. } else {
  220. $obj->$property = $value;
  221. }
  222. }
  223. else
  224. {
  225. $obj->$property = str_replace('\n', "\n", $value);
  226. }
  227. }
  228. }
  229. else
  230. {
  231. if ($line && $line{0} == ';') {
  232. continue;
  233. }
  234. if ($process_sections)
  235. {
  236. $property = '__invalid'.$unparsed ++.'__';
  237. if ($process_sections)
  238. {
  239. if ($sec_name != '') {
  240. $obj->$sec_name->$property = trim($line);
  241. } else {
  242. $obj->$property = trim($line);
  243. }
  244. }
  245. else
  246. {
  247. $obj->$property = trim($line);
  248. }
  249. }
  250. }
  251. }
  252. }
  253. $inistocache[$hash] = clone($obj);
  254. return $obj;
  255. }
  256. }