-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.php
95 lines (78 loc) · 2.2 KB
/
utils.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
<?php
function sendPostReq($url, $data, $header = array("Content-type: application/json"))
{
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$res = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
return $err;
}
if ($res) {
return $res;
}
}
function sendDingMsg($secret, $ding_hook, $msg, $title, $type = "text", $ats = array(), $atAll = false)
{
// $now = (int) (microtime(true) * 1000);
$now = time() * 1000;
$stringToSign = $now . "\n" . $secret;
$sign = base64_encode(hash_hmac('sha256', $stringToSign, $secret, true));
$sendUrl = $ding_hook . '&' . http_build_query(array(
'timestamp' => $now,
'sign' => $sign
));
$data = json_encode(array(
'msgtype' => $type, // text/markdown
'markdown' => array(
'title' => $title,
'text' => $msg,
),
'text' => array(
'content' => $msg,
),
'at' => array(
'atMobiles' => $ats,
'isAtAll' => $atAll,
),
));
$res = json_decode(sendPostReq($sendUrl, $data));
return $res->errcode == 0;
}
function getFileContent($fileName, $formatJSON = true, $newFileContent = '[]')
{
if (!file_exists($fileName)) {
$f = fopen($fileName, 'w+');
fwrite($f, $newFileContent);
fclose($f);
}
$content = file_get_contents($fileName);
return $formatJSON ? json_decode($content) : $content;
}
function setFileContent($fileName, $content)
{
$f = fopen($fileName, 'w+');
fwrite($f, $content);
fclose($f);
}
function wLog($content, $isEncode = true, $title = '', $dir = './logs/')
{
$date = date('Y-m-d');
$path = $dir . $date . '.log';
$dir_name = dirname($path);
if (!file_exists($dir_name)) {
mkdir(iconv("UTF-8", "GBK", $dir_name), 0666, true);
}
if ($isEncode == true) {
$content = json_encode($content);
}
$now_date = date('Y-m-d H:i:s', time());
$content = $now_date . $title . "\n" . $content . "\n\n\n";
file_put_contents($path, $content, FILE_APPEND);
}