Open Journal Systems  3.3.0
ConfigParser.inc.php
1 <?php
2 
17 class ConfigParser {
18 
20  var $content;
21 
25  function __construct() {
26  }
27 
34  static function &readConfig($file) {
35  $configData = array();
36  $currentSection = false;
37  $falseValue = false;
38 
39  if (!file_exists($file) || !is_readable($file)) {
40  return $falseValue;
41  }
42 
43  $fp = fopen($file, 'rb');
44  if (!$fp) {
45  return $falseValue;
46  }
47 
48  while (!feof($fp)) {
49  $line = fgets($fp, 1024);
50  $line = trim($line);
51  if ($line === '' || strpos($line, ';') === 0) {
52  // Skip empty or commented line
53  continue;
54  }
55 
56  if (preg_match('/^\[(.+)\]/', $line, $matches)) {
57  // Found a section
58  $currentSection = $matches[1];
59  if (!isset($configData[$currentSection])) {
60  $configData[$currentSection] = array();
61  }
62 
63  } else if (strpos($line, '=') !== false) {
64  // Found a setting
65  list($key, $value) = explode('=', $line, 2);
66  $key = trim($key);
67  $value = trim($value);
68 
69  // FIXME This may produce incorrect results if the line contains a comment
70  if (preg_match('/^[\"\'](.*)[\"\']$/', $value, $matches)) {
71  // Treat value as a string
72  $value = stripslashes($matches[1]);
73 
74  } else {
75  preg_match('/^([\S]*)/', $value, $matches);
76  $value = $matches[1];
77 
78  // Try to determine the type of the value
79  if ($value === '') {
80  $value = null;
81 
82  } else if (is_numeric($value)) {
83  if (strstr($value, '.')) {
84  // floating-point
85  $value = (float) $value;
86  } else if (substr($value, 0, 2) == '0x') {
87  // hex
88  $value = intval($value, 16);
89  } else if (substr($value, 0, 1) == '0') {
90  // octal
91  $value = intval($value, 8);
92  } else {
93  // integer
94  $value = (int) $value;
95  }
96 
97  } else if (strtolower($value) == 'true' || strtolower($value) == 'on') {
98  $value = true;
99 
100  } else if (strtolower($value) == 'false' || strtolower($value) == 'off') {
101  $value = false;
102 
103  } else if (defined($value)) {
104  // The value matches a named constant
105  $value = constant($value);
106  }
107  }
108 
109  if ($currentSection === false) {
110  $configData[$key] = $value;
111 
112  } else if (is_array($configData[$currentSection])) {
113  $configData[$currentSection][$key] = $value;
114  }
115  }
116  }
117 
118  fclose($fp);
119 
120  return $configData;
121  }
122 
131  function updateConfig($file, $params) {
132  if (!file_exists($file) || !is_readable($file)) {
133  return false;
134  }
135 
136  $this->content = '';
137  $lines = file($file);
138 
139  // Parse each line of the configuration file
140  for ($i=0, $count=count($lines); $i < $count; $i++) {
141  $line = $lines[$i];
142 
143  if (preg_match('/^;/', $line) || preg_match('/^\s*$/', $line)) {
144  // Comment or empty line
145  $this->content .= $line;
146 
147  } else if (preg_match('/^\s*\[(\w+)\]/', $line, $matches)) {
148  // Start of new section
149  $currentSection = $matches[1];
150  $this->content .= $line;
151 
152  } else if (preg_match('/^\s*(\w+)\s*=/', $line, $matches)) {
153  // Variable definition
154  $key = $matches[1];
155 
156  if (!isset($currentSection) && array_key_exists($key, $params) && !is_array($params[$key])) {
157  // Variable not in a section
158  $value = $params[$key];
159 
160  } else if (isset($params[$currentSection]) && is_array($params[$currentSection]) && array_key_exists($key, $params[$currentSection])) {
161  // Variable in a section
162  $value = $params[$currentSection][$key];
163 
164  } else {
165  // Variable not to be changed, do not modify line
166  $this->content .= $line;
167  continue;
168  }
169 
170  if (preg_match('/[^\w\-\/]/', $value)) {
171  // Escape strings containing non-alphanumeric characters
172  $valueString = '"' . addslashes($value) . '"';
173  } else {
174  $valueString = $value;
175  }
176 
177  $this->content .= "$key = $valueString\n";
178 
179  } else {
180  $this->content .= $line;
181  }
182  }
183 
184  return true;
185  }
186 
192  function writeConfig($file) {
193  if (!(file_exists($file) && is_writable($file))
194  && !(!file_exists($file) && is_dir(dirname($file)) && is_writable(dirname($file)))) {
195  // File location cannot be written to
196  return false;
197  }
198 
199  $fp = @fopen($file, 'wb');
200  if (!$fp) {
201  return false;
202  }
203 
204  fwrite($fp, $this->content);
205  fclose($fp);
206  return true;
207  }
208 
213  function getFileContents() {
214  return $this->content;
215  }
216 }
217 
218 
ConfigParser\readConfig
static & readConfig($file)
Definition: ConfigParser.inc.php:34
ConfigParser\__construct
__construct()
Definition: ConfigParser.inc.php:25
ConfigParser
Class for parsing and modifying php.ini style configuration files.
Definition: ConfigParser.inc.php:17
ConfigParser\getFileContents
getFileContents()
Definition: ConfigParser.inc.php:213
ConfigParser\$content
$content
Definition: ConfigParser.inc.php:20
ConfigParser\writeConfig
writeConfig($file)
Definition: ConfigParser.inc.php:192
ConfigParser\updateConfig
updateConfig($file, $params)
Definition: ConfigParser.inc.php:131