common.php
4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
// 初始化日志
use WebPlugin\PhpLog;
$log = new PhpLog (SDK_LOG_FILE_PATH, "PRC", SDK_LOG_LEVEL);
/**
* 数组 排序后转化为字体串
*
* @param array $params
* @return string
*/
function coverParamsToString($params)
{
$sign_str = '';
// 排序
ksort($params);
foreach ($params as $key => $val) {
if ($key == 'signature') {
continue;
}
$sign_str .= sprintf("%s=%s&", $key, $val);
}
return substr($sign_str, 0, strlen($sign_str) - 1);
}
/**
* 字符串转换为 数组
*
* @param unknown_type $str
* @return multitype:unknown
*/
function coverStringToArray($str)
{
$result = array();
if (!empty ($str)) {
$temp = preg_split('/&/', $str);
if (!empty ($temp)) {
foreach ($temp as $key => $val) {
$arr = preg_split('/=/', $val, 2);
if (!empty ($arr)) {
$k = $arr ['0'];
$v = $arr ['1'];
$result [$k] = $v;
}
}
}
}
return $result;
}
/**
* 处理返回报文 解码客户信息 , 如果编码为utf-8 则转为utf-8
*
* @param unknown_type $params
*/
function deal_params(&$params)
{
/**
* 解码 customerInfo
*/
if (!empty ($params ['customerInfo'])) {
$params ['customerInfo'] = base64_decode($params ['customerInfo']);
}
if (!empty ($params ['encoding']) && strtoupper($params ['encoding']) == 'utf-8') {
foreach ($params as $key => $val) {
$params [$key] = iconv('utf-8', 'UTF-8', $val);
}
}
}
/**
* 压缩文件 对应java deflate
*
* @param unknown_type $params
*/
function deflate_file(&$params)
{
global $log;
foreach ($_FILES as $file) {
$log->LogInfo("---------处理文件---------");
if (file_exists($file ['tmp_name'])) {
$params ['fileName'] = $file ['name'];
$file_content = file_get_contents($file ['tmp_name']);
$file_content_deflate = gzcompress($file_content);
$params ['fileContent'] = base64_encode($file_content_deflate);
$log->LogInfo("压缩后文件内容为>" . base64_encode($file_content_deflate));
} else {
$log->LogInfo(">>>>文件上传失败<<<<<");
}
}
}
/**
* 处理报文中的文件
*
* @param unknown_type $params
*/
function deal_file($params)
{
global $log;
if (isset ($params ['fileContent'])) {
$log->LogInfo("---------处理后台报文返回的文件---------");
$fileContent = $params ['fileContent'];
if (empty ($fileContent)) {
$log->LogInfo('文件内容为空');
} else {
// 文件内容 解压缩
$content = gzuncompress(base64_decode($fileContent));
$root = SDK_FILE_DOWN_PATH;
$filePath = null;
if (empty ($params ['fileName'])) {
$log->LogInfo("文件名为空");
$filePath = $root . $params ['merId'] . '_' . $params ['batchNo'] . '_' . $params ['txnTime'] . 'txt';
} else {
$filePath = $root . $params ['fileName'];
}
$handle = fopen($filePath, "w+");
if (!is_writable($filePath)) {
$log->LogInfo("文件:" . $filePath . "不可写,请检查!");
} else {
file_put_contents($filePath, $content);
$log->LogInfo("文件位置 >:" . $filePath);
}
fclose($handle);
}
}
}
/**
* 构造自动提交表单
*
* @param unknown_type $params
* @param unknown_type $action
* @return string
*/
function create_html($params, $action)
{
$encodeType = isset ($params ['encoding']) ? $params ['encoding'] : 'UTF-8';
$html = <<<eot
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset={$encodeType}" />
</head>
<body onload="javascript:document.pay_form.submit();">
<form id="pay_form" name="pay_form" action="{$action}" method="post">
eot;
foreach ($params as $key => $value) {
$html .= " <input type=\"hidden\" name=\"{$key}\" id=\"{$key}\" value=\"{$value}\" />\n";
}
$html .= <<<eot
<input type="submit" type="hidden">
</form>
</body>
</html>
eot;
return $html;
}
?>