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
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-07-12 07:03:30 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
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 = [
|
2019-09-24 00:37:38 +02:00
|
|
|
'message' => 'Invalid token',
|
2020-10-28 11:10:49 +01:00
|
|
|
'errors' => new stdClass,
|
2019-09-24 00:37:38 +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
|
|
|
|
|
|
|
if(!config('ninja.db.multi_db_enabled'))
|
|
|
|
return $next($request);
|
|
|
|
|
|
|
|
|
|
|
|
if (strpos($request->getHost(), 'invoicing.co') !== false)
|
|
|
|
{
|
2021-05-15 06:00:29 +02:00
|
|
|
$subdomain = explode('.', $request->getHost())[0];
|
2021-05-15 05:31:41 +02:00
|
|
|
|
|
|
|
$query = [
|
|
|
|
'subdomain' => $subdomain,
|
|
|
|
'portal_mode' => 'subdomain',
|
|
|
|
];
|
|
|
|
|
2021-05-27 01:14:21 +02:00
|
|
|
if($company = MultiDB::findAndSetDbByDomain($query)){
|
|
|
|
$request->attributes->add(['account_id' => $company->account_id]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-05-15 05:31:41 +02:00
|
|
|
if ($request->json) {
|
|
|
|
return response()->json($error, 403);
|
|
|
|
} else {
|
2021-05-16 11:44:44 +02:00
|
|
|
MultiDB::setDb('db-ninja-01');
|
|
|
|
nlog("I could not set the DB - defaulting to DB1");
|
|
|
|
//abort(400, 'Domain not found');
|
2021-05-15 05:31:41 +02:00
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2021-05-15 05:31:41 +02:00
|
|
|
|
2019-07-12 07:03:30 +02:00
|
|
|
}
|
2021-05-15 05:31:41 +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',
|
|
|
|
];
|
|
|
|
|
2021-05-27 01:14:21 +02:00
|
|
|
if($company = MultiDB::findAndSetDbByDomain($query)){
|
|
|
|
$request->attributes->add(['account_id' => $company->account_id]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-05-15 05:31:41 +02:00
|
|
|
if ($request->json) {
|
|
|
|
return response()->json($error, 403);
|
|
|
|
} else {
|
2021-05-16 11:44:44 +02:00
|
|
|
MultiDB::setDb('db-ninja-01');
|
|
|
|
nlog("I could not set the DB - defaulting to DB1");
|
|
|
|
//abort(400, 'Domain not found');
|
2021-05-15 05:31:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-02 05:31:53 +02:00
|
|
|
// config(['app.url' => $request->getSchemeAndHttpHost()]);
|
2019-07-12 07:03:30 +02:00
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
}
|