Project

General

Profile

1
<?php
2

    
3
// $Id: class.frontend.php 798 2008-04-04 19:40:52Z thorn $
4

    
5
/*
6

    
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2008, Ryan Djurovich
9

    
10
 Website Baker is free software; you can redistribute it and/or modify
11
 it under the terms of the GNU General Public License as published by
12
 the Free Software Foundation; either version 2 of the License, or
13
 (at your option) any later version.
14

    
15
 Website Baker is distributed in the hope that it will be useful,
16
 but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 GNU General Public License for more details.
19

    
20
 You should have received a copy of the GNU General Public License
21
 along with Website Baker; if not, write to the Free Software
22
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23

    
24
*/
25

    
26
/*
27

    
28
Frontend class
29

    
30
*/
31

    
32
if(!defined('WB_PATH')) {
33
	header('Location: ../index.php');
34
	exit(0);
35
}
36

    
37

    
38
require_once(WB_PATH.'/framework/class.wb.php');
39

    
40
class frontend extends wb {
41
	// defaults
42
	var $default_link,$default_page_id;
43
	// when multiple blocks are used, show home page blocks on 
44
	// pages where no content is defined (search, login, ...)
45
	var $default_block_content=true;
46

    
47
	// page details
48
	// page database row
49
	var $page;
50
	var $page_id,$page_title,$menu_title,$parent,$root_parent,$level,$visibility;
51
	var $page_description,$page_keywords,$page_link;
52
	var $page_trail=array();
53
	
54
	var $page_access_denied;
55
	var $page_no_active_sections;
56
	
57
	// website settings
58
	var $website_title,$website_description,$website_keywords,$website_header,$website_footer;
59

    
60
	// ugly database stuff
61
	var $extra_where_sql, $sql_where_language;
62

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

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

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

    
255
		// Work-out if any possible in-line search boxes should be shown
256
		if(SEARCH == 'public') {
257
			define('SHOW_SEARCH', true);
258
		} elseif(SEARCH == 'private' AND VISIBILITY == 'private') {
259
			define('SHOW_SEARCH', true);
260
		} elseif(SEARCH == 'private' AND $this->is_authenticated() == true) {
261
			define('SHOW_SEARCH', true);
262
		} elseif(SEARCH == 'registered' AND $this->is_authenticated() == true) {
263
			define('SHOW_SEARCH', true);	
264
		} else {
265
			define('SHOW_SEARCH', false);
266
		}
267
		// Work-out if menu should be shown
268
		if(!defined('SHOW_MENU')) {
269
			define('SHOW_MENU', true);
270
		}
271
		// Work-out if login menu constants should be set
272
		if(FRONTEND_LOGIN) {
273
			// Set login menu constants
274
			define('LOGIN_URL', WB_URL.'/account/login.php');
275
			define('LOGOUT_URL', WB_URL.'/account/logout.php');
276
			define('FORGOT_URL', WB_URL.'/account/forgot.php');
277
			define('PREFERENCES_URL', WB_URL.'/account/preferences.php');
278
			define('SIGNUP_URL', WB_URL.'/account/signup.php');
279
		}
280
	}
281
	
282
	function preprocess(&$content) {
283
		global $database;
284
		// Replace [wblink--PAGE_ID--] with real link
285
		$pattern = '/\[wblink(.+?)\]/s';
286
		preg_match_all($pattern,$content,$ids);
287
		foreach($ids[1] AS $page_id) {
288
			$pattern = '/\[wblink'.$page_id.'\]/s';
289
			// Get page link
290
			$get_link = $database->query("SELECT link FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id' LIMIT 1");
291
			$fetch_link = $get_link->fetchRow();
292
			$link = $this->page_link($fetch_link['link']);
293
			$content = preg_replace($pattern,$link,$content);
294
		}
295
	}
296
	
297
	function menu() {
298
		global $wb;
299
	   if (!isset($wb->menu_number)) {
300
	   	$wb->menu_number = 1;
301
	   }
302
	   if (!isset($wb->menu_start_level)) {
303
	   	$wb->menu_start_level = 0;
304
	   }
305
	   if (!isset($wb->menu_recurse)) {
306
	   	$wb->menu_recurse = -1;
307
	   }
308
	   if (!isset($wb->menu_collapse)) {
309
	   	$wb->menu_collapse = true;
310
	   }
311
	   if (!isset($wb->menu_item_template)) {
312
	   	$wb->menu_item_template = '<li><span[class]>[a] [menu_title] [/a]</span>';
313
	   }
314
	   if (!isset($wb->menu_item_footer)) {
315
	   	$wb->menu_item_footer = '</li>';
316
	   }
317
	   if (!isset($wb->menu_header)) {
318
	   	$wb->menu_header = '<ul>';
319
	   }
320
	   if (!isset($wb->menu_footer)) {
321
	   	$wb->menu_footer = '</ul>';
322
	   }
323
	   if (!isset($wb->menu_default_class)) {
324
	   	$wb->menu_default_class = ' class="menu_default"';
325
	   }
326
	   if (!isset($wb->menu_current_class)) {
327
	   	$wb->menu_current_class = ' class="menu_current"';
328
	   }
329
	   if (!isset($wb->menu_parent)) {
330
	   	$wb->menu_parent = 0;
331
	   }
332
	   $wb->show_menu();
333
	}
334
	
335
	function show_menu() {
336
		global $database;
337
		if ($this->menu_start_level>0) {
338
			$key_array=array_keys($this->page_trail);
339
			if (isset($key_array[$this->menu_start_level-1])) {
340
				$real_start=$key_array[$this->menu_start_level-1];
341
				$this->menu_parent=$real_start;
342
				$this->menu_start_level=0;
343
			} else {
344
				return;
345
			}
346
		}
347
		if ($this->menu_recurse==0)
348
	       return;
349
		// Check if we should add menu number check to query
350
		if($this->menu_parent == 0) {
351
			$menu_number = "menu = '$this->menu_number'";
352
		} else {
353
			$menu_number = '1';
354
		}
355
		// Query pages
356
		$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");
357
		// Check if there are any pages to show
358
		if($query_menu->numRows() > 0) {
359
			// Print menu header
360
			echo "\n".$this->menu_header;
361
			// Loop through pages
362
			while($page = $query_menu->fetchRow()) {
363
				// check whether to show this menu-link
364
				if($this->page_is_active($page)==false && $page['link']!=$this->default_link && !INTRO_PAGE) {
365
					continue; // no active sections
366
				}
367
				if($this->page_is_visible($page)==false) {
368
					if($page['visibility'] != 'registered') // special case: page_to_visible() check wheter to show the page contents, but the menu should be visible allways
369
						continue;
370
				}
371
				// Create vars
372
				$vars = array('[class]','[a]', '[/a]', '[menu_title]', '[page_title]');
373
				// Work-out class
374
				if($page['page_id'] == PAGE_ID) {
375
					$class = $this->menu_current_class;
376
				} else {
377
					$class = $this->menu_default_class;
378
				}
379
				// Check if link is same as first page link, and if so change to WB URL
380
				if($page['link'] == $this->default_link AND !INTRO_PAGE) {
381
					$link = WB_URL;
382
				} else {
383
					$link = $this->page_link($page['link']);
384
				}
385
				// Create values
386
				$values = array($class,'<a href="'.$link.'" target="'.$page['target'].'" '.$class.'>', '</a>', $page['menu_title'], $page['page_title']);
387
				// Replace vars with value and print
388
				echo "\n".str_replace($vars, $values, $this->menu_item_template);
389
				// Generate sub-menu
390
				if($this->menu_collapse==false OR ($this->menu_collapse==true AND isset($this->page_trail[$page['page_id']]))) {
391
					$this->menu_recurse--;
392
					$this->menu_parent=$page['page_id'];
393
					$this->show_menu();
394
				}
395
				echo "\n".$this->menu_item_footer;
396
			}
397
			// Print menu footer
398
			echo "\n".$this->menu_footer;
399
		}
400
	}
401

    
402

    
403
	// Function to show the "Under Construction" page
404
	function print_under_construction() {
405
		global $MESSAGE;
406
		require_once(WB_PATH.'/languages/'.DEFAULT_LANGUAGE.'.php');
407
		echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
408
		<head><title>'.$MESSAGE['GENERIC']['WEBSITE_UNDER_CONSTRUCTION'].'</title>
409
		<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; }
410
		h1 { margin: 0; padding: 0; font-size: 18px; color: #000; text-transform: uppercase;
411
}--></style></head><body>
412
		<br /><h1>'.$MESSAGE['GENERIC']['WEBSITE_UNDER_CONSTRUCTION'].'</h1><br />
413
		'.$MESSAGE['GENERIC']['PLEASE_CHECK_BACK_SOON'].'</body></html>';
414
	}
415
}
416

    
417
?>
(4-4/13)