PHP实现Java的decryptWithUnzipFromBase64

protected static $appSecret = 'xxxxxxx';
$str='xxxxxx';
$secret=base64_encode(self::pbkdf2(self::$appSecret,hash('md5',self::$appSecret,true),65536,16));
$res=self::decryptWithUnzipFromBase64($str,base64_decode($secret),hash('md5',self::$appSecret,true));
/**
 * pbkdf2解密
 * @param $password //待解密数据
 * @param $salt //盐
 * @param $iterations //迭代次数
 * @param $keyLength //数据长度
 * @return false|string
 */
public static function pbkdf2($password, $salt, $iterations, $keyLength)
{
    $blockSize  = 64;
    $hashLength = 20;
    $numBlocks  = ceil($keyLength / $hashLength);
    $output = '';
    for ($i = 1; $i <= $numBlocks; $i++) {
        $lastResult = $block = hash_hmac('sha1', $salt . pack('N', $i), $password, true);
        for ($j = 1; $j < $iterations; $j++) {
            $block      = hash_hmac('sha1', $block, $password, true);
            $lastResult ^= $block;
        }
        $output .= $lastResult;
    }
    return substr($output, 0, $keyLength);
}
/**
 * base64数据解压
 * @param $base64Data //待解压base64数据
 * @param $key //秘钥
 * @param $iv //向量
 * @return false|string
 */
public static function decryptWithUnzipFromBase64($base64Data, $key, $iv)
{
    // 解码 Base64 字符串中的数据
    $decodedData = base64_decode($base64Data);
    // 使用 AES-128-CBC 算法解密数据
    $decryptedData = openssl_decrypt($decodedData, 'AES-128-CBC', $key, 1, $iv);
    // 解压数据
    $uncompressedData = gzuncompress($decryptedData);
    // 返回解压后的数据
    return $uncompressedData;
}
2023-12-22

ThinkPHP5.0将数据库从MySQL迁移到达梦数据库

电脑系统:Windows
达梦数据库:DM8
在thinkphp/library/think/db/builder中新建Dm.php

namespace think\db\builder;
use think\db\Builder;
use think\db\Query;
class Dm extends Builder
{
    protected $selectSql = 'SELECT * FROM (SELECT thinkphp.*, rownum AS numrow FROM (SELECT  %DISTINCT% %FIELD% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%) thinkphp ) %LIMIT%%COMMENT%';
    /**
     * limit分析
     * @access protected
     * @param Query $query 查询对象
     * @param mixed $limit
     * @return string
     */
    protected function parseLimit($limit)
    {
        $limitStr = '';
        if (!empty($limit)) {
            $limit = explode(',', $limit);

            if (count($limit) > 1) {
                $limitStr = "(numrow>" . $limit[0] . ") AND (numrow<=" . ($limit[0] + $limit[1]) . ")";
            } else {
                $limitStr = "(numrow>0 AND numrow<=" . $limit[0] . ")";
            }
        }
        return $limitStr ? ' WHERE ' . $limitStr : '';
    }
    /**
     * 设置锁机制
     * @access protected
     * @param Query $query 查询对象
     * @param bool|false $lock
     * @return string
     */
    protected function parseLock($lock = false)
    {
        if (!$lock) {
            return '';
        }
        return ' FOR UPDATE NOWAIT ';
    }
    /**
     * 字段和表名处理
     * @access public
     * @param Query $query 查询对象
     * @param string $key
     * @param string $strict
     * @return string
     */
    public function parseKey($key, $options = [], $strict = false)
    {
        if (strpos($key, '->') && false === strpos($key, '(')) {
            // JSON字段支持
            list($field, $name) = explode($key, '->');
            $key = '"' . $field . '"' . '."' . $name . '"';
        }
        return $key;
    }
    /**
     * 随机排序
     * @access protected
     * @param Query $query 查询对象
     * @return string
     */
    protected function parseRand()
    {
        return 'DBMS_RANDOM.value';
    }
}

在thinkphp/library/think/db/connector中新建Dm.php

namespace think\db\connector;
use PDO;
use think\db\Connection;
class Dm extends Connection
{
    protected $builder = '\\think\\db\\builder\\Dm';
    /**
     * 解析pdo连接的dsn信息
     * @access protected
     * @param array $config 连接信息
     * @return string
     */
    protected function parseDsn($config)
    {
        $dsn = 'dm:host=';
        if (!empty($config['hostname'])) {
            $dsn .= $config['hostname'] . ($config['hostport'] ? ':' . $config['hostport'] : '');
        }
        return $dsn;
    }
    /**
     * 取得数据表的字段信息
     * @access public
     * @param string $tableName
     * @return array
     */
    public function getFields($tableName)
    {
        list($tableName) = explode(' ', $tableName);
        $sql = "select a.column_name,data_type,DECODE (nullable, 'Y', 0, 1) notnull,data_default, DECODE (A .column_name,b.column_name,1,0) pk from all_tab_columns a,(select column_name from all_constraints c, all_cons_columns col where c.constraint_name = col.constraint_name and c.constraint_type = 'P' and c.table_name = '" . strtoupper($tableName) . "' ) b where table_name = '" . strtoupper($tableName) . "' and a.column_name = b.column_name (+)";
        $pdo    = $this->query($sql, [], false, true);
        $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
        $info   = [];
        if ($result) {
            foreach ($result as $key => $val) {
                $val                       = array_change_key_case($val);
                $info[$val['column_name']] = [
                    'name'    => $val['column_name'],
                    'type'    => $val['data_type'],
                    'notnull' => $val['notnull'],
                    'default' => $val['data_default'],
                    'primary' => $val['pk'],
                    'autoinc' => $val['pk'],
                ];
            }
        }
        return $this->fieldCase($info);
    }
    /**
     * 取得数据库的表信息(暂时实现取得用户表信息)
     * @access   public
     * @param string $dbName
     * @return array
     */
    public function getTables($dbName = '')
    {
        $sql    = 'select table_name from all_tables';
        $pdo    = $this->query($sql, [], false, true);
        $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
        $info   = [];
        foreach ($result as $key => $val) {
            $info[$key] = current($val);
        }
        return $info;
    }
    /**
     * SQL性能分析
     * @access protected
     * @param string $sql
     * @return array
     */
    protected function getExplain($sql)
    {
        return [];
    }
    protected function supportSavepoint()
    {
        return true;
    }
}

修改application/database.php

use think\Env;
return [
    // 数据库类型
    'type'            => 'dm',
    // 服务器地址
    'hostname'        => '127.0.0.1',
    // 数据库名
    'database'        => 'MAIN',
    // 用户名
    'username'        => 'SYSDBA',
    // 密码
    'password'        => 'SYSDBA001',
    // 端口
    'hostport'        => '5236',
    // 连接dsn
    'dsn'             => '',
    // 数据库连接参数
    'params'          => [
        \PDO::ATTR_PERSISTENT   => true,
        \PDO::ATTR_CASE         => \PDO::CASE_LOWER
    ],
    // 数据库编码默认采用 utf8mb4
    'charset'         => 'UTF-8',
    // 数据库表前缀
    'prefix'          => '表前缀',
    // 数据库调试模式
    'debug'           => false,
    // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
    'deploy'          => 0,
    // 数据库读写是否分离 主从式有效
    'rw_separate'     => false,
    // 读写分离后 主服务器数量
    'master_num'      => 1,
    // 指定从服务器序号
    'slave_no'        => '',
    // 是否严格检查字段是否存在
    'fields_strict'   => true,
    // 数据集返回类型
    'resultset_type'  => 'array',
    // 自动写入时间戳字段
    'auto_timestamp'  => false,
    // 时间字段取出后的默认时间格式,默认为Y-m-d H:i:s
    'datetime_format' => false,
    // 是否需要进行SQL性能分析
    'sql_explain'     => false,
    // Builder类
    'builder'         => '',
    // Query类
    'query'           => '\\think\\db\\Query',
    // 断线标识字符串
    'break_match_str' => []
];

在C:\Windows\System32新建dm_svc.conf,然后重启电脑

TIME_ZONE=(480)
LANGUAGE=(cn)
CHAR_CODE=(PG_UTF8)

使用DM数据库配置助手新建数据库时在初始化参数这一步要将字符集设置为UTF-8
1703234277647.jpg
使用DM数据库迁移工具时数据源勾选自定义url,内容为jdbc:mysql://192.168.1.250:3306/数据库名称?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
1703233959631.jpg

2023-12-22

ThinkPHP5.0使用区块链服务网络(BSN)

BSN平台类型:Fabric-2.3.2-secp256r1
创建一个区块链上链信息表保存上链记录

CREATE TABLE `bl_block_list` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `order_no` varchar(50) DEFAULT '' COMMENT '订单编号',
  `txId` varchar(100) DEFAULT '' COMMENT '区块链交易id',
  `block_hash` varchar(100) DEFAULT '' COMMENT '区块链块hash',
  `block_num` int(10) DEFAULT NULL COMMENT '区块链块号',
  `create_name` varchar(100) DEFAULT '' COMMENT '上链用户名',
  `block_time` int(10) DEFAULT NULL COMMENT '区块链交易时间',
  `createtime` datetime DEFAULT NULL COMMENT '创建时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `txId` (`txId`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='区块链上链信息';

下载BSN代码压缩包BSN.zip然后将extend\venyuanbsn放到根目录extend文件夹中
在api/controller中创建BSN.php

<?php
namespace app\api\controller;

use app\common\controller\Api;
use think\Exception;
use think\Log;
use think\Request;
use venyuanbsn\Config;
use venyuanbsn\NodeServer;
use venyuanbsn\SendHelper;

class Bsn extends Api{
    protected $config;

    public function _initialize()
    {
        $this->config = Config::initConfig([
            'reqUrl' => 'https://xxxx.bsngate.com:17602',//证书节点地址
            'appInfo' => [
                'AppCode' => 'xxxxxxxx'//证书的appcode
            ],
            'userCode' => 'xxxxxx',//身份code
            'mspDir' => './mspdir',
            'httpsCert' => ''
        ]);
        parent::_initialize();
    }

    public function registUser()
    {
        $node = new NodeServer($this->config);
        try {
            $res = $node->RegisterUser([
                'name' => 'xxxx',//姓名
                'secret' => 'xxxx'//密码
            ]);
            $this->success('success',$res,0);
        } catch (Exception $e) {
            Log::error('错误:'.$e->getMessage().'行:'.$e->getLine());
            $this->error('错误:'.$e->getMessage(),[],-1);
        }
    }

    /**
     * 保存区块链数据 
     */
    public function reqChainCode()
    {
        try {
            $baseKey = 'PR202202251002';//订单id
            $data = [
                'order_no' => 'xxxxx',
                'appoint_time' => '2023-12-16 00:00:00',
                'goods_list' => []
            ];//待保存的数据
            $node = new NodeServer($this->config);
            $value = base64_encode(json_encode($data,JSON_UNESCAPED_UNICODE));
            $res = $node->ReqChainCode([
                'args' => ['{"baseKey":"'.$baseKey.'","baseValue":"'.$value.'"}'],
                'nonce' => SendHelper::generateRandomString(),
                'chainCode' => 'cc_app0001202203021319180253003_01',
                'funcName' => 'set',
                'userName' => '',
                'transientData' => null
            ]);
            $header = $res->header;
            if ($header->code == -1) {
                Log::error('保存区块链数据失败:'.$header->msg);
                $this->error('保存区块链数据失败',[],-1);
            }
            return $res->body->blockInfo->txId;
        } catch (Exception $e) {
            Log::error('调用保存区块链数据接口失败:'.$e->getMessage().'行:'.$e->getLine());
            $this->error('调用保存区块链数据接口失败:'.$e->getMessage(),[],-1);
        }
    }

    /**
     * 更新区块链数据
     */
    public function upChainCode()
    {
        try {
            $baseKey = 'PR202202251002';//订单编号
            $data = [
                'order_no' => 'xxxx',
                'appoint_time' => '2023-12-17 00:00:00',
                'stock_in_data': []
            ];//待更新的数据
            $node = new NodeServer($this->config);
            $value = base64_encode(json_encode($data,JSON_UNESCAPED_UNICODE));
            $res = $node->ReqChainCode([
                'args' => ['{"baseKey":"'.$baseKey.'","baseValue":"'.$value.'"}'],
                'nonce' => SendHelper::generateRandomString(),
                'chainCode' => 'cc_app0001202203021319180253003_01',
                'funcName' => 'update',
                'userName' => '',
                'transientData' => null
            ]);
            $header = $res->header;
            if ($header->code == -1) {
                Log::error('更新区块链数据失败:'.$header->msg);
                $this->error('更新区块链数据失败',[],-1);
            }
            return $res->body->blockInfo->txId;

        } catch (Exception $e) {
            Log::error('调用更新区块链数据接口失败:'.$e->getMessage().'行:'.$e->getLine());
            $this->error('调用更新区块链数据接口失败:'.$e->getMessage(),[],-1);
        }
    }

    /**
     * 获取区块链上传数据
     */
    public function getChainCode()
    {        
        try {
            $baseKey = 'PR202202251002';//订单id
            $node = new NodeServer($this->config);
            $res = $node->ReqChainCode([
                'args' => [$baseKey],
                'nonce' => SendHelper::generateRandomString(),
                'chainCode' => 'cc_app0001202203021319180253003_01',
                'funcName' => 'get',
                'userName' => '',
                'transientData' => null
            ]);
            $header = $res->header;
            if ($header->code == -1) {
                Log::error('获取区块链数据失败:'.$header->msg);
                $this->error('获取区块链数据失败',[],-1);
            }
            $data = $res->body->ccRes->ccData;
            $data = json_decode(base64_decode($data),TRUE);

            $this->success('success',$data,0);
        } catch (Exception $e) {
            Log::error('调用获取区块链数据接口失败:'.$e->getMessage().'行:'.$e->getLine());
            $this->error('调用获取区块链数据接口失败:'.$e->getMessage(),[],-1);
        }
    }

    /**
     * 获取区块链历史数据
     */
    public function getHistoryChainCode()
    {        
        try {
            $baseKey = 'PR202202251002';//订单id
            $node = new NodeServer($this->config);
            $res = $node->ReqChainCode([
                'args' => [$baseKey],
                'nonce' => SendHelper::generateRandomString(),
                'chainCode' => 'cc_app0001202203021319180253003_01',
                'funcName' => 'getHistory',
                'userName' => '',
                'transientData' => null
            ]);
            $header = $res->header;
            if ($header->code == -1) {
                Log::error('获取区块链数据失败:'.$header->msg);
                $this->error('获取区块链数据失败',[],-1);
            }
            $data = $res->body->ccRes->ccData;
            $data = json_decode($data,TRUE);
            $history = [];
            foreach ($data as $item) {
                $txId = $item['txId'];
                array_push($history,$txId);
            }
            $this->success('success',$history,0);
        } catch (Exception $e) {
            Log::error('调用获取区块链数据接口失败:'.$e->getMessage().'行:'.$e->getLine());
            $this->error('调用获取区块链数据接口失败:'.$e->getMessage(),[],-1);
        }
    }

    /**
     * 获取区块链交易信息
     * @param $txId //区块链交易id
     */
    public function getTransaction()
    {
        $txId = 'xxxxxx';//区块链交易id
        $node = new NodeServer($this->config);
        try {
            $res = $node->GetTransaction([
                'txId' => $txId
            ]);
            $header = $res->header;
            if ($header->code == -1) {
                Log::error('获取区块链数据失败:'.$header->msg);
                $this->error('获取区块链数据失败',[],-1);
            }
            return $res->body;
        } catch (Exception $e) {
            Log::error('错误:'.$e->getMessage().'行:'.$e->getLine());
            $this->error('错误:'.$e->getMessage(),[],-1);
        }
    }

    /**
     * 获取区块链交易数据
     */
    public function getTransdata()
    {
        $txId = 'xxxxxx';//区块链交易id
        $node = new NodeServer($this->config);
        try {
            $res = $node->GetTransdata([
                'txId' => $txId
            ]);
            $header = $res->header;
            if ($header->code == -1) {
                Log::error('获取区块链数据失败:'.$header->msg);
                $this->error('获取区块链数据失败',[],-1);
            }
            $res = $res->body;
            $this->success('success',$res,0);
        } catch (Exception $e) {
            Log::error('错误:'.$e->getMessage().'行:'.$e->getLine());
            $this->error('错误:'.$e->getMessage(),[],-1);
        }
    }

    /**
     * 获取区块链块信息
     */
    public function getBlockInfo()
    {
        $txId = 'xxxxxxxxx';//区块链交易id
        $node = new NodeServer($this->config);
        try {
            $res = $node->GetBlockInfo([
                'blockNumber' => 0,
                'blockHash' => '',
                'txId' => $txId
            ]);
            $header = $res->header;
            if ($header->code == -1) {
                Log::error('获取区块链块信息失败:'.$header->msg);
                $this->error('获取区块链块信息失败',[],-1);
            }
            $res = $res->body;
            $this->success('success',$res,0);
        } catch (Exception $e) {
            Log::error('调用获取区块链块信息失败:'.$e->getMessage().'行:'.$e->getLine());
            $this->error('调用获取区块链块信息失败:'.$e->getMessage(),[],-1);
        }
    }

    /**
     * 获取区块链块数据
     */
    public function getBlockData()
    {
        $txId = 'xxxxxxxx';//区块链交易id
        $node = new NodeServer($this->config);
        try {
            $res = $node->GetBlockData([
                'blockNumber' => 0,
                'blockHash' => '',
                'txId' => $txId,
                'dataType' => ''
            ]);
            $header = $res->header;
            if ($header->code == -1) {
                Log::error('获取区块链块数据失败:'.$header->msg);
                $this->error('获取区块链块数据失败',[],-1);
            }
            $res = $res->body;
            $this->success('success',$res,0);
        } catch (Exception $e) {
            Log::error('调用获取区块链块数据失败:'.$e->getMessage().'行:'.$e->getLine());
            $this->error('调用获取区块链块数据失败:'.$e->getMessage(),[],-1);
        }
    }

    /**
     * 获取区块链最新账本信息
     */
    public function getLedgerInfo()
    {
        $node = new NodeServer($this->config);
        try {
            $res = $node->GetLedgerInfo();
            $header = $res->header;
            if ($header->code == -1) {
                Log::error('获取区块链最新账本信息失败:'.$header->msg);
                $this->error('获取区块链最新账本信息失败',[],-1);
            }
            $res = $res->body;
            $this->success('success',$res,0);
        } catch (Exception $e) {
            Log::error('调用获取区块链最新账本信息失败:'.$e->getMessage().'行:'.$e->getLine());
            $this->error('调用获取区块链最新账本信息失败:'.$e->getMessage(),[],-1);
        }
    }
}

使用

public function addBlockInfo($order_no,$trace_info)
{
    $bsn = new Bsn();
    $block_info = db('block_list')
        ->field('id')
        ->where('order_no',$order_no)
        ->order('createtime','desc')
        ->find();
     if ($block_info) {
         $res = $bsn->upChainCode($order_no,$trace_info);
     } else {
         $res = $bsn->reqChainCode($order_no,$trace_info);
     }
     $txId = $res;
     $trans = $bsn->getTransaction($txId);
     $block_data = [
         'order_no' => $order_no,
         'txId' => $txId,
         'block_hash' => $trans->blockHash,
         'block_num' => $trans->blockNumber,
         'create_name' => $trans->createName,
         'block_time' => $trans->timeSpanSec,
         'createtime' => date('Y-m-d H:i:s')
      ];
      db('block_list')->insert($block_data);
}
2023-12-22

FILE

FILE

文件目录

一、概述

用途:
- 网站静态化
- 记录日志

二、应用

 1、PHP对于文件系统的概述
       文件的分类
       - dir 目录
       - file 文件
       - block  块文件 
       - char   字符型文件
       - fifo   管道类型文件
       - link   链接文件
       - unknow 未知类型

2017-09-30

GD

GD

一、概述

PHP用于处理动态图片。
用途:
- 验证码
- 水印
- 图片的缩放和剪切
- 动态图表 

2017-09-30