2024.8.23
Loading...
Searching...
No Matches
class.action.startup.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 ActionStartup
12 * Handles the startup process of the Bearsampp application, including initializing services,
13 * cleaning temporary files, refreshing configurations, and more.
14 */
16{
17 private $splash;
18 private $restart;
19 private $startTime;
20 private $error;
21
22 private $rootPath;
23 private $filesToScan;
24
25 const GAUGE_SERVICES = 5;
26 const GAUGE_OTHERS = 19;
27
28 /**
29 * ActionStartup constructor.
30 * Initializes the startup process, including the splash screen and various configurations.
31 *
32 * @param array $args Command line arguments.
33 */
34 public function __construct($args)
35 {
36 global $bearsamppRoot, $bearsamppCore, $bearsamppLang, $bearsamppBins, $bearsamppWinbinder;
37 $this->writeLog( 'Starting ' . APP_TITLE );
38
39 // Init
40 $this->splash = new Splash();
41 $this->restart = false;
42 $this->startTime = Util::getMicrotime();
43 $this->error = '';
44
45 $this->rootPath = $bearsamppRoot->getRootPath();
46 $this->filesToScan = array();
47
48 $gauge = self::GAUGE_SERVICES * count( $bearsamppBins->getServices() );
49 $gauge += self::GAUGE_OTHERS + 1;
50
51 // Start splash screen
52 $this->splash->init(
53 $bearsamppLang->getValue( Lang::STARTUP ),
54 $gauge,
55 sprintf( $bearsamppLang->getValue( Lang::STARTUP_STARTING_TEXT ), APP_TITLE . ' ' . $bearsamppCore->getAppVersion() )
56 );
57
58 $bearsamppWinbinder->setHandler( $this->splash->getWbWindow(), $this, 'processWindow', 1000 );
59 $bearsamppWinbinder->mainLoop();
60 $bearsamppWinbinder->reset();
61 }
62
63 /**
64 * Processes the main window events during startup.
65 *
66 * @param mixed $window The window handle.
67 * @param int $id The event ID.
68 * @param mixed $ctrl The control that triggered the event.
69 * @param mixed $param1 Additional parameter 1.
70 * @param mixed $param2 Additional parameter 2.
71 */
72 public function processWindow($window, $id, $ctrl, $param1, $param2)
73 {
74 global $bearsamppRoot, $bearsamppCore, $bearsamppLang, $bearsamppBins, $bearsamppTools, $bearsamppApps, $bearsamppWinbinder;
75
76 // Rotation logs
77 $this->rotationLogs();
78
79 // Clean
80 $this->cleanTmpFolders();
81 $this->cleanOldBehaviors();
82
83 // List procs
84 if ( $bearsamppRoot->getProcs() !== false ) {
85 $this->writeLog( 'List procs:' );
86 $listProcs = array();
87 foreach ( $bearsamppRoot->getProcs() as $proc ) {
89 $listProcs[] = '-> ' . basename( $unixExePath ) . ' (PID ' . $proc[Win32Ps::PROCESS_ID] . ') in ' . $unixExePath;
90 }
91 sort( $listProcs );
92 foreach ( $listProcs as $proc ) {
93 $this->writeLog( $proc );
94 }
95 }
96
97 // List modules
98 $this->writeLog( 'List bins modules:' );
99 foreach ( $bearsamppBins->getAll() as $module ) {
100 if ( !$module->isEnable() ) {
101 $this->writeLog( '-> ' . $module->getName() . ': ' . $bearsamppLang->getValue( Lang::DISABLED ) );
102 }
103 else {
104 $this->writeLog( '-> ' . $module->getName() . ': ' . $module->getVersion() . ' (' . $module->getRelease() . ')' );
105 }
106 }
107 $this->writeLog( 'List tools modules:' );
108 foreach ( $bearsamppTools->getAll() as $module ) {
109 if ( !$module->isEnable() ) {
110 $this->writeLog( '-> ' . $module->getName() . ': ' . $bearsamppLang->getValue( Lang::DISABLED ) );
111 }
112 else {
113 $this->writeLog( '-> ' . $module->getName() . ': ' . $module->getVersion() . ' (' . $module->getRelease() . ')' );
114 }
115 }
116 $this->writeLog( 'List apps modules:' );
117 foreach ( $bearsamppApps->getAll() as $module ) {
118 if ( !$module->isEnable() ) {
119 $this->writeLog( '-> ' . $module->getName() . ': ' . $bearsamppLang->getValue( Lang::DISABLED ) );
120 }
121 else {
122 $this->writeLog( '-> ' . $module->getName() . ': ' . $module->getVersion() . ' (' . $module->getRelease() . ')' );
123 }
124 }
125
126 // Kill old instances
127 $this->killOldInstances();
128
129 // Prepare app
130 $this->refreshHostname();
131 $this->checkLaunchStartup();
132 $this->checkBrowser();
133 $this->sysInfos();
134 $this->refreshAliases();
135 $this->refreshVhosts();
136
137 // Check app path
138 $this->checkPath();
139 $this->scanFolders();
140 $this->changePath();
141 $this->savePath();
142
143 // Check BEARSAMPP_PATH, BEARSAMPP_BINS and System Path reg keys
144 $this->checkPathRegKey();
145 $this->checkBinsRegKey();
146 $this->checkSystemPathRegKey();
147
148 // Update config
149 $this->updateConfig();
150
151 // Create SSL certificates
152 $this->createSslCrts();
153
154 // Install
155 $this->installServices();
156
157 // Actions if everything OK
158 if ( !$this->restart && empty( $this->error ) ) {
159 $this->refreshGitRepos();
160 $this->writeLog( 'Started in ' . round( Util::getMicrotime() - $this->startTime, 3 ) . 's' );
161 }
162 else {
163 $this->splash->incrProgressBar( 2 );
164 }
165
166 if ( $this->restart ) {
167 $this->writeLog( APP_TITLE . ' has to be restarted' );
168 $this->splash->setTextLoading(
169 sprintf(
171 APP_TITLE . ' ' . $bearsamppCore->getAppVersion()
172 )
173 );
174 foreach ( $bearsamppBins->getServices() as $sName => $service ) {
175 $service->delete();
176 }
178 }
179
180 if ( !empty( $this->error ) ) {
181 $this->writeLog( 'Error: ' . $this->error );
182 $bearsamppWinbinder->messageBoxError( $this->error, $bearsamppLang->getValue( Lang::STARTUP_ERROR_TITLE ) );
183 }
184
186 $bearsamppWinbinder->destroyWindow( $window );
187 }
188
189 /**
190 * Rotates the logs by archiving old logs and purging old archives.
191 */
192 private function rotationLogs()
193 {
195
196 $this->splash->setTextLoading( $bearsamppLang->getValue( Lang::STARTUP_ROTATION_LOGS_TEXT ) );
197 $this->splash->incrProgressBar();
198
199 $archivesPath = $bearsamppRoot->getLogsPath() . '/archives';
200 if ( !is_dir( $archivesPath ) ) {
201 mkdir( $archivesPath, 0777, true );
202
203 return;
204 }
205
206 $date = date( 'Y-m-d-His', time() );
207 $archiveLogsPath = $archivesPath . '/' . $date;
208 $archiveScriptsPath = $archiveLogsPath . '/scripts';
209
210 // Create archive folders
211 mkdir( $archiveLogsPath, 0777, true );
212 mkdir( $archiveScriptsPath, 0777, true );
213
214 // Count archives
215 $archives = array();
216 $handle = @opendir( $archivesPath );
217 if ( !$handle ) {
218 return;
219 }
220 while ( false !== ($file = readdir( $handle )) ) {
221 if ( $file == '.' || $file == '..' ) {
222 continue;
223 }
224 $archives[] = $archivesPath . '/' . $file;
225 }
226 closedir( $handle );
227 sort( $archives );
228
229 // Remove old archives
230 if ( count( $archives ) > $bearsamppConfig->getMaxLogsArchives() ) {
231 $total = count( $archives ) - $bearsamppConfig->getMaxLogsArchives();
232 for ( $i = 0; $i < $total; $i++ ) {
233 Util::deleteFolder( $archives[$i] );
234 }
235 }
236
237 // Logs
238 $srcPath = $bearsamppRoot->getLogsPath();
239 $handle = @opendir( $srcPath );
240 if ( !$handle ) {
241 return;
242 }
243 while ( false !== ($file = readdir( $handle )) ) {
244 if ( $file == '.' || $file == '..' || is_dir( $srcPath . '/' . $file ) ) {
245 continue;
246 }
247 copy( $srcPath . '/' . $file, $archiveLogsPath . '/' . $file );
248 }
249 closedir( $handle );
250
251 // Scripts
252 $srcPath = $bearsamppCore->getTmpPath();
253 $handle = @opendir( $srcPath );
254 if ( !$handle ) {
255 return;
256 }
257 while ( false !== ($file = readdir( $handle )) ) {
258 if ( $file == '.' || $file == '..' || is_dir( $srcPath . '/' . $file ) ) {
259 continue;
260 }
261 copy( $srcPath . '/' . $file, $archiveScriptsPath . '/' . $file );
262 }
263 closedir( $handle );
264
265 // Purge logs
266 Util::clearFolders( $bearsamppBins->getLogsPath() );
267 Util::clearFolder( $bearsamppRoot->getLogsPath(), array('archives', '.gitignore') );
268 }
269
270 /**
271 * Cleans temporary folders by removing unnecessary files.
272 */
273 private function cleanTmpFolders()
274 {
276
277 $this->splash->setTextLoading( $bearsamppLang->getValue( Lang::STARTUP_CLEAN_TMP_TEXT ) );
278 $this->splash->incrProgressBar();
279
280 $this->writeLog( 'Clear tmp folders' );
281 Util::clearFolder( $bearsamppRoot->getTmpPath(), array('cachegrind', 'composer', 'openssl', 'mailhog', 'mailpit', 'xlight', 'npm-cache', 'pip', 'yarn', '.gitignore') );
282 Util::clearFolder( $bearsamppCore->getTmpPath(), array('.gitignore') );
283 }
284
285 /**
286 * Cleans old behaviors by removing outdated registry entries.
287 */
288 private function cleanOldBehaviors()
289 {
290 global $bearsamppLang, $bearsamppRegistry;
291
292 $this->writeLog( 'Clean old behaviors' );
293
294 $this->splash->setTextLoading( $bearsamppLang->getValue( Lang::STARTUP_CLEAN_OLD_BEHAVIORS_TEXT ) );
295 $this->splash->incrProgressBar();
296
297 // App >= 1.0.13
298 $bearsamppRegistry->deleteValue(
300 'SOFTWARE\Microsoft\Windows\CurrentVersion\Run',
302 );
303 }
304
305 /**
306 * Kills old instances of Bearsampp processes.
307 */
308 private function killOldInstances()
309 {
310 global $bearsamppLang;
311
312 $this->splash->setTextLoading( $bearsamppLang->getValue( Lang::STARTUP_KILL_OLD_PROCS_TEXT ) );
313 $this->splash->incrProgressBar();
314
315 // Stop services
316 /*foreach ($bearsamppBins->getServices() as $sName => $service) {
317 $serviceInfos = $service->infos();
318 if ($serviceInfos === false) {
319 continue;
320 }
321 $service->stop();
322 }*/
323
324 // Stop third party procs
325 $procsKilled = Win32Ps::killBins();
326 if ( !empty( $procsKilled ) ) {
327 $this->writeLog( 'Procs killed:' );
328 $procsKilledSort = array();
329 foreach ( $procsKilled as $proc ) {
331 $procsKilledSort[] = '-> ' . basename( $unixExePath ) . ' (PID ' . $proc[Win32Ps::PROCESS_ID] . ') in ' . $unixExePath;
332 }
333 sort( $procsKilledSort );
334 foreach ( $procsKilledSort as $proc ) {
335 $this->writeLog( $proc );
336 }
337 }
338 }
339
340 /**
341 * Refreshes the hostname in the configuration.
342 */
343 private function refreshHostname()
344 {
346
347 $this->splash->setTextLoading( $bearsamppLang->getValue( Lang::STARTUP_REFRESH_HOSTNAME_TEXT ) );
348 $this->splash->incrProgressBar();
349 $this->writeLog( 'Refresh hostname' );
350
351 $bearsamppConfig->replace( Config::CFG_HOSTNAME, gethostname() );
352 }
353
354 /**
355 * Checks and sets the launch startup configuration.
356 */
357 private function checkLaunchStartup()
358 {
359 global $bearsamppConfig;
360
361 $this->writeLog( 'Check launch startup' );
362
363 if ( $bearsamppConfig->isLaunchStartup() ) {
365 }
366 else {
368 }
369 }
370
371 /**
372 * Checks and sets the default browser configuration.
373 */
374 private function checkBrowser()
375 {
377
378 $this->splash->setTextLoading( $bearsamppLang->getValue( Lang::STARTUP_CHECK_BROWSER_TEXT ) );
379 $this->splash->incrProgressBar();
380 $this->writeLog( 'Check browser' );
381
382 $currentBrowser = $bearsamppConfig->getBrowser();
383 if ( empty( $currentBrowser ) || !file_exists( $currentBrowser ) ) {
385 }
386 }
387
388 /**
389 * Logs system information.
390 */
391 private function sysInfos()
392 {
393 global $bearsamppLang;
394
395 $this->splash->setTextLoading( $bearsamppLang->getValue( Lang::STARTUP_SYS_INFOS ) );
396 $this->splash->incrProgressBar();
397
398 $os = Batch::getOsInfo();
399 $this->writeLog( sprintf( 'OS: %s', $os ) );
400 }
401
402 /**
403 * Refreshes the aliases in the Apache configuration.
404 */
405 private function refreshAliases()
406 {
408
409 $this->splash->setTextLoading( $bearsamppLang->getValue( Lang::STARTUP_REFRESH_ALIAS_TEXT ) );
410 $this->splash->incrProgressBar();
411 $this->writeLog( 'Refresh aliases' );
412
413 $bearsamppBins->getApache()->refreshAlias( $bearsamppConfig->isOnline() );
414 }
415
416 /**
417 * Refreshes the virtual hosts in the Apache configuration.
418 */
419 private function refreshVhosts()
420 {
422
423 $this->splash->setTextLoading( $bearsamppLang->getValue( Lang::STARTUP_REFRESH_VHOSTS_TEXT ) );
424 $this->splash->incrProgressBar();
425 $this->writeLog( 'Refresh vhosts' );
426
427 $bearsamppBins->getApache()->refreshVhosts( $bearsamppConfig->isOnline() );
428 }
429
430 /**
431 * Checks the application path and logs the last path content.
432 */
433 private function checkPath()
434 {
436
437 $this->splash->setTextLoading( $bearsamppLang->getValue( Lang::STARTUP_CHECK_PATH_TEXT ) );
438 $this->splash->incrProgressBar();
439
440 $this->writeLog( 'Last path: ' . $bearsamppCore->getLastPathContent() );
441 }
442
443 /**
444 * Scans folders and logs the number of files to scan.
445 */
446 private function scanFolders()
447 {
448 global $bearsamppLang;
449
450 $this->splash->setTextLoading( $bearsamppLang->getValue( Lang::STARTUP_SCAN_FOLDERS_TEXT ) );
451 $this->splash->incrProgressBar();
452
453 $this->filesToScan = Util::getFilesToScan();
454 $this->writeLog( 'Files to scan: ' . count( $this->filesToScan ) );
455 }
456
457 /**
458 * Changes the application path and logs the number of files and occurrences changed.
459 */
460 private function changePath()
461 {
462 global $bearsamppLang;
463
464 $this->splash->setTextLoading( sprintf( $bearsamppLang->getValue( Lang::STARTUP_CHANGE_PATH_TEXT ), $this->rootPath ) );
465 $this->splash->incrProgressBar();
466
467 $result = Util::changePath( $this->filesToScan, $this->rootPath );
468 $this->writeLog( 'Nb files changed: ' . $result['countChangedFiles'] );
469 $this->writeLog( 'Nb occurences changed: ' . $result['countChangedOcc'] );
470 }
471
472 /**
473 * Saves the current application path.
474 */
475 private function savePath()
476 {
477 global $bearsamppCore;
478
479 file_put_contents( $bearsamppCore->getLastPath(), $this->rootPath );
480 $this->writeLog( 'Save current path: ' . $this->rootPath );
481 }
482
483 /**
484 * Checks and updates the application path registry key.
485 */
486 private function checkPathRegKey()
487 {
488 global $bearsamppRoot, $bearsamppLang, $bearsamppRegistry;
489
490 $this->splash->setTextLoading( sprintf( $bearsamppLang->getValue( Lang::STARTUP_REGISTRY_TEXT ), Registry::APP_PATH_REG_ENTRY ) );
491 $this->splash->incrProgressBar();
492
493 $currentAppPathRegKey = Util::getAppPathRegKey();
494 $genAppPathRegKey = Util::formatWindowsPath( $bearsamppRoot->getRootPath() );
495 $this->writeLog( 'Current app path reg key: ' . $currentAppPathRegKey );
496 $this->writeLog( 'Gen app path reg key: ' . $genAppPathRegKey );
497 if ( $currentAppPathRegKey != $genAppPathRegKey ) {
498 if ( !Util::setAppPathRegKey( $genAppPathRegKey ) ) {
499 if ( !empty( $this->error ) ) {
500 $this->error .= PHP_EOL . PHP_EOL;
501 }
503 $this->error .= PHP_EOL . $bearsamppRegistry->getLatestError();
504 }
505 else {
506 $this->writeLog( 'Need restart: checkPathRegKey' );
507 $this->restart = true;
508 }
509 }
510 }
511
512 /**
513 * Checks and updates the application bins registry key.
514 * If the current registry key does not match the generated key, it updates the registry key.
515 * Logs the current and generated registry keys.
516 * Sets an error message if the registry key update fails.
517 * Sets a restart flag if the registry key is updated.
518 */
519 private function checkBinsRegKey()
520 {
521 global $bearsamppLang, $bearsamppRegistry;
522
523 $this->splash->setTextLoading( sprintf( $bearsamppLang->getValue( Lang::STARTUP_REGISTRY_TEXT ), Registry::APP_BINS_REG_ENTRY ) );
524 $this->splash->incrProgressBar();
525
526 $currentAppBinsRegKey = Util::getAppBinsRegKey();
527 $genAppBinsRegKey = Util::getAppBinsRegKey( false );
528 $this->writeLog( 'Current app bins reg key: ' . $currentAppBinsRegKey );
529 $this->writeLog( 'Gen app bins reg key: ' . $genAppBinsRegKey );
530 if ( $currentAppBinsRegKey != $genAppBinsRegKey ) {
531 if ( !Util::setAppBinsRegKey( $genAppBinsRegKey ) ) {
532 if ( !empty( $this->error ) ) {
533 $this->error .= PHP_EOL . PHP_EOL;
534 }
536 $this->error .= PHP_EOL . $bearsamppRegistry->getLatestError();
537 }
538 else {
539 $this->writeLog( 'Need restart: checkBinsRegKey' );
540 $this->restart = true;
541 }
542 }
543 }
544
545 /**
546 * Checks and updates the system PATH registry key.
547 * Ensures the application bins registry entry is at the beginning of the system PATH.
548 * Logs the current and new system PATH.
549 * Sets an error message if the system PATH update fails.
550 * Sets a restart flag if the system PATH is updated.
551 */
552 private function checkSystemPathRegKey()
553 {
554 global $bearsamppLang, $bearsamppRegistry;
555
556 $this->splash->setTextLoading( sprintf( $bearsamppLang->getValue( Lang::STARTUP_REGISTRY_TEXT ), Registry::SYSPATH_REG_ENTRY ) );
557 $this->splash->incrProgressBar();
558
559 $currentSysPathRegKey = Util::getSysPathRegKey();
560 $this->writeLog( 'Current system PATH: ' . $currentSysPathRegKey );
561
562 $newSysPathRegKey = str_replace( '%' . Registry::APP_BINS_REG_ENTRY . '%;', '', $currentSysPathRegKey );
563 $newSysPathRegKey = str_replace( '%' . Registry::APP_BINS_REG_ENTRY . '%', '', $newSysPathRegKey );
564 $newSysPathRegKey = '%' . Registry::APP_BINS_REG_ENTRY . '%;' . $newSysPathRegKey;
565 $this->writeLog( 'New system PATH: ' . $newSysPathRegKey );
566
567 if ( $currentSysPathRegKey != $newSysPathRegKey ) {
568 if ( !Util::setSysPathRegKey( $newSysPathRegKey ) ) {
569 if ( !empty( $this->error ) ) {
570 $this->error .= PHP_EOL . PHP_EOL;
571 }
573 $this->error .= PHP_EOL . $bearsamppRegistry->getLatestError();
574 }
575 else {
576 $this->writeLog( 'Need restart: checkSystemPathRegKey' );
577 $this->restart = true;
578 }
579 }
580 else {
581 $this->writeLog( 'Refresh system PATH: ' . $currentSysPathRegKey );
582 Util::setSysPathRegKey( str_replace( '%' . Registry::APP_BINS_REG_ENTRY . '%', '', $currentSysPathRegKey ) );
583 Util::setSysPathRegKey( $currentSysPathRegKey );
584 }
585 }
586
587 /**
588 * Updates the configuration for bins, tools, and apps.
589 * Logs the update process.
590 */
591 private function updateConfig()
592 {
593 global $bearsamppLang, $bearsamppBins, $bearsamppTools, $bearsamppApps;
594
595 $this->splash->setTextLoading( $bearsamppLang->getValue( Lang::STARTUP_UPDATE_CONFIG_TEXT ) );
596 $this->splash->incrProgressBar();
597 $this->writeLog( 'Update config' );
598
599 $bearsamppBins->update();
600 $bearsamppTools->update();
601 $bearsamppApps->update();
602 }
603
604 /**
605 * Creates SSL certificates if they do not already exist.
606 * Logs the creation process.
607 */
608 private function createSslCrts()
609 {
610 global $bearsamppLang, $bearsamppOpenSsl;
611
612 $this->splash->incrProgressBar();
613 if ( !$bearsamppOpenSsl->existsCrt( 'localhost' ) ) {
614 $this->splash->setTextLoading( sprintf( $bearsamppLang->getValue( Lang::STARTUP_GEN_SSL_CRT_TEXT ), 'localhost' ) );
615 $bearsamppOpenSsl->createCrt( 'localhost' );
616 }
617 }
618
619 /**
620 * Installs and starts services for the application.
621 * Checks if services are already installed and updates them if necessary.
622 * Logs the installation process and any errors encountered.
623 */
624 private function installServices()
625 {
627
628 if ( !$this->restart ) {
629
630 foreach ( $bearsamppBins->getServices() as $sName => $service ) {
631 $serviceError = '';
632 $serviceRestart = false;
633 $serviceAlreadyInstalled = false;
634 $serviceToRemove = false;
635 $startServiceTime = Util::getMicrotime();
636
637 $syntaxCheckCmd = null;
638 $bin = null;
639 $port = 0;
640 if ( $sName == BinMailhog::SERVICE_NAME ) {
641 $bin = $bearsamppBins->getMailhog();
642 $port = $bearsamppBins->getMailhog()->getSmtpPort();
643 }
644 elseif ( $sName == BinMailpit::SERVICE_NAME ) {
645 $bin = $bearsamppBins->getMailpit();
646 $port = $bearsamppBins->getMailpit()->getSmtpPort();
647 }
648 elseif ( $sName == BinMemcached::SERVICE_NAME ) {
649 $bin = $bearsamppBins->getMemcached();
650 $port = $bearsamppBins->getMemcached()->getPort();
651 }
652 elseif ( $sName == BinApache::SERVICE_NAME ) {
653 $bin = $bearsamppBins->getApache();
654 $port = $bearsamppBins->getApache()->getPort();
655 $syntaxCheckCmd = BinApache::CMD_SYNTAX_CHECK;
656 }
657 elseif ( $sName == BinMysql::SERVICE_NAME ) {
658 $bin = $bearsamppBins->getMysql();
659 $port = $bearsamppBins->getMysql()->getPort();
660 $syntaxCheckCmd = BinMysql::CMD_SYNTAX_CHECK;
661 }
662 elseif ( $sName == BinMariadb::SERVICE_NAME ) {
663 $bin = $bearsamppBins->getMariadb();
664 $port = $bearsamppBins->getMariadb()->getPort();
665 $syntaxCheckCmd = BinMariadb::CMD_SYNTAX_CHECK;
666 }
667 elseif ( $sName == BinPostgresql::SERVICE_NAME ) {
668 $bin = $bearsamppBins->getPostgresql();
669 $port = $bearsamppBins->getPostgresql()->getPort();
670 }
671 elseif ( $sName == BinFilezilla::SERVICE_NAME ) {
672 $bin = $bearsamppBins->getFilezilla();
673 $port = $bearsamppBins->getFilezilla()->getPort();
674 }
675 elseif ( $sName == BinXlight::SERVICE_NAME ) {
676 $bin = $bearsamppBins->getXlight();
677 $port = $bearsamppBins->getXlight()->getPort();
678 }
679
680 $name = $bin->getName() . ' ' . $bin->getVersion() . ' (' . $service->getName() . ')';
681
682 $this->splash->incrProgressBar();
683 $this->splash->setTextLoading( sprintf( $bearsamppLang->getValue( Lang::STARTUP_CHECK_SERVICE_TEXT ), $name ) );
684 $serviceInfos = $service->infos();
685 if ( $serviceInfos !== false ) {
686 $serviceAlreadyInstalled = true;
687 $this->writeLog( $name . ' service already installed' );
688 foreach ( $serviceInfos as $key => $value ) {
689 $this->writeLog( '-> ' . $key . ': ' . $value );
690 }
691 $serviceGenPathName = trim( str_replace( '"', '', $service->getBinPath() . ($service->getParams() ? ' ' . $service->getParams() : '') ) );
692 $serviceVbsPathName = trim( str_replace( '"', '', $serviceInfos[Win32Service::VBS_PATH_NAME] ) );
693 if ( $serviceGenPathName != $serviceVbsPathName ) {
694 $serviceToRemove = true;
695 $this->writeLog( $name . ' service has to be removed' );
696 $this->writeLog( '-> serviceGenPathName: ' . $serviceGenPathName );
697 $this->writeLog( '-> serviceVbsPathName: ' . $serviceVbsPathName );
698 }
699 }
700
701 $this->splash->incrProgressBar();
702 if ( $serviceToRemove && !$service->delete() ) {
703 $serviceRestart = true;
704 }
705
706 if ( !$serviceRestart ) {
707 $isPortInUse = Util::isPortInUse( $port );
708 if ( $isPortInUse === false ) {
709 $this->splash->incrProgressBar();
710 if ( !$serviceAlreadyInstalled && !$serviceToRemove ) {
711 $this->splash->setTextLoading( sprintf( $bearsamppLang->getValue( Lang::STARTUP_INSTALL_SERVICE_TEXT ), $name ) );
712 if ( !$service->create() ) {
713 $serviceError .= sprintf( $bearsamppLang->getValue( Lang::STARTUP_SERVICE_CREATE_ERROR ), $service->getError() );
714 }
715 }
716
717 $this->splash->incrProgressBar();
718 $this->splash->setTextLoading( sprintf( $bearsamppLang->getValue( Lang::STARTUP_START_SERVICE_TEXT ), $name ) );
719 if ( !$service->start() ) {
720 if ( !empty( $serviceError ) ) {
721 $serviceError .= PHP_EOL;
722 }
723 $serviceError .= sprintf( $bearsamppLang->getValue( Lang::STARTUP_SERVICE_START_ERROR ), $service->getError() );
724 if ( !empty( $syntaxCheckCmd ) ) {
725 $cmdSyntaxCheck = $bin->getCmdLineOutput( $syntaxCheckCmd );
726 if ( !$cmdSyntaxCheck['syntaxOk'] ) {
727 $serviceError .= PHP_EOL . sprintf( $bearsamppLang->getValue( Lang::STARTUP_SERVICE_SYNTAX_ERROR ), $cmdSyntaxCheck['content'] );
728 }
729 }
730 }
731 $this->splash->incrProgressBar();
732 }
733 else {
734 if ( !empty( $serviceError ) ) {
735 $serviceError .= PHP_EOL;
736 }
737 $serviceError .= sprintf( $bearsamppLang->getValue( Lang::STARTUP_SERVICE_PORT_ERROR ), $port, $isPortInUse );
738 $this->splash->incrProgressBar( 3 );
739 }
740 }
741 else {
742 $this->writeLog( 'Need restart: installService ' . $bin->getName() );
743 $this->restart = true;
744 $this->splash->incrProgressBar( 3 );
745 }
746
747 if ( !empty( $serviceError ) ) {
748 if ( !empty( $this->error ) ) {
749 $this->error .= PHP_EOL . PHP_EOL;
750 }
751 $this->error .= sprintf( $bearsamppLang->getValue( Lang::STARTUP_SERVICE_ERROR ), $name ) . PHP_EOL . $serviceError;
752 }
753 else {
754 $this->writeLog( $name . ' service installed in ' . round( Util::getMicrotime() - $startServiceTime, 3 ) . 's' );
755 }
756 }
757 }
758 else {
759 $this->splash->incrProgressBar( self::GAUGE_SERVICES * count( $bearsamppBins->getServices() ) );
760 }
761 }
762
763 /**
764 * Refreshes Git repositories if the scan on startup is enabled.
765 * Logs the number of repositories found.
766 */
767 private function refreshGitRepos()
768 {
769 global $bearsamppLang, $bearsamppTools;
770
771 $this->splash->incrProgressBar();
772 if ( $bearsamppTools->getGit()->isScanStartup() ) {
773 $this->splash->setTextLoading( $bearsamppLang->getValue( Lang::STARTUP_REFRESH_GIT_REPOS_TEXT ) );
774
775 $repos = $bearsamppTools->getGit()->findRepos( false );
776 $this->writeLog( 'Update GIT repos: ' . count( $repos ) . ' found' );
777 }
778 }
779
780 /**
781 * Writes a log message to the startup log file.
782 *
783 * @param string $log The log message to write.
784 */
785 private function writeLog($log)
786 {
787 global $bearsamppRoot;
788 Util::logDebug( $log, $bearsamppRoot->getStartupLogFilePath() );
789 }
790}
$result
global $bearsamppBins
global $bearsamppLang
global $bearsamppRoot
$port
global $bearsamppCore
$proc
Definition ajax.php:43
processWindow($window, $id, $ctrl, $param1, $param2)
static getOsInfo()
const CMD_SYNTAX_CHECK
const CMD_SYNTAX_CHECK
const SERVICE_NAME
const CFG_HOSTNAME
const CFG_BROWSER
const STARTUP_INSTALL_SERVICE_TEXT
const STARTUP_CLEAN_OLD_BEHAVIORS_TEXT
const STARTUP_CHECK_SERVICE_TEXT
const STARTUP_SERVICE_CREATE_ERROR
const STARTUP_REGISTRY_ERROR_TEXT
const STARTUP_SERVICE_ERROR
const STARTUP_CLEAN_TMP_TEXT
const STARTUP_GEN_SSL_CRT_TEXT
const STARTUP_PREPARE_RESTART_TEXT
const STARTUP_CHECK_BROWSER_TEXT
const STARTUP_SERVICE_SYNTAX_ERROR
const STARTUP_CHECK_PATH_TEXT
const STARTUP_SERVICE_PORT_ERROR
const STARTUP_CHANGE_PATH_TEXT
const STARTUP_SYS_INFOS
const STARTUP_START_SERVICE_TEXT
const STARTUP
const STARTUP_REFRESH_HOSTNAME_TEXT
const STARTUP_KILL_OLD_PROCS_TEXT
const DISABLED
const STARTUP_UPDATE_CONFIG_TEXT
const STARTUP_REFRESH_GIT_REPOS_TEXT
const STARTUP_REFRESH_VHOSTS_TEXT
const STARTUP_REFRESH_ALIAS_TEXT
const STARTUP_REGISTRY_TEXT
const STARTUP_ROTATION_LOGS_TEXT
const STARTUP_SCAN_FOLDERS_TEXT
const STARTUP_SERVICE_START_ERROR
const STARTUP_ERROR_TITLE
const STARTUP_STARTING_TEXT
const APP_BINS_REG_ENTRY
const SYSPATH_REG_ENTRY
const APP_PATH_REG_ENTRY
const HKEY_LOCAL_MACHINE
static setSysPathRegKey($value)
static deleteFolder($path)
static getFilesToScan($path=null)
static clearFolders($paths, $exclude=array())
static getSysPathRegKey()
static formatUnixPath($path)
static changePath($filesToScan, $rootPath=null)
static startLoading()
static enableLaunchStartup()
static setAppPathRegKey($value)
static logDebug($data, $file=null)
static getAppPathRegKey()
static isPortInUse($port)
static setAppBinsRegKey($value)
static getMicrotime()
static disableLaunchStartup()
static clearFolder($path, $exclude=array())
static formatWindowsPath($path)
static getAppBinsRegKey($fromRegistry=true)
static getDefaultBrowser()
Definition class.vbs.php:80
const PROCESS_ID
static killBins($refreshProcs=false)
const EXECUTABLE_PATH
global $bearsamppConfig
Definition homepage.php:26
const APP_TITLE
Definition root.php:12