Project

General

Profile

1
<?php
2

    
3
/*
4

    
5
 Website Baker Project <http://www.websitebaker.org/>
6
 Copyright (C) 2004-2005, Ryan Djurovich
7

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

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

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

    
22
*/
23

    
24
/*
25

    
26
Frontend class
27

    
28
*/
29

    
30

    
31

    
32
require_once(WB_PATH.'/framework/class.wb.php');
33

    
34
class frontend extends wb {
35
	// defaults
36
	var	$default_link,$default_page_id;
37

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

    
50
	// ugly database stuff
51
	var $extra_sql,$extra_where_sql;
52

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

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

    
171
		// End code to set details as either variables of constants
172
		}
173

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

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

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

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

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

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

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

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

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

    
451
?>
(3-3/11)