Commit 425b3aee authored by wolfcode's avatar wolfcode

update

parent 15dcf008
......@@ -3,3 +3,4 @@
/tests/.phpunit.result.cache
/public/upload
/vendor/workerman/workerman.log
/extend/install.lock
<?php
namespace app\common\exception;
use Webman\Http\Request;
use Webman\Http\Response;
use Throwable;
use Webman\Exception\ExceptionHandler;
class Handler extends ExceptionHandler
{
public $dontReport = [];
public function report(Throwable $exception)
{
parent::report($exception);
}
public function render(Request $request, Throwable $exception): Response
{
return parent::render($request, $exception);
}
}
\ No newline at end of file
......@@ -7,6 +7,27 @@ function adminPasswordAuth(string $password, string $salt): string
return password_hash($password . $salt, PASSWORD_BCRYPT);
}
function sql_split($sql): array
{
$sql = preg_replace("/TYPE=(InnoDB|MyISAM|MEMORY)( DEFAULT CHARSET=[^; ]+)?/", "ENGINE=\\1 DEFAULT CHARSET=utf8mb4", $sql);
$sql = str_replace("\r", "\n", $sql);
$ret = array();
$num = 0;
$quirkiest = explode(";\n", trim($sql));
unset($sql);
foreach ($quirkiest as $query) {
$ret[$num] = '';
$queries = explode("\n", trim($query));
$queries = array_filter($queries);
foreach ($queries as $query) {
$str1 = substr($query, 0, 1);
if ($str1 != '#' && $str1 != '-')
$ret[$num] .= $query;
}
$num++;
}
return $ret;
}
if (!function_exists('sysConfig')) {
function sysConfig($type, $name = null, $default = '')
......
......@@ -15,44 +15,71 @@ class InstallCheck implements MiddlewareInterface
{
public function process(Request $request, callable $handler): Response
{
$base_path = base_path() . DIRECTORY_SEPARATOR . 'extend' . DIRECTORY_SEPARATOR;
$lock_file = $base_path . 'install.lock';
$sql_data = $base_path . 'm_blog.sql';
if (!file_exists($lock_file)) {
$dbHost = env('db_host');
$dbUser = env('db_username');
$dbPwd = env('db_password');
$dbPort = env('db_port');
$dbCharset = env('db_charset');
$dbName = env('db_database');
try {
$conn = mysqli_connect($dbHost, $dbUser, $dbPwd, null, $dbPort);
mysqli_query($conn, "SET NAMES {$dbCharset}");
if (!mysqli_select_db($conn, $dbName)) {
if (!mysqli_query($conn, "CREATE DATABASE IF NOT EXISTS `{$dbName}` DEFAULT CHARACTER SET {$dbCharset};")) {
$errorMsg = "数据库{$dbName} 不存在,也没权限创建新的数据库!";
mysqli_close($conn);
return response($errorMsg, 400);
}
}
if (file_exists($sql_data)) {
$sql = file_get_contents($sql_data);
if (!mysqli_select_db($conn, $dbName)) {
$errorMsg = "数据表{$dbName}不存在!";
return response($errorMsg, 400);
}
$exp = array_filter(explode('INSERT INTO', ($sql)));
foreach ($exp as $key => $value) {
$query_sql = 'INSERT INTO ' . htmlspecialchars_decode($value);
$result = mysqli_query($conn, $query_sql);
}
mysqli_close($conn);
}
} catch (\Exception $e) {
$errorMsg = "连接 MySQL 失败: " . mysqli_connect_error() . $e->getMessage();
return response($errorMsg, 400);
}
}
return $handler($request);
// $base_path = base_path() . DIRECTORY_SEPARATOR . 'extend' . DIRECTORY_SEPARATOR;
// $lock_file = $base_path . 'install.lock';
// if (file_exists($lock_file)) {
// return $handler($request);
// }
// if (version_compare(PHP_VERSION, '7.1.0', '<')) {
// $errorMsg = "PHP版本需要7.4或者以上,推荐8.1+";
// return response($errorMsg, 400);
// }
// $db_base_data = $base_path . 'database.sql';
// $sql_data = $base_path . 'my_blog.sql';
// if (file_exists($db_base_data) === false) {
// $errorMsg = "数据库基础获取异常,请确认{$db_base_data}文件是否存在";
// return response($errorMsg, 400);
// }
// if (file_exists($sql_data) === false) {
// $errorMsg = "数据库基础获取异常,请确认{$sql_data}文件是否存在";
// return response($errorMsg, 400);
// }
// $dbHost = env('db_host');
// $dbUser = env('db_username');
// $dbPwd = env('db_password');
// $dbPort = env('db_port');
// $dbCharset = env('db_charset');
// $dbName = env('db_database');
// try {
// $conn = mysqli_connect($dbHost, $dbUser, $dbPwd, null, $dbPort);
// mysqli_query($conn, "SET NAMES {$dbCharset}");
// $initDb = mysqli_select_db($conn, $dbName);
// if (!$initDb) {
// if (!mysqli_query($conn, "CREATE DATABASE IF NOT EXISTS `{$dbName}` DEFAULT CHARACTER SET {$dbCharset};")) {
// $errorMsg = "数据库{$dbName} 不存在,也没权限创建新的数据库!";
// mysqli_close($conn);
// return response($errorMsg, 400);
// }
// }
// // 先建表
// $db_data = file_get_contents($db_base_data);
// $sqlFormat = sql_split($db_data);
// $counts = count($sqlFormat);
// mysqli_select_db($conn, $dbName);
// for ($index = 0; $index < $counts; $index++) {
// $sql = trim($sqlFormat[$index]);
// if (strstr($sql, 'CREATE TABLE')) {
// mysqli_query($conn, $sql);
// }
// $index++;
// }
// @touch($base_path . 'install.lock');
// // $sql = file_get_contents($sql_data);
// // if (!mysqli_select_db($conn, $dbName)) {
// // $errorMsg = "数据表{$dbName}不存在!";
// // return response($errorMsg, 400);
// // }
// // $exp = array_filter(explode('INSERT INTO', ($sql)));
// // foreach ($exp as $value) {
// // $query_sql = 'INSERT INTO ' . htmlspecialchars_decode($value);
// // mysqli_query($conn, $query_sql);
// // }
// return $handler($request);
// } catch (\Exception $e) {
// $errorMsg = "连接 MySQL 失败: " . mysqli_connect_error() . $e->getMessage();
// return response($errorMsg, 400);
// }
}
}
\ No newline at end of file
......@@ -13,5 +13,5 @@
*/
return [
'' => support\exception\Handler::class,
'' => app\common\exception\Handler::class
];
\ No newline at end of file
INSERT INTO `article` VALUES (2, '白马王子', '啊发生的发生的分\n阿萨德发撒旦发射点发射点发射得分\n阿萨德发生地方', 1, '/upload/20220506\\1651852534FglIUK.jpg', '啊发生的发生的分\n<div>阿萨德发撒旦发射点发射点发射得分</div>\n<div>阿萨德发生地方</div>', '2022-05-08', 0, 1, 0, '2022-05-08 15:55:46', NULL);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment