Project

General

Profile

1 1152 Luisehahne
<?php
2 1248 Luisehahne
/**
3
 *
4 1269 Luisehahne
 * @category        frontend
5
 * @package         framework
6
 * @author          WebsiteBaker Project
7
 * @copyright       2004-2009, Ryan Djurovich
8 1349 Luisehahne
 * @copyright       2009-2011, Website Baker Org. e.V.
9 1269 Luisehahne
 * @link			http://www.websitebaker2.org/
10
 * @license         http://www.gnu.org/licenses/gpl.html
11
 * @platform        WebsiteBaker 2.8.x
12 1373 Luisehahne
 *  * @requirements    PHP 5.2.2 and higher
13 1269 Luisehahne
 * @version         $Id$
14 1270 Luisehahne
 * @filesource		$HeadURL$
15
 * @lastmodified    $Date$
16 1248 Luisehahne
 *
17 1152 Luisehahne
*/
18
19
if(!defined('WB_PATH')) {
20
	header('Location: ../index.php');
21
	exit(0);
22
}
23
24
require_once(WB_PATH.'/framework/class.wb.php');
25
26
class frontend extends wb {
27
	// defaults
28
	var $default_link,$default_page_id;
29
	// when multiple blocks are used, show home page blocks on
30
	// pages where no content is defined (search, login, ...)
31
	var $default_block_content=true;
32
33
	// page details
34
	// page database row
35
	var $page;
36
	var $page_id,$page_title,$menu_title,$parent,$root_parent,$level,$visibility;
37
	var $page_description,$page_keywords,$page_link;
38
	var $page_trail=array();
39
40
	var $page_access_denied;
41
	var $page_no_active_sections;
42
43
	// website settings
44
	var $website_title,$website_description,$website_keywords,$website_header,$website_footer;
45
46
	// ugly database stuff
47
	var $extra_where_sql, $sql_where_language;
48
49
	function page_select() {
50
		global $page_id,$no_intro;
51
		global $database;
52
		// We have no page id and are supposed to show the intro page
53
		if((INTRO_PAGE AND !isset($no_intro)) AND (!isset($page_id) OR !is_numeric($page_id))) {
54
			// Since we have no page id check if we should go to intro page or default page
55
			// Get intro page content
56
			$filename = WB_PATH.PAGES_DIRECTORY.'/intro'.PAGE_EXTENSION;
57
			if(file_exists($filename)) {
58
				$handle = @fopen($filename, "r");
59
				$content = @fread($handle, filesize($filename));
60
				@fclose($handle);
61
				$this->preprocess($content);
62
				header("Location: ".WB_URL.PAGES_DIRECTORY."/intro".PAGE_EXTENSION."");   // send intro.php as header to allow parsing of php statements
63
				echo ($content);
64
				return false;
65
			}
66
		}
67
		// Check if we should add page language sql code
68
		if(PAGE_LANGUAGES) {
69
			$this->sql_where_language = " AND language = '".LANGUAGE."'";
70
		}
71
		// Get default page
72
		// Check for a page id
73
		$table_p = TABLE_PREFIX.'pages';
74
		$table_s = TABLE_PREFIX.'sections';
75
		$now = time();
76
		$query_default = "
77
			SELECT `p`.`page_id`, `link`
78
			FROM `$table_p` AS `p` INNER JOIN `$table_s` USING(`page_id`)
79
			WHERE `parent` = '0' AND `visibility` = 'public'
80
			AND (($now>=`publ_start` OR `publ_start`=0) AND ($now<=`publ_end` OR `publ_end`=0))
81
			$this->sql_where_language
82
			ORDER BY `p`.`position` ASC LIMIT 1";
83
		$get_default = $database->query($query_default);
84
		$default_num_rows = $get_default->numRows();
85
		if(!isset($page_id) OR !is_numeric($page_id)){
86
			// Go to or show default page
87
			if($default_num_rows > 0) {
88
				$fetch_default = $get_default->fetchRow();
89
				$this->default_link = $fetch_default['link'];
90
				$this->default_page_id = $fetch_default['page_id'];
91
				// Check if we should redirect or include page inline
92
				if(HOMEPAGE_REDIRECTION) {
93
					// Redirect to page
94
					header("Location: ".$this->page_link($this->default_link));
95
					exit();
96
				} else {
97
					// Include page inline
98
					$this->page_id = $this->default_page_id;
99
				}
100
			} else {
101
		   		// No pages have been added, so print under construction page
102
				$this->print_under_construction();
103
				exit();
104
			}
105
		} else {
106
			$this->page_id=$page_id;
107
		}
108
		// Get default page link
109
		if(!isset($fetch_default)) {
110
		  	$fetch_default = $get_default->fetchRow();
111
	 		$this->default_link = $fetch_default['link'];
112
			$this->default_page_id = $fetch_default['page_id'];
113
		}
114
		return true;
115
	}
116
117
	function get_page_details() {
118
		global $database;
119
	    if($this->page_id != 0) {
120
			// Query page details
121
			$query_page = "SELECT * FROM ".TABLE_PREFIX."pages WHERE page_id = '{$this->page_id}'";
122
			$get_page = $database->query($query_page);
123
			// Make sure page was found in database
124
			if($get_page->numRows() == 0) {
125
				// Print page not found message
126
				exit("Page not found");
127
			}
128
			// Fetch page details
129
			$this->page = $get_page->fetchRow();
130
			// Check if the page language is also the selected language. If not, send headers again.
131
			if ($this->page['language']!=LANGUAGE) {
132
				if(isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != '') { // check if there is an query-string
133
					header('Location: '.$this->page_link($this->page['link']).'?'.$_SERVER['QUERY_STRING'].'&lang='.$this->page['language']);
134
				} else {
135
					header('Location: '.$this->page_link($this->page['link']).'?lang='.$this->page['language']);
136
				}
137
				exit();
138
			}
139
			// Begin code to set details as either variables of constants
140
			// Page ID
141 1248 Luisehahne
			if(!defined('PAGE_ID')) {define('PAGE_ID', $this->page['page_id']);}
142 1152 Luisehahne
			// Page Title
143 1248 Luisehahne
			if(!defined('PAGE_TITLE')) {define('PAGE_TITLE', $this->page['page_title']);}
144 1152 Luisehahne
			$this->page_title=PAGE_TITLE;
145
			// Menu Title
146
			$menu_title = $this->page['menu_title'];
147
			if($menu_title != '') {
148 1248 Luisehahne
				if(!defined('MENU_TITLE')) {define('MENU_TITLE', $menu_title);}
149 1152 Luisehahne
			} else {
150 1248 Luisehahne
				if(!defined('MENU_TITLE')) {define('MENU_TITLE', PAGE_TITLE);}
151 1152 Luisehahne
			}
152 1248 Luisehahne
			$this->menu_title = MENU_TITLE;
153 1152 Luisehahne
			// Page parent
154 1248 Luisehahne
			if(!defined('PARENT')) {define('PARENT', $this->page['parent']);}
155 1152 Luisehahne
			$this->parent=$this->page['parent'];
156
			// Page root parent
157 1248 Luisehahne
			if(!defined('ROOT_PARENT')) {define('ROOT_PARENT', $this->page['root_parent']);}
158 1152 Luisehahne
			$this->root_parent=$this->page['root_parent'];
159
			// Page level
160 1248 Luisehahne
			if(!defined('LEVEL')) {define('LEVEL', $this->page['level']);}
161 1152 Luisehahne
			$this->level=$this->page['level'];
162
			// Page visibility
163 1248 Luisehahne
			if(!defined('VISIBILITY')) {define('VISIBILITY', $this->page['visibility']);}
164 1152 Luisehahne
			$this->visibility=$this->page['visibility'];
165
			// Page trail
166
			foreach(explode(',', $this->page['page_trail']) AS $pid) {
167
				$this->page_trail[$pid]=$pid;
168
			}
169
			// Page description
170
			$this->page_description=$this->page['description'];
171
			if($this->page_description != '') {
172
				define('PAGE_DESCRIPTION', $this->page_description);
173
			} else {
174
				define('PAGE_DESCRIPTION', WEBSITE_DESCRIPTION);
175
			}
176
			// Page keywords
177
			$this->page_keywords=$this->page['keywords'];
178
			// Page link
179
			$this->link=$this->page_link($this->page['link']);
180
181
		// End code to set details as either variables of constants
182
		}
183
184
		// Figure out what template to use
185
		if(!defined('TEMPLATE')) {
186
			if(isset($this->page['template']) AND $this->page['template'] != '') {
187
				if(file_exists(WB_PATH.'/templates/'.$this->page['template'].'/index.php')) {
188
					define('TEMPLATE', $this->page['template']);
189
				} else {
190
					define('TEMPLATE', DEFAULT_TEMPLATE);
191
				}
192
			} else {
193
				define('TEMPLATE', DEFAULT_TEMPLATE);
194
			}
195
		}
196
		// Set the template dir
197
		define('TEMPLATE_DIR', WB_URL.'/templates/'.TEMPLATE);
198
199
		// Check if user is allowed to view this page
200
		if($this->page && $this->page_is_visible($this->page) == false) {
201
			if(VISIBILITY == 'deleted' OR VISIBILITY == 'none') {
202
				// User isnt allowed on this page so tell them
203
				$this->page_access_denied=true;
204
			} elseif(VISIBILITY == 'private' OR VISIBILITY == 'registered') {
205
				// Check if the user is authenticated
206
				if($this->is_authenticated() == false) {
207
					// User needs to login first
208
					header("Location: ".WB_URL."/account/login.php?redirect=".$this->link);
209
					exit(0);
210
				} else {
211
					// User isnt allowed on this page so tell them
212
					$this->page_access_denied=true;
213
				}
214
215
			}
216
		}
217
		// check if there is at least one active section
218
		if($this->page && $this->page_is_active($this->page) == false) {
219
			$this->page_no_active_sections=true;
220
		}
221
	}
222
223 1291 Luisehahne
	function get_website_settings()
224
    {
225 1152 Luisehahne
		global $database;
226
227
		// set visibility SQL code
228
		// never show no-vis, hidden or deleted pages
229
		$this->extra_where_sql = "visibility != 'none' AND visibility != 'hidden' AND visibility != 'deleted'";
230
		// Set extra private sql code
231
		if($this->is_authenticated()==false) {
232
			// if user is not authenticated, don't show private pages either
233
			$this->extra_where_sql .= " AND visibility != 'private'";
234
			// and 'registered' without frontend login doesn't make much sense!
235
			if (FRONTEND_LOGIN==false) {
236
				$this->extra_where_sql .= " AND visibility != 'registered'";
237
			}
238
		}
239
		$this->extra_where_sql .= $this->sql_where_language;
240
241
		// Work-out if any possible in-line search boxes should be shown
242
		if(SEARCH == 'public') {
243
			define('SHOW_SEARCH', true);
244
		} elseif(SEARCH == 'private' AND VISIBILITY == 'private') {
245
			define('SHOW_SEARCH', true);
246
		} elseif(SEARCH == 'private' AND $this->is_authenticated() == true) {
247
			define('SHOW_SEARCH', true);
248
		} elseif(SEARCH == 'registered' AND $this->is_authenticated() == true) {
249
			define('SHOW_SEARCH', true);
250
		} else {
251
			define('SHOW_SEARCH', false);
252
		}
253
		// Work-out if menu should be shown
254
		if(!defined('SHOW_MENU')) {
255
			define('SHOW_MENU', true);
256
		}
257
		// Work-out if login menu constants should be set
258
		if(FRONTEND_LOGIN) {
259
			// Set login menu constants
260
			define('LOGIN_URL', WB_URL.'/account/login.php');
261
			define('LOGOUT_URL', WB_URL.'/account/logout.php');
262
			define('FORGOT_URL', WB_URL.'/account/forgot.php');
263
			define('PREFERENCES_URL', WB_URL.'/account/preferences.php');
264
			define('SIGNUP_URL', WB_URL.'/account/signup.php');
265
		}
266
	}
267 1291 Luisehahne
268
/*
269
 * replace all "[wblink{page_id}]" with real links
270
 * @param string &$content : reference to global $content
271
 * @return void
272
 * @history 100216 17:00:00 optimise errorhandling, speed, SQL-strict
273
 */
274
	function preprocess(&$content)
275
	{
276
		global $database;
277
		$replace_list = array();
278
		$pattern = '/\[wblink([0-9]+)\]/isU';
279
		if(preg_match_all($pattern,$content,$ids))
280
		{
281
			foreach($ids[1] as $key => $page_id)
282
			{
283
				$replace_list[$page_id] = $ids[0][$key];
284
			}
285
			foreach($replace_list as $page_id => $tag)
286
			{
287
				$sql = 'SELECT `link` FROM `'.TABLE_PREFIX.'pages` WHERE `page_id` = '.(int)$page_id;
288
				$link = $database->get_one($sql);
289
				if(!is_null($link))
290
				{
291
					$link = $this->page_link($link);
292
					$content = str_replace($tag, $link, $content);
293
				}
294
			}
295
		}
296
	}
297
298
/*
299 1152 Luisehahne
	function preprocess(&$content) {
300
		global $database;
301
		// Replace [wblink--PAGE_ID--] with real link
302
		$pattern = '/\[wblink(.+?)\]/s';
303
		preg_match_all($pattern,$content,$ids);
304
		foreach($ids[1] AS $page_id) {
305
			$pattern = '/\[wblink'.$page_id.'\]/s';
306
			// Get page link
307
			$get_link = $database->query("SELECT link FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id' LIMIT 1");
308
			$fetch_link = $get_link->fetchRow();
309
			$link = $this->page_link($fetch_link['link']);
310
			$content = preg_replace($pattern,$link,$content);
311
		}
312
	}
313 1291 Luisehahne
*/
314 1152 Luisehahne
	function menu() {
315
		global $wb;
316
	   if (!isset($wb->menu_number)) {
317
	   	$wb->menu_number = 1;
318
	   }
319
	   if (!isset($wb->menu_start_level)) {
320
	   	$wb->menu_start_level = 0;
321
	   }
322
	   if (!isset($wb->menu_recurse)) {
323
	   	$wb->menu_recurse = -1;
324
	   }
325
	   if (!isset($wb->menu_collapse)) {
326
	   	$wb->menu_collapse = true;
327
	   }
328
	   if (!isset($wb->menu_item_template)) {
329
	   	$wb->menu_item_template = '<li><span[class]>[a] [menu_title] [/a]</span>';
330
	   }
331
	   if (!isset($wb->menu_item_footer)) {
332
	   	$wb->menu_item_footer = '</li>';
333
	   }
334
	   if (!isset($wb->menu_header)) {
335
	   	$wb->menu_header = '<ul>';
336
	   }
337
	   if (!isset($wb->menu_footer)) {
338
	   	$wb->menu_footer = '</ul>';
339
	   }
340
	   if (!isset($wb->menu_default_class)) {
341
	   	$wb->menu_default_class = ' class="menu_default"';
342
	   }
343
	   if (!isset($wb->menu_current_class)) {
344
	   	$wb->menu_current_class = ' class="menu_current"';
345
	   }
346
	   if (!isset($wb->menu_parent)) {
347
	   	$wb->menu_parent = 0;
348
	   }
349
	   $wb->show_menu();
350
	}
351
352
	function show_menu() {
353
		global $database;
354
		if ($this->menu_start_level>0) {
355
			$key_array=array_keys($this->page_trail);
356
			if (isset($key_array[$this->menu_start_level-1])) {
357
				$real_start=$key_array[$this->menu_start_level-1];
358
				$this->menu_parent=$real_start;
359
				$this->menu_start_level=0;
360
			} else {
361
				return;
362
			}
363
		}
364
		if ($this->menu_recurse==0)
365
	       return;
366
		// Check if we should add menu number check to query
367
		if($this->menu_parent == 0) {
368
			$menu_number = "menu = '$this->menu_number'";
369
		} else {
370
			$menu_number = '1';
371
		}
372
		// Query pages
373
		$query_menu = $database->query("SELECT page_id,menu_title,page_title,link,target,level,visibility,viewing_groups,viewing_users FROM ".TABLE_PREFIX."pages WHERE parent = '$this->menu_parent' AND $menu_number AND $this->extra_where_sql ORDER BY position ASC");
374
		// Check if there are any pages to show
375
		if($query_menu->numRows() > 0) {
376
			// Print menu header
377
			echo "\n".$this->menu_header;
378
			// Loop through pages
379
			while($page = $query_menu->fetchRow()) {
380
				// check whether to show this menu-link
381
				if($this->page_is_active($page)==false && $page['link']!=$this->default_link && !INTRO_PAGE) {
382
					continue; // no active sections
383
				}
384
				if($this->page_is_visible($page)==false) {
385
					if($page['visibility'] != 'registered') // special case: page_to_visible() check wheter to show the page contents, but the menu should be visible allways
386
						continue;
387
				}
388
				// Create vars
389
				$vars = array('[class]','[a]', '[/a]', '[menu_title]', '[page_title]');
390
				// Work-out class
391
				if($page['page_id'] == PAGE_ID) {
392
					$class = $this->menu_current_class;
393
				} else {
394
					$class = $this->menu_default_class;
395
				}
396
				// Check if link is same as first page link, and if so change to WB URL
397
				if($page['link'] == $this->default_link AND !INTRO_PAGE) {
398
					$link = WB_URL;
399
				} else {
400
					$link = $this->page_link($page['link']);
401
				}
402
				// Create values
403
				$values = array($class,'<a href="'.$link.'" target="'.$page['target'].'" '.$class.'>', '</a>', $page['menu_title'], $page['page_title']);
404
				// Replace vars with value and print
405
				echo "\n".str_replace($vars, $values, $this->menu_item_template);
406
				// Generate sub-menu
407
				if($this->menu_collapse==false OR ($this->menu_collapse==true AND isset($this->page_trail[$page['page_id']]))) {
408
					$this->menu_recurse--;
409
					$this->menu_parent=$page['page_id'];
410
					$this->show_menu();
411
				}
412
				echo "\n".$this->menu_item_footer;
413
			}
414
			// Print menu footer
415
			echo "\n".$this->menu_footer;
416
		}
417
	}
418
419
420
	// Function to show the "Under Construction" page
421
	function print_under_construction() {
422
		global $MESSAGE;
423
		require_once(WB_PATH.'/languages/'.DEFAULT_LANGUAGE.'.php');
424
		echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
425
		<head><title>'.$MESSAGE['GENERIC']['WEBSITE_UNDER_CONSTRUCTION'].'</title>
426
		<style type="text/css"><!-- body{ font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 12px; background-image: url("'.ADMIN_URL.'/interface/background.png");background-repeat: repeat-x; background-color: #A8BCCB; text-align: center; }
427
		h1 { margin: 0; padding: 0; font-size: 18px; color: #000; text-transform: uppercase;
428
}--></style></head><body>
429
		<br /><h1>'.$MESSAGE['GENERIC']['WEBSITE_UNDER_CONSTRUCTION'].'</h1><br />
430
		'.$MESSAGE['GENERIC']['PLEASE_CHECK_BACK_SOON'].'</body></html>';
431
	}
432
}
433
434
?>