content = ''; $lines = file($file); // Parse each line of the configuration file for ($i=0, $count=count($lines); $i < $count; $i++) { $line = $lines[$i]; if (preg_match('/^;/', $line) || preg_match('/^\s*$/', $line)) { // Comment or empty line $this->content .= $line; } else if (preg_match('/^\s*\[(\w+)\]/', $line, $matches)) { // Start of new section $currentSection = $matches[1]; $this->content .= $line; } else if (preg_match('/^\s*(\w+)\s*=/', $line, $matches)) { // Variable definition $key = $matches[1]; if (!isset($currentSection) && array_key_exists($key, $params) && !is_array($params[$key])) { // Variable not in a section $value = $params[$key]; } else if (isset($params[$currentSection]) && is_array($params[$currentSection]) && array_key_exists($key, $params[$currentSection])) { // Variable in a section $value = $params[$currentSection][$key]; } else { // Variable not to be changed, do not modify line $this->content .= $line; continue; } if (preg_match('/[^\w\-\/]/', $value)) { // Escape strings containing non-alphanumeric characters $valueString = '"' . $value . '"'; } else { $valueString = $value; } $this->content .= "$key = $valueString\n"; } else { $this->content .= $line; } } return true; } /** * Write contents of current config file * @param $file string full path to output file * @return boolean file write is successful */ function writeConfig($file) { if (!(file_exists($file) && is_writable($file)) && !(!file_exists($file) && is_dir(dirname($file)) && is_writable(dirname($file)))) { // File location cannot be written to return false; } $fp = @fopen($file, 'wb'); if (!$fp) { return false; } fwrite($fp, $this->content); fclose($fp); return true; } /** * Return the contents of the current config file. * @return string */ function getFileContents() { return $this->content; } } ?>