<?php/********************************************************************************************
* BlueShoes Framework; This file is part of the php application framework.
* NOTE: This code is stripped (obfuscated). To get the clean documented code goto 
*       www.blueshoes.org and register for the free open source *DEVELOPER* version or 
*       buy the commercial version.
*       
*       In case you've already got the developer version, then this is one of the few 
*       packages/classes that is only available to *PAYING* customers.
*       To get it go to www.blueshoes.org and buy a commercial version.
* 
* @copyright www.blueshoes.org
* @author    Samuel Blume <sam at blueshoes dot org>
* @author    Andrej Arn <andrej at blueshoes dot org>
*/?><?php
define('BS_INIHANDLER_VERSION',      '4.5.$Revision: 1.4 $' );if (!@$APP) require_once($_SERVER['DOCUMENT_ROOT'] . $GLOBALS['APP']['path']['diffFromDocRootToGlobalConf'] . 'global.conf.php');define('BS_INIHANDLER_UNQUOTE_NONE',   0);define('BS_INIHANDLER_UNQUOTE_DOUBLE', 1);define('BS_INIHANDLER_UNQUOTE_SINGLE', 2);define('BS_INIHANDLER_UNQUOTE_ALL',    3);define('BS_INIHANDLER_HEAD_COMMENT',   "# Bs_IniHandler File generated by www.blueshoes.org\n\n");class Bs_IniHandler extends Bs_Object {var $commentChars = array('#', '/', ';');var $unQuote = BS_INIHANDLER_UNQUOTE_ALL;var $_sections;var $_params;var $_comments;var $_fileFullPath;function Bs_IniHandler($fileFullPath='') {parent::Bs_Object(); if (!empty($fileFullPath)) {$this->loadFile($fileFullPath);}
}
function loadFile($fileFullPath) {$this->reset();if (!file_exists($fileFullPath)) {Bs_Error::setError("File doesn't exists: [{$fileFullPath}]", 'ERROR');return FALSE;}
if (!is_readable($fileFullPath)) {Bs_Error::setError("File is not readable: [{$fileFullPath}]", 'ERROR');return FALSE;}
$this->_fileFullPath = $fileFullPath;$fileContent = file($fileFullPath);$this->_parseFromArray($fileContent);return TRUE;}
function loadString($str) {$this->reset();$arr = explode("\n", $str);$this->_parseFromArray($arr);return TRUE;}
function setQuoteHandling($mode=BS_INIHANDLER_UNQUOTE_ALL) {$this->unQuote = $mode;}
function _parseFromArray($arr) {$this->_comments = array();$comment = array();$section = '';foreach($arr as $line) {$sectionFound = $valueFound = FALSE;$param = array('key'=>'', 'val'=>'');do { $line = trim($line);if (empty($line)) break; if (in_array($line[0], $this->commentChars)) {$comment[] = $line;break; }
if ($line[0] === '[') {if (preg_match('/\[(.*)\]/', $line, $ar)) {$section = $ar[1];$sectionFound = TRUE;break; }
}
$tmp = explode('=', $line);if (!is_array($tmp)) break; if (sizeOf($tmp) < 2) {$comment[] = @$tmp[0];break;}
$param['key'] = trim($tmp[0]);array_shift($tmp);if (sizeOf($tmp)>1) $tmp[0] = implode('=', $tmp);$param['val'] = isSet($tmp[0]) ? trim($tmp[0]) : '';if (empty($param['val'])) {$valueFound = TRUE;break; }
$unQuote = '';if ($this->unQuote & BS_INIHANDLER_UNQUOTE_DOUBLE) $unQuote .= '"';if ($this->unQuote & BS_INIHANDLER_UNQUOTE_SINGLE) $unQuote .= "'";if (empty($unQuote)) {$valueFound = TRUE;break; }
$regEx = '/^(['.$unQuote.']?)(.*)\1$/';if (preg_match($regEx, $param['val'], $ar)) {$param['val'] = $ar[2];$valueFound = TRUE;break; } else {break; }
} while(FALSE);if ($sectionFound) {$this->_sections[] = $section;if (!empty($comment)) $this->_comments[$section] = $comment;$comment = array();} else if ($valueFound) {$this->_params[$section][$param['key']] = $param['val'];if (!empty($comment)) $this->_comments[$section .'__'. $param['key']] = $comment;$comment = array();}
} if (!empty($comment)) $this->_comments['__LastComment__'] = $comment;}
function toString() {$outStr = BS_INIHANDLER_HEAD_COMMENT;foreach ($this->_params as $section => $params) {if (isSet($this->_comments[$section])) {$oldHeaderFound = FALSE;foreach ($this->_comments[$section] as $comment) {if (!$oldHeaderFound AND (trim(BS_INIHANDLER_HEAD_COMMENT) == trim($comment))) {$oldHeaderFound = TRUE;continue;}
$outStr .= "{$comment}\n";}
}
$outStr .= "[".$section."]\n";foreach ($params as $key => $value) {if (isSet($this->_comments[$section .'__'. $key])) {foreach ($this->_comments[$section .'__'. $key] as $comment) $outStr .= "  {$comment}\n";}
$outStr .= "  " .$key. " = " .$value. "\n";}
$outStr .= "\n";}
if (isSet($this->_comments['__LastComment__'])) {foreach ($this->_comments['__LastComment__'] as $comment) $outStr .= "{$comment}\n";}
return $outStr;}
function saveFile($fileFullPath=NULL) {$outStr = $this->toString();if (empty($fileFullPath)) $fileFullPath = $this->_fileFullPath;if (empty($fileFullPath)) return FALSE;if (!$fp = fopen($fileFullPath, 'wb')) {Bs_Error::setError("Failed open the file for writing: [{$fileFullPath}]", 'ERROR');return FALSE;}
if (!fwrite($fp, $outStr)){Bs_Error::setError("Failed to write (but was able to open) the file: [{$fileFullPath}]", 'ERROR');return FALSE;}
@fclose($fp);return TRUE;}
function reset() {unset($this->_sections);unset($this->_params);unset($this->_fileFullPath);}
function get($section=NULL, $key=NULL) {if (is_null($section)) return $this->_params;if (!isSet($this->_params[$section])) return NULL; if (is_null($key))     return $this->_params[$section];if (!isSet($this->_params[$section][$key])) return NULL; return $this->_params[$section][$key];}
function has($section, $key=NULL) {if (is_null($key)) {return (isSet($this->_params[$section])); } else {return (isSet($this->_params[$section]) && isSet($this->_params[$section][$key]));}
}
function set($section, $key, $value) {if (!isSet($this->_params))           $this->_params = array();if (!isSet($this->_params[$section])) $this->_params[$section] = array();$this->_params[$section][$key] = $value;}
}
if (basename($_SERVER['PHP_SELF']) == 'Bs_IniHandler.class.php') {$header = BS_INIHANDLER_HEAD_COMMENT;$testData =<<<EOD
   # Comment 1
     $header
   # comment 2
   []
   globalData = foo
   
   # comment A
   [Some test data]
      # comment B
      one = hallo
      two = "hallo"
      # comment C
      food = "Tom's Pizza = 'good stuff'"
      more food = Sam's Pizza's = 'best stuff'
      empty = ""
      noVal =
   # comment D
   [more test data]
      one = hi
      two = 'hi'
      food = 'Pizza = "good"'
      empty = ''
      noVal
   # comment E
EOD;
$iniHandler = new Bs_IniHandler();$iniHandler->loadString($testData);XR_dump($iniHandler->toString(), __LINE__, '', __FILE__);}
?>