Solved

Appending PHP Code to Several Files Across Multiple Directories

Posted on 2006-07-05
22
246 Views
Last Modified: 2010-04-22
Hello All,

We have a web server running L.A.M.P. and we have several directories under the "HOME" directory that contain PHP files.

The issue for us is that we have to append some addtional lines of code to several of these pages. Can SED or AWK or any tool that is found under LINUX that will look for lines of code and append to it if found? If so, can someone give us an idea as to  how this is done?

Here is the current code:

<?php
$x_EventCodeList = "<select name=\"x_EventCode\"><option value=\"\">Please Select</option>";
$x_EventCodeList .= "<option value=\"" . htmlspecialchars("OTHER") . "\"";
if (@$x_EventCode == "OTHER") {
      $x_EventCodeList .= " selected";
}
$x_EventCodeList .= ">" . "OTHER" . "</option>";
$x_EventCodeList .= "<option value=\"" . htmlspecialchars("SCA") . "\"";
if (@$x_EventCode == "SCA") {
      $x_EventCodeList .= " selected";
}
$x_EventCodeList .= ">" . "SCA" . "</option>";
$x_EventCodeList .= "<option value=\"" . htmlspecialchars("EAA") . "\"";
if (@$x_EventCode == "EAA") {
      $x_EventCodeList .= " selected";
}
$x_EventCodeList .= ">" . "EAA" . "</option>";
$x_EventCodeList .= "<option value=\"" . htmlspecialchars("FCA") . "\"";
if (@$x_EventCode == "FCA") {
      $x_EventCodeList .= " selected";
}
$x_EventCodeList .= ">" . "FCA" . "</option>";
$x_EventCodeList .= "<option value=\"" . htmlspecialchars("MMA") . "\"";
if (@$x_EventCode == "MMA") {
      $x_EventCodeList .= " selected";
}
$x_EventCodeList .= ">" . "MMA" . "</option>";
$x_EventCodeList .= "<option value=\"" . htmlspecialchars("ONE") . "\"";
if (@$x_EventCode == "ONE") {
      $x_EventCodeList .= " selected";
}
$x_EventCodeList .= ">" . "ONE" . "</option>";
$x_EventCodeList .= "<option value=\"" . htmlspecialchars("CH") . "\"";
if (@$x_EventCode == "CH") {
      $x_EventCodeList .= " selected";
}
$x_EventCodeList .= ">" . "CH" . "</option>";
$x_EventCodeList .= "<option value=\"" . htmlspecialchars("TL") . "\"";
if (@$x_EventCode == "TL") {
      $x_EventCodeList .= " selected";
}
$x_EventCodeList .= ">" . "TL" . "</option>";
$x_EventCodeList .= "<option value=\"" . htmlspecialchars("BLC") . "\"";
if (@$x_EventCode == "BLC") {
      $x_EventCodeList .= " selected";
}
$x_EventCodeList .= ">" . "BLC" . "</option>";
$x_EventCodeList .= "</select>";
echo $x_EventCodeList;
?>

Here is what we need to replace it with:

<?php
$x_EventCodeList = "<select name=\"x_EventCode\"><option value=\"\">Please Select</option>";
$x_EventCodeList .= "<option value=\"" . htmlspecialchars("OTHER") . "\"";
if (@$x_EventCode == "OTHER") {
      $x_EventCodeList .= " selected";
}
$x_EventCodeList .= ">" . "OTHER" . "</option>";
$x_EventCodeList .= "<option value=\"" . htmlspecialchars("SCA") . "\"";
if (@$x_EventCode == "SCA") {
      $x_EventCodeList .= " selected";
}
$x_EventCodeList .= ">" . "SCA" . "</option>";
$x_EventCodeList .= "<option value=\"" . htmlspecialchars("EAA") . "\"";
if (@$x_EventCode == "EAA") {
      $x_EventCodeList .= " selected";
}
$x_EventCodeList .= ">" . "EAA" . "</option>";
$x_EventCodeList .= "<option value=\"" . htmlspecialchars("FCA") . "\"";
if (@$x_EventCode == "FCA") {
      $x_EventCodeList .= " selected";
}
$x_EventCodeList .= ">" . "FCA" . "</option>";
$x_EventCodeList .= "<option value=\"" . htmlspecialchars("MMA") . "\"";
if (@$x_EventCode == "MMA") {
      $x_EventCodeList .= " selected";
}
$x_EventCodeList .= ">" . "MMA" . "</option>";
$x_EventCodeList .= "<option value=\"" . htmlspecialchars("ONE") . "\"";
if (@$x_EventCode == "ONE") {
      $x_EventCodeList .= " selected";
}
$x_EventCodeList .= ">" . "ONE" . "</option>";
$x_EventCodeList .= "<option value=\"" . htmlspecialchars("CH") . "\"";
if (@$x_EventCode == "CH") {
      $x_EventCodeList .= " selected";
}
$x_EventCodeList .= ">" . "CH" . "</option>";
$x_EventCodeList .= "<option value=\"" . htmlspecialchars("TL") . "\"";
if (@$x_EventCode == "TL") {
      $x_EventCodeList .= " selected";
}
$x_EventCodeList .= ">" . "TL" . "</option>";
$x_EventCodeList .= "<option value=\"" . htmlspecialchars("BLC") . "\"";
if (@$x_EventCode == "BLC") {
      $x_EventCodeList .= " selected";
}
$x_EventCodeList .= ">" . "BLC" . "</option>";
$x_EventCodeList .= "</select>";

$x_EventCodeList .= "<option value=\"" . htmlspecialchars("TLA") . "\"";
if (@$x_EventCode == "TLA") {
      $x_EventCodeList .= " selected";
}
$x_EventCodeList .= ">" . "TLA" . "</option>";
$x_EventCodeList .= "<option value=\"" . htmlspecialchars("BLB") . "\"";
if (@$x_EventCode == "BLB") {
      $x_EventCodeList .= " selected";
}
$x_EventCodeList .= ">" . "BLB" . "</option>";
$x_EventCodeList .= "</select>";
echo $x_EventCodeList;
?>

If anyone has a suggestion as to how this can be done, their help would be greatly appreciated.
0
Comment
Question by:safepointmedia
  • 11
  • 11
22 Comments
 
LVL 15

Accepted Solution

by:
bpmurray earned 500 total points
ID: 17051632
I presume you're using linux or some other flavor of U**x. You could run this shell script, where the contents of newtxt is the markup below (the new stuff).

1. Shell script:
for F in *.php
do
   export lc=`wc -l $F | sed -e "s/ .*$//"`
   head -qn `expr $lc - 3` $F> $F.new
   cat newtext >>$F.new
   mv $F.new $F
done

===============================
2. newtext file contents:
$x_EventCodeList .= "<option value=\"" . htmlspecialchars("TLA") . "\"";
if (@$x_EventCode == "TLA") {
     $x_EventCodeList .= " selected";
}
$x_EventCodeList .= ">" . "TLA" . "</option>";
$x_EventCodeList .= "<option value=\"" . htmlspecialchars("BLB") . "\"";
if (@$x_EventCode == "BLB") {
     $x_EventCodeList .= " selected";
}
$x_EventCodeList .= ">" . "BLB" . "</option>";
$x_EventCodeList .= "</select>";
echo $x_EventCodeList;
?>
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 17051645
I forgot to mention that this script adds these lines to all php files. You didn't say if every file had to be changed exactly the same way (as this does), nor did you mention what criteria determine which files should be changed.
0
 

Author Comment

by:safepointmedia
ID: 17051732
No, not all the files need to be changed. I guess what I was hoping for was something that would drop into each of the subdirectories off of the home directory and reveiw all the *.php files to see if the code was present and if it was append the addtional information.

Oh by the way, LAMP stand for Linux, APACHE, mySQL, PHP, so to answer your question, yes we are using a *NIX.

Thanks

0
 
LVL 15

Expert Comment

by:bpmurray
ID: 17051806
What code exactly? It's not at all clear which code. Do you mean all the pages have exactly the same code? I can see in the example that you want to add a few extra lines, which is what the solution above does. But it is not possible to see *exactly* what code determines which files should be updated
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 17052007
To find the files to be changed, you could change the start of the script above to:

     grep -rl <pattern> *.php | while read F
     do

where "<pattern>" is a regular expression to identify the files that should be changed
0
 

Author Comment

by:safepointmedia
ID: 17052053
okay, let's see if I can narrow it down some;

first the above code is not the only code located in the file, its code that is present in a drop down box on the web page.

Second there are a number of sub directories about 300 or so. To be exact, each of those directories contain about a hundred files. Out of those hundred there are two files that have the above code contained withen its overall code base, these files are:

eventcalendaradd.php
eventcalendaredit.php

All the remaining files don't need to be touched. Just these two per directory.

I hope I made better sense of that.
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 17052174
Let me restate it, and you can tell me how far off I am:

        You want to alter the contents of all instances of eventcalendaradd.php and eventcalendaredit.php in a directory and its sub-directories.
        The code above is only an example of what you would like to change, and is not even the entire contents of any of these files.

Now that you've clarified which files should be changed, for anyone to help you, you must be MUCH more specific about the contents of the files:
    1. Is the code to be changed identical in each file? Is it character-for-character identical?
    2. Can the particular code pattern occur more than once in a file?
    3. What is the exact change you want? Do you want a specific snippet of code added, or do you want a generic pattern of code to be added? Should any lines be deleted?

Anyway, to apply the change, the framework is:

find . -name eventcalendar*.php -print | while read F
   if [ $F == "eventcalendaradd.php" ]
   then
        make changes for this file
   elif [ $F == "eventcalendaredit.php" ]
   then
        make changes for this file
   fi
0
 

Author Comment

by:safepointmedia
ID: 17052595
Maybe I am looking or explaining this the wrong way:

Here is what I thought could happen;

find this line of code in eventcalendaradd.php and eventcalendaredit.php and delete it:

echo $x_EventCodeList .= "</select>";

The above stands on it's own line in the files and only appears once

Then once that line is deleted add the following lines:

$x_EventCodeList .= "<option value=\"" . htmlspecialchars("TLA") . "\"";
if (@$x_EventCode == "TLA") {
     $x_EventCodeList .= " selected";
}
$x_EventCodeList .= ">" . "TLA" . "</option>";
$x_EventCodeList .= "<option value=\"" . htmlspecialchars("BLB") . "\"";
if (@$x_EventCode == "BLB") {
     $x_EventCodeList .= " selected";
}
$x_EventCodeList .= ">" . "BLB" . "</option>";
$x_EventCodeList .= "</select>";

Then the code would continue as there is more after this but all of that doesn't need to be changed or addressed.

Thanks
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 17052930
You mean replace the echo ... line with this block of lines?
0
 

Author Comment

by:safepointmedia
ID: 17052998
exactly
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 17053075
Try this:

1. The script:
find . -name "eventcal*.php" -print | while read F
do
   export BN=`basename $F`
   if [ $BN == "eventcalendaradd.php" -o  $BN == "eventcalendaredit.php" ]
   then
      echo $F
        awk -f update.awk $F >/tmp/eventcal.tmp
      mv /tmp/eventcal.tmp $F
   fi
done

=========================
2. update.awk
{
   if ($0 ~ /^ *echo *\$x_EventCodeList *.= *"<\/select>"; *$/) {
      print "$x_EventCodeList .= \"<option value=\\\"\" . htmlspecialchars(\"TLA\") . \"\\\"\"";
      print "if (@$x_EventCode == \"TLA\") {";
      print "     $x_EventCodeList .= \" selected\";";
      print "}";
      print "$x_EventCodeList .= \">\" . \"TLA\" . \"</option>\";";
      print "$x_EventCodeList .= \"<option value=\\\"\" . htmlspecialchars(\"BLB\") . \"\\\"\";";
      print "if (@$x_EventCode == \"BLB\") {";
      print "     $x_EventCodeList .= \" selected\";";
      print "}";
      print "$x_EventCodeList .= \">\" . \"BLB\" . \"</option>\";";
      print "$x_EventCodeList .= \"</select>\";";
      print "";
   }
   else
      print;
}
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:safepointmedia
ID: 17055952
Thanks, script one works, but the second one is a little off. Something to do with curly bracket on line two.

line 2: syntax error near unexpected token `{'

I am going to try to google awk and see if I can find out what's wrong with it since it's late. I do appreciate your help.
0
 

Author Comment

by:safepointmedia
ID: 17056148
I put awk in verbose mode and this is the input it throws back:

./144.org/httpdocs/cp/eventcalendaradd.php
awk: syntax error at source line 2 source file update.awk
 context is
           >>>  if <<<  ($0 ~ /^ *echo *\$x_EventCodeList *.= *"<\/select>"; *$/);
        missing }
awk: bailing out at source line 16
./144.org/httpdocs/cp/eventcalendaredit.php
awk: syntax error at source line 2 source file update.awk
 context is
           >>>  if <<<  ($0 ~ /^ *echo *\$x_EventCodeList *.= *"<\/select>"; *$/);
        missing }
awk: bailing out at source line 16
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 17056961
Did you copy & paste the lines above or type them in? I ran it on my machine & it worked fine, but there are so many escapes, it's very difficult to get it just right! You should copy everything from the opening "{" to the closing "}" inclusive, and paste it into a file. It may be that you're missing the initial "{". Remember too, that there should not be a blank line at the top of the script.
0
 

Author Comment

by:safepointmedia
ID: 17060549
Thanks for getting back to me. I copied the above using vim and tested it on my MAC. I got it to run but no changes where made. All I get back on the screen was that it found the two files and it gives me the  path of them. When I go back to review the files, they are the same.

Let me make sure I did this right;

I copied the code from script one and put into a file that is named script. I then made it executable. Then I created a file called update.awk and put the code from above.

I then placed both of these files in the root directory that I wanted it to search in and ran the script file.

Do I have it right, if so is there something that I can add that will let me see what the script is encountering as it goes along??

Thanks
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 17061271
This is weird.  I just copied & pasted them from the comment above, and ran it on two different machines, using gawk, nawk and awk, in Linux and Cygwin, and it worked fine. Just to go back to the original error - it said line 2, which indicates that there was a blank line in the top of the file: the first line contains only "{". The fact that it's complaining about the "if" indicates to me that it's not inside a block, i.e. it's missing the "{". So, my question to clear this up is: is the first line in the awk script simply the left curly bracket "{" with the "if" statement starting on the second line? If so, try calling the file as: awk -f update.awk and that should do a syntax check - do you get any error messages?
0
 

Author Comment

by:safepointmedia
ID: 17061462
Maybe its's MAC. I ran it and all the cursur does is hang on the next line. Since I can't post an image of my vim window, here is the exact code I copied from the file:
{
   if ($0 ~ /^ *echo *\$x_EventCodeList *.= *"<\/select>"; *$/) {
      print "$x_EventCodeList .= \"<option value=\\\"\" . htmlspecialchars(\"TLA\") . \"\\\"\"";
      print "if (@$x_EventCode == \"TLA\") {";
      print "     $x_EventCodeList .= \" selected\";";
      print "}";
      print "$x_EventCodeList .= \">\" . \"TLA\" . \"</option>\";";
      print "$x_EventCodeList .= \"<option value=\\\"\" . htmlspecialchars(\"BLB\") . \"\\\"\";";
      print "if (@$x_EventCode == \"BLB\") {";
      print "     $x_EventCodeList .= \" selected\";";
      print "}";
      print "$x_EventCodeList .= \">\" . \"BLB\" . \"</option>\";";
      print "$x_EventCodeList .= \"</select>\";";
      print "";
   }
   else
      print;
}
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 17061592
If the cursor hangs, that's OK. Type a few chars & hit Enter, simulating an ordinary line. It should siply echo the line back to the screen. If that works OK, try typing the test line in:
   echo $x_EventCodeList .= "</select>";

and hit Enter. It should replace it with that block of code. If this works, then then awk script is OK.

One thing I forgot to ask - which shell are you using? The above is for bash, and it would be slightly different for ksh or sh.
0
 

Author Comment

by:safepointmedia
ID: 17061926
Okay, the script works at least what you requested as a test, however I went to the server copied a couple of test directories over to a test "home" directory, created the script, tested them out as you suggested and they worked, however it found the files in question but didn't change anything.
0
 

Author Comment

by:safepointmedia
ID: 17061960
Oh, here is a copy of the test section on the actual server:

[root@144.org]# awk -f update.awk
ff
ff
echo $x_EventCodeList .= "</select>";
$x_EventCodeList .= "<option value=\"" . htmlspecialchars("TLA") . "\""
if (@$x_EventCode == "TLA") {
     $x_EventCodeList .= " selected";
}
$x_EventCodeList .= ">" . "TLA" . "</option>";
$x_EventCodeList .= "<option value=\"" . htmlspecialchars("BLB") . "\"";
if (@$x_EventCode == "BLB") {
     $x_EventCodeList .= " selected";
}
$x_EventCodeList .= ">" . "BLB" . "</option>";
$x_EventCodeList .= "</select>";


0
 
LVL 15

Expert Comment

by:bpmurray
ID: 17062439
So that shows that the awk script is working OK. However, are you sure that the test string is the one we should be looking for? Looking at the initial question, it appears that the line to be changed is:
      echo $x_EventCodeList;

and not
      echo $x_EventCodeList .= "</select>";

The awk script tests for this second statement - are you certain that this is the one to be changed? That's what's in the first statement, which is an if with the regular expression:
       ^ *echo *\$x_EventCodeList *.= *"<\/select>"; *$

0
 

Author Comment

by:safepointmedia
ID: 17063630
Okay,

Well all I can say is thanks, the problem was in the second script. There is no echo in the IF statement. Once I dealt with that the thing worked like a charm. Also there was a semi colon issue as well.

I wish I could give you some more points!!

Thank you very much!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now