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

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