Attendize/app/Http/Controllers/InstallerController.php

186 lines
5.9 KiB
PHP
Raw Normal View History

2016-03-05 00:18:10 +00:00
<?php
2016-02-29 15:59:36 +00:00
2016-03-05 00:18:10 +00:00
namespace App\Http\Controllers;
use App\Models\Timezone;
use Artisan;
2016-02-29 15:59:36 +00:00
use Config;
2016-03-05 00:18:10 +00:00
use DB;
use Illuminate\Http\Request;
2016-02-29 15:59:36 +00:00
class InstallerController extends Controller
{
/**
* InstallerController constructor.
*/
2016-02-29 15:59:36 +00:00
public function __construct()
{
/**
* If we're already installed kill the request
* @todo Check if DB is installed etc.
*/
2016-03-05 00:18:10 +00:00
if (file_exists(base_path('installed'))) {
abort(403, 'Unauthorized action.');
}
2016-02-29 15:59:36 +00:00
}
/**
* Show the application installer
*
* @return mixed
*/
2016-02-29 15:59:36 +00:00
public function showInstaller()
{
/*
* Path we need to make sure are writable
*/
2016-02-29 15:59:36 +00:00
$data['paths'] = [
storage_path('app'),
storage_path('framework'),
storage_path('logs'),
public_path(config('attendize.event_images_path')),
public_path(config('attendize.organiser_images_path')),
public_path(config('attendize.event_pdf_tickets_path')),
base_path('bootstrap/cache'),
2016-03-05 00:18:10 +00:00
base_path('.env'),
2016-05-21 18:23:18 +00:00
base_path(),
2016-02-29 15:59:36 +00:00
];
/*
* Required PHP extensions
*/
2016-02-29 15:59:36 +00:00
$data['requirements'] = [
'openssl',
'pdo',
'mbstring',
'fileinfo',
2016-03-05 00:18:10 +00:00
'tokenizer',
'gd',
2016-05-21 18:23:18 +00:00
'zip',
];
/*
* Optional PHP extensions
*/
$data['optional_requirements'] = [
'pdo_pgsql',
'pdo_mysql',
2016-02-29 15:59:36 +00:00
];
return view('Installer.Installer', $data);
2016-02-29 15:59:36 +00:00
}
/**
* Attempts to install the system
*
* @param Request $request
* @return \Illuminate\Http\RedirectResponse
*/
public function postInstaller(Request $request)
2016-02-29 15:59:36 +00:00
{
2016-03-02 23:37:33 +00:00
set_time_limit(300);
2016-02-29 15:59:36 +00:00
2016-05-21 18:23:18 +00:00
$database['type'] = $request->get('database_type');
$database['host'] = $request->get('database_host');
$database['name'] = $request->get('database_name');
$database['username'] = $request->get('database_username');
$database['password'] = $request->get('database_password');
$mail['driver'] = $request->get('mail_driver');
$mail['port'] = $request->get('mail_port');
$mail['username'] = $request->get('mail_username');
$mail['password'] = $request->get('mail_password');
$mail['encryption'] = $request->get('mail_encryption');
$mail['from_address'] = $request->get('mail_from_address');
$mail['from_name'] = $request->get('mail_from_name');
$mail['host'] = $request->get('mail_host');
$app_url = $request->get('app_url');
2016-02-29 15:59:36 +00:00
$app_key = str_random(16);
2016-03-02 23:37:33 +00:00
$version = file_get_contents(base_path('VERSION'));
2016-02-29 15:59:36 +00:00
if ($request->get('test') === 'db') {
2016-02-29 15:59:36 +00:00
$is_db_valid = self::testDatabase($database);
if ($is_db_valid === 'yes') {
2016-05-21 18:23:18 +00:00
return [
2016-03-05 00:18:10 +00:00
'status' => 'success',
2016-02-29 15:59:36 +00:00
'message' => 'Success, Your connection works!',
2016-03-05 00:18:10 +00:00
'test' => 1,
2016-05-21 18:23:18 +00:00
];
2016-02-29 15:59:36 +00:00
}
2016-05-21 18:23:18 +00:00
return [
2016-03-05 00:18:10 +00:00
'status' => 'error',
2016-02-29 15:59:36 +00:00
'message' => 'Unable to connect! Please check your settings',
2016-03-05 00:18:10 +00:00
'test' => 1,
2016-05-21 18:23:18 +00:00
];
2016-02-29 15:59:36 +00:00
}
2016-09-06 20:39:27 +00:00
$config = "APP_ENV=production\n" .
"APP_DEBUG=false\n" .
"APP_URL={$app_url}\n" .
"APP_KEY={$app_key}\n" .
"DB_TYPE={$database['type']}\n" .
"DB_HOST={$database['host']}\n" .
"DB_DATABASE={$database['name']}\n" .
"DB_USERNAME={$database['username']}\n" .
"DB_PASSWORD={$database['password']}\n\n" .
"MAIL_DRIVER={$mail['driver']}\n" .
"MAIL_PORT={$mail['port']}\n" .
"MAIL_ENCRYPTION={$mail['encryption']}\n" .
"MAIL_HOST={$mail['host']}\n" .
"MAIL_USERNAME={$mail['username']}\n" .
"MAIL_FROM_NAME=\"{$mail['from_name']}\"\n" .
"MAIL_FROM_ADDRESS={$mail['from_address']}\n" .
"WKHTML2PDF_BIN_FILE=wkhtmltopdf-amd64\n" .
2016-02-29 15:59:36 +00:00
"MAIL_PASSWORD={$mail['password']}\n\n";
2016-09-06 20:39:27 +00:00
$fp = fopen(base_path() . '/.env', 'w');
2016-02-29 15:59:36 +00:00
fwrite($fp, $config);
fclose($fp);
2016-03-02 23:37:33 +00:00
Config::set('database.default', $database['type']);
2016-05-21 18:23:18 +00:00
Config::set("database.connections.{$database['type']}.host", $database['host']);
Config::set("database.connections.{$database['type']}.database", $database['name']);
Config::set("database.connections.{$database['type']}.username", $database['username']);
Config::set("database.connections.{$database['type']}.password", $database['password']);
2016-03-02 23:37:33 +00:00
DB::reconnect();
2016-05-21 18:23:18 +00:00
//force laravel to regenerate a new key (see key:generate sources)
Config::set('app.key', $app_key);
Artisan::call('key:generate');
2016-03-05 00:18:10 +00:00
Artisan::call('migrate', ['--force' => true]);
2016-02-29 15:59:36 +00:00
if (Timezone::count() == 0) {
2016-03-05 00:18:10 +00:00
Artisan::call('db:seed', ['--force' => true]);
2016-02-29 15:59:36 +00:00
}
2016-03-05 00:18:10 +00:00
Artisan::call('optimize', ['--force' => true]);
2016-02-29 15:59:36 +00:00
2016-09-06 20:39:27 +00:00
$fp = fopen(base_path() . '/installed', 'w');
2016-03-02 23:37:33 +00:00
fwrite($fp, $version);
2016-02-29 23:50:41 +00:00
fclose($fp);
return redirect()->route('showSignup', ['first_run' => 'yup']);
2016-02-29 15:59:36 +00:00
}
private function testDatabase($database)
{
Config::set('database.default', $database['type']);
2016-05-21 18:23:18 +00:00
Config::set("database.connections.{$database['type']}.host", $database['host']);
Config::set("database.connections.{$database['type']}.database", $database['name']);
Config::set("database.connections.{$database['type']}.username", $database['username']);
Config::set("database.connections.{$database['type']}.password", $database['password']);
2016-02-29 15:59:36 +00:00
try {
DB::reconnect();
$success = DB::connection()->getDatabaseName() ? 'yes' : 'no';
} catch (Exception $e) {
return $e->getMessage();
}
2016-03-05 00:18:10 +00:00
2016-02-29 15:59:36 +00:00
return $success;
}
2016-03-05 00:18:10 +00:00
}