| 1 | package CGI::AppBuilder::Apps; | ||
| 2 | |||
| 3 | # Perl standard modules | ||
| 4 | # use strict; | ||
| 5 | use warnings; | ||
| 6 | # use Getopt::Std; | ||
| 7 | use POSIX qw(strftime); | ||
| 8 | # use CGI; | ||
| 9 | # use CGI::Carp qw(fatalsToBrowser warningsToBrowser); | ||
| 10 | # use CGI::Pretty ':standard'; | ||
| 11 | use CGI::AppBuilder; | ||
| 12 | |||
| 13 | our $VERSION = 1.0001; | ||
| 14 | |||
| 15 | # require Exporter; | ||
| 16 | our @ISA = qw(Exporter CGI::AppBuilder); | ||
| 17 | our @EXPORT = qw(); | ||
| 18 | our @EXPORT_OK = (qw(start_app end_app build_html_header)); | ||
| 19 | our %EXPORT_TAGS = ( | ||
| 20 | all => [@EXPORT_OK] | ||
| 21 | ); | ||
| 22 | |||
| 23 | =head1 NAME | ||
| 24 | |||
| 25 | CGI::AppBuilder - CGI Application Builder | ||
| 26 | |||
| 27 | =head1 SYNOPSIS | ||
| 28 | |||
| 29 | use CGI::AppBuilder::Apps; | ||
| 30 | |||
| 31 | my $cg = CGI::AppBuilder::Apps->new('ifn', 'my_init.cfg', 'opt', 'vhS:a:'); | ||
| 32 | my $ar = $cg->get_inputs; | ||
| 33 | |||
| 34 | =head1 DESCRIPTION | ||
| 35 | |||
| 36 | There are already many application builders out there. Why you need | ||
| 37 | another one? Well, if you are already familiar with CGI::Builder or | ||
| 38 | CGI::Application, this one will provide some useful methods to you to | ||
| 39 | read your configuration file and pre-process your templates. | ||
| 40 | Please read on. | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | =head3 new (ifn => 'file.cfg', opt => 'hvS:') | ||
| 45 | |||
| 46 | Input variables: | ||
| 47 | |||
| 48 | $ifn - input/initial file name. | ||
| 49 | $opt - options for Getopt::Std | ||
| 50 | |||
| 51 | Variables used or routines called: | ||
| 52 | |||
| 53 | None | ||
| 54 | |||
| 55 | How to use: | ||
| 56 | |||
| 57 | my $ca = new CGI::AppBuilder::Apps; # or | ||
| 58 | my $ca = CGI::AppBuilder::Apps->new; # or | ||
| 59 | my $ca = CGI::AppBuilder::Apps->new(ifn=>'file.cfg',opt=>'hvS:'); # or | ||
| 60 | my $ca = CGI::AppBuilder::Apps->new('ifn', 'file.cfg','opt','hvS:'); | ||
| 61 | |||
| 62 | Return: new empty or initialized CGI::AppBuilder::Apps object. | ||
| 63 | |||
| 64 | This method constructs a Perl object and capture any parameters if | ||
| 65 | specified. It creates and defaults the following variables: | ||
| 66 | |||
| 67 | $self->{ifn} = "" | ||
| 68 | $self->{opt} = 'hvS:'; | ||
| 69 | |||
| 70 | =cut | ||
| 71 | |||
| 72 | sub new { | ||
| 73 | my ($s, %args) = @_; | ||
| 74 | return $s->SUPER::new(%args); | ||
| 75 | } | ||
| 76 | |||
| 77 | =head3 start_app ($prg,$arg,$nhh) | ||
| 78 | |||
| 79 | Input variables: | ||
| 80 | |||
| 81 | $prg - program name | ||
| 82 | $arg - array ref for arguments - %ARGV | ||
| 83 | $nhh - no html header pre-printed | ||
| 84 | 0 - HTML header will be set when it is possible | ||
| 85 | 1 - no HTML header is set in any circumstance | ||
| 86 | |||
| 87 | Variables used or routines called: | ||
| 88 | |||
| 89 | build_html_header - build HTML header array | ||
| 90 | echo_msg - echo messages | ||
| 91 | start_log - start and write message log | ||
| 92 | get_inputs - read input file and/or CGI form inputs | ||
| 93 | |||
| 94 | |||
| 95 | How to use: | ||
| 96 | |||
| 97 | my ($q, $ar, $ar_log) = $self->start_app($0,\@ARGV); | ||
| 98 | |||
| 99 | Return: ($q,$ar,$ar_log) where | ||
| 100 | |||
| 101 | $q - a CGI object | ||
| 102 | $ar - hash ref containing parameters from input file and/or | ||
| 103 | CGI form inputs and the following elements: | ||
| 104 | ifn - initial file name | ||
| 105 | opt - command input options | ||
| 106 | cfg - configuration array | ||
| 107 | html_header - HTML header parameters (hash ref) | ||
| 108 | msg - contain message hash | ||
| 109 | $ar_log - hash ref containing log information | ||
| 110 | |||
| 111 | This method performs the following tasks: | ||
| 112 | |||
| 113 | 1) initial a CGI object | ||
| 114 | 2) read initial file if specified or search for a default file | ||
| 115 | and save the file name to $ar->{ifn}. The default file is your | ||
| 116 | program name with .ini extension. For instance, if your CGI program | ||
| 117 | name is 'myapp.pl', it will look for a file named 'myapp.ini'. | ||
| 118 | 3) define message level. Default to level 1. | ||
| 119 | 4) start HTML header and body using I<page_title> and I<page_style> | ||
| 120 | if they are defined. | ||
| 121 | 5) parse CGI form inputs and combine them with parameters defined | ||
| 122 | in initial file | ||
| 123 | 6) read configuration file ($prg.cfgi, for instance 'myapp.cfg') if it | ||
| 124 | exists and save the array to $ar->{cfg} | ||
| 125 | 7) prepare log record if writing log is enabled | ||
| 126 | |||
| 127 | It checks the parameters read from initial file for page_title, | ||
| 128 | page_style, page_author, page_meta, top_nav, bottom_nav, and js_src. | ||
| 129 | |||
| 130 | =cut | ||
| 131 | |||
| 132 | sub start_app { | ||
| 133 | my $s = shift; | ||
| 134 | my ($prg, $ar_arg, $nhh) = @_; | ||
| 135 | |||
| 136 | # 0. check if it is in the web mode | ||
| 137 | my $web_flag = (exists $ENV{HTTP_HOST}||exists $ENV{QUERY_STRING}) ? 1 : 0; | ||
| 138 | my $ct_printed = 0; | ||
| 139 | if (defined($nhh) && $nhh =~ /^0$/ && $web_flag) { | ||
| 140 | my $ct = "Content-Type: text/html\n\n"; print $ct; $ct_printed = 1; | ||
| 141 | } | ||
| 142 | my $args = ($ar_arg && ref($ar_arg) =~ /ARRAY/)?(join " ", @$ar_arg):''; | ||
| 143 | my $frt = $prg; $frt =~ s/\.\w+$//; # file name root | ||
| 144 | my ($ifn,$cfg,$pod) = ("$frt.ini", "$frt.cfg", "$frt.pod"); | ||
| 145 | my $opt = 'a:v:hS:'; | ||
| 146 | my ($q, $ar); | ||
| 147 | |||
| 148 | # 1. check if it is in verbose mode or not | ||
| 149 | if ($web_flag) { | ||
| 150 | $q = CGI->new; | ||
| 151 | my $v1 = $q->param('v'); | ||
| 152 | my $v2 = $q->Vars->{v}; | ||
| 153 | if ((defined($v1) && $v1) || (defined($v2) && $v2)) { | ||
| 154 | if (! $ct_printed && $nhh !~ /^1$/) { | ||
| 155 | print $q->header("text/html"); ++$ct_printed; | ||
| 156 | } | ||
| 157 | } | ||
| 158 | } | ||
| 159 | # | ||
| 160 | # 2. Read initial file | ||
| 161 | ($q,$ar) = $s->get_inputs($ifn,$opt); | ||
| 162 | $ar->{ini_fn} = $ifn; | ||
| 163 | $ar->{cfg_fn} = $cfg; | ||
| 164 | $ar->{pod_fn} = $pod; | ||
| 165 | my $ck_flag = $s->set_cookies($q, $ar); # set $ar->{_cookie} | ||
| 166 | |||
| 167 | if ((exists $ar->{Verbose} && $ar->{Verbose} =~ /^Y/i) || | ||
| 168 | (exists $ar->{v} && $ar->{v}) ){ | ||
| 169 | print $q->header("text/html") if ! $ct_printed; | ||
| 170 | } | ||
| 171 | $s->echo_msg(" += Starting application...",2); | ||
| 172 | $s->echo_msg(" ++ Reading initial file $ifn...",2) if -f $ifn; | ||
| 173 | $s->echo_msg(" + Initial file - $ifn: not found.",2) if !-f $ifn; | ||
| 174 | # if user has defined messages in the initial file, we need to | ||
| 175 | # convert it into hash. | ||
| 176 | $ar->{msg} = eval $ar->{msg} if exists $ar->{msg}; | ||
| 177 | |||
| 178 | # 3. start HTML header | ||
| 179 | my %ar_hdr = $s->build_html_header($q, $ar); | ||
| 180 | $ar->{html_header} = \%ar_hdr if ! exists $ar->{html_header}; | ||
| 181 | $ar->{svr_name} = `hostname` | ||
| 182 | if (!exists $ar->{svr_name} || !$ar->{svr_name}) && $^O !~ /^MSWin/i; | ||
| 183 | chomp $ar->{svr_name} if exists $ar->{svr_name}; | ||
| 184 | |||
| 185 | # 4. start the HTML page | ||
| 186 | my $pretty = $s->set_param('tab_pretty', $ar); | ||
| 187 | if (!$nhh && ( | ||
| 188 | exists $ENV{HTTP_HOST} || exists $ENV{QUERY_STRING})) { | ||
| 189 | print $q->header("text/html") if ! $ct_printed; | ||
| 190 | ++$ct_printed; | ||
| 191 | print "\n" if $pretty; | ||
| 192 | print $q->start_html(%ar_hdr); | ||
| 193 | print "\n" if $pretty; | ||
| 194 | print $ar->{top_nav} if exists $ar->{top_nav} && $ar->{top_nav}; | ||
| 195 | print "\n" if $pretty; | ||
| 196 | } | ||
| 197 | |||
| 198 | # 5. read configuration file | ||
| 199 | if (-f $cfg) { | ||
| 200 | $s->echo_msg(" ++ Reading config file $cfg...",1); | ||
| 201 | $ar->{cfg} = $s->read_cfg_file($cfg); | ||
| 202 | } else { | ||
| 203 | $s->echo_msg("WARN: (start_app) could not find config file $cfg.", 3); | ||
| 204 | } | ||
| 205 | |||
| 206 | # 6. set log array | ||
| 207 | my ($ds,$log_dir,$log_brf, $log_dtl) = ('/',"","",""); | ||
| 208 | $log_dir = (exists ${$ar}{log_dir})?${$ar}{log_dir}:'.'; | ||
| 209 | my $lgf = $ifn; $lgf =~ s/\.\w+//; $lgf =~ s/.*[\/\\](\w+)$/$1/; | ||
| 210 | my $tmp = ""; | ||
| 211 | $tmp = $ar->{svr_name} if exists $ar->{svr_name} && $ar->{svr_name}; | ||
| 212 | $tmp .= '_' . (strftime "%Y%m%d", localtime time); | ||
| 213 | $log_brf = join $ds, $log_dir, "${lgf}_brief.log"; | ||
| 214 | $log_dtl = join $ds, $log_dir, "${lgf}_${tmp}.log"; | ||
| 215 | my ($lfh_brf,$lfh_dtl,$txt,$ar_log) = ("","","",""); | ||
| 216 | if (exists ${$ar}{write_log} && ${$ar}{write_log}) { | ||
| 217 | $ar_log = $s->start_log($log_dtl,$log_brf,"",$args,2); | ||
| 218 | $s->{write_log} = $ar->{write_log}; | ||
| 219 | } | ||
| 220 | |||
| 221 | # my $c2 = $s->get_cookies($q, $ar); | ||
| 222 | # $s->echo_msg($c2, 0); | ||
| 223 | |||
| 224 | $s->echo_msg($ar,5); | ||
| 225 | $s->echo_msg($ar_log,5); | ||
| 226 | return ($q,$ar,$ar_log); | ||
| 227 | } | ||
| 228 | |||
| 229 | =head3 end_app ($q, $ar, $ar_log, $nhh) | ||
| 230 | |||
| 231 | Input variables: | ||
| 232 | |||
| 233 | $q - CGI object | ||
| 234 | $ar - array ref for parameters | ||
| 235 | $ar_log - hash ref for log record | ||
| 236 | $nhh - no html header pre-printed | ||
| 237 | 1 - no HTML is printed in any circumstance | ||
| 238 | 0 - HTML header will be printed when it is possible | ||
| 239 | |||
| 240 | Variables used or routines called: | ||
| 241 | |||
| 242 | echo_msg - echo messages | ||
| 243 | end_log - start and write message log | ||
| 244 | set_param - get a parameter from hash array | ||
| 245 | |||
| 246 | How to use: | ||
| 247 | |||
| 248 | my ($q, $ar, $ar_log) = $self->start_app($0,\@ARGV); | ||
| 249 | $self->end_app($q, $ar, $ar_log); | ||
| 250 | |||
| 251 | Return: none | ||
| 252 | |||
| 253 | This method performs the following tasks: | ||
| 254 | |||
| 255 | 1) ends HTML document | ||
| 256 | 2) writes log records to log files | ||
| 257 | 3) close database connection if it finds DB handler in {dbh} | ||
| 258 | |||
| 259 | =cut | ||
| 260 | |||
| 261 | sub end_app { | ||
| 262 | my $s = shift; | ||
| 263 | my ($q, $ar, $ar_log, $nhh) = @_; | ||
| 264 | if (exists ${$ar}{write_log} && ${$ar}{write_log}) { | ||
| 265 | $s->end_log($ar_log); | ||
| 266 | } | ||
| 267 | my $dbh = $s->set_param('dbh', $ar); | ||
| 268 | my $pretty = $s->set_param('tab_pretty', $ar); | ||
| 269 | $dbh->disconnect() if $dbh; | ||
| 270 | if (exists $ar->{bottom_nav} && $pretty) { | ||
| 271 | $ar->{bottom_nav} =~ s/<br>/<br>\n/g; | ||
| 272 | } | ||
| 273 | if (exists $ENV{HTTP_HOST} || exists $ENV{QUERY_STRING}) { | ||
| 274 | print $ar->{bottom_nav} if exists $ar->{bottom_nav} && !$nhh; | ||
| 275 | print "\n" if !$nhh && $pretty; | ||
| 276 | print $q->end_html if !$nhh; | ||
| 277 | } | ||
| 278 | # clear the stored parameters | ||
| 279 | $_[0] = undef; | ||
| 280 | $_[1] = undef; | ||
| 281 | $_[2] = undef; | ||
| 282 | $_[3] = undef; | ||
| 283 | } | ||
| 284 | |||
| 285 | 1; | ||
| 286 | |||
| 287 | =head1 HISTORY | ||
| 288 | |||
| 289 | =over 4 | ||
| 290 | |||
| 291 | =item * Version 0.10 | ||
| 292 | |||
| 293 | This version is to extract out the app methods from CGI::Getopt class. | ||
| 294 | It was too much for CGI::Getopt to include the start_app, end_app, | ||
| 295 | build_html_header, and disp_form methods. | ||
| 296 | |||
| 297 | 0.11 Rewrote start_app method so that content-type can be changed. | ||
| 298 | 0.12 Moved disp_form to CGI::AppBuilder::Form, | ||
| 299 | moved build_html_header to CGI::AppBuilder::Header, and | ||
| 300 | imported all the methods in sub-classes into this class. | ||
| 301 | |||
| 302 | =item * Version 1.0001 | ||
| 303 | |||
| 304 | The start_app and end_app were extracted out of CGI::AppBuilder class and formed | ||
| 305 | this sub class. | ||
| 306 | |||
| 307 | =cut | ||
| 308 | |||
| 309 | =head1 SEE ALSO (some of docs that I check often) | ||
| 310 | |||
| 311 | Oracle::Loader, Oracle::Trigger, CGI::Getopt, File::Xcopy, | ||
| 312 | CGI::AppBuilder, CGI::AppBuilder::Message, CGI::AppBuilder::Log, | ||
| 313 | CGI::AppBuilder::Config, etc. | ||
| 314 | |||
| 315 | =head1 AUTHOR | ||
| 316 | |||
| 317 | Copyright (c) 2005 ~ 2015 Hanming Tu. All rights reserved. | ||
| 318 | |||
| 319 | This package is free software and is provided "as is" without express | ||
| 320 | or implied warranty. It may be used, redistributed and/or modified | ||
| 321 | under the terms of the Perl Artistic License (see | ||
| 322 | http://www.perl.com/perl/misc/Artistic.html) | ||
| 323 | |||
| 324 | =cut | ||
| 325 | |||
| 326 |