-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathupdate.php
96 lines (87 loc) · 2.73 KB
/
update.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
<?php
//get config
require 'config.php';
//curl get function
function curlGet($url, $ua, $auth = '')
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERPWD, $auth);
curl_setopt($ch, CURLOPT_USERAGENT, $ua);
$response = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200) {
//request failed
error_log('Curl request failed!' . $response);
http_response_code(500);
die('An error occurred when request:' . $url);
$response = false;
}
curl_close($ch);
return $response;
}
//get the directory
function getDirectory($repo, $path)
{
$rep = json_decode(curlGet('https://api.github.com/repos/' . $repo . '/contents' . $path, 'HoYoRandom-PHP', $GLOBALS['ghAuth']));
$files = array();
foreach ($rep as $file) {
switch ($file->type) {
case 'file':
#add to list
$files[$file->name] = $file->path;
break;
case 'dir':
if (substr($file->name, 0, 1) != '.') {
$files = array_merge($files, getDirectory($repo, '/' . $file->path));
}
break;
default:
break;
}
}
return $files;
}
//verify the secret
function verify()
{
if (isset(CONFIG['WEBHOOK_SECRET'])) {
$secret = CONFIG['WEBHOOK_SECRET'];
} else {
return true;
}
foreach (str_split($_SERVER['QUERY_STRING'], '&') as $arg) {
if (trim($arg) == 's=' . $secret) {
return true;
}
}
return false;
}
//verify request
if (!isset($argc)) {
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
http_response_code(405);
die('Method Not Allowed');
}
if (!verify()) {
http_response_code(403);
die('Invalid Secret');
}}
//get the github auth token
$ghAuth = CONFIG['GITHUB_AUTH'] ?? '';
//get the directory
$repo = CONFIG['RES_REPO_NAME'] ?? http_response_code(500) && die('Server error:RES_REPO_NAME no set!');
$files = getDirectory($repo, '/');
//write to file
!empty($files) ? file_put_contents(__DIR__ . '/contents.json', json_encode($files, JSON_UNESCAPED_UNICODE)) : http_response_code(500) && die('Unable to get the file list');
//download the *.hitokoto.json
if (!is_dir(__DIR__ . '/hitokoto/')) {
mkdir(__DIR__ . '/hitokoto/');
}
foreach ($files as $fileName => $filePath) {
if (preg_match('/(\.hitokoto\.json)$/i', $fileName) == 1) {
file_put_contents(__DIR__ . '/hitokoto/' . $fileName, curlGet('https://cdn.jsdelivr.net/gh/' . $repo . '/' . $filePath, 'HoYoRandom-PHP'));
}
}
echo 'All Done!';