blessing-skin-server/app/Http/Controllers/SetupController.php

321 lines
10 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
2018-07-21 16:55:36 +08:00
use DB;
use Log;
2017-08-05 15:10:08 +08:00
use File;
use Option;
use Schema;
use Artisan;
use Storage;
use App\Models\User;
use Illuminate\Http\Request;
use Composer\Semver\Comparator;
2016-11-21 21:50:24 +08:00
use App\Exceptions\PrettyPageException;
class SetupController extends Controller
{
2018-07-21 16:55:36 +08:00
public function database(Request $request)
{
if ($request->isMethod('get')) {
try {
DB::getPdo();
2019-03-18 16:44:38 +08:00
$content = File::get('..'.DIRECTORY_SEPARATOR.'.env');
$content = str_replace('FIRST_RUN = true', '', $content);
File::put('..'.DIRECTORY_SEPARATOR.'.env', $content);
return redirect('setup/info');
} catch (\Exception $e) {
return view('setup.wizard.database');
}
}
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'),
'database.connections.temp.database' => $request->input('db') == '' ? null : $request->input('db'),
'database.connections.temp.prefix' => $request->input('prefix') == '' ? null : $request->input('prefix'),
]);
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
}
$content = File::get('..'.DIRECTORY_SEPARATOR.'.env');
2018-07-21 16:55:36 +08:00
$content = str_replace(
'DB_CONNECTION = '.env('DB_CONNECTION'),
'DB_CONNECTION = '.$request->input('type'),
$content
);
$content = str_replace(
'DB_HOST = '.env('DB_HOST'),
'DB_HOST = '.$request->input('host'),
$content
);
$content = str_replace(
'DB_PORT = '.env('DB_PORT'),
'DB_PORT = '.$request->input('port'),
$content
);
$content = str_replace(
'DB_DATABASE = '.env('DB_DATABASE'),
'DB_DATABASE = '.$request->input('db'),
$content
);
$content = str_replace(
'DB_USERNAME = '.env('DB_USERNAME'),
'DB_USERNAME = '.$request->input('username'),
$content
);
$content = str_replace(
'DB_PASSWORD = '.env('DB_PASSWORD'),
'DB_PASSWORD = '.$request->input('password'),
$content
);
$content = str_replace(
'DB_PREFIX = '.env('DB_PREFIX'),
'DB_PREFIX = '.$request->input('prefix'),
$content
);
2019-03-15 13:21:47 +08:00
$content = str_replace(
'FIRST_RUN = true',
'',
$content
);
File::put('..'.DIRECTORY_SEPARATOR.'.env', $content);
2018-07-21 16:55:36 +08:00
return redirect('setup/info');
}
public function info()
{
$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
return view('setup.wizard.info');
}
public function finish(Request $request)
{
$data = $this->validate($request, [
'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',
'site_name' => 'required',
]);
2017-11-20 19:56:24 +08:00
if ($request->has('generate_random')) {
2018-02-16 17:31:04 +08:00
// Generate new APP_KEY & SALT randomly
if (is_writable(app()->environmentFile())) {
Artisan::call('key:random');
Artisan::call('salt:random');
} else {
2017-11-20 19:56:24 +08:00
// @codeCoverageIgnoreStart
Log::warning('[SetupWizard] Failed to set application key. No write permission.');
2017-11-20 19:56:24 +08:00
// @codeCoverageIgnoreEnd
}
}
// Create tables
Artisan::call('migrate', ['--force' => true]);
Log::info('[SetupWizard] Tables migrated.');
Option::set('site_name', $request->input('site_name'));
$siteUrl = url('/');
if (ends_with($siteUrl, '/index.php')) {
2017-11-20 19:56:24 +08:00
$siteUrl = substr($siteUrl, 0, -10); // @codeCoverageIgnore
}
Option::set('site_url', $siteUrl);
2018-02-16 17:31:04 +08:00
// Register super admin
$user = new User;
$user->email = $data['email'];
2018-07-20 17:23:54 +08:00
$user->nickname = $data['nickname'];
$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();
$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;
$user->save();
$this->createDirectories();
return view('setup.wizard.finish')->with([
'email' => $request->input('email'),
'password' => $request->input('password'),
]);
}
public function update()
{
if (Comparator::lessThanOrEqualTo(config('app.version'), option('version'))) {
2018-02-16 17:31:04 +08:00
// No updates available
return view('setup.locked');
}
return view('setup.updates.welcome');
}
public function doUpdate()
{
$resource = opendir(database_path('update_scripts'));
$updateScriptExist = false;
$tips = [];
while ($filename = @readdir($resource)) {
if ($filename != '.' && $filename != '..') {
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]) ||
Comparator::lessThan($matches[2], config('app.version'))) {
continue;
}
$result = require database_path('update_scripts')."/$filename";
if (is_array($result)) {
2018-02-16 17:31:04 +08:00
// Push the tip into array
foreach ($result as $tip) {
$tips[] = $tip;
}
}
$updateScriptExist = true;
}
}
closedir($resource);
foreach (config('options') as $key => $value) {
2018-02-16 17:31:04 +08:00
if (! Option::has($key)) {
Option::set($key, $value);
2018-02-16 17:31:04 +08:00
}
}
2018-02-16 17:31:04 +08:00
if (! $updateScriptExist) {
// If there is no update script given
Option::set('version', config('app.version'));
}
2018-02-16 17:31:04 +08:00
// Clear all compiled view files
2017-08-05 15:10:08 +08:00
try {
Artisan::call('view:clear');
} catch (\Exception $e) {
2017-11-20 19:56:24 +08:00
Log::error('Error occured when processing view:clear', [$e]);
$files = collect(File::files(storage_path('framework/views')));
$files->reject(function ($path) {
return ends_with($path, '.gitignore');
})->each(function ($path) {
File::delete($path);
});
2017-08-05 15:10:08 +08:00
}
2017-01-13 22:34:06 +08:00
return view('setup.updates.success', ['tips' => $tips]);
}
/**
* Check if the given tables exist in current database.
*
* @param array $tables
* @param bool $returnExisting
* @return bool|array
*/
public static function checkTablesExist($tables = [], $returnExistingTables = false)
{
if (request()->is('setup') || request()->is('setup/database')) {
// We're ready to install Blessing Skin, so skip the checks.
try {
DB::connection(env('DB_CONNECTION'))->getPdo();
} catch (\Exception $e) {
return false;
}
}
2019-03-15 13:21:47 +08:00
// @codeCoverageIgnoreStart
if (env('FIRST_RUN') && request()->is('/')) {
return false;
}
// @codeCoverageIgnoreEnd
$existingTables = [];
2019-03-14 23:55:49 +08:00
$tables = $tables ?: ['users', 'user_closet', 'players', 'textures', 'options'];
2018-02-16 17:31:04 +08:00
foreach ($tables as $tableName) {
2018-02-16 17:31:04 +08:00
// Table prefix will be added automatically
if (Schema::hasTable($tableName)) {
$existingTables[] = $tableName;
}
}
if (count($existingTables) == count($tables)) {
return $returnExistingTables ? $existingTables : true;
} else {
return $returnExistingTables ? $existingTables : false;
}
}
public static function checkDirectories()
{
$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)) {
return false;
2018-02-16 17:31:04 +08:00
}
}
}
return true;
} catch (\Exception $e) {
return false;
}
}
protected function createDirectories()
{
return self::checkDirectories();
}
}