2016-11-18 16:46:58 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2017-01-02 13:27:53 +08:00
|
|
|
use Log;
|
2016-11-18 16:46:58 +08:00
|
|
|
use Utils;
|
|
|
|
use Schema;
|
|
|
|
use Option;
|
2016-11-21 21:50:24 +08:00
|
|
|
use Storage;
|
2016-11-18 16:46:58 +08:00
|
|
|
use Artisan;
|
|
|
|
use Database;
|
|
|
|
use App\Models\User;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Validation\Validator;
|
2016-11-21 21:50:24 +08:00
|
|
|
use App\Exceptions\PrettyPageException;
|
2016-11-18 16:46:58 +08:00
|
|
|
|
|
|
|
class SetupController extends Controller
|
|
|
|
{
|
|
|
|
public function welcome()
|
|
|
|
{
|
|
|
|
// already installed
|
2016-12-18 17:32:46 +08:00
|
|
|
if (self::checkTablesExist()) {
|
2016-11-18 16:46:58 +08:00
|
|
|
return view('setup.locked');
|
|
|
|
} else {
|
|
|
|
$config = config('database.connections.mysql');
|
|
|
|
|
|
|
|
return view('setup.wizard.welcome')->with('server', "{$config['username']}@{$config['host']}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-02 13:27:53 +08:00
|
|
|
public function info()
|
|
|
|
{
|
|
|
|
if (self::checkTablesExist()) {
|
|
|
|
return view('setup.locked');
|
|
|
|
}
|
|
|
|
|
|
|
|
return view('setup.wizard.info');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function finish(Request $request)
|
|
|
|
{
|
|
|
|
if (self::checkTablesExist()) {
|
|
|
|
return view('setup.locked');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->validate($request, [
|
|
|
|
'email' => 'required|email',
|
|
|
|
'password' => 'required|min:6|max:16|confirmed',
|
|
|
|
'site_name' => 'required'
|
|
|
|
]);
|
|
|
|
|
2017-01-08 11:28:55 +08:00
|
|
|
if (isset($_POST['generate_random'])) {
|
|
|
|
// generate new APP_KEY & SALT randomly
|
|
|
|
if (is_writable(app()->environmentFile())) {
|
|
|
|
Artisan::call('key:random');
|
|
|
|
Artisan::call('salt:random');
|
|
|
|
|
|
|
|
Log::info("[SetupWizard] Random application key & salt set successfully.", [
|
|
|
|
'key' => config('app.key'),
|
|
|
|
'salt' => config('secure.salt')
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
Log::warning("[SetupWizard] Failed to set application key. No write permission.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-02 13:27:53 +08:00
|
|
|
// create tables
|
|
|
|
Artisan::call('migrate', ['--force' => true]);
|
|
|
|
Log::info("[SetupWizard] Tables migrated.");
|
|
|
|
|
|
|
|
Option::set('site_name', $request->input('site_name'));
|
|
|
|
Option::set('site_url', url('/'));
|
|
|
|
|
|
|
|
// register super admin
|
|
|
|
$user = User::register(
|
|
|
|
$request->input('email'),
|
|
|
|
$request->input('password'), function ($user)
|
|
|
|
{
|
|
|
|
$user->ip = Utils::getClientIp();
|
|
|
|
$user->score = option('user_initial_score');
|
|
|
|
$user->register_at = Utils::getTimeFormatted();
|
|
|
|
$user->last_sign_at = Utils::getTimeFormatted(time() - 86400);
|
|
|
|
$user->permission = User::SUPER_ADMIN;
|
|
|
|
});
|
|
|
|
Log::info("[SetupWizard] Super Admin registered.", ['user' => $user]);
|
|
|
|
|
|
|
|
$this->createDirectories();
|
|
|
|
Log::info("[SetupWizard] Installation completed.");
|
|
|
|
|
|
|
|
return view('setup.wizard.finish')->with([
|
|
|
|
'email' => $request->input('email'),
|
|
|
|
'password' => $request->input('password')
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2016-11-18 16:46:58 +08:00
|
|
|
public function update()
|
|
|
|
{
|
2017-01-02 16:23:31 +08:00
|
|
|
if (Utils::versionCompare(config('app.version'), option('version', ''), '<=')) {
|
2016-11-18 16:46:58 +08:00
|
|
|
// no updates available
|
|
|
|
return view('setup.locked');
|
|
|
|
}
|
|
|
|
|
|
|
|
return view('setup.updates.welcome');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function doUpdate()
|
|
|
|
{
|
|
|
|
$resource = opendir(database_path('update_scripts'));
|
2016-12-18 17:32:46 +08:00
|
|
|
$updateScriptExist = false;
|
2016-11-18 16:46:58 +08:00
|
|
|
|
|
|
|
$tips = [];
|
|
|
|
|
|
|
|
while($filename = @readdir($resource)) {
|
|
|
|
if ($filename != "." && $filename != "..") {
|
|
|
|
preg_match('/update-(.*)-to-(.*).php/', $filename, $matches);
|
|
|
|
|
|
|
|
// skip if the file is not valid or expired
|
|
|
|
if (!isset($matches[2]) ||
|
2017-01-02 16:23:31 +08:00
|
|
|
Utils::versionCompare($matches[2], config('app.version'), '<')) {
|
2016-11-18 16:46:58 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = require database_path('update_scripts')."/$filename";
|
|
|
|
|
|
|
|
if (is_array($result)) {
|
|
|
|
// push tip to array
|
|
|
|
foreach ($result as $tip) {
|
|
|
|
$tips[] = $tip;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-18 17:32:46 +08:00
|
|
|
$updateScriptExist = true;
|
2016-11-18 16:46:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir($resource);
|
|
|
|
|
|
|
|
foreach (config('options') as $key => $value) {
|
|
|
|
if (!Option::has($key))
|
|
|
|
Option::set($key, $value);
|
|
|
|
}
|
|
|
|
|
2016-12-18 17:32:46 +08:00
|
|
|
if (!$updateScriptExist) {
|
2016-11-18 16:46:58 +08:00
|
|
|
// if update script is not given
|
|
|
|
Option::set('version', config('app.version'));
|
|
|
|
}
|
|
|
|
|
2017-01-13 22:34:06 +08:00
|
|
|
// clear all compiled view files
|
|
|
|
Artisan::call('view:clear');
|
|
|
|
|
2016-11-18 16:46:58 +08:00
|
|
|
return view('setup.updates.success', ['tips' => $tips]);
|
|
|
|
}
|
|
|
|
|
2016-12-18 17:32:46 +08:00
|
|
|
/**
|
|
|
|
* Check if the given tables exist in current database.
|
|
|
|
*
|
2016-12-31 16:43:23 +08:00
|
|
|
* @param array $tables
|
2016-12-18 17:32:46 +08:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function checkTablesExist($tables = [
|
|
|
|
'users', 'closets', 'players', 'textures', 'options'
|
2016-12-31 16:43:23 +08:00
|
|
|
]) {
|
|
|
|
$totalTables = 0;
|
|
|
|
|
|
|
|
foreach ($tables as $tableName) {
|
2016-11-18 16:46:58 +08:00
|
|
|
// prefix will be added automatically
|
2016-12-31 16:43:23 +08:00
|
|
|
if (Schema::hasTable($tableName)) {
|
|
|
|
$totalTables++;
|
2016-11-18 16:46:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-31 16:43:23 +08:00
|
|
|
if ($totalTables == count($tables)) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// not installed completely
|
|
|
|
foreach (array_merge($tables, ['migrations']) as $tableName) {
|
|
|
|
Schema::dropIfExists($tableName);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-18 16:46:58 +08:00
|
|
|
}
|
|
|
|
|
2017-01-02 14:49:28 +08:00
|
|
|
public static function checkDirectories()
|
2016-12-18 17:32:46 +08:00
|
|
|
{
|
2017-01-02 14:49:28 +08:00
|
|
|
$directories = ['storage/textures', 'plugins'];
|
|
|
|
|
|
|
|
try {
|
|
|
|
foreach ($directories as $dir) {
|
|
|
|
if (!Storage::disk('root')->has($dir)) {
|
|
|
|
// mkdir
|
|
|
|
if (!Storage::disk('root')->makeDirectory($dir))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-12-18 17:32:46 +08:00
|
|
|
|
2017-01-02 14:49:28 +08:00
|
|
|
return true;
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-18 17:32:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function createDirectories()
|
|
|
|
{
|
2017-01-02 14:49:28 +08:00
|
|
|
return self::checkDirectories();
|
2016-12-18 17:32:46 +08:00
|
|
|
}
|
|
|
|
|
2016-11-18 16:46:58 +08:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
protected function formatValidationErrors(Validator $validator)
|
|
|
|
{
|
|
|
|
return $validator->errors()->all();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|