Config
[ class tree: Config ] [ index: Config ] [ all elements ]

Source for file IniCommented.php

Documentation is available at IniCommented.php

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP Version 4                                                        |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2003 The PHP Group                                |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2.0 of the PHP license,       |
  8. // | that is bundled with this package in the file LICENSE, and is        |
  9. // | available at through the world-wide-web at                           |
  10. // | http://www.php.net/license/2_02.txt.                                 |
  11. // | If you did not receive a copy of the PHP license and are unable to   |
  12. // | obtain it through the world-wide-web, please send a note to          |
  13. // | license@php.net so we can mail you a copy immediately.               |
  14. // +----------------------------------------------------------------------+
  15. // | Author: Bertrand Mansion <bmansion@mamasam.com>                      |
  16. // +----------------------------------------------------------------------+
  17. //
  18. // $Id: IniCommented.php,v 1.25 2006/02/14 00:48:30 aashley Exp $
  19.  
  20. /**
  21. * Config parser for PHP .ini files with comments
  22. *
  23. @author      Bertrand Mansion <bmansion@mamasam.com>
  24. @package     Config
  25. */
  26.  
  27.     /**
  28.     * This class options
  29.     * Not used at the moment
  30.     *
  31.     * @var  array 
  32.     */
  33.     var $options = array();
  34.  
  35.     /**
  36.     * Constructor
  37.     *
  38.     * @access public
  39.     * @param    string  $options    (optional)Options to be used by renderer
  40.     */
  41.     function Config_Container_IniCommented($options = array())
  42.     {
  43.         $this->options = $options;
  44.     // end constructor
  45.  
  46.     /**
  47.     * Parses the data of the given configuration file
  48.     *
  49.     * @access public
  50.     * @param string $datasrc    path to the configuration file
  51.     * @param object $obj        reference to a config object
  52.     * @return mixed returns a PEAR_ERROR, if error occurs or true if ok
  53.     */
  54.     function &parseDatasrc($datasrc&$obj)
  55.     {
  56.         $return = true;
  57.         if (!file_exists($datasrc)) {
  58.             return PEAR::raiseError("Datasource file does not exist."nullPEAR_ERROR_RETURN);
  59.         }
  60.         $lines file($datasrc);
  61.         $n = 0;
  62.         $lastline '';
  63.         $currentSection =$obj->container;
  64.         foreach ($lines as $line{
  65.             $n++;
  66.             if (preg_match('/^\s*;(.*?)\s*$/'$line$match)) {
  67.                 // a comment
  68.                 $currentSection->createComment($match[1]);
  69.             elseif (preg_match('/^\s*$/'$line)) {
  70.                 // a blank line
  71.                 $currentSection->createBlank();
  72.             elseif (preg_match('/^\s*([a-zA-Z0-9_\-\.\s:]*)\s*=\s*(.*)\s*$/'$line$match)) {
  73.                 // a directive
  74.                 
  75.                 $values $this->_quoteAndCommaParser($match[2]);
  76.                 if (PEAR::isError($values)) {
  77.                     return PEAR::raiseError($values);
  78.                 }
  79.                 
  80.                 if (count($values)) {
  81.                     foreach($values as $value{
  82.                         if ($value[0== 'normal'{
  83.                             $currentSection->createDirective(trim($match[1])$value[1]);
  84.                         }
  85.                         if ($value[0== 'comment'{
  86.                             $currentSection->createComment(substr($value[1]1));
  87.                         }
  88.                     }
  89.                 }
  90.             elseif (preg_match('/^\s*\[\s*(.*)\s*\]\s*$/'$line$match)) {
  91.                 // a section
  92.                 $currentSection =$obj->container->createSection($match[1]);
  93.             else {
  94.                 return PEAR::raiseError("Syntax error in '$datasrc' at line $n."nullPEAR_ERROR_RETURN);
  95.             }
  96.         }
  97.         return $return;
  98.     // end func parseDatasrc
  99.  
  100.     /**
  101.      * Quote and Comma Parser for INI files
  102.      *
  103.      * This function allows complex values such as:
  104.      *
  105.      * <samp>
  106.      * mydirective = "Item, number \"1\"", Item 2 ; "This" is really, really tricky
  107.      * </samp>
  108.      * @param  string  $text    value of a directive to parse for quotes/multiple values
  109.      * @return array   The array returned contains multiple values, if any (unquoted literals
  110.      *                  to be used as is), and a comment, if any.  The format of the array is:
  111.      *
  112.      *  <pre>
  113.      *  array(array('normal', 'first value'),
  114.      *        array('normal', 'next value'),...
  115.      *        array('comment', '; comment with leading ;'))
  116.      *  </pre>
  117.      * @author Greg Beaver <cellog@users.sourceforge.net>
  118.      * @access private
  119.      */
  120.     function _quoteAndCommaParser($text)
  121.     {   
  122.         $text trim($text);
  123.         if ($text == ''{
  124.             return array();
  125.         }
  126.  
  127.         // tokens
  128.         $tokens['normal'= array('"'';'',');
  129.         $tokens['quote'= array('"''\\');
  130.         $tokens['escape'= false; // cycle
  131.         $tokens['after_quote'= array(','';');
  132.         
  133.         // events
  134.         $events['normal'= array('"' => 'quote'';' => 'comment'',' => 'normal');
  135.         $events['quote'= array('"' => 'after_quote''\\' => 'escape');
  136.         $events['after_quote'= array(',' => 'normal'';' => 'comment');
  137.         
  138.         // state stack
  139.         $stack = array();
  140.  
  141.         // return information
  142.         $return = array();
  143.         $returnpos = 0;
  144.         $returntype 'normal';
  145.  
  146.         // initialize
  147.         array_push($stack'normal');
  148.         $pos = 0; // position in $text
  149.  
  150.         do {
  151.             $char $text{$pos};
  152.             $state $this->_getQACEvent($stack);
  153.  
  154.             if ($tokens[$state]{
  155.                 if (in_array($char$tokens[$state])) {
  156.                     switch($events[$state][$char]{
  157.                         case 'quote' :
  158.                             if ($state == 'normal' &&
  159.                                 isset($return[$returnpos]&&
  160.                                 !empty($return[$returnpos][1])) {
  161.                                 return PEAR::raiseError("invalid ini syntax, quotes cannot follow text '$text'",
  162.                                                         nullPEAR_ERROR_RETURN);
  163.                             }
  164.                             if ($returnpos >= 0 && isset($return[$returnpos])) {
  165.                                 // trim any unnecessary whitespace in earlier entries
  166.                                 $return[$returnpos][1trim($return[$returnpos][1]);
  167.                             else {
  168.                                 $returnpos++;
  169.                             }
  170.                             $return[$returnpos= array('normal''');
  171.                             array_push($stack'quote');
  172.                             continue 2;
  173.                         break;
  174.                         case 'comment' :
  175.                             // comments go to the end of the line, so we are done
  176.                             $return[++$returnpos= array('comment'substr($text$pos));
  177.                             return $return;
  178.                         break;
  179.                         case 'after_quote' :
  180.                             array_push($stack'after_quote');
  181.                         break;
  182.                         case 'escape' :
  183.                             // don't save the first slash
  184.                             array_push($stack'escape');
  185.                             continue 2;
  186.                         break;
  187.                         case 'normal' :
  188.                         // start a new segment
  189.                             if ($state == 'normal'{
  190.                                 $returnpos++;
  191.                                 continue 2;
  192.                             else {
  193.                                 while ($state != 'normal'{
  194.                                     array_pop($stack);
  195.                                     $state $this->_getQACEvent($stack);
  196.                                 }
  197.                                 $returnpos++;
  198.                             }
  199.                         break;
  200.                         default :
  201.                             PEAR::raiseError("::_quoteAndCommaParser oops, state missing"nullPEAR_ERROR_DIE);
  202.                         break;
  203.                     }
  204.                 else {
  205.                     if ($state != 'after_quote'{
  206.                         if (!isset($return[$returnpos])) {
  207.                             $return[$returnpos= array('normal''');
  208.                         }
  209.                         // add this character to the current ini segment if non-empty, or if in a quote
  210.                         if ($state == 'quote'{
  211.                             $return[$returnpos][1.= $char;
  212.                         elseif (!empty($return[$returnpos][1]||
  213.                                  (empty($return[$returnpos][1]&& trim($char!= '')) {
  214.                             if (!isset($return[$returnpos])) {
  215.                                 $return[$returnpos= array('normal''');
  216.                             }
  217.                             $return[$returnpos][1.= $char;
  218.                             if (strcasecmp('true'$return[$returnpos][1]== 0{
  219.                               $return[$returnpos][1'1';
  220.                             elseif (strcasecmp('false'$return[$returnpos][1]== 0{
  221.                               $return[$returnpos][1'';
  222.                             }
  223.                         }
  224.                     else {
  225.                         if (trim($char!= ''{
  226.                             return PEAR::raiseError("invalid ini syntax, text after a quote not allowed '$text'",
  227.                                                     nullPEAR_ERROR_RETURN);
  228.                         }
  229.                     }
  230.                 }
  231.             else {
  232.                 // no tokens, so add this one and cycle to previous state
  233.                 $return[$returnpos][1.= $char;
  234.                 array_pop($stack);
  235.             }
  236.         while (++$pos strlen($text));
  237.         return $return;
  238.     // end func _quoteAndCommaParser
  239.     
  240.     /**
  241.      * Retrieve the state off of a state stack for the Quote and Comma Parser
  242.      * @param  array  $stack    The parser state stack
  243.      * @author Greg Beaver <cellog@users.sourceforge.net>
  244.      * @access private
  245.      */
  246.     function _getQACEvent($stack)
  247.     {
  248.         return array_pop($stack);
  249.     // end func _getQACEvent
  250.  
  251.     /**
  252.     * Returns a formatted string of the object
  253.     * @param    object  $obj    Container object to be output as string
  254.     * @access   public
  255.     * @return   string 
  256.     */
  257.     function toString(&$obj)
  258.     {
  259.         static $childrenCount$commaString;
  260.  
  261.         if (!isset($string)) {
  262.             $string '';
  263.         }
  264.         switch ($obj->type{
  265.             case 'blank':
  266.                 $string "\n";
  267.                 break;
  268.             case 'comment':
  269.                 $string ';'.$obj->content."\n";
  270.                 break;
  271.             case 'directive':
  272.                 $count $obj->parent->countChildren('directive'$obj->name);
  273.                 $content $obj->content;
  274.                 if ($content === false{
  275.                     $content '0';
  276.                 elseif ($content === true{
  277.                     $content '1';
  278.                 elseif (strlen(trim($content)) strlen($content||
  279.                           strpos($content','!== false ||
  280.                           strpos($content';'!== false ||
  281.                           strpos($content'='!== false ||
  282.                           strpos($content'"'!== false ||
  283.                           strpos($content'%'!== false ||
  284.                           strpos($content'~'!== false{
  285.                     $content '"'.addslashes($content).'"';          
  286.                 }
  287.                 if ($count > 1{
  288.                     // multiple values for a directive are separated by a comma
  289.                     if (isset($childrenCount[$obj->name])) {
  290.                         $childrenCount[$obj->name]++;
  291.                     else {
  292.                         $childrenCount[$obj->name= 0;
  293.                         $commaString[$obj->name$obj->name.' = ';
  294.                     }
  295.                     if ($childrenCount[$obj->name== $count-1{
  296.                         // Clean the static for future calls to toString
  297.                         $string .= $commaString[$obj->name].$content."\n";
  298.                         unset($childrenCount[$obj->name]);
  299.                         unset($commaString[$obj->name]);
  300.                     else {
  301.                         $commaString[$obj->name.= $content.', ';
  302.                     }
  303.                 else {
  304.                     $string $obj->name.' = '.$content."\n";
  305.                 }
  306.                 break;
  307.             case 'section':
  308.                 if (!$obj->isRoot()) {
  309.                     $string '['.$obj->name."]\n";
  310.                 }
  311.                 if (count($obj->children> 0{
  312.                     for ($i = 0; $i count($obj->children)$i++{
  313.                         $string .= $this->toString($obj->getChild($i));
  314.                     }
  315.                 }
  316.                 break;
  317.             default:
  318.                 $string '';
  319.         }
  320.         return $string;
  321.     // end func toString
  322. // end class Config_Container_IniCommented
  323. ?>

Documentation generated on Mon, 11 Mar 2019 14:36:35 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.