Project

General

Profile

1
<?php
2

    
3
// $Id: class.frontend.php 310 2006-02-19 05:31:10Z ryan $
4

    
5
/*
6

    
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2006, 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
				echo ($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
				$this->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: ".$this->page_link($this->default_link));
98
					exit();
99
				} else {
100
					// Include page inline
101
					$this->page_id = $this->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
			$this->default_page_id = $fetch_default['page_id'];
116
		}
117
		return true;
118
	}
119

    
120
	function get_page_details() {
121
		global $database;
122
	    if($this->page_id != 0) {
123
			// Query page details
124
			$query_page = "SELECT * FROM ".TABLE_PREFIX."pages WHERE page_id = '{$this->page_id}'";
125
			$get_page = $database->query($query_page);
126
			// Make sure page was found in database
127
			if($get_page->numRows() == 0) {
128
				// Print page not found message
129
				exit("Page not found");
130
			}
131
			// Fetch page details
132
			$this->page = $get_page->fetchRow();
133
			// Check if the page language is also the selected language. If not, send headers again.
134
			if ($this->page['language']!=LANGUAGE) {
135
				header('Location: '.$this->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
			// Page Title
142
			define('PAGE_TITLE', ($this->page['page_title']));
143
			$this->page_title=PAGE_TITLE;
144
			// Menu Title
145
			$menu_title = ($this->page['menu_title']);
146
			if($menu_title != '') {
147
				define('MENU_TITLE', $menu_title);
148
			} else {
149
				define('MENU_TITLE', PAGE_TITLE);
150
			}
151
			$this->menu_title=MENU_TITLE;
152
			// Page parent
153
			define('PARENT', $this->page['parent']);
154
			$this->parent=$this->page['parent'];
155
			// Page root parent
156
			define('ROOT_PARENT', $this->page['root_parent']);
157
			$this->root_parent=$this->page['root_parent'];
158
			// Page level
159
			define('LEVEL', $this->page['level']);
160
			$this->level=$this->page['level'];
161
			// Page visibility
162
			define('VISIBILITY', $this->page['visibility']);
163
			$this->visibility=$this->page['visibility'];
164
			// Page trail
165
			foreach(explode(',', $this->page['page_trail']) AS $pid) {
166
				$this->page_trail[$pid]=$pid;
167
			}
168
			// Page description
169
			$this->page_description=$this->page['description'];
170
			if($this->page_description != '') {
171
				define('PAGE_DESCRIPTION', $this->page_description);
172
			} else {
173
				define('PAGE_DESCRIPTION', WEBSITE_DESCRIPTION);
174
			}
175
			// Page keywords
176
			$this->page_keywords=$this->page['keywords'];
177
			// Page link
178
			$this->link=$this->page_link($this->page['link']);
179

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

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

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

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

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

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

    
372

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

    
387
?>
(3-3/11)