2024.8.23
Loading...
Searching...
No Matches
class.action.genSslCertificate.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 ActionGenSslCertificate
12 *
13 * This class handles the generation of SSL certificates through a WinBinder GUI.
14 * It provides a user interface for inputting the server name and target directory,
15 * and includes functionality for browsing directories, saving the certificate, and displaying progress.
16 */
18{
19 private $wbWindow;
20 private $wbLabelName;
21 private $wbInputName;
22 private $wbLabelDest;
23 private $wbInputDest;
24 private $wbBtnDest;
26 private $wbBtnSave;
27 private $wbBtnCancel;
28
29 const GAUGE_SAVE = 2;
30
31 /**
32 * Constructor for ActionGenSslCertificate.
33 *
34 * Initializes the WinBinder window and its controls, sets up event handlers, and starts the main loop.
35 *
36 * @param array $args Command line arguments passed to the script.
37 */
38 public function __construct($args)
39 {
40 global $bearsamppRoot, $bearsamppLang, $bearsamppWinbinder;
41
42 $initServerName = 'test.local';
43 $initDocumentRoot = Util::formatWindowsPath($bearsamppRoot->getSslPath());
44
45 $bearsamppWinbinder->reset();
46 $this->wbWindow = $bearsamppWinbinder->createAppWindow($bearsamppLang->getValue(Lang::GENSSL_TITLE), 490, 160, WBC_NOTIFY, WBC_KEYDOWN | WBC_KEYUP);
47
48 $this->wbLabelName = $bearsamppWinbinder->createLabel($this->wbWindow, $bearsamppLang->getValue(Lang::NAME) . ' :', 15, 15, 85, null, WBC_RIGHT);
49 $this->wbInputName = $bearsamppWinbinder->createInputText($this->wbWindow, $initServerName, 105, 13, 150, null);
50
51 $this->wbLabelDest = $bearsamppWinbinder->createLabel($this->wbWindow, $bearsamppLang->getValue(Lang::TARGET) . ' :', 15, 45, 85, null, WBC_RIGHT);
52 $this->wbInputDest = $bearsamppWinbinder->createInputText($this->wbWindow, $initDocumentRoot, 105, 43, 190, null, null, WBC_READONLY);
53 $this->wbBtnDest = $bearsamppWinbinder->createButton($this->wbWindow, $bearsamppLang->getValue(Lang::BUTTON_BROWSE), 300, 43, 110);
54
55 $this->wbProgressBar = $bearsamppWinbinder->createProgressBar($this->wbWindow, self::GAUGE_SAVE + 1, 15, 97, 275);
56 $this->wbBtnSave = $bearsamppWinbinder->createButton($this->wbWindow, $bearsamppLang->getValue(Lang::BUTTON_SAVE), 300, 92);
57 $this->wbBtnCancel = $bearsamppWinbinder->createButton($this->wbWindow, $bearsamppLang->getValue(Lang::BUTTON_CANCEL), 387, 92);
58
59 $bearsamppWinbinder->setHandler($this->wbWindow, $this, 'processWindow');
60 $bearsamppWinbinder->mainLoop();
61 $bearsamppWinbinder->reset();
62 }
63
64 /**
65 * Processes window events.
66 *
67 * Handles button clicks and other window events, such as browsing for a directory,
68 * saving the SSL certificate, and closing the window.
69 *
70 * @param resource $window The window resource.
71 * @param int $id The control ID that triggered the event.
72 * @param resource $ctrl The control resource that triggered the event.
73 * @param mixed $param1 Additional parameter 1.
74 * @param mixed $param2 Additional parameter 2.
75 */
76 public function processWindow($window, $id, $ctrl, $param1, $param2)
77 {
78 global $bearsamppLang, $bearsamppOpenSsl, $bearsamppWinbinder;
79
80 $name = $bearsamppWinbinder->getText($this->wbInputName[WinBinder::CTRL_OBJ]);
81 $target = $bearsamppWinbinder->getText($this->wbInputDest[WinBinder::CTRL_OBJ]);
82
83 switch ($id) {
84 case $this->wbBtnDest[WinBinder::CTRL_ID]:
85 $target = $bearsamppWinbinder->sysDlgPath($window, $bearsamppLang->getValue(Lang::GENSSL_PATH), $target);
86 if ($target && is_dir($target)) {
87 $bearsamppWinbinder->setText($this->wbInputDest[WinBinder::CTRL_OBJ], $target . '\\');
88 }
89 break;
90 case $this->wbBtnSave[WinBinder::CTRL_ID]:
91 $bearsamppWinbinder->setProgressBarMax($this->wbProgressBar, self::GAUGE_SAVE + 1);
92 $bearsamppWinbinder->incrProgressBar($this->wbProgressBar);
93
94 $target = Util::formatUnixPath($target);
95 if ($bearsamppOpenSsl->createCrt($name, $target)) {
96 $bearsamppWinbinder->incrProgressBar($this->wbProgressBar);
97 $bearsamppWinbinder->messageBoxInfo(
98 sprintf($bearsamppLang->getValue(Lang::GENSSL_CREATED), $name),
100 $bearsamppWinbinder->destroyWindow($window);
101 } else {
102 $bearsamppWinbinder->messageBoxError($bearsamppLang->getValue(Lang::GENSSL_CREATED_ERROR), $bearsamppLang->getValue(Lang::GENSSL_TITLE));
103 $bearsamppWinbinder->resetProgressBar($this->wbProgressBar);
104 }
105 break;
106 case IDCLOSE:
107 case $this->wbBtnCancel[WinBinder::CTRL_ID]:
108 $bearsamppWinbinder->destroyWindow($window);
109 break;
110 }
111 }
112}
global $bearsamppLang
global $bearsamppRoot
processWindow($window, $id, $ctrl, $param1, $param2)
const BUTTON_CANCEL
const GENSSL_CREATED
const BUTTON_SAVE
const GENSSL_PATH
const GENSSL_CREATED_ERROR
const NAME
const GENSSL_TITLE
const TARGET
const BUTTON_BROWSE
static formatUnixPath($path)
static formatWindowsPath($path)