Project

General

Profile

1
<?php
2

    
3
// $Id: class.frontend.php 471 2007-06-05 20:35:39Z Ruebenwurzel $
4

    
5
/*
6

    
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2007, 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
	
56
	// website settings
57
	var $website_title,$website_description,$website_keywords,$website_header,$website_footer;
58

    
59
	// ugly database stuff
60
	var $extra_where_sql, $sql_where_language;
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
				header("Location: pages/intro.php");   // send intro.php as header to allow parsing of php statements
76
				echo ($content);
77
				return false;
78
			}
79
		}
80
		// Check if we should add page language sql code
81
		if(PAGE_LANGUAGES) {
82
			$this->sql_where_language = " AND language = '".LANGUAGE."'";
83
		}
84
		// Get default page
85
		// Check for a page id
86
		$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";
87
		$get_default = $database->query($query_default);
88
		$default_num_rows = $get_default->numRows();
89
		if(!isset($page_id) OR !is_numeric($page_id)){
90
			// Go to or show default page
91
			if($default_num_rows > 0) {
92
				$fetch_default = $get_default->fetchRow();
93
				$this->default_link = $fetch_default['link'];
94
				$this->default_page_id = $fetch_default['page_id'];
95
				// Check if we should redirect or include page inline
96
				if(HOMEPAGE_REDIRECTION) {
97
					// Redirect to page
98
					header("Location: ".$this->page_link($this->default_link));
99
					exit();
100
				} else {
101
					// Include page inline
102
					$this->page_id = $this->default_page_id;
103
				}
104
			} else {
105
		   		// No pages have been added, so print under construction page
106
				$this->print_under_construction();
107
				exit();
108
			}
109
		} else {
110
			$this->page_id=$page_id;
111
		}
112
		// Get default page link
113
		if(!isset($fetch_default)) {
114
		  	$fetch_default = $get_default->fetchRow();
115
	 		$this->default_link = $fetch_default['link'];
116
			$this->default_page_id = $fetch_default['page_id'];
117
		}
118
		return true;
119
	}
120

    
121
	function get_page_details() {
122
		global $database;
123
	    if($this->page_id != 0) {
124
			// Query page details
125
			$query_page = "SELECT * FROM ".TABLE_PREFIX."pages WHERE page_id = '{$this->page_id}'";
126
			$get_page = $database->query($query_page);
127
			// Make sure page was found in database
128
			if($get_page->numRows() == 0) {
129
				// Print page not found message
130
				exit("Page not found");
131
			}
132
			// Fetch page details
133
			$this->page = $get_page->fetchRow();
134
			// Check if the page language is also the selected language. If not, send headers again.
135
			if ($this->page['language']!=LANGUAGE) {
136
				header('Location: '.$this->page_link($this->page['link']).'?lang='.$this->page['language']);
137
				exit();
138
			}
139
			// Begin code to set details as either variables of constants
140
			// Page ID
141
			define('PAGE_ID', $this->page['page_id']);
142
			// Page Title
143
			define('PAGE_TITLE', $this->page['page_title']);
144
			$this->page_title=PAGE_TITLE;
145
			// Menu Title
146
			$menu_title = $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
			if($this->page_description != '') {
172
				define('PAGE_DESCRIPTION', $this->page_description);
173
			} else {
174
				define('PAGE_DESCRIPTION', WEBSITE_DESCRIPTION);
175
			}
176
			// Page keywords
177
			$this->page_keywords=$this->page['keywords'];
178
			// Page link
179
			$this->link=$this->page_link($this->page['link']);
180

    
181
		// End code to set details as either variables of constants
182
		}
183

    
184
		// Figure out what template to use
185
		if(!defined('TEMPLATE')) {
186
			if(isset($this->page['template']) AND $this->page['template'] != '') {
187
				if(file_exists(WB_PATH.'/templates/'.$this->page['template'].'/index.php')) {
188
					define('TEMPLATE', $this->page['template']);
189
				} else {
190
					define('TEMPLATE', DEFAULT_TEMPLATE);
191
				}
192
			} else {
193
				define('TEMPLATE', DEFAULT_TEMPLATE);
194
			}
195
		}
196
		// Set the template dir
197
		define('TEMPLATE_DIR', WB_URL.'/templates/'.TEMPLATE);
198

    
199
		// Check if user is allowed to view this page
200
		if(VISIBILITY == 'private' OR VISIBILITY == 'registered') {
201
			// Check if the user is authenticated
202
			if($this->is_authenticated() == false) {
203
				// User needs to login first
204
				header("Location: ".WB_URL."/account/login".PAGE_EXTENSION.'?redirect='.$this->link);
205
				exit(0);
206
			}
207
			// Check if we should show this page
208
			if($this->show_page($this->page) == false) {
209
				$this->page_access_denied=true;
210
			}
211
		} elseif(VISIBILITY == 'deleted' OR VISIBILITY == 'none') {
212
			// User isnt allowed on this page so tell them
213
			$this->page_access_denied=true;
214
		}
215
	}
216

    
217
	function get_website_settings() {
218
		global $database;
219

    
220
		// set visibility SQL code
221
		// never show no-vis, hidden or deleted pages
222
		$this->extra_where_sql = "visibility != 'none' AND visibility != 'hidden' AND visibility != 'deleted'";
223
		// Set extra private sql code
224
		if($this->is_authenticated()==false) {
225
			// if user is not authenticated, don't show private pages either
226
			$this->extra_where_sql .= " AND visibility != 'private'";
227
			// and 'registered' without frontend login doesn't make much sense!
228
			if (FRONTEND_LOGIN==false) {
229
				$this->extra_where_sql .= " AND visibility != 'registered'";
230
			}
231
		}
232
		$this->extra_where_sql .= $this->sql_where_language;
233

    
234
		// Work-out if any possible in-line search boxes should be shown
235
		if(SEARCH == 'public') {
236
			define('SHOW_SEARCH', true);
237
		} elseif(SEARCH == 'private' AND VISIBILITY == 'private') {
238
			define('SHOW_SEARCH', true);
239
		} elseif(SEARCH == 'private' AND $this->is_authenticated() == true) {
240
			define('SHOW_SEARCH', true);
241
		} elseif(SEARCH == 'registered' AND $this->is_authenticated() == true) {
242
			define('SHOW_SEARCH', true);			
243
		} else {
244
			define('SHOW_SEARCH', false);
245
		}
246
		// Work-out if menu should be shown
247
		if(!defined('SHOW_MENU')) {
248
			define('SHOW_MENU', true);
249
		}
250
		// Work-out if login menu constants should be set
251
		if(FRONTEND_LOGIN) {
252
			// Set login menu constants
253
			define('LOGIN_URL', WB_URL.'/account/login'.PAGE_EXTENSION);
254
			define('LOGOUT_URL', WB_URL.'/account/logout'.PAGE_EXTENSION);
255
			define('FORGOT_URL', WB_URL.'/account/forgot'.PAGE_EXTENSION);
256
			define('PREFERENCES_URL', WB_URL.'/account/preferences'.PAGE_EXTENSION);
257
			define('SIGNUP_URL', WB_URL.'/account/signup'.PAGE_EXTENSION);
258
		}
259
	}
260
	
261
	function preprocess(&$content) {
262
		global $database;
263
		// Replace [wblink--PAGE_ID--] with real link
264
		$pattern = '/\[wblink(.+?)\]/s';
265
		preg_match_all($pattern,$content,$ids);
266
		foreach($ids[1] AS $page_id) {
267
			$pattern = '/\[wblink'.$page_id.'\]/s';
268
			// Get page link
269
			$get_link = $database->query("SELECT link FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id' LIMIT 1");
270
			$fetch_link = $get_link->fetchRow();
271
			$link = $this->page_link($fetch_link['link']);
272
			$content = preg_replace($pattern,$link,$content);
273
		}
274
	}
275
	
276
	function menu() {
277
		global $wb;
278
	   if (!isset($wb->menu_number)) {
279
	   	$wb->menu_number = 1;
280
	   }
281
	   if (!isset($wb->menu_start_level)) {
282
	   	$wb->menu_start_level = 0;
283
	   }
284
	   if (!isset($wb->menu_recurse)) {
285
	   	$wb->menu_recurse = -1;
286
	   }
287
	   if (!isset($wb->menu_collapse)) {
288
	   	$wb->menu_collapse = true;
289
	   }
290
	   if (!isset($wb->menu_item_template)) {
291
	   	$wb->menu_item_template = '<li><span[class]>[a] [menu_title] [/a]</span>';
292
	   }
293
	   if (!isset($wb->menu_item_footer)) {
294
	   	$wb->menu_item_footer = '</li>';
295
	   }
296
	   if (!isset($wb->menu_header)) {
297
	   	$wb->menu_header = '<ul>';
298
	   }
299
	   if (!isset($wb->menu_footer)) {
300
	   	$wb->menu_footer = '</ul>';
301
	   }
302
	   if (!isset($wb->menu_default_class)) {
303
	   	$wb->menu_default_class = ' class="menu_default"';
304
	   }
305
	   if (!isset($wb->menu_current_class)) {
306
	   	$wb->menu_current_class = ' class="menu_current"';
307
	   }
308
	   if (!isset($wb->menu_parent)) {
309
	   	$wb->menu_parent = 0;
310
	   }
311
	   $wb->show_menu();
312
	}
313
	
314
	function show_menu() {
315
		global $database;
316
		if ($this->menu_start_level>0) {
317
			$key_array=array_keys($this->page_trail);
318
			if (isset($key_array[$this->menu_start_level-1])) {
319
				$real_start=$key_array[$this->menu_start_level-1];
320
				$this->menu_parent=$real_start;
321
				$this->menu_start_level=0;
322
			} else {
323
				return;
324
			}
325
		}
326
	   if ($this->menu_recurse==0)
327
	       return;
328
	   // Check if we should add menu number check to query
329
	   if($this->menu_parent == 0) {
330
	       $menu_number = "menu = '$this->menu_number'";
331
	   } else {
332
	      $menu_number = '1';
333
	   }
334
	   // Query pages
335
	   $query_menu = $database->query("SELECT page_id,menu_title,page_title,link,target,level,visibility FROM ".
336
	TABLE_PREFIX."pages WHERE parent = '$this->menu_parent' AND $menu_number AND $this->extra_where_sql ORDER BY position ASC");
337
	   // Check if there are any pages to show
338
	   if($query_menu->numRows() > 0) {
339
	   	  // Print menu header
340
	   	  echo "\n".$this->menu_header;
341
	      // Loop through pages
342
	      while($page = $query_menu->fetchRow()) {
343
	      	 // Check if this page should be shown
344
	         // Create vars
345
	         $vars = array('[class]','[a]', '[/a]', '[menu_title]', '[page_title]');
346
	         // Work-out class
347
	         if($page['page_id'] == PAGE_ID) {
348
	            $class = $this->menu_current_class;
349
	         } else {
350
	            $class = $this->menu_default_class;
351
	         }
352
	         // Check if link is same as first page link, and if so change to WB URL
353
	         if($page['link'] == $this->default_link AND !INTRO_PAGE) {
354
	            $link = WB_URL;
355
	         } else {
356
	            $link = $this->page_link($page['link']);
357
	         }
358
	         // Create values
359
	         $values = array($class,'<a href="'.$link.'" target="'.$page['target'].'" '.$class.'>', '</a>', $page['menu_title'], $page['page_title']);
360
	         // Replace vars with value and print
361
	         echo "\n".str_replace($vars, $values, $this->menu_item_template);
362
	         // Generate sub-menu
363
	         if($this->menu_collapse==false OR ($this->menu_collapse==true AND isset($this->page_trail[$page['page_id']]))) {
364
	            $this->menu_recurse--;
365
	            $this->menu_parent=$page['page_id'];
366
	            $this->show_menu();
367
	         }
368
	         echo "\n".$this->menu_item_footer;
369
	      }
370
	      // Print menu footer
371
	      echo "\n".$this->menu_footer;
372
	   }
373
	}
374

    
375

    
376
	// Function to show the "Under Construction" page
377
	function print_under_construction() {
378
		global $MESSAGE;
379
		require_once(WB_PATH.'/languages/'.DEFAULT_LANGUAGE.'.php');
380
		echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
381
		<head><title>'.$MESSAGE['GENERIC']['WEBSITE_UNDER_CONSTRUCTION'].'</title>
382
		<style type="text/css"><!-- body { font-family: Verdana, Arial, Helvetica, sans-serif;
383
		font-size: 12px; color: #000000;	background-color: #FFFFFF;	margin: 20px; text-align: center; }
384
		h1 { margin: 0; padding: 0; }--></style></head><body>
385
		<h1>'.$MESSAGE['GENERIC']['WEBSITE_UNDER_CONSTRUCTION'].'</h1><br />
386
		'.$MESSAGE['GENERIC']['PLEASE_CHECK_BACK_SOON'].'</body></html>';
387
	}
388
}
389

    
390
?>
(3-3/12)