]> git.p6c8.net - devedit.git/blob - modules/Config/DevEdit.pm
8d9d50398f6eb7d19a388e25c66ea984edb608d1
[devedit.git] / modules / Config / DevEdit.pm
1 package Config::DevEdit;
2
3 #
4 # Dev-Editor - Module Config::DevEdit
5 #
6 # Read and parse the configuration files
7 #
8 # Author: Patrick Canterino <patrick@patshaping.de>
9 # Last modified: 2005-09-30
10 #
11
12 use strict;
13
14 use vars qw(@EXPORT);
15 use Carp qw(croak);
16
17 use Text::ParseWords;
18
19 ### Export ###
20
21 use base qw(Exporter);
22
23 @EXPORT = qw(read_config);
24
25 # read_config()
26 #
27 # Read the configuration files of Dev-Editor
28 #
29 # Params: Path to main configuration file
30 #
31 # Return: Configuration (Hash Reference)
32
33 sub read_config($)
34 {
35 my $file = shift;
36
37 my $config = parse_config($file);
38
39 $config->{'errors'} = parse_config($config->{'error_file'});
40 $config->{'templates'} = parse_config($config->{'template_file'});
41
42 # Check if we have to parse the user config file
43
44 if($ENV{'REMOTE_USER'} && $config->{'userconf_file'} && -f $config->{'userconf_file'})
45 {
46 my $userconf = parse_config($config->{'userconf_file'});
47
48 # Parse aliases (we use references, so we won't get a memory
49 # problem so soon...)
50
51 foreach my $user(keys(%$userconf))
52 {
53 if(my $aliases = $userconf->{$user}->{'aliases'})
54 {
55 foreach my $alias(parse_line('\s+',0,$aliases))
56 {
57 $userconf->{$alias} = $userconf->{$user} unless($userconf->{$alias});
58 }
59 }
60 }
61
62 if($userconf->{$ENV{'REMOTE_USER'}})
63 {
64 # The current HTTP Auth user has got an individual configuration
65 # Overwrite the default values
66
67 my $new_conf = $userconf->{$ENV{'REMOTE_USER'}};
68
69 $config->{'fileroot'} = $new_conf->{'fileroot'} if($new_conf->{'fileroot'});
70 $config->{'httproot'} = $new_conf->{'httproot'} if($new_conf->{'httproot'});
71
72 $config->{'forbidden'} = $new_conf->{'forbidden'} if(defined $new_conf->{'forbidden'});
73
74 $config->{'hide_dot_files'} = $new_conf->{'hide_dot_files'} if(defined $new_conf->{'hide_dot_files'});
75
76 $config->{'user_config'} = 1;
77 }
78 }
79
80 # Parse list of forbidden files
81
82 if($config->{'forbidden'})
83 {
84 my @files;
85
86 foreach my $file(parse_line('\s+',0,$config->{'forbidden'}))
87 {
88 $file =~ tr!\\!/!;
89
90 $file = '/'.$file unless($file =~ m!^/!);
91 $file =~ s!/+$!!g;
92
93 push(@files,$file);
94 }
95
96 $config->{'forbidden'} = \@files;
97 }
98 else
99 {
100 $config->{'forbidden'} = [];
101 }
102
103 return $config;
104 }
105
106 # parse_config()
107 #
108 # Parse a configuration file
109 #
110 # Params: Path to configuration file
111 #
112 # Return: Configuration (Hash Reference)
113
114 sub parse_config($)
115 {
116 my $file = shift;
117 local *CF;
118
119 open(CF,'<'.$file) or croak("Open $file: $!");
120 read(CF, my $data, -s $file);
121 close(CF);
122
123 my @lines = split(/\015\012|\012|\015/,$data);
124 my $config = {};
125 my $count = 0;
126 my $sect;
127
128 foreach my $line(@lines)
129 {
130 $count++;
131
132 next if($line =~ /^\s*#/);
133
134 if($line =~ /^\s*\[(\S+)\]\s*$/)
135 {
136 # Switch to new section
137
138 $sect = $1;
139 }
140 elsif($line =~ /^\s*\S+\s*=.*$/)
141 {
142 # A normal "key = value" line
143
144 my ($key,$value) = split(/=/,$line,2);
145
146 # Remove whitespaces at the beginning and at the end
147
148 $key =~ s/^\s+//g;
149 $key =~ s/\s+$//g;
150 $value =~ s/^\s+//g;
151 $value =~ s/\s+$//g;
152
153 if($sect)
154 {
155 $config->{$sect} = {} if(ref($config->{$sect}) ne 'HASH');
156
157 croak "Configuration option '$key' of section '$sect' defined twice in line $count of configuration file '$file'" if($config->{$sect}->{$key});
158
159 $config->{$sect}->{$key} = $value;
160 }
161 else
162 {
163 croak "Configuration option '$key' defined twice in line $count of configuration file '$file'" if($config->{$key});
164
165 $config->{$key} = $value;
166 }
167 }
168 }
169
170 return $config;
171 }
172
173 # it's true, baby ;-)
174
175 1;
176
177 #
178 ### End ###

patrick-canterino.de