<commit_before><?php

namespace Xlstudio\Hupun;

class HupunClient
{
    protected $appKey;

    protected $secretKey;

    protected $gatewayUrl = 'https://erp-open.hupun.com/api';

    protected $format = 'json';

    protected $connectTimeout;

    protected $readTimeout;

    // 日志存放的工作目录
    protected $hupunSdkWorkDir = './data/';

    protected $signMethod = 'md5';

    protected $apiVersion = 'v1';

    protected $sdkVersion = 'hupun-open-api-php-sdk-20190508';

    public function __construct($appKey = '', $secretKey = '', $options = [])
    {
        $this->appKey = trim($appKey);
        $this->secretKey = trim($secretKey);

        if ($options) {
            if ($options['api_url']) {
                $this->gatewayUrl = trim($options['api_url']);
            }
            if ($options['api_work_dir']) {
                $this->hupunSdkWorkDir = $options['api_work_dir'];
            }
        }
    }

    public function setGatewayUrl($gatewayUrl)
    {
        $this->gatewayUrl = trim($gatewayUrl);
    }

    public function setFormat($format)
    {
        $this->format = $format;
    }

    public function setConnectTimeout($connectTimeout)
    {
        $this->connectTimeout = $connectTimeout;
    }

    public function setReadTimeout($readTimeout)
    {
        $this->readTimeout = $readTimeout;
    }

    public function setHupunSdkWorkDir($hupunSdkWorkDir)
    {
        $this->hupunSdkWorkDir = $hupunSdkWorkDir;
    }

    protected function generateSign($params, $isOpen = false)
    {
        ksort($params);

        $stringToBeSigned = $this->secretKey;
        foreach ($params as $k => $v) {
            if ('@' != substr($v, 0, 1)) {
                if ($isOpen) {
                    $stringToBeSigned .= urlencode($k) . '=' . urlencode($v) . '&';
                } else {
                    $stringToBeSigned .= "$k$v";
                }
            }
            unset($k, $v);
        }
        $stringToBeSigned = rtrim($stringToBeSigned, '&');
        $stringToBeSigned .= $this->secretKey;

        return strtoupper(md5($stringToBeSigned));
    }

    public function curl($url, $postFields = null)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FAILONERROR, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        if ($this->readTimeout) {
            curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout);
        }
        if ($this->connectTimeout) {
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
        }
        curl_setopt($ch, CURLOPT_USERAGENT, 'hupun-openapi-php-sdk');

        // HTTPS 请求
        if (strlen($url) > 5 && 'https' == strtolower(substr($url, 0, 5))) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }

        if (null != $postFields && is_array($postFields) && 0 < count($postFields)) {
            $postBodyString = '';
            $postMultipart = false;

            foreach ($postFields as $k => $v) {
                if ('@' != substr($v, 0, 1)) {// 判断是不是文件上传
                    $postBodyString .= urlencode($k) . '=' . urlencode($v) . '&';
                } else {// 文件上传用 multipart/form-data，否则用 www-form-urlencoded
                    $postMultipart = true;
                    if (class_exists('\CURLFile')) {
                        $postFields[$k] = new \CURLFile(substr($v, 1), '', '');
                    }
                }
            }
            unset($k, $v);

            curl_setopt($ch, CURLOPT_POST, true);

            if ($postMultipart) {
                if (class_exists('\CURLFile')) {
                    curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
                } else {
                    if (defined('CURLOPT_SAFE_UPLOAD')) {
                        curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
                    }
                }
                curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
            } else {
                $header = ['content-type: application/x-www-form-urlencoded; charset=UTF-8'];
                curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
                curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString, 0, -1));
            }
        }

        $response = curl_exec($ch);

        if (curl_errno($ch)) {
            throw new \Exception(curl_error($ch), 0);
        } else {
            $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            if (200 !== $httpStatusCode) {
                throw new \Exception($response, $httpStatusCode);
            }
        }

        curl_close($ch);

        return $response;
    }

    public function curlWithMemoryFile($url, $postFields = null, $fileFields = null)<commit_msg>Del: 删除不兼容代码<commit_after><?php

namespace Xlstudio\Hupun;

class HupunClient
{
    protected $appKey;

    protected $secretKey;

    protected $gatewayUrl = 'https://erp-open.hupun.com/api';

    protected $format = 'json';

    protected $connectTimeout;

    protected $readTimeout;

    // 日志存放的工作目录
    protected $hupunSdkWorkDir = './data/';

    protected $signMethod = 'md5';

    protected $apiVersion = 'v1';

    protected $sdkVersion = 'hupun-open-api-php-sdk-20190508';

    public function __construct($appKey = '', $secretKey = '', $options = [])
    {
        $this->appKey = trim($appKey);
        $this->secretKey = trim($secretKey);

        if ($options) {
            if ($options['api_url']) {
                $this->gatewayUrl = trim($options['api_url']);
            }
            if ($options['api_work_dir']) {
                $this->hupunSdkWorkDir = $options['api_work_dir'];
            }
        }
    }

    public function setGatewayUrl($gatewayUrl)
    {
        $this->gatewayUrl = trim($gatewayUrl);
    }

    public function setFormat($format)
    {
        $this->format = $format;
    }

    public function setConnectTimeout($connectTimeout)
    {
        $this->connectTimeout = $connectTimeout;
    }

    public function setReadTimeout($readTimeout)
    {
        $this->readTimeout = $readTimeout;
    }

    public function setHupunSdkWorkDir($hupunSdkWorkDir)
    {
        $this->hupunSdkWorkDir = $hupunSdkWorkDir;
    }

    protected function generateSign($params, $isOpen = false)
    {
        ksort($params);

        $stringToBeSigned = $this->secretKey;
        foreach ($params as $k => $v) {
            if ('@' != substr($v, 0, 1)) {
                if ($isOpen) {
                    $stringToBeSigned .= urlencode($k) . '=' . urlencode($v) . '&';
                } else {
                    $stringToBeSigned .= "$k$v";
                }
            }
            unset($k, $v);
        }
        $stringToBeSigned = rtrim($stringToBeSigned, '&');
        $stringToBeSigned .= $this->secretKey;

        return strtoupper(md5($stringToBeSigned));
    }

    public function curl($url, $postFields = null)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FAILONERROR, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        if ($this->readTimeout) {
            curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout);
        }
        if ($this->connectTimeout) {
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
        }
        curl_setopt($ch, CURLOPT_USERAGENT, 'hupun-openapi-php-sdk');

        // HTTPS 请求
        if (strlen($url) > 5 && 'https' == strtolower(substr($url, 0, 5))) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }

        if (null != $postFields && is_array($postFields) && 0 < count($postFields)) {
            $postBodyString = '';
            $postMultipart = false;

            foreach ($postFields as $k => $v) {
                if ('@' != substr($v, 0, 1)) {// 判断是不是文件上传
                    $postBodyString .= urlencode($k) . '=' . urlencode($v) . '&';
                } else {// 文件上传用 multipart/form-data，否则用 www-form-urlencoded
                    $postMultipart = true;
                    if (class_exists('\CURLFile')) {
                        $postFields[$k] = new \CURLFile(substr($v, 1), '', '');
                    }
                }
            }
            unset($k, $v);

            curl_setopt($ch, CURLOPT_POST, true);

            if ($postMultipart) {
                curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
            } else {
                $header = ['content-type: application/x-www-form-urlencoded; charset=UTF-8'];
                curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
                curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString, 0, -1));
            }
        }

        $response = curl_exec($ch);

        if (curl_errno($ch)) {
            throw new \Exception(curl_error($ch), 0);
        } else {
            $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            if (200 !== $httpStatusCode) {
                throw new \Exception($response, $httpStatusCode);
            }
        }

        curl_close($ch);

        return $response;
    }

    public function curlWithMemoryFile($url, $postFields = null, $fileFields = null)
{'fraction_non_alphanumeric': 0.1418744625967326, 'fraction_numerical': 0.005803955288048152, 'mean_word_length': 1.889751552795031, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 39, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5481844', 'n_tokens_mistral': 3046, 'n_tokens_neox': 2888, 'n_words': 489}