Project

General

Profile

1
<?php
2
/**
3
 *
4
 * @category        frontend
5
 * @package         framework
6
 * @author          WebsiteBaker Project
7
 * @copyright       2004-2009, Ryan Djurovich
8
 * @copyright       2009-2011, Website Baker Org. e.V.
9
 * @link			http://www.websitebaker2.org/
10
 * @license         http://www.gnu.org/licenses/gpl.html
11
 * @platform        WebsiteBaker 2.8.x
12
 * @requirements    PHP 5.2.2 and higher
13
 * @version         $Id: class.frontend.php 1499 2011-08-12 11:21:25Z DarkViper $
14
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/class.frontend.php $
15
 * @lastmodified    $Date: 2011-08-12 13:21:25 +0200 (Fri, 12 Aug 2011) $
16
 *
17
*/
18
/* -------------------------------------------------------- */
19
// Must include code to stop this file being accessed directly
20
if(!defined('WB_PATH')) {
21
	require_once(dirname(__FILE__).'/globalExceptionHandler.php');
22
	throw new IllegalFileException();
23
}
24
/* -------------------------------------------------------- */
25
require_once(WB_PATH.'/framework/class.wb.php');
26
//require_once(WB_PATH.'/framework/SecureForm.php');
27

    
28
class frontend extends wb {
29
	// defaults
30
	public $default_link,$default_page_id;
31
	// when multiple blocks are used, show home page blocks on
32
	// pages where no content is defined (search, login, ...)
33
	public $default_block_content=true;
34

    
35
	// page details
36
	// page database row
37
	public $page;
38
	public $page_id,$page_title,$menu_title,$parent,$root_parent,$level,$position,$visibility;
39
	public $page_description,$page_keywords,$page_link;
40
	public $page_trail=array();
41
	
42
	public $page_access_denied;
43
	public $page_no_active_sections;
44
	
45
	// website settings
46
	public $website_title,$website_description,$website_keywords,$website_header,$website_footer;
47

    
48
	// ugly database stuff
49
	public $extra_where_sql, $sql_where_language;
50
	
51
	public function __construct() {
52
		parent::__construct(SecureForm::FRONTEND);
53
	}
54

    
55
	public function page_select() {
56
		global $page_id,$no_intro;
57
		global $database;
58
		// We have no page id and are supposed to show the intro page
59
		if((INTRO_PAGE AND !isset($no_intro)) AND (!isset($page_id) OR !is_numeric($page_id))) {
60
			// Since we have no page id check if we should go to intro page or default page
61
			// Get intro page content
62
			$filename = WB_PATH.PAGES_DIRECTORY.'/intro'.PAGE_EXTENSION;
63
			if(file_exists($filename)) {
64
				$handle = @fopen($filename, "r");
65
				$content = @fread($handle, filesize($filename));
66
				@fclose($handle);
67
				$this->preprocess($content);
68
				header("Location: ".WB_URL.PAGES_DIRECTORY."/intro".PAGE_EXTENSION."");   // send intro.php as header to allow parsing of php statements
69
				echo ($content);
70
				return false;
71
			}
72
		}
73
		// Check if we should add page language sql code
74
		if(PAGE_LANGUAGES) {
75
			$this->sql_where_language = ' AND `language`=\''.LANGUAGE.'\'';
76
		}
77
		// Get default page
78
		// Check for a page id
79
		$table_p = TABLE_PREFIX.'pages';
80
		$table_s = TABLE_PREFIX.'sections';
81
		$now = time();
82
		$sql  = 'SELECT `p`.`page_id`, `link` ';
83
		$sql .= 'FROM `'.$table_p.'` AS `p` INNER JOIN `'.$table_s.'` USING(`page_id`) ';
84
		$sql .= 'WHERE `parent`=0 AND `visibility`=\'public\' ';
85
		$sql .=     'AND (('.$now.'>=`publ_start` OR `publ_start`=0) ';
86
		$sql .=     'AND ('.$now.'<=`publ_end` OR `publ_end`=0)) ';
87
		if(trim($this->sql_where_language) != '') {
88
			$sql .= trim($this->sql_where_language).' ';
89
		}
90
		$sql .= 'ORDER BY `p`.`position` ASC';
91
		$get_default = $database->query($sql);
92
		$default_num_rows = $get_default->numRows();
93
		if(!isset($page_id) OR !is_numeric($page_id)){
94
			// Go to or show default page
95
			if($default_num_rows > 0) {
96
				$fetch_default = $get_default->fetchRow();
97
				$this->default_link = $fetch_default['link'];
98
				$this->default_page_id = $fetch_default['page_id'];
99
				// Check if we should redirect or include page inline
100
				if(HOMEPAGE_REDIRECTION) {
101
					// Redirect to page
102
					header("Location: ".$this->page_link($this->default_link));
103
					exit();
104
				} else {
105
					// Include page inline
106
					$this->page_id = $this->default_page_id;
107
				}
108
			} else {
109
		   		// No pages have been added, so print under construction page
110
				$this->print_under_construction();
111
				exit();
112
			}
113
		} else {
114
			$this->page_id=$page_id;
115
		}
116
		// Get default page link
117
		if(!isset($fetch_default)) {
118
		  	$fetch_default = $get_default->fetchRow();
119
	 		$this->default_link = $fetch_default['link'];
120
			$this->default_page_id = $fetch_default['page_id'];
121
		}
122
		return true;
123
	}
124

    
125
	public function get_page_details() {
126
		global $database;
127
	    if($this->page_id != 0) {
128
			// Query page details
129
			$sql = 'SELECT * FROM `'.TABLE_PREFIX.'pages` WHERE `page_id`='.(int)$this->page_id;
130
			$get_page = $database->query($sql);
131
			// Make sure page was found in database
132
			if($get_page->numRows() == 0) {
133
				// Print page not found message
134
				exit("Page not found");
135
			}
136
			// Fetch page details
137
			$this->page = $get_page->fetchRow();
138
			// Check if the page language is also the selected language. If not, send headers again.
139
			if ($this->page['language']!=LANGUAGE) {
140
				if(isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != '') { // check if there is an query-string
141
					header('Location: '.$this->page_link($this->page['link']).'?'.$_SERVER['QUERY_STRING'].'&lang='.$this->page['language']);
142
				} else {
143
					header('Location: '.$this->page_link($this->page['link']).'?lang='.$this->page['language']);
144
				}
145
				exit();
146
			}
147
			// Begin code to set details as either variables of constants
148
			// Page ID
149
			if(!defined('PAGE_ID')) {define('PAGE_ID', $this->page['page_id']);}
150
			// Page Title
151
			if(!defined('PAGE_TITLE')) {define('PAGE_TITLE', $this->page['page_title']);}
152
			$this->page_title=PAGE_TITLE;
153
			// Menu Title
154
			$menu_title = $this->page['menu_title'];
155
			if($menu_title != '') {
156
				if(!defined('MENU_TITLE')) {define('MENU_TITLE', $menu_title);}
157
			} else {
158
				if(!defined('MENU_TITLE')) {define('MENU_TITLE', PAGE_TITLE);}
159
			}
160
			$this->menu_title = MENU_TITLE;
161
			// Page parent
162
			if(!defined('PARENT')) {define('PARENT', $this->page['parent']);}
163
			$this->parent=$this->page['parent'];
164
			// Page root parent
165
			if(!defined('ROOT_PARENT')) {define('ROOT_PARENT', $this->page['root_parent']);}
166
			$this->root_parent=$this->page['root_parent'];
167
			// Page level
168
			if(!defined('LEVEL')) {define('LEVEL', $this->page['level']);}
169
			$this->level=$this->page['level'];
170
			// Page position
171
			$this->level=$this->page['position'];
172
			// Page visibility
173
			if(!defined('VISIBILITY')) {define('VISIBILITY', $this->page['visibility']);}
174
			$this->visibility=$this->page['visibility'];
175
			// Page trail
176
			foreach(explode(',', $this->page['page_trail']) AS $pid) {
177
				$this->page_trail[$pid]=$pid;
178
			}
179
			// Page description
180
			$this->page_description=$this->page['description'];
181
			if($this->page_description != '') {
182
				define('PAGE_DESCRIPTION', $this->page_description);
183
			} else {
184
				define('PAGE_DESCRIPTION', WEBSITE_DESCRIPTION);
185
			}
186
			// Page keywords
187
			$this->page_keywords=$this->page['keywords'];
188
			// Page link
189
			$this->link=$this->page_link($this->page['link']);
190

    
191
		// End code to set details as either variables of constants
192
		}
193

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

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

    
233
	public function get_website_settings()
234
    {
235
		global $database;
236

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

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

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

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

    
434

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

    
449
?>
(9-9/20)