Apache2-PageKit
view release on metacpan or search on metacpan
lib/Apache2/PageKit.pm view on Meta::CPAN
}
}
sub open_view {
my ($pk) = @_;
my $pkit_view = $pk->{apr}->param('pkit_view') || 'Default';
# open template file
$pk->{view}->open_view($pk->{page_id}, $pkit_view, $pk->{lang});
}
sub prepare_and_print_view {
my ($pk) = @_;
my $apr = $pk->{apr};
my $view = $pk->{view};
my $config = $pk->{config};
my $model = $pk->{model};
my $page_id = $pk->{page_id};
# set view fillinform_objects and associated_objects, if approriate
my $fill_in_form = $config->get_page_attr($page_id,'fill_in_form') || 'yes';
# $apr comes first, so that fillinform overrides request parameters
my @fillinform_objects_array = ( $apr, $pk->{fillinform_object} );
if ( $fill_in_form eq 'no' ) {
# we want only the $pk->{fillinform_object} object
shift @fillinform_objects_array;
}
$view->{fillinform_objects} = [ grep {$_->param} @fillinform_objects_array ];
$view->{ignore_fillinform_fields} = $pk->{ignore_fillinform_fields};
my $request_param_in_tmpl = $config->get_page_attr($page_id,'request_param_in_tmpl')
|| $config->get_global_attr('request_param_in_tmpl')
|| 'no';
if( $request_param_in_tmpl eq 'yes' ) {
$view->{associated_objects} = [$apr];
}
# set up page template and run component code
my $output_ref = $view->fill_in_view;
# determine output media type
my $pkit_view = $apr->param('pkit_view') || 'Default';
my $output_media = $config->get_page_attr($page_id, 'content_type')
|| $config->get_view_attr($pkit_view, 'content_type')
|| $Apache2::PageKit::DefaultMediaMap{$pkit_view}
|| 'text/html';
# set expires to now so prevent caching
#$apr->no_cache(1) if $apr->param('pkit_logout') || $config->get_page_attr($pk->{page_id},'template_cache') eq 'no';
# see http://support.microsoft.com/support/kb/articles/Q234/0/67.ASP
# and http://www.pacificnet.net/~johnr/meta.html
my $browser_cache = $config->get_page_attr($page_id,'browser_cache') || $pk->{browser_cache} || 'yes';
$apr->headers_out->{'Expires'} = '-1' if $apr->param('pkit_logout') || $browser_cache eq 'no' || $apr->user;
my $content_type = $output_media;
my $default_output_charset = $view->{default_output_charset};
my @charsets = ();
if($output_media eq 'text/html'){
# first get accepted charsets from incoming Accept-Charset HTTP header
if(my $accept_charset = $apr->headers_in->{'Accept-Charset'}){
my @quality = split(/\s*;\s*/, $accept_charset);
my @accept_charsets = split(/\s*,\s*/, shift @quality);
my $pos = 0;
for ( @accept_charsets ) {
s/^(iso|utf)/\U$1/;
s/^(us\-)?ascii/US-ASCII/;
$quality[$pos] =~ /^q=(\d+(?:\.\d+)?)/;
push @charsets, [ $_, $1 || '0.1', $pos++ ];
}
@charsets = sort {$b->[1] <=> $a->[1] || $a->[2] <=> $b->[2] } @charsets;
# set a content-type perhaps we overwrite this later if we know about the charset for the output pages
}
} elsif ($output_media eq 'application/pdf'){
my $fop_command = $config->get_server_attr('fop_command')
|| $config->get_global_attr('fop_command');
if ( $fop_command ) {
# write output_media to file, using process number of Apache child process
my $view_cache_dir = $view->{cache_dir};
my $fo_file = "$view_cache_dir/$$.fo";
my $pdf_file = "$view_cache_dir/$$.pdf";
open FO_TEMPLATE, ">$fo_file" or die "can't open file: $fo_file ($!)";
binmode FO_TEMPLATE;
print FO_TEMPLATE $$output_ref;
close FO_TEMPLATE;
# my $error_message = `$fop_command $fo_file $pdf_file 2>&1 1>/dev/null`;
my $error_message = `$fop_command $fo_file $pdf_file 2>&1`;
## the recommended fop converter has no usefull error messages.
## the errormoessages go also to STDOUT
## and the returncode is always 0
unless ($error_message =~ /^\[ERROR\]:/m){
local $/;
open PDF_OUTPUT, "<$pdf_file" or die "can't open file: $pdf_file ($!)";
binmode PDF_OUTPUT;
$$output_ref = <PDF_OUTPUT>;
close PDF_OUTPUT;
}
else {
die "Error processing template with Apache XML FOP: $error_message";
}
}
} else {
# just set content_type but it is already $output_media
;
}
# for a head request
if ($apr->header_only) {
$apr->content_type($content_type);
return;
}
# call output filter, if applicable
$model->pkit_output_filter($output_ref)
if $model->can('pkit_output_filter');
my ( $converted_data, $retcharset );
if ($output_media eq 'text/html'){
my $data;
while (@charsets){
$retcharset = (shift @charsets)->[0];
eval {
$converted_data = Encode::encode($retcharset, $$output_ref, Encode::FB_CROAK );
};
last unless ($@);
$retcharset = undef;
}
## here no action is needed, if we did not convert the data to anything usefull.
## we deliver in our default_output_charset.
# correct the header
if ($retcharset) {
$content_type = "text/html; charset=$retcharset";
}
else {
$content_type = "text/html; charset=$default_output_charset";
$converted_data = Encode::encode( $default_output_charset, $$output_ref,Encode::FB_DEFAULT );
}
# it is already "text/html"
}
# only pages with propper $retcharset are tranfered gzipped.
# this can maybe changed!? Needs some tests
my $send_gzipped = ( $retcharset && $pk->{use_gzip} eq 'all' );
$apr->content_encoding('gzip') if ($send_gzipped);
$apr->content_type($content_type) unless $apr->main;
if ($send_gzipped) {
$apr->print(Compress::Zlib::memGzip($converted_data || $$output_ref));
} else {
$apr->print($converted_data || $$output_ref);
}
}
sub new {
my $class = shift;
my $rr = shift || die "missing RequestRec";
my $self = {@_};
bless $self, $class;
# set up contained objects
my $pkit_root = $rr->dir_config('PKIT_ROOT');
die "Must specify PerlSetVar PKIT_ROOT in httpd.conf file" unless $pkit_root;
my $config_dir = $pkit_root . '/Config';
my $content_dir = $pkit_root . '/Content';
my $view_dir = $pkit_root . '/View';
my $server = $rr->dir_config('PKIT_SERVER');
die "Must specify PerlSetVar PKIT_SERVER in httpd.conf file" unless $server;
my $config = $self->{config} = Apache2::PageKit::Config->new(config_dir => $config_dir,
server => $server);
my $post_max = $self->{config}->get_global_attr('post_max') || 64_000_000;
my $upload_tmp_dir = $self->{config}->get_global_attr('upload_tmp_dir');
# the TEMP_DIR option is only avail since version 1.0 of libapreq
# so we set it only on request.
my @apr_params = ();
push @apr_params, TEMP_DIR => $upload_tmp_dir if $upload_tmp_dir;
my $request_class = $self->{config}->get_global_attr('request_class') || "Apache2::Request::PageKit";
my $apr = $self->{apr} = $request_class->new($rr, POST_MAX => $post_max, @apr_params);
my $model_base_class = $self->{config}->get_global_attr('model_base_class') || "MyPageKit::Common";
$self->_check_gzip;
my $model;
eval {$model = $self->{model} = $model_base_class->new(pkit_pk => $self)};
if($@){
unless($model_base_class){
die "model_base_class not specified";
} else {
die "Model class $model_base_class has no new method ($@)";
}
}
$self->{dbh} = $model->pkit_dbi_connect if $model->can('pkit_dbi_connect');
my $default_lang = $config->get_global_attr('default_lang') || 'en';
my $default_input_charset = $config->get_global_attr('default_input_charset') || 'ISO-8859-1';
my $default_output_charset = $config->get_global_attr('default_output_charset') || 'ISO-8859-1';
my $html_clean_level = $config->get_server_attr('html_clean_level') || 0;
my $can_edit = $config->get_server_attr('can_edit') || 'no';
my $reload = $config->get_server_attr('reload') || 'no';
my $cache_dir = $config->get_global_attr('cache_dir');
my $view_cache_dir = $cache_dir ? $cache_dir . '/pkit_cache' : $pkit_root . '/View/pkit_cache';
my $relaxed_parser = $config->get_global_attr('relaxed_parser') || 'no';
my $errorspan_begin_tag = $config->get_global_attr('errorspan_begin_tag') || q{<font color="<PKIT_ERRORSTR>">};
my $errorspan_end_tag = $config->get_global_attr('errorspan_end_tag') || q{</font>};
my $default_errorstr = $config->get_global_attr('default_errorstr') || '#ff0000';
my $uri_prefix = $config->get_global_attr('uri_prefix') || '';
my $template_class = $config->get_global_attr('template_class')
|| 'HTML::Template';
my $view_class = $template_class =~ /^HTML::Template/ ? 'Apache2::PageKit::View' : 'Apache2::PageKit::View::TT2';
$self->{view} = $view_class->new(
root_dir => $pkit_root,
view_dir => "$pkit_root/View",
content_dir => "$pkit_root/Content",
cache_dir => $view_cache_dir,
default_lang => $default_lang,
default_input_charset => $default_input_charset,
default_output_charset => $default_output_charset,
reload => $reload,
html_clean_level => $html_clean_level,
input_param_object => $apr,
output_param_object => $self->{output_param_object},
can_edit => $can_edit,
relaxed_parser => $relaxed_parser,
errorspan_begin_tag => $errorspan_begin_tag,
errorspan_end_tag => $errorspan_end_tag,
default_errorstr => $default_errorstr,
template_class => $template_class,
uri_prefix => $uri_prefix,
# used only to set browser_cache = '..' maybe another
# way to set browser_cache is better to leave the View
# independent from pk
pkit_pk => $self,
);
return $self;
}
sub page_sub {
my $pk = shift;
my $page_id = shift || $pk->{page_id};
# change all the / to ::
$page_id =~ s!/!::!g;
my $perl_sub;
if($page_id =~ s/^pkit_edit:://){
$perl_sub = 'Apache2::PageKit::Edit::' . $page_id;
} else {
my $model_dispatch_prefix = $pk->{config}->get_global_attr('model_dispatch_prefix');
$perl_sub = $model_dispatch_prefix . '::' . $page_id;
}
return $perl_sub if defined &{$perl_sub};
my ($class_package) = $perl_sub =~ m/^(.*)::/;
return if exists $Apache2::PageKit::checked_classes{$class_package};
# with this funny require line, we can check also for files with expressions like
# Foo::Bar-Foo::Bar without a warining and this is more secure
eval "require $class_package" if ( $class_package =~ /^[\w\d:]+$/ );
$Apache2::PageKit::checked_classes{$class_package} = 1;
return undef unless (defined &{$perl_sub});
my $model_base_class = $pk->{config}->get_global_attr('model_base_class') || "MyPageKit::Common";
warn qq{For full preformance please add "use $class_package" in your $model_base_class or startup.pl script};
return $perl_sub;
}
sub page_code {
my $pk = shift;
my ( $common_page_id, $model_dispatch_prefix, $default_code_perl_sub );
( run in 2.376 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )