Файл: app/Http/Controllers/InstallController.php
Строк: 410
<?php
namespace AppHttpControllers;
use AppHttpRequestsInstallDatabaseRequest;
use AppHttpRequestsInstallConfigRequest;
use AppModelsUser;
use GuzzleHttpClient as HttpClient;
use IlluminateHttpRequest;
use IlluminateSupportFacadesArtisan;
use IlluminateSupportFacadesDB;
use IlluminateSupportFacadesHash;
use IlluminateSupportStr;
class InstallController extends Controller
{
/**
* Show the Welcome page.
*
* @return IlluminateContractsFoundationApplication|IlluminateContractsViewFactory|IlluminateViewView
*/
public function index()
{
return view('install.container', ['view' => 'install.welcome']);
}
/**
* Show the Requirements page.
*
* @return IlluminateContractsFoundationApplication|IlluminateContractsViewFactory|IlluminateViewView
*/
public function requirements()
{
$requirements = config('install.extensions');
$results = [];
// Check the requirements
foreach ($requirements as $type => $extensions) {
if (strtolower($type) == 'php') {
foreach ($requirements[$type] as $extensions) {
$results['extensions'][$type][$extensions] = true;
if (!extension_loaded($extensions)) {
$results['extensions'][$type][$extensions] = false;
$results['errors'] = true;
}
}
} elseif (strtolower($type) == 'apache') {
foreach ($requirements[$type] as $extensions) {
// Check if the function exists
// Prevents from returning a false error
if (function_exists('apache_get_modules')) {
$results['extensions'][$type][$extensions] = true;
if (!in_array($extensions, apache_get_modules())) {
$results['extensions'][$type][$extensions] = false;
$results['errors'] = true;
}
}
}
}
}
// If the current php version doesn't meet the requirements
if (version_compare(PHP_VERSION, config('install.php_version')) == -1) {
$results['errors'] = true;
}
return view('install.container', ['view' => 'install.requirements', 'results' => $results]);
}
/**
* Show the Permissions page.
*
* @return IlluminateContractsFoundationApplication|IlluminateContractsViewFactory|IlluminateViewView
*/
public function permissions()
{
$permissions = config('install.permissions');
$results = [];
foreach ($permissions as $type => $files) {
foreach ($files as $file) {
if (is_writable(base_path($file))) {
$results['permissions'][$type][$file] = true;
} else {
$results['permissions'][$type][$file] = false;
$results['errors'] = true;
}
}
}
return view('install.container', ['view' => 'install.permissions', 'results' => $results]);
}
/**
* Show the Database configuration page.
*
* @return IlluminateContractsFoundationApplication|IlluminateContractsViewFactory|IlluminateViewView
*/
public function database()
{
return view('install.container', ['view' => 'install.database']);
}
/**
* Show the Admin credentials page.
*
* @return IlluminateContractsFoundationApplication|IlluminateContractsViewFactory|IlluminateViewView
*/
public function account()
{
return view('install.container', ['view' => 'install.account']);
}
/**
* Show the Complete page.
*
* @return IlluminateContractsFoundationApplication|IlluminateContractsViewFactory|IlluminateViewView
*/
public function complete()
{
return view('install.container', ['view' => 'install.complete']);
}
/**
* Validate the database credentials, and write the .env config file.
*
* @param InstallConfigRequest $request
* @return IlluminateHttpRedirectResponse
*/
public function storeConfig(InstallConfigRequest $request)
{
$validateDatabase = $this->validateDatabaseCredentials($request);
if ($validateDatabase !== true) {
return back()->with('error', __('Invalid database credentials. ' . $validateDatabase))->withInput();
}
$validateConfigFile = $this->writeEnvFile($request);
if ($validateConfigFile !== true) {
return back()->with('error', __('Unable to save .env file, check file permissions. ' . $validateConfigFile))->withInput();
}
return redirect()->route('install.account');
}
/**
* Migrate the database, and add the default admin user.
*
* @param InstallDatabaseRequest $request
* @return IlluminateHttpRedirectResponse
*/
public function storeDatabase(InstallDatabaseRequest $request)
{
$migrateDatabase = $this->migrateDatabase();
if ($migrateDatabase !== true) {
return back()->with('error', __('Failed to migrate the database. ' . $migrateDatabase))->withInput();
}
$createDefaultUser = $this->createDefaultUser($request);
if ($createDefaultUser !== true) {
return back()->with('error', __('Failed to create the default user. ' . $createDefaultUser))->withInput();
}
$saveInstalledFile = $this->writeEnvInstalledStatus();
if ($saveInstalledFile !== true) {
return back()->with('error', __('Failed to finalize the installation. ' . $saveInstalledFile))->withInput();
}
if ($request->input('newsletter')) {
$this->subscribeNewsletter($request);
}
return redirect()->route('install.complete');
}
/**
* Validate the database credentials.
*
* @return bool|string
*/
private function validateDatabaseCredentials(Request $request)
{
$settings = config("database.connections.mysql");
config([
'database' => [
'default' => 'mysql',
'connections' => [
'mysql' => array_merge($settings, [
'driver' => 'mysql',
'host' => $request->input('database_hostname'),
'port' => $request->input('database_port'),
'database' => $request->input('database_name'),
'username' => $request->input('database_username'),
'password' => $request->input('database_password'),
]),
],
],
]);
DB::purge();
try {
DB::connection()->getPdo();
return true;
} catch (Exception $e) {
return $e->getMessage();
}
}
/**
* Migrate the database.
*
* @return bool|string
*/
private function migrateDatabase()
{
try {
Artisan::call('migrate', ['--force' => true]);
return true;
} catch (Exception $e) {
return $e->getMessage();
}
}
/**
* Create the default admin user.
*
* @return bool|string
*/
private function createDefaultUser(Request $request)
{
try {
$user = new User;
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->password = Hash::make($request->input('password'));
$user->locale = config('app.locale');
$user->role = 1;
$user->timezone = 'UTC';
$user->api_token = Str::random(64);
$user->save();
$user->markEmailAsVerified();
} catch (Exception $e) {
return $e->getMessage();
}
return true;
}
/**
* Write the .env file.
*
* @return bool|string
*/
private function writeEnvFile(Request $request)
{
$config =
"APP_NAME='".config('info.software.name')."'n".
"APP_ENV=productionn".
"APP_KEY=base64:'".base64_encode(Str::random(32))."'n".
"APP_DEBUG=falsen".
"APP_URL='".route('home')."'n".
"n".
"LOG_CHANNEL=stackn".
"n".
"DB_CONNECTION=mysqln".
"DB_HOST='".$request->input('database_hostname')."'n".
"DB_PORT=".$request->input('database_port')."n".
"DB_DATABASE='".$request->input('database_name')."'n".
"DB_USERNAME='".$request->input('database_username')."'n".
"DB_PASSWORD='".$request->input('database_password')."'n".
"n".
"BROADCAST_DRIVER=logn".
"CACHE_DRIVER=filen".
"QUEUE_CONNECTION=syncn".
"SESSION_DRIVER=filen".
"SESSION_LIFETIME=120n".
"n".
"REDIS_HOST=127.0.0.1n".
"REDIS_PASSWORD=nulln".
"REDIS_PORT=6379n".
"n".
"MAIL_DRIVER=smtpn".
"MAIL_HOST=smtp.mailtrap.ion".
"MAIL_PORT=2525n".
"MAIL_USERNAME=nulln".
"MAIL_PASSWORD=nulln".
"MAIL_ENCRYPTION=nulln".
"MAIL_FROM_ADDRESS=nulln".
"MAIL_FROM_NAME="${APP_NAME}"n".
"n".
"AWS_ACCESS_KEY_ID=n".
"AWS_SECRET_ACCESS_KEY=n".
"AWS_DEFAULT_REGION=us-east-1n".
"AWS_BUCKET=n".
"n".
"PUSHER_APP_ID=n".
"PUSHER_APP_KEY=n".
"PUSHER_APP_SECRET=n".
"PUSHER_APP_CLUSTER=mt1n".
"n".
"MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"n".
"MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"";
try {
file_put_contents(base_path('.env'), $config);
} catch (Exception $e) {
return $e->getMessage();
}
return true;
}
/**
* Write the installed status to the .env file.
*
* @return bool|string
*/
private function writeEnvInstalledStatus()
{
try {
file_put_contents(base_path('.env'), "nnAPP_INSTALLED=true", FILE_APPEND);
} catch (Exception $e) {
return $e->getMessage();
}
return true;
}
/**
* Subscribe to the newsletter.
*
* @throws GuzzleHttpExceptionGuzzleException
*/
private function subscribeNewsletter(Request $request)
{
$httpClient = new HttpClient(['timeout' => 10, 'verify' => false]);
try {
$httpClient->request('POST', 'https://api.lunatio.com/newsletter',
[
'form_params' => [
'email' => $request->input('email')
]
]
);
} catch (Exception $e) {}
}
}