forked from Alanaktion/MCHostPanel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax.php
161 lines (144 loc) · 4.27 KB
/
ajax.php
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
require_once 'inc/lib.php';
session_start();
if (!$user = user_info($_SESSION['user']))
exit();
switch ($_POST['req']) {
case 'dir':
// Initial vars
$dirs = array();
$files = array();
// Get directory contents
$h = opendir($user['home'] . $_POST['dir']);
while (false !== ($f = readdir($h)))
if ($f != '.' && $f != '..')
if (is_dir($user['home'] . $_POST['dir'] . '/' . $f))
$dirs[] = $f;
elseif (is_file($user['home'] . $_POST['dir'] . '/' . $f))
$files[] = $f;
closedir($h);
unset($f);
// Sort data
sort($dirs);
sort($files);
// Get file sizes
$sizes = array();
foreach ($files as $f)
$sizes[] = filesize($user['home'] . $_POST['dir'] . '/' . $f);
// Output data
echo json_encode(array(
'dirs' => $dirs,
'files' => $files,
'sizes' => $sizes
));
break;
case 'file_get':
if (is_file($user['home'] . $_POST['file']))
echo file_get_contents($user['home'] . $_POST['file']);
break;
case 'file_put':
if (is_file($user['home'] . $_POST['file']))
file_put_contents($user['home'] . $_POST['file'], $_POST['data']);
break;
case 'delete':
foreach ($_POST['files'] as $f)
if (is_file($user['home'] . $f))
unlink($user['home'] . $f);
break;
case 'rename':
file_rename($_POST['path'], $_POST['newname'], $user['home']);
break;
case 'cron_exists':
header('Content-type: application/json');
echo json_encode(check_cron_exists($_POST['user']));
break;
case 'get_cron':
header('Content-type: application/json');
echo json_encode(get_cron($_POST['user']));
break;
case 'server_start':
echo server_start($user['user']);
break;
case 'server_cmd':
server_cmd($user['user'], $_POST['cmd']);
break;
case 'server_stop':
server_stop($user['user']);
break;
case 'server_kill':
server_kill($user['user']);
break;
case 'server_running':
echo json_encode(server_running($user['user']));
break;
case 'server_log':
/*if($files = glob($user['home'] . "screenlog.?*")) {
// Prefer GNU screen log
echo mclogparse2(file_backread($user['home']));
} else*/
if(is_file($user['home'] . "/logs/latest.log")) {
// 1.7 logs
echo mclogparse2(file_backread($user['home'] . '/logs/latest.log', 64));
} elseif(is_file($user['home'] . "/server.log")) {
// 1.6 and earlier
echo mclogparse2(file_backread($user['home'] . '/server.log', 64));
} elseif(is_file($user['home'] . "/proxy.log.0")) {
// BungeeCord
echo mclogparse2(file_backread($user['home'] . '/proxy.log.0', 64));
} else {
echo "No log file found.";
}
break;
case 'server_log_bytes':
header('Content-type: application/json');
// Find log file
if(is_file($user['home'] . '/logs/latest.log')) {
$file = $user['home'] . '/logs/latest.log';
} elseif(is_file($user['home'] . '/server.log')) {
$file = $user['home'] . '/server.log';
} elseif(is_file($user['home'] . '/proxy.log.0')) {
$file = $user['home'] . '/proxy.log.0';
} else {
exit(json_encode(array('error' => 1, 'msg' => 'No log file found.')));
}
// Get requested byte range
$start = isset($_REQUEST['start']) ? intval($_REQUEST['start']) : 0;
$end = isset($_REQUEST['end']) ? intval($_REQUEST['end']) : null;
$data = @file_get_contents($file, false, null, $start, $end);
$return = array(
'start' => $start,
'end' => $start + strlen($data),
'data' => $data,
);
if($data === false) {
$data = file_get_contents($file, false, null, 0, 30*1024);
$return = array(
'error' => 2,
'msg' => 'Failed to requested bytes from the log file. Returned first 30 KB.',
'start' => 0,
'end' => strlen($data),
'data' => $data,
);
}
echo json_encode($return);
case 'players':
require_once 'inc/MinecraftQuery.class.php';
$mq = new MinecraftQuery();
try {
$mq->Connect(KT_LOCAL_IP, $user['port'], 2); // 2 second timeout
} catch (MinecraftQueryException $ex) {
echo json_encode(array('error' => 1, 'msg' => $ex->getMessage()));
die();
}
$data = array(
'info' => $mq->GetInfo(),
'players' => $mq->GetPlayers()
);
echo json_encode($data);
break;
case 'set_jar':
$result = user_modify($user['user'], $user['pass'], $user['role'], $user['home'], $user['ram'], $user['port'], $_POST['jar']);
echo json_encode($result);
break;
}
?>