Bearsampp 2025.8.29
Loading...
Searching...
No Matches
class.bin.apache.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
17class BinApache extends Module
18{
19 const SERVICE_NAME = 'bearsamppapache';
20 const SERVICE_PARAMS = '-k runservice';
21
22 const ROOT_CFG_ENABLE = 'apacheEnable';
23 const ROOT_CFG_VERSION = 'apacheVersion';
24
25 const LOCAL_CFG_EXE = 'apacheExe';
26 const LOCAL_CFG_CONF = 'apacheConf';
27 const LOCAL_CFG_PORT = 'apachePort';
28 const LOCAL_CFG_SSL_PORT = 'apacheSslPort';
29 const LOCAL_CFG_OPENSSL_EXE = 'apacheOpensslExe';
30
31 const CMD_VERSION_NUMBER = '-v';
35 const CMD_VHOSTS_SETTINGS = '-S';
36 const CMD_LOADED_MODULES = '-M';
37 const CMD_SYNTAX_CHECK = '-t';
38
39 const TAG_START_SWITCHONLINE = '# START switchOnline tag - Do not replace!';
40 const TAG_END_SWITCHONLINE = '# END switchOnline tag - Do not replace!';
41
42 private $service;
43 private $modulesPath;
44 private $sslConf;
45 private $accessLog;
46 private $rewriteLog;
47 private $errorLog;
48
49 private $exe;
50 private $conf;
51 private $port;
52 private $sslPort;
53 private $opensslExe;
54
61 public function __construct($id, $type)
62 {
63 Util::logInitClass( $this );
64 $this->reload( $id, $type );
65 }
66
73 public function reload($id = null, $type = null)
74 {
76 Util::logReloadClass( $this );
77
78 $this->name = $bearsamppLang->getValue( Lang::APACHE );
79 $this->version = $bearsamppConfig->getRaw( self::ROOT_CFG_VERSION );
80 parent::reload( $id, $type );
81
82 $this->enable = $this->enable && $bearsamppConfig->getRaw( self::ROOT_CFG_ENABLE );
83 $this->service = new Win32Service( self::SERVICE_NAME );
84 $this->modulesPath = $this->symlinkPath . '/modules';
85 $this->sslConf = $this->symlinkPath . '/conf/extra/httpd-ssl.conf';
86 $this->accessLog = $bearsamppRoot->getLogsPath() . '/apache_access.log';
87 $this->rewriteLog = $bearsamppRoot->getLogsPath() . '/apache_rewrite.log';
88 $this->errorLog = $bearsamppRoot->getLogsPath() . '/apache_error.log';
89
90 if ( $this->bearsamppConfRaw !== false ) {
91 $this->exe = $this->symlinkPath . '/' . $this->bearsamppConfRaw[self::LOCAL_CFG_EXE];
92 $this->conf = $this->symlinkPath . '/' . $this->bearsamppConfRaw[self::LOCAL_CFG_CONF];
93 $this->port = $this->bearsamppConfRaw[self::LOCAL_CFG_PORT];
94 $this->sslPort = $this->bearsamppConfRaw[self::LOCAL_CFG_SSL_PORT];
95 $this->opensslExe = $this->symlinkPath . '/' . $this->bearsamppConfRaw[self::LOCAL_CFG_OPENSSL_EXE];
96 }
97
98 if ( !$this->enable ) {
99 Util::logInfo( $this->name . ' is not enabled!' );
100
101 return;
102 }
103 if ( !is_dir( $this->currentPath ) ) {
104 Util::logError( sprintf( $bearsamppLang->getValue( Lang::ERROR_FILE_NOT_FOUND ), $this->name . ' ' . $this->version, $this->currentPath ) );
105
106 return;
107 }
108 if ( !is_dir( $this->symlinkPath ) ) {
109 Util::logError( sprintf( $bearsamppLang->getValue( Lang::ERROR_FILE_NOT_FOUND ), $this->name . ' ' . $this->version, $this->symlinkPath ) );
110
111 return;
112 }
113 if ( !is_file( $this->bearsamppConf ) ) {
114 Util::logError( sprintf( $bearsamppLang->getValue( Lang::ERROR_CONF_NOT_FOUND ), $this->name . ' ' . $this->version, $this->bearsamppConf ) );
115
116 return;
117 }
118 if ( !is_file( $this->sslConf ) ) {
119 Util::logError( sprintf( $bearsamppLang->getValue( Lang::ERROR_CONF_NOT_FOUND ), $this->name . ' ' . $this->version, $this->sslConf ) );
120
121 return;
122 }
123 if ( !is_file( $this->exe ) ) {
124 Util::logError( sprintf( $bearsamppLang->getValue( Lang::ERROR_EXE_NOT_FOUND ), $this->name . ' ' . $this->version, $this->exe ) );
125
126 return;
127 }
128 if ( !is_file( $this->conf ) ) {
129 Util::logError( sprintf( $bearsamppLang->getValue( Lang::ERROR_CONF_NOT_FOUND ), $this->name . ' ' . $this->version, $this->conf ) );
130
131 return;
132 }
133 if ( !is_numeric( $this->port ) || $this->port <= 0 ) {
134 Util::logError( sprintf( $bearsamppLang->getValue( Lang::ERROR_INVALID_PARAMETER ), self::LOCAL_CFG_PORT, $this->port ) );
135
136 return;
137 }
138 if ( !is_numeric( $this->sslPort ) || $this->sslPort <= 0 ) {
139 Util::logError( sprintf( $bearsamppLang->getValue( Lang::ERROR_INVALID_PARAMETER ), self::LOCAL_CFG_SSL_PORT, $this->sslPort ) );
140
141 return;
142 }
143 if ( !is_file( $this->opensslExe ) ) {
144 Util::logError( sprintf( $bearsamppLang->getValue( Lang::ERROR_EXE_NOT_FOUND ), $this->name . ' ' . $this->version, $this->opensslExe ) );
145
146 return;
147 }
148
149 $nssm = new Nssm( self::SERVICE_NAME );
150 $nssm->setDisplayName( APP_TITLE . ' ' . $this->getName() );
151 $nssm->setBinPath( $this->exe );
152 $nssm->setStart( Nssm::SERVICE_DEMAND_START );
153
154 $this->service->setNssm( $nssm );
155 }
156
162 protected function replaceAll($params)
163 {
164 $content = file_get_contents( $this->bearsamppConf );
165
166 foreach ( $params as $key => $value ) {
167 $content = preg_replace( '|' . $key . ' = .*|', $key . ' = ' . '"' . $value . '"', $content );
168 $this->bearsamppConfRaw[$key] = $value;
169 switch ( $key ) {
170 case self::LOCAL_CFG_PORT:
171 $this->port = $value;
172 break;
173 case self::LOCAL_CFG_SSL_PORT:
174 $this->sslPort = $value;
175 break;
176 }
177 }
178
179 file_put_contents( $this->bearsamppConf, $content );
180 }
181
191 public function changePort($port, $checkUsed = false, $wbProgressBar = null)
192 {
193 global $bearsamppWinbinder;
194
195 if ( !Util::isValidPort( $port ) ) {
196 Util::logError( $this->getName() . ' port not valid: ' . $port );
197
198 return false;
199 }
200
201 $port = intval( $port );
202 $bearsamppWinbinder->incrProgressBar( $wbProgressBar );
203
204 $isPortInUse = Util::isPortInUse( $port );
205 if ( !$checkUsed || $isPortInUse === false ) {
206 // bearsampp.conf
207 $this->setPort( $port );
208 $bearsamppWinbinder->incrProgressBar( $wbProgressBar );
209
210 // conf
211 $this->update();
212 $bearsamppWinbinder->incrProgressBar( $wbProgressBar );
213
214 return true;
215 }
216
217 Util::logDebug( $this->getName() . ' port in used: ' . $port . ' - ' . $isPortInUse );
218
219 return $isPortInUse;
220 }
221
231 public function checkPort($port, $ssl = false, $showWindow = false)
232 {
233 global $bearsamppLang, $bearsamppWinbinder, $bearsamppHomepage;
234 $boxTitle = sprintf( $bearsamppLang->getValue( Lang::CHECK_PORT_TITLE ), $this->getName(), $port );
235
236 if ( !Util::isValidPort( $port ) ) {
237 Util::logError( $this->getName() . ' port not valid: ' . $port );
238
239 return false;
240 }
241
242 $headers = Util::getHttpHeaders( 'http' . ($ssl ? 's' : '') . '://localhost:' . $port . '/' . $bearsamppHomepage->getResourcesPath() . '/ping.php' );
243 if ( !empty( $headers ) ) {
244 foreach ( $headers as $row ) {
245 if ( Util::startWith( $row, 'Server: ' ) || Util::startWith( $row, 'server: ' ) ) {
246 Util::logDebug( $this->getName() . ' port ' . $port . ' is used by: ' . $this->getName() . ' ' . str_replace( 'Server: ', '', str_replace( 'server: ', '', trim( $row ) ) ) );
247 if ( $showWindow ) {
248 $bearsamppWinbinder->messageBoxInfo(
249 sprintf( $bearsamppLang->getValue( Lang::PORT_USED_BY ), $port, str_replace( 'Server: ', '', str_replace( 'server: ', '', trim( $row ) ) ) ),
250 $boxTitle
251 );
252 }
253
254 return true;
255 }
256 }
257 Util::logDebug( $this->getName() . ' port ' . $port . ' is used by another application' );
258 if ( $showWindow ) {
259 $bearsamppWinbinder->messageBoxWarning(
260 sprintf( $bearsamppLang->getValue( Lang::PORT_NOT_USED_BY ), $port ),
261 $boxTitle
262 );
263 }
264 }
265 else {
266 Util::logDebug( $this->getName() . ' port ' . $port . ' is not used' );
267 if ( $showWindow ) {
268 $bearsamppWinbinder->messageBoxError(
269 sprintf( $bearsamppLang->getValue( Lang::PORT_NOT_USED ), $port ),
270 $boxTitle
271 );
272 }
273 }
274
275 return false;
276 }
277
286 public function switchVersion($version, $showWindow = false)
287 {
288 Util::logDebug( 'Switch ' . $this->name . ' version to ' . $version );
289
290 return $this->updateConfig( $version, 0, $showWindow );
291 }
292
302 protected function updateConfig($version = null, $sub = 0, $showWindow = false)
303 {
304 global $bearsamppRoot, $bearsamppLang, $bearsamppBins, $bearsamppWinbinder;
305
306 if ( !$this->enable ) {
307 return true;
308 }
309
310 $version = $version == null ? $this->version : $version;
311 Util::logDebug( ($sub > 0 ? str_repeat( ' ', 2 * $sub ) : '') . 'Update ' . $this->name . ' ' . $version . ' config' );
312
313 $boxTitle = sprintf( $bearsamppLang->getValue( Lang::SWITCH_VERSION_TITLE ), $this->getName(), $version );
314
315 $conf = str_replace( 'apache' . $this->getVersion(), 'apache' . $version, $this->getConf() );
316 $bearsamppConf = str_replace( 'apache' . $this->getVersion(), 'apache' . $version, $this->bearsamppConf );
317
318 $tsDll = $bearsamppBins->getPhp()->getTsDll();
319
320 $apachePhpModuleName = null;
321 if ( $tsDll !== false ) {
322 $apachemoduleNamePrefix = substr( $tsDll, 0, 4 );
323 $apachePhpModuleName = ($apachemoduleNamePrefix == 'php8' ? 'php' : $apachemoduleNamePrefix) . '_module';
324 }
325 $apachePhpModulePath = $bearsamppBins->getPhp()->getApacheModule( $version );
326 $apachePhpModuleDll = basename( $apachePhpModulePath );
327
328 Util::logDebug( ($sub > 0 ? str_repeat( ' ', 2 * $sub ) : '') . 'PHP TsDll found: ' . $tsDll );
329 Util::logDebug( ($sub > 0 ? str_repeat( ' ', 2 * $sub ) : '') . 'PHP Apache module found: ' . $apachePhpModulePath );
330
331 if ( !file_exists( $conf ) || !file_exists( $bearsamppConf ) ) {
332 Util::logError( 'bearsampp config files not found for ' . $this->getName() . ' ' . $version );
333 if ( $showWindow ) {
334 $bearsamppWinbinder->messageBoxError(
335 sprintf( $bearsamppLang->getValue( Lang::BEARSAMPP_CONF_NOT_FOUND_ERROR ), $this->getName() . ' ' . $version ),
336 $boxTitle
337 );
338 }
339
340 return false;
341 }
342
343 $bearsamppConfRaw = parse_ini_file( $bearsamppConf );
344 if ( $bearsamppConfRaw === false || !isset( $bearsamppConfRaw[self::ROOT_CFG_VERSION] ) || $bearsamppConfRaw[self::ROOT_CFG_VERSION] != $version ) {
345 Util::logError( 'bearsampp config file malformed for ' . $this->getName() . ' ' . $version );
346 if ( $showWindow ) {
347 $bearsamppWinbinder->messageBoxError(
348 sprintf( $bearsamppLang->getValue( Lang::BEARSAMPP_CONF_MALFORMED_ERROR ), $this->getName() . ' ' . $version ),
349 $boxTitle
350 );
351 }
352
353 return false;
354 }
355
356 if ( $tsDll === false || $apachePhpModulePath === false ) {
357 Util::logDebug( $this->getName() . ' ' . $version . ' does not seem to be compatible with PHP ' . $bearsamppBins->getPhp()->getVersion() );
358 if ( $showWindow ) {
359 $bearsamppWinbinder->messageBoxError(
360 sprintf( $bearsamppLang->getValue( Lang::APACHE_INCPT ), $version, $bearsamppBins->getPhp()->getVersion() ),
361 $boxTitle
362 );
363 }
364
365 return false;
366 }
367
368 // httpd.conf
369 $this->setVersion( $version );
370
371 // conf
372 Util::logDebug( 'httpd.conf = ' . $conf );
374 // PHP module
375 '/^#?PHPIniDir\s.*/' => ($bearsamppBins->getPhp()->isEnable() ? '' : '#') . 'PHPIniDir "' . $bearsamppBins->getPhp()->getSymlinkPath() . '"',
376 '/^#?LoadFile\s.*php.ts\.dll.*/' => ($bearsamppBins->getPhp()->isEnable() ? '' : '#') . (!file_exists( $bearsamppBins->getPhp()->getSymlinkPath() . '/' . $tsDll ) ? '#' : '') . 'LoadFile "' . $bearsamppBins->getPhp()->getSymlinkPath() . '/' . $tsDll . '"',
377 '/^#?LoadModule\sphp.*/' => ($bearsamppBins->getPhp()->isEnable() ? '' : '#') . 'LoadModule ' . $apachePhpModuleName . ' "' . $bearsamppBins->getPhp()->getSymlinkPath() . '/' . $apachePhpModuleDll . '"',
378 '/^#?LoadModule\sphp_*/' => ($bearsamppBins->getPhp()->isEnable() ? '' : '#') . 'LoadModule ' . $apachePhpModuleName . ' "' . $bearsamppBins->getPhp()->getSymlinkPath() . '/' . $apachePhpModuleDll . '"',
379
380
381 // Port
382 '/^Listen\s(\d+)/' => 'Listen ' . $this->port,
383 '/^ServerName\s+([a-zA-Z0-9.]+):(\d+)/' => 'ServerName {{1}}:' . $this->port,
384 '/^NameVirtualHost\s+([a-zA-Z0-9.*]+):(\d+)/' => 'NameVirtualHost {{1}}:' . $this->port,
385 '/^<VirtualHost\s+([a-zA-Z0-9.*]+):(\d+)>/' => '<VirtualHost {{1}}:' . $this->port . '>'
386 ) );
387
388 // vhosts
389 foreach ( $this->getVhosts() as $vhost ) {
390 Util::replaceInFile( $bearsamppRoot->getVhostsPath() . '/' . $vhost . '.conf', array(
391 '/^<VirtualHost\s+([a-zA-Z0-9.*]+):(\d+)>$/' => '<VirtualHost {{1}}:' . $this->port . '>$'
392 ) );
393 }
394
395 // www .htaccess
396 Util::replaceInFile( $bearsamppRoot->getWwwPath() . '/.htaccess', array(
397 '/(.*)http:\/\/localhost(.*)/' => '{{1}}http://localhost' . ($this->port != 80 ? ':' . $this->port : '') . '/$1 [QSA,R=301,L]',
398 ) );
399
400 return true;
401 }
402
408 public function getModules()
409 {
410 $fromFolder = $this->getModulesFromFolder();
411 $fromConf = $this->getModulesFromConf();
412 $result = array_merge( $fromFolder, $fromConf );
413 ksort( $result );
414
415 return $result;
416 }
417
423 public function getModulesFromConf()
424 {
425 $result = array();
426
427 if ( !$this->enable ) {
428 return $result;
429 }
430
431 $confContent = file( $this->getConf() );
432 foreach ( $confContent as $row ) {
433 $modMatch = array();
434 if ( preg_match( '/^(#)?LoadModule\s*([a-z0-9_-]+)\s*"?(.*)"?/i', $row, $modMatch ) ) {
435 $name = $modMatch[2];
436 //$path = $modMatch[3];
437 if ( !Util::startWith( $name, 'php' ) ) {
438 if ( $modMatch[1] == '#' ) {
440 }
441 else {
443 }
444 }
445 }
446 }
447
448 ksort( $result );
449
450 return $result;
451 }
452
458 public function getModulesLoaded()
459 {
460 $result = array();
461 foreach ( $this->getModulesFromConf() as $name => $status ) {
462 if ( $status == ActionSwitchApacheModule::SWITCH_ON ) {
463 $result[] = $name;
464 }
465 }
466
467 return $result;
468 }
469
475 private function getModulesFromFolder()
476 {
477 $result = array();
478
479 if ( !$this->enable ) {
480 return $result;
481 }
482
483 $handle = @opendir( $this->getModulesPath() );
484 if ( !$handle ) {
485 return $result;
486 }
487
488 while ( false !== ($file = readdir( $handle )) ) {
489 if ( $file != '.' && $file != '..' && Util::startWith( $file, 'mod_' ) && (Util::endWith( $file, '.so' ) || Util::endWith( $file, '.dll' )) ) {
490 $name = str_replace( array('mod_', '.so', '.dll'), '', $file ) . '_module';
492 }
493 }
494
495 closedir( $handle );
496 ksort( $result );
497
498 return $result;
499 }
500
506 public function getAlias()
507 {
508 global $bearsamppRoot;
509 $result = array();
510
511 $handle = @opendir( $bearsamppRoot->getAliasPath() );
512 if ( !$handle ) {
513 return $result;
514 }
515
516 while ( false !== ($file = readdir( $handle )) ) {
517 if ( $file != '.' && $file != '..' && Util::endWith( $file, '.conf' ) ) {
518 $result[] = str_replace( '.conf', '', $file );
519 }
520 }
521
522 closedir( $handle );
523 ksort( $result );
524
525 return $result;
526 }
527
533 public function getVhosts()
534 {
535 global $bearsamppRoot;
536 $result = array();
537
538 $handle = @opendir( $bearsamppRoot->getVhostsPath() );
539 if ( !$handle ) {
540 return $result;
541 }
542
543 while ( false !== ($file = readdir( $handle )) ) {
544 if ( $file != '.' && $file != '..' && Util::endWith( $file, '.conf' ) ) {
545 $result[] = str_replace( '.conf', '', $file );
546 }
547 }
548
549 closedir( $handle );
550 ksort( $result );
551
552 return $result;
553 }
554
560 public function getVhostsUrl()
561 {
562 global $bearsamppRoot;
563 $result = array();
564
565 foreach ( $this->getVhosts() as $vhost ) {
566 $vhostContent = file( $bearsamppRoot->getVhostsPath() . '/' . $vhost . '.conf' );
567 foreach ( $vhostContent as $vhostLine ) {
568 $vhostLine = trim( $vhostLine );
569 $enabled = !Util::startWith( $vhostLine, '#' );
570 if ( preg_match_all( '/ServerName\s+(.*)/', $vhostLine, $matches ) ) {
571 foreach ( $matches as $match ) {
572 $found = isset( $match[1] ) ? trim( $match[1] ) : trim( $match[0] );
573 if ( filter_var( 'http://' . $found, FILTER_VALIDATE_URL ) !== false ) {
574 $result[$found] = $enabled;
575 break 2;
576 }
577 }
578 }
579 }
580 }
581
582 return $result;
583 }
584
590 public function getWwwDirectories()
591 {
592 global $bearsamppRoot;
593 $result = array();
594
595 $handle = @opendir( $bearsamppRoot->getWwwPath() );
596 if ( !$handle ) {
597 return $result;
598 }
599
600 while ( false !== ($file = readdir( $handle )) ) {
601 if ( $file != '.' && $file != '..' && is_dir( $bearsamppRoot->getWwwPath() . '/' . $file ) ) {
602 $result[] = $file;
603 }
604 }
605
606 closedir( $handle );
607 ksort( $result );
608
609 return $result;
610 }
611
619 public function getCmdLineOutput($cmd)
620 {
621 $result = array(
622 'syntaxOk' => false,
623 'content' => null,
624 );
625
626 if ( file_exists( $this->getExe() ) ) {
627 $tmpResult = Batch::exec( 'apacheGetCmdLineOutput', '"' . $this->getExe() . '" ' . $cmd );
628 if ( $tmpResult !== false && is_array( $tmpResult ) ) {
629 $result['syntaxOk'] = trim( $tmpResult[count( $tmpResult ) - 1] ) == 'Syntax OK';
630 if ( $result['syntaxOk'] ) {
631 unset( $tmpResult[count( $tmpResult ) - 1] );
632 }
633 $result['content'] = implode( PHP_EOL, $tmpResult );
634 }
635 }
636
637 return $result;
638 }
639
647 private function getOnlineContent($version = null)
648 {
649 $version = $version != null ? $version : $this->getVersion();
650 $result = self::TAG_START_SWITCHONLINE . PHP_EOL;
651
652 if ( Util::startWith( $version, '2.4' ) ) {
653 $result .= 'Require all granted' . PHP_EOL;
654 }
655 else {
656 $result .= 'Order Allow,Deny' . PHP_EOL .
657 'Allow from all' . PHP_EOL;
658 }
659
660 return $result . self::TAG_END_SWITCHONLINE;
661 }
662
670 private function getOfflineContent($version = null)
671 {
672 $version = $version != null ? $version : $this->getVersion();
673 $result = self::TAG_START_SWITCHONLINE . PHP_EOL;
674
675 if ( Util::startWith( $version, '2.4' ) ) {
676 $result .= 'Require local' . PHP_EOL;
677 }
678 else {
679 $result .= 'Order Deny,Allow' . PHP_EOL .
680 'Deny from all' . PHP_EOL .
681 'Allow from 127.0.0.1 ::1' . PHP_EOL;
682 }
683
684 return $result . self::TAG_END_SWITCHONLINE;
685 }
686
694 private function getRequiredContent($version = null)
695 {
696 global $bearsamppConfig;
697
698 return $bearsamppConfig->isOnline() ? $this->getOnlineContent( $version ) : $this->getOfflineContent( $version );
699 }
700
709 public function getAliasContent($name, $dest)
710 {
711 $dest = Util::formatUnixPath( $dest );
712
713 return 'Alias /' . $name . ' "' . $dest . '"' . PHP_EOL . PHP_EOL .
714 '<Directory "' . $dest . '">' . PHP_EOL .
715 ' Options Indexes FollowSymLinks MultiViews' . PHP_EOL .
716 ' AllowOverride all' . PHP_EOL .
717 $this->getRequiredContent() . PHP_EOL .
718 '</Directory>' . PHP_EOL;
719 }
720
729 public function getVhostContent($serverName, $documentRoot)
730 {
731 global $bearsamppRoot;
732
733 $documentRoot = Util::formatUnixPath( $documentRoot );
734
735 return '<VirtualHost *:' . $this->getPort() . '>' . PHP_EOL .
736 ' ServerAdmin webmaster@' . $serverName . PHP_EOL .
737 ' DocumentRoot "' . $documentRoot . '"' . PHP_EOL .
738 ' ServerName ' . $serverName . PHP_EOL .
739 ' ErrorLog "' . $bearsamppRoot->getLogsPath() . '/' . $serverName . '_error.log"' . PHP_EOL .
740 ' CustomLog "' . $bearsamppRoot->getLogsPath() . '/' . $serverName . '_access.log" combined' . PHP_EOL . PHP_EOL .
741 ' <Directory "' . $documentRoot . '">' . PHP_EOL .
742 ' Options Indexes FollowSymLinks MultiViews' . PHP_EOL .
743 ' AllowOverride all' . PHP_EOL .
744 $this->getRequiredContent() . PHP_EOL .
745 ' </Directory>' . PHP_EOL .
746 '</VirtualHost>' . PHP_EOL . PHP_EOL .
747 '<IfModule ssl_module>' . PHP_EOL .
748 '<VirtualHost *:' . $this->getSslPort() . '> #SSL' . PHP_EOL .
749 ' DocumentRoot "' . $documentRoot . '"' . PHP_EOL .
750 ' ServerName ' . $serverName . PHP_EOL .
751 ' ServerAdmin webmaster@' . $serverName . PHP_EOL .
752 ' ErrorLog "' . $bearsamppRoot->getLogsPath() . '/' . $serverName . '_error.log"' . PHP_EOL .
753 ' TransferLog "' . $bearsamppRoot->getLogsPath() . '/' . $serverName . '_access.log"' . PHP_EOL . PHP_EOL .
754 ' SSLEngine on' . PHP_EOL .
755 ' SSLProtocol all -SSLv2' . PHP_EOL .
756 ' SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5' . PHP_EOL .
757 ' SSLCertificateFile "' . $bearsamppRoot->getSslPath() . '/' . $serverName . '.crt"' . PHP_EOL .
758 ' SSLCertificateKeyFile "' . $bearsamppRoot->getSslPath() . '/' . $serverName . '.pub"' . PHP_EOL .
759 ' BrowserMatch "MSIE [2-5]" nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0' . PHP_EOL .
760 ' CustomLog "' . $bearsamppRoot->getLogsPath() . '/' . $serverName . '_sslreq.log" "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"' . PHP_EOL . PHP_EOL .
761 ' <Directory "' . $documentRoot . '">' . PHP_EOL .
762 ' SSLOptions +StdEnvVars' . PHP_EOL .
763 ' Options Indexes FollowSymLinks MultiViews' . PHP_EOL .
764 ' AllowOverride all' . PHP_EOL .
765 $this->getRequiredContent() . PHP_EOL .
766 ' </Directory>' . PHP_EOL .
767 '</VirtualHost>' . PHP_EOL .
768 '</IfModule>' . PHP_EOL;
769 }
770
776 public function refreshConf($putOnline)
777 {
778 if ( !$this->enable ) {
779 return;
780 }
781
782 $onlineContent = $this->getOnlineContent();
783 $offlineContent = $this->getOfflineContent();
784
785 $conf = file_get_contents( $this->getConf() );
786 Util::logTrace( 'refreshConf ' . $this->getConf() );
787 preg_match( '/' . self::TAG_START_SWITCHONLINE . '(.*?)' . self::TAG_END_SWITCHONLINE . '/s', $conf, $matches );
788 Util::logTrace( isset( $matches[1] ) ? print_r( $matches[1], true ) : 'N/A' );
789
790 if ( $putOnline ) {
791 $conf = preg_replace( '/' . self::TAG_START_SWITCHONLINE . '(.*?)' . self::TAG_END_SWITCHONLINE . '/s', $onlineContent, $conf, -1, $count );
792 }
793 else {
794 $conf = preg_replace( '/' . self::TAG_START_SWITCHONLINE . '(.*?)' . self::TAG_END_SWITCHONLINE . '/s', $offlineContent, $conf, -1, $count );
795 }
796 file_put_contents( $this->getConf(), $conf );
797 Util::logDebug( 'Refresh ' . $this->getConf() . ': ' . $count . ' occurrence(s) replaced' );
798
799 $sslConf = file_get_contents( $this->getSslConf() );
800 Util::logTrace( 'refreshConf ' . $this->getSslConf() );
801 preg_match( '/' . self::TAG_START_SWITCHONLINE . '(.*?)' . self::TAG_END_SWITCHONLINE . '/s', $sslConf, $matches );
802 Util::logTrace( isset( $matches[1] ) ? print_r( $matches[1], true ) : 'N/A' );
803
804 if ( $putOnline ) {
805 $sslConf = preg_replace( '/' . self::TAG_START_SWITCHONLINE . '(.*?)' . self::TAG_END_SWITCHONLINE . '/s', $onlineContent, $sslConf, -1, $count );
806 }
807 else {
808 $sslConf = preg_replace( '/' . self::TAG_START_SWITCHONLINE . '(.*?)' . self::TAG_END_SWITCHONLINE . '/s', $offlineContent, $sslConf, -1, $count );
809 }
810 file_put_contents( $this->getSslConf(), $sslConf );
811 Util::logDebug( 'Refresh ' . $this->getSslConf() . ': ' . $count . ' occurrence(s) replaced' );
812 }
813
819 public function refreshAlias($putOnline)
820 {
822
823 if ( !$this->enable ) {
824 return;
825 }
826
827 $onlineContent = $this->getOnlineContent();
828 $offlineContent = $this->getOfflineContent();
829
830 foreach ( $this->getAlias() as $alias ) {
831 $aliasConf = file_get_contents( $bearsamppRoot->getAliasPath() . '/' . $alias . '.conf' );
832 Util::logTrace( 'refreshAlias ' . $bearsamppRoot->getAliasPath() . '/' . $alias . '.conf' );
833 preg_match( '/' . self::TAG_START_SWITCHONLINE . '(.*?)' . self::TAG_END_SWITCHONLINE . '/s', $aliasConf, $matches );
834 Util::logTrace( isset( $matches[1] ) ? print_r( $matches[1], true ) : 'N/A' );
835
836 if ( $putOnline ) {
837 $aliasConf = preg_replace( '/' . self::TAG_START_SWITCHONLINE . '(.*?)' . self::TAG_END_SWITCHONLINE . '/s', $onlineContent, $aliasConf, -1, $count );
838 }
839 else {
840 $aliasConf = preg_replace( '/' . self::TAG_START_SWITCHONLINE . '(.*?)' . self::TAG_END_SWITCHONLINE . '/s', $offlineContent, $aliasConf, -1, $count );
841 }
842 file_put_contents( $bearsamppRoot->getAliasPath() . '/' . $alias . '.conf', $aliasConf );
843 Util::logDebug( 'Refresh ' . $bearsamppRoot->getAliasPath() . '/' . $alias . '.conf: ' . $count . ' occurrence(s) replaced' );
844 }
845
846 // Homepage
847 $bearsamppHomepage->refreshAliasContent();
848 }
849
855 public function refreshVhosts($putOnline)
856 {
857 global $bearsamppRoot;
858
859 if ( !$this->enable ) {
860 return;
861 }
862
863 $onlineContent = $this->getOnlineContent();
864 $offlineContent = $this->getOfflineContent();
865
866 foreach ( $this->getVhosts() as $vhost ) {
867 $vhostConf = file_get_contents( $bearsamppRoot->getVhostsPath() . '/' . $vhost . '.conf' );
868 Util::logTrace( 'refreshVhost ' . $bearsamppRoot->getVhostsPath() . '/' . $vhost . '.conf' );
869 preg_match( '/' . self::TAG_START_SWITCHONLINE . '(.*?)' . self::TAG_END_SWITCHONLINE . '/s', $vhostConf, $matches );
870 Util::logTrace( isset( $matches[1] ) ? print_r( $matches[1], true ) : 'N/A' );
871
872 if ( $putOnline ) {
873 $vhostConf = preg_replace( '/' . self::TAG_START_SWITCHONLINE . '(.*?)' . self::TAG_END_SWITCHONLINE . '/s', $onlineContent, $vhostConf, -1, $count );
874 }
875 else {
876 $vhostConf = preg_replace( '/' . self::TAG_START_SWITCHONLINE . '(.*?)' . self::TAG_END_SWITCHONLINE . '/s', $offlineContent, $vhostConf, -1, $count );
877 }
878 file_put_contents( $bearsamppRoot->getVhostsPath() . '/' . $vhost . '.conf', $vhostConf );
879 Util::logDebug( 'Refresh ' . $bearsamppRoot->getVhostsPath() . '/' . $vhost . '.conf: ' . $count . ' occurrence(s) replaced' );
880 }
881 }
882
893 public function setEnable($enabled, $showWindow = false)
894 {
895 global $bearsamppConfig, $bearsamppLang, $bearsamppWinbinder;
896
897 if ( $enabled == Config::ENABLED && !is_dir( $this->currentPath ) ) {
898 Util::logDebug( $this->getName() . ' cannot be enabled because bundle ' . $this->getVersion() . ' does not exist in ' . $this->currentPath );
899 if ( $showWindow ) {
900 $bearsamppWinbinder->messageBoxError(
901 sprintf( $bearsamppLang->getValue( Lang::ENABLE_BUNDLE_NOT_EXIST ), $this->getName(), $this->getVersion(), $this->currentPath ),
902 sprintf( $bearsamppLang->getValue( Lang::ENABLE_TITLE ), $this->getName() )
903 );
904 }
905 $enabled = Config::DISABLED;
906 }
907
908 Util::logInfo( $this->getName() . ' switched to ' . ($enabled == Config::ENABLED ? 'enabled' : 'disabled') );
909 $this->enable = $enabled == Config::ENABLED;
910 $bearsamppConfig->replace( self::ROOT_CFG_ENABLE, $enabled );
911
912 $this->reload();
913 if ( $this->enable ) {
914 Util::installService( $this, $this->port, self::CMD_SYNTAX_CHECK, $showWindow );
915 }
916 else {
917 Util::removeService( $this->service, $this->name );
918 }
919 }
920
928 public function setVersion($version)
929 {
930 global $bearsamppConfig;
931 $this->version = $version;
932 $bearsamppConfig->replace( self::ROOT_CFG_VERSION, $version );
933 $this->reload();
934 }
935
941 public function getService()
942 {
943 return $this->service;
944 }
945
951 public function getModulesPath()
952 {
953 return $this->modulesPath;
954 }
955
961 public function getSslConf()
962 {
963 return $this->sslConf;
964 }
965
971 public function getAccessLog()
972 {
973 return $this->accessLog;
974 }
975
981 public function getRewriteLog()
982 {
983 return $this->rewriteLog;
984 }
985
991 public function getErrorLog()
992 {
993 return $this->errorLog;
994 }
995
1001 public function getExe()
1002 {
1003 return $this->exe;
1004 }
1005
1011 public function getConf()
1012 {
1013 return $this->conf;
1014 }
1015
1021 public function getPort()
1022 {
1023 return $this->port;
1024 }
1025
1033 public function setPort($port)
1034 {
1035 $this->replace( self::LOCAL_CFG_PORT, $port );
1036 }
1037
1043 public function getSslPort()
1044 {
1045 return $this->sslPort;
1046 }
1047
1055 public function setSslPort($sslPort)
1056 {
1057 $this->replace( self::LOCAL_CFG_SSL_PORT, $sslPort );
1058 }
1059
1065 public function getOpensslExe()
1066 {
1067 return $this->opensslExe;
1068 }
1069}
$result
global $bearsamppBins
global $bearsamppLang
global $bearsamppRoot
static exec($basename, $content, $timeout=true, $catchOutput=true, $standalone=false, $silent=true, $rebuild=true)
const TAG_START_SWITCHONLINE
const CMD_SYNTAX_CHECK
const CMD_CONFIG_DIRECTIVES
replaceAll($params)
getRequiredContent($version=null)
getOnlineContent($version=null)
updateConfig($version=null, $sub=0, $showWindow=false)
const LOCAL_CFG_SSL_PORT
getAliasContent($name, $dest)
const CMD_COMPILE_SETTINGS
switchVersion($version, $showWindow=false)
setSslPort($sslPort)
const ROOT_CFG_ENABLE
const CMD_COMPILED_MODULES
setVersion($version)
changePort($port, $checkUsed=false, $wbProgressBar=null)
setEnable($enabled, $showWindow=false)
refreshConf($putOnline)
const CMD_LOADED_MODULES
checkPort($port, $ssl=false, $showWindow=false)
const TAG_END_SWITCHONLINE
getVhostContent($serverName, $documentRoot)
const CMD_VERSION_NUMBER
const LOCAL_CFG_OPENSSL_EXE
reload($id=null, $type=null)
getOfflineContent($version=null)
const CMD_VHOSTS_SETTINGS
refreshAlias($putOnline)
const ROOT_CFG_VERSION
__construct($id, $type)
refreshVhosts($putOnline)
const DISABLED
const ENABLED
const ENABLE_BUNDLE_NOT_EXIST
const BEARSAMPP_CONF_MALFORMED_ERROR
const APACHE_INCPT
const ERROR_EXE_NOT_FOUND
const ERROR_CONF_NOT_FOUND
const APACHE
const BEARSAMPP_CONF_NOT_FOUND_ERROR
const PORT_NOT_USED
const ERROR_INVALID_PARAMETER
const ENABLE_TITLE
const PORT_NOT_USED_BY
const SWITCH_VERSION_TITLE
const CHECK_PORT_TITLE
const PORT_USED_BY
const ERROR_FILE_NOT_FOUND
update($sub=0, $showWindow=false)
replace($key, $value)
const SERVICE_DEMAND_START
static logError($data, $file=null)
static installService($bin, $port, $syntaxCheckCmd, $showWindow=false)
static logInitClass($classInstance)
static removeService($service, $name)
static logTrace($data, $file=null)
static isValidPort($port)
static logInfo($data, $file=null)
static getHttpHeaders($pingUrl)
static logDebug($data, $file=null)
static logReloadClass($classInstance)
static startWith($string, $search)
static endWith($string, $search)
static formatUnixPath($path)
static isPortInUse($port)
static replaceInFile($path, $replaceList)
global $bearsamppConfig
Definition homepage.php:27
global $bearsamppHomepage
Definition homepage.php:27
const APP_TITLE
Definition root.php:13