Project

General

Profile

1
<?php
2

    
3
// $Id: class.frontend.php 11 2005-09-04 09:20:29Z ryan $
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

    
33

    
34
require_once(WB_PATH.'/framework/class.wb.php');
35

    
36
class frontend extends wb {
37
	// defaults
38
	var	$default_link,$default_page_id;
39

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

    
52
	// ugly database stuff
53
	var $extra_sql,$extra_where_sql;
54

    
55
	function frontend() {
56
		$this->wb();
57
	}
58
	
59
	function page_select() {
60
		global $page_id,$no_intro;
61
		global $database;
62
		// We have no page id and are supposed to show the intro page
63
		if((INTRO_PAGE AND !isset($no_intro)) AND (!isset($page_id) OR !is_numeric($page_id))) {
64
			// Since we have no page id check if we should go to intro page or default page
65
			// Get intro page content
66
			$filename = WB_PATH.PAGES_DIRECTORY.'/intro.php';
67
			if(file_exists($filename)) {
68
				$handle = fopen($filename, "r");
69
				$content = fread($handle, filesize($filename));
70
				fclose($handle);
71
				$this->preprocess($content);
72
				echo stripslashes($content);
73
				return false;
74
			}
75
		}
76
		// Check if we should add page language sql code
77
		if(PAGE_LANGUAGES) {
78
			$this->sql_where_language = " AND language = '".LANGUAGE."'";
79
		}
80
		// Get default page
81
		// Check for a page id
82
		$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";
83
		$get_default = $database->query($query_default);
84
		$default_num_rows = $get_default->numRows();
85
		if(!isset($page_id) OR !is_numeric($page_id)){
86
			// Go to or show default page
87
			if($default_num_rows > 0) {
88
				$fetch_default = $get_default->fetchRow();
89
				$this->default_link = $fetch_default['link'];
90
				$default_page_id = $fetch_default['page_id'];
91
				// Check if we should redirect or include page inline
92
				if(HOMEPAGE_REDIRECTION) {
93
					// Redirect to page
94
					header("Location: ".page_link($this->default_link));
95
					exit();
96
				} else {
97
					// Include page inline
98
					$this->page_id = $default_page_id;
99
				}
100
			} else {
101
		   		// No pages have been added, so print under construction page
102
				$this->print_under_construction();
103
				exit();
104
			}
105
		} else {
106
			$this->page_id=$page_id;
107
		}
108
		// Get default page link
109
		if(!isset($fetch_default)) {
110
		  	$fetch_default = $get_default->fetchRow();
111
	 		$this->default_link = $fetch_default['link'];
112
		}
113
		return true;
114
	}
115

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

    
173
		// End code to set details as either variables of constants
174
		}
175

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

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

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

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

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

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

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

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

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

    
453
?>
(3-3/11)