Bearsampp
2026.7.11
Toggle main menu visibility
Loading...
Searching...
No Matches
class.core.php
Go to the documentation of this file.
1
<?php
2
/*
3
*
4
* * Copyright (c) 2021-2024 Bearsampp
5
* * License: GNU General Public License version 3 or later; see LICENSE.txt
6
* * Website: https://bearsampp.com
7
* * Github: https://github.com/Bearsampp
8
*
9
*/
10
18
class
Core
19
{
20
// Constants for various file names and versions
21
const
isRoot_FILE
=
'root.php'
;
22
const
PATH_WIN_PLACEHOLDER
=
'~BEARSAMPP_WIN_PATH~'
;
23
const
PATH_LIN_PLACEHOLDER
=
'~BEARSAMPP_LIN_PATH~'
;
24
25
const
PHP_EXE
=
'php-win.exe'
;
26
const
SETENV_EXE
=
'SetEnv.exe'
;
27
const
NSSM_EXE
=
'nssm.exe'
;
28
const
OPENSSL_EXE
=
'openssl.exe'
;
29
const
OPENSSL_CONF
=
'openssl.cfg'
;
30
const
HOSTSEDITOR_EXE
=
'hEdit_x64.exe'
;
31
const
LN_EXE
=
'ln.exe'
;
32
const
PWGEN_EXE
=
"PwTech.exe"
;
33
34
const
APP_VERSION
=
'version.dat'
;
35
const
LAST_PATH
=
'lastPath.dat'
;
36
const
EXEC
=
'exec.dat'
;
37
const
LOADING_PID
=
'loading.pid'
;
38
44
public
function
__construct
()
45
{
46
if
( extension_loaded(
'winbinder'
) ) {
47
require_once
Path::getLibsPath
() .
'/winbinder/winbinder.php'
;
48
}
49
}
50
56
public
function
getAppVersion
()
57
{
58
global
$bearsamppLang
;
59
60
$filePath =
Path::getResourcesPath
() .
'/'
. self::APP_VERSION;
61
if
( !is_file( $filePath ) ) {
62
Log::error
( sprintf(
$bearsamppLang
->getValue(
Lang::ERROR_CONF_NOT_FOUND
),
APP_TITLE
, $filePath ) );
63
64
return
null
;
65
}
66
67
return
trim( file_get_contents( $filePath ) );
68
}
69
75
public
function
getLastPathContent
()
76
{
77
return
@file_get_contents(
Path::getLastPath
() );
78
}
79
85
public
function
getPreviousExec
()
86
{
87
$file = $this->
getExec
();
88
if
(file_exists($file)) {
89
return
trim(file_get_contents($file));
90
}
91
return
false
;
92
}
93
101
public
function
getExec
($aetrayPath =
false
)
102
{
103
return
Path::getTmpPath
( $aetrayPath ) .
'/'
. self::EXEC;
104
}
105
111
public
function
setExec
($action)
112
{
113
file_put_contents( $this->
getExec
(), $action );
114
}
115
123
public
function
getLoadingPid
($aetrayPath =
false
)
124
{
125
return
Path::getResourcesPath
( $aetrayPath ) .
'/'
. self::LOADING_PID;
126
}
127
133
public
function
addLoadingPid
($pid)
134
{
135
file_put_contents( $this->
getLoadingPid
(), $pid . PHP_EOL, FILE_APPEND );
136
}
137
143
public
function
__toString
()
144
{
145
return
'core object'
;
146
}
147
168
public
function
unzipFile
($filePath, $destination, $progressCallback =
null
)
169
{
170
global
$bearsamppRoot
;
171
172
$sevenZipPath =
Path::getLibsPath
() .
'/7zip/7za.exe'
;
173
174
if
( !file_exists( $sevenZipPath ) ) {
175
Log::error
(
'7za.exe not found at: '
. $sevenZipPath );
176
177
return
false
;
178
}
179
180
if
($progressCallback) {
181
call_user_func($progressCallback,
'Initializing archive test...'
);
182
}
183
184
// Test the archive to determine the number of files
185
if
($progressCallback) {
186
call_user_func($progressCallback,
'Analyzing archive...'
);
187
}
188
$testOutput =
CommandRunner::exec
($sevenZipPath, [
't'
, $filePath,
'-y'
,
'-bsp1'
]);
189
preg_match(
'/Files: (\d+)/'
, $testOutput !==
false
? $testOutput :
''
, $matches);
190
$numFiles = isset($matches[1]) ? (int) $matches[1] : 0;
191
Log::trace
(
'Number of files to be extracted: '
. $numFiles);
192
193
if
($progressCallback) {
194
call_user_func($progressCallback,
'Initializing extraction...'
);
195
}
196
// Extract the archive, streaming progress line-by-line
197
$returnVar =
CommandRunner::stream
(
198
$sevenZipPath,
199
[
'x'
, $filePath,
'-y'
,
'-bsp1'
,
'-bb0'
,
'-o'
. $destination],
200
function
(
string
$line) use ($progressCallback) {
201
Log::trace
(
"Processing line: $line"
);
202
if
($line ===
'Everything is Ok'
) {
203
if
($progressCallback) {
204
Log::trace
(
'Extraction progress: 100%'
);
205
call_user_func($progressCallback, 100);
206
}
207
} elseif ($progressCallback && preg_match(
'/(?:^|\s)(\d+)%/'
, $line, $matches)) {
208
$currentPercentage = intval($matches[1]);
209
Log::trace
(
"Extraction progress: $currentPercentage%"
);
210
call_user_func($progressCallback, $currentPercentage);
211
}
else
{
212
Log::trace
(
"Line did not match pattern: $line"
);
213
}
214
}
215
);
216
217
if
($returnVar ===
false
) {
218
Log::error
(
'Failed to open process for: '
. $sevenZipPath);
219
return
[
'error'
=>
'Failed to open process'
,
'numFiles'
=> $numFiles];
220
}
221
222
Log::trace
(
'Command return value: '
. $returnVar);
223
224
if
($returnVar === 0 && $progressCallback) {
225
Log::trace
(
'Extraction completed successfully. Setting progress to 100%'
);
226
call_user_func($progressCallback, 100);
227
usleep(100000);
// 100 milliseconds
228
}
229
230
if
($returnVar === 0) {
231
Log::debug
(
'Successfully unzipped file to: '
. $destination);
232
return
[
'success'
=>
true
,
'numFiles'
=> $numFiles];
233
}
234
235
Log::error
(
'Failed to unzip file. Command return value: '
. $returnVar);
236
return
[
'error'
=>
'Failed to unzip file'
,
'numFiles'
=> $numFiles];
237
}
238
253
public
function
getFileFromUrl
(
string
$moduleUrl,
string
$filePath, $progressBar =
false
)
254
{
255
// Open the URL for reading
256
$inputStream = @fopen( $moduleUrl,
'rb'
);
257
if
( $inputStream ===
false
) {
258
Log::error
(
'Error fetching content from URL: '
. $moduleUrl );
259
260
return
[
'error'
=>
'Error fetching module'
];
261
}
262
263
// Open the file for writing
264
$outputStream = @fopen( $filePath,
'wb'
);
265
if
( $outputStream ===
false
) {
266
Log::error
(
'Error opening file for writing: '
. $filePath );
267
fclose( $inputStream );
268
269
return
[
'error'
=>
'Error saving module'
];
270
}
271
272
// Read and write in chunks to avoid memory overload
273
$bufferSize = 8096;
// 8KB
274
$chunksRead = 0;
275
276
while
( !feof( $inputStream ) ) {
277
$buffer = fread( $inputStream, $bufferSize );
278
fwrite( $outputStream, $buffer );
279
$chunksRead++;
280
281
// Send progress update
282
if
( $progressBar ) {
283
$progress = $chunksRead;
284
echo json_encode( [
'progress'
=> $progress] ) . PHP_EOL;
285
286
// Check if output buffering is active before calling ob_flush()
287
if
( ob_get_length() !==
false
) {
288
ob_flush();
289
}
290
flush();
291
}
292
}
293
294
fclose( $inputStream );
295
fclose( $outputStream );
296
297
return
[
'success'
=>
true
];
298
}
299
}
$bearsamppLang
global $bearsamppLang
Definition
ajax.apache.php:16
$bearsamppRoot
global $bearsamppRoot
Definition
ajax.apache.php:16
CommandRunner\stream
static stream(string $executable, array $args, callable $lineCallback)
Definition
class.commandrunner.php:126
CommandRunner\exec
static exec(string $executable, array $args=[], string &$stderr='')
Definition
class.commandrunner.php:51
Core
Definition
class.core.php:19
Core\OPENSSL_CONF
const OPENSSL_CONF
Definition
class.core.php:29
Core\unzipFile
unzipFile($filePath, $destination, $progressCallback=null)
Definition
class.core.php:168
Core\__construct
__construct()
Definition
class.core.php:44
Core\PHP_EXE
const PHP_EXE
Definition
class.core.php:25
Core\PWGEN_EXE
const PWGEN_EXE
Definition
class.core.php:32
Core\isRoot_FILE
const isRoot_FILE
Definition
class.core.php:21
Core\getAppVersion
getAppVersion()
Definition
class.core.php:56
Core\addLoadingPid
addLoadingPid($pid)
Definition
class.core.php:133
Core\getPreviousExec
getPreviousExec()
Definition
class.core.php:85
Core\getExec
getExec($aetrayPath=false)
Definition
class.core.php:101
Core\getLastPathContent
getLastPathContent()
Definition
class.core.php:75
Core\__toString
__toString()
Definition
class.core.php:143
Core\getFileFromUrl
getFileFromUrl(string $moduleUrl, string $filePath, $progressBar=false)
Definition
class.core.php:253
Core\PATH_LIN_PLACEHOLDER
const PATH_LIN_PLACEHOLDER
Definition
class.core.php:23
Core\EXEC
const EXEC
Definition
class.core.php:36
Core\getLoadingPid
getLoadingPid($aetrayPath=false)
Definition
class.core.php:123
Core\NSSM_EXE
const NSSM_EXE
Definition
class.core.php:27
Core\SETENV_EXE
const SETENV_EXE
Definition
class.core.php:26
Core\LN_EXE
const LN_EXE
Definition
class.core.php:31
Core\PATH_WIN_PLACEHOLDER
const PATH_WIN_PLACEHOLDER
Definition
class.core.php:22
Core\LOADING_PID
const LOADING_PID
Definition
class.core.php:37
Core\setExec
setExec($action)
Definition
class.core.php:111
Core\HOSTSEDITOR_EXE
const HOSTSEDITOR_EXE
Definition
class.core.php:30
Core\APP_VERSION
const APP_VERSION
Definition
class.core.php:34
Core\OPENSSL_EXE
const OPENSSL_EXE
Definition
class.core.php:28
Core\LAST_PATH
const LAST_PATH
Definition
class.core.php:35
Lang\ERROR_CONF_NOT_FOUND
const ERROR_CONF_NOT_FOUND
Definition
class.lang.php:153
Log\debug
static debug($data, $file=null)
Definition
class.log.php:616
Log\trace
static trace($data, $file=null)
Definition
class.log.php:605
Log\error
static error($data, $file=null)
Definition
class.log.php:650
Path\getResourcesPath
static getResourcesPath($aetrayPath=false)
Definition
class.path.php:841
Path\getTmpPath
static getTmpPath($aetrayPath=false)
Definition
class.path.php:940
Path\getLibsPath
static getLibsPath($aetrayPath=false)
Definition
class.path.php:582
Path\getLastPath
static getLastPath($aetrayPath=false)
Definition
class.path.php:571
APP_TITLE
const APP_TITLE
Definition
root.php:13
sandbox
core
classes
class.core.php
Generated by
1.17.0