0

I have a PHP script for my website, which prints the dmesg command out, and has a button to download the output. Here is the script:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>DMESG</title>
        <script src="/admin/JS/JQuery.js"></script>
        <script>
        function download(){
            var downloadString = encodeURIComponent($(".dmesg").html()).replace("%3Cbr%3E", "\n");
            var a = document.body.appendChild(
                document.createElement("a")
            );
            a.download = "DMESG.txt";
            a.href = "data:text/plain," + downloadString;
            a.click();
            $("a").remove();
        }
    </script>
    </head>

    <body>
        <center id="header">
            <button onclick="download()">Download</button>
        </center>
        <hr />
        <div class="dmesg">
            <?php
                exec("dmesg 2>&1", $dmesgOutput, $dmesgReturn);

                if($dmesgReturn != "0") {
                    echo "Error!";
                } else {
                    foreach ($dmesgOutput as $dmesgLine) {
                        echo $dmesgLine . "<br>";
                    }
                }
            ?>
        </div>
    </body>
</html>

And upon clicking the Download button, the contents of the file are here (notice the <br>'s).

I was wondering what I am doing wrong, and how can I fix this?

EDIT:

Here is the output of the encodeURIComponent() variable.

MaliciouZzHD
  • 49
  • 1
  • 12
  • 1
    can you confirm that the result of encodeURIcomponent contains just one exact instance of that exact string you want to replace – Jaromanda X Mar 22 '18 at 07:29
  • ....and if the output of `encodeURIComponent($(".dmesg").html())` does indeed contain `%3Cbr%3E` instead of `
    `?
    – web-nomad Mar 22 '18 at 07:33
  • I updated with a Pastebin of the raw `encodeURIComponent` variable, and it does contain `%3Cbr%3E`. – MaliciouZzHD Mar 22 '18 at 07:36
  • In that case, you will have to replace all occurrences of that string. https://stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string-in-javascript – web-nomad Mar 22 '18 at 07:38
  • Thanks, now I just need to figure out why the `\n` in `.replaceAll("%3Cbr%3E", "\n")` doesn't do a newline in Notepad. EDIT: Fixed, forgot carriage return! – MaliciouZzHD Mar 22 '18 at 07:51

0 Answers0