From b816664428a0691cf96c49b35fc1cfa93cf2cdec Mon Sep 17 00:00:00 2001 From: printempw Date: Wed, 14 Sep 2016 19:57:24 +0800 Subject: [PATCH] fix bootstrap of setup and migration --- .../views/setup/migrations/import-v2-both.tpl | 9 +- .../setup/migrations/import-v2-textures.tpl | 8 +- .../setup/migrations/import-v2-users.tpl | 8 +- setup/{ => includes}/bootstrap.php | 145 ++++++------------ setup/includes/helpers.php | 88 +++++++++++ setup/{ => includes}/tables.php | 0 setup/index.php | 6 +- setup/migrations/import_v2_both.php | 10 +- setup/migrations/import_v2_textures.php | 13 +- setup/migrations/import_v2_users.php | 6 +- setup/migrations/index.php | 16 +- setup/update.php | 6 +- 12 files changed, 179 insertions(+), 136 deletions(-) rename setup/{ => includes}/bootstrap.php (50%) create mode 100644 setup/includes/helpers.php rename setup/{ => includes}/tables.php (100%) diff --git a/resources/views/setup/migrations/import-v2-both.tpl b/resources/views/setup/migrations/import-v2-both.tpl index 2ec84e2a..65e9b40b 100644 --- a/resources/views/setup/migrations/import-v2-both.tpl +++ b/resources/views/setup/migrations/import-v2-both.tpl @@ -47,9 +47,8 @@ - - @if (isset($_SESSION['msg'])) - + @if (Session::has('msg')) + @endif

@@ -63,7 +62,7 @@ @if ($step == '2') 导入成功 - +

已导入 {{ $result['user']['imported'] }} 个用户,{{ $result['user']['duplicated'] }} 个用户因重复而未导入。

已导入 {{ $result['texture']['imported'] }} 个材质到皮肤库,{{ $result['texture']['duplicated'] }} 个材质因重复而未导入。

diff --git a/resources/views/setup/migrations/import-v2-textures.tpl b/resources/views/setup/migrations/import-v2-textures.tpl index 70d5a9e3..cfea8a73 100644 --- a/resources/views/setup/migrations/import-v2-textures.tpl +++ b/resources/views/setup/migrations/import-v2-textures.tpl @@ -56,8 +56,8 @@ - @if (isset($_SESSION['msg'])) - + @if (Session::has('msg')) + @endif

@@ -71,7 +71,7 @@ @if ($step == '2') 导入成功 - +

已导入 {{ $result['imported'] }} 个材质到皮肤库,{{ $result['duplicated'] }} 个材质因重复而未导入。

注意:请将 v2 的 textures 文件夹内容复制到 v3 的 textures 文件夹中

diff --git a/resources/views/setup/migrations/import-v2-users.tpl b/resources/views/setup/migrations/import-v2-users.tpl index b860e710..0da86a2e 100644 --- a/resources/views/setup/migrations/import-v2-users.tpl +++ b/resources/views/setup/migrations/import-v2-users.tpl @@ -26,8 +26,8 @@ - @if (isset($_SESSION['msg'])) - + @if (Session::has('msg')) + @endif

@@ -41,7 +41,7 @@ @if ($step == '2') 导入成功 - +

已导入 {{ $result['imported'] }} 个用户,{{ $result['duplicated'] }} 个用户因重复而未导入。

diff --git a/setup/bootstrap.php b/setup/includes/bootstrap.php similarity index 50% rename from setup/bootstrap.php rename to setup/includes/bootstrap.php index 3ecc30ee..c7a3960d 100644 --- a/setup/bootstrap.php +++ b/setup/includes/bootstrap.php @@ -4,7 +4,7 @@ */ // Define Base Directory -define('BASE_DIR', dirname(dirname(__FILE__))); +define('BASE_DIR', dirname(dirname(__DIR__))); // Register Composer Auto Loader require BASE_DIR.'/vendor/autoload.php'; @@ -29,40 +29,6 @@ $app = new Illuminate\Foundation\Application(BASE_DIR); // Set Container for Facades Illuminate\Support\Facades\Facade::setFacadeApplication($app); -// Register Basic Service Providers manually -(new Illuminate\View\ViewServiceProvider($app))->register(); -(new Illuminate\Foundation\Bootstrap\LoadConfiguration)->bootstrap($app); -(new Illuminate\Database\DatabaseServiceProvider($app))->register(); -(new Illuminate\Filesystem\FilesystemServiceProvider($app))->register(); - -$app['url'] = $app->share(function ($app) { - $routes = $app['router']->getRoutes(); - - // The URL generator needs the route collection that exists on the router. - // Keep in mind this is an object, so we're passing by references here - // and all the registered routes will be available to the generator. - $app->instance('routes', $routes); - - $request = Symfony\Component\HttpFoundation\Request::createFromGlobals(); - - $request = (new Illuminate\Http\Request)->duplicate( - $request->query->all(), $request->request->all(), $request->attributes->all(), - // quick fix: replace request URI with empty string - $request->cookies->all(), $request->files->all(), array_replace($request->server->all(), ['REQUEST_URI' => '']) - ); - - $url = new Illuminate\Routing\UrlGenerator( - $routes, $request - ); - - return $url; -}); - -$app->singleton('database', App\Services\Database\Database::class); -$app->singleton('option', App\Services\OptionRepository::class); - -require BASE_DIR.'/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php'; - // Load Aliases $config = require BASE_DIR.'/config/app.php'; @@ -70,18 +36,52 @@ foreach ($config['aliases'] as $facade => $class) { class_alias($class, $facade); } +// Register Basic Service Providers manually +(new Illuminate\View\ViewServiceProvider($app))->register(); +(new Illuminate\Foundation\Bootstrap\LoadConfiguration)->bootstrap($app); +(new Illuminate\Database\DatabaseServiceProvider($app))->register(); +(new Illuminate\Filesystem\FilesystemServiceProvider($app))->register(); +(new Illuminate\Session\SessionServiceProvider($app))->register(); +(new Illuminate\Encryption\EncryptionServiceProvider($app))->register(); + +$request = Symfony\Component\HttpFoundation\Request::createFromGlobals(); + +$request = (new Illuminate\Http\Request)->duplicate( + $request->query->all(), $request->request->all(), $request->attributes->all(), + // quick fix: replace request URI with empty string + $request->cookies->all(), $request->files->all(), array_replace($request->server->all(), ['REQUEST_URI' => '']) +); + +$app->bind('url', function ($app) { + $routes = $app['router']->getRoutes(); + + // The URL generator needs the route collection that exists on the router. + // Keep in mind this is an object, so we're passing by references here + // and all the registered routes will be available to the generator. + $app->instance('routes', $routes); + + $url = new Illuminate\Routing\UrlGenerator( + $routes, $app['request'] + ); + + return $url; +}); + +$app->instance('request', $request); + +$app->singleton('database', App\Services\Database\Database::class); +$app->singleton('option', App\Services\OptionRepository::class); + +require BASE_DIR.'/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php'; +require __DIR__."/helpers.php"; + + View::addExtension('tpl', 'blade'); -$config = require BASE_DIR.'/config/database.php'; - -$db_config = $config['connections']['mysql']; +$db_config = get_db_config(); // Check Database Config -@$conn = new mysqli($db_config['host'], $db_config['username'], $db_config['password'], $db_config['database'], $db_config['port']); - -if ($conn->connect_error) { - throw new App\Exceptions\E("无法连接至 MySQL 服务器,请检查你的配置:".$conn->connect_error, $conn->connect_errno, true); -} +check_db_config($db_config); // Boot Eloquent ORM $capsule = new Illuminate\Database\Capsule\Manager; @@ -91,57 +91,8 @@ $capsule->bootEloquent(); // Start Session session_start(); - -function checkTableExist() { - $tables = ['users', 'closets', 'players', 'textures', 'options']; - - foreach ($tables as $table_name) { - // prefix will be added automatically - if (!Database::hasTable($table_name)) { - return false; - } - } - - return true; -} - -function redirect_to($url, $msg = "") { - if ($msg !== "") { - if (app()->bound('session')) { - Session::flash('msg', $msg); - Session::save(); - } else { - $_SESSION['msg'] = $msg; - } - } - - if (!headers_sent()) { - header('Location: '.$url); - } else { - echo ""; - } - exit; -} - -/** - * Check POST values in a simple way - * - * @param array $keys - * @return void - */ -function check_post(Array $keys) { - foreach ($keys as $key) { - if (!isset($_POST[$key])) { - return false; - } - } - return true; -} - -function check_password($password) -{ - if (strlen($password) > 16 || strlen($password) < 8) { - return false; - } - return true; -} +// start laravel session +$encrypter = $app->make('Illuminate\Contracts\Encryption\Encrypter'); +$session = $app->make('session')->driver(); +$session->setId($encrypter->decrypt($_COOKIE['bs_session'])); +$session->start(); diff --git a/setup/includes/helpers.php b/setup/includes/helpers.php new file mode 100644 index 00000000..58cbc511 --- /dev/null +++ b/setup/includes/helpers.php @@ -0,0 +1,88 @@ +bound('session')) { + Session::put('msg', $msg); + Session::save(); + } else { + $_SESSION['msg'] = $msg; + } + } + + if (!headers_sent()) { + header('Location: '.$url); + } else { + echo ""; + } + exit; +} + +/** + * Check POST values in a simple way + * + * @param array $keys + * @return void + */ +function check_post(Array $keys) { + foreach ($keys as $key) { + if (!isset($_POST[$key])) { + return false; + } + } + return true; +} + +function check_password($password) +{ + if (strlen($password) > 16 || strlen($password) < 8) { + return false; + } + return true; +} + +function get_db_config() +{ + $config = require BASE_DIR.'/config/database.php'; + + return $config['connections']['mysql']; +} + +function check_db_config($config) +{ + @$conn = new mysqli($config['host'], $config['username'], $config['password'], $config['database'], $config['port']); + + if ($conn->connect_error) { + throw new App\Exceptions\PrettyPageException("无法连接至 MySQL 服务器,请检查你的配置:".$conn->connect_error, $conn->connect_errno, true); + } +} + +function migrate($migration) +{ + if (strpos($migration, 'import') !== false) { + $filename = BASE_DIR."/setup/migrations/".str_replace('-', '_', $migration).".php"; + if (file_exists($filename)) { + return require $filename; + } + } + throw new InvalidArgumentException('Non-existent migration'); +} diff --git a/setup/tables.php b/setup/includes/tables.php similarity index 100% rename from setup/tables.php rename to setup/includes/tables.php diff --git a/setup/index.php b/setup/index.php index 5bf7cf98..e0110c89 100644 --- a/setup/index.php +++ b/setup/index.php @@ -3,10 +3,10 @@ * Installation of Blessing Skin Server */ -require __DIR__."/bootstrap.php"; +require __DIR__."/includes/bootstrap.php"; // If already installed -if (checkTableExist()) { +if (check_table_exists()) { View::show('setup.locked'); exit; } @@ -53,7 +53,7 @@ switch ($step) { } // create tables - App\Services\Database\Migration::creatTables($db_config['prefix']); + require BASE_DIR."/includes/setup/tables.php"; // import options $options = require BASE_DIR."/config/options.php"; diff --git a/setup/migrations/import_v2_both.php b/setup/migrations/import_v2_both.php index cd2fc09d..8c2eed5d 100644 --- a/setup/migrations/import_v2_both.php +++ b/setup/migrations/import_v2_both.php @@ -3,7 +3,7 @@ * @Author: printempw * @Date: 2016-08-18 17:46:19 * @Last Modified by: printempw - * @Last Modified time: 2016-08-25 22:34:35 + * @Last Modified time: 2016-09-14 19:44:05 */ use App\Models\UserModel; @@ -14,7 +14,7 @@ use App\Models\Texture; if (!defined('BASE_DIR')) exit('Permission denied.'); $v2_table_name = $_POST['v2_table_name']; -$prefix = Config::getDbConfig()['prefix']; +$prefix = get_db_config()['prefix']; $v3_users = $prefix."users"; $v3_players = $prefix."players"; $v3_closets = $prefix."closets"; @@ -26,7 +26,7 @@ $texture_imported = 0; $texture_duplicated = 0; // use db helper instead of fat ORM in some operations :( -$db = DB::table($v2_table_name, true); +$db = Database::table($v2_table_name, true); $steps = ceil($db->getRecordNum() / 250); @@ -72,12 +72,14 @@ for ($i = 0; $i <= $steps; $i++) { if (!$res) { $t = new Texture; + // file size in bytes + $size = Storage::disk('textures')->has($row["hash_$model"]) ? Storage::disk('textures')->size($row["hash_$model"]) : 0; $t->name = $name; $t->type = $model; $t->likes = 1; $t->hash = $row["hash_$model"]; - $t->size = ceil(Storage::size(BASE_DIR.'/textures/'.$row["hash_$model"]) / 1024); + $t->size = ceil($size / 1024); $t->uploader = $user->uid; $t->public = $public; $t->upload_at = $row['last_modified'] ? : Utils::getTimeFormatted(); diff --git a/setup/migrations/import_v2_textures.php b/setup/migrations/import_v2_textures.php index b06f2ec6..e8b3fd92 100644 --- a/setup/migrations/import_v2_textures.php +++ b/setup/migrations/import_v2_textures.php @@ -3,7 +3,7 @@ * @Author: printempw * @Date: 2016-08-09 21:44:13 * @Last Modified by: printempw - * @Last Modified time: 2016-08-25 22:30:49 + * @Last Modified time: 2016-09-14 19:44:30 * * There are still some coupling relationships here but, * Just let it go :) @@ -12,13 +12,13 @@ if (!defined('BASE_DIR')) exit('Permission denied.'); $v2_table_name = $_POST['v2_table_name']; -$v3_table_name = Config::getDbConfig()['prefix']."textures"; +$v3_table_name = get_db_config()['prefix']."textures"; $imported = 0; $duplicated = 0; // use db helper instead of fat ORM -$db = DB::table($v2_table_name, true); +$db = Database::table($v2_table_name, true); $steps = ceil($db->getRecordNum() / 250); @@ -42,12 +42,15 @@ for ($i = 0; $i <= $steps; $i++) { $name = str_replace('{model}', $model, $name); if (!$db->has('hash', $row["hash_$model"], $v3_table_name)) { + // file size in bytes + $size = Storage::disk('textures')->has($row["hash_$model"]) ? Storage::disk('textures')->size($row["hash_$model"]) : 0; + $db->insert([ 'name' => $name, 'type' => $model, 'likes' => 0, 'hash' => $row["hash_$model"], - 'size' => ceil(Storage::size(BASE_DIR.'/textures/'.$row["hash_$model"]) / 1024), + 'size' => ceil($size / 1024), 'uploader' => $_POST['uploader_uid'], 'public' => $public, 'upload_at' => Utils::getTimeFormatted() @@ -65,6 +68,6 @@ for ($i = 0; $i <= $steps; $i++) { } return [ - 'imported' => $imported, + 'imported' => $imported, 'duplicated' => $duplicated ]; diff --git a/setup/migrations/import_v2_users.php b/setup/migrations/import_v2_users.php index 5653288a..bdf8eb07 100644 --- a/setup/migrations/import_v2_users.php +++ b/setup/migrations/import_v2_users.php @@ -3,13 +3,13 @@ * @Author: printempw * @Date: 2016-08-18 17:46:19 * @Last Modified by: printempw - * @Last Modified time: 2016-08-25 22:30:49 + * @Last Modified time: 2016-09-14 19:43:03 */ if (!defined('BASE_DIR')) exit('Permission denied.'); $v2_table_name = $_POST['v2_table_name']; -$prefix = Config::getDbConfig()['prefix']; +$prefix = get_db_config()['prefix']; $v3_users = $prefix."users"; $v3_players = $prefix."players"; $v3_closets = $prefix."closets"; @@ -18,7 +18,7 @@ $imported = 0; $duplicated = 0; // use db helper instead of fat ORM -$db = DB::table($v2_table_name, true); +$db = Database::table($v2_table_name, true); $steps = ceil($db->getRecordNum() / 250); diff --git a/setup/migrations/index.php b/setup/migrations/index.php index a1afe6dc..766e2424 100644 --- a/setup/migrations/index.php +++ b/setup/migrations/index.php @@ -3,11 +3,11 @@ * Migrations Bootstrap of Blessing Skin Server */ -require dirname(__DIR__)."/bootstrap.php"; +require dirname(__DIR__)."/includes/bootstrap.php"; // If already installed -if (!checkTableExist()) { - Http::redirect('../index.php'); +if (!check_table_exists()) { + redirect_to('../index.php'); } if (isset($_COOKIE['uid']) && isset($_COOKIE['token'])) { @@ -20,13 +20,13 @@ if (isset($_SESSION['uid'])) { $user = new App\Models\User($_SESSION['uid']); if ($_SESSION['token'] != $user->getToken()) - Http::redirect('../../auth/login', '无效的 token,请重新登录~'); + redirect_to('../../auth/login', '无效的 token,请重新登录~'); if ($user->getPermission() != "2") - Http::abort(403, '此页面仅超级管理员可访问'); + abort(403, '此页面仅超级管理员可访问'); } else { - Http::redirect('../../auth/login', '非法访问,请先登录'); + redirect_to('../../auth/login', '非法访问,请先登录'); } $action = isset($_GET['action']) ? $_GET['action'] : 'index'; @@ -49,8 +49,8 @@ switch ($action) { break; default: - throw new App\Exceptions\E('非法参数', 1, true); + throw new App\Exceptions\PrettyPageException('非法参数', 1, true); break; } - +Session::save(); diff --git a/setup/update.php b/setup/update.php index 7f826012..eca97251 100644 --- a/setup/update.php +++ b/setup/update.php @@ -1,9 +1,9 @@