-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.php
231 lines (215 loc) · 9.64 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
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
<?php
/*
TechBench dump
Copyright (C) 2024 TechBench dump website authors and contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
require_once 'shared/utils.php';
require_once 'shared/lang.php';
require_once 'shared/dump.php';
require_once 'shared/style.php';
$config = getConfig();
if($_SERVER['REQUEST_METHOD'] == 'POST' && $_SERVER['CONTENT_TYPE'] == 'application/x-www-form-urlencoded') {
if(isset($_POST['Progress'])) {
if(is_file('dump.json.lock')) echo file_get_contents('dump.json.lock');
exit();
} else if(isset($_POST['startDump'])) {
execBackground($config['php'], 'dump.php update --quiet');
exit();
} else if(isset($_POST['Info'])) {
if(is_file('dump.json')) {
$dump = json_decode(file_get_contents('dump.json'), true);
echo json_encode([
'ProductNumber' => count($dump['ProdInfo']),
'LastUpdateTime' => date("Y-m-d H:i:s T", $dump['TechInfo']['LastUpdateTime'])
]);
}
exit();
} else if(isset($_POST['recheck'])) {
if($_POST['reCheck'] == 'basic') {
execBackground($config['php'], 'dump.php recheck');
exit();
}
if(is_file('dump.json')) {
$dump = json_decode(file_get_contents('dump.json'), true);
$checkList = [];
switch($_POST['recheck']) {
case 'WIP':
foreach($dump['ProdInfo'] as $productId => $product) if($product['Status'] != 'Unavailable' && in_array('WIP', $product['Category'])) $checkList[] = $productId;
break;
case 'Xbox':
foreach($dump['ProdInfo'] as $productId => $product) if($product['Status'] != 'Unavailable' && in_array('Xbox', $product['Category'])) $checkList[] = $productId;
break;
default:
exit();
}
echo json_encode($checkList);
}
exit();
} else if(isset($_POST['NewSession'])) {
exit(genSessionId());
}
} else if($_SERVER['REQUEST_METHOD'] == 'POST' && $_SERVER['CONTENT_TYPE'] == 'application/json') {
$json = file_get_contents('php://input');
if(strpos($json, 'Available') == false) exit();
$infos = json_decode($json, true);
$info = array_pop($infos);
if($info['type'] == 'reCheck' && $info['status'] == 'result') {
if(is_file('dump.json')) {
$dump = json_decode('dump.json');
$ProductNumber = count($dump['ProdInfo']);
foreach($infos as $info) {
$productId = $info['ID'];
$status = $info['Status'];
$arch = $info['Arch'];
$dump['ProdInfo'][$ProductId]['Status'] = $status;
if($status != 'Unavailable' && $arch != ['Unknown'] && ($arch != ['neutral'] || $dump['ProdInfo'][$productId]['Status'] == 'Unknown')) $dump['ProdInfo'][$ProductId]['Arch'] = $arch;
}
if(!is_file('dump.bak')) copy('dump.json', 'dump.bak');
file_put_contents('dump.json', json_encode($dump, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
exit();
}
}
}
$notice = 'Normal';
if(is_file('dump.json')) {
$dump = json_decode(file_get_contents('dump.json'), true);
$productNumber = count($dump['ProdInfo']);
$lastUpdateTime = date("Y-m-d H:i:s T", $dump['TechInfo']['LastUpdateTime']);
} else {
$productNumber = 0;
$lastUpdateTime = '';
$notice = 'File not exist';
}
styleTop('update');
echo <<<HTML
<script defer="defer" src="js/update.js"></script>
<div class="my-5 text-center">
<h1 class="fw-bold">{$s['tbDump']}
<span class="badge rounded-pill bg-primary position-absolute">v$websiteVersion</span>
</h1>
</div>
<div class="alert alert-info mt-4" id="info">
<h4><i class="bi bi-info"></i> {$s['techInfo']}</h4>
<p class="mb-0"> {$s['lastUpdate']}: <b> $lastUpdateTime</b><br>
{$s['productsNumber']}: <b>$productNumber</b></p>
<p><span>{$s['status']}: </span><span>$notice</span></p>
</div>
<div class="card text-bg">
<div class="card-body pb-1">
<div id="progress" class="progress" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="display: none;">
<div class="progress-bar progress-bar-striped progress-bar-animated bg-info text-dark overflow-visible" style="width: 0%"></div>
<p class="position-absolute" style="width: calc(50% + 1rem); right: 0">0.00%</p>
</div>
<p class="text-center my-3" id="count" style="display: none;"></p>
<button type="button" id="dumpBtn" class="btn btn-primary mb-3" disabled>Check for Updates</button>
<button type="button" id="checkBtn" class="btn btn-info mb-3" disabled>Recheck Status</button>
</div>
</div>
<script>
var info = document.getElementById('info');
var dumpBtn = document.getElementById('dumpBtn');
var checkBtn = document.getElementById('checkBtn');
var progress = document.getElementById('progress');
var progressbar = progress.getElementsByTagName('div').item(0);
var progresstext = progress.getElementsByTagName('p').item(0);
var count = document.getElementById('count');
var lastUpd = info.getElementsByTagName('b').item(0);
var prodNum = info.getElementsByTagName('b').item(1);
function checkProgress() {
progress.style.display = '';
count.style.display = '';
info.getElementsByTagName('span').item(1).innerText = '{$s['checking']}';
var xhr = new XMLHttpRequest();
xhr.open('POST',window.location.href);
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr.send('Progress=1');
xhr.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
if (xhr.responseText != '') var state = JSON.parse(xhr.responseText);
if ((progress.getAttribute('aria-valuenow') > 0 && typeof state === 'undefined') || typeof state !== 'undefined' && (state.progress == '100.00' || state.status == 'Successful')) {
clearInterval(intervalId);
updInfo();
} else if (typeof state !== 'undefined' && (state.status == 'Exception' || state.status == 'Error')) {
clearInterval(intervalId);
info.getElementsByTagName('span').item(1).innerText = '{$s['error']}';
progressbar.classList.add("bg-warning");
progressbar.classList.remove("progress-bar-animated");
} else if (typeof state !== 'undefined' && state.progress != '0.00' && !isNaN(state.progress)) {
if(state.status == 'recheck') progressbar.classList.add("bg-info");
progress.setAttribute('aria-valuenow', state.progress);
progressbar.style.width = state.progress + '%';
progresstext.innerText = state.progress + '%';
if(state.progress >= 10.00) {
progresstext.style.width = 'calc(50% + 1.25rem)';
}
count.innerText = state.current + ' / ' + state.total;
max = state.total;
}
}
}
}
function updInfo() {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
var infoJson = JSON.parse(xhr.responseText);
info.getElementsByTagName('span').item(1).innerText = 'Latest';
progress.setAttribute('aria-valuenow', '100');
progressbar.style.width = '100.00%';
progresstext.innerText = '100.00%';
progresstext.style.width = 'calc(50% + 1.5rem)';
dumpBtn.disabled = false;
count.innerText = max + ' / ' + max;
lastUpd.innerText = ' ' + infoJson.LastUpdateTime;
prodNum.innerText = infoJson.ProductNumber;
}
}
xhr.open('POST',window.location.href);
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr.send('Info=1');
}
dumpBtn.onclick = function () {
dumpBtn.disabled = true;
checkBtn.disabled = true;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (this.readyState == 4) {
if(this.status == 200) {
intervalId = setInterval(checkProgress, 500);
} else {
dumpBtn.disabled = false;
checkBtn.disabled = false;
}
}
}
xhr.open('POST',window.location.href);
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr.send('startDump=1');
};
var xhr = new XMLHttpRequest();
xhr.open('POST',window.location.href);
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr.send('Progress=1');
xhr.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
if(xhr.responseText != '') var state = JSON.parse(xhr.responseText);
if (typeof state === 'undefined' || state.status == 'Success' || new Date().getTime() / 1000 - state.time > 600) {
dumpBtn.disabled = false;
checkBtn.disabled = false;
} else {
intervalId = setInterval(checkProgress, 500);
}
}
}
</script>
HTML;?>
<?php styleBottom(); ?>