firefly-iii/app/Http/Middleware/Installer.php

97 lines
2.8 KiB
PHP
Raw Normal View History

2018-03-07 13:21:36 -06:00
<?php
declare(strict_types=1);
2018-03-07 13:21:36 -06:00
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)
{
2018-03-10 15:38:20 -06:00
if (env('APP_ENV') === 'testing') {
return $next($request);
}
2018-03-10 15:38:20 -06:00
$url = $request->url();
2018-03-07 13:21:36 -06:00
$strpos = stripos($url, '/install');
if (!($strpos === false)) {
2018-03-07 13:56:52 -06:00
Log::debug(sprintf('URL is %s, will NOT run installer middleware', $url));
2018-03-10 15:38:20 -06:00
2018-03-07 13:21:36 -06:00
return $next($request);
}
2018-03-24 12:55:02 -05:00
// Log::debug(sprintf('URL is %s, will run installer middleware', $url));
2018-03-07 13:21:36 -06:00
// 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.');
2018-03-10 00:17:05 -06:00
return response()->redirectTo(route('installer.index'));
2018-03-07 13:21:36 -06:00
}
throw new FireflyException(sprintf('Could not access the database: %s', $message));
}
// older version in config than database?
2018-04-02 08:10:40 -05:00
$configVersion = (int)config('firefly.db_version');
$dbVersion = (int)FireflyConfig::getFresh('db_version', 1)->data;
2018-03-07 13:21:36 -06:00
if ($configVersion > $dbVersion) {
2018-03-10 15:38:20 -06:00
Log::warning(
sprintf(
'The current installed version (%d) is older than the required version (%d). Redirect to migrate routine.', $dbVersion, $configVersion
)
);
2018-03-07 13:21:36 -06:00
// redirect to migrate routine:
2018-03-10 00:17:05 -06:00
return response()->redirectTo(route('installer.index'));
2018-03-07 13:21:36 -06:00
}
2018-03-10 15:38:20 -06:00
2018-03-07 13:21:36 -06:00
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);
}
}