]> git.p6c8.net - devedit.git/blob - modules/Config/DevEdit.pm
You can now define alias names for users who have an individual configuration,
[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 use Data::Dumper;
26
27 # read_config()
28 #
29 # Read the configuration files of Dev-Editor
30 #
31 # Params: Path to main configuration file
32 #
33 # Return: Configuration (Hash Reference)
34
35 sub read_config($)
36 {
37 my $file = shift;
38
39 my $config = parse_config($file);
40
41 $config->{'errors'} = parse_config($config->{'error_file'});
42 $config->{'templates'} = parse_config($config->{'template_file'});
43
44 # Check if we have to parse the user config file
45
46 if($ENV{'REMOTE_USER'} && $config->{'userconf_file'} && -f $config->{'userconf_file'})
47 {
48 my $userconf = parse_config($config->{'userconf_file'});
49
50 # Parse aliases (we use references, so we won't get a memory
51 # problem so soon...)
52
53 foreach my $user(keys(%$userconf))
54 {
55 if(my $aliases = $userconf->{$user}->{'aliases'})
56 {
57 foreach my $alias(parse_line('\s+',0,$aliases))
58 {
59 $userconf->{$alias} = $userconf->{$user} unless($userconf->{$alias});
60 }
61 }
62 }
63
64 if($userconf->{$ENV{'REMOTE_USER'}})
65 {
66 # The current HTTP Auth user has got an individual configuration
67 # Overwrite the default values
68
69 my $new_conf = $userconf->{$ENV{'REMOTE_USER'}};
70
71 $config->{'fileroot'} = $new_conf->{'fileroot'} if($new_conf->{'fileroot'});
72 $config->{'httproot'} = $new_conf->{'httproot'} if($new_conf->{'httproot'});
73
74 $config->{'forbidden'} = $new_conf->{'forbidden'} if(defined $new_conf->{'forbidden'});
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