class.config.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /*
  3. * Project: template_lite, a smarter template engine
  4. * File: class.config.php
  5. * Author: Paul Lockaby <paul@paullockaby.com>, Mark Dickenson <akapanamajack@sourceforge.net>
  6. * Copyright: 2003,2004,2005 by Paul Lockaby, 2005,2006 Mark Dickenson
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. * The latest version of template_lite can be obtained from:
  23. * http://templatelite.sourceforge.net
  24. *
  25. */
  26. class config {
  27. var $overwrite = false; // overwrite variables of the same name? if false, an array will be created
  28. var $booleanize = true; // turn true/false, yes/no, on/off, into 1/0
  29. var $fix_new_lines = true; // turns \r\n into \n?
  30. var $read_hidden = true; // read hidden sections?
  31. var $_db_qstr_regexp = null;
  32. var $_bool_true_regexp = null;
  33. var $_bool_false_regexp = null;
  34. var $_qstr_regexp = null;
  35. function config()
  36. {
  37. $this->_db_qstr_regexp = '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"';
  38. $this->_bool_true_regexp = 'true|yes|on';
  39. $this->_bool_false_regexp = 'false|no|off';
  40. $this->_qstr_regexp = '(?:' . $this->_db_qstr_regexp . '|' . $this->_bool_true_regexp . '|' . $this->_bool_false_regexp . ')';
  41. }
  42. function config_load($file, $section_name = null, $var_name = null)
  43. {
  44. $_result = array();
  45. $contents = file_get_contents($file);
  46. if (empty($contents))
  47. {
  48. die("Could not open $file");
  49. }
  50. // insert new line into beginning of file
  51. $contents = "\n" . $contents;
  52. // fix new-lines
  53. if ($this->fix_new_lines)
  54. {
  55. $contents = str_replace("\r\n","\n",$contents);
  56. }
  57. // match globals
  58. if (preg_match("/^(.*?)(\n\[|\Z)/s", $contents, $match))
  59. {
  60. $_result["globals"] = $this->_parse_config_section($match[1]);
  61. }
  62. // match sections
  63. if (preg_match_all("/^\[(.*?)\]/m", $contents, $match))
  64. {
  65. foreach ($match[1] as $section)
  66. {
  67. if ($section{0} == '.' && !$this->read_hidden)
  68. {
  69. continue;
  70. }
  71. preg_match("/\[".preg_quote($section)."\](.*?)(\n\[|\Z)/s",$contents,$match);
  72. if ($section{0} == '.')
  73. {
  74. $section = substr($section, 1);
  75. }
  76. $_result[$section] = $this->_parse_config_section($match[1]);
  77. }
  78. }
  79. if (!empty($var_name))
  80. {
  81. if (empty($section_name))
  82. {
  83. return $_result["globals"][$var_name];
  84. }
  85. else
  86. {
  87. if(isset($_result[$section_name][$var_name]))
  88. {
  89. return $_result[$section_name][$var_name];
  90. }
  91. else
  92. {
  93. return array();
  94. }
  95. }
  96. }
  97. else
  98. {
  99. if (empty($section_name))
  100. {
  101. return $_result;
  102. }
  103. else
  104. {
  105. if(isset($_result[$section_name]))
  106. {
  107. return $_result[$section_name];
  108. }
  109. else
  110. {
  111. return array();
  112. }
  113. }
  114. }
  115. }
  116. function _parse_config_section($body)
  117. {
  118. $_result = array();
  119. preg_match_all('!(\n\s*[a-zA-Z0-9_]+)\s*=\s*(' . $this->_qstr_regexp . ')!s', $body, $ini);
  120. $keys = $ini[1];
  121. $values = $ini[2];
  122. for($i = 0, $for_max = count($ini[0]); $i < $for_max; $i++)
  123. {
  124. if ($this->booleanize)
  125. {
  126. if (preg_match('/^(' . $this->_bool_true_regexp . ')$/i', $values[$i]))
  127. {
  128. $values[$i] = true;
  129. }
  130. elseif (preg_match('/^(' . $this->_bool_false_regexp . ')$/i', $values[$i]))
  131. {
  132. $values[$i] = false;
  133. }
  134. }
  135. if (!is_numeric($values[$i]) && !is_bool($values[$i]))
  136. {
  137. $values[$i] = str_replace("\n",'',stripslashes(substr($values[$i], 1, -1)));
  138. }
  139. if ($this->overwrite || !isset($_result[trim($keys[$i])]))
  140. {
  141. $_result[trim($keys[$i])] = $values[$i];
  142. }
  143. else
  144. {
  145. if (!is_array($_result[trim($keys[$i])]))
  146. {
  147. $_result[trim($keys[$i])] = array($_result[trim($keys[$i])]);
  148. }
  149. $_result[trim($keys[$i])][] = $values[$i];
  150. }
  151. }
  152. return $_result;
  153. }
  154. }
  155. ?>