-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsetup_settings.php
More file actions
98 lines (81 loc) · 2.64 KB
/
setup_settings.php
File metadata and controls
98 lines (81 loc) · 2.64 KB
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
<?php
/**
* Created by PhpStorm.
* Author: popstarfreas (https://dark-gaming.com/profile/popstarfreas)
* Date: 26/12/14
* Time: 21:11
*/
if (!defined('index')) exit;
function handle_form($post, $ctx)
{
$errors = array();
$ip = $post['ip'];
$port = $post['port'];
$rest_user = $post['username'];
$rest_pass = $post['password'];
if(empty($ip)) {
$errors[] = "IP cannot be empty";
}
if(empty($port)) {
$errors[] = "Port cannot be empty";
}
if(empty($rest_user)) {
$errors[] = "Username cannot be empty";
}
if(!empty($errors)) return $errors;
$location = "$ip:$port";
// Test Connection
$response = json_decode(file_get_contents("http://$location/v2/token/create?username=$rest_user&password=$rest_pass", 0, $ctx));
if (empty($response)) {
$errors[] = "Failed to connect to $location";
} else {
if ($response->status == "401") {
$errors[] = 'Invalid username/password combination';
}
if ($response->status == "200") {
// Now we're going to setup ip, port and password
$contents =
"<?php
\$ip = '$ip';
\$port = '$port';
\$rest_user = '$rest_user';
\$rest_pass = '$rest_pass';
";
file_put_contents('settings.php', $contents);
}
}
return $errors;
}
$setup = false;
$form = true;
// Check values are correct and then test connection
if (!empty($_POST['ip'])) {
$errors = handle_form($_POST, $ctx);
if (empty($errors)) {
$form = false;
$setup = true;
}
}
if ($form) {
// Error list
if (!empty($errors)) {
$list = "";
foreach ($errors as $e) {
$list .= '<li>' . $e . '</li>';
}
echo "<ul>$list</ul>";
}
$ip = isset($_POST['ip']) ? $_POST['ip'] : '';
$port = isset($_POST['port']) ? $_POST['port'] : '';
$username = isset($_POST['username']) ? $_POST['username'] : '';
$password = isset($_POST['password']) ? $_POST['password'] : '';
echo 'Please enter the values for the REST API access
<form action="" method="POST">
IP: <input type="text" name="ip" placeholder="127.0.0.1" value="'.$ip.'"/> <br />
Port: <input type="text" name="port" placeholder="7878" value="'.$port.'"/> <br />
Username: <input type="text" name="username" placeholder="RestUser" value="'.$username.'"/> <br />
Password: <input type="password" name="password" value="'.$password.'"/> <br />
<input type="submit" value="Submit" />
</form>';
exit;
}