Project

General

Profile

1 4 ryan
<?php
2
3
// $Id: index.php,v 1.20 2005/06/22 05:41:23 rdjurovich Exp $
4
5
/*
6
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2005, Ryan Djurovich
9
10
 Website Baker is free software; you can redistribute it and/or modify
11
 it under the terms of the GNU General Public License as published by
12
 the Free Software Foundation; either version 2 of the License, or
13
 (at your option) any later version.
14
15
 Website Baker is distributed in the hope that it will be useful,
16
 but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 GNU General Public License for more details.
19
20
 You should have received a copy of the GNU General Public License
21
 along with Website Baker; if not, write to the Free Software
22
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
24
*/
25
26
// Include config file
27
if(!defined('WB_URL')) {
28
	require('config.php');
29
}
30
// Say that this file has been loaded
31
if(!defined('FRONTEND_LOADED')) {
32
	define('FRONTEND_LOADED', true);
33
}
34
// Check if the config file has been set-up
35
if(!defined('WB_PATH')) {
36
	// Work-out where to go to get to the installer
37
	if(isset($no_intro) AND $no_intro == true) {
38
		header("Location: ../install/index.php");
39
	} else {
40
		header("Location: install/index.php");
41
	}
42
}
43
// Get language (if set)
44
if(isset($_GET['lang']) AND $_GET['lang'] != '' AND !is_numeric($_GET['lang']) AND strlen($_GET['lang']) == 2) {
45
	define('LANGUAGE', strtoupper($_GET['lang']));
46
	define('GET_LANGUAGE', true);
47
}
48
// Function to work out a page link
49
function page_link($link) {
50
	// Check for :// in the link (used in URL's)
51
	if(strstr($link, '://') == '') {
52
		return WB_URL.PAGES_DIRECTORY.$link.PAGE_EXTENSION;
53
	} else {
54
		return $link;
55
	}
56
}
57
// Work-out if we should include the database class file or admin class file
58
if(FRONTEND_LOGIN) {
59
	// Include admin class file
60
	require_once(WB_PATH.'/framework/class.admin.php');
61
	// Create new admin object
62
	if(!isset($admin)) {
63
		$admin = new admin('Start', 'start', false, false);
64
	}
65
} else {
66
	// Include database class file
67
	require_once(WB_PATH.'/framework/class.database.php');
68
	// Create new database object
69
	if(!isset($admin)) {
70
		$database = new database();
71
	}
72
}
73
/*
74
Begin user-changeable settings
75
*/
76
// Get users language
77
if(!defined('LANGUAGE')) {
78
	if(isset($_SESSION['LANGUAGE']) AND $_SESSION['LANGUAGE'] != '') {
79
		define('LANGUAGE', $_SESSION['LANGUAGE']);
80
		define('USER_LANGUAGE', true);
81
	} else {
82
		define('LANGUAGE', DEFAULT_LANGUAGE);
83
	}
84
}
85
// Get users timezone
86
if(!defined('TIMEZONE')) {
87
	if(isset($_SESSION['TIMEZONE'])) {
88
		define('TIMEZONE', $_SESSION['TIMEZONE']);
89
	} else {
90
		define('TIMEZONE', DEFAULT_TIMEZONE);
91
	}
92
}
93
// Get users date format
94
if(!defined('DATE_FORMAT')) {
95
	if(isset($_SESSION['DATE_FORMAT'])) {
96
		define('DATE_FORMAT', $_SESSION['DATE_FORMAT']);
97
	} else {
98
		define('DATE_FORMAT', DEFAULT_DATE_FORMAT);
99
	}
100
}
101
// Get users time format
102
if(!defined('TIME_FORMAT')) {
103
	if(isset($_SESSION['TIME_FORMAT'])) {
104
		define('TIME_FORMAT', $_SESSION['TIME_FORMAT']);
105
	} else {
106
		define('TIME_FORMAT', DEFAULT_TIME_FORMAT);
107
	}
108
}
109
// Load the language file
110
if(!defined('LANGUAGE_LOADED')) {
111
	if(!file_exists(WB_PATH.'/languages/'.LANGUAGE.'.php')) {
112
		exit('Error loading language file '.LANGUAGE.', please check configuration');
113
	} else {
114
		require(WB_PATH.'/languages/'.LANGUAGE.'.php');
115
	}
116
}
117
/*
118
End user-changeable settings
119
*/
120
/*
121
Begin page-select code
122
*/
123
// Get default page
124
$query_default = "SELECT page_id,link FROM ".TABLE_PREFIX."pages WHERE parent = '0' AND visibility = 'public' ORDER BY position ASC LIMIT 1";
125
$get_default = $database->query($query_default);
126
$default_num_rows = $get_default->numRows();
127
// Check for a page id
128
if(!isset($page_id) OR !is_numeric($page_id)) {
129
	// Since we have no page id check if we should go to intro page or default page
130
	if(INTRO_PAGE AND !isset($no_intro)) {
131
		// Get intro page content
132
		$filename = WB_PATH.PAGES_DIRECTORY.'/intro.php';
133
		if(file_exists($filename)) {
134
			$handle = fopen($filename, "r");
135
			$content = fread($handle, filesize($filename));
136
			fclose($handle);
137
			// Replace [wblink--PAGE_ID--] with real link
138
			$pattern = '/\[wblink(.+?)\]/s';
139
			preg_match_all($pattern,$content,$ids);
140
			foreach($ids[1] AS $page_id) {
141
				$pattern = '/\[wblink'.$page_id.'\]/s';
142
				// Get page link
143
				$get_link = $database->query("SELECT link FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id' LIMIT 1");
144
				$fetch_link = $get_link->fetchRow();
145
				$link = page_link($fetch_link['link']);
146
				$content = preg_replace($pattern,$link,$content);
147
			}
148
			echo stripslashes($content);
149
			exit();
150
		} else {
151
			header("Location: ".WB_URL.PAGES_DIRECTORY."/index".PAGE_EXTENSION);
152
			exit();
153
		}
154
	} else {
155
		// Go to or show default page
156
		if($default_num_rows > 0) {
157
			$fetch_default = $get_default->fetchRow();
158
			$default_link = $fetch_default['link'];
159
			$default_page_id = $fetch_default['page_id'];
160
			// Check if we should redirect or include page inline
161
			if(HOMEPAGE_REDIRECTION) {
162
				// Redirect to page
163
				header("Location: ".page_link($default_link));
164
				exit();
165
			} else {
166
				// Include page inline
167
				$page_id = $default_page_id;
168
			}
169
		} else {
170
			// No pages have been added, so print under construction page
171
			require_once(WB_PATH.'/languages/'.DEFAULT_LANGUAGE.'.php');
172
			?>
173
			<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
174
			<head><title><?php echo $MESSAGE['GENERIC']['WEBSITE_UNDER_CONTRUCTION']; ?></title>
175
			<style type="text/css"><!-- body { font-family: Verdana, Arial, Helvetica, sans-serif;
176
			font-size: 12px; color: #000000;	background-color: #FFFFFF;	margin: 20px; text-align: center; }
177
			h1 { margin: 0; padding: 0; }--></style></head><body>
178
			<h1><?php echo $MESSAGE['GENERIC']['WEBSITE_UNDER_CONTRUCTION']; ?></h1><br />
179
			<?php echo $MESSAGE['GENERIC']['PLEASE_CHECK_BACK_SOON']; ?></body></html>
180
			<?php
181
			exit();
182
		}
183
	}
184
}
185
// Get default page link
186
if(!isset($fetch_default)) { $fetch_default = $get_default->fetchRow(); $default_link = $fetch_default['link']; }
187
/*
188
End page-select code
189
*/
190
/*
191
Begin page details code
192
*/
193
// Get page details
194
if($page_id != 0) {
195
	// Query page details
196
	$query_page = "SELECT * FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id'";
197
	$get_page = $database->query($query_page);
198
	// Make sure page was found in database
199
	if($get_page->numRows() == 0) {
200
		// Print page not found message
201
		exit("Page not found");
202
	}
203
	// Fetch page details
204
	$page = $get_page->fetchRow();
205
	// Begin code to set details as either variables of constants
206
		// Page ID
207
		define('PAGE_ID', $page['page_id']);
208
		// Page Title
209
		define('PAGE_TITLE', stripslashes($page['page_title']));
210
		// Menu Title
211
		$menu_title = stripslashes($page['menu_title']);
212
		if($menu_title != '') {
213
			define('MENU_TITLE', $menu_title);
214
		} else {
215
			define('MENU_TITLE', PAGE_TITLE);
216
		}
217
		// Page parent
218
		define('PARENT', $page['parent']);
219
		// Page root parent
220
		define('ROOT_PARENT', $page['root_parent']);
221
		// Page level
222
		define('LEVEL', $page['level']);
223
		// Page visibility
224
		define('VISIBILITY', $page['visibility']);
225
		// Page trail
226
		$page_trail = array();
227
		foreach(explode(',', $page['page_trail']) AS $pid) {
228
			$page_trail[$pid] = $pid;
229
		}
230
		// Page description
231
		$page_description = $page['description'];
232
		// Page keywords
233
		$page_keywords = $page['keywords'];
234
		// Page link
235
		$page_link_original = $page['link'];
236
		$page_link = page_link($page_link_original);
237
	// End code to set details as either variables of constants
238
}
239
// Work-out if any possible in-line search boxes should be shown
240
if(SEARCH == 'public') {
241
	define('SHOW_SEARCH', true);
242
} elseif(SEARCH == 'private' AND VISIBILITY == 'private') {
243
	define('SHOW_SEARCH', true);
244
} elseif(SEARCH == 'private' AND isset($admin) AND $admin->is_authenticated() == true) {
245
	define('SHOW_SEARCH', true);
246
} else {
247
	define('SHOW_SEARCH', false);
248
}
249
// Work-out if menu should be shown
250
if(!defined('SHOW_MENU')) {
251
	define('SHOW_MENU', true);
252
}
253
// Work-out if login menu constants should be set
254
if(FRONTEND_LOGIN) {
255
	// Set login menu constants
256
	define('LOGIN_URL', WB_URL.'/account/login'.PAGE_EXTENSION);
257
	define('LOGOUT_URL', WB_URL.'/account/logout'.PAGE_EXTENSION);
258
	define('FORGOT_URL', WB_URL.'/account/forgot'.PAGE_EXTENSION);
259
	define('PREFERENCES_URL', WB_URL.'/account/preferences'.PAGE_EXTENSION);
260
	define('SIGNUP_URL', WB_URL.'/account/signup'.PAGE_EXTENSION);
261
}
262
// Check user is allow to view this page
263
if(FRONTEND_LOGIN AND VISIBILITY == 'private' OR FRONTEND_LOGIN AND VISIBILITY == 'registered') {
264
	// Double-check front-end login is enabled
265
	if(FRONTEND_LOGIN != true) {
266
		// Users shouldnt be allowed to view private pages
267
		header("Location: ".WB_URL.PAGES_DIRECTORY."/index".PAGE_EXTENSION);
268
	}
269
	// Check if the user is authenticated
270
	if($admin->is_authenticated() == false) {
271
		// User needs to login first
272
		header("Location: ".WB_URL."/account/login".PAGE_EXTENSION);
273
	}
274
	// Check if we should show this page
275
	if($admin->show_page($page, $admin) == false) {
276
		// User isnt allowed on this page so tell them
277
		function page_content($block = 1) {
278
			global $MESSAGE;
279
			echo $MESSAGE['FRONTEND']['SORRY_NO_VIEWING_PERMISSIONS'];
280
		}
281
	}
282
	// Set extra private sql code
283
	$extra_sql = ",viewing_groups,viewing_users";
284
	$extra_where_sql = "visibility != 'none' AND visibility != 'hidden' AND visibility != 'deleted'";
285
} elseif(!FRONTEND_LOGIN AND VISIBILITY == 'private' OR !FRONTEND_LOGIN AND VISIBILITY == 'registered') {
286
	// User isnt allowed on this page so tell them
287
	function page_content($block = 1) {
288
		global $MESSAGE;
289
		echo $MESSAGE['FRONTEND']['SORRY_NO_VIEWING_PERMISSIONS'];
290
	}
291
} elseif(VISIBILITY == 'deleted') {
292
	// User isnt allowed on this page so tell them
293
	function page_content($block = 1) {
294
		global $MESSAGE;
295
		echo $MESSAGE['FRONTEND']['SORRY_NO_VIEWING_PERMISSIONS'];
296
	}
297
}
298
if(!isset($extra_sql)) {
299
	// Set extra private sql code
300
	if(FRONTEND_LOGIN == 'enabled') {
301
		if($admin->is_authenticated()) {
302
			$extra_sql = '';
303
			$extra_where_sql = "visibility != 'none' AND visibility != 'hidden' AND visibility != 'deleted'";
304
		} else {
305
			$extra_sql = '';
306
			$extra_where_sql = "visibility != 'none' AND visibility != 'hidden' AND visibility != 'deleted' AND visibility != 'private'";
307
		}
308
	} else {
309
		$extra_sql = '';
310
		$extra_where_sql = "visibility != 'none' AND visibility != 'hidden' AND visibility != 'deleted' AND visibility != 'private' AND visibility != 'registered'";
311
	}
312
}
313
// Check if we should add page language sql code
314
if(PAGE_LANGUAGES) {
315
	if(defined('GET_LANGUAGE')) {
316
		$extra_where_sql .= " AND language = '".LANGUAGE."'";
317
	} elseif(defined('USER_LANGUAGE')) {
318
		$extra_where_sql .= " AND language = '".DEFAULT_LANGUAGE."'";
319
	} else {
320
		$extra_where_sql .= " AND language = '".DEFAULT_LANGUAGE."'";
321
	}
322
}
323
// Get website settings (title, keywords, description, header, and footer)
324
$query_settings = "SELECT name,value FROM ".TABLE_PREFIX."settings";
325
$get_settings = $database->query($query_settings);
326
while($setting = $get_settings->fetchRow()) {
327
	switch($setting['name']) {
328
		case 'title':
329
			define('WEBSITE_TITLE', stripslashes($setting['value']));
330
		break;
331
		case 'description':
332
			if($page_description != '') {
333
				define('WEBSITE_DESCRIPTION', $page_description);
334
			} else {
335
				define('WEBSITE_DESCRIPTION', stripslashes($setting['value']));
336
			}
337
		break;
338
		case 'keywords':
339
			if($page_keywords != '') {
340
				define('WEBSITE_KEYWORDS', stripslashes($setting['value']).' '.$page_keywords);
341
			} else {
342
				define('WEBSITE_KEYWORDS', stripslashes($setting['value']));
343
			}
344
		break;
345
		case 'header':
346
			define('WEBSITE_HEADER', stripslashes($setting['value']));
347
		break;
348
		case 'footer':
349
			define('WEBSITE_FOOTER', stripslashes($setting['value']));
350
		break;
351
	}
352
}
353
// Figure out what template to use
354
if(!defined('TEMPLATE')) {
355
	if(isset($page['template']) AND $page['template'] != '') {
356
		if(file_exists(WB_PATH.'/templates/'.$page['template'].'/index.php')) {
357
			define('TEMPLATE', $page['template']);
358
		} else {
359
			define('TEMPLATE', DEFAULT_TEMPLATE);
360
		}
361
	} else {
362
		define('TEMPLATE', DEFAULT_TEMPLATE);
363
	}
364
}
365
// Set the template dir
366
define('TEMPLATE_DIR', WB_URL.'/templates/'.TEMPLATE);
367
/*
368
End page details code
369
*/
370
/*
371
Begin Template functions
372
*/
373
// Function for page title
374
function page_title($spacer = ' - ', $template = '[WEBSITE_TITLE][SPACER][PAGE_TITLE]') {
375
	$vars = array('[WEBSITE_TITLE]', '[PAGE_TITLE]', '[MENU_TITLE]', '[SPACER]');
376
	$values = array(WEBSITE_TITLE, PAGE_TITLE, MENU_TITLE, $spacer);
377
	echo str_replace($vars, $values, $template);
378
}
379
// Function for page description
380
function page_description() {
381
	echo WEBSITE_DESCRIPTION;
382
}
383
// Function for page keywords
384
function page_keywords() {
385
	echo WEBSITE_KEYWORDS;
386
}
387
// Function for page header
388
function page_header($date_format = 'Y') {
389
	echo WEBSITE_HEADER;
390
}
391
// Function for page footer
392
function page_footer($date_format = 'Y') {
393
	echo str_replace('[YEAR]', date($date_format), WEBSITE_FOOTER);
394
}
395
// Function to generate menu
396
function page_menu($parent = 0, $menu_number = 1, $item_template = '<li[class]>[a][menu_title][/a]</li>', $menu_header = '<ul>', $menu_footer = '</ul>', $default_class = ' class="menu_default"', $current_class = ' class="menu_current"', $recurse = LEVEL) {
397
	global $database, $admin, $page_id, $page_trail, $default_link, $extra_sql, $extra_where_sql;
398
	// Check if we should add menu number check to query
399
	if($parent == 0) {
400
		 $menu_number = "menu = '$menu_number'";
401
	} else {
402
		$menu_number = '1';
403
	}
404
	// Query pages
405
	$query_menu = $database->query("SELECT page_id,menu_title,page_title,link,target,level,visibility$extra_sql FROM ".TABLE_PREFIX."pages WHERE parent = '$parent' AND $menu_number AND $extra_where_sql ORDER BY position ASC");
406
	// Check if there are any pages to show
407
	if($query_menu->numRows() > 0) {
408
		// Print menu header
409
		echo $menu_header;
410
		// Loop through pages
411
		while($page = $query_menu->fetchRow()) {
412
			// Create vars
413
			$vars = array('[class]', '[a]', '[/a]', '[menu_title]', '[page_title]');
414
			// Work-out class
415
			if($page['page_id'] == PAGE_ID) {
416
				$class = $current_class;
417
			} else {
418
				$class = $default_class;
419
			}
420
			// Check if link is same as first page link, and if so change to WB URL
421
			if($page['link'] == $default_link AND !INTRO_PAGE) {
422
				$link = WB_URL;
423
			} else {
424
				$link = page_link($page['link']);
425
			}
426
			// Create values
427
			$values = array($class, '<a href="'.$link.'" target="'.$page['target'].'">', '</a>', stripslashes($page['menu_title']), stripslashes($page['page_title']));
428
			// Replace vars with value and print
429
			echo str_replace($vars, $values, $item_template);
430
			// Generate sub-menu
431
			if(isset($page_trail[$page['page_id']])) {
432
				page_menu($page['page_id'], $menu_number, $item_template, $menu_header, $menu_footer, $default_class, $current_class, $recurse-1);
433
			}
434
		}
435
		// Print menu footer
436
		echo $menu_footer;
437
	}
438
}
439
// Function for page content
440
$globals[] = 'database';
441
$globals[] = 'admin';
442
$globals[] = 'TEXT';
443
$globals[] = 'MENU';
444
$globals[] = 'HEADING';
445
$globals[] = 'MESSAGE';
446
if(!function_exists('page_content')) {
447
	function page_content($block = 1) {
448
		// Get outside objects
449
		global $globals;
450
		if(isset($globals) AND is_array($globals)) { foreach($globals AS $global_name) { global $$global_name; } }
451
		// Make sure block is numeric
452
		if(!is_numeric($block)) { $block = 1; }
453
		// Include page content
454
		if(!defined('PAGE_CONTENT')) {
455
			// First get all sections for this page
456
			$query_sections = $database->query("SELECT section_id,module FROM ".TABLE_PREFIX."sections WHERE page_id = '".PAGE_ID."' AND block = '$block' ORDER BY position");
457
			if($query_sections->numRows() > 0) {
458
				// Loop through them and include there modules file
459
				while($section = $query_sections->fetchRow()) {
460
					$section_id = $section['section_id'];
461
					$module = $section['module'];
462
					require(WB_PATH.'/modules/'.$module.'/view.php');
463
				}
464
			}
465
		} else {
466
			if($block == 1) {
467
				require(PAGE_CONTENT);
468
			}
469
		}
470
	}
471
}
472
/*
473
End Template functions
474
*/
475
// Begin WB < 2.4.x template compatibility code
476
	// Make extra_sql accessable through private_sql
477
	$private_sql = $extra_sql;
478
	$private_where_sql = $extra_where_sql;
479
	// Query pages for menu
480
	$menu1 = $database->query("SELECT page_id,menu_title,page_title,link,target,visibility$extra_sql FROM ".TABLE_PREFIX."pages WHERE parent = '0' AND $extra_where_sql ORDER BY position ASC");
481
	// Check if current pages is a parent page and if we need its submenu
482
	if(PARENT == 0) {
483
		// Get the pages submenu
484
		$menu2 = $database->query("SELECT page_id,menu_title,page_title,link,target,visibility$extra_sql FROM ".TABLE_PREFIX."pages WHERE parent = '".PAGE_ID."' AND $extra_where_sql ORDER BY position ASC");
485
	} else {
486
		// Get the pages submenu
487
		$menu2 = $database->query("SELECT page_id,menu_title,page_title,link,target,visibility$extra_sql FROM ".TABLE_PREFIX."pages WHERE parent = '".PARENT."' AND $extra_where_sql ORDER BY position ASC");
488
	}
489
// End WB < 2.4.x template compatibility code
490
// Include template file
491
require(WB_PATH.'/templates/'.TEMPLATE.'/index.php');
492
493
?>