2024.8.23
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
10/**
11 * Class Root
12 *
13 * This class represents the root of the Bearsampp application. It handles the initialization,
14 * configuration, and management of various components and settings within the application.
15 */
16class Root
17{
18 const ERROR_HANDLER = 'errorHandler';
19
20 public $path;
21 private $procs;
22 private $isRoot;
23
24 /**
25 * Constructs a Root object with the specified root path.
26 *
27 * @param string $rootPath The root path of the application.
28 */
29 public function __construct($rootPath)
30 {
31 $this->path = str_replace('\\', '/', rtrim($rootPath, '/\\'));
32 $this->isRoot = $_SERVER['PHP_SELF'] == 'root.php';
33 }
34
35 /**
36 * Registers the application components and initializes error handling.
37 */
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
74 /**
75 * Initializes error handling settings for the application.
76 */
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
85 /**
86 * Removes the custom error handling, reverting to the default PHP error handling.
87 */
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
96 /**
97 * Retrieves the list of processes.
98 *
99 * @return array The list of processes.
100 */
101 public function getProcs()
102 {
103 return $this->procs;
104 }
105
106 /**
107 * Checks if the current script is executed from the root path.
108 *
109 * @return bool True if executed from the root, false otherwise.
110 */
111 public function isRoot()
112 {
113 return $this->isRoot;
114 }
115
116 /**
117 * Gets the root path, optionally formatted for AeTrayMenu.
118 *
119 * @param bool $aetrayPath Whether to format the path for AeTrayMenu.
120 * @return string The root path.
121 */
122 public function getRootPath($aetrayPath = false)
123 {
124 $path = dirname($this->path);
125 return $aetrayPath ? $this->aetrayPath($path) : $path;
126 }
127
128 /**
129 * Formats a path for AeTrayMenu.
130 *
131 * @param string $path The path to format.
132 * @return string The formatted path.
133 */
134 private function aetrayPath($path)
135 {
136 $path = str_replace($this->getRootPath(), '', $path);
137 return '%AeTrayMenuPath%' . substr($path, 1, strlen($path));
138 }
139
140 /**
141 * Gets the path to the alias directory.
142 *
143 * @param bool $aetrayPath Whether to format the path for AeTrayMenu.
144 * @return string The alias path.
145 */
146 public function getAliasPath($aetrayPath = false)
147 {
148 return $this->getRootPath($aetrayPath) . '/alias';
149 }
150
151 /**
152 * Gets the path to the apps directory.
153 *
154 * @param bool $aetrayPath Whether to format the path for AeTrayMenu.
155 * @return string The apps path.
156 */
157 public function getAppsPath($aetrayPath = false)
158 {
159 return $this->getRootPath($aetrayPath) . '/apps';
160 }
161
162 /**
163 * Gets the path to the bin directory.
164 *
165 * @param bool $aetrayPath Whether to format the path for AeTrayMenu.
166 * @return string The bin path.
167 */
168 public function getBinPath($aetrayPath = false)
169 {
170 return $this->getRootPath($aetrayPath) . '/bin';
171 }
172
173 /**
174 * Gets the path to the core directory.
175 *
176 * @param bool $aetrayPath Whether to format the path for AeTrayMenu.
177 * @return string The core path.
178 */
179 public function getCorePath($aetrayPath = false)
180 {
181 return $aetrayPath ? $this->aetrayPath($this->path) : $this->path;
182 }
183
184 /**
185 * Gets the path to the logs directory.
186 *
187 * @param bool $aetrayPath Whether to format the path for AeTrayMenu.
188 * @return string The logs path.
189 */
190 public function getLogsPath($aetrayPath = false)
191 {
192 return $this->getRootPath($aetrayPath) . '/logs';
193 }
194
195 /**
196 * Gets the path to the SSL directory.
197 *
198 * @param bool $aetrayPath Whether to format the path for AeTrayMenu.
199 * @return string The SSL path.
200 */
201 public function getSslPath($aetrayPath = false)
202 {
203 return $this->getRootPath($aetrayPath) . '/ssl';
204 }
205
206 /**
207 * Gets the path to the temporary directory.
208 *
209 * @param bool $aetrayPath Whether to format the path for AeTrayMenu.
210 * @return string The temporary path.
211 */
212 public function getTmpPath($aetrayPath = false)
213 {
214 return $this->getRootPath($aetrayPath) . '/tmp';
215 }
216
217 /**
218 * Gets the path to the tools directory.
219 *
220 * @param bool $aetrayPath Whether to format the path for AeTrayMenu.
221 * @return string The tools path.
222 */
223 public function getToolsPath($aetrayPath = false)
224 {
225 return $this->getRootPath($aetrayPath) . '/tools';
226 }
227
228 /**
229 * Gets the path to the virtual hosts directory.
230 *
231 * @param bool $aetrayPath Whether to format the path for AeTrayMenu.
232 * @return string The virtual hosts path.
233 */
234 public function getVhostsPath($aetrayPath = false)
235 {
236 return $this->getRootPath($aetrayPath) . '/vhosts';
237 }
238
239 /**
240 * Gets the path to the WWW directory.
241 *
242 * @param bool $aetrayPath Whether to format the path for AeTrayMenu.
243 * @return string The WWW path.
244 */
245 public function getWwwPath($aetrayPath = false)
246 {
247 return $this->getRootPath($aetrayPath) . '/www';
248 }
249
250 /**
251 * Gets the path to the executable file.
252 *
253 * @param bool $aetrayPath Whether to format the path for AeTrayMenu.
254 * @return string The executable file path.
255 */
256 public function getExeFilePath($aetrayPath = false)
257 {
258 return $this->getRootPath($aetrayPath) . '/bearsampp.exe';
259 }
260
261 /**
262 * Gets the path to the configuration file.
263 *
264 * @param bool $aetrayPath Whether to format the path for AeTrayMenu.
265 * @return string The configuration file path.
266 */
267 public function getConfigFilePath($aetrayPath = false)
268 {
269 return $this->getRootPath($aetrayPath) . '/bearsampp.conf';
270 }
271
272 /**
273 * Gets the path to the INI file.
274 *
275 * @param bool $aetrayPath Whether to format the path for AeTrayMenu.
276 * @return string The INI file path.
277 */
278 public function getIniFilePath($aetrayPath = false)
279 {
280 return $this->getRootPath($aetrayPath) . '/bearsampp.ini';
281 }
282
283 /**
284 * Gets the path to the SSL configuration file.
285 *
286 * @param bool $aetrayPath Whether to format the path for AeTrayMenu.
287 * @return string The SSL configuration file path.
288 */
289 public function getSslConfPath($aetrayPath = false)
290 {
291 return $this->getSslPath($aetrayPath) . '/openssl.cnf';
292 }
293
294 /**
295 * Gets the path to the log file.
296 *
297 * @param bool $aetrayPath Whether to format the path for AeTrayMenu.
298 * @return string The log file path.
299 */
300 public function getLogFilePath($aetrayPath = false)
301 {
302 return $this->getLogsPath($aetrayPath) . '/bearsampp.log';
303 }
304
305 /**
306 * Gets the path to the error log file.
307 *
308 * @param bool $aetrayPath Whether to format the path for AeTrayMenu.
309 * @return string The error log file path.
310 */
311 public function getErrorLogFilePath($aetrayPath = false)
312 {
313 return $this->getLogsPath($aetrayPath) . '/bearsampp-error.log';
314 }
315
316 /**
317 * Gets the path to the homepage log file.
318 *
319 * @param bool $aetrayPath Whether to format the path for AeTrayMenu.
320 * @return string The homepage log file path.
321 */
322 public function getHomepageLogFilePath($aetrayPath = false)
323 {
324 return $this->getLogsPath($aetrayPath) . '/bearsampp-homepage.log';
325 }
326
327 /**
328 * Gets the path to the services log file.
329 *
330 * @param bool $aetrayPath Whether to format the path for AeTrayMenu.
331 * @return string The services log file path.
332 */
333 public function getServicesLogFilePath($aetrayPath = false)
334 {
335 return $this->getLogsPath($aetrayPath) . '/bearsampp-services.log';
336 }
337
338 /**
339 * Gets the path to the registry log file.
340 *
341 * @param bool $aetrayPath Whether to format the path for AeTrayMenu.
342 * @return string The registry log file path.
343 */
344 public function getRegistryLogFilePath($aetrayPath = false)
345 {
346 return $this->getLogsPath($aetrayPath) . '/bearsampp-registry.log';
347 }
348
349 /**
350 * Gets the path to the startup log file.
351 *
352 * @param bool $aetrayPath Whether to format the path for AeTrayMenu.
353 * @return string The startup log file path.
354 */
355 public function getStartupLogFilePath($aetrayPath = false)
356 {
357 return $this->getLogsPath($aetrayPath) . '/bearsampp-startup.log';
358 }
359
360 /**
361 * Gets the path to the batch log file.
362 *
363 * @param bool $aetrayPath Whether to format the path for AeTrayMenu.
364 * @return string The batch log file path.
365 */
366 public function getBatchLogFilePath($aetrayPath = false)
367 {
368 return $this->getLogsPath($aetrayPath) . '/bearsampp-batch.log';
369 }
370
371 /**
372 * Gets the path to the VBS log file.
373 *
374 * @param bool $aetrayPath Whether to format the path for AeTrayMenu.
375 * @return string The VBS log file path.
376 */
377 public function getVbsLogFilePath($aetrayPath = false)
378 {
379 return $this->getLogsPath($aetrayPath) . '/bearsampp-vbs.log';
380 }
381
382 /**
383 * Gets the path to the Winbinder log file.
384 *
385 * @param bool $aetrayPath Whether to format the path for AeTrayMenu.
386 * @return string The Winbinder log file path.
387 */
388 public function getWinbinderLogFilePath($aetrayPath = false)
389 {
390 return $this->getLogsPath($aetrayPath) . '/bearsampp-winbinder.log';
391 }
392
393 /**
394 * Gets the path to the NSSM log file.
395 *
396 * @param bool $aetrayPath Whether to format the path for AeTrayMenu.
397 * @return string The NSSM log file path.
398 */
399 public function getNssmLogFilePath($aetrayPath = false)
400 {
401 return $this->getLogsPath($aetrayPath) . '/bearsampp-nssm.log';
402 }
403
404 /**
405 * Gets the path to the homepage file.
406 *
407 * @param bool $aetrayPath Whether to format the path for AeTrayMenu.
408 * @return string The homepage file path.
409 */
410 public function getHomepageFilePath($aetrayPath = false)
411 {
412 return $this->getWwwPath($aetrayPath) . '/index.php';
413 }
414
415 /**
416 * Gets the name of the process.
417 *
418 * @return string The process name.
419 */
420 public function getProcessName()
421 {
422 return 'bearsampp';
423 }
424
425 /**
426 * Constructs a local URL with the specified request.
427 *
428 * @param string|null $request The specific request to append to the URL.
429 * @return string The constructed local URL.
430 */
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
440 /**
441 * Loads the core components of the application.
442 */
443 public static function loadCore()
444 {
445 global $bearsamppCore;
446 $bearsamppCore = new Core();
447 }
448
449 /**
450 * Loads the configuration settings of the application.
451 */
452 public static function loadConfig()
453 {
454 global $bearsamppConfig;
455 $bearsamppConfig = new Config();
456 }
457
458 /**
459 * Loads the language settings of the application.
460 */
461 public static function loadLang()
462 {
463 global $bearsamppLang;
464 $bearsamppLang = new LangProc();
465 }
466
467 /**
468 * Loads the OpenSSL settings of the application.
469 */
470 public static function loadOpenSsl()
471 {
472 global $bearsamppOpenSsl;
473 $bearsamppOpenSsl = new OpenSsl();
474 }
475
476 /**
477 * Loads the binary components of the application.
478 */
479 public static function loadBins()
480 {
481 global $bearsamppBins;
482 $bearsamppBins = new Bins();
483 }
484
485 /**
486 * Loads the tools components of the application.
487 */
488 public static function loadTools()
489 {
490 global $bearsamppTools;
491 $bearsamppTools = new Tools();
492 }
493
494 /**
495 * Loads the apps components of the application.
496 */
497 public static function loadApps()
498 {
499 global $bearsamppApps;
500 $bearsamppApps = new Apps();
501 }
502
503 /**
504 * Loads the Winbinder extension if available.
505 */
506 public static function loadWinbinder()
507 {
508 global $bearsamppWinbinder;
509 if (extension_loaded('winbinder')) {
510 $bearsamppWinbinder = new WinBinder();
511 }
512 }
513
514 /**
515 * Loads the registry settings of the application.
516 */
517 public static function loadRegistry()
518 {
519 global $bearsamppRegistry;
520 $bearsamppRegistry = new Registry();
521 }
522
523 /**
524 * Loads the homepage settings of the application.
525 */
526 public static function loadHomepage()
527 {
528 global $bearsamppHomepage;
530 }
531
532 /**
533 * Handles errors and logs them to the error log file.
534 *
535 * @param int $errno The level of the error raised.
536 * @param string $errstr The error message.
537 * @param string $errfile The filename that the error was raised in.
538 * @param int $errline The line number the error was raised at.
539 */
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_STRICT => 'E_STRICT',
566 E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
567 E_DEPRECATED => 'E_DEPRECATED',
568 );
569
570 $content = '[' . date('Y-m-d H:i:s', time()) . '] ';
571 $content .= $errNames[$errno] . ' ';
572 $content .= $errstr . ' in ' . $errfile;
573 $content .= ' on line ' . $errline . PHP_EOL;
574 $content .= self::debugStringBacktrace() . PHP_EOL;
575
576 file_put_contents($this->getErrorLogFilePath(), $content, FILE_APPEND);
577 }
578
579 /**
580 * Generates a debug backtrace string.
581 *
582 * @return string The debug backtrace.
583 */
584 private static function debugStringBacktrace()
585 {
586 ob_start();
587 debug_print_backtrace();
588 $trace = ob_get_contents();
589 ob_end_clean();
590
591 $trace = preg_replace('/^#0\s+Root::debugStringBacktrace[^\n]*\n/', '', $trace, 1);
592 $trace = preg_replace('/^#1\s+isRoot->errorHandler[^\n]*\n/', '', $trace, 1);
593 $trace = preg_replace_callback('/^#(\d+)/m', 'debugStringPregReplace', $trace);
594 return $trace;
595 }
596}
597
598 /**
599 * Adjusts the trace number in debug backtrace.
600 *
601 * @param array $match The matches from the regular expression.
602 * @return string The adjusted trace number.
603 */
604 function debugStringPregReplace($match)
605 {
606 return ' #' . ($match[1] - 1);
607 }
global $bearsamppBins
global $bearsamppLang
global $bearsamppCore
static loadConfig()
getLogFilePath($aetrayPath=false)
getStartupLogFilePath($aetrayPath=false)
getSslConfPath($aetrayPath=false)
initErrorHandling()
static loadBins()
getServicesLogFilePath($aetrayPath=false)
getBinPath($aetrayPath=false)
getTmpPath($aetrayPath=false)
getLocalUrl($request=null)
getCorePath($aetrayPath=false)
getWinbinderLogFilePath($aetrayPath=false)
getWwwPath($aetrayPath=false)
getExeFilePath($aetrayPath=false)
getErrorLogFilePath($aetrayPath=false)
static loadOpenSsl()
static loadTools()
getAliasPath($aetrayPath=false)
getVhostsPath($aetrayPath=false)
static loadRegistry()
const ERROR_HANDLER
static loadWinbinder()
static loadLang()
getProcessName()
errorHandler($errno, $errstr, $errfile, $errline)
getRegistryLogFilePath($aetrayPath=false)
getRootPath($aetrayPath=false)
getSslPath($aetrayPath=false)
getToolsPath($aetrayPath=false)
getProcs()
aetrayPath($path)
getHomepageFilePath($aetrayPath=false)
static debugStringBacktrace()
getAppsPath($aetrayPath=false)
getLogsPath($aetrayPath=false)
getIniFilePath($aetrayPath=false)
getVbsLogFilePath($aetrayPath=false)
getConfigFilePath($aetrayPath=false)
__construct($rootPath)
static loadCore()
getNssmLogFilePath($aetrayPath=false)
removeErrorHandling()
static loadHomepage()
static loadApps()
getBatchLogFilePath($aetrayPath=false)
getHomepageLogFilePath($aetrayPath=false)
static logSeparator()
static formatUnixPath($path)
static getListProcs()
debugStringPregReplace($match)
global $bearsamppConfig
Definition homepage.php:26
global $bearsamppHomepage
Definition homepage.php:26
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