Bearsampp 2026.5.5
Loading...
Searching...
No Matches
class.root.php
Go to the documentation of this file.
1<?php
2/*
3 * Copyright (c) 2021-2024 Bearsampp
4 * License: GNU General Public License version 3 or later; see LICENSE.txt
5 * Author: Bear
6 * Website: https://bearsampp.com
7 * Github: https://github.com/Bearsampp
8 */
9
16class Root
17{
18 const ERROR_HANDLER = 'errorHandler';
19
20 public $path;
21 private $procs;
22 private $isRoot;
23
29 public function __construct($rootPath)
30 {
31 $this->path = str_replace('\\', '/', rtrim($rootPath, '/\\'));
32 $this->isRoot = $_SERVER['PHP_SELF'] == 'root.php';
33 }
34
38 public function register()
39 {
40 // Params
41 set_time_limit(0);
42 clearstatcache();
43
44 // External classes required for error handling and path utilities
45 require_once $this->getCorePath() . '/classes/class.util.path.php';
46
47 // Error log
48 $this->initErrorHandling();
49
50 // External classes
51 require_once $this->getCorePath() . '/classes/class.log.php';
52 require_once $this->getCorePath() . '/classes/class.util.php';
53 require_once $this->getCorePath() . '/classes/class.util.string.php';
54 require_once $this->getCorePath() . '/classes/class.httpclient.php';
55 Log::init();
56
57 // Autoloader
58 require_once $this->getCorePath() . '/classes/class.autoloader.php';
59 $bearsamppAutoloader = new Autoloader();
60 $bearsamppAutoloader->register();
61
62 // Load
74
75 // Init
76 if ($this->isRoot) {
77 $this->procs = Win32Ps::getListProcs();
78 }
79 }
80
84 public function initErrorHandling()
85 {
86 error_reporting(-1);
87 ini_set('error_log', $this->getErrorLogFilePath());
88 ini_set('display_errors', '1');
89 set_error_handler(array($this, self::ERROR_HANDLER));
90 }
91
95 public function removeErrorHandling()
96 {
97 error_reporting(0);
98 ini_set('error_log', null);
99 ini_set('display_errors', '0');
100 restore_error_handler();
101 }
102
108 public function getProcs()
109 {
110 return $this->procs;
111 }
112
118 public function isRoot()
119 {
120 return $this->isRoot;
121 }
122
129 public function getRootPath($aetrayPath = false)
130 {
131 $path = dirname($this->path);
132 return $aetrayPath ? $this->aetrayPath($path) : $path;
133 }
134
141 private function aetrayPath($path)
142 {
143 $path = str_replace($this->getRootPath(), '', $path);
144 return '%AeTrayMenuPath%' . substr($path, 1, strlen($path));
145 }
146
153 public function getAliasPath($aetrayPath = false)
154 {
155 return $this->getRootPath($aetrayPath) . '/alias';
156 }
157
164 public function getAppsPath($aetrayPath = false)
165 {
166 return $this->getRootPath($aetrayPath) . '/apps';
167 }
168
175 public function getBinPath($aetrayPath = false)
176 {
177 return $this->getRootPath($aetrayPath) . '/bin';
178 }
179
186 public function getCorePath($aetrayPath = false)
187 {
188 return $aetrayPath ? $this->aetrayPath($this->path) : $this->path;
189 }
190
197 public function getLogsPath($aetrayPath = false)
198 {
199 return $this->getRootPath($aetrayPath) . '/logs';
200 }
201
208 public function getSslPath($aetrayPath = false)
209 {
210 return $this->getRootPath($aetrayPath) . '/ssl';
211 }
212
219 public function getTmpPath($aetrayPath = false)
220 {
221 return $this->getRootPath($aetrayPath) . '/tmp';
222 }
223
230 public function getToolsPath($aetrayPath = false)
231 {
232 return $this->getRootPath($aetrayPath) . '/tools';
233 }
234
241 public function getVhostsPath($aetrayPath = false)
242 {
243 return $this->getRootPath($aetrayPath) . '/vhosts';
244 }
245
252 public function getWwwPath($aetrayPath = false)
253 {
254 return $this->getRootPath($aetrayPath) . '/www';
255 }
256
263 public function getExeFilePath($aetrayPath = false)
264 {
265 return $this->getRootPath($aetrayPath) . '/bearsampp.exe';
266 }
267
274 public function getConfigFilePath($aetrayPath = false)
275 {
276 return $this->getRootPath($aetrayPath) . '/bearsampp.conf';
277 }
278
285 public function getIniFilePath($aetrayPath = false)
286 {
287 return $this->getRootPath($aetrayPath) . '/bearsampp.ini';
288 }
289
296 public function getSslConfPath($aetrayPath = false)
297 {
298 return $this->getSslPath($aetrayPath) . '/openssl.cnf';
299 }
300
307 public function getLogFilePath($aetrayPath = false)
308 {
309 return $this->getLogsPath($aetrayPath) . '/bearsampp.log';
310 }
311
318 public function getErrorLogFilePath($aetrayPath = false)
319 {
320 return $this->getLogsPath($aetrayPath) . '/bearsampp-error.log';
321 }
322
329 public function getHomepageLogFilePath($aetrayPath = false)
330 {
331 return $this->getLogsPath($aetrayPath) . '/bearsampp-homepage.log';
332 }
333
340 public function getServicesLogFilePath($aetrayPath = false)
341 {
342 return $this->getLogsPath($aetrayPath) . '/bearsampp-services.log';
343 }
344
351 public function getRegistryLogFilePath($aetrayPath = false)
352 {
353 return $this->getLogsPath($aetrayPath) . '/bearsampp-registry.log';
354 }
355
362 public function getStartupLogFilePath($aetrayPath = false)
363 {
364 return $this->getLogsPath($aetrayPath) . '/bearsampp-startup.log';
365 }
366
373 public function getBatchLogFilePath($aetrayPath = false)
374 {
375 return $this->getLogsPath($aetrayPath) . '/bearsampp-batch.log';
376 }
377
384 public function getWinbinderLogFilePath($aetrayPath = false)
385 {
386 return $this->getLogsPath($aetrayPath) . '/bearsampp-winbinder.log';
387 }
388
395 public function getNssmLogFilePath($aetrayPath = false)
396 {
397 return $this->getLogsPath($aetrayPath) . '/bearsampp-nssm.log';
398 }
399
406 public function getHomepageFilePath($aetrayPath = false)
407 {
408 return $this->getWwwPath($aetrayPath) . '/index.php';
409 }
410
416 public function getProcessName()
417 {
418 return 'bearsampp';
419 }
420
427 public function getLocalUrl($request = null)
428 {
429 global $bearsamppBins;
430 return (isset($_SERVER['HTTPS']) ? 'https://' : 'http://') .
431 (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost') .
432 ($bearsamppBins->getApache()->getPort() != 80 && !isset($_SERVER['HTTPS']) ? ':' . $bearsamppBins->getApache()->getPort() : '') .
433 (!empty($request) ? '/' . $request : '');
434 }
435
439 public static function loadCore()
440 {
441 global $bearsamppCore;
442 $bearsamppCore = new Core();
443 }
444
448 public static function loadConfig()
449 {
450 global $bearsamppConfig;
451 $bearsamppConfig = new Config();
452 }
453
457 public static function loadLang()
458 {
459 global $bearsamppLang;
460 $bearsamppLang = new LangProc();
461 }
462
466 public static function loadOpenSsl()
467 {
468 global $bearsamppOpenSsl;
469 $bearsamppOpenSsl = new OpenSsl();
470 }
471
475 public static function loadBins()
476 {
477 global $bearsamppBins;
478 $bearsamppBins = new Bins();
479 }
480
484 public static function loadTools()
485 {
486 global $bearsamppTools;
487 $bearsamppTools = new Tools();
488 }
489
493 public static function loadApps()
494 {
495 global $bearsamppApps;
496 $bearsamppApps = new Apps();
497 }
498
502 public static function loadWinbinder()
503 {
504 global $bearsamppWinbinder;
505 if (extension_loaded('winbinder')) {
506 $bearsamppWinbinder = new WinBinder();
507 }
508 }
509
513 public static function loadRegistry()
514 {
515 global $bearsamppRegistry;
516 $bearsamppRegistry = new Registry();
517 }
518
522 public static function loadHomepage()
523 {
524 global $bearsamppHomepage;
526 }
527
536 public function errorHandler($errno, $errstr, $errfile, $errline)
537 {
538 if (error_reporting() === 0) {
539 return;
540 }
541
542 $errfile = UtilPath::formatUnixPath($errfile);
543 $errfile = str_replace($this->getRootPath(), '', $errfile);
544
545 if (!defined('E_DEPRECATED')) {
546 define('E_DEPRECATED', 8192);
547 }
548
549 $errNames = array(
550 E_ERROR => 'E_ERROR',
551 E_WARNING => 'E_WARNING',
552 E_PARSE => 'E_PARSE',
553 E_NOTICE => 'E_NOTICE',
554 E_CORE_ERROR => 'E_CORE_ERROR',
555 E_CORE_WARNING => 'E_CORE_WARNING',
556 E_COMPILE_ERROR => 'E_COMPILE_ERROR',
557 E_COMPILE_WARNING => 'E_COMPILE_WARNING',
558 E_USER_ERROR => 'E_USER_ERROR',
559 E_USER_WARNING => 'E_USER_WARNING',
560 E_USER_NOTICE => 'E_USER_NOTICE',
561 E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
562 E_DEPRECATED => 'E_DEPRECATED',
563 );
564
565 $content = '[' . date('Y-m-d H:i:s', time()) . '] ';
566 $content .= $errNames[$errno] . ' ';
567 $content .= $errstr . ' in ' . $errfile;
568 $content .= ' on line ' . $errline . PHP_EOL;
569 $content .= self::debugStringBacktrace() . PHP_EOL;
570
571 file_put_contents($this->getErrorLogFilePath(), $content, FILE_APPEND);
572 }
573
579 private static function debugStringBacktrace()
580 {
581 ob_start();
582 debug_print_backtrace();
583 $trace = ob_get_contents();
584 ob_end_clean();
585
586 $trace = preg_replace('/^#0\s+Root::debugStringBacktrace[^\n]*\n/', '', $trace, 1);
587 $trace = preg_replace('/^#1\s+isRoot->errorHandler[^\n]*\n/', '', $trace, 1);
588 $trace = preg_replace_callback('/^#(\d+)/m', 'debugStringPregReplace', $trace);
589 return $trace;
590 }
591}
592
599 function debugStringPregReplace($match)
600 {
601 return ' #' . ($match[1] - 1);
602 }
global $bearsamppBins
global $bearsamppLang
global $bearsamppCore
debugStringPregReplace($match)
static init()
Definition class.log.php:58
static separator()
getLogsPath($aetrayPath=false)
getBinPath($aetrayPath=false)
removeErrorHandling()
getAppsPath($aetrayPath=false)
getWwwPath($aetrayPath=false)
static debugStringBacktrace()
static loadBins()
getServicesLogFilePath($aetrayPath=false)
aetrayPath($path)
getNssmLogFilePath($aetrayPath=false)
getRootPath($aetrayPath=false)
getLogFilePath($aetrayPath=false)
errorHandler($errno, $errstr, $errfile, $errline)
getExeFilePath($aetrayPath=false)
getProcessName()
getToolsPath($aetrayPath=false)
getVhostsPath($aetrayPath=false)
static loadOpenSsl()
getIniFilePath($aetrayPath=false)
getLocalUrl($request=null)
getWinbinderLogFilePath($aetrayPath=false)
static loadLang()
static loadApps()
static loadTools()
getAliasPath($aetrayPath=false)
getStartupLogFilePath($aetrayPath=false)
static loadWinbinder()
getErrorLogFilePath($aetrayPath=false)
getSslConfPath($aetrayPath=false)
static loadHomepage()
getSslPath($aetrayPath=false)
getRegistryLogFilePath($aetrayPath=false)
getBatchLogFilePath($aetrayPath=false)
getCorePath($aetrayPath=false)
getProcs()
getTmpPath($aetrayPath=false)
__construct($rootPath)
static loadCore()
const ERROR_HANDLER
getHomepageLogFilePath($aetrayPath=false)
static loadConfig()
getConfigFilePath($aetrayPath=false)
getHomepageFilePath($aetrayPath=false)
static loadRegistry()
initErrorHandling()
static formatUnixPath($path)
static getListProcs()
global $bearsamppConfig
Definition homepage.php:41
global $bearsamppHomepage
Definition homepage.php:41
if(!extension_loaded('winbinder')) if(!dl('php_winbinder.dll')) trigger_error("WinBinder extension could not be loaded.\n" E_USER_ERROR
Definition winbinder.php:14