[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

/libraries/joomla/registry/format/ -> ini.php (source)

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


Generated: Wed Mar 28 15:54:07 2012 Cross-referenced by PHPXref 0.7.1