Source for file IniCommented.php
Documentation is available at IniCommented.php
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Author: Bertrand Mansion <bmansion@mamasam.com> |
// +----------------------------------------------------------------------+
// $Id: IniCommented.php 306554 2010-12-21 20:04:20Z cweiske $
* Config parser for PHP .ini files with comments
* @author Bertrand Mansion <bmansion@mamasam.com>
* Options for this class:
* - linebreak - Character to use as new line break when serializing
* @param string $options (optional)Options to be used by renderer
* Parses the data of the given configuration file
* @param string $datasrc path to the configuration file
* @param object $obj reference to a config object
* @return mixed returns a PEAR_ERROR, if error occurs or true if ok
'Datasource file does not exist.',
'File could not be read',
$currentSection = & $obj->container;
foreach ($lines as $line) {
if (preg_match('/^\s*;(.*?)\s*$/', $line, $match)) {
$currentSection->createComment ($match[1 ]);
$currentSection->createBlank ();
} elseif (preg_match('/^\s*([a-zA-Z0-9_\-\.\s:]*)\s*=\s*(.*)\s*$/', $line, $match)) {
$values = $this->_quoteAndCommaParser ($match[2 ]);
if (PEAR ::isError ($values)) {
return PEAR ::raiseError ($values);
foreach($values as $value) {
if ($value[0 ] == 'normal') {
$currentSection->createDirective (trim($match[1 ]), $value[1 ]);
if ($value[0 ] == 'comment') {
$currentSection->createComment (substr($value[1 ], 1 ));
} elseif (preg_match('/^\s*\[\s*(.*)\s*\]\s*$/', $line, $match)) {
$currentSection = & $obj->container ->createSection ($match[1 ]);
return PEAR ::raiseError (" Syntax error in '$datasrc' at line $n." , null , PEAR_ERROR_RETURN );
} // end func parseDatasrc
* Quote and Comma Parser for INI files
* This function allows complex values such as:
* mydirective = "Item, number \"1\"", Item 2 ; "This" is really, really tricky
* @param string $text value of a directive to parse for quotes/multiple values
* @return array The array returned contains multiple values, if any (unquoted literals
* to be used as is), and a comment, if any. The format of the array is:
* array(array('normal', 'first value'),
* array('normal', 'next value'),...
* array('comment', '; comment with leading ;'))
* @author Greg Beaver <cellog@users.sourceforge.net>
function _quoteAndCommaParser ($text)
$emptyNode[0 ][0 ] = 'normal';
$tokens['normal'] = array ('"', ';', ',');
$tokens['quote'] = array ('"', '\\');
$tokens['escape'] = false; // cycle
$tokens['after_quote'] = array (',', ';');
$events['normal'] = array ('"' => 'quote', ';' => 'comment', ',' => 'normal');
$events['quote'] = array ('"' => 'after_quote', '\\' => 'escape');
$events['after_quote'] = array (',' => 'normal', ';' => 'comment');
$pos = 0; // position in $text
$state = $this->_getQACEvent ($stack);
switch($events[$state][$char]) {
if ($state == 'normal' &&
isset ($return[$returnpos]) &&
!empty ($return[$returnpos][1 ])) {
'invalid ini syntax, quotes cannot follow'
if ($returnpos >= 0 && isset ($return[$returnpos])) {
// trim any unnecessary whitespace in earlier entries
$return[$returnpos][1 ] = trim($return[$returnpos][1 ]);
$return[$returnpos] = array ('normal', '');
// comments go to the end of the line, so we are done
$return[++ $returnpos] = array ('comment', substr($text, $pos));
// don't save the first slash
if ($state == 'normal') {
while ($state != 'normal') {
$state = $this->_getQACEvent ($stack);
"::_quoteAndCommaParser oops, state missing",
if ($state != 'after_quote') {
if (!isset ($return[$returnpos])) {
$return[$returnpos] = array ('normal', '');
// add this character to the current ini segment if
// non-empty, or if in a quote
$return[$returnpos][1 ] .= $char;
} elseif (!empty ($return[$returnpos][1 ]) ||
(empty ($return[$returnpos][1 ]) && trim($char) != '')) {
if (!isset ($return[$returnpos])) {
$return[$returnpos] = array ('normal', '');
$return[$returnpos][1 ] .= $char;
if (strcasecmp('true', $return[$returnpos][1 ]) == 0 ) {
$return[$returnpos][1 ] = '1';
} elseif (strcasecmp('false', $return[$returnpos][1 ]) == 0 ) {
$return[$returnpos][1 ] = '';
'invalid ini syntax, text after a quote'
. " not allowed '$text'" ,
// no tokens, so add this one and cycle to previous state
$return[$returnpos][1 ] .= $char;
} while (++ $pos < strlen($text));
} // end func _quoteAndCommaParser
* Retrieve the state off of a state stack for the Quote and Comma Parser
* @param array $stack The parser state stack
* @author Greg Beaver <cellog@users.sourceforge.net>
function _getQACEvent ($stack)
} // end func _getQACEvent
* Returns a formatted string of the object
* @param object $obj Container object to be output as string
static $childrenCount, $commaString;
$string = $this->options['linebreak'];
$string = ';'. $obj->content . $this->options['linebreak'];
$count = $obj->parent ->countChildren ('directive', $obj->name );
$content = $obj->content;
if ($content === false ) {
} elseif ($content === true ) {
strpos($content, ',') !== false ||
strpos($content, ';') !== false ||
strpos($content, '=') !== false ||
strpos($content, '"') !== false ||
strpos($content, '%') !== false ||
strpos($content, '~') !== false ||
strpos($content, '!') !== false ||
strpos($content, '|') !== false ||
strpos($content, '&') !== false ||
strpos($content, '(') !== false ||
strpos($content, ')') !== false ||
// multiple values for a directive are separated by a comma
if (isset ($childrenCount[$obj->name ])) {
$childrenCount[$obj->name ]++;
$childrenCount[$obj->name ] = 0;
$commaString[$obj->name ] = $obj->name. ' = ';
if ($childrenCount[$obj->name ] == $count-1 ) {
// Clean the static for future calls to toString
$string .= $commaString[$obj->name ] . $content
unset ($childrenCount[$obj->name ]);
unset ($commaString[$obj->name ]);
$commaString[$obj->name ] .= $content. ', ';
$string = $obj->name. ' = '. $content . $this->options['linebreak'];
$string = '[' . $obj->name . ']' . $this->options['linebreak'];
if (count($obj->children ) > 0 ) {
for ($i = 0; $i < count($obj->children ); $i++ ) {
$string .= $this->toString($obj->getChild ($i));
} // end class Config_Container_IniCommented
Documentation generated on Mon, 11 Mar 2019 15:42:04 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|