#!perl BEGIN { chdir('..'); } use strict; use warnings; use IO::File; use CGI ':standard'; use CGI::Carp 'fatalsToBrowser'; use File::Copy; use Date; require "lib_pl"; our ($program, $submit_i, $file_i, $password_i, @month); my $conf = read_data("$program.conf"); my $password = $conf->{password}; my $target_dir = $conf->{directory}; my $file_pattern = $conf->{file_pattern}; my $update_index = $conf->{update_index}; my $index_file = $conf->{index_file}; hdr("Upload Newsletter"); if (param()) { submitted(); } else { display_form(); } ftr(); sub display_form { print tbl_tsv(<{file_pattern_eg}"); $failed = 1; } } if ($failed) { msg("", "Press and try again."); } else { my ($day, $month, $year) = $filename =~ /^$file_pattern$/; # they better not change the pattern! my $month_dir = ['', 'JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC']->[$month]; $month_dir .= $year; chdir $target_dir; mkdir $month_dir; chdir $month_dir; my $file = slurp(param("file")); belch($filename, $file); msg("file uploaded: $filename"); if ($update_index) { update_index($filename, $month_dir); } msg("", "Please check that the upload worked okay."); msg("You may need to use shift-reload to see updated files."); } } sub update_index { my ($filename, $month_dir) = @_; my $html = slurp($index_file); copy($index_file, "$index_file~") || die "can't make backup file $index_file~"; my $filename_esc = $filename; # $filename_esc =~ s/ /%20/g; $filename =~ /(\d+)-(\d+)-(\d+)/ or die "filename does not contain a date"; my $date = Date->new_ymd(2000+$3, $2, $1); my $month_day_year = $date->month_day . " " . $date->{y}; my $entry = qq{$month_day_year
}; if (index($html, $entry) == -1) { # move old "this weeks" to "back issues" # It takes some care with the spacing. $html =~ s,(\s*)(.*?)(\s*),$1$entry$3, or die "can't add new this weeks entry"; my $old_entry = $2; $html =~ s,(\s*)(),$1$old_entry$1$2, or die "can't relocate old entry to back issues"; belch($index_file, $html); msg("index updated"); } else { msg("this entry is already in the index"); } }