【问题标题】:CURLOPT_FOLLOWLOCATION and open_basedir problems with mediatemple gridmediatemple 网格的 CURLOPT_FOLLOWLOCATION 和 open_basedir 问题
【发布时间】:2014-02-08 22:43:09
【问题描述】:

我在 mediatemple 网格上托管了一个 WordPress 网站,我在尝试提交表单时收到有关 open_basedir 的错误消息。提交时,表单会在 Freshbooks 中创建一个新客户。安全模式已关闭,所以我非常肯定问题出在 open_basedir 上。我收到的错误是:

Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir is set in /nfs/c04/h03/mnt/181950/domains/domain_dir/html/wp-content/plugins/gravityformsfreshbooks/api/HttpClient.php on line 79

查看 mediatemple 文档,他们建议在 php.ini 文件中添加一行。他们的例子是:

open_basedir = "/path/to/first/folder:/path/to/second/folder"

我转到我的 php.ini 文件并添加:

open_basedir = "/home/domains/domain_dir/html/:/home/181950/data/tmp/"

添加此行后,问题没有解决。将该行添加到我的 php.ini 文件后,我的网站没有显示。唯一出现的是这个错误:

Warning: require() [function.require]: open_basedir restriction in effect. File(/nfs/c04/h03/mnt/181950/domains/domain_dir/html/wp-blog-header.php) is not within the allowed path(s): (/home/domains/domain_dir/html/:/home/181950/data/tmp/) in /nfs/c04/h03/mnt/181950/domains/domain_dir/html/index.php on line 17

【问题讨论】:

    标签: php wordpress curl open-basedir


    【解决方案1】:

    我遇到了同样的问题。我编写了一些手动跟随重定向的代码。请确保您阅读并理解代码,并在将其用于生产之前了解任何安全问题!

    function remote_file($f){
        $i = 0;
        do {
            if($i++ > 10) die();
            //Pull the headers and check for redirects
            $cr = curl_init($f);
            curl_setopt($cr, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($cr, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($cr, CURLOPT_BINARYTRANSFER, TRUE);
            curl_setopt($cr, CURLOPT_HEADER, TRUE);
            $curl_ret = curl_exec($cr);
            //echo $curl_ret;
            if(curl_getinfo($cr,CURLINFO_HTTP_CODE)!=200) {
                $matches = preg_match_all('~location: ?(.*)~i',$curl_ret,$output);
                if($matches) {
                    $f = $output[1][0];
                }
            } else $matches = 0;
        } while ($matches);
    
        $cr = curl_init($f);
        curl_setopt($cr, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($cr, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($cr, CURLOPT_BINARYTRANSFER, TRUE);
        //curl_setopt($cr, CURLOPT_FOLLOWLOCATION, TRUE);    //Doesn't work with open_basedir retriction in effect
        $curl_ret = curl_exec($cr);
    
        curl_close($cr);
        return $curl_ret;
    }
    

    编辑:在 HttpClient.php 中,你应该有一个类似的函数

    private function _init($url,$token)
        {
                $this->_url = $url;
                $this->_token = $token;
                //init connection
                $this->_curlConn = curl_init($this->_url);
                curl_setopt($this->_curlConn, CURLOPT_HEADER, false);
                curl_setopt($this->_curlConn, CURLOPT_NOBODY, false);
                curl_setopt($this->_curlConn, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($this->_curlConn, CURLOPT_FOLLOWLOCATION, true);
                curl_setopt($this->_curlConn, CURLOPT_USERPWD, $this->_token);
                curl_setopt($this->_curlConn, CURLOPT_TIMEOUT, 4);
                curl_setopt($this->_curlConn, CURLOPT_SSL_VERIFYPEER, FALSE); // Validate SSL certificate
                curl_setopt($this->_curlConn, CURLOPT_SSL_VERIFYHOST, FALSE);
                curl_setopt($this->_curlConn, CURLOPT_USERAGENT, "FreshBooks API AJAX tester 1.0");
                return $this;
        }
    

    你需要重写这个函数,比如

    private function _init($url,$token)
        {
                $this->_url = $url;
                $this->_token = $token;
                //init connection
    
                $i = 0;
                do {
                    if($i++ > 10) die();
                    //Pull the headers and check for redirects
                    $this->_curlConn = curl_init($this->_url);
                    curl_setopt($this->_curlConn, CURLOPT_SSL_VERIFYPEER, FALSE);
                    curl_setopt($this->_curlConn, CURLOPT_RETURNTRANSFER, TRUE);
                    curl_setopt($this->_curlConn, CURLOPT_BINARYTRANSFER, TRUE);
                    curl_setopt($this->_curlConn, CURLOPT_HEADER, TRUE);
                    curl_setopt($this->_curlConn, CURLOPT_USERPWD, $this->_token);
                    curl_setopt($this->_curlConn, CURLOPT_USERAGENT, "FreshBooks API AJAX tester 1.0");
    
                    $curl_ret = curl_exec($cr);
                    //echo $curl_ret;
                    if(curl_getinfo($cr,CURLINFO_HTTP_CODE)!=200) {
                        $matches = preg_match_all('~location: ?(.*)~i',$curl_ret,$output);
                        if($matches) {
                            $this->_url = $output[1][0];
                        }
                    } else $matches = 0;
                } while ($matches);
    
                $this->_curlConn = curl_init($this->_url);
                curl_setopt($this->_curlConn, CURLOPT_HEADER, false);
                curl_setopt($this->_curlConn, CURLOPT_NOBODY, false);
                curl_setopt($this->_curlConn, CURLOPT_RETURNTRANSFER, true);
                //curl_setopt($this->_curlConn, CURLOPT_FOLLOWLOCATION, true);
                curl_setopt($this->_curlConn, CURLOPT_USERPWD, $this->_token);
                curl_setopt($this->_curlConn, CURLOPT_TIMEOUT, 4);
                curl_setopt($this->_curlConn, CURLOPT_SSL_VERIFYPEER, FALSE); // Validate SSL certificate
                curl_setopt($this->_curlConn, CURLOPT_SSL_VERIFYHOST, FALSE);
                curl_setopt($this->_curlConn, CURLOPT_USERAGENT, "FreshBooks API AJAX tester 1.0");
                return $this;
        }
    

    这将有效地继续将请求发送到他们的 API,直到它返回不是重定向的东西。当它感到高兴时,它会将那个重定向后的 URL 传递给标准代码,标准代码会拾取它并继续前进。

    哦,在进行这项工作之前我要尝试的另一件事...如果您只是注释掉 HttpClient.php 中的违规行怎么办?它真的需要遵循任何重定向吗?

    【讨论】:

    • 感谢您的帮助!我实际上仍然有同样的问题。是否有我需要将该功能添加到的特定文件?我将它添加到我的 functions.php 文件和 HttpClient.php 文件中。
    • 应该这样做!您所要做的就是在您的functions.php(或其他任何内容)中弹出它,然后更改HttpClient.php ..我会将代码编辑到我的答案中
    猜你喜欢
    • 2014-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-17
    • 2016-11-26
    • 1970-01-01
    • 2020-05-15
    • 2019-05-31
    相关资源
    最近更新 更多