]> git.p6c8.net - devedit.git/blob - modules/Config/DevEdit.pm
bc93cfeb290be450ea4a46e23f349a41939c345d
[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->{'user_config'} = 1;
75 }
76 }
77
78 # Parse list of forbidden files
79
80 if($config->{'forbidden'})
81 {
82 my @files;
83
84 foreach my $file(parse_line('\s+',0,$config->{'forbidden'}))
85 {
86 $file =~ tr!\\!/!;
87
88 $file = '/'.$file unless($file =~ m!^/!);
89 $file =~ s!/+$!!g;
90
91 push(@files,$file);
92 }
93
94 $config->{'forbidden'} = \@files;
95 }
96 else
97 {
98 $config->{'forbidden'} = [];
99 }
100
101 return $config;
102 }
103
104 # parse_config()
105 #
106 # Parse a configuration file
107 #
108 # Params: Path to configuration file
109 #
110 # Return: Configuration (Hash Reference)
111
112 sub parse_config($)
113 {
114 my $file = shift;
115 local *CF;
116
117 open(CF,'<'.$file) or croak("Open $file: $!");
118 read(CF, my $data, -s $file);
119 close(CF);
120
121 my @lines = split(/\015\012|\012|\015/,$data);
122 my $config = {};
123 my $count = 0;
124 my $sect;
125
126 foreach my $line(@lines)
127 {
128 $count++;
129
130 next if($line =~ /^\s*#/);
131
132 if($line =~ /^\s*\[(\S+)\]\s*$/)
133 {
134 # Switch to new section
135
136 $sect = $1;
137 }
138 elsif($line =~ /^\s*\S+\s*=.*$/)
139 {
140 # A normal "key = value" line
141
142 my ($key,$value) = split(/=/,$line,2);
143
144 # Remove whitespaces at the beginning and at the end
145
146 $key =~ s/^\s+//g;
147 $key =~ s/\s+$//g;
148 $value =~ s/^\s+//g;
149 $value =~ s/\s+$//g;
150
151 if($sect)
152 {
153 $config->{$sect} = {} if(ref($config->{$sect}) ne 'HASH');
154
155 croak "Configuration option '$key' of section '$sect' defined twice in line $count of configuration file '$file'" if($config->{$sect}->{$key});
156
157 $config->{$sect}->{$key} = $value;
158 }
159 else
160 {
161 croak "Configuration option '$key' defined twice in line $count of configuration file '$file'" if($config->{$key});
162
163 $config->{$key} = $value;
164 }
165 }
166 }
167
168 return $config;
169 }
170
171 # it's true, baby ;-)
172
173 1;
174
175 #
176 ### End ###

patrick-canterino.de