filename = $filename; $this->new = $new; } // Config files use Apache-style directives // Example: UserName joe // Comments start with # and extend to rest of line function parse() { if ($this->new == true) { return Configurator::error(CONFIG_ERROR_NOFILE); } $contents = @file($this->filename); if (!$contents) { return Configurator::error(CONFIG_ERROR_NOLOAD); } foreach ($contents as $line) { // Gets rid of comments $line = preg_replace("/; .*$/i", "", $line); $line = ltrim($line); $line = chop($line); if ($line == '') { continue; } elseif (preg_match('/^Archiver (.*)/', $line, $tagopen)) { $this->currenttag['info']['tagname'] = $tagopen[1]; if (array_key_exists('2', $tagopen)) { $this->currenttag['info']['attributes'] = $tagopen[2]; } else { $this->currenttag['info']['attributes'] = ""; } // echo "TAGs: (1) ".$tagopen[1].", (2) ".$tagopen[2]."\r\n"; } elseif (preg_match('/^End Archiver$/', $line, $tagclose)) { //if ($this->taghandler != '') { // $this->taghandler($this->currenttag); //} $this->directives[$this->currenttag['info']['tagname'] . $this->currenttag['info']['attributes']] = $this->currenttag['directives']; $this->currenttag = array(); } else { $directive = explode(' ', $line); $directiveName = $directive[0]; unset ($directive[0]); $directiveValue = ltrim(implode(' ', $directive)); if ($this->currenttag != array()) { $this->currenttag['directives'][$directiveName] = $directiveValue; } else { $this->directives[$directiveName] = $directiveValue; } } } if ($this->currenttag != array()) { Configurator::error(CONFIG_ERROR_SYNTAX); } } function getDirectives() { return $this->directives; } function getDirective($directive) { return $this->directives[$directive]; } function getDirectiveFromGroup($group, $directive) { return $this->directives[$group][$directive]; } function setDirectives($directives) { if (!is_array($directives)) { return Configurator::error(CONFIG_ERROR_INVALID); } else { $this->directives = array_merge($this->directives, $directives); } } function setDirective($name, $value) { $this->directives[$name] = $value; } function setDirectiveInGroup($group, $directive, $value) { $this->directives[$group][$directive] = $value; } function parseToArray() { $this->parse(); return $this->getDirectives(); } function remove($directive) { unset($this->directives[$directive]); } function removeFromGroup($group, $directive) { unset($this->directives[$group][$directive]); } // writes current set of directives to file // NOTE: does not save comments function write() { $fp = fopen($this->filename, 'w'); $filecontents = << # Configuration file # Auto-generated file CONTENTS; foreach ($this->directives as $name => $value) { if (is_array($value)) { $filecontents .= "\n\n<$name>"; foreach ($value as $childname => $childvalue) { $filecontents .= "\n\t$childname $childvalue"; } $namearr = explode(' ', $name); $filecontents .= "\n"; } else { $filecontents .= "\n\n$name $value"; } } fwrite($fp, $filecontents); fclose($fp); $this->new = false; } function writeFromArray($directives) { $this->setDirectives($directives); $this->write(); } } class ConfiguratorError { var $code; function ConfiguratorError($code) { $this->code = $code; switch ($code) { case CONFIG_ERROR_NOFILE: die('Configurator Fatal Error: config file could not be found.'); break; case CONFIG_ERROR_NOLOAD: die('Configurator Fatal Error: config file could not be loaded. Please check your file permissions.'); break; case CONFIG_ERROR_INVALID: break; case CONFIG_ERROR_SYNTAX: die('Configurator Fatal Error: syntax error in config file. Perhaps an unclosed tag?'); break; } } function message() { switch ($this->code) { case CONFIG_ERROR_NOFILE: return 'Configurator Fatal Error: config file could not be found.'; break; case CONFIG_ERROR_NOLOAD: return 'Configurator Fatal Error: config file could not be loaded. Please check your file permissions.'; break; case CONFIG_ERROR_INVALID: return 'Configurator Warning: invalid argument. Operation not performed.'; break; case CONFIG_ERROR_SYNTAX: return 'Configurator Fatal Error: syntax error in config file. Perhaps an unclosed tag?'; break; } } } ?>