################################################################################ # Starburst CMS | Source Code File # ################################################################################ # config.lib | Handles the reading and writing of config files. sub readConfig { my $name = shift; my $file = ''; # Check its existence. if (-f "$root->{base}/conf/$name") { # It exists. $file = "$root->{base}/conf/$name"; } else { # Does the default exist? if (-f "$root->{base}/conf/default/$name") { $file = "$root->{base}/conf/default/$name"; } else { # No? Error! &panic ("Config file $file not found!"); } } # Read the file. my $conf = {}; my $lab = 'default'; if (length $file > 0) { &capture ($file); open (READ, $file); my @data = ; close (READ); &release($file); chomp @data; foreach my $line (@data) { $line =~ s/^\s+//ig; $line =~ s/\s+$//ig; next if $line =~ /^#/; if ($line =~ /^\[(.+?)\]$/i) { $lab = $1; } else { if ($line =~ /=/) { my ($what,$is) = split(/=/, $line, 2); $what = &normalize($what); $conf->{$lab}->{$what} = $is; } } } } # Return the data. return $conf; } sub writeConfig { my ($file,$data) = @_; # Lock the file. &capture("$root->{base}/conf/$file"); open (WRITE, ">$root->{base}/conf/$file"); print WRITE "# File updated on " . localtime(time()) . "\n"; # Write everything. foreach my $header (sort { $a cmp $b } keys %{$data}) { print "\n[$header]\n"; foreach my $field (sort { $a cmp $b } keys %{$data->{$header}}) { my $value = $data->{$header}->{$field}; print WRITE "$field=$value\n"; } } close (WRITE); &release("$root->{base}/conf/$file"); return 1; } 1;