Project

General

Profile

1
<?php
2
/**
3
 *
4
 * @category        framework
5
 * @package         frontend
6
 * @author          Ryan Djurovich, WebsiteBaker Project
7
 * @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: class.frontend.php 1685 2012-05-06 14:29:47Z darkviper $
13
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/class.frontend.php $
14
 * @lastmodified    $Date: 2012-05-06 16:29:47 +0200 (Sun, 06 May 2012) $
15
 *
16
*/
17
/* -------------------------------------------------------- */
18
// Must include code to stop this file being accessed directly
19
if(!defined('WB_PATH')) {
20
	require_once(dirname(__FILE__).'/globalExceptionHandler.php');
21
	throw new IllegalFileException();
22
}
23
/* -------------------------------------------------------- */
24
require_once(WB_PATH.'/framework/class.wb.php');
25
//require_once(WB_PATH.'/framework/SecureForm.php');
26

    
27
class frontend extends wb {
28
	// defaults
29
	public $default_link,$default_page_id;
30
	// when multiple blocks are used, show home page blocks on
31
	// 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
	public $page_id,$page_title,$menu_title,$parent,$root_parent,$level,$position,$visibility;
38
	public $page_description,$page_keywords,$page_link, $page_icon, $menu_icon_0, $menu_icon_1, $tooltip;
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
			$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
		$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
		$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
			$sql = 'SELECT * FROM `'.TABLE_PREFIX.'pages` WHERE `page_id`='.(int)$this->page_id;
129
			$get_page = $database->query($sql);
130
			// 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
			$this->page_icon = $this->page['page_icon'];
161
			$this->menu_icon_0 = $this->page['menu_icon_0'];
162
			$this->menu_icon_1 = $this->page['menu_icon_1'];
163
			$this->tooltip = $this->page['tooltip'];
164
			// Page parent
165
			if(!defined('PARENT')) {define('PARENT', $this->page['parent']);}
166
			$this->parent=$this->page['parent'];
167
			// Page root parent
168
			if(!defined('ROOT_PARENT')) {define('ROOT_PARENT', $this->page['root_parent']);}
169
			$this->root_parent=$this->page['root_parent'];
170
			// Page level
171
			if(!defined('LEVEL')) {define('LEVEL', $this->page['level']);}
172
			$this->level=$this->page['level'];
173
			// Page position
174
			$this->level=$this->page['position'];
175
			// Page visibility
176
			if(!defined('VISIBILITY')) {define('VISIBILITY', $this->page['visibility']);}
177
			$this->visibility=$this->page['visibility'];
178
			// Page trail
179
			foreach(explode(',', $this->page['page_trail']) AS $pid) {
180
				$this->page_trail[$pid]=$pid;
181
			}
182
			// Page description
183
			$this->page_description=$this->page['description'];
184
			if($this->page_description != '') {
185
				define('PAGE_DESCRIPTION', $this->page_description);
186
			} else {
187
				define('PAGE_DESCRIPTION', WEBSITE_DESCRIPTION);
188
			}
189
			// Page keywords
190
			$this->page_keywords=$this->page['keywords'];
191
			// Page link
192
			$this->link=$this->page_link($this->page['link']);
193
			$_SESSION['PAGE_ID'] = $this->page_id;
194
			$_SESSION['HTTP_REFERER'] = $this->link;
195

    
196
		// End code to set details as either variables of constants
197
		}
198

    
199
		// Figure out what template to use
200
		if(!defined('TEMPLATE')) {
201
			if(isset($this->page['template']) AND $this->page['template'] != '') {
202
				if(file_exists(WB_PATH.'/templates/'.$this->page['template'].'/index.php')) {
203
					define('TEMPLATE', $this->page['template']);
204
				} else {
205
					define('TEMPLATE', DEFAULT_TEMPLATE);
206
				}
207
			} else {
208
				define('TEMPLATE', DEFAULT_TEMPLATE);
209
			}
210
		}
211
		// Set the template dir
212
		define('TEMPLATE_DIR', WB_URL.'/templates/'.TEMPLATE);
213

    
214
		// Check if user is allowed to view this page
215
		if($this->page && $this->page_is_visible($this->page) == false) {
216
			if(VISIBILITY == 'deleted' OR VISIBILITY == 'none') {
217
				// User isnt allowed on this page so tell them
218
				$this->page_access_denied=true;
219
			} elseif(VISIBILITY == 'private' OR VISIBILITY == 'registered') {
220
				// Check if the user is authenticated
221
				if($this->is_authenticated() == false) {
222
					// User needs to login first
223
					header("Location: ".WB_URL."/account/login.php?redirect=".$this->link);
224
					exit(0);
225
				} else {
226
					// User isnt allowed on this page so tell them
227
					$this->page_access_denied=true;
228
				}
229
				
230
			}
231
		}
232
		// check if there is at least one active section
233
		if($this->page && $this->page_is_active($this->page) == false) {
234
			$this->page_no_active_sections=true;
235
		}
236
	}
237

    
238
	public function get_website_settings()
239
    {
240
		global $database;
241

    
242
		// set visibility SQL code
243
		// never show no-vis, hidden or deleted pages
244
		$this->extra_where_sql = '`visibility`!=\'none\' AND `visibility`!=\'hidden\' AND `visibility`!=\'deleted\'';
245
		// Set extra private sql code
246
		if($this->is_authenticated()==false) {
247
			// if user is not authenticated, don't show private pages either
248
			$this->extra_where_sql .= ' AND `visibility`!=\'private\'';
249
			// and 'registered' without frontend login doesn't make much sense!
250
			if (FRONTEND_LOGIN==false) {
251
				$this->extra_where_sql .= ' AND `visibility`!=\'registered\'';
252
			}
253
		}
254
		$this->extra_where_sql .= $this->sql_where_language;
255

    
256
		// Work-out if any possible in-line search boxes should be shown
257
		if(SEARCH == 'public') {
258
			define('SHOW_SEARCH', true);
259
		} elseif(SEARCH == 'private' AND VISIBILITY == 'private') {
260
			define('SHOW_SEARCH', true);
261
		} elseif(SEARCH == 'private' AND $this->is_authenticated() == true) {
262
			define('SHOW_SEARCH', true);
263
		} elseif(SEARCH == 'registered' AND $this->is_authenticated() == true) {
264
			define('SHOW_SEARCH', true);	
265
		} else {
266
			define('SHOW_SEARCH', false);
267
		}
268
		// Work-out if menu should be shown
269
		if(!defined('SHOW_MENU')) {
270
			define('SHOW_MENU', true);
271
		}
272
		// Work-out if login menu constants should be set
273
		if(FRONTEND_LOGIN) {
274
			// Set login menu constants
275
			define('LOGIN_URL', WB_URL.'/account/login.php');
276
			define('LOGOUT_URL', WB_URL.'/account/logout.php');
277
			define('FORGOT_URL', WB_URL.'/account/forgot.php');
278
			define('PREFERENCES_URL', WB_URL.'/account/preferences.php');
279
			define('SIGNUP_URL', WB_URL.'/account/signup.php');
280
		}
281
	}
282

    
283
/*
284
 * replace all "[wblink{page_id}]" with real links
285
 * @param string &$content : reference to global $content
286
 * @return void
287
 * @history 100216 17:00:00 optimise errorhandling, speed, SQL-strict
288
 */
289
	public function preprocess(&$content)
290
	{
291
		global $database;
292
		$replace_list = array();
293
		$pattern = '/\[wblink([0-9]+)\]/isU';
294
		if(preg_match_all($pattern,$content,$ids))
295
		{
296
			foreach($ids[1] as $key => $page_id)
297
			{
298
				$replace_list[$page_id] = $ids[0][$key];
299
			}
300
			foreach($replace_list as $page_id => $tag)
301
			{
302
				$sql = 'SELECT `link` FROM `'.TABLE_PREFIX.'pages` WHERE `page_id` = '.(int)$page_id;
303
				$link = $database->get_one($sql);
304
				if(!is_null($link))
305
				{
306
					$link = $this->page_link($link);
307
					$content = str_replace($tag, $link, $content);
308
				}
309
			}
310
		}
311
	}
312

    
313
/*
314
	function preprocess(&$content) {
315
		global $database;
316
		// Replace [wblink--PAGE_ID--] with real link
317
		$pattern = '/\[wblink(.+?)\]/s';
318
		preg_match_all($pattern,$content,$ids);
319
		foreach($ids[1] AS $page_id) {
320
			$pattern = '/\[wblink'.$page_id.'\]/s';
321
			// Get page link
322
			$get_link = $database->query("SELECT link FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id' LIMIT 1");
323
			$fetch_link = $get_link->fetchRow();
324
			$link = $this->page_link($fetch_link['link']);
325
			$content = preg_replace($pattern,$link,$content);
326
		}
327
	}
328
*/
329
	public function menu() {
330
		global $wb;
331
	   if (!isset($wb->menu_number)) {
332
	   	$wb->menu_number = 1;
333
	   }
334
	   if (!isset($wb->menu_start_level)) {
335
	   	$wb->menu_start_level = 0;
336
	   }
337
	   if (!isset($wb->menu_recurse)) {
338
	   	$wb->menu_recurse = -1;
339
	   }
340
	   if (!isset($wb->menu_collapse)) {
341
	   	$wb->menu_collapse = true;
342
	   }
343
	   if (!isset($wb->menu_item_template)) {
344
	   	$wb->menu_item_template = '<li><span[class]>[a] [menu_title] [/a]</span>';
345
	   }
346
	   if (!isset($wb->menu_item_footer)) {
347
	   	$wb->menu_item_footer = '</li>';
348
	   }
349
	   if (!isset($wb->menu_header)) {
350
	   	$wb->menu_header = '<ul>';
351
	   }
352
	   if (!isset($wb->menu_footer)) {
353
	   	$wb->menu_footer = '</ul>';
354
	   }
355
	   if (!isset($wb->menu_default_class)) {
356
	   	$wb->menu_default_class = ' class="menu_default"';
357
	   }
358
	   if (!isset($wb->menu_current_class)) {
359
	   	$wb->menu_current_class = ' class="menu_current"';
360
	   }
361
	   if (!isset($wb->menu_parent)) {
362
	   	$wb->menu_parent = 0;
363
	   }
364
	   $wb->show_menu();
365
	}
366
	
367
	public function show_menu() {
368
		global $database;
369
		if ($this->menu_start_level>0) {
370
			$key_array=array_keys($this->page_trail);
371
			if (isset($key_array[$this->menu_start_level-1])) {
372
				$real_start=$key_array[$this->menu_start_level-1];
373
				$this->menu_parent=$real_start;
374
				$this->menu_start_level=0;
375
			} else {
376
				return;
377
			}
378
		}
379
		if ($this->menu_recurse==0)
380
	       return;
381
		// Check if we should add menu number check to query
382
		if($this->menu_parent == 0) {
383
			$menu_number = '`menu`='.intval($this->menu_number);
384
		} else {
385
			$menu_number = '1';
386
		}
387
		// Query pages
388
		$sql  = 'SELECT `page_id`,`menu_title`,`page_title`,`link`,`target`,`level`,';
389
		$sql .=        '`visibility`,viewing_groups,viewing_users ';
390
		$sql .= 'FROM `'.TABLE_PREFIX.'pages` ';
391
		$sql .= 'WHERE `parent`='.(int)$this->menu_parent.' AND '.$menu_number.' AND '.$this->extra_where_sql.' ';
392
		$sql .= 'ORDER BY `position` ASC';
393
		$query_menu = $database->query($sql);
394
		// Check if there are any pages to show
395
		if($query_menu->numRows() > 0) {
396
			// Print menu header
397
			echo "\n".$this->menu_header;
398
			// Loop through pages
399
			while($page = $query_menu->fetchRow()) {
400
				// check whether to show this menu-link
401
				if($this->page_is_active($page)==false && $page['link']!=$this->default_link && !INTRO_PAGE) {
402
					continue; // no active sections
403
				}
404
				if($this->page_is_visible($page)==false) {
405
					if($page['visibility'] != 'registered') // special case: page_to_visible() check wheter to show the page contents, but the menu should be visible allways
406
						continue;
407
				}
408
				// Create vars
409
				$vars = array('[class]','[a]', '[/a]', '[menu_title]', '[page_title]');
410
				// Work-out class
411
				if($page['page_id'] == PAGE_ID) {
412
					$class = $this->menu_current_class;
413
				} else {
414
					$class = $this->menu_default_class;
415
				}
416
				// Check if link is same as first page link, and if so change to WB URL
417
				if($page['link'] == $this->default_link AND !INTRO_PAGE) {
418
					$link = WB_URL;
419
				} else {
420
					$link = $this->page_link($page['link']);
421
				}
422
				// Create values
423
				$values = array($class,'<a href="'.$link.'" target="'.$page['target'].'" '.$class.'>', '</a>', $page['menu_title'], $page['page_title']);
424
				// Replace vars with value and print
425
				echo "\n".str_replace($vars, $values, $this->menu_item_template);
426
				// Generate sub-menu
427
				if($this->menu_collapse==false OR ($this->menu_collapse==true AND isset($this->page_trail[$page['page_id']]))) {
428
					$this->menu_recurse--;
429
					$this->menu_parent=$page['page_id'];
430
					$this->show_menu();
431
				}
432
				echo "\n".$this->menu_item_footer;
433
			}
434
			// Print menu footer
435
			echo "\n".$this->menu_footer;
436
		}
437
	}
438

    
439

    
440
	// Function to show the "Under Construction" page
441
	public function print_under_construction() {
442
		global $MESSAGE;
443
		require_once(WB_PATH.'/languages/'.DEFAULT_LANGUAGE.'.php');
444
		echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
445
		<head><title>'.$MESSAGE['GENERIC']['WEBSITE_UNDER_CONSTRUCTION'].'</title>
446
		<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; }
447
		h1 { margin: 0; padding: 0; font-size: 18px; color: #000; text-transform: uppercase;
448
}--></style></head><body>
449
		<br /><h1>'.$MESSAGE['GENERIC']['WEBSITE_UNDER_CONSTRUCTION'].'</h1><br />
450
		'.$MESSAGE['GENERIC']['PLEASE_CHECK_BACK_SOON'].'</body></html>';
451
	}
452
}
453

    
454
?>
(13-13/25)