Bearsampp 2026.5.5
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 = "PWGenPortable.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 $this->getLibsPath() . '/winbinder/winbinder.php';
48 }
49 }
50
58 public function getLangsPath($aetrayPath = false)
59 {
60 global $bearsamppRoot;
61
62 return $bearsamppRoot->getCorePath( $aetrayPath ) . '/langs';
63 }
64
72 public function getLibsPath($aetrayPath = false)
73 {
74 global $bearsamppRoot;
75
76 return $bearsamppRoot->getCorePath( $aetrayPath ) . '/libs';
77 }
78
86 public function getResourcesPath($aetrayPath = false)
87 {
88 global $bearsamppRoot;
89
90 return $bearsamppRoot->getCorePath( $aetrayPath ) . '/resources';
91 }
92
100 public function getIconsPath($aetrayPath = false)
101 {
102 return $this->getImagesPath($aetrayPath) . '/icons';
103 }
104
112 public function getImagesPath($aetrayPath = false)
113 {
114 global $bearsamppCore;
115
116 return $bearsamppCore->getHomepagePath($aetrayPath) . '/img';
117 }
118
126 public function getScriptsPath($aetrayPath = false)
127 {
128 global $bearsamppRoot;
129
130 return $bearsamppRoot->getCorePath( $aetrayPath ) . '/scripts';
131 }
132
133 public function getHomepagePath($aetrayPath = false)
134 {
135 return $this->getResourcesPath( $aetrayPath ) . '/homepage';
136 }
137
138 public function getAjaxPath($aetrayPath = false)
139 {
140 return $this->getHomepagePath( $aetrayPath ) . '/ajax';
141 }
142
150 public function getScript($type)
151 {
152 return $this->getScriptsPath() . '/' . $type;
153 }
154
162 public function getTmpPath($aetrayPath = false)
163 {
164 global $bearsamppRoot;
165
166 return $bearsamppRoot->getCorePath( $aetrayPath ) . '/tmp';
167 }
168
176 public function getisRootFilePath($aetrayPath = false)
177 {
178 global $bearsamppRoot;
179
180 return $bearsamppRoot->getCorePath( $aetrayPath ) . '/' . self::isRoot_FILE;
181 }
182
188 public function getAppVersion()
189 {
190 global $bearsamppLang;
191
192 $filePath = $this->getResourcesPath() . '/' . self::APP_VERSION;
193 if ( !is_file( $filePath ) ) {
194 Log::error( sprintf( $bearsamppLang->getValue( Lang::ERROR_CONF_NOT_FOUND ), APP_TITLE, $filePath ) );
195
196 return null;
197 }
198
199 return trim( file_get_contents( $filePath ) );
200 }
201
209 public function getLastPath($aetrayPath = false)
210 {
211 return $this->getResourcesPath( $aetrayPath ) . '/' . self::LAST_PATH;
212 }
213
219 public function getLastPathContent()
220 {
221 return @file_get_contents( $this->getLastPath() );
222 }
223
229 public function getPreviousExec()
230 {
231 $file = $this->getExec();
232 if (file_exists($file)) {
233 return trim(file_get_contents($file));
234 }
235 return false;
236 }
237
245 public function getExec($aetrayPath = false)
246 {
247 return $this->getTmpPath( $aetrayPath ) . '/' . self::EXEC;
248 }
249
255 public function setExec($action)
256 {
257 file_put_contents( $this->getExec(), $action );
258 }
259
267 public function getLoadingPid($aetrayPath = false)
268 {
269 return $this->getResourcesPath( $aetrayPath ) . '/' . self::LOADING_PID;
270 }
271
277 public function addLoadingPid($pid)
278 {
279 file_put_contents( $this->getLoadingPid(), $pid . PHP_EOL, FILE_APPEND );
280 }
281
289 public function getPhpPath($aetrayPath = false)
290 {
291 return $this->getLibsPath( $aetrayPath ) . '/php';
292 }
293
301 public function getPhpExe($aetrayPath = false)
302 {
303 return $this->getPhpPath( $aetrayPath ) . '/' . self::PHP_EXE;
304 }
305
313 public function getSetEnvPath($aetrayPath = false)
314 {
315 return $this->getLibsPath( $aetrayPath ) . '/setenv';
316 }
317
325 public function getSetEnvExe($aetrayPath = false)
326 {
327 return $this->getSetEnvPath( $aetrayPath ) . '/' . self::SETENV_EXE;
328 }
329
337 public function getNssmPath($aetrayPath = false)
338 {
339 return $this->getLibsPath( $aetrayPath ) . '/nssm';
340 }
341
349 public function getNssmExe($aetrayPath = false)
350 {
351 return $this->getNssmPath( $aetrayPath ) . '/' . self::NSSM_EXE;
352 }
353
361 public function getOpenSslPath($aetrayPath = false)
362 {
363 return $this->getLibsPath( $aetrayPath ) . '/openssl';
364 }
365
373 public function getOpenSslExe($aetrayPath = false)
374 {
375 return $this->getOpenSslPath( $aetrayPath ) . '/' . self::OPENSSL_EXE;
376 }
377
385 public function getOpenSslConf($aetrayPath = false)
386 {
387 return $this->getOpenSslPath( $aetrayPath ) . '/' . self::OPENSSL_CONF;
388 }
389
397 public function getHostsEditorPath($aetrayPath = false)
398 {
399 return $this->getLibsPath( $aetrayPath ) . '/hostseditor';
400 }
401
409 public function getHostsEditorExe($aetrayPath = false)
410 {
411 return $this->getHostsEditorPath( $aetrayPath ) . '/' . self::HOSTSEDITOR_EXE;
412 }
413
421 public function getLnPath($aetrayPath = false)
422 {
423 return $this->getLibsPath( $aetrayPath ) . '/ln';
424 }
425
433 public function getLnExe($aetrayPath = false)
434 {
435 return $this->getLnPath( $aetrayPath ) . '/' . self::LN_EXE;
436 }
437
445 public function getPwgenPath($aetrayPath = false)
446 {
447 return $this->getLibsPath( $aetrayPath ) . '/pwgen';
448 }
449
457 public function getPwgenExe($aetrayPath = false)
458 {
459 return $this->getPwgenPath( $aetrayPath ) . '/' . self::PWGEN_EXE;
460 }
461
467 public function __toString()
468 {
469 return 'core object';
470 }
471
492 public function unzipFile($filePath, $destination, $progressCallback = null)
493 {
494 global $bearsamppRoot;
495
496 $sevenZipPath = $this->getLibsPath() . '/7zip/7za.exe';
497
498 if ( !file_exists( $sevenZipPath ) ) {
499 Log::error( '7za.exe not found at: ' . $sevenZipPath );
500
501 return false;
502 }
503
504 // Test the archive to determine the number of files
505 $testOutput = CommandRunner::exec($sevenZipPath, ['t', $filePath, '-y', '-bsp1']);
506 preg_match('/Files: (\d+)/', $testOutput !== false ? $testOutput : '', $matches);
507 $numFiles = isset($matches[1]) ? (int) $matches[1] : 0;
508 Log::trace('Number of files to be extracted: ' . $numFiles);
509
510 // Extract the archive, streaming progress line-by-line
511 $returnVar = CommandRunner::stream(
512 $sevenZipPath,
513 ['x', $filePath, '-y', '-bsp1', '-bb0', '-o' . $destination],
514 function (string $line) use ($progressCallback) {
515 Log::trace("Processing line: $line");
516 if ($line === 'Everything is Ok') {
517 if ($progressCallback) {
518 Log::trace('Extraction progress: 100%');
519 call_user_func($progressCallback, 100);
520 }
521 } elseif ($progressCallback && preg_match('/(?:^|\s)(\d+)%/', $line, $matches)) {
522 $currentPercentage = intval($matches[1]);
523 Log::trace("Extraction progress: $currentPercentage%");
524 call_user_func($progressCallback, $currentPercentage);
525 } else {
526 Log::trace("Line did not match pattern: $line");
527 }
528 }
529 );
530
531 if ($returnVar === false) {
532 Log::error('Failed to open process for: ' . $sevenZipPath);
533 return ['error' => 'Failed to open process', 'numFiles' => $numFiles];
534 }
535
536 Log::trace('Command return value: ' . $returnVar);
537
538 if ($returnVar === 0 && $progressCallback) {
539 Log::trace('Extraction completed successfully. Setting progress to 100%');
540 call_user_func($progressCallback, 100);
541 usleep(100000); // 100 milliseconds
542 }
543
544 if ($returnVar === 0) {
545 Log::debug('Successfully unzipped file to: ' . $destination);
546 return ['success' => true, 'numFiles' => $numFiles];
547 }
548
549 Log::error('Failed to unzip file. Command return value: ' . $returnVar);
550 return ['error' => 'Failed to unzip file', 'numFiles' => $numFiles];
551 }
552
567 public function getFileFromUrl(string $moduleUrl, string $filePath, $progressBar = false)
568 {
569 // Open the URL for reading
570 $inputStream = @fopen( $moduleUrl, 'rb' );
571 if ( $inputStream === false ) {
572 Log::error( 'Error fetching content from URL: ' . $moduleUrl );
573
574 return ['error' => 'Error fetching module'];
575 }
576
577 // Open the file for writing
578 $outputStream = @fopen( $filePath, 'wb' );
579 if ( $outputStream === false ) {
580 Log::error( 'Error opening file for writing: ' . $filePath );
581 fclose( $inputStream );
582
583 return ['error' => 'Error saving module'];
584 }
585
586 // Read and write in chunks to avoid memory overload
587 $bufferSize = 8096; // 8KB
588 $chunksRead = 0;
589
590 while ( !feof( $inputStream ) ) {
591 $buffer = fread( $inputStream, $bufferSize );
592 fwrite( $outputStream, $buffer );
593 $chunksRead++;
594
595 // Send progress update
596 if ( $progressBar ) {
597 $progress = $chunksRead;
598 echo json_encode( ['progress' => $progress] );
599
600 // Check if output buffering is active before calling ob_flush()
601 if ( ob_get_length() !== false ) {
602 ob_flush();
603 }
604 flush();
605 }
606 }
607
608 fclose( $inputStream );
609 fclose( $outputStream );
610
611 return ['success' => true];
612 }
613}
global $bearsamppLang
global $bearsamppRoot
global $bearsamppCore
static stream(string $executable, array $args, callable $lineCallback)
static exec(string $executable, array $args=[], string &$stderr='')
getAjaxPath($aetrayPath=false)
const OPENSSL_CONF
unzipFile($filePath, $destination, $progressCallback=null)
__construct()
getLibsPath($aetrayPath=false)
getLnPath($aetrayPath=false)
getLastPath($aetrayPath=false)
getPwgenExe($aetrayPath=false)
const PHP_EXE
const PWGEN_EXE
getNssmPath($aetrayPath=false)
getScript($type)
getSetEnvExe($aetrayPath=false)
getLnExe($aetrayPath=false)
const isRoot_FILE
getHomepagePath($aetrayPath=false)
getAppVersion()
addLoadingPid($pid)
getOpenSslPath($aetrayPath=false)
getPreviousExec()
getPhpPath($aetrayPath=false)
getResourcesPath($aetrayPath=false)
getSetEnvPath($aetrayPath=false)
getExec($aetrayPath=false)
getLastPathContent()
__toString()
getIconsPath($aetrayPath=false)
getisRootFilePath($aetrayPath=false)
getFileFromUrl(string $moduleUrl, string $filePath, $progressBar=false)
const PATH_LIN_PLACEHOLDER
const EXEC
getLangsPath($aetrayPath=false)
getPhpExe($aetrayPath=false)
getNssmExe($aetrayPath=false)
getLoadingPid($aetrayPath=false)
const NSSM_EXE
getPwgenPath($aetrayPath=false)
getOpenSslConf($aetrayPath=false)
const SETENV_EXE
getTmpPath($aetrayPath=false)
getImagesPath($aetrayPath=false)
const LN_EXE
const PATH_WIN_PLACEHOLDER
getScriptsPath($aetrayPath=false)
getOpenSslExe($aetrayPath=false)
const LOADING_PID
getHostsEditorExe($aetrayPath=false)
setExec($action)
const HOSTSEDITOR_EXE
getHostsEditorPath($aetrayPath=false)
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)
const APP_TITLE
Definition root.php:13