Bearsampp 2026.7.11
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
18class 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}
global $bearsamppLang
global $bearsamppRoot
static stream(string $executable, array $args, callable $lineCallback)
static exec(string $executable, array $args=[], string &$stderr='')
const OPENSSL_CONF
unzipFile($filePath, $destination, $progressCallback=null)
__construct()
const PHP_EXE
const PWGEN_EXE
const isRoot_FILE
getAppVersion()
addLoadingPid($pid)
getPreviousExec()
getExec($aetrayPath=false)
getLastPathContent()
__toString()
getFileFromUrl(string $moduleUrl, string $filePath, $progressBar=false)
const PATH_LIN_PLACEHOLDER
const EXEC
getLoadingPid($aetrayPath=false)
const NSSM_EXE
const SETENV_EXE
const LN_EXE
const PATH_WIN_PLACEHOLDER
const LOADING_PID
setExec($action)
const HOSTSEDITOR_EXE
const APP_VERSION
const OPENSSL_EXE
const LAST_PATH
const ERROR_CONF_NOT_FOUND
static debug($data, $file=null)
static trace($data, $file=null)
static error($data, $file=null)
static getResourcesPath($aetrayPath=false)
static getTmpPath($aetrayPath=false)
static getLibsPath($aetrayPath=false)
static getLastPath($aetrayPath=false)
const APP_TITLE
Definition root.php:13