xref: /webtrees/index.php (revision 997ff73384aec328ac6e60dfaa8a0f7f5f11f70d)
1<?php
2/**
3 * My Page page allows a logged in user the abilty
4 * to keep bookmarks, see a list of upcoming events, etc.
5 *
6 * webtrees: Web based Family History software
7 * Copyright (C) 2010 webtrees development team.
8 *
9 * Derived from PhpGedView
10 * Copyright (C) 2002 to 2009  PGV Development Team.  All rights reserved.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25 *
26 * @package webtrees
27 * @subpackage Display
28 * @version $Id$
29 */
30
31define('WT_SCRIPT_NAME', 'index.php');
32require './includes/session.php';
33require_once WT_ROOT.'includes/index_cache.php';
34require_once WT_ROOT.'includes/functions/functions_print_facts.php';  //--needed for the expand url function in some of the blocks
35
36if (isset($_REQUEST['action'])) $action = $_REQUEST['action'];
37if (isset($_REQUEST['ctype'])) $ctype = $_REQUEST['ctype'];
38$message_id = safe_GET('message_id');
39$gid = safe_POST('gid');
40$favnote = safe_POST('favnote');
41$favtype = safe_POST('favtype');
42$url = safe_POST('url', WT_REGEX_URL);
43$favtitle = safe_POST('favtitle');
44$fv_id = safe_GET('fv_id');
45$news_id = safe_GET('news_id');
46
47/**
48 * Block definition array
49 *
50 * The following block definition array defines the
51 * blocks that can be used to customize the portals
52 * their names and the function to call them
53 * 'name' is the name of the block in the lists
54 * 'descr' is a description of this block
55 * 'type' the options are 'user' or 'gedcom' or 'both'
56 * - The type determines which lists the block is available in.
57 * - Leaving the type undefined allows it to be on both the user and gedcom portal
58 * @global $WT_BLOCKS
59 */
60
61/**
62 * Load List of Blocks in blocks directory (unchanged)
63 */
64$WT_BLOCKS = array();
65$d = opendir('blocks');
66while (false !== ($f=readdir($d))) {
67	if (preg_match('/\.php$/', $f)>0) {
68		require_once WT_ROOT.'blocks/'.$f;
69	}
70}
71closedir($d);
72/**
73 * End loading list of Blocks in blocks directory
74 *
75 * Load List of Blocks in modules/XX/blocks directories
76 */
77if (file_exists(WT_ROOT.'modules')) {
78	$dir=dir(WT_ROOT.'modules');
79	while (false !== ($entry = $dir->read())) {
80		if (!strstr($entry,'.') && ($entry!='..') && ($entry!='CVS')&& !strstr($entry, 'svn')) {
81			$path = WT_ROOT.'modules/' . $entry.'/blocks';
82			if (is_readable($path)) {
83				$d=dir($path);
84				while (false !== ($entry = $d->read())) {
85					if (($entry!='.') && ($entry!='..') && ($entry!='CVS')&& !strstr($entry, 'svn')&&(preg_match('/\.php$/', $entry)>0)) {
86						$p=$path.'/'.$entry;
87						require_once $p;
88					}
89				}
90			}
91		}
92	}
93}
94/**
95 * End loading list of Blocks in modules/XX/blocks directories
96*/
97
98$time = client_time();
99
100if (!isset($action)) $action='';
101
102// Visitors should see any links to a user page, but they may be
103// following a link after an inactivity logout.
104if (!WT_USER_ID) {
105	if (!empty($ctype) && $ctype=='user') {
106		header('Location: login.php&url=index.php?ctype=user');
107		exit;
108	} else {
109		$ctype = 'gedcom';
110	}
111}
112
113if (empty($ctype)) {
114	if ($PAGE_AFTER_LOGIN == 'welcome') $ctype = 'gedcom';
115	else $ctype = 'user';
116}
117
118if (WT_USER_ID) {
119	//-- add favorites action
120	if ($action=='addfav' && !empty($gid)) {
121		$gid = strtoupper($gid);
122		$indirec = find_gedcom_record($gid, WT_GED_ID);
123		$ct = preg_match('/0 @(.*)@ (.*)/', $indirec, $match);
124		if ($indirec && $ct>0) {
125			$favorite = array();
126			if (empty($favtype)) {
127				if ($ctype=='user') $favtype = 'user';
128				else $favtype = 'gedcom';
129			}
130			if ($favtype=='gedcom') {
131				$favtype = $GEDCOM;
132				$_SESSION['clearcache'] = true;
133			}
134			else $favtype=WT_USER_NAME;
135			$favorite['username'] = $favtype;
136			$favorite['gid'] = $gid;
137			$favorite['type'] = trim($match[2]);
138			$favorite['file'] = $GEDCOM;
139			$favorite['url'] = '';
140			$favorite['note'] = $favnote;
141			$favorite['title'] = '';
142			addFavorite($favorite);
143		}
144	}
145	if (($action=='addfav')&&(!empty($url))) {
146		if (empty($favtitle)) $favtitle = $url;
147		$favorite = array();
148		if (!isset($favtype)) {
149			if ($ctype=='user') $favtype = 'user';
150			else $favtype = 'gedcom';
151		}
152		if ($favtype=='gedcom') {
153			$favtype = $GEDCOM;
154			$_SESSION['clearcache'] = true;
155		}
156		else $favtype=WT_USER_NAME;
157		$favorite['username'] = $favtype;
158		$favorite['gid'] = '';
159		$favorite['type'] = 'URL';
160		$favorite['file'] = $GEDCOM;
161		$favorite['url'] = $url;
162		$favorite['note'] = $favnote;
163		$favorite['title'] = $favtitle;
164		addFavorite($favorite);
165	}
166	if (($action=='deletefav')&&(!empty($fv_id))) {
167		deleteFavorite($fv_id);
168		if ($ctype=='gedcom') $_SESSION['clearcache'] = true;
169	}
170	else if ($action=='deletemessage') {
171		if (isset($message_id)) {
172			if (!is_array($message_id)) deleteMessage($message_id);
173			else {
174				foreach($message_id as $indexval => $mid) {
175					if (isset($mid)) deleteMessage($mid);
176				}
177			}
178			if ($ctype=='gedcom') $_SESSION['clearcache'] = true;
179		}
180	}
181	else if (($action=='deletenews')&&(isset($news_id))) {
182		deleteNews($news_id);
183		if ($ctype=='gedcom') $_SESSION['clearcache'] = true;
184	}
185}
186
187//-- get the blocks list
188if ($ctype=='user') {
189	$ublocks = getBlocks(WT_USER_NAME);
190	if ((count($ublocks['main'])==0) && (count($ublocks['right'])==0)) {
191		$ublocks['main'][] = array('print_todays_events', '');
192		$ublocks['main'][] = array('print_user_messages', '');
193		$ublocks['main'][] = array('print_user_favorites', '');
194
195		$ublocks['right'][] = array('print_welcome_block', '');
196		$ublocks['right'][] = array('print_random_media', '');
197		$ublocks['right'][] = array('print_upcoming_events', '');
198		$ublocks['right'][] = array('print_logged_in_users', '');
199	}
200}
201else {
202	$ublocks = getBlocks($GEDCOM);
203	if ((count($ublocks['main'])==0) && (count($ublocks['right'])==0)) {
204		$ublocks['main'][] = array('print_gedcom_stats', '');
205		$ublocks['main'][] = array('print_gedcom_news', '');
206		$ublocks['main'][] = array('print_gedcom_favorites', '');
207		$ublocks['main'][] = array('review_changes_block', '');
208
209		$ublocks['right'][] = array('print_gedcom_block', '');
210		$ublocks['right'][] = array('print_random_media', '');
211		$ublocks['right'][] = array('print_todays_events', '');
212		$ublocks['right'][] = array('print_logged_in_users', '');
213	}
214}
215
216//-- Set some behaviour controls that depend on which blocks are selected
217$welcome_block_present = false;
218$gedcom_block_present = false;
219$top10_block_present = false;
220$login_block_present = false;
221foreach($ublocks['right'] as $block) {
222	if ($block[0]=='print_welcome_block') $welcome_block_present = true;
223	if ($block[0]=='print_gedcom_block') $gedcom_block_present = true;
224	if ($block[0]=='print_block_name_top10') $top10_block_present = true;
225	if ($block[0]=='print_login_block') $login_block_present = true;
226}
227foreach($ublocks['main'] as $block) {
228	if ($block[0]=='print_welcome_block') $welcome_block_present = true;
229	if ($block[0]=='print_gedcom_block') $gedcom_block_present = true;
230	if ($block[0]=='print_block_name_top10') $top10_block_present = true;
231	if ($block[0]=='print_login_block') $login_block_present = true;
232}
233
234//-- clear the GEDCOM cache files
235if (!empty($_SESSION['clearcache'])) {
236	$_SESSION['clearcache'] = false;
237	clearCache();
238}
239
240// We have finished writing to $_SESSION, so release the lock
241session_write_close();
242
243//-- handle block AJAX calls
244/**
245 * In order for a block to make an AJAX call the following request parameters must be set
246 * block = the method name of the block to call (e.g. 'print_random_media')
247 * side = the side of the page the block is on (e.g. 'main' or 'right')
248 * bindex = the number of the block on that side, first block = 0
249 */
250if ($action=='ajax') {
251	header('Content-Type: text/html; charset=UTF-8');
252	//--  if a block wasn't sent then exit with nothing
253	if (!isset($_REQUEST['block'])) {
254		echo 'Block not sent';
255		exit;
256	}
257	$block = $_REQUEST['block'];
258	//-- set which side the block is on
259	$side = 'main';
260	if (isset($_REQUEST['side'])) $side = $_REQUEST['side'];
261	//-- get the block number
262	if (isset($_REQUEST['bindex'])) {
263		if (isset($ublocks[$side][$_REQUEST['bindex']])) {
264			$blockval = $ublocks[$side][$_REQUEST['bindex']];
265			if ($blockval[0]==$block && array_key_exists($blockval[0], $WT_BLOCKS)) {
266				if ($side=='main') {
267					$param1 = 'false';
268				} else {
269					$param1 = 'true';
270				}
271				if (array_key_exists($blockval[0], $WT_BLOCKS) && !loadCachedBlock($blockval, $side.$_REQUEST['bindex'])) {
272					ob_start();
273					eval($blockval[0]."($param1, \$blockval[1], \"$side\", ".$_REQUEST['bindex'].");");
274					$content = ob_get_contents();
275					saveCachedBlock($blockval, $side.$_REQUEST['bindex'], $content);
276					ob_end_flush();
277				}
278				if (WT_DEBUG) {
279					echo execution_stats();
280				}
281				if (WT_DEBUG_SQL) {
282					echo WT_DB::getQueryLog();
283				}
284				exit;
285			}
286		}
287	}
288
289	//-- not sure which block to call so call the first one we find
290	foreach($ublocks['main'] as $bindex=>$blockval) {
291		if ($blockval[0]==$block && array_key_exists($blockval[0], $WT_BLOCKS)) {
292			eval($blockval[0]."(false, \$blockval[1], \"main\", $bindex);");
293		}
294	}
295	foreach($ublocks['right'] as $bindex=>$blockval) {
296		if ($blockval[0]==$block && array_key_exists($blockval[0], $WT_BLOCKS)) {
297			eval($blockval[0]."(true, \$blockval[1], \"right\", $bindex);");
298		}
299	}
300	exit;
301}
302//-- end of ajax call handler
303
304if ($ctype=='user') {
305	$helpindex = 'index_myged_help';
306	print_header(i18n::translate('My Page'));
307} else {
308	print_header(get_gedcom_setting(WT_GED_ID, 'title'));
309}
310
311if (WT_USE_LIGHTBOX) {
312	require WT_ROOT.'modules/lightbox/lb_defaultconfig.php';
313	require WT_ROOT.'modules/lightbox/functions/lb_call_js.php';
314}
315
316echo WT_JS_START;
317?>
318	function refreshpage() {
319		window.location = 'index.php?ctype=<?php echo $ctype; ?>';
320	}
321	function addnews(uname) {
322		window.open('editnews.php?username='+uname, '_blank', 'top=50,left=50,width=600,height=500,resizable=1,scrollbars=1');
323	}
324	function editnews(news_id) {
325		window.open('editnews.php?news_id='+news_id, '_blank', 'top=50,left=50,width=600,height=500,resizable=1,scrollbars=1');
326	}
327	var pastefield;
328	function paste_id(value) {
329		pastefield.value=value;
330	}
331/**
332 * blocks may use this JS function to update themselves using AJAX technology
333 * @param string targetId	the id of the block to target the results too
334 * @param string block 	the method name of the block to call (e.g. 'print_random_media')
335 * @param string side 	the side of the page the block is on (e.g. 'main' or 'right')
336 * @param int bindex 	the number of the block on that side, first block = 0
337 * @param string ctype 	shows whether block is on Welcome or My Page ('gedcom' or 'user')
338 * @param boolean loading  Whether or not to show the loading message
339 */
340	function ajaxBlock(targetId, block, side, bindex, ctype, loading) {
341		target = document.getElementById(targetId);
342		if (!target) return false;
343
344		target.style.height = (target.offsetHeight) + "px";
345		if (loading) target.innerHTML = "<br /><br /><?php echo i18n::translate('Loading...'); ?><br /><br />";
346
347		var oXmlHttp = createXMLHttp();
348		link = "index.php?action=ajax&block="+block+"&side="+side+"&bindex="+bindex+"&ctype="+ctype;
349		oXmlHttp.open("get", link, true);
350		oXmlHttp.onreadystatechange=function()
351		{
352			if (oXmlHttp.readyState==4)
353 			{
354 				target.innerHTML = oXmlHttp.responseText;
355 				target.style.height = 'auto';
356 			}
357		};
358 		oXmlHttp.send(null);
359 		return false;
360	}
361<?php
362echo WT_JS_END;
363//-- start of main content section
364echo '<table width="100%"><tr><td>';		// This is needed so that page footers print in the right place
365if ($ctype=='user') {
366	echo '<div align="center" style="width: 99%;">';
367	echo '<h1>', i18n::translate('My Page'), '</h1>';
368	echo i18n::translate('My Page allows you to keep bookmarks of your favorite people, track upcoming events, and collaborate with other webtrees users.');
369	echo '<br /><br /></div>';
370}
371if (count($ublocks['main'])!=0) {
372	if (count($ublocks['right'])!=0) {
373		echo '<div id="index_main_blocks">';
374	} else {
375		echo '<div id="index_full_blocks">';
376	}
377	echo '<script src="js/jquery/jquery.min.js" type="text/javascript"></script>';
378	echo '<script type="text/javascript">jQuery.noConflict();</script>';
379	foreach($ublocks['main'] as $bindex=>$block) {
380		if (WT_DEBUG) {
381			echo execution_stats();
382		}
383		if (array_key_exists($block[0], $WT_BLOCKS) && !loadCachedBlock($block, 'main'.$bindex)) {
384			$url="index.php?action=ajax&block={$block[0]}&side=main&bindex={$bindex}&ctype={$ctype}";
385			if ($SEARCH_SPIDER || WT_DEBUG) {
386				// Search spiders get the blocks directly
387				ob_start();
388				eval($block[0]."(false, \$block[1], \"main\", $bindex);");
389				$content = ob_get_contents();
390				$temp = $SEARCH_SPIDER;
391				$SEARCH_SPIDER = false;
392				saveCachedBlock($block, 'main'.$bindex, $content);
393				$SEARCH_SPIDER = $temp;
394				ob_end_flush();
395			} else {
396				// Interactive users get the blocks via ajax
397				echo '<div id="block_main_', $bindex, '"><img src="images/loading.gif" alt="', htmlspecialchars(i18n::translate('Loading...')),  '"/></div>';
398				echo WT_JS_START, "jQuery('#block_main_{$bindex}').load('{$url}');", WT_JS_END;
399			}
400		}
401	}
402	echo '</div>';
403}
404//-- end of main content section
405
406//-- start of blocks section
407if (count($ublocks['right'])!=0) {
408	if (count($ublocks['main'])!=0) {
409		echo '<div id="index_small_blocks">';
410	} else {
411		echo '<div id="index_full_blocks">';
412	}
413	foreach($ublocks['right'] as $bindex=>$block) {
414		if (WT_DEBUG) {
415			echo execution_stats();
416		}
417		if (array_key_exists($block[0], $WT_BLOCKS) && !loadCachedBlock($block, 'right'.$bindex)) {
418			$url="index.php?action=ajax&block={$block[0]}&side=right&bindex={$bindex}&ctype={$ctype}";
419			if ($SEARCH_SPIDER || WT_DEBUG) {
420				// Search spiders get the blocks directly
421				ob_start();
422				eval($block[0]."(true, \$block[1], \"right\", $bindex);");
423				$content = ob_get_contents();
424				saveCachedBlock($block, 'right'.$bindex, $content);
425				ob_end_flush();
426			} else {
427				// Interactive users get the blocks via ajax
428				echo '<div id="block_right_', $bindex, '"><img src="images/loading.gif" alt="', htmlspecialchars(i18n::translate('Loading...')),  '"/></div>';
429				echo WT_JS_START, "jQuery('#block_right_{$bindex}').load('{$url}');", WT_JS_END;
430			}
431		}
432	}
433	echo '</div>';
434}
435//-- end of blocks section
436
437echo '</td></tr></table><br />';		// Close off that table
438
439if ($ctype=='user' && !$welcome_block_present) {
440	echo '<div align="center" style="width: 99%;">';
441	echo "<a href=\"javascript:;\" onclick=\"window.open('index_edit.php?name=".WT_USER_NAME."&ctype=user', '_blank', 'top=50,left=10,width=600,height=500,scrollbars=1,resizable=1');\">".i18n::translate('Customize My Page').'</a>';
442	echo help_link('mygedview_customize');
443	echo '</div>';
444}
445if ($ctype=='gedcom' && !$gedcom_block_present) {
446	if (WT_USER_IS_ADMIN) {
447		echo '<div align="center" style="width: 99%;">';
448		echo "<a href=\"javascript:;\" onclick=\"window.open('".encode_url("index_edit.php?name={$GEDCOM}&ctype=gedcom", false)."', '_blank', 'top=50,left=10,width=600,height=500,scrollbars=1,resizable=1');\">".i18n::translate('Customize this GEDCOM Home Page').'</a>';
449		echo '</div>';
450	}
451}
452
453print_footer();
454