diff --git a/app/Console/Commands/ChargeRenewalInvoices.php b/app/Console/Commands/ChargeRenewalInvoices.php index 870406a3af87..ad41b3d77ea3 100644 --- a/app/Console/Commands/ChargeRenewalInvoices.php +++ b/app/Console/Commands/ChargeRenewalInvoices.php @@ -60,6 +60,10 @@ class ChargeRenewalInvoices extends Command { $this->info(date('Y-m-d').' ChargeRenewalInvoices...'); + if ($database = $this->option('database')) { + config(['database.default' => $database]); + } + $ninjaAccount = $this->accountRepo->getNinjaAccount(); $invoices = Invoice::whereAccountId($ninjaAccount->id) ->whereDueDate(date('Y-m-d')) @@ -120,6 +124,8 @@ class ChargeRenewalInvoices extends Command */ protected function getOptions() { - return []; + return [ + ['database', null, InputOption::VALUE_OPTIONAL, 'Database', null], + ]; } } diff --git a/app/Console/Commands/CheckData.php b/app/Console/Commands/CheckData.php index d24ce1432e71..445d50f49052 100644 --- a/app/Console/Commands/CheckData.php +++ b/app/Console/Commands/CheckData.php @@ -64,6 +64,10 @@ class CheckData extends Command { $this->logMessage(date('Y-m-d') . ' Running CheckData...'); + if ($database = $this->option('database')) { + config(['database.default' => $database]); + } + if (! $this->option('client_id')) { $this->checkBlankInvoiceHistory(); $this->checkPaidToDate(); @@ -544,6 +548,7 @@ class CheckData extends Command return [ ['fix', null, InputOption::VALUE_OPTIONAL, 'Fix data', null], ['client_id', null, InputOption::VALUE_OPTIONAL, 'Client id', null], + ['database', null, InputOption::VALUE_OPTIONAL, 'Database', null], ]; } } diff --git a/app/Console/Commands/CreateTestData.php b/app/Console/Commands/CreateTestData.php index ac046f3500c9..87d32b82d3c7 100644 --- a/app/Console/Commands/CreateTestData.php +++ b/app/Console/Commands/CreateTestData.php @@ -74,6 +74,10 @@ class CreateTestData extends Command $this->info(date('Y-m-d').' Running CreateTestData...'); $this->count = $this->argument('count'); + if ($database = $this->option('database')) { + config(['database.default' => $database]); + } + if (filter_var($this->argument('create_account'), FILTER_VALIDATE_BOOLEAN)) { $this->info('Creating new account...'); $account = $this->accountRepo->create( @@ -218,6 +222,8 @@ class CreateTestData extends Command */ protected function getOptions() { - return []; + return [ + ['database', null, InputOption::VALUE_OPTIONAL, 'Database', null], + ]; } } diff --git a/app/Console/Commands/GenerateResources.php b/app/Console/Commands/GenerateResources.php deleted file mode 100644 index 17e139188caf..000000000000 --- a/app/Console/Commands/GenerateResources.php +++ /dev/null @@ -1,63 +0,0 @@ - $value) { - if (is_array($value)) { - echo $key; - } else { - echo "$key => $value\n"; - } - } - } - - /** - * @return array - */ - protected function getArguments() - { - return []; - } - - /** - * @return array - */ - protected function getOptions() - { - return []; - } -} diff --git a/app/Console/Commands/Inspire.php b/app/Console/Commands/Inspire.php deleted file mode 100644 index f49e17c9d96e..000000000000 --- a/app/Console/Commands/Inspire.php +++ /dev/null @@ -1,36 +0,0 @@ -comment(PHP_EOL.Inspiring::quote().PHP_EOL); - } -} diff --git a/app/Console/Commands/PruneData.php b/app/Console/Commands/PruneData.php index 33d2bd64c0a9..0ebccbdf50a4 100644 --- a/app/Console/Commands/PruneData.php +++ b/app/Console/Commands/PruneData.php @@ -14,7 +14,7 @@ class PruneData extends Command * @var string */ protected $name = 'ninja:prune-data'; - + /** * @var string */ @@ -24,6 +24,10 @@ class PruneData extends Command { $this->info(date('Y-m-d').' Running PruneData...'); + if ($database = $this->option('database')) { + config(['database.default' => $database]); + } + // delete accounts who never registered, didn't create any invoices, // hansn't logged in within the past 6 months and isn't linked to another account $sql = 'select a.id @@ -42,14 +46,14 @@ class PruneData extends Command having count(i.id) = 0'; $results = DB::select($sql); - + foreach ($results as $result) { $this->info("Deleting {$result->id}"); DB::table('accounts') ->where('id', '=', $result->id) ->delete(); } - + $this->info('Done'); } @@ -66,6 +70,8 @@ class PruneData extends Command */ protected function getOptions() { - return []; + return [ + ['database', null, InputOption::VALUE_OPTIONAL, 'Database', null], + ]; } } diff --git a/app/Console/Commands/RemoveOrphanedDocuments.php b/app/Console/Commands/RemoveOrphanedDocuments.php index 489d8d94421f..969c62aab4ad 100644 --- a/app/Console/Commands/RemoveOrphanedDocuments.php +++ b/app/Console/Commands/RemoveOrphanedDocuments.php @@ -19,14 +19,18 @@ class RemoveOrphanedDocuments extends Command * @var string */ protected $description = 'Removes old documents not associated with an expense or invoice'; - + public function fire() { $this->info(date('Y-m-d').' Running RemoveOrphanedDocuments...'); + if ($database = $this->option('database')) { + config(['database.default' => $database]); + } + $documents = Document::whereRaw('invoice_id IS NULL AND expense_id IS NULL AND updated_at <= ?', [new DateTime('-1 hour')]) ->get(); - + $this->info(count($documents).' orphaned document(s) found'); foreach ($documents as $document) { @@ -49,6 +53,8 @@ class RemoveOrphanedDocuments extends Command */ protected function getOptions() { - return []; + return [ + ['database', null, InputOption::VALUE_OPTIONAL, 'Database', null], + ]; } } diff --git a/app/Console/Commands/ResetData.php b/app/Console/Commands/ResetData.php index 70e9791a27c9..1fb94d2d36e9 100644 --- a/app/Console/Commands/ResetData.php +++ b/app/Console/Commands/ResetData.php @@ -14,7 +14,7 @@ class ResetData extends Command * @var string */ protected $name = 'ninja:reset-data'; - + /** * @var string */ @@ -28,8 +28,24 @@ class ResetData extends Command return; } + if ($database = $this->option('database')) { + config(['database.default' => $database]); + } + Artisan::call('migrate:reset'); Artisan::call('migrate'); Artisan::call('db:seed'); } + + /** + * @return array + */ + protected function getOptions() + { + return [ + ['fix', null, InputOption::VALUE_OPTIONAL, 'Fix data', null], + ['client_id', null, InputOption::VALUE_OPTIONAL, 'Client id', null], + ['database', null, InputOption::VALUE_OPTIONAL, 'Database', null], + ]; + } } diff --git a/app/Console/Commands/ResetInvoiceSchemaCounter.php b/app/Console/Commands/ResetInvoiceSchemaCounter.php deleted file mode 100644 index be221c4d6390..000000000000 --- a/app/Console/Commands/ResetInvoiceSchemaCounter.php +++ /dev/null @@ -1,75 +0,0 @@ -invoice = $invoice; - } - - /** - * Execute the console command. - * - * @return mixed - */ - public function handle() - { - $force = $this->option('force'); - $account = $this->argument('account'); - - $accounts = null; - - if ($account) { - $accounts = Account::find($account)->get(); - } else { - $accounts = Account::all(); - } - - $latestInvoice = $this->invoice->latest()->first(); - $invoiceYear = Carbon::parse($latestInvoice->created_at)->year; - - if (Carbon::now()->year > $invoiceYear || $force) { - $accounts->transform(function ($a) { - /* @var Account $a */ - $a->invoice_number_counter = 1; - $a->update(); - }); - - $this->info('The counter has been resetted successfully for '.$accounts->count().' account(s).'); - } - } -} diff --git a/app/Console/Commands/SendRecurringInvoices.php b/app/Console/Commands/SendRecurringInvoices.php index 0a62ca626fbf..bd5dc83278c6 100644 --- a/app/Console/Commands/SendRecurringInvoices.php +++ b/app/Console/Commands/SendRecurringInvoices.php @@ -61,6 +61,10 @@ class SendRecurringInvoices extends Command $this->info(date('Y-m-d H:i:s') . ' Running SendRecurringInvoices...'); $today = new DateTime(); + if ($database = $this->option('database')) { + config(['database.default' => $database]); + } + // check for counter resets $accounts = Account::where('reset_counter_frequency_id', '>', 0) ->orderBy('id', 'asc') @@ -130,6 +134,8 @@ class SendRecurringInvoices extends Command */ protected function getOptions() { - return []; + return [ + ['database', null, InputOption::VALUE_OPTIONAL, 'Database', null], + ]; } } diff --git a/app/Console/Commands/SendReminders.php b/app/Console/Commands/SendReminders.php index af5f9bfca2c2..eb1d99666754 100644 --- a/app/Console/Commands/SendReminders.php +++ b/app/Console/Commands/SendReminders.php @@ -58,6 +58,10 @@ class SendReminders extends Command { $this->info(date('Y-m-d') . ' Running SendReminders...'); + if ($database = $this->option('database')) { + config(['database.default' => $database]); + } + $accounts = $this->accountRepo->findWithReminders(); $this->info(count($accounts) . ' accounts found'); @@ -103,6 +107,8 @@ class SendReminders extends Command */ protected function getOptions() { - return []; + return [ + ['database', null, InputOption::VALUE_OPTIONAL, 'Database', null], + ]; } } diff --git a/app/Console/Commands/SendRenewalInvoices.php b/app/Console/Commands/SendRenewalInvoices.php index edfc2471b31d..34b56e6b282d 100644 --- a/app/Console/Commands/SendRenewalInvoices.php +++ b/app/Console/Commands/SendRenewalInvoices.php @@ -51,6 +51,10 @@ class SendRenewalInvoices extends Command { $this->info(date('Y-m-d').' Running SendRenewalInvoices...'); + if ($database = $this->option('database')) { + config(['database.default' => $database]); + } + // get all accounts with plans expiring in 10 days $companies = Company::whereRaw("datediff(plan_expires, curdate()) = 10 and (plan = 'pro' or plan = 'enterprise')") ->orderBy('id') @@ -123,6 +127,8 @@ class SendRenewalInvoices extends Command */ protected function getOptions() { - return []; + return [ + ['database', null, InputOption::VALUE_OPTIONAL, 'Database', null], + ]; } } diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 2f2eca9da424..c1cf74d0e6d0 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -24,7 +24,6 @@ class Kernel extends ConsoleKernel 'App\Console\Commands\SendRenewalInvoices', 'App\Console\Commands\ChargeRenewalInvoices', 'App\Console\Commands\SendReminders', - 'App\Console\Commands\GenerateResources', 'App\Console\Commands\TestOFX', 'App\Console\Commands\MakeModule', 'App\Console\Commands\MakeClass', diff --git a/app/Constants.php b/app/Constants.php index 7ddb3aabce0a..8fd7434ffa7e 100644 --- a/app/Constants.php +++ b/app/Constants.php @@ -229,8 +229,7 @@ if (! defined('APP_NAME')) { define('SESSION_REFERRAL_CODE', 'referralCode'); define('SESSION_LEFT_SIDEBAR', 'showLeftSidebar'); define('SESSION_RIGHT_SIDEBAR', 'showRightSidebar'); - define('SESSION_USER_DB_SERVER', 'userDbServer'); - define('SESSION_CONTACT_DB_SERVER', 'contactDbServer'); + define('SESSION_DB_SERVER', 'dbServer'); define('SESSION_LAST_REQUEST_PAGE', 'SESSION_LAST_REQUEST_PAGE'); define('SESSION_LAST_REQUEST_TIME', 'SESSION_LAST_REQUEST_TIME'); @@ -293,7 +292,8 @@ if (! defined('APP_NAME')) { define('REQUESTED_PRO_PLAN', 'REQUESTED_PRO_PLAN'); define('DEMO_ACCOUNT_ID', 'DEMO_ACCOUNT_ID'); - define('NINJA_ACCOUNT_KEY', 'zg4ylmzDkdkPOT8yoKQw9LTWaoZJx79h'); + define('NINJA_ACCOUNT_KEY', env('NINJA_ACCOUNT_KEY', 'zg4ylmzDkdkPOT8yoKQw9LTWaoZJx79h')); + define('NINJA_ACCOUNT_EMAIL', env('NINJA_ACCOUNT_EMAIL', 'contact@invoiceninja.com')); define('NINJA_LICENSE_ACCOUNT_KEY', 'AsFmBAeLXF0IKf7tmi0eiyZfmWW9hxMT'); define('NINJA_GATEWAY_ID', GATEWAY_STRIPE); define('NINJA_GATEWAY_CONFIG', 'NINJA_GATEWAY_CONFIG'); diff --git a/app/Http/Middleware/DatabaseLookup.php b/app/Http/Middleware/DatabaseLookup.php index cac9a6d2fe8a..954a91c41068 100644 --- a/app/Http/Middleware/DatabaseLookup.php +++ b/app/Http/Middleware/DatabaseLookup.php @@ -18,7 +18,7 @@ class DatabaseLookup if ($guard == 'user') { // user's value is set when logging in - if (! session('SESSION_USER_DB_SERVER')) { + if (! session(SESSION_DB_SERVER)) { return redirect('/logout'); } } elseif ($guard == 'api') { @@ -31,6 +31,8 @@ class DatabaseLookup } elseif (request()->contact_key) { LookupContact::setServerByField('contact_key', request()->contact_key); } + } elseif ($guard == 'postmark') { + LookupInvitation::setServerByField('message_id', request()->MessageID); } return $next($request); diff --git a/app/Http/routes.php b/app/Http/routes.php index 5b9249253cfa..386ea02a261a 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -76,10 +76,13 @@ Route::group(['middleware' => 'cors'], function () { Route::match(['GET', 'POST', 'OPTIONS'], '/buy_now/{gateway_type?}', 'OnlinePaymentController@handleBuyNow'); }); -Route::post('/hook/email_bounced', 'AppController@emailBounced'); -Route::post('/hook/email_opened', 'AppController@emailOpened'); -Route::post('/hook/bot/{platform?}', 'BotController@handleMessage'); +Route::group(['middleware' => 'lookup:postmark'], function () { + Route::post('/hook/email_bounced', 'AppController@emailBounced'); + Route::post('/hook/email_opened', 'AppController@emailOpened'); +}); + Route::post('/payment_hook/{accountKey}/{gatewayId}', 'OnlinePaymentController@handlePaymentWebhook'); +//Route::post('/hook/bot/{platform?}', 'BotController@handleMessage'); // Laravel auth routes Route::get('/signup', ['as' => 'signup', 'uses' => 'Auth\AuthController@getRegister']); diff --git a/app/Models/LookupModel.php b/app/Models/LookupModel.php index a43577482299..aa1d5cd7c793 100644 --- a/app/Models/LookupModel.php +++ b/app/Models/LookupModel.php @@ -65,10 +65,11 @@ class LookupModel extends Eloquent $className = get_called_class(); $className = str_replace('Lookup', '', $className); $key = sprintf('server:%s:%s:%s', $className, $field, $value); + $isUser = $className == 'App\Models\User'; // check if we've cached this lookup if ($server = session($key)) { - static::setDbServer($server); + static::setDbServer($server, $isUser); return; } @@ -78,7 +79,7 @@ class LookupModel extends Eloquent if ($value && $lookupUser = static::where($field, '=', $value)->first()) { $entity = new $className(); $server = $lookupUser->getDbServer(); - static::setDbServer($server); + static::setDbServer($server, $isUser); // check entity is found on the server if (! $entity::where($field, '=', $value)->first()) { @@ -91,10 +92,13 @@ class LookupModel extends Eloquent } } - public static function setDbServer($server) + public static function setDbServer($server, $isUser = false) { - session(['SESSION_USER_DB_SERVER' => $server]); config(['database.default' => $server]); + + if ($isUser) { + session([SESSION_DB_SERVER => $server]); + } } public function getDbServer() diff --git a/app/Ninja/Repositories/AccountRepository.php b/app/Ninja/Repositories/AccountRepository.php index f64cd05566f5..89ea2c1d90c6 100644 --- a/app/Ninja/Repositories/AccountRepository.php +++ b/app/Ninja/Repositories/AccountRepository.php @@ -359,13 +359,12 @@ class AccountRepository $emailSettings = new AccountEmailSettings(); $account->account_email_settings()->save($emailSettings); - $random = strtolower(str_random(RANDOM_KEY_LENGTH)); $user = new User(); $user->registered = true; $user->confirmed = true; - $user->email = 'contact@invoiceninja.com'; - $user->password = $random; - $user->username = $random; + $user->email = NINJA_ACCOUNT_EMAIL; + $user->username = NINJA_ACCOUNT_EMAIL; + $user->password = strtolower(str_random(RANDOM_KEY_LENGTH)); $user->first_name = 'Invoice'; $user->last_name = 'Ninja'; $user->notify_sent = true;