首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >评估插件系统

评估插件系统
EN

Stack Overflow用户
提问于 2013-07-19 23:51:34
回答 1查看 69关注 0票数 0

做这件事最好的方法是什么?我得到了一个模板,里面有一些东西,比如{:HELLO-WORLD:}标签。

我还得到了一个类似这样的数组:

代码语言:javascript
复制
Array
(
[0] => Array
    (
        [Name] => {:HELLO-WORLD:}
        [Plugin] => "<?php return 'Hello World'; ?>"
        [Settings] => 
    )

)

我可以做些什么来确保用Hello World的输出替换{:HELLO-WORLD:}

我目前正在尝试:

代码语言:javascript
复制
    private function PluginReplacer($arr, $str){
        $gsCt = count($arr);
        $kv = array();
        for ($i=0;$i<$gsCt;++$i){
            $kv[$arr[$i]['Name']] = $arr[$i]['Plugin'];
        }
        return str_replace(array_keys($kv), $this->EvalCode(array_values($kv)), $str);
    }

    // Eval Some Code
    private function EvalCode($var){
        require_once('plugins.php');
        $pr = new CloudCMSPluginRunner();
        $pr->Code = $var;
        $pr->SitePath = GetSiteAssetsPath($this->SiteID);
        $pr->RunIt();
        echo $pr->Error;
    }

<?php

class CloudCMSPluginRunner {

public $Code = '';
public $Error = '';
public $SitePath = '';
private $DoNotAllow = array('echo', 'eval', 'phpinfo', '/`/', 'chmod', 'chown', 'umask', 'shell_exec', 
                            'exec', 'escapeshellcmd', 'proc_open', 'proc_terminate', 'proc_get_status', 
                            'passthru', 'proc_nice', 'system', 'escapeshellarg', 'ob_start', 'ob_end_clean', 
                            'ob_get_clean', 'session_start', 'putenv', 'header', 'sleep', 'uwait', 'ini_set', 
                            'error_reporting', 'chgrp', 'basename', 'clearstatcache', 'copy', 'delete', 
                            'dirname', 'disk_free_space', 'disk_total_space', 'diskfreespace', 'fclose', 
                            'feof', 'fflush', 'fgetc', 'fgetcsv', 'fgets', 'fgetss', 'file_exists', 'file_get_contents', 
                            'file_put_contents', 'file', 'fileatime', 'filectime', 'filegroup', 'fileinode', 'filemtime', 
                            'fileowner', 'fileperms', 'filesize', 'filetype', 'flock', 'fnmatch', 'fopen', 'fpassthru', 
                            'fputcsv', 'fputs', 'fread', 'fscanf', 'fseek', 'fstat', 'ftell', 'ftruncate', 'fwrite', 'glob', 
                            'is_dir', 'is_executable', 'is_file', 'is_link', 'is_readable', 'is_uploaded_file', 'is_writeable', 
                            'is_writable', 'lchgrp', 'lchown', 'link', 'linkinfo', 'lstat', 'mkdir', 'move_uploaded_file', 
                            'parse_ini_file', 'parse_ini_string', 'pathinfo', 'pclose', 'popen', 'readfile', 'readlink', 
                            'realpath_cache_get', 'realpath_cache_size', 'realpath', 'rename', 'rewind', 'rmdir', 'set_file_buffer', 
                            'stat', 'symlink', 'tempnam', 'tmpfile', 'touch', 'unlink', 'chdir', 'chroot', 'closedir', 'dir', 
                            'getcwd', 'opendir', 'readdir', 'rewinddir', 'scandir', 'dio_close', 'dio_fcntl', 'dio_open', 'dio_read', 
                            'dio_seek', 'dio_stat', 'dio_tcsetattr', 'dio_truncate', 'dio_write', 'finfo_buffer', 'finfo_close', 
                            'finfo_file', 'finfo_open', 'finfo_set_flags', 'mime_content_type', 'inotify_add_watch', 'inotify_init', 
                            'inotify_queue_len', 'inotify_read', 'inotify_rm_watch', 'setproctitle', 'setthreadtitle', 'xattr_get', 
                            'xattr_list', 'xattr_remove', 'xattr_set', 'xattr_supported');

public function RunIt(){
    $valid = $this->CheckIt();
    if($valid){
        eval($this->Code);
    }else{
        // code is invalid
        $this->Error = 'The code in this plugin is invalid.';
        return null;    
    }
}

private function CheckIt(){
    $ret = false;
    ob_start(); // Catch potential parse error messages
    $code = eval('if(0){' . "\n" . $this->Code . "\n" . '}');
    ob_end_clean();
    $ret = ($code !== false);
    // run a check against the dissallowed
    $ret = (stripos($this->Code , $this->DoNotAllow) !== false);
    // make sure any path is there's and there's alone
    $ret = (stripos($this->Code , $this->SitePath) !== false);
    return $ret;
}

}

?>

但什么都没发生...事实上,我试图在空白页面上运行这个页面(这意味着发生了一个错误)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-20 02:10:20

您生成的代码格式如下:

代码语言:javascript
复制
eval("function GetPageWeAreOn(){$p=explode('/',$_SERVER['REQUEST_URI']);return $p[1];}");

发生的情况是PHP错误地解释了变量-它不是将它们传递给eval函数,而是首先对它们进行插值。

我通过转义它们来避免这个错误:

代码语言:javascript
复制
eval("function GetPageWeAreOn(){\$p=explode('/',\$_SERVER['REQUEST_URI']);return \$p[1];}");

你也可以通过将你要求值的字符串放在单引号中来避免转义的需要--这样就不会试图插入变量:

代码语言:javascript
复制
eval('function GetPageWeAreOn(){$p=explode("/",$_SERVER["REQUEST_URI"]);return $p[1];}');
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17750371

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档