Skip to content

Commit ae757be

Browse files
authored
add setup=auth, check if another building
1 parent 21c1120 commit ae757be

File tree

1 file changed

+79
-57
lines changed

1 file changed

+79
-57
lines changed

platform/Vercel_env.php

Lines changed: 79 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,6 @@ function install()
163163
//$tmp['language'] = $_POST['language'];
164164
$tmp['timezone'] = $_COOKIE['timezone'];
165165
$APIKey = $_POST['APIKey'];
166-
//if ($APIKey=='') {
167-
// $APIKey = getConfig('APIKey');
168-
//}
169166
$tmp['APIKey'] = $APIKey;
170167

171168
$token = $APIKey;
@@ -184,16 +181,24 @@ function install()
184181
$title = 'Error';
185182
return message($html, $title, 400);
186183
} else {
187-
/*$html = '<script>
184+
$html = getconstStr('Success') . '
185+
<script>
188186
var status = "' . $response['DplStatus'] . '";
187+
var i = 0;
189188
var expd = new Date();
190189
expd.setTime(expd.getTime()+1000);
191190
var expires = "expires="+expd.toGMTString();
192191
document.cookie=\'language=; path=/; \'+expires;
192+
var uploadList = setInterval(function(){
193+
if (document.getElementById("dis").style.display=="none") {
194+
console.log(i++);
195+
} else {
196+
clearInterval(uploadList);
197+
location.href = "' . path_format($_SERVER['base_path'] . '/') . '";
198+
}
199+
}, 1000);
193200
</script>';
194-
return message($html, $title, 201, 1);*/
195-
$data["dplId"] = $response['DplStatus'];
196-
return output(json_encode($data), 201);
201+
return message($html, $title, 201, 1);
197202
}
198203
}
199204
}
@@ -205,10 +210,9 @@ function install()
205210
$html .= '
206211
<label><input type="radio" name="language" value="'.$key1.'" '.($key1==$constStr['language']?'checked':'').' onclick="changelanguage(\''.$key1.'\')">'.$value1.'</label><br>';
207212
}
208-
//if (getConfig('APIKey')=='')
209213
$html .= '<br>
210214
<a href="https://vercel.com/account/tokens" target="_blank">' . getconstStr('Create') . ' token</a><br>
211-
<label>Token:<input name="APIKey" type="password" placeholder="" value="' . getConfig('APIKey') . '"></label><br>';
215+
<label>Token:<input name="APIKey" type="password" placeholder="" value=""></label><br>';
212216
$html .= '<br>
213217
<label>Set admin password:<input name="admin" type="password" placeholder="' . getconstStr('EnvironmentsDescription')['admin'] . '" size="' . strlen(getconstStr('EnvironmentsDescription')['admin']) . '"></label><br>';
214218
$html .= '
@@ -241,52 +245,7 @@ function notnull(t)
241245
alert(\'input Token\');
242246
return false;
243247
}
244-
t.style.display = "none";
245-
errordiv.innerHTML = "' . getconstStr('Wait') . '";
246-
var xhr = new XMLHttpRequest();
247-
xhr.open("POST", t.action);
248-
xhr.onload = function(e) {
249-
if (xhr.status==201) {
250-
var res = JSON.parse(xhr.responseText);
251-
getStatus(res.dplId, t.APIKey.value);
252-
} else {
253-
t.style.display = "";
254-
errordiv.innerHTML = xhr.status + "<br>" + xhr.responseText;
255-
}
256-
}
257-
xhr.send("admin=" + t.admin.value + "&APIKey=" + t.APIKey.value);
258-
259-
var x = "";
260-
var min = 0;
261-
function getStatus(id, VercelToken) {
262-
x += ".";
263-
min++;
264-
var xhr = new XMLHttpRequest();
265-
var url = "https://api.vercel.com/v11/now/deployments/" + id;
266-
xhr.open("GET", url);
267-
xhr.setRequestHeader("Authorization", "Bearer " + VercelToken);
268-
xhr.onload = function(e) {
269-
if (xhr.status==200) {
270-
var deployStat = JSON.parse(xhr.responseText).readyState;
271-
if (deployStat=="READY") {
272-
x = "";
273-
min = 0;
274-
errordiv.innerHTML = "Deploy done.";
275-
location.href = "/";
276-
} else {
277-
errordiv.innerHTML = deployStat + ", " + min + ".<br>' . getconstStr('Wait') . ' " + x;
278-
if (deployStat!=="ERROR") setTimeout(function() { getStatus(id, VercelToken) }, 1000);
279-
}
280-
} else {
281-
t.style.display = "";
282-
console.log(xhr.status);
283-
console.log(xhr.responseText);
284-
}
285-
}
286-
xhr.send(null);
287-
}
288-
289-
return false;
248+
return true;
290249
}
291250
</script>';
292251
$title = getconstStr('SelectLanguage');
@@ -335,7 +294,8 @@ function setVercelConfig($envs, $appId, $token)
335294

336295
function VercelUpdate($appId, $token, $sourcePath = "")
337296
{
338-
$url = "https://api.vercel.com/v12/now/deployments";
297+
if (checkBuilding($appId, $token)) return '{"error":{"message":"Another building is in progress."}}';
298+
$url = "https://api.vercel.com/v13/deployments";
339299
$header["Authorization"] = "Bearer " . $token;
340300
$header["Content-Type"] = "application/json";
341301
$data["name"] = "OneManager";
@@ -357,6 +317,23 @@ function VercelUpdate($appId, $token, $sourcePath = "")
357317
return json_encode($result);
358318
}
359319

320+
function checkBuilding($projectId, $token)
321+
{
322+
$r = 0;
323+
$url = "https://api.vercel.com/v6/deployments/?projectId=" . $projectId;
324+
$header["Authorization"] = "Bearer " . $token;
325+
$header["Content-Type"] = "application/json";
326+
$response = curl("GET", $url, '', $header);
327+
//echo json_encode($response, JSON_PRETTY_PRINT) . " ,res<br>";
328+
$result = json_decode($response["body"], true);
329+
foreach ( $result['deployments'] as $deployment ) {
330+
if ($deployment['state']!=="READY") $r++;
331+
}
332+
return $r;
333+
//if ($r===0) return true;
334+
//else return false;
335+
}
336+
360337
function getEachFiles(&$file, $base, $path = "")
361338
{
362339
//if (substr($base, -1)=="/") $base = substr($base, 0, -1);
@@ -438,7 +415,7 @@ function WaitFunction($deployid) {
438415
}
439416
$header["Authorization"] = "Bearer " . getConfig('APIKey');
440417
$header["Content-Type"] = "application/json";
441-
$url = "https://api.vercel.com/v11/now/deployments/" . $deployid;
418+
$url = "https://api.vercel.com/v11/deployments/" . $deployid;
442419
$response = curl("GET", $url, "", $header);
443420
if ($response['stat']==200) {
444421
$result = json_decode($response['body'], true);
@@ -450,3 +427,48 @@ function WaitFunction($deployid) {
450427
return $response;
451428
}
452429
}
430+
431+
function changeAuthKey() {
432+
if ($_POST['APIKey']!='') {
433+
$APIKey = $_POST['APIKey'];
434+
$tmp['APIKey'] = $APIKey;
435+
$response = json_decode(setVercelConfig($tmp, getConfig('HerokuappId'), $APIKey), true);
436+
if (api_error($response)) {
437+
$html = api_error_msg($response);
438+
$title = 'Error';
439+
return message($html, $title, 400);
440+
} else {
441+
$html = getconstStr('Success') . '
442+
<script>
443+
var status = "' . $response['DplStatus'] . '";
444+
var i = 0;
445+
var uploadList = setInterval(function(){
446+
if (document.getElementById("dis").style.display=="none") {
447+
console.log(i++);
448+
} else {
449+
clearInterval(uploadList);
450+
location.href = "' . path_format($_SERVER['base_path'] . '/') . '";
451+
}
452+
}, 1000);
453+
</script>';
454+
return message($html, $title, 201, 1);
455+
}
456+
}
457+
$html = '
458+
<form action="" method="post" onsubmit="return notnull(this);">
459+
<a href="https://vercel.com/account/tokens" target="_blank">' . getconstStr('Create') . ' token</a><br>
460+
<label>Token:<input name="APIKey" type="password" placeholder="" value=""></label><br>
461+
<input type="submit" value="' . getconstStr('Submit') . '">
462+
</form>
463+
<script>
464+
function notnull(t)
465+
{
466+
if (t.APIKey.value==\'\') {
467+
alert(\'Input Token\');
468+
return false;
469+
}
470+
return true;
471+
}
472+
</script>';
473+
return message($html, 'Change platform Auth token or key', 200);
474+
}

0 commit comments

Comments
 (0)