2024.8.23
Loading...
Searching...
No Matches
class.app.adminer.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 AppAdminer
12 *
13 * This class represents the Adminer application module in the Bearsampp application.
14 * It extends the Module class and provides functionalities specific to the Adminer module,
15 * such as configuration management and version control.
16 */
17class AppAdminer extends Module
18{
19 /**
20 * Configuration key for the Adminer version in the root configuration.
21 */
22 const ROOT_CFG_VERSION = 'adminerVersion';
23
24 /**
25 * Configuration key for the Adminer configuration file in the local configuration.
26 */
27 const LOCAL_CFG_CONF = 'adminerConf';
28
29 /**
30 * @var string The path to the Adminer configuration file.
31 */
32 private $conf;
33
34 /**
35 * Constructor for the AppAdminer class.
36 *
37 * @param string $id The ID of the module.
38 * @param string $type The type of the module.
39 */
40 public function __construct($id, $type) {
41 Util::logInitClass($this);
42 $this->reload($id, $type);
43 }
44
45 /**
46 * Reloads the module configuration based on the provided ID and type.
47 *
48 * @param string|null $id The ID of the module. If null, the current ID is used.
49 * @param string|null $type The type of the module. If null, the current type is used.
50 */
51 public function reload($id = null, $type = null) {
54
55 $this->name = $bearsamppLang->getValue(Lang::ADMINER);
56 $this->version = $bearsamppConfig->getRaw(self::ROOT_CFG_VERSION);
57 parent::reload($id, $type);
58
59 if ($this->bearsamppConfRaw !== false) {
60 $this->conf = $this->symlinkPath . '/' . $this->bearsamppConfRaw[self::LOCAL_CFG_CONF];
61 }
62
63 if (!$this->enable) {
64 Util::logInfo($this->name . ' is not enabled!');
65 return;
66 }
67 if (!is_dir($this->currentPath)) {
68 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_FILE_NOT_FOUND), $this->name . ' ' . $this->version, $this->currentPath));
69 }
70 if (!is_dir($this->symlinkPath)) {
71 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_FILE_NOT_FOUND), $this->name . ' ' . $this->version, $this->symlinkPath));
72 return;
73 }
74 if (!is_file($this->bearsamppConf)) {
75 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_CONF_NOT_FOUND), $this->name . ' ' . $this->version, $this->bearsamppConf));
76 }
77 if (!is_file($this->conf)) {
78 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_CONF_NOT_FOUND), $this->name . ' ' . $this->version, $this->conf));
79 }
80 }
81
82 /**
83 * Updates the module configuration with a specific version.
84 *
85 * @param string|null $version The version to update to. If null, the current version is used.
86 * @param int $sub The sub-level for logging indentation.
87 * @param bool $showWindow Whether to show a window during the update process.
88 * @return bool True if the update was successful, false otherwise.
89 */
90 protected function updateConfig($version = null, $sub = 0, $showWindow = false) {
92
93 if (!$this->enable) {
94 return true;
95 }
96
97 $version = $version == null ? $this->version : $version;
98 Util::logDebug(($sub > 0 ? str_repeat(' ', 2 * $sub) : '') . 'Update ' . $this->name . ' ' . $version . ' config');
99
100 $alias = $bearsamppRoot->getAliasPath() . '/adminer.conf';
101 if (is_file($alias)) {
102 Util::replaceInFile($alias, array(
103 '/^Alias\s\/adminer\s.*/' => 'Alias /adminer "' . $this->getSymlinkPath() . '/"',
104 '/^<Directory\s.*/' => '<Directory "' . $this->getSymlinkPath() . '/">',
105 ));
106 } else {
107 Util::logError($this->getName() . ' alias not found : ' . $alias);
108 }
109
110 if ($bearsamppBins->getMysql()->isEnable()) {
111 Util::replaceInFile($this->getConf(), array(
112 '/^\$mysqlPort\s=\s(\d+)/' => '$mysqlPort = ' . $bearsamppBins->getMysql()->getPort() . ';',
113 '/^\$mysqlRootUser\s=\s/' => '$mysqlRootUser = \'' . $bearsamppBins->getMysql()->getRootUser() . '\';',
114 '/^\$mysqlRootPwd\s=\s/' => '$mysqlRootPwd = \'' . $bearsamppBins->getMysql()->getRootPwd() . '\';'
115 ));
116 }
117 if ($bearsamppBins->getMariadb()->isEnable()) {
118 Util::replaceInFile($this->getConf(), array(
119 '/^\$mariadbPort\s=\s(\d+)/' => '$mariadbPort = ' . $bearsamppBins->getMariadb()->getPort() . ';',
120 '/^\$mariadbRootUser\s=\s/' => '$mariadbRootUser = \'' . $bearsamppBins->getMariadb()->getRootUser() . '\';',
121 '/^\$mariadbRootPwd\s=\s/' => '$mariadbRootPwd = \'' . $bearsamppBins->getMariadb()->getRootPwd() . '\';'
122 ));
123 }
124 if ($bearsamppBins->getPostgresql()->isEnable()) {
125 Util::replaceInFile($this->getConf(), array(
126 '/^\$postgresqlPort\s=\s(\d+)/' => '$postgresqlPort = ' . $bearsamppBins->getPostgresql()->getPort() . ';',
127 '/^\$postgresqlRootUser\s=\s/' => '$postgresqlRootUser = \'' . $bearsamppBins->getPostgresql()->getRootUser() . '\';',
128 '/^\$postgresqlRootPwd\s=\s/' => '$postgresqlRootPwd = \'' . $bearsamppBins->getPostgresql()->getRootPwd() . '\';'
129 ));
130 }
131
132 return true;
133 }
134
135 /**
136 * Sets the version of the module.
137 *
138 * @param string $version The version to set.
139 */
140 public function setVersion($version) {
141 global $bearsamppConfig;
142 $this->version = $version;
143 $bearsamppConfig->replace(self::ROOT_CFG_VERSION, $version);
144 $this->reload();
145 }
146
147 /**
148 * Gets the path to the Adminer configuration file.
149 *
150 * @return string The path to the Adminer configuration file.
151 */
152 public function getConf() {
153 return $this->conf;
154 }
155}
global $bearsamppBins
global $bearsamppLang
global $bearsamppRoot
updateConfig($version=null, $sub=0, $showWindow=false)
__construct($id, $type)
reload($id=null, $type=null)
const ERROR_FILE_NOT_FOUND
const ADMINER
const ERROR_CONF_NOT_FOUND
static logReloadClass($classInstance)
static replaceInFile($path, $replaceList)
static logError($data, $file=null)
static logDebug($data, $file=null)
static logInitClass($classInstance)
static logInfo($data, $file=null)
global $bearsamppConfig
Definition homepage.php:26