api/shared/utils.php

190 lines
4.8 KiB
PHP
Raw Permalink Normal View History

2018-08-11 00:15:04 +02:00
<?php
/*
Copyright 2020 whatever127
2018-08-11 00:15:04 +02:00
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.
*/
function uupApiPrintBrand() {
global $uupApiBrandPrinted;
if(!isset($uupApiBrandPrinted)) {
consoleLogger('UUP dump API v'.uupApiVersion());
$uupApiBrandPrinted = 1;
}
}
function randStr($length = 4) {
$characters = '0123456789abcdef';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
function genUUID() {
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
rand(0, 0xffff),
rand(0, 0xffff),
rand(0, 0xffff),
rand(0, 0x0fff) | 0x4000,
rand(0, 0x3fff) | 0x8000,
rand(0, 0xffff),
rand(0, 0xffff),
rand(0, 0xffff)
);
}
function sendWuPostRequestInternal($url, $postData, $saveCookie = true) {
2018-08-11 00:15:04 +02:00
$req = curl_init($url);
2021-07-01 17:44:04 +02:00
$proxy = uupDumpApiGetConfig();
if(isset($proxy['proxy'])) {
curl_setopt($req, CURLOPT_PROXY, $proxy['proxy']);
}
2018-08-11 00:15:04 +02:00
curl_setopt($req, CURLOPT_HEADER, 0);
curl_setopt($req, CURLOPT_POST, 1);
curl_setopt($req, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($req, CURLOPT_ENCODING, '');
curl_setopt($req, CURLOPT_POSTFIELDS, $postData);
2022-09-06 17:22:36 +02:00
curl_setopt($req, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($req, CURLOPT_TIMEOUT, 15);
2018-08-11 00:15:04 +02:00
curl_setopt($req, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($req, CURLOPT_HTTPHEADER, array(
'User-Agent: Windows-Update-Agent/10.0.10011.16384 Client-Protocol/2.50',
2018-08-11 00:15:04 +02:00
'Content-Type: application/soap+xml; charset=utf-8',
));
$out = curl_exec($req);
$error = curl_getinfo($req, CURLINFO_RESPONSE_CODE);
2018-08-11 00:15:04 +02:00
curl_close($req);
if($saveCookie === true)
uupSaveCookieFromResponse($out);
2018-08-11 00:15:04 +02:00
return [
'error' => $error,
'out' => $out
];
}
2018-08-11 00:15:04 +02:00
function sendWuPostRequest($url, $postData) {
return sendWuPostRequestInternal($url, $postData)['out'];
}
2018-08-11 00:15:04 +02:00
function sendWuPostRequestHelper(
$endpoint,
$postComposer,
$postComposerArgs,
$saveCookie = true
) {
$endpoints = [
'client' => 'https://fe3.delivery.mp.microsoft.com/ClientWebService/client.asmx',
'clientSecured' => 'https://fe3cr.delivery.mp.microsoft.com/ClientWebService/client.asmx/secured'
];
$postData = call_user_func_array($postComposer, $postComposerArgs);
$data = sendWuPostRequestInternal($endpoints[$endpoint], $postData, $saveCookie);
if($data['error'] == 500 && preg_match('/<ErrorCode>(ConfigChanged|CookieExpired|InvalidCookie)<\/ErrorCode>/', $data['out'])) {
uupInvalidateCookie();
$postData = call_user_func_array($postComposer, $postComposerArgs);
return sendWuPostRequestInternal($endpoints[$endpoint], $postData, $saveCookie);
2018-08-11 00:15:04 +02:00
}
return $data;
2018-08-11 00:15:04 +02:00
}
function consoleLogger($message, $showTime = 1) {
if(php_sapi_name() != 'cli') return
$currTime = '';
if($showTime) {
$currTime = '['.date('Y-m-d H:i:s T', time()).'] ';
}
$msg = $currTime.$message;
fwrite(STDERR, $msg."\n");
}
2021-07-01 17:44:04 +02:00
function uupDumpApiGetConfig() {
if(!file_exists('config.ini')) {
return null;
}
2021-07-01 17:44:04 +02:00
return parse_ini_file('config.ini');
}
2019-09-14 17:26:57 +02:00
function uupApiCheckUpdateId($updateId) {
return preg_match(
'/^[\da-fA-F]{8}-([\da-fA-F]{4}-){3}[\da-fA-F]{12}(_rev\.\d+)?$/',
$updateId
);
}
2022-08-29 03:01:11 +02:00
function uupApiIsServer($skuId) {
$serverSkus = [
7, 8, 12, 13, 79, 80, 120, 145, 146,
147, 148, 159, 160, 406, 407, 408
];
return in_array($skuId, $serverSkus);
}
function uupApiBuildMajor($build) {
2022-09-07 01:03:23 +02:00
if($build == null)
return null;
if(!str_contains($build, '.'))
2022-08-29 03:01:11 +02:00
return intval($build);
2022-09-07 01:03:23 +02:00
2022-08-29 03:01:11 +02:00
return intval(explode('.', $build)[0]);
}
2022-09-07 01:28:19 +02:00
function uupApiFixDownloadLink($link) {
2022-10-05 16:32:40 +02:00
return $link;
2022-09-07 01:28:19 +02:00
}
2022-09-13 23:40:03 +02:00
function uupApiReadJson($path) {
$data = @file_get_contents($path);
if(empty($data))
return false;
return json_decode($data, true);
}
function uupApiWriteJson($path, $data) {
return file_put_contents($path, json_encode($data)."\n");
}
2022-09-15 16:12:10 +02:00
function uupApiPacksExist($updateId) {
return file_exists('packs/'.$updateId.'.json.gz');
}
2022-09-16 00:24:18 +02:00
function uupApiConfigIsTrue($config) {
$data = uupDumpApiGetConfig();
if(!isset($data[$config]))
return false;
return $data[$config] == true;
}