1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 08:21:34 +02:00
invoiceninja/app/Http/Controllers/AppController.php

312 lines
10 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-09-02 12:59:03 +02:00
use Event;
2015-05-08 10:21:29 +02:00
use Session;
2015-05-09 20:25:16 +02:00
use Cookie;
use Response;
2015-09-02 12:59:03 +02:00
use Redirect;
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;
use App\Ninja\Mailers\Mailer;
use App\Ninja\Repositories\AccountRepository;
2015-09-02 12:59:03 +02:00
use App\Events\UserSettingsChanged;
2015-10-11 16:41:09 +02:00
use App\Services\EmailService;
2015-03-16 22:45:25 +01:00
class AppController extends BaseController
{
protected $accountRepo;
protected $mailer;
2015-10-11 16:41:09 +02:00
protected $emailService;
2015-03-16 22:45:25 +01:00
2015-10-11 16:41:09 +02:00
public function __construct(AccountRepository $accountRepo, Mailer $mailer, EmailService $emailService)
2015-03-16 22:45:25 +01:00
{
parent::__construct();
$this->accountRepo = $accountRepo;
$this->mailer = $mailer;
2015-10-11 16:41:09 +02:00
$this->emailService = $emailService;
2015-03-16 22:45:25 +01:00
}
public function showSetup()
{
2015-08-14 14:04:33 +02:00
if (Utils::isNinjaProd() || (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-11-04 14:57:59 +01:00
if (Utils::isNinjaProd()) {
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');
2015-10-20 19:12:34 +02:00
$app['key'] = env('APP_KEY') ?: str_random(RANDOM_KEY_LENGTH);
2015-11-04 14:57:59 +01:00
$app['debug'] = Input::get('debug') ? 'true' : 'false';
2015-03-16 22:45:25 +01:00
$database = Input::get('database');
2015-11-04 14:57:59 +01:00
$dbType = 'mysql'; // $database['default'];
2015-03-25 20:56:31 +01:00
$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-11-04 14:57:59 +01:00
if (Utils::isDatabaseSetup() && Account::count() > 0) {
return Redirect::to('/');
}
2015-06-10 10:34:20 +02:00
$config = "APP_ENV=production\n".
2015-11-04 14:57:59 +01:00
"APP_DEBUG={$app['debug']}\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".
2015-09-25 11:57:40 +02:00
"MAIL_PASSWORD={$mail['password']}\n\n".
2015-10-25 08:13:06 +01:00
"PHANTOMJS_CLOUD_KEY='a-demo-key-with-low-quota-per-ip-address'";
// 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));
2015-07-09 16:12:43 +02:00
if (Industry::count() == 0) {
Artisan::call('db:seed', array('--force' => true));
}
2015-08-30 14:08:15 +02:00
Cache::flush();
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
return Redirect::to('/login');
2015-03-16 22:45:25 +01:00
}
2015-11-04 14:57:59 +01:00
public function updateSetup()
2015-03-16 22:45:25 +01:00
{
2015-11-04 14:57:59 +01:00
if (Utils::isNinjaProd()) {
return Redirect::to('/');
}
2015-03-16 22:45:25 +01:00
2015-11-04 14:57:59 +01:00
if (!Auth::check() && Utils::isDatabaseSetup() && Account::count() > 0) {
return Redirect::to('/');
}
if ( ! $canUpdateEnv = @fopen(base_path()."/.env", 'w')) {
Session::flash('error', 'Warning: Permission denied to write to .env config file, try running <code>sudo chown www-data:www-data /path/to/ninja/.env</code>');
return Redirect::to('/settings/system_settings');
}
$app = Input::get('app');
$db = Input::get('database');
$mail = Input::get('mail');
$_ENV['APP_URL'] = $app['url'];
$_ENV['APP_DEBUG'] = Input::get('debug') ? 'true' : 'false';
$_ENV['DB_TYPE'] = 'mysql'; // $db['default'];
$_ENV['DB_HOST'] = $db['type']['host'];
$_ENV['DB_DATABASE'] = $db['type']['database'];
$_ENV['DB_USERNAME'] = $db['type']['username'];
$_ENV['DB_PASSWORD'] = $db['type']['password'];
2015-03-25 20:56:31 +01:00
2015-11-04 14:57:59 +01:00
if ($mail) {
$_ENV['MAIL_DRIVER'] = $mail['driver'];
$_ENV['MAIL_PORT'] = $mail['port'];
$_ENV['MAIL_ENCRYPTION'] = $mail['encryption'];
$_ENV['MAIL_HOST'] = $mail['host'];
$_ENV['MAIL_USERNAME'] = $mail['username'];
$_ENV['MAIL_FROM_NAME'] = $mail['from']['name'];
$_ENV['MAIL_PASSWORD'] = $mail['password'];
$_ENV['MAIL_FROM_ADDRESS'] = $mail['username'];
}
$config = '';
foreach ($_ENV as $key => $val) {
$config .= "{$key}={$val}\n";
}
$fp = fopen(base_path()."/.env", 'w');
fwrite($fp, $config);
fclose($fp);
Session::flash('message', trans('texts.updated_settings'));
return Redirect::to('/settings/system_settings');
}
private function testDatabase($database)
{
$dbType = 'mysql'; // $database['default'];
Config::set('database.default', $dbType);
2015-03-16 22:45:25 +01:00
foreach ($database['connections'][$dbType] as $key => $val) {
Config::set("database.connections.{$dbType}.{$key}", $val);
}
2015-11-04 14:57:59 +01:00
2015-03-16 22:45:25 +01:00
try {
2015-11-04 14:57:59 +01:00
DB::reconnect();
2015-03-16 22:45:25 +01:00
$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()
{
2015-08-14 14:04:33 +02:00
if (!Utils::isNinjaProd() && !Utils::isDatabaseSetup()) {
2015-03-16 22:45:25 +01:00
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()
{
2015-08-14 14:04:33 +02:00
if (!Utils::isNinjaProd()) {
2015-03-16 22:45:25 +01:00
try {
Cache::flush();
Session::flush();
Artisan::call('optimize', array('--force' => true));
2015-03-25 21:00:00 +01:00
Artisan::call('migrate', array('--force' => true));
foreach ([
'PaymentLibraries',
'Fonts',
'Banks',
'InvoiceStatus'
] as $seeder) {
Artisan::call('db:seed', array('--force' => true, '--class' => "{$seeder}Seeder"));
}
2015-09-02 12:59:03 +02:00
Event::fire(new UserSettingsChanged());
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('/');
}
2015-10-11 16:41:09 +02:00
public function emailBounced()
{
$messageId = Input::get('MessageID');
$error = Input::get('Name') . ': ' . Input::get('Description');
return $this->emailService->markBounced($messageId, $error) ? RESULT_SUCCESS : RESULT_FAILURE;
}
public function emailOpened()
{
$messageId = Input::get('MessageID');
return $this->emailService->markOpened($messageId) ? RESULT_SUCCESS : RESULT_FAILURE;
return RESULT_SUCCESS;
}
2016-01-28 13:04:55 +01:00
public function stats()
{
if (Input::get('password') != env('RESELLER_PASSWORD')) {
sleep(3);
return '';
}
2016-01-28 15:07:03 +01:00
if (Utils::getResllerType() == RESELLER_REVENUE_SHARE) {
2016-01-28 13:04:55 +01:00
$payments = DB::table('accounts')
->leftJoin('payments', 'payments.account_id', '=', 'accounts.id')
->leftJoin('clients', 'clients.id', '=', 'payments.client_id')
->where('accounts.account_key', '=', NINJA_ACCOUNT_KEY)
->where('payments.is_deleted', '=', false)
->get([
'clients.public_id as client_id',
'payments.public_id as payment_id',
'payments.payment_date',
'payments.amount'
]);
2016-01-28 15:07:03 +01:00
} else {
$payments = DB::table('accounts')
->leftJoin('payments', 'payments.account_id', '=', 'accounts.id')
->leftJoin('clients', 'clients.id', '=', 'payments.client_id')
->where('accounts.account_key', '=', NINJA_ACCOUNT_KEY)
->where('payments.is_deleted', '=', false)
->groupBy('clients.id')
->count();
2016-01-28 13:04:55 +01:00
}
2016-01-28 15:07:03 +01:00
return json_encode($payments);
2016-01-28 13:04:55 +01:00
}
2016-01-28 15:07:03 +01:00
}