]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/WWW/Reporter.pm
passing stage dir
[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
60         # if the user is not logged in via cookie, route them to the login page
61         if(!(verify_login( $cgi->cookie("ses") ))) {
62                 $ttk = "login";
63         }
64
65         print "Content-type: text/html; charset=utf-8\n\n";
66
67         _process_template(
68                         apache          => $apache,
69                         template                => "$ttk.ttk",
70                         params          => { stage_dir => $ttk, config_xml => $base_xml },
71                         );
72
73         return Apache2::Const::OK;
74 }
75
76
77 sub _process_template {
78
79         my %params = @_;
80         my $ttk                         = $params{template}             || return undef;
81         my $apache                      = $params{apache}                       || undef;
82         my $param_hash          = $params{params}                       || {};
83
84         my $template;
85
86         $template = Template->new( { 
87                 OUTPUT                  => $apache, 
88                 ABSOLUTE                => 1, 
89                 RELATIVE                => 1,
90                 PLUGIN_BASE             => $plugin_base,
91                 INCLUDE_PATH    => $includes, 
92                 PRE_CHOMP               => 1,
93                 POST_CHOMP              => 1,
94                 #LOAD_PERL              => 1,
95                 } 
96         );
97
98         try {
99
100                 if( ! $template->process( $ttk, $param_hash ) ) { 
101                         warn  "Error Processing Template: " . $template->error();
102                         my $err = $template->error();
103                         $err =~ s/\n/\<br\/\>/g;
104                         warn "Error processing template $ttk\n";        
105                         my $string =  "<br><b>Unable to process template:<br/><br/> " . $err . "</b>";
106                         print "ERROR: $string";
107                         #$template->process( $error_ttk , { error => $string } );
108                 }
109
110         } catch Error with {
111                 my $e = shift;
112                 warn "Error processing template $ttk:  $e - $@ \n";     
113                 print "<center><br/><br/><b>Error<br/><br/> $e <br/><br/> $@ </b><br/></center>";
114                 return;
115         };
116
117 }
118
119 # returns 1 if the session is valid, 0 otherwise
120 sub verify_login {
121         my $auth_token = shift;
122         return 0 unless $auth_token;
123
124         my $session = OpenSRF::AppSession->create("open-ils.auth");
125         my $req = $session->request("open-ils.auth.session.retrieve", $auth_token );
126         my $user = $req->gather(1);
127
128         return 1 if ref($user);
129         return 0;
130 }
131
132
133
134 1;