Bearsampp 2025.8.29
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
39 const SCRIPT_EXEC_SILENT = 'execSilent.vbs';
40
46 public function __construct()
47 {
48 if ( extension_loaded( 'winbinder' ) ) {
49 require_once $this->getLibsPath() . '/winbinder/winbinder.php';
50 }
51 }
52
60 public function getLangsPath($aetrayPath = false)
61 {
62 global $bearsamppRoot;
63
64 return $bearsamppRoot->getCorePath( $aetrayPath ) . '/langs';
65 }
66
74 public function getLibsPath($aetrayPath = false)
75 {
76 global $bearsamppRoot;
77
78 return $bearsamppRoot->getCorePath( $aetrayPath ) . '/libs';
79 }
80
88 public function getResourcesPath($aetrayPath = false)
89 {
90 global $bearsamppRoot;
91
92 return $bearsamppRoot->getCorePath( $aetrayPath ) . '/resources';
93 }
94
102 public function getIconsPath($aetrayPath = false)
103 {
104 return $this->getImagesPath($aetrayPath) . '/icons';
105 }
106
114 public function getImagesPath($aetrayPath = false)
115 {
116 global $bearsamppCore;
117
118 return $bearsamppCore->getHomepagePath($aetrayPath) . '/img';
119 }
120
128 public function getScriptsPath($aetrayPath = false)
129 {
130 global $bearsamppRoot;
131
132 return $bearsamppRoot->getCorePath( $aetrayPath ) . '/scripts';
133 }
134
135 public function getHomepagePath($aetrayPath = false)
136 {
137 return $this->getResourcesPath( $aetrayPath ) . '/homepage';
138 }
139
140 public function getAjaxPath($aetrayPath = false)
141 {
142 return $this->getHomepagePath( $aetrayPath ) . '/ajax';
143 }
144
152 public function getScript($type)
153 {
154 return $this->getScriptsPath() . '/' . $type;
155 }
156
164 public function getTmpPath($aetrayPath = false)
165 {
166 global $bearsamppRoot;
167
168 return $bearsamppRoot->getCorePath( $aetrayPath ) . '/tmp';
169 }
170
178 public function getisRootFilePath($aetrayPath = false)
179 {
180 global $bearsamppRoot;
181
182 return $bearsamppRoot->getCorePath( $aetrayPath ) . '/' . self::isRoot_FILE;
183 }
184
190 public function getAppVersion()
191 {
192 global $bearsamppLang;
193
194 $filePath = $this->getResourcesPath() . '/' . self::APP_VERSION;
195 if ( !is_file( $filePath ) ) {
196 Util::logError( sprintf( $bearsamppLang->getValue( Lang::ERROR_CONF_NOT_FOUND ), APP_TITLE, $filePath ) );
197
198 return null;
199 }
200
201 return trim( file_get_contents( $filePath ) );
202 }
203
211 public function getLastPath($aetrayPath = false)
212 {
213 return $this->getResourcesPath( $aetrayPath ) . '/' . self::LAST_PATH;
214 }
215
221 public function getLastPathContent()
222 {
223 return @file_get_contents( $this->getLastPath() );
224 }
225
233 public function getExec($aetrayPath = false)
234 {
235 return $this->getTmpPath( $aetrayPath ) . '/' . self::EXEC;
236 }
237
243 public function setExec($action)
244 {
245 file_put_contents( $this->getExec(), $action );
246 }
247
255 public function getLoadingPid($aetrayPath = false)
256 {
257 return $this->getResourcesPath( $aetrayPath ) . '/' . self::LOADING_PID;
258 }
259
265 public function addLoadingPid($pid)
266 {
267 file_put_contents( $this->getLoadingPid(), $pid . PHP_EOL, FILE_APPEND );
268 }
269
277 public function getPhpPath($aetrayPath = false)
278 {
279 return $this->getLibsPath( $aetrayPath ) . '/php';
280 }
281
289 public function getPhpExe($aetrayPath = false)
290 {
291 return $this->getPhpPath( $aetrayPath ) . '/' . self::PHP_EXE;
292 }
293
301 public function getSetEnvPath($aetrayPath = false)
302 {
303 return $this->getLibsPath( $aetrayPath ) . '/setenv';
304 }
305
313 public function getSetEnvExe($aetrayPath = false)
314 {
315 return $this->getSetEnvPath( $aetrayPath ) . '/' . self::SETENV_EXE;
316 }
317
325 public function getNssmPath($aetrayPath = false)
326 {
327 return $this->getLibsPath( $aetrayPath ) . '/nssm';
328 }
329
337 public function getNssmExe($aetrayPath = false)
338 {
339 return $this->getNssmPath( $aetrayPath ) . '/' . self::NSSM_EXE;
340 }
341
349 public function getOpenSslPath($aetrayPath = false)
350 {
351 return $this->getLibsPath( $aetrayPath ) . '/openssl';
352 }
353
361 public function getOpenSslExe($aetrayPath = false)
362 {
363 return $this->getOpenSslPath( $aetrayPath ) . '/' . self::OPENSSL_EXE;
364 }
365
373 public function getOpenSslConf($aetrayPath = false)
374 {
375 return $this->getOpenSslPath( $aetrayPath ) . '/' . self::OPENSSL_CONF;
376 }
377
385 public function getHostsEditorPath($aetrayPath = false)
386 {
387 return $this->getLibsPath( $aetrayPath ) . '/hostseditor';
388 }
389
397 public function getHostsEditorExe($aetrayPath = false)
398 {
399 return $this->getHostsEditorPath( $aetrayPath ) . '/' . self::HOSTSEDITOR_EXE;
400 }
401
409 public function getLnPath($aetrayPath = false)
410 {
411 return $this->getLibsPath( $aetrayPath ) . '/ln';
412 }
413
421 public function getLnExe($aetrayPath = false)
422 {
423 return $this->getLnPath( $aetrayPath ) . '/' . self::LN_EXE;
424 }
425
433 public function getPwgenPath($aetrayPath = false)
434 {
435 return $this->getLibsPath( $aetrayPath ) . '/pwgen';
436 }
437
445 public function getPwgenExe($aetrayPath = false)
446 {
447 return $this->getPwgenPath( $aetrayPath ) . '/' . self::PWGEN_EXE;
448 }
449
455 public function __toString()
456 {
457 return 'core object';
458 }
459
480 public function unzipFile($filePath, $destination, $progressCallback = null)
481 {
482 global $bearsamppRoot;
483
484 $sevenZipPath = $this->getLibsPath() . '/7zip/7za.exe';
485
486 if ( !file_exists( $sevenZipPath ) ) {
487 Util::logError( '7za.exe not found at: ' . $sevenZipPath );
488
489 return false;
490 }
491
492 // Command to test the archive and get the number of files
493 $testCommand = escapeshellarg( $sevenZipPath ) . ' t ' . escapeshellarg( $filePath ) . ' -y -bsp1';
494 $testOutput = shell_exec( $testCommand );
495
496 // Extract the number of files from the test command output
497 preg_match( '/Files: (\d+)/', $testOutput, $matches );
498 $numFiles = isset( $matches[1] ) ? (int) $matches[1] : 0;
499 Util::logTrace( 'Number of files to be extracted: ' . $numFiles );
500
501 // Command to extract the archive
502 $command = escapeshellarg( $sevenZipPath ) . ' x ' . escapeshellarg( $filePath ) . ' -y -bsp1 -bb0 -o' . escapeshellarg( $destination );
503 Util::logTrace( 'Executing command: ' . $command );
504
505 $process = popen( $command, 'rb' );
506
507 if ( $process ) {
508 $buffer = '';
509 while ( !feof( $process ) ) {
510 $buffer .= fread( $process, 8192 ); // Read in chunks of 8KB
511 while ( ($pos = strpos( $buffer, "\r" )) !== false ) {
512 $line = substr( $buffer, 0, $pos );
513 $buffer = substr( $buffer, $pos + 1 );
514 $line = trim( $line ); // Remove any leading/trailing whitespace
515 Util::logTrace( "Processing line: $line" );
516
517 // Check if the line indicates everything is okay
518 if ( $line === "Everything is Ok" ) {
519 if ( $progressCallback ) {
520 Util::logTrace( "Extraction progress: 100%" );
521 call_user_func( $progressCallback, 100 );
522 Util::logTrace( "Progress callback called with percentage: 100" );
523 }
524 }
525 else if ( $progressCallback && preg_match( '/(?:^|\s)(\d+)%/', $line, $matches ) ) {
526 $currentPercentage = intval( $matches[1] );
527 Util::logTrace( "Extraction progress: $currentPercentage%" );
528 call_user_func( $progressCallback, $currentPercentage );
529 Util::logTrace( "Progress callback called with percentage: $currentPercentage" );
530 }
531 else {
532 Util::logTrace( "Line did not match pattern: $line" );
533 }
534 }
535 }
536
537 // Process any remaining data in the buffer
538 if ( !empty( $buffer ) ) {
539 $line = trim( $buffer );
540 Util::logTrace( "Processing remaining line: $line" );
541
542 // Check if the remaining line indicates everything is okay
543 if ( $line === "Everything is Ok" ) {
544 if ( $progressCallback ) {
545 Util::logTrace( "Extraction progress: 100%" );
546 call_user_func( $progressCallback, 100 );
547 Util::logTrace( "Progress callback called with percentage: 100" );
548 }
549 }
550 else if ( $progressCallback && preg_match( '/(?:^|\s)(\d+)%/', $line, $matches ) ) {
551 $currentPercentage = intval( $matches[1] );
552 Util::logTrace( "Extraction progress: $currentPercentage%" );
553 call_user_func( $progressCallback, $currentPercentage );
554 Util::logTrace( "Progress callback called with percentage: $currentPercentage" );
555 }
556 else {
557 Util::logTrace( "Remaining line did not match pattern: $line" );
558 }
559 }
560
561 $returnVar = pclose( $process );
562 Util::logTrace( 'Command return value: ' . $returnVar );
563
564 // Set progress to 100% if the command was successful
565 if ( $returnVar === 0 && $progressCallback ) {
566 Util::logTrace( "Extraction completed successfully. Setting progress to 100%" );
567 call_user_func( $progressCallback, 100 );
568 Util::logTrace( "Progress callback called with percentage: 100" );
569
570 // Adding a small delay to ensure the progress bar update is processed
571 usleep( 100000 ); // 100 milliseconds
572 }
573
574 if ( $returnVar === 0 ) {
575 Util::logDebug( 'Successfully unzipped file to: ' . $destination );
576
577 return ['success' => true, 'numFiles' => $numFiles];
578 }
579 else {
580 Util::logError( 'Failed to unzip file. Command return value: ' . $returnVar );
581
582 return ['error' => 'Failed to unzip file', 'numFiles' => $numFiles];
583 }
584 }
585 else {
586 Util::logError( 'Failed to open process for command: ' . $command );
587
588 return ['error' => 'Failed to open process', 'numFiles' => $numFiles];
589 }
590 }
591
606 public function getFileFromUrl(string $moduleUrl, string $filePath, $progressBar = false)
607 {
608 // Open the URL for reading
609 $inputStream = @fopen( $moduleUrl, 'rb' );
610 if ( $inputStream === false ) {
611 Util::logError( 'Error fetching content from URL: ' . $moduleUrl );
612
613 return ['error' => 'Error fetching module'];
614 }
615
616 // Open the file for writing
617 $outputStream = @fopen( $filePath, 'wb' );
618 if ( $outputStream === false ) {
619 Util::logError( 'Error opening file for writing: ' . $filePath );
620 fclose( $inputStream );
621
622 return ['error' => 'Error saving module'];
623 }
624
625 // Read and write in chunks to avoid memory overload
626 $bufferSize = 8096; // 8KB
627 $chunksRead = 0;
628
629 while ( !feof( $inputStream ) ) {
630 $buffer = fread( $inputStream, $bufferSize );
631 fwrite( $outputStream, $buffer );
632 $chunksRead++;
633
634 // Send progress update
635 if ( $progressBar ) {
636 $progress = $chunksRead;
637 echo json_encode( ['progress' => $progress] );
638
639 // Check if output buffering is active before calling ob_flush()
640 if ( ob_get_length() !== false ) {
641 ob_flush();
642 }
643 flush();
644 }
645 }
646
647 fclose( $inputStream );
648 fclose( $outputStream );
649
650 return ['success' => true];
651 }
652}
global $bearsamppLang
global $bearsamppRoot
global $bearsamppCore
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 SCRIPT_EXEC_SILENT
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)
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 logError($data, $file=null)
static logTrace($data, $file=null)
static logDebug($data, $file=null)
const APP_TITLE
Definition root.php:13