Bearsampp 2026.3.26
API documentation
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 341 of file class.bin.mysql.php.

342 {
343 global $bearsamppWinbinder;
344 $startTime = microtime(true);
345 $error = null;
346 $timeout = 5; // 5 seconds timeout
347
348 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
349
350 try {
351 // Connect using PDO
352 $options = [
353 \PDO::ATTR_TIMEOUT => $timeout,
354 \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION
355 ];
356
357 $dsn = 'mysql:host=127.0.0.1;port=' . $this->port;
358 $dbLink = new \PDO($dsn, $this->rootUser, $currentPwd, $options);
359
360 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
361
362 // Determine MySQL version to use appropriate password update syntax
363 $stmt = $dbLink->query('SELECT VERSION()');
364 $version = $stmt->fetchColumn();
365
366 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
367
368 // Use appropriate SQL syntax based on MySQL version
369 if (version_compare($version, '5.7.6', '>=')) {
370 // MySQL 5.7.6 and newer uses ALTER USER
371 $sql = "ALTER USER '{$this->rootUser}'@'localhost' IDENTIFIED BY :password";
372 $stmt = $dbLink->prepare($sql);
373 $stmt->bindParam(':password', $newPwd);
374 } else {
375 // Older versions use SET PASSWORD
376 $sql = "SET PASSWORD FOR '{$this->rootUser}'@'localhost' = PASSWORD(:password)";
377 $stmt = $dbLink->prepare($sql);
378 $stmt->bindParam(':password', $newPwd);
379 }
380
381 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
382 $stmt->execute();
383
384 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
385 $dbLink->query('FLUSH PRIVILEGES');
386
387 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
388 $dbLink = null; // Close connection properly
389
390 } catch (\PDOException $e) {
391 $error = $e->getMessage();
392 }
393
394 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
395
396 if (!empty($error)) {
397 $totalTime = round(microtime(true) - $startTime, 2);
398 Util::logTrace("MySQL password change failed in {$totalTime}s: " . $error);
399
400 return $error;
401 }
402
403 // bearsampp.conf
404 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
405 $this->setRootPwd($newPwd);
406
407 // conf
408 $this->update();
409 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
410
411 $totalTime = round(microtime(true) - $startTime, 2);
412 Util::logTrace("MySQL password change completed in {$totalTime}s");
413
414 return true;
415 }
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 // Use new constant name for PHP 8.5+ while maintaining backward compatibility
256 // Check PHP version to avoid deprecation warnings
257 if (PHP_VERSION_ID >= 80500) {
258 $initCommandAttr = \Pdo\Mysql::ATTR_INIT_COMMAND;
259 } else {
260 $initCommandAttr = \PDO::MYSQL_ATTR_INIT_COMMAND;
261 }
262
263 $options = [
264 \PDO::ATTR_TIMEOUT => $timeout,
265 \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
266 $initCommandAttr => "SET SESSION sql_mode=''"
267 ];
268
269 $dsn = 'mysql:host=127.0.0.1;port=' . $port;
270 $cachedConnection = new \PDO($dsn, $this->rootUser, $this->rootPwd, $options);
271 $lastPort = $port;
272 } catch (\PDOException $e) {
273 Util::logDebug($this->getName() . ' port ' . $port . ' connection failed: ' . $e->getMessage());
274 if ($showWindow) {
275 $bearsamppWinbinder->messageBoxWarning(
276 sprintf($bearsamppLang->getValue(Lang::PORT_NOT_USED_BY), $port),
277 $boxTitle
278 );
279 }
280 return false;
281 }
282 }
283
284 try {
285 // Single optimized query to get both version and type
286 $stmt = $cachedConnection->query("SELECT @@version, @@version_comment");
287 $row = $stmt->fetch(\PDO::FETCH_NUM);
288
289 if (!$row) {
290 return false;
291 }
292
293 $version = explode('-', $row[0]);
294 $version = count($version) > 1 ? $version[0] : $row[0];
295 $isMysql = Util::startWith(strtolower($row[1]), 'mysql');
296
297 if (!$isMysql) {
298 Util::logDebug($this->getName() . ' port used by another DBMS: ' . $port);
299 if ($showWindow) {
300 $bearsamppWinbinder->messageBoxWarning(
302 $boxTitle
303 );
304 }
305 return false;
306 }
307
308 Util::logDebug($this->getName() . ' port ' . $port . ' is used by: ' . $this->getName() . ' ' . $version);
309 if ($showWindow) {
310 $bearsamppWinbinder->messageBoxInfo(
311 sprintf($bearsamppLang->getValue(Lang::PORT_USED_BY), $port, $this->getName() . ' ' . $version),
312 $boxTitle
313 );
314 }
315
316 $totalTime = round(microtime(true) - $startTime, 2);
317 Util::logTrace("MySQL port check completed in {$totalTime}s");
318 return true;
319
320 } catch (\PDOException $e) {
321 Util::logDebug($this->getName() . ' port ' . $port . ' validation error: ' . $e->getMessage());
322 if ($showWindow) {
323 $bearsamppWinbinder->messageBoxWarning(
324 sprintf($bearsamppLang->getValue(Lang::PORT_NOT_USED_BY), $port),
325 $boxTitle
326 );
327 }
328 return false;
329 }
330 }
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 425 of file class.bin.mysql.php.

426 {
427 global $bearsamppWinbinder;
428 $startTime = microtime(true);
429 $currentPwd = $currentPwd == null ? $this->rootPwd : $currentPwd;
430 $error = null;
431 $timeout = 2; // Reduced timeout for faster validation
432
433 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
434
435 // Use cached connection for password validation if available
436 static $passwordCache = [];
437 $cacheKey = md5($this->rootUser . ':' . $currentPwd . ':' . $this->port);
438
439 if (isset($passwordCache[$cacheKey]) && (time() - $passwordCache[$cacheKey]['time']) < 30) {
440 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
441 $totalTime = round(microtime(true) - $startTime, 2);
442 Util::logTrace("MySQL password check completed from cache in {$totalTime}s");
443 return $passwordCache[$cacheKey]['result'];
444 }
445
446 try {
447 // Use new constant name for PHP 8.5+ while maintaining backward compatibility
448 // Check PHP version to avoid deprecation warnings
449 if (PHP_VERSION_ID >= 80500) {
450 $initCommandAttr = \Pdo\Mysql::ATTR_INIT_COMMAND;
451 } else {
452 $initCommandAttr = \PDO::MYSQL_ATTR_INIT_COMMAND;
453 }
454
455 $options = [
456 \PDO::ATTR_TIMEOUT => $timeout,
457 \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
458 $initCommandAttr => "SET SESSION sql_mode=''"
459 ];
460
461 $dsn = 'mysql:host=127.0.0.1;port=' . $this->port;
462 $dbLink = new \PDO($dsn, $this->rootUser, $currentPwd, $options);
463
464 // Quick validation query
465 $dbLink->query('SELECT 1');
466 $dbLink = null; // Close connection properly
467
468 // Cache successful result
469 $passwordCache[$cacheKey] = [
470 'result' => true,
471 'time' => time()
472 ];
473
474 } catch (\PDOException $e) {
475 $error = $e->getMessage();
476
477 // Cache failed result for shorter time
478 $passwordCache[$cacheKey] = [
479 'result' => $error,
480 'time' => time() - 25 // Cache for only 5 seconds
481 ];
482 }
483
484 $bearsamppWinbinder->incrProgressBar($wbProgressBar);
485
486 if (!empty($error)) {
487 $totalTime = round(microtime(true) - $startTime, 2);
488 Util::logTrace("MySQL password check failed in {$totalTime}s: " . $error);
489 return $error;
490 }
491
492 $totalTime = round(microtime(true) - $startTime, 2);
493 Util::logTrace("MySQL password check completed in {$totalTime}s");
494 return true;
495 }

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 881 of file class.bin.mysql.php.

882 {
883 return $this->admin;
884 }

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 871 of file class.bin.mysql.php.

872 {
873 return $this->cliExe;
874 }

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 685 of file class.bin.mysql.php.

686 {
687 $result = array(
688 'syntaxOk' => false,
689 'content' => null,
690 );
691
692 $bin = $this->getExe();
693 $removeLines = 0;
694 $outputFrom = '';
695 if ($cmd == self::CMD_SYNTAX_CHECK) {
696 $outputFrom = '2';
697 } elseif ($cmd == self::CMD_VARIABLES) {
698 $bin = $this->getAdmin();
699 $cmd .= ' --user=' . $this->getRootUser();
700 if ($this->getRootPwd()) {
701 $cmd .= ' --password=' . $this->getRootPwd();
702 }
703 $removeLines = 2;
704 }
705
706 if (file_exists($bin)) {
707 $tmpResult = Batch::exec('mysqlGetCmdLineOutput', '"' . $bin . '" ' . $cmd . ' ' . $outputFrom, 5);
708 if ($tmpResult !== false && is_array($tmpResult)) {
709 $result['syntaxOk'] = empty($tmpResult) || !Util::contains(trim($tmpResult[count($tmpResult) - 1]), '[ERROR]');
710 for ($i = 0; $i < $removeLines; $i++) {
711 unset($tmpResult[$i]);
712 }
713 $result['content'] = trim(str_replace($bin, '', implode(PHP_EOL, $tmpResult)));
714 }
715 }
716
717 return $result;
718 }
$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 801 of file class.bin.mysql.php.

802 {
803 return $this->conf;
804 }

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 891 of file class.bin.mysql.php.

892 {
893 return $this->dataDir;
894 }

References $dataDir.

◆ getErrorLog()

getErrorLog ( )

Retrieves the path to the MySQL error log.

Returns
string The path to the error log.

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

782 {
783 return $this->errorLog;
784 }

References $errorLog.

◆ getExe()

getExe ( )

Retrieves the path to the MySQL executable.

Returns
string The path to the MySQL executable.

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

792 {
793 return $this->exe;
794 }

References $exe.

Referenced by getCmdLineOutput().

◆ getPort()

getPort ( )

Retrieves the MySQL port number.

Returns
int The port number.

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

812 {
813 return $this->port;
814 }

References $port.

◆ getRootPwd()

getRootPwd ( )

Retrieves the MySQL root password.

Returns
string The root password.

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

852 {
853 return $this->rootPwd;
854 }

References $rootPwd.

Referenced by getCmdLineOutput().

◆ getRootUser()

getRootUser ( )

Retrieves the MySQL root username.

Returns
string The root username.

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

832 {
833 return $this->rootUser;
834 }

References $rootUser.

Referenced by getCmdLineOutput().

◆ getService()

getService ( )

Retrieves the MySQL service object.

Returns
Win32Service The MySQL service object.

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

739 {
740 return $this->service;
741 }

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 593 of file class.bin.mysql.php.

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

750 {
751 global $bearsamppConfig, $bearsamppLang, $bearsamppWinbinder;
752
753 if ($enabled == Config::ENABLED && !is_dir($this->currentPath)) {
754 Util::logDebug($this->getName() . ' cannot be enabled because bundle ' . $this->getVersion() . ' does not exist in ' . $this->currentPath);
755 if ($showWindow) {
756 $bearsamppWinbinder->messageBoxError(
757 sprintf($bearsamppLang->getValue(Lang::ENABLE_BUNDLE_NOT_EXIST), $this->getName(), $this->getVersion(), $this->currentPath),
758 sprintf($bearsamppLang->getValue(Lang::ENABLE_TITLE), $this->getName())
759 );
760 }
761 $enabled = Config::DISABLED;
762 }
763
764 Util::logInfo($this->getName() . ' switched to ' . ($enabled == Config::ENABLED ? 'enabled' : 'disabled'));
765 $this->enable = $enabled == Config::ENABLED;
766 $bearsamppConfig->replace(self::ROOT_CFG_ENABLE, $enabled);
767
768 $this->reload();
769 if ($this->enable) {
770 Util::installService($this, $this->port, self::CMD_SYNTAX_CHECK, $showWindow);
771 } else {
772 Util::removeService($this->service, $this->name);
773 }
774 }
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 821 of file class.bin.mysql.php.

822 {
823 $this->replace(self::LOCAL_CFG_PORT, $port);
824 }
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 861 of file class.bin.mysql.php.

862 {
863 $this->replace(self::LOCAL_CFG_ROOT_PWD, $rootPwd);
864 }

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 841 of file class.bin.mysql.php.

842 {
843 $this->replace(self::LOCAL_CFG_ROOT_USER, $rootUser);
844 }

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 725 of file class.bin.mysql.php.

726 {
727 global $bearsamppConfig;
728 $this->version = $version;
729 $bearsamppConfig->replace(self::ROOT_CFG_VERSION, $version);
730 $this->reload();
731 }

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 505 of file class.bin.mysql.php.

506 {
507 Util::logDebug('Switch ' . $this->name . ' version to ' . $version);
508
509 return $this->updateConfig($version, 0, $showWindow);
510 }
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 521 of file class.bin.mysql.php.

522 {
523 global $bearsamppLang, $bearsamppBins, $bearsamppApps, $bearsamppWinbinder;
524
525 if (!$this->enable) {
526 return true;
527 }
528
529 $version = $version == null ? $this->version : $version;
530 Util::logDebug(($sub > 0 ? str_repeat(' ', 2 * $sub) : '') . 'Update ' . $this->name . ' ' . $version . ' config');
531
532 $boxTitle = sprintf($bearsamppLang->getValue(Lang::SWITCH_VERSION_TITLE), $this->getName(), $version);
533
534 $currentPath = str_replace('mysql' . $this->getVersion(), 'mysql' . $version, $this->getCurrentPath());
535 $conf = str_replace('mysql' . $this->getVersion(), 'mysql' . $version, $this->getConf());
536 $bearsamppConf = str_replace('mysql' . $this->getVersion(), 'mysql' . $version, $this->bearsamppConf);
537
538 if ($this->version != $version) {
540 }
541
542 if (!file_exists($conf) || !file_exists($bearsamppConf)) {
543 Util::logError('bearsampp config files not found for ' . $this->getName() . ' ' . $version);
544 if ($showWindow) {
545 $bearsamppWinbinder->messageBoxError(
546 sprintf($bearsamppLang->getValue(Lang::BEARSAMPP_CONF_NOT_FOUND_ERROR), $this->getName() . ' ' . $version),
547 $boxTitle
548 );
549 }
550
551 return false;
552 }
553
554 $bearsamppConfRaw = parse_ini_file($bearsamppConf);
555 if ($bearsamppConfRaw === false || !isset($bearsamppConfRaw[self::ROOT_CFG_VERSION]) || $bearsamppConfRaw[self::ROOT_CFG_VERSION] != $version) {
556 Util::logError('bearsampp config file malformed for ' . $this->getName() . ' ' . $version);
557 if ($showWindow) {
558 $bearsamppWinbinder->messageBoxError(
559 sprintf($bearsamppLang->getValue(Lang::BEARSAMPP_CONF_MALFORMED_ERROR), $this->getName() . ' ' . $version),
560 $boxTitle
561 );
562 }
563
564 return false;
565 }
566
567 // bearsampp.conf
568 $this->setVersion($version);
569
570 // conf
571 Util::replaceInFile($this->getConf(), array(
572 '/^port(.*?)=(.*?)(\d+)/' => 'port = ' . $this->port
573 ));
574
575 // phpmyadmin
576 $bearsamppApps->getPhpmyadmin()->update($sub + 1);
577
578 // php
579 $bearsamppBins->getPhp()->update($sub + 1);
580
581 return true;
582 }
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

◆ 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: