2019-07-12 07:03:30 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-07-12 07:03:30 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-07-12 07:03:30 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-07-12 07:03:30 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
|
|
|
|
use App\Libraries\MultiDB;
|
|
|
|
use Closure;
|
2020-10-28 11:10:49 +01:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use stdClass;
|
2019-07-12 07:03:30 +02:00
|
|
|
|
|
|
|
class SetDomainNameDb
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Handle an incoming request.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param Request $request
|
|
|
|
* @param Closure $next
|
2019-07-12 07:03:30 +02:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle($request, Closure $next)
|
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
$error = [
|
2022-06-21 11:57:17 +02:00
|
|
|
'message' => 'Invalid token',
|
2024-01-14 05:05:00 +01:00
|
|
|
'errors' => new stdClass(),
|
2022-06-21 11:57:17 +02:00
|
|
|
];
|
2019-12-30 22:59:12 +01:00
|
|
|
/*
|
2019-07-12 07:03:30 +02:00
|
|
|
* Use the host name to set the active DB
|
|
|
|
**/
|
2021-05-15 05:31:41 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! config('ninja.db.multi_db_enabled')) {
|
2021-05-15 05:31:41 +02:00
|
|
|
return $next($request);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-05-15 05:31:41 +02:00
|
|
|
|
2021-12-07 12:14:46 +01:00
|
|
|
$domain_name = $request->getHost();
|
2021-05-15 05:31:41 +02:00
|
|
|
|
2023-11-24 00:23:40 +01:00
|
|
|
if (strpos($domain_name, config('ninja.app_domain')) !== false) {
|
2021-12-07 12:14:46 +01:00
|
|
|
$subdomain = explode('.', $domain_name)[0];
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-05-15 05:31:41 +02:00
|
|
|
$query = [
|
|
|
|
'subdomain' => $subdomain,
|
|
|
|
'portal_mode' => 'subdomain',
|
|
|
|
];
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($company = MultiDB::findAndSetDbByDomain($query)) {
|
2021-12-09 11:50:29 +01:00
|
|
|
session()->put('company_key', $company->company_key);
|
2022-06-21 11:57:17 +02:00
|
|
|
} else {
|
2021-05-15 05:31:41 +02:00
|
|
|
if ($request->json) {
|
2022-06-21 11:57:17 +02:00
|
|
|
return response()->json($error, 403);
|
2021-05-15 05:31:41 +02:00
|
|
|
} else {
|
2021-05-16 11:44:44 +02:00
|
|
|
MultiDB::setDb('db-ninja-01');
|
2022-06-21 11:57:17 +02:00
|
|
|
nlog('I could not set the DB - defaulting to DB1');
|
2021-05-16 11:44:44 +02:00
|
|
|
//abort(400, 'Domain not found');
|
2021-05-15 05:31:41 +02:00
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
} else {
|
|
|
|
$query = [
|
2021-05-15 06:38:32 +02:00
|
|
|
'portal_domain' => $request->getSchemeAndHttpHost(),
|
2021-05-15 05:31:41 +02:00
|
|
|
'portal_mode' => 'domain',
|
|
|
|
];
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($company = MultiDB::findAndSetDbByDomain($query)) {
|
2021-12-09 11:50:29 +01:00
|
|
|
session()->put('company_key', $company->company_key);
|
2022-06-21 11:57:17 +02:00
|
|
|
} else {
|
2021-05-15 05:31:41 +02:00
|
|
|
if ($request->json) {
|
2022-06-21 11:57:17 +02:00
|
|
|
return response()->json($error, 403);
|
2021-05-15 05:31:41 +02:00
|
|
|
} else {
|
2021-05-16 11:44:44 +02:00
|
|
|
MultiDB::setDb('db-ninja-01');
|
2022-06-21 11:57:17 +02:00
|
|
|
nlog('I could not set the DB - defaulting to DB1');
|
2021-05-15 05:31:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-12 07:03:30 +02:00
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
}
|