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 1486 2011-08-08 12:03:20Z DarkViper $
14
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/class.frontend.php $
15
 * @lastmodified    $Date: 2011-08-08 14:03:20 +0200 (Mon, 08 Aug 2011) $
16
 *
17
*/
18

    
19
// Must include code to stop this file being access directly
20
if(defined('WB_PATH') == false) { die("Cannot access this file directly"); }
21

    
22
require_once(WB_PATH.'/framework/class.wb.php');
23
//require_once(WB_PATH.'/framework/SecureForm.php');
24

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

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

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

    
52
	public function page_select() {
53
		global $page_id,$no_intro;
54
		global $database;
55
		// We have no page id and are supposed to show the intro page
56
		if((INTRO_PAGE AND !isset($no_intro)) AND (!isset($page_id) OR !is_numeric($page_id))) {
57
			// Since we have no page id check if we should go to intro page or default page
58
			// Get intro page content
59
			$filename = WB_PATH.PAGES_DIRECTORY.'/intro'.PAGE_EXTENSION;
60
			if(file_exists($filename)) {
61
				$handle = @fopen($filename, "r");
62
				$content = @fread($handle, filesize($filename));
63
				@fclose($handle);
64
				$this->preprocess($content);
65
				header("Location: ".WB_URL.PAGES_DIRECTORY."/intro".PAGE_EXTENSION."");   // send intro.php as header to allow parsing of php statements
66
				echo ($content);
67
				return false;
68
			}
69
		}
70
		// Check if we should add page language sql code
71
		if(PAGE_LANGUAGES) {
72
			$this->sql_where_language = ' AND `language`=\''.LANGUAGE.'\'';
73
		}
74
		// Get default page
75
		// Check for a page id
76
		$table_p = TABLE_PREFIX.'pages';
77
		$table_s = TABLE_PREFIX.'sections';
78
		$now = time();
79
		$sql  = 'SELECT `p`.`page_id`, `link` ';
80
		$sql .= 'FROM `'.$table_p.'` AS `p` INNER JOIN `'.$table_s.'` USING(`page_id`) ';
81
		$sql .= 'WHERE `parent`=0 AND `visibility`=\'public\' ';
82
		$sql .=     'AND (('.$now.'>=`publ_start` OR `publ_start`=0) ';
83
		$sql .=     'AND ('.$now.'<=`publ_end` OR `publ_end`=0)) ';
84
		if(trim($this->sql_where_language) != '') {
85
			$sql .= trim($this->sql_where_language).' ';
86
		}
87
		$sql .= 'ORDER BY `p`.`position` ASC';
88
		$get_default = $database->query($sql);
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
	public function get_page_details() {
123
		global $database;
124
	    if($this->page_id != 0) {
125
			// Query page details
126
			$sql = 'SELECT * FROM `'.TABLE_PREFIX.'pages` WHERE `page_id`='.(int)$this->page_id;
127
			$get_page = $database->query($sql);
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
			if(!defined('PAGE_ID')) {define('PAGE_ID', $this->page['page_id']);}
147
			// Page Title
148
			if(!defined('PAGE_TITLE')) {define('PAGE_TITLE', $this->page['page_title']);}
149
			$this->page_title=PAGE_TITLE;
150
			// Menu Title
151
			$menu_title = $this->page['menu_title'];
152
			if($menu_title != '') {
153
				if(!defined('MENU_TITLE')) {define('MENU_TITLE', $menu_title);}
154
			} else {
155
				if(!defined('MENU_TITLE')) {define('MENU_TITLE', PAGE_TITLE);}
156
			}
157
			$this->menu_title = MENU_TITLE;
158
			// Page parent
159
			if(!defined('PARENT')) {define('PARENT', $this->page['parent']);}
160
			$this->parent=$this->page['parent'];
161
			// Page root parent
162
			if(!defined('ROOT_PARENT')) {define('ROOT_PARENT', $this->page['root_parent']);}
163
			$this->root_parent=$this->page['root_parent'];
164
			// Page level
165
			if(!defined('LEVEL')) {define('LEVEL', $this->page['level']);}
166
			$this->level=$this->page['level'];
167
			// Page position
168
			$this->level=$this->page['position'];
169
			// Page visibility
170
			if(!defined('VISIBILITY')) {define('VISIBILITY', $this->page['visibility']);}
171
			$this->visibility=$this->page['visibility'];
172
			// Page trail
173
			foreach(explode(',', $this->page['page_trail']) AS $pid) {
174
				$this->page_trail[$pid]=$pid;
175
			}
176
			// Page description
177
			$this->page_description=$this->page['description'];
178
			if($this->page_description != '') {
179
				define('PAGE_DESCRIPTION', $this->page_description);
180
			} else {
181
				define('PAGE_DESCRIPTION', WEBSITE_DESCRIPTION);
182
			}
183
			// Page keywords
184
			$this->page_keywords=$this->page['keywords'];
185
			// Page link
186
			$this->link=$this->page_link($this->page['link']);
187

    
188
		// End code to set details as either variables of constants
189
		}
190

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

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

    
230
	public function get_website_settings()
231
    {
232
		global $database;
233

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

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

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

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

    
431

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

    
446
?>
(8-8/19)