common.php
5.8 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
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
<?php
// 应用公共文件
function error($msg ='',$data = []){
return json(['msg'=>$msg,'status'=>'error','data'=>$data]);
}
function success($msg ='',$data = []){
return json(['msg'=>$msg,'status'=>'success','data'=>$data]);
}
/**
* curl 获取指定长度($len)字符串
* @param int $len 长度
* @param int $index 开始位置
* @param string $baseStr 基础字符串
* @return string 返回内容
**/
function getRandStr(int $len,int $index = 0,string $baseStr=""){
$chars = array(
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'0','1','2','3','4','5','6','7','8','9'
);
$charsLen = count($chars) - 1;
shuffle($chars);
$output = $baseStr.base_convert($index, 10, 36);
for ($i = 0; $i < $len; $i++) {
$output .= $chars[mt_rand(0, $charsLen)];
}
return $output;
}
/**
* curl get 请求
* @param string $url 基于的baseUrl
* @param int $flag 标志位, 是否验证ssl
* @return string 返回的资源内容
**/
function curlGet($url, $flag = 0){
$ch = curl_init();
if(! $flag) curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
curl_close($ch);
//-------请求为空
if(empty($response)){
return null;
}
return $response;
}
/**
* curl post 请求
* @param string $url 基于的baseUrl
* @param array $data 请求的参数列表
* @param int $flag 标志位, 是否验证ssl
* @return string 返回的资源内容
**/
function curlPost($url, $data, $flag = 0){
$ch = curl_init();
if(! $flag) curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
// curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_URL, $url);
$ret = curl_exec($ch);
curl_close($ch);
return $ret;
}
/**
* 去表情符
* @param $adminUserId int 代理id
* @return \think\Response
*/
function filterEmoji($str){
$str = preg_replace_callback( '/./u',
function (array $match) {
return strlen($match[0]) >= 4 ? '' : $match[0];
},
$str);
return $str;
}
/**
* xml转数组
*
* @return \think\Response
*/
function xmlToArray($xml)
{
$res = simplexml_load_string($xml, 'SimpleXmlElement', LIBXML_NOCDATA);
return json_decode(json_encode($res), true);
}
/**
* 获取所有应用下指定文件列表
* @param string $file_name
* @return array
*/
function getFileList($fileName=''){
$fileList=[];
if(!$fileName)return $fileList;
$addons_dir = base_path();
$dirs = scandir($addons_dir);
foreach ($dirs as $dir) {
$path=$addons_dir.$dir;
if(!is_dir($path)) continue;
if($dir!='.'&&$dir!='..'){
if(is_file($path."/{$fileName}")){
$fileList[]=$path."/{$fileName}";
}
}
}
return $fileList;
}
/**
* 设置返回头
* @param $key
* @param $value
* @return array
*/
function set_header($key=null,$value=null){
static $header=[];
if($key)$header[$key]=$value;
return $header;
}
/**
* import css js image out of pub director
* @param $path
* @param $code
* @param bool $refresh
* @return mixed|string
*/
function import($path,$code=''){
$app_path = $code ? base_path().$code.'/' : app_path();
$ext = substr(strrchr($path, '.'), 1);
$source_path= $app_path.'view/'.ltrim($path,'/\\');
if(($ext == 'html'||$ext == 'php')&&is_file($source_path)){
include($source_path);
return;
}
if(is_file($source_path)){
$pub_son_path ="static/".basename($app_path)."/".ltrim($path,'/\\');
$publish_path=root_path().'public/'.$pub_son_path;
$param='';
//文件如果被改变就重新发布
if(!is_file($publish_path)|| filemtime($source_path) > filemtime($publish_path)){
set_header('X-Accel-Buffering','no'); // 刷新缓存
set_header("Expires","Mon, 26 Jul 1997 05:00:00 GMT");
set_header("Cache-Control","no-cache, must-revalidate");
set_header("Pragma: no-cache");
is_dir(dirname($publish_path)) or mkdir(dirname($publish_path),0751,true);
copy($source_path,$publish_path);
}
$param='?t='.time();
$url=request()->domain().'/'.$pub_son_path;
if($ext == 'js'){
return "<script src='{$url}{$param}'></script>";
}elseif ($ext == 'css'){
return '<link rel="stylesheet" type="text/css" href="'.$url.'" media="all" />';
}else{
return $url;
}
}
return '';
}
/**
* 加密
* @param string $str 要加密的字符串
* @param string $key 盐
* @return string 返回内容
*/
function ktEncrypt($str="",$key=""){
$key = $key?:'kt';
$str = md5($str).$key;
$str = base64_encode($str);
return $str;
}
/**
* 站点信息
* @return array
*/
function site_info(){
$info = app('db')->table('kt_base_agent')->where(['domain'=>app('request')->host()])->field('id,user_logo,webname,webtitle,key_word,describe,kf_code,telephone,gzh_code,copyright,company_name,company_address,record_number,email,domain,qq,login_logo')->find();
if(!$info){
$info = app('db')->table('kt_base_agent')->where(['isadmin'=>1])->field('id,user_logo,webname,webtitle,key_word,describe,kf_code,telephone,gzh_code,copyright,company_name,company_address,record_number,email,domain,qq,login_logo')->find();
}
return $info;
}