Skip to content
This repository was archived by the owner on Jul 26, 2022. It is now read-only.

Commit b7a23bc

Browse files
committed
Small optimizations
1 parent 5064af3 commit b7a23bc

File tree

3 files changed

+47
-37
lines changed

3 files changed

+47
-37
lines changed

pages/webinterface/task/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class="h-10 bg-blue-500 text-white rounded-md px-4 py-2 m-2 hover:bg-blue-600 fo
8686
<!-- Create Task -->
8787
<div class="w-full">
8888
<form method="post">
89-
<input name="action" value="startService" type="hidden">
89+
<input name="action" value="createTask" type="hidden">
9090
<input name="csrf" value="<?= $_SESSION['cn3-wi-csrf'] ?>" type="hidden">
9191
<div class="coding inverse-toggle px-5 pt-4 shadow-lg text-gray-100 dark:bg-gray-800 bg-white pb-6 pt-4 rounded-lg leading-normal overflow-hidden">
9292
<div class="top mb-2 flex">

public/index.php

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -104,22 +104,25 @@
104104

105105
if (isset($_POST['action'])) {
106106
if (!main::validCSRF()) {
107-
header('Location: ' . main::getUrl() . "/tasks/".$task_name."?action&success=false&message=csrfFailed");
107+
header('Location: ' . main::getUrl() . "/tasks/" . $task_name . "?action&success=false&message=csrfFailed");
108108
die();
109109
}
110-
// FUNCTIONS
111110

112-
if($_POST['action'] == "stopService" AND isset($_POST['service_id'])){
113-
$response = $main::buildDefaultRequest("service/".$_POST['service_id'], "DELETE");
114-
header('Location: ' . main::getUrl() . "/tasks/".$task_name."?action&success=true&message=stopService");
111+
if ($_POST['action'] == "stopService" and isset($_POST['service_id'])) {
112+
$response = $main::buildDefaultRequest("service/" . $_POST['service_id'], "DELETE");
113+
header('Location: ' . main::getUrl() . "/tasks/" . $task_name . "?action&success=true&message=stopService");
114+
die();
115115
}
116-
if($_POST['action'] == "startService" AND isset($_POST['count'])){
116+
117+
if ($_POST['action'] == "startService" and isset($_POST['count'])) {
117118
$i = $_POST['count'];
118119
while ($i != 0) {
119120
$i -= 1;
120121
$response = $main::buildDefaultRequest("service/create", params: json_encode(array("start" => isset($_POST['start']), "serviceTaskName" => $task_name)));
121122
}
122-
header('Location: ' . main::getUrl() . "/tasks/".$task_name."?action&success=true&message=startService");
123+
124+
header('Location: ' . main::getUrl() . "/tasks/" . $task_name . "?action&success=true&message=startService");
125+
die();
123126
}
124127
}
125128

@@ -135,13 +138,7 @@
135138
die();
136139
}
137140

138-
$ticket = main::buildDefaultRequest("wsTicket");
139-
if (!$ticket['success']) {
140-
header('Location: ' . main::getUrl() . "/tasks/".$task_name."?action&success=false&message=notFound");
141-
die();
142-
}
143-
144-
$ticket = $ticket['id'];
141+
$ticket = main::requestWsTicket("tasks/$task_name?action&success=false&message=notFound");
145142

146143
include "../pages/header.php";
147144
include "../pages/webinterface/task/console.php";
@@ -165,11 +162,13 @@
165162
header('Location: ' . main::getUrl() . "/cluster?action&success=true&message=nodeStop");
166163
die();
167164
}
165+
168166
if ($_POST['action'] == "deleteNode" and isset($_POST['node_id'])) {
169167
main::buildDefaultRequest("cluster/" . $_POST['node_id'], "DELETE", array(), array());
170168
header('Location: ' . main::getUrl() . "/cluster?action&success=true&message=nodeDelete");
171169
die();
172170
}
171+
173172
if ($_POST['action'] == "createNode" and isset($_POST['name'])) {
174173
$action = main::buildDefaultRequest("cluster", "POST", array(), json_encode(array("properties" => array(), "uniqueId" => $_POST['name'], "listeners" => array((array("host" => $_POST['host'], "port" => $_POST['port']))))));
175174
header('Location: ' . main::getUrl() . "/cluster?action&success=true&message=nodeCreate");
@@ -182,19 +181,15 @@
182181
include "../pages/webinterface/cluster/index.php";
183182
include "../pages/footer.php";
184183
});
184+
185185
$this->any('/?/console', function ($node_id) use ($main) {
186186
$cluster = main::buildDefaultRequest("node/" . $node_id, "GET");
187187
if (!$cluster['success']) {
188188
header('Location: ' . main::getUrl() . "/cluster?action&success=false&message=notFound");
189189
die();
190190
}
191-
$ticket = main::buildDefaultRequest("wsTicket");
192-
if (!$ticket['success']) {
193-
header('Location: ' . main::getUrl() . "/cluster?action&success=false&message=notFound");
194-
die();
195-
}
196191

197-
$ticket = $ticket['id'];
192+
$ticket = main::requestWsTicket("cluster?action&success=false&message=notFound");
198193

199194
include "../pages/header.php";
200195
include "../pages/webinterface/cluster/console.php";

src/webinterface/main.php

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace webinterface;
44

5-
use JetBrains\PhpStorm\ArrayShape;
65
use JetBrains\PhpStorm\Pure;
76

87
class main
@@ -35,9 +34,7 @@ public static function getUrl($only = "all"): string
3534
$pfad = $config['url']['pfad'];
3635
$without_sub = $config['url']['without_sub'];
3736

38-
if ($only == "all") {
39-
return $ssl . "" . $main . "" . $pfad;
40-
} elseif ($only == "pfad") {
37+
if ($only == "pfad") {
4138
return $pfad;
4239
} elseif ($only == "main") {
4340
return $main;
@@ -60,6 +57,17 @@ public static function getUrl($only = "all"): string
6057
return self::getconfig()['cloudnet']['socket']['protocol'] . self::getconfig()['cloudnet']['socket']['ip'] . ":" . self::getconfig()['cloudnet']['socket']['port'] . self::getconfig()['cloudnet']['socket']['path'];
6158
}
6259

60+
public static function requestWsTicket(string $errorPathRedirect): string
61+
{
62+
$ticket = self::buildDefaultRequest("wsTicket");
63+
if (!$ticket['success']) {
64+
header('Location: ' . main::getUrl() . "/$errorPathRedirect");
65+
die();
66+
}
67+
68+
return $ticket['id'];
69+
}
70+
6371
public static function buildDefaultRequest($url, $method = "POST", $headers = array(), $params = array()): mixed
6472
{
6573
return self::buildRequest($url, $_SESSION['cn3-wi-access_token'], $method, $headers, $params);
@@ -121,30 +129,37 @@ public static function getVersion(): array
121129
if ($response === FALSE) {
122130
return array("success" => false, "response" => "server down");
123131
}
132+
124133
return json_decode($json, true);
125134
}
126135

127136
public static function testIfLatestVersion(): array
128137
{
129138
$version = self::getCurrentVersion();
130-
$version_e = self::getVersion();
131-
if(!$version_e['success']) return array("success" => false, "response" => array("error_code" => 503, "error_message" => "version-server down"));
139+
$version_latest = self::getVersion();
140+
141+
if (!$version_latest['success']) {
142+
return array("success" => false, "response" => array(
143+
"error_code" => 503,
144+
"error_message" => "version-server down"
145+
));
146+
}
132147

133-
if($version != $version_e['response']['version']){
134-
return array("success" => false, "response" => array("error_code" => 202, "error_message" => "not latest version", "error_extra" => array("current" => $version, "latest" => $version_e['response']['version'])));
148+
if ($version != $version_latest['response']['version']) {
149+
return array("success" => false, "response" => array(
150+
"error_code" => 202,
151+
"error_message" => "not latest version",
152+
"error_extra" => array(
153+
"current" => $version,
154+
"latest" => $version_latest['response']['version'])
155+
));
135156
} else {
136157
return array("success" => true);
137158
}
138159
}
160+
139161
public static function validCSRF(): bool
140162
{
141-
if (isset($_POST['csrf'])) {
142-
if ($_POST['csrf'] != $_SESSION['cn3-wi-csrf']) {
143-
return false;
144-
}
145-
} else {
146-
return false;
147-
}
148-
return true;
163+
return isset($_POST['csrf']) ?? false and $_POST['csrf'] == $_SESSION['cn3-wi-csrf'];
149164
}
150165
}

0 commit comments

Comments
 (0)