Project

General

Profile

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