Apache/iNcom/Request.pm untested

filter: Statementstmt 0.0% 0 / 292 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 41 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 0.0% 0 / 37 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 475 CC 33 SCAR 35.7
CC
33
Cov
0%
setup_aliases
132.0
new
72.0
Include
30.0
CRAP 35.4
1 #
2 # Request.pm - Object that encaspulates an iNcom request.
3 #
4 # This file is part of Apache::iNcom.
5 #
6 # Author: Francis J. Lacoste <francis.lacoste@iNsu.COM>
7 #
8 # Copyright (C) 1999 Francis J. Lacoste, iNsu Innovations
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #
24 package Apache::iNcom::Request;
25  
26 use strict;
27  
28 use Apache::Util;
29  
30 use Apache::iNcom::CartManager;
31 use Apache::iNcom::OrderManager;
32 use Apache::iNcom::Localizer;
33  
34 use DBIx::SearchProfiles;
35 use DBIx::UserDB;
36  
37 use HTML::FormValidator;
38  
39 use Symbol;
40  
41 use vars qw( @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION );
42  
43 use Carp;
44  
45 BEGIN {
46 use Exporter;
47  
48 @ISA = qw( Exporter );
49 @EXPORT = ();
50 @EXPORT_OK = ();
51 %EXPORT_TAGS = (); # This is filled later near the
52 # declaration of global variables
53  
54 ($VERSION) = '$Revision: 1.15 $' =~ /Revision: ([\d.]+)/;
55 }
56  
57 =pod
58  
59 =head1 NAME
60  
61 Apache::iNcom::Request - Manages the Apache::iNcom request's informations.
62  
63 =head1 SYNOPSIS
64  
65 my $user = $Request->user
66  
67 etc.
68  
69 =head1 DESCRIPTION
70  
71 This module is responsible for managing the environment in which the
72 Apache::iNcom page will execute. It setups all the objects that will
73 be accessible to the pages through globals and also provides the
74 page with a bunch of utility functions. It also provides a bunch
75 of methods for managing the information associated with the request.
76  
77  
78 =head1 INITIALIZATION
79  
80 An object is automatically initialized on each request by the
81 Apache::iNcom framework. It is accessible through the $Request global
82 variable in Apache::iNcom pages.
83  
84 =cut
85  
86 sub new {
87 my $proto = shift;
88 my $class = ref $proto || $proto;
89  
90 my $req_rec = shift;
91 my $package = shift;
92  
93 my $self = { req_rec => $req_rec,
94 package => $package,
95 };
96 bless $self, $class;
97  
98 $self->{session} = $req_rec->pnotes( "INCOM_SESSION" );
99 $self->{dbh} = $req_rec->pnotes( "INCOM_DBH" );
100  
101 my $root = $req_rec->dir_config( "INCOM_ROOT" );
102 $root = $req_rec->server_root_relative( $root );
103 my ($current) = $req_rec->filename =~ m!^$root/*(.*)!;
104 $self->{current_page} = $current;
105 $self->{last_page} = $self->{session}{_incom_last_page};
106  
107 # Save current for next session
108 $self->{session}{_incom_last_page} = $current;
109  
110 # Setup the database object
111 my $sql_profile = $req_rec->dir_config( "INCOM_SEARCH_PROFILE" )
112 || "conf/search_profiles.pl";
113 unless ( $sql_profile eq "NONE" ) {
114 $sql_profile = $req_rec->server_root_relative( $sql_profile );
115 $self->{database} = new DBIx::SearchProfiles( $self->{dbh},
116 $sql_profile );
117  
118 # Setup the UserDB object
119 my $userdb_tmpl = $req_rec->dir_config( "INCOM_USERDB_PROFILE" );
120 unless ( $userdb_tmpl eq "NONE" ) {
121 $self->{userdb} = new DBIx::UserDB( $self->{database},
122 $userdb_tmpl,
123 $req_rec->dir_config( "INCOM_GROUPDB_PROFILE" ) );
124  
125 my $scramble = $req_rec->dir_config( "INCOM_SCRAMBLE_PASSWORD" );
126 if ( defined $scramble ) {
127 $scramble = $scramble =~ /t(rue)?|1|on|y(es)?/i;
128 $self->{userdb}->scramble_password( $scramble );
129 }
130  
131 # Load it if the user has logged into this session
132 if ( exists $self->{session}{_incom_logged_in} ) {
133 $self->{user} =
134 $self->{userdb}->user_get( $self->{session}{_incom_logged_in} );
135 }
136 }
137 }
138  
139 # Setup validator object
140 my $input_profile = $req_rec->dir_config( "INCOM_INPUT_PROFILE" )
141 || "conf/input_profiles.pl";
142 unless ( $input_profile eq "NONE" ) {
143 $input_profile = $req_rec->server_root_relative( $input_profile );
144  
145 $self->{validator} = new HTML::FormValidator( $input_profile );
146 }
147  
148 # Setup the cart object
149 my $price_profile = $req_rec->dir_config( "INCOM_PRICING_PROFILE" )
150 || "conf/pricing_profile.pl";
151 unless ( $price_profile eq "NONE" ) {
152 $price_profile = $req_rec->server_root_relative( $price_profile );
153 $self->{cart} = new Apache::iNcom::CartManager( $self->{session}{_incom_cart},
154 $package,
155 $price_profile );
156  
157 # Make sure the session contains the cart references
158 # (In case it wasn't present)
159 $self->{session}{_incom_cart} = $self->{cart}->cart();
160 }
161  
162 # Setup order manager object
163 my $order_profile = $req_rec->dir_config( "INCOM_ORDER_PROFILE" )
164 || "conf/order_profiles.pl";
165 unless ( $order_profile eq "NONE" ) {
166 $order_profile = $req_rec->server_root_relative( $order_profile );
167 $self->{order} = new Apache::iNcom::OrderManager( $self->{database},
168 $order_profile,
169 $self,
170 );
171  
172 }
173  
174 $self;
175 }
176  
177 =pod
178  
179 =head2 logged_in
180  
181 Returns true if the request is associated with a UserDB's user.
182  
183 =cut
184  
185 sub logged_in {
186 # Throw an exception if the UserDB feature was turn off.
187 croak "logged_in called when INCOM_USERDB_PROFILE set to NONE"
188 unless $_[0]->{userdb};
189 return defined $_[0]->{user};
190 }
191  
192 =pod
193  
194 =head2 user
195  
196 Returns the UserDB's user associated with the current request.
197  
198 =cut
199  
200 sub user {
201 # Throw an exception if the UserDB feature was turn off.
202 croak "user() called when INCOM_USERDB_PROFILE set to NONE"
203 unless $_[0]->{userdb};
204 return $_[0]->{user};
205 }
206  
207 =pod
208  
209 =head2 current
210  
211 Returns the name of the current page relative to INCOM_PREFIX.
212  
213 =cut
214  
215 sub current {
216 return $_[0]->{current_page};
217 }
218  
219 =pod
220  
221 =head2 previous
222  
223 Returns the name of the previous page fetched by the user.
224  
225 =cut
226  
227 sub previous {
228 return $_[0]->{last_page};
229 }
230  
231 =pod
232  
233 =head2 browser
234  
235 Returns the user agent string sent by the user's browser.
236  
237 =cut
238  
239 sub browser {
240 return $_[0]->{req_rec}->header_in( "User-Agent" );
241 }
242  
243 =pod
244  
245 =head2 remote_host
246  
247 Returns the hostname of the user. This can be an IP address is
248 hostname resolution is turn off.
249  
250 =cut
251  
252 sub remote_host {
253 return $_[0]->{req_rec}->connection->remote_host;
254  
255 }
256  
257 =pod
258  
259 =head2 remote_ip
260  
261 Returns the ip address of the user.
262  
263 =cut
264  
265 sub remote_ip {
266 return $_[0]->{req_rec}->connection->remote_ip;
267 }
268  
269 =pod
270  
271 =head2 login ( $username, $password )
272  
273 Invokes the C<login> methods of the UserDB and if the login succeeded,
274 the user will be associated with the current Session, and its informations
275 will be available on each subsequent requests until the user logout.
276  
277 =cut
278  
279 sub login {
280 my ($self,$username,$password) = @_;
281  
282 # Throw an exception if the UserDB feature was turn off.
283 croak "login called when INCOM_USERDB_PROFILE set to NONE"
284 unless $_[0]->{userdb};
285  
286 my $user;
287 if ( $user = $self->{userdb}->user_login( $username, $password ) ) {
288 # The login succeeded
289 # Update the session and save the user
290 $self->{session}{_incom_logged_in} = $user->{uid};
291  
292 $user->{last_login} = time;
293 $user->{last_host} = $self->remote_host || $self->remote_ip;
294 $user->{visits} ||= 0;
295 $user->{visits}++;
296  
297 $self->{userdb}->user_update( $user );
298  
299 # Create the user session
300 $self->{session}{_incom_user_session} = {};
301 $self->{user} = $user;
302 }
303  
304 return $user;
305 }
306  
307 =pod
308  
309 =head2 logout
310  
311 Removes the association between the user and the request.
312  
313 =cut
314  
315 sub logout {
316 my $self = shift;
317  
318 # Throw an exception if the UserDB feature was turn off.
319 croak "logout called when INCOM_USERDB_PROFILE set to NONE"
320 unless $self->{userdb};
321  
322 my $save_cart = shift;
323 if ( exists $self->{user} ) {
324 delete $self->{user};
325 delete $self->{session}{_incom_logged_in};
326 delete $self->{session}{_incom_user_session};
327 }
328 }
329  
330 # We need to use globals for the magic
331 # symbol table manipulation, because
332 # Include files remember the state
333 # of lexical variable -> closure.
334 use vars qw( $DB %Session %UserSession $package $Cart $Request $UserDB
335 $Validator $Order $Locale $Localizer ); #)
336  
337 BEGIN {
338 push @EXPORT_OK, qw( $package );
339 $EXPORT_TAGS{globals} = [ qw( $DB %Session %UserSession $Cart $Request
340 $UserDB $Validator $Order $Locale
341 $Localizer ) ];
342 $EXPORT_TAGS{functions} = [ qw( Localize Currency Include
343 TextInclude QueryArgs ) ];
344  
345 Exporter::export_ok_tags( 'globals' );
346 Exporter::export_ok_tags( 'functions' );
347 }
348  
349 =pod
350  
351 =head1 APACHE::INCOM PAGE GLOBALS
352  
353 Here is a list of the global variables that are defined in the page when
354 it is executing.
355  
356 =over
357  
358 =item $Request
359  
360 An Apache::iNcom::Request object which can used to query information about
361 the current request.
362  
363 =item $DB
364  
365 A DBIx::SearchProfiles object initialized with as requested by the
366 Apache::iNcom configuration.
367  
368 =item $Cart
369  
370 An Apache::iNcom::CartManager object initialized with the configured
371 pricing profile.
372  
373 =item $Order
374  
375 An Apache::iNcom::OrderManager object initialized with the configured
376 order profiles.
377  
378 =item %Session
379  
380 A hash which associated with the current client. Values in that hash
381 will persist across request until the user close its browser or
382 C<INCOM_SESSION_EXPIRES> time has elapsed.
383  
384 =item %UserSession
385  
386 A hash which associated with the user currently logged. Values in that
387 hash will persist across request as long as the client is logged in
388 and will be cleared once the user logs out.
389  
390  
391 =item $UserDB
392  
393 A DBIx::UserDB object which should be used for user management.
394  
395 =item $Validator
396  
397 An HTML::Validator object initialized with the configured input
398 profiles.
399  
400 =item $Localizer
401  
402 An Apache::iNcom::Localizer object initialized with the user requested
403 language.
404  
405 =item $Locale
406  
407 A Locale::Maketext object initialized with the proper locale. The
408 Locale::Maketext subclass used is specified in the C<INCOM_LOCALE>
409 configuration directives.
410  
411 =back
412  
413 =cut
414  
415 sub setup_aliases {
416 my ( $self ) = shift;
417  
418 $package = $self->{package};
419 $DB = $self->{database};
420 $Cart = $self->{cart};
421 $Order = $self->{order};
422 $Request = $self;
423 *Session = $self->{session};
424 *UserSession = $self->{session}{_incom_user_session};
425 $UserDB = $self->{userdb};
426 $Validator = $self->{validator};
427 $Localizer = $self->{req_rec}->pnotes( "INCOM_LOCALIZER" );
428 if ( $self->{req_rec}->dir_config( "INCOM_LOCALE" ) ) {
429 $Locale =
430 $Localizer->get_handle( $self->{req_rec}->dir_config( "INCOM_LOCALE" ) );
431 }
432 # Play magic in the namespace of the page
433 {
434 no strict 'refs';
435  
436 *{"$package\:\:DB"} = \$DB if $DB;
437 *{"$package\:\:UserDB"} = \$UserDB if $UserDB;
438 *{"$package\:\:Cart"} = \$Cart if $Cart;
439 *{"$package\:\:Order"} = \$Order if $Cart;
440 *{"$package\:\:Validator"} = \$Validator if $Validator;
441 *{"$package\:\:Locale"} = \$Locale if $Locale;
442 *{"$package\:\:Localizer"} = \$Localizer if $Localizer;
443 *{"$package\:\:Session"} = \%Session;
444 *{"$package\:\:UserSession"} = \%UserSession;
445 *{"$package\:\:Request"} = \$Request;
446 *{"$package\:\:Localize"} = \&Localize if $Locale;
447 *{"$package\:\:Currency"} = \&Currency if $Locale;
448 *{"$package\:\:Include"} = \&Include;
449 *{"$package\:\:TextInclude"} = \&TextInclude;
450 *{"$package\:\:QueryArgs"} = \&QueryArgs;
451 };
452 }
453  
454 sub cleanup_aliases {
455 my ( $self ) = shift;
456  
457 $package = undef;
458 $DB = undef;
459 $Cart = undef;
460 $Order = undef;
461 $Request = undef;
462 *Session = undef; # Undef the symbol, do not destroy the hash
463 *UserSession = undef;
464 $UserDB = undef;
465 $Validator = undef;
466 $Localizer = undef;
467 $Locale = undef;
468  
469 # Play magic in the namespace of the page
470 {
471 no strict 'refs';
472  
473 *{"$package\:\:DB"} = undef;
474 *{"$package\:\:UserDB"} = undef;
475 *{"$package\:\:Cart"} = undef;
476 *{"$package\:\:Order"} = undef;
477 *{"$package\:\:Validator"} = undef;
478 *{"$package\:\:Locale"} = undef;
479 *{"$package\:\:Localizer"} = undef;
480 *{"$package\:\:Session"} = undef;
481 *{"$package\:\:UserSession"} = undef;
482 *{"$package\:\:Request"} = undef;
483 *{"$package\:\:Localize"} = undef;
484 *{"$package\:\:Currency"} = undef;
485 *{"$package\:\:Include"} = undef;
486 *{"$package\:\:TextInclude"} = undef;
487 *{"$package\:\:QueryArgs"} = undef;
488 };
489 }
490  
491 =pod
492  
493 =head1 APACHE::INCOM PAGE FUNCTIONS
494  
495 Here is list of the helper functions that are defined in the context
496 of the executing page and that can be used.
497  
498 =head2 Localize ( ... )
499  
500 This acts as a wrapper around $Locale->maketext. It should be used
501 to format messages in a localized format for the user.
502  
503 =cut
504  
505 sub Localize {
506 return $Locale->maketext( @_ );
507 }
508  
509 =pod
510  
511 =head2 Currency ( ... )
512  
513 This acts as a wrapper around $Locale->currency. It should be used to
514 format amount of money for display.
515  
516 =cut
517  
518 sub Currency {
519 return $Locale->currency( @_ );
520 }
521  
522 =pod
523  
524 =head2 Include ( $file_or_param_ref )
525  
526 This is a function which is like HTML::Embperl::Execute in that it
527 includes another page in the current one. The difference is that this
528 included page will be executed in the name space of the current so
529 that all global variables remains accessible. Also, this functions checks
530 for the presence of a localized version of the file and checks in the
531 C<INCOM_TEMPLATE_PATH> if the path is not absolute.
532  
533 =cut
534  
535 sub Include {
536 my ($file,$params) ;
537 my $r = $Request->{req_rec};
538 # Check if we are called with a file or hash param
539 if ( ref $_[0] ) {
540 # We hope the caller knows what he was doing when
541 # he setup the parameter hash.
542 $params = shift;
543 $file = $params->{inputfile};
544 } else {
545 $file = shift;
546  
547 # Create default params
548 # Since Include file can contain fragment,
549 my $debug = $r->dir_config( "EMBPERL_DEBUG" ) ||
550 $ENV{EMBPERL_DEBUG} || 0;
551 # Default = optRawInput + optDisableTableScan
552 # disable Table scan by default
553 my $options = $r->dir_config( "EMBPERL_OPTIONS" ) ||
554 $ENV{EMBPERL_OPTIONS} || 16 | 2048;
555 # optDisableFormData
556 $options |= 256 ;
557 $params = {
558 param => \@_,
559 options => $options,
560 debug => $debug,
561 };
562 }
563  
564 # Search for template in TEMPLATE_PATH
565 my $path = $r->pnotes( "INCOM_TEMPLATE_PATH" );
566  
567 unless ( $path ) {
568 # Memoize template path
569 $path = [];
570 foreach my $p ( split /:/, $r->dir_config( "INCOM_TEMPLATE_PATH" ) ) {
571 push @$path, $r->server_root_relative( $p );
572 }
573 $r->pnotes( "INCOM_TEMPLATE_PATH", $path );
574 }
575 unless ( substr( $file, 0, 1) eq "/" || -e $file ) {
576 foreach my $p ( @$path ) {
577 if ( -e $p . "/" . $file ) {
578 $file = $p . "/" . $file;
579 last;
580 }
581 }
582 }
583  
584 # Localize the template
585 my $localizer = $r->pnotes( "INCOM_LOCALIZER" );
586 $file = $localizer->find_localized_file( $file );
587  
588 # Set the localized and normalized file
589 $params->{inputfile} = $file;
590  
591 # Set the package in which to execute the template
592 $params->{package} = $package;
593  
594 HTML::Embperl::Execute( $params );
595 }
596  
597 =pod
598  
599 =head2 TextInclude ( file )
600  
601 This is a function is like C<Include> except that it is not interpreted
602 for embedded perl.
603  
604 =cut
605  
606 sub TextInclude {
607 my $file = shift;
608  
609 # Search for template in TEMPLATE_PATH
610 my $r = $Request->{req_rec};
611 my $path = $r->pnotes( "INCOM_TEMPLATE_PATH" );
612  
613 unless ( $path ) {
614 # Memoize template path
615 $path = [];
616 foreach my $p ( split /:/, $r->dir_config( "INCOM_TEMPLATE_PATH" ) ) {
617 push @$path, $r->server_root_relative( $p );
618 }
619 $r->pnotes( "INCOM_TEMPLATE_PATH", $path );
620 }
621  
622 unless ( substr( $file, 0, 1) eq "/" || -e $file ) {
623 foreach my $p ( @$path ) {
624 if ( -e $p . "/" . $file ) {
625 $file = $p . "/" . $file;
626 last;
627 }
628 }
629 }
630  
631 # Localize the template
632 my $localizer = $r->pnotes( "INCOM_LOCALIZER" );
633 $file = $localizer->find_localized_file( $file );
634  
635 {
636 no strict 'refs';
637 my $fh = gensym;
638 open $fh, $file
639 or die "can't open file $file\n";
640 print {"$package\:\:OUT"} <$fh>;
641 close $fh;
642 };
643  
644 }
645  
646 =pod
647  
648 =head2 QueryArgs ( [ $fdat ], [ $odat ], [ $idat ] )
649  
650 This function is similar to the [$ hidden $] directive in
651 HTML::Embperl but instead of generating hidden input fields, it
652 returns the form data as a query string.
653  
654 =over
655  
656 =item $fdat
657  
658 The form data to output. Defaults to the %fdat hash.
659  
660 =item $odat
661  
662 Override data. Fields present in that hash will override the
663 one in %fdat.
664  
665 =item $idat
666  
667 Ignored data. Fields present in that hash will be ignored and
668 not output in the resulting query string.
669  
670 =back
671  
672 Usage example :
673  
674 <a href="search.html?[- QueryArgs() -]">Next</a>
675  
676 =cut
677  
678 sub QueryArgs {
679 my ( $fdat,$odat,$idat) = @_;
680  
681 {
682 # Needed to access symbolically the page variables
683 no strict 'refs';
684 $fdat ||= *{"$package\:\:fdat"};
685 };
686 $odat ||= {};
687 $idat ||= {};
688 my $uri = join "&", map {
689 if ( exists $odat->{$_} ) {
690 Apache::Util::escape_uri( $_ ) . "=" .
691 Apache::Util::escape_uri( $odat->{$_} );
692 } elsif ( not exists $idat->{$_} ) {
693 Apache::Util::escape_uri( $_ ) . "=" .
694 Apache::Util::escape_uri( $fdat->{$_} );
695 }
696 } keys %$fdat;
697  
698 {
699 no strict 'refs';
700 my $old = ${"$package\:\:escmode"};
701 ${"$package\:\:escmode"} = 0;
702 print {"$package\:\:OUT"} $uri;
703 ${"$package\:\:escmode"} = $old;
704 };
705 }
706  
707 1;
708  
709 __END__