fisharebest /
webtrees
| 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\UseTransaction; |
||||
| 32 | use Fisharebest\Webtrees\I18N; |
||||
| 33 | use Fisharebest\Webtrees\Module\ModuleThemeInterface; |
||||
| 34 | use Fisharebest\Webtrees\Module\WebtreesTheme; |
||||
| 35 | use Fisharebest\Webtrees\Services\MigrationService; |
||||
| 36 | use Fisharebest\Webtrees\Services\ModuleService; |
||||
| 37 | use Fisharebest\Webtrees\Services\TimeoutService; |
||||
| 38 | 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.
Loading history...
|
|||||
| 39 | use Fisharebest\Webtrees\Site; |
||||
| 40 | use Fisharebest\Webtrees\Tree; |
||||
| 41 | 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.
Loading history...
|
|||||
| 42 | use Fisharebest\Webtrees\Webtrees; |
||||
| 43 | use Illuminate\Cache\ArrayStore; |
||||
| 44 | use Illuminate\Cache\Repository; |
||||
| 45 | use Illuminate\Support\Collection; |
||||
| 46 | use League\Flysystem\Adapter\Local; |
||||
| 47 | use League\Flysystem\Cached\CachedAdapter; |
||||
| 48 | use League\Flysystem\Cached\Storage\Memory; |
||||
| 49 | use League\Flysystem\Filesystem; |
||||
| 50 | 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.
Loading history...
|
|||||
| 51 | 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.
Loading history...
|
|||||
| 52 | |||||
| 53 | require __DIR__ . '/vendor/autoload.php'; |
||||
| 54 | |||||
| 55 | const WT_ROOT = __DIR__ . DIRECTORY_SEPARATOR; |
||||
| 56 | |||||
| 57 | Webtrees::init(); |
||||
| 58 | |||||
| 59 | // Initialise the DebugBar for development. |
||||
| 60 | // Use `composer install --dev` on a development build to enable. |
||||
| 61 | // Note that you may need to increase the size of the fcgi buffers on nginx. |
||||
| 62 | // e.g. add these lines to your fastcgi_params file: |
||||
| 63 | // fastcgi_buffers 16 16m; |
||||
| 64 | // fastcgi_buffer_size 32m; |
||||
| 65 | DebugBar::init(class_exists('\\DebugBar\\StandardDebugBar')); |
||||
| 66 | |||||
| 67 | // Use an array cache for database calls, etc. |
||||
| 68 | app()->instance('cache.array', new Repository(new ArrayStore())); |
||||
| 69 | |||||
| 70 | // Extract the request parameters. |
||||
| 71 | $request = Request::createFromGlobals(); |
||||
| 72 | app()->instance(Request::class, $request); |
||||
| 73 | |||||
| 74 | // Dummy value, until we have created our first tree. |
||||
| 75 | app()->bind(Tree::class, function () { |
||||
| 76 | return null; |
||||
| 77 | }); |
||||
| 78 | |||||
| 79 | // Calculate the base URL, so we can generate absolute URLs. |
||||
| 80 | $request_uri = $request->getSchemeAndHttpHost() . $request->getRequestUri(); |
||||
| 81 | |||||
| 82 | // Remove any PHP script name and parameters. |
||||
| 83 | $base_uri = preg_replace('/[^\/]+\.php(\?.*)?$/', '', $request_uri); |
||||
| 84 | define('WT_BASE_URL', $base_uri); |
||||
| 85 | |||||
| 86 | DebugBar::startMeasure('init database'); |
||||
| 87 | |||||
| 88 | // Connect to the database |
||||
| 89 | try { |
||||
| 90 | // No config file? Run the setup wizard |
||||
| 91 | if (!file_exists(Webtrees::CONFIG_FILE)) { |
||||
| 92 | define('WT_DATA_DIR', 'data/'); |
||||
| 93 | /** @var SetupController $controller */ |
||||
| 94 | $controller = app()->make(SetupController::class); |
||||
| 95 | $response = $controller->setup($request); |
||||
| 96 | $response->prepare($request)->send(); |
||||
| 97 | |||||
| 98 | return; |
||||
| 99 | } |
||||
| 100 | |||||
| 101 | $database_config = parse_ini_file(Webtrees::CONFIG_FILE); |
||||
| 102 | |||||
| 103 | if ($database_config === false) { |
||||
| 104 | throw new Exception('Invalid config file: ' . Webtrees::CONFIG_FILE); |
||||
| 105 | } |
||||
| 106 | |||||
| 107 | // Read the connection settings and create the database |
||||
| 108 | Database::connect($database_config); |
||||
| 109 | |||||
| 110 | // Update the database schema, if necessary. |
||||
| 111 | app()->make(MigrationService::class) |
||||
| 112 | ->updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION); |
||||
| 113 | } catch (PDOException $exception) { |
||||
| 114 | defined('WT_DATA_DIR') || define('WT_DATA_DIR', 'data/'); |
||||
| 115 | I18N::init(); |
||||
| 116 | if ($exception->getCode() === 1045) { |
||||
| 117 | // Error during connection? |
||||
| 118 | $content = view('errors/database-connection', ['error' => $exception->getMessage()]); |
||||
| 119 | } else { |
||||
| 120 | // Error in a migration script? |
||||
| 121 | $content = view('errors/database-error', ['error' => $exception->getMessage()]); |
||||
| 122 | } |
||||
| 123 | $html = view('layouts/error', ['content' => $content]); |
||||
| 124 | $response = new Response($html, Response::HTTP_SERVICE_UNAVAILABLE); |
||||
| 125 | $response->prepare($request)->send(); |
||||
| 126 | |||||
| 127 | return; |
||||
| 128 | } catch (Throwable $exception) { |
||||
| 129 | defined('WT_DATA_DIR') || define('WT_DATA_DIR', 'data/'); |
||||
| 130 | I18N::init(); |
||||
| 131 | $content = view('errors/database-connection', ['error' => $exception->getMessage()]); |
||||
| 132 | $html = view('layouts/error', ['content' => $content]); |
||||
| 133 | $response = new Response($html, Response::HTTP_SERVICE_UNAVAILABLE); |
||||
| 134 | $response->prepare($request)->send(); |
||||
| 135 | |||||
| 136 | return; |
||||
| 137 | } |
||||
| 138 | |||||
| 139 | DebugBar::stopMeasure('init database'); |
||||
| 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 | // Some broken servers block access to their own temp folder using open_basedir... |
||||
| 148 | $filesystem->createDir('tmp'); |
||||
| 149 | putenv('TMPDIR=' . WT_DATA_DIR . 'tmp'); |
||||
| 150 | Swift_Preferences::getInstance()->setTempDir(WT_DATA_DIR . 'tmp'); |
||||
| 151 | |||||
| 152 | // Request more resources - if we can/want to |
||||
| 153 | $memory_limit = Site::getPreference('MEMORY_LIMIT'); |
||||
| 154 | if ($memory_limit !== '' && strpos(ini_get('disable_functions'), 'ini_set') === false) { |
||||
| 155 | ini_set('memory_limit', $memory_limit); |
||||
| 156 | } |
||||
| 157 | $max_execution_time = Site::getPreference('MAX_EXECUTION_TIME'); |
||||
| 158 | if ($max_execution_time !== '' && strpos(ini_get('disable_functions'), 'set_time_limit') === false) { |
||||
| 159 | set_time_limit((int) $max_execution_time); |
||||
| 160 | } |
||||
| 161 | |||||
| 162 | // Sessions |
||||
| 163 | Session::start(); |
||||
| 164 | |||||
| 165 | define('WT_TIMESTAMP', Carbon::now('UTC')->timestamp); |
||||
| 166 | define('WT_CLIENT_JD', 2440588 + intdiv(WT_TIMESTAMP, 86400)); |
||||
| 167 | |||||
| 168 | // Update the last-login time no more than once a minute |
||||
| 169 | if (WT_TIMESTAMP - Session::get('activity_time') >= 60) { |
||||
| 170 | if (Session::get('masquerade') === null) { |
||||
| 171 | Auth::user()->setPreference('sessiontime', (string) WT_TIMESTAMP); |
||||
| 172 | } |
||||
| 173 | Session::put('activity_time', WT_TIMESTAMP); |
||||
| 174 | } |
||||
| 175 | |||||
| 176 | DebugBar::startMeasure('routing'); |
||||
| 177 | |||||
| 178 | try { |
||||
| 179 | // Most requests will need the current tree and user. |
||||
| 180 | $tree = Tree::findByName($request->get('ged')) ?? null; |
||||
| 181 | |||||
| 182 | // No tree specified/available? Choose one. |
||||
| 183 | if ($tree === null && $request->getMethod() === Request::METHOD_GET) { |
||||
| 184 | $tree = Tree::findByName(Site::getPreference('DEFAULT_GEDCOM')) ?? array_values(Tree::getAll())[0] ?? null; |
||||
| 185 | } |
||||
| 186 | |||||
| 187 | // Select a locale |
||||
| 188 | define('WT_LOCALE', I18N::init('', $tree)); |
||||
| 189 | Session::put('locale', WT_LOCALE); |
||||
| 190 | |||||
| 191 | // Most layouts will require a tree for the page header/footer |
||||
| 192 | View::share('tree', $tree); |
||||
| 193 | |||||
| 194 | // Load the route and routing table. |
||||
| 195 | $route = $request->get('route'); |
||||
| 196 | $routes = require 'routes/web.php'; |
||||
| 197 | |||||
| 198 | // Find the controller and action for the selected route |
||||
| 199 | $controller_action = $routes[$request->getMethod() . ':' . $route] ?? 'ErrorController@noRouteFound'; |
||||
| 200 | [$controller_name, $action] = explode('@', $controller_action); |
||||
| 201 | $controller_class = '\\Fisharebest\\Webtrees\\Http\\Controllers\\' . $controller_name; |
||||
| 202 | |||||
| 203 | app()->instance(Tree::class, $tree); |
||||
| 204 | app()->instance(UserInterface::class, Auth::user()); |
||||
| 205 | app()->instance(LocaleInterface::class, WebtreesLocale::create(WT_LOCALE)); |
||||
| 206 | app()->instance(TimeoutService::class, new TimeoutService(microtime(true))); |
||||
| 207 | app()->instance(Filesystem::class, $filesystem); |
||||
| 208 | |||||
| 209 | $controller = app()->make($controller_class); |
||||
| 210 | |||||
| 211 | DebugBar::stopMeasure('routing'); |
||||
| 212 | |||||
| 213 | DebugBar::startMeasure('init theme'); |
||||
| 214 | |||||
| 215 | /** @var Collection|ModuleThemeInterface[] $themes */ |
||||
| 216 | $themes = app()->make(ModuleService::class)->findByInterface(ModuleThemeInterface::class); |
||||
| 217 | |||||
| 218 | // Last theme used? |
||||
| 219 | $theme = $themes->get(Session::get('theme_id', '')); |
||||
| 220 | |||||
| 221 | // Default for tree? |
||||
| 222 | if ($theme === null && $tree instanceof Tree) { |
||||
| 223 | $theme = $themes->get($tree->getPreference('THEME_DIR')); |
||||
| 224 | } |
||||
| 225 | |||||
| 226 | // Default for site? |
||||
| 227 | if ($theme === null) { |
||||
| 228 | $theme = $themes->get(Site::getPreference('THEME_DIR')); |
||||
| 229 | } |
||||
| 230 | |||||
| 231 | // Default |
||||
| 232 | if ($theme === null) { |
||||
| 233 | $theme = app()->make(WebtreesTheme::class); |
||||
| 234 | } |
||||
| 235 | |||||
| 236 | // Bind this theme into the container |
||||
| 237 | app()->instance(ModuleThemeInterface::class, $theme); |
||||
| 238 | |||||
| 239 | // Remember this setting |
||||
| 240 | Session::put('theme_id', $theme->name()); |
||||
| 241 | |||||
| 242 | DebugBar::stopMeasure('init theme'); |
||||
| 243 | |||||
| 244 | // Note that we can't stop this timer, as running the action will |
||||
| 245 | // generate the response - which includes (and stops) the timer |
||||
| 246 | DebugBar::startMeasure('controller_action'); |
||||
| 247 | |||||
| 248 | $middleware_stack = [ |
||||
| 249 | CheckForMaintenanceMode::class, |
||||
| 250 | ]; |
||||
| 251 | |||||
| 252 | if (class_exists(DebugBar::class)) { |
||||
| 253 | $middleware_stack[] = DebugBarData::class; |
||||
| 254 | } |
||||
| 255 | |||||
| 256 | if ($request->getMethod() === Request::METHOD_GET) { |
||||
| 257 | $middleware_stack[] = Housekeeping::class; |
||||
| 258 | } |
||||
| 259 | |||||
| 260 | if ($request->getMethod() === Request::METHOD_POST) { |
||||
| 261 | $middleware_stack[] = UseTransaction::class; |
||||
| 262 | $middleware_stack[] = CheckCsrf::class; |
||||
| 263 | } |
||||
| 264 | |||||
| 265 | // Apply the middleware using the "onion" pattern. |
||||
| 266 | $pipeline = array_reduce($middleware_stack, function (Closure $next, string $middleware): Closure { |
||||
| 267 | // Create a closure to apply the middleware. |
||||
| 268 | return function (Request $request) use ($middleware, $next): Response { |
||||
| 269 | return app()->make($middleware)->handle($request, $next); |
||||
| 270 | }; |
||||
| 271 | }, 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. Loading history...
|
|||||
| 272 | return app()->dispatch($controller, $action); |
||||
| 273 | }); |
||||
| 274 | |||||
| 275 | $response = call_user_func($pipeline, $request); |
||||
| 276 | } catch (Exception $exception) { |
||||
| 277 | $response = (new Handler())->render($request, $exception); |
||||
| 278 | } |
||||
| 279 | |||||
| 280 | // Send response |
||||
| 281 | $response->prepare($request)->send(); |
||||
| 282 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: