?
Solved

What do i change to make this cgi script.. recognize and prombt for "save as" upon clicking

Posted on 1997-07-24
9
Medium Priority
?
266 Views
Last Modified: 2013-12-25
This cgi script is running now on my server

as you see it takes a password and delivers a .ra file

except right now the file comes up as text in the browser

i need the exact changes that would need to be made

to make the .ra file  prompt the

user for a "save as" and and then allow for download

of the song??  

exact detailed changes please!!!

#!/usr/bin/perl
#
# WEB900.PL (Verifies web900 codes)
#
# - Original script and file locking                  1/24/96 Bert Love
#
# - Modified the location of the challenge, good and bad HTML files for
# - security reasons. The $oordir variable is the out-of-reach directory.
# -                                          5/21/96      S. Thomas
#

#
# File locations
#
#      oordir      - Set this to the directory just out of reach of the web server directory.
#            - Conventionally, this is the directory just above public_html:
# /usr2
# /usr2/shane                        (this is oordir)
# /usr2/shane/public_html            (this is the top of the HTTP dirs)
# /usr2/shane/public_html/cgi-bin

$oordir = '/usr/home/igor3000/webauth';
$webfile = "/$oordir/web.data";
$formfile = "/$oordir/webchall.html";
$goodfile = "$oordir/song.ra";
$badfile = "$oordir/bad.html";
$logfile = "$oordir/web.log";

#
# You shouldn't need to touch anything below this line!
#

require "ctime.pl";
chop($date = &ctime(time));
$WEBLEN =  8;             # fixed record length in webfile

# copy the values of these environment variables
$method = $ENV{"REQUEST_METHOD"};
$type = $ENV{"CONTENT_TYPE"};

# if the script was called with the GET method,
#  output the form
if($method eq "GET") {
      &send_file($formfile);
      exit;
}

# check for the POST method and the HTML form's MIME-type
if($method ne "POST" || $type ne "application/x-www-form-urlencoded") {
      &system_error("Web authorization code must come from a Form\n");
      exit;
}

# read form data from standard input
%input_values = &break_input;

# convert from the strange URL syntax to normal ascii
#  and translate non-UNIX line endings to UNIX convention
$number = &normalize_query($input_values{"number"});
$email = &normalize_query($input_values{"email"});

# verify number is seven digits
if(length($number) != 7) {
      &system_error("Sorry, but that number is not valid\n");
      exit;
}

# open Web file for read/write and lock
unless(open(WEB,"+<$webfile")) {
      &system_error("Could not open web data file.\n");
      exit;
}
#flock(WEB, $LOCK_EX);
seek(WEB, 0, 0);

# open log file as append and lock
unless(open(WLOG,">>$logfile")) {
#        flock(WEB, $LOCK_UN);
      &system_error("Could not open log file.\n");
      exit;
}
#flock(WLOG, $LOCK_EX);
#seek(WLOG, 0, 2);

$goodbad=$badfile;
SEARCH:
for ($recnum = 0; read(WEB, $goodnum, $WEBLEN); $recnum++) {
      $goodnum =~ tr/0-9//cd;
      if($number eq $goodnum) {            # got the number
            $goodbad=$goodfile;
            seek(WLOG, 0, 2);            # make sure we're at end
            print (WLOG $date, "  ", $number, "  ", $email, "\n");
            seek(WEB, -$WEBLEN, 1);
            $goodnum =~ tr/0-9/x/;
            print WEB $goodnum;
            last SEARCH;
      }
      seek(WEB, $recnum*$WEBLEN, 0);
}

# unlock and close files
#flock(WEB, $LOCK_UN);
#flock(WLOG, $LOCK_UN);
close(WEB);
close(WLOG);
chmod(0666, $logfile);

# send the good or bad news
&send_file($goodbad);
exit;

##
## SUBROUTINES
##

# read CONTENT_LENGTH bytes from the standard input and decode
# the URL format input, breaking it into an associative array
# of HTML variable names and their values.
sub break_input {
      local ($i);
      read(STDIN,$input,$ENV{'CONTENT_LENGTH'});
      @form_names = split('&',$input);
      foreach $i(@form_names) {
            ($html_name,$html_value) = split('=',$i);
            $input_values{$html_name} = $html_value;
      }
      return %input_values;
}

# handle system errors
sub system_error {
      local($errmsg) = @_;
      &print_header("System Error");
      print $errmsg;
      &print_footer;
}

# given a title, print the return header
sub print_header {
      local($title) = @_;
      print "Content-type: text/html\n\n";
      print "<HTML>\n";
      print "<HEAD>\n";
      print "<TITLE>$title</TITLE>\n";
      print "</HEAD>\n";
      print "<BODY>\n";
      print "<H1>$title</H1>\n";
}

# finish off HTML page
sub print_footer {
      print "</BODY>\n";
      print "</HTML>\n";
}

# URL syntax converts most non-alphanumeric characters into a
# percentage sign, followed by the character's value in hex.
sub normalize_query {
      local($value) = @_;
      $value =~ tr/+/ /;
      $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
      return $value;
}

# send specified HTML file
sub send_file {
      local($file) = @_;
        unless(open(FILE,$file)) {
            &system_error("Failed to open file $file\n");
            exit;
      }
      print "Content-type: text/html\n\n";
      while (<FILE>) {
            print;
      }
      close(FILE);
}
0
Comment
Question by:igor3000
  • 5
  • 3
9 Comments
 
LVL 2

Expert Comment

by:Voodoo
ID: 1829384
The content-type header you return for the .ra files should not be text/html. Make it this instead:

print "Content-type: audio/x-realaudio\n\n";

That is the right header for .ra files.

0
 

Author Comment

by:igor3000
ID: 1829385
i do not wish the program to load to "viewer"

i wish it to prompt a "save to hard drive"

this is not just for .ra files but for all audio files

and some of them  will be zipped

please give further detailed instructions


0
 
LVL 2

Expert Comment

by:Voodoo
ID: 1829386
Then change the return header to:

print "Content-type: application/octet-stream\n";
print "Content-Disposition: inline; filename=$filename\n\n";

$filename can be used to indicate what to save the file as on the users machine.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:igor3000
ID: 1829387
Below is the changes i made to the cgi

see what i did wrong . or suggest something more

because it's doing the same exact thing as before






#!/usr/bin/perl
#
# WEB900.PL (Verifies web900 codes)
#
# - Original script and file locking                  1/24/96 Bert Love
#
# - Modified the location of the challenge, good and bad HTML files for
# - security reasons. The $oordir variable is the out-of-reach directory.
# -                                          5/21/96      S. Thomas
#

#
# File locations
#
#      oordir      - Set this to the directory just out of reach of the web server directory.
#            - Conventionally, this is the directory just above public_html:
# /usr2
# /usr2/shane                        (this is oordir)
# /usr2/shane/public_html            (this is the top of the HTTP dirs)
# /usr2/shane/public_html/cgi-bin

$oordir = '/usr/home/igor3000/webauth5';
$webfile = "/$oordir/web.data";
$formfile = "/$oordir/webchall.html";
$goodfile = "$oordir/gimmecd.ra";
$badfile = "$oordir/bad.html";
$logfile = "$oordir/web.log";

#
# You shouldn't need to touch anything below this line!
#

require "ctime.pl";
chop($date = &ctime(time));
$WEBLEN =  8;             # fixed record length in webfile

# copy the values of these environment variables
$method = $ENV{"REQUEST_METHOD"};
$type = $ENV{"CONTENT_TYPE"};

# if the script was called with the GET method,
#  output the form
if($method eq "GET") {
      &send_file($formfile);
      exit;
}

# check for the POST method and the HTML form's MIME-type
if($method ne "POST" || $type ne "application/x-www-form-urlencoded") {
      &system_error("Web authorization code must come from a Form\n");
      exit;
}

# read form data from standard input
%input_values = &break_input;

# convert from the strange URL syntax to normal ascii
#  and translate non-UNIX line endings to UNIX convention
$number = &normalize_query($input_values{"number"});
$email = &normalize_query($input_values{"email"});

# verify number is seven digits
if(length($number) != 7) {
      &system_error("Sorry, but that number is not valid\n");
      exit;
}

# open Web file for read/write and lock
unless(open(WEB,"+<$webfile")) {
      &system_error("Could not open web data file.\n");
      exit;
}
#flock(WEB, $LOCK_EX);
seek(WEB, 0, 0);

# open log file as append and lock
unless(open(WLOG,">>$logfile")) {
#        flock(WEB, $LOCK_UN);
      &system_error("Could not open log file.\n");
      exit;
}
#flock(WLOG, $LOCK_EX);
#seek(WLOG, 0, 2);

$goodbad=$badfile;
SEARCH:
for ($recnum = 0; read(WEB, $goodnum, $WEBLEN); $recnum++) {
      $goodnum =~ tr/0-9//cd;
      if($number eq $goodnum) {            # got the number
            $goodbad=$goodfile;
            seek(WLOG, 0, 2);            # make sure we're at end
            print (WLOG $date, "  ", $number, "  ", $email, "\n");
            seek(WEB, -$WEBLEN, 1);
            $goodnum =~ tr/0-9/x/;
            print WEB $goodnum;
            last SEARCH;
      }
      seek(WEB, $recnum*$WEBLEN, 0);
}

# unlock and close files
#flock(WEB, $LOCK_UN);
#flock(WLOG, $LOCK_UN);
close(WEB);
close(WLOG);
chmod(0666, $logfile);

# send the good or bad news
&send_file($goodbad);
exit;

##
## SUBROUTINES
##

# read CONTENT_LENGTH bytes from the standard input and decode
# the URL format input, breaking it into an associative array
# of HTML variable names and their values.
sub break_input {
      local ($i);
      read(STDIN,$input,$ENV{'CONTENT_LENGTH'});
      @form_names = split('&',$input);
      foreach $i(@form_names) {
            ($html_name,$html_value) = split('=',$i);
            $input_values{$html_name} = $html_value;
      }
      return %input_values;
}

# handle system errors
sub system_error {
      local($errmsg) = @_;
      &print_header("System Error");
      print $errmsg;
      &print_footer;
}

# given a title, print the return header
sub print_header {
      local($title) = @_;
      print "Content-type: application/octet-stream\n";
      print "Content-Disposition: inline; filename=$filename\n\n";
      print "<HTML>\n";
      print "<HEAD>\n";
      print "<TITLE>$title</TITLE>\n";
      print "</HEAD>\n";
      print "<BODY>\n";
      print "<H1>$title</H1>\n";
}

# finish off HTML page
sub print_footer {
      print "</BODY>\n";
      print "</HTML>\n";
}

# URL syntax converts most non-alphanumeric characters into a
# percentage sign, followed by the character's value in hex.
sub normalize_query {
      local($value) = @_;
      $value =~ tr/+/ /;
      $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
      return $value;
}

# send specified HTML file
sub send_file {
      local($file) = @_;
        unless(open(FILE,$file)) {
            &system_error("Failed to open file $file\n");
            exit;
      }
      print "Content-type: text/html\n\n";
        while (<FILE>) {
            print;
      }
      close(FILE);
}

0
 
LVL 2

Expert Comment

by:Voodoo
ID: 1829388
You changed the wrong content type. Its the content type in &send_file that needs to be changed, not your HTML header content type.

Also, you can't just return a different content type header straight after another. I.e. you won't be able to return a HTML page, followed by a file.
0
 

Author Comment

by:igor3000
ID: 1829389
>>You changed the wrong content type. Its the content type in >>&send_file that needs to be changed,
                         not your HTML header content type.

--i don't see where your talking about to try the changes
--could you please reprint the cgi (or portion) to make
-- the change at..

   >                      Also, you can't just return a different >content type header straight after another. I.e. you won't be >able
                         to return a HTML page, followed by a >file.

--why is this?


0
 

Author Comment

by:igor3000
ID: 1829390
hello?

are you gonna answer my question?


0
 

Author Comment

by:igor3000
ID: 1829391
it's really important that i get this modyfiled

cgi to run.. could you copy the portion

of the script .. make the changes and then reprint them in

so i get a reference to what it looks like


0
 
LVL 1

Accepted Solution

by:
wormzer earned 100 total points
ID: 1829392
You changed the wrong one.  There's one at the very bottom of the script.  I would try also, dumping the second line (the disposition line..)  What that's trying to do is doubling the effort that you're going to here.  Try both... I could be wrong.


0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will focus on how to use WhizBase as a tool for sending ICQ messages to ICQ. Here I will use a new technology in WhizBase, published in WhizBase 5.1 version. In this tutorial I will use 3 files, pager.wbsp for the processing, e…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question