如何在php中查看php源代码?

时间:2014-07-18 07:18:01

标签: php jquery viewer

我想用php输出一些PHP代码,但我似乎无法获得正确的格式。

我查看代码的功能是:

public function openFile($path, $save = false)
    {
    if($save == true) $filename = basename($path); else $filename = "tmp/".substr(hash("sha256", time()), 8, 25).".tmp";//Datei speichern
    #$fp = fopen($filename, "wr");
    $url = "ftp://".$this->username.":".$this->password."@".$this->host.":".$this->port.$path;
    $handle = curl_init();
    curl_setopt($handle, CURLOPT_URL, $url);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($handle, CURLOPT_UPLOAD, false);
    curl_setopt($handle, CURLOPT_HEADER, true);
    #curl_setopt($handle, CURLOPT_HTTPHEADER, array("Content-Type: text/plain;"));
    #curl_setopt($handle, CURLOPT_FILE, $fp);
    $result = curl_exec($handle);
    $info = curl_getinfo ($handle);
    curl_close($handle);
    header("Content-Type: text/plain;");
    echo htmlentities($result);
    return [$result, $info];
    }

我有一个" ajaxActions.php"我处理所有行动的地方:

<?php
require_once('../classes/ftp.class.php');
$webftp = new FTP("127.0.0.1", 21, "root", "123", false, true);


if(!empty($_REQUEST["action"]) && !empty($_REQUEST["path"]))
    {
    switch ($_REQUEST["action"])
        {
        case "getDirs":
            $dirs = $webftp->listDirectory($_REQUEST["path"]);
            echo "<tr data-type=\"dir\">";
            echo "<td></td>";
            echo "<td><a onClick=\"dirUp();\" href=\"#\">..</a></td>";
            echo "<td>".$webftp->getSize("..")."</td>";
            echo "<td></td>";
            echo "<td></td>";
            echo "</tr>";
            foreach ($dirs as $dir)
                {
                // Überprüfen ob der Pfad ein Verzeichnis ist, falls ja, kann man es betreten ansonsten wird die Datei angezeigt
                if($webftp->ftpIsDir($dir)==true) $data = "folder-close"; else $data = "file";

                $aTag = "<a onClick=\"reloadTable('".$dir."');\" href=\"#\">".$dir."</a>";
                if($data == "file") $aTag = "<a onClick=\"openFile('".$dir."');\" href=\"#\">".$dir."</a>";
                echo "<tr data-type=\"".$data."\">";
                echo "<td><i class=\"glyphicon glyphicon-".$data."\"></i></td>";
                echo "<td>".$aTag."</td>";
                echo "<td>".$webftp->getSize($dir)."</td>";
                echo "<td>".($data == "file" ? '<i class="glyphicon glyphicon-eye-open"></i>' : '')."<i class=\"glyphicon glyphicon-pencil\"></i><i class=\"glyphicon glyphicon-floppy-save\"></i><i class=\"glyphicon glyphicon-remove\"></i></td>";
                echo "<td></td>";
                echo "</tr>";
                }
            break;

        case "dirUp":
            $webftp->dirUp();
            break;

        case "openFile":
            $webftp->openFile($_REQUEST["path"]);
            break;

        default:
            # code...
            break;
        }
    }
?>

在我的index.php上我通过jQuery解析代码:

function openFile(path) {
  $.get("lib/includes/ajaxActions.php?action=openFile&path=" + path, function(data) {
    console.log(data);
    $('#editor').html(data);
    $('#sourceCode').modal('show')
  });
}

console.log()返回正确的代码,但在div#编辑器上没有语法,所有的中断都消失了。

输出: 的console.log():

&lt;?php

    class FTP  {
    private $host = &quot;&quot;;
    private $port = &quot;&quot;;
    private $username = &quot;&quot;;
    private $password = &quot;&quot;;
    private $passive = false;
    private $connection = false;
    private $ssl = false;
    private $dir = false;


    public function __construct($host, $port = 21, $username, $password, $ssl = false, $passive = false)
        {
        if ( !extension_loaded('ftp') )
            {
            throw new Exception('FTP extension is not loaded!');
            }
        $this-&gt;host = $host;
        $this-&gt;port = $port;
        $this-&gt;username = $username;
        $this-&gt;password = $password;
        $this-&gt;ssl = $ssl;
        $this-&gt;passive = $passive;

        $this-&gt;connect($this-&gt;host, $this-&gt;port, $this-&gt;ssl, 90);
        $this-&gt;login($this-&gt;username, $this-&gt;password);
        }

DIV#编辑:

<?php class FTP { private $host = ""; private $port = ""; private $username = ""; private $password = ""; private $passive = false; private $connection = false; private $ssl = false; private $dir = false; public function __construct($host, $port = 21, $username, $password, $ssl = false, $passive = false) { if ( !extension_loaded('ftp') ) { throw new Exception('FTP extension is not loaded!'); } $this->host = $host; $this->port = $port; $this->username = $username; $this->password = $password; $this->ssl = $ssl; $this->passive = $passive; $this->connect($this->host, $this->port, $this->ssl, 90); $this->login($this->username, $this->password); } public function connect($host ,$port = 21, $ssl = false, $timeout = 90) { if ($ssl) { $this->connection = ftp_ssl_connect($host, $port, $timeout); } else { $this->connection = ftp_connect($host, $port, $timeout); } if ($this->connection == null) { throw new Exception('Unable to connect'); } else { return $this; } }

3 个答案:

答案 0 :(得分:2)

变化:

echo htmlentities($result);

为:

echo '<code>' . nl2br(htmlentities($result)) . '</code>';

nl2br在所有换行符之前添加<br>标记。添加<code>应该保持代码的格式。虽然我不确定您是否可以依赖 Tab 正确排队 - 但浏览器的制表位可能与您的IDE不同。

答案 1 :(得分:0)

您要呈现的输出字符串必须正确转义。这意味着替换所有“&lt;”和“&gt;”可能还有其他角色以及他们的HTML回应者。 长话短说,而不是多次使用回声,将所有文本分配给$var并执行echo htmlentities($var);。对于PHP标签,我认为你必须手动替换“&lt;”和“&gt;”带有urlEncoded值。 我希望它至少有一些帮助。

答案 2 :(得分:0)

我找到了解决方案: 我的javascript代码如下所示:

var editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
editor.getSession().setMode("ace/mode/php");
editor.setOptions({
    maxLines: 50
});

function openFile(path) {
    $.get("lib/includes/ajaxActions.php?action=openFile&path=" + path, function(data) {
        editor.setValue(data);
        $('#sourceCode').modal('show')
    });
}

我的phpcode:

public function openFile($path, $save = false)
        {
        if($save == true) $filename = basename($path); else $filename = "tmp/".substr(hash("sha256", time()), 8, 25).".tmp";//Datei speichern
        #$fp = fopen($filename, "wr");
        $url = "ftp://".$this->username.":".$this->password."@".$this->host.":".$this->port.$path;
        $handle = curl_init();
        curl_setopt($handle, CURLOPT_URL, $url);
        curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($handle, CURLOPT_UPLOAD, false);
        curl_setopt($handle, CURLOPT_HEADER, true);
        #curl_setopt($handle, CURLOPT_HTTPHEADER, array("Content-Type: text/plain;"));
        #curl_setopt($handle, CURLOPT_FILE, $fp);
        $result = curl_exec($handle);
        $info = curl_getinfo ($handle);
        curl_close($handle);
        #header("Content-Type: text/plain;");
        echo $result;
        return [$result, $info];
        }