Bearsampp 2025.8.29
Loading...
Searching...
No Matches
BinMysql Class Reference
Inheritance diagram for BinMysql:

Public Member Functions

 __construct ($id, $type)
 changePort ($port, $checkUsed=false, $wbProgressBar=null)
 changeRootPassword ($currentPwd, $newPwd, $wbProgressBar=null)
 checkPort ($port, $showWindow=false)
 checkRootPassword ($currentPwd=null, $wbProgressBar=null)
 getAdmin ()
 getCliExe ()
 getCmdLineOutput ($cmd)
 getConf ()
 getDataDir ()
 getErrorLog ()
 getExe ()
 getPort ()
 getRootPwd ()
 getRootUser ()
 getService ()
 initData ($path=null, $version=null)
 reload ($id=null, $type=null)
 setEnable ($enabled, $showWindow=false)
 setPort ($port)
 setRootPwd ($rootPwd)
 setRootUser ($rootUser)
 setVersion ($version)
 switchVersion ($version, $showWindow=false)
Public Member Functions inherited from Module
 __toString ()
 getCurrentPath ()
 getId ()
 getName ()
 getRelease ()
 getRootPath ()
 getSymlinkPath ()
 getType ()
 getVersion ()
 getVersionList ()
 isEnable ()
 update ($sub=0, $showWindow=false)

Data Fields

const CMD_SYNTAX_CHECK = '--help --verbose 1>NUL'
const CMD_VARIABLES = 'variables'
const CMD_VERSION = '--version'
const LOCAL_CFG_ADMIN = 'mysqlAdmin'
const LOCAL_CFG_CLI_EXE = 'mysqlCliExe'
const LOCAL_CFG_CONF = 'mysqlConf'
const LOCAL_CFG_EXE = 'mysqlExe'
const LOCAL_CFG_PORT = 'mysqlPort'
const LOCAL_CFG_ROOT_PWD = 'mysqlRootPwd'
const LOCAL_CFG_ROOT_USER = 'mysqlRootUser'
const ROOT_CFG_ENABLE = 'mysqlEnable'
const ROOT_CFG_VERSION = 'mysqlVersion'
const SERVICE_NAME = 'bearsamppmysql'
Data Fields inherited from Module
const BUNDLE_RELEASE = 'bundleRelease'

Protected Member Functions

 replaceAll ($params)
 updateConfig ($version=null, $sub=0, $showWindow=false)
Protected Member Functions inherited from Module
 __construct ()
 replace ($key, $value)

Private Attributes

 $admin
 $cliExe
 $conf
 $dataDir
 $errorLog
 $exe
 $port
 $rootPwd
 $rootUser
 $service

Additional Inherited Members

Protected Attributes inherited from Module
 $bearsamppConf
 $bearsamppConfRaw
 $currentPath
 $enable
 $name
 $release = 'N/A'
 $rootPath
 $symlinkPath
 $version

Detailed Description

Class BinMysql

This class represents the MySQL binary module in the Bearsampp application. It handles the configuration, management, and operations related to MySQL.

Definition at line 16 of file class.bin.mysql.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( $id,
$type )

Constructs a BinMysql object and initializes the MySQL module.

Parameters
string$idThe ID of the module.
string$typeThe type of the module.

Definition at line 53 of file class.bin.mysql.php.

54 {
55 Util::logInitClass($this);
56 $this->reload($id, $type);
57 }
reload($id=null, $type=null)
static logInitClass($classInstance)

References Module\$id, Module\$type, Util\logInitClass(), and reload().

Member Function Documentation

◆ changePort()

changePort ( $port,
$checkUsed = false,
$wbProgressBar = null )

Changes the MySQL port and updates the configuration.

Parameters
int$portThe new port number.
bool$checkUsedWhether to check if the port is already in use.
mixed$wbProgressBarThe progress bar object for UI updates.
Returns
bool|string True if the port was changed successfully, or an error message if the port is in use.

Definition at line 184 of file class.bin.mysql.php.

185 {
186 global $bearsamppWinbinder;
187
188 if (!Util::isValidPort($port)) {
189 Util::logError($this->getName() . ' port not valid: ' . $port);
190
191 return false;
192 }
193
194 $port = intval($port);
195 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
196
197 $isPortInUse = Util::isPortInUse($port);
198 if (!$checkUsed || $isPortInUse === false) {
199 // bearsampp.conf
200 $this->setPort($port);
201 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
202
203 // conf
204 $this->update();
205 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
206
207 return true;
208 }
209
210 Util::logDebug($this->getName() . ' port in used: ' . $port . ' - ' . $isPortInUse);
211
212 return $isPortInUse;
213 }
update($sub=0, $showWindow=false)
static logError($data, $file=null)
static isValidPort($port)
static logDebug($data, $file=null)
static isPortInUse($port)

References $port, Module\getName(), Util\isPortInUse(), Util\isValidPort(), Util\logDebug(), Util\logError(), setPort(), and Module\update().

◆ changeRootPassword()

changeRootPassword ( $currentPwd,
$newPwd,
$wbProgressBar = null )

Changes the MySQL root password.

Parameters
string$currentPwdThe current root password.
string$newPwdThe new root password.
mixed$wbProgressBarThe progress bar object for UI updates.
Returns
bool|string True if the password was changed successfully, or an error message if the operation failed.

Definition at line 333 of file class.bin.mysql.php.

334 {
335 global $bearsamppWinbinder;
336 $startTime = microtime(true);
337 $error = null;
338 $timeout = 5; // 5 seconds timeout
339
340 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
341
342 try {
343 // Connect using PDO
344 $options = [
345 \PDO::ATTR_TIMEOUT => $timeout,
346 \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION
347 ];
348
349 $dsn = 'mysql:host=127.0.0.1;port=' . $this->port;
350 $dbLink = new \PDO($dsn, $this->rootUser, $currentPwd, $options);
351
352 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
353
354 // Determine MySQL version to use appropriate password update syntax
355 $stmt = $dbLink->query('SELECT VERSION()');
356 $version = $stmt->fetchColumn();
357
358 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
359
360 // Use appropriate SQL syntax based on MySQL version
361 if (version_compare($version, '5.7.6', '>=')) {
362 // MySQL 5.7.6 and newer uses ALTER USER
363 $sql = "ALTER USER '{$this->rootUser}'@'localhost' IDENTIFIED BY :password";
364 $stmt = $dbLink->prepare($sql);
365 $stmt->bindParam(':password', $newPwd);
366 } else {
367 // Older versions use SET PASSWORD
368 $sql = "SET PASSWORD FOR '{$this->rootUser}'@'localhost' = PASSWORD(:password)";
369 $stmt = $dbLink->prepare($sql);
370 $stmt->bindParam(':password', $newPwd);
371 }
372
373 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
374 $stmt->execute();
375
376 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
377 $dbLink->query('FLUSH PRIVILEGES');
378
379 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
380 $dbLink = null; // Close connection properly
381
382 } catch (\PDOException $e) {
383 $error = $e->getMessage();
384 }
385
386 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
387
388 if (!empty($error)) {
389 $totalTime = round(microtime(true) - $startTime, 2);
390 Util::logTrace("MySQL password change failed in {$totalTime}s: " . $error);
391
392 return $error;
393 }
394
395 // bearsampp.conf
396 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
397 $this->setRootPwd($newPwd);
398
399 // conf
400 $this->update();
401 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
402
403 $totalTime = round(microtime(true) - $startTime, 2);
404 Util::logTrace("MySQL password change completed in {$totalTime}s");
405
406 return true;
407 }
setRootPwd($rootPwd)
static logTrace($data, $file=null)

References $port, Module\$version, Util\logTrace(), setRootPwd(), and Module\update().

◆ checkPort()

checkPort ( $port,
$showWindow = false )

Checks if the specified port is in use by MySQL.

Parameters
int$portThe port number to check.
bool$showWindowWhether to show a message box with the result.
Returns
bool True if the port is in use by MySQL, false otherwise.

Definition at line 223 of file class.bin.mysql.php.

224 {
225 global $bearsamppLang, $bearsamppWinbinder;
226 $boxTitle = sprintf($bearsamppLang->getValue(Lang::CHECK_PORT_TITLE), $this->getName(), $port);
227 $startTime = microtime(true);
228
229 if (!Util::isValidPort($port)) {
230 Util::logError($this->getName() . ' port not valid: ' . $port);
231 return false;
232 }
233
234 // Quick socket check first - much faster than PDO connection
235 $timeout = 1; // Reduced timeout for better performance
236 $fp = @fsockopen('127.0.0.1', $port, $errno, $errstr, $timeout);
237 if (!$fp) {
238 Util::logDebug($this->getName() . ' port ' . $port . ' is not used');
239 if ($showWindow) {
240 $bearsamppWinbinder->messageBoxError(
241 sprintf($bearsamppLang->getValue(Lang::PORT_NOT_USED), $port),
242 $boxTitle
243 );
244 }
245 return false;
246 }
247 fclose($fp);
248
249 // Use cached connection if available for better performance
250 static $cachedConnection = null;
251 static $lastPort = null;
252
253 if ($cachedConnection === null || $lastPort !== $port) {
254 try {
255 $options = [
256 \PDO::ATTR_TIMEOUT => $timeout,
257 \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
258 \PDO::MYSQL_ATTR_INIT_COMMAND => "SET SESSION sql_mode=''"
259 ];
260
261 $dsn = 'mysql:host=127.0.0.1;port=' . $port;
262 $cachedConnection = new \PDO($dsn, $this->rootUser, $this->rootPwd, $options);
263 $lastPort = $port;
264 } catch (\PDOException $e) {
265 Util::logDebug($this->getName() . ' port ' . $port . ' connection failed: ' . $e->getMessage());
266 if ($showWindow) {
267 $bearsamppWinbinder->messageBoxWarning(
268 sprintf($bearsamppLang->getValue(Lang::PORT_NOT_USED_BY), $port),
269 $boxTitle
270 );
271 }
272 return false;
273 }
274 }
275
276 try {
277 // Single optimized query to get both version and type
278 $stmt = $cachedConnection->query("SELECT @@version, @@version_comment");
279 $row = $stmt->fetch(\PDO::FETCH_NUM);
280
281 if (!$row) {
282 return false;
283 }
284
285 $version = explode('-', $row[0]);
286 $version = count($version) > 1 ? $version[0] : $row[0];
287 $isMysql = Util::startWith(strtolower($row[1]), 'mysql');
288
289 if (!$isMysql) {
290 Util::logDebug($this->getName() . ' port used by another DBMS: ' . $port);
291 if ($showWindow) {
292 $bearsamppWinbinder->messageBoxWarning(
294 $boxTitle
295 );
296 }
297 return false;
298 }
299
300 Util::logDebug($this->getName() . ' port ' . $port . ' is used by: ' . $this->getName() . ' ' . $version);
301 if ($showWindow) {
302 $bearsamppWinbinder->messageBoxInfo(
303 sprintf($bearsamppLang->getValue(Lang::PORT_USED_BY), $port, $this->getName() . ' ' . $version),
304 $boxTitle
305 );
306 }
307
308 $totalTime = round(microtime(true) - $startTime, 2);
309 Util::logTrace("MySQL port check completed in {$totalTime}s");
310 return true;
311
312 } catch (\PDOException $e) {
313 Util::logDebug($this->getName() . ' port ' . $port . ' validation error: ' . $e->getMessage());
314 if ($showWindow) {
315 $bearsamppWinbinder->messageBoxWarning(
316 sprintf($bearsamppLang->getValue(Lang::PORT_NOT_USED_BY), $port),
317 $boxTitle
318 );
319 }
320 return false;
321 }
322 }
global $bearsamppLang
const PORT_NOT_USED
const PORT_NOT_USED_BY
const CHECK_PORT_TITLE
const PORT_USED_BY
const PORT_USED_BY_ANOTHER_DBMS
static startWith($string, $search)

References $bearsamppLang, $port, Module\$version, Lang\CHECK_PORT_TITLE, Module\getName(), Util\isValidPort(), Util\logDebug(), Util\logError(), Util\logTrace(), Lang\PORT_NOT_USED, Lang\PORT_NOT_USED_BY, Lang\PORT_USED_BY, Lang\PORT_USED_BY_ANOTHER_DBMS, and Util\startWith().

◆ checkRootPassword()

checkRootPassword ( $currentPwd = null,
$wbProgressBar = null )

Checks if the provided root password is correct.

Parameters
string | null$currentPwdThe current root password. If null, the stored root password is used.
mixed$wbProgressBarThe progress bar object for UI updates.
Returns
bool|string True if the password is correct, or an error message if the operation failed.

Definition at line 417 of file class.bin.mysql.php.

418 {
419 global $bearsamppWinbinder;
420 $startTime = microtime(true);
421 $currentPwd = $currentPwd == null ? $this->rootPwd : $currentPwd;
422 $error = null;
423 $timeout = 2; // Reduced timeout for faster validation
424
425 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
426
427 // Use cached connection for password validation if available
428 static $passwordCache = [];
429 $cacheKey = md5($this->rootUser . ':' . $currentPwd . ':' . $this->port);
430
431 if (isset($passwordCache[$cacheKey]) && (time() - $passwordCache[$cacheKey]['time']) < 30) {
432 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
433 $totalTime = round(microtime(true) - $startTime, 2);
434 Util::logTrace("MySQL password check completed from cache in {$totalTime}s");
435 return $passwordCache[$cacheKey]['result'];
436 }
437
438 try {
439 $options = [
440 \PDO::ATTR_TIMEOUT => $timeout,
441 \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
442 \PDO::MYSQL_ATTR_INIT_COMMAND => "SET SESSION sql_mode=''"
443 ];
444
445 $dsn = 'mysql:host=127.0.0.1;port=' . $this->port;
446 $dbLink = new \PDO($dsn, $this->rootUser, $currentPwd, $options);
447
448 // Quick validation query
449 $dbLink->query('SELECT 1');
450 $dbLink = null; // Close connection properly
451
452 // Cache successful result
453 $passwordCache[$cacheKey] = [
454 'result' => true,
455 'time' => time()
456 ];
457
458 } catch (\PDOException $e) {
459 $error = $e->getMessage();
460
461 // Cache failed result for shorter time
462 $passwordCache[$cacheKey] = [
463 'result' => $error,
464 'time' => time() - 25 // Cache for only 5 seconds
465 ];
466 }
467
468 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
469
470 if (!empty($error)) {
471 $totalTime = round(microtime(true) - $startTime, 2);
472 Util::logTrace("MySQL password check failed in {$totalTime}s: " . $error);
473 return $error;
474 }
475
476 $totalTime = round(microtime(true) - $startTime, 2);
477 Util::logTrace("MySQL password check completed in {$totalTime}s");
478 return true;
479 }

References $port, and Util\logTrace().

◆ getAdmin()

getAdmin ( )

Retrieves the path to the MySQL admin executable.

Returns
string The path to the admin executable.

Definition at line 865 of file class.bin.mysql.php.

866 {
867 return $this->admin;
868 }

References $admin.

Referenced by getCmdLineOutput().

◆ getCliExe()

getCliExe ( )

Retrieves the path to the MySQL CLI executable.

Returns
string The path to the CLI executable.

Definition at line 855 of file class.bin.mysql.php.

856 {
857 return $this->cliExe;
858 }

References $cliExe.

◆ getCmdLineOutput()

getCmdLineOutput ( $cmd)

Executes a MySQL command and retrieves the output.

Parameters
string$cmdThe command to execute.
Returns
array An associative array containing 'syntaxOk' (boolean) and 'content' (string|null).

Definition at line 669 of file class.bin.mysql.php.

670 {
671 $result = array(
672 'syntaxOk' => false,
673 'content' => null,
674 );
675
676 $bin = $this->getExe();
677 $removeLines = 0;
678 $outputFrom = '';
679 if ($cmd == self::CMD_SYNTAX_CHECK) {
680 $outputFrom = '2';
681 } elseif ($cmd == self::CMD_VARIABLES) {
682 $bin = $this->getAdmin();
683 $cmd .= ' --user=' . $this->getRootUser();
684 if ($this->getRootPwd()) {
685 $cmd .= ' --password=' . $this->getRootPwd();
686 }
687 $removeLines = 2;
688 }
689
690 if (file_exists($bin)) {
691 $tmpResult = Batch::exec('mysqlGetCmdLineOutput', '"' . $bin . '" ' . $cmd . ' ' . $outputFrom, 5);
692 if ($tmpResult !== false && is_array($tmpResult)) {
693 $result['syntaxOk'] = empty($tmpResult) || !Util::contains(trim($tmpResult[count($tmpResult) - 1]), '[ERROR]');
694 for ($i = 0; $i < $removeLines; $i++) {
695 unset($tmpResult[$i]);
696 }
697 $result['content'] = trim(str_replace($bin, '', implode(PHP_EOL, $tmpResult)));
698 }
699 }
700
701 return $result;
702 }
$result
static exec($basename, $content, $timeout=true, $catchOutput=true, $standalone=false, $silent=true, $rebuild=true)
static contains($string, $search)

References $result, Util\contains(), Batch\exec(), getAdmin(), getExe(), getRootPwd(), and getRootUser().

◆ getConf()

getConf ( )

Retrieves the path to the MySQL configuration file.

Returns
string The path to the configuration file.

Definition at line 785 of file class.bin.mysql.php.

786 {
787 return $this->conf;
788 }

References $conf.

Referenced by updateConfig().

◆ getDataDir()

getDataDir ( )

Retrieves the path to the MySQL data directory.

Returns
string The path to the data directory.

Definition at line 875 of file class.bin.mysql.php.

876 {
877 return $this->dataDir;
878 }

References $dataDir.

◆ getErrorLog()

getErrorLog ( )

Retrieves the path to the MySQL error log.

Returns
string The path to the error log.

Definition at line 765 of file class.bin.mysql.php.

766 {
767 return $this->errorLog;
768 }

References $errorLog.

◆ getExe()

getExe ( )

Retrieves the path to the MySQL executable.

Returns
string The path to the MySQL executable.

Definition at line 775 of file class.bin.mysql.php.

776 {
777 return $this->exe;
778 }

References $exe.

Referenced by getCmdLineOutput().

◆ getPort()

getPort ( )

Retrieves the MySQL port number.

Returns
int The port number.

Definition at line 795 of file class.bin.mysql.php.

796 {
797 return $this->port;
798 }

References $port.

◆ getRootPwd()

getRootPwd ( )

Retrieves the MySQL root password.

Returns
string The root password.

Definition at line 835 of file class.bin.mysql.php.

836 {
837 return $this->rootPwd;
838 }

References $rootPwd.

Referenced by getCmdLineOutput().

◆ getRootUser()

getRootUser ( )

Retrieves the MySQL root username.

Returns
string The root username.

Definition at line 815 of file class.bin.mysql.php.

816 {
817 return $this->rootUser;
818 }

References $rootUser.

Referenced by getCmdLineOutput().

◆ getService()

getService ( )

Retrieves the MySQL service object.

Returns
Win32Service The MySQL service object.

Definition at line 722 of file class.bin.mysql.php.

723 {
724 return $this->service;
725 }

References $service.

◆ initData()

initData ( $path = null,
$version = null )

Initializes the MySQL data directory if needed. Triggers reinitialization when the directory exists but is incomplete (e.g., missing performance_schema).

Parameters
string | null$pathThe path to the MySQL installation. If null, the current path is used.
string | null$versionThe version of MySQL. If null, the current version is used.
Returns
bool True if initialization was successful or not needed

Definition at line 577 of file class.bin.mysql.php.

578 {
579 Util::logTrace('Starting MySQL data initialization');
580 $startTime = microtime(true);
581
582 $path = $path != null ? $path : $this->getCurrentPath();
583 $version = $version != null ? $version : $this->getVersion();
584 $dataDir = $path . '/data';
585 $perfSchemaDir = $dataDir . '/performance_schema';
586
587 if (version_compare($version, '5.7.0', '<')) {
588 Util::logTrace('MySQL version below 5.7.0, skipping initialization');
589
590 return true;
591 }
592
593 $needsInit = false;
594
595 if (!is_dir($dataDir)) {
596 Util::logTrace('MySQL data directory does not exist; initialization required');
597 $needsInit = true;
598 } else {
599 if (!is_dir($perfSchemaDir)) {
600 Util::logTrace('performance_schema directory missing; reinitialization required');
601 $needsInit = true;
602 }
603 }
604
605 if (!$needsInit) {
606 Util::logTrace('MySQL data directory already initialized');
607
608 return true;
609 }
610
611 // Prepare a clean data directory (mysqld --initialize-insecure requires an empty/nonexistent directory)
612 if (is_dir($dataDir)) {
613 $backupDir = $dataDir . '_bak_' . date('Ymd_His');
614 if (@rename($dataDir, $backupDir)) {
615 Util::logTrace('Backed up existing data directory to: ' . $backupDir);
616 } else {
617 Util::logTrace('Failed to backup existing data directory; attempting to clear it');
618 try {
619 $it = new \RecursiveDirectoryIterator($dataDir, \FilesystemIterator::SKIP_DOTS);
620 $files = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::CHILD_FIRST);
621 foreach ($files as $file) {
622 if ($file->isDir()) {
623 @rmdir($file->getPathname());
624 } else {
625 @unlink($file->getPathname());
626 }
627 }
628 @rmdir($dataDir);
629 } catch (\Throwable $t) {
630 Util::logTrace('Error clearing data directory: ' . $t->getMessage());
631 }
632 }
633 }
634
635 if (!is_dir($dataDir)) {
636 @mkdir($dataDir, 0777, true);
637 Util::logTrace('Created clean MySQL data directory');
638 }
639
640 // Use Bearsampp built-in initialization (init.bat via Batch)
641 try {
643 } catch (\Throwable $e) {
644 Util::logTrace('Error during MySQL initialization via Batch: ' . $e->getMessage());
645
646 return false;
647 }
648
649 // Verify initialization by checking performance_schema existence
650 if (!is_dir($perfSchemaDir)) {
651 Util::logTrace('MySQL initialization appears to have failed: performance_schema still missing');
652
653 return false;
654 }
655
656 $totalTime = round(microtime(true) - $startTime, 2);
657 Util::logTrace("MySQL initialization completed in {$totalTime}s");
658
659 return true;
660 }
static initializeMysql($path)

References $dataDir, Module\$version, Module\getCurrentPath(), Module\getVersion(), Batch\initializeMysql(), and Util\logTrace().

Referenced by updateConfig().

◆ reload()

reload ( $id = null,
$type = null )

Reloads the MySQL module configuration based on the provided ID and type.

Parameters
string | null$idThe ID of the module. If null, the current ID is used.
string | null$typeThe type of the module. If null, the current type is used.

Reimplemented from Module.

Definition at line 65 of file class.bin.mysql.php.

66 {
69
70 $this->name = $bearsamppLang->getValue(Lang::MYSQL);
71 $this->version = $bearsamppConfig->getRaw(self::ROOT_CFG_VERSION);
72 parent::reload($id, $type);
73
74 $this->enable = $this->enable && $bearsamppConfig->getRaw(self::ROOT_CFG_ENABLE);
75 $this->service = new Win32Service(self::SERVICE_NAME);
76 $this->errorLog = $bearsamppRoot->getLogsPath() . '/mysql.log';
77
78 if ($this->bearsamppConfRaw !== false) {
79 $this->exe = $this->symlinkPath . '/' . $this->bearsamppConfRaw[self::LOCAL_CFG_EXE];
80 $this->conf = $this->symlinkPath . '/' . $this->bearsamppConfRaw[self::LOCAL_CFG_CONF];
81 $this->port = $this->bearsamppConfRaw[self::LOCAL_CFG_PORT];
82 $this->rootUser = isset($this->bearsamppConfRaw[self::LOCAL_CFG_ROOT_USER]) ? $this->bearsamppConfRaw[self::LOCAL_CFG_ROOT_USER] : 'root';
83 $this->rootPwd = isset($this->bearsamppConfRaw[self::LOCAL_CFG_ROOT_PWD]) ? $this->bearsamppConfRaw[self::LOCAL_CFG_ROOT_PWD] : '';
84 $this->cliExe = $this->symlinkPath . '/' . $this->bearsamppConfRaw[self::LOCAL_CFG_CLI_EXE];
85 $this->admin = $this->symlinkPath . '/' . $this->bearsamppConfRaw[self::LOCAL_CFG_ADMIN];
86 $this->dataDir = $this->symlinkPath . '/data';
87 }
88
89 if (!$this->enable) {
90 Util::logInfo($this->name . ' is not enabled!');
91
92 return;
93 }
94 if (!is_dir($this->currentPath)) {
95 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_FILE_NOT_FOUND), $this->name . ' ' . $this->version, $this->currentPath));
96
97 return;
98 }
99 if (!is_dir($this->symlinkPath)) {
100 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_FILE_NOT_FOUND), $this->name . ' ' . $this->version, $this->symlinkPath));
101
102 return;
103 }
104 if (!is_file($this->bearsamppConf)) {
105 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_CONF_NOT_FOUND), $this->name . ' ' . $this->version, $this->bearsamppConf));
106
107 return;
108 }
109 if (!is_file($this->exe)) {
110 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_EXE_NOT_FOUND), $this->name . ' ' . $this->version, $this->exe));
111
112 return;
113 }
114 if (!is_file($this->conf)) {
115 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_CONF_NOT_FOUND), $this->name . ' ' . $this->version, $this->conf));
116
117 return;
118 }
119 if (!is_numeric($this->port) || $this->port <= 0) {
120 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_INVALID_PARAMETER), self::LOCAL_CFG_PORT, $this->port));
121
122 return;
123 }
124 if (empty($this->rootUser)) {
125 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_INVALID_PARAMETER), self::LOCAL_CFG_ROOT_USER, $this->rootUser));
126
127 return;
128 }
129 if (!is_file($this->cliExe)) {
130 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_EXE_NOT_FOUND), $this->name . ' ' . $this->version, $this->cliExe));
131
132 return;
133 }
134 if (!is_file($this->admin)) {
135 Util::logError(sprintf($bearsamppLang->getValue(Lang::ERROR_EXE_NOT_FOUND), $this->name . ' ' . $this->version, $this->admin));
136
137 return;
138 }
139
140 $this->service->setDisplayName(APP_TITLE . ' ' . $this->getName());
141 $this->service->setBinPath($this->exe);
142 $this->service->setParams(self::SERVICE_NAME);
143 $this->service->setStartType(Win32Service::SERVICE_DEMAND_START);
144 $this->service->setErrorControl(Win32Service::SERVER_ERROR_NORMAL);
145 }
global $bearsamppRoot
const ERROR_EXE_NOT_FOUND
const ERROR_CONF_NOT_FOUND
const ERROR_INVALID_PARAMETER
const MYSQL
const ERROR_FILE_NOT_FOUND
static logInfo($data, $file=null)
static logReloadClass($classInstance)
global $bearsamppConfig
Definition homepage.php:27
const APP_TITLE
Definition root.php:13

References $bearsamppConfig, $bearsamppLang, $bearsamppRoot, Module\$id, Module\$type, APP_TITLE, Lang\ERROR_CONF_NOT_FOUND, Lang\ERROR_EXE_NOT_FOUND, Lang\ERROR_FILE_NOT_FOUND, Lang\ERROR_INVALID_PARAMETER, Module\getName(), Util\logError(), Util\logInfo(), Util\logReloadClass(), Lang\MYSQL, Win32Service\SERVER_ERROR_NORMAL, and Win32Service\SERVICE_DEMAND_START.

Referenced by __construct(), setEnable(), and setVersion().

◆ replaceAll()

replaceAll ( $params)
protected

Replaces multiple key-value pairs in the configuration file.

Parameters
array$paramsAn associative array of key-value pairs to replace.

Reimplemented from Module.

Definition at line 152 of file class.bin.mysql.php.

153 {
154 $content = file_get_contents($this->bearsamppConf);
155
156 foreach ($params as $key => $value) {
157 $content = preg_replace('|' . $key . ' = .*|', $key . ' = ' . '"' . $value . '"', $content);
158 $this->bearsamppConfRaw[$key] = $value;
159 switch ($key) {
160 case self::LOCAL_CFG_PORT:
161 $this->port = $value;
162 break;
163 case self::LOCAL_CFG_ROOT_USER:
164 $this->rootUser = $value;
165 break;
166 case self::LOCAL_CFG_ROOT_PWD:
167 $this->rootPwd = $value;
168 break;
169 }
170 }
171
172 file_put_contents($this->bearsamppConf, $content);
173 }

◆ setEnable()

setEnable ( $enabled,
$showWindow = false )

Enables or disables the MySQL module and updates the configuration.

Parameters
bool$enabledWhether to enable or disable the module.
bool$showWindowWhether to show a message box with the result.

Definition at line 733 of file class.bin.mysql.php.

734 {
735 global $bearsamppConfig, $bearsamppLang, $bearsamppWinbinder;
736
737 if ($enabled == Config::ENABLED && !is_dir($this->currentPath)) {
738 Util::logDebug($this->getName() . ' cannot be enabled because bundle ' . $this->getVersion() . ' does not exist in ' . $this->currentPath);
739 if ($showWindow) {
740 $bearsamppWinbinder->messageBoxError(
741 sprintf($bearsamppLang->getValue(Lang::ENABLE_BUNDLE_NOT_EXIST), $this->getName(), $this->getVersion(), $this->currentPath),
742 sprintf($bearsamppLang->getValue(Lang::ENABLE_TITLE), $this->getName())
743 );
744 }
745 $enabled = Config::DISABLED;
746 }
747
748 Util::logInfo($this->getName() . ' switched to ' . ($enabled == Config::ENABLED ? 'enabled' : 'disabled'));
749 $this->enable = $enabled == Config::ENABLED;
750 $bearsamppConfig->replace(self::ROOT_CFG_ENABLE, $enabled);
751
752 $this->reload();
753 if ($this->enable) {
754 Util::installService($this, $this->port, self::CMD_SYNTAX_CHECK, $showWindow);
755 } else {
756 Util::removeService($this->service, $this->name);
757 }
758 }
const DISABLED
const ENABLED
const ENABLE_BUNDLE_NOT_EXIST
const ENABLE_TITLE
static installService($bin, $port, $syntaxCheckCmd, $showWindow=false)
static removeService($service, $name)

References $bearsamppConfig, $bearsamppLang, Config\DISABLED, Lang\ENABLE_BUNDLE_NOT_EXIST, Lang\ENABLE_TITLE, Config\ENABLED, Module\getName(), Module\getVersion(), Util\installService(), Util\logDebug(), Util\logInfo(), reload(), and Util\removeService().

◆ setPort()

setPort ( $port)

Sets the MySQL port number and updates the configuration.

Parameters
int$portThe port number to set.

Definition at line 805 of file class.bin.mysql.php.

806 {
807 $this->replace(self::LOCAL_CFG_PORT, $port);
808 }
replace($key, $value)

References $port, and Module\replace().

Referenced by changePort().

◆ setRootPwd()

setRootPwd ( $rootPwd)

Sets the MySQL root password and updates the configuration.

Parameters
string$rootPwdThe root password to set.

Definition at line 845 of file class.bin.mysql.php.

846 {
847 $this->replace(self::LOCAL_CFG_ROOT_PWD, $rootPwd);
848 }

References $rootPwd, and Module\replace().

Referenced by changeRootPassword().

◆ setRootUser()

setRootUser ( $rootUser)

Sets the MySQL root username and updates the configuration.

Parameters
string$rootUserThe root username to set.

Definition at line 825 of file class.bin.mysql.php.

826 {
827 $this->replace(self::LOCAL_CFG_ROOT_USER, $rootUser);
828 }

References $rootUser, and Module\replace().

◆ setVersion()

setVersion ( $version)

Sets the MySQL version and reloads the configuration.

Parameters
string$versionThe version to set.

Reimplemented from Module.

Definition at line 709 of file class.bin.mysql.php.

710 {
711 global $bearsamppConfig;
712 $this->version = $version;
713 $bearsamppConfig->replace(self::ROOT_CFG_VERSION, $version);
714 $this->reload();
715 }

References $bearsamppConfig, Module\$version, and reload().

Referenced by updateConfig().

◆ switchVersion()

switchVersion ( $version,
$showWindow = false )

Switches the MySQL version and updates the configuration.

Parameters
string$versionThe new MySQL version.
bool$showWindowWhether to show a message box with the result.
Returns
bool True if the version was switched successfully, false otherwise.

Definition at line 489 of file class.bin.mysql.php.

490 {
491 Util::logDebug('Switch ' . $this->name . ' version to ' . $version);
492
493 return $this->updateConfig($version, 0, $showWindow);
494 }
updateConfig($version=null, $sub=0, $showWindow=false)

References Module\$version, Util\logDebug(), and updateConfig().

◆ updateConfig()

updateConfig ( $version = null,
$sub = 0,
$showWindow = false )
protected

Updates the MySQL configuration with a specific version.

Parameters
string | null$versionThe version to update to. If null, the current version is used.
int$subThe sub-level for logging indentation.
bool$showWindowWhether to show a message box with the result.
Returns
bool True if the configuration was updated successfully, false otherwise.

Reimplemented from Module.

Definition at line 505 of file class.bin.mysql.php.

506 {
507 global $bearsamppLang, $bearsamppBins, $bearsamppApps, $bearsamppWinbinder;
508
509 if (!$this->enable) {
510 return true;
511 }
512
513 $version = $version == null ? $this->version : $version;
514 Util::logDebug(($sub > 0 ? str_repeat(' ', 2 * $sub) : '') . 'Update ' . $this->name . ' ' . $version . ' config');
515
516 $boxTitle = sprintf($bearsamppLang->getValue(Lang::SWITCH_VERSION_TITLE), $this->getName(), $version);
517
518 $currentPath = str_replace('mysql' . $this->getVersion(), 'mysql' . $version, $this->getCurrentPath());
519 $conf = str_replace('mysql' . $this->getVersion(), 'mysql' . $version, $this->getConf());
520 $bearsamppConf = str_replace('mysql' . $this->getVersion(), 'mysql' . $version, $this->bearsamppConf);
521
522 if ($this->version != $version) {
524 }
525
526 if (!file_exists($conf) || !file_exists($bearsamppConf)) {
527 Util::logError('bearsampp config files not found for ' . $this->getName() . ' ' . $version);
528 if ($showWindow) {
529 $bearsamppWinbinder->messageBoxError(
530 sprintf($bearsamppLang->getValue(Lang::BEARSAMPP_CONF_NOT_FOUND_ERROR), $this->getName() . ' ' . $version),
531 $boxTitle
532 );
533 }
534
535 return false;
536 }
537
538 $bearsamppConfRaw = parse_ini_file($bearsamppConf);
539 if ($bearsamppConfRaw === false || !isset($bearsamppConfRaw[self::ROOT_CFG_VERSION]) || $bearsamppConfRaw[self::ROOT_CFG_VERSION] != $version) {
540 Util::logError('bearsampp config file malformed for ' . $this->getName() . ' ' . $version);
541 if ($showWindow) {
542 $bearsamppWinbinder->messageBoxError(
543 sprintf($bearsamppLang->getValue(Lang::BEARSAMPP_CONF_MALFORMED_ERROR), $this->getName() . ' ' . $version),
544 $boxTitle
545 );
546 }
547
548 return false;
549 }
550
551 // bearsampp.conf
552 $this->setVersion($version);
553
554 // conf
555 Util::replaceInFile($this->getConf(), array(
556 '/^port(.*?)=(.*?)(\d+)/' => 'port = ' . $this->port
557 ));
558
559 // phpmyadmin
560 $bearsamppApps->getPhpmyadmin()->update($sub + 1);
561
562 // php
563 $bearsamppBins->getPhp()->update($sub + 1);
564
565 return true;
566 }
global $bearsamppBins
setVersion($version)
initData($path=null, $version=null)
const BEARSAMPP_CONF_MALFORMED_ERROR
const BEARSAMPP_CONF_NOT_FOUND_ERROR
const SWITCH_VERSION_TITLE
static replaceInFile($path, $replaceList)

References $bearsamppBins, Module\$bearsamppConf, Module\$bearsamppConfRaw, $bearsamppLang, $conf, Module\$currentPath, Module\$version, Lang\BEARSAMPP_CONF_MALFORMED_ERROR, Lang\BEARSAMPP_CONF_NOT_FOUND_ERROR, getConf(), Module\getCurrentPath(), Module\getName(), Module\getVersion(), initData(), Util\logDebug(), Util\logError(), Util\replaceInFile(), setVersion(), and Lang\SWITCH_VERSION_TITLE.

Referenced by switchVersion().

Field Documentation

◆ $admin

$admin
private

Definition at line 44 of file class.bin.mysql.php.

Referenced by getAdmin().

◆ $cliExe

$cliExe
private

Definition at line 43 of file class.bin.mysql.php.

Referenced by getCliExe().

◆ $conf

$conf
private

Definition at line 39 of file class.bin.mysql.php.

Referenced by getConf(), and updateConfig().

◆ $dataDir

$dataDir
private

Definition at line 45 of file class.bin.mysql.php.

Referenced by getDataDir(), and initData().

◆ $errorLog

$errorLog
private

Definition at line 36 of file class.bin.mysql.php.

Referenced by getErrorLog().

◆ $exe

$exe
private

Definition at line 38 of file class.bin.mysql.php.

Referenced by getExe().

◆ $port

$port
private

◆ $rootPwd

$rootPwd
private

Definition at line 42 of file class.bin.mysql.php.

Referenced by getRootPwd(), and setRootPwd().

◆ $rootUser

$rootUser
private

Definition at line 41 of file class.bin.mysql.php.

Referenced by getRootUser(), and setRootUser().

◆ $service

$service
private

Definition at line 35 of file class.bin.mysql.php.

Referenced by getService().

◆ CMD_SYNTAX_CHECK

const CMD_SYNTAX_CHECK = '--help --verbose 1>NUL'

◆ CMD_VARIABLES

const CMD_VARIABLES = 'variables'

◆ CMD_VERSION

const CMD_VERSION = '--version'

◆ LOCAL_CFG_ADMIN

const LOCAL_CFG_ADMIN = 'mysqlAdmin'

Definition at line 25 of file class.bin.mysql.php.

◆ LOCAL_CFG_CLI_EXE

const LOCAL_CFG_CLI_EXE = 'mysqlCliExe'

Definition at line 24 of file class.bin.mysql.php.

◆ LOCAL_CFG_CONF

const LOCAL_CFG_CONF = 'mysqlConf'

Definition at line 26 of file class.bin.mysql.php.

◆ LOCAL_CFG_EXE

const LOCAL_CFG_EXE = 'mysqlExe'

Definition at line 23 of file class.bin.mysql.php.

◆ LOCAL_CFG_PORT

const LOCAL_CFG_PORT = 'mysqlPort'

Definition at line 27 of file class.bin.mysql.php.

◆ LOCAL_CFG_ROOT_PWD

const LOCAL_CFG_ROOT_PWD = 'mysqlRootPwd'

Definition at line 29 of file class.bin.mysql.php.

◆ LOCAL_CFG_ROOT_USER

const LOCAL_CFG_ROOT_USER = 'mysqlRootUser'

Definition at line 28 of file class.bin.mysql.php.

◆ ROOT_CFG_ENABLE

const ROOT_CFG_ENABLE = 'mysqlEnable'

Definition at line 20 of file class.bin.mysql.php.

◆ ROOT_CFG_VERSION

const ROOT_CFG_VERSION = 'mysqlVersion'

Definition at line 21 of file class.bin.mysql.php.

◆ SERVICE_NAME


The documentation for this class was generated from the following file: