2024.8.23
Loading...
Searching...
No Matches
class.action.addVhost.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 ActionAddVhost
12 * Handles the creation of a new virtual host (vhost) in the Bearsampp application.
13 */
15{
16 private $wbWindow;
22 private $wbLabelExp;
24 private $wbBtnSave;
25 private $wbBtnCancel;
26
27 const GAUGE_SAVE = 2;
28
29 /**
30 * ActionAddVhost constructor.
31 * Initializes the window and its components for adding a new virtual host.
32 *
33 * @param array $args Arguments passed to the constructor.
34 */
35 public function __construct($args)
36 {
37 global $bearsamppRoot, $bearsamppLang, $bearsamppWinbinder;
38
39 $initServerName = 'test.local';
40 $initDocumentRoot = Util::formatWindowsPath($bearsamppRoot->getWwwPath()) . '\\' . $initServerName;
41
42 $bearsamppWinbinder->reset();
43 $this->wbWindow = $bearsamppWinbinder->createAppWindow($bearsamppLang->getValue(Lang::ADD_VHOST_TITLE), 490, 200, WBC_NOTIFY, WBC_KEYDOWN | WBC_KEYUP);
44
45 $this->wbLabelServerName = $bearsamppWinbinder->createLabel($this->wbWindow, $bearsamppLang->getValue(Lang::VHOST_SERVER_NAME_LABEL) . ' :', 15, 15, 85, null, WBC_RIGHT);
46 $this->wbInputServerName = $bearsamppWinbinder->createInputText($this->wbWindow, $initServerName, 105, 13, 150, null);
47
48 $this->wbLabelDocRoot = $bearsamppWinbinder->createLabel($this->wbWindow, $bearsamppLang->getValue(Lang::VHOST_DOCUMENT_ROOT_LABEL) . ' :', 15, 45, 85, null, WBC_RIGHT);
49 $this->wbInputDocRoot = $bearsamppWinbinder->createInputText($this->wbWindow, $initDocumentRoot, 105, 43, 190, null, null, WBC_READONLY);
50 $this->wbBtnDocRoot = $bearsamppWinbinder->createButton($this->wbWindow, $bearsamppLang->getValue(Lang::BUTTON_BROWSE), 300, 43, 110);
51
52 $this->wbLabelExp = $bearsamppWinbinder->createLabel($this->wbWindow, sprintf($bearsamppLang->getValue(Lang::VHOST_EXP_LABEL), $initServerName, $initDocumentRoot), 15, 80, 470, 50);
53
54 $this->wbProgressBar = $bearsamppWinbinder->createProgressBar($this->wbWindow, self::GAUGE_SAVE + 1, 15, 137, 275);
55 $this->wbBtnSave = $bearsamppWinbinder->createButton($this->wbWindow, $bearsamppLang->getValue(Lang::BUTTON_SAVE), 300, 132);
56 $this->wbBtnCancel = $bearsamppWinbinder->createButton($this->wbWindow, $bearsamppLang->getValue(Lang::BUTTON_CANCEL), 387, 132);
57
58 $bearsamppWinbinder->setHandler($this->wbWindow, $this, 'processWindow');
59 $bearsamppWinbinder->mainLoop();
60 $bearsamppWinbinder->reset();
61 }
62
63 /**
64 * Processes window events and handles user interactions.
65 *
66 * @param resource $window The window resource.
67 * @param int $id The ID of the control that triggered the event.
68 * @param resource $ctrl The control resource.
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, $bearsamppBins, $bearsamppLang, $bearsamppOpenSsl, $bearsamppWinbinder;
75
76 $serverName = $bearsamppWinbinder->getText($this->wbInputServerName[WinBinder::CTRL_OBJ]);
77 $documentRoot = $bearsamppWinbinder->getText($this->wbInputDocRoot[WinBinder::CTRL_OBJ]);
78
79 switch ($id) {
80 case $this->wbInputServerName[WinBinder::CTRL_ID]:
81 $bearsamppWinbinder->setText(
82 $this->wbLabelExp[WinBinder::CTRL_OBJ],
83 sprintf($bearsamppLang->getValue(Lang::VHOST_EXP_LABEL), $serverName, $documentRoot)
84 );
85 $bearsamppWinbinder->setEnabled($this->wbBtnSave[WinBinder::CTRL_OBJ], empty($serverName) ? false : true);
86 break;
87 case $this->wbBtnDocRoot[WinBinder::CTRL_ID]:
88 $documentRoot = $bearsamppWinbinder->sysDlgPath($window, $bearsamppLang->getValue(Lang::VHOST_DOC_ROOT_PATH), $documentRoot);
89 if ($documentRoot && is_dir($documentRoot)) {
90 $bearsamppWinbinder->setText($this->wbInputDocRoot[WinBinder::CTRL_OBJ], $documentRoot . '\\');
91 $bearsamppWinbinder->setText(
92 $this->wbLabelExp[WinBinder::CTRL_OBJ],
93 sprintf($bearsamppLang->getValue(Lang::VHOST_EXP_LABEL), $serverName, $documentRoot . '\\')
94 );
95 }
96 break;
97 case $this->wbBtnSave[WinBinder::CTRL_ID]:
98 $bearsamppWinbinder->setProgressBarMax($this->wbProgressBar, self::GAUGE_SAVE + 1);
99 $bearsamppWinbinder->incrProgressBar($this->wbProgressBar);
100
101 if (!Util::isValidDomainName($serverName)) {
102 $bearsamppWinbinder->messageBoxError(
103 sprintf($bearsamppLang->getValue(Lang::VHOST_NOT_VALID_DOMAIN), $serverName),
105 $bearsamppWinbinder->resetProgressBar($this->wbProgressBar);
106 break;
107 }
108
109 if (is_file($bearsamppRoot->getVhostsPath() . '/' . $serverName . '.conf')) {
110 $bearsamppWinbinder->messageBoxError(
111 sprintf($bearsamppLang->getValue(Lang::VHOST_ALREADY_EXISTS), $serverName),
113 $bearsamppWinbinder->resetProgressBar($this->wbProgressBar);
114 break;
115 }
116
117 if ($bearsamppOpenSsl->createCrt($serverName) && file_put_contents($bearsamppRoot->getVhostsPath() . '/' . $serverName . '.conf', $bearsamppBins->getApache()->getVhostContent($serverName, $documentRoot)) !== false) {
118 $bearsamppWinbinder->incrProgressBar($this->wbProgressBar);
119
120 $bearsamppBins->getApache()->getService()->restart();
121 $bearsamppWinbinder->incrProgressBar($this->wbProgressBar);
122
123 $bearsamppWinbinder->messageBoxInfo(
124 sprintf($bearsamppLang->getValue(Lang::VHOST_CREATED), $serverName, $serverName, $documentRoot),
126 $bearsamppWinbinder->destroyWindow($window);
127 } else {
128 $bearsamppWinbinder->messageBoxError($bearsamppLang->getValue(Lang::VHOST_CREATED_ERROR), $bearsamppLang->getValue(Lang::ADD_VHOST_TITLE));
129 $bearsamppWinbinder->resetProgressBar($this->wbProgressBar);
130 }
131 break;
132 case IDCLOSE:
133 case $this->wbBtnCancel[WinBinder::CTRL_ID]:
134 $bearsamppWinbinder->destroyWindow($window);
135 break;
136 }
137 }
138}
global $bearsamppBins
global $bearsamppLang
global $bearsamppRoot
processWindow($window, $id, $ctrl, $param1, $param2)
const VHOST_EXP_LABEL
const VHOST_DOCUMENT_ROOT_LABEL
const VHOST_NOT_VALID_DOMAIN
const VHOST_CREATED_ERROR
const BUTTON_CANCEL
const VHOST_DOC_ROOT_PATH
const BUTTON_SAVE
const ADD_VHOST_TITLE
const VHOST_SERVER_NAME_LABEL
const VHOST_CREATED
const VHOST_ALREADY_EXISTS
const BUTTON_BROWSE
static isValidDomainName($domainName)
static formatWindowsPath($path)