Bearsampp 2026.9.23
Loading...
Searching...
No Matches
class.tpl.powershell.php
Go to the documentation of this file.
1<?php
2/*
3 * Copyright (c) 2021-2026 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
17{
18 // Icon constants - Currently unused as native PowerShell consoles do not support custom icons
19 // These icons were used with ConsoleZ but are not supported by standard PowerShell console host
20 // To use icons, consider using a terminal emulator like ConEmu, Cmder, or Windows Terminal
21 // Icon files are located at: core/resources/homepage/img/icons/
22 /*
23 const ICON_APP = 'app.ico';
24 const ICON_POWERSHELL = 'powershell.ico';
25 const ICON_PEAR = 'pear.ico';
26 const ICON_DB = 'db.ico';
27 const ICON_GHOSTSCRIPT = 'ghostscript.ico';
28 const ICON_GIT = 'git.ico';
29 const ICON_NODEJS = 'nodejs.ico';
30 const ICON_COMPOSER = 'composer.ico';
31 const ICON_PYTHON = 'python.ico';
32 const ICON_RUBY = 'ruby.ico';
33 const ICON_PERL = 'perl.ico';
34 const ICON_NGROK = 'ngrok.ico';
35 */
36
40 private function __construct()
41 {
42 }
43
54 public static function process()
55 {
56 global $bearsamppTools;
57
58 $fontName = 'CaskaydiaMono NF'; // Default Nerd Font
59
60 // Collect all console window titles that need font configuration
61 $titles = [];
62 $titles[] = $bearsamppTools->getPowerShell()->getTabTitleDefault();
63 $titles[] = $bearsamppTools->getPowerShell()->getTabTitlePowershell();
64 $titles[] = 'Console';
65 $titles[] = 'Bearsampp Powershell Console'; // Fallback casing
66
67 try {
68 $titles[] = $bearsamppTools->getPowerShell()->getTabTitlePear();
69 } catch (Exception $e) {
70 }
71 try {
72 $titles[] = $bearsamppTools->getPowerShell()->getTabTitleMysql();
73 } catch (Exception $e) {
74 }
75 try {
76 $titles[] = $bearsamppTools->getPowerShell()->getTabTitleMariadb();
77 } catch (Exception $e) {
78 }
79 try {
80 $titles[] = $bearsamppTools->getPowerShell()->getTabTitlePostgresql();
81 } catch (Exception $e) {
82 }
83 try {
84 $titles[] = $bearsamppTools->getPowerShell()->getTabTitleGit();
85 } catch (Exception $e) {
86 }
87 try {
88 $titles[] = $bearsamppTools->getPowerShell()->getTabTitleNodejs();
89 } catch (Exception $e) {
90 }
91 try {
92 $titles[] = $bearsamppTools->getPowerShell()->getTabTitleComposer();
93 } catch (Exception $e) {
94 }
95 try {
96 $titles[] = $bearsamppTools->getPowerShell()->getTabTitlePython();
97 } catch (Exception $e) {
98 }
99 try {
100 $titles[] = $bearsamppTools->getPowerShell()->getTabTitleRuby();
101 } catch (Exception $e) {
102 }
103 try {
104 $titles[] = $bearsamppTools->getPowerShell()->getTabTitlePerl();
105 } catch (Exception $e) {
106 }
107 try {
108 $titles[] = $bearsamppTools->getPowerShell()->getTabTitleGhostscript();
109 } catch (Exception $e) {
110 }
111 try {
112 $titles[] = $bearsamppTools->getPowerShell()->getTabTitleNgrok();
113 } catch (Exception $e) {
114 }
115
116 // Also include generic/short titles
117 $shortTitles = ["Composer", "Ghostscript", "ngrok", "PEAR", "Perl", "Ruby", "Git", "Python", "MariaDB", "MySQL", "PostgreSQL", "Node.js"];
118 $allTitles = array_unique(array_merge($titles, $shortTitles));
119
120 // Build a single .reg file with all HKCU Console font settings.
121 // This replaces ~140 individual exec("reg add ...") calls, each of which
122 // would spawn a separate process and potentially flash a console window.
123 $regContent = "Windows Registry Editor Version 5.00\r\n";
124
125 // Global HKCU\Console defaults
126 $regContent .= "\r\n[HKEY_CURRENT_USER\\Console]\r\n";
127 $regContent .= '"FaceName"="' . $fontName . '"' . "\r\n";
128 $regContent .= '"FontFamily"=dword:00000036' . "\r\n";
129 $regContent .= '"CodePage"=dword:0000fde9' . "\r\n";
130
131 // Per-title settings
132 foreach ($allTitles as $title) {
133 if (empty($title)) {
134 continue;
135 }
136 $regContent .= "\r\n[HKEY_CURRENT_USER\\Console\\" . $title . "]\r\n";
137 $regContent .= '"FaceName"="' . $fontName . '"' . "\r\n";
138 $regContent .= '"FontFamily"=dword:00000036' . "\r\n";
139 $regContent .= '"FontSize"=dword:00100000' . "\r\n";
140 $regContent .= '"FontWeight"=dword:00000190' . "\r\n";
141 $regContent .= '"CodePage"=dword:0000fde9' . "\r\n";
142 $regContent .= '"ScreenBufferSize"=dword:0bb8006e' . "\r\n";
143 $regContent .= '"WindowSize"=dword:001e006e' . "\r\n";
144 }
145
146 // Register as a valid TrueType console font (HKCU only — no elevation needed)
147 $regContent .= "\r\n[HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Console\\TrueTypeFont]\r\n";
148 $regContent .= '"0"="' . $fontName . '"' . "\r\n";
149 $regContent .= '"00"="' . $fontName . '"' . "\r\n";
150 $regContent .= '"000"="' . $fontName . '"' . "\r\n";
151
152 // Write and import the .reg file in a single process — zero flashing windows
153 $tmpReg = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'bearsampp_console_font.reg';
154
155 $bytes = file_put_contents($tmpReg, $regContent);
156 if ($bytes === false || $bytes === 0) {
157 return false;
158 }
159
160 $cmd = 'reg import "' . $tmpReg . '"';
161 $exitCode = 1;
162 try {
163 $wsh = new COM('WScript.Shell');
164 $exitCode = $wsh->Run($cmd, 0, true);
165 } catch (Throwable $e) {
166 $output = [];
167 exec($cmd, $output, $exitCode);
168 }
169
170 @unlink($tmpReg);
171
172 if ($exitCode !== 0) {
173 return false;
174 }
175
176 return true;
177 }
178
205
217 private static function getTabCmdSection()
218 {
219 global $bearsamppRoot, $bearsamppTools;
220
221 return self::getTab(
222 $bearsamppTools->getPowerShell()->getTabTitleDefault(),
223 null, // self::ICON_APP - Icon not supported in native PowerShell console
224 $bearsamppTools->getPowerShell()->getShell(),
226 ) . PHP_EOL;
227 }
228
240 private static function getTabPowerShellSection()
241 {
242 global $bearsamppRoot, $bearsamppTools;
243
244 $powerShellPath = Path::getPowerShellPath();
245 if ($powerShellPath !== false) {
246 return self::getTab(
247 $bearsamppTools->getPowerShell()->getTabTitlePowershell(),
248 null, // self::ICON_POWERSHELL - Icon not supported in native PowerShell console
249 $powerShellPath,
251 ) . PHP_EOL;
252 }
253
254 return "";
255 }
256
268 private static function getTabPearSection()
269 {
270 global $bearsamppBins, $bearsamppTools;
271
272 $shell = $bearsamppTools->getPowerShell()->getShell('&quot;' . $bearsamppBins->getPhp()->getPearExe() . '&quot;');
273 if (!file_exists($bearsamppBins->getPhp()->getPearExe())) {
274 $shell = $bearsamppTools->getPowerShell()->getShell('echo ' . $bearsamppBins->getPhp()->getPearExe() . ' not found');
275 }
276
277 return self::getTab(
278 $bearsamppTools->getPowerShell()->getTabTitlePear(),
279 null, // self::ICON_PEAR - Icon not supported in native PowerShell console
280 $shell,
281 Path::getModuleSymlinkPath($bearsamppBins->getPhp()) . '/pear'
282 ) . PHP_EOL;
283 }
284
296 private static function getTabMysqlSection()
297 {
298 global $bearsamppBins, $bearsamppTools;
299
300 $shell = $bearsamppTools->getPowerShell()->getShell(
301 '&quot;' . $bearsamppBins->getMysql()->getCliExe() . '&quot; -u' .
302 $bearsamppBins->getMysql()->getRootUser() .
303 ($bearsamppBins->getMysql()->getRootPwd() ? ' -p' : '')
304 );
305 if (!file_exists($bearsamppBins->getMysql()->getCliExe())) {
306 $shell = $bearsamppTools->getPowerShell()->getShell('echo ' . $bearsamppBins->getMysql()->getCliExe() . ' not found');
307 }
308
309 return self::getTab(
310 $bearsamppTools->getPowerShell()->getTabTitleMysql(),
311 null, // self::ICON_DB - Icon not supported in native PowerShell console
312 $shell,
314 ) . PHP_EOL;
315 }
316
328 private static function getTabMariadbSection()
329 {
330 global $bearsamppBins, $bearsamppTools;
331
332 $shell = $bearsamppTools->getPowerShell()->getShell(
333 '&quot;' . $bearsamppBins->getMariadb()->getCliExe() . '&quot; -u' .
334 $bearsamppBins->getMariadb()->getRootUser() .
335 ($bearsamppBins->getMariadb()->getRootPwd() ? ' -p' : '')
336 );
337 if (!file_exists($bearsamppBins->getMariadb()->getCliExe())) {
338 $shell = $bearsamppTools->getPowerShell()->getShell('echo ' . $bearsamppBins->getMariadb()->getCliExe() . ' not found');
339 }
340
341 return self::getTab(
342 $bearsamppTools->getPowerShell()->getTabTitleMariadb(),
343 null, // self::ICON_DB - Icon not supported in native PowerShell console
344 $shell,
346 ) . PHP_EOL;
347 }
348
360 private static function getTabPostgresqlSection()
361 {
362 global $bearsamppBins, $bearsamppTools;
363
364 $shell = $bearsamppTools->getPowerShell()->getShell(
365 '&quot;' . $bearsamppBins->getPostgresql()->getCliExe() . '&quot;' .
366 ' -h ' . APP_LOCALHOST .
367 ' -p ' . $bearsamppBins->getPostgresql()->getPort() .
368 ' -U ' . $bearsamppBins->getPostgresql()->getRootUser() .
369 ' -d postgres'
370 );
371 if (!file_exists($bearsamppBins->getPostgresql()->getCliExe())) {
372 $shell = $bearsamppTools->getPowerShell()->getShell('echo ' . $bearsamppBins->getPostgresql()->getCliExe() . ' not found');
373 }
374
375 return self::getTab(
376 $bearsamppTools->getPowerShell()->getTabTitlePostgresql(),
377 null, // self::ICON_DB - Icon not supported in native PowerShell console
378 $shell,
380 ) . PHP_EOL;
381 }
382
394 private static function getTabGitSection()
395 {
396 global $bearsamppRoot, $bearsamppTools;
397
398 $shell = $bearsamppTools->getPowerShell()->getShell();
399 if (!file_exists($bearsamppTools->getGit()->getExe())) {
400 $shell = $bearsamppTools->getPowerShell()->getShell('echo ' . $bearsamppTools->getGit()->getExe() . ' not found');
401 }
402
403 return self::getTab(
404 $bearsamppTools->getPowerShell()->getTabTitleGit(),
405 null, // self::ICON_GIT - Icon not supported in native PowerShell console
406 $shell,
408 ) . PHP_EOL;
409 }
410
423 private static function getTabNodejsSection()
424 {
425 global $bearsamppRoot, $bearsamppBins, $bearsamppTools;
426
427 $shell = $bearsamppTools->getPowerShell()->getShell('&quot;' . $bearsamppBins->getNodejs()->getLaunch() . '&quot;');
428 if (!file_exists($bearsamppBins->getNodejs()->getLaunch())) {
429 $shell = $bearsamppTools->getPowerShell()->getShell('echo ' . $bearsamppBins->getNodejs()->getLaunch() . ' not found');
430 }
431
432 return self::getTab(
433 $bearsamppTools->getPowerShell()->getTabTitleNodejs(),
434 null, // self::ICON_NODEJS - Icon not supported in native PowerShell console
435 $shell,
437 ) . PHP_EOL;
438 }
439
451 private static function getTabComposerSection()
452 {
453 global $bearsamppRoot, $bearsamppTools;
454
455 $shell = $bearsamppTools->getPowerShell()->getShell('&quot;' . $bearsamppTools->getComposer()->getExe() . '&quot;');
456 if (!file_exists($bearsamppTools->getComposer()->getExe())) {
457 $shell = $bearsamppTools->getPowerShell()->getShell('echo ' . $bearsamppTools->getComposer()->getExe() . ' not found');
458 }
459
460 return self::getTab(
461 $bearsamppTools->getPowerShell()->getTabTitleComposer(),
462 null, // self::ICON_COMPOSER - Icon not supported in native PowerShell console
463 $shell,
465 ) . PHP_EOL;
466 }
467
479 private static function getTabPythonSection()
480 {
481 global $bearsamppRoot, $bearsamppTools;
482
483 $shell = $bearsamppTools->getPowerShell()->getShell('&quot;' . $bearsamppTools->getPython()->getExe() . '&quot;');
484 if (!file_exists($bearsamppTools->getPython()->getExe())) {
485 $shell = $bearsamppTools->getPowerShell()->getShell('echo ' . $bearsamppTools->getPython()->getExe() . ' not found');
486 }
487
488 return self::getTab(
489 $bearsamppTools->getPowerShell()->getTabTitlePython(),
490 null, // self::ICON_PYTHON - Icon not supported in native PowerShell console
491 $shell,
493 ) . PHP_EOL;
494 }
495
507 private static function getTabRubySection()
508 {
509 global $bearsamppRoot, $bearsamppTools;
510
511 // Check if Ruby exists first
512 if (!file_exists($bearsamppTools->getRuby()->getExe())) {
513 $shell = $bearsamppTools->getPowerShell()->getShell('echo ' . $bearsamppTools->getRuby()->getExe() . ' not found');
514 } else {
515 // Use irb (Interactive Ruby) for an interactive shell
516 $rubyDir = dirname($bearsamppTools->getRuby()->getExe());
517 $irbExe = $rubyDir . '/irb.bat';
518
519 // Check if irb.bat exists, otherwise try irb.cmd or just use ruby with -i flag
520 if (file_exists($irbExe)) {
521 $shell = $bearsamppTools->getPowerShell()->getShell('&quot;' . $irbExe . '&quot;');
522 } elseif (file_exists($rubyDir . '/irb.cmd')) {
523 $shell = $bearsamppTools->getPowerShell()->getShell('&quot;' . $rubyDir . '/irb.cmd' . '&quot;');
524 } elseif (file_exists($rubyDir . '/irb')) {
525 $shell = $bearsamppTools->getPowerShell()->getShell('&quot;' . $rubyDir . '/irb' . '&quot;');
526 } else {
527 // Fallback to just opening a shell in the Ruby directory
528 $shell = $bearsamppTools->getPowerShell()->getShell();
529 }
530 }
531
532 return self::getTab(
533 $bearsamppTools->getPowerShell()->getTabTitleRuby(),
534 null, // self::ICON_RUBY - Icon not supported in native PowerShell console
535 $shell,
537 ) . PHP_EOL;
538 }
539
551 private static function getTabPerlSection()
552 {
553 global $bearsamppRoot, $bearsamppTools;
554
555 $shell = $bearsamppTools->getPowerShell()->getShell('&quot;' . $bearsamppTools->getPerl()->getExe() . '&quot;');
556 if (!file_exists($bearsamppTools->getPerl()->getExe())) {
557 $shell = $bearsamppTools->getPowerShell()->getShell('echo ' . $bearsamppTools->getPerl()->getExe() . ' not found');
558 }
559
560 return self::getTab(
561 $bearsamppTools->getPowerShell()->getTabTitlePerl(),
562 null, // self::ICON_PERL - Icon not supported in native PowerShell console
563 $shell,
565 ) . PHP_EOL;
566 }
567
581 private static function getTabGhostscriptSection()
582 {
583 global $bearsamppRoot, $bearsamppTools;
584
585 $shell = $bearsamppTools->getPowerShell()->getShell('&quot;' . $bearsamppTools->getGhostscript()->getExeConsole() . '&quot;');
586 if (!file_exists($bearsamppTools->getGhostscript()->getExeConsole())) {
587 $shell = $bearsamppTools->getPowerShell()->getShell('echo ' . $bearsamppTools->getGhostscript()->getExeConsole() . ' not found');
588 }
589
590 return self::getTab(
591 $bearsamppTools->getPowerShell()->getTabTitleGhostscript(),
592 null, // self::ICON_GHOSTSCRIPT - Icon not supported in native PowerShell console
593 $shell,
595 ) . PHP_EOL;
596 }
597
611 private static function getTabNgrokSection()
612 {
613 global $bearsamppRoot, $bearsamppTools;
614
615 $shell = $bearsamppTools->getPowerShell()->getShell('&quot;' . $bearsamppTools->getNgrok()->getExe() . '&quot;');
616 if (!file_exists($bearsamppTools->getNgrok()->getExe())) {
617 $shell = $bearsamppTools->getPowerShell()->getShell('echo ' . $bearsamppTools->getNgrok()->getExe() . ' not found');
618 }
619
620 return self::getTab(
621 $bearsamppTools->getPowerShell()->getTabTitleNgrok(),
622 null, // self::ICON_NGROK - Icon not supported in native PowerShell console
623 $shell,
625 ) . PHP_EOL;
626 }
627
646 private static function getTab($title, $icon, $shell, $initDir)
647 {
648 global $bearsamppCore;
649 // Icon parameter is ignored as native PowerShell console does not support custom icons
650 // Return tab information as a formatted string
651 return "Title: $title | Shell: $shell | InitDir: $initDir";
652 }
653}
global $bearsamppBins
global $bearsamppRoot
global $bearsamppCore
static getRootPath($aetrayPath=false)
static getWwwPath($aetrayPath=false)
static getModuleSymlinkPath($module)
static getPowerShellPath()
static getTab($title, $icon, $shell, $initDir)
static getTabGhostscriptSection()
const APP_LOCALHOST
Definition root.php:21