Bearsampp 2026.7.11
Loading...
Searching...
No Matches
class.path.php
Go to the documentation of this file.
1<?php
2/*
3 *
4 * * Copyright (c) 2022-2025 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
24class Path
25{
32 public static function getModuleRootPath($module)
33 {
34 return $module->rootPath;
35 }
36
43 public static function getModuleCurrentPath($module)
44 {
45 return $module->currentPath;
46 }
47
54 public static function getModuleSymlinkPath($module)
55 {
56 return $module->symlinkPath;
57 }
58
63 private static $pathFormatCache = [];
64
69 private static $rootPath;
70
75 private static $corePath;
76
84 public static function init($rootPath, $corePath)
85 {
86 self::$rootPath = str_replace('\\', '/', rtrim($rootPath, '/\\'));
87 self::$corePath = str_replace('\\', '/', rtrim($corePath, '/\\'));
88 }
89
94 private static $pathFormatCacheMaxSize = 500;
95
100 private static $pathFormatStats = [
101 'unix_hits' => 0,
102 'unix_misses' => 0,
103 'windows_hits' => 0,
104 'windows_misses' => 0,
105 ];
106
118 public static function formatWindowsPath($path)
119 {
120 if (empty($path)) {
121 return $path;
122 }
123
124 $cacheKey = 'w_' . $path;
125 if (isset(self::$pathFormatCache[$cacheKey])) {
126 self::$pathFormatStats['windows_hits']++;
127 return self::$pathFormatCache[$cacheKey];
128 }
129
130 self::$pathFormatStats['windows_misses']++;
131
132 $result = str_replace('/', '\\', $path);
133
134 if (count(self::$pathFormatCache) < self::$pathFormatCacheMaxSize) {
135 self::$pathFormatCache[$cacheKey] = $result;
136 } else {
137 $removeCount = (int)(self::$pathFormatCacheMaxSize * 0.1);
138 self::$pathFormatCache = array_slice(self::$pathFormatCache, $removeCount, null, true);
139 self::$pathFormatCache[$cacheKey] = $result;
140 }
141
142 return $result;
143 }
144
156 public static function formatUnixPath($path)
157 {
158 if (empty($path)) {
159 return $path;
160 }
161
162 $cacheKey = 'u_' . $path;
163 if (isset(self::$pathFormatCache[$cacheKey])) {
164 self::$pathFormatStats['unix_hits']++;
165 return self::$pathFormatCache[$cacheKey];
166 }
167
168 self::$pathFormatStats['unix_misses']++;
169
170 $result = str_replace('\\', '/', $path);
171
172 if (count(self::$pathFormatCache) < self::$pathFormatCacheMaxSize) {
173 self::$pathFormatCache[$cacheKey] = $result;
174 } else {
175 $removeCount = (int)(self::$pathFormatCacheMaxSize * 0.1);
176 self::$pathFormatCache = array_slice(self::$pathFormatCache, $removeCount, null, true);
177 self::$pathFormatCache[$cacheKey] = $result;
178 }
179
180 return $result;
181 }
182
189 public static function getPathFormatStats()
190 {
191 return self::$pathFormatStats;
192 }
193
201 public static function changePath($filesToScan, $rootPath = null)
202 {
204
205 $result = array(
206 'countChangedOcc' => 0,
207 'countChangedFiles' => 0
208 );
209
211 $unixOldPath = Path::formatUnixPath($bearsamppCore->getLastPathContent());
212 $windowsOldPath = Path::formatWindowsPath($bearsamppCore->getLastPathContent());
213 $unixCurrentPath = Path::formatUnixPath($rootPath);
214 $windowsCurrentPath = Path::formatWindowsPath($rootPath);
215
216 foreach ($filesToScan as $fileToScan) {
217 $tmpCountChangedOcc = 0;
218 $fileContentOr = file_get_contents($fileToScan);
219
220 if ($fileContentOr === false) {
221 Log::error("changePath(): Failed to read file: " . $fileToScan);
222 continue;
223 }
224
225 $fileContent = $fileContentOr;
226
227 // old path
228 if (stripos($fileContent, $unixOldPath) !== false) {
229 $fileContent = str_ireplace($unixOldPath, $unixCurrentPath, $fileContent, $countChanged);
230 $tmpCountChangedOcc += $countChanged;
231 }
232 if (stripos($fileContent, $windowsOldPath) !== false) {
233 $fileContent = str_ireplace($windowsOldPath, $windowsCurrentPath, $fileContent, $countChanged);
234 $tmpCountChangedOcc += $countChanged;
235 }
236
237 // placeholders
238 if (strpos($fileContent, Core::PATH_LIN_PLACEHOLDER) !== false) {
239 $fileContent = str_replace(Core::PATH_LIN_PLACEHOLDER, $unixCurrentPath, $fileContent, $countChanged);
240 $tmpCountChangedOcc += $countChanged;
241 }
242 if (strpos($fileContent, Core::PATH_WIN_PLACEHOLDER) !== false) {
243 $fileContent = str_replace(Core::PATH_WIN_PLACEHOLDER, $windowsCurrentPath, $fileContent, $countChanged);
244 $tmpCountChangedOcc += $countChanged;
245 }
246
247 if ($fileContentOr != $fileContent) {
248 if (file_put_contents($fileToScan, $fileContent) !== false) {
249 $result['countChangedOcc'] += $tmpCountChangedOcc;
250 $result['countChangedFiles'] += 1;
251 } else {
252 Log::error("changePath(): Failed to write to file: " . $fileToScan);
253 }
254 }
255 }
256
257 Log::debug('changePath() completed: ' . $result['countChangedFiles'] . ' files changed, ' . $result['countChangedOcc'] . ' total occurrences');
258
259 return $result;
260 }
261
268 public static function clearPathFormatCache()
269 {
270 self::$pathFormatCache = [];
271 self::$pathFormatStats = [
272 'unix_hits' => 0,
273 'unix_misses' => 0,
274 'windows_hits' => 0,
275 'windows_misses' => 0,
276 ];
277 }
278
284 public static function getPathFormatCacheSize()
285 {
286 return count(self::$pathFormatCache);
287 }
288
295 public static function aetrayPath($path)
296 {
298 $path = str_replace($rootPath, '', $path);
299 $path = ltrim(self::formatUnixPath($path), '/');
300 return '%AeTrayMenuPath%' . $path;
301 }
302
309 public static function getRootPath($aetrayPath = false)
310 {
311 return $aetrayPath ? self::aetrayPath(self::$rootPath) : self::$rootPath;
312 }
313
320 public static function getAjaxPath($aetrayPath = false)
321 {
322 return self::getHomepagePath($aetrayPath) . '/ajax';
323 }
324
331 public static function getAliasPath($aetrayPath = false)
332 {
333 return self::getRootPath($aetrayPath) . '/alias';
334 }
335
342 public static function getAppsPath($aetrayPath = false)
343 {
344 return self::getRootPath($aetrayPath) . '/apps';
345 }
346
353 public static function getBatchLogFilePath($aetrayPath = false)
354 {
355 return self::getLogsPath($aetrayPath) . '/bearsampp-batch.log';
356 }
357
364 public static function getBinPath($aetrayPath = false)
365 {
366 return self::getRootPath($aetrayPath) . '/bin';
367 }
368
375 public static function getConfigFilePath($aetrayPath = false)
376 {
377 return self::getRootPath($aetrayPath) . '/bearsampp.conf';
378 }
379
386 public static function getCorePath($aetrayPath = false)
387 {
388 return $aetrayPath ? self::aetrayPath(self::$corePath) : self::$corePath;
389 }
390
396 public static function getProcessName()
397 {
398 return 'bearsampp';
399 }
400
407 public static function getLocalUrl($request = null)
408 {
409 global $bearsamppBins;
410 $isHttps = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off');
411 $scheme = $isHttps ? 'https://' : 'http://';
412 $host = (isset($_SERVER['SERVER_NAME'])) ? $_SERVER['SERVER_NAME'] : 'localhost';
413
414 $port = 80;
415 if (isset($bearsamppBins) && $bearsamppBins->getApache() !== null) {
416 $port = $isHttps ? $bearsamppBins->getApache()->getSslPort() : $bearsamppBins->getApache()->getPort();
417 }
418
419 $portSuffix = '';
420 if ($isHttps) {
421 if ($port != 443) {
422 $portSuffix = ':' . $port;
423 }
424 } else {
425 if ($port != 80) {
426 $portSuffix = ':' . $port;
427 }
428 }
429
430 return $scheme . $host . $portSuffix . (!empty($request) ? '/' . $request : '');
431 }
432
439 public static function getErrorLogFilePath($aetrayPath = false)
440 {
441 return self::getLogsPath($aetrayPath) . '/bearsampp-error.log';
442 }
443
450 public static function getExeFilePath($aetrayPath = false)
451 {
452 return self::getRootPath($aetrayPath) . '/bearsampp.exe';
453 }
454
461 public static function getHomepageFilePath($aetrayPath = false)
462 {
463 return self::getWwwPath($aetrayPath) . '/index.php';
464 }
465
472 public static function getHomepageLogFilePath($aetrayPath = false)
473 {
474 return self::getLogsPath($aetrayPath) . '/bearsampp-homepage.log';
475 }
476
483 public static function getHomepagePath($aetrayPath = false)
484 {
485 return self::getResourcesPath($aetrayPath) . '/homepage';
486 }
487
494 public static function getHostsEditorPath($aetrayPath = false)
495 {
496 return self::getLibsPath($aetrayPath) . '/hostseditor';
497 }
498
505 public static function getHostsEditorExe($aetrayPath = false)
506 {
507 return self::getHostsEditorPath($aetrayPath) . '/' . Core::HOSTSEDITOR_EXE;
508 }
509
516 public static function getIconsPath($aetrayPath = false)
517 {
518 return self::getImagesPath($aetrayPath) . '/icons';
519 }
520
527 public static function getImagesPath($aetrayPath = false)
528 {
529 return self::getHomepagePath($aetrayPath) . '/img';
530 }
531
538 public static function getIniFilePath($aetrayPath = false)
539 {
540 return self::getRootPath($aetrayPath) . '/bearsampp.ini';
541 }
542
549 public static function getisRootFilePath($aetrayPath = false)
550 {
551 return self::getCorePath($aetrayPath) . '/' . Core::isRoot_FILE;
552 }
553
560 public static function getLangsPath($aetrayPath = false)
561 {
562 return self::getCorePath($aetrayPath) . '/langs';
563 }
564
571 public static function getLastPath($aetrayPath = false)
572 {
573 return self::getResourcesPath($aetrayPath) . '/' . Core::LAST_PATH;
574 }
575
582 public static function getLibsPath($aetrayPath = false)
583 {
584 return self::getCorePath($aetrayPath) . '/libs';
585 }
586
593 public static function getLnPath($aetrayPath = false)
594 {
595 return self::getLibsPath($aetrayPath) . '/ln';
596 }
597
604 public static function getLnExe($aetrayPath = false)
605 {
606 return self::getLnPath($aetrayPath) . '/' . Core::LN_EXE;
607 }
608
615 public static function getLogFilePath($aetrayPath = false)
616 {
617 return self::getLogsPath($aetrayPath) . '/bearsampp.log';
618 }
619
626 public static function getLogsPath($aetrayPath = false)
627 {
628 return self::getRootPath($aetrayPath) . '/logs';
629 }
630
637 public static function getNssmPath($aetrayPath = false)
638 {
639 return self::getLibsPath($aetrayPath) . '/nssm';
640 }
641
648 public static function getNssmExe($aetrayPath = false)
649 {
650 return self::getNssmPath($aetrayPath) . '/' . Core::NSSM_EXE;
651 }
652
658 public static function getNssmEnvPaths()
659 {
660 global $bearsamppBins, $bearsamppTools;
661
662 $paths = '';
663
664 // Add paths for enabled bins
665 if (isset($bearsamppBins)) {
666 if ($bearsamppBins->getApache() && $bearsamppBins->getApache()->isEnable()) {
667 $paths .= self::getModuleSymlinkPath($bearsamppBins->getApache()) . '/bin;';
668 }
669 if ($bearsamppBins->getPhp() && $bearsamppBins->getPhp()->isEnable()) {
670 $paths .= self::getModuleSymlinkPath($bearsamppBins->getPhp()) . ';';
671 $paths .= self::getModuleSymlinkPath($bearsamppBins->getPhp()) . '/pear;';
672 $paths .= self::getModuleSymlinkPath($bearsamppBins->getPhp()) . '/deps;';
673 $paths .= self::getModuleSymlinkPath($bearsamppBins->getPhp()) . '/imagick;';
674 }
675 if ($bearsamppBins->getNodejs() && $bearsamppBins->getNodejs()->isEnable()) {
676 $paths .= self::getModuleSymlinkPath($bearsamppBins->getNodejs()) . ';';
677 }
678 }
679
680 // Add paths for enabled tools
681 if (isset($bearsamppTools)) {
682 if ($bearsamppTools->getComposer() && $bearsamppTools->getComposer()->isEnable()) {
683 $paths .= self::getModuleSymlinkPath($bearsamppTools->getComposer()) . ';';
684 $paths .= self::getModuleSymlinkPath($bearsamppTools->getComposer()) . '/vendor/bin;';
685 }
686 if ($bearsamppTools->getGhostscript() && $bearsamppTools->getGhostscript()->isEnable()) {
687 $paths .= self::getModuleSymlinkPath($bearsamppTools->getGhostscript()) . '/bin;';
688 }
689 if ($bearsamppTools->getGit() && $bearsamppTools->getGit()->isEnable()) {
690 $paths .= self::getModuleSymlinkPath($bearsamppTools->getGit()) . '/bin;';
691 }
692 if ($bearsamppTools->getNgrok() && $bearsamppTools->getNgrok()->isEnable()) {
693 $paths .= self::getModuleSymlinkPath($bearsamppTools->getNgrok()) . ';';
694 }
695 if ($bearsamppTools->getPerl() && $bearsamppTools->getPerl()->isEnable()) {
696 $paths .= self::getModuleSymlinkPath($bearsamppTools->getPerl()) . '/perl/site/bin;';
697 $paths .= self::getModuleSymlinkPath($bearsamppTools->getPerl()) . '/perl/bin;';
698 $paths .= self::getModuleSymlinkPath($bearsamppTools->getPerl()) . '/c/bin;';
699 }
700 if ($bearsamppTools->getPython() && $bearsamppTools->getPython()->isEnable()) {
701 $paths .= self::getModuleSymlinkPath($bearsamppTools->getPython()) . '/bin;';
702 }
703 if ($bearsamppTools->getRuby() && $bearsamppTools->getRuby()->isEnable()) {
704 $paths .= self::getModuleSymlinkPath($bearsamppTools->getRuby()) . '/bin;';
705 }
706 }
707
708 return self::formatWindowsPath($paths);
709 }
710
717 public static function getNssmLogFilePath($aetrayPath = false)
718 {
719 return self::getLogsPath($aetrayPath) . '/bearsampp-nssm.log';
720 }
721
728 public static function getOpenSslConf($aetrayPath = false)
729 {
730 return self::getOpenSslPath($aetrayPath) . '/' . Core::OPENSSL_CONF;
731 }
732
739 public static function getOpenSslExe($aetrayPath = false)
740 {
741 return self::getOpenSslPath($aetrayPath) . '/' . Core::OPENSSL_EXE;
742 }
743
750 public static function getOpenSslPath($aetrayPath = false)
751 {
752 return self::getLibsPath($aetrayPath) . '/openssl';
753 }
754
761 public static function getPhpPath($aetrayPath = false)
762 {
763 return self::getLibsPath($aetrayPath) . '/php';
764 }
765
772 public static function getPhpExe($aetrayPath = false)
773 {
774 return self::getPhpPath($aetrayPath) . '/' . Core::PHP_EXE;
775 }
776
782 public static function getPowerShellPath()
783 {
784 if (is_dir('C:\Windows\System32\WindowsPowerShell')) {
785 return Util::findFile('C:\Windows\System32\WindowsPowerShell', 'powershell.exe');
786 }
787
788 return false;
789 }
790
797 public static function getPwgenPath($aetrayPath = false)
798 {
799 return self::getLibsPath($aetrayPath) . '/pwgen';
800 }
801
808 public static function getPwgenExe($aetrayPath = false)
809 {
810 return self::getPwgenPath($aetrayPath) . '/' . Core::PWGEN_EXE;
811 }
812
819 public static function getRegistryLogFilePath($aetrayPath = false)
820 {
821 return self::getLogsPath($aetrayPath) . '/bearsampp-registry.log';
822 }
823
829 public static function getStartupLnkPath()
830 {
831 $startupPath = Win32Native::getSpecialFolderPath('Startup');
832 return $startupPath ? $startupPath . '/' . APP_TITLE . '.lnk' : false;
833 }
834
841 public static function getResourcesPath($aetrayPath = false)
842 {
843 return self::getCorePath($aetrayPath) . '/resources';
844 }
845
852 public static function getScript($type)
853 {
854 return self::getScriptsPath() . '/' . $type;
855 }
856
863 public static function getScriptsPath($aetrayPath = false)
864 {
865 return self::getCorePath($aetrayPath) . '/scripts';
866 }
867
874 public static function getServicesLogFilePath($aetrayPath = false)
875 {
876 return self::getLogsPath($aetrayPath) . '/bearsampp-services.log';
877 }
878
885 public static function getSetEnvPath($aetrayPath = false)
886 {
887 return self::getLibsPath($aetrayPath) . '/setenv';
888 }
889
896 public static function getSetEnvExe($aetrayPath = false)
897 {
898 return self::getSetEnvPath($aetrayPath) . '/' . Core::SETENV_EXE;
899 }
900
907 public static function getSslConfPath($aetrayPath = false)
908 {
909 return self::getSslPath($aetrayPath) . '/openssl.cnf';
910 }
911
918 public static function getSslPath($aetrayPath = false)
919 {
920 return self::getRootPath($aetrayPath) . '/ssl';
921 }
922
929 public static function getStartupLogFilePath($aetrayPath = false)
930 {
931 return self::getLogsPath($aetrayPath) . '/bearsampp-startup.log';
932 }
933
940 public static function getTmpPath($aetrayPath = false)
941 {
942 return self::getCorePath($aetrayPath) . '/tmp';
943 }
944
951 public static function getToolsPath($aetrayPath = false)
952 {
953 return self::getRootPath($aetrayPath) . '/tools';
954 }
955
962 public static function getMkcertPath($aetrayPath = false)
963 {
964 return self::getLibsPath($aetrayPath) . '/mkcert';
965 }
966
973 public static function getMkcertExe($aetrayPath = false)
974 {
975 return self::getMkcertPath($aetrayPath) . '/mkcert.exe';
976 }
977
983 public static function getMkcertRootCaName()
984 {
985 return 'rootCA.pem';
986 }
987
994 public static function getVhostsPath($aetrayPath = false)
995 {
996 return self::getRootPath($aetrayPath) . '/vhosts';
997 }
998
1004 public static function getWebIconsPath()
1005 {
1006 return self::getWebImagesPath() . '/icons';
1007 }
1008
1014 public static function getWebImagesPath()
1015 {
1016 return self::getWebResourcesPath() . '/img';
1017 }
1018
1024 public static function getWebResourcesPath()
1025 {
1026 return rtrim(md5(APP_TITLE), '/');
1027 }
1028
1034 public static function getWebResourcesUrl()
1035 {
1036 global $bearsamppBins;
1037 $request = self::getWebResourcesPath();
1038 $isHttps = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off');
1039 $scheme = $isHttps ? 'https://' : 'http://';
1040 $host = (isset($_SERVER['SERVER_NAME'])) ? $_SERVER['SERVER_NAME'] : 'localhost';
1041
1042 $port = 80;
1043 if (isset($bearsamppBins) && $bearsamppBins->getApache() !== null) {
1044 $port = $isHttps ? $bearsamppBins->getApache()->getSslPort() : $bearsamppBins->getApache()->getPort();
1045 }
1046
1047 $portSuffix = '';
1048 if ($isHttps) {
1049 if ($port != 443) {
1050 $portSuffix = ':' . $port;
1051 }
1052 } else {
1053 if ($port != 80) {
1054 $portSuffix = ':' . $port;
1055 }
1056 }
1057
1058 return $scheme . $host . $portSuffix . (!empty($request) ? '/' . $request : '');
1059 }
1060
1067 public static function getWinbinderLogFilePath($aetrayPath = false)
1068 {
1069 return self::getLogsPath($aetrayPath) . '/bearsampp-winbinder.log';
1070 }
1071
1078 public static function getWwwPath($aetrayPath = false)
1079 {
1080 return self::getRootPath($aetrayPath) . '/www';
1081 }
1082}
$result
global $bearsamppBins
global $bearsamppRoot
$port
global $bearsamppCore
const OPENSSL_CONF
const PHP_EXE
const PWGEN_EXE
const isRoot_FILE
const PATH_LIN_PLACEHOLDER
const NSSM_EXE
const SETENV_EXE
const LN_EXE
const PATH_WIN_PLACEHOLDER
const HOSTSEDITOR_EXE
const OPENSSL_EXE
const LAST_PATH
static debug($data, $file=null)
static error($data, $file=null)
static getHomepageFilePath($aetrayPath=false)
static getNssmPath($aetrayPath=false)
static getScriptsPath($aetrayPath=false)
static getExeFilePath($aetrayPath=false)
static getBatchLogFilePath($aetrayPath=false)
static getPathFormatCacheSize()
static getSslConfPath($aetrayPath=false)
static clearPathFormatCache()
static getHostsEditorExe($aetrayPath=false)
static getAjaxPath($aetrayPath=false)
static $rootPath
static getCorePath($aetrayPath=false)
static getWebResourcesPath()
static getHomepagePath($aetrayPath=false)
static getAliasPath($aetrayPath=false)
static getPwgenPath($aetrayPath=false)
static getSslPath($aetrayPath=false)
static getRootPath($aetrayPath=false)
static getLogFilePath($aetrayPath=false)
static getStartupLogFilePath($aetrayPath=false)
static getIconsPath($aetrayPath=false)
static getWebImagesPath()
static init($rootPath, $corePath)
static getProcessName()
static aetrayPath($path)
static getServicesLogFilePath($aetrayPath=false)
static getToolsPath($aetrayPath=false)
static getSetEnvExe($aetrayPath=false)
static getOpenSslExe($aetrayPath=false)
static getLogsPath($aetrayPath=false)
static getResourcesPath($aetrayPath=false)
static getConfigFilePath($aetrayPath=false)
static getWebResourcesUrl()
static getMkcertExe($aetrayPath=false)
static getLocalUrl($request=null)
static getWwwPath($aetrayPath=false)
static getNssmEnvPaths()
static $corePath
static formatWindowsPath($path)
static changePath($filesToScan, $rootPath=null)
static getVhostsPath($aetrayPath=false)
static getOpenSslPath($aetrayPath=false)
static getMkcertPath($aetrayPath=false)
static getNssmLogFilePath($aetrayPath=false)
static formatUnixPath($path)
static $pathFormatCacheMaxSize
static getPhpExe($aetrayPath=false)
static getRegistryLogFilePath($aetrayPath=false)
static getHostsEditorPath($aetrayPath=false)
static $pathFormatStats
static getModuleSymlinkPath($module)
static getPathFormatStats()
static getScript($type)
static getWebIconsPath()
static getisRootFilePath($aetrayPath=false)
static getOpenSslConf($aetrayPath=false)
static getBinPath($aetrayPath=false)
static getPwgenExe($aetrayPath=false)
static getTmpPath($aetrayPath=false)
static getLibsPath($aetrayPath=false)
static getLastPath($aetrayPath=false)
static getModuleCurrentPath($module)
static getHomepageLogFilePath($aetrayPath=false)
static getLnExe($aetrayPath=false)
static getLangsPath($aetrayPath=false)
static getSetEnvPath($aetrayPath=false)
static getModuleRootPath($module)
static getStartupLnkPath()
static getNssmExe($aetrayPath=false)
static getPhpPath($aetrayPath=false)
static getErrorLogFilePath($aetrayPath=false)
static getImagesPath($aetrayPath=false)
static getPowerShellPath()
static $pathFormatCache
static getAppsPath($aetrayPath=false)
static getMkcertRootCaName()
static getWinbinderLogFilePath($aetrayPath=false)
static getIniFilePath($aetrayPath=false)
static getLnPath($aetrayPath=false)
static findFile($startPath, $findFile)
static getSpecialFolderPath($folderName)
const APP_TITLE
Definition root.php:13