I wrote "username" just to hide the real username, im using the real username instead of it, dont worry.
Ok, heres the script i used:
#!/usr/bin/env ksh
cd /home/username/public_html/tests/
find . -name "*.php" | while read file
do
echo "munging: $file" # nice to see progress as it works
mv "$file" "$file-" # back it up
awk ' # read the file and delete the block of php code
/<?php/ { drop = idx = 0; snarf = 1; } # start of a block start buffering
/?>/ { # end of a block
if( ! drop ) # magic string not found -- show this block
{
for( i = 0; i < idx; i++ )
printf( "%s\n", buffer );
printf( "%s\n", $0 );
}
snarf = 0; # turn off buffering
next;
}
### change the string between the slants to be something unique to the block you wish to delete.
/PHP_UNIQUE_CODE/ { drop = 1; } # magic string found drop if we are in a php block
snarf { # if buffering hold the record until end of block reached.
buffer[idx++] = $0;
next;
}
{ print; } # not buffering just print the record.
' "$file-" >"$file"
if (( $? > 0 )) # handle failure by putting the file back in place
then
echo "edit of $file failed" >&2
mv "$file-" "$file" # restore original
else
rm "$file-" # worked delete backup
fi
done
(my script filename is "newdelete2.ksh" ) Still when i execute it just by typing
newdelete2.ksh
it does not work (same problem as before) BUT i tried this
/newdelete2.ksh
and this
./newdelete2.ksh
and they both return the following:
[/]# /newdelete2.ksh
munging: ./footer.php
munging: ./home.php
munging: ./index.php
I've made a "test" directory (as you can see on the "cd /path/") with those 3 php files in it. footer, home and index.
Unfortunatly nothing happens, the php code was not removed from those files, no code was removed.
Any more ideas ? Thank you! Already helped too much
---------- Post updated 03-05-12 at 11:57 AM ---------- Previous update was 03-04-12 at 01:32 PM ----------
Just found out something, when the PHP block i want to delete is in a position like the one below (notice the "<?php get_header(); ?>" that comes before and on the same line as the "<?php"):
<?php get_header(); ?><?php
$sql = "SELECT * FROM articles WHERE id = '".$_GET['article']."'";
$do->doQuery($sql);
$article = $do->getRows();
if(isset($_POST['add'])) {
if(trim($_POST['nick']) != '') {
$nick = trim($_POST['nick']);
} else {
$errorX['nick'] = 'Please enter your nickname.';
}
if(trim($_POST['comment']) != '') {
$comment = trim($_POST['comment']);
} else {
$errorX['comment'] = 'Please enter a comment.';
}
if(empty($errorX)) {
$sql = "INSERT INTO comments (website, article_id, nickname, message, email) VALUES ('".$_POST['website']."','".$_GET['article']."','".$nick."','".$comment."','".$email."')";
$do->doQuery($sql);
header('Location: '.$_SERVER['HTTP_REFERER']);
}
}
?>
it will only delete the "?>" at the end of the code, and leave the rest of the code intact.
And there are a lot of instances in which the PHP block appears in that position (with the "<?php" appearing in front and right next to a random piece of code, without space between them)
So i edited the PHP test files, and placed the PHP block exactly like the one below:
<?php
$sql = "SELECT * FROM articles WHERE id = '".$_GET['article']."'";
$do->doQuery($sql);
$article = $do->getRows();
if(isset($_POST['add'])) {
if(trim($_POST['nick']) != '') {
$nick = trim($_POST['nick']);
} else {
$errorX['nick'] = 'Please enter your nickname.';
}
if(trim($_POST['comment']) != '') {
$comment = trim($_POST['comment']);
} else {
$errorX['comment'] = 'Please enter a comment.';
}
if(empty($errorX)) {
$sql = "INSERT INTO comments (website, article_id, nickname, message, email) VALUES ('".$_POST['website']."','".$_GET['article']."','".$nick."','".$comment."','".$email."')";
$do->doQuery($sql);
header('Location: '.$_SERVER['HTTP_REFERER']);
}
}
?>
and it worked, the whole PHP block was removed.
But now, how to make it work when the "<?php" is on the same line, and right next to a random piece of code ?
Thank you!