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 1420 2011-01-26 17:43:56Z Luisehahne $
14
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/class.frontend.php $
15
 * @lastmodified    $Date: 2011-01-26 18:43:56 +0100 (Wed, 26 Jan 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,$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
		$query_default = "
80
			SELECT `p`.`page_id`, `link`
81
			FROM `$table_p` AS `p` INNER JOIN `$table_s` USING(`page_id`)
82
			WHERE `parent` = '0' AND `visibility` = 'public'
83
			AND (($now>=`publ_start` OR `publ_start`=0) AND ($now<=`publ_end` OR `publ_end`=0))
84
			$this->sql_where_language
85
			ORDER BY `p`.`position` ASC LIMIT 1";
86
		$get_default = $database->query($query_default);
87
		$default_num_rows = $get_default->numRows();
88
		if(!isset($page_id) OR !is_numeric($page_id)){
89
			// Go to or show default page
90
			if($default_num_rows > 0) {
91
				$fetch_default = $get_default->fetchRow();
92
				$this->default_link = $fetch_default['link'];
93
				$this->default_page_id = $fetch_default['page_id'];
94
				// Check if we should redirect or include page inline
95
				if(HOMEPAGE_REDIRECTION) {
96
					// Redirect to page
97
					header("Location: ".$this->page_link($this->default_link));
98
					exit();
99
				} else {
100
					// Include page inline
101
					$this->page_id = $this->default_page_id;
102
				}
103
			} else {
104
		   		// No pages have been added, so print under construction page
105
				$this->print_under_construction();
106
				exit();
107
			}
108
		} else {
109
			$this->page_id=$page_id;
110
		}
111
		// Get default page link
112
		if(!isset($fetch_default)) {
113
		  	$fetch_default = $get_default->fetchRow();
114
	 		$this->default_link = $fetch_default['link'];
115
			$this->default_page_id = $fetch_default['page_id'];
116
		}
117
		return true;
118
	}
119

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

    
184
		// End code to set details as either variables of constants
185
		}
186

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

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

    
226
	public function get_website_settings()
227
    {
228
		global $database;
229

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

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

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

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

    
422

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

    
437
?>
(6-6/16)