Link to home
Start Free TrialLog in
Avatar of algabatz
algabatz

asked on

Cut'n'paste guestbook w. umlauts & linebreaks

  I need a guesbook script that uses textareas, each that can be preformatted to give an output as
   follows:
   First field: Headline in size 5 and bold
Second field: Introduction size 3 and bold  
Third field: Text, size 3
   Fourth field: Byline, size 2 and bold.
   Another area should allow the submission of an URL, subsequently presenting an image (Could be
   omitted.)
   The script should write over the previous file, creating a new file with the same name, thus replacing
   the old one everytime one uses the script.
   There should be a password protected submission area only. No public access, except for the output.
   The tricky part, i suppose, is to allow the textareas to maintain the line breaks and umlauts from the
   cut'n'pasted text, presenting them in a sort of "edited" form in the guestbook.
   
  I've posted a form and an output document to show you what i need. Please take a look at:

      http://www.gp.se/nyheter/varldens_gang/kaseriform.html 

      and

      http://www.gp.se/nyheter/varldens_gang/output.html 

      That should give you an idea of what i want.
   
   "Datum" is the date, found at the top of the output page. "Rubrik" is the headline, "Ingress" is
      the introduction (bold), "Bild" means image, with the options "Ja/Nej" : Yes/No
      "Brödtext" is the text, "Byline" is of course the byline, "Publisera" means publish, ie Submit, and
      finally "Radera" means erase the form.
   
   I have looked everywhere for a script like this, and have also been told that it can't be done. I thought
   i'd give it a shot here. X-perts here have helped me before
   Algabatz
Avatar of mouatts
mouatts

It can be done.

The only things that need to be remembered is that a newline is appended to each line of the textarea as it is returned and that to retain the formatting these should be retained. However if the client machine is an MS-DOS based one the newline will consist of two characters namely a CR and a LF. The CR (ASCII 13) should be removed from the output as subsequent display wil cause an extra blank line to appear.

As far as the umlauts are concerned the environment variable HTTP_ACCEPT_CHARSET holds the character set code and this should be checked against the expected one to ensure that any required conversion of non-standard characters are converted accordingly.

Steve
Avatar of algabatz

ASKER

OK, sorry to reject your answer, Mouatts, but i'll be needing a complete runnable script. We're using a Netscape Entreprise server. I'm adding another 100, total 400 points, for a complete script. I realize that it might be hard to get it right, please advise if 400 is too cheap for a script like this.
Algabatz
Sorry not for a commercial site I'd be doing myself out of a living.

Steve
I see... but since i'm in Sweden you'd hardly be losing any money anyway. I'm adding more points. Please either reconsider or make me an e-mail offer. I'm a bit desperate... ;-)
Best,
algabatz@letterbox.com
Avatar of Michel Plungjan
Is the following at all interesting? You will need to save the result on the server yourself, but that is easy (in Netscape at least) if the server has FTP access and allows PUT, then save it on harddisk and drag and drop it on the directory you are ftp'ing to in the browser.

The image can be put in where you want it just by typing the image tag into the textarea. If you wish, the image can be put into the output (but where?) if the button is selected

Be careful when cutting and pasting. All lines starting with Text += are on one line until the semi-colon ;

<HTML>
<HEAD>
<META http-equiv="content-type" content="text/html;charset=iso-8859-1">
<TITLE>Post en artikel</TITLE>
<SCRIPT LANGUAGE="JavaScript"><!-- // cloak
/* Post an article will format the form contents and write a piece of HTML
   suitable for saveing into a new window.
   (c) 1998 Michel Plungjan michel@irt.org
*/
   
function reWrap(Text) {
   crlfType = null
   if (Text.indexOf('\r\n')    !=-1) crlfType = '\r\n';
   else if (Text.indexOf('\r') !=-1) crlfType = '\r';
   else if (Text.indexOf('\n') !=-1) crlfType = '\n';
   if (!crlfType) return Text;
   while (Text.indexOf(crlfType) !=-1) {
      Text = Text.substring(0,Text.indexOf(crlfType)) + '<BR>' + Text.substring(Text.indexOf(crlfType)+crlfType.length);
   }
   return Text;
}
function crtHtml(theForm) {
   theFONT = 'Arial,Helvetica,Geneva,Swiss,SunSans-Regular';
   Text = '<HT' + 'ML>';
   Text+= '\n<BO'+'DY BGCOLOR="#FFFFFF">';
   Text+= '\n<TA' + 'BLE BORDER="0" CELLPADDING="0" CELLSPACING="2" FRAME WIDTH="387">';
   Text+= '\n   <TR>';
   Text+= '\n      <TD><IMG HEIGHT="86" WIDTH="351" SRC="VG_LOGGA.GIF"></TD>';
   Text+= '\n   </TR>';
   Text+= '\n   <TR>';
   Text+= '\n      <TD><!-- Date --><FONT FACE="' + theFONT + '" SIZE="2">';
   Text+= theForm.date.value;
   Text+= '\n         </FONT>';
   Text+= '\n         <HR NOSHADE WIDTH="100%" ALIGN="LEFT">';
   Text+= '\n         <P><!-- Headline --><FONT FACE="' + theFONT + '" SIZE=5><B>';
   Text+= theForm.headline.value;
   Text+= '\n         </B></FONT>';
   Text+= '\n         <P><!-- Ingress --><FONT SIZE=3><B>';
   Text+= reWrap(theForm.ingress.value);
   Text+= '\n         </B>';
   Text+= reWrap(theForm.breadtext.value);
   Text+= '\n         </FONT>';      
   Text+= '\n         <P><!-- Byline --><FONT SIZE=2><B>';      
   Text+= theForm.byline.value;
   Text+= '\n         </B>';
   Text+= '\n         </FONT>';
   Text+= '\n      </TD>';        
   Text+= '\n   </TR>';              
   Text+= '\n</TABLE>';                      
   Text+= '\n</BO'+'DY>';                            
   Text+= '\n</HT'+'ML>';                                    
   WinId=window.open('','newwin');
   WinId.document.open();
   WinId.document.write(Text);      
   WinId.document.close();      
   WinId.focus();
}
</SCRIPT>
</HEAD>

<BODY>
    <FORM name="FormName" onSubmit="top.crtHtml(this);return false;">
    <FONT FACE="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"><B>Datum:</B></FONT><BR>
    <INPUT type="text" name="date" size="24">
    <P><FONT FACE="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"><B>Rubrik:<BR>
    </B></FONT><INPUT type="text" name="headline" size="82"><BR>
    <FONT FACE="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"><B>Ingress:<BR>
    </B></FONT><TEXTAREA name="ingress" cols="79" rows="4" wrap="physical"></TEXTAREA><BR>
    <FONT FACE="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"><B>Bild :<BR>
    <!--The image always has the URL "http://www.GP.se/nyheter/varldens_gang/bild.jpg"-->
    Ja</B></FONT><INPUT type="radio" value="yes" name="default">
    <FONT FACE="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"><B>Nej</B></FONT><INPUT type="radio" value="no" name="default" checked></P>
    <P><FONT FACE="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"><B>Br&ouml;dtext:<BR>
    </B></FONT><TEXTAREA name="breadtext" cols="80" rows="40" wrap="physical"></TEXTAREA></P>
    <DIV align="left">
      <P><FONT FACE="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"><B>Byline:<BR>
      </B></FONT><INPUT type="text" name="byline" size="35"></P>
    </DIV>
    <P><INPUT type="submit" name="Publicera" value="Publicera"><INPUT type="reset" value="Radera"> </P>
    <P><TABLE border="0" cellpadding="0" cellspacing="2" frame width="200" height="160">
      <TR>
        <TD></TD>
      </TR>
    </TABLE>
    </FORM>
  </BODY>

</HTML>

Sorry, we're running a bad-a** firewall, no ftp:ing allowed. But i'll check out your code and see if i can get any ideas,
/Al
At least one could post the html created to a REAL simple cgi that just wrote the html to disk...

Michel
Well that script is really fantastic, a true piece of art. So unless anyone else has a full cgi to offer, i am going  to give the points to Michel, but i'd still be needing that REAL simple cgi...
/Al
Tackar, Tackar...

Let's see if I cannot find some perl for you.

Change the above SCript lines from

   WinId=window.open('','newwin');
   WinId.document.open();
   WinId.document.write(Text);        
   WinId.document.close();        
   WinId.focus();
to
document.hiddenform.theHtml.value = Text;
document.hiddenform.theHtml.submit();

and add the following form to the page:

<FORM NAME="hiddenform" ACTION="cgi-bin/formparser.pl">
<INPUT TYPE="HIDDEN" NAME="theHtml">
</FORM>

Here is a totally untested script I whipped up by merging two script from my friend Jason.
If it doesn't work, I will ask Jason to fix it (I am not a perl person myself)
Call it formparser.pl

#!/usr/local/bin/perl -w
 ############################
 ##  Form Parser script    ##
 ##   Jason Nugent, 1998   ##
 ############################

 my $method = $ENV{'REQUEST_METHOD'};

 if ($method eq "GET") {
     $text = $ENV{'QUERY_STRING'};
 }

 else { # default to POST
     read(STDIN, $text, $ENV{'CONTENT_LENGTH'});
 }                                                        

 my @value_pairs = split (/&/,$text);
 my %form_results = ();

 foreach $pair (@value_pairs) {                
     ($key, $value) = split (/=/,$pair);
     $value =~ tr/+/ /;
     $value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;
     $form_results{$key} = $value;  # store the key in the results hash
 }

 # next line sets the MIME type for the output

 print "Content-type: text/html\n\n";              


 open (FILE, ">/home/wwwroot/myfile.html") or die "cannot open file: $!";
 flock(FILE, 2) or die "cannot lock file: $!";

 # loop through the results and save the html in a file
 foreach $key (sort keys(%form_results)) {
     print "$key has value $form_results{$key}<BR>\n";
     if ($key eq "theHtml") {
       print FILE,$form_results{$key}; # I hope
 }
 close FILE;  # also unlocks the file
take a look at http://techware.prometeus.nsc.ru/test

If you find it usefull drop me note at andy@prometeus.nsc.ru
That looks like a complete solution...

Algabatz, If you wish, you may ask customer services to award you points to split between experts. Just ask busy Linda.

Michel


I will try to split the points, since i probably will try to use both solutions in different ways. How can i contact busy Linda?
/Algabatz
-- here is my scpript, checket by algabatz--- tested with Win, Netware---
#!/usr/local/bin/perl
require "cgi-lib2.pl";
######################################################################


# Configuration

# The full path to your directory containing the script (not an url)
$path= "data:/inw_web/shared/docs/lcgi/perl5/test";

#$path="e:/FOLKWEB/cgi-bin";

# Funny password
$password="password";

# The name of the text file where message stored
$page= "/data.txt";

# The full URL to the script
#This don't work??
#$scripturl=&MyURL;
$scripturl="http://techware.prometeus.nsc.ru/perl/test/Pb.pl";

# header of each page
#$header= <<EOT;
#      <div align=center>
#      <img height="86" width="351" src="/vg_logga.gif" ALT="Vg_Logga">
#      </div>
#EOT

# footer of each page
$footer = <<EOT;
 <div align=center><small>script by <a href=\"http://techware.prometeus.nsc.ru\">aioudine<\/a><\/small></div>
EOT

# succeful post
$good = <<EOT;
      <h2 align=center>Thank you for adding to our Profile<\/h2>
      <h3 align=center><a href=$scripturl>You may view your entry here<\/a><\/h3>
EOT

$script_name="ProfileBook";


# Colors placed in BODY tag
$bgcolor= "white";            # Background Color
$textcolor= "black";            # Text Color
$linkcolor= "blue";            # Link Color
$vlinkcolor= "darkblue";      # Visited Link Color
$alinkcolor= "red";            # Active Link Color

# Set this to a background image, or leave blank if you prefer

$bgimage= "";            # Full URL to your background image.

# End Configuration DO NOT MODIFY BELOW THIS LINE!
######################################################################



#######################
# Main
&ReadParse(*input);
{
  if ($input{'Cmd'} eq "ViewOnly"){
    &ViewProfile;
  }
 elsif ($input{'Cmd'} ne "")
  {
    &ProcessForm;
  }
 else
  {
    &ViewProfile;
  }
}



########################
# sub ViewProfile
sub ViewProfile{
      print &PrintHeader;

      print &Top ($script_name);
#      print $header;

print <<EOT;
      <table border="0" cellpadding="0" cellspacing="2" frame width="387">
      <tr>
            <td><img height="86" width="351" src="/vg_logga.gif"></td>
      </tr>
EOT

      open (DB, "$path$page") || print "<br><h1><font COLOR=red>ERROR:Can't open message file ".$path.$page."<\/h1><\/font>";
      @Data = <DB>;
      close DB;

      foreach $Match (@Data) {
            @TempData = split (/&&/, $Match);

#Date
print '      <TR><td><font face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular" size="2">'.$TempData[0];
print '      <hr noshade width="100%" align="left">'."</font><BR></td></TR>\n";

# HeadLine
print '<TR><td><h1><FONT size="6" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">'.$TempData[1]."</FONT></h1></td></TR>\n";


# Subheader
print '<TR><td><FONT face="Arial, Helvetica, sans-serif" size="3"><B>'.$TempData[2] ."</B>\n";

# text1
            $text=$TempData[3];
            @textarray=split(/ /,$text);
##            $dd=10;
$dd=(@textarray-@textarray%2)/2;


$i=0;
$text1=$textarray[$i];


while ($i < $dd) {
   ++$i;
   $text1=$text1." ".@textarray[$i];
   };

print "$text1\n";

              
       
# Image
            if ($TempData[5] eq "Yes"){
                    $image = $TempData[4];
             $image =~ s/\&lt\;/\</g;
             $image =~ s/\&gt\;/\>/g;
             print "$image\n";
            }
# text2

#$text2=$textarray[$i];

while ($i <= @textarray) {
   ++$i;
   $text2=$text2." ".@textarray[$i];
   };
      

            print "$text2<BR></FONT><BR><BR></td></TR>\n";
              }

# Byline
      print '<tr><Td><hr noshade width="100%"  align="left">';
      print '      <font face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular" size="5"><tt><b>'.$TempData[7]."</b></tt></font>\n";
      print '      <p><font face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular" size="2"><A HREF="mailto:'.$TempData[6].'">'.$TempData[6].'</a></font>';
      print "</td></TR></TABLE><P>\n<BR><BR><BR><hr>\n";


if ($input{'Cmd'} ne "ViewOnly")
{            print <<EOT;

            Would you like to add somthing?<br>
            <p>
            <FORM ACTION=\"$scripturl\" METHOD=\"POST\">
            Date (lates changes are  effective):<br>
            <input type=text name="date" size=20><br>

            The Title of Your Profile(lates changes are  effective):<br>
            <input type=text name="title" size=60><br>

            Subtitle:<br>
            <input type=text name="subtitle" size=60><br>
            
                Youre Story:<br>
                   <TEXTAREA NAME="Profile" ROWS=10 COLS=58 wrap=virtual>
            <\/TEXTAREA><br>
            Image parameters:<br>
            <TEXTAREA NAME="image" ROWS=2 COLS=58 wrap=virtual><IMG src="/vinci.jpg" align="left"><\/TEXTAREA><br>

            Add Image?:<br>
            <input name="yesno" type="radio" value="Yes">Yes<br>
            <input name="yesno" type="radio" value="No" checked>No<BR>

            Byline(lates changes are  effective):<BR>
            <input type=text name="byline" size=60><br><BR>
            Youre email(lates changes are  effective):<br>
            <input type=text name="email" size=60><br>

<BR>
            <input name="Cmd" type="radio" value="AddTop" checked>Add to top
            <input name="Cmd" type="radio" value="AddBootom">Add to bootom
            <input name="Cmd" type="radio" value="Over">Overwrite<br>
            Password:<br>
            <input type=password name="pasw" size=10><br>
            <input type=hidden name="user"value="test">
            <INPUT TYPE=submit VALUE=Submit><INPUT TYPE=Reset VALUE=Reset>
            <\/FORM>
            <br>

EOT
};                  
            print $footer;
            print &HtmlBot;

}

########################
# sub ProcessForm

sub ProcessForm{


        $Cmd = $input{'Cmd'};
        $email = $input{'email'};
      $date= $input{'date'};

      $title= $input{'title'};
      $title =~ s/\</\&lt\;/g;
      $title =~ s/\>/\&gt\;/g;


      $subtitle= $input{'subtitle'};
      $pass= $input{'pasw'};

      $Profile= $input{'Profile'};
      $Profile =~ s/\</\&lt\;/g;
      $Profile =~ s/\>/\&gt\;/g;
      $Profile =~ s/\n/<br>/g;
#      $Profile =~ s/\015/<br>/g;

##      $Profile2= $input{'Profile2'};
##      $Profile2 =~ s/\</\&lt\;/g;
##      $Profile2 =~ s/\>/\&gt\;/g;
##      $Profile2 =~ s/\n/<br>/g;
#      $Profile2 =~ s/\015/<br>/g;


      $image= $input{'image'};
      $image =~ s/\</\&lt\;/g;
      $image =~ s/\>/\&gt\;/g;
      $image =~ s/\n//g;
      $Profile =~ s/\015//g;


      $yesno= $input{'yesno'};

      $byline= $input{'byline'};
      $byline= $input{'byline'};
      $byline =~ s/\</\&lt\;/g;
      $byline =~ s/\>/\&gt\;/g;



       if ($Profile eq ""){
            print &PrintHeader;
            print &Top('Error -- No Profile');
            print <<EOT;
            <h2 align=center>You need to enter your Profile!<\/h2><br>
            <center>Please press your browser's back button and try again</center>
EOT
            print $footer;
            print &HtmlBot;
            }

        elsif ($pass ne $password){
            print &PrintHeader;
            print &Top('Error -- Wrong passwor use "password" $pass $password');
            print <<EOT;
            <h2 align=center>Wrong passwor use "password"!<\/h2><br>
            <center>Please press your browser's back button and try again</center>
EOT
            print $footer;
            print &HtmlBot;
            }

        elsif ($title eq""){
            print &PrintHeader;
            print &Top('Error -- No Title');
            print <<EOT;
            <h2 align=center>You need to enter your Title!<\/h2><br>
            <center>Please press your browser's back button and try again</center>
EOT

            print $footer;
            print &HtmlBot;
            }

        else{
            &AddProfile;
            }      

}


#######################
# sub AddProfile

sub AddProfile{

      open(DATA,"$path$page") || print "ERROR: Can't read".$path.$page;
      @oldentries = <DATA>;
      close(DATA);


      open(DATA,">$path$page") || print "ERROR: Can't write".$path.$page;
#      flock(DATA,2);

      $P =  $date."&&".$title."&&".$subtitle."&&".$Profile."&&".$image."&&".$yesno."&&".$email."&&".$byline."&&";
      $P =~ s/\n/<br>/g;
      $P =~ s/\015//g;
   


  if ($Cmd eq "Over"){
          print DATA $P."\n";
   }
   elsif ($Cmd eq "AddTop"){
      print DATA $P."\n";
      print DATA @oldentries;
       }
    else{
        print DATA @oldentries;
      print DATA $P."\n";
      };

      close(DATA);      
#      }

      print &PrintHeader;
      print &Top ('Thank you');
      print $good;
      print $footer;
      print &HtmlBot

}
########################
# sub Top

sub Top{

  local ($title) = @_;

  return <<EOT;
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>$title</title>
</head>
<body background=\"$bgimage\" bgcolor=\"$bgcolor\" text=\"$textcolor\" link=\"$linkcolor\" vlink=\"$vlinkcolor\" alink=\"$alinkcolor\">
EOT
}
---- end ----
Customer Services are at https://www.experts-exchange.com/topics/experts-exchange/

(Experts-exchange link from Home page)

Ask for help with splitting points on question 10087119
You have the following options:

1. Accept my answer and post a question titled "For aioudine" when you get the points from linda

2. reject my answer and ask aioudine to post his as an answer and then later post a question for me (if you still want to reward my efforts (jag hoppas!))

Michel
There should be no comma in the print btw:

print FILE $form_results{$key};
I am going to post questions in this topic area to divide the points between aioudine and mplungian.  Reject the answer of mplungjan so he does not get award twice.

Since it looks like this question should be saved in the previously answer questions(PAQ), I will post an answer so it can be saved in the PAQ.

Linda
Points have been split. This question is now dead.
/Algabatz
ASKER CERTIFIED SOLUTION
Avatar of linda101698
linda101698

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial