Bearsampp 2026.7.11
Loading...
Searching...
No Matches
Core Class Reference

Public Member Functions

 __construct ()
 __toString ()
 addLoadingPid ($pid)
 getAppVersion ()
 getExec ($aetrayPath=false)
 getFileFromUrl (string $moduleUrl, string $filePath, $progressBar=false)
 getLastPathContent ()
 getLoadingPid ($aetrayPath=false)
 getPreviousExec ()
 setExec ($action)
 unzipFile ($filePath, $destination, $progressCallback=null)

Data Fields

const APP_VERSION = 'version.dat'
const EXEC = 'exec.dat'
const HOSTSEDITOR_EXE = 'hEdit_x64.exe'
const isRoot_FILE = 'root.php'
const LAST_PATH = 'lastPath.dat'
const LN_EXE = 'ln.exe'
const LOADING_PID = 'loading.pid'
const NSSM_EXE = 'nssm.exe'
const OPENSSL_CONF = 'openssl.cfg'
const OPENSSL_EXE = 'openssl.exe'
const PATH_LIN_PLACEHOLDER = '~BEARSAMPP_LIN_PATH~'
const PATH_WIN_PLACEHOLDER = '~BEARSAMPP_WIN_PATH~'
const PHP_EXE = 'php-win.exe'
const PWGEN_EXE = "PwTech.exe"
const SETENV_EXE = 'SetEnv.exe'

Detailed Description

Class Core

This class provides core functionalities and constants for the Bearsampp application. It includes methods for retrieving paths, managing application versions, and handling various executable files and configurations.

Definition at line 18 of file class.core.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( )

Core constructor.

Loads the WinBinder extension if available.

Definition at line 44 of file class.core.php.

45 {
46 if ( extension_loaded( 'winbinder' ) ) {
47 require_once Path::getLibsPath() . '/winbinder/winbinder.php';
48 }
49 }
static getLibsPath($aetrayPath=false)

References Path\getLibsPath().

Here is the call graph for this function:

Member Function Documentation

◆ __toString()

__toString ( )

Provides a string representation of the core object.

Returns
string A string describing the core object.

Definition at line 143 of file class.core.php.

144 {
145 return 'core object';
146 }

◆ addLoadingPid()

addLoadingPid ( $pid)

Adds a PID to the loading PID file.

Parameters
int$pidThe PID to add.

Definition at line 133 of file class.core.php.

134 {
135 file_put_contents( $this->getLoadingPid(), $pid . PHP_EOL, FILE_APPEND );
136 }
getLoadingPid($aetrayPath=false)

References getLoadingPid().

Here is the call graph for this function:

◆ getAppVersion()

getAppVersion ( )

Retrieves the application version.

Returns
string|null The application version or null if not found.

Definition at line 56 of file class.core.php.

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 }
global $bearsamppLang
const ERROR_CONF_NOT_FOUND
static error($data, $file=null)
static getResourcesPath($aetrayPath=false)
const APP_TITLE
Definition root.php:13

References $bearsamppLang, APP_TITLE, Log\error(), Lang\ERROR_CONF_NOT_FOUND, and Path\getResourcesPath().

Here is the call graph for this function:

◆ getExec()

getExec ( $aetrayPath = false)

Retrieves the path to the exec file.

Parameters
bool$aetrayPathWhether to format the path for AeTrayMenu.
Returns
string The path to the exec file.

Definition at line 101 of file class.core.php.

102 {
103 return Path::getTmpPath( $aetrayPath ) . '/' . self::EXEC;
104 }
static getTmpPath($aetrayPath=false)

References Path\getTmpPath().

Referenced by getPreviousExec(), and setExec().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getFileFromUrl()

getFileFromUrl ( string $moduleUrl,
string $filePath,
$progressBar = false )

Fetches a file from a given URL and saves it to a specified file path.

This method attempts to retrieve the content from the provided URL and save it to the specified file path. If any error occurs during fetching or saving, it logs the error and returns an error message. If the operation is successful, it returns the file path. The method also logs the file size if the input stream is a valid resource.

Parameters
string$moduleUrlThe URL from which to fetch the file content.
string$filePathThe path where the file content should be saved.
bool$progressBarOptional. Whether to display a progress bar during the download process. Default is false.
Returns
array Returns the file path if successful, or an array with an error message if an error occurs.

Definition at line 253 of file class.core.php.

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 }

References Log\error().

Here is the call graph for this function:

◆ getLastPathContent()

getLastPathContent ( )

Retrieves the content of the last path file.

Returns
string|false The content of the last path file or false on failure.

Definition at line 75 of file class.core.php.

76 {
77 return @file_get_contents( Path::getLastPath() );
78 }
static getLastPath($aetrayPath=false)

References Path\getLastPath().

Here is the call graph for this function:

◆ getLoadingPid()

getLoadingPid ( $aetrayPath = false)

Retrieves the path to the loading PID file.

Parameters
bool$aetrayPathWhether to format the path for AeTrayMenu.
Returns
string The path to the loading PID file.

Definition at line 123 of file class.core.php.

124 {
125 return Path::getResourcesPath( $aetrayPath ) . '/' . self::LOADING_PID;
126 }

References Path\getResourcesPath().

Referenced by addLoadingPid().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getPreviousExec()

getPreviousExec ( )

Retrieves the content of the exec file without unlinking it.

Returns
string|false The content of the exec file or false if it doesn't exist.

Definition at line 85 of file class.core.php.

86 {
87 $file = $this->getExec();
88 if (file_exists($file)) {
89 return trim(file_get_contents($file));
90 }
91 return false;
92 }
getExec($aetrayPath=false)

References getExec().

Here is the call graph for this function:

◆ setExec()

setExec ( $action)

Sets the content of the exec file.

Parameters
string$actionThe content to set in the exec file.

Definition at line 111 of file class.core.php.

112 {
113 file_put_contents( $this->getExec(), $action );
114 }

References getExec().

Here is the call graph for this function:

◆ unzipFile()

unzipFile ( $filePath,
$destination,
$progressCallback = null )

Unzips a file to the specified directory and provides progress updates.

This method uses the 7-Zip command-line tool to extract the contents of a zip file. It first tests the archive to determine the number of files to be extracted, then proceeds with the extraction while providing progress updates via a callback function.

Parameters
string$filePathThe path to the zip file.
string$destinationThe directory to extract the files to.
callable | null$progressCallbackA callback function to report progress. The callback receives two parameters:
  • int $currentFile: The current file number being extracted.
  • int $totalFiles: The total number of files to be extracted.

@global object $bearsamppRoot Global object to get core paths.

Returns
array|false An array containing the result of the extraction on success or failure:
  • On success: ['success' => true, 'numFiles' => int]
  • On failure: ['error' => string, 'numFiles' => int]
  • Returns false if the 7-Zip executable is not found.

Definition at line 168 of file class.core.php.

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 }
global $bearsamppRoot
static stream(string $executable, array $args, callable $lineCallback)
static exec(string $executable, array $args=[], string &$stderr='')
static debug($data, $file=null)
static trace($data, $file=null)

References $bearsamppRoot, Log\debug(), Log\error(), CommandRunner\exec(), Path\getLibsPath(), CommandRunner\stream(), and Log\trace().

Here is the call graph for this function:

Field Documentation

◆ APP_VERSION

const APP_VERSION = 'version.dat'

Definition at line 34 of file class.core.php.

◆ EXEC

const EXEC = 'exec.dat'

Definition at line 36 of file class.core.php.

◆ HOSTSEDITOR_EXE

const HOSTSEDITOR_EXE = 'hEdit_x64.exe'

Definition at line 30 of file class.core.php.

Referenced by Path\getHostsEditorExe().

◆ isRoot_FILE

const isRoot_FILE = 'root.php'

◆ LAST_PATH

const LAST_PATH = 'lastPath.dat'

Definition at line 35 of file class.core.php.

Referenced by Path\getLastPath().

◆ LN_EXE

const LN_EXE = 'ln.exe'

Definition at line 31 of file class.core.php.

Referenced by Path\getLnExe().

◆ LOADING_PID

const LOADING_PID = 'loading.pid'

Definition at line 37 of file class.core.php.

◆ NSSM_EXE

const NSSM_EXE = 'nssm.exe'

Definition at line 27 of file class.core.php.

Referenced by Path\getNssmExe().

◆ OPENSSL_CONF

const OPENSSL_CONF = 'openssl.cfg'

Definition at line 29 of file class.core.php.

Referenced by Path\getOpenSslConf().

◆ OPENSSL_EXE

const OPENSSL_EXE = 'openssl.exe'

Definition at line 28 of file class.core.php.

Referenced by Path\getOpenSslExe().

◆ PATH_LIN_PLACEHOLDER

const PATH_LIN_PLACEHOLDER = '~BEARSAMPP_LIN_PATH~'

Definition at line 23 of file class.core.php.

Referenced by Path\changePath().

◆ PATH_WIN_PLACEHOLDER

const PATH_WIN_PLACEHOLDER = '~BEARSAMPP_WIN_PATH~'

Definition at line 22 of file class.core.php.

Referenced by Path\changePath().

◆ PHP_EXE

const PHP_EXE = 'php-win.exe'

Definition at line 25 of file class.core.php.

Referenced by Path\getPhpExe().

◆ PWGEN_EXE

const PWGEN_EXE = "PwTech.exe"

Definition at line 32 of file class.core.php.

Referenced by Path\getPwgenExe().

◆ SETENV_EXE

const SETENV_EXE = 'SetEnv.exe'

Definition at line 26 of file class.core.php.

Referenced by Path\getSetEnvExe().


The documentation for this class was generated from the following file: