Iframe removal

From Cheatsheet
Revision as of 16:50, 20 August 2011 by Admin (talk | contribs) (Created page with 'Download here:<br> wget not available yet. To make script: vim /scripts/iframe-remover chmod +x /scripts/iframe-remover Paste the following into it. <syntaxhighlight lang="p…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Download here:
wget not available yet.

To make script:

vim /scripts/iframe-remover
chmod +x /scripts/iframe-remover

Paste the following into it.

#!/usr/bin/perl
use File::Find;

#set config_dir to something other than . perhaps /usr/local/src
$config_dir = '.';

#iframes in always_remove.dat will be removed without prompting

open(IN, "<$config_dir/always_remove.dat");
chomp(@always=<IN>);
close(IN);

#iframes in never_remove.dat will be skipped without prompting

open(IN, "<$config_dir/never_remove.dat");
chomp(@never=<IN>);
close(IN);

#auto only mode will only remove iframes in the always_remove.dat file
#and will not prompt for others it finds

if ($ARGV[0] eq '--auto-only') {
        $auto_mode = 1;
        shift @ARGV;
        print "Entering auto only mode...\n";
} else {
        $auto_mode = undef;
}

if (!$ARGV[0]) {
	die ("no directory specificed, exiting ...");
} 

%iframe_options = (
	wanted => \&iframe_action,
	no_chdir => 1
);

find(\%iframe_options, @ARGV);

open (OUT, ">$config_dir/never_remove.dat");
foreach $line (@never) {
        print OUT "$line\n";
}
close(OUT);

open (OUT, ">$config_dir/always_remove.dat");
foreach $line (@always) {
        print OUT "$line\n";
}
close(OUT);

sub timestamp {
  ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
  $year=$year+1900;
  return $year . sprintf('%02u%02u_%02u%02u%02u', $mon, $mday, $hour, $min, $sec);
}

sub iframe_action {
	$file = $_;
        $stamp = timestamp();
        print "[$stamp] : ";
        print "Scanning $file ... \n";
        open(IN, $file);
        chomp (@lines=<IN>);
        close(IN);
        $text = join "\n", @lines;
        $text_orig = $text;

        while ($text =~   m/\<iframe.*?\<\/iframe\>|\<iframe.*?\/\>/gsi  )  {
                $stamp = timestamp();
                print "[$stamp] ";
                $match=$&;
                chomp($match);
                my $action = undef;

                #check to see if the found iframe is in always_remove

                foreach (@always) {
                        if (m#\Q$match\E#) {
                                print "Auto ";
                                $action = "r";
                                last;
                        }
                }

                #check to see if the found iframe is in never_remove
                #never_remove wins in the case the iframe ends up in
                #both files

                foreach (@never) {
                        if (m#\Q$match\E#) {
                                print "Auto ";
                                $action = "s";
                                last;
                        }
                }

                if (!$action and !$auto_mode) {
                        print "match: $match\n(r)remove, (a)lways remove, (s)kip, (i)gnore : ";
                        $action = <STDIN>;
                        chomp ($action);
                }

                for(lc($action)) {

                        if (/r/) {
                                print "Removing... $match \n";
                                $text =~ s#\Q$match\E##;
                        } elsif (/a/) {
                                print "Always Removing... $match \n";
                                $text =~ s#\Q$match\E##;
                                push (@always, $match);
                        } elsif (/i/) {
                                push(@never, $match);
                                print "Always Skipping... $match \n";
                        } elsif (/s/) {
                                print "Skipping... $match \n";
				$match = "";
                        }
                }
        }

       if ($text ne $text_orig) {
                print "[$stamp] : backing up $file as $file.$stamp\n";
                open (OUT, ">$file.$stamp");
                print OUT $text_orig;
                print "[$stamp] : writing $file ... \n";
                close(OUT);
                open (OUT, ">$file");
                print OUT $text;
                close(OUT);
       } else {
                print "[$stamp] : $file file unchanged\n";
       }
}