Skip to content

Commit

Permalink
Merge pull request #273 from boris1993/mysql-ssl-support
Browse files Browse the repository at this point in the history
添加MySQL SSL连接的支持
  • Loading branch information
n0099 authored Dec 20, 2024
2 parents 09ca411 + 2f2308b commit e1b6d1d
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 11 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.idea/
setup/install.lock
!plugins/wmzz_debug
!plugins/wmzz_debug
.DS_Store
.devcontainer
.vscode
2 changes: 2 additions & 0 deletions config.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
define('DB_PASSWD', '');
//MySQL 数据库名称
define('DB_NAME', 'tiebacloud');
//MySQL 启用SSL连接,如需启用请将值改为1
define('DB_SSL', 0);

////////////////////////////以下选项使用任何数据库都需填写////////////////////////////
//数据库前缀,建议保持默认
Expand Down
4 changes: 2 additions & 2 deletions lib/class.S.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ class S extends wmysql
* @param string $name 数据库名
* @param bool $long 是否开启长连接
*/
public function __construct($host, $user, $pw, $name, $long = false)
public function __construct($host, $user, $pw, $name, $long = false, $useSSL = false)
{
try {
parent::__construct($host, $user, $pw, $name, $long);
parent::__construct($host, $user, $pw, $name, $long, $useSSL);
} catch (Exception $ex) {
msg($ex->getMessage());
}
Expand Down
12 changes: 9 additions & 3 deletions lib/class.mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,21 @@ class wmysql
* @param string $name 数据库名
* @param bool $long 是否开启长连接
*/
public function __construct($host, $user, $pw, $name, $long = false)
public function __construct($host, $user, $pw, $name, $long = false, $useSSL = false)
{
if (!function_exists('mysql_connect')) {
throw new Exception('服务器PHP不支持MySql数据库');
}

$flag = 0;
if ($useSsl) {
$flag = $flag & MYSQL_CLIENT_SSL;
}

if ($long) {
$this->conn = @mysql_pconnect($host, $user, $pw);
$this->conn = @mysql_pconnect($host, $user, $pw, $flag);
} else {
$this->conn = @mysql_connect($host, $user, $pw);
$this->conn = @mysql_connect($host, $user, $pw, client_flags: $flag);
}
if (!$this->conn) {
switch ($this->geterrno()) {
Expand Down
21 changes: 17 additions & 4 deletions lib/class.mysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,37 @@ class wmysql
* @param string $name 数据库名
* @param bool $long 是否开启长连接
*/
public function __construct($host, $user, $pw, $name, $long = false)
public function __construct($host, $user, $pw, $name, $long = false, $useSSL = false)
{
if (!class_exists('mysqli')) {
throw new Exception('服务器不支持MySqli类');
}
$coninfo = strpos($host, ':');

$flags = 0;
$mysqli = mysqli_init();
if ($useSsl) {
$flags = $flags | MYSQLI_CLIENT_SSL;
}

$connected = false;
if ($coninfo === false) {
if ($long) {
$host = 'p:' . $host;
}
@$this->conn = new mysqli($host, $user, $pw, $name);
$connected = $mysqli->real_connect($host, $user, $pw, $name, flags: $flags);
} else {
if ($long) {
@$this->conn = new mysqli('p:' . substr($host, 0, $coninfo), $user, $pw, $name, substr($host, $coninfo + 1));
$connected = $mysqli->real_connect('p:' . substr($host, 0, $coninfo), $user, $pw, $name, substr($host, $coninfo + 1), flags: $flags);
} else {
@$this->conn = new mysqli(substr($host, 0, $coninfo), $user, $pw, $name, substr($host, $coninfo + 1));
$connected = $mysqli->real_connect(substr($host, 0, $coninfo), $user, $pw, $name, substr($host, $coninfo + 1), flags: $flags);
}
}

if (!$connected) {
throw new Exception("连接数据库失败,原因:{$mysqli->error}");
}
@$this->conn = $mysqli;

if ($this->conn->connect_error) {
switch ($this->conn->connect_errno) {
Expand Down
2 changes: 1 addition & 1 deletion lib/mysql_autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
require SYSTEM_ROOT . '/lib/class.mysql.php';
}
require SYSTEM_ROOT . '/lib/class.S.php';
$m = new S(DB_HOST, DB_USER, DB_PASSWD, DB_NAME, LONGSQL); //以后直接使用$m->函数()即可操作数据库
$m = new S(DB_HOST, DB_USER, DB_PASSWD, DB_NAME, LONGSQL, DB_SSL); //以后直接使用$m->函数()即可操作数据库
8 changes: 8 additions & 0 deletions setup/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@
define(\'DB_PASSWD\',\'******\');
//MySQL 数据库名称
define(\'DB_NAME\',\'******\');
//MySQL 启用SSL连接,如需启用请将值改为1
define(\'DB_SSL\', 0);
////////////////////////////以下选项使用任何数据库都需填写////////////////////////////
//数据库前缀,建议保持默认
Expand Down Expand Up @@ -133,8 +135,10 @@
echo '<div class="input-group"><span class="input-group-addon">数据库密码</span><input type="text" class="form-control" value="' . DB_PASSWD . '" disabled></div><br/>';
echo '<div class="input-group"><span class="input-group-addon">数据库名称</span><input type="text" class="form-control" value="' . DB_NAME . '" disabled></div><br/>';
echo '<div class="input-group"><span class="input-group-addon">数据表前缀</span><input type="text" class="form-control" value="' . DB_PREFIX . '" disabled></div><br/>';
echo '<div class="input-group"><span class="input-group-addon">数据库开启SSL</span><select name="dbssl" class="form-control" required="true"><option value="0">否</option><option value="1">是</option></select></div><br/>';
echo '<input type="hidden" name="isbae" value="1">';
echo '<input type="hidden" name="from_config" value="1">';
echo '<input type="hidden" name="dbssl" value="0">';
} else {
echo '<br/><b>提示 1:</b>如果您已经手动写好了 config.php ,请选择 [ <b>自动获得数据库配置信息</b> ] 为 <b>是</b><br/>';
echo '<b>提示 2:</b>如果程序并未写入数据库 [ 安装完成后进入首页提示 Table XX doesn\'t exist ] 请选择强制手动导入 SQL<br/><br/>';
Expand All @@ -145,6 +149,7 @@
echo '<div class="input-group"><span class="input-group-addon">数据库密码</span><input type="text" class="form-control" name="dbpw" placeholder=""></div><br/>';
echo '<div class="input-group"><span class="input-group-addon">数据库名称</span><input type="text" class="form-control" name="dbname" placeholder=""></div><br/>';
echo '<div class="input-group"><span class="input-group-addon">数据表前缀</span><input type="text" class="form-control" name="dbprefix" value="tc_" placeholder=""></div><br/>';
echo '<div class="input-group"><span class="input-group-addon">数据库开启SSL</span><select name="dbssl" class="form-control" required="true"><option value="0">否</option><option value="1">是</option></select></div><br/>';
echo '</div>';
}
echo '<h4>站点创始人信息</h4><br/>';
Expand All @@ -171,6 +176,7 @@
define('DB_PASSWD', $_POST['dbpw']);
define('DB_NAME', $_POST['dbname']);
define('DB_PREFIX', $_POST['dbprefix']);
define('DB_SSL', $_POST['dbssl']);
}
$sql = str_ireplace('{VAR-PREFIX}', DB_PREFIX, file_get_contents(SYSTEM_ROOT2 . '/install.template.sql'));
$sql = str_ireplace('{VAR-DB}', DB_NAME, $sql);
Expand Down Expand Up @@ -222,6 +228,8 @@
define(\'DB_PASSWD\',\'' . DB_PASSWD . '\');
//MySQL 数据库名称
define(\'DB_NAME\',\'' . DB_NAME . '\');
//MySQL 启用SSL连接,如需启用请将值改为1
define(\'DB_SSL\',' . DB_SSL .');
////////////////////////////以下选项使用任何数据库都需填写////////////////////////////
//数据库前缀,建议保持默认
Expand Down

0 comments on commit e1b6d1d

Please sign in to comment.