2016-11-18 16:46:58 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2018-07-21 16:55:36 +08:00
|
|
|
use DB;
|
2017-01-02 13:27:53 +08:00
|
|
|
use Log;
|
2017-08-05 15:10:08 +08:00
|
|
|
use File;
|
2016-11-18 16:46:58 +08:00
|
|
|
use Option;
|
2019-03-02 22:58:37 +08:00
|
|
|
use Schema;
|
2016-11-18 16:46:58 +08:00
|
|
|
use Artisan;
|
2019-03-02 22:58:37 +08:00
|
|
|
use Storage;
|
2016-11-18 16:46:58 +08:00
|
|
|
use App\Models\User;
|
|
|
|
use Illuminate\Http\Request;
|
2018-06-30 16:31:39 +08:00
|
|
|
use Composer\Semver\Comparator;
|
2016-11-21 21:50:24 +08:00
|
|
|
use App\Exceptions\PrettyPageException;
|
2016-11-18 16:46:58 +08:00
|
|
|
|
|
|
|
class SetupController extends Controller
|
|
|
|
{
|
2019-04-09 13:21:31 +08:00
|
|
|
public function welcome()
|
|
|
|
{
|
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
if (! File::exists(base_path('.env'))) {
|
|
|
|
File::copy(base_path('.env.example'), base_path('.env'));
|
|
|
|
}
|
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
|
|
|
|
return view('setup.wizard.welcome');
|
|
|
|
}
|
|
|
|
|
2018-07-21 16:55:36 +08:00
|
|
|
public function database(Request $request)
|
2016-11-18 16:46:58 +08:00
|
|
|
{
|
2019-03-17 12:05:31 +08:00
|
|
|
if ($request->isMethod('get')) {
|
|
|
|
try {
|
|
|
|
DB::getPdo();
|
2019-04-19 19:36:36 +08:00
|
|
|
|
2019-03-17 12:05:31 +08:00
|
|
|
return redirect('setup/info');
|
2019-03-30 16:22:08 +08:00
|
|
|
// @codeCoverageIgnoreStart
|
2019-03-17 12:05:31 +08:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
return view('setup.wizard.database');
|
2019-03-30 16:22:08 +08:00
|
|
|
// @codeCoverageIgnoreEnd
|
2019-03-17 12:05:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-21 16:55:36 +08:00
|
|
|
config([
|
|
|
|
'database.connections.temp.driver' => $request->input('type'),
|
|
|
|
'database.connections.temp.host' => $request->input('host'),
|
|
|
|
'database.connections.temp.port' => $request->input('port'),
|
|
|
|
'database.connections.temp.username' => $request->input('username'),
|
|
|
|
'database.connections.temp.password' => $request->input('password'),
|
2019-04-09 13:21:31 +08:00
|
|
|
'database.connections.temp.database' => $request->input('db'),
|
|
|
|
'database.connections.temp.prefix' => $request->input('prefix'),
|
2018-07-21 16:55:36 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
try {
|
|
|
|
DB::connection('temp')->getPdo();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$msg = iconv('gbk', 'utf-8', $e->getMessage());
|
|
|
|
$type = humanize_db_type($request->input('type'));
|
|
|
|
|
|
|
|
throw new PrettyPageException(
|
|
|
|
trans('setup.database.connection-error', compact('msg', 'type')),
|
|
|
|
$e->getCode()
|
|
|
|
);
|
2018-02-22 21:26:23 +08:00
|
|
|
}
|
|
|
|
|
2019-03-31 10:33:03 +08:00
|
|
|
$content = File::get(base_path('.env'));
|
2019-04-09 13:21:31 +08:00
|
|
|
$content = preg_replace(
|
|
|
|
'/DB_CONNECTION.+/',
|
2018-07-21 16:55:36 +08:00
|
|
|
'DB_CONNECTION = '.$request->input('type'),
|
|
|
|
$content
|
|
|
|
);
|
2019-04-09 13:21:31 +08:00
|
|
|
$content = preg_replace(
|
|
|
|
'/DB_HOST.+/',
|
2018-07-21 16:55:36 +08:00
|
|
|
'DB_HOST = '.$request->input('host'),
|
|
|
|
$content
|
|
|
|
);
|
2019-04-09 13:21:31 +08:00
|
|
|
$content = preg_replace(
|
|
|
|
'/DB_PORT.+/',
|
2018-07-21 16:55:36 +08:00
|
|
|
'DB_PORT = '.$request->input('port'),
|
|
|
|
$content
|
|
|
|
);
|
2019-04-09 13:21:31 +08:00
|
|
|
$content = preg_replace(
|
|
|
|
'/DB_DATABASE.+/',
|
2018-07-21 16:55:36 +08:00
|
|
|
'DB_DATABASE = '.$request->input('db'),
|
|
|
|
$content
|
|
|
|
);
|
2019-04-09 13:21:31 +08:00
|
|
|
$content = preg_replace(
|
|
|
|
'/DB_USERNAME.+/',
|
2018-07-21 16:55:36 +08:00
|
|
|
'DB_USERNAME = '.$request->input('username'),
|
|
|
|
$content
|
|
|
|
);
|
2019-04-09 13:21:31 +08:00
|
|
|
$content = preg_replace(
|
|
|
|
'/DB_PASSWORD.+/',
|
2018-07-21 16:55:36 +08:00
|
|
|
'DB_PASSWORD = '.$request->input('password'),
|
|
|
|
$content
|
|
|
|
);
|
2019-04-09 13:21:31 +08:00
|
|
|
$content = preg_replace(
|
|
|
|
'/DB_PREFIX.+/',
|
2018-07-21 16:55:36 +08:00
|
|
|
'DB_PREFIX = '.$request->input('prefix'),
|
|
|
|
$content
|
|
|
|
);
|
2019-03-31 10:33:03 +08:00
|
|
|
File::put(base_path('.env'), $content);
|
2018-07-21 16:55:36 +08:00
|
|
|
|
|
|
|
return redirect('setup/info');
|
2016-11-18 16:46:58 +08:00
|
|
|
}
|
|
|
|
|
2017-01-02 13:27:53 +08:00
|
|
|
public function info()
|
|
|
|
{
|
2018-02-22 22:29:25 +08:00
|
|
|
$existingTables = static::checkTablesExist([], true);
|
|
|
|
|
|
|
|
// Not installed completely
|
|
|
|
if (count($existingTables) > 0) {
|
|
|
|
Log::info('Remaining tables detected, exit setup wizard now', [$existingTables]);
|
|
|
|
|
|
|
|
$existingTables = array_map(function ($item) {
|
|
|
|
return get_db_config()['prefix'].$item;
|
|
|
|
}, $existingTables);
|
|
|
|
|
|
|
|
throw new PrettyPageException(trans('setup.database.table-already-exists', ['tables' => json_encode($existingTables)]), 1);
|
|
|
|
}
|
|
|
|
|
2018-03-11 12:36:23 +08:00
|
|
|
// @codeCoverageIgnoreStart
|
2018-02-22 23:07:23 +08:00
|
|
|
if (! function_exists('escapeshellarg')) {
|
|
|
|
throw new PrettyPageException(trans('setup.disabled-functions.escapeshellarg'), 1);
|
|
|
|
}
|
2018-03-11 12:36:23 +08:00
|
|
|
// @codeCoverageIgnoreEnd
|
2018-02-22 23:07:23 +08:00
|
|
|
|
2017-01-02 13:27:53 +08:00
|
|
|
return view('setup.wizard.info');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function finish(Request $request)
|
|
|
|
{
|
2018-07-20 14:42:43 +08:00
|
|
|
$data = $this->validate($request, [
|
2017-01-02 13:27:53 +08:00
|
|
|
'email' => 'required|email',
|
2018-07-20 17:23:54 +08:00
|
|
|
'nickname' => 'required|no_special_chars|max:255',
|
2018-06-18 21:50:32 +08:00
|
|
|
'password' => 'required|min:8|max:32|confirmed',
|
2019-03-02 22:58:37 +08:00
|
|
|
'site_name' => 'required',
|
2017-01-02 13:27:53 +08:00
|
|
|
]);
|
|
|
|
|
2017-11-20 19:56:24 +08:00
|
|
|
if ($request->has('generate_random')) {
|
2019-03-19 10:13:57 +08:00
|
|
|
Artisan::call('key:random');
|
|
|
|
Artisan::call('salt:random');
|
2017-01-08 11:28:55 +08:00
|
|
|
}
|
2019-04-23 10:05:58 +08:00
|
|
|
Artisan::call('jwt:secret', ['--no-interaction' => true]);
|
2019-04-25 23:24:24 +08:00
|
|
|
Artisan::call('passport:keys', ['--no-interaction' => true]);
|
2017-01-08 11:28:55 +08:00
|
|
|
|
2018-02-22 22:29:25 +08:00
|
|
|
// Create tables
|
2017-01-02 13:27:53 +08:00
|
|
|
Artisan::call('migrate', ['--force' => true]);
|
2019-03-02 22:58:37 +08:00
|
|
|
Log::info('[SetupWizard] Tables migrated.');
|
2017-01-02 13:27:53 +08:00
|
|
|
|
|
|
|
Option::set('site_name', $request->input('site_name'));
|
2017-11-07 21:40:53 +08:00
|
|
|
|
|
|
|
$siteUrl = url('/');
|
|
|
|
|
|
|
|
if (ends_with($siteUrl, '/index.php')) {
|
2017-11-20 19:56:24 +08:00
|
|
|
$siteUrl = substr($siteUrl, 0, -10); // @codeCoverageIgnore
|
2017-11-07 21:40:53 +08:00
|
|
|
}
|
|
|
|
|
2019-03-02 22:58:37 +08:00
|
|
|
Option::set('site_url', $siteUrl);
|
2017-01-02 13:27:53 +08:00
|
|
|
|
2018-02-16 17:31:04 +08:00
|
|
|
// Register super admin
|
2018-07-20 14:42:43 +08:00
|
|
|
$user = new User;
|
|
|
|
$user->email = $data['email'];
|
2018-07-20 17:23:54 +08:00
|
|
|
$user->nickname = $data['nickname'];
|
2018-07-20 14:42:43 +08:00
|
|
|
$user->score = option('user_initial_score');
|
|
|
|
$user->avatar = 0;
|
|
|
|
$user->password = User::getEncryptedPwdFromEvent($data['password'], $user)
|
|
|
|
?: app('cipher')->hash($data['password'], config('secure.salt'));
|
2018-08-17 22:54:26 +08:00
|
|
|
$user->ip = get_client_ip();
|
2018-07-20 14:42:43 +08:00
|
|
|
$user->permission = User::SUPER_ADMIN;
|
2018-08-17 22:54:26 +08:00
|
|
|
$user->register_at = get_datetime_string();
|
|
|
|
$user->last_sign_at = get_datetime_string(time() - 86400);
|
2018-08-21 11:03:57 +08:00
|
|
|
$user->verified = true;
|
2018-07-20 14:42:43 +08:00
|
|
|
|
|
|
|
$user->save();
|
2017-01-02 13:27:53 +08:00
|
|
|
|
|
|
|
$this->createDirectories();
|
|
|
|
|
|
|
|
return view('setup.wizard.finish')->with([
|
|
|
|
'email' => $request->input('email'),
|
2019-03-02 22:58:37 +08:00
|
|
|
'password' => $request->input('password'),
|
2017-01-02 13:27:53 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2016-11-18 16:46:58 +08:00
|
|
|
public function update()
|
|
|
|
{
|
2018-06-30 16:31:39 +08:00
|
|
|
if (Comparator::lessThanOrEqualTo(config('app.version'), option('version'))) {
|
2018-02-16 17:31:04 +08:00
|
|
|
// No updates available
|
2016-11-18 16:46:58 +08:00
|
|
|
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
|
|
|
|
2019-03-02 22:58:37 +08:00
|
|
|
while ($filename = @readdir($resource)) {
|
|
|
|
if ($filename != '.' && $filename != '..') {
|
2016-11-18 16:46:58 +08:00
|
|
|
preg_match('/update-(.*)-to-(.*).php/', $filename, $matches);
|
|
|
|
|
2018-02-16 17:31:04 +08:00
|
|
|
// Skip if the file is not valid or expired
|
|
|
|
if (! isset($matches[2]) ||
|
2018-06-30 16:31:39 +08:00
|
|
|
Comparator::lessThan($matches[2], config('app.version'))) {
|
2016-11-18 16:46:58 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-05-03 08:46:15 +08:00
|
|
|
$tips = require database_path('update_scripts')."/$filename";
|
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) {
|
2018-02-16 17:31:04 +08:00
|
|
|
if (! Option::has($key)) {
|
2016-11-18 16:46:58 +08:00
|
|
|
Option::set($key, $value);
|
2018-02-16 17:31:04 +08:00
|
|
|
}
|
2016-11-18 16:46:58 +08:00
|
|
|
}
|
2019-04-22 22:56:24 +08:00
|
|
|
Option::set('version', config('app.version'));
|
2016-11-18 16:46:58 +08:00
|
|
|
|
2019-05-03 08:46:15 +08:00
|
|
|
Artisan::call('view:clear');
|
2017-01-13 22:34:06 +08:00
|
|
|
|
2019-05-03 23:44:04 +08:00
|
|
|
return view('setup.updates.success', ['tips' => $tips ?? []]);
|
2016-11-18 16:46:58 +08:00
|
|
|
}
|
|
|
|
|
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
|
2018-02-22 22:29:25 +08:00
|
|
|
* @param bool $returnExisting
|
|
|
|
* @return bool|array
|
2016-12-18 17:32:46 +08:00
|
|
|
*/
|
2019-02-18 16:35:39 +08:00
|
|
|
public static function checkTablesExist($tables = [], $returnExistingTables = false)
|
|
|
|
{
|
2018-02-22 22:29:25 +08:00
|
|
|
$existingTables = [];
|
2019-03-30 11:38:30 +08:00
|
|
|
$tables = $tables ?: [
|
|
|
|
'users',
|
|
|
|
'user_closet',
|
|
|
|
'players',
|
|
|
|
'textures',
|
|
|
|
'options',
|
|
|
|
'reports',
|
|
|
|
];
|
2018-02-16 17:31:04 +08:00
|
|
|
|
2016-12-31 16:43:23 +08:00
|
|
|
foreach ($tables as $tableName) {
|
|
|
|
if (Schema::hasTable($tableName)) {
|
2018-02-22 22:29:25 +08:00
|
|
|
$existingTables[] = $tableName;
|
2016-11-18 16:46:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-22 22:29:25 +08:00
|
|
|
if (count($existingTables) == count($tables)) {
|
2019-03-17 12:05:31 +08:00
|
|
|
return $returnExistingTables ? $existingTables : true;
|
2016-12-31 16:43:23 +08:00
|
|
|
} else {
|
2018-02-22 22:29:25 +08:00
|
|
|
return $returnExistingTables ? $existingTables : false;
|
2016-12-31 16:43:23 +08:00
|
|
|
}
|
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) {
|
2018-02-16 17:31:04 +08:00
|
|
|
if (! Storage::disk('root')->has($dir)) {
|
|
|
|
// Try to mkdir
|
|
|
|
if (! Storage::disk('root')->makeDirectory($dir)) {
|
2017-01-02 14:49:28 +08:00
|
|
|
return false;
|
2018-02-16 17:31:04 +08:00
|
|
|
}
|
2017-01-02 14:49:28 +08:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|