2015-03-17 02:30:56 +01:00
|
|
|
<?php namespace App\Http\Controllers;
|
|
|
|
|
2015-03-26 05:15:18 +01:00
|
|
|
use Auth;
|
2015-03-17 02:30:56 +01:00
|
|
|
use Artisan;
|
|
|
|
use Cache;
|
|
|
|
use Config;
|
|
|
|
use DB;
|
|
|
|
use Exception;
|
|
|
|
use Input;
|
|
|
|
use Utils;
|
|
|
|
use View;
|
2015-05-08 10:21:29 +02:00
|
|
|
use Session;
|
2015-05-09 20:25:16 +02:00
|
|
|
use Cookie;
|
|
|
|
use Response;
|
2015-03-26 05:15:18 +01:00
|
|
|
use App\Models\User;
|
2015-06-20 19:40:50 +02:00
|
|
|
use App\Models\Account;
|
2015-07-09 16:12:43 +02:00
|
|
|
use App\Models\Industry;
|
2015-03-24 09:21:12 +01:00
|
|
|
use App\Ninja\Mailers\Mailer;
|
|
|
|
use App\Ninja\Repositories\AccountRepository;
|
2015-03-23 07:52:01 +01:00
|
|
|
use Redirect;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
class AppController extends BaseController
|
|
|
|
{
|
|
|
|
protected $accountRepo;
|
|
|
|
protected $mailer;
|
|
|
|
|
|
|
|
public function __construct(AccountRepository $accountRepo, Mailer $mailer)
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->accountRepo = $accountRepo;
|
|
|
|
$this->mailer = $mailer;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function showSetup()
|
|
|
|
{
|
2015-06-20 19:40:50 +02:00
|
|
|
if (Utils::isNinja() || (Utils::isDatabaseSetup() && Account::count() > 0)) {
|
2015-03-16 22:45:25 +01:00
|
|
|
return Redirect::to('/');
|
|
|
|
}
|
|
|
|
|
2015-07-07 22:08:16 +02:00
|
|
|
return View::make('setup');
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function doSetup()
|
|
|
|
{
|
2015-07-07 22:08:16 +02:00
|
|
|
if (Utils::isNinja() || (Utils::isDatabaseSetup() && Account::count() > 0)) {
|
2015-03-27 02:09:13 +01:00
|
|
|
return Redirect::to('/');
|
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
$valid = false;
|
|
|
|
$test = Input::get('test');
|
|
|
|
|
|
|
|
$app = Input::get('app');
|
|
|
|
$app['key'] = str_random(RANDOM_KEY_LENGTH);
|
|
|
|
|
|
|
|
$database = Input::get('database');
|
2015-03-25 20:56:31 +01:00
|
|
|
$dbType = $database['default'];
|
|
|
|
$database['connections'] = [$dbType => $database['type']];
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
$mail = Input::get('mail');
|
|
|
|
$email = $mail['username'];
|
|
|
|
$mail['from']['address'] = $email;
|
|
|
|
|
|
|
|
if ($test == 'mail') {
|
|
|
|
return self::testMail($mail);
|
|
|
|
}
|
|
|
|
|
2015-03-25 20:56:31 +01:00
|
|
|
$valid = self::testDatabase($database);
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
if ($test == 'db') {
|
|
|
|
return $valid === true ? 'Success' : $valid;
|
|
|
|
} elseif (!$valid) {
|
|
|
|
return Redirect::to('/setup')->withInput();
|
|
|
|
}
|
2015-03-24 00:54:49 +01:00
|
|
|
|
2015-06-10 10:34:20 +02:00
|
|
|
$config = "APP_ENV=production\n".
|
|
|
|
"APP_DEBUG=false\n".
|
2015-04-12 11:58:28 +02:00
|
|
|
"APP_URL={$app['url']}\n".
|
2015-03-25 20:56:31 +01:00
|
|
|
"APP_KEY={$app['key']}\n\n".
|
|
|
|
"DB_TYPE={$dbType}\n".
|
|
|
|
"DB_HOST={$database['type']['host']}\n".
|
|
|
|
"DB_DATABASE={$database['type']['database']}\n".
|
|
|
|
"DB_USERNAME={$database['type']['username']}\n".
|
|
|
|
"DB_PASSWORD={$database['type']['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_PASSWORD={$mail['password']}\n";
|
2015-03-24 00:54:49 +01:00
|
|
|
|
|
|
|
// Write Config Settings
|
2015-03-25 20:56:31 +01:00
|
|
|
$fp = fopen(base_path()."/.env", 'w');
|
|
|
|
fwrite($fp, $config);
|
|
|
|
fclose($fp);
|
2015-03-24 00:54:49 +01:00
|
|
|
|
|
|
|
// == DB Migrate & Seed == //
|
2015-03-26 15:35:57 +01:00
|
|
|
// Artisan::call('migrate:rollback', array('--force' => true)); // Debug Purposes
|
2015-03-25 20:56:31 +01:00
|
|
|
Artisan::call('migrate', array('--force' => true));
|
2015-07-09 16:12:43 +02:00
|
|
|
if (Industry::count() == 0) {
|
|
|
|
Artisan::call('db:seed', array('--force' => true));
|
|
|
|
}
|
2015-04-16 19:12:56 +02:00
|
|
|
Artisan::call('optimize', array('--force' => true));
|
2015-03-26 05:15:18 +01:00
|
|
|
|
2015-03-29 14:37:42 +02:00
|
|
|
$firstName = trim(Input::get('first_name'));
|
|
|
|
$lastName = trim(Input::get('last_name'));
|
|
|
|
$email = trim(strtolower(Input::get('email')));
|
|
|
|
$password = trim(Input::get('password'));
|
|
|
|
$account = $this->accountRepo->create($firstName, $lastName, $email, $password);
|
|
|
|
$user = $account->users()->first();
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-03-29 14:37:42 +02:00
|
|
|
return Redirect::to('/login');
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function testDatabase($database)
|
|
|
|
{
|
2015-03-25 20:56:31 +01:00
|
|
|
$dbType = $database['default'];
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
Config::set('database.default', $dbType);
|
2015-03-25 20:56:31 +01:00
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
foreach ($database['connections'][$dbType] as $key => $val) {
|
|
|
|
Config::set("database.connections.{$dbType}.{$key}", $val);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$valid = DB::connection()->getDatabaseName() ? true : false;
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return $e->getMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function testMail($mail)
|
|
|
|
{
|
|
|
|
$email = $mail['username'];
|
|
|
|
$fromName = $mail['from']['name'];
|
|
|
|
|
|
|
|
foreach ($mail as $key => $val) {
|
|
|
|
Config::set("mail.{$key}", $val);
|
|
|
|
}
|
|
|
|
|
|
|
|
Config::set('mail.from.address', $email);
|
|
|
|
Config::set('mail.from.name', $fromName);
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'text' => 'Test email',
|
|
|
|
];
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->mailer->sendTo($email, $email, $fromName, 'Test email', 'contact', $data);
|
|
|
|
|
|
|
|
return 'Sent';
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return $e->getMessage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function install()
|
|
|
|
{
|
|
|
|
if (!Utils::isNinja() && !Utils::isDatabaseSetup()) {
|
|
|
|
try {
|
2015-03-25 21:00:00 +01:00
|
|
|
Artisan::call('migrate', array('--force' => true));
|
2015-07-09 16:12:43 +02:00
|
|
|
if (Industry::count() == 0) {
|
|
|
|
Artisan::call('db:seed', array('--force' => true));
|
|
|
|
}
|
2015-04-16 19:12:56 +02:00
|
|
|
Artisan::call('optimize', array('--force' => true));
|
2015-03-16 22:45:25 +01:00
|
|
|
} catch (Exception $e) {
|
|
|
|
Response::make($e->getMessage(), 500);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Redirect::to('/');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function update()
|
|
|
|
{
|
|
|
|
if (!Utils::isNinja()) {
|
|
|
|
try {
|
2015-03-25 21:00:00 +01:00
|
|
|
Artisan::call('migrate', array('--force' => true));
|
2015-05-10 10:45:03 +02:00
|
|
|
Artisan::call('db:seed', array('--force' => true, '--class' => 'PaymentLibrariesSeeder'));
|
2015-04-16 19:12:56 +02:00
|
|
|
Artisan::call('optimize', array('--force' => true));
|
2015-03-16 22:45:25 +01:00
|
|
|
Cache::flush();
|
2015-05-10 10:45:03 +02:00
|
|
|
Session::flash('message', trans('texts.processed_updates'));
|
2015-03-16 22:45:25 +01:00
|
|
|
} catch (Exception $e) {
|
|
|
|
Response::make($e->getMessage(), 500);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Redirect::to('/');
|
|
|
|
}
|
|
|
|
}
|