Bearsampp 2025.8.29
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 // Error log
45 $this->initErrorHandling();
46
47 // External classes
48 require_once $this->getCorePath() . '/classes/class.util.php';
50
51 // Autoloader
52 require_once $this->getCorePath() . '/classes/class.autoloader.php';
53 $bearsamppAutoloader = new Autoloader();
54 $bearsamppAutoloader->register();
55
56 // Load
67
68 // Init
69 if ($this->isRoot) {
70 $this->procs = Win32Ps::getListProcs();
71 }
72 }
73
77 public function initErrorHandling()
78 {
79 error_reporting(-1);
80 ini_set('error_log', $this->getErrorLogFilePath());
81 ini_set('display_errors', '1');
82 set_error_handler(array($this, self::ERROR_HANDLER));
83 }
84
88 public function removeErrorHandling()
89 {
90 error_reporting(0);
91 ini_set('error_log', null);
92 ini_set('display_errors', '0');
93 restore_error_handler();
94 }
95
101 public function getProcs()
102 {
103 return $this->procs;
104 }
105
111 public function isRoot()
112 {
113 return $this->isRoot;
114 }
115
122 public function getRootPath($aetrayPath = false)
123 {
124 $path = dirname($this->path);
125 return $aetrayPath ? $this->aetrayPath($path) : $path;
126 }
127
134 private function aetrayPath($path)
135 {
136 $path = str_replace($this->getRootPath(), '', $path);
137 return '%AeTrayMenuPath%' . substr($path, 1, strlen($path));
138 }
139
146 public function getAliasPath($aetrayPath = false)
147 {
148 return $this->getRootPath($aetrayPath) . '/alias';
149 }
150
157 public function getAppsPath($aetrayPath = false)
158 {
159 return $this->getRootPath($aetrayPath) . '/apps';
160 }
161
168 public function getBinPath($aetrayPath = false)
169 {
170 return $this->getRootPath($aetrayPath) . '/bin';
171 }
172
179 public function getCorePath($aetrayPath = false)
180 {
181 return $aetrayPath ? $this->aetrayPath($this->path) : $this->path;
182 }
183
190 public function getLogsPath($aetrayPath = false)
191 {
192 return $this->getRootPath($aetrayPath) . '/logs';
193 }
194
201 public function getSslPath($aetrayPath = false)
202 {
203 return $this->getRootPath($aetrayPath) . '/ssl';
204 }
205
212 public function getTmpPath($aetrayPath = false)
213 {
214 return $this->getRootPath($aetrayPath) . '/tmp';
215 }
216
223 public function getToolsPath($aetrayPath = false)
224 {
225 return $this->getRootPath($aetrayPath) . '/tools';
226 }
227
234 public function getVhostsPath($aetrayPath = false)
235 {
236 return $this->getRootPath($aetrayPath) . '/vhosts';
237 }
238
245 public function getWwwPath($aetrayPath = false)
246 {
247 return $this->getRootPath($aetrayPath) . '/www';
248 }
249
256 public function getExeFilePath($aetrayPath = false)
257 {
258 return $this->getRootPath($aetrayPath) . '/bearsampp.exe';
259 }
260
267 public function getConfigFilePath($aetrayPath = false)
268 {
269 return $this->getRootPath($aetrayPath) . '/bearsampp.conf';
270 }
271
278 public function getIniFilePath($aetrayPath = false)
279 {
280 return $this->getRootPath($aetrayPath) . '/bearsampp.ini';
281 }
282
289 public function getSslConfPath($aetrayPath = false)
290 {
291 return $this->getSslPath($aetrayPath) . '/openssl.cnf';
292 }
293
300 public function getLogFilePath($aetrayPath = false)
301 {
302 return $this->getLogsPath($aetrayPath) . '/bearsampp.log';
303 }
304
311 public function getErrorLogFilePath($aetrayPath = false)
312 {
313 return $this->getLogsPath($aetrayPath) . '/bearsampp-error.log';
314 }
315
322 public function getHomepageLogFilePath($aetrayPath = false)
323 {
324 return $this->getLogsPath($aetrayPath) . '/bearsampp-homepage.log';
325 }
326
333 public function getServicesLogFilePath($aetrayPath = false)
334 {
335 return $this->getLogsPath($aetrayPath) . '/bearsampp-services.log';
336 }
337
344 public function getRegistryLogFilePath($aetrayPath = false)
345 {
346 return $this->getLogsPath($aetrayPath) . '/bearsampp-registry.log';
347 }
348
355 public function getStartupLogFilePath($aetrayPath = false)
356 {
357 return $this->getLogsPath($aetrayPath) . '/bearsampp-startup.log';
358 }
359
366 public function getBatchLogFilePath($aetrayPath = false)
367 {
368 return $this->getLogsPath($aetrayPath) . '/bearsampp-batch.log';
369 }
370
377 public function getVbsLogFilePath($aetrayPath = false)
378 {
379 return $this->getLogsPath($aetrayPath) . '/bearsampp-vbs.log';
380 }
381
388 public function getWinbinderLogFilePath($aetrayPath = false)
389 {
390 return $this->getLogsPath($aetrayPath) . '/bearsampp-winbinder.log';
391 }
392
399 public function getNssmLogFilePath($aetrayPath = false)
400 {
401 return $this->getLogsPath($aetrayPath) . '/bearsampp-nssm.log';
402 }
403
410 public function getHomepageFilePath($aetrayPath = false)
411 {
412 return $this->getWwwPath($aetrayPath) . '/index.php';
413 }
414
420 public function getProcessName()
421 {
422 return 'bearsampp';
423 }
424
431 public function getLocalUrl($request = null)
432 {
433 global $bearsamppBins;
434 return (isset($_SERVER['HTTPS']) ? 'https://' : 'http://') .
435 (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost') .
436 ($bearsamppBins->getApache()->getPort() != 80 && !isset($_SERVER['HTTPS']) ? ':' . $bearsamppBins->getApache()->getPort() : '') .
437 (!empty($request) ? '/' . $request : '');
438 }
439
443 public static function loadCore()
444 {
445 global $bearsamppCore;
446 $bearsamppCore = new Core();
447 }
448
452 public static function loadConfig()
453 {
454 global $bearsamppConfig;
455 $bearsamppConfig = new Config();
456 }
457
461 public static function loadLang()
462 {
463 global $bearsamppLang;
464 $bearsamppLang = new LangProc();
465 }
466
470 public static function loadOpenSsl()
471 {
472 global $bearsamppOpenSsl;
473 $bearsamppOpenSsl = new OpenSsl();
474 }
475
479 public static function loadBins()
480 {
481 global $bearsamppBins;
482 $bearsamppBins = new Bins();
483 }
484
488 public static function loadTools()
489 {
490 global $bearsamppTools;
491 $bearsamppTools = new Tools();
492 }
493
497 public static function loadApps()
498 {
499 global $bearsamppApps;
500 $bearsamppApps = new Apps();
501 }
502
506 public static function loadWinbinder()
507 {
508 global $bearsamppWinbinder;
509 if (extension_loaded('winbinder')) {
510 $bearsamppWinbinder = new WinBinder();
511 }
512 }
513
517 public static function loadRegistry()
518 {
519 global $bearsamppRegistry;
520 $bearsamppRegistry = new Registry();
521 }
522
526 public static function loadHomepage()
527 {
528 global $bearsamppHomepage;
530 }
531
540 public function errorHandler($errno, $errstr, $errfile, $errline)
541 {
542 if (error_reporting() === 0) {
543 return;
544 }
545
546 $errfile = Util::formatUnixPath($errfile);
547 $errfile = str_replace($this->getRootPath(), '', $errfile);
548
549 if (!defined('E_DEPRECATED')) {
550 define('E_DEPRECATED', 8192);
551 }
552
553 $errNames = array(
554 E_ERROR => 'E_ERROR',
555 E_WARNING => 'E_WARNING',
556 E_PARSE => 'E_PARSE',
557 E_NOTICE => 'E_NOTICE',
558 E_CORE_ERROR => 'E_CORE_ERROR',
559 E_CORE_WARNING => 'E_CORE_WARNING',
560 E_COMPILE_ERROR => 'E_COMPILE_ERROR',
561 E_COMPILE_WARNING => 'E_COMPILE_WARNING',
562 E_USER_ERROR => 'E_USER_ERROR',
563 E_USER_WARNING => 'E_USER_WARNING',
564 E_USER_NOTICE => 'E_USER_NOTICE',
565 E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
566 E_DEPRECATED => 'E_DEPRECATED',
567 );
568
569 $content = '[' . date('Y-m-d H:i:s', time()) . '] ';
570 $content .= $errNames[$errno] . ' ';
571 $content .= $errstr . ' in ' . $errfile;
572 $content .= ' on line ' . $errline . PHP_EOL;
573 $content .= self::debugStringBacktrace() . PHP_EOL;
574
575 file_put_contents($this->getErrorLogFilePath(), $content, FILE_APPEND);
576 }
577
583 private static function debugStringBacktrace()
584 {
585 ob_start();
586 debug_print_backtrace();
587 $trace = ob_get_contents();
588 ob_end_clean();
589
590 $trace = preg_replace('/^#0\s+Root::debugStringBacktrace[^\n]*\n/', '', $trace, 1);
591 $trace = preg_replace('/^#1\s+isRoot->errorHandler[^\n]*\n/', '', $trace, 1);
592 $trace = preg_replace_callback('/^#(\d+)/m', 'debugStringPregReplace', $trace);
593 return $trace;
594 }
595}
596
603 function debugStringPregReplace($match)
604 {
605 return ' #' . ($match[1] - 1);
606 }
global $bearsamppBins
global $bearsamppLang
global $bearsamppCore
debugStringPregReplace($match)
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()
getVbsLogFilePath($aetrayPath=false)
const ERROR_HANDLER
getHomepageLogFilePath($aetrayPath=false)
static loadConfig()
getConfigFilePath($aetrayPath=false)
getHomepageFilePath($aetrayPath=false)
static loadRegistry()
initErrorHandling()
static logSeparator()
static formatUnixPath($path)
static getListProcs()
global $bearsamppConfig
Definition homepage.php:27
global $bearsamppHomepage
Definition homepage.php:27
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