Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HOW TO? Sending PHP code from CodeMirror editor via jQuery.ajax:POST and using php: file_put_contents();

I am making a http://c9.io like service to edit .php files on my server right on browser.

I have implemented CodeMirror editor there. Editor looks following: http://codemirror.net/mode/php/index.html

My problem is that I can't send data with php code in it via jQuery.ajax POST.

Lets imagine that I would like to save following lines to hello.php:

 <?php 
         require_once("lukujA.php"); 
 ?>

I am using following js / jquery code to save the file:

$(".save-file").click(function (){
            var content = editor.getValue(); //textarea text
            var path = "hello.php";
        //following line shows content-data as it shows on CodeMirror editor
        //confirm box without any quotes / slashes / and with every linebreak

            var response = confirm("Do you want to save? DATA: " + content);
            if(response)
            {
                $.ajax({
                    type: "GET",
                    url: "saveFile.php",
                    data: "content="+content+"&path="+path+"",
                    success: function(){
                        alert("File saved!"); 
                    }
                });
            }
            else{
                alert("File not saved!");
            }
        });

saveFile.php:

$path = $_GET['path'];
$content = $_GET['content'];

if($path !== "" and is_writable($path))
    file_put_contents($path, $content);

above code outputs hello.php as something like following (on one line and with slashes)(using POST seems to remove any line breaks I made on the editor):

<?php require_once(\"lukujA.php\"); ?>

I can't use stripslashes($content); on saveFile.php cause if I have php code like:

<?php echo "<input type=\"text\" name=\"foo\">"; ?>

strip_slashes would remove those slashes and code would become invalid when executed.

How should I come across this and how should I save the new code to a file? How would you make this kind of editor?

Thanks

like image 201
Crazywako Avatar asked Sep 14 '25 22:09

Crazywako


1 Answers

Got the whole thing working with following:

    $(".save-file").click(function (){
        editor.save();
        var content = editor.getValue(); //textarea text
        var path = $("#hiddenFilePath").text(); //path of the file to save
        var response = confirm("Do you want to save?");
        if(response)
        {
            $.ajax({
                type: "POST",
                url: "saveFile.php",
                data: {c:content,p:path},
                dataType: 'text',
                success: function(){
                    alert("File saved!"); 
                }
            });
        }
        else{
            alert("File not saved!");
        }
    });

See the code data: {c:content,p:path}, dataType: 'text',

and in saveFile.php I use stripslashes($content) cause it seems I have magicquotes on on php settings.

When I need to send data like echo "<br><a href=\"?p=$p&vko=$vkoPrev\">Edellinen</a> <a href=\"?p=$p&vko=$vko\">Tämä viikko</a> <a href=\"?p=$p&vko=$vkoNext\">Seuraava</a><br><br>"; stripslashes still preserves those slashes before my data's quotes cause when I send data through POST like that seen above urlEncoding adds slashes before my datas slashes too.

Hard to explain. Hope someone will in future get something out of this :) Sorry for not so good english either.

like image 194
Crazywako Avatar answered Sep 17 '25 20:09

Crazywako