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 Fisharebest\Localization\Locale as WebtreesLocale; |
||||
| 19 | use Fisharebest\Localization\Locale\LocaleInterface; |
||||
| 20 | use Fisharebest\Webtrees\Auth; |
||||
|
0 ignored issues
–
show
|
|||||
| 21 | use Fisharebest\Webtrees\Database; |
||||
| 22 | use Fisharebest\Webtrees\DebugBar; |
||||
| 23 | use Fisharebest\Webtrees\Exceptions\Handler; |
||||
| 24 | use Fisharebest\Webtrees\Http\Controllers\SetupController; |
||||
| 25 | use Fisharebest\Webtrees\Http\Middleware\CheckCsrf; |
||||
| 26 | use Fisharebest\Webtrees\Http\Middleware\CheckForMaintenanceMode; |
||||
| 27 | use Fisharebest\Webtrees\Http\Middleware\DebugBarData; |
||||
| 28 | use Fisharebest\Webtrees\Http\Middleware\Housekeeping; |
||||
| 29 | use Fisharebest\Webtrees\Http\Middleware\PageHitCounter; |
||||
| 30 | use Fisharebest\Webtrees\Http\Middleware\UseTransaction; |
||||
| 31 | use Fisharebest\Webtrees\I18N; |
||||
| 32 | use Fisharebest\Webtrees\Services\TimeoutService; |
||||
| 33 | 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...
|
|||||
| 34 | use Fisharebest\Webtrees\Site; |
||||
| 35 | use Fisharebest\Webtrees\Theme; |
||||
| 36 | use Fisharebest\Webtrees\Tree; |
||||
| 37 | use Fisharebest\Webtrees\User; |
||||
| 38 | 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...
|
|||||
| 39 | use Fisharebest\Webtrees\Webtrees; |
||||
| 40 | use Illuminate\Cache\ArrayStore; |
||||
| 41 | use Illuminate\Cache\Repository; |
||||
| 42 | use Illuminate\Database\Capsule\Manager as DB; |
||||
|
0 ignored issues
–
show
This use statement conflicts with another class in this namespace,
DB. 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...
|
|||||
| 43 | use League\Flysystem\Adapter\Local; |
||||
| 44 | use League\Flysystem\Cached\CachedAdapter; |
||||
| 45 | use League\Flysystem\Cached\Storage\Memory; |
||||
| 46 | use League\Flysystem\Filesystem; |
||||
| 47 | 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...
|
|||||
| 48 | 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...
|
|||||
| 49 | |||||
| 50 | require __DIR__ . '/vendor/autoload.php'; |
||||
| 51 | |||||
| 52 | // Regular expressions for validating user input, etc. |
||||
| 53 | const WT_MINIMUM_PASSWORD_LENGTH = 6; |
||||
| 54 | const WT_REGEX_PASSWORD = '.{' . WT_MINIMUM_PASSWORD_LENGTH . ',}'; |
||||
| 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 | $controller = new SetupController(); |
||||
| 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::createInstance($database_config); |
||||
| 109 | |||||
| 110 | // Update the database schema, if necessary. |
||||
| 111 | Database::updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION); |
||||
| 112 | } catch (PDOException $exception) { |
||||
| 113 | define('WT_DATA_DIR', 'data/'); |
||||
| 114 | I18N::init(); |
||||
| 115 | if ($exception->getCode() === 1045) { |
||||
| 116 | // Error during connection? |
||||
| 117 | $content = view('errors/database-connection', ['error' => $exception->getMessage()]); |
||||
| 118 | } else { |
||||
| 119 | // Error in a migration script? |
||||
| 120 | $content = view('errors/database-error', ['error' => $exception->getMessage()]); |
||||
| 121 | } |
||||
| 122 | $html = view('layouts/error', ['content' => $content]); |
||||
| 123 | $response = new Response($html, Response::HTTP_SERVICE_UNAVAILABLE); |
||||
| 124 | $response->prepare($request)->send(); |
||||
| 125 | |||||
| 126 | return; |
||||
| 127 | } catch (Throwable $exception) { |
||||
| 128 | define('WT_DATA_DIR', 'data/'); |
||||
| 129 | I18N::init(); |
||||
| 130 | $content = view('errors/database-connection', ['error' => $exception->getMessage()]); |
||||
| 131 | $html = view('layouts/error', ['content' => $content]); |
||||
| 132 | $response = new Response($html, Response::HTTP_SERVICE_UNAVAILABLE); |
||||
| 133 | $response->prepare($request)->send(); |
||||
| 134 | |||||
| 135 | return; |
||||
| 136 | } |
||||
| 137 | |||||
| 138 | DebugBar::stopMeasure('init database'); |
||||
| 139 | |||||
| 140 | // The config.ini.php file must always be in a fixed location. |
||||
| 141 | // Other user files can be stored elsewhere... |
||||
| 142 | define('WT_DATA_DIR', realpath(Site::getPreference('INDEX_DIRECTORY', 'data/')) . DIRECTORY_SEPARATOR); |
||||
| 143 | |||||
| 144 | $filesystem = new Filesystem(new CachedAdapter(new Local(WT_DATA_DIR), new Memory())); |
||||
| 145 | |||||
| 146 | // Some broken servers block access to their own temp folder using open_basedir... |
||||
| 147 | $filesystem->createDir('tmp'); |
||||
| 148 | putenv('TMPDIR=' . WT_DATA_DIR . 'tmp'); |
||||
| 149 | Swift_Preferences::getInstance()->setTempDir(WT_DATA_DIR . 'tmp'); |
||||
| 150 | |||||
| 151 | // Request more resources - if we can/want to |
||||
| 152 | $memory_limit = Site::getPreference('MEMORY_LIMIT'); |
||||
| 153 | if ($memory_limit !== '' && strpos(ini_get('disable_functions'), 'ini_set') === false) { |
||||
| 154 | ini_set('memory_limit', $memory_limit); |
||||
| 155 | } |
||||
| 156 | $max_execution_time = Site::getPreference('MAX_EXECUTION_TIME'); |
||||
| 157 | if ($max_execution_time !== '' && strpos(ini_get('disable_functions'), 'set_time_limit') === false) { |
||||
| 158 | set_time_limit((int) $max_execution_time); |
||||
| 159 | } |
||||
| 160 | |||||
| 161 | // Sessions |
||||
| 162 | Session::start(); |
||||
| 163 | |||||
| 164 | // Note that the database/webservers may not be synchronised, so use DB time throughout. |
||||
| 165 | define('WT_TIMESTAMP', DB::select('SELECT UNIX_TIMESTAMP() AS unix_timestamp')[0]->unix_timestamp); |
||||
| 166 | |||||
| 167 | // Users get their own time-zone. Visitors get the site time-zone. |
||||
| 168 | try { |
||||
| 169 | if (Auth::check()) { |
||||
| 170 | date_default_timezone_set(Auth::user()->getPreference('TIMEZONE')); |
||||
| 171 | } else { |
||||
| 172 | date_default_timezone_set(Site::getPreference('TIMEZONE')); |
||||
| 173 | } |
||||
| 174 | } catch (ErrorException $exception) { |
||||
| 175 | // Server upgrades and migrations can leave us with invalid timezone settings. |
||||
| 176 | date_default_timezone_set('UTC'); |
||||
| 177 | } |
||||
| 178 | |||||
| 179 | define('WT_TIMESTAMP_OFFSET', (new DateTime('now'))->getOffset()); |
||||
| 180 | |||||
| 181 | define('WT_CLIENT_JD', 2440588 + intdiv(WT_TIMESTAMP + WT_TIMESTAMP_OFFSET, 86400)); |
||||
| 182 | |||||
| 183 | // Update the last-login time no more than once a minute |
||||
| 184 | if (WT_TIMESTAMP - Session::get('activity_time') >= 60) { |
||||
| 185 | if (Session::get('masquerade') === null) { |
||||
| 186 | Auth::user()->setPreference('sessiontime', (string) WT_TIMESTAMP); |
||||
| 187 | } |
||||
| 188 | Session::put('activity_time', WT_TIMESTAMP); |
||||
| 189 | } |
||||
| 190 | |||||
| 191 | DebugBar::startMeasure('routing'); |
||||
| 192 | |||||
| 193 | try { |
||||
| 194 | // Most requests will need the current tree and user. |
||||
| 195 | $tree = Tree::findByName($request->get('ged')) ?? null; |
||||
| 196 | |||||
| 197 | // No tree specified/available? Choose one. |
||||
| 198 | if ($tree === null && $request->getMethod() === Request::METHOD_GET) { |
||||
| 199 | $tree = Tree::findByName(Site::getPreference('DEFAULT_GEDCOM')) ?? array_values(Tree::getAll())[0] ?? null; |
||||
| 200 | } |
||||
| 201 | |||||
| 202 | // Select a locale |
||||
| 203 | define('WT_LOCALE', I18N::init('', $tree)); |
||||
| 204 | Session::put('locale', WT_LOCALE); |
||||
| 205 | |||||
| 206 | // Most layouts will require a tree for the page header/footer |
||||
| 207 | View::share('tree', $tree); |
||||
| 208 | |||||
| 209 | // Load the route and routing table. |
||||
| 210 | $route = $request->get('route'); |
||||
| 211 | $routes = require 'routes/web.php'; |
||||
| 212 | |||||
| 213 | // Find the controller and action for the selected route |
||||
| 214 | $controller_action = $routes[$request->getMethod() . ':' . $route] ?? 'ErrorController@noRouteFound'; |
||||
| 215 | [$controller_name, $action] = explode('@', $controller_action); |
||||
| 216 | $controller_class = '\\Fisharebest\\Webtrees\\Http\\Controllers\\' . $controller_name; |
||||
| 217 | |||||
| 218 | app()->instance(Tree::class, $tree); |
||||
| 219 | app()->instance(User::class, Auth::user()); |
||||
| 220 | app()->instance(LocaleInterface::class, WebtreesLocale::create(WT_LOCALE)); |
||||
| 221 | app()->instance(TimeoutService::class, new TimeoutService(microtime(true))); |
||||
| 222 | app()->instance(Filesystem::class, $filesystem); |
||||
| 223 | |||||
| 224 | $controller = app()->make($controller_class); |
||||
| 225 | |||||
| 226 | DebugBar::stopMeasure('routing'); |
||||
| 227 | |||||
| 228 | DebugBar::startMeasure('init theme'); |
||||
| 229 | |||||
| 230 | // Last theme used? |
||||
| 231 | $theme_id = Session::get('theme_id'); |
||||
| 232 | // Default for tree |
||||
| 233 | if (!array_key_exists($theme_id, Theme::themeNames()) && $tree) { |
||||
| 234 | $theme_id = $tree->getPreference('THEME_DIR'); |
||||
| 235 | } |
||||
| 236 | // Default for site |
||||
| 237 | if (!array_key_exists($theme_id, Theme::themeNames())) { |
||||
| 238 | $theme_id = Site::getPreference('THEME_DIR'); |
||||
| 239 | } |
||||
| 240 | // Default |
||||
| 241 | if (!array_key_exists($theme_id, Theme::themeNames())) { |
||||
| 242 | $theme_id = 'webtrees'; |
||||
| 243 | } |
||||
| 244 | foreach (Theme::installedThemes() as $theme) { |
||||
| 245 | if ($theme->themeId() === $theme_id) { |
||||
| 246 | Theme::theme($theme)->init($request, $tree); |
||||
| 247 | // Remember this setting |
||||
| 248 | if (Site::getPreference('ALLOW_USER_THEMES') === '1') { |
||||
| 249 | Session::put('theme_id', $theme_id); |
||||
| 250 | } |
||||
| 251 | break; |
||||
| 252 | } |
||||
| 253 | } |
||||
| 254 | |||||
| 255 | DebugBar::stopMeasure('init theme'); |
||||
| 256 | |||||
| 257 | // Note that we can't stop this timer, as running the action will |
||||
| 258 | // generate the response - which includes (and stops) the timer |
||||
| 259 | DebugBar::startMeasure('controller_action'); |
||||
| 260 | |||||
| 261 | $middleware_stack = [ |
||||
| 262 | CheckForMaintenanceMode::class, |
||||
| 263 | ]; |
||||
| 264 | |||||
| 265 | if (class_exists(DebugBar::class)) { |
||||
| 266 | $middleware_stack[] = DebugBarData::class; |
||||
| 267 | } |
||||
| 268 | |||||
| 269 | if ($request->getMethod() === Request::METHOD_GET) { |
||||
| 270 | $middleware_stack[] = PageHitCounter::class; |
||||
| 271 | $middleware_stack[] = Housekeeping::class; |
||||
| 272 | } |
||||
| 273 | |||||
| 274 | if ($request->getMethod() === Request::METHOD_POST) { |
||||
| 275 | $middleware_stack[] = UseTransaction::class; |
||||
| 276 | $middleware_stack[] = CheckCsrf::class; |
||||
| 277 | } |
||||
| 278 | |||||
| 279 | // Apply the middleware using the "onion" pattern. |
||||
| 280 | $pipeline = array_reduce($middleware_stack, function (Closure $next, string $middleware): Closure { |
||||
| 281 | // Create a closure to apply the middleware. |
||||
| 282 | return function (Request $request) use ($middleware, $next): Response { |
||||
| 283 | return app()->make($middleware)->handle($request, $next); |
||||
| 284 | }; |
||||
| 285 | }, 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...
|
|||||
| 286 | return app()->dispatch($controller, $action); |
||||
| 287 | }); |
||||
| 288 | |||||
| 289 | $response = call_user_func($pipeline, $request); |
||||
| 290 | } catch (Exception $exception) { |
||||
| 291 | $response = (new Handler())->render($request, $exception); |
||||
| 292 | } |
||||
| 293 | |||||
| 294 | // Send response |
||||
| 295 | $response->prepare($request)->send(); |
||||
| 296 |
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: