# mod_expires.pl # Defines editors for setting expires times sub mod_expires_directives { local($rv); $rv = [ [ 'ExpiresActive ExpiresByType ExpiresDefault', 1, 5, 'virtual directory htaccess' ] ]; return &make_directives($rv, $_[0], "mod_expires"); } sub edit_ExpiresActive_ExpiresByType_ExpiresDefault { local($rv, $max, $i); $rv = "Generate Expires headers?\n". &choice_input($_[0]->[0]->{'value'}, "ExpiresActive", "off", "Yes,on", "No,off")."
\n"; $rv .= "Default expiry time?\n"; $rv .= sprintf " None\n", $_[2]->[0] ? "" : "checked"; $rv .= sprintf "  \n", $_[2]->[0] ? "checked" : ""; $rv .= &expires_input("ExpiresDefault", $_[2]->[0]->{'value'})."
\n"; $rv .= "\n". "\n"; $max = @{$_[1]} + 1; for($i=0; $i<$max; $i++) { if ($_[1]->[$i]->{'value'} =~ /^(\S+)\s+(.*)$/) { $type = $1; $when = $2; } else { $type = $when = ""; } $rv .= "\n"; $rv .= "\n"; $rv .= "\n"; $rv .= "\n"; } $rv .= "
MIME type Expiry time
".&expires_input("Expires_when_$i", $when)."
\n"; return (2, "Expires headers", $rv); } sub save_Expires { local($i, $type, @rv); for($i=0; defined($type = $in{"Expires_type_$i"}); $i++) { if ($type !~ /\S/) { next; } $type =~ /^(\S+)\/(\S+)$/ || &error("'$type' is not a valid MIME type"); push(@rv, "$type ".&parse_expires("Expires_when_$i")); } return ( &parse_choice("ExpiresActive", "off"), \@rv, $in{'ExpiresDefault_def'} ? [ ] : &parse_expires("ExpiresDefault") ); } @Expires_units = (60, 60, 24, 30, 365); @Expires_words = ('second', 'minute', 'hour', 'day', 'week', 'month', 'year'); # expires_input(name, value) sub expires_input { local(@tm, @w, $i, $rv, $from, $secs); if ($_[1] =~ /^"(\S+)\s+(plus\s+)?(.*)"$/) { @w = split(/\s+/, $3); $from = ($1 =~ /access|now/ ? "A" : "M"); @tm = (0, 0, 0, 0, 0, 0, 0); for($i=0; $i<@w; $i+=2) { $w[$i+1] =~ s/s$//g; $tm[&indexof($w[$i+1], @Expires_words)] = $w[$i]; } } elsif ($_[1] =~ /^(A|M)(\d+)$/) { $from = $1; $secs = $2; for($i=0; $i<@Expires_units; $i++) { push(@tm, $secs % $Expires_units[$i]); $secs = int($secs / $Expires_units[$i]); } push(@tm, $secs); splice(@tm, 4, 1, 0); } else { $from = "A"; @tm = (0, 0, 0, 0, 0, 0, 0); } $rv = " time plus \n"; for($i=0; $i<7; $i++) { $rv .= "". substr($Expires_words[$i],0,1)."\n"; } return $rv; } # parse_expires(name) sub parse_expires { local($rv, $i, $v); $rv = $in{"$_[0]_from"} eq "A" ? "access" : "modification"; $rv .= " plus"; for($i=0; $i<7; $i++) { $v = $in{"$_[0]_$i"}; if ($v !~ /^\d*$/) { &error("'$v' is not a valid number of ". "$Expires_words[$i]s"); } if ($v) { $rv .= " $v $Expires_words[$i]s"; } } if ($rv =~ /plus$/) { $rv .= " 0 seconds"; } return "\"$rv\""; } 1;