48 foreach ($paths as $path) {
69 $handle = @opendir($path);
74 while (
false !== ($file = readdir($handle))) {
75 if ($file ==
'.' || $file ==
'..' || in_array($file, $exclude)) {
78 if (is_dir($path .
'/' . $file)) {
86 $r = @unlink($path .
'/' . $file);
112 $path = rtrim($path,
'/\\') .
'/';
113 $files = glob($path .
'*', GLOB_MARK);
115 if ($files ===
false) {
116 Log::error(
"deleteFolder(): Failed to glob path: " . $path);
121 foreach ($files as $file) {
122 $normalizedFile = rtrim($file,
'/\\');
124 if (is_link($normalizedFile)) {
125 if (!@unlink($normalizedFile) && !@rmdir($normalizedFile)) {
126 Log::error(
"deleteFolder(): Failed to unlink symlink: " . $normalizedFile);
128 } elseif (is_dir($file)) {
131 if (!@unlink($normalizedFile)) {
132 Log::error(
"deleteFolder(): Failed to unlink file: " . $normalizedFile);
137 if (!@rmdir($path)) {
140 Log::error(
"deleteFolder(): Failed to remove directory: " . $path);
154 public static function findFile($startPath, $findFile)
158 $handle = @opendir($startPath);
163 while (
false !== ($file = readdir($handle))) {
164 if ($file ==
'.' || $file ==
'..') {
167 if (is_dir($startPath .
'/' . $file)) {
172 } elseif ($file == $findFile) {
192 return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)
193 || filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
217 if (strtoupper(substr(PHP_OS, 0, 3)) !==
'WIN') {
219 if (function_exists(
'posix_geteuid')) {
220 return posix_geteuid() === 0;
230 if ($output !==
null) {
232 if (stripos($output,
'Access is denied') !==
false ||
233 stripos($output,
'System error 5') !==
false ||
234 stripos($output,
'Zugriff verweigert') !==
false) {
240 if (stripos($output,
'There are no entries') !==
false ||
241 stripos($output,
'These workstations') !==
false ||
242 preg_match(
'/\\\\\\\\/', $output)) {
249 if ($output !==
null && !empty($output)) {
251 if (stripos($output,
'S-1-16-12288') !==
false ||
252 stripos($output,
'S-1-5-32-544') !==
false) {
257 if (stripos($output,
'S-1-16-8192') !==
false) {
264 $testFile = getenv(
'SystemRoot') .
'\\Temp\\bearsampp_admin_test_' . uniqid() .
'.tmp';
265 $result = @file_put_contents($testFile,
'test');
288 '/^define\((.*?)' . $var .
'(.*?),/' =>
'define(\'' . $var .
'\',
' . (is_int($value) ? $value : '\
'' . $value .
'\'') .
');'
302 if (file_exists($path)) {
303 $lines = file($path);
304 $fp = fopen($path,
'w');
305 foreach ($lines as $nb => $line) {
306 $replaceDone =
false;
307 foreach ($replaceList as $regex => $replace) {
308 if (preg_match($regex, $line, $matches)) {
309 $currentReplace = $replace;
310 $countParams = preg_match_all(
'/{{(\d+)}}/', $currentReplace, $paramsMatches);
311 if ($countParams > 0 && $countParams <= count($matches)) {
312 foreach ($paramsMatches[1] as $paramsMatch) {
313 $currentReplace = str_replace(
'{{' . $paramsMatch .
'}}', $matches[$paramsMatch], $currentReplace);
316 Log::trace(
'Replace in file ' . $path .
' :');
319 Log::trace(
'## new: ' . trim($currentReplace));
322 $ending = (preg_match(
"/\r\n$/", $line)) ?
"\r\n" : (preg_match(
"/\n$/", $line) ?
"\n" :
"");
323 fwrite($fp, rtrim($currentReplace) . $ending);
349 $handle = @opendir($path);
354 $prefix = basename($path);
356 while (
false !== ($file = readdir($handle))) {
357 $filePath = $path .
'/' . $file;
358 if ($file !=
'.' && $file !=
'..' && is_dir($filePath) && $file !=
'current') {
359 if (strpos($file, $prefix) === 0) {
360 $version = substr($file, strlen($prefix));
381 list($usec, $sec) = explode(
' ', microtime());
383 return ((
float)$usec + (
float)$sec);
396 return $lnk ? file_exists($lnk) :
false;
409 if (!$shortcutPath) {
431 if (file_exists($startupLnkPath)) {
432 return @unlink($startupLnkPath);
450 public static function findRepos($initPath, $startPath, $checkFile, $maxDepth = 1)
452 $depth = substr_count(str_replace($initPath,
'', $startPath),
'/');
455 $handle = @opendir($startPath);
460 while (
false !== ($file = readdir($handle))) {
461 if ($file ==
'.' || $file ==
'..') {
464 if (is_dir($startPath .
'/' . $file) && ($initPath == $startPath || $depth <= $maxDepth)) {
465 $tmpResults =
self::findRepos($initPath, $startPath .
'/' . $file, $checkFile, $maxDepth);
466 foreach ($tmpResults as $tmpResult) {
469 } elseif (is_file($startPath .
'/' . $checkFile) && !in_array($startPath,
$result)) {
488 $type = pathinfo($path, PATHINFO_EXTENSION);
489 $data = file_get_contents($path);
491 return 'data:image/' . $type .
';base64,' . base64_encode($data);
504 if ($direction ===
'to_utf8') {
520 return iconv(
'UTF-8',
'WINDOWS-1252//IGNORE', $data);
532 return iconv(
'WINDOWS-1252',
'UTF-8//IGNORE', $data);
567 foreach ($pids as $pid) {
590 file_put_contents($statusFile, json_encode([
'text' => $text]));
603 if (file_exists($statusFile)) {
604 @unlink($statusFile);
618 public static function getFilesToScan($path =
null, $useCache =
true, $forceRefresh =
false)
621 $cacheKey = md5(serialize($path));
624 if ($useCache && !$forceRefresh) {
626 if ($cachedResult !==
false) {
628 Log::debug(
'File scan cache HIT (saved expensive scan operation)');
630 return $cachedResult;
635 Log::debug(
'File scan cache MISS (performing full scan)');
642 foreach ($pathsToScan as $pathToScan) {
644 $findFiles =
self::findFiles($pathToScan[
'path'], $pathToScan[
'includes'], $pathToScan[
'recursive']);
645 foreach ($findFiles as $findFile) {
648 Log::debug($pathToScan[
'path'] .
' scanned in ' . round(self::getMicrotime() - $pathStartTime, 3) .
's');
651 $totalTime = round(self::getMicrotime() - $startTime, 3);
652 Log::info(
'Full file scan completed in ' . $totalTime .
's (' . count(
$result) .
' files found)');
695 'includes' => array(
''),
702 'includes' => array(
''),
709 'includes' => array(
'openssl.cfg'),
716 'includes' => array(
'alias.conf'),
722 foreach ($folderList as $folder) {
725 'includes' => array(
'.ini',
'.conf'),
732 foreach ($folderList as $folder) {
735 'includes' => array(
'.php',
'.bat',
'.ini',
'.reg',
'.inc'),
742 foreach ($folderList as $folder) {
745 'includes' => array(
'my.ini'),
752 foreach ($folderList as $folder) {
755 'includes' => array(
'my.ini'),
760 if (is_dir($dataPath)) {
763 'includes' => array(
'my.ini'),
771 foreach ($folderList as $folder) {
774 'includes' => array(
'.conf',
'.bat',
'.ber'),
781 foreach ($folderList as $folder) {
784 'includes' => array(
'npmrc'),
789 'includes' => array(
'npmrc'),
796 foreach ($folderList as $folder) {
799 'includes' => array(
'giscus.json'),
806 foreach ($folderList as $folder) {
809 'includes' => array(
'console.xml',
'.ini',
'.btm'),
816 foreach ($folderList as $folder) {
819 'includes' => array(
'.bat'),
824 'includes' => array(
'winpython.ini'),
831 foreach ($folderList as $folder) {
834 'includes' => array(
'!.dll',
'!.exe'),
851 private static function findFiles($startPath, $includes = array(
''), $recursive =
true)
855 $handle = @opendir($startPath);
860 while (
false !== ($file = readdir($handle))) {
861 if ($file ==
'.' || $file ==
'..') {
864 if (is_dir($startPath .
'/' . $file) && $recursive) {
866 foreach ($tmpResults as $tmpResult) {
869 } elseif (is_file($startPath .
'/' . $file)) {
870 foreach ($includes as $include) {
872 $include = ltrim($include,
'!');
875 } elseif ($file != $include) {
900 Log::trace(
'[VCHK-3] getLatestVersion() START - fetching latest version from: ' . $url);
905 Log::trace(
'[VCHK-3] getLatestVersion() EXIT - empty response received');
910 $resultArray = json_decode(
$result,
true);
911 if (isset($resultArray[
'tag_name']) && isset($resultArray[
'assets'][0][
'browser_download_url'])) {
912 $tagName = $resultArray[
'tag_name'];
913 $downloadUrl = $resultArray[
'assets'][0][
'browser_download_url'];
914 $name = $resultArray[
'name'];
915 Log::trace(
'Latest version tag name: ' . $tagName);
918 Log::trace(
'[VCHK-3] getLatestVersion() SUCCESS - version found: ' . $tagName);
920 return [
'version' => $tagName,
'html_url' => $downloadUrl,
'name' => $name];
922 Log::error(
'Tag name, download URL, or name not found in the response: ' .
$result);
923 Log::trace(
'[VCHK-3] getLatestVersion() EXIT - tag_name/download_url missing in JSON response');
945 return match ($unit) {
946 'GB' => number_format($size / (1 << 30), 2) .
'GB',
947 'MB' => number_format($size / (1 << 20), 2) .
'MB',
948 'KB' => number_format($size / (1 << 10), 2) .
'KB',
949 default => number_format($size) .
' bytes',
954 if (function_exists(
'human_readable_size')) {
955 return human_readable_size($size, precision: 2);
959 $units = array(
'B',
'KB',
'MB',
'GB',
'TB');
960 $power = $size > 0 ? (int)floor(log($size, 1024)) : 0;
961 $power = min($power, count($units) - 1);
963 return number_format($size / (1024 ** $power), 2) . $units[$power];
973 global $bearsamppRegistry;
974 $processor = $bearsamppRegistry->getProcessorRegKey();
988 return filter_var($domainName, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME) !==
false;
1002 $handle = @opendir($path);
1007 while (
false !== ($file = readdir($handle))) {
1008 $filePath = $path .
'/' . $file;
1009 if ($file !=
'.' && $file !=
'..' && is_dir($filePath) && $file !=
'current') {
1033 $caption = preg_replace(
'/[<>:"\/\\\\|?*\x00-\x1F]|\.$/',
'', trim($caption));
1035 file_put_contents($tmpFile, $content);
1039 if ($editor ==
null || trim($editor) ==
'') {
1040 $editor =
'notepad.exe';
1044 if ($editor ===
false) {
1045 $editor = getenv(
'SystemRoot') .
'\System32\notepad.exe';
1048 $bearsamppWinbinder->exec($editor,
'"' . $tmpFile .
'"',
false,
true,
true);
static set($cacheKey, $data)
static shellExec(string $command)
static getApiJson($url, $verify=true)
static info($data, $file=null)
static debug($data, $file=null)
static trace($data, $file=null)
static error($data, $file=null)
static getExeFilePath($aetrayPath=false)
static findExecutable($name)
static getAliasPath($aetrayPath=false)
static getRootPath($aetrayPath=false)
static getIconsPath($aetrayPath=false)
static getResourcesPath($aetrayPath=false)
static getVhostsPath($aetrayPath=false)
static getOpenSslPath($aetrayPath=false)
static formatUnixPath($path)
static getPhpExe($aetrayPath=false)
static getTmpPath($aetrayPath=false)
static getModuleRootPath($module)
static getStartupLnkPath()
static findRepos($initPath, $startPath, $checkFile, $maxDepth=1)
static disableLaunchStartup()
static deleteFolder($path)
static isValidPort($port)
static cp1252ToUtf8($data)
static imgToBase64($path)
static getVersionList($path)
static utf8ToCp1252($data)
static isValidDomainName($domainName)
static getLatestVersion($url)
static getFolderList($path)
static humanFileSize(int $size, string $unit='')
static openFileContent($caption, $content)
static clearLoadingText()
static findFiles($startPath, $includes=array(''), $recursive=true)
static getFilesToScan($path=null, $useCache=true, $forceRefresh=false)
static replaceDefine($path, $var, $value)
static clearFolder($path, $exclude=array())
static convertEncoding($data, $direction='to_cp1252')
static clearFolders($paths, $exclude=array())
static findFile($startPath, $findFile)
static updateLoadingText($text)
static enableLaunchStartup()
static replaceInFile($path, $replaceList)
static contains($string, $search)
static startWith($string, $search)
static endWith($string, $search)
static createShortcut($shortcutPath, $targetPath, $workingDir='', $description='', $iconPath='')