<?php

namespace App\Http\Middleware;

use App\Util;
use Closure;

class EncryptInOut
{
    /**
     * 验证签名 API接口
     * @param \Illuminate\Http\Request $request
     * @param Closure $next
     */
    public function handle($request, Closure $next)
    {
        $path = $request->path();
        // 现在 $path 变量包含了当前请求的路径
        $arr=$request->all();
        $newarr=[];
        foreach ($arr as $value){
//            Util::WriteLog("encrypt",self::decrypt($value,$path));
            $temp=[];
            parse_str(self::decrypt($value,$path),$temp);
            $newarr=array_merge($newarr,$temp);
            break;
        }
        $request->replace($newarr);

        $response = $next($request);

        // 对返回内容进行加密处理
        $content = $response->getContent();

        $encryptedContent = self::encrypt($content,$path);

        // 将加密后的内容设置回响应
        $response->setContent($encryptedContent);
//        $response=$response->header('Access-Control-Allow-Origin', '*')
//            ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
        return $response;
    }


    public static function encrypt($str,$pwd){
        try {
            if (empty($str) || empty($pwd)) {
                # code...
                return '';
            }
            $pwd = urlencode(iconv("gbk", "UTF-8", $pwd));
            $str = urlencode(iconv("gbk", "UTF-8", $str));
            $prand = '';
            for ($i = 0; $i < strlen($pwd); $i++) {
                $prand = $prand . ord(mb_substr($pwd, $i, 1));
            }
            $sPos = floor(strlen($prand) / 5);
            $mult = intval(mb_substr($prand, $sPos, 1) . mb_substr($prand, $sPos * 2, 1) . mb_substr($prand, $sPos * 3, 1) . mb_substr($prand, $sPos * 4, 1) . mb_substr($prand, $sPos * 5, 1));
            $incr = round(strlen($pwd) / 2);
            $modu = pow(2, 31) - 1;
            if (strlen($mult) < 2) {
                # code...
                return '';
            }
            $salt = round(rand(0, 1000000000)) % 100000000;
            $prand = $prand . $salt;
            while (strlen($prand) > 10) {
                # code...
                $left = $prand;
                $num = 0;
                while (strlen($left) > 0) {
                    # code...
                    $temp = intval(mb_substr($left, 0, 10));
                    $num = $num + $temp;
                    $left = mb_substr($left, 10);
                }
                $prand = $num . '';
            }
            $prand = ($mult * intval($prand) + $incr) % $modu;
            $encStr = '';
            for ($i = 0; $i < strlen($str); $i++) {
                $encChr = intval(ord(mb_substr($str, $i, 1)) ^ floor(($prand / $modu) * 255));
                if ($encChr < 16) {
                    # code...
                    $encStr = $encStr . '0' . dechex($encChr);
                } else {
                    $encStr = $encStr . dechex($encChr);
                }
                $prand = ($mult * intval($prand) + $incr) % $modu;
            }
            $salt = dechex($salt);
            while (strlen($salt) < 8) {
                # code...
                $salt = '0' . $salt;
            }
            $encStr = $encStr . $salt;
        }catch (\Exception $exception){
            Util::WriteLog('encrypt',[http_response_code()]);
            $encStr=$str;
        }
        return $encStr;
    }
    public static function decrypt($str, $pwd){
        if (empty($str)||empty($pwd)) {
            # code...
            return '';
        }
        if (strlen($str)<8||strlen($pwd)<=0) {
            # code...
            return '';
        }
        $pwd=urlencode(iconv("gbk", "UTF-8", $pwd));
        $prand='';
        for ($i=0; $i <strlen($pwd); $i++) {
            $prand=$prand.ord(mb_substr($pwd, $i, 1));
        }
        $sPos=floor(strlen($prand)/5);
        $mult=intval(mb_substr($prand,$sPos,1).mb_substr($prand,$sPos*2,1).mb_substr($prand,$sPos*3,1).mb_substr($prand,$sPos*4,1).mb_substr($prand,$sPos*5,1));
        $incr=round(strlen($pwd)/2);
        $modu=pow(2, 31)-1;
        $salt=hexdec(mb_substr($str,strlen($str)-8));
        $str=mb_substr($str,0,strlen($str)-8);
        $prand=$prand.$salt;
        while (strlen($prand)>10) {
            # code...
            $left=$prand;
            $num=0;

            while (strlen($left)>0) {
                # code...
                $temp=intval(mb_substr($left, 0,10));
                $num=$num+$temp;
                $left=mb_substr($left, 10);
            }
            $prand=$num.'';
        }
        $prand=($mult*intval($prand)+$incr)%$modu;
        $encStr='';
        for ($i=0; $i <strlen($str) ; $i+=2) {
            # code...
            $encChr=intval(hexdec(mb_substr($str, $i,2))^floor(($prand/$modu)*255));
            $encStr=$encStr.chr($encChr);
            $prand=($mult*intval($prand)+$incr)%$modu;
        }
        return urldecode($encStr);
    }
}
