]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/WWW/Reporter.pm
fixing session timeout bug
[working/Evergreen.git] / Open-ILS / src / perlmods / OpenILS / WWW / Reporter.pm
1 package OpenILS::WWW::Reporter;
2 use strict; use warnings;
3
4 use vars qw/$dtype_xform_map $dtype_xform/;
5
6 use Apache2 ();
7 use Apache2::Log;
8 use Apache2::Const -compile => qw(OK REDIRECT DECLINED :log);
9 use APR::Const    -compile => qw(:error SUCCESS);
10 use Apache2::RequestRec ();
11 use Apache2::RequestIO ();
12 use Apache2::RequestUtil;
13 use CGI;
14 use Data::Dumper;
15
16 use Template qw(:template);
17
18 use OpenSRF::EX qw(:try);
19 use OpenSRF::System;
20 use XML::LibXML;
21
22 use OpenSRF::Utils::SettingsParser;
23 use OpenILS::Utils::Fieldmapper;
24 use OpenILS::WWW::Reporter::transforms;
25
26
27 # set the bootstrap config and template include directory when 
28 # this module is loaded
29 my $bootstrap;
30 my $includes = [];  
31 my $base_xml;
32 #my $base_xml_doc;
33
34 sub import {
35         my( $self, $bs_config, $core_xml, @incs ) = @_;
36         $bootstrap = $bs_config;
37         $base_xml = $core_xml;
38         $includes = [ @incs ];
39 }
40
41
42 # our templates plugins are here
43 my $plugin_base = 'OpenILS::Template::Plugin';
44
45 sub child_init {
46         OpenSRF::System->bootstrap_client( config_file => $bootstrap );
47
48         #parse the base xml file
49         #my $parser = XML::LibXML->new;
50         #$parser->expand_xinclude(1);
51
52         #$base_xml_doc = $parser->parse_file($base_xml);
53
54 }
55
56 sub handler {
57
58         my $apache = shift;
59         return Apache2::Const::DECLINED if (-e $apache->filename);
60
61         my $cgi = CGI->new;
62
63         my $path = $apache->path_info;
64         (my $ttk = $path) =~ s{^/?([a-zA-Z0-9_]+).*?$}{$1}o;
65
66         $ttk = $apache->filename unless $ttk;
67         $ttk = "dashboard" unless $ttk;
68
69         $ttk = (split '/', $ttk)[-1];
70         
71         my $user;
72
73         # if the user is not logged in via cookie, route them to the login page
74         if(! ($user = verify_login($cgi->cookie("ses"))) ) {
75                 $ttk = "login";
76         }
77
78
79         print "Content-type: text/html; charset=utf-8\n\n";
80         #print "Content-type: text/html\n\n";
81
82         _process_template(
83                         apache          => $apache,
84                         template                => "$ttk.ttk",
85                         params          => { 
86                                 user => $user, 
87                                 stage_dir => $ttk, 
88                                 config_xml => $base_xml, 
89                                 },
90                         );
91
92         return Apache2::Const::OK;
93 }
94
95
96 sub _process_template {
97
98         my %params = @_;
99         my $ttk                         = $params{template}             || return undef;
100         my $apache                      = $params{apache}                       || undef;
101         my $param_hash          = $params{params}                       || {};
102         $$param_hash{dtype_xform_map} = $OpenILS::WWW::Reporter::dtype_xform_map;
103         $$param_hash{dtype_xforms} = $OpenILS::WWW::Reporter::dtype_xforms;
104
105         my $template;
106
107         $template = Template->new( { 
108                 OUTPUT                  => $apache, 
109                 ABSOLUTE                => 1, 
110                 RELATIVE                => 1,
111                 PLUGIN_BASE             => $plugin_base,
112                 INCLUDE_PATH    => $includes, 
113                 PRE_CHOMP               => 1,
114                 POST_CHOMP              => 1,
115                 #LOAD_PERL              => 1,
116                 } 
117         );
118
119         try {
120
121                 if( ! $template->process( $ttk, $param_hash ) ) { 
122                         warn  "Error Processing Template: " . $template->error();
123                         my $err = $template->error();
124                         $err =~ s/\n/\<br\/\>/g;
125                         warn "Error processing template $ttk\n";        
126                         my $string =  "<br><b>Unable to process template:<br/><br/> " . $err . "</b>";
127                         print "ERROR: $string";
128                         #$template->process( $error_ttk , { error => $string } );
129                 }
130
131         } catch Error with {
132                 my $e = shift;
133                 warn "Error processing template $ttk:  $e - $@ \n";     
134                 print "<center><br/><br/><b>Error<br/><br/> $e <br/><br/> $@ </b><br/></center>";
135                 return;
136         };
137
138 }
139
140 # returns the user object if the session is valid, 0 otherwise
141 sub verify_login {
142         my $auth_token = shift;
143         return 0 unless $auth_token;
144
145         my $session = OpenSRF::AppSession->create("open-ils.auth");
146         my $req = $session->request(
147                 "open-ils.auth.session.retrieve", $auth_token );
148         my $user = $req->gather(1);
149
150         if (ref($user) eq 'HASH' && $user->{ilsevent} == 1001) {
151                 return 0;
152         }
153
154         return $user if ref($user);
155         return 0;
156 }
157
158
159
160 1;