Project

General

Profile

1
<?php
2

    
3
// $Id: class.frontend.php 18 2005-09-04 16:11:53Z stefan $
4

    
5
/*
6

    
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2005, 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
}
35

    
36

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

    
39
class frontend extends wb {
40
	// defaults
41
	var	$default_link,$default_page_id;
42

    
43
	// page details
44
	// page database row
45
	var $page;
46
	var $page_id,$page_title,$menu_title,$parent,$root_parent,$level,$visibility;
47
	var $page_description,$page_keywords,$page_link_original,$page_link;
48
	var $page_trail=array();
49
	
50
	var $page_access_denied;
51
	
52
	// website settings
53
	var $website_title,$website_description,$website_keywords,$website_header,$website_footer;
54

    
55
	// ugly database stuff
56
	var $extra_sql,$extra_where_sql;
57

    
58
	function frontend() {
59
		$this->wb();
60
	}
61
	
62
	function page_select() {
63
		global $page_id,$no_intro;
64
		global $database;
65
		// We have no page id and are supposed to show the intro page
66
		if((INTRO_PAGE AND !isset($no_intro)) AND (!isset($page_id) OR !is_numeric($page_id))) {
67
			// Since we have no page id check if we should go to intro page or default page
68
			// Get intro page content
69
			$filename = WB_PATH.PAGES_DIRECTORY.'/intro.php';
70
			if(file_exists($filename)) {
71
				$handle = fopen($filename, "r");
72
				$content = fread($handle, filesize($filename));
73
				fclose($handle);
74
				$this->preprocess($content);
75
				echo stripslashes($content);
76
				return false;
77
			}
78
		}
79
		// Check if we should add page language sql code
80
		if(PAGE_LANGUAGES) {
81
			$this->sql_where_language = " AND language = '".LANGUAGE."'";
82
		}
83
		// Get default page
84
		// Check for a page id
85
		$query_default = "SELECT page_id,link FROM ".TABLE_PREFIX."pages WHERE parent = '0' AND visibility = 'public'$this->sql_where_language ORDER BY 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
				$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: ".page_link($this->default_link));
98
					exit();
99
				} else {
100
					// Include page inline
101
					$this->page_id = $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
		}
116
		return true;
117
	}
118

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

    
176
		// End code to set details as either variables of constants
177
		}
178

    
179
		// Work-out if any possible in-line search boxes should be shown
180
		if(SEARCH == 'public') {
181
			define('SHOW_SEARCH', true);
182
		} elseif(SEARCH == 'private' AND VISIBILITY == 'private') {
183
			define('SHOW_SEARCH', true);
184
		} elseif(SEARCH == 'private' AND $wb->is_authenticated() == true) {
185
			define('SHOW_SEARCH', true);
186
		} else {
187
			define('SHOW_SEARCH', false);
188
		}
189
		// Work-out if menu should be shown
190
		if(!defined('SHOW_MENU')) {
191
			define('SHOW_MENU', true);
192
		}
193
		// Work-out if login menu constants should be set
194
		if(FRONTEND_LOGIN) {
195
			// Set login menu constants
196
			define('LOGIN_URL', WB_URL.'/account/login'.PAGE_EXTENSION);
197
			define('LOGOUT_URL', WB_URL.'/account/logout'.PAGE_EXTENSION);
198
			define('FORGOT_URL', WB_URL.'/account/forgot'.PAGE_EXTENSION);
199
			define('PREFERENCES_URL', WB_URL.'/account/preferences'.PAGE_EXTENSION);
200
			define('SIGNUP_URL', WB_URL.'/account/signup'.PAGE_EXTENSION);
201
		}
202

    
203
		// Figure out what template to use
204
		if(!defined('TEMPLATE')) {
205
			if(isset($this->page['template']) AND $this->page['template'] != '') {
206
				if(file_exists(WB_PATH.'/templates/'.$this->page['template'].'/index.php')) {
207
					define('TEMPLATE', $this->page['template']);
208
				} else {
209
					define('TEMPLATE', DEFAULT_TEMPLATE);
210
				}
211
			} else {
212
				define('TEMPLATE', DEFAULT_TEMPLATE);
213
			}
214
		}
215
		// Set the template dir
216
		define('TEMPLATE_DIR', WB_URL.'/templates/'.TEMPLATE);
217

    
218
		// Check if user is allow to view this page
219
		if(FRONTEND_LOGIN AND VISIBILITY == 'private' OR FRONTEND_LOGIN AND VISIBILITY == 'registered') {
220
			// Double-check front-end login is enabled
221
			if(FRONTEND_LOGIN != true) {
222
				// Users shouldnt be allowed to view private pages
223
				header("Location: ".WB_URL.PAGES_DIRECTORY."/index".PAGE_EXTENSION);
224
			}
225
			// Check if the user is authenticated
226
			if($this->is_authenticated() == false) {
227
				// User needs to login first
228
				header("Location: ".WB_URL."/account/login".PAGE_EXTENSION);
229
			}
230
			// Check if we should show this page
231
			if($this->show_page($this->page) == false) {
232
				$this->page_access_denied=true;
233
			}
234
			// Set extra private sql code
235
			$this->extra_sql = ",viewing_groups,viewing_users";
236
			$this->extra_where_sql = "visibility != 'none' AND visibility != 'hidden' AND visibility != 'deleted'";
237
		} elseif(!FRONTEND_LOGIN AND VISIBILITY == 'private' OR !FRONTEND_LOGIN AND VISIBILITY == 'registered') {
238
			// User isnt allowed on this page so tell them
239
			$this->page_access_denied=true;
240
		} elseif(VISIBILITY == 'deleted') {
241
			// User isnt allowed on this page so tell them
242
			$this->page_access_denied=true;
243
		}
244
		if(!isset($this->extra_sql)) {
245
			// Set extra private sql code
246
			if(FRONTEND_LOGIN == 'enabled') {
247
				if($this->is_authenticated()) {
248
					$this->extra_sql = '';
249
					$this->extra_where_sql = "visibility != 'none' AND visibility != 'hidden' AND visibility != 'deleted'";
250
				} else {
251
					$this->extra_sql = '';
252
					$this->extra_where_sql = "visibility != 'none' AND visibility != 'hidden' AND visibility != 'deleted' AND visibility != 'private'";
253
				}
254
			} else {
255
				$this->extra_sql = '';
256
				$this->extra_where_sql = "visibility != 'none' AND visibility != 'hidden' AND visibility != 'deleted' AND visibility != 'private' AND visibility != 'registered'";
257
			}
258
		}
259
		$this->extra_where_sql .= $this->sql_where_language;
260
	}
261

    
262
	function get_website_settings() {
263
		global $database;
264
		// Get website settings (title, keywords, description, header, and footer)
265
		$query_settings = "SELECT name,value FROM ".TABLE_PREFIX."settings";
266
		$get_settings = $database->query($query_settings);
267
		while($setting = $get_settings->fetchRow()) {
268
			switch($setting['name']) {
269
				case 'title':
270
					define('WEBSITE_TITLE', stripslashes($setting['value']));
271
					$this->website_title=WEBSITE_TITLE;
272
				break;
273
				case 'description':
274
					if($page_description != '') {
275
						define('WEBSITE_DESCRIPTION', $page_description);
276
					} else {
277
						define('WEBSITE_DESCRIPTION', stripslashes($setting['value']));
278
					}
279
					$this->website_description=WEBSITE_DESCRIPTION;
280
				break;
281
				case 'keywords':
282
					if($page_keywords != '') {
283
						define('WEBSITE_KEYWORDS', stripslashes($setting['value']).' '.$page_keywords);
284
					} else {
285
						define('WEBSITE_KEYWORDS', stripslashes($setting['value']));
286
					}
287
					$this->website_keywords=WEBSITE_KEYWORDS;
288
				break;
289
				case 'header':
290
					define('WEBSITE_HEADER', stripslashes($setting['value']));
291
					$this->website_header=WEBSITE_HEADER;
292
				break;
293
				case 'footer':
294
					define('WEBSITE_FOOTER', stripslashes($setting['value']));
295
					$this->website_footer=WEBSITE_FOOTER;
296
				break;
297
			}
298
		}
299
	}
300
	
301
	function page_link($link){
302
		// Check for :// in the link (used in URL's)
303
		if(strstr($link, '://') == '') {
304
			return WB_URL.PAGES_DIRECTORY.$link.PAGE_EXTENSION;
305
		} else {
306
			return $link;
307
		}
308
	}
309
	
310
	function preprocess(&$content) {
311
		global $database;
312
		// Replace [wblink--PAGE_ID--] with real link
313
		$pattern = '/\[wblink(.+?)\]/s';
314
		preg_match_all($pattern,$content,$ids);
315
		foreach($ids[1] AS $page_id) {
316
			$pattern = '/\[wblink'.$page_id.'\]/s';
317
			// Get page link
318
			$get_link = $database->query("SELECT link FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id' LIMIT 1");
319
			$fetch_link = $get_link->fetchRow();
320
			$link = page_link($fetch_link['link']);
321
			$content = preg_replace($pattern,$link,$content);
322
		}
323
	}
324
	
325
	function menu($menu_number = 1, $start_level=0, $recurse = -1, $collapse = true, $item_template = '<li><span[class]>[a][menu_title][/a]</span>', $item_footer = '</li>', $menu_header = '<ul>', $menu_footer = '</ul>', $default_class = ' class="menu_default"', $current_class = ' class="menu_current"', $parent = 0) {
326
	   global $database;
327
	   if ($recurse==0)
328
	       return;
329
	   if ($start_level>0) {
330
	       $key_array=array_keys($this->page_trail);
331
	       $real_start=$key_array[$start_level-1];
332
	       if (isset($real_start))
333
	       {
334
	          menu($menu_number, 0, $recurse,$collapse,$item_template, $item_footer, $menu_header, $menu_footer, $default_class, $current_class, $real_start);
335
	      }
336
	       return;
337
	   }
338
	   // Check if we should add menu number check to query
339
	   if($parent == 0) {
340
	       $menu_number = "menu = '$menu_number'";
341
	   } else {
342
	      $menu_number = '1';
343
	   }
344
	   // Query pages
345
	   $query_menu = $database->query("SELECT page_id,menu_title,page_title,link,target,level,visibility$this->extra_sql FROM ".
346
	TABLE_PREFIX."pages WHERE parent = '$parent' AND $menu_number AND $this->extra_where_sql ORDER BY position ASC");
347
	   // Check if there are any pages to show
348
	   if($query_menu->numRows() > 0) {
349
	      // Print menu header
350
	      echo "\n".$menu_header;
351
	      // Loop through pages
352
	      while($page = $query_menu->fetchRow()) {
353
	         // Check if this page should be shown
354
	         // Create vars
355
	         $vars = array('[class]','[a]', '[/a]', '[menu_title]', '[page_title]');
356
	         // Work-out class
357
	         if($page['page_id'] == PAGE_ID) {
358
	            $class = $current_class;
359
	         } else {
360
	            $class = $default_class;
361
	         }
362
	         // Check if link is same as first page link, and if so change to WB URL
363
	         if($page['link'] == $default_link AND !INTRO_PAGE) {
364
	            $link = WB_URL;
365
	         } else {
366
	            $link = page_link($page['link']);
367
	         }
368
	         // Create values
369
	         $values = array($class,'<a href="'.$link.'" target="'.$page['target'].'" '.$class.'>', '</a>', stripslashes($page['menu_title']), stripslashes($page['page_title']));
370
	         // Replace vars with value and print
371
	         echo "\n".str_replace($vars, $values, $item_template);
372
	         // Generate sub-menu
373
	         if($collapse==false OR ($collapse==true AND isset($this->page_trail[$page['page_id']]))) {
374
	            $this->menu($menu_number, 0, $recurse-1, $collapse, $item_template, $item_footer, $menu_header, $menu_footer, $default_class, $current_class, $page['page_id']);
375
	         }
376
	         echo "\n".$item_footer;
377
	      }
378
	      // Print menu footer
379
	      echo "\n".$menu_footer;
380
	   }
381
	}
382

    
383
	function page_content($block = 1) {
384
		// Get outside objects
385
		global $database,$admin,$TEXT,$MENU,$HEADING,$MESSAGE;
386
		global $globals;
387
		if ($this->page_access_denied==true) {
388
            echo $MESSAGE['FRONTEND']['SORRY_NO_VIEWING_PERMISSIONS'];
389
			exit();
390
		}
391
		if(isset($globals) AND is_array($globals)) { foreach($globals AS $global_name) { global $$global_name; } }
392
		// Make sure block is numeric
393
		if(!is_numeric($block)) { $block = 1; }
394
		// Include page content
395
		if(!defined('PAGE_CONTENT')) {
396
			// First get all sections for this page
397
			$query_sections = $database->query("SELECT section_id,module FROM ".TABLE_PREFIX."sections WHERE page_id = '".PAGE_ID."' AND block = '$block' ORDER BY position");
398
			if($query_sections->numRows() > 0) {
399
				// Loop through them and include there modules file
400
				while($section = $query_sections->fetchRow()) {
401
					$section_id = $section['section_id'];
402
					$module = $section['module'];
403
					require(WB_PATH.'/modules/'.$module.'/view.php');
404
				}
405
			}
406
		} else {
407
			if($block == 1) {
408
				require(PAGE_CONTENT);
409
			}
410
		}
411
	}
412

    
413
	// Function for page title
414
	function page_title($spacer = ' - ', $template = '[WEBSITE_TITLE][SPACER][PAGE_TITLE]') {
415
		$vars = array('[WEBSITE_TITLE]', '[PAGE_TITLE]', '[MENU_TITLE]', '[SPACER]');
416
		$values = array(WEBSITE_TITLE, PAGE_TITLE, MENU_TITLE, $spacer);
417
		echo str_replace($vars, $values, $template);
418
	}
419

    
420
	// Function for page description
421
	function page_description() {
422
		echo WEBSITE_DESCRIPTION;
423
	}
424
	// Function for page keywords
425
	function page_keywords() {
426
		echo WEBSITE_KEYWORDS;
427
	}
428
	// Function for page header
429
	function page_header($date_format = 'Y') {
430
		echo WEBSITE_HEADER;
431
	}
432

    
433
	// Function for page footer
434
	function page_footer($date_format = 'Y') {
435
		global $starttime;
436
   		$vars = array('[YEAR]', '[PROCESSTIME]');
437
   		$processtime=(microtime()>$starttime)?microtime()-$starttime:microtime()-$starttime+1;
438
		$values = array(date($date_format),$processtime);
439
		echo str_replace($vars, $values, WEBSITE_FOOTER);
440
	}
441

    
442
	// Function to show the "Under Construction" page
443
	function print_under_construction() {
444
		global $MESSAGE;
445
		require_once(WB_PATH.'/languages/'.DEFAULT_LANGUAGE.'.php');
446
		echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
447
		<head><title>'.$MESSAGE['GENERIC']['WEBSITE_UNDER_CONTRUCTION'].'</title>
448
		<style type="text/css"><!-- body { font-family: Verdana, Arial, Helvetica, sans-serif;
449
		font-size: 12px; color: #000000;	background-color: #FFFFFF;	margin: 20px; text-align: center; }
450
		h1 { margin: 0; padding: 0; }--></style></head><body>
451
		<h1>'.$MESSAGE['GENERIC']['WEBSITE_UNDER_CONTRUCTION'];'.</h1><br />
452
		'.$MESSAGE['GENERIC']['PLEASE_CHECK_BACK_SOON'].'</body></html>';
453
	}
454
}
455

    
456
?>
(3-3/11)