Project

General

Profile

1
<?php
2

    
3
// $Id: class.frontend.php 663 2008-02-03 09:08:49Z 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
				header('Location: '.$this->page_link($this->page['link']).'?lang='.$this->page['language']);
148
				exit();
149
			}
150
			// Begin code to set details as either variables of constants
151
			// Page ID
152
			define('PAGE_ID', $this->page['page_id']);
153
			// Page Title
154
			define('PAGE_TITLE', $this->page['page_title']);
155
			$this->page_title=PAGE_TITLE;
156
			// Menu Title
157
			$menu_title = $this->page['menu_title'];
158
			if($menu_title != '') {
159
				define('MENU_TITLE', $menu_title);
160
			} else {
161
				define('MENU_TITLE', PAGE_TITLE);
162
			}
163
			$this->menu_title=MENU_TITLE;
164
			// Page parent
165
			define('PARENT', $this->page['parent']);
166
			$this->parent=$this->page['parent'];
167
			// Page root parent
168
			define('ROOT_PARENT', $this->page['root_parent']);
169
			$this->root_parent=$this->page['root_parent'];
170
			// Page level
171
			define('LEVEL', $this->page['level']);
172
			$this->level=$this->page['level'];
173
			// Page visibility
174
			define('VISIBILITY', $this->page['visibility']);
175
			$this->visibility=$this->page['visibility'];
176
			// Page trail
177
			foreach(explode(',', $this->page['page_trail']) AS $pid) {
178
				$this->page_trail[$pid]=$pid;
179
			}
180
			// Page description
181
			$this->page_description=$this->page['description'];
182
			if($this->page_description != '') {
183
				define('PAGE_DESCRIPTION', $this->page_description);
184
			} else {
185
				define('PAGE_DESCRIPTION', WEBSITE_DESCRIPTION);
186
			}
187
			// Page keywords
188
			$this->page_keywords=$this->page['keywords'];
189
			// Page link
190
			$this->link=$this->page_link($this->page['link']);
191

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

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

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

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

    
398

    
399
	// Function to show the "Under Construction" page
400
	function print_under_construction() {
401
		global $MESSAGE;
402
		require_once(WB_PATH.'/languages/'.DEFAULT_LANGUAGE.'.php');
403
		echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
404
		<head><title>'.$MESSAGE['GENERIC']['WEBSITE_UNDER_CONSTRUCTION'].'</title>
405
		<style type="text/css"><!-- body{ font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 12px; background-image: url("admin/interface/background.png");background-repeat: repeat-x; margin: 20px; text-align: center; }
406
		h1 { margin: 0; padding: 0; }--></style></head><body>
407
		<h1>'.$MESSAGE['GENERIC']['WEBSITE_UNDER_CONSTRUCTION'].'</h1><br />
408
		'.$MESSAGE['GENERIC']['PLEASE_CHECK_BACK_SOON'].'</body></html>';
409
	}
410
}
411

    
412
?>
(4-4/13)