<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE);
/*
* 外部编程接口处理标签内容示范文件
*/
class Its_test {
    function tocurl($url, $header, $content) {
        $ch = curl_init();
        if(substr($url, 0, 5) == 'https') {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($ch, CURLOPT_SSLVERSION, 1);
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        if (is_array($header)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        curl_setopt($ch, CURLOPT_POST, true);
        if (!empty($content)) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($content) ? http_build_query($content) : $content);
        }
        $response = curl_exec($ch);
        $error = curl_error($ch);
        if ($error) {
            die($error);
        }
        curl_close($ch);
        return $response;
    }

    function xfyun_translate($text) {
        $app_id = "4d38";
        $api_sec = "MjMxNGRiOThkMmJlNDEzODA2";
        $api_key = "f9406a1adc3a8e369ce08113fe";
        $url = "https://itrans.xfyun.cn/v2/its";

        $body = json_encode($this->getBody($app_id, "cn", "en", $text));
        $date = gmdate('D, d M Y H:i:s') . ' GMT';
        $digestBase64 = "SHA-256=" . base64_encode(hash("sha256", $body, true));
        $builder = sprintf("host: %s\ndate: %s\nPOST /v2/its HTTP/1.1\ndigest: %s", "itrans.xfyun.cn", $date, $digestBase64);
        $sha = base64_encode(hash_hmac("sha256", $builder, $api_sec, true));
        $authorization = sprintf("api_key=\"%s\", algorithm=\"%s\", headers=\"%s\", signature=\"%s\"", $api_key, "hmac-sha256", "host date request-line digest", $sha);
        
        $header = array(
    "Authorization: " . $authorization,
    'Content-Type: application/json',
    'Accept: application/json,version=1.0',
    'Host: itrans.xfyun.cn',
    'Date: ' . $date,
    'Digest: ' . $digestBase64
);

        $response = $this->tocurl($url, $header, $body);
        $result = json_decode($response, true);
        
        return $result['data']['result']['trans_result']['dst']; // Return the translated text
    }

    function getBody($app_id, $from, $to, $text) {
    return array(
        'common' => array('app_id' => $app_id),
        'business' => array('from' => $from, 'to' => $to),
        'data' => array('text' => base64_encode($text))
    );
}
}

$translator = new Its_test(); // Initialize translator class

// 处理标签数组
if ($LabelArray['PageType'] == "List") {
    $LabelArray['Html'] = '当前页面的网址为:' . $LabelUrl . "\r\n页面类型为:" . $LabelArray['PageType'] . "\r\n接收到的数据是:" . $LabelArray['Html'];
} else if ($LabelArray['PageType'] == "Content") {
    $LabelArray['Html'] = '当前页面的网址为:' . $LabelUrl . "\r\n页面类型为:" . $LabelArray['PageType'] . "\r\n接收到的数据是:" . $LabelArray['Html'];
} else if ($LabelArray['PageType'] == "Save") {
    // 翻译标题和内容
    $LabelArray['内容'] = $translator->xfyun_translate($LabelArray['内容']);
    $LabelArray['标题'] = $translator->xfyun_translate($LabelArray['标题']);

    // 处理内容
    $LabelArray['内容'] = $LabelArray['内容'];
    $LabelArray['内容'] = str_replace('旧字符串', '新字符串', $LabelArray['内容']);
    $LabelArray['标题'] = $LabelArray['标题'];
    $LabelArray['时间'] = date('Y-m-d H:i:s', time());
}

// 输出结果
echo serialize($LabelArray);
?>
