fix bootstrap of setup and migration

This commit is contained in:
printempw 2016-09-14 19:57:24 +08:00
parent a5a48789c5
commit b816664428
12 changed files with 179 additions and 136 deletions

View File

@ -47,9 +47,8 @@
</tr> </tr>
</table> </table>
@if (Session::has('msg'))
@if (isset($_SESSION['msg'])) <div class="alert alert-warning" role="alert">{{ Session::pull('msg') }}</div>
<div class="alert alert-warning" role="alert">{{ $_SESSION['msg'] }}</div> <?php unset($_SESSION['msg']); ?>
@endif @endif
<p class="step"> <p class="step">
@ -63,7 +62,7 @@
@if ($step == '2') @if ($step == '2')
<?php <?php
if (Validate::checkPost(['v2_table_name', 'texture_name_pattern'], true)) { if (check_post(['v2_table_name', 'texture_name_pattern'], true)) {
if ($_POST['v2_table_name'] == "") { if ($_POST['v2_table_name'] == "") {
redirect_to('index.php?action=import-v2-both&step=1', 'v2 users 表名不能为空'); redirect_to('index.php?action=import-v2-both&step=1', 'v2 users 表名不能为空');
} else { } else {
@ -82,7 +81,7 @@
<h1>导入成功</h1> <h1>导入成功</h1>
<?php $result = Migration::importV2Both(); ?> <?php $result = migrate('import-v2-both'); ?>
<p>已导入 {{ $result['user']['imported'] }} 个用户,{{ $result['user']['duplicated'] }} 个用户因重复而未导入。</p> <p>已导入 {{ $result['user']['imported'] }} 个用户,{{ $result['user']['duplicated'] }} 个用户因重复而未导入。</p>
<p>已导入 {{ $result['texture']['imported'] }} 个材质到皮肤库,{{ $result['texture']['duplicated'] }} 个材质因重复而未导入。</p> <p>已导入 {{ $result['texture']['imported'] }} 个材质到皮肤库,{{ $result['texture']['duplicated'] }} 个材质因重复而未导入。</p>

View File

@ -56,8 +56,8 @@
</tr> </tr>
</table> </table>
@if (isset($_SESSION['msg'])) @if (Session::has('msg'))
<div class="alert alert-warning" role="alert">{{ $_SESSION['msg'] }}</div> <?php unset($_SESSION['msg']); ?> <div class="alert alert-warning" role="alert">{{ Session::pull('msg') }}</div>
@endif @endif
<p class="step"> <p class="step">
@ -71,7 +71,7 @@
@if ($step == '2') @if ($step == '2')
<?php <?php
if (Validate::checkPost(['v2_table_name', 'uploader_uid', 'texture_name_pattern'], true)) { if (check_post(['v2_table_name', 'uploader_uid', 'texture_name_pattern'], true)) {
if ($_POST['v2_table_name'] == "") { if ($_POST['v2_table_name'] == "") {
redirect_to('index.php?action=import-v2-textures&step=1', 'v2 users 表名不能为空'); redirect_to('index.php?action=import-v2-textures&step=1', 'v2 users 表名不能为空');
} else { } else {
@ -91,7 +91,7 @@
<h1>导入成功</h1> <h1>导入成功</h1>
<?php $result = Migration::importV2Textures(); ?> <?php $result = migrate('import-v2-textures'); ?>
<p>已导入 {{ $result['imported'] }} 个材质到皮肤库,{{ $result['duplicated'] }} 个材质因重复而未导入。</p> <p>已导入 {{ $result['imported'] }} 个材质到皮肤库,{{ $result['duplicated'] }} 个材质因重复而未导入。</p>
<p>注意:请将 v2 textures 文件夹内容复制到 v3 textures 文件夹中</p> <p>注意:请将 v2 textures 文件夹内容复制到 v3 textures 文件夹中</p>

View File

@ -26,8 +26,8 @@
</tr> </tr>
</table> </table>
@if (isset($_SESSION['msg'])) @if (Session::has('msg'))
<div class="alert alert-warning" role="alert">{{ $_SESSION['msg'] }}</div> <?php unset($_SESSION['msg']); ?> <div class="alert alert-warning" role="alert">{{ Session::pull('msg') }}</div>
@endif @endif
<p class="step"> <p class="step">
@ -41,7 +41,7 @@
@if ($step == '2') @if ($step == '2')
<?php <?php
if (Validate::checkPost(['v2_table_name'], true)) { if (check_post(['v2_table_name'], true)) {
if ($_POST['v2_table_name'] == "") { if ($_POST['v2_table_name'] == "") {
redirect_to('index.php?action=import-v2-users&step=1', 'v2 users 表名不能为空'); redirect_to('index.php?action=import-v2-users&step=1', 'v2 users 表名不能为空');
} else { } else {
@ -59,7 +59,7 @@
<h1>导入成功</h1> <h1>导入成功</h1>
<?php $result = Migration::importV2Users(); ?> <?php $result = migrate('import-v2-users'); ?>
<p>已导入 {{ $result['imported'] }} 个用户,{{ $result['duplicated'] }} 个用户因重复而未导入。</p> <p>已导入 {{ $result['imported'] }} 个用户,{{ $result['duplicated'] }} 个用户因重复而未导入。</p>

View File

@ -4,7 +4,7 @@
*/ */
// Define Base Directory // Define Base Directory
define('BASE_DIR', dirname(dirname(__FILE__))); define('BASE_DIR', dirname(dirname(__DIR__)));
// Register Composer Auto Loader // Register Composer Auto Loader
require BASE_DIR.'/vendor/autoload.php'; require BASE_DIR.'/vendor/autoload.php';
@ -29,40 +29,6 @@ $app = new Illuminate\Foundation\Application(BASE_DIR);
// Set Container for Facades // Set Container for Facades
Illuminate\Support\Facades\Facade::setFacadeApplication($app); 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 // Load Aliases
$config = require BASE_DIR.'/config/app.php'; $config = require BASE_DIR.'/config/app.php';
@ -70,18 +36,52 @@ foreach ($config['aliases'] as $facade => $class) {
class_alias($class, $facade); 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'); View::addExtension('tpl', 'blade');
$config = require BASE_DIR.'/config/database.php'; $db_config = get_db_config();
$db_config = $config['connections']['mysql'];
// Check Database Config // Check Database Config
@$conn = new mysqli($db_config['host'], $db_config['username'], $db_config['password'], $db_config['database'], $db_config['port']); check_db_config($db_config);
if ($conn->connect_error) {
throw new App\Exceptions\E("无法连接至 MySQL 服务器,请检查你的配置:".$conn->connect_error, $conn->connect_errno, true);
}
// Boot Eloquent ORM // Boot Eloquent ORM
$capsule = new Illuminate\Database\Capsule\Manager; $capsule = new Illuminate\Database\Capsule\Manager;
@ -91,57 +91,8 @@ $capsule->bootEloquent();
// Start Session // Start Session
session_start(); session_start();
// start laravel session
function checkTableExist() { $encrypter = $app->make('Illuminate\Contracts\Encryption\Encrypter');
$tables = ['users', 'closets', 'players', 'textures', 'options']; $session = $app->make('session')->driver();
$session->setId($encrypter->decrypt($_COOKIE['bs_session']));
foreach ($tables as $table_name) { $session->start();
// 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 "<meta http-equiv='Refresh' content='0; URL=$url'>";
}
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;
}

View File

@ -0,0 +1,88 @@
<?php
/**
* @Author: printempw
* @Date: 2016-09-14 16:57:37
* @Last Modified by: printempw
* @Last Modified time: 2016-09-14 19:53:22
*/
function check_table_exists() {
$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::put('msg', $msg);
Session::save();
} else {
$_SESSION['msg'] = $msg;
}
}
if (!headers_sent()) {
header('Location: '.$url);
} else {
echo "<meta http-equiv='Refresh' content='0; URL=$url'>";
}
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');
}

View File

@ -3,10 +3,10 @@
* Installation of Blessing Skin Server * Installation of Blessing Skin Server
*/ */
require __DIR__."/bootstrap.php"; require __DIR__."/includes/bootstrap.php";
// If already installed // If already installed
if (checkTableExist()) { if (check_table_exists()) {
View::show('setup.locked'); View::show('setup.locked');
exit; exit;
} }
@ -53,7 +53,7 @@ switch ($step) {
} }
// create tables // create tables
App\Services\Database\Migration::creatTables($db_config['prefix']); require BASE_DIR."/includes/setup/tables.php";
// import options // import options
$options = require BASE_DIR."/config/options.php"; $options = require BASE_DIR."/config/options.php";

View File

@ -3,7 +3,7 @@
* @Author: printempw * @Author: printempw
* @Date: 2016-08-18 17:46:19 * @Date: 2016-08-18 17:46:19
* @Last Modified by: printempw * @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; use App\Models\UserModel;
@ -14,7 +14,7 @@ use App\Models\Texture;
if (!defined('BASE_DIR')) exit('Permission denied.'); if (!defined('BASE_DIR')) exit('Permission denied.');
$v2_table_name = $_POST['v2_table_name']; $v2_table_name = $_POST['v2_table_name'];
$prefix = Config::getDbConfig()['prefix']; $prefix = get_db_config()['prefix'];
$v3_users = $prefix."users"; $v3_users = $prefix."users";
$v3_players = $prefix."players"; $v3_players = $prefix."players";
$v3_closets = $prefix."closets"; $v3_closets = $prefix."closets";
@ -26,7 +26,7 @@ $texture_imported = 0;
$texture_duplicated = 0; $texture_duplicated = 0;
// use db helper instead of fat ORM in some operations :( // 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); $steps = ceil($db->getRecordNum() / 250);
@ -72,12 +72,14 @@ for ($i = 0; $i <= $steps; $i++) {
if (!$res) { if (!$res) {
$t = new Texture; $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->name = $name;
$t->type = $model; $t->type = $model;
$t->likes = 1; $t->likes = 1;
$t->hash = $row["hash_$model"]; $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->uploader = $user->uid;
$t->public = $public; $t->public = $public;
$t->upload_at = $row['last_modified'] ? : Utils::getTimeFormatted(); $t->upload_at = $row['last_modified'] ? : Utils::getTimeFormatted();

View File

@ -3,7 +3,7 @@
* @Author: printempw * @Author: printempw
* @Date: 2016-08-09 21:44:13 * @Date: 2016-08-09 21:44:13
* @Last Modified by: printempw * @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, * There are still some coupling relationships here but,
* Just let it go :) * Just let it go :)
@ -12,13 +12,13 @@
if (!defined('BASE_DIR')) exit('Permission denied.'); if (!defined('BASE_DIR')) exit('Permission denied.');
$v2_table_name = $_POST['v2_table_name']; $v2_table_name = $_POST['v2_table_name'];
$v3_table_name = Config::getDbConfig()['prefix']."textures"; $v3_table_name = get_db_config()['prefix']."textures";
$imported = 0; $imported = 0;
$duplicated = 0; $duplicated = 0;
// use db helper instead of fat ORM // 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); $steps = ceil($db->getRecordNum() / 250);
@ -42,12 +42,15 @@ for ($i = 0; $i <= $steps; $i++) {
$name = str_replace('{model}', $model, $name); $name = str_replace('{model}', $model, $name);
if (!$db->has('hash', $row["hash_$model"], $v3_table_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([ $db->insert([
'name' => $name, 'name' => $name,
'type' => $model, 'type' => $model,
'likes' => 0, 'likes' => 0,
'hash' => $row["hash_$model"], 'hash' => $row["hash_$model"],
'size' => ceil(Storage::size(BASE_DIR.'/textures/'.$row["hash_$model"]) / 1024), 'size' => ceil($size / 1024),
'uploader' => $_POST['uploader_uid'], 'uploader' => $_POST['uploader_uid'],
'public' => $public, 'public' => $public,
'upload_at' => Utils::getTimeFormatted() 'upload_at' => Utils::getTimeFormatted()
@ -65,6 +68,6 @@ for ($i = 0; $i <= $steps; $i++) {
} }
return [ return [
'imported' => $imported, 'imported' => $imported,
'duplicated' => $duplicated 'duplicated' => $duplicated
]; ];

View File

@ -3,13 +3,13 @@
* @Author: printempw * @Author: printempw
* @Date: 2016-08-18 17:46:19 * @Date: 2016-08-18 17:46:19
* @Last Modified by: printempw * @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.'); if (!defined('BASE_DIR')) exit('Permission denied.');
$v2_table_name = $_POST['v2_table_name']; $v2_table_name = $_POST['v2_table_name'];
$prefix = Config::getDbConfig()['prefix']; $prefix = get_db_config()['prefix'];
$v3_users = $prefix."users"; $v3_users = $prefix."users";
$v3_players = $prefix."players"; $v3_players = $prefix."players";
$v3_closets = $prefix."closets"; $v3_closets = $prefix."closets";
@ -18,7 +18,7 @@ $imported = 0;
$duplicated = 0; $duplicated = 0;
// use db helper instead of fat ORM // 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); $steps = ceil($db->getRecordNum() / 250);

View File

@ -3,11 +3,11 @@
* Migrations Bootstrap of Blessing Skin Server * Migrations Bootstrap of Blessing Skin Server
*/ */
require dirname(__DIR__)."/bootstrap.php"; require dirname(__DIR__)."/includes/bootstrap.php";
// If already installed // If already installed
if (!checkTableExist()) { if (!check_table_exists()) {
Http::redirect('../index.php'); redirect_to('../index.php');
} }
if (isset($_COOKIE['uid']) && isset($_COOKIE['token'])) { if (isset($_COOKIE['uid']) && isset($_COOKIE['token'])) {
@ -20,13 +20,13 @@ if (isset($_SESSION['uid'])) {
$user = new App\Models\User($_SESSION['uid']); $user = new App\Models\User($_SESSION['uid']);
if ($_SESSION['token'] != $user->getToken()) if ($_SESSION['token'] != $user->getToken())
Http::redirect('../../auth/login', '无效的 token请重新登录~'); redirect_to('../../auth/login', '无效的 token请重新登录~');
if ($user->getPermission() != "2") if ($user->getPermission() != "2")
Http::abort(403, '此页面仅超级管理员可访问'); abort(403, '此页面仅超级管理员可访问');
} else { } else {
Http::redirect('../../auth/login', '非法访问,请先登录'); redirect_to('../../auth/login', '非法访问,请先登录');
} }
$action = isset($_GET['action']) ? $_GET['action'] : 'index'; $action = isset($_GET['action']) ? $_GET['action'] : 'index';
@ -49,8 +49,8 @@ switch ($action) {
break; break;
default: default:
throw new App\Exceptions\E('非法参数', 1, true); throw new App\Exceptions\PrettyPageException('非法参数', 1, true);
break; break;
} }
Session::save();

View File

@ -1,9 +1,9 @@
<?php <?php
/** /**
* Migrations Bootstrap of Blessing Skin Server * Update Bootstrap of Blessing Skin Server
*/ */
require __DIR__."/bootstrap.php"; require __DIR__."/includes/bootstrap.php";
// If no update is available // If no update is available
if (config('app.version') == Option::get('version', '')) { if (config('app.version') == Option::get('version', '')) {
@ -51,7 +51,7 @@ switch ($step) {
break; break;
default: default:
throw new App\Exceptions\E('非法参数', 1, true); throw new App\Exceptions\PrettyPageException('非法参数', 1, true);
break; break;
} }