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

407 lines
13 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
2015-03-17 02:30:56 +01:00
2017-01-30 20:40:43 +01:00
namespace App\Http\Controllers;
use App\Events\UserSettingsChanged;
use App\Models\Account;
use App\Models\Industry;
use App\Ninja\Mailers\Mailer;
use App\Ninja\Repositories\AccountRepository;
use App\Services\EmailService;
2015-03-17 02:30:56 +01:00
use Artisan;
2017-01-30 20:40:43 +01:00
use Auth;
2015-03-17 02:30:56 +01:00
use Cache;
use Config;
use DB;
2017-01-30 20:40:43 +01:00
use Event;
2015-03-17 02:30:56 +01:00
use Exception;
use Input;
2017-01-30 20:40:43 +01:00
use Redirect;
use Response;
use Session;
2015-03-17 02:30:56 +01:00
use Utils;
use View;
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');
2017-04-02 19:46:01 +02:00
$app['key'] = env('APP_KEY') ?: strtolower(str_random(RANDOM_KEY_LENGTH));
2015-11-04 14:57:59 +01:00
$app['debug'] = Input::get('debug') ? 'true' : 'false';
2016-12-25 21:31:02 +01:00
$app['https'] = Input::get('https') ? '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;
2017-01-30 20:40:43 +01:00
} elseif (! $valid) {
2015-03-16 22:45:25 +01:00
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'];
2017-04-14 09:39:58 +02:00
$_ENV['APP_LOCALE'] = 'en';
2016-03-27 16:39:26 +02:00
$_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');
2017-04-14 09:39:58 +02:00
$_ENV['REQUIRE_HTTPS'] = $app['https'];
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';
2017-04-02 19:46:01 +02:00
$_ENV['PHANTOMJS_SECRET'] = strtolower(str_random(RANDOM_KEY_LENGTH));
$_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 == //
$sqlFile = base_path() . '/database/setup.sql';
DB::unprepared(file_get_contents($sqlFile));
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
2017-01-30 20:40:43 +01:00
if (! Auth::check() && Utils::isDatabaseSetup() && Account::count() > 0) {
2015-11-04 14:57:59 +01:00
return Redirect::to('/');
}
2017-01-30 17:05:31 +01:00
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>');
2017-01-30 20:40:43 +01:00
2015-11-04 14:57:59 +01:00
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';
2016-12-25 21:31:02 +01:00
$_ENV['REQUIRE_HTTPS'] = Input::get('https') ? 'true' : 'false';
2015-11-04 14:57:59 +01:00
$_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)) {
2017-01-30 17:05:31 +01:00
$val = "'{$val}'";
}
2015-11-04 14:57:59 +01:00
$config .= "{$key}={$val}\n";
}
2017-03-29 17:19:51 +02:00
$filePath = base_path().'/.env';
$fp = fopen($filePath, 'w');
2015-11-04 14:57:59 +01:00
fwrite($fp, $config);
fclose($fp);
Session::flash('message', trans('texts.updated_settings'));
2017-01-30 20:40:43 +01:00
2015-11-04 14:57:59 +01:00
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',
2017-02-06 16:19:01 +01:00
'fromEmail' => $email
2015-03-16 22:45:25 +01:00
];
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()
{
2017-01-30 20:40:43 +01: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);
2017-01-30 20:40:43 +01:00
return Response::make($e->getMessage(), 500);
2015-03-16 22:45:25 +01:00
}
}
return Redirect::to('/');
}
public function update()
{
2017-01-30 20:40:43 +01:00
if (! Utils::isNinjaProd()) {
2015-03-16 22:45:25 +01:00
try {
set_time_limit(60 * 5);
2017-03-09 20:43:53 +01:00
$this->checkInnoDB();
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]);
2017-05-10 10:38:21 +02:00
Auth::logout();
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
2017-01-30 17:05:31 +01:00
if (! env('APP_CIPHER')) {
2016-07-19 09:38:54 +02:00
$fp = fopen(base_path().'/.env', 'a');
2016-12-20 15:28:44 +01:00
fwrite($fp, "\nAPP_CIPHER=AES-256-CBC");
2016-07-19 09:38:54 +02:00
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);
2017-01-30 20:40:43 +01:00
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
2017-03-09 20:43:53 +01:00
// MySQL changed the default table type from MyISAM to InnoDB
// We need to make sure all tables are InnoDB to prevent migration failures
public function checkInnoDB()
{
2017-03-10 11:48:23 +01:00
$result = DB::select("SELECT engine
FROM information_schema.TABLES
WHERE TABLE_NAME='clients' AND TABLE_SCHEMA='ninja'");
if (count($result) && $result[0]->engine == 'InnoDB') {
return;
}
2017-03-09 20:43:53 +01:00
$tables = DB::select('SHOW TABLES');
$sql = "SET sql_mode = 'ALLOW_INVALID_DATES';\n";
foreach($tables as $table) {
$fieldName = 'Tables_in_' . env('DB_DATABASE');
$sql .= "ALTER TABLE {$table->$fieldName} engine=InnoDB;\n";
}
DB::unprepared($sql);
}
2015-10-11 16:41:09 +02:00
public function emailBounced()
{
$messageId = Input::get('MessageID');
$error = Input::get('Name') . ': ' . Input::get('Description');
2017-01-30 20:40:43 +01:00
2015-10-11 16:41:09 +02:00
return $this->emailService->markBounced($messageId, $error) ? RESULT_SUCCESS : RESULT_FAILURE;
}
public function emailOpened()
{
$messageId = Input::get('MessageID');
2017-01-30 20:40:43 +01:00
2015-10-11 16:41:09 +02:00
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
2017-03-09 16:48:21 +01:00
public function checkData()
{
try {
Artisan::call('ninja:check-data');
2017-05-02 21:39:07 +02:00
Artisan::call('ninja:init-lookup', ['--validate' => true]);
2017-08-13 16:43:22 +02:00
// check error log is empty
$errorLog = storage_path('logs/laravel-error.log');
if (file_exists($errorLog)) {
return 'Failure: error log exists';
}
2017-03-09 16:48:21 +01:00
return RESULT_SUCCESS;
} catch (Exception $exception) {
2017-05-02 21:46:29 +02:00
return $exception->getMessage() ?: RESULT_FAILURE;
2017-03-09 16:48:21 +01:00
}
}
public function errors()
{
if (Utils::isNinjaProd()) {
return redirect('/');
}
$errors = Utils::getErrors();
return view('errors.list', compact('errors'));
}
2016-01-28 13:04:55 +01:00
public function stats()
{
2017-01-30 17:05:31 +01:00
if (! hash_equals(Input::get('password'), env('RESELLER_PASSWORD'))) {
2016-01-28 13:04:55 +01:00
sleep(3);
2017-01-30 20:40:43 +01:00
2016-01-28 13:04:55 +01:00
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',
2017-01-30 20:40:43 +01:00
'payments.amount',
2016-01-28 13:04:55 +01:00
]);
2016-01-28 15:07:03 +01:00
} else {
2017-04-26 20:51:24 +02: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
}
}