/www/devel/upgrade/upms_server/lib/pear/Config/Container/IniFile.php

https://github.com/orchestra-io/sample-openx · PHP · 169 lines · 109 code · 6 blank · 54 comment · 21 complexity · c1c475156cd68b4bd8dd0576a157269c MD5 · raw file

  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. // | Authors: Bertrand Mansion <bmansion@mamasam.com> |
  16. // +----------------------------------------------------------------------+
  17. //
  18. // $Id: IniFile.php 18922 2008-04-16 08:56:47Z monique.szpak@openx.org $
  19. /**
  20. * Config parser for PHP .ini files
  21. * Faster because it uses parse_ini_file() but get rid of comments,
  22. * quotes, types and converts On, Off, True, False, Yes, No to 0 and 1.
  23. *
  24. * @author Bertrand Mansion <bmansion@mamasam.com>
  25. * @package Config
  26. */
  27. class Config_Container_IniFile {
  28. /**
  29. * This class options
  30. * Not used at the moment
  31. *
  32. * @var array
  33. */
  34. var $options = array();
  35. /**
  36. * Constructor
  37. *
  38. * @access public
  39. * @param string $options (optional)Options to be used by renderer
  40. */
  41. function Config_Container_IniFile($options = array())
  42. {
  43. $this->options = $options;
  44. } // end constructor
  45. /**
  46. * Parses the data of the given configuration file
  47. *
  48. * @access public
  49. * @param string $datasrc path to the configuration file
  50. * @param object $obj reference to a config object
  51. * @return mixed returns a PEAR_ERROR, if error occurs or true if ok
  52. */
  53. function &parseDatasrc($datasrc, &$obj)
  54. {
  55. if (!file_exists($datasrc)) {
  56. return PEAR::raiseError("Datasource file does not exist.", null, PEAR_ERROR_RETURN);
  57. }
  58. $currentSection =& $obj->container;
  59. $confArray = parse_ini_file($datasrc, true);
  60. if (!$confArray) {
  61. return PEAR::raiseError("File '$datasrc' does not contain configuration data.", null, PEAR_ERROR_RETURN);
  62. }
  63. foreach ($confArray as $key => $value) {
  64. if (is_array($value)) {
  65. $currentSection =& $obj->container->createSection($key);
  66. foreach ($value as $directive => $content) {
  67. // try to split the value if comma found
  68. if (strpos($content, '"') === false) {
  69. $values = preg_split('/\s*,\s+/', $content);
  70. if (count($values) > 1) {
  71. foreach ($values as $k => $v) {
  72. $currentSection->createDirective($directive, $v);
  73. }
  74. } else {
  75. $currentSection->createDirective($directive, $content);
  76. }
  77. } else {
  78. $currentSection->createDirective($directive, $content);
  79. }
  80. }
  81. } else {
  82. $currentSection->createDirective($key, $value);
  83. }
  84. }
  85. return true;
  86. } // end func parseDatasrc
  87. /**
  88. * Returns a formatted string of the object
  89. * @param object $obj Container object to be output as string
  90. * @access public
  91. * @return string
  92. */
  93. function toString(&$obj)
  94. {
  95. static $childrenCount, $commaString;
  96. if (!isset($string)) {
  97. $string = '';
  98. }
  99. switch ($obj->type) {
  100. case 'blank':
  101. $string = "\n";
  102. break;
  103. case 'comment':
  104. $string = ';'.$obj->content."\n";
  105. break;
  106. case 'directive':
  107. $count = $obj->parent->countChildren('directive', $obj->name);
  108. $content = $obj->content;
  109. if ($content === false) {
  110. $content = '0';
  111. } elseif ($content === true) {
  112. $content = '1';
  113. } elseif (strlen(trim($content)) < strlen($content) ||
  114. strpos($content, ',') !== false ||
  115. strpos($content, ';') !== false ||
  116. strpos($content, '"') !== false ||
  117. strpos($content, '%') !== false ||
  118. strpos($content, '~') !== false ||
  119. strpos($content, '!') !== false ||
  120. strpos($content, '|') !== false ||
  121. strpos($content, '&') !== false ||
  122. strpos($content, '(') !== false ||
  123. strpos($content, ')') !== false ||
  124. $content === 'none') {
  125. $content = '"'.addslashes($content).'"';
  126. }
  127. if ($count > 1) {
  128. // multiple values for a directive are separated by a comma
  129. if (isset($childrenCount[$obj->name])) {
  130. $childrenCount[$obj->name]++;
  131. } else {
  132. $childrenCount[$obj->name] = 0;
  133. $commaString[$obj->name] = $obj->name.'=';
  134. }
  135. if ($childrenCount[$obj->name] == $count-1) {
  136. // Clean the static for future calls to toString
  137. $string .= $commaString[$obj->name].$content."\n";
  138. unset($childrenCount[$obj->name]);
  139. unset($commaString[$obj->name]);
  140. } else {
  141. $commaString[$obj->name] .= $content.', ';
  142. }
  143. } else {
  144. $string = $obj->name.'='.$content."\n";
  145. }
  146. break;
  147. case 'section':
  148. if (!$obj->isRoot()) {
  149. $string = '['.$obj->name."]\n";
  150. }
  151. if (count($obj->children) > 0) {
  152. for ($i = 0; $i < count($obj->children); $i++) {
  153. $string .= $this->toString($obj->getChild($i));
  154. }
  155. }
  156. break;
  157. default:
  158. $string = '';
  159. }
  160. return $string;
  161. } // end func toString
  162. } // end class Config_Container_IniFile
  163. ?>