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

341 lines
11 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 Response;
2015-09-02 12:59:03 +02:00
use Redirect;
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
{
2016-03-02 14:36:42 +01:00
//parent::__construct();
2015-03-16 22:45:25 +01:00
$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');
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();
}
2016-03-02 14:36:42 +01:00
2015-11-04 14:57:59 +01:00
if (Utils::isDatabaseSetup() && Account::count() > 0) {
return Redirect::to('/');
}
2016-03-27 16:39:26 +02:00
$_ENV['APP_ENV'] = 'production';
$_ENV['APP_DEBUG'] = $app['debug'];
$_ENV['APP_URL'] = $app['url'];
$_ENV['APP_KEY'] = $app['key'];
2016-07-19 09:38:54 +02:00
$_ENV['APP_CIPHER'] = env('APP_CIPHER', 'AES-256-CBC');
2016-03-27 16:39:26 +02:00
$_ENV['DB_TYPE'] = $dbType;
$_ENV['DB_HOST'] = $database['type']['host'];
$_ENV['DB_DATABASE'] = $database['type']['database'];
$_ENV['DB_USERNAME'] = $database['type']['username'];
$_ENV['DB_PASSWORD'] = $database['type']['password'];
$_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'];
2016-12-06 10:37:04 +01:00
$_ENV['MAIL_FROM_ADDRESS'] = $mail['from']['address'];
2016-03-27 16:39:26 +02:00
$_ENV['MAIL_PASSWORD'] = $mail['password'];
$_ENV['PHANTOMJS_CLOUD_KEY'] = 'a-demo-key-with-low-quota-per-ip-address';
$_ENV['MAILGUN_DOMAIN'] = $mail['mailgun_domain'];
$_ENV['MAILGUN_SECRET'] = $mail['mailgun_secret'];
$config = '';
foreach ($_ENV as $key => $val) {
2016-04-09 22:00:24 +02:00
if (is_array($val)) {
continue;
}
if (preg_match('/\s/', $val)) {
$val = "'{$val}'";
}
$config .= "{$key}={$val}\n";
}
// Write Config Settings
$fp = fopen(base_path().'/.env', 'w');
2015-03-25 20:56:31 +01:00
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
Artisan::call('migrate', ['--force' => true]);
2015-07-09 16:12:43 +02:00
if (Industry::count() == 0) {
Artisan::call('db:seed', ['--force' => true]);
2015-07-09 16:12:43 +02:00
}
2015-08-30 14:08:15 +02:00
Cache::flush();
Artisan::call('optimize', ['--force' => true]);
2016-03-02 14:36:42 +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
}
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')) {
2015-11-04 14:57:59 +01:00
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'];
2016-03-02 14:36:42 +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'];
2016-12-06 10:37:04 +01:00
$_ENV['MAIL_FROM_ADDRESS'] = $mail['from']['address'];
2015-11-04 14:57:59 +01:00
$_ENV['MAIL_PASSWORD'] = $mail['password'];
$_ENV['MAILGUN_DOMAIN'] = $mail['mailgun_domain'];
$_ENV['MAILGUN_SECRET'] = $mail['mailgun_secret'];
2015-11-04 14:57:59 +01:00
}
$config = '';
foreach ($_ENV as $key => $val) {
if (is_array($val)) {
continue;
}
if (preg_match('/\s/', $val)) {
$val = "'{$val}'";
}
2015-11-04 14:57:59 +01:00
$config .= "{$key}={$val}\n";
}
$fp = fopen(base_path().'/.env', 'w');
2015-11-04 14:57:59 +01:00
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);
}
2016-03-02 14:36:42 +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)
{
2016-12-06 10:37:04 +01:00
$email = $mail['from']['address'];
2015-03-16 22:45:25 +01:00
$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);
2016-03-02 14:36:42 +01:00
2015-03-16 22:45:25 +01:00
$data = [
'text' => 'Test email',
];
try {
$response = $this->mailer->sendTo($email, $email, $fromName, 'Test email', 'contact', $data);
2015-03-16 22:45:25 +01:00
return $response === true ? 'Sent' : $response;
2015-03-16 22:45:25 +01:00
} 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 {
set_time_limit(60 * 5); // shouldn't take this long but just in case
Artisan::call('migrate', ['--force' => true]);
2015-07-09 16:12:43 +02:00
if (Industry::count() == 0) {
Artisan::call('db:seed', ['--force' => true]);
2015-07-09 16:12:43 +02:00
}
Artisan::call('optimize', ['--force' => true]);
2015-03-16 22:45:25 +01:00
} catch (Exception $e) {
Utils::logError($e);
return Response::make($e->getMessage(), 500);
2015-03-16 22:45:25 +01:00
}
}
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 {
set_time_limit(60 * 5);
2016-05-29 16:48:55 +02:00
Artisan::call('clear-compiled');
Artisan::call('cache:clear');
Artisan::call('debugbar:clear');
Artisan::call('route:clear');
Artisan::call('view:clear');
Artisan::call('config:clear');
Artisan::call('optimize', ['--force' => true]);
Cache::flush();
Session::flush();
Artisan::call('migrate', ['--force' => true]);
Artisan::call('db:seed', ['--force' => true, '--class' => 'UpdateSeeder']);
2015-09-02 12:59:03 +02:00
Event::fire(new UserSettingsChanged());
2016-05-23 08:18:39 +02:00
2016-07-19 09:38:54 +02:00
// legacy fix: check cipher is in .env file
if ( ! env('APP_CIPHER')) {
$fp = fopen(base_path().'/.env', 'a');
fwrite($fp, "\nAPP_CIPHER=rijndael-128");
fclose($fp);
}
2016-05-23 08:18:39 +02:00
// show message with link to Trello board
$message = trans('texts.see_whats_new', ['version' => NINJA_VERSION]);
$message = link_to(RELEASES_URL, $message, ['target' => '_blank']);
$message = sprintf('%s - %s', trans('texts.processed_updates'), $message);
Session::flash('warning', $message);
2015-03-16 22:45:25 +01:00
} catch (Exception $e) {
Utils::logError($e);
return Response::make($e->getMessage(), 500);
2015-03-16 22:45:25 +01:00
}
}
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;
2016-03-02 14:36:42 +01:00
2015-10-11 16:41:09 +02:00
return RESULT_SUCCESS;
}
2016-01-28 13:04:55 +01:00
public function stats()
{
2016-05-02 15:45:12 +02:00
if ( ! hash_equals(Input::get('password'), env('RESELLER_PASSWORD'))) {
2016-01-28 13:04:55 +01:00
sleep(3);
return '';
}
2016-01-28 15:07:03 +01:00
if (Utils::getResllerType() == RESELLER_REVENUE_SHARE) {
2016-02-25 11:29:26 +01:00
$data = DB::table('accounts')
2016-01-28 13:04:55 +01:00
->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 {
2016-02-25 11:29:26 +01:00
$data = DB::table('users')->count();
2016-01-28 13:04:55 +01:00
}
2016-01-28 15:07:03 +01:00
2016-02-25 11:29:26 +01:00
return json_encode($data);
2016-01-28 13:04:55 +01:00
}
}