mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-09 23:15:45 -06:00
87 lines
2.5 KiB
PHP
87 lines
2.5 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace FireflyIII\Http\Middleware;
|
||
|
|
||
|
use Closure;
|
||
|
use DB;
|
||
|
use FireflyConfig;
|
||
|
use FireflyIII\Exceptions\FireflyException;
|
||
|
use Illuminate\Database\QueryException;
|
||
|
use Log;
|
||
|
|
||
|
/**
|
||
|
* Class Installer
|
||
|
*/
|
||
|
class Installer
|
||
|
{
|
||
|
/**
|
||
|
* Handle an incoming request.
|
||
|
*
|
||
|
* @throws FireflyException
|
||
|
*
|
||
|
* @param \Illuminate\Http\Request $request
|
||
|
* @param \Closure $next
|
||
|
*
|
||
|
* @return mixed
|
||
|
*/
|
||
|
public function handle($request, Closure $next)
|
||
|
{
|
||
|
$url = $request->url();
|
||
|
$strpos = stripos($url, '/install');
|
||
|
if (!($strpos === false)) {
|
||
|
return $next($request);
|
||
|
}
|
||
|
|
||
|
// no tables present?
|
||
|
try {
|
||
|
DB::table('users')->count();
|
||
|
} catch (QueryException $e) {
|
||
|
$message = $e->getMessage();
|
||
|
Log::error('Access denied: ' . $message);
|
||
|
if ($this->isAccessDenied($message)) {
|
||
|
throw new FireflyException('It seems your database configuration is not correct. Please verify the username and password in your .env file.');
|
||
|
}
|
||
|
if ($this->noTablesExist($message)) {
|
||
|
// redirect to UpdateController
|
||
|
Log::warning('There are no Firefly III tables present. Redirect to migrate routine.');
|
||
|
|
||
|
return response()->redirectTo(route('installer.migrate'));
|
||
|
}
|
||
|
throw new FireflyException(sprintf('Could not access the database: %s', $message));
|
||
|
}
|
||
|
|
||
|
// older version in config than database?
|
||
|
$configVersion = intval(config('firefly.db_version'));
|
||
|
$dbVersion = intval(FireflyConfig::get('db_version', 1)->data);
|
||
|
if ($configVersion > $dbVersion) {
|
||
|
Log::warning(sprintf(
|
||
|
'The current installed version (%d) is older than the required version (%d). Redirect to migrate routine.', $dbVersion, $configVersion
|
||
|
));
|
||
|
|
||
|
// redirect to migrate routine:
|
||
|
return response()->redirectTo(route('installer.migrate'));
|
||
|
}
|
||
|
return $next($request);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param string $message
|
||
|
*
|
||
|
* @return bool
|
||
|
*/
|
||
|
protected function isAccessDenied(string $message): bool
|
||
|
{
|
||
|
return !(stripos($message, 'Access denied') === false);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param string $message
|
||
|
*
|
||
|
* @return bool
|
||
|
*/
|
||
|
protected function noTablesExist(string $message): bool
|
||
|
{
|
||
|
return !(stripos($message, 'Base table or view not found') === false);
|
||
|
}
|
||
|
}
|