#!/usr/bin/perl -wT
########################################################################## # # line_end.cgi # # Text file line ending conversion cgi script. Allows for conversion # of dos/win text files to and from unix and similarly for mac. # # Presents two kinds of user interfaces. # 1) generic # This is presented by default if the justconvert parameter is # not specified. It presents a pull down menu of platforms for # conversion and generic instructions. # 2) specific # If the justconvert parameter is present: # e.g. http://mysite/cgi-bin/line_end.cgi?justconvert=unix2dos # then a form will presented without a pull down menu and with # specific instructions describing the particular conversion intended. # This optimizes for simplicity for the end user. # # Prerequisite: Text::CRLF # # Written by [EMAIL PROTECTED]: Ronald Schmidt # ########################################################################## use strict; use Text::CRLF; use Data::Dumper; use CGI qw/:standard/; use CGI::Carp 'fatalsToBrowser'; $CGI::POST_MAX=1024 * 1024; # max 1Meg posts ########################################################################## # If the OS/platform you need for conversion is not listed in the # platform drop down menu you can add it here. Platform names supported # by Text::CRLF will also be supported by this program. ########################################################################## my @platform_select = ( 'unix2dos', 'dos2unix', 'mac2dos', 'mac2unix', 'dos2mac', 'unix2mac', '2dos', '2unix', '2mac' ); my %platform_select_labels = ( $platform_select[0] => 'UNIX to Dos/Windows', $platform_select[1] => 'Dos/Windows to UNIX', $platform_select[2] => 'Macintosh to Dos/Windows', $platform_select[3] => 'Macintosh to Dos/Windows', $platform_select[4] => 'Dos/Windows to Macintosh', $platform_select[5] => 'UNIX to Macintosh', $platform_select[6] => 'UNIX or Mac to Dos/Windows', $platform_select[7] => 'Dos/Windows or Mac to UNIX', $platform_select[8] => 'Dos/Windows or UNIX to Mac' ); ########################################################################## # start of main logic ########################################################################## # convert and return the input file if (param('input_file')) { my ($platform) = (param('platform') =~ /^(\w*?2\w+)$/); # launder my $input_fh = param('input_file'); binmode($input_fh); # allow running under win/dos too bless $input_fh, 'IO::Handle'; # persuade CRLF its a file handle my $output_file = eval "Text::CRLF::$platform(undef, \$input_fh)"; my $output_fn = param('output_file'); print <<EOT; Content-Disposition: attachment; filename="$output_fn" Content-Type: application/octet-stream EOT print $output_file->[0]; } # the input was too big - some fuss with eval for backwards compatibility elsif (my $too_big = eval "cgi_error()") { print header(-status=> $too_big ), start_html, h1($too_big), end_html; } # present the form to allow input of files etc. else { my ($just_convert, $jc_from, $jc_to); my ($title, $small_header, $submit_text); if (param('justconvert') && (param('justconvert') =~ /(\w*?)2(\w+)/)) { $just_convert = 1; $jc_from = $1; $jc_to = $2; } # set up explanations/descriptions used on form based on specific # conversion requested or (! $just_convert) generic instructions. if ($just_convert) { if ($jc_from) { $title = "CGI $jc_from line ending to $jc_to " . 'line ending facility.'; $submit_text = "$jc_from line ending to $jc_to line ending"; } else { $title = "CGI conversion to $jc_to line ending " . 'facility.'; $submit_text = "Convert to $jc_to line ending"; } $small_header = 'Select a file on your machine.'; } else { $title = 'Line ending conversion program.'; $small_header = 'Select a platform conversion and file on '. 'your machine.'; $submit_text = 'Convert file'; } # helps set output file name in dialog in some end cases my $JSCRIPT=<<END; function set_download_file() { if (document.forms["line_end"].output_file.value.length > 0) { document.forms["line_end"].action += "/" + document.forms["line_end"].output_file.value; } else if (document.forms["line_end"].input_file.value.length > 0) { document.forms["line_end"].action += "/" + document.forms["line_end"].input_file.value; } } END print header, start_html( -title => $title, -script => $JSCRIPT, -BGCOLOR => 'white' ), h1($title), h2("$small_header<BR>\n" . 'Submit the form to convert the file.'), start_form( -method => 'POST', -enctype => &CGI::MULTIPART, -action => $ENV{'SCRIPT_NAME'}, -name => 'line_end', -onSubmit => 'set_download_file()' ); if ($just_convert) { print hidden(-name => 'platform', -value => lc(param('justconvert'))); } else { print p(), "Platforms "; print popup_menu( -name => 'platform', -values => \@platform_select, -labels => \%platform_select_labels ); } print p(), "Input File ", filefield(-name => 'input_file', -size => 30); print p(), 'Output File ', textfield(-name => 'output_file'); print br, br, submit(-value => $submit_text), hr; print end_form, end_html; }
