#!/usr/bin/perl
# Smart Tag Generator
# Provided by CGI Connection
# http://www.CGIConnection.com
$| = 1;
srand();
# Where to start searching for your web pages
# Generally it's the absolute path to your home pages
# Eg. /path/to/your/pages
$webdir = "!WEBDIR!";
# Name of folder to create to place backups of pages in
# This folder will be created under each directory
# where files were converted
$backup_folder = "backup";
# Filename extension to use for backups
$backupext = "old";
# DO NOT EDIT BELOW HERE
#########################################
&parse_form;
$nonwritable = 0;
$htmlfiles = 0;
$foundtags = 0;
$nofoundtags = 0;
$dircount = 1;
splice(@all_dirs, 0);
$tmpext = int(rand(10000));
@all_dirs[0] = $webdir;
print "Content-type: multipart/mixed\n\n";
print "
Smart Tag\n";
for ($x = 0; $x < 50; $x++)
{
print "\n";
}
if ($FORM{'area'} eq "")
{
&login_screen;
exit;
}
splice(@all_ext, 0);
@all_ext = split(/, /, $FORM{'ext'});
for ($xx = 0; $xx < @all_ext; $xx++)
{
$DOEXT{@all_ext[$xx]} = 1;
}
print "Smart Tag Generator
\n";
if ($FORM{'viewonly'} ne "")
{
print "Files are not being converted (view only)
\n";
}
for ($xx = 0; $xx < @all_dirs; $xx++)
{
if (@all_dirs[$xx] ne "")
{
&get_files(@all_dirs[$xx]);
&convert_files(@all_dirs[$xx]);
}
}
print "Total Files: $htmlfiles
\n";
print "Files converted: $nofoundtags
\n";
print "Files that could not be written to: $nonwritable
\n";
print "Files already containing smart tag: $foundtags
\n";
print "\n";
print "\n";
exit;
sub get_files
{
my $tempdir = @_[0];
splice(@all_files, 0);
opendir(FILES, "$tempdir");
@all_files = readdir(FILES);
closedir(FILES);
@all_files = sort(@all_files);
$filecount = @all_files;
}
sub convert_files
{
$nowdir = @_[0];
$j = 2;
$backok = 0;
$backok = mkdir("$nowdir\/$backup_folder", 0777);
print "
Directory: $nowdir";
if ($backok == 0 and not -e "$nowdir\/$backup_folder")
{
print " (Could not create backup folder - SKIPPING)";
}
print "
\n";
if ($backok == 1 and $FORM{'viewonly'} ne "")
{
rmdir("$nowdir\/$backup_folder");
}
print "\n";
until ($j > @all_files)
{
$count = 0;
$headline = 0;
$htmlline = 0;
$tfile = "";
$found = 0;
$foundit = 0;
until($foundit == 1 or $j > @all_files)
{
$readfile = @all_files[$j];
$ridx = rindex($readfile, ".");
$tfile = substr($readfile, $ridx + 1);
if ("\L$tfile\E" eq "htm" and $DOEXT{'htm'} == 1)
{
$foundit = 1;
}
elsif ("\L$tfile\E" eq "html" and $DOEXT{'html'} == 1)
{
$foundit = 1;
}
elsif ("\L$tfile\E" eq "shtm" and $DOEXT{'shtm'} == 1)
{
$foundit = 1;
}
elsif ("\L$tfile\E" eq "shtml" and $DOEXT{'shtml'} == 1)
{
$foundit = 1;
}
elsif ("\L$tfile\E" eq "lasso" and $DOEXT{'lasso'} == 1)
{
$foundit = 1;
}
elsif ("\L$tfile\E" eq "pl" and $DOEXT{'pl'} == 1)
{
$foundit = 1;
}
elsif ("\L$tfile\E" eq "cgi" and $DOEXT{'cgi'} == 1)
{
$foundit = 1;
}
elsif ("\L$tfile\E" eq "php" and $DOEXT{'php'} == 1)
{
$foundit = 1;
}
if (-d "$nowdir\/$readfile" and $readfile ne "" and $readfile ne "." and $readfile ne "..")
{
@all_dirs[$dircount] = "$nowdir\/$readfile";
$dircount++;
}
$j++;
}
$htmlfiles++;
open(FILE, "<$nowdir\/$readfile");
until(eof(FILE) or $found == 1)
{
$line = ;
if ($line =~ m//i)
{
$htmlline = $count;
}
if ($line =~ m//i)
{
$headline = $count;
}
if ($line =~ m//i)
{
$found = 1;
$foundtags++;
}
$count++;
}
close(FILE);
$hadit = "";
$nowrite = 0;
$nodir = 0;
if (not -w "$nowdir\/$readfile" and not -d "$nowdir\/$readfile")
{
$hadit .= "X ";
$nowrite = 1;
$nonwritable++;
}
if (-d "$nowdir\/$readfile")
{
$nodir = 1;
}
if ($found == 1)
{
$hadit .= "* ";
}
print "$hadit$readfile
\n";
print "\n";
if ($found == 0 and $nowrite == 0 and $nodir == 0 and $backok == 1)
{
$count = 0;
$nofoundtags++;
$writefile = "$readfile\.$backupext";
if ($FORM{'viewonly'} eq "")
{
open(FILE, "<$nowdir\/$readfile");
open(FILE2, ">$nowdir\/$backup_folder\/$writefile");
until(eof(FILE))
{
$line = ;
if ($headline == 0 and $count == $htmlline)
{
$tmpval = index("\L$line\E", "");
$tmpline = substr($line, 0, $tmpval + 6);
$tmpline .= "";
$tmpline .= substr($line, $tmpval + 6);
$line = $tmpline;
}
print FILE2 "$line";
if ($count == $headline and $headline > 0)
{
print FILE2 "\n";
}
$count++;
}
close(FILE);
close(FILE2);
rename("$nowdir\/$readfile", "$nowdir\/$readfile\.$tmpext");
rename("$nowdir\/$backup_folder\/$writefile", "$nowdir\/$readfile");
rename("$nowdir\/$readfile\.$tmpext", "$nowdir\/$backup_folder\/$writefile");
}
}
}
}
sub parse_form {
if ("\U$ENV{'REQUEST_METHOD'}\E" eq 'GET') {
# Split the name-value pairs
@pairs = split(/&/, $ENV{'QUERY_STRING'});
}
elsif ("\U$ENV{'REQUEST_METHOD'}\E" eq 'POST') {
# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
# Split the name-value pairs
@pairs = split(/&/, $buffer);
}
else {
&error('request_method');
}
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
# If they try to include server side includes, erase them, so they
# arent a security risk if the html gets returned. Another
# security hole plugged up.
$value =~ s///g;
# Remove HTML Tags
if ($allow_html == 0)
{
$value =~ s/<([^>]|\n)*>//g;
}
# Create two associative arrays here. One is a configuration array
# which includes all fields that this form recognizes. The other
# is for fields which the form does not recognize and will report
# back to the user in the html return page and the e-mail message.
# Also determine required fields.
if ($FORM{$name}) {
$FORM{$name} = "$FORM{$name}, $value";
}
elsif ($value ne "") {
$FORM{$name} = $value;
}
}
}
sub error
{
local($msg) = @_;
print "Content-Type: text/html\n\n";
print "$msg
\n";
exit;
}
sub login_screen
{
{
print<
Smart Tag
Smart Tag Generator
END
}
}