]> git.imager.perl.org - bse.git/blame - site/cgi-bin/modules/BSE/Cfg.pm
[rt #1279] when logging in (as a siteuser) start a new session object
[bse.git] / site / cgi-bin / modules / BSE / Cfg.pm
CommitLineData
589b789c
TC
1package BSE::Cfg;
2use strict;
0d454b28 3use base "DevHelp::Cfg";
e1e2a2b5 4use Carp qw(confess);
589b789c 5use constant MAIN_CFG => 'bse.cfg';
589b789c 6
8ebed4c6 7our $VERSION = "1.004";
cb7fd78d 8
589b789c
TC
9my %cache;
10
e1e2a2b5 11my $single;
589b789c
TC
12
13=head1 NAME
14
15 BSE::Cfg - configuration file for BSE
16
17=head1 SYNOPSIS
18
19 my $cfg = BSE::Cfg->new();
20 my $entry1 = $cfg->entry($section, $key); # undef on failure
21 my $entry2 = $cfg->entryErr($section, $key); # abort on failure
22 my $entry3 = $cfg->entryVar($section, $key); # replace variables in value
23
24=head1 DESCRIPTION
25
26Provides a simple configuration file object for BSE.
27
28Currently just provides access to a single config file, but could
29later be modified to provide access to one based on the current site,
30for use in a mod_perl version of BSE.
31
32=head1 METHODS
33
34=over
35
36=item BSE::Cfg->new
37
e1e2a2b5
TC
38Create a new configuration file object.
39
40Parameters:
41
42=over
43
44=item *
45
46path - the path to start searching for the config file in, typically
47the BSE cgi-bin.
48
49=back
589b789c
TC
50
51=cut
52
53sub new {
54 my ($class, %opts) = @_;
55
0d454b28 56 $single = $class->SUPER::new(filename => MAIN_CFG, %opts);
e1e2a2b5
TC
57
58 return $single;
59}
60
61=item single
62
63Return the BSE configuration object.
64
65This is used to avoid always passing around a config object.
66
67=cut
68
69sub single {
70 my ($class) = @_;
71
72 $single or confess "BSE's configuration hasn't been initialized yet";
73
74 return $single;
589b789c
TC
75}
76
0d454b28 77=item utf8
531fb3bc 78
0d454b28 79Return a true value if BSE is working in UTF-8 internal mode.
589b789c
TC
80
81=cut
82
0d454b28
TC
83sub utf8 {
84 return $single->entry("basic", "utf8", 0);
589b789c
TC
85}
86
0d454b28 87=item charset
589b789c 88
0d454b28 89Return the BSE character set.
589b789c
TC
90
91=cut
92
0d454b28
TC
93sub charset {
94 return $single->entry('html', 'charset', 'iso-8859-1');
589b789c
TC
95}
96
13a986ee
TC
97=item user_url($script, $target)
98
99=cut
100
101sub user_url {
102 my ($cfg, $script, $target, @options) = @_;
103
ede1cb40
TC
104 my $secure = $script =~ /^(shop|user)$/;
105 $secure = $cfg->entry("secure user url", $script, $secure);
8ebed4c6 106 my $base;
13a986ee
TC
107 my $template;
108 if ($target) {
109 if ($script eq 'nuser') {
110 $template = "/cgi-bin/nuser.pl/user/TARGET";
111 }
112 else {
8ebed4c6 113 $template = "/cgi-bin/$script.pl?a_TARGET=1";
13a986ee
TC
114 }
115 $template = $cfg->entry('targets', $script, $template);
116 $template =~ s/TARGET/$target/;
117 }
118 else {
119 if ($script eq 'nuser') {
120 $template = "/cgi-bin/nuser.pl/user";
121 }
122 else {
8ebed4c6 123 $template = "/cgi-bin/$script.pl";
13a986ee
TC
124 }
125 $template = $cfg->entry('targets', $script.'_n', $template);
126 }
127 if (@options) {
13a986ee
TC
128 my @entries;
129 while (my ($key, $value) = splice(@options, 0, 2)) {
130 require BSE::Util::HTML;
8ebed4c6
TC
131 if ($key eq '-base') {
132 $base = $value;
133 }
134 else {
135 push @entries, "$key=" . BSE::Util::HTML::escape_uri($value);
136 }
137 }
138 if (@entries) {
139 $template .= $template =~ /\?/ ? '&' : '?';
140 $template .= join '&', @entries;
13a986ee 141 }
13a986ee
TC
142 }
143
8ebed4c6
TC
144 $base ||= $secure ? $cfg->entryVar('site', 'secureurl') : '';
145
146 return $base . $template;
13a986ee
TC
147}
148
a5093f33
TC
149sub admin_url {
150 my ($self, $action, $params, $name) = @_;
151
152 require BSE::CfgInfo;
153 my $url = BSE::CfgInfo::admin_base_url($self);
154 if ($self->entry("nadmin controllers", $action)) {
155 $url .= "/cgi-bin/admin/nadmin.pl/$action";
156 }
157 else {
158 $url .= "/cgi-bin/admin/$action.pl";
159 }
160 if ($params && keys %$params) {
161 require BSE::Util::HTML;
162 $url .= "?" . join("&", map { "$_=".BSE::Util::HTML::escape_uri($params->{$_}) } keys %$params);
163 }
164 $url .= "#$name" if $name;
165
166 return $url;
167}
168
5abe2da5
TC
169=item content_base_path()
170
171Site document root. Previously $Constants::CONTENTBASE.
172
173=cut
174
175sub content_base_path {
176 my ($self) = @_;
177
178 ref $self
179 or $self = $self->single;
180
181 my $path = $self->entryIfVar("paths", "public_html");
182 unless ($path) {
183 # backward compatibility
184 require Constants;
185 $path = $Constants::CONTENTBASE;
186 }
187
188 return $path;
189}
a5093f33 190
589b789c
TC
1911;
192
193=head1 AUTHOR
194
195Tony Cook <tony@develop-help.com>
196
197=cut