Приглашаем посетить
Кулинария (cook-lib.ru)

WebAdverts (Display)

#!/usr/bin/perl

############################################
##                                        ##
##          WebAdverts (Display)          ##
##           by Darryl Burgdorf           ##
##       (e-mail burgdorf@awsd.com)       ##
##                                        ##
##         last modified: 5/27/97         ##
##           copyright (c) 1997           ##
##                                        ##
##    latest version is available from    ##
##        http://awsd.com/scripts/        ##
##                                        ##
############################################

require "/usr/www/users/dburgdor/scripts/ads/ads_configure.pl";
&ConfigureAdverts;

$UseLocking = 1;
$displayad = $ENV{'QUERY_STRING'};
srand();

if ($displayad) { &gotoad; }
else {
	print "Content-type: text/html\n\n";
	$WrapCounter = 0;
	&displayad;
}

sub displayad {
	$WrapCounter++;
	open (COUNT, "+<$advertcount");
	$lockerror = &LockFile(COUNT);
	if ($lockerror) {
		print "[ File Lock Error ($advertcount)! ]";
		exit;
	}
	@lines = <COUNT>;
	if ((@lines < 4) || ($WrapCounter > @lines-3)) {
		&UnlockFile(COUNT);
		close (COUNT);
		exit;
	}
	foreach $line (@lines) { chop ($line) if ($line =~ /\n$/); }
	$count = $lines[0];
	$exposures = $lines[1];
	$displayad = $lines[$count+2];
	$exposures++;
	$count++;
	if ($count > @lines-3) { $count = 1; }
	$lines[0] = $count;
	unless ($shown > 0) {
		$lines[1] = $exposures;
	}
	truncate (COUNT,0);
	seek(COUNT, 0, 0);
	foreach $line (@lines) { print COUNT "$line\n"; }
	&UnlockFile(COUNT);
	close (COUNT);
	open (DISPLAY, "+<$adverts_dir/$displayad.txt");
	$lockerror = &LockFile(DISPLAY);
	if ($lockerror) {
		print "[ File Lock Error ($displayad.txt)! ]";
		exit;
	}
	@lines = <DISPLAY>;
	foreach $line (@lines) { chop ($line) if ($line =~ /\n$/); }
	($max,$shown,$visits,$url,$image,$height,$width,$alt,$pass,$text,$start,$weight) = @lines;
	unless ($weight) { $weight = 1; }
	unless ($text) { $text = "Please Visit Our Sponsor!"; }
	if ((($AdvertTracking < 2) && ($max <= $shown)) ||
	  (($AdvertTracking == 2) && ($max <= $visits)) ||
	  ((rand) > $weight)) {
		&UnlockFile(DISPLAY);
		close (DISPLAY);
		&displayad;
		exit;
	}
	print "<P ALIGN=CENTER><A HREF=\"$gotoadvert?$displayad\">";
	print "<IMG SRC=\"$image\" HEIGHT=$height WIDTH=$width ALT=\"$alt\">";
	print "</A><BR><SMALL><A HREF=\"$gotoadvert?$displayad\">";
	print "$text</A></SMALL></P>\n";
	$shown += 1;
	truncate (DISPLAY,0);
	seek(DISPLAY, 0, 0);
	print DISPLAY "$max\n";
	print DISPLAY "$shown\n";
	print DISPLAY "$visits\n";
	print DISPLAY "$url\n";
	print DISPLAY "$image\n";
	print DISPLAY "$height\n";
	print DISPLAY "$width\n";
	print DISPLAY "$alt\n";
	print DISPLAY "$pass\n";
	print DISPLAY "$text\n";
	print DISPLAY "$start\n";
	print DISPLAY "$weight\n";
	&UnlockFile(DISPLAY);
	close (DISPLAY);
	exit;
}

sub gotoad {
	open (DISPLAY, "+<$adverts_dir/$displayad.txt");
	$lockerror = &LockFile(DISPLAY);
	if ($lockerror) { exit; }
	@lines = <DISPLAY>;
	foreach $line (@lines) { chop ($line) if ($line =~ /\n$/); }
	$lines[2] += 1;
	truncate (DISPLAY,0);
	seek(DISPLAY, 0, 0);
	foreach $line (@lines) { print DISPLAY ("$line\n"); }
	&UnlockFile(DISPLAY);
	close (DISPLAY);
	print ("Location: $lines[3]\n\n");
	exit;
}

sub LockFile {
	local(*FILE) = @_;
	local($TrysLeft) = 10;
	if ($UseLocking) {
		while ($TrysLeft--) {
			$lockresult = eval("flock(FILE,6)");
			if ($@) {
				$UseLocking = 0;
				last;
			}
			if (!$lockresult) {
				select(undef,undef,undef,0.1);
			}
			else {
				last;
			}
		}
	}
	if ($TrysLeft >= 0) {
		return 0;
	}
	else {
		return -1;
	}
}

sub UnlockFile {
	local(*FILE) = @_;
	if ($UseLocking) {
		flock(FILE,8);
	}
}