1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Http/Controllers/AppController.php

199 lines
5.8 KiB
PHP
Raw Normal View History

2015-03-17 02:30:56 +01:00
<?php namespace App\Http\Controllers;
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;
use App\Models\User;
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()
{
if (Utils::isNinja() || Utils::isDatabaseSetup()) {
return Redirect::to('/');
}
2015-05-09 20:25:16 +02:00
$view = View::make('setup');
/*
$cookie = Cookie::forget('ninja_session', '/', 'www.ninja.dev');
Cookie::queue($cookie);
return Response::make($view)->withCookie($cookie);
*/
return Response::make($view);
2015-03-16 22:45:25 +01:00
}
public function doSetup()
{
2015-03-27 02:09:13 +01:00
if (Utils::isNinja() || Utils::isDatabaseSetup()) {
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();
}
// == ENV Settings (Production) == //
2015-03-25 20:56:31 +01:00
$config = "APP_ENV=development\n".
"APP_DEBUG=true\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";
// Write Config Settings
2015-03-25 20:56:31 +01:00
$fp = fopen(base_path()."/.env", 'w');
fwrite($fp, $config);
fclose($fp);
// == 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));
Artisan::call('db:seed', array('--force' => true));
2015-04-16 19:12:56 +02:00
Artisan::call('optimize', array('--force' => true));
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
//Auth::login($user, true);
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));
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('/');
}
}