<?php

class CurlHelper
{

    public static function sendGetRequest($url, $params = array(), $curlConfigs = array())
    {
        $curl = curl_init();
        $curlConfigs[CURLOPT_URL] = CurlHelper::createGetUrl($url, $params);
        CurlHelper::applyCurlConfigs($curl, $curlConfigs);
        $response = CurlHelper::getCurlResponseArray($curl);
        curl_close($curl);
        return $response;
    }

    public static function createGetUrl($url, $params)
    {
        $url.= "?" . CurlHelper::getCustomizedParams($params);
        return $url;
    }

    public static function getCustomizedParams($params)
    {
        $customizedParams = array();
        foreach ($params as $key => $value) {
            $customizedParams[] = implode("=", array($key, $value));
        }
        return implode("&", $customizedParams);
    }

    public static function sendPostRequest($url, $params = array(), $curlConfigs = array())
    {
        $curl = curl_init();
        $curlConfigs[CURLOPT_URL] = $url;
        $curlConfigs[CURLOPT_POST] = true;

        if (!empty($params)) {
            $curlConfigs[CURLOPT_POSTFIELDS] = $params;
        }

        CurlHelper::applyCurlConfigs($curl, $curlConfigs);
        $response = CurlHelper::getCurlResponseArray($curl);
        curl_close($curl);
        return $response;
    }

    private static function applyCurlConfigs(&$curl, $curlConfigs)
    {
        $completeCurlConfigs = $curlConfigs + CurlHelper::defaultConfigs();
        foreach ($completeCurlConfigs as $key => $value) {
            curl_setopt($curl, $key, $value);
        }
    }

    private static function defaultConfigs()
    {
        $defaultConfigs = CurlHelper::applyCurlProxyConfigs();
        $defaultConfigs[CURLOPT_RETURNTRANSFER] = true;
        $defaultConfigs[CURLOPT_FOLLOWLOCATION] = true;
        return $defaultConfigs;
    }

    private static function applyCurlProxyConfigs()
    {
        $curlProxyConfigs = array();
        $isSecureCon = Yii::app()->request->getIsSecureConnection();
        if ($isSecureCon) {
            // setup https proxy
            if (cfg('HTTPS_PROXY_PORT') != '' && cfg('HTTPS_PROXY_IP_ADDRESS') != '') {
                $curlProxyConfigs[CURLOPT_PROXY] = cfg('HTTPS_PROXY_IP_ADDRESS') . ':' . cfg('HTTPS_PROXY_PORT');
            }

            if (cfg('HTTPS_PROXY_USERNAME') != '') {
                $curlProxyConfigs[CURLOPT_PROXYUSERPWD] = cfg('HTTPS_PROXY_USERNAME') . ':' . cfg('HTTPS_PROXY_PASSWORD');
            }
        } else {
            // setup http proxy
            if (cfg('HTTP_PROXY_PORT') != '' && cfg('HTTP_PROXY_IP_ADDRESS') != '') {
                $curlProxyConfigs[CURLOPT_PROXY] = cfg('HTTP_PROXY_IP_ADDRESS') . ':' . cfg('HTTP_PROXY_PORT');
            }

            if (cfg('HTTP_PROXY_USERNAME') != '') {
                $curlProxyConfigs[CURLOPT_PROXYUSERPWD] = cfg('HTTP_PROXY_USERNAME') . ':' . cfg('HTTP_PROXY_PASSWORD');
            }
        }
        return $curlProxyConfigs;
    }

    private static function getCurlResponseArray(&$curl)
    {
        $response = array();
        $response['content'] = curl_exec($curl);
        $response['err'] = curl_errno($curl);
        $response['errmsg'] = curl_error($curl);
        $response['header'] = curl_getinfo($curl);
        $response['httpCode'] = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        return $response;
    }
}
