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