common.php
16.0 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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
<?php
use OSS\OssClient; // | OSS客户端类,用户通过OssClient的实例调用接口 |
use OSS\Core\OssException; // | OSS异常类,用户在使用的过程中,只需要注意这个异常|
// 应用公共文件
define('MANAGER_AUTH_KEY', 'k]J$[8b)%0Fv=lmecB3qI|D4@#LdC`xOaN6-{QWZ'); //加密KEY
//获取毫秒
function getMillisecond()
{
list($t1, $t2) = explode(' ', microtime());
return (float) sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
}
function microtime_format($tag, $time)
{
$a = substr($time, 0, 10);
$b = substr($time, 10);
$date = date($tag, $a) . '-' . $b;
return $date;
}
/**
* 获取文件后缀名,并判断是否合法
*
* @param string $file_name
* @param array $allow_type
* @return blob
*/
function get_file_suffix($file_name, $allow_type = array())
{
$fnarray=explode('.', $file_name);
$file_suffix = strtolower(array_pop($fnarray));
if (empty($allow_type)) {
return $file_suffix;
} else {
if (in_array($file_suffix, $allow_type)) {
return true;
} else {
return false;
}
}
}
/**
* 远程文件下载
* @param $file_url
* @param $filePath 保存路径
*/
function downfile($file_url, $filePath)
{
$header[] = "content-type: application/x-www-form-urlencoded;
charset = UTF-8";
$ch1 = curl_init();
$timeout = 60;
curl_setopt($ch1, CURLOPT_URL, $file_url);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch1, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch1, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, false);
$content = curl_exec($ch1);
curl_close($ch1);
// $content = file_get_contents($file_url);
if (file_put_contents($filePath, $content)) {
return true;
} else {
return false;
}
}
//手机验证格式
function check_mobile($mobile_phone)
{
$mobile_phone = trim($mobile_phone);
if (preg_match("/^1[3-9]{1}[0-9]{9}$/", $mobile_phone)) {
return $mobile_phone;
} else {
return false;
}
}
//验证邮箱格式
function checke_mail($email)
{
$pattern = "/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/"; //[ch]定义验证email正则表达式
if (preg_match($pattern, $email, $counts)) {
return true;
} else {
return false;
}
}
/**
* 取得文件扩展
*
* @param $filename
* @return
*/
function fileext($filename)
{
return strtolower(trim(substr(strrchr($filename, '.'), 1, 10)));
}
/**
* IE浏览器判断
*/
function is_ie()
{
$useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
if ((strpos($useragent, 'opera') !== false) || (strpos($useragent, 'konqueror') !== false)) {
return false;
}
if (strpos($useragent, 'msie ') !== false) {
return true;
}
return false;
}
/**
* 创建目录
* @param string $path 要创建的目录
* @return boolean 创建状态,true-成功,false-失败
*/
function mkdirs($path, $mode = 0755)
{
if (!is_dir(dirname($path))) {
mkdirs(dirname($path), $mode);
}
if (!file_exists($path)) {
return @mkdir($path, $mode);
}
}
/**
* 压缩音频文件
* @param string $file 需要压缩的文件路径
* @param string $path 压缩后的文件路径
* @return boolean
*/
function ffmpeg($file, $path)
{
$rest = shell_exec("(ffmpeg -i " . $file . " -vn -c:a libmp3lame -b:a 128k -ac 2 " . $path . " >/dev/null & ) ");
return true;
}
/**
* 系统非常规MD5加密方法
* @param string $str 要加密的字符串
* @return string
*/
function think_center_md5($str, $key = '')
{
return '' === $str ? '' : md5(sha1($str) . $key);
}
//apk文件路径生成hash值
function file_hash($path){
$cmd = '-tfile_sha -s'.base64_encode($path);
$hash = call($cmd);
return $hash;
}
function call($icbp_info){
if($icbp_info==""){
\think\Log::write('info', '执行的hst_icbp命令:命令没有参数 --line:'.__LINE__);
return false;
exit;
}
$descriptorspec = array(
0 => array("pipe", "r"), // stdin
1 => array("pipe", "w"), // stdout
2 => array("pipe", "w") // stderr
);
$cmd = '/home/alg_icbp/hst_icbp '.$icbp_info; // 替换为你要执行的shell脚本
\think\Log::write('info', '执行的cmd命令:'.print_r($cmd,true).'--line:'.__LINE__);
$proc = proc_open($cmd, $descriptorspec, $pipes, null, null);
// $proc为false,表明命令执行失败
if ($proc == false) {
// do sth with HTTP response
} else {
$stdout = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[2]);
$status = proc_close($proc); // 释放proc
}
$data = array(
'code' => 0,
'msg' => array(
'stdout' => $stdout,
'stderr' => $stderr,
'retval' => $status
)
);
\think\Log::write('info', '执行的CMD命令结果:'.print_r($data['msg']['stdout'],true).'--line:'.__LINE__);
return $data['msg']['stdout'];
}
/**
* 系统加密方法
* @param string $data 要加密的字符串
* @param string $key 加密密钥
* @param int $expire 过期时间 单位 秒
* @return string
* @author 麦当苗儿 <zuojiazi@vip.qq.com>
*/
function think_encrypt($data, $key, $expire = 0)
{
$key = md5($key);
$data = base64_encode($data);
$x = 0;
$len = strlen($data);
$l = strlen($key);
$char = '';
for ($i = 0; $i < $len; $i++) {
if ($x == $l) {
$x = 0;
}
$char .= substr($key, $x, 1);
$x++;
}
$str = sprintf('%010d', $expire ? $expire + time() : 0);
for ($i = 0; $i < $len; $i++) {
$str .= chr(ord(substr($data, $i, 1)) + (ord(substr($char, $i, 1))) % 256);
}
return str_replace(array('+', '/', '='), array('-', '_', ''), base64_encode($str));
}
/**
* 系统解密方法
* @param string $data 要解密的字符串 (必须是think_encrypt方法加密的字符串)
* @param string $key 加密密钥
* @return string
* @author 麦当苗儿 <zuojiazi@vip.qq.com>
*/
function think_decrypt($data, $key)
{
$key = md5($key);
$data = str_replace(array('-', '_'), array('+', '/'), $data);
$mod4 = strlen($data) % 4;
if ($mod4) {
$data .= substr('====', $mod4);
}
$data = base64_decode($data);
$expire = substr($data, 0, 10);
$data = substr($data, 10);
if ($expire > 0 && $expire < time()) {
return '';
}
$x = 0;
$len = strlen($data);
$l = strlen($key);
$char = $str = '';
for ($i = 0; $i < $len; $i++) {
if ($x == $l) {
$x = 0;
}
$char .= substr($key, $x, 1);
$x++;
}
for ($i = 0; $i < $len; $i++) {
if (ord(substr($data, $i, 1)) < ord(substr($char, $i, 1))) {
$str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1)));
} else {
$str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1)));
}
}
return base64_decode($str);
}
/**
* 判断字符串是否以指定字符开头
* @param $str '字符串'
* @param $needle '指定字符'
* @return
*/
function startWith($str, $needle)
{
return strpos($str, $needle) === 0;
}
/**
*
* @return int
*/
function is_login()
{
//判断当前用户是否还存在
$token = session('user_auth');
$user = \think\Cache::get($token);
//TODO::版本号
if (empty($user)) {
return '';
} else {
return $user;
}
}
function getMemberRoleToString($role, $field = 'nickname')
{
$lists = app\admin\model\Manager::where('role', $role)->column('nickname');
return implode(',', $lists);
}
/**
* 判断是否是超级管理员
* @param $uid
* @return
*/
function is_administrator($uid = null)
{
return $uid && (intval($uid) === \think\Config::get('is_administrator_id')) ? 1 : 0;
}
/**
* 获取菜单树(html)
* @param $pid
* @param $id
* @param $table
* @return
*/
function getTree($pid = 0, $id = 0, $ispid = false)
{
$auth = new \app\admin\model\AuthRule();
$map = ['id' => ['neq', $id]];
$ispid && $map['pid'] = 0;
// $map['type'] = ['lt', 3];
$menu = $auth->where($map)->field('id,title,pid')->select()->toArray();
$tree = " <option value=\"0\">顶级菜单</option>";
foreach ($menu as $k => $v) {
if ($v['pid'] == 0) {
if ($ispid) {
$tree .= "<option value=\"" . $v['id'] . "\" ";
if ($v['id'] == $pid) {
$tree .= "selected=\"selected\"";
}
$tree .= " >" . $v['title'] . "</option>";
continue;
}
$cate_prent = new \tree\Tree($menu);
$tree .= "<option value=\"" . $v['id'] . "\" ";
if ($v['id'] == $pid) {
$tree .= "selected=\"selected\"";
}
$tree .= " >" . $v['title'] . "</option>";
$tree .= $cate_prent->get_tree($v['id'], "<option value=\$id \$selected>\$spacer\$title</option>", $pid);
}
}
return $tree;
}
/**
* 生成图片文件地址
* @param type $sha1 sha1
* @param type $type image
* @return type
*/
function getFileUrl($sha1, $type = 'image')
{
$url = '';
switch ($type) {
case 'image':
if (strpos($sha1, "http://") !== false) {
$url = $sha1;
} else {
$url = $sha1 ? url('/image/' . $sha1, '', '', true) : '';
}
break;
case 'images':
break;
case 'file':
$url = $sha1 ? url('/file/' . $sha1, '', '',true) : '';
break;
}
return $url;
}
/**
* 发送http post请求
* @param type $url
* @param type $post_data
*/
function http_post($url, $post_data)
{
$header[] = "charset = UTF-8";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($ch);
if (curl_errno($ch)) {
\think\Log::write(print_r(curl_errno($ch),1),'debug---1');
}
curl_close($ch);
return json_decode($res, true);
}
/**
* 发送http get请求
* @param type $url
* @return type
*/
function http_get($url)
{
$header[] = "content-type: application/x-www-form-urlencoded;
charset = UTF-8";
$ch1 = curl_init();
$timeout = 5;
curl_setopt($ch1, CURLOPT_URL, $url);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch1, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch1, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, false);
$accesstxt = curl_exec($ch1);
curl_close($ch1);
return json_decode($accesstxt, true);
}
/**
* 正则验证手机号
* @param $tel 11位手机号
* @return bool
*/
function is_mobile($tel)
{
if (!preg_match("/^1[3,6,5,8,7][0-9]{9}$/", $tel)) {
return false;
}
return true;
}
/**
* 生成随机字符串
* @param int $length 要生成的随机字符串长度
* @param string $type 随机码类型:0 数字+大小写字母 1 数字 2 小写字母 3 大写字母 4 特殊字符 -1 数字+大小写字母+特殊字符
* @return string
*/
function randCode($length = 5, $type = 0)
{
$arr = array(1 => "0123456789", 2 => "abcdefghijklmnopqrstuvwxyz", 3 => "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 4 => "~@#$%^&*(){}[]|");
if ($type == 0) {
array_pop($arr);
$string = implode("", $arr);
} elseif ($type == "-1") {
$string = implode("", $arr);
} else {
$string = $arr[$type];
}
$count = strlen($string) - 1;
$code = '';
for ($i = 0; $i < $length; $i++) {
$code .= $string[rand(0, $count)];
}
return $code;
}
/**
* 随机取数组N个元素
*
* @param array $arr 原数组
* @param int $num 个数
* @return array
*/
function array_random($arr = [], $num = 1)
{
shuffle($arr);
$r = [];
$num = $num > count($arr) ? count($arr) : $num;
for ($i = 0; $i < $num; $i++) {
$r[] = $arr[$i];
}
return 1 == $num ? $r[0] : $r;
}
/**
*
* @param type $len 邀请码长度
* @return varchar $string 字符串
* /
*/
function rand_code($len)
{
$chars = '0123456789abcdefghijklmnopqrstuvwxyz';
$string = substr(time(), -4);
for (; $len >= 1; $len--) {
$position = rand() % strlen($chars);
$position2 = rand() % strlen($string);
$string = substr_replace($string, substr($chars, $position, 1), $position2, 0);
}
return $string;
}
/**
* 截取字符串
*/
function substrs($str){
//去除标签
$str = strip_tags($str);
return substr($str,0,50);
}
/***
* 写入日志
* @param operator:操作人
* @param remark:详情
*/
function set_log($data){
$log = new \app\admin\model\YLog();
return $log->add($data);
}
/***
*使用mq发送消息
*/
function set_mq_msg($rout_key,$msg){
$str = '/home/anmi/amqp_sendstring 127.0.0.1 5672 amq.topic ctrl 123';
shell_exec($str);
return;
}
//验证ip是否合法
function isOk_ip($ip){
if(preg_match('/^((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1 -9]?\d))))$/', $ip)) {
return 1;
}else{
return 0;
}
}
/**
* @name:修改ini文件参数
* @param $key
* @param $value
* @param $filename//文件路径
*/
function system_config($key,$value,$filename){
$str = "sed -i 's/".$key."=.*/".$key."=".$value."/g' ".$filename;
calls($str);
}
//读取ini文件
function set_ini_file($file_name){
if(!is_file($file_name)){
$this->error('当前文件不存在','');
}
return parse_ini_file($file_name);
}
//读取非ini文件
function get_sip_id($file_name){
$list = file_get_contents($file_name);
$list = str_replace(array("\r\n", "\r", "\n"), ',', $list);
$list = trim($list,',');
$list = explode(',',$list);
$arr = '';
foreach ($list as $v){
if(strpos($v,'SIP_ID') !== false){
$arr = str_replace('SIP_ID=','',$v);
break;
}
}
return $arr;
}
//发送sip消息到资源服务器
function send_msg($data){
//ycx 修改 20190809 如果配置文件中设置了内网IP就使用内网
$sip_to_server = config('sip_to_server');
if(config('sip_to_server_inner')){
$sip_to_server = config('sip_to_server_inner');
}
\think\Log::write(print_r(config('sip')." miscapps send_message '".config('sip_to_account')."' '".config('sip_to_resource_account')."' '"
.base64_encode(json_encode($data)). "' '".config('sip_to_resource_account')."' '".$sip_to_server."' ''",1),'debug');
shell_exec(config('sip')." miscapps send_message '".config('sip_to_account')."' '".config('sip_to_resource_account')."' '"
.base64_encode(json_encode($data)). "' '".config('sip_to_resource_account')."' '".$sip_to_server."' ''");
}
//发送sip消息通知客户端
function send_app_msg($data,$sip){
$sip_to_server = config('sip_to_server');
// if(config('sip_to_server_inner')){
// $sip_to_server = config('sip_to_server_inner');
//}
\think\Log::write(print_r(config('sip')." miscapps send_message '".config('sip_to_account')."' '".$sip."' '"
.base64_encode(json_encode($data)). "' '".$sip."' '".$sip_to_server."' ''",1),'debug');
shell_exec(config('sip')." miscapps send_message '".config('sip_to_account')."' '".$sip."' '"
.base64_encode(json_encode($data)). "' '".$sip."' '".$sip_to_server."' ''");
}