1 | <?php |
||||
2 | /** |
||||
3 | * webtrees: online genealogy |
||||
4 | * Copyright (C) 2019 webtrees development team |
||||
5 | * This program is free software: you can redistribute it and/or modify |
||||
6 | * it under the terms of the GNU General Public License as published by |
||||
7 | * the Free Software Foundation, either version 3 of the License, or |
||||
8 | * (at your option) any later version. |
||||
9 | * This program is distributed in the hope that it will be useful, |
||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
12 | * GNU General Public License for more details. |
||||
13 | * You should have received a copy of the GNU General Public License |
||||
14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
15 | */ |
||||
16 | declare(strict_types=1); |
||||
17 | |||||
18 | use Carbon\Carbon; |
||||
19 | use Fisharebest\Localization\Locale as WebtreesLocale; |
||||
20 | use Fisharebest\Localization\Locale\LocaleInterface; |
||||
21 | use Fisharebest\Webtrees\Auth; |
||||
0 ignored issues
–
show
|
|||||
22 | use Fisharebest\Webtrees\Contracts\UserInterface; |
||||
23 | use Fisharebest\Webtrees\Database; |
||||
24 | use Fisharebest\Webtrees\DebugBar; |
||||
25 | use Fisharebest\Webtrees\Exceptions\Handler; |
||||
26 | use Fisharebest\Webtrees\Http\Controllers\SetupController; |
||||
27 | use Fisharebest\Webtrees\Http\Middleware\CheckCsrf; |
||||
28 | use Fisharebest\Webtrees\Http\Middleware\CheckForMaintenanceMode; |
||||
29 | use Fisharebest\Webtrees\Http\Middleware\DebugBarData; |
||||
30 | use Fisharebest\Webtrees\Http\Middleware\Housekeeping; |
||||
31 | use Fisharebest\Webtrees\Http\Middleware\MiddlewareInterface; |
||||
32 | use Fisharebest\Webtrees\Http\Middleware\UseTransaction; |
||||
33 | use Fisharebest\Webtrees\I18N; |
||||
34 | use Fisharebest\Webtrees\Module\ModuleThemeInterface; |
||||
35 | use Fisharebest\Webtrees\Module\WebtreesTheme; |
||||
36 | use Fisharebest\Webtrees\Services\MigrationService; |
||||
37 | use Fisharebest\Webtrees\Services\ModuleService; |
||||
38 | use Fisharebest\Webtrees\Services\TimeoutService; |
||||
39 | use Fisharebest\Webtrees\Session; |
||||
0 ignored issues
–
show
This use statement conflicts with another class in this namespace,
Session . Consider defining an alias.
Let?s assume that you have a directory layout like this: .
|-- OtherDir
| |-- Bar.php
| `-- Foo.php
`-- SomeDir
`-- Foo.php
and let?s assume the following content of // Bar.php
namespace OtherDir;
use SomeDir\Foo; // This now conflicts the class OtherDir\Foo
If both files PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as // Bar.php
namespace OtherDir;
use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
![]() |
|||||
40 | use Fisharebest\Webtrees\Site; |
||||
41 | use Fisharebest\Webtrees\Tree; |
||||
42 | use Fisharebest\Webtrees\View; |
||||
0 ignored issues
–
show
This use statement conflicts with another class in this namespace,
View . Consider defining an alias.
Let?s assume that you have a directory layout like this: .
|-- OtherDir
| |-- Bar.php
| `-- Foo.php
`-- SomeDir
`-- Foo.php
and let?s assume the following content of // Bar.php
namespace OtherDir;
use SomeDir\Foo; // This now conflicts the class OtherDir\Foo
If both files PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as // Bar.php
namespace OtherDir;
use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
![]() |
|||||
43 | use Fisharebest\Webtrees\Webtrees; |
||||
44 | use Illuminate\Cache\ArrayStore; |
||||
45 | use Illuminate\Cache\Repository; |
||||
46 | use Illuminate\Support\Collection; |
||||
47 | use League\Flysystem\Adapter\Local; |
||||
48 | use League\Flysystem\Cached\CachedAdapter; |
||||
49 | use League\Flysystem\Cached\Storage\Memory; |
||||
50 | use League\Flysystem\Filesystem; |
||||
51 | use Symfony\Component\HttpFoundation\Request; |
||||
0 ignored issues
–
show
This use statement conflicts with another class in this namespace,
Request . Consider defining an alias.
Let?s assume that you have a directory layout like this: .
|-- OtherDir
| |-- Bar.php
| `-- Foo.php
`-- SomeDir
`-- Foo.php
and let?s assume the following content of // Bar.php
namespace OtherDir;
use SomeDir\Foo; // This now conflicts the class OtherDir\Foo
If both files PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as // Bar.php
namespace OtherDir;
use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
![]() |
|||||
52 | use Symfony\Component\HttpFoundation\Response; |
||||
0 ignored issues
–
show
This use statement conflicts with another class in this namespace,
Response . Consider defining an alias.
Let?s assume that you have a directory layout like this: .
|-- OtherDir
| |-- Bar.php
| `-- Foo.php
`-- SomeDir
`-- Foo.php
and let?s assume the following content of // Bar.php
namespace OtherDir;
use SomeDir\Foo; // This now conflicts the class OtherDir\Foo
If both files PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as // Bar.php
namespace OtherDir;
use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
![]() |
|||||
53 | |||||
54 | require __DIR__ . '/vendor/autoload.php'; |
||||
55 | |||||
56 | const WT_ROOT = __DIR__ . DIRECTORY_SEPARATOR; |
||||
57 | |||||
58 | Webtrees::init(); |
||||
59 | |||||
60 | // Initialise the DebugBar for development. |
||||
61 | // Use `composer install --dev` on a development build to enable. |
||||
62 | // Note that you may need to increase the size of the fcgi buffers on nginx. |
||||
63 | // e.g. add these lines to your fastcgi_params file: |
||||
64 | // fastcgi_buffers 16 16m; |
||||
65 | // fastcgi_buffer_size 32m; |
||||
66 | DebugBar::init(class_exists('\\DebugBar\\StandardDebugBar')); |
||||
67 | |||||
68 | // Use an array cache for database calls, etc. |
||||
69 | app()->instance('cache.array', new Repository(new ArrayStore())); |
||||
70 | |||||
71 | // Extract the request parameters. |
||||
72 | $request = Request::createFromGlobals(); |
||||
73 | app()->instance(Request::class, $request); |
||||
74 | |||||
75 | // Dummy value, until we have created our first tree. |
||||
76 | app()->bind(Tree::class, function () { |
||||
77 | return null; |
||||
78 | }); |
||||
79 | |||||
80 | // Calculate the base URL, so we can generate absolute URLs. |
||||
81 | $request_uri = $request->getSchemeAndHttpHost() . $request->getRequestUri(); |
||||
82 | |||||
83 | // Remove any PHP script name and parameters. |
||||
84 | $base_uri = preg_replace('/[^\/]+\.php(\?.*)?$/', '', $request_uri); |
||||
85 | define('WT_BASE_URL', $base_uri); |
||||
86 | |||||
87 | DebugBar::startMeasure('init database'); |
||||
88 | |||||
89 | // Connect to the database |
||||
90 | try { |
||||
91 | // No config file? Run the setup wizard |
||||
92 | if (!file_exists(Webtrees::CONFIG_FILE)) { |
||||
93 | define('WT_DATA_DIR', 'data/'); |
||||
94 | /** @var SetupController $controller */ |
||||
95 | $controller = app()->make(SetupController::class); |
||||
96 | $response = $controller->setup($request); |
||||
97 | $response->prepare($request)->send(); |
||||
98 | |||||
99 | return; |
||||
100 | } |
||||
101 | |||||
102 | $database_config = parse_ini_file(Webtrees::CONFIG_FILE); |
||||
103 | |||||
104 | if ($database_config === false) { |
||||
105 | throw new Exception('Invalid config file: ' . Webtrees::CONFIG_FILE); |
||||
106 | } |
||||
107 | |||||
108 | // Read the connection settings and create the database |
||||
109 | Database::connect($database_config); |
||||
110 | |||||
111 | // Update the database schema, if necessary. |
||||
112 | app()->make(MigrationService::class) |
||||
113 | ->updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION); |
||||
114 | } catch (PDOException $exception) { |
||||
115 | defined('WT_DATA_DIR') || define('WT_DATA_DIR', 'data/'); |
||||
116 | I18N::init(); |
||||
117 | if ($exception->getCode() === 1045) { |
||||
118 | // Error during connection? |
||||
119 | $content = view('errors/database-connection', ['error' => $exception->getMessage()]); |
||||
120 | } else { |
||||
121 | // Error in a migration script? |
||||
122 | $content = view('errors/database-error', ['error' => $exception->getMessage()]); |
||||
123 | } |
||||
124 | $html = view('layouts/error', ['content' => $content]); |
||||
125 | $response = new Response($html, Response::HTTP_SERVICE_UNAVAILABLE); |
||||
126 | $response->prepare($request)->send(); |
||||
127 | |||||
128 | return; |
||||
129 | } catch (Throwable $exception) { |
||||
130 | defined('WT_DATA_DIR') || define('WT_DATA_DIR', 'data/'); |
||||
131 | I18N::init(); |
||||
132 | $content = view('errors/database-connection', ['error' => $exception->getMessage()]); |
||||
133 | $html = view('layouts/error', ['content' => $content]); |
||||
134 | $response = new Response($html, Response::HTTP_SERVICE_UNAVAILABLE); |
||||
135 | $response->prepare($request)->send(); |
||||
136 | |||||
137 | return; |
||||
138 | } |
||||
139 | |||||
140 | DebugBar::stopMeasure('init database'); |
||||
141 | |||||
142 | // The config.ini.php file must always be in a fixed location. |
||||
143 | // Other user files can be stored elsewhere... |
||||
144 | define('WT_DATA_DIR', realpath(Site::getPreference('INDEX_DIRECTORY', 'data/')) . DIRECTORY_SEPARATOR); |
||||
145 | |||||
146 | $filesystem = new Filesystem(new CachedAdapter(new Local(WT_DATA_DIR), new Memory())); |
||||
147 | |||||
148 | // Request more resources - if we can/want to |
||||
149 | $memory_limit = Site::getPreference('MEMORY_LIMIT'); |
||||
150 | if ($memory_limit !== '' && strpos(ini_get('disable_functions'), 'ini_set') === false) { |
||||
151 | ini_set('memory_limit', $memory_limit); |
||||
152 | } |
||||
153 | $max_execution_time = Site::getPreference('MAX_EXECUTION_TIME'); |
||||
154 | if ($max_execution_time !== '' && strpos(ini_get('disable_functions'), 'set_time_limit') === false) { |
||||
155 | set_time_limit((int) $max_execution_time); |
||||
156 | } |
||||
157 | |||||
158 | // Sessions |
||||
159 | Session::start(); |
||||
160 | |||||
161 | // Update the last-login time no more than once a minute. |
||||
162 | $next_session_update = Carbon::createFromTimestamp((int) Session::get('session_time_updates'))->addMinute(); |
||||
163 | if ($next_session_update < Carbon::now()) { |
||||
164 | $timestamp_now = Carbon::now()->timestamp; |
||||
165 | |||||
166 | if (Session::get('masquerade') === null) { |
||||
167 | Auth::user()->setPreference('sessiontime', (string) $timestamp_now); |
||||
168 | } |
||||
169 | Session::put('session_time_updates', $timestamp_now); |
||||
170 | } |
||||
171 | |||||
172 | DebugBar::startMeasure('routing'); |
||||
173 | |||||
174 | try { |
||||
175 | // Most requests will need the current tree and user. |
||||
176 | $tree = Tree::findByName($request->get('ged')) ?? null; |
||||
177 | |||||
178 | // No tree specified/available? Choose one. |
||||
179 | if ($tree === null && $request->getMethod() === Request::METHOD_GET) { |
||||
180 | $tree = Tree::findByName(Site::getPreference('DEFAULT_GEDCOM')) ?? array_values(Tree::getAll())[0] ?? null; |
||||
181 | } |
||||
182 | |||||
183 | // Select a locale |
||||
184 | define('WT_LOCALE', I18N::init('', $tree)); |
||||
185 | Session::put('locale', WT_LOCALE); |
||||
186 | |||||
187 | // Most layouts will require a tree for the page header/footer |
||||
188 | View::share('tree', $tree); |
||||
189 | |||||
190 | // Load the route and routing table. |
||||
191 | $route = $request->get('route'); |
||||
192 | $routes = require 'routes/web.php'; |
||||
193 | |||||
194 | // Find the controller and action for the selected route |
||||
195 | $controller_action = $routes[$request->getMethod() . ':' . $route] ?? 'ErrorController@noRouteFound'; |
||||
196 | [$controller_name, $action] = explode('@', $controller_action); |
||||
197 | $controller_class = '\\Fisharebest\\Webtrees\\Http\\Controllers\\' . $controller_name; |
||||
198 | |||||
199 | app()->instance(Tree::class, $tree); |
||||
200 | app()->instance(UserInterface::class, Auth::user()); |
||||
201 | app()->instance(LocaleInterface::class, WebtreesLocale::create(WT_LOCALE)); |
||||
202 | app()->instance(TimeoutService::class, new TimeoutService(microtime(true))); |
||||
203 | app()->instance(Filesystem::class, $filesystem); |
||||
204 | |||||
205 | $controller = app()->make($controller_class); |
||||
206 | |||||
207 | DebugBar::stopMeasure('routing'); |
||||
208 | |||||
209 | DebugBar::startMeasure('init theme'); |
||||
210 | |||||
211 | /** @var Collection|ModuleThemeInterface[] $themes */ |
||||
212 | $themes = app()->make(ModuleService::class)->findByInterface(ModuleThemeInterface::class); |
||||
213 | |||||
214 | // Last theme used? |
||||
215 | $theme = $themes->get(Session::get('theme_id', '')); |
||||
216 | |||||
217 | // Default for tree? |
||||
218 | if ($theme === null && $tree instanceof Tree) { |
||||
219 | $theme = $themes->get($tree->getPreference('THEME_DIR')); |
||||
220 | } |
||||
221 | |||||
222 | // Default for site? |
||||
223 | if ($theme === null) { |
||||
224 | $theme = $themes->get(Site::getPreference('THEME_DIR')); |
||||
225 | } |
||||
226 | |||||
227 | // Default |
||||
228 | if ($theme === null) { |
||||
229 | $theme = app()->make(WebtreesTheme::class); |
||||
230 | } |
||||
231 | |||||
232 | // Bind this theme into the container |
||||
233 | app()->instance(ModuleThemeInterface::class, $theme); |
||||
234 | |||||
235 | // Remember this setting |
||||
236 | Session::put('theme_id', $theme->name()); |
||||
237 | |||||
238 | DebugBar::stopMeasure('init theme'); |
||||
239 | |||||
240 | // Note that we can't stop this timer, as running the action will |
||||
241 | // generate the response - which includes (and stops) the timer |
||||
242 | DebugBar::startMeasure('controller_action'); |
||||
243 | |||||
244 | $middleware_stack = [ |
||||
245 | CheckForMaintenanceMode::class, |
||||
246 | ]; |
||||
247 | |||||
248 | if (class_exists(DebugBar::class)) { |
||||
249 | $middleware_stack[] = DebugBarData::class; |
||||
250 | } |
||||
251 | |||||
252 | if ($request->getMethod() === Request::METHOD_GET) { |
||||
253 | $middleware_stack[] = Housekeeping::class; |
||||
254 | } |
||||
255 | |||||
256 | if ($request->getMethod() === Request::METHOD_POST) { |
||||
257 | $middleware_stack[] = UseTransaction::class; |
||||
258 | $middleware_stack[] = CheckCsrf::class; |
||||
259 | } |
||||
260 | |||||
261 | // Allow modules to provide middleware. |
||||
262 | foreach (app()->make(ModuleService::class)->findByInterface(MiddlewareInterface::class) as $middleware) { |
||||
263 | $middleware[] = $middleware; |
||||
264 | } |
||||
265 | |||||
266 | // Apply the middleware using the "onion" pattern. |
||||
267 | $pipeline = array_reduce($middleware_stack, function (Closure $next, string $middleware): Closure { |
||||
268 | // Create a closure to apply the middleware. |
||||
269 | return function (Request $request) use ($middleware, $next): Response { |
||||
270 | return app()->make($middleware)->handle($request, $next); |
||||
271 | }; |
||||
272 | }, function (Request $request) use ($controller, $action): Response { |
||||
0 ignored issues
–
show
The parameter
$request is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||
273 | return app()->dispatch($controller, $action); |
||||
274 | }); |
||||
275 | |||||
276 | $response = call_user_func($pipeline, $request); |
||||
277 | } catch (Exception $exception) { |
||||
278 | $response = (new Handler())->render($request, $exception); |
||||
279 | } |
||||
280 | |||||
281 | // Send response |
||||
282 | $response->prepare($request)->send(); |
||||
283 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: