微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHP 写的代码在线运行工具

 

 

 

 

 

由于权限问题,编译 c 语言要 root 权限,这名改成你自己的root密码    echo 'root' | sudo

<?PHP
/**
author:zbSEOag
仅限于本地使用,不要放在服务器上
因为没有任何安全过滤.
而且还能执行 PHP 代码.
**/
error_reporting(E_ALL);
date_default_timezone_set('Asia/Shanghai');

if(!empty($_REQUEST)){

    echo '
    <style>
        *{padding:0; margin: 0; font:14px "微软雅黑";}
        table{ border-collapse: collapse;  }
        tr{   border: 1px solid  #CCC; }
        td, th{  border: 1px solid  #CCC; }
    </style>
    ';

    $action = $_REQUEST['action'];
    $data = $_REQUEST['data'];

    $tool = new Tool($data);
    if(method_exists($tool, $action)){
        $tool->$action();
    }else{
        $tool->output = '未定义的工具: ' . $action;
    }

    exit;
}

?>

<!doctype html>
<html>
<head>
<Meta charset="utf-8">
<title>我的工具包</title>
</head>
<style>
*{padding:0; margin: 0; font:14px "微软雅黑";}
html,body{ width:100%;}
button{padding:4px 10px;}
.button {padding:4px 10px; min-width: 110px; margin:4px 0; }
li{ margin-bottom: 10px;}
</style>
<body style="padding:10px; Box-sizing: border-Box; ">

<form id="form" action=""  method="post" target="iframe" autocomplete="off">
    <ul style="list-style:none;">
        <li><textarea name="data"  id="data" style="width: 100%;padding: 4px;font:16px 'Courier New'; Box-sizing: border-Box;" rows="20"></textarea></li>
        <li style="position: absolute; top: 10px; right: 10px;">
            <a target='_blank' href='http://tmp.com'><button type="button">浏览</button></a>
            <a target='iframe' id="codefile" onclick="codefile()"><button type="button">源码</button></a>
            <button type="reset">清空</button>
        </li>
    </ul>
    <input id="action" type="hidden" name="action" />
</form>


<button class="button" value="exec_c" type="button">C 语言</button>
<button class="button" value="exec_PHP" type="button">PHP 语言</button>
<button class="button" value="exec_python" type="button">Python 语言</button>

<iframe id="iframe" name="iframe" style="border:none; margin-top:20px; display:block; width:100%;" onl oad="this.height=iframe.document.body.scrollHeight;" ></iframe>
</body>
</html>

<script>
function codefile(){
    document.getElementById('codefile').setAttribute('href', '?action=look&data=' + localStorage.getItem('codefile'));
    return true;
}

var buttons = document.getElementsByClassName('button');
for(var i in buttons){

    buttons[i].onclick = function(){
        
        var form = document.getElementById('form');

        var index = 0;
        if(this.hasAttribute('data-switch-value')){

            var switch_value = this.getAttribute('data-switch-value').split('|');
            for(var i in switch_value){
                if(this.value == switch_value[i]){
                    if(i == switch_value.length - 1) break;
                    index = ++i; break;
                }

            }
            this.value = switch_value[index];
        }

        document.getElementById('action').setAttribute('value', this.getAttribute('value'));
        form.submit();
        
    }
    
}

</script>




<?PHP

class Tool{
    
    public $file = '';
    public $output =  null;
    public $data =  '';
    public $command = '';

    public function __construct($data){

        $this->file = sys_get_temp_dir() . DIRECTORY_SEParaTOR . 'index';
        $this->data = trim($data);

    }

    public function save(){
        file_put_contents($this->file, $this->data, LOCK_EX);
    }

    public static function p($args){
        call_user_func_array(array('Debug', 'p'), func_get_args());
    }

    public function __destruct(){

        if($this->command){
            $this->output = shell_exec($this->command . ' 2>&1');
            $this->output = preg_replace('/\[sudo\].*:\s+/', '', $this->output);
        }

        echo "<script>localStorage.setItem('codefile', '$this->file');</script>";
        self::p(htmlspecialchars($this->output, ENT_NOQUOTES));

    }

    public function exec_c(){

        $this->file = $this->file . '.c';
        $include = "#include <stdio.h>\n#include <math.h>\n";

        $pattern = '/(\s*#\w+\s+.*\n)+/';
        if(!preg_match('/(int|void)\s+main\(.*\)\s*\{/', $this->data)){

            preg_match_all($pattern, $this->data, $out);
            if(isset($out[0][0])) $include .= $out[0][0];
            
            $this->data = $include . "int main(){\n\t" .preg_replace($pattern, '', $this->data) . "\n\n}";
        }else{

            $this->data = $include . $this->data;
        }

        $this->save();
        $this->command = "echo 'root' | sudo -S gcc -o $this->file.out $this->file 2>&1 && $this->file.out";

    }


    public function exec_PHP(){

        if(substr($this->data, 0, 5) != '<?PHP') $this->data = "<?PHP\n\n" . $this->data;

        $this->file = $this->file . '.PHP';
        $this->save();

        $this->command = "PHP $this->file";

    }

    
    public function exec_python(){

        $this->file = $this->file . '.py';
        $this->save();

        $this->command = "python3.8 $this->file";

    }

    public function look(){

        $this->file = $this->data;
        $this->output = file_get_contents($this->file);
      
    }


}

?>

 

依赖文件: Debug.PHP

<?PHP
/**
+------------------------------------------------------------+
代码调试方便打印数据,包含:对象,类名,函数,数组,字符串,null bool
可一次批量打印多个变量
不方便打印的,还可以写日志(/sys_temp_dir/debug.txt)
@author: 相望成伤(zbSEOag)

Debug::p(1,2,3);
打印并退出
Debug::stop(1,2,3);
//输出返回结果
echo Debug::v(1,2,3);
//写日志
Debug::log(1, 2, 3);
+------------------------------------------------------------+
*/

class Debug {

    //日志文件
    public static $file = '';

    /**
     * 设置日志文件路径
     * @param string $file
     * @return mixed|string
     */
    public static function file($file = ''){

        self::$file = empty($file)? sys_get_temp_dir() . DIRECTORY_SEParaTOR . 'debug.txt' : str_replace('\\', DIRECTORY_SEParaTOR, $file);
        return self::$file;
    }

    /**
     * 格式化数据
     * @param $data
     * @return string
     */
    public static function format($data){
        
        if(in_array($data, array('TRUE','true', 'false', 'FALSE', 'null', 'NULL'), true )) $data = "\"$data\"";
        if(is_bool($data)) $data = $data? 'true' : 'false';
        if(is_null($data)) $data = 'null';
        if($data === '') $data = '""';
        
        //thinPHP 支持
        if(is_array($data) && current($data) instanceof \think\Model){ $data = collection($data)->toArray();}

        if(is_string($data)) $data = self::unicode($data);

        $output = array();
        if(is_string($data) && function_exists($data)){
            $object = new \ReflectionFunction($data);
            $output = array('Function-Name' => $data, 'Function-Namespace' => $object->getNamespaceName(),  'Function-File' => $object->getFilename());
        }

        if(is_object($data) || (is_string($data) && class_exists($data, false))){

            $message = '';
            if(is_object($data)){

                if($data instanceof \Exception){

                    $file = $data->getFile() . ' (' .$data->getLine() .')';
                    $message =  $data->getMessage() . ' (' .$data->getCode() .')';
                }

                $name = get_class($data);
                $fields = get_object_vars($data);

            }else{
                $name = $data;
                $fields = get_class_vars($data);
            }

            $methods = get_class_methods($data);

            $object = new \ReflectionClass($data);
            if(!isset($file)) $file = $object->getFilename();

            $output += array('Class-Name' => $name, 'Class-Namespace' => $object->getNamespaceName(), 'Class-Exception' => $message, 'Class-File' => $file, 'Class-Attr' => $fields, 'Class-Method' => $methods);
            
        }

        return empty($output)? $data : $output;

    }

    /**
     * 打印当前输入数据
     *
     */
    public static function input(){

        self::p('$GLOBALS:', $GLOBALS);
        self::p('PHP://input:', file_get_contents('PHP://input'));

    }

    /**
     * unicode 解码
     * @param $string
     * @return string|string[]|null
     */
    public static function unicode($string) {
        
        return preg_replace_callback('/\\\\u([0-9a-f]{4})/i', function($match){
            return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
        }, $string);
        
    }
    
    /**
     * 返回格式化后的内容
     * $args 参数列表
     */
    public static function v(){

        $args = func_get_args();
        $count = func_num_args();
        if($count == 0) $args = array();
   
        $cli = PHP_SAPI == 'cli'? true : false;
        $output = $cli? '' : '<pre style="background:#f3f3f4;padding:5px;border:1px solid #aaa;">' ;
        foreach($args as $key => $data){
            $data = self::format($data);
            $output .= print_r($data, true);

            if($key < $count - 1) $output .= $cli? PHP_EOL."--------------------------------------------------------".PHP_EOL : '<hr/>';
        }

        $output .= $cli? PHP_EOL : '</pre>';
        return $output;
        
    }

    /**
     * 打印数据
     * $args 参数列表
     */
    public static function p(){
        
        echo call_user_func_array(array('Debug', 'v'), func_get_args());
    }


    /**
     * 打印并退出
     * $args 参数列表
     */
    public static function stop(){

        call_user_func_array(array('Debug', 'p'), func_get_args());
        exit;
    }


    /**
     * 浏览器控制台打印数据
     */
    public static function console(){
    
        $output = '';
        $args = func_get_args();
        foreach($args as $key => $data) $output .= self::format($data);
        
        echo '<script>console.log("';
        echo preg_replace('/\r|\n/', '', $output);
        echo '")</script>';
        
    }
    

    /**
     * 写入格式化的日志内容
     */
    public static function log($args = ''){

        $args = func_get_args();
        $count = func_num_args();

        self::file();
        foreach($args as $key => $data){

            $data = self::format($data);
            if(!is_string($data)){
                $data = var_export($data, true);
                $data = preg_replace(array('/(=>)(\s+)\n\s+(array)/'), array('\1\2\3'), $data);
            }

            file_put_contents(self::$file, $data, FILE_APPEND | LOCK_EX);
            if($key < $count - 1) file_put_contents(self::$file, "\n----------------------------------------------------------------------------\n", FILE_APPEND | LOCK_EX);
    
        }
    
        file_put_contents(self::$file, "\n==================================================[time ".date('Y-m-d H:i:s')."]==================================================\n", FILE_APPEND | LOCK_EX);
    }


    /**
     * 写文件
     */
    public static function write($args = ''){

        $args = func_get_args();
        if(empty(self::$file)) self::file();

        foreach($args as $key => $data){
            file_put_contents(self::$file, (is_string($data)? $data : var_export($data, true)), FILE_APPEND | LOCK_EX);
        }

    }

    /**
     * 读取文件内容
     */
    public static function read($file = ''){

        if(empty($file)) $file = self::$file;
        self::p('调试文件:', file_get_contents($file));
    }

    /**
     * 清空日志文件
     */
    public static function clear(){

        self::file();
        file_put_contents(self::$file, '',LOCK_EX);
    }


}


//快捷函数
if(!function_exists('v')){

    function v(){
        return call_user_func_array(array('Debug', 'v'), func_get_args());
    }

}

if(!function_exists('p')){

    function p(){
        call_user_func_array(array('Debug', 'p'), func_get_args());
    }

}


if(!function_exists('stop')){

    function stop(){
        return call_user_func_array(array('Debug', 'stop'), func_get_args());
    }

}

 

 

 

 

 

  • 添加到短语集  
    • 没有此单词集: → 中文(简体)...  
    • 创建新的单词集...
  • 拷贝
  • 添加到短语集  
    • 没有此单词集: → 中文(简体)...  
    • 创建新的单词集...
  • 拷贝

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐