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 1462 2011-06-30 17:21:40Z DarkViper $
14
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/class.frontend.php $
15
 * @lastmodified    $Date: 2011-06-30 19:21:40 +0200 (Thu, 30 Jun 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
		$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 position
166
			$this->level=$this->page['position'];
167
			// Page visibility
168
			if(!defined('VISIBILITY')) {define('VISIBILITY', $this->page['visibility']);}
169
			$this->visibility=$this->page['visibility'];
170
			// Page trail
171
			foreach(explode(',', $this->page['page_trail']) AS $pid) {
172
				$this->page_trail[$pid]=$pid;
173
			}
174
			// Page description
175
			$this->page_description=$this->page['description'];
176
			if($this->page_description != '') {
177
				define('PAGE_DESCRIPTION', $this->page_description);
178
			} else {
179
				define('PAGE_DESCRIPTION', WEBSITE_DESCRIPTION);
180
			}
181
			// Page keywords
182
			$this->page_keywords=$this->page['keywords'];
183
			// Page link
184
			$this->link=$this->page_link($this->page['link']);
185

    
186
		// End code to set details as either variables of constants
187
		}
188

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

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

    
228
	public function get_website_settings()
229
    {
230
		global $database;
231

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

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

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

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

    
424

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

    
439
?>
(8-8/18)