function cw_https_request_httpscli($method, $url, $data = "", $join = "&", $cookie = "", $conttype = "application/x-www-form-urlencoded", $referer = "", $cert = "", $kcert = "", $headers = "")
{
    if ($method != "POST" && $method != "GET") {
        return array("0", "HTTPS: Invalid method");
    }
    if (!preg_match("/^(https?:\\/\\/)(.*\\@)?([a-z0-9_\\.\\-]+):(\\d+)(\\/.*)\$/Ui", $url, $m)) {
        return array("0", "HTTPS: Invalid URL");
    }
    $ui = parse_url($url);
    $binary = cw_find_executable("https_cli");
    if (!$binary) {
        return array("0", "HTTPS: https_cli executable is not found");
    }
    if (!CW_IS_OS_WINDOWS) {
        putenv("LD_LIBRARY_PATH=" . getenv("LD_LIBRARY_PATH") . ":" . dirname($binary));
    }
    $request = cw_https_prepare_request($method, $ui, $data, $join, $cookie, $conttype, $referer, $headers);
    $tmpfile = cw_temp_store($request);
    if (empty($tmpfile)) {
        return array(0, "HTTPS: cannot create temporaly file");
    }
    $cmdline = cw_shellquote($binary) . " {$ui['host']} {$ui['port']} " . cw_shellquote($cert) . " " . cw_shellquote($kcert) . " < " . cw_shellquote($tmpfile);
    // make pipe
    $fp = popen($cmdline, "r");
    if (!$fp) {
        return array(0, "HTTPS: https_cli execution failed");
    }
    $res = cw_https_receive_result($fp);
    pclose($fp);
    @unlink($tmpfile);
    return $res;
}
function cw_https_request_ssleay($method, $url, $data = "", $join = "&", $cookie = "", $conttype = "application/x-www-form-urlencoded", $referer = "", $cert = "", $kcert = "", $headers = "")
{
    global $config;
    global $app_main_dir;
    if ($method != "POST" && $method != "GET") {
        return array("0", "HTTPS: Invalid method");
    }
    if (!preg_match("/^(https?:\\/\\/)(.*\\@)?([a-z0-9_\\.\\-]+):(\\d+)(\\/.*)\$/Ui", $url, $m)) {
        return array("0", "HTTPS: Invalid URL");
    }
    $perl_exe = cw_find_executable("perl", $config['General']['perl_binary']);
    if ($perl_exec === false) {
        return array("0", "HTTPS: perl is not found");
    }
    $includes = " -I" . cw_shellquote($app_main_dir . '/payment');
    $includes .= " -I" . cw_shellquote($app_main_dir . '/payment/Net');
    $execline = cw_shellquote($perl_exe) . ' ' . $includes . ' ' . cw_shellquote($app_main_dir . "/payment/netssleay.pl");
    $ui = parse_url($url);
    if (empty($ui['port'])) {
        $ui['port'] = 443;
    }
    $request = cw_https_prepare_request($method, $ui, $data, $join, $cookie, $conttype, $referer, $headers);
    $tmpfile = cw_temp_store($request);
    if (empty($tmpfile)) {
        return array(0, "HTTPS: cannot create temporaly file");
    }
    $ignorefile = cw_temp_store("");
    $execline .= " {$ui['host']} {$ui['port']} " . cw_shellquote($cert) . ' ' . cw_shellquote($kcert) . ' < ' . cw_shellquote($tmpfile) . ' 2>' . cw_shellquote($ignorefile);
    $fp = popen($execline, "r");
    if (!$fp) {
        return array(0, "HTTPS: Net::SSLeay execution failed");
    }
    $res = cw_https_receive_result($fp);
    pclose($fp);
    @unlink($tmpfile);
    @unlink($ignorefile);
    return $res;
}
function cw_https_tunnel_request($connection, $method, $parsed_url, $data = "", $join = "&", $cookie = "", $conttype = "application/x-www-form-urlencoded", $referer = "", $headers = "")
{
    $request = cw_https_prepare_request($method, $parsed_url, $data, $join, $cookie, $conttype, $referer, $headers);
    fputs($connection, $request);
    return cw_https_receive_result($connection);
}
function cw_https_request_curl($method, $url, $data = "", $join = "&", $cookie = "", $conttype = "application/x-www-form-urlencoded", $referer = "", $cert = "", $kcert = "", $headers = "", $timeout = 0)
{
    global $config;
    if ($method != "POST" && $method != "GET") {
        return array("0", "HTTPS: Invalid method");
    }
    if (!preg_match("/^(https?:\\/\\/)(.*\\@)?([a-z0-9_\\.\\-]+):(\\d+)(\\/.*)\$/SUi", $url, $m)) {
        return array("0", "HTTPS: Invalid URL");
    }
    $curl_binary = cw_find_executable("curl");
    if (!$curl_binary) {
        return array("0", "HTTPS: curl executable is not found");
    }
    if (!CW_IS_OS_WINDOWS) {
        putenv("LD_LIBRARY_PATH=" . getenv("LD_LIBRARY_PATH") . ":" . dirname($curl_binary));
    }
    $tmpfile = cw_temp_store("");
    if (empty($tmpfile)) {
        return array(0, "HTTPS: cannot create temporaly file");
    }
    $execline = cw_shellquote($curl_binary) . " --http1.0 -D-";
    @exec(cw_shellquote($curl_binary) . " --version", $output);
    $version = @$output[0];
    # -k|--insecure key is supported by curl since version 7.10
    $supports_insecure = false;
    if (preg_match('/curl ([^ $]+)/S', $version, $m)) {
        $parts = explode(".", $m[1]);
        if ($parts[0] > 7 || ($parts[0] = 7 && $parts[1] >= 10)) {
            $supports_insecure = true;
        }
    }
    if (!empty($config['General']['https_proxy'])) {
        $execline .= " --proxytunnel --proxy " . $config['General']['https_proxy'];
    }
    # Set GET method flag
    if ($method == "GET") {
        $execline .= " --get";
    }
    # Set TimeOut parameter
    $timeout = abs(intval($timeout));
    if (!empty($timeout)) {
        $execline .= " --connect-timeout " . $timeout . " -m " . $timeout;
    }
    # Combine REQUEST string
    $request_file = false;
    if ($data) {
        if ($join) {
            foreach ($data as $k => $v) {
                list($a, $b) = explode('=', trim($v), 2);
                $data[$k] = $a . "=" . urlencode($b);
            }
        }
        $request_file = cw_temp_store(join($join, $data));
        $execline .= " -d " . cw_shellquote('@' . $request_file);
    }
    # Add SSL Certificate
    if ($cert) {
        $execline .= " --cert " . cw_shellquote($cert);
        # Add SSL Key-Certificate
        if ($kcert) {
            $execline .= " --key " . cw_shellquote($kcert);
        }
    }
    if ($supports_insecure) {
        $execline .= " -k ";
    }
    if ($cookie) {
        $execline .= " --cookie " . cw_shellquote(join(';', $cookie));
    }
    # Add Content-Type...
    if ($conttype != "application/x-www-form-urlencoded") {
        $execline .= " -H " . cw_shellquote('Content-Type: ' . $conttype);
    }
    # Add referer
    if ($referer != "") {
        $execline .= " -H " . cw_shellquote('Referer: ' . $referer);
    }
    # Additional headers
    if ($headers != "") {
        foreach ($headers as $k => $v) {
            if (is_integer($k)) {
                $execline .= " -H \"" . addslashes($v) . "\"";
            } else {
                $execline .= " -H \"{$k}: " . addslashes($v) . "\"";
            }
        }
    }
    $fp = popen($execline . " " . cw_shellquote($url) . " 2>" . cw_shellquote($tmpfile), "r");
    if (!$fp) {
        @unlink($tmpfile);
        return array(0, "HTTPS: curl execution failed");
    }
    $res = cw_https_receive_result($fp);
    pclose($fp);
    @unlink($tmpfile);
    if ($request_file !== false) {
        @unlink($request_file);
    }
    cw_https_ctl('PUT', $res);
    return $res;
}