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.16 2003/11/29 10:44:39 mansion 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.         if (!file_exists($datasrc)) {
  57.             return PEAR::raiseError("Datasource file does not exist."nullPEAR_ERROR_RETURN);
  58.         }
  59.         $lines file($datasrc);
  60.         $n = 0;
  61.         $lastline '';
  62.         $currentSection =$obj->container;
  63.         foreach ($lines as $line{
  64.             $n++;
  65.             if (preg_match('/^\s*;(.*?)\s*$/'$line$match)) {
  66.                 // a comment
  67.                 $currentSection->createComment($match[1]);
  68.             elseif (preg_match('/^\s*$/'$line)) {
  69.                 // a blank line
  70.                 $currentSection->createBlank();
  71.             elseif (preg_match('/^\s*([a-zA-Z0-9_\-\.]*)\s*=\s*(.*)\s*$/'$line$match)) {
  72.                 // a directive
  73.                 
  74.                 $values $this->_quoteAndCommaParser($match[2]);
  75.                 if (PEAR::isError($values)) {
  76.                     return PEAR::raiseError($values);
  77.                 }
  78.                 
  79.                 if (count($values)) {
  80.                     foreach($values as $value{
  81.                         if ($value[0== 'normal'{
  82.                             $currentSection->createDirective($match[1]$value[1]);
  83.                         }
  84.                         if ($value[0== 'comment'{
  85.                             $currentSection->createComment(substr($value[1]1));
  86.                         }
  87.                     }
  88.                 }
  89.             elseif (preg_match('/^\s*\[\s*(.*)\s*\]\s*$/'$line$match)) {
  90.                 // a section
  91.                 $currentSection =$obj->container->createSection($match[1]);
  92.             else {
  93.                 return PEAR::raiseError("Syntax error in '$datasrc' at line $n."nullPEAR_ERROR_RETURN);
  94.             }
  95.         }
  96.         return true;
  97.     // end func parseDatasrc
  98.  
  99.     /**
  100.      * Quote and Comma Parser for INI files
  101.      *
  102.      * This function allows complex values such as:
  103.      *
  104.      * <samp>
  105.      * mydirective = "Item, number \"1\"", Item 2 ; "This" is really, really tricky
  106.      * </samp>
  107.      * @param  string  $text    value of a directive to parse for quotes/multiple values
  108.      * @return array   The array returned contains multiple values, if any (unquoted literals
  109.      *                  to be used as is), and a comment, if any.  The format of the array is:
  110.      *
  111.      *  <pre>
  112.      *  array(array('normal', 'first value'),
  113.      *        array('normal', 'next value'),...
  114.      *        array('comment', '; comment with leading ;'))
  115.      *  </pre>
  116.      * @author Greg Beaver <cellog@users.sourceforge.net>
  117.      * @access private
  118.      */
  119.     function _quoteAndCommaParser($text)
  120.     {   
  121.         $text trim($text);
  122.         if ($text == ''{
  123.             return array();
  124.         }
  125.  
  126.         // tokens
  127.         $tokens['normal'= array('"'';'',');
  128.         $tokens['quote'= array('"''\\');
  129.         $tokens['escape'= false; // cycle
  130.         $tokens['after_quote'= array(','';');
  131.         
  132.         // events
  133.         $events['normal'= array('"' => 'quote'';' => 'comment'',' => 'normal');
  134.         $events['quote'= array('"' => 'after_quote''\\' => 'escape');
  135.         $events['after_quote'= array(',' => 'normal'';' => 'comment');
  136.         
  137.         // state stack
  138.         $stack = array();
  139.         // return information
  140.         $return = array();
  141.         $returnpos = 0;
  142.         $returntype 'normal';
  143.         // initialize
  144.         array_push($stack'normal');
  145.         $pos = 0; // position in $text
  146.         do {
  147.             $char $text{$pos};
  148.             $state $this->_getQACEvent($stack);
  149.             
  150.             if ($tokens[$state]{
  151.                 if (in_array($char$tokens[$state])) {
  152.                     switch($events[$state][$char]{
  153.                         case 'quote' :
  154.                             if ($state == 'normal' &&
  155.                                 isset($return[$returnpos]&&
  156.                                 !empty($return[$returnpos][1])) {
  157.                                 return PEAR::raiseError("invalid ini syntax, quotes cannot follow text '$text'",
  158.                                                         nullPEAR_ERROR_RETURN);
  159.                                 }
  160.                             if ($returnpos >= 0{
  161.                             // trim any unnecessary whitespace in earlier entries
  162.                                 $return[$returnpos][1trim($return[$returnpos][1]);
  163.                             else {
  164.                                 $returnpos++;
  165.                             }
  166.                             $return[$returnpos= array('normal''');
  167.                             array_push($stack'quote');
  168.                             continue 2;
  169.                         break;
  170.                         case 'comment' :
  171.                             // comments go to the end of the line, so we are done
  172.                             $return[++$returnpos= array('comment'substr($text$pos));
  173.                             return $return;
  174.                         break;
  175.                         case 'after_quote' :
  176.                             array_push($stack'after_quote');
  177.                         break;
  178.                         case 'escape' :
  179.                             // don't save the first slash
  180.                             array_push($stack'escape');
  181.                             continue 2;
  182.                         break;
  183.                         case 'normal' :
  184.                         // start a new segment
  185.                             if ($state == 'normal'{
  186.                                 $returnpos++;
  187.                                 continue 2;
  188.                             else {
  189.                                 while ($state != 'normal'{
  190.                                     array_pop($stack);
  191.                                     $state $this->_getQACEvent($stack);
  192.                                 }
  193.                             }
  194.                         break;
  195.                         default :
  196.                             PEAR::raiseError("::_quoteAndCommaParser oops, state missing"nullPEAR_ERROR_DIE);
  197.                         break;
  198.                     }
  199.                 else {
  200.                     if ($state != 'after_quote'{
  201.                         if (!isset($return[$returnpos])) {
  202.                             $return[$returnpos= array('normal''');
  203.                         }
  204.                         // add this character to the current ini segment if non-empty, or if in a quote
  205.                         if ($state == 'quote'{
  206.                             $return[$returnpos][1.= $char;
  207.                         elseif (!empty($return[$returnpos][1]||
  208.                                  (empty($return[$returnpos][1]&& trim($char!= '')) {
  209.                             if (!isset($return[$returnpos])) {
  210.                                 $return[$returnpos= array('normal''');
  211.                             }
  212.                             $return[$returnpos][1.= $char;
  213.                         }
  214.                     else {
  215.                         if (trim($char!= ''{
  216.                             return PEAR::raiseError("invalid ini syntax, text after a quote not allowed '$text'",
  217.                                                     nullPEAR_ERROR_RETURN);
  218.                         }
  219.                     }
  220.                 }
  221.             else {
  222.                 // no tokens, so add this one and cycle to previous state
  223.                 $return[$returnpos][1.= $char;
  224.                 array_pop($stack);
  225.             }
  226.         while (++$pos strlen($text));
  227.         return $return;
  228.     // end func _quoteAndCommaParser
  229.     
  230.     /**
  231.      * Retrieve the state off of a state stack for the Quote and Comma Parser
  232.      * @param  array  $stack    The parser state stack
  233.      * @author Greg Beaver <cellog@users.sourceforge.net>
  234.      * @access private
  235.      */
  236.     function _getQACEvent($stack)
  237.     {
  238.         return array_pop($stack);
  239.     // end func _getQACEvent
  240.  
  241.     /**
  242.     * Returns a formatted string of the object
  243.     * @param    object  $obj    Container object to be output as string
  244.     * @access   public
  245.     * @return   string 
  246.     */
  247.     function toString(&$obj)
  248.     {
  249.         static $childrenCount$commaString;
  250.  
  251.         if (!isset($string)) {
  252.             $string '';
  253.         }
  254.         switch ($obj->type{
  255.             case 'blank':
  256.                 $string "\n";
  257.                 break;
  258.             case 'comment':
  259.                 $string ';'.$obj->content."\n";
  260.                 break;
  261.             case 'directive':
  262.                 $count $obj->parent->countChildren('directive'$obj->name);
  263.                 $content $obj->content;
  264.                 if ($content === false{
  265.                     $content '0';
  266.                 elseif ($content === true{
  267.                     $content '1';
  268.                 elseif (strlen(trim($content)) strlen($content||
  269.                           strpos($content','!== false ||
  270.                           strpos($content';'!== false ||
  271.                           strpos($content'"'!== false ||
  272.                           strpos($content'%'!== false{
  273.                     $content '"'.addslashes($content).'"';          
  274.                 }
  275.                 if ($count > 1{
  276.                     // multiple values for a directive are separated by a comma
  277.                     if (isset($childrenCount[$obj->name])) {
  278.                         $childrenCount[$obj->name]++;
  279.                     else {
  280.                         $childrenCount[$obj->name= 0;
  281.                         $commaString[$obj->name$obj->name.'=';
  282.                     }
  283.                     if ($childrenCount[$obj->name== $count-1{
  284.                         // Clean the static for future calls to toString
  285.                         $string .= $commaString[$obj->name].$content."\n";
  286.                         unset($childrenCount[$obj->name]);
  287.                         unset($commaString[$obj->name]);
  288.                     else {
  289.                         $commaString[$obj->name.= $content.', ';
  290.                     }
  291.                 else {
  292.                     $string $obj->name.'='.$content."\n";
  293.                 }
  294.                 break;
  295.             case 'section':
  296.                 if (!$obj->isRoot()) {
  297.                     $string '['.$obj->name."]\n";
  298.                 }
  299.                 if (count($obj->children> 0{
  300.                     for ($i = 0; $i count($obj->children)$i++{
  301.                         $string .= $this->toString($obj->getChild($i));
  302.                     }
  303.                 }
  304.                 break;
  305.             default:
  306.                 $string '';
  307.         }
  308.         return $string;
  309.     // end func toString
  310. // end class Config_Container_IniCommented
  311. ?>

Documentation generated on Mon, 11 Mar 2019 13:51:34 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.