-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhelper.func.php
311 lines (261 loc) · 8.73 KB
/
helper.func.php
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<?php
defined('IN_IA') or exit('Access Denied');
//注意使用 load()->func() 导致静态全局变量失效,只能声明常量使用;
defined('CR') or define('CR', '<br>');
defined('HDIVELE') or define('HDIVELE','<div style="border:1px solid #ccc; background:#eee;padding:20px;">');
defined('HPREELE') or define('HPREELE','<pre style="font-size:16px;font-family:微软雅黑;white-space:pre-wrap!important;word-wrap:break-word!important;*white-space:normal!important;">');
defined('HDIVEND') or define('HDIVEND','</div>');
defined('HPREEND') or define('HPREEND','</pre>');
load()->classs("markdown");
/****************************
Common
****************************/
function dump($var, $quit=true, $file=""){
echo HDIVELE . HPREELE;
$varType = gettype($var);
switch($varType){
case 'array':
$var['__count'] = count($var,COUNT_RECURSIVE);
print_r($var);
break;
case 'object':
$className = get_class($var);
$var->__properties = get_object_vars($var);
$var->__methods = get_class_methods($className);
$var->__className = $className;
$var->__parentClassName = get_parent_class($className);
print_r($var);
break;
case 'string':
echo 'string('.strlen($var).') '. CR . htmlspecialchars($var);
break;
default:
echo var_dump($var);
}
if($file) echo "@@ ".$file;
echo HPREEND.HDIVEND;
if($quit) exit;
}
function performanceTest($begin=false){
static $t;
if($begin){
$t = microtime(true);
}else{
echo "耗时: ".round(microtime(true)-$t, 3)."秒<br>";
echo '耗存: '.round(memory_get_usage()/1000000,3).'M<br/>';
$t = 0;
}
}
function clearSession(){
session_start();
$_SESSION = array();
if(isset($_COOKIE[session_name()]))
setcookie(session_name(), '', time()-42000, '/');
session_destroy();
}
/****************************
Database
****************************/
function pdo_showsql($onlyLast = false){
$debug = pdo_debug(0);
if($onlyLast){
$count = count($debug);
$debug = array($debug[$count-1]);
}
foreach($debug as $val){
echo HDIVELE;
$sql = $val['sql'];
$key = $val['params'];
foreach($key as $k => $v){
$sql = str_replace($k, "'$v'", $sql);
}
foreach($key as $k => $v){
$sql .= CR. "[$k] => $v" ;
}
echo $sql. HDIVEND;
}
}
function pdo_setdata($tableName, $update, $condition){
echo HDIVELE;
$prefix = tablename();
if(!preg_match('prefix',$tableName))
$tableName = tablename($tableName);
$res = pdo_update($tableName, $update, $condition);
if($res){
echo "修改成功!<br>";
} else{
echo "修改失败<br>";
pdo_lastsql();
}
echo HDIVEND;
}
/****************************
payment
****************************/
//微信支付文档: https://pay.weixin.qq.com/wiki/doc/api/index.html
//阿里支付文档: https://docs.open.alipay.com/203
//通过UA判断支付方式
function getPaymentPlatform(){
if(isset($_SERVER['HTTP_USER_AGENT'])){
$UA = $_SERVER['HTTP_USER_AGENT'];
if(strstr($UA, 'QQBrowser')) return 'wxpay';
else if(strstr($UA ,'AlipayClient')) return 'alipay';
else return 'unknow';
}
return 'No HTTP_USER_AGENT';
}
//微信下单示例,仅供参考
if (!function_exists(wxpay)){
function wxpay(){
$config['mch_id'] = 'your mch_id';
$config['appid'] = 'your appid';
$config['appsecret'] = 'your appsecret';
$config['key'] = 'your pay key';
$config['notify_url'] = 'http://'. $_SERVER['SERVER_ADDR'].'/notify.php';
$param['body'] = 'your payment title';
$param['total_fee'] = 300;
$param['openid'] = $openid;
$param['out_trade_no'] = $out_trade_no;
//记得配置授权目录,支付目录
load()->classs('mywxpay');
$pay = new Mywxpay($config);
$jsp = $pay->unifiedOrder($param);
include $this->template('wxpay');
exit;
}
}
//阿里下单示例,仅供参考
if (!function_exists(alipay)){
function alipay(){
$config['app_id'] = 'your app_id';
$config['notify_url'] = 'http://'. $_SERVER['SERVER_ADDR'].'/notify_url.php';
$config['return_url'] = 'http://'. $_SERVER['SERVER_ADDR'].'/return_url.php';
$config['alipay_public_key'] = 'MIIBIjANBg...';
$config['merchant_private_key'] = 'MIIEvQIBADANBgkqhkiG9...';
$param['subject'] = 'payment subject';
$param['body'] = 'customer description';
$param['total_amount'] = 300;
$param['out_trade_no'] = date('YmdHis').time();
//执行完自动弹出支付,界面不像微信要自己写
load()->classs('myalipay');
$pay = new Myalipay($config);
$pay->wapPay($param);
}
}
/****************************
Markdown
****************************/
//@param1 __FILE__
//@param2 mysql:root@localhost/database/password
function showHead($filename, $connstr){
$showdoc = new Wdebug($connstr);
$showdoc->AutoMarkdown($filename);
}
function showFoot($result){
$showdoc = new Wdebug;
error_reporting(0);
$showdoc->AutoResult($result);
}
function showComment($tableName, $highLightArr=[]){
$showdoc = new Wdebug;
$showdoc->showComment($tableName, $highLightArr);
}
function showCommentByKey($keyword=""){
$showdoc = new Wdebug;
$showdoc->showCommentByKeyword($keyword);
}
function showGpcTable($arr){
$showdoc = new Wdebug;
$showdoc->MkGpcTable($arr);
}
function showMdTable($arr){
$showdoc = new Wdebug;
$showdoc->MdTable($arr);
}
function makeTestData(){
$showdoc = new Wdebug;
$showdoc->AutoInsertData();
}
/****************************
Netowrk
****************************/
function sendHttpGet($url){
$con = curl_init((string)$url);
curl_setopt($con, CURLOPT_RETURNTRANSFER, true);
curl_setopt($con, CURLOPT_TIMEOUT, 5);
$result = curl_exec($con);
curl_close($con);
return $result;
}
function sendHttpForm($url, $data){
$con = curl_init((string)$url);
if(is_array($data))
$data = http_build_query($data);
curl_setopt($con, CURLOPT_POST, true);
curl_setopt($con, CURLOPT_HTTPHEADER, Array("Content-Type:application/x-www-form-urlencoded; charset=utf-8"));
curl_setopt($con, CURLOPT_POSTFIELDS, $data);
curl_setopt($con, CURLOPT_RETURNTRANSFER, true);
curl_setopt($con, CURLOPT_TIMEOUT, 5);
curl_setopt($con, CURLOPT_VERBOSE, 1);
$result = curl_exec($con);
curl_close($con);
return $result;
}
function sendHttpJson($url, $json){
$con = curl_init((string)$url);
if(gettype($json) === 'array')
$json = json_encode($json);
curl_setopt($con, CURLOPT_POST, true);
curl_setopt($con, CURLOPT_HTTPHEADER, Array("Content-Type:application/json; charset=utf-8"));
curl_setopt($con, CURLOPT_POSTFIELDS, $json);
curl_setopt($con, CURLOPT_RETURNTRANSFER, true);
curl_setopt($con, CURLOPT_TIMEOUT, 5);
curl_setopt($con, CURLOPT_VERBOSE, 1);
$result = curl_exec($con);
curl_close($con);
return $result;
}
function sendHttpXml($url, $xml){
$con = curl_init((string)$url);
if(!stristr($xml,"<xml>"))
return "xml invalid";
curl_setopt($con, CURLOPT_POST, true);
curl_setopt($con, CURLOPT_HTTPHEADER, Array("Content-Type:text/xml; charset=utf-8"));
curl_setopt($con, CURLOPT_POSTFIELDS, $xml);
curl_setopt($con, CURLOPT_RETURNTRANSFER, true);
curl_setopt($con, CURLOPT_TIMEOUT, 5);
curl_setopt($con, CURLOPT_VERBOSE, 1);
$result = curl_exec($con);
curl_close($con);
return $result;
}
function sendHttpDel($url, $headers){
if($ch = curl_init($url)){
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return (int) $status;
}
else
return false;
}
function sendHttpPut($url, $field, $headers){
$fields = (is_array($fields)) ? http_build_query($fields) : $fields;
if($headers)
$headers[] = "Content-Length: ". strlen($fields);
if($ch = curl_init($url)){
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return (int) $status;
}
else
return false;
}