CGI & Perl - Ñïèñîê ðàññûëêè íîâîñòåé
Ñïèñîê ðàññûëêè íîâîñòåé
Ïîìèìî ãîñòåâîé êíèãè îñíîâíûì àòðèáóòîì ñåðüåçíîãî Web-óçëà ÿâëÿåòñÿ ñïèñîê ðàññûëêè íîâîñòåé. Perl-ïðîãðàììà äëÿ âûïîëíåíèÿ ðàññûëêè äîâîëüíî ïðîñòà, ïðè æåëàíèè åå ìîæíî íåñêîëüêî óëó÷øèòü, äîáàâèâ íîâûå âîçìîæíîñòè (ñì. ëèñòèíã 9).
Ïðè ñîçäàíèè Web-óçëà, îðèåíòèðîâàííîãî êàê íà ðóññêîÿçû÷íûõ, òàê è íà àíãëîÿçû÷íûõ ïîñåòèòåëåé, â ïðîãðàììå ïðåäóñìîòðåíà âîçìîæíîñòü âûäà÷è ñîîáùåíèé â ñîîòâåòñòâóþùåì âèäå. Îñíîâíàÿ ôîðìà (ðèñ. 3) â çàâèñèìîñòè îò ÿçûêà óçëà äîëæíà îáÿçàòåëüíî ñîäåðæàòü ïîëå:
<input type=»hidden» name=»lang» value=”english”>
èëè
<input type=”hidden” name=”lang” value=”russian”>
äëÿ ðàñïîçíàâàíèÿ ÿçûêà âûäà÷è ñîîáùåíèé.
×òîáû îáëåã÷èòü ðàçáîð ïðîãðàììû ðàññûëêè íîâîñòåé, â íåé ðàññòàâëåíû êîììåíòàðèè.
Ëèñòèíã 9. Ñïèñîê ðàññûëêè íîâîñòåé
#!/usr/local/bin/perl
############################
# SCRIPT: POSTER.CGI #
# (c) Michael Yevdokimov #
############################
require ‘cgi_lib.pl’;
$PathList=”../mlist”; #ôèçè÷åñêèé ïóòü ê êàòàëîãó ñ áàçîé ïîñåòèòåëåé
$PathNews=”../mnews”; #ôèçè÷åñêèé ïóòü ê êàòàëîãó ñ äîêóìåíòîì äëÿ ïîñûëêè
$ReturnPage=”index.html” #ñòðàíèöà, ê êîòîðîé íóæíî áóäåò âîçâðàòèòüñÿ
$mail_prog=”/usr/sbin/sendmail”; #ôèçè÷åñêèé àäðåñ ê ïî÷òîâîé ïðîãðàììå
$Title=”News List”; #çàãîëîâîê ñïèñêà ðàññûëêè
$From=”maillist@mysite.ru”; #àäðåñ ñïèñêà ðàññûëêè
$Website=”www.mysite.ru”; #èñòî÷íèê ñïèñêà ðàññûëêè äëÿ Subject
# Method Detection and Decoding
&Parse_Data; # äåêîäèðîâàòü äàííûå èç ôîðìû / URL
$Lang=$CGI{‘lang’}; # òèï ÿçûêà (Ðóñ/Àíãë)
$User=$CGI{‘user_name’}; # èìÿ ïîñåòèòåëÿ
$Email=$CGI{‘user_email’}; # email ïîñåòèòåëÿ
$Flag=$CGI{‘flag’}; # ôëàã äëÿ ïðîâåðêè íåêîòîðûõ óñëîâèé â ïðîãðàììå
$Command=$CGI{‘command’}; # ñ÷èòàòü êîìàíäó (send èëè ‘’)
if ($Lang eq ‘english’) {
$db=”userbase.eng”; # áàçà ñ èíîñòðàííûìè ïîäïèñ÷èêàìè
$nf=”news.eng” # àíãëèéñêàÿ ”ïîñûëêà” (îáû÷íûé TXT)
} else {
if ($Lang eq ‘russian’) {
$db=”userbase.rus”; ” ñ ðîññèéñêèìè ïîäïèñ÷èêàìè
$nf=”news.rus”; ðóññêàÿ ”ïîñûëêà” (îáû÷íûé TXT)
} else {
&Print_Head; # àíàëîãè÷íî print ”Content-type:text/htmlnn”;
print ”<H3>ATTENTION! ERROR OCCURED!<H3>n”;
print ”PRESS <A HREF=”$ENV{‘HTTP_REFERER’}”>[BACK]</A> TO RETURN!n”;
exit(0);
}
}
if ($Command eq ‘send’) { # ïðîâåðêà òîé ñàìîé êîìàíäû
if ($db eq ‘userbase.eng’) {
#Post English News
&Post_News($User,$Email);
} else {
#Post Russian News
&Post_News($User,$Email);
}
}
#Ïðîâåðèòü çíà÷åíèå àòðèáóòà flag
if ($Flag eq ‘eng_yes’) { # åñëè èíôîðìàöèÿ âåðíà
&Check_Email; # âûçâàòü ïîäïðîãðàììó ïðîâåðêè ââåäåííîãî email
&Add_Record($Path,$db); # äîáàâèòü çàïèñü â ñîîòâåò. áàçó
&Print_Head;
print ”Your records were added to databasen”;
print ”Click <a href=”/$ReturnPage”>here</a> to return...n”;
exit(0);
} else {
if ($Flag eq ‘rus_yes’) {
&Check_Email;
&Add_Record;
&Print_Head;
print ”Âàøè äàííûå çàíåñåíû â áàçó äàííûõ n”;
print ”Íàæìèòå <a href=”/$ReturnPage”>çäåñü</a> äëÿ âîçâðàòà...n”;
exit(0);
} else {
}
}
# âûâåñòè ôîðìó ïîäòâåðæäåíèÿ ïðàâèëüíîñòè ââåäåííîé èíôîðìàöèè
if ($db eq ‘userbase.eng’) {
#Àíãëèéñêèé âàðèàíò ôîðìû
&Print_Head;
print ”<H2><b>News List Registration</b></H2>n”;
print ”<PRE>n”;
print ”<FORM METHOD=”GET” ACTION=”/cgi-bin/poster.cgi”>n”;
print ”<input type=hidden name=lang value=english><br>n”;
print ” Your Name: <input type=text name=user_name size=15 value=”$User”><br>n”;
print ”Your Email: <input type=text name=user_email size=15 value=”$Email”><br>n”;
# äîáàâëÿåì â ôîðìó ñêðûòîå ïîëå flag, çíà÷åíèå êîòîðîãî ïðîâåðÿåòñÿ
# ýòîé ïðîãðàììîé: åñëè flag ðàâåí îäíîìó èç äâóõ óêàçàííûõ çäåñü
# çíà÷åíèé, òî âûïîëíÿåòñÿ ïîäïðîãðàììà äîáàâëåíèÿ ïîëüçîâàòåëÿ â ÁÄ
print ”<input type=hidden name=flag value=eng_yes><br>n”;
print ”<input type=submit value=” Correct ”> ”;
print ”<input type=reset value=” Incorrect ”><br>n”;
print ”</FORM>n”;
print ”</PRE>n”;
} else {
#Ðóññêèé âàðèàíò ôîðìû
&Print_Head;
print ”<H2><b>Ðåãèñòðàöèÿ â ñïèñêå ðàññûëêè íîâîñòåé</b> </H2>n”;
print ”<PRE>n”;
print ”<FORM METHOD=”GET” ACTION=”/cgi-bin/poster.cgi”>n”;
print ”<input type=hidden name=lang value=russian><br>n”;
print ” Âàøå èìÿ: <input type=text name=user_name size=15 value=”$User”><br>n”;
print ”Âàø Email: <input type=text name=user_email size=15 value=”$Email”><br>n”;
# äîáàâëÿåì ñêðûòîå ïîëå flag (ñì. îïèñàíèå âûøå)
print ”<input type=hidden name=flag value=rus_yes><br>n”;
print ”<input type=submit value=”Âñå ïðàâèëüíî”> ”;
print ”<input type=reset value=”Íåïðàâèëüíî”><br>n”;
print ”</FORM>n”;
print ”</PRE>n”;
}
sub Add_Record { # ïîäïðîãðàììà çàïèñè ïîñåòèòåëåé â ÁÄ
open (UB, ”>>$PathList/$db”) || die (”Error during adding a record $!”);
print UB join (”::”, ”$User”, ”$Email”), ”n”;
close (UB);
}
sub Post_News { # ïîäïðîãðàììà ðàññûëêè íîâîñòåé
#Îòêðûòü UB - áàçó ïîëüçîâàòåëåé
open (UB, ”$PathList/$db”) || die (”Error during
reading file $!”);
while (<UB>) {
$line=$_;
($User, $Email) = split (/::/, $line);
#Íàïðàâèòü âûâîä íà ”email”
open (MAIL, ”|$mail_prog $Email”);
print MAIL ”From: $Fromn”;
print MAIL ”Reply-to: $Fromn”;
print MAIL ”Subject: $Title from $Websiten”;
print MAIL ”nn”;
print MAIL ”Dear $User,nn”;
#Îòêðûâàåì ôàéë íîâîñòåé è ñ÷èòûâàåì èç íåãî äàííûå
open (NF, ”$PathNews/$nf”) || die (”Error during reading file $!”);
while (<NF>) {
print MAIL ”$_”; # íàïðàâëÿåì ñ÷èòàííûå äàííûå íà email
}
close (NF);
#
print MAIL ”nnThis script was created by Michael Yevdokimovn”;
print MAIL ”Developer’s website: http://www.basicnet.sonnet.run”;
print MAIL ”nn”;
close (MAIL);
}
close (UB);
&Print_Head;
print ”PRESS <A HREF=”/$ReturnPage”>[BACK]</A>
TO RETURN!n”;
exit(0);
}
sub Check_Email { # ïîäïðîãðàììà ïðîâåðêè ïðàâèëüíîñòè
ââåäåííîãî email
&Err_Msg;
if ($Email !~ /.+@.+..+/) {
&Print_Head;
&Err_Msg;
print ”$ErrMsgn”;
#print ”Location: $ENV{‘HTTP_REFERER’}nn”;
exit(0);
}
#exit(0);
}
sub Err_Msg { # ôóíêöèÿ âûâîäà ñîîáùåíèÿ îá îøèáêå â email
if ($Lang eq ‘english’) {
$ErrMsg=”<B>ATTENTION! ERROR OCCURED!</B><P>n
<I>Email: <B>$Email</B>
which you have entered is incorrect!</I><P>n
<B>PRESS [BACK] TO RETURN<B>n”;
return $ErrMsg;
} else {
$ErrMsg=”<B>ÂÍÈÌÀÍÈÅ! ÂÎÇÍÈÊËÀ ÎØÈÁÊÀ!</B><P>n
<I>Email: <B>$Email</B>,
êîòîðûé âû ââåëè íåïðàâèëåí!</I><P>n
<B>ÍÀÆÌÈÒÅ [BACK] ÄËß ÂÎÇÂÐÀÒÀ</B>n”;
return $ErrMsg;
}
}
Íàçàä | Ñîäåðæàíèå
| Âïåðåä
|