PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/template_engines_bench/libs/smarty-light/src/class.config.php

https://github.com/limb-php-framework/limb-tools
PHP | 128 lines | 82 code | 10 blank | 36 comment | 26 complexity | e5e54037a8d84b03a32303a05a03c7af MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1
  1. <?php
  2. /*
  3. * Project: Smarty-Light, a smarter template engine
  4. * File: class.config.php
  5. * Author: Paul Lockaby <paul@paullockaby.com>
  6. * Version: 2.2.11
  7. * Copyright: 2003,2004,2005 by Paul Lockaby
  8. * Credit: This work is a light version of Smarty: the PHP compiling
  9. * template engine, v2.5.0-CVS. Smarty was originally
  10. * programmed by Monte Ohrt and Andrei Zmievski and can be
  11. * found at http://smarty.php.net
  12. *
  13. * This library is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU Lesser General Public
  15. * License as published by the Free Software Foundation; either
  16. * version 2.1 of the License, or (at your option) any later version.
  17. *
  18. * This library is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * Lesser General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public
  24. * License along with this library; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. *
  27. * You may contact the author of Smarty-Light by e-mail at:
  28. * paul@paullockaby.com
  29. *
  30. * The latest version of Smarty-Light can be obtained from:
  31. * http://www.paullockaby.com/projects/smarty-light
  32. *
  33. */
  34. class config {
  35. var $overwrite = false; // overwrite variables of the same name? if false, an array will be created
  36. var $booleanize = true; // turn true/false, yes/no, on/off, into 1/0
  37. var $fix_new_lines = true; // turns \r\n into \n?
  38. var $read_hidden = true; // read hidden sections?
  39. var $_db_qstr_regexp = null;
  40. var $_bool_true_regexp = null;
  41. var $_bool_false_regexp = null;
  42. var $_qstr_regexp = null;
  43. function config() {
  44. $this->_db_qstr_regexp = '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"';
  45. $this->_bool_true_regexp = 'true|yes|on';
  46. $this->_bool_false_regexp = 'false|no|off';
  47. $this->_qstr_regexp = '(?:' . $this->_db_qstr_regexp . '|' . $this->_bool_true_regexp . '|' . $this->_bool_false_regexp . ')';
  48. }
  49. function config_load($file, $section_name = null, $var_name = null) {
  50. $_result = array();
  51. $contents = file_get_contents($file);
  52. if (empty($contents))
  53. die("Could not open $file");
  54. // insert new line into beginning of file
  55. $contents = "\n" . $contents;
  56. // fix new-lines
  57. if ($this->fix_new_lines)
  58. $contents = str_replace("\r\n","\n",$contents);
  59. // match globals
  60. if (preg_match("/^(.*?)(\n\[|\Z)/s", $contents, $match))
  61. $_result["globals"] = $this->_parse_config_section($match[1]);
  62. // match sections
  63. if (preg_match_all("/^\[(.*?)\]/m", $contents, $match)) {
  64. foreach ($match[1] as $section) {
  65. if ($section{0} == '.' && !$this->read_hidden)
  66. continue;
  67. preg_match("/\[".preg_quote($section)."\](.*?)(\n\[|\Z)/s",$contents,$match);
  68. if ($section{0} == '.')
  69. $section = substr($section, 1);
  70. $_result[$section] = $this->_parse_config_section($match[1]);
  71. }
  72. }
  73. if (!empty($var_name)) {
  74. if (empty($section_name)) {
  75. return $_result["globals"][$var_name];
  76. } else {
  77. if(isset($_result[$section_name][$var_name]))
  78. return $_result[$section_name][$var_name];
  79. else
  80. return array();
  81. }
  82. } else {
  83. if (empty($section_name)) {
  84. return $_result;
  85. } else {
  86. if(isset($_result[$section_name]))
  87. return $_result[$section_name];
  88. else
  89. return array();
  90. }
  91. }
  92. }
  93. function _parse_config_section($body) {
  94. $_result = array();
  95. preg_match_all('!(\n\s*[a-zA-Z0-9_]+)\s*=\s*(' . $this->_qstr_regexp . ')!s', $body, $ini);
  96. $keys = $ini[1];
  97. $values = $ini[2];
  98. for($i = 0, $for_max = count($ini[0]); $i < $for_max; $i++) {
  99. if ($this->booleanize) {
  100. if (preg_match('/^(' . $this->_bool_true_regexp . ')$/i', $values[$i]))
  101. $values[$i] = true;
  102. elseif (preg_match('/^(' . $this->_bool_false_regexp . ')$/i', $values[$i]))
  103. $values[$i] = false;
  104. }
  105. if (!is_numeric($values[$i]) && !is_bool($values[$i]))
  106. $values[$i] = str_replace("\n",'',stripslashes(substr($values[$i], 1, -1)));
  107. if ($this->overwrite || !isset($_result[trim($keys[$i])])) {
  108. $_result[trim($keys[$i])] = $values[$i];
  109. } else {
  110. if (!is_array($_result[trim($keys[$i])]))
  111. $_result[trim($keys[$i])] = array($_result[trim($keys[$i])]);
  112. $_result[trim($keys[$i])][] = $values[$i];
  113. }
  114. }
  115. return $_result;
  116. }
  117. }
  118. ?>