blue-twilight/app/Http/Controllers/InstallController.php

152 lines
5.2 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
use App\Configuration;
use App\Helpers\MiscHelper;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
class InstallController extends Controller
{
public function administrator(Request $request)
{
// Validate we're at the required stage
$stage = 3;
if (intval($request->session()->get('install_stage')) < $stage)
{
return redirect(route('install.check'));
}
if ($request->method() == 'POST')
{
$user = new User();
$user->name = $request->get('name');
$user->email = $request->get('email');
$user->password = bcrypt($request->get('password'));
$user->is_admin = true;
$user->is_activated = true;
$user->save();
return redirect(url('login'));
}
return view('install.administrator');
}
public function check(Request $request)
{
// This is the first installation step therefore it doesn't need to verify the stage
if ($request->getMethod() == 'POST')
{
$request->session()->set('install_stage', 2);
return redirect(route('install.database'));
}
$canContinue = true;
$requiredModules = [
'pdo_mysql' => 'installer.php_modules.mysql',
'gd' => 'installer.php_modules.gd'
];
$availableModules = [];
foreach ($requiredModules as $key => $langString)
{
$availableModules[$key] = extension_loaded($key);
if (!$availableModules[$key])
{
$canContinue = false;
}
}
$uploadLimit = MiscHelper::convertToBytes(ini_get('upload_max_filesize'));
$postMaxSize = MiscHelper::convertToBytes(ini_get('post_max_size'));
$recommendedMinimum = 4 * 1024 * 1024;
return view('install.check', [
'available_modules' => $availableModules,
'can_continue' => $canContinue,
'post_max_size' => ($postMaxSize / 1024 / 1024),
'post_max_size_warning' => $postMaxSize < $recommendedMinimum,
'recommended_minimum_upload' => ($recommendedMinimum / 1024 / 1024),
'upload_limit' => ($uploadLimit / 1024 / 1024),
'upload_limit_warning' => $uploadLimit < $recommendedMinimum,
'required_modules' => $requiredModules
]);
}
public function database(Request $request)
{
// Validate we're at the required stage
$stage = 2;
if (intval($request->session()->get('install_stage')) < $stage)
{
return redirect(route('install.check'));
}
if ($request->method() == 'POST')
{
$baseDirectory = dirname(dirname(dirname(__DIR__)));
$data = [
sprintf('APP_KEY=%s', config('app.key')),
'DB_CONNECTION=mysql',
sprintf('DB_HOST=%s', $request->get('mysql-server')),
sprintf('DB_PORT=%s', intval($request->get('mysql-port'))),
sprintf('DB_DATABASE=%s', $request->get('mysql-database')),
sprintf('DB_USERNAME=%s', $request->get('mysql-username')),
sprintf('DB_PASSWORD=%s', $request->get('mysql-password'))
];
file_put_contents(sprintf('%s/.env', $baseDirectory), join(PHP_EOL, $data) . PHP_EOL);
// Update the in-memory configuration ready for Artisan :)
$configData = [
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => $request->get('mysql-server'),
'port' => intval($request->get('mysql-port')),
'database' => $request->get('mysql-database'),
'username' => $request->get('mysql-username'),
'password' => $request->get('mysql-password')
]
]
];
config(['database' => array_replace_recursive(config('database'), $configData)]);
try
{
DB::reconnect();
Artisan::call('cache:clear');
Artisan::call('migrate', ['--force' => true]);
$result = Configuration::where('key', 'install_date')->first();
if (is_null($result))
{
$result = new Configuration();
$result->key = 'install_completed';
$result->value = true;
$result->save();
}
$request->session()->set('install_stage', 3);
return redirect(route('install.administrator'));
}
catch (\Exception $ex)
{
$request->session()->flash('database_error', $ex->getMessage());
return redirect(route('install.database'))
->withInput($request->input());
}
}
return view('install.database', [
'database_error' => $request->session()->get('database_error')
]);
}
}