2016-11-18 16:46:58 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2019-12-14 11:10:37 +08:00
|
|
|
use App\Exceptions\PrettyPageException;
|
2016-11-18 16:46:58 +08:00
|
|
|
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;
|
2016-11-18 16:46:58 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
) {
|
2019-03-17 12:05:31 +08:00
|
|
|
if ($request->isMethod('get')) {
|
|
|
|
try {
|
2019-09-07 10:18:24 +08:00
|
|
|
$connection->getPdo();
|
2019-04-19 19:36:36 +08:00
|
|
|
|
2019-03-17 12:05:31 +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'),
|
|
|
|
]);
|
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 {
|
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');
|
2016-11-18 16:46:58 +08:00
|
|
|
}
|
|
|
|
|
2019-09-07 10:18:24 +08:00
|
|
|
public function finish(Request $request, Filesystem $filesystem, Artisan $artisan)
|
2017-01-02 13:27:53 +08:00
|
|
|
{
|
2018-07-20 14:42:43 +08:00
|
|
|
$data = $this->validate($request, [
|
2019-12-14 11:10:37 +08:00
|
|
|
'email' => 'required|email',
|
2019-12-22 10:46:10 +08:00
|
|
|
'nickname' => 'required',
|
2019-12-14 11:10:37 +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-09-07 10:18:24 +08:00
|
|
|
$artisan->call('key:generate');
|
|
|
|
$artisan->call('salt:random');
|
2017-01-08 11:28:55 +08:00
|
|
|
}
|
2019-09-07 10:18:24 +08:00
|
|
|
$artisan->call('jwt:secret', ['--no-interaction' => true]);
|
|
|
|
$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
|
2019-09-07 10:18:24 +08:00
|
|
|
$artisan->call('migrate', [
|
2019-06-30 23:25:48 +08:00
|
|
|
'--force' => true,
|
|
|
|
'--path' => [
|
|
|
|
'database/migrations',
|
2019-08-15 23:27:29 +08:00
|
|
|
'vendor/laravel/passport/database/migrations',
|
|
|
|
],
|
2019-06-30 23:25:48 +08:00
|
|
|
]);
|
2017-11-07 21:40:53 +08:00
|
|
|
|
|
|
|
$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
|
2017-11-07 21:40:53 +08:00
|
|
|
}
|
2019-09-07 10:18:24 +08:00
|
|
|
option([
|
|
|
|
'site_name' => $request->input('site_name'),
|
|
|
|
'site_url' => $siteUrl,
|
|
|
|
]);
|
2017-01-02 13:27:53 +08:00
|
|
|
|
2018-02-16 17:31:04 +08:00
|
|
|
// Register super admin
|
2019-12-14 11:10:37 +08:00
|
|
|
$user = new User();
|
2018-07-20 14:42:43 +08:00
|
|
|
$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;
|
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();
|
2018-07-20 14:42:43 +08:00
|
|
|
$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;
|
2018-07-20 14:42:43 +08:00
|
|
|
|
|
|
|
$user->save();
|
2017-01-02 13:27:53 +08:00
|
|
|
|
2019-09-06 23:53:47 +08:00
|
|
|
$filesystem->put(storage_path('install.lock'), '');
|
2017-01-02 13:27:53 +08:00
|
|
|
|
|
|
|
return view('setup.wizard.finish')->with([
|
2019-09-19 22:13:25 +08:00
|
|
|
'email' => $request->input('email'),
|
2017-01-02 13:27:53 +08:00
|
|
|
]);
|
|
|
|
}
|
2016-11-18 16:46:58 +08:00
|
|
|
}
|