Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
ini.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Registry
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
7  * @license GNU General Public License version 2 or later; see LICENSE
8  */
9 
10 defined('JPATH_PLATFORM') or die;
11 
12 /**
13  * INI format handler for JRegistry.
14  *
15  * @package Joomla.Platform
16  * @subpackage Registry
17  * @since 11.1
18  */
20 {
21  /**
22  * Cache of processed data
23  *
24  * @var array
25  * @since 11.1
26  */
27  protected static $cache = array();
28 
29  /**
30  * Converts an object into an INI formatted string
31  * - Unfortunately, there is no way to have ini values nested further than two
32  * levels deep. Therefore we will only go through the first two levels of
33  * the object.
34  *
35  * @param object $object Data source object.
36  * @param array $options Options used by the formatter.
37  *
38  * @return string INI formatted string.
39  *
40  * @since 11.1
41  */
42  public function objectToString($object, $options = array())
43  {
44  $local = array();
45  $global = array();
46 
47  // Iterate over the object to set the properties.
48  foreach (get_object_vars($object) as $key => $value)
49  {
50  // If the value is an object then we need to put it in a local section.
51  if (is_object($value))
52  {
53  // Add the section line.
54  $local[] = '';
55  $local[] = '[' . $key . ']';
56 
57  // Add the properties for this section.
58  foreach (get_object_vars($value) as $k => $v)
59  {
60  $local[] = $k . '=' . $this->getValueAsINI($v);
61  }
62  }
63  else
64  {
65  // Not in a section so add the property to the global array.
66  $global[] = $key . '=' . $this->getValueAsINI($value);
67  }
68  }
69 
70  return implode("\n", array_merge($global, $local));
71  }
72 
73  /**
74  * Parse an INI formatted string and convert it into an object.
75  *
76  * @param string $data INI formatted string to convert.
77  * @param mixed $options An array of options used by the formatter, or a boolean setting to process sections.
78  *
79  * @return object Data object.
80  *
81  * @since 11.1
82  */
83  public function stringToObject($data, array $options = array())
84  {
85  $sections = (isset($options['processSections'])) ? $options['processSections'] : false;
86 
87  // Check the memory cache for already processed strings.
88  $hash = md5($data . ':' . (int) $sections);
89 
90  if (isset(self::$cache[$hash]))
91  {
92  return self::$cache[$hash];
93  }
94 
95  // If no lines present just return the object.
96  if (empty($data))
97  {
98  return new stdClass;
99  }
100 
101  $obj = new stdClass;
102  $section = false;
103  $lines = explode("\n", $data);
104 
105  // Process the lines.
106  foreach ($lines as $line)
107  {
108  // Trim any unnecessary whitespace.
109  $line = trim($line);
110 
111  // Ignore empty lines and comments.
112  if (empty($line) || ($line{0} == ';'))
113  {
114  continue;
115  }
116 
117  if ($sections)
118  {
119  $length = strlen($line);
120 
121  // If we are processing sections and the line is a section add the object and continue.
122  if (($line[0] == '[') && ($line[$length - 1] == ']'))
123  {
124  $section = substr($line, 1, $length - 2);
125  $obj->$section = new stdClass;
126  continue;
127  }
128  }
129  elseif ($line{0} == '[')
130  {
131  continue;
132  }
133 
134  // Check that an equal sign exists and is not the first character of the line.
135  if (!strpos($line, '='))
136  {
137  // Maybe throw exception?
138  continue;
139  }
140 
141  // Get the key and value for the line.
142  list ($key, $value) = explode('=', $line, 2);
143 
144  // Validate the key.
145  if (preg_match('/[^A-Z0-9_]/i', $key))
146  {
147  // Maybe throw exception?
148  continue;
149  }
150 
151  // If the value is quoted then we assume it is a string.
152  $length = strlen($value);
153 
154  if ($length && ($value[0] == '"') && ($value[$length - 1] == '"'))
155  {
156  // Strip the quotes and Convert the new line characters.
157  $value = stripcslashes(substr($value, 1, ($length - 2)));
158  $value = str_replace('\n', "\n", $value);
159  }
160  else
161  {
162  // If the value is not quoted, we assume it is not a string.
163 
164  // If the value is 'false' assume boolean false.
165  if ($value == 'false')
166  {
167  $value = false;
168  }
169  // If the value is 'true' assume boolean true.
170  elseif ($value == 'true')
171  {
172  $value = true;
173  }
174  // If the value is numeric than it is either a float or int.
175  elseif (is_numeric($value))
176  {
177  // If there is a period then we assume a float.
178  if (strpos($value, '.') !== false)
179  {
180  $value = (float) $value;
181  }
182  else
183  {
184  $value = (int) $value;
185  }
186  }
187  }
188 
189  // If a section is set add the key/value to the section, otherwise top level.
190  if ($section)
191  {
192  $obj->$section->$key = $value;
193  }
194  else
195  {
196  $obj->$key = $value;
197  }
198  }
199 
200  // Cache the string to save cpu cycles -- thus the world :)
201  self::$cache[$hash] = clone ($obj);
202 
203  return $obj;
204  }
205 
206  /**
207  * Method to get a value in an INI format.
208  *
209  * @param mixed $value The value to convert to INI format.
210  *
211  * @return string The value in INI format.
212  *
213  * @since 11.1
214  */
215  protected function getValueAsINI($value)
216  {
217  $string = '';
218 
219  switch (gettype($value))
220  {
221  case 'integer':
222  case 'double':
223  $string = $value;
224  break;
225 
226  case 'boolean':
227  $string = $value ? 'true' : 'false';
228  break;
229 
230  case 'string':
231  // Sanitize any CRLF characters..
232  $string = '"' . str_replace(array("\r\n", "\n"), '\\n', $value) . '"';
233  break;
234  }
235 
236  return $string;
237  }
238 }