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 Fisharebest\Webtrees\Database; |
||
19 | use Fisharebest\Webtrees\DebugBar; |
||
20 | use Fisharebest\Webtrees\Exceptions\Handler; |
||
21 | use Fisharebest\Webtrees\Http\Controllers\SetupController; |
||
22 | use Fisharebest\Webtrees\Http\Middleware\CheckCsrf; |
||
23 | use Fisharebest\Webtrees\Http\Middleware\CheckForMaintenanceMode; |
||
24 | use Fisharebest\Webtrees\Http\Middleware\DebugBarData; |
||
25 | use Fisharebest\Webtrees\Http\Middleware\Housekeeping; |
||
26 | use Fisharebest\Webtrees\Http\Middleware\MiddlewareInterface; |
||
27 | use Fisharebest\Webtrees\Http\Middleware\UseLocale; |
||
28 | use Fisharebest\Webtrees\Http\Middleware\UseSession; |
||
29 | use Fisharebest\Webtrees\Http\Middleware\UseTheme; |
||
30 | use Fisharebest\Webtrees\Http\Middleware\UseTransaction; |
||
31 | use Fisharebest\Webtrees\I18N; |
||
32 | use Fisharebest\Webtrees\Services\MigrationService; |
||
33 | use Fisharebest\Webtrees\Services\ModuleService; |
||
34 | use Fisharebest\Webtrees\Services\TimeoutService; |
||
35 | use Fisharebest\Webtrees\Site; |
||
36 | use Fisharebest\Webtrees\Tree; |
||
37 | use Fisharebest\Webtrees\View; |
||
0 ignored issues
–
show
|
|||
38 | use Fisharebest\Webtrees\Webtrees; |
||
39 | use Illuminate\Cache\ArrayStore; |
||
40 | use Illuminate\Cache\Repository; |
||
41 | use League\Flysystem\Adapter\Local; |
||
42 | use League\Flysystem\Cached\CachedAdapter; |
||
43 | use League\Flysystem\Cached\Storage\Memory; |
||
44 | use League\Flysystem\Filesystem; |
||
45 | use League\Flysystem\FilesystemInterface; |
||
46 | 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.
![]() |
|||
47 | 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.
![]() |
|||
48 | |||
49 | require __DIR__ . '/vendor/autoload.php'; |
||
50 | |||
51 | const WT_ROOT = __DIR__ . DIRECTORY_SEPARATOR; |
||
52 | |||
53 | Webtrees::init(); |
||
54 | |||
55 | // Initialise the DebugBar for development. |
||
56 | // Use `composer install --dev` on a development build to enable. |
||
57 | // Note that you may need to increase the size of the fcgi buffers on nginx. |
||
58 | // e.g. add these lines to your fastcgi_params file: |
||
59 | // fastcgi_buffers 16 16m; |
||
60 | // fastcgi_buffer_size 32m; |
||
61 | DebugBar::init(class_exists('\\DebugBar\\StandardDebugBar')); |
||
62 | |||
63 | // Use an array cache for database calls, etc. |
||
64 | app()->instance('cache.array', new Repository(new ArrayStore())); |
||
65 | |||
66 | // Start the timer. |
||
67 | app()->instance(TimeoutService::class, new TimeoutService(microtime(true))); |
||
68 | |||
69 | // Extract the request parameters. |
||
70 | $request = Request::createFromGlobals(); |
||
71 | app()->instance(Request::class, $request); |
||
72 | |||
73 | // Dummy value, until we have created our first tree. |
||
74 | app()->bind(Tree::class, function () { |
||
75 | return null; |
||
76 | }); |
||
77 | |||
78 | // Calculate the base URL, so we can generate absolute URLs. |
||
79 | $request_uri = $request->getSchemeAndHttpHost() . $request->getRequestUri(); |
||
80 | |||
81 | // Remove any PHP script name and parameters. |
||
82 | $base_uri = preg_replace('/[^\/]+\.php(\?.*)?$/', '', $request_uri); |
||
83 | define('WT_BASE_URL', $base_uri); |
||
84 | |||
85 | // Connect to the database |
||
86 | try { |
||
87 | // No config file? Run the setup wizard |
||
88 | if (!file_exists(Webtrees::CONFIG_FILE)) { |
||
89 | define('WT_DATA_DIR', 'data/'); |
||
90 | /** @var SetupController $controller */ |
||
91 | $controller = app()->make(SetupController::class); |
||
92 | $response = $controller->setup($request); |
||
93 | $response->prepare($request)->send(); |
||
94 | |||
95 | return; |
||
96 | } |
||
97 | |||
98 | $database_config = parse_ini_file(Webtrees::CONFIG_FILE); |
||
99 | |||
100 | if ($database_config === false) { |
||
101 | throw new Exception('Invalid config file: ' . Webtrees::CONFIG_FILE); |
||
102 | } |
||
103 | |||
104 | DebugBar::startMeasure('init database'); |
||
105 | |||
106 | // Read the connection settings and create the database |
||
107 | Database::connect($database_config); |
||
108 | |||
109 | // Update the database schema, if necessary. |
||
110 | app()->make(MigrationService::class) |
||
111 | ->updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION); |
||
112 | |||
113 | DebugBar::stopMeasure('init database'); |
||
114 | |||
115 | } catch (PDOException $exception) { |
||
116 | defined('WT_DATA_DIR') || define('WT_DATA_DIR', 'data/'); |
||
117 | I18N::init(); |
||
118 | if ($exception->getCode() === 1045) { |
||
119 | // Error during connection? |
||
120 | $content = view('errors/database-connection', ['error' => $exception->getMessage()]); |
||
121 | } else { |
||
122 | // Error in a migration script? |
||
123 | $content = view('errors/database-error', ['error' => $exception->getMessage()]); |
||
124 | } |
||
125 | $html = view('layouts/error', ['content' => $content]); |
||
126 | $response = new Response($html, Response::HTTP_SERVICE_UNAVAILABLE); |
||
127 | $response->prepare($request)->send(); |
||
128 | |||
129 | return; |
||
130 | } catch (Throwable $exception) { |
||
131 | defined('WT_DATA_DIR') || define('WT_DATA_DIR', 'data/'); |
||
132 | I18N::init(); |
||
133 | $content = view('errors/database-connection', ['error' => $exception->getMessage()]); |
||
134 | $html = view('layouts/error', ['content' => $content]); |
||
135 | $response = new Response($html, Response::HTTP_SERVICE_UNAVAILABLE); |
||
136 | $response->prepare($request)->send(); |
||
137 | |||
138 | return; |
||
139 | } |
||
140 | |||
141 | // The config.ini.php file must always be in a fixed location. |
||
142 | // Other user files can be stored elsewhere... |
||
143 | define('WT_DATA_DIR', realpath(Site::getPreference('INDEX_DIRECTORY', 'data/')) . DIRECTORY_SEPARATOR); |
||
144 | |||
145 | $filesystem = new Filesystem(new CachedAdapter(new Local(WT_DATA_DIR), new Memory())); |
||
146 | |||
147 | // Request more resources - if we can/want to |
||
148 | $memory_limit = Site::getPreference('MEMORY_LIMIT'); |
||
149 | if ($memory_limit !== '' && strpos(ini_get('disable_functions'), 'ini_set') === false) { |
||
150 | ini_set('memory_limit', $memory_limit); |
||
151 | } |
||
152 | $max_execution_time = Site::getPreference('MAX_EXECUTION_TIME'); |
||
153 | if ($max_execution_time !== '' && strpos(ini_get('disable_functions'), 'set_time_limit') === false) { |
||
154 | set_time_limit((int) $max_execution_time); |
||
155 | } |
||
156 | |||
157 | try { |
||
158 | // Most requests will need the current tree and user. |
||
159 | $tree = Tree::findByName($request->get('ged')) ?? null; |
||
160 | |||
161 | // No tree specified/available? Choose one. |
||
162 | if ($tree === null && $request->getMethod() === Request::METHOD_GET) { |
||
163 | $tree = Tree::findByName(Site::getPreference('DEFAULT_GEDCOM')) ?? array_values(Tree::getAll())[0] ?? null; |
||
164 | } |
||
165 | |||
166 | // Most layouts will require a tree for the page header/footer |
||
167 | View::share('tree', $tree); |
||
168 | |||
169 | app()->instance(Tree::class, $tree); |
||
170 | app()->instance(FilesystemInterface::class, $filesystem); |
||
171 | |||
172 | $middleware_stack = [ |
||
173 | app()->make(CheckForMaintenanceMode::class), |
||
174 | app()->make(UseSession::class), |
||
175 | app()->make(UseLocale::class), |
||
176 | ]; |
||
177 | |||
178 | if (class_exists(DebugBar::class)) { |
||
179 | $middleware_stack[] = app()->make(DebugBarData::class); |
||
180 | } |
||
181 | |||
182 | if ($request->getMethod() === Request::METHOD_GET) { |
||
183 | $middleware_stack[] = app()->make(Housekeeping::class); |
||
184 | $middleware_stack[] = app()->make(UseTheme::class); |
||
185 | } |
||
186 | |||
187 | if ($request->getMethod() === Request::METHOD_POST) { |
||
188 | $middleware_stack[] = app()->make(UseTransaction::class); |
||
189 | $middleware_stack[] = app()->make(CheckCsrf::class); |
||
190 | } |
||
191 | |||
192 | // Allow modules to provide middleware. |
||
193 | foreach (app()->make(ModuleService::class)->findByInterface(MiddlewareInterface::class) as $middleware) { |
||
194 | $middleware_stack[] = $middleware; |
||
195 | } |
||
196 | |||
197 | // We build the "onion" from the inside outwards, and some middlewares are dependant on others. |
||
198 | $middleware_stack = array_reverse($middleware_stack); |
||
199 | |||
200 | // Apply the middleware using the "onion" pattern. |
||
201 | $pipeline = array_reduce($middleware_stack, function (Closure $next, MiddlewareInterface $middleware): Closure { |
||
202 | // Create a closure to apply the middleware. |
||
203 | return function (Request $request) use ($middleware, $next): Response { |
||
204 | return $middleware->handle($request, $next); |
||
205 | }; |
||
206 | }, function (Request $request): Response { |
||
207 | // Load the route and routing table. |
||
208 | $route = $request->get('route'); |
||
209 | $routes = require 'routes/web.php'; |
||
210 | |||
211 | // Find the controller and action for the selected route |
||
212 | $controller_action = $routes[$request->getMethod() . ':' . $route] ?? 'ErrorController@noRouteFound'; |
||
213 | [$controller_name, $action] = explode('@', $controller_action); |
||
214 | $controller_class = '\\Fisharebest\\Webtrees\\Http\\Controllers\\' . $controller_name; |
||
215 | |||
216 | $controller = app()->make($controller_class); |
||
217 | |||
218 | return app()->dispatch($controller, $action); |
||
219 | }); |
||
220 | |||
221 | $response = call_user_func($pipeline, $request); |
||
222 | } catch (Exception $exception) { |
||
223 | $response = (new Handler())->render($request, $exception); |
||
224 | } |
||
225 | |||
226 | // Send response |
||
227 | $response->prepare($request)->send(); |
||
228 |
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: