|
@@ -9,10 +9,17 @@ sub get_args {
|
|
|
return $args_hr;
|
|
return $args_hr;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+our $CP_SECURITY_TOKEN = $ENV{'cp_security_token'} || 'cpsess0000000000';
|
|
|
|
|
+our $CP_USER = $ENV{'REMOTE_USER'} || 'nobody';
|
|
|
|
|
|
|
|
# XXX TODO TODO TODO Need to make API calls return 304 unmodified if the data is
|
|
# XXX TODO TODO TODO Need to make API calls return 304 unmodified if the data is
|
|
|
# unchanged! hehehe
|
|
# unchanged! hehehe
|
|
|
|
|
|
|
|
|
|
+# TODO delete cached templates via cron or hook or something, as the cache files
|
|
|
|
|
+# will otherwise begin to pile up.
|
|
|
|
|
+# Suggest using Cpanel::Session::is_active_security_token_for_user($user,$token)
|
|
|
|
|
+# in a loop and trashing inactive templates.
|
|
|
|
|
+
|
|
|
# Yay. I now have ~250ms pageloads instead of 350+ms pageloads.
|
|
# Yay. I now have ~250ms pageloads instead of 350+ms pageloads.
|
|
|
# XXX Need to check when the chrome updates and pop cache then too?
|
|
# XXX Need to check when the chrome updates and pop cache then too?
|
|
|
# Maybe upcp hook instead?
|
|
# Maybe upcp hook instead?
|
|
@@ -21,15 +28,15 @@ sub render_cached_or_process_template {
|
|
|
if( $input_hr->{'troglodyne_do_static_render'} ) {
|
|
if( $input_hr->{'troglodyne_do_static_render'} ) {
|
|
|
|
|
|
|
|
# Try to print from cache, as Cpanel::Template is slow AF
|
|
# Try to print from cache, as Cpanel::Template is slow AF
|
|
|
- my ( $cached, $cache_file ) = cached( $service, $input_hr->{'template_file'} );
|
|
|
|
|
- return if( $cached && render_from_cache( $cache_file ) );
|
|
|
|
|
|
|
+ my ( $cached, $cache_dir ) = cached( $service, $input_hr->{'template_file'} );
|
|
|
|
|
+ return if( $cached && render_from_cache($cache_dir) );
|
|
|
|
|
|
|
|
# OK, so no cache. Let's fix that.
|
|
# OK, so no cache. Let's fix that.
|
|
|
$input_hr->{'print'} = 0;
|
|
$input_hr->{'print'} = 0;
|
|
|
require Cpanel::Template;
|
|
require Cpanel::Template;
|
|
|
my ( $success, $output_sr ) = Cpanel::Template::process_template( $service, $input_hr );
|
|
my ( $success, $output_sr ) = Cpanel::Template::process_template( $service, $input_hr );
|
|
|
if( $success ) {
|
|
if( $success ) {
|
|
|
- return if render_to_cache_and_print( $cache_file, $output_sr );
|
|
|
|
|
|
|
+ return if render_to_cache_and_print( $cache_dir, $output_sr );
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -40,10 +47,12 @@ sub render_cached_or_process_template {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
sub render_to_cache_and_print {
|
|
sub render_to_cache_and_print {
|
|
|
- my ( $cache_file, $content_sr ) = @_;
|
|
|
|
|
|
|
+ my ( $cache_dir, $content_sr ) = @_;
|
|
|
local $@;
|
|
local $@;
|
|
|
|
|
+ require Cpanel::Mkdir;
|
|
|
|
|
+ Cpanel::Mkdir::ensure_directory_existence_and_mode($cache_dir, 0711);
|
|
|
my $worked = eval {
|
|
my $worked = eval {
|
|
|
- open( my $fh, '>', $cache_file ) or die "Couldn't open cache file \"$cache_file\" for writing: $!";
|
|
|
|
|
|
|
+ open( my $fh, '>', "$cache_dir/$CP_SECURITY_TOKEN" ) or die "Couldn't open cache file \"$cache_dir/$CP_SECURITY_TOKEN\" for writing: $!";
|
|
|
print $fh $$content_sr;
|
|
print $fh $$content_sr;
|
|
|
print STDOUT $$content_sr;
|
|
print STDOUT $$content_sr;
|
|
|
};
|
|
};
|
|
@@ -57,10 +66,10 @@ sub render_to_cache_and_print {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
sub render_from_cache {
|
|
sub render_from_cache {
|
|
|
- my ( $cache_file ) = @_;
|
|
|
|
|
|
|
+ my ( $cache_dir ) = @_;
|
|
|
local $@;
|
|
local $@;
|
|
|
my $worked = eval {
|
|
my $worked = eval {
|
|
|
- open( my $fh, '<', $cache_file ) or die "Couldn't open cache file \"$cache_file\" for reading: $!";
|
|
|
|
|
|
|
+ open( my $fh, '<', "$cache_dir/$CP_SECURITY_TOKEN" ) or die "Couldn't open cache file \"$cache_dir/$CP_SECURITY_TOKEN\" for reading: $!";
|
|
|
while( <$fh> ) { print $_; }
|
|
while( <$fh> ) { print $_; }
|
|
|
};
|
|
};
|
|
|
if(my $err = $@) {
|
|
if(my $err = $@) {
|
|
@@ -72,6 +81,7 @@ sub render_from_cache {
|
|
|
return $worked;
|
|
return $worked;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+# These MUST be indexed by cp_security_token... sadly
|
|
|
our $ULC = '/usr/local/cpanel';
|
|
our $ULC = '/usr/local/cpanel';
|
|
|
our %TMPL_DIRS_BY_SVC = (
|
|
our %TMPL_DIRS_BY_SVC = (
|
|
|
'whostmgr' => 'whostmgr/docroot/templates',
|
|
'whostmgr' => 'whostmgr/docroot/templates',
|
|
@@ -81,11 +91,12 @@ our %TMPL_DIRS_BY_SVC = (
|
|
|
sub cached {
|
|
sub cached {
|
|
|
my ( $service, $tmpl_file ) = @_;
|
|
my ( $service, $tmpl_file ) = @_;
|
|
|
my $tmpl_path = "$ULC/$TMPL_DIRS_BY_SVC{$service}/$tmpl_file";
|
|
my $tmpl_path = "$ULC/$TMPL_DIRS_BY_SVC{$service}/$tmpl_file";
|
|
|
- my $cache_path = "$tmpl_path.cache";
|
|
|
|
|
|
|
+ my $cache_dir = "${tmpl_path}_caches/$CP_USER";
|
|
|
|
|
+ my $cache_path = "$cache_dir/$CP_SECURITY_TOKEN";
|
|
|
|
|
|
|
|
# If cache mtime is older than template mtime, we are fine to use the cache.
|
|
# If cache mtime is older than template mtime, we are fine to use the cache.
|
|
|
my $cached = ( -s $cache_path && ( (stat(_))[9] > (stat($tmpl_path))[9] ) );
|
|
my $cached = ( -s $cache_path && ( (stat(_))[9] > (stat($tmpl_path))[9] ) );
|
|
|
- return ( $cached, $cache_path );
|
|
|
|
|
|
|
+ return ( $cached, $cache_dir );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
1;
|
|
1;
|