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

Commit d594270

Browse files
author
Moritz Walter
committed
add login
1 parent 47754ba commit d594270

File tree

10 files changed

+304
-53
lines changed

10 files changed

+304
-53
lines changed

config/version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
22
$version = array(
33
"version" => "1.0_alpha1",
4-
"version_url" => "https://project.the-systems.eu/api/resource/?resourceid=14&type=allinfos"
4+
"version_url" => "https://111project.the-systems.eu/api/resource/?resourceid=14&type=allinfos"
55
);

pages/footer.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<script type="text/javascript" src="/assets/js/materialize.min.js"></script>
2+
</body>
3+
</html>

pages/header.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!DOCTYPE html>
2+
<html lang="de">
3+
<head>
4+
<title></title>
5+
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
6+
<link type="text/css" rel="stylesheet" href="/assets/css/materialize.min.css" media="screen,projection"/>
7+
<style>
8+
nav ul a,
9+
nav .brand-logo {
10+
color: #444;
11+
}
12+
</style>
13+
14+
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
15+
</head>
16+
<body>
17+
<nav class="white" role="navigation">
18+
<div class="nav-wrapper container">
19+
<a id="logo-container" href="#" class="brand-logo">Logo</a>
20+
<ul class="right hide-on-med-and-down">
21+
<li><a href="#">Navbar Link</a></li>
22+
</ul>
23+
24+
<ul id="nav-mobile" class="sidenav">
25+
<li><a href="#">Navbar Link</a></li>
26+
</ul>
27+
<a href="#" data-target="nav-mobile" class="sidenav-trigger"><i class="material-icons">menu</i></a>
28+
</div>
29+
</nav>

pages/webinterface/index.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<div class="container">
2+
<div class="section">
3+
<div class="row">
4+
<div class="col s12 m6">
5+
<div class="card-panel">
6+
</div>
7+
</div>
8+
<div class="col s12 m6">
9+
<div class="card-panel">
10+
<h4 class="center">CloudNet - Webinterface</h4>
11+
<h5 class="center"><?= \webinterface\main::getMessage("version") ?> <?= \webinterface\main::getCurrentVersion(); ?> <?= \webinterface\main::getMessage("from") ?></h5>
12+
13+
<p class="center"><a href="https://discord.gg/CYHuDpx" class="btn center"><?= \webinterface\main::getMessage("supportdiscord") ?></a></p>
14+
<p class="center"><a href="https://www.spigotmc.org/resources/cloudnet-webinterface.58905/" class="btn center"><?= \webinterface\main::getMessage("spigotpage") ?></a></p>
15+
<?php
16+
$json = \webinterface\main::getVersion();
17+
$version = \webinterface\main::getCurrentVersion();
18+
$new_version = $json['response']['version'];
19+
print_r($json);
20+
if ($json['success'] != true) { ?>
21+
<h1><span style="color: #FF0000">Der Kontrollserver ist zurzeit nicht erreichbar.</span></h1><?php
22+
} elseif ($version != $new_version) { ?>
23+
<p><span style="color: #FF0000"> <?= \webinterface\main::getMessage("oldversion1") ?></span></p>
24+
<p><span style="color: #FF0000"> <?= \webinterface\main::getMessage("oldversion2") ?></span></p><?php
25+
}
26+
?>
27+
</div>
28+
</div>
29+
</div>
30+
</div>
31+
</div>

pages/webinterface/login.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<div class="container">
2+
<div class="section">
3+
<div class="row">
4+
<div class="col s12 m4 offset-m4">
5+
<div class="card-panel">
6+
<h5 class="center">Login</h5>
7+
<div class="row">
8+
<form class="col s12" method="post">
9+
<input type="hidden" name="action" value="login">
10+
<input type="hidden" name="csrf" value="<?= $_SESSION['cn3-wi-access_csrf']; ?>">
11+
12+
<div class="row">
13+
<div class="input-field col s12">
14+
<input id="username" type="text" name="username" class="validate">
15+
<label for="username">Benutzername</label>
16+
</div>
17+
</div>
18+
<div class="row">
19+
<div class="input-field col s12">
20+
<input id="password" type="password" name="password" class="validate">
21+
<label for="password">Password</label>
22+
</div>
23+
</div>
24+
<button type="submit" class="waves-effect waves-light btn">Anmelden</button>
25+
</form>
26+
</div>
27+
</div>
28+
</div>
29+
</div>
30+
</div>
31+
</div>

public/assets/.htaccess

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
RewriteEngine Off

public/index.php

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
2-
use webinterface;
2+
3+
use webinterface\main;
4+
35
session_start();
46

57
define('DS', DIRECTORY_SEPARATOR, true);
@@ -9,7 +11,7 @@
911
$path_vendor = __DIR__ . '/../vendor/autoload.php';
1012
$path_config = BASE_PATH . '../config/config.php';
1113
$path_version = BASE_PATH . '../config/version.php';
12-
$path_message = BASE_PATH . '../config/message.json';
14+
$path_message = BASE_PATH . '../config/messages.json';
1315

1416
if (file_exists($path_vendor)) {
1517
require $path_vendor;
@@ -34,17 +36,73 @@
3436
die('<h1><span style="color: #FF0000">Ein Fehler ist aufgetreten.</span></h1><h3>Die Datei "/config/message.json" konnte nicht gefunden werden</h3><h3>Führe das Setup mit "wisetup" im Master erneut aus!</h3>');
3537
}
3638

37-
$main = new webinterface($config, $version);
39+
$main = new webinterface\main($config, $version);
3840

3941
$app = System\App::instance();
4042
$app->request = System\Request::instance();
4143
$app->route = System\Route::instance($app->request);
4244

4345
$route = $app->route;
4446

47+
if(isset($_SESSION['cn3-wi-access_token'])){
48+
$route->any('/', function () {
49+
if(isset($_POST['action'])){
50+
if (isset($_POST['csrf'])) {
51+
if ($_POST['csrf'] != $_SESSION['csrf']) {
52+
header('Location: ' . main::getUrl() . "/?action&success=false&message=csrfFailed");
53+
die();
54+
}
55+
} else {
56+
header('Location: ' . main::getUrl() . "/?action&success=false&message=csrfFailed");
57+
die();
58+
}
59+
60+
if($_POST['action'] == "login" AND isset($_POST['username']) AND isset($_POST['password'])){
61+
$action = \webinterface\authorizeController::login($_POST['username'], $_POST['password']);
62+
if($action['success'] == true) {
63+
header('Location: ' . main::getUrl());
64+
} else {
65+
header('Location: ' . main::getUrl() . "/?action&success=false&message=loginFailed");
66+
}
67+
die();
68+
}
69+
}
4570

46-
$this->any('/', function () {
71+
include "../pages/header.php";
72+
include "../pages/webinterface/index.php";
73+
include "../pages/footer.php";
74+
75+
});
76+
} else {
77+
$route->any('/', function () {
78+
if(isset($_POST['action'])){
79+
if (isset($_POST['csrf'])) {
80+
if ($_POST['csrf'] != $_SESSION['csrf']) {
81+
header('Location: ' . main::getUrl() . "/?action&success=false&message=csrfFailed");
82+
die();
83+
}
84+
} else {
85+
header('Location: ' . main::getUrl() . "/?action&success=false&message=csrfFailed");
86+
die();
87+
}
88+
89+
if($_POST['action'] == "login" AND isset($_POST['username']) AND isset($_POST['password'])){
90+
$action = \webinterface\authorizeController::login($_POST['username'], $_POST['password']);
91+
if($action['success'] == true) {
92+
header('Location: ' . main::getUrl());
93+
} else {
94+
header('Location: ' . main::getUrl() . "/?action&success=false&message=loginFailed");
95+
}
96+
die();
97+
}
98+
}
99+
100+
include "../pages/header.php";
101+
include "../pages/webinterface/login.php";
102+
include "../pages/footer.php";
103+
104+
});
105+
}
47106

48-
});
49107

50108
$route->end();
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
namespace webinterface;
3+
4+
class authorizeController
5+
{
6+
public static function login($username, $password){
7+
$url = main::getconfig()['cloudnet']['protocol'] . main::getconfig()['cloudnet']['ip'] . ":" . main::getconfig()['cloudnet']['port'] . main::getconfig()['cloudnet']['path'] . "/auth";
8+
$token = base64_encode($username.":".$password);
9+
10+
$curl = curl_init();
11+
12+
curl_setopt_array($curl, array(
13+
CURLOPT_URL => $url,
14+
CURLOPT_RETURNTRANSFER => true,
15+
CURLOPT_MAXREDIRS => 1,
16+
CURLOPT_TIMEOUT => 5,
17+
CURLOPT_CUSTOMREQUEST => "POST",
18+
CURLOPT_HTTPHEADER => array(
19+
'Authorization: Basic '.$token
20+
),
21+
));
22+
23+
$response = curl_exec($curl);
24+
if($response === FALSE){
25+
return array("success" => false, "response" => "server down");
26+
}
27+
28+
curl_close($curl);
29+
$response = json_decode($response, true);
30+
31+
if($response['success'] == true){
32+
$_SESSION['cn3-wi-access_token'] = $response['token'];
33+
34+
return array("success" => true, "response" => "login success");
35+
} else {
36+
return array("success" => false, "response" => "password wrong");
37+
}
38+
}
39+
}

src/webinterface/main.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
namespace webinterface;
3+
4+
use JetBrains\PhpStorm\Pure;
5+
6+
class main
7+
{
8+
protected static array $configObj;
9+
protected static array $versionObj;
10+
11+
public function __construct($config, $version)
12+
{
13+
self::$configObj = $config;
14+
self::$versionObj = $version;
15+
}
16+
17+
public static function getConfig(): array
18+
{
19+
return self::$configObj;
20+
}
21+
public static function getVersionObj(): array
22+
{
23+
return self::$versionObj;
24+
}
25+
26+
27+
public static function buildRequest($url, $token, $method = "POST", $params = array()){
28+
$url = self::getconfig()['cloudnet']['protocol'] . self::getconfig()['cloudnet']['ip'] . ":" . self::getconfig()['cloudnet']['port'] . self::getconfig()['cloudnet']['path'] . "/".$url;
29+
30+
$curl = curl_init();
31+
32+
curl_setopt_array($curl, array(
33+
CURLOPT_URL => $url,
34+
CURLOPT_RETURNTRANSFER => true,
35+
CURLOPT_MAXREDIRS => 1,
36+
CURLOPT_TIMEOUT => 5,
37+
CURLOPT_CUSTOMREQUEST => $method,
38+
CURLOPT_POSTFIELDS => $params,
39+
CURLOPT_HTTPHEADER => array(
40+
'Authorization: Bearer '.$token
41+
),
42+
));
43+
44+
$response = curl_exec($curl);
45+
46+
curl_close($curl);
47+
48+
return json_decode($response, true);
49+
}
50+
51+
public static function getUrl($only = "all"): string
52+
{
53+
$config = self::$configObj;
54+
55+
$main = $config['url']['main'];
56+
$ssl = $config['url']['ssl'];
57+
$pfad = $config['url']['pfad'];
58+
$without_sub = $config['url']['without_sub'];
59+
60+
if ($only == "all") {
61+
return $ssl . "" . $main . "" . $pfad;
62+
} elseif ($only == "pfad") {
63+
return $pfad;
64+
} elseif ($only == "main") {
65+
return $main;
66+
} elseif ($only == "ssl") {
67+
return $ssl;
68+
} elseif ($only == "without_sub") {
69+
return $without_sub;
70+
} else {
71+
return $ssl . "" . $main . "" . $pfad;
72+
}
73+
}
74+
public static function getMessage($key){
75+
$file = dirname(__FILE__) . "/../../config/message.json";
76+
$json = file_get_contents($file);
77+
$message = json_decode($json, true);
78+
if(isset($message[$key])) {
79+
return $message[$key];
80+
} else {
81+
return $key;
82+
}
83+
}
84+
85+
#[Pure] public static function getCurrentVersion()
86+
{
87+
return main::getVersionObj()['version'];
88+
}
89+
public static function getVersion(): array
90+
{
91+
$url = main::getVersionObj()['version_url'];
92+
93+
$ch = curl_init();
94+
curl_setopt($ch, CURLOPT_URL, $url);
95+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
96+
$json = curl_exec($ch);
97+
curl_close($ch);
98+
99+
$response = curl_exec($ch);
100+
if($response === FALSE){
101+
return array("success" => false, "response" => "server down");
102+
}
103+
return json_decode($json, true);
104+
}
105+
106+
}

0 commit comments

Comments
 (0)