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