Source for file ini.php

Documentation is available at ini.php

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

Documentation generated on Tue, 19 Nov 2013 15:05:33 +0100 by phpDocumentor 1.4.3