Bearsampp 2026.7.11
Loading...
Searching...
No Matches
BinMysql Class Reference
Inheritance diagram for BinMysql:
Collaboration 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 ()
 getId ()
 getName ()
 getRelease ()
 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
 $currentPath
 $rootPath
 $symlinkPath
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

Static Public Member Functions inherited from Module
static clearMemoryCache ()
static invalidateConfigCacheForPath ($sourcePath)
Protected Attributes inherited from Module
 $bearsamppConf
 $bearsamppConfRaw
 $enable
 $name
 $release = 'N/A'
 $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 Log::initClass($this);
56 $this->reload($id, $type);
57 }
reload($id=null, $type=null)
static initClass($classInstance)

References Module\$id, Module\$type, Log\initClass(), and reload().

Here is the call graph for this function:

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 Log::error($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 Log::debug($this->getName() . ' port in used: ' . $port . ' - ' . $isPortInUse);
211
212 return $isPortInUse;
213 }
static debug($data, $file=null)
static error($data, $file=null)
update($sub=0, $showWindow=false)
static isValidPort($port)
static isPortInUse($port)

References $port, Log\debug(), Log\error(), Module\getName(), Util\isPortInUse(), Util\isValidPort(), setPort(), and Module\update().

Here is the call graph for this function:

◆ 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 Log::trace("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 Log::trace("MySQL password change completed in {$totalTime}s");
413
414 return true;
415 }
setRootPwd($rootPwd)
static trace($data, $file=null)

References $port, Module\$version, setRootPwd(), Log\trace(), and Module\update().

Here is the call graph for this function:

◆ 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 Log::error($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 Log::debug($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 Log::debug($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 = UtilString::startWith(strtolower($row[1]), 'mysql');
296
297 if (!$isMysql) {
298 Log::debug($this->getName() . ' port used by another DBMS: ' . $port);
299 if ($showWindow) {
300 $bearsamppWinbinder->messageBoxWarning(
302 $boxTitle
303 );
304 }
305 return false;
306 }
307
308 Log::debug($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 Log::trace("MySQL port check completed in {$totalTime}s");
318 return true;
319
320 } catch (\PDOException $e) {
321 Log::debug($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, Log\debug(), Log\error(), Module\getName(), Util\isValidPort(), Lang\PORT_NOT_USED, Lang\PORT_NOT_USED_BY, Lang\PORT_USED_BY, Lang\PORT_USED_BY_ANOTHER_DBMS, UtilString\startWith(), and Log\trace().

Here is the call graph for this function:

◆ 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 Log::trace("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 Log::trace("MySQL password check failed in {$totalTime}s: " . $error);
489 return $error;
490 }
491
492 $totalTime = round(microtime(true) - $startTime, 2);
493 Log::trace("MySQL password check completed in {$totalTime}s");
494 return true;
495 }

References $port, and Log\trace().

Here is the call graph for this function:

◆ getAdmin()

getAdmin ( )

Retrieves the path to the MySQL admin executable.

Returns
string The path to the admin executable.

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

885 {
886 return $this->admin;
887 }

References $admin.

Referenced by getCmdLineOutput().

Here is the caller graph for this function:

◆ getCliExe()

getCliExe ( )

Retrieves the path to the MySQL CLI executable.

Returns
string The path to the CLI executable.

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

875 {
876 return $this->cliExe;
877 }

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

689 {
690 $result = array(
691 'syntaxOk' => false,
692 'content' => null,
693 );
694
695 $bin = $this->getExe();
696 $removeLines = 0;
697 $outputFrom = '';
698 if ($cmd == self::CMD_SYNTAX_CHECK) {
699 $outputFrom = '2';
700 } elseif ($cmd == self::CMD_VARIABLES) {
701 $bin = $this->getAdmin();
702 $cmd .= ' --user=' . $this->getRootUser();
703 if ($this->getRootPwd()) {
704 $cmd .= ' --password=' . $this->getRootPwd();
705 }
706 $removeLines = 2;
707 }
708
709 if (file_exists($bin)) {
710 $tmpResult = Batch::exec('mysqlGetCmdLineOutput', '"' . $bin . '" ' . $cmd . ' ' . $outputFrom, 5);
711 if ($tmpResult !== false && is_array($tmpResult)) {
712 $result['syntaxOk'] = empty($tmpResult) || !UtilString::contains(trim($tmpResult[count($tmpResult) - 1]), '[ERROR]');
713 for ($i = 0; $i < $removeLines; $i++) {
714 unset($tmpResult[$i]);
715 }
716 $result['content'] = trim(str_replace($bin, '', implode(PHP_EOL, $tmpResult)));
717 }
718 }
719
720 return $result;
721 }
$result
static exec($basename, $content, $timeout=true, $catchOutput=true, $standalone=false, $silent=true, $rebuild=true)
static contains($string, $search)

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

Here is the call graph for this function:

◆ getConf()

getConf ( )

Retrieves the path to the MySQL configuration file.

Returns
string The path to the configuration file.

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

805 {
806 return $this->conf;
807 }

References $conf.

Referenced by updateConfig().

Here is the caller graph for this function:

◆ getDataDir()

getDataDir ( )

Retrieves the path to the MySQL data directory.

Returns
string The path to the data directory.

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

895 {
896 return $this->dataDir;
897 }

References $dataDir.

◆ getErrorLog()

getErrorLog ( )

Retrieves the path to the MySQL error log.

Returns
string The path to the error log.

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

785 {
786 return $this->errorLog;
787 }

References $errorLog.

◆ getExe()

getExe ( )

Retrieves the path to the MySQL executable.

Returns
string The path to the MySQL executable.

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

795 {
796 return $this->exe;
797 }

References $exe.

Referenced by getCmdLineOutput().

Here is the caller graph for this function:

◆ getPort()

getPort ( )

Retrieves the MySQL port number.

Returns
int The port number.

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

815 {
816 return $this->port;
817 }

References $port.

◆ getRootPwd()

getRootPwd ( )

Retrieves the MySQL root password.

Returns
string The root password.

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

855 {
856 return $this->rootPwd;
857 }

References $rootPwd.

Referenced by getCmdLineOutput().

Here is the caller graph for this function:

◆ getRootUser()

getRootUser ( )

Retrieves the MySQL root username.

Returns
string The root username.

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

835 {
836 return $this->rootUser;
837 }

References $rootUser.

Referenced by getCmdLineOutput().

Here is the caller graph for this function:

◆ getService()

getService ( )

Retrieves the MySQL service object.

Returns
Win32Service The MySQL service object.

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

742 {
743 return $this->service;
744 }

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

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

References $dataDir, Module\$version, Path\getModuleCurrentPath(), Module\getVersion(), Batch\initializeMysql(), and Log\trace().

Referenced by updateConfig().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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 {
68 Log::reloadClass($this);
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 = Path::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 Log::info($this->name . ' is not enabled!');
91
92 return;
93 }
94 if (!is_dir($this->currentPath)) {
95 Log::error(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 Log::error(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 Log::error(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 Log::error(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 Log::error(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 Log::error(sprintf($bearsamppLang->getValue(Lang::ERROR_INVALID_PARAMETER), self::LOCAL_CFG_PORT, $this->port));
121
122 return;
123 }
124 if (empty($this->rootUser)) {
125 Log::error(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 Log::error(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 Log::error(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 info($data, $file=null)
static reloadClass($classInstance)
static getLogsPath($aetrayPath=false)
global $bearsamppConfig
Definition homepage.php:41
const APP_TITLE
Definition root.php:13

References $bearsamppConfig, $bearsamppLang, $bearsamppRoot, Module\$id, Module\$type, APP_TITLE, Log\error(), Lang\ERROR_CONF_NOT_FOUND, Lang\ERROR_EXE_NOT_FOUND, Lang\ERROR_FILE_NOT_FOUND, Lang\ERROR_INVALID_PARAMETER, Path\getLogsPath(), Module\getName(), Log\info(), Lang\MYSQL, Log\reloadClass(), Win32Service\SERVER_ERROR_NORMAL, and Win32Service\SERVICE_DEMAND_START.

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

Here is the call graph for this function:
Here is the caller graph for this function:

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

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

Here is the call graph for this function:

◆ setPort()

setPort ( $port)

Sets the MySQL port number and updates the configuration.

Parameters
int$portThe port number to set.

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

825 {
826 $this->replace(self::LOCAL_CFG_PORT, $port);
827 }
replace($key, $value)

References $port, and Module\replace().

Referenced by changePort().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setRootPwd()

setRootPwd ( $rootPwd)

Sets the MySQL root password and updates the configuration.

Parameters
string$rootPwdThe root password to set.

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

865 {
866 $this->replace(self::LOCAL_CFG_ROOT_PWD, $rootPwd);
867 }

References $rootPwd, and Module\replace().

Referenced by changeRootPassword().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setRootUser()

setRootUser ( $rootUser)

Sets the MySQL root username and updates the configuration.

Parameters
string$rootUserThe root username to set.

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

845 {
846 $this->replace(self::LOCAL_CFG_ROOT_USER, $rootUser);
847 }

References $rootUser, and Module\replace().

Here is the call graph for this function:

◆ setVersion()

setVersion ( $version)

Sets the MySQL version and reloads the configuration.

Parameters
string$versionThe version to set.

Reimplemented from Module.

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

729 {
730 global $bearsamppConfig;
731 $this->version = $version;
732 $bearsamppConfig->replace(self::ROOT_CFG_VERSION, $version);
733 $this->reload();
734 }

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

Referenced by updateConfig().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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 Log::debug('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, Log\debug(), and updateConfig().

Here is the call graph for this function:

◆ 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 Log::debug(($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, Path::getModuleCurrentPath($this));
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 Log::error('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 Log::error('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 // Invalidate cache for the new version's config
572
573 // conf
574 Util::replaceInFile($this->getConf(), array(
575 '/^port(.*?)=(.*?)(\d+)/' => 'port = ' . $this->port
576 ));
577
578 // phpmyadmin
579 $bearsamppApps->getPhpmyadmin()->update($sub + 1);
580
581 // php
582 $bearsamppBins->getPhp()->update($sub + 1);
583
584 return true;
585 }
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 invalidateConfigCacheForPath($sourcePath)
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, Log\debug(), Log\error(), getConf(), Path\getModuleCurrentPath(), Module\getName(), Module\getVersion(), initData(), Module\invalidateConfigCacheForPath(), Util\replaceInFile(), setVersion(), and Lang\SWITCH_VERSION_TITLE.

Referenced by switchVersion().

Here is the call graph for this function:
Here is the caller graph for this function:

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: