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_get_image_size($filename, $is_image = false)
{
    static $img_types = array('1' => 'image/gif', '2' => 'image/jpeg', '3' => 'image/png', '4' => 'application/x-shockwave-flash', '5' => 'image/psd', '6' => 'image/bmp', '13' => 'application/x-shockwave-flash');
    if (empty($filename)) {
        return false;
    }
    if ($is_image) {
        global $var_dirs;
        $size = strlen($filename);
        $filename = cw_temp_store($filename);
        if (!$filename) {
            return false;
        }
    }
    list($width, $height, $type) = @getimagesize($filename);
    if (!empty($img_types[$type])) {
        $type = $img_types[$type];
    } else {
        if ($is_image) {
            @unlink($filename);
        }
        return false;
    }
    if ($is_image) {
        @unlink($filename);
    } else {
        $size = cw_filesize($filename);
    }
    return array(intval($size), $width, $height, $type);
}
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_zoomer_save_image($to_img, $prefix, $dest = false, $q = false)
{
    global $config;
    if ($q) {
    } elseif ($config["Watermark"][$prefix . "quality"] > 100) {
        $q = 100;
    } elseif ($config["Watermark"][$prefix . "quality"] < 1) {
        $q = 1;
    } else {
        $q = $config["Watermark"][$prefix . "quality"];
    }
    if (!$dest) {
        $dest = cw_temp_store("");
    }
    $dest .= "." . $config["Watermark"][$prefix . "type"];
    if ($config["Watermark"][$prefix . "type"] == "png") {
        $q = 9 - floor(0.09 * $q);
        # 0..9, where 0 - no compression
        ImagePNG($to_img, $dest, $q);
    } else {
        ImageJPEG($to_img, $dest, $q);
    }
    return $dest;
}
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;
}
function cw_pgp_add_key()
{
    global $config;
    if (!$config['Security']['crypt_method']) {
        return false;
    }
    if ($config['Security']['crypt_method'] == 'G') {
        putenv("GNUPGHOME=" . $config['Security']['gpg_home_dir']);
        $gpg_prog = cw_shellquote($config['Security']['gpg_prog']);
        $gpg_key = $config['Security']['gpg_key'];
        $fn = cw_temp_store($config['Security']['gpg_public_key']);
        chmod($fn, 0666);
        @exec($gpg_prog . ' --batch --yes --import ' . cw_shellquote($fn));
    } else {
        putenv("PGPPATH=" . $config['Security']['pgp_home_dir']);
        putenv("PGPHOME=" . $config['Security']['pgp_home_dir']);
        $fn = cw_temp_store($config['Security']['pgp_public_key']);
        $pgp_prog = cw_shellquote($config['Security']['pgp_prog']);
        $pgp_key = $config['Security']['pgp_key'];
        $ftmp = cw_temp_store('');
        if ($config['Security']['use_pgp6'] == "Y") {
            @exec($pgp_prog . ' +batchmode -ka ' . cw_shellquote($fn) . ' 2> ' . cw_shellquote($ftmp));
            @exec($pgp_prog . ' +batchmode -ks "' . $pgp_key . '"');
        } else {
            @exec($pgp_prog . ' -ka +force +batchmode ' . cw_shellquote($fn) . ' 2> ' . cw_shellquote($ftmp));
            @exec($pgp_prog . ' +batchmode -ks "' . $pgp_key . '"');
        }
        unlink($ftmp);
    }
    unlink($fn);
}