2024.8.23
Loading...
Searching...
No Matches
class.tpl.app.apache.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 TplAppApache
12 *
13 * This class provides methods to generate menu items and actions for managing Apache services
14 * within the Bearsampp application. It includes functionalities for enabling/disabling Apache,
15 * switching versions, changing ports, managing modules, aliases, and virtual hosts.
16 */
18{
19 // Constants for menu and action identifiers
20 const MENU = 'apache';
21 const MENU_VERSIONS = 'apacheVersions';
22 const MENU_SERVICE = 'apacheService';
23 const MENU_DEBUG = 'apacheDebug';
24 const MENU_MODULES = 'apacheModules';
25 const MENU_ALIAS = 'apacheAlias';
26 const MENU_VHOSTS = 'apacheVhosts';
27
28 const ACTION_ENABLE = 'enableApache';
29 const ACTION_SWITCH_VERSION = 'switchApacheVersion';
30 const ACTION_CHANGE_PORT = 'changeApachePort';
31 const ACTION_INSTALL_SERVICE = 'installApacheService';
32 const ACTION_REMOVE_SERVICE = 'removeApacheService';
33 const ACTION_SWITCH_MODULE = 'switchApacheModule';
34 const ACTION_ADD_ALIAS = 'addAlias';
35 const ACTION_EDIT_ALIAS = 'editAlias';
36 const ACTION_ADD_VHOST = 'addVhost';
37 const ACTION_EDIT_VHOST = 'editVhost';
38
39 /**
40 * Generates the main Apache menu with options to enable/disable Apache and access submenus.
41 *
42 * @global object $bearsamppLang Provides language support for retrieving language-specific values.
43 * @global object $bearsamppBins Provides access to system binaries and their configurations.
44 *
45 * @return string The generated menu items and actions for Apache.
46 */
47 public static function process()
48 {
50
51 return TplApp::getMenuEnable($bearsamppLang->getValue(Lang::APACHE), self::MENU, get_called_class(), $bearsamppBins->getApache()->isEnable());
52 }
53
54 /**
55 * Generates the Apache menu with options for versions, service, debug, modules, aliases, and virtual hosts.
56 *
57 * @global object $bearsamppBins Provides access to system binaries and their configurations.
58 * @global object $bearsamppLang Provides language support for retrieving language-specific values.
59 *
60 * @return string The generated menu items and actions for Apache.
61 */
62 public static function getMenuApache()
63 {
65 $resultItems = $resultActions = '';
66
67 $isEnabled = $bearsamppBins->getApache()->isEnable();
68
69 // Download
70 $resultItems .= TplAestan::getItemLink(
72 Util::getWebsiteUrl('module/apache', '#releases'),
73 false,
75 ) . PHP_EOL;
76
77 // Enable
78 $tplEnable = TplApp::getActionMulti(
79 self::ACTION_ENABLE, array($isEnabled ? Config::DISABLED : Config::ENABLED),
80 array($bearsamppLang->getValue(Lang::MENU_ENABLE), $isEnabled ? TplAestan::GLYPH_CHECK : ''),
81 false, get_called_class()
82 );
83 $resultItems .= $tplEnable[TplApp::SECTION_CALL] . PHP_EOL;
84 $resultActions .= $tplEnable[TplApp::SECTION_CONTENT] . PHP_EOL;
85
86 if ($isEnabled) {
87 $resultItems .= TplAestan::getItemSeparator() . PHP_EOL;
88
89 // Versions
90 $tplVersions = TplApp::getMenu($bearsamppLang->getValue(Lang::VERSIONS), self::MENU_VERSIONS, get_called_class());
91 $resultItems .= $tplVersions[TplApp::SECTION_CALL] . PHP_EOL;
92 $resultActions .= $tplVersions[TplApp::SECTION_CONTENT] . PHP_EOL;
93
94 // Service
95 $tplService = TplApp::getMenu($bearsamppLang->getValue(Lang::SERVICE), self::MENU_SERVICE, get_called_class());
96 $resultItems .= $tplService[TplApp::SECTION_CALL] . PHP_EOL;
97 $resultActions .= $tplService[TplApp::SECTION_CONTENT] . PHP_EOL;
98
99 // Debug
100 $tplDebug = TplApp::getMenu($bearsamppLang->getValue(Lang::DEBUG), self::MENU_DEBUG, get_called_class());
101 $resultItems .= $tplDebug[TplApp::SECTION_CALL] . PHP_EOL;
102 $resultActions .= $tplDebug[TplApp::SECTION_CONTENT] . PHP_EOL;
103
104 // Modules
105 $tplModules = TplApp::getMenu($bearsamppLang->getValue(Lang::MODULES), self::MENU_MODULES, get_called_class());
106 $resultItems .= $tplModules[TplApp::SECTION_CALL] . PHP_EOL;
107 $resultActions .= $tplModules[TplApp::SECTION_CONTENT] . PHP_EOL;
108
109 // Alias
110 $tplAlias = TplApp::getMenu($bearsamppLang->getValue(Lang::ALIASES), self::MENU_ALIAS, get_called_class());
111 $resultItems .= $tplAlias[TplApp::SECTION_CALL] . PHP_EOL;
112 $resultActions .= $tplAlias[TplApp::SECTION_CONTENT] . PHP_EOL;
113
114 // Vhosts
115 $tplVhosts = TplApp::getMenu($bearsamppLang->getValue(Lang::VIRTUAL_HOSTS), self::MENU_VHOSTS, get_called_class());
116 $resultItems .= $tplVhosts[TplApp::SECTION_CALL] . PHP_EOL;
117 $resultActions .= $tplVhosts[TplApp::SECTION_CONTENT];
118
119 // Conf
120 $resultItems .= TplAestan::getItemNotepad(basename($bearsamppBins->getApache()->getConf()), $bearsamppBins->getApache()->getConf()) . PHP_EOL;
121
122 // Access log
123 $resultItems .= TplAestan::getItemNotepad($bearsamppLang->getValue(Lang::MENU_ACCESS_LOGS), $bearsamppBins->getApache()->getAccessLog()) . PHP_EOL;
124
125 // Rewrite log
126 $resultItems .= TplAestan::getItemNotepad($bearsamppLang->getValue(Lang::MENU_REWRITE_LOGS), $bearsamppBins->getApache()->getRewriteLog()) . PHP_EOL;
127
128 // Error log
129 $resultItems .= TplAestan::getItemNotepad($bearsamppLang->getValue(Lang::MENU_ERROR_LOGS), $bearsamppBins->getApache()->getErrorLog()) . PHP_EOL;
130 }
131
132 return $resultItems . PHP_EOL . $resultActions;
133 }
134
135 /**
136 * Generates the Apache versions menu with options to switch between different versions.
137 *
138 * @global object $bearsamppBins Provides access to system binaries and their configurations.
139 *
140 * @return string The generated menu items and actions for Apache versions.
141 */
142 public static function getMenuApacheVersions()
143 {
144 global $bearsamppBins;
145 $items = '';
146 $actions = '';
147
148 foreach ($bearsamppBins->getApache()->getVersionList() as $version) {
149 $glyph = '';
150 $apachePhpModule = $bearsamppBins->getPhp()->getApacheModule($version);
151 if ($apachePhpModule === false) {
153 } elseif ($version == $bearsamppBins->getApache()->getVersion()) {
154 $glyph = TplAestan::GLYPH_CHECK;
155 }
156
157 $tplSwitchApacheVersion = TplApp::getActionMulti(
158 self::ACTION_SWITCH_VERSION, array($version),
159 array($version, $glyph),
160 false, get_called_class()
161 );
162
163 // Item
164 $items .= $tplSwitchApacheVersion[TplApp::SECTION_CALL] . PHP_EOL;
165
166 // Action
167 $actions .= PHP_EOL . $tplSwitchApacheVersion[TplApp::SECTION_CONTENT];
168 }
169
170 return $items . $actions;
171 }
172
173 /**
174 * Generates the action to enable or disable Apache.
175 *
176 * @global object $bearsamppBins Provides access to system binaries and their configurations.
177 *
178 * @param int $enable The flag to enable (1) or disable (0) Apache.
179 * @return string The generated action to enable or disable Apache.
180 */
181 public static function getActionEnableApache($enable)
182 {
183 global $bearsamppBins;
184
185 return TplApp::getActionRun(Action::ENABLE, array($bearsamppBins->getApache()->getName(), $enable)) . PHP_EOL .
187 }
188
189 /**
190 * Generates the action to switch the Apache version.
191 *
192 * @global object $bearsamppBins Provides access to system binaries and their configurations.
193 *
194 * @param string $version The version to switch to.
195 * @return string The generated action to switch the Apache version.
196 */
197 public static function getActionSwitchApacheVersion($version)
198 {
199 global $bearsamppBins;
200 return TplApp::getActionRun(Action::SWITCH_VERSION, array($bearsamppBins->getApache()->getName(), $version)) . PHP_EOL .
202 }
203
204 /**
205 * Generates the Apache service menu with options to start, stop, restart, and manage the service.
206 *
207 * @global object $bearsamppRoot Provides access to the root path of the application.
208 * @global object $bearsamppLang Provides language support for retrieving language-specific values.
209 * @global object $bearsamppBins Provides access to system binaries and their configurations.
210 *
211 * @return string The generated menu items and actions for Apache service.
212 */
213 public static function getMenuApacheService()
214 {
216
217 $tplChangePort = TplApp::getActionMulti(
218 self::ACTION_CHANGE_PORT, null,
220 false, get_called_class()
221 );
222
223 $result = TplAestan::getItemActionServiceStart($bearsamppBins->getApache()->getService()->getName()) . PHP_EOL .
224 TplAestan::getItemActionServiceStop($bearsamppBins->getApache()->getService()->getName()) . PHP_EOL .
225 TplAestan::getItemActionServiceRestart($bearsamppBins->getApache()->getService()->getName()) . PHP_EOL .
226 TplAestan::getItemSeparator() . PHP_EOL .
228 Action::CHECK_PORT, array($bearsamppBins->getApache()->getName(), $bearsamppBins->getApache()->getPort()),
229 array(sprintf($bearsamppLang->getValue(Lang::MENU_CHECK_PORT), $bearsamppBins->getApache()->getPort()), TplAestan::GLYPH_LIGHT)
230 ) . PHP_EOL .
232 Action::CHECK_PORT, array($bearsamppBins->getApache()->getName(), $bearsamppBins->getApache()->getSslPort(), true),
233 array(sprintf($bearsamppLang->getValue(Lang::MENU_CHECK_PORT), $bearsamppBins->getApache()->getSslPort()) . ' (SSL)', TplAestan::GLYPH_RED_LIGHT)
234 ) . PHP_EOL .
235 $tplChangePort[TplApp::SECTION_CALL] . PHP_EOL .
236 TplAestan::getItemNotepad($bearsamppLang->getValue(Lang::MENU_UPDATE_ENV_PATH), $bearsamppRoot->getRootPath() . '/nssmEnvPaths.dat') . PHP_EOL;
237
238 $isInstalled = $bearsamppBins->getApache()->getService()->isInstalled();
239 if (!$isInstalled) {
240 $tplInstallService = TplApp::getActionMulti(
241 self::ACTION_INSTALL_SERVICE, null,
243 $isInstalled, get_called_class()
244 );
245
246 $result .= $tplInstallService[TplApp::SECTION_CALL] . PHP_EOL . PHP_EOL .
247 $tplInstallService[TplApp::SECTION_CONTENT] . PHP_EOL;
248 } else {
249 $tplRemoveService = TplApp::getActionMulti(
250 self::ACTION_REMOVE_SERVICE, null,
252 !$isInstalled, get_called_class()
253 );
254
255 $result .= $tplRemoveService[TplApp::SECTION_CALL] . PHP_EOL . PHP_EOL .
256 $tplRemoveService[TplApp::SECTION_CONTENT] . PHP_EOL;
257 }
258
259 $result .= $tplChangePort[TplApp::SECTION_CONTENT] . PHP_EOL;
260
261 return $result;
262 }
263
264 /**
265 * Generates the action to change the Apache port.
266 *
267 * @global object $bearsamppBins Provides access to system binaries and their configurations.
268 *
269 * @return string The generated action to change the Apache port.
270 */
271 public static function getActionChangeApachePort()
272 {
273 global $bearsamppBins;
274
275 return TplApp::getActionRun(Action::CHANGE_PORT, array($bearsamppBins->getApache()->getName())) . PHP_EOL .
277 }
278
279 /**
280 * Generates the action to install the Apache service.
281 *
282 * @return string The generated action to install the Apache service.
283 */
289
290 /**
291 * Generates the action to remove the Apache service.
292 *
293 * @return string The generated action to remove the Apache service.
294 */
300
301 /**
302 * Generates the Apache debug menu with options to run various debug commands.
303 *
304 * @global object $bearsamppLang Provides language support for retrieving language-specific values.
305 *
306 * @return string The generated menu items and actions for Apache debug.
307 */
308 public static function getMenuApacheDebug()
309 {
310 global $bearsamppLang;
311
315 ) . PHP_EOL .
319 ) . PHP_EOL .
323 ) . PHP_EOL .
327 ) . PHP_EOL .
331 ) . PHP_EOL .
335 ) . PHP_EOL .
339 ) . PHP_EOL;
340 }
341
342 /**
343 * Generates the Apache modules menu with options to switch modules on or off.
344 *
345 * @global object $bearsamppBins Provides access to system binaries and their configurations.
346 *
347 * @return string The generated menu items and actions for Apache modules.
348 */
349 public static function getMenuApacheModules()
350 {
351 global $bearsamppBins;
352 $items = '';
353 $actions = '';
354
355 foreach ($bearsamppBins->getApache()->getModulesFromConf() as $module => $switch) {
356 $tplSwitchApacheModule = TplApp::getActionMulti(
357 self::ACTION_SWITCH_MODULE, array($module, $switch),
358 array($module, ($switch == ActionSwitchApacheModule::SWITCH_ON ? TplAestan::GLYPH_CHECK : '')),
359 false, get_called_class()
360 );
361
362 // Item
363 $items .= $tplSwitchApacheModule[TplApp::SECTION_CALL] . PHP_EOL;
364
365 // Action
366 $actions .= PHP_EOL . $tplSwitchApacheModule[TplApp::SECTION_CONTENT];
367 }
368
369 return $items . $actions;
370 }
371
372 /**
373 * Generates the action to switch an Apache module on or off.
374 *
375 * @param string $module The module to switch.
376 * @param string $switch The current switch state of the module.
377 * @return string The generated action to switch the Apache module.
378 */
386 /**
387 * Generates the Apache Alias menu with options to add and edit aliases.
388 *
389 * @global object $bearsamppLang Provides language support for retrieving language-specific values.
390 * @global object $bearsamppBins Provides access to system binaries and their configurations.
391 *
392 * @return string The generated menu items and actions for Apache aliases.
393 */
394 public static function getMenuApacheAlias()
395 {
397
398 $tplAddAlias = TplApp::getActionMulti(
399 self::ACTION_ADD_ALIAS, null,
401 false, get_called_class()
402 );
403
404 // Items
405 $items = $tplAddAlias[TplApp::SECTION_CALL] . PHP_EOL .
406 TplAestan::getItemSeparator() . PHP_EOL;
407
408 // Actions
409 $actions = PHP_EOL . $tplAddAlias[TplApp::SECTION_CONTENT];
410
411 foreach ($bearsamppBins->getApache()->getAlias() as $alias) {
412 $tplEditAlias = TplApp::getActionMulti(
413 self::ACTION_EDIT_ALIAS, array($alias),
414 array(sprintf($bearsamppLang->getValue(Lang::MENU_EDIT_ALIAS), $alias), TplAestan::GLYPH_FILE),
415 false, get_called_class()
416 );
417
418 // Items
419 $items .= $tplEditAlias[TplApp::SECTION_CALL] . PHP_EOL;
420
421 // Actions
422 $actions .= PHP_EOL . PHP_EOL . $tplEditAlias[TplApp::SECTION_CONTENT];
423 }
424
425 return $items . $actions;
426 }
427
428 /**
429 * Generates the action to add an Apache alias.
430 *
431 * @return string The generated action to add an Apache alias.
432 */
433 public static function getActionAddAlias()
434 {
435 return TplApp::getActionRun(Action::ADD_ALIAS) . PHP_EOL .
437 }
438
439 /**
440 * Generates the action to edit an Apache alias.
441 *
442 * @param string $alias The alias to edit.
443 * @return string The generated action to edit an Apache alias.
444 */
445 public static function getActionEditAlias($alias)
446 {
447 return TplApp::getActionRun(Action::EDIT_ALIAS, array($alias)) . PHP_EOL .
449 }
450
451 /**
452 * Generates the Apache Virtual Hosts (Vhosts) menu with options to add and edit Vhosts.
453 *
454 * @global object $bearsamppLang Provides language support for retrieving language-specific values.
455 * @global object $bearsamppBins Provides access to system binaries and their configurations.
456 *
457 * @return string The generated menu items and actions for Apache Vhosts.
458 */
459 public static function getMenuApacheVhosts()
460 {
462
463 $tplAddVhost = TplApp::getActionMulti(
464 self::ACTION_ADD_VHOST, null,
466 false, get_called_class()
467 );
468
469 // Items
470 $items = $tplAddVhost[TplApp::SECTION_CALL] . PHP_EOL .
471 TplAestan::getItemSeparator() . PHP_EOL;
472
473 // Actions
474 $actions = PHP_EOL . $tplAddVhost[TplApp::SECTION_CONTENT];
475
476 foreach ($bearsamppBins->getApache()->getVhosts() as $vhost) {
477 $tplEditVhost = TplApp::getActionMulti(
478 self::ACTION_EDIT_VHOST, array($vhost),
479 array(sprintf($bearsamppLang->getValue(Lang::MENU_EDIT_VHOST), $vhost), TplAestan::GLYPH_FILE),
480 false, get_called_class()
481 );
482
483 // Items
484 $items .= $tplEditVhost[TplApp::SECTION_CALL] . PHP_EOL;
485
486 // Actions
487 $actions .= PHP_EOL . PHP_EOL . $tplEditVhost[TplApp::SECTION_CONTENT];
488 }
489
490 return $items . $actions;
491 }
492
493 /**
494 * Generates the action to add an Apache Virtual Host (Vhost).
495 *
496 * @return string The generated action to add an Apache Vhost.
497 */
498 public static function getActionAddVhost()
499 {
500 return TplApp::getActionRun(Action::ADD_VHOST) . PHP_EOL .
502 }
503
504 /**
505 * Generates the action to edit an Apache Virtual Host (Vhost).
506 *
507 * @param string $vhost The Vhost to edit.
508 * @return string The generated action to edit an Apache Vhost.
509 */
510 public static function getActionEditVhost($vhost)
511 {
512 return TplApp::getActionRun(Action::EDIT_VHOST, array($vhost)) . PHP_EOL .
514 }
515}
$result
global $bearsamppBins
global $bearsamppLang
global $bearsamppRoot
const CHANGE_PORT
const ADD_VHOST
const EDIT_VHOST
const CHECK_PORT
const ADD_ALIAS
const ENABLE
const SWITCH_VERSION
const EDIT_ALIAS
const DEBUG_APACHE
const SERVICE
const SWITCH_APACHE_MODULE
const CMD_LOADED_MODULES
const CMD_VERSION_NUMBER
const CMD_VHOSTS_SETTINGS
const CMD_CONFIG_DIRECTIVES
const CMD_COMPILE_SETTINGS
const CMD_SYNTAX_CHECK
const CMD_COMPILED_MODULES
const DISABLED
const ENABLED
const MENU_EDIT_ALIAS
const MENU_ERROR_LOGS
const DEBUG_APACHE_CONFIG_DIRECTIVES
const MENU_ACCESS_LOGS
const VERSIONS
const MENU_INSTALL_SERVICE
const DEBUG_APACHE_COMPILE_SETTINGS
const DEBUG_APACHE_COMPILED_MODULES
const ALIASES
const VIRTUAL_HOSTS
const MENU_ADD_ALIAS
const DEBUG_APACHE_VHOSTS_SETTINGS
const DEBUG_APACHE_SYNTAX_CHECK
const MENU_EDIT_VHOST
const MENU_ADD_VHOST
const MENU_UPDATE_ENV_PATH
const DEBUG
const MENU_ENABLE
const DEBUG_APACHE_VERSION_NUMBER
const DOWNLOAD_MORE
const MODULES
const DEBUG_APACHE_LOADED_MODULES
const MENU_REMOVE_SERVICE
const APACHE
const MENU_REWRITE_LOGS
const MENU_CHECK_PORT
const SERVICE
const MENU_CHANGE_PORT
const GLYPH_SERVICE_REMOVE
static getItemActionServiceStop($service)
static getItemSeparator()
static getItemActionServiceStart($service)
const GLYPH_RED_LIGHT
static getItemActionServiceRestart($service)
static getItemLink($caption, $link, $local=false, $glyph=self::GLYPH_WEB_PAGE)
const GLYPH_SERVICE_INSTALL
static getItemNotepad($caption, $path)
static getActionEditAlias($alias)
static getActionSwitchApacheModule($module, $switch)
static getActionEditVhost($vhost)
static getActionInstallApacheService()
static getActionChangeApachePort()
static getActionEnableApache($enable)
static getActionRemoveApacheService()
static getActionSwitchApacheVersion($version)
static getActionMulti($action, $args=array(), $item=array(), $disabled=false, $class=false)
const SECTION_CALL
static getMenuEnable($caption, $menu, $class, $enabled=true)
const SECTION_CONTENT
static getActionRun($action, $args=array(), $item=array(), $waitUntilTerminated=true)
static getMenu($caption, $menu, $class)
static getActionRestart($sName)
static getWebsiteUrl($path='', $fragment='', $utmSource=true)