1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Installer.php |
||
5 | * Copyright (c) 2018 [email protected] |
||
6 | * |
||
7 | * This file is part of Firefly III. |
||
8 | * |
||
9 | * Firefly III is free software: you can redistribute it and/or modify |
||
10 | * it under the terms of the GNU General Public License as published by |
||
11 | * the Free Software Foundation, either version 3 of the License, or |
||
12 | * (at your option) any later version. |
||
13 | * |
||
14 | * Firefly III is distributed in the hope that it will be useful, |
||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
17 | * GNU General Public License for more details. |
||
18 | * |
||
19 | * You should have received a copy of the GNU General Public License |
||
20 | * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. |
||
21 | */ |
||
22 | |||
23 | declare(strict_types=1); |
||
24 | |||
25 | namespace FireflyIII\Http\Middleware; |
||
26 | |||
27 | use Closure; |
||
28 | use DB; |
||
29 | use FireflyConfig; |
||
30 | use FireflyIII\Exceptions\FireflyException; |
||
31 | use Illuminate\Database\QueryException; |
||
32 | use Log; |
||
33 | |||
34 | /** |
||
35 | * Class Installer |
||
36 | * @codeCoverageIgnore |
||
37 | * |
||
38 | */ |
||
39 | class Installer |
||
40 | { |
||
41 | /** |
||
42 | * Handle an incoming request. |
||
43 | * |
||
44 | * @throws FireflyException |
||
45 | * |
||
46 | * @param \Illuminate\Http\Request $request |
||
47 | * @param \Closure $next |
||
48 | * |
||
49 | * @return mixed |
||
50 | * |
||
51 | * @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
||
52 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
53 | * |
||
54 | */ |
||
55 | public function handle($request, Closure $next) |
||
56 | { |
||
57 | if ('testing' === config('app.env')) { |
||
58 | return $next($request); |
||
59 | } |
||
60 | $url = $request->url(); |
||
61 | $strpos = stripos($url, '/install'); |
||
62 | if (!(false === $strpos)) { |
||
63 | Log::debug(sprintf('URL is %s, will NOT run installer middleware', $url)); |
||
64 | |||
65 | return $next($request); |
||
66 | } |
||
67 | // no tables present? |
||
68 | try { |
||
69 | DB::table('users')->count(); |
||
70 | } catch (QueryException $e) { |
||
71 | $message = $e->getMessage(); |
||
72 | Log::error('Access denied: ' . $message); |
||
73 | if ($this->isAccessDenied($message)) { |
||
74 | throw new FireflyException('It seems your database configuration is not correct. Please verify the username and password in your .env file.'); |
||
75 | } |
||
76 | if ($this->noTablesExist($message)) { |
||
77 | // redirect to UpdateController |
||
78 | Log::warning('There are no Firefly III tables present. Redirect to migrate routine.'); |
||
79 | |||
80 | return response()->redirectTo(route('installer.index')); |
||
81 | } |
||
82 | throw new FireflyException(sprintf('Could not access the database: %s', $message)); |
||
83 | } |
||
84 | |||
85 | // older version in config than database? |
||
86 | $configVersion = (int)config('firefly.db_version'); |
||
87 | $dbVersion = (int)FireflyConfig::getFresh('db_version', 1)->data; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
88 | if ($configVersion > $dbVersion) { |
||
89 | Log::warning( |
||
90 | sprintf( |
||
91 | 'The current installed version (%d) is older than the required version (%d). Redirect to migrate routine.', $dbVersion, $configVersion |
||
92 | ) |
||
93 | ); |
||
94 | |||
95 | // redirect to migrate routine: |
||
96 | return response()->redirectTo(route('installer.index')); |
||
97 | } |
||
98 | |||
99 | return $next($request); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Is access denied error. |
||
104 | * |
||
105 | * @param string $message |
||
106 | * |
||
107 | * @return bool |
||
108 | */ |
||
109 | protected function isAccessDenied(string $message): bool |
||
110 | { |
||
111 | return !(false === stripos($message, 'Access denied')); |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Is no tables exist error. |
||
116 | * |
||
117 | * @param string $message |
||
118 | * |
||
119 | * @return bool |
||
120 | */ |
||
121 | protected function noTablesExist(string $message): bool |
||
122 | { |
||
123 | return !(false === stripos($message, 'Base table or view not found')); |
||
124 | } |
||
125 | } |
||
126 |