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.18 2004/10/19 00:57:59 ryansking 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*(.*)\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.  
  140.         // return information
  141.         $return = array();
  142.         $returnpos = 0;
  143.         $returntype 'normal';
  144.  
  145.         // initialize
  146.         array_push($stack'normal');
  147.         $pos = 0; // position in $text
  148.  
  149.         do {
  150.             $char $text{$pos};
  151.             $state $this->_getQACEvent($stack);
  152.  
  153.             if ($tokens[$state]{
  154.                 if (in_array($char$tokens[$state])) {
  155.                     switch($events[$state][$char]{
  156.                         case 'quote' :
  157.                             if ($state == 'normal' &&
  158.                                 isset($return[$returnpos]&&
  159.                                 !empty($return[$returnpos][1])) {
  160.                                 return PEAR::raiseError("invalid ini syntax, quotes cannot follow text '$text'",
  161.                                                         nullPEAR_ERROR_RETURN);
  162.                             }
  163.                             if ($returnpos >= 0 && isset($return[$returnpos])) {
  164.                                 // trim any unnecessary whitespace in earlier entries
  165.                                 $return[$returnpos][1trim($return[$returnpos][1]);
  166.                             else {
  167.                                 $returnpos++;
  168.                             }
  169.                             $return[$returnpos= array('normal''');
  170.                             array_push($stack'quote');
  171.                             continue 2;
  172.                         break;
  173.                         case 'comment' :
  174.                             // comments go to the end of the line, so we are done
  175.                             $return[++$returnpos= array('comment'substr($text$pos));
  176.                             return $return;
  177.                         break;
  178.                         case 'after_quote' :
  179.                             array_push($stack'after_quote');
  180.                         break;
  181.                         case 'escape' :
  182.                             // don't save the first slash
  183.                             array_push($stack'escape');
  184.                             continue 2;
  185.                         break;
  186.                         case 'normal' :
  187.                         // start a new segment
  188.                             if ($state == 'normal'{
  189.                                 $returnpos++;
  190.                                 continue 2;
  191.                             else {
  192.                                 while ($state != 'normal'{
  193.                                     array_pop($stack);
  194.                                     $state $this->_getQACEvent($stack);
  195.                                 }
  196.                             }
  197.                         break;
  198.                         default :
  199.                             PEAR::raiseError("::_quoteAndCommaParser oops, state missing"nullPEAR_ERROR_DIE);
  200.                         break;
  201.                     }
  202.                 else {
  203.                     if ($state != 'after_quote'{
  204.                         if (!isset($return[$returnpos])) {
  205.                             $return[$returnpos= array('normal''');
  206.                         }
  207.                         // add this character to the current ini segment if non-empty, or if in a quote
  208.                         if ($state == 'quote'{
  209.                             $return[$returnpos][1.= $char;
  210.                         elseif (!empty($return[$returnpos][1]||
  211.                                  (empty($return[$returnpos][1]&& trim($char!= '')) {
  212.                             if (!isset($return[$returnpos])) {
  213.                                 $return[$returnpos= array('normal''');
  214.                             }
  215.                             $return[$returnpos][1.= $char;
  216.                         }
  217.                     else {
  218.                         if (trim($char!= ''{
  219.                             return PEAR::raiseError("invalid ini syntax, text after a quote not allowed '$text'",
  220.                                                     nullPEAR_ERROR_RETURN);
  221.                         }
  222.                     }
  223.                 }
  224.             else {
  225.                 // no tokens, so add this one and cycle to previous state
  226.                 $return[$returnpos][1.= $char;
  227.                 array_pop($stack);
  228.             }
  229.         while (++$pos strlen($text));
  230.         return $return;
  231.     // end func _quoteAndCommaParser
  232.     
  233.     /**
  234.      * Retrieve the state off of a state stack for the Quote and Comma Parser
  235.      * @param  array  $stack    The parser state stack
  236.      * @author Greg Beaver <cellog@users.sourceforge.net>
  237.      * @access private
  238.      */
  239.     function _getQACEvent($stack)
  240.     {
  241.         return array_pop($stack);
  242.     // end func _getQACEvent
  243.  
  244.     /**
  245.     * Returns a formatted string of the object
  246.     * @param    object  $obj    Container object to be output as string
  247.     * @access   public
  248.     * @return   string 
  249.     */
  250.     function toString(&$obj)
  251.     {
  252.         static $childrenCount$commaString;
  253.  
  254.         if (!isset($string)) {
  255.             $string '';
  256.         }
  257.         switch ($obj->type{
  258.             case 'blank':
  259.                 $string "\n";
  260.                 break;
  261.             case 'comment':
  262.                 $string ';'.$obj->content."\n";
  263.                 break;
  264.             case 'directive':
  265.                 $count $obj->parent->countChildren('directive'$obj->name);
  266.                 $content $obj->content;
  267.                 if ($content === false{
  268.                     $content '0';
  269.                 elseif ($content === true{
  270.                     $content '1';
  271.                 elseif (strlen(trim($content)) strlen($content||
  272.                           strpos($content','!== false ||
  273.                           strpos($content';'!== false ||
  274.                           strpos($content'"'!== false ||
  275.                           strpos($content'%'!== false{
  276.                     $content '"'.addslashes($content).'"';          
  277.                 }
  278.                 if ($count > 1{
  279.                     // multiple values for a directive are separated by a comma
  280.                     if (isset($childrenCount[$obj->name])) {
  281.                         $childrenCount[$obj->name]++;
  282.                     else {
  283.                         $childrenCount[$obj->name= 0;
  284.                         $commaString[$obj->name$obj->name.'=';
  285.                     }
  286.                     if ($childrenCount[$obj->name== $count-1{
  287.                         // Clean the static for future calls to toString
  288.                         $string .= $commaString[$obj->name].$content."\n";
  289.                         unset($childrenCount[$obj->name]);
  290.                         unset($commaString[$obj->name]);
  291.                     else {
  292.                         $commaString[$obj->name.= $content.', ';
  293.                     }
  294.                 else {
  295.                     $string $obj->name.'='.$content."\n";
  296.                 }
  297.                 break;
  298.             case 'section':
  299.                 if (!$obj->isRoot()) {
  300.                     $string '['.$obj->name."]\n";
  301.                 }
  302.                 if (count($obj->children> 0{
  303.                     for ($i = 0; $i count($obj->children)$i++{
  304.                         $string .= $this->toString($obj->getChild($i));
  305.                     }
  306.                 }
  307.                 break;
  308.             default:
  309.                 $string '';
  310.         }
  311.         return $string;
  312.     // end func toString
  313. // end class Config_Container_IniCommented
  314. ?>

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