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

170 lines
5.4 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
2019-12-14 11:10:37 +08:00
use App\Exceptions\PrettyPageException;
use App\Models\User;
2019-12-21 15:50:29 +08:00
use Carbon\Carbon;
2019-12-14 11:10:37 +08:00
use Illuminate\Contracts\Console\Kernel as Artisan;
2019-09-07 10:18:24 +08:00
use Illuminate\Database\Connection;
use Illuminate\Database\DatabaseManager;
2019-12-14 11:10:37 +08:00
use Illuminate\Filesystem\Filesystem;
use Illuminate\Http\Request;
2019-12-21 15:50:29 +08:00
use Illuminate\Support\Arr;
2019-12-14 11:10:37 +08:00
use Illuminate\Support\Str;
class SetupController extends Controller
{
2019-09-06 23:53:47 +08:00
public function welcome(Filesystem $filesystem)
2019-04-09 13:21:31 +08:00
{
2019-12-14 11:10:37 +08:00
if (!$filesystem->exists(base_path('.env'))) {
2019-09-06 23:53:47 +08:00
$filesystem->copy(base_path('.env.example'), base_path('.env'));
2019-04-09 13:21:31 +08:00
}
return view('setup.wizard.welcome');
}
2019-09-07 10:18:24 +08:00
public function database(
Request $request,
Filesystem $filesystem,
Connection $connection,
DatabaseManager $manager
) {
if ($request->isMethod('get')) {
try {
2019-09-07 10:18:24 +08:00
$connection->getPdo();
2019-04-19 19:36:36 +08:00
return redirect('setup/info');
} catch (\Exception $e) {
2019-09-19 22:13:25 +08:00
return view('setup.wizard.database', [
'host' => env('DB_HOST'),
'port' => env('DB_PORT'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'database' => env('DB_DATABASE'),
'prefix' => env('DB_PREFIX'),
]);
}
}
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 {
2019-09-07 10:18:24 +08:00
$manager->connection('temp')->getPdo();
2018-07-21 16:55:36 +08:00
} catch (\Exception $e) {
$msg = iconv('gbk', 'utf-8', $e->getMessage());
2019-12-21 15:50:29 +08:00
$type = Arr::get([
'mysql' => 'MySQL/MariaDB',
'sqlite' => 'SQLite',
'pgsql' => 'PostgreSQL',
], $request->input('type'), '');
2018-07-21 16:55:36 +08:00
2019-12-14 11:10:37 +08:00
throw new PrettyPageException(trans('setup.database.connection-error', compact('msg', 'type')), $e->getCode());
2018-02-22 21:26:23 +08:00
}
2019-09-07 10:18:24 +08:00
$content = $filesystem->get(base_path('.env'));
2019-04-09 13:21:31 +08:00
$content = preg_replace(
'/DB_CONNECTION.+/',
2019-09-12 19:06:18 +08:00
'DB_CONNECTION='.$request->input('type', ''),
2018-07-21 16:55:36 +08:00
$content
);
2019-04-09 13:21:31 +08:00
$content = preg_replace(
'/DB_HOST.+/',
2019-09-12 19:06:18 +08:00
'DB_HOST='.$request->input('host', ''),
2018-07-21 16:55:36 +08:00
$content
);
2019-04-09 13:21:31 +08:00
$content = preg_replace(
'/DB_PORT.+/',
2019-09-12 19:06:18 +08:00
'DB_PORT='.$request->input('port', ''),
2018-07-21 16:55:36 +08:00
$content
);
2019-04-09 13:21:31 +08:00
$content = preg_replace(
'/DB_DATABASE.+/',
2019-09-12 19:06:18 +08:00
'DB_DATABASE='.$request->input('db', ''),
2018-07-21 16:55:36 +08:00
$content
);
2019-04-09 13:21:31 +08:00
$content = preg_replace(
'/DB_USERNAME.+/',
2019-09-12 19:06:18 +08:00
'DB_USERNAME='.$request->input('username', ''),
2018-07-21 16:55:36 +08:00
$content
);
2019-04-09 13:21:31 +08:00
$content = preg_replace(
'/DB_PASSWORD.+/',
2019-09-12 19:06:18 +08:00
'DB_PASSWORD='.$request->input('password', ''),
2018-07-21 16:55:36 +08:00
$content
);
2019-04-09 13:21:31 +08:00
$content = preg_replace(
'/DB_PREFIX.+/',
2019-09-12 19:06:18 +08:00
'DB_PREFIX='.$request->input('prefix', ''),
2018-07-21 16:55:36 +08:00
$content
);
2019-09-07 10:18:24 +08:00
$filesystem->put(base_path('.env'), $content);
2018-07-21 16:55:36 +08:00
return redirect('setup/info');
}
2019-09-07 10:18:24 +08:00
public function finish(Request $request, Filesystem $filesystem, Artisan $artisan)
{
$data = $this->validate($request, [
2019-12-14 11:10:37 +08:00
'email' => 'required|email',
'nickname' => 'required',
2019-12-14 11:10:37 +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')) {
2019-09-07 10:18:24 +08:00
$artisan->call('key:generate');
$artisan->call('salt:random');
}
2019-09-07 10:18:24 +08:00
$artisan->call('jwt:secret', ['--no-interaction' => true]);
$artisan->call('passport:keys', ['--no-interaction' => true]);
// Create tables
2019-09-07 10:18:24 +08:00
$artisan->call('migrate', [
'--force' => true,
'--path' => [
'database/migrations',
'vendor/laravel/passport/database/migrations',
],
]);
$siteUrl = url('/');
2019-09-06 23:53:47 +08:00
if (Str::endsWith($siteUrl, '/index.php')) {
2017-11-20 19:56:24 +08:00
$siteUrl = substr($siteUrl, 0, -10); // @codeCoverageIgnore
}
2019-09-07 10:18:24 +08:00
option([
'site_name' => $request->input('site_name'),
'site_url' => $siteUrl,
]);
2018-02-16 17:31:04 +08:00
// Register super admin
2019-12-14 11:10:37 +08:00
$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;
2019-07-30 15:12:31 +08:00
$user->password = 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;
2019-12-21 15:50:29 +08:00
$user->register_at = Carbon::now();
$user->last_sign_at = Carbon::now()->subDay();
2018-08-21 11:03:57 +08:00
$user->verified = true;
$user->save();
2019-09-06 23:53:47 +08:00
$filesystem->put(storage_path('install.lock'), '');
return view('setup.wizard.finish')->with([
2019-09-19 22:13:25 +08:00
'email' => $request->input('email'),
]);
}
}