Project

General

Profile

1 5 stefan
<?php
2
3 11 ryan
// $Id$
4
5 5 stefan
/*
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 18 stefan
if(!defined('WB_PATH')) {
33
	header('Location: ../index.php');
34
}
35 5 stefan
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 20 stefan
	function menu() {
326
	   if (!isset($this->menu_number)) {
327
	   	$this->menu_number = 1;
328
	   }
329
	   if (!isset($this->menu_start_level)) {
330
	   	$this->menu_start_level = 0;
331
	   }
332
	   if (!isset($this->menu_recurse)) {
333
	   	$this->menu_recurse = -1;
334
	   }
335
	   if (!isset($this->menu_collapse)) {
336
	   	$this->menu_collapse = true;
337
	   }
338
	   if (!isset($this->menu_item_template)) {
339
	   	$this->menu_item_template = '<li><span[class]>[a][menu_title][/a]</span>';
340
	   }
341
	   if (!isset($this->menu_item_footer)) {
342
	   	$this->menu_item_footer = '</li>';
343
	   }
344
	   if (!isset($this->menu_header)) {
345
	   	$this->menu_header = '<ul>';
346
	   }
347
	   if (!isset($this->menu_footer)) {
348
	   	$this->menu_footer = '<ul>';
349
	   }
350
	   if (!isset($this->menu_default_class)) {
351
	   	$this->menu_default_class = ' class="menu_default"';
352
	   }
353
	   if (!isset($this->menu_current_class)) {
354
	   	$this->menu_current_class = ' class="menu_current"';
355
	   }
356
       if (!isset($this->menu_parent)) {
357
     	$this->menu_parent = 0;
358
	   }
359
	   $this->show_menu();
360 5 stefan
	   if ($start_level>0) {
361
	       $key_array=array_keys($this->page_trail);
362
	       $real_start=$key_array[$start_level-1];
363
	       if (isset($real_start))
364
	       {
365 20 stefan
	       		$this->menu_parent=$real_start;
366
	          $this->show_menu();
367
	       }
368 5 stefan
	       return;
369
	   }
370 20 stefan
371
	}
372
373
	function show_menu() {
374
	   global $database;
375
	   if ($this->menu_recurse==0)
376
	       return;
377 5 stefan
	   // Check if we should add menu number check to query
378 20 stefan
	   if($menu_parent == 0) {
379
	       $menu_number = "menu = '$this->menu_number'";
380 5 stefan
	   } else {
381
	      $menu_number = '1';
382
	   }
383
	   // Query pages
384
	   $query_menu = $database->query("SELECT page_id,menu_title,page_title,link,target,level,visibility$this->extra_sql FROM ".
385 20 stefan
	TABLE_PREFIX."pages WHERE parent = '$this->menu_parent' AND $menu_number AND $this->extra_where_sql ORDER BY position ASC");
386 5 stefan
	   // Check if there are any pages to show
387
	   if($query_menu->numRows() > 0) {
388 20 stefan
	   	  // Print menu header
389
	   	  echo "\n".$this->menu_header;
390 5 stefan
	      // Loop through pages
391
	      while($page = $query_menu->fetchRow()) {
392 20 stefan
	      	 // Check if this page should be shown
393 5 stefan
	         // Create vars
394
	         $vars = array('[class]','[a]', '[/a]', '[menu_title]', '[page_title]');
395
	         // Work-out class
396
	         if($page['page_id'] == PAGE_ID) {
397 20 stefan
	            $class = $this->menu_current_class;
398 5 stefan
	         } else {
399 20 stefan
	            $class = $this->menu_default_class;
400 5 stefan
	         }
401
	         // Check if link is same as first page link, and if so change to WB URL
402 20 stefan
	         if($page['link'] == $this->default_link AND !INTRO_PAGE) {
403 5 stefan
	            $link = WB_URL;
404
	         } else {
405 20 stefan
	            $link = $this->page_link($page['link']);
406 5 stefan
	         }
407
	         // Create values
408
	         $values = array($class,'<a href="'.$link.'" target="'.$page['target'].'" '.$class.'>', '</a>', stripslashes($page['menu_title']), stripslashes($page['page_title']));
409
	         // Replace vars with value and print
410 20 stefan
	         echo "\n".str_replace($vars, $values, $this->menu_item_template);
411 5 stefan
	         // Generate sub-menu
412 20 stefan
	         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 5 stefan
	         }
417 20 stefan
	         echo "\n".$this->menu_item_footer;
418 5 stefan
	      }
419
	      // Print menu footer
420 20 stefan
	      echo "\n".$this->menu_footer;
421 5 stefan
	   }
422
	}
423
424
	function page_content($block = 1) {
425
		// Get outside objects
426 16 stefan
		global $database,$admin,$TEXT,$MENU,$HEADING,$MESSAGE;
427 5 stefan
		global $globals;
428
		if ($this->page_access_denied==true) {
429
            echo $MESSAGE['FRONTEND']['SORRY_NO_VIEWING_PERMISSIONS'];
430
			exit();
431
		}
432
		if(isset($globals) AND is_array($globals)) { foreach($globals AS $global_name) { global $$global_name; } }
433
		// Make sure block is numeric
434
		if(!is_numeric($block)) { $block = 1; }
435
		// Include page content
436
		if(!defined('PAGE_CONTENT')) {
437
			// First get all sections for this page
438
			$query_sections = $database->query("SELECT section_id,module FROM ".TABLE_PREFIX."sections WHERE page_id = '".PAGE_ID."' AND block = '$block' ORDER BY position");
439
			if($query_sections->numRows() > 0) {
440
				// Loop through them and include there modules file
441
				while($section = $query_sections->fetchRow()) {
442
					$section_id = $section['section_id'];
443
					$module = $section['module'];
444
					require(WB_PATH.'/modules/'.$module.'/view.php');
445
				}
446
			}
447
		} else {
448
			if($block == 1) {
449
				require(PAGE_CONTENT);
450
			}
451
		}
452
	}
453
454
	// Function for page title
455
	function page_title($spacer = ' - ', $template = '[WEBSITE_TITLE][SPACER][PAGE_TITLE]') {
456
		$vars = array('[WEBSITE_TITLE]', '[PAGE_TITLE]', '[MENU_TITLE]', '[SPACER]');
457
		$values = array(WEBSITE_TITLE, PAGE_TITLE, MENU_TITLE, $spacer);
458
		echo str_replace($vars, $values, $template);
459
	}
460
461
	// Function for page description
462
	function page_description() {
463
		echo WEBSITE_DESCRIPTION;
464
	}
465
	// Function for page keywords
466
	function page_keywords() {
467
		echo WEBSITE_KEYWORDS;
468
	}
469
	// Function for page header
470
	function page_header($date_format = 'Y') {
471
		echo WEBSITE_HEADER;
472
	}
473
474
	// Function for page footer
475
	function page_footer($date_format = 'Y') {
476
		global $starttime;
477
   		$vars = array('[YEAR]', '[PROCESSTIME]');
478
   		$processtime=(microtime()>$starttime)?microtime()-$starttime:microtime()-$starttime+1;
479
		$values = array(date($date_format),$processtime);
480
		echo str_replace($vars, $values, WEBSITE_FOOTER);
481
	}
482
483
	// Function to show the "Under Construction" page
484
	function print_under_construction() {
485
		global $MESSAGE;
486
		require_once(WB_PATH.'/languages/'.DEFAULT_LANGUAGE.'.php');
487
		echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
488
		<head><title>'.$MESSAGE['GENERIC']['WEBSITE_UNDER_CONTRUCTION'].'</title>
489
		<style type="text/css"><!-- body { font-family: Verdana, Arial, Helvetica, sans-serif;
490
		font-size: 12px; color: #000000;	background-color: #FFFFFF;	margin: 20px; text-align: center; }
491
		h1 { margin: 0; padding: 0; }--></style></head><body>
492
		<h1>'.$MESSAGE['GENERIC']['WEBSITE_UNDER_CONTRUCTION'];'.</h1><br />
493
		'.$MESSAGE['GENERIC']['PLEASE_CHECK_BACK_SOON'].'</body></html>';
494
	}
495
}
496
497
?>