xref: /webtrees/index.php (revision ba5cd25e4897e5ca3268036ab8a43da7979eff40)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2017 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 */
16namespace Fisharebest\Webtrees;
17
18use Fisharebest\Webtrees\Controller\AjaxController;
19use Fisharebest\Webtrees\Controller\PageController;
20use Fisharebest\Webtrees\Functions\Functions;
21use Fisharebest\Webtrees\Functions\FunctionsDb;
22use Symfony\Component\HttpFoundation\Request;
23
24require 'includes/session.php';
25
26// Extract the route from the the HTTP request.
27$request = Request::createFromGlobals();
28$route   = $request->get('route', '');
29
30if ($route === '') {
31	// Legacy URLs:
32	// index.php?ctype=gedcom
33	// index.php?ctype=user
34} else {
35	// Route the request to its controller action.
36	$response = require 'routes/web.php';
37
38	// Send the response back to the browser.
39	return $response->prepare($request)->send();
40}
41
42/** @global Tree $WT_TREE */
43global $WT_TREE;
44
45// The only option for action is "ajax"
46$ajax = (bool) Filter::get('ajax');
47
48// The default view depends on whether we are logged in
49if (Auth::check()) {
50	$ctype = Filter::get('ctype', 'gedcom|user', 'user');
51} else {
52	$ctype = 'gedcom';
53}
54
55// Get the blocks list
56if ($ctype === 'user') {
57	$blocks = FunctionsDb::getUserBlocks(Auth::id());
58} else {
59	$blocks = FunctionsDb::getTreeBlocks($WT_TREE->getTreeId());
60}
61
62$active_blocks = Module::getActiveBlocks($WT_TREE);
63
64// The latest version is shown on the administration page. This updates it every day.
65Functions::fetchLatestVersion();
66
67// We generate individual blocks using AJAX
68if ($ajax) {
69	$controller = new AjaxController;
70	$controller->pageHeader();
71
72	// Check we’re displaying an allowable block.
73	$block_id = Filter::getInteger('block_id');
74	if (array_key_exists($block_id, $blocks['main'])) {
75		$module_name = $blocks['main'][$block_id];
76	} elseif (array_key_exists($block_id, $blocks['side'])) {
77		$module_name = $blocks['side'][$block_id];
78	} else {
79		return;
80	}
81	if (array_key_exists($module_name, $active_blocks)) {
82		echo $active_blocks[$module_name]->getBlock($block_id);
83	}
84
85	return;
86}
87
88// Redirect search engines to the full URL
89if (Filter::get('ctype') !== $ctype || Filter::get('ged') !== $WT_TREE->getName()) {
90	header('Location: index.php?ctype=' . $ctype . '&ged=' . $WT_TREE->getNameUrl());
91
92	return;
93}
94
95$controller = new PageController;
96if ($ctype === 'user') {
97	$controller->restrictAccess(Auth::check());
98}
99$controller
100	->setPageTitle($ctype === 'user' ? I18N::translate('My page') : $WT_TREE->getTitle())
101	->setMetaRobots('index,follow')
102	->pageHeader()
103	// By default jQuery modifies AJAX URLs to disable caching, causing JS libraries to be loaded many times.
104	->addInlineJavascript('jQuery.ajaxSetup({cache:true});');
105
106if ($ctype === 'user') {
107	echo '<h2 class="text-center">', $controller->getPageTitle(), '</h2>';
108}
109
110echo '<div class="row">';
111
112if ($blocks['main']) {
113	if ($blocks['side']) {
114		echo '<div class="col-sm-8 wt-main-blocks">';
115	} else {
116		echo '<div class="col-sm-12 wt-main-blocks">';
117	}
118	foreach ($blocks['main'] as $block_id => $module_name) {
119		if (array_key_exists($module_name, $active_blocks)) {
120			if ($active_blocks[$module_name]->loadAjax()) {
121				// Load the block asynchronously
122				$ajax_url = Html::url('index.php', ['ctype' => $ctype, 'block_id' => $block_id, 'ajax' => 1]);
123				echo '<div class="wt-ajax-load" data-ajax-url="' . Html::escape($ajax_url) . '"></div>';
124			} else {
125				// Load the block directly
126				echo $active_blocks[$module_name]->getBlock($block_id);
127			}
128		}
129	}
130	echo '</div>';
131}
132if ($blocks['side']) {
133	if ($blocks['main']) {
134		echo '<div class="col-sm-4 wt-side-blocks">';
135	} else {
136		echo '<div class="col-sm-12 wt-side-blocks">';
137	}
138	foreach ($blocks['side'] as $block_id => $module_name) {
139		if (array_key_exists($module_name, $active_blocks)) {
140			if ($active_blocks[$module_name]->loadAjax()) {
141				// Load the block asynchronously
142				$ajax_url = Html::url('index.php', ['ctype' => $ctype, 'block_id' => $block_id, 'ajax' => 1]);
143				echo '<div class="wt-ajax-load" data-ajax-url="' . Html::escape($ajax_url) . '"></div>';
144			} else {
145				// Load the block directly
146				echo $active_blocks[$module_name]->getBlock($block_id);
147			}
148		}
149	}
150	echo '</div>';
151}
152echo '</div>';
153