-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphpterminal.php
More file actions
263 lines (247 loc) · 7.71 KB
/
phpterminal.php
File metadata and controls
263 lines (247 loc) · 7.71 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
/**
* PHP Terminal
* Main Entry Point
*
* @package PHPTerminal
* @version 2.0.0
* @license MIT
*/
// Define application constants
define('ABSPATH', dirname(__FILE__) . '/');
define('PHPTERM_PATH', ABSPATH . 'phpterminal/');
define('PHPTERM_URL', 'http' . (!empty($_SERVER['HTTPS']) ? 's' : '') . '://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
define('PHPTERM_VERSION', '2.0.0');
define('PHPTERM_DOMAIN', 'phpterm');
// Load environment variables if .env file exists
if (file_exists(ABSPATH . '.env')) {
$lines = file(ABSPATH . '.env', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
if (strpos($line, '=') !== false && strpos($line, '#') !== 0) {
list($key, $value) = explode('=', $line, 2);
putenv(trim($key) . '=' . trim($value));
}
}
}
// Set default configuration values
define('PHPTERM_USERNAME', getenv('PHPTERM_USERNAME') ?: 'admin');
define('PHPTERM_PASSWORD', getenv('PHPTERM_PASSWORD') ?: 'admin');
define('PHPTERM_SESSION_TIMEOUT', getenv('PHPTERM_SESSION_TIMEOUT') ?: 3600);
define('PHPTERM_MAX_FILE_SIZE', getenv('PHPTERM_MAX_FILE_SIZE') ?: 10 * 1024 * 1024);
define('PHPTERM_DEBUG', getenv('PHPTERM_DEBUG') === 'true');
// Set error reporting based on debug mode
if (PHPTERM_DEBUG) {
error_reporting(E_ALL);
ini_set('display_errors', 1);
} else {
error_reporting(0);
ini_set('display_errors', 0);
}
// Set timezone
if (!ini_get('date.timezone')) {
date_default_timezone_set('UTC');
}
// Start session if not already started
if (session_status() === PHP_SESSION_NONE) {
session_name('PHPTERMINAL_SESSION');
session_set_cookie_params([
'lifetime' => PHPTERM_SESSION_TIMEOUT,
'path' => '/',
'domain' => '',
'secure' => isset($_SERVER['HTTPS']),
'httponly' => true,
'samesite' => 'Strict'
]);
session_start();
}
// Load required files
require_once(PHPTERM_PATH . 'config/config.php');
require_once(PHPTERM_PATH . 'config/security.php');
require_once(PHPTERM_PATH . 'core/Application.php');
use PHPTerminal\Config\AuthenticationManager;
use PHPTerminal\Core\Application;
try {
// Initialize configuration
\PHPTerminal\Config\Config::init();
// Handle authentication
if (!AuthenticationManager::isAuthenticated()) {
// Check for basic auth credentials
$username = $_SERVER['PHP_AUTH_USER'] ?? '';
$password = $_SERVER['PHP_AUTH_PW'] ?? '';
if (empty($username) || empty($password)) {
header('WWW-Authenticate: Basic realm="PHP Terminal"');
header('HTTP/1.0 401 Unauthorized');
echo '<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PHP Terminal - Authentication Required</title>
<link rel="stylesheet" href="' . dirname(PHPTERM_URL) . '/phpterminal/media/fonts/jetbrainsmono.css" />
<style>
body {
margin: 0;
padding: 0;
background: #1e1e1e;
color: #fff;
font-family: jetbrains_monoregular, monovoid, monospace, monospace;
font-size: 16px;
line-height: 1.4;
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
}
.auth-container {
text-align: center;
padding: 40px;
background: #2d2d2d;
border-radius: 8px;
box-shadow: 0 4px 20px rgba(0,0,0,0.3);
}
.logo {
font-size: 24px;
font-weight: bold;
color: #007acc;
margin-bottom: 20px;
}
.message {
color: #ff6b6b;
margin-bottom: 20px;
}
.info {
color: #888;
font-size: 14px;
}
</style>
</head>
<body>
<div class="auth-container">
<div class="logo">🚀 PHP Terminal</div>
<div class="message">Authentication Required</div>
<div class="info">Please enter your credentials to access the terminal.</div>
</div>
</body>
</html>';
exit;
}
// Authenticate user
if (!AuthenticationManager::authenticate($username, $password)) {
header('WWW-Authenticate: Basic realm="PHP Terminal"');
header('HTTP/1.0 401 Unauthorized');
echo '<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PHP Terminal - Authentication Failed</title>
<link rel="stylesheet" href="' . dirname(PHPTERM_URL) . '/phpterminal/media/fonts/jetbrainsmono.css" />
<style>
body {
margin: 0;
padding: 0;
background: #1e1e1e;
color: #fff;
font-family: jetbrains_monoregular, monovoid, monospace, monospace;
font-size: 16px;
line-height: 1.4;
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
}
.auth-container {
text-align: center;
padding: 40px;
background: #2d2d2d;
border-radius: 8px;
box-shadow: 0 4px 20px rgba(0,0,0,0.3);
}
.logo {
font-size: 24px;
font-weight: bold;
color: #007acc;
margin-bottom: 20px;
}
.message {
color: #ff6b6b;
margin-bottom: 20px;
}
.info {
color: #888;
font-size: 14px;
}
</style>
</head>
<body>
<div class="auth-container">
<div class="logo">🚀 PHP Terminal</div>
<div class="message">Authentication Failed</div>
<div class="info">Invalid username or password. Please try again.</div>
</div>
</body>
</html>';
exit;
}
}
// Initialize and run the application
$app = new Application();
$app->handleRequest();
} catch (Exception $e) {
// Log error
error_log('PHP Terminal Error: ' . $e->getMessage());
// Show error page
http_response_code(500);
echo '<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PHP Terminal - Error</title>
<link rel="stylesheet" href="' . dirname(PHPTERM_URL) . '/phpterminal/media/fonts/jetbrainsmono.css" />
<style>
body {
margin: 0;
padding: 0;
background: #1e1e1e;
color: #fff;
font-family: jetbrains_monoregular, monovoid, monospace, monospace;
font-size: 16px;
line-height: 1.4;
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
}
.error-container {
text-align: center;
padding: 40px;
background: #2d2d2d;
border-radius: 8px;
box-shadow: 0 4px 20px rgba(0,0,0,0.3);
}
.logo {
font-size: 24px;
font-weight: bold;
color: #007acc;
margin-bottom: 20px;
}
.message {
color: #ff6b6b;
margin-bottom: 20px;
}
.info {
color: #888;
font-size: 14px;
}
</style>
</head>
<body>
<div class="error-container">
<div class="logo">🚀 PHP Terminal</div>
<div class="message">Internal Server Error</div>
<div class="info">An error occurred while loading the terminal. Please try again later.</div>
</div>
</body>
</html>';
}