📄 addpost_new.pl
字号:
#!/usr/bin/perl
# addPost.pl
# Adds a posting to a forum
use warnings;
use strict;
use CGI qw( :standard );
use XML::DOM;
use Fcntl qw( :flock );
$| = 1;
my $prefix = "forum.pl?file=";
if ( $ENV{ "HTTP_USER_AGENT" } =~ /MSIE/i ) {
$prefix = "../XML/";
}
else {
$prefix = "forum.pl?file=";
}
if ( param( "submit" ) ) {
my ( $parser, $document, $forum, $message, $element );
my $file = param( "file" );
$file =~ /^\w+\.xml$/ or die "Not a valid file: $!";
open FORUM, "+< ../htdocs/XML/$file" or die "Could not open: $!";
flock FORUM, LOCK_EX;
$parser = new XML::DOM::Parser;
$document = $parser->parse( \*FORUM );
$forum = $document->getDocumentElement;
$message = $document->createElement( "message" );
$message->setAttribute( "timestamp", scalar( localtime ) );
$forum->appendChild( $message );
foreach ( qw( user title text ) ) {
$element = $document->createElement( $_ );
$element->addText( param( $_ ) );
$message->appendChild( $element );
}
seek FORUM, 0, 0;
truncate FORUM, 0;
$document->printToFileHandle( \*FORUM );
close FORUM;
print redirect ( "$prefix$file" );
}
elsif ( param ) {
my $file = param( "file" );
print header, start_html( -title => "Add a posting",
-style => { -src => "../XML/site.css" } );
print start_form( -action => "addPost.pl?file=$file" ),
"User", br,
textfield( -name => "user", -size => 40 ), br,
"Message Title", br,
textfield( -name => "title", -size => 40 ), br,
"Message Text", br,
textarea( -name => "text", -cols => 40, -rows => 5 ), br,
hidden( -name => "file", -value => $file ),
submit( -name => "submit", -value => "Submit" ),
reset, end_form,
a( { -href => "$prefix$file" }, "Return to Forum" ),
end_html;
}
else {
print redirect( "error.html" );
}
###########################################################################
# (C) Copyright 2001 by Deitel & Associates, Inc. and Prentice Hall. #
# All Rights Reserved. #
# #
# DISCLAIMER: The authors and publisher of this book have used their #
# best efforts in preparing the book. These efforts include the #
# development, research, and testing of the theories and programs #
# to determine their effectiveness. The authors and publisher make #
# no warranty of any kind, expressed or implied, with regard to these #
# programs or to the documentation contained in these books. The authors #
# and publisher shall not be liable in any event for incidental or #
# consequential damages in connection with, or arising out of, the #
# furnishing, performance, or use of these programs. #
###########################################################################
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -