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