Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /joomla/registry/format/ini.php

Documentation is available at ini.php

  1. <?php
  2. /**
  3.  * @version        $Id: ini.php 6472 2007-02-03 10:47:26Z pasamio $
  4.  * @package        Joomla.Framework
  5.  * @subpackage    Registry
  6.  * @copyright    Copyright (C) 2005 - 2007 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.  * @author         Samuel Moffatt <[email protected]>
  22.  * @package     Joomla.Framework
  23.  * @subpackage        Registry
  24.  * @since        1.5
  25.  */
  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.         // Initialize variables
  41.         $retval = '';
  42.         $prepend = '';
  43.  
  44.         // First handle groups (or first level key/value pairs)
  45.         foreach (get_object_vars( $object ) as $key => $level1) {
  46.  
  47.             if (is_object($level1)) {
  48.                 // This field is an object, so we treat it as a section
  49.                 $retval .= "[".$key."]\n";
  50.                 foreach (get_object_vars( $level1 ) as $key => $level2) {
  51.                     if (!is_object($level2) && !is_array($level2)) {
  52.                         $retval .= $key."=".$level2."\n";
  53.                     }
  54.                 }
  55.                 $retval .= "\n";
  56.             } else {
  57.                 $prepend .= $key."=".$level1."\n";
  58.             }
  59.         }
  60.         return $prepend."\n".$retval;
  61.     }
  62.  
  63.     /**
  64.      * Parse an .ini string, based on phpDocumentor phpDocumentor_parse_ini_file function
  65.      *
  66.      * @access public
  67.      * @param mixed The INI string or array of lines
  68.      * @param boolean add an associative index for each section [in brackets]
  69.      * @return object Data Object
  70.      */
  71.     function &stringToObject( $data, $process_sections = false )
  72.     {
  73.         if (is_string($data)) {
  74.             $lines = explode("\n", $data);
  75.         } else {
  76.             if (is_array($data)) {
  77.                 $lines = $data;
  78.             } else {
  79.                 $lines = array ();
  80.             }
  81.         }
  82.         $obj = new stdClass();
  83.  
  84.         $sec_name = '';
  85.         $unparsed = 0;
  86.         if (!$lines) {
  87.             return $obj;
  88.         }
  89.         foreach ($lines as $line) {
  90.             // ignore comments
  91.             if ($line && $line{0} == ';') {
  92.                 continue;
  93.             }
  94.             $line = trim($line);
  95.  
  96.             if ($line == '') {
  97.                 continue;
  98.             }
  99.             $lineLen = strlen($line);
  100.             if ($line && $line{0} == '[' && $line{$lineLen-1} == ']') {
  101.                 $sec_name = substr($line, 1, $lineLen - 2);
  102.                 if ($process_sections) {
  103.                     $obj-> $sec_name = new stdClass();
  104.                 }
  105.             } else {
  106.                 if ($pos = strpos($line, '=')) {
  107.                     $property = trim(substr($line, 0, $pos));
  108.  
  109.                     // property is assumed to be ascii
  110.                     if ($property && $property{0} == '"') {
  111.                         $propLen = strlen( $property );
  112.                         if ($property{$propLen-1} == '"') {
  113.                             $property = stripcslashes(substr($property, 1, $propLen - 2));
  114.                         }
  115.                     }
  116.                     // AJE: 2006-11-06 Fixes problem where you want leading spaces
  117.                     // for some parameters, eg, class suffix
  118.                     // $value = trim(substr($line, $pos +1));
  119.                     $value = substr($line, $pos +1);
  120.                     if ($value == 'false') {
  121.                         $value = false;
  122.                     }
  123.                     else if ($value == 'true') {
  124.                         $value = true;
  125.                     }
  126.                     else if ($value && $value{0} == '"') {
  127.                         $valueLen = strlen( $value );
  128.                         if ($value{$valueLen-1} == '"') {
  129.                             $value = stripcslashes(substr($value, 1, $valueLen - 2));
  130.                         }
  131.                     }
  132.  
  133.                     if ($process_sections) {
  134.                         $value = str_replace('\n', "\n", $value);
  135.                         if ($sec_name != '') {
  136.                             $obj->$sec_name->$property = $value;
  137.                         } else {
  138.                             $obj->$property = $value;
  139.                         }
  140.                     } else {
  141.                         $obj->$property = str_replace('\n', "\n", $value);
  142.                     }
  143.                 } else {
  144.                     if ($line && $line{0} == ';') {
  145.                         continue;
  146.                     }
  147.                     if ($process_sections) {
  148.                         $property = '__invalid'.$unparsed ++.'__';
  149.                         if ($process_sections) {
  150.                             if ($sec_name != '') {
  151.                                 $obj->$sec_name->$property = trim($line);
  152.                             } else {
  153.                                 $obj->$property = trim($line);
  154.                             }
  155.                         } else {
  156.                             $obj->$property = trim($line);
  157.                         }
  158.                     }
  159.                 }
  160.             }
  161.         }
  162.         return $obj;
  163.     }
  164. }
  165. ?>

Documentation generated on Mon, 05 Mar 2007 21:07:54 +0000 by phpDocumentor 1.3.1