0

I am running an Ubuntu EC2 instance. I often use find and replace to update, and tweak things which are used in all files.
However, when I have to replace PHP code, I inevitably have to replace the dollar sign, which has never successfully worked for me. I have learned, and tried all escaping methods to my knowledge. I, believing I had a method of escaping the dollar sign, which would actually replace the text with the desired text, tried replacing; to which, every one of my files containing a dollar sign--which is all of them--has now been completely torn apart, and filled with the content I was trying to replace.
The command I used was:

find ./ -name \*.php -exec sed -i "s|[\'REQUEST_URI\']|\$_SERVER[\'REQUEST_URI\']|g" {} \;

I have no idea why this occurred, and now the website I have been creating for over a year now, has been completely destroyed.
The effects are as follows:

<?php
       $sql =
       "$_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI']L$_SERVER['REQUEST_URI']C$_SERVER['REQUEST_URI'] questions.question$_SERVER['REQUEST_URI']title, questions.question$_SERVER['REQUEST_URI']content, questions.question$_SERVER['REQUEST_URI']date, questions.question$_SERVER['REQUEST_URI']answered
       F$_SERVER['REQUEST_URI']OM questions
       WH$_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI'] questions.question$_SERVER['REQUEST_URI']topic = $topic$_SERVER['REQUEST_URI']D AND questions.question$_SERVER['REQUEST_URI']answered $_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI'] N$_SERVER['REQUEST_URI']LL
       O$_SERVER['REQUEST_URI']D$_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI'] BY questions.question$_SERVER['REQUEST_URI']date D$_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI']C
       L$_SERVER['REQUEST_URI']M$_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI'] 5";

       $result = $conn->query($sql);

       if($result !== N$_SERVER['REQUEST_URI']LL && $result->num$_SERVER['REQUEST_URI']rows > 0)
       while($row = $result->fetch$_SERVER['REQUEST_URI']assoc()) {
       echo "
       <a href=$_SERVER['REQUEST_URI']view$_SERVER['REQUEST_URI']uestion.php?" . $$_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI']V$_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI'][$_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI']Y$_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI']NG$_SERVER['REQUEST_URI']] . "&question=" . $row[$_SERVER['REQUEST_URI']question$_SERVER['REQUEST_URI']title$_SERVER['REQUEST_URI']] . "$_SERVER['REQUEST_URI'] class=$_SERVER['REQUEST_URI']questionAnchor$_SERVER['REQUEST_URI']><div id=$_SERVER['REQUEST_URI']unanswered$_SERVER['REQUEST_URI']>
                  <div class=$_SERVER['REQUEST_URI']offset$_SERVER['REQUEST_URI']uestion$_SERVER['REQUEST_URI']>
                  <h4 class=$_SERVER['REQUEST_URI']thumbnail$_SERVER['REQUEST_URI']itle$_SERVER['REQUEST_URI']>" . $row[$_SERVER['REQUEST_URI']question$_SERVER['REQUEST_URI']title$_SERVER['REQUEST_URI']] . "</h4>
                  <div class=$_SERVER['REQUEST_URI']thumbnailDesc$_SERVER['REQUEST_URI']>" . $row[$_SERVER['REQUEST_URI']question$_SERVER['REQUEST_URI']content$_SERVER['REQUEST_URI']] . "</div>
                  </div>
        </div></a>
    ";
kos
  • 35,891
Cartier
  • 135
  • 8
  • Look into using a version control system (such as git or svn). Github.com is a great place for open projects but you need to pay for private repositories. I use bitbucket.org for personal projects because you can have as many private repositories as you need (with limited team abilities). Version control gives you the ability to "go back in time" for things like this. Sorry about your website. – chaptuck Jan 12 '16 at 00:35

1 Answers1

2

I'm afraid you severely damaged your site.

Analyzing your sed command:

sed -i "s|[\'REQUEST_URI\']|\$_SERVER[\'REQUEST_URI\']|g"

The command had the effect of replacing any character in the 'REQUST_I set (that is every single ', R, E, ...) with the string $_SERVER['REQUEST_URI'] in every file found by the find command.

This is not reversible, as any instance of $_SERVER['REQUEST_URI'] may now be corresponding to any of the characters in the 'REQUST_I set.

The only thing that would help a bit would be running a command to replace every instance of $_SERVER['REQUEST_URI'] with a single character in every file affected by the command, to improve the readability and to make repairing the files less painful.

For example to replace every instance of $_SERVER['REQUEST_URI'] with a dot (this will obviously have the effect of replacing also the legitimate instances of $_SERVER['REQUEST_URI']):

sed "s/\$_SERVER\['REQUEST_URI'\]/./g" file

Here's what I get if I run the command on your snippet:

<?php
       $sql =
       "..L.C. questions.question.title, questions.question.content, questions.question.date, questions.question.answered
       F.OM questions
       WH... questions.question.topic = $topic.D AND questions.question.answered .. N.LL
       O.D.. BY questions.question.date D..C
       L.M.. 5";

       $result = $conn->query($sql);

       if($result !== N.LL && $result->num.rows > 0)
       while($row = $result->fetch.assoc()) {
       echo "
       <a href=.view.uestion.php?" . $....V..[.....Y.....NG.] . "&question=" . $row[.question.title.] . ". class=.questionAnchor.><div id=.unanswered.>
                  <div class=.offset.uestion.>
                  <h4 class=.thumbnail.itle.>" . $row[.question.title.] . "</h4>
                  <div class=.thumbnailDesc.>" . $row[.question.content.] . "</div>
                  </div>
        </div></a>
    ";
kos
  • 35,891
  • Alright, thank you very, very much for your input, it is very much appreciated. Is there possibly any way I could restore to a given point in the past? Or is this not possible? – Cartier Jan 12 '16 at 00:17
  • @Cartier I'm afraid there's not. The only possibility of recovering something would be to try to recover sed's temporary files that were created by sed in the processed files' directories. You may try to use testdisk to recover those temporary files. But bear in mind that: 1) it's not guaranteed that you will be able to recover all of them 2) it's not guaranteed that they will all be intact 3) their name will be totally meaningless, since it will be a name randomly generated by sed. – kos Jan 12 '16 at 00:44
  • @Cartier You can try to adapt this old answer of mine to your case: http://askubuntu.com/a/601535/380067. See also testdisk's site (and man testdisk) for further guidance. Not to rub salt on the wound, since at this point I'm sure you understood the importance of a regular backup already, but I warmly suggest setting up some backup method for the future. I have been a web developer as well for a short time, and I understand how frustratring and time consuming is to lose work. I really wouldn't wanna deal with this again. – kos Jan 12 '16 at 00:45
  • Alright, thank you very much. I believe I am going to cut my losses, and go with your originally suggested method; I don't want to take any further chances. No worries, this very unwanted experience has shown me that, and I will be regularly backing up my files from now on. Thanks again for all of your help, it is very much appreciated. :) – Cartier Jan 12 '16 at 00:48
  • 1
    @Cartier No problem, glad that it helps. It shouldn't be too much of a pain to fix that, expecially if you are the developer and you know your code enough. ;) – kos Jan 12 '16 at 00:53
  • It helped a lot. Yeah, I have been programming this website for quite some time now, which is good and bad: I know the lay of the land, very well, but I also have a lot of files to fix. But this method helps flesh everything out. I may just "happen" to comment when everything is fixed, to show you what it looked like before all of this. – Cartier Jan 12 '16 at 00:56
  • @Cartier Alright! :) – kos Jan 12 '16 at 01:14
  • @Cartier I saw your post here: I see you deleted it so you may have figured this out already, but you were about to do the same mistake: [] are used to match character classes (i.e. s|['REQUESTED_URI']|\$_SERVER['REQUESTED_URI']|g will replace any character in the 'REQUSTD_I set with $_SERVER['REQUESTED_URI']). You need to escape the square brackets to make sed interpret them literally: sed "s|\['REQUESTED_URI'\]|\$_SERVER['REQUESTED_URI']|g". – kos Jan 12 '16 at 01:38
  • @Cartier I see you deleted the post, but for a second I feared you were about to do the same mistake, so since better safe than sorry I wanted to warn you before your situation got even worse! – kos Jan 12 '16 at 01:41
  • I thought you may see that. I actually wanted to just ask you about it, however, I was getting warned about continued conversation in the comment section, so I thought I should just post a new question. I assumed that was the case, as well, so I was actually going to use the code you provided, with the proper escaping. Should I escape the square brackets in the second portion of the command? Or just the first? Thanks again! (I can do everything else, but when it comes to this, I just can't...) – Cartier Jan 12 '16 at 01:42
  • @Cartier There's no problem, it shouldn't extend the conversation too much. Try the command I posted (sed "s|\['REQUESTED_URI'\]|\$_SERVER['REQUESTED_URI']|g") but do that without the -i switch (maybe on a single file) to see if everything is ok before doing the run with find. – kos Jan 12 '16 at 01:46
  • Alright, I will do that; I am just looking to use sed to help me recover things faster. I will use that command exactly, then. And thanks for the advice, with the -i, I hadn't thought of that... I guess that could have saved me a lot of time originally. Thanks a lot. Again. ;) Do you have any suggestions as to how I can replace large pieces of code, all at once? – Cartier Jan 12 '16 at 01:49
  • @Cartier No problem. Let me know if the last command works ok (it should), in case it doesn't we can work on it. – kos Jan 12 '16 at 01:51
  • @Cartier That really depends on the portion of code you want to replace, but in general sed is good at that. If there is too much escaping involved then Perl does a better job. If you need to post a question just do that, text processing is perfectly on-topic, and feel free also to leave me a link to it here if you want me to take a look. – kos Jan 12 '16 at 01:53
  • I will do so, and I am sure it will. Alright, I was actually about to suggest that; I think you deserve some more reputation for this extra help. :) Here is the link to the new question: http://askubuntu.com/questions/720046/how-to-find-and-replace-large-portions-of-code-text – Cartier Jan 12 '16 at 02:03