⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 remote.php

📁 是一个教学内容管理系统
💻 PHP
📖 第 1 页 / 共 2 页
字号:
                $ext = 'sl';
            } elseif (PHP_OS == 'AIX') {
                $ext = 'a';
            } else {
                $ext = 'so';
            }
            $ext = OS_WINDOWS ? 'dll' : 'so';
            @dl("xmlrpc-epi.$ext");
            if (extension_loaded("xmlrpc")) {
                break;
            }
            @dl("xmlrpc.$ext");
            if (extension_loaded("xmlrpc")) {
                break;
            }
            return $this->raiseError("unable to load xmlrpc extension");
        } while (false);
        $server_channel = $this->config->get('default_channel');
        $channel = $this->_registry->getChannel($server_channel);
        if ($channel) {
            $mirror = $this->config->get('preferred_mirror');
            if ($channel->getMirror($mirror)) {
                if ($channel->supports('xmlrpc', $method, $mirror)) {
                    $server_channel = $server_host = $mirror; // use the preferred mirror
                    $server_port = $channel->getPort($mirror);
                } elseif (!$channel->supports('xmlrpc', $method)) {
                    return $this->raiseError("Channel $server_channel does not " .
                        "support xml-rpc method $method");
                }
            }
            if (!isset($server_host)) {
                if (!$channel->supports('xmlrpc', $method)) {
                    return $this->raiseError("Channel $server_channel does not support " .
                        "xml-rpc method $method");
                } else {
                    $server_host = $server_channel;
                    $server_port = $channel->getPort();
                }
            }
        } else {
            return $this->raiseError("Unknown channel '$server_channel'");
        }
        $params = func_get_args();
        array_shift($params);
        $method = str_replace("_", ".", $method);
        $request = xmlrpc_encode_request($method, $params);
        if ($http_proxy = $this->config->get('http_proxy')) {
            $proxy = parse_url($http_proxy);
            $proxy_host = $proxy_port = $proxy_user = $proxy_pass = '';
            $proxy_host = @$proxy['host'];
            if (isset($proxy['scheme']) && $proxy['scheme'] == 'https') {
                $proxy_host = 'ssl://' . $proxy_host;
            }
            $proxy_port = @$proxy['port'];
            $proxy_user = @urldecode(@$proxy['user']);
            $proxy_pass = @urldecode(@$proxy['pass']);
            $fp = @fsockopen($proxy_host, $proxy_port);
            $use_proxy = true;
            if ($channel->getSSL()) {
                $server_host = "https://$server_host";
            }
        } else {
            $use_proxy = false;
            $ssl = $channel->getSSL();
            $fp = @fsockopen(($ssl ? 'ssl://' : '') . $server_host, $server_port);
            if (!$fp) {
                $server_host = "$ssl$server_host"; // for error-reporting
            }
        }
        if (!$fp && $http_proxy) {
            return $this->raiseError("PEAR_Remote::call: fsockopen(`$proxy_host', $proxy_port) failed");
        } elseif (!$fp) {
            return $this->raiseError("PEAR_Remote::call: fsockopen(`$server_host', $server_port) failed");
        }
        $len = strlen($request);
        $req_headers = "Host: $server_host:$server_port\r\n" .
             "Content-type: text/xml\r\n" .
             "Content-length: $len\r\n";
        $username = $this->config->get('username');
        $password = $this->config->get('password');
        if ($username && $password) {
            $req_headers .= "Cookie: PEAR_USER=$username; PEAR_PW=$password\r\n";
            $tmp = base64_encode("$username:$password");
            $req_headers .= "Authorization: Basic $tmp\r\n";
        }
        if ($this->cache !== null) {
            $maxAge = '?maxAge='.$this->cache['lastChange'];
        } else {
            $maxAge = '';
        }

        if ($use_proxy && $proxy_host != '' && $proxy_user != '') {
            $req_headers .= 'Proxy-Authorization: Basic '
                .base64_encode($proxy_user.':'.$proxy_pass)
                ."\r\n";
        }

        if ($this->config->get('verbose') > 3) {
            print "XMLRPC REQUEST HEADERS:\n";
            var_dump($req_headers);
            print "XMLRPC REQUEST BODY:\n";
            var_dump($request);
        }

        if ($use_proxy && $proxy_host != '') {
            $post_string = "POST http://".$server_host;
            if ($proxy_port > '') {
                $post_string .= ':'.$server_port;
            }
        } else {
            $post_string = "POST ";
        }

        $path = '/' . $channel->getPath('xmlrpc');
        fwrite($fp, ($post_string . $path . "$maxAge HTTP/1.0\r\n$req_headers\r\n$request"));
        $response = '';
        $line1 = fgets($fp, 2048);
        if (!preg_match('!^HTTP/[0-9\.]+ (\d+) (.*)!', $line1, $matches)) {
            return $this->raiseError("PEAR_Remote: invalid HTTP response from XML-RPC server");
        }
        switch ($matches[1]) {
            case "200": // OK
                break;
            case "304": // Not Modified
                return $this->cache['content'];
            case "401": // Unauthorized
                if ($username && $password) {
                    return $this->raiseError("PEAR_Remote ($server_host:$server_port) " .
                        ": authorization failed", 401);
                } else {
                    return $this->raiseError("PEAR_Remote ($server_host:$server_port) " .
                        ": authorization required, please log in first", 401);
                }
            default:
                return $this->raiseError("PEAR_Remote ($server_host:$server_port) : " .
                    "unexpected HTTP response", (int)$matches[1], null, null,
                    "$matches[1] $matches[2]");
        }
        while (trim(fgets($fp, 2048)) != ''); // skip rest of headers
        while ($chunk = fread($fp, 10240)) {
            $response .= $chunk;
        }
        fclose($fp);
        if ($this->config->get('verbose') > 3) {
            print "XMLRPC RESPONSE:\n";
            var_dump($response);
        }
        $ret = xmlrpc_decode($response);
        if (is_array($ret) && isset($ret['__PEAR_TYPE__'])) {
            if ($ret['__PEAR_TYPE__'] == 'error') {
                if (isset($ret['__PEAR_CLASS__'])) {
                    $class = $ret['__PEAR_CLASS__'];
                } else {
                    $class = "PEAR_Error";
                }
                if ($ret['code']     === '') $ret['code']     = null;
                if ($ret['message']  === '') $ret['message']  = null;
                if ($ret['userinfo'] === '') $ret['userinfo'] = null;
                if (strtolower($class) == 'db_error') {
                    $ret = $this->raiseError(PEAR::errorMessage($ret['code']),
                                             $ret['code'], null, null,
                                             $ret['userinfo']);
                } else {
                    $ret = $this->raiseError($ret['message'], $ret['code'],
                                             null, null, $ret['userinfo']);
                }
            }
        } elseif (is_array($ret) && sizeof($ret) == 1 && isset($ret[0])
                  && is_array($ret[0]) &&
                  !empty($ret[0]['faultString']) &&
                  !empty($ret[0]['faultCode'])) {
            extract($ret[0]);
            $faultString = "XML-RPC Server Fault: " .
                 str_replace("\n", " ", $faultString);
            return $this->raiseError($faultString, $faultCode);
        } elseif (is_array($ret) && sizeof($ret) == 2 && !empty($ret['faultString']) &&
              !empty($ret['faultCode'])) {
            extract($ret);
            $faultString = "XML-RPC Server Fault: " .
                 str_replace("\n", " ", $faultString);
            return $this->raiseError($faultString, $faultCode);
        }
        return $ret;
    }

    // }}}

    // {{{ _encode

    // a slightly extended version of XML_RPC_encode
    function _encode($php_val)
    {
        global $XML_RPC_Boolean, $XML_RPC_Int, $XML_RPC_Double;
        global $XML_RPC_String, $XML_RPC_Array, $XML_RPC_Struct;

        $type = gettype($php_val);
        $xmlrpcval = new XML_RPC_Value;

        switch($type) {
            case "array":
                reset($php_val);
                $firstkey = key($php_val);
                end($php_val);
                $lastkey = key($php_val);
                reset($php_val);
                if ($firstkey === 0 && is_int($lastkey) &&
                    ($lastkey + 1) == count($php_val)) {
                    $is_continuous = true;
                    reset($php_val);
                    $size = count($php_val);
                    for ($expect = 0; $expect < $size; $expect++, next($php_val)) {
                        if (key($php_val) !== $expect) {
                            $is_continuous = false;
                            break;
                        }
                    }
                    if ($is_continuous) {
                        reset($php_val);
                        $arr = array();
                        while (list($k, $v) = each($php_val)) {
                            $arr[$k] = $this->_encode($v);
                        }
                        $xmlrpcval->addArray($arr);
                        break;
                    }
                }
                // fall though if not numerical and continuous
            case "object":
                $arr = array();
                while (list($k, $v) = each($php_val)) {
                    $arr[$k] = $this->_encode($v);
                }
                $xmlrpcval->addStruct($arr);
                break;
            case "integer":
                $xmlrpcval->addScalar($php_val, $XML_RPC_Int);
                break;
            case "double":
                $xmlrpcval->addScalar($php_val, $XML_RPC_Double);
                break;
            case "string":
            case "NULL":
                $xmlrpcval->addScalar($php_val, $XML_RPC_String);
                break;
            case "boolean":
                $xmlrpcval->addScalar($php_val, $XML_RPC_Boolean);
                break;
            case "unknown type":
            default:
                return null;
        }
        return $xmlrpcval;
    }

    // }}}

}

?>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -