WeCenter在php7环境下,公众号功能无反应、出错解决方案

君笑尘
君笑尘 这家伙很懒,还没有设置简介

0 人点赞了该文章 · 727 浏览

出错原因

出现这个问题是由于mcrypt_decrypt、等函数已自 PHP 7.1.0 起废弃,并将自 PHP 7.2.0 起移除

但是由于wecenter3.6.1以及之前的版本还在使用mcrypt_decrypt等函数,所以会导致在php7.2 + 的环境下,wecenter公众号功能会出现错误,导致无法使用公众号回复功能。


解决方案

用openssl_decrypt替换mcrypt_decrypt等函数

打开system/Services/Weixin/Prpcrypt.php,

找到encrypt函数,替换为

    /**
     * 对明文进行加密
     * @param string $text 需要加密的明文
     * @return string 加密后的密文
     */
    public function encrypt($text, $appid)
    {
        try {
            //获得16位随机字符串,填充到明文之前
            $random = $this->getRandomStr();
            $text = $random . pack("N", strlen($text)) . $text . $appid;

            $iv = substr($this->key, 0, 16);
            //使用自定义的填充方式对明文进行补位填充
            $pkc_encoder = new Services_Weixin_PKCS7Encoder;
            $text = $pkc_encoder->encode($text);

            $encrypted = openssl_encrypt($text, 'AES-256-CBC', $this->key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $this->hexToStr($iv));

            //使用BASE64对加密后的字符串进行编码
            return array(Services_Weixin_ErrorCode::$OK, base64_encode($encrypted));

        } catch (Exception $e) {
            //print $e;
            return array(Services_Weixin_ErrorCode::$EncryptAESError, null);
        }
    }

找到decrypt函数,替换为

    /**
     * 对密文进行解密
     * @param string $encrypted 需要解密的密文
     * @return string 解密得到的明文
     */
    public function decrypt($encrypted, $appid)
    {

        try {
            //使用BASE64对需要解密的字符串进行解码
            $ciphertext_dec = base64_decode($encrypted);
            $iv = substr($this->key, 0, 16);

            //解密
            $decrypted = openssl_decrypt($ciphertext_dec, 'AES-256-CBC', $this->key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $this->hexToStr($iv));

        } catch (Exception $e) {
            return array(Services_Weixin_ErrorCode::$DecryptAESError, null);
        }


        try {
            //去除补位字符
            $pkc_encoder = new Services_Weixin_PKCS7Encoder;
            $result = $pkc_encoder->decode($decrypted);
            //去除16位随机字符串,网络字节序和AppId
            if (strlen($result) < 16)
                return "";
            $content = substr($result, 16, strlen($result));
            $len_list = unpack("N", substr($content, 0, 4));
            $xml_len = $len_list[1];
            $xml_content = substr($content, 4, $xml_len);
            $from_appid = substr($content, $xml_len + 4);
            if (!$appid)
                $appid = $from_appid;
        } catch (Exception $e) {
            //print $e;
            return array(Services_Weixin_ErrorCode::$IllegalBuffer, null);
        }
        if ($from_appid != $appid)
            return array(Services_Weixin_ErrorCode::$ValidateAppidError, null);
        return array(0, $xml_content, $appid);
    }
 

同时在下方增加一个自定义函数hexToStr

    public function hexToStr($hex)
    {
        $string='';
        for ($i=0; $i < strlen($hex)-1; $i+=2)
        {
            $string .= chr(hexdec($hex[$i].$hex[$i+1]));
        }
        return $string;
    }


下载修改后的文件

这里给大家整理好了修改后的文件,

可以下载后覆盖到自己网站system/Services/Weixin/Prpcrypt.php即可在php7环境下使用WeCenter的公众号功能了

Prpcrypt.php.zip

发布于 2023-02-07 20:40

免责声明:

本文由 君笑尘 原创发布于 君笑尘编程学习社区 ,著作权归作者所有。

登录一下,更多精彩内容等你发现,贡献精彩回答,参与评论互动

登录! 还没有账号?去注册

暂无评论

All Rights Reserved Powered BY WeCenter V4.1.0 © 2024 粤ICP备2020123311号