File Coverage

blib/lib/Labyrinth/Variables.pm
Criterion Covered Total %
statement 31 33 93.9
branch n/a
condition n/a
subroutine 11 11 100.0
pod n/a
total 42 44 95.4


line stmt bran cond sub pod time code
1             package Labyrinth::Variables;
2              
3 7     7   80 use warnings;
  7         8  
  7         194  
4 7     7   23 use strict;
  7         10  
  7         184  
5              
6 7     7   23 use vars qw($VERSION @ISA %EXPORT_TAGS @EXPORT @EXPORT_OK);
  7         839  
  7         522  
7             $VERSION = '5.31';
8              
9             =head1 NAME
10              
11             Labyrinth::Variables - Generic Variables for Labyrinth
12              
13             =head1 SYNOPSIS
14              
15             use Labyrinth::Variables;
16              
17             # output values
18             $tvars{title} = 'My Title';
19              
20             =head1 DESCRIPTION
21              
22             The Variables package contains a number of variables that are
23             used across the system. The variables contain input and output values,
24             and the functions are generic.
25              
26             =head1 EXPORT
27              
28             use Labyrinth::Variables; # default (:all) = (:vars :subs)
29             use Labyrinth::Variables qw(:vars); # all variable containers
30             use Labyrinth::Variables qw(:subs); # all standard subroutines
31             use Labyrinth::Variables qw(:xsub); # all extended subroutines
32              
33             =cut
34              
35             # -------------------------------------
36             # Constants
37              
38 7     7   26 use constant PUBLIC => 0;
  7         7  
  7         369  
39 7     7   40 use constant USER => 1;
  7         24  
  7         243  
40 7     7   26 use constant EDITOR => 2;
  7         7  
  7         221  
41 7     7   27 use constant PUBLISHER => 3;
  7         7  
  7         201  
42 7     7   25 use constant ADMIN => 4;
  7         9  
  7         228  
43 7     7   26 use constant MASTER => 5;
  7         7  
  7         653  
44              
45             # -------------------------------------
46             # Export Details
47              
48             require Exporter;
49             @ISA = qw(Exporter);
50              
51             %EXPORT_TAGS = (
52             'vars' => [ qw(
53             PUBLIC USER EDITOR PUBLISHER ADMIN MASTER
54             $dbi %cgiparams %tvars %settings $cgi
55             ) ],
56             'subs' => [ qw(
57             CGIArray ParamsCheck SetError SetCommand
58             ) ],
59             'all' => [ qw(
60             PUBLIC USER EDITOR PUBLISHER ADMIN MASTER
61             $dbi %cgiparams %tvars %settings $cgi
62             CGIArray ParamsCheck SetError SetCommand
63             LoadProfiles LoadAccess
64             ) ],
65             );
66              
67             @EXPORT_OK = ( @{$EXPORT_TAGS{'all'}} );
68             @EXPORT = ( @{$EXPORT_TAGS{'all'}} );
69              
70             # -------------------------------------
71             # Library Modules
72              
73 7     7   4655 use Config::IniFiles;
  7         122916  
  7         229  
74 7     7   6540 use Regexp::Assemble;
  0            
  0            
75              
76             # -------------------------------------
77             # Variables
78              
79             =head2 Global Variables
80              
81             =over 4
82              
83             =item %cgiparams
84              
85             Holds all the scalar CGI parameter values. Access parameters as:
86              
87             my $value = $cgiparams{$name};
88              
89             =item %tvars
90              
91             Holds all the template variable values, for use with the template parser.
92             Access template variables as:
93              
94             my $value = $tvars{$name}; # get the named variable
95             $tvars{$name} = $value; # set scalar variable
96             $tvars{$hash} = \%hash; # set hash variable
97             $tvars{$list} = \@list; # set array variable
98              
99             =item $dbi
100              
101             Holds the reference to the DB access object. Created by the DBConnect()
102             method, which must be called before any database activity commences.
103              
104             =back
105              
106             =cut
107              
108             our %cgiparams; # contains valid CGI parameters
109             our %tvars; # template variable container
110             our %settings; # internal settings hash
111             our $dbi; # database object
112             our $cgi; # CGI object
113              
114             # -------------------------------------
115             # Variable Functions
116              
117             =head2 Initialisation
118              
119             =over 4
120              
121             =item init
122              
123             Prepares the standard variable values, so that they are only called once on setup.
124              
125             =back
126              
127             =cut
128              
129             sub init {
130             my $prot = qr{(?:http|https|ftp|afs|news|nntp|mid|cid|mailto|wais|prospero|telnet|gopher|git|file)://};
131             my $atom = qr{[a-z\d]}i;
132             my $host = qr{(?:$atom(?:(?:$atom|-)*$atom)?)};
133             my $domain = qr{(?:(?:(?:$host(?:\.$host)*))*(?:\.[a-zA-Z](?:$atom)*$atom)+)};
134             my $ip = qr{(?:(?:\d+)(?:\.(?:\d+)){3})(?::(?:\d+))?};
135             my $enc = qr{%[a-fA-F\d]{2}};
136             my $legal1 = qr{[a-zA-Z\d\$_.+!*\'(),~\#-]};
137             my $legal2 = qr{[\/;:@&=]};
138             my $legal3 = qr{(?:(?:$legal1|$enc)+(?:(?:$legal2)+(?:$legal1|$enc)+)*)};
139             my $path = qr{\/(?:$legal3)+};
140             my $query = qr{(?:\?$legal3)+};
141             my $local = qr{[-\w\'=.]+};
142              
143             my $url1 = qr{(?: ($prot)? ($domain|$ip|\/$|$path) ($path)* ($query)? ) (\#[-\w.]+)?}x;
144             my $url2 = qr{(?: (?:$prot) (?:$domain|$ip|\/$|$path) (?:$path)* (?:$query)? ) (?:\#[-\w.]+)?}x;
145             my $email = qr{$local\@(?:$domain|$ip)};
146              
147             $settings{protregex} = $prot;
148             $settings{urlregex} = $url1; #qr{\b$url1\b};
149             $settings{urlstrict} = $url2; #qr{\b$url2\b};
150             $settings{emailregex} = $email;
151              
152              
153             $settings{crawler} = 0;
154             if($settings{crawlers}) {
155             my $ra = Regexp::Assemble->new;
156             $ra->add( '\b' . quotemeta( $_ ) . '\b' ) for(@{ $settings{crawlers} });
157             my $re = $ra->re;
158             $settings{crawler} = 1 if($ENV{'HTTP_USER_AGENT'} =~ $re);
159             }
160              
161              
162             $settings{'query-parser'} ||= 'CGI';
163             my $class = 'Labyrinth::Query::' . $settings{'query-parser'};
164              
165             eval {
166             eval "CORE::require $class";
167             $cgi = $class->new();
168             };
169              
170             die "Cannot load Query package for '$settings{'query-parser'}': $@" if($@);
171             }
172              
173             =head2 CGI Parameter Handling
174              
175             =over 4
176              
177             =item CGIArray($name)
178              
179             ParseParams only handles the scalar interface (CGI) parameters. In the event
180             an array is required, CGIArray() is used to find and validate the parameter,
181             before returning the list of values.
182              
183             =item ParamsCheck
184              
185             Given a list of fields, checks whether the interface (CGI) parameters have
186             been set. Sets error conditions if any are missing.
187              
188             =back
189              
190             =cut
191              
192             sub CGIArray {
193             my $name = shift;
194             return () unless(defined $cgiparams{$name} && $cgiparams{$name});
195             return ($cgiparams{$name}) unless(ref $cgiparams{$name} eq 'ARRAY');
196             return @{$cgiparams{$name}};
197             }
198              
199             sub ParamsCheck {
200             for my $field (@_) {
201             next if($cgiparams{$field});
202             $tvars{errcode} = 'MESSAGE';
203             $tvars{errmess} = "Missing parameter ($field)";
204             return 0;
205             }
206              
207             return 1;
208             }
209              
210             =head2 Process Flow Handling
211              
212             =over
213              
214             =item SetError
215              
216             Sets the error condition as given.
217              
218             =item SetCommand
219              
220             Set the next commmand to be run.
221              
222             =back
223              
224             =cut
225              
226             sub SetError {
227             $tvars{errcode} = shift;
228             $tvars{errmess} = shift if(@_);
229             }
230              
231             sub SetCommand {
232             $tvars{errcode} = 'NEXT';
233             $tvars{command} = shift;
234             }
235              
236             =head2 Default Variable Loaders
237              
238             =over
239              
240             =item LoadProfiles
241              
242             Loads the permissions profiles, as stored in profiles config file.
243              
244             =item LoadAccess
245              
246             Loads the access permissions, as stored in the database.
247              
248             =back
249              
250             =cut
251              
252             sub LoadProfiles {
253             return if(defined $settings{profiles});
254              
255             # ensure we can access the profile file
256             if(!$settings{profile} || !-f $settings{profile} || !-r $settings{profile}) {
257             LogError("Cannot read profile file [$settings{profile}]");
258             $tvars{errcode} = 'ERROR';
259             return;
260             }
261              
262             my $cfg = Config::IniFiles->new( -file => $settings{profile} );
263             unless(defined $cfg) {
264             LogError("Unable to load profile file [$settings{profile}]: @Config::IniFiles::errors");
265             $tvars{errcode} = 'ERROR';
266             return;
267             }
268              
269             # load the configuration data
270             my $value = $cfg->val('MAIN','default');
271             my @value = $cfg->val('MAIN','profiles');
272              
273             $settings{profiles}{default} = $value;
274              
275             for my $profile (@value) {
276             for my $name ($cfg->Parameters($profile)) {
277             $value = $cfg->val($profile,$name);
278             $settings{profiles}{profiles}{$profile}{$name} = $value;
279             }
280             }
281             }
282              
283             sub LoadAccess {
284             return if(defined $settings{access});
285              
286             my @rows = $dbi->GetQuery('hash','AllAccess',9);
287             for my $row (@rows) {
288             $settings{access}{names}{$row->{accessname}} = $row->{accessid};
289             $settings{access}{ids}{$row->{accessid}} = $row->{accessname};
290             }
291             }
292              
293             1;
294              
295             __END__