🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.lang.perl
Article #689

Re: Problem z CGI...

#689
From: Piotr Klaban
Date: Fri, 07 Nov 1997 09:54
86 lines
2930 bytes
Jerzy Orzeszek <Jerzy.Orzeszek@univcomp.waw.pl> wrote:
> A teraz sedno sprawy:
> Jak napisać cgi którym będę mógł przeszukać plik dane.html ?
> Chodzi o przeszukiwanie wg wpisanej nazwy w formularzu tak aby skrypt
> otwierał sobie plik i szukał odpowiedniego wpisu, jak znajdzie 
> nich cofnie się o cztery znaki - linia zaczyna się od:
> <h3>Nazwa</h3>
> i wczytuje powiedzmy pięć linii w dół. A wynik wyświetla na ekranie
> w postaci strony html.

> Czy ktoś mógłby mi pomóc???
#!/usr/bin/perl
# skrypt CGI w perlu szukajaca tekstu <h3>TEXT</h3> na stronie data.html
# wyswietla kawalek strony data.html z TEXT'em od <h3>TEXT</h3>
# do linii z <hr> na poczatku
# Piotr Klaban <makler@man.torun.pl>

$head = <<'END'; # head of the HTML document
<html>
<head>
<title>Wynik</title>
</head>
<body>
<h1 align="center">To jest to co chciales znalezc</h1>
END

$foot = <<'END'; # footer of the HTML document
<hr>
</body>
</html>
END

$file = 'dane.html';

########### BEGIN ##############

&ReadParse;
$TEXT = $in{'text'};

print "Content-Type: text/html\n\n";
print $head;


open(PLIK,"$file") || die "Open $file: $!\n";
while (<PLIK>) {
	if (/^<h3>([^<]+)<\/h3>/) { # find <h3> case insensitive
		$found = 1 if $1 eq $TEXT; # or => $1 =~ /^$TEXT$/i
	}
	last if $found && /^<hr>/i; # the end of the data record
	print if $found;
}
close(PLIK) || die "Close $file: $!\n";

print "$TEXT not found. ;-(<br>\n" if !($found);

print $foot;

sub ReadParse { # my readparse method
        my($req,@req);
        $req = $ENV{'REQUEST_METHOD'} eq 'GET' ?
           $ENV{'QUERY_STRING'} :
           join('',<STDIN>);
	$req =~ s/\n//g;
        @req = split(/&/,$req);
        foreach (@req) {
                my($key,$value) = split(/=/,$_,2);
                $in{"$key"} = &uri_unescape($value);
        }
}

sub uri_unescape # from URI::Escape with my own improvements
{
    # Note from RFC1630:  "Sequences which start with a percent sign
    # but are not followed by two hexadecimal characters are reserved
    # for future extension"
    my @copy = @_;
    for (@copy) { s/\+/ /g; s/%([\dA-Fa-f]{2})/chr(hex($1))/eg; }
    wantarray ? @copy : $copy[0];
}

#P.K.

-- 
Piotr Klaban
--
makler@man.torun.pl
Technical Details
Message-ID: <63uogl$7lv$1@www.man.torun.pl>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!pingwin.icm.edu.pl!mat.uni.torun.pl!uw.edu.pl!news.icm.edu.pl!news.man.torun.pl!news.man.torun.pl!not-for-mail
References: <34608A8E.91D2CBAF@univcomp.waw.pl>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!pingwin.icm.edu.pl!mat.uni.torun.pl!uw.edu.pl!news.icm.edu.pl!news.man.torun.pl!news.man.torun.pl!not-for-mail
From: Piotr Klaban <makler@man.torun.pl>
Newsgroups: pl.comp.lang.perl
Subject: Re: Problem z CGI...
Date: 7 Nov 1997 10:54:29 +0100
Organization: Nicholas Copernicus University
Lines: 86
Message-ID: <63uogl$7lv$1@www.man.torun.pl>
References: <34608A8E.91D2CBAF@univcomp.waw.pl>
NNTP-Posting-Host: flis.man.torun.pl
Mime-Version: 1.0
X-Newsreader: TIN [UNIX 1.3 unoff BETA 970930; sun4d SunOS 5.5]
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit